use std::{ collections::HashMap, fs::{read_dir, read_to_string}, ops::Rem, }; use marble_engine::parse; use raylib::prelude::*; mod marble_engine; mod util; use util::*; #[derive(Debug, Clone, PartialEq)] enum EditorState { Editing, Playing, Stepping, } fn main() { let (mut rl, thread) = raylib::init() .resizable() .title("good window title") .build(); rl.set_target_fps(60); let board = parse(&read_to_string("boards/adder.mbl").unwrap()); let mut pos_offset = Vector2::zero(); let mut machine = marble_engine::Machine::new(board, "Vec::new()".bytes().collect()); let mut editor_state = EditorState::Editing; let mut output_as_text = false; let mut input_as_text = true; let mut input_text_selected = false; let mut zoom = 1; let mut textures: HashMap = HashMap::new(); for d in read_dir("assets/tiles").unwrap().flatten() { let name = d.file_name(); if d.path().is_file() { let name = name.to_string_lossy(); let texture = rl .load_texture(&thread, &format!("assets/tiles/{name}")) .unwrap(); textures.insert(name.to_string(), texture); } } while !rl.window_should_close() { if rl.is_key_pressed(KeyboardKey::KEY_SPACE) || rl.is_key_down(KeyboardKey::KEY_ENTER) { machine.step(); } if rl.get_mouse_wheel_move() > 0. && zoom < 3 { zoom += 1; } if rl.get_mouse_wheel_move() < 0. && zoom > 0 { zoom -= 1; } if rl.is_mouse_button_down(MouseButton::MOUSE_BUTTON_MIDDLE) { pos_offset += rl.get_mouse_delta() } if rl.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_RIGHT) { pos_offset = Vector2::zero(); } let mut d = rl.begin_drawing(&thread); d.clear_background(Color::new(64, 64, 64, 255)); machine.draw(&mut d, &textures, pos_offset, zoom); // UI // d.draw_rectangle(x, y, width, height, color); let height = d.get_screen_height(); let footer_height = 100; let footer_top = (height - footer_height) as f32; d.draw_rectangle( 0, height - footer_height, d.get_screen_width(), footer_height, Color::new(32, 32, 32, 255), ); let tile_size = (16 << zoom) as f32; let grid_spill_x = (pos_offset.x).rem(tile_size) - tile_size; let grid_spill_y = (pos_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., height as f32 - grid_spill_y - footer_height as f32, ), None, tile_size, 1, ); d.gui_check_box( Rectangle::new(5., footer_top + 5., 25., 25.), Some(rstr!("output as text")), &mut output_as_text, ); let out_text = if output_as_text { String::from_utf8_lossy(machine.output()).to_string() } else { format!("{:?}", machine.output()) }; d.draw_text(&out_text, 5, footer_top as i32 + 35, 20, Color::WHITE); let mut input_text = String::from_utf8_lossy(machine.input()).to_string(); if text_input( &mut d, Rectangle::new(350., footer_top + 60., 200., 25.), &mut input_text, &mut input_text_selected, ) { machine.set_input(input_text.into_bytes()); } if d.gui_button( Rectangle::new(0., height as f32 - 40., 100.0, 30.0), Some(rstr!("meow")), ) { println!("meow"); } d.draw_fps(2, 2); } }