marble-machinations/src/main.rs

132 lines
3.1 KiB
Rust
Raw Normal View History

2024-10-04 01:21:52 +02:00
use std::{
collections::HashMap,
fs::{read_dir, read_to_string},
2024-10-04 21:20:53 +02:00
ops::Rem,
2024-10-04 01:21:52 +02:00
};
2024-10-03 22:59:49 +02:00
use marble_engine::parse;
use raylib::prelude::*;
mod marble_engine;
2024-10-04 21:20:53 +02:00
mod util;
use util::*;
#[derive(Debug, Clone, PartialEq)]
enum EditorState {
Editing,
Playing,
Stepping,
}
2024-10-03 22:59:49 +02:00
fn main() {
2024-10-04 01:21:52 +02:00
let (mut rl, thread) = raylib::init()
.resizable()
.title("good window title")
.build();
2024-10-03 22:59:49 +02:00
rl.set_target_fps(60);
2024-10-04 10:00:07 +02:00
let board = parse(&read_to_string("boards/adder.mbl").unwrap());
2024-10-03 22:59:49 +02:00
let mut pos_offset = Vector2::zero();
let mut machine = marble_engine::Machine::new(board, "Vec::new()".bytes().collect());
2024-10-04 21:20:53 +02:00
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;
2024-10-03 22:59:49 +02:00
2024-10-04 01:21:52 +02:00
let mut textures: HashMap<String, Texture2D> = 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);
}
}
2024-10-03 22:59:49 +02:00
while !rl.window_should_close() {
2024-10-04 21:20:53 +02:00
if rl.is_key_pressed(KeyboardKey::KEY_SPACE) || rl.is_key_down(KeyboardKey::KEY_ENTER) {
2024-10-03 22:59:49 +02:00
machine.step();
}
2024-10-04 21:20:53 +02:00
if rl.get_mouse_wheel_move() > 0. && zoom < 3 {
zoom += 1;
}
if rl.get_mouse_wheel_move() < 0. && zoom > 0 {
zoom -= 1;
}
2024-10-03 22:59:49 +02:00
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));
2024-10-04 21:20:53 +02:00
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");
}
2024-10-03 22:59:49 +02:00
d.draw_fps(2, 2);
}
}