From e9b03b937bb3176b020ec2e3feb59c626a8be7eb Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Mon, 7 Oct 2024 17:29:56 +0200 Subject: [PATCH] toggle to show input as bytes or text --- src/editor.rs | 64 ++++++++++++++++++++++++++++++++------------ src/marble_engine.rs | 4 +++ 2 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index 1727775..e59390a 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -435,17 +435,47 @@ impl Editor { draw_usize(d, textures, self.machine.step_count(), 420, 4, 5, 2); - let mut input_text = String::from_utf8_lossy(self.machine.input()).to_string(); + d.draw_text("input:", 523, 8, 10, Color::WHITE); + if simple_button(d, 520, 20, 35, 15) { + self.input_as_text = !self.input_as_text + } + let input_mode_text = if self.input_as_text { "text" } else { "bytes" }; + d.draw_text(input_mode_text, 523, 23, 10, Color::WHITE); + + let input_x = 560; let width = d.get_screen_width(); - d.draw_text("input:", width - 260, 10, 20, Color::WHITE); - if text_input( - d, - Rectangle::new(width as f32 - 205., 5., 200., 30.), - &mut input_text, - &mut self.input_text_selected, - self.level.is_sandbox(), - ) { - self.machine.set_input(input_text.into_bytes()); + if self.input_as_text { + let mut input_text = String::from_utf8_lossy(self.machine.input()).to_string(); + if text_input( + d, + Rectangle::new(input_x as f32, 5., (width - input_x - 5) as f32, 30.), + &mut input_text, + &mut self.input_text_selected, + self.level.is_sandbox(), + ) { + self.machine.set_input(input_text.into_bytes()); + } + } else { + let input_cell_width = 43; + let input_cells = (d.get_screen_width() - input_x) as usize / input_cell_width as usize; + + let input_start = self.machine.input_index().saturating_sub(input_cells); + let input_end = input_start + input_cells; + for (box_index, index) in (input_start..input_end).enumerate() { + let x = input_x + input_cell_width * box_index as i32; + let byte = self.machine.input().get(index); + d.draw_rectangle(x, 5, input_cell_width - 5, 30, Color::DIMGRAY); + let color = if index < self.machine.input_index() { + d.draw_rectangle(x + 4, 25, input_cell_width - 13, 8, Color::LIME); + Color::LIGHTGREEN + } else { + Color::WHITE + }; + if let Some(&byte) = byte { + let top_text = format!("{}", byte); + d.draw_text(&top_text, x + 2, 5, 20, color); + } + } } } @@ -520,7 +550,7 @@ impl Editor { let output_x = 370; let output_cell_width = 43; - let output_cells = (d.get_screen_width() - output_x) as usize / 43; + let output_cells = (d.get_screen_width() - output_x) as usize / output_cell_width as usize; let y = footer_top as i32 + 5; if simple_button(d, output_x, y + 70, 65, 15) { @@ -552,8 +582,8 @@ impl Editor { (Color::DARKGRAY, Color::DIMGRAY) }; - d.draw_rectangle(x, y, 38, 30, top_color); - d.draw_rectangle(x, y + 35, 38, 30, bottom_color); + d.draw_rectangle(x, y, output_cell_width - 5, 30, top_color); + d.draw_rectangle(x, y + 35, output_cell_width - 5, 30, bottom_color); if let Some(&expected_byte) = expected_byte { let top_text = if self.output_as_text && (expected_byte.is_ascii_graphic() || expected_byte.is_ascii_whitespace()) @@ -674,10 +704,10 @@ impl Editor { Tool::SelectArea(_, _) => (), } } - if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT) { - if self.active_tool == Tool::Erase { - self.set_tile(tile_pos.into(), Tile::Blank) - } + if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT) + && self.active_tool == Tool::Erase + { + self.set_tile(tile_pos.into(), Tile::Blank) } if let Tool::SelectArea(selection, is_selecting) = &mut self.active_tool { if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT) { diff --git a/src/marble_engine.rs b/src/marble_engine.rs index ffd6fc7..9f7c2fd 100644 --- a/src/marble_engine.rs +++ b/src/marble_engine.rs @@ -58,6 +58,10 @@ impl Machine { &self.input } + pub fn input_index(&self) -> usize { + self.input_index + } + pub fn step_count(&self) -> usize { self.steps }