diff --git a/README.md b/README.md index 2f7fa49..2345688 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ logic mostly like https://git.crispypin.cc/CrispyPin/marble ## todo -- show level info in editor - comments - accessibility - ui scaling diff --git a/src/editor.rs b/src/editor.rs index bc6d6dc..7227ff3 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -443,6 +443,12 @@ impl Editor { } 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) { self.sim_state = SimState::Editing; 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 || self.sim_state != SimState::Editing || mouse_pos.x > SIDEBAR_WIDTH as f32) && mouse_pos.y > HEADER_HEIGHT as f32 && (mouse_pos.y as i32) < (rl.get_screen_height() - FOOTER_HEIGHT) { - if rl.get_mouse_wheel_move() > 0. { - self.zoom_in(rl); - } - if rl.get_mouse_wheel_move() < 0. { - self.zoom_out(rl); + match self.mouse.scroll() { + Some(Scroll::Up) => self.zoom_in(rl), + Some(Scroll::Down) => self.zoom_out(rl), + None => (), } } - 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) { 4. } 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.board_overlay(d, textures); self.draw_bottom_bar(d, textures); @@ -587,13 +586,22 @@ impl Editor { 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 { Popup::Success | Popup::Failure => { - self.tooltip.reset(); self.draw_end_popup(d, textures); } Popup::LevelInfo => { - self.tooltip.reset(); let bounds = screen_centered_rect_dyn(d, 100, 100); d.draw_rectangle_rec(bounds, BG_MEDIUM); 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"); 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"); 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) { 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; if self.sim_state == SimState::Editing { if let Some(board) = &self.pasting_board { @@ -1167,18 +1184,18 @@ impl Editor { self.pasting_board = None; 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( self.view_offset.x.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.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; + if self.mouse.left_click() { + 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 board = self.pasting_board.take().unwrap(); 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 { - let tile_pos = (mouse_pos - self.view_offset) / tile_size; + if self.mouse.pos().y < footer_top && self.mouse.pos().y > HEADER_HEIGHT as f32 { + 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_screen_pos = self.pos_to_screen(tile_pos); @@ -1249,7 +1266,7 @@ impl Editor { 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(); match self.active_tool { Tool::None | Tool::Erase | Tool::SelectArea(_) => (), @@ -1272,7 +1289,7 @@ impl Editor { } } 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) { let board = bp.get_board().unwrap().clone(); self.set_area(pos, board); @@ -1281,13 +1298,11 @@ impl Editor { } } } - if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT) - && self.active_tool == Tool::Erase - { + if self.mouse.left_hold() && self.active_tool == Tool::Erase { self.set_tile(tile_pos.into(), Tile::BLANK); } 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 let Some((_start, end)) = &mut selection.area { *end = tile_pos.into(); @@ -1298,7 +1313,7 @@ impl Editor { selection.area = Some((tile_pos.into(), tile_pos.into())); 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; } } @@ -1308,18 +1323,20 @@ impl Editor { self.view_offset.x.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.y -= offset.y.rem(tile_size); offset += view_offset; bp.convert_board().draw(d, textures, offset, self.zoom); } - if mouse_pos.x < SIDEBAR_WIDTH as f32 { - if scroll_delta < 0. + if self.mouse.pos().x < SIDEBAR_WIDTH as f32 { + if self.mouse.scroll() == Some(Scroll::Down) && self.blueprint_scroll < self.blueprints.len().saturating_sub(5) { 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; } } diff --git a/src/ui.rs b/src/ui.rs index 6c18c9c..7ef9660 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -99,7 +99,7 @@ pub struct 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(); *self = Self { text: None, @@ -356,6 +356,7 @@ pub fn draw_usize_small( pub fn slider( d: &mut RaylibDrawHandle, + mouse: &MouseInput, value: &mut u8, min: u8, max: u8, @@ -368,18 +369,17 @@ pub fn slider( 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 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); - if bounds.check_collision_point_rec(mouse_pos) { - if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT) { - let percent = (mouse_pos.x - bounds.x) / bounds.width; + if mouse.is_over(bounds) { + if mouse.left_hold() { + let percent = (mouse.pos().x - bounds.x) / bounds.width; let new_value = min + (percent * (max - min + 1) as f32) as u8; if *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; - } else if d.get_mouse_wheel_move() < -0.5 && *value > min { + } else if mouse.scroll() == Some(Scroll::Down) && *value > min { *value -= 1; } } diff --git a/src/util.rs b/src/util.rs index 297bbe2..de8cd9d 100644 --- a/src/util.rs +++ b/src/util.rs @@ -67,7 +67,7 @@ pub struct MouseInput { pos: Vector2, left_click: bool, left_hold: bool, - right_click: bool, + left_release: bool, right_hold: bool, scroll: Option, } @@ -78,7 +78,7 @@ impl MouseInput { pos: rl.get_mouse_position(), left_click: rl.is_mouse_button_pressed(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), scroll: get_scroll(rl), } @@ -104,8 +104,8 @@ impl MouseInput { self.left_hold } - pub fn right_click(&self) -> bool { - self.right_click + pub fn left_release(&self) -> bool { + self.left_release } pub fn right_hold(&self) -> bool {