stop cloning strings for every tile texture name lookup, general clippy fixes

This commit is contained in:
Crispy 2024-12-15 00:38:53 +01:00
parent 499aad7898
commit 6d8bfa03b0
3 changed files with 108 additions and 67 deletions

View file

@ -170,7 +170,7 @@ impl Editor {
} }
fn pos_to_screen(&self, pos: Vector2) -> Vector2 { fn pos_to_screen(&self, pos: Vector2) -> Vector2 {
pos * (TILE_TEXTURE_SIZE * self.zoom) as f32 + self.view_offset pos * TILE_TEXTURE_SIZE * self.zoom + self.view_offset
} }
fn start_sim(&mut self) { fn start_sim(&mut self) {
@ -238,7 +238,7 @@ impl Editor {
} }
pub fn center_view(&mut self, d: &RaylibHandle) { pub fn center_view(&mut self, d: &RaylibHandle) {
let tile_size = (TILE_TEXTURE_SIZE * self.zoom) as f32; let tile_size = TILE_TEXTURE_SIZE * self.zoom;
let tile_x = self.source_board.width() as f32 / 2. * tile_size; let tile_x = self.source_board.width() as f32 / 2. * tile_size;
let tile_y = self.source_board.height() as f32 / 2. * tile_size; let tile_y = self.source_board.height() as f32 / 2. * tile_size;
let screen_x = d.get_screen_width() as f32 / 2.; let screen_x = d.get_screen_width() as f32 / 2.;
@ -248,11 +248,11 @@ impl Editor {
} }
fn change_zoom_level(&mut self, d: &RaylibHandle, delta: f32) { fn change_zoom_level(&mut self, d: &RaylibHandle, delta: f32) {
let tile_size = (TILE_TEXTURE_SIZE * self.zoom) as f32; let tile_size = TILE_TEXTURE_SIZE * self.zoom;
let mouse_pos = d.get_mouse_position(); let mouse_pos = d.get_mouse_position();
let tile_pos_of_mouse = (mouse_pos - self.view_offset) / tile_size; let tile_pos_of_mouse = (mouse_pos - self.view_offset) / tile_size;
self.zoom += delta; self.zoom += delta;
let tile_size = (TILE_TEXTURE_SIZE * self.zoom) as f32; let tile_size = TILE_TEXTURE_SIZE * self.zoom;
self.view_offset = mouse_pos - tile_pos_of_mouse * tile_size; self.view_offset = mouse_pos - tile_pos_of_mouse * tile_size;
self.view_offset.x = self.view_offset.x.floor(); self.view_offset.x = self.view_offset.x.floor();
self.view_offset.y = self.view_offset.y.floor(); self.view_offset.y = self.view_offset.y.floor();
@ -292,7 +292,7 @@ impl Editor {
} }
fn grow_board_and_update_view(&mut self, pos: &mut Pos) { fn grow_board_and_update_view(&mut self, pos: &mut Pos) {
let tile_size = (TILE_TEXTURE_SIZE * self.zoom) as f32; let tile_size = TILE_TEXTURE_SIZE * self.zoom;
let (x, y) = self.source_board.grow_to_include(*pos); let (x, y) = self.source_board.grow_to_include(*pos);
if x != 0 || y != 0 { if x != 0 || y != 0 {
self.view_offset.x -= x as f32 * tile_size; self.view_offset.x -= x as f32 * tile_size;
@ -319,7 +319,7 @@ impl Editor {
} }
fn set_tile(&mut self, mut pos: Pos, tile: Tile) { fn set_tile(&mut self, mut pos: Pos, tile: Tile) {
let tile_size = (TILE_TEXTURE_SIZE * self.zoom) as f32; let tile_size = TILE_TEXTURE_SIZE * self.zoom;
pos.x -= BOARD_MARGIN; pos.x -= BOARD_MARGIN;
pos.y -= BOARD_MARGIN; pos.y -= BOARD_MARGIN;
self.grow_board_and_update_view(&mut pos); self.grow_board_and_update_view(&mut pos);
@ -792,7 +792,7 @@ impl Editor {
tool_button((1, 0), "marble", Tool::SetTile(Tile::from_char('o'))); tool_button((1, 0), "marble", Tool::SetTile(Tile::from_char('o')));
match tool_button( match tool_button(
(1, 1), (1, 1),
&Tile::Wire(self.tool_wire, false).texture(), Tile::Wire(self.tool_wire, false).texture(),
Tool::Wire, Tool::Wire,
) { ) {
Some(Scroll::Down) => self.tool_wire.next(), Some(Scroll::Down) => self.tool_wire.next(),
@ -800,14 +800,14 @@ impl Editor {
None => (), None => (),
} }
match tool_button((1, 2), &Tile::Arrow(self.tool_arrow).texture(), Tool::Arrow) { match tool_button((1, 2), Tile::Arrow(self.tool_arrow).texture(), Tool::Arrow) {
Some(Scroll::Down) => self.tool_arrow = self.tool_arrow.right(), Some(Scroll::Down) => self.tool_arrow = self.tool_arrow.right(),
Some(Scroll::Up) => self.tool_arrow = self.tool_arrow.left(), Some(Scroll::Up) => self.tool_arrow = self.tool_arrow.left(),
None => (), None => (),
} }
if tool_button( if tool_button(
(1, 3), (1, 3),
&Tile::Mirror(self.tool_mirror).texture(), Tile::Mirror(self.tool_mirror).texture(),
Tool::Mirror, Tool::Mirror,
) )
.is_some() .is_some()
@ -816,7 +816,7 @@ impl Editor {
} }
match tool_button( match tool_button(
(1, 4), (1, 4),
&Tile::Powerable(PTile::Math(self.tool_math), false).texture(), Tile::Powerable(PTile::Math(self.tool_math), false).texture(),
Tool::Math, Tool::Math,
) { ) {
Some(Scroll::Down) => self.tool_math.next(), Some(Scroll::Down) => self.tool_math.next(),
@ -825,7 +825,7 @@ impl Editor {
} }
match tool_button( match tool_button(
(1, 5), (1, 5),
&Tile::Powerable(PTile::Comparator(self.tool_comparator), false).texture(), Tile::Powerable(PTile::Comparator(self.tool_comparator), false).texture(),
Tool::Comparator, Tool::Comparator,
) { ) {
Some(Scroll::Down) => self.tool_comparator.next(), Some(Scroll::Down) => self.tool_comparator.next(),
@ -906,16 +906,16 @@ impl Editor {
} }
if mouse_pos.y < footer_top && mouse_pos.y > HEADER_HEIGHT as f32 { if mouse_pos.y < footer_top && mouse_pos.y > HEADER_HEIGHT as f32 {
let view_offset = Vector2::new( let view_offset = Vector2::new(
self.view_offset.x.rem(tile_size as f32), self.view_offset.x.rem(tile_size),
self.view_offset.y.rem(tile_size as f32), self.view_offset.y.rem(tile_size),
); );
let mut offset = mouse_pos - view_offset; let mut offset = mouse_pos - view_offset;
offset.x -= offset.x.rem(tile_size as f32); offset.x -= offset.x.rem(tile_size);
offset.y -= offset.y.rem(tile_size as f32); offset.y -= offset.y.rem(tile_size);
offset += view_offset; offset += view_offset;
board.draw(d, textures, offset, self.zoom); board.draw(d, textures, offset, self.zoom);
if d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT) { if d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT) {
let tile_pos = (mouse_pos - self.view_offset) / tile_size as f32; let tile_pos = (mouse_pos - self.view_offset) / tile_size;
let tile_pos = Vector2::new(tile_pos.x.floor(), tile_pos.y.floor()); let tile_pos = Vector2::new(tile_pos.x.floor(), tile_pos.y.floor());
let mut pos = tile_pos.into(); let mut pos = tile_pos.into();
@ -930,7 +930,7 @@ impl Editor {
return; return;
} }
if let Tool::Digits(Some(pos)) = &mut self.active_tool { if let Tool::Digits(Some(pos)) = &mut self.active_tool {
let tile_screen_pos = pos.to_vec() * tile_size as f32 + self.view_offset; let tile_screen_pos = pos.to_vec() * tile_size + self.view_offset;
d.draw_texture_ex( d.draw_texture_ex(
textures.get("selection"), textures.get("selection"),
tile_screen_pos, tile_screen_pos,
@ -958,7 +958,7 @@ impl Editor {
} }
} }
if mouse_pos.y < footer_top && mouse_pos.y > HEADER_HEIGHT as f32 { if mouse_pos.y < footer_top && mouse_pos.y > HEADER_HEIGHT as f32 {
let tile_pos = (mouse_pos - self.view_offset) / tile_size as f32; let tile_pos = (mouse_pos - self.view_offset) / tile_size;
let tile_pos = Vector2::new(tile_pos.x.floor(), tile_pos.y.floor()); let tile_pos = Vector2::new(tile_pos.x.floor(), tile_pos.y.floor());
let tile_screen_pos = self.pos_to_screen(tile_pos); let tile_screen_pos = self.pos_to_screen(tile_pos);
@ -966,26 +966,26 @@ impl Editor {
if self.active_tool != Tool::None { if self.active_tool != Tool::None {
let tex = match &self.active_tool { let tex = match &self.active_tool {
Tool::None => unreachable!(), Tool::None => unreachable!(),
Tool::Erase => "cancel".into(), Tool::Erase => "cancel",
Tool::SetTile(t) => t.texture(), Tool::SetTile(t) => t.texture(),
Tool::Math => format!("{}_off", self.tool_math.texture_name()), Tool::Math => self.tool_math.texture_name_off(),
Tool::Comparator => format!("{}_off", self.tool_comparator.texture_name()), Tool::Comparator => self.tool_comparator.texture_name_off(),
Tool::Wire => format!("{}_off", self.tool_wire.texture_name()), Tool::Wire => self.tool_wire.texture_name_off(),
Tool::Arrow => self.tool_arrow.arrow_tile_texture_name().into(), Tool::Arrow => self.tool_arrow.arrow_tile_texture_name(),
Tool::Mirror => self.tool_mirror.texture_name().into(), Tool::Mirror => self.tool_mirror.texture_name(),
Tool::Digits(_) => "selection".into(), Tool::Digits(_) => "selection",
Tool::SelectArea(selection) => { Tool::SelectArea(selection) => {
if selection.is_selecting { if selection.is_selecting {
"transparent".into() "transparent"
} else { } else {
"area_full".into() "area_full"
} }
} }
Tool::Blueprint => "transparent".into(), Tool::Blueprint => "transparent",
}; };
d.draw_texture_ex( d.draw_texture_ex(
textures.get(&tex), textures.get(tex),
tile_screen_pos, tile_screen_pos,
0., 0.,
self.zoom, self.zoom,
@ -1056,12 +1056,12 @@ impl Editor {
if let Tool::Blueprint = self.active_tool { if let Tool::Blueprint = self.active_tool {
if let Some(bp) = self.blueprints.get_mut(self.selected_blueprint) { if let Some(bp) = self.blueprints.get_mut(self.selected_blueprint) {
let view_offset = Vector2::new( let view_offset = Vector2::new(
self.view_offset.x.rem(tile_size as f32), self.view_offset.x.rem(tile_size),
self.view_offset.y.rem(tile_size as f32), self.view_offset.y.rem(tile_size),
); );
let mut offset = mouse_pos - view_offset; let mut offset = mouse_pos - view_offset;
offset.x -= offset.x.rem(tile_size as f32); offset.x -= offset.x.rem(tile_size);
offset.y -= offset.y.rem(tile_size as f32); offset.y -= offset.y.rem(tile_size);
offset += view_offset; offset += view_offset;
bp.convert_board().draw(d, textures, offset, self.zoom); bp.convert_board().draw(d, textures, offset, self.zoom);
} }

View file

@ -263,7 +263,7 @@ impl Board {
if texname.is_empty() { if texname.is_empty() {
continue; continue;
} }
let texture = textures.get(&texname); let texture = textures.get(texname);
draw_scaled_texture(d, texture, px, py, scale); draw_scaled_texture(d, texture, px, py, scale);
} else { } else {
d.draw_rectangle(px, py, tile_size, tile_size, Color::new(0, 0, 0, 80)); d.draw_rectangle(px, py, tile_size, tile_size, Color::new(0, 0, 0, 80));

View file

@ -167,40 +167,57 @@ impl Tile {
} }
} }
pub fn texture(&self) -> String { pub fn texture(&self) -> &str {
match self { match self {
Tile::Open(OpenTile::Blank, _) => "", Tile::Open(OpenTile::Blank, _) => "",
Tile::Block => "block", Tile::Block => "block",
Tile::Marble { value: _, dir: _ } => "marble", Tile::Marble { value: _, dir: _ } => "marble",
Tile::Open(OpenTile::Digit(n), _) => return format!("tile_digit_{n}"), Tile::Open(OpenTile::Digit(n), _) => match n {
0 => "tile_digit_0",
1 => "tile_digit_1",
2 => "tile_digit_2",
3 => "tile_digit_3",
4 => "tile_digit_4",
5 => "tile_digit_5",
6 => "tile_digit_6",
7 => "tile_digit_7",
8 => "tile_digit_8",
9 => "tile_digit_9",
_ => unreachable!(),
},
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 {
"button_on" return "button_on";
} else {
"button_off"
} }
"button_off"
} }
Tile::Wire(wire, state) => { Tile::Wire(wire, state) => {
return format!( if *state {
"{}_{}", return wire.texture_name_on();
wire.texture_name(), }
if *state { "on" } else { "off" } wire.texture_name_off()
)
} }
Tile::Powerable(tile, state) => { Tile::Powerable(tile, state) => {
let root = match tile { if *state {
PTile::Comparator(comp) => comp.texture_name(), return match tile {
PTile::Math(math_op) => math_op.texture_name(), PTile::Comparator(comp) => comp.texture_name_on(),
PTile::Silo => "silo", PTile::Math(math_op) => math_op.texture_name_on(),
PTile::Flipper => "flipper", PTile::Silo => "silo_on",
PTile::IO => "io_tile", PTile::Flipper => "flipper_on",
PTile::IO => "io_tile_on",
};
}
return match tile {
PTile::Comparator(comp) => comp.texture_name_off(),
PTile::Math(math_op) => math_op.texture_name_off(),
PTile::Silo => "silo_off",
PTile::Flipper => "flipper_off",
PTile::IO => "io_tile_off",
}; };
return format!("{root}_{}", if *state { "on" } else { "off" });
} }
} }
.to_owned()
} }
} }
@ -296,11 +313,18 @@ impl WireType {
} }
} }
pub const fn texture_name(&self) -> &'static str { pub const fn texture_name_on(&self) -> &'static str {
match self { match self {
WireType::Vertical => "wire_vertical", WireType::Vertical => "wire_vertical_on",
WireType::Horizontal => "wire_horizontal", WireType::Horizontal => "wire_horizontal_on",
WireType::Cross => "wire_cross", WireType::Cross => "wire_cross_on",
}
}
pub const fn texture_name_off(&self) -> &'static str {
match self {
WireType::Vertical => "wire_vertical_off",
WireType::Horizontal => "wire_horizontal_off",
WireType::Cross => "wire_cross_off",
} }
} }
} }
@ -339,13 +363,22 @@ impl MirrorType {
} }
impl MathOp { impl MathOp {
pub const fn texture_name(&self) -> &'static str { pub const fn texture_name_on(&self) -> &'static str {
match self { match self {
MathOp::Add => "add", MathOp::Add => "add_on",
MathOp::Sub => "sub", MathOp::Sub => "sub_on",
MathOp::Mul => "mul", MathOp::Mul => "mul_on",
MathOp::Div => "div", MathOp::Div => "div_on",
MathOp::Rem => "rem", MathOp::Rem => "rem_on",
}
}
pub const fn texture_name_off(&self) -> &'static str {
match self {
MathOp::Add => "add_off",
MathOp::Sub => "sub_off",
MathOp::Mul => "mul_off",
MathOp::Div => "div_off",
MathOp::Rem => "rem_off",
} }
} }
@ -371,12 +404,20 @@ impl MathOp {
} }
impl Comparison { impl Comparison {
pub const fn texture_name(&self) -> &'static str { pub const fn texture_name_on(&self) -> &'static str {
match self { match self {
Comparison::LessThan => "lt", Comparison::LessThan => "lt_on",
Comparison::GreaterThan => "gt", Comparison::GreaterThan => "gt_on",
Comparison::Equal => "eq", Comparison::Equal => "eq_on",
Comparison::NotEqual => "neq", Comparison::NotEqual => "neq_on",
}
}
pub const fn texture_name_off(&self) -> &'static str {
match self {
Comparison::LessThan => "lt_off",
Comparison::GreaterThan => "gt_off",
Comparison::Equal => "eq_off",
Comparison::NotEqual => "neq_off",
} }
} }