This commit is contained in:
Crispy 2024-12-16 22:43:12 +01:00
parent ba1f404250
commit 4427b4c2fc
5 changed files with 36 additions and 38 deletions

View file

@ -196,8 +196,8 @@ impl Editor {
let action = &self.undo_history[self.undo_index]; let action = &self.undo_history[self.undo_index];
match action { match action {
Action::SetArea(deltas, pos, old, _new) => { Action::SetArea(deltas, pos, old, _new) => {
self.source_board.paste_board(*pos, &old); self.source_board.paste_board(*pos, old);
self.source_board.shrink(&deltas); self.source_board.shrink(deltas);
self.shift_world(-(deltas.x_neg as f32), -(deltas.y_neg as f32)); self.shift_world(-(deltas.x_neg as f32), -(deltas.y_neg as f32));
} }
} }
@ -356,7 +356,7 @@ impl Editor {
let id = get_free_id(&self.blueprints, Blueprint::id); let id = get_free_id(&self.blueprints, Blueprint::id);
let mut blueprint = Blueprint::new(&board, id); let mut blueprint = Blueprint::new(&board, id);
if !self.new_blueprint_name.is_empty() { if !self.new_blueprint_name.is_empty() {
blueprint.name = self.new_blueprint_name.clone(); blueprint.name.clone_from(&self.new_blueprint_name);
} }
blueprint.save(); blueprint.save();
self.blueprints.push(blueprint); self.blueprints.push(blueprint);
@ -944,7 +944,7 @@ impl Editor {
{ {
format!("{:?}", expected_byte as char) format!("{:?}", expected_byte as char)
} else { } else {
format!("{}", expected_byte) format!("{expected_byte}")
}; };
d.draw_text(&top_text, x + 2, y + 5, 20, Color::WHITE); d.draw_text(&top_text, x + 2, y + 5, 20, Color::WHITE);
} }
@ -954,7 +954,7 @@ impl Editor {
{ {
format!("{:?}", real_byte as char) format!("{:?}", real_byte as char)
} else { } else {
format!("{}", real_byte) format!("{real_byte}")
}; };
d.draw_text(&bottom_text, x + 2, y + 40, 20, Color::WHITE); d.draw_text(&bottom_text, x + 2, y + 40, 20, Color::WHITE);
} }
@ -1057,11 +1057,10 @@ impl Editor {
if d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT) { if d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT) {
let pos = tile_pos.into(); let pos = tile_pos.into();
match self.active_tool { match self.active_tool {
Tool::None => (), Tool::None | Tool::Erase | Tool::SelectArea(_) => (),
Tool::Erase => (),
Tool::SetTile(tile) => self.set_tile(pos, tile), Tool::SetTile(tile) => self.set_tile(pos, tile),
Tool::Math => { Tool::Math => {
self.set_tile(pos, Tile::Powerable(PTile::Math(self.tool_math), false)) self.set_tile(pos, Tile::Powerable(PTile::Math(self.tool_math), false));
} }
Tool::Comparator => self.set_tile( Tool::Comparator => self.set_tile(
pos, pos,
@ -1086,13 +1085,12 @@ impl Editor {
} }
} }
} }
Tool::SelectArea(_) => (),
} }
} }
if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT) if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT)
&& self.active_tool == Tool::Erase && self.active_tool == Tool::Erase
{ {
self.set_tile(tile_pos.into(), Tile::BLANK) self.set_tile(tile_pos.into(), Tile::BLANK);
} }
if let Tool::SelectArea(selection) = &mut self.active_tool { if let Tool::SelectArea(selection) = &mut self.active_tool {
if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT) { if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT) {

View file

@ -302,7 +302,7 @@ fn get_solutions() -> HashMap<String, Vec<Solution>> {
.as_deref() .as_deref()
.and_then(|s| serde_json::from_str(s).ok()); .and_then(|s| serde_json::from_str(s).ok());
if let Some(solution) = s { if let Some(solution) = s {
solutions.push(solution) solutions.push(solution);
} }
} }
solutions.sort_unstable_by_key(Solution::id); solutions.sort_unstable_by_key(Solution::id);

View file

@ -45,7 +45,7 @@ impl Board {
let mut out = String::new(); let mut out = String::new();
for row in &self.rows { for row in &self.rows {
for tile in row { for tile in row {
out.push(tile.to_char()) out.push(tile.to_char());
} }
out.push('\n'); out.push('\n');
} }
@ -153,7 +153,7 @@ impl Board {
// top // top
{ {
let mut n = 0; let mut n = 0;
while n < self.height && self.rows[n].iter().all(Tile::is_blank) { while n < self.height && self.rows[n].iter().all(|t| t.is_blank()) {
n += 1; n += 1;
} }
let trim_top = n.saturating_sub(margin); let trim_top = n.saturating_sub(margin);
@ -166,7 +166,7 @@ impl Board {
// bottom // bottom
{ {
let mut n = 0; let mut n = 0;
while n < self.height && self.rows[self.height - n - 1].iter().all(Tile::is_blank) { while n < self.height && self.rows[self.height - n - 1].iter().all(|t| t.is_blank()) {
n += 1; n += 1;
} }
let trim_bottom = n.saturating_sub(margin); let trim_bottom = n.saturating_sub(margin);

View file

@ -155,19 +155,19 @@ impl Tile {
} }
} }
pub fn is_blank(&self) -> bool { pub fn is_blank(self) -> bool {
matches!(self, Tile::Open(OpenTile::Blank, _)) matches!(self, Tile::Open(OpenTile::Blank, _))
} }
pub fn read_value(&self) -> MarbleValue { pub fn read_value(self) -> MarbleValue {
match self { match self {
Tile::Marble { value, dir: _ } => *value, Tile::Marble { value, dir: _ } => value,
Tile::Open(OpenTile::Digit(d), _) => *d as MarbleValue, Tile::Open(OpenTile::Digit(d), _) => d as MarbleValue,
_ => 0, _ => 0,
} }
} }
pub fn texture(&self) -> &str { pub fn texture(self) -> &'static str {
match self { match self {
Tile::Open(OpenTile::Blank, _) => "", Tile::Open(OpenTile::Blank, _) => "",
Tile::Block => "block", Tile::Block => "block",
@ -188,19 +188,19 @@ impl Tile {
Tile::Mirror(mirror) => mirror.texture_name(), Tile::Mirror(mirror) => mirror.texture_name(),
Tile::Arrow(dir) => dir.arrow_tile_texture_name(), Tile::Arrow(dir) => dir.arrow_tile_texture_name(),
Tile::Button(state) => { Tile::Button(state) => {
if *state { if state {
return "button_on"; return "button_on";
} }
"button_off" "button_off"
} }
Tile::Wire(wire, state) => { Tile::Wire(wire, state) => {
if *state { if state {
return wire.texture_name_on(); return wire.texture_name_on();
} }
wire.texture_name_off() wire.texture_name_off()
} }
Tile::Powerable(tile, state) => { Tile::Powerable(tile, state) => {
if *state { if state {
return match tile { return match tile {
PTile::Comparator(comp) => comp.texture_name_on(), PTile::Comparator(comp) => comp.texture_name_on(),
PTile::Math(math_op) => math_op.texture_name_on(), PTile::Math(math_op) => math_op.texture_name_on(),
@ -209,13 +209,13 @@ impl Tile {
PTile::IO => "io_tile_on", PTile::IO => "io_tile_on",
}; };
} }
return match tile { match tile {
PTile::Comparator(comp) => comp.texture_name_off(), PTile::Comparator(comp) => comp.texture_name_off(),
PTile::Math(math_op) => math_op.texture_name_off(), PTile::Math(math_op) => math_op.texture_name_off(),
PTile::Silo => "silo_off", PTile::Silo => "silo_off",
PTile::Flipper => "flipper_off", PTile::Flipper => "flipper_off",
PTile::IO => "io_tile_off", PTile::IO => "io_tile_off",
}; }
} }
} }
} }
@ -229,7 +229,7 @@ impl Direction {
Direction::Right, Direction::Right,
]; ];
pub fn opposite(&self) -> Direction { pub fn opposite(self) -> Direction {
match self { match self {
Direction::Up => Direction::Down, Direction::Up => Direction::Down,
Direction::Down => Direction::Up, Direction::Down => Direction::Up,
@ -238,7 +238,7 @@ impl Direction {
} }
} }
pub fn right(&self) -> Direction { pub fn right(self) -> Direction {
match self { match self {
Direction::Up => Direction::Right, Direction::Up => Direction::Right,
Direction::Down => Direction::Left, Direction::Down => Direction::Left,
@ -247,11 +247,11 @@ impl Direction {
} }
} }
pub fn left(&self) -> Direction { pub fn left(self) -> Direction {
self.right().opposite() self.right().opposite()
} }
pub fn step(&self, mut pos: Pos) -> Pos { pub fn step(self, mut pos: Pos) -> Pos {
match self { match self {
Direction::Up => pos.y -= 1, Direction::Up => pos.y -= 1,
Direction::Down => pos.y += 1, Direction::Down => pos.y += 1,
@ -261,7 +261,7 @@ impl Direction {
pos pos
} }
pub const fn arrow_tile_texture_name(&self) -> &'static str { pub const fn arrow_tile_texture_name(self) -> &'static str {
match self { match self {
Direction::Up => "arrow_up", Direction::Up => "arrow_up",
Direction::Down => "arrow_down", Direction::Down => "arrow_down",
@ -270,7 +270,7 @@ impl Direction {
} }
} }
pub const fn arrow_texture_name(&self) -> &'static str { pub const fn arrow_texture_name(self) -> &'static str {
match self { match self {
Direction::Up => "direction_up", Direction::Up => "direction_up",
Direction::Down => "direction_down", Direction::Down => "direction_down",
@ -313,14 +313,14 @@ impl WireType {
} }
} }
pub const fn texture_name_on(&self) -> &'static str { pub const fn texture_name_on(self) -> &'static str {
match self { match self {
WireType::Vertical => "wire_vertical_on", WireType::Vertical => "wire_vertical_on",
WireType::Horizontal => "wire_horizontal_on", WireType::Horizontal => "wire_horizontal_on",
WireType::Cross => "wire_cross_on", WireType::Cross => "wire_cross_on",
} }
} }
pub const fn texture_name_off(&self) -> &'static str { pub const fn texture_name_off(self) -> &'static str {
match self { match self {
WireType::Vertical => "wire_vertical_off", WireType::Vertical => "wire_vertical_off",
WireType::Horizontal => "wire_horizontal_off", WireType::Horizontal => "wire_horizontal_off",
@ -354,7 +354,7 @@ impl MirrorType {
} }
} }
pub const fn texture_name(&self) -> &'static str { pub const fn texture_name(self) -> &'static str {
match self { match self {
MirrorType::Forward => "mirror_forward", MirrorType::Forward => "mirror_forward",
MirrorType::Back => "mirror_back", MirrorType::Back => "mirror_back",
@ -363,7 +363,7 @@ impl MirrorType {
} }
impl MathOp { impl MathOp {
pub const fn texture_name_on(&self) -> &'static str { pub const fn texture_name_on(self) -> &'static str {
match self { match self {
MathOp::Add => "add_on", MathOp::Add => "add_on",
MathOp::Sub => "sub_on", MathOp::Sub => "sub_on",
@ -372,7 +372,7 @@ impl MathOp {
MathOp::Rem => "rem_on", MathOp::Rem => "rem_on",
} }
} }
pub const fn texture_name_off(&self) -> &'static str { pub const fn texture_name_off(self) -> &'static str {
match self { match self {
MathOp::Add => "add_off", MathOp::Add => "add_off",
MathOp::Sub => "sub_off", MathOp::Sub => "sub_off",
@ -404,7 +404,7 @@ impl MathOp {
} }
impl Comparison { impl Comparison {
pub const fn texture_name_on(&self) -> &'static str { pub const fn texture_name_on(self) -> &'static str {
match self { match self {
Comparison::LessThan => "lt_on", Comparison::LessThan => "lt_on",
Comparison::GreaterThan => "gt_on", Comparison::GreaterThan => "gt_on",
@ -412,7 +412,7 @@ impl Comparison {
Comparison::NotEqual => "neq_on", Comparison::NotEqual => "neq_on",
} }
} }
pub const fn texture_name_off(&self) -> &'static str { pub const fn texture_name_off(self) -> &'static str {
match self { match self {
Comparison::LessThan => "lt_off", Comparison::LessThan => "lt_off",
Comparison::GreaterThan => "gt_off", Comparison::GreaterThan => "gt_off",

View file

@ -278,7 +278,7 @@ pub fn slider(
let percent = (mouse_pos.x - bounds.x) / bounds.width; let percent = (mouse_pos.x - bounds.x) / bounds.width;
let new_value = min + (percent * (max - min + 1) as f32) as u8; let new_value = min + (percent * (max - min + 1) as f32) as u8;
if *value != new_value { if *value != new_value {
*value = new_value *value = new_value;
} }
} else if d.get_mouse_wheel_move() > 0.5 && *value < max { } else if d.get_mouse_wheel_move() > 0.5 && *value < max {
*value += 1; *value += 1;