Browse Source

blade width setting

master
Stephen 1 month ago
parent
commit
18e4d80437
4 changed files with 65 additions and 4 deletions
  1. +0
    -1
      Cargo.toml
  2. +4
    -0
      public/style.css
  3. +1
    -1
      src/calculator.rs
  4. +60
    -2
      src/lib.rs

+ 0
- 1
Cargo.toml View File

@ -13,7 +13,6 @@ codegen-units = 1
opt-level = 'z'
lto = true
[dependencies]
yew = "0.17"
wasm-bindgen = "0.2.67"


+ 4
- 0
public/style.css View File

@ -21,6 +21,10 @@ input[type=number] {
margin-right: 8px;
}
.bottom-margin {
margin-bottom: 8px;
}
.header {
width: 100%;
margin-bottom: 8px;


+ 1
- 1
src/calculator.rs View File

@ -74,7 +74,7 @@ pub fn find_best_cuts<'a>(
.map(|x| x.remaining)
.fold(f64::NAN, f64::max);
for r in &required {
if r.length > avail_max {
if r.length + blade_width > avail_max {
return Err("Error: One of your required pieces is larger than all available pieces.");
}
}


+ 60
- 2
src/lib.rs View File

@ -80,6 +80,9 @@ enum Msg {
ChangeRequiredQuantity(usize, String),
DeleteRequired(usize),
ChangeBladeWidth(String),
ForceUpdate,
Calculate,
}
@ -89,6 +92,32 @@ struct Model {
required: Vec<RequiredRow>,
total_cost: u32,
output: Vec<CutRow>,
metric: bool,
blade_width: f64, // mm
}
impl Model {
// convert into metric/imperial depending on user's settings
fn get_blade_width_string(&self) -> String {
format!("{:.3}", if self.metric {
self.blade_width
}
else {
self.blade_width / 25.4
})
}
fn set_blade_width(&mut self, w: f64) {
if self.metric {
self.blade_width = w;
} else {
self.blade_width = w * 25.4;
}
}
fn blade_width_units(&self) -> &'static str {
if self.metric { "mm" } else { "\"" }
}
}
impl Component for Model {
@ -101,10 +130,13 @@ impl Component for Model {
required: vec![],
total_cost: 0,
output: vec![],
metric: false,
blade_width: 0.0,
}
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
let mut change = true;
match msg {
Msg::AddAvailable => self.available.push(AvailableRow::new()),
@ -146,6 +178,16 @@ impl Component for Model {
self.required[id].deleted = true;
}
Msg::ChangeBladeWidth(s) => {
if let Ok(x) = s.parse() {
self.set_blade_width(x);
}
// kind of hacky. Good enough for now
change = false;
}
Msg::ForceUpdate => {}
Msg::Calculate => {
// convert available/required wood to the expected types
let available_names: Vec<String> =
@ -167,7 +209,7 @@ impl Component for Model {
}
// Do the calculations
match find_best_cuts(available, required, 0.0) {
match find_best_cuts(available, required, self.blade_width) {
Ok(output) => {
self.total_cost = output.iter().map(|x| x.cost).fold(0, |acc, x| acc + x);
self.output = output;
@ -181,7 +223,7 @@ impl Component for Model {
}
}
true
change
}
fn change(&mut self, _props: Self::Properties) -> ShouldRender {
@ -280,6 +322,22 @@ impl Component for Model {
}
</div>
<div class="col-sm">
<div>
<h4>{ "General Settings" }</h4>
<table>
<tr>
<td>
{ "Sawblade Width" }
</td>
<td>
<input type="number" min="0" step="0.125" value={self.get_blade_width_string()}
oninput=self.link.callback(|e: InputData| Msg::ChangeBladeWidth(e.value))
/>
{ self.blade_width_units() }
</td>
</tr>
</table>
</div>
<button
class="btn btn-primary"
onclick=self.link.callback(|_| Msg::Calculate)>{ "Calculate" }</button>


Loading…
Cancel
Save