use std::{mem::transmute, ops::Rem}; use raylib::prelude::*; use crate::{ level::Level, marble_engine::{ board::{Board, Pos}, tile::{Direction, GateType, MathOp, MirrorType, PTile, Tile, WireType}, Machine, }, simple_button, solution::{Score, Solution}, text_input, texture_option_button, Textures, }; const HEADER_HEIGHT: i32 = 40; const FOOTER_HEIGHT: i32 = 95; #[derive(Debug)] pub struct Editor { source_board: Board, level: Level, machine: Machine, sim_state: SimState, view_offset: Vector2, zoom: i32, output_as_text: bool, input_as_text: bool, active_tool: Tool, tool_menu_math: MathOp, tool_menu_gate: GateType, tool_menu_arrow: Direction, tool_menu_mirror: MirrorType, tool_menu_wire: WireType, input_text_selected: bool, sim_speed: f32, time_since_step: f32, exit_state: ExitState, exit_menu: bool, complete_popup: Popup, fail_popup: Popup, score: Option, } #[derive(Debug, PartialEq)] enum Popup { Start, Visible, Dismissed, } #[derive(Debug, Clone)] enum Tool { None, SetTile(Tile), Digits(Option), Math, Gate, Wire, Arrow, Mirror, // SelectArea, } #[derive(Debug, Clone, PartialEq)] enum SimState { Editing, Running, Stepping, } #[derive(Debug, Clone, Copy)] pub enum ExitState { Dont, ExitAndSave, Save, ExitNoSave, } impl Editor { pub fn new(solution: Solution, level: Level) -> Self { Self { source_board: Board::parse(&solution.board), machine: Machine::new_empty(level.inputs().to_owned(), 1), sim_state: SimState::Editing, view_offset: Vector2::zero(), zoom: 1, active_tool: Tool::None, output_as_text: false, input_as_text: false, input_text_selected: false, sim_speed: 8., time_since_step: 0., tool_menu_math: MathOp::Add, tool_menu_gate: GateType::Equal, tool_menu_arrow: Direction::Right, tool_menu_mirror: MirrorType::Forward, tool_menu_wire: WireType::Vertical, level, exit_state: ExitState::Dont, exit_menu: false, complete_popup: Popup::Start, fail_popup: Popup::Start, score: solution.score, } } pub fn get_exit_state(&self) -> ExitState { self.exit_state } pub fn level_id(&self) -> &str { self.level.id() } pub fn source_board(&self) -> &Board { &self.source_board } pub fn score(&self) -> Option { self.score.clone() } fn start_sim(&mut self) { self.machine.reset(); self.machine.set_board(self.source_board.clone()); } fn step(&mut self) { self.machine.step(); if self.level.outputs() == self.machine.output() && self.complete_popup == Popup::Start { self.complete_popup = Popup::Visible; self.exit_state = ExitState::Save; self.sim_state = SimState::Stepping; self.score = Some(Score { cycles: self.machine.step_count(), tiles: self.source_board.count_tiles(), area: 0, }); } } fn rotate_tool(&mut self, shift: bool) { match &self.active_tool { Tool::Math => { self.tool_menu_math = match self.tool_menu_math { MathOp::Add => MathOp::Sub, MathOp::Sub => MathOp::Mul, MathOp::Mul => MathOp::Div, MathOp::Div => MathOp::Rem, MathOp::Rem => MathOp::Add, } } Tool::Gate => { self.tool_menu_gate = match self.tool_menu_gate { GateType::LessThan => GateType::GreaterThan, GateType::GreaterThan => GateType::Equal, GateType::Equal => GateType::NotEqual, GateType::NotEqual => GateType::LessThan, } } Tool::Arrow => { self.tool_menu_arrow = if shift { self.tool_menu_arrow.left() } else { self.tool_menu_arrow.right() } } Tool::Mirror => { self.tool_menu_mirror = match self.tool_menu_mirror { MirrorType::Forward => MirrorType::Back, MirrorType::Back => MirrorType::Forward, } } Tool::Wire => { self.tool_menu_wire = match self.tool_menu_wire { WireType::Vertical => WireType::Horizontal, WireType::Horizontal => WireType::Cross, WireType::Cross => WireType::Vertical, } } Tool::None => (), Tool::SetTile(_) => (), Tool::Digits(_) => (), } } fn set_tile(&mut self, pos: Pos, tile: Tile) { self.source_board.grow_to_include(pos); self.source_board.set(pos, tile); } pub fn update(&mut self, rl: &RaylibHandle) { if rl.is_key_pressed(KeyboardKey::KEY_ESCAPE) { self.sim_state = SimState::Editing; self.complete_popup = Popup::Start; } if self.sim_state == SimState::Running { self.time_since_step += rl.get_frame_time(); if self.time_since_step > 1. / self.sim_speed { self.time_since_step = 0.; self.step(); } } if rl.is_key_pressed(KeyboardKey::KEY_SPACE) { match self.sim_state { SimState::Editing => { self.start_sim(); self.step(); } SimState::Running => (), SimState::Stepping => self.step(), } self.sim_state = SimState::Stepping; } if rl.is_key_pressed(KeyboardKey::KEY_ENTER) { match self.sim_state { SimState::Editing => { self.start_sim(); self.sim_state = SimState::Running; } SimState::Running => { self.sim_state = SimState::Editing; self.complete_popup = Popup::Start; } SimState::Stepping => self.sim_state = SimState::Running, } } if rl.get_mouse_wheel_move() > 0. && self.zoom < 3 { self.zoom += 1; } if rl.get_mouse_wheel_move() < 0. && self.zoom > 0 { self.zoom -= 1; } if rl.is_mouse_button_down(MouseButton::MOUSE_BUTTON_MIDDLE) { self.view_offset += rl.get_mouse_delta() } if rl.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_RIGHT) { self.view_offset = Vector2::zero(); } if rl.is_key_pressed(KeyboardKey::KEY_R) { self.rotate_tool(rl.is_key_down(KeyboardKey::KEY_LEFT_SHIFT)); } } fn draw_board(&self, d: &mut RaylibDrawHandle, textures: &Textures) { if self.sim_state == SimState::Editing { self.source_board .draw(d, textures, self.view_offset, self.zoom); } else { self.machine .board() .draw(d, textures, self.view_offset, self.zoom); self.machine .draw_marble_values(d, 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, ); self.draw_board(d, textures); self.board_overlay(d, textures); self.draw_bottom_bar(d, textures); self.draw_top_bar(d, textures); if self.complete_popup == Popup::Visible { let width = 310; let height = 165; let x = d.get_screen_width() / 2 - width / 2; let y = d.get_screen_height() / 2 - height / 2; d.draw_rectangle(x, y, width, height, Color::DIMGRAY); d.draw_text("Level Complete!", x + 10, y + 10, 30, Color::LIME); if let Some(score) = &self.score { d.draw_text("cycles", x + 15, y + 45, 20, Color::WHITE); d.draw_rectangle(x + 10, y + 70, 90, 30, Color::DARKGREEN); d.draw_text( &format!("{}", score.cycles), x + 15, y + 75, 20, Color::WHITE, ); d.draw_text("tiles", x + 115, y + 45, 20, Color::WHITE); d.draw_rectangle(x + 110, y + 70, 90, 30, Color::DARKGREEN); d.draw_text( &format!("{}", score.tiles), x + 115, y + 75, 20, Color::WHITE, ); d.draw_text("area", x + 215, y + 45, 20, Color::WHITE); d.draw_rectangle(x + 210, y + 70, 90, 30, Color::DARKGREEN); d.draw_text( &format!("{}", score.area), x + 215, y + 75, 20, Color::WHITE, ); } if simple_button(d, x + 10, y + 110, 140, 45) { self.complete_popup = Popup::Dismissed; } d.draw_text("continue\nediting", x + 15, y + 115, 20, Color::WHITE); if simple_button(d, x + width / 2 + 5, y + 110, 140, 45) { self.exit_state = ExitState::ExitAndSave; } d.draw_text( "return to\nlevel list", x + width / 2 + 10, y + 115, 20, Color::WHITE, ); } } fn draw_top_bar(&mut self, d: &mut RaylibDrawHandle, _textures: &Textures) { // background d.draw_rectangle( 0, 0, d.get_screen_width(), HEADER_HEIGHT, Color::new(32, 32, 32, 255), ); if self.exit_menu { if simple_button(d, 5, 5, 80, 30) { self.exit_menu = false; } d.draw_text("cancel", 10, 10, 20, Color::WHITE); if simple_button(d, 90, 5, 80, 30) { self.exit_state = ExitState::ExitAndSave; } d.draw_text("save", 95, 10, 20, Color::WHITE); if simple_button(d, 175, 5, 80, 30) { self.exit_state = ExitState::ExitNoSave; } d.draw_text("revert", 180, 10, 20, Color::WHITE); } else { if simple_button(d, 5, 5, 80, 30) { self.exit_menu = true; } d.draw_text("exit", 10, 10, 20, Color::WHITE); if simple_button(d, 90, 5, 80, 30) { self.exit_state = ExitState::Save; } d.draw_text("save", 95, 10, 20, Color::WHITE); } let mut input_text = String::from_utf8_lossy(self.machine.input()).to_string(); 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.machine.set_input(input_text.into_bytes()); } } fn draw_bottom_bar(&mut self, d: &mut RaylibDrawHandle, textures: &Textures) { let height = d.get_screen_height(); let footer_top = (height - FOOTER_HEIGHT) as f32; // background d.draw_rectangle( 0, height - FOOTER_HEIGHT, d.get_screen_width(), FOOTER_HEIGHT, Color::new(32, 32, 32, 255), ); let mut tool_button = |(row, col): (i32, i32), texture: &str, tool_option: Tool| { let border = 4.; let gap = 2.; let bound_offset = 32. + gap * 2. + border * 2.; texture_option_button( d, Vector2 { x: 100. + col as f32 * bound_offset - if col < 0 { 10. } else { 0. }, y: footer_top + 5. + row as f32 * bound_offset, }, textures.get(texture), tool_option, &mut self.active_tool, 32., border, ); }; tool_button((0, -2), "eraser", Tool::SetTile(Tile::from_char(' '))); tool_button((0, -1), "digit_tool", Tool::Digits(None)); tool_button((1, -1), "transparent", Tool::None); tool_button((0, 0), "block", Tool::SetTile(Tile::from_char('#'))); tool_button((0, 1), "bag_off", Tool::SetTile(Tile::from_char('B'))); tool_button((0, 2), "trigger_off", Tool::SetTile(Tile::from_char('*'))); tool_button((0, 3), "input_off", Tool::SetTile(Tile::from_char('I'))); tool_button((0, 4), "output_off", Tool::SetTile(Tile::from_char('P'))); tool_button((0, 5), "flipper_off", Tool::SetTile(Tile::from_char('F'))); tool_button((1, 0), "marble", Tool::SetTile(Tile::from_char('o'))); tool_button( (1, 1), &Tile::Powerable(PTile::Wire(self.tool_menu_wire), false).texture(), Tool::Wire, ); tool_button( (1, 2), &Tile::Arrow(self.tool_menu_arrow).texture(), Tool::Arrow, ); tool_button( (1, 3), &Tile::Mirror(self.tool_menu_mirror).texture(), Tool::Mirror, ); tool_button( (1, 4), &Tile::Powerable(PTile::Math(self.tool_menu_math), false).texture(), Tool::Math, ); tool_button( (1, 5), &Tile::Powerable(PTile::Gate(self.tool_menu_gate), false).texture(), Tool::Gate, ); let output_x = 370; let output_cell_width = 43; let output_cells = (d.get_screen_width() - output_x) as usize / 43; let y = footer_top as i32 + 5; if simple_button(d, output_x, y + 70, 65, 15) { self.output_as_text = !self.output_as_text } let output_mode_text = if self.output_as_text { "show bytes" } else { "show text" }; d.draw_text(output_mode_text, output_x + 5, y + 72, 10, Color::WHITE); let output_start = self.machine.output().len().saturating_sub(output_cells); let output_end = output_start + output_cells; for (box_index, index) in (output_start..output_end).enumerate() { let x = output_x + output_cell_width * box_index as i32; let expected_byte = self.level.outputs().get(index); let real_byte = self.machine.output().get(index); let (top_color, bottom_color) = if let (Some(&real_byte), Some(&expected_byte)) = (real_byte, expected_byte) { if expected_byte == real_byte { (Color::GREEN, Color::DARKGREEN) } else { (Color::RED, Color::DARKRED) } } else { (Color::DARKGRAY, Color::DIMGRAY) }; d.draw_rectangle(x, y, 38, 30, top_color); d.draw_rectangle(x, y + 35, 38, 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()) { format!("{:?}", expected_byte as char) } else { format!("{}", expected_byte) }; d.draw_text(&top_text, x + 2, y + 5, 20, Color::WHITE); } if let Some(&real_byte) = real_byte { let bottom_text = if self.output_as_text && (real_byte.is_ascii_graphic() || real_byte.is_ascii_whitespace()) { format!("{:?}", real_byte as char) } else { format!("{}", real_byte) }; d.draw_text(&bottom_text, x + 2, y + 40, 20, Color::WHITE); } } } 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(); if self.sim_state == SimState::Editing { if let Tool::Digits(Some(pos)) = &mut self.active_tool { let tile_screen_pos = pos.to_vec() * (16 << self.zoom) as f32 + self.view_offset; d.draw_texture_ex( textures.get("selection"), tile_screen_pos, 0., (1 << self.zoom) as f32, Color::new(255, 180, 20, 255), ); if d.is_key_pressed(KeyboardKey::KEY_LEFT) { pos.x -= 1; } if d.is_key_pressed(KeyboardKey::KEY_RIGHT) { pos.x += 1; } if d.is_key_pressed(KeyboardKey::KEY_UP) { pos.y -= 1; } if d.is_key_pressed(KeyboardKey::KEY_DOWN) { pos.y += 1; } let pos = *pos; for n in 0..10 { if d.is_key_pressed(unsafe { transmute::(b'0' as u32 + n) }) { self.set_tile(pos, Tile::Digit(n as u8)); } } } if mouse_pos.y < footer_top && mouse_pos.y > HEADER_HEIGHT as f32 { let tile_pos = (mouse_pos - self.view_offset) / (16 << self.zoom) as f32; let tile_pos = Vector2::new(tile_pos.x.floor(), tile_pos.y.floor()); let tile_screen_pos = tile_pos * (16 << self.zoom) as f32 + self.view_offset; if self.active_tool != Tool::None { let tex = match self.active_tool { Tool::None => unreachable!(), Tool::SetTile(t) => { if t == Tile::Blank { "selection".into() } else { t.texture() } } Tool::Math => format!("{}_off", self.tool_menu_math.texture_name()), Tool::Gate => format!("{}_off", self.tool_menu_gate.texture_name()), Tool::Wire => format!("{}_off", self.tool_menu_wire.texture_name()), Tool::Arrow => self.tool_menu_arrow.arrow_texture_name().into(), Tool::Mirror => self.tool_menu_mirror.texture_name().into(), Tool::Digits(_) => "selection".into(), }; d.draw_texture_ex( textures.get(&tex), tile_screen_pos, 0., (1 << self.zoom) as f32, Color::new(255, 255, 255, 100), ); } if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT) { match self.active_tool { Tool::None => (), Tool::SetTile(tile) => self.set_tile(tile_pos.into(), tile), Tool::Math => self.set_tile( tile_pos.into(), Tile::Powerable(PTile::Math(self.tool_menu_math), false), ), Tool::Gate => self.set_tile( tile_pos.into(), Tile::Powerable(PTile::Gate(self.tool_menu_gate), false), ), Tool::Wire => self.set_tile( tile_pos.into(), Tile::Powerable(PTile::Wire(self.tool_menu_wire), false), ), Tool::Arrow => { self.set_tile(tile_pos.into(), Tile::Arrow(self.tool_menu_arrow)) } Tool::Mirror => { self.set_tile(tile_pos.into(), Tile::Mirror(self.tool_menu_mirror)) } Tool::Digits(_pos) => { self.active_tool = Tool::Digits(Some(tile_pos.into())); if let Some(tile) = self.source_board.get_mut(tile_pos.into()) { if let Tile::Digit(_) = tile { } else { *tile = Tile::Digit(0); } } } } } } } } } impl PartialEq for Tool { fn eq(&self, other: &Self) -> bool { match (self, other) { (Self::SetTile(l0), Self::SetTile(r0)) => l0 == r0, (Self::Digits(_), Self::Digits(_)) => true, _ => ::core::mem::discriminant(self) == ::core::mem::discriminant(other), } } }