disable all editor interactions while a popup is visible

This commit is contained in:
Crispy 2024-12-23 01:09:18 +01:00
parent b5600b301d
commit c5328126b3
4 changed files with 63 additions and 47 deletions

View file

@ -4,7 +4,6 @@
logic mostly like https://git.crispypin.cc/CrispyPin/marble logic mostly like https://git.crispypin.cc/CrispyPin/marble
## todo ## todo
- show level info in editor
- comments - comments
- accessibility - accessibility
- ui scaling - ui scaling

View file

@ -443,6 +443,12 @@ impl Editor {
} }
pub fn update(&mut self, rl: &RaylibHandle) { pub fn update(&mut self, rl: &RaylibHandle) {
self.tooltip.init_frame(rl);
self.mouse = MouseInput::get(rl);
if !matches!(self.popup, Popup::None | Popup::Dismissed) {
self.mouse.clear();
}
if rl.is_key_pressed(KeyboardKey::KEY_ESCAPE) { if rl.is_key_pressed(KeyboardKey::KEY_ESCAPE) {
self.sim_state = SimState::Editing; self.sim_state = SimState::Editing;
self.popup = Popup::None; self.popup = Popup::None;
@ -492,21 +498,20 @@ impl Editor {
} }
} }
let mouse_pos = rl.get_mouse_position(); let mouse_pos = self.mouse.pos();
if (self.active_tool != Tool::Blueprint if (self.active_tool != Tool::Blueprint
|| self.sim_state != SimState::Editing || self.sim_state != SimState::Editing
|| mouse_pos.x > SIDEBAR_WIDTH as f32) || mouse_pos.x > SIDEBAR_WIDTH as f32)
&& mouse_pos.y > HEADER_HEIGHT as f32 && mouse_pos.y > HEADER_HEIGHT as f32
&& (mouse_pos.y as i32) < (rl.get_screen_height() - FOOTER_HEIGHT) && (mouse_pos.y as i32) < (rl.get_screen_height() - FOOTER_HEIGHT)
{ {
if rl.get_mouse_wheel_move() > 0. { match self.mouse.scroll() {
self.zoom_in(rl); Some(Scroll::Up) => self.zoom_in(rl),
} Some(Scroll::Down) => self.zoom_out(rl),
if rl.get_mouse_wheel_move() < 0. { None => (),
self.zoom_out(rl);
} }
} }
if rl.is_mouse_button_down(MouseButton::MOUSE_BUTTON_RIGHT) { if self.mouse.right_hold() {
let speed = if rl.is_key_down(KeyboardKey::KEY_LEFT_SHIFT) { let speed = if rl.is_key_down(KeyboardKey::KEY_LEFT_SHIFT) {
4. 4.
} else { } else {
@ -570,12 +575,6 @@ impl Editor {
} }
} }
self.tooltip.init_frame(d);
self.mouse = MouseInput::get(d);
if !matches!(self.popup, Popup::None | Popup::Dismissed) {
self.mouse.clear();
}
self.draw_board(d, textures); self.draw_board(d, textures);
self.board_overlay(d, textures); self.board_overlay(d, textures);
self.draw_bottom_bar(d, textures); self.draw_bottom_bar(d, textures);
@ -587,13 +586,22 @@ impl Editor {
self.mouse = MouseInput::get(d); self.mouse = MouseInput::get(d);
if !matches!(self.popup, Popup::None | Popup::Dismissed) {
self.tooltip.reset();
d.draw_rectangle(
0,
0,
d.get_screen_width(),
d.get_screen_height(),
Color::new(100, 100, 100, 120),
);
}
match self.popup { match self.popup {
Popup::Success | Popup::Failure => { Popup::Success | Popup::Failure => {
self.tooltip.reset();
self.draw_end_popup(d, textures); self.draw_end_popup(d, textures);
} }
Popup::LevelInfo => { Popup::LevelInfo => {
self.tooltip.reset();
let bounds = screen_centered_rect_dyn(d, 100, 100); let bounds = screen_centered_rect_dyn(d, 100, 100);
d.draw_rectangle_rec(bounds, BG_MEDIUM); d.draw_rectangle_rec(bounds, BG_MEDIUM);
d.draw_text(self.level.name(), 110, 110, 30, Color::LIGHTBLUE); d.draw_text(self.level.name(), 110, 110, 30, Color::LIGHTBLUE);
@ -823,7 +831,17 @@ impl Editor {
self.tooltip.add(368, 4, 48, 32, "Speed"); self.tooltip.add(368, 4, 48, 32, "Speed");
draw_usize(d, textures, 1 << self.sim_speed, 368, 4, SPEED_DIGITS, 1); draw_usize(d, textures, 1 << self.sim_speed, 368, 4, SPEED_DIGITS, 1);
slider(d, &mut self.sim_speed, 0, MAX_SPEED_POWER, 368, 24, 48, 12); slider(
d,
&self.mouse,
&mut self.sim_speed,
0,
MAX_SPEED_POWER,
368,
24,
48,
12,
);
self.tooltip.add(420, 4, 180, 32, "Steps"); self.tooltip.add(420, 4, 180, 32, "Steps");
draw_usize(d, textures, self.machine.step_count(), 420, 4, 9, 2); draw_usize(d, textures, self.machine.step_count(), 420, 4, 9, 2);
@ -1158,8 +1176,7 @@ impl Editor {
fn board_overlay(&mut self, d: &mut RaylibDrawHandle, textures: &Textures) { fn board_overlay(&mut self, d: &mut RaylibDrawHandle, textures: &Textures) {
let footer_top = (d.get_screen_height() - FOOTER_HEIGHT) as f32; let footer_top = (d.get_screen_height() - FOOTER_HEIGHT) as f32;
let mouse_pos = d.get_mouse_position();
let scroll_delta = d.get_mouse_wheel_move();
let tile_size = TILE_TEXTURE_SIZE * self.zoom; let tile_size = TILE_TEXTURE_SIZE * self.zoom;
if self.sim_state == SimState::Editing { if self.sim_state == SimState::Editing {
if let Some(board) = &self.pasting_board { if let Some(board) = &self.pasting_board {
@ -1167,18 +1184,18 @@ impl Editor {
self.pasting_board = None; self.pasting_board = None;
return; return;
} }
if mouse_pos.y < footer_top && mouse_pos.y > HEADER_HEIGHT as f32 { if self.mouse.pos().y < footer_top && self.mouse.pos().y > HEADER_HEIGHT as f32 {
let view_offset = Vector2::new( let view_offset = Vector2::new(
self.view_offset.x.rem(tile_size), self.view_offset.x.rem(tile_size),
self.view_offset.y.rem(tile_size), self.view_offset.y.rem(tile_size),
); );
let mut offset = mouse_pos - view_offset; let mut offset = self.mouse.pos() - view_offset;
offset.x -= offset.x.rem(tile_size); offset.x -= offset.x.rem(tile_size);
offset.y -= offset.y.rem(tile_size); 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 self.mouse.left_click() {
let tile_pos = (mouse_pos - self.view_offset) / tile_size; let tile_pos = (self.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 board = self.pasting_board.take().unwrap(); let board = self.pasting_board.take().unwrap();
self.set_area(tile_pos.into(), board); self.set_area(tile_pos.into(), board);
@ -1214,8 +1231,8 @@ impl Editor {
} }
} }
} }
if mouse_pos.y < footer_top && mouse_pos.y > HEADER_HEIGHT as f32 { if self.mouse.pos().y < footer_top && self.mouse.pos().y > HEADER_HEIGHT as f32 {
let tile_pos = (mouse_pos - self.view_offset) / tile_size; let tile_pos = (self.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);
@ -1249,7 +1266,7 @@ impl Editor {
Color::new(255, 255, 255, 100), Color::new(255, 255, 255, 100),
); );
} }
if d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT) { if self.mouse.left_click() {
let pos = tile_pos.into(); let pos = tile_pos.into();
match self.active_tool { match self.active_tool {
Tool::None | Tool::Erase | Tool::SelectArea(_) => (), Tool::None | Tool::Erase | Tool::SelectArea(_) => (),
@ -1272,7 +1289,7 @@ impl Editor {
} }
} }
Tool::Blueprint => { Tool::Blueprint => {
if mouse_pos.x > SIDEBAR_WIDTH as f32 { if self.mouse.pos().x > SIDEBAR_WIDTH as f32 {
if let Some(bp) = self.blueprints.get(self.selected_blueprint) { if let Some(bp) = self.blueprints.get(self.selected_blueprint) {
let board = bp.get_board().unwrap().clone(); let board = bp.get_board().unwrap().clone();
self.set_area(pos, board); self.set_area(pos, board);
@ -1281,13 +1298,11 @@ impl Editor {
} }
} }
} }
if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT) if self.mouse.left_hold() && 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 self.mouse.left_hold() {
if selection.is_selecting { if selection.is_selecting {
if let Some((_start, end)) = &mut selection.area { if let Some((_start, end)) = &mut selection.area {
*end = tile_pos.into(); *end = tile_pos.into();
@ -1298,7 +1313,7 @@ impl Editor {
selection.area = Some((tile_pos.into(), tile_pos.into())); selection.area = Some((tile_pos.into(), tile_pos.into()));
selection.is_selecting = true; selection.is_selecting = true;
} }
} else if d.is_mouse_button_released(MouseButton::MOUSE_BUTTON_LEFT) { } else if self.mouse.left_release() {
selection.is_selecting = false; selection.is_selecting = false;
} }
} }
@ -1308,18 +1323,20 @@ impl Editor {
self.view_offset.x.rem(tile_size), self.view_offset.x.rem(tile_size),
self.view_offset.y.rem(tile_size), self.view_offset.y.rem(tile_size),
); );
let mut offset = mouse_pos - view_offset; let mut offset = self.mouse.pos() - view_offset;
offset.x -= offset.x.rem(tile_size); offset.x -= offset.x.rem(tile_size);
offset.y -= offset.y.rem(tile_size); 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);
} }
if mouse_pos.x < SIDEBAR_WIDTH as f32 { if self.mouse.pos().x < SIDEBAR_WIDTH as f32 {
if scroll_delta < 0. if self.mouse.scroll() == Some(Scroll::Down)
&& self.blueprint_scroll < self.blueprints.len().saturating_sub(5) && self.blueprint_scroll < self.blueprints.len().saturating_sub(5)
{ {
self.blueprint_scroll += 1; self.blueprint_scroll += 1;
} else if scroll_delta > 0. && self.blueprint_scroll > 0 { } else if self.mouse.scroll() == Some(Scroll::Up)
&& self.blueprint_scroll > 0
{
self.blueprint_scroll -= 1; self.blueprint_scroll -= 1;
} }
} }

View file

@ -99,7 +99,7 @@ pub struct Tooltip {
} }
impl Tooltip { impl Tooltip {
pub fn init_frame(&mut self, d: &mut RaylibDrawHandle) { pub fn init_frame(&mut self, d: &RaylibHandle) {
let p = d.get_mouse_position(); let p = d.get_mouse_position();
*self = Self { *self = Self {
text: None, text: None,
@ -356,6 +356,7 @@ pub fn draw_usize_small(
pub fn slider( pub fn slider(
d: &mut RaylibDrawHandle, d: &mut RaylibDrawHandle,
mouse: &MouseInput,
value: &mut u8, value: &mut u8,
min: u8, min: u8,
max: u8, max: u8,
@ -368,18 +369,17 @@ pub fn slider(
let percent = (*value - min + 1) as f32 / (max - min + 1) as f32; let percent = (*value - min + 1) as f32 / (max - min + 1) as f32;
d.draw_rectangle(x, y, width, height, BG_WIDGET); d.draw_rectangle(x, y, width, height, BG_WIDGET);
d.draw_rectangle(x, y, (width as f32 * percent) as i32, height, Color::CYAN); d.draw_rectangle(x, y, (width as f32 * percent) as i32, height, Color::CYAN);
let mouse_pos = d.get_mouse_position();
let bounds = Rectangle::new(x as f32, y as f32, width as f32, height as f32); let bounds = Rectangle::new(x as f32, y as f32, width as f32, height as f32);
if bounds.check_collision_point_rec(mouse_pos) { if mouse.is_over(bounds) {
if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT) { if mouse.left_hold() {
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 mouse.scroll() == Some(Scroll::Up) && *value < max {
*value += 1; *value += 1;
} else if d.get_mouse_wheel_move() < -0.5 && *value > min { } else if mouse.scroll() == Some(Scroll::Down) && *value > min {
*value -= 1; *value -= 1;
} }
} }

View file

@ -67,7 +67,7 @@ pub struct MouseInput {
pos: Vector2, pos: Vector2,
left_click: bool, left_click: bool,
left_hold: bool, left_hold: bool,
right_click: bool, left_release: bool,
right_hold: bool, right_hold: bool,
scroll: Option<Scroll>, scroll: Option<Scroll>,
} }
@ -78,7 +78,7 @@ impl MouseInput {
pos: rl.get_mouse_position(), pos: rl.get_mouse_position(),
left_click: rl.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT), left_click: rl.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT),
left_hold: rl.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT), left_hold: rl.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT),
right_click: rl.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_RIGHT), left_release: rl.is_mouse_button_released(MouseButton::MOUSE_BUTTON_LEFT),
right_hold: rl.is_mouse_button_down(MouseButton::MOUSE_BUTTON_RIGHT), right_hold: rl.is_mouse_button_down(MouseButton::MOUSE_BUTTON_RIGHT),
scroll: get_scroll(rl), scroll: get_scroll(rl),
} }
@ -104,8 +104,8 @@ impl MouseInput {
self.left_hold self.left_hold
} }
pub fn right_click(&self) -> bool { pub fn left_release(&self) -> bool {
self.right_click self.left_release
} }
pub fn right_hold(&self) -> bool { pub fn right_hold(&self) -> bool {