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 {
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) {
@ -238,7 +238,7 @@ impl Editor {
}
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_y = self.source_board.height() as f32 / 2. * tile_size;
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) {
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 tile_pos_of_mouse = (mouse_pos - self.view_offset) / tile_size;
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.x = self.view_offset.x.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) {
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);
if x != 0 || y != 0 {
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) {
let tile_size = (TILE_TEXTURE_SIZE * self.zoom) as f32;
let tile_size = TILE_TEXTURE_SIZE * self.zoom;
pos.x -= BOARD_MARGIN;
pos.y -= BOARD_MARGIN;
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')));
match tool_button(
(1, 1),
&Tile::Wire(self.tool_wire, false).texture(),
Tile::Wire(self.tool_wire, false).texture(),
Tool::Wire,
) {
Some(Scroll::Down) => self.tool_wire.next(),
@ -800,14 +800,14 @@ impl Editor {
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::Up) => self.tool_arrow = self.tool_arrow.left(),
None => (),
}
if tool_button(
(1, 3),
&Tile::Mirror(self.tool_mirror).texture(),
Tile::Mirror(self.tool_mirror).texture(),
Tool::Mirror,
)
.is_some()
@ -816,7 +816,7 @@ impl Editor {
}
match tool_button(
(1, 4),
&Tile::Powerable(PTile::Math(self.tool_math), false).texture(),
Tile::Powerable(PTile::Math(self.tool_math), false).texture(),
Tool::Math,
) {
Some(Scroll::Down) => self.tool_math.next(),
@ -825,7 +825,7 @@ impl Editor {
}
match tool_button(
(1, 5),
&Tile::Powerable(PTile::Comparator(self.tool_comparator), false).texture(),
Tile::Powerable(PTile::Comparator(self.tool_comparator), false).texture(),
Tool::Comparator,
) {
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 {
let view_offset = Vector2::new(
self.view_offset.x.rem(tile_size as f32),
self.view_offset.y.rem(tile_size as f32),
self.view_offset.x.rem(tile_size),
self.view_offset.y.rem(tile_size),
);
let mut offset = mouse_pos - view_offset;
offset.x -= offset.x.rem(tile_size as f32);
offset.y -= offset.y.rem(tile_size as f32);
offset.x -= offset.x.rem(tile_size);
offset.y -= offset.y.rem(tile_size);
offset += view_offset;
board.draw(d, textures, offset, self.zoom);
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 mut pos = tile_pos.into();
@ -930,7 +930,7 @@ impl Editor {
return;
}
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(
textures.get("selection"),
tile_screen_pos,
@ -958,7 +958,7 @@ impl Editor {
}
}
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_screen_pos = self.pos_to_screen(tile_pos);
@ -966,26 +966,26 @@ impl Editor {
if self.active_tool != Tool::None {
let tex = match &self.active_tool {
Tool::None => unreachable!(),
Tool::Erase => "cancel".into(),
Tool::Erase => "cancel",
Tool::SetTile(t) => t.texture(),
Tool::Math => format!("{}_off", self.tool_math.texture_name()),
Tool::Comparator => format!("{}_off", self.tool_comparator.texture_name()),
Tool::Wire => format!("{}_off", self.tool_wire.texture_name()),
Tool::Arrow => self.tool_arrow.arrow_tile_texture_name().into(),
Tool::Mirror => self.tool_mirror.texture_name().into(),
Tool::Digits(_) => "selection".into(),
Tool::Math => self.tool_math.texture_name_off(),
Tool::Comparator => self.tool_comparator.texture_name_off(),
Tool::Wire => self.tool_wire.texture_name_off(),
Tool::Arrow => self.tool_arrow.arrow_tile_texture_name(),
Tool::Mirror => self.tool_mirror.texture_name(),
Tool::Digits(_) => "selection",
Tool::SelectArea(selection) => {
if selection.is_selecting {
"transparent".into()
"transparent"
} else {
"area_full".into()
"area_full"
}
}
Tool::Blueprint => "transparent".into(),
Tool::Blueprint => "transparent",
};
d.draw_texture_ex(
textures.get(&tex),
textures.get(tex),
tile_screen_pos,
0.,
self.zoom,
@ -1056,12 +1056,12 @@ impl Editor {
if let Tool::Blueprint = self.active_tool {
if let Some(bp) = self.blueprints.get_mut(self.selected_blueprint) {
let view_offset = Vector2::new(
self.view_offset.x.rem(tile_size as f32),
self.view_offset.y.rem(tile_size as f32),
self.view_offset.x.rem(tile_size),
self.view_offset.y.rem(tile_size),
);
let mut offset = mouse_pos - view_offset;
offset.x -= offset.x.rem(tile_size as f32);
offset.y -= offset.y.rem(tile_size as f32);
offset.x -= offset.x.rem(tile_size);
offset.y -= offset.y.rem(tile_size);
offset += view_offset;
bp.convert_board().draw(d, textures, offset, self.zoom);
}

View file

@ -263,7 +263,7 @@ impl Board {
if texname.is_empty() {
continue;
}
let texture = textures.get(&texname);
let texture = textures.get(texname);
draw_scaled_texture(d, texture, px, py, scale);
} else {
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 {
Tile::Open(OpenTile::Blank, _) => "",
Tile::Block => "block",
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::Arrow(dir) => dir.arrow_tile_texture_name(),
Tile::Button(state) => {
if *state {
"button_on"
} else {
"button_off"
return "button_on";
}
"button_off"
}
Tile::Wire(wire, state) => {
return format!(
"{}_{}",
wire.texture_name(),
if *state { "on" } else { "off" }
)
if *state {
return wire.texture_name_on();
}
wire.texture_name_off()
}
Tile::Powerable(tile, state) => {
let root = match tile {
PTile::Comparator(comp) => comp.texture_name(),
PTile::Math(math_op) => math_op.texture_name(),
PTile::Silo => "silo",
PTile::Flipper => "flipper",
PTile::IO => "io_tile",
if *state {
return match tile {
PTile::Comparator(comp) => comp.texture_name_on(),
PTile::Math(math_op) => math_op.texture_name_on(),
PTile::Silo => "silo_on",
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 {
WireType::Vertical => "wire_vertical",
WireType::Horizontal => "wire_horizontal",
WireType::Cross => "wire_cross",
WireType::Vertical => "wire_vertical_on",
WireType::Horizontal => "wire_horizontal_on",
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 {
pub const fn texture_name(&self) -> &'static str {
pub const fn texture_name_on(&self) -> &'static str {
match self {
MathOp::Add => "add",
MathOp::Sub => "sub",
MathOp::Mul => "mul",
MathOp::Div => "div",
MathOp::Rem => "rem",
MathOp::Add => "add_on",
MathOp::Sub => "sub_on",
MathOp::Mul => "mul_on",
MathOp::Div => "div_on",
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 {
pub const fn texture_name(&self) -> &'static str {
pub const fn texture_name_on(&self) -> &'static str {
match self {
Comparison::LessThan => "lt",
Comparison::GreaterThan => "gt",
Comparison::Equal => "eq",
Comparison::NotEqual => "neq",
Comparison::LessThan => "lt_on",
Comparison::GreaterThan => "gt_on",
Comparison::Equal => "eq_on",
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",
}
}