From 59aeeff5b6bc81d6e2076bb0b95b2ca3cc4a8e95 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Tue, 10 Dec 2024 21:50:46 +0100 Subject: [PATCH] option to hide grid and marble direction/value --- src/editor.rs | 46 ++++++++++++++++++++++++++-------------------- src/solution.rs | 5 +---- src/util.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index 18a4510..c104217 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -17,7 +17,7 @@ use crate::{ tile::{Claim, Direction, GateType, MathOp, MirrorType, OpenTile, PTile, Tile, WireType}, Machine, }, - simple_button, simple_option_button, slider, + simple_button, simple_option_button, simple_toggle_button, slider, solution::{Score, Solution}, text_input, texture_option_button, theme::*, @@ -65,6 +65,7 @@ pub struct Editor { max_step_time: u128, start_time: Instant, pasting_board: Option, + draw_overlay: bool, } #[derive(Debug, PartialEq)] @@ -144,6 +145,7 @@ impl Editor { max_step_time: 0, start_time: Instant::now(), pasting_board: None, + draw_overlay: true, } } @@ -192,9 +194,7 @@ impl Editor { if self.popup != EndPopup::None { self.popup = EndPopup::Dismissed; } - if !self.level.outputs().is_empty() - && self.popup == EndPopup::None - { + if !self.level.outputs().is_empty() && self.popup == EndPopup::None { if self.level.outputs() == self.machine.output() { self.popup = EndPopup::Success; println!("completed in {:?}", self.start_time.elapsed()); @@ -432,28 +432,32 @@ impl Editor { self.machine .board() .draw(d, textures, self.view_offset, self.zoom); - self.machine - .draw_marble_values(d, textures, self.view_offset, self.zoom); + if self.draw_overlay { + self.machine + .draw_marble_values(d, textures, self.view_offset, self.zoom); + } } } pub fn draw(&mut self, d: &mut RaylibDrawHandle, textures: &Textures) { d.clear_background(Color::new(64, 64, 64, 255)); - let tile_size = (16 << self.zoom) as f32; - let grid_spill_x = (self.view_offset.x).rem(tile_size) - tile_size; - let grid_spill_y = (self.view_offset.y).rem(tile_size) - tile_size; - d.gui_grid( - Rectangle::new( - grid_spill_x, - grid_spill_y, - d.get_screen_width() as f32 * 2., - d.get_screen_height() as f32 * 2., - ), - None, - tile_size, - 1, - ); + if self.draw_overlay { + let tile_size = (16 << self.zoom) as f32; + let grid_spill_x = (self.view_offset.x).rem(tile_size) - tile_size; + let grid_spill_y = (self.view_offset.y).rem(tile_size) - tile_size; + d.gui_grid( + Rectangle::new( + grid_spill_x, + grid_spill_y, + d.get_screen_width() as f32 * 2., + d.get_screen_height() as f32 * 2., + ), + None, + tile_size, + 1, + ); + } self.draw_board(d, textures); self.board_overlay(d, textures); @@ -592,6 +596,8 @@ impl Editor { d.draw_text("save", 90, 10, 20, Color::WHITE); } + simple_toggle_button(d, &mut self.draw_overlay, 235, 4, 20, 32, 4); + match self.sim_state { SimState::Editing => { if simple_button(d, 260, 4, 32, 32) { diff --git a/src/solution.rs b/src/solution.rs index f3f7784..11fd23f 100644 --- a/src/solution.rs +++ b/src/solution.rs @@ -57,10 +57,7 @@ impl Solution { pub fn score_text(&self) -> String { if let Some(score) = &self.score { - format!( - "C: {} T: {}", - score.cycles, score.tiles - ) + format!("C: {} T: {}", score.cycles, score.tiles) } else { "unsolved".into() } diff --git a/src/util.rs b/src/util.rs index 2be433a..e0baf10 100644 --- a/src/util.rs +++ b/src/util.rs @@ -44,6 +44,38 @@ pub fn simple_button(d: &mut RaylibDrawHandle, x: i32, y: i32, width: i32, heigh pressed } +pub fn simple_toggle_button( + d: &mut RaylibDrawHandle, + state: &mut bool, + x: i32, + y: i32, + width: i32, + height: i32, + margin: i32, +) { + let mouse_pos = d.get_mouse_position(); + let bounds = Rectangle { + x: x as f32, + y: y as f32, + width: width as f32, + height: height as f32, + }; + let hover = bounds.check_collision_point_rec(mouse_pos); + d.draw_rectangle(x, y, width, height, widget_bg(hover)); + if *state { + d.draw_rectangle( + x + margin, + y + margin, + width - margin * 2, + height - margin * 2, + BG_DARK, + ); + } + if hover && d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT) { + *state = !*state; + } +} + pub fn simple_option_button( d: &mut RaylibDrawHandle, x: i32,