toggle to show input as bytes or text

This commit is contained in:
Crispy 2024-10-07 17:29:56 +02:00
parent 34fb5f1dd2
commit e9b03b937b
2 changed files with 51 additions and 17 deletions

View file

@ -435,18 +435,48 @@ 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 self.input_as_text {
let mut input_text = String::from_utf8_lossy(self.machine.input()).to_string();
if text_input(
d,
Rectangle::new(width as f32 - 205., 5., 200., 30.),
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);
}
}
}
}
fn draw_bottom_bar(&mut self, d: &mut RaylibDrawHandle, textures: &Textures) {
@ -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,11 +704,11 @@ impl Editor {
Tool::SelectArea(_, _) => (),
}
}
if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT) {
if self.active_tool == Tool::Erase {
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) {
if *is_selecting {

View file

@ -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
}