add ui helper for 32x32 texture buttons with a tooltip, to reduce duplication of magic number coordinates

This commit is contained in:
Crispy 2024-12-24 00:32:47 +01:00
parent bc0d1ab94a
commit a4cfbb4beb
2 changed files with 94 additions and 53 deletions

View file

@ -652,13 +652,16 @@ impl Editor {
.enumerate()
{
let i = i + self.blueprint_scroll;
self.tooltip.add(5, y, 32, 32, "Delete");
if simple_button(d, &self.mouse, 5, y, 32, 32) {
if tex32_button(
(d, &self.mouse),
(5, y),
textures.get("rubbish"),
(&mut self.tooltip, "Delete"),
) {
b.remove_file();
self.blueprints.remove(i);
break;
}
draw_scaled_texture(d, textures.get("rubbish"), 5, y, 2.);
let is_selected = self.selected_blueprint == i;
let mut text_selected = is_selected && self.blueprint_name_selected;
text_input(
@ -753,28 +756,34 @@ impl Editor {
Color::new(32, 32, 32, 255),
);
if self.exit_menu {
if simple_button(d, &self.mouse, 4, 4, 32, 32) {
if tex32_button(
(d, &self.mouse),
(4, 4),
textures.get("exit"),
(&mut self.tooltip, "exit"),
) {
if self.exit_menu {
self.exit_state = ExitState::ExitAndSave;
}
self.tooltip.add(4, 4, 32, 32, "exit");
draw_scaled_texture(d, textures.get("exit"), 4, 4, 2.);
if simple_button(d, &self.mouse, 40, 4, 32, 32) {
self.exit_menu = false;
}
draw_scaled_texture(d, textures.get("cancel"), 40, 4, 2.);
self.tooltip.add(40, 4, 32, 32, "cancel");
} else {
if simple_button(d, &self.mouse, 4, 4, 32, 32) {
} else {
self.exit_menu = true;
}
self.tooltip.add(4, 4, 32, 32, "exit");
draw_scaled_texture(d, textures.get("exit"), 4, 4, 2.);
if simple_button(d, &self.mouse, 40, 4, 32, 32) {
self.exit_state = ExitState::Save;
}
if self.exit_menu {
if tex32_button(
(d, &self.mouse),
(40, 4),
textures.get("cancel"),
(&mut self.tooltip, "cancel"),
) {
self.exit_menu = false;
}
draw_scaled_texture(d, textures.get("save"), 40, 4, 2.);
self.tooltip.add(40, 4, 32, 32, "save");
} else if tex32_button(
(d, &self.mouse),
(40, 4),
textures.get("save"),
(&mut self.tooltip, "save"),
) {
self.exit_state = ExitState::Save;
}
if text_button(d, &self.mouse, 76, 5, 50, "info") {
@ -782,71 +791,90 @@ impl Editor {
}
if self.sim_state == SimState::Editing {
self.tooltip.add(150, 4, 32, 32, "Undo");
if simple_button(d, &self.mouse, 150, 4, 32, 32) {
self.undo()
}
let undo_icon = if self.undo_index > 0 {
"undo"
} else {
"undo_disabled"
};
draw_scaled_texture(d, textures.get(undo_icon), 150, 4, 2.);
self.tooltip.add(186, 4, 32, 32, "Redo");
if simple_button(d, &self.mouse, 186, 4, 32, 32) {
self.redo()
if tex32_button(
(d, &self.mouse),
(150, 4),
textures.get(undo_icon),
(&mut self.tooltip, "Undo"),
) {
self.undo()
}
let redo_icon = if self.undo_index < self.undo_history.len() {
"redo"
} else {
"redo_disabled"
};
draw_scaled_texture(d, textures.get(redo_icon), 186, 4, 2.);
if tex32_button(
(d, &self.mouse),
(186, 4),
textures.get(redo_icon),
(&mut self.tooltip, "Redo"),
) {
self.redo()
}
}
self.tooltip.add(223, 4, 32, 32, "Toggle overlay");
if simple_button(d, &self.mouse, 223, 4, 32, 32) {
self.draw_overlay = !self.draw_overlay;
}
let overlay_btn_icon = if self.draw_overlay {
"marble_overlay"
} else {
"marble"
};
draw_scaled_texture(d, textures.get(overlay_btn_icon), 223, 4, 2.);
if tex32_button(
(d, &self.mouse),
(223, 4),
textures.get(overlay_btn_icon),
(&mut self.tooltip, "Toggle overlay"),
) {
self.draw_overlay = !self.draw_overlay;
}
if self.sim_state == SimState::Running {
self.tooltip.add(260, 4, 32, 32, "Pause");
if simple_button(d, &self.mouse, 260, 4, 32, 32) {
if tex32_button(
(d, &self.mouse),
(260, 4),
textures.get("pause"),
(&mut self.tooltip, "Pause"),
) {
self.sim_state = SimState::Stepping;
}
draw_scaled_texture(d, textures.get("pause"), 260, 4, 2.);
} else {
self.tooltip.add(260, 4, 32, 32, "Start");
if simple_button(d, &self.mouse, 260, 4, 32, 32) {
if self.sim_state == SimState::Editing {
self.init_sim()
}
self.sim_state = SimState::Running;
} else if tex32_button(
(d, &self.mouse),
(260, 4),
textures.get("play"),
(&mut self.tooltip, "Start"),
) {
if self.sim_state == SimState::Editing {
self.init_sim()
}
draw_scaled_texture(d, textures.get("play"), 260, 4, 2.);
self.sim_state = SimState::Running;
}
if self.sim_state != SimState::Editing {
self.tooltip.add(296, 4, 32, 32, "Stop");
if simple_button(d, &self.mouse, 296, 4, 32, 32) {
if tex32_button(
(d, &self.mouse),
(296, 4),
textures.get("stop"),
(&mut self.tooltip, "Stop"),
) {
self.sim_state = SimState::Editing;
self.popup = Popup::None;
}
draw_scaled_texture(d, textures.get("stop"), 296, 4, 2.);
}
self.tooltip.add(332, 4, 32, 32, "Step");
if simple_button(d, &self.mouse, 332, 4, 32, 32) {
if tex32_button(
(d, &self.mouse),
(332, 4),
textures.get("step"),
(&mut self.tooltip, "Step"),
) {
self.step_pressed();
}
draw_scaled_texture(d, textures.get("step"), 332, 4, 2.);
self.tooltip.add(368, 4, 48, 32, "Speed");
draw_usize(d, textures, 1 << self.sim_speed, 368, 4, SPEED_DIGITS, 1);

View file

@ -194,6 +194,19 @@ pub fn text_button(
clicked
}
pub fn tex32_button(
(d, mouse): (&mut RaylibDrawHandle, &MouseInput),
(x, y): (i32, i32),
texture: &Texture2D,
(tooltip, text): (&mut Tooltip, &'static str),
) -> bool {
let size = 32;
let clicked = simple_button(d, mouse, x, y, 32, 32);
draw_scaled_texture(d, texture, x, y, 2.);
tooltip.add(x, y, size, size, text);
clicked
}
pub fn simple_option_button<T>(
d: &mut RaylibDrawHandle,
mouse: &MouseInput,