toolbar gui parts
This commit is contained in:
parent
5a23dde43a
commit
1257388168
3 changed files with 94 additions and 11 deletions
BIN
assets/eraser.png
Normal file
BIN
assets/eraser.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 248 B |
61
src/main.rs
61
src/main.rs
|
@ -41,6 +41,19 @@ enum SimState {
|
|||
Stepping,
|
||||
}
|
||||
|
||||
fn load_textures_from(folder: &str, rl: &mut RaylibHandle, thread:&RaylibThread,textures:&mut HashMap<String,Texture2D>){
|
||||
for d in read_dir(folder).unwrap().flatten() {
|
||||
let path = d.path();
|
||||
if path.is_file() {
|
||||
let name = path.file_stem().unwrap().to_string_lossy();
|
||||
let texture = rl
|
||||
.load_texture(&thread, &format!("{folder}/{name}.png"))
|
||||
.unwrap();
|
||||
textures.insert(name.to_string(), texture);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let (mut rl, thread) = raylib::init()
|
||||
.resizable()
|
||||
|
@ -50,16 +63,9 @@ fn main() {
|
|||
rl.set_exit_key(None);
|
||||
|
||||
let mut textures: HashMap<String, Texture2D> = HashMap::new();
|
||||
for d in read_dir("assets/tiles").unwrap().flatten() {
|
||||
let path = d.path();
|
||||
if path.is_file() {
|
||||
let name = path.file_stem().unwrap().to_string_lossy();
|
||||
let texture = rl
|
||||
.load_texture(&thread, &format!("assets/tiles/{name}.png"))
|
||||
.unwrap();
|
||||
textures.insert(name.to_string(), texture);
|
||||
}
|
||||
}
|
||||
load_textures_from("assets", &mut rl, &thread, &mut textures);
|
||||
load_textures_from("assets/tiles", &mut rl, &thread, &mut textures);
|
||||
|
||||
let mut game = Game::new_sandbox();
|
||||
let board = parse(&read_to_string("boards/adder.mbl").unwrap());
|
||||
game.load_board(board);
|
||||
|
@ -210,5 +216,40 @@ impl Game {
|
|||
self.machine.set_input(input_text.into_bytes());
|
||||
}
|
||||
|
||||
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_button(
|
||||
d,
|
||||
Vector2 {
|
||||
x: 300. + col as f32 * bound_offset,
|
||||
y: footer_top + 5. + row as f32 * bound_offset,
|
||||
},
|
||||
textures.get(texture),
|
||||
tool_option,
|
||||
&mut self.active_tool,
|
||||
32.,
|
||||
border,
|
||||
);
|
||||
};
|
||||
tool_button((0, -1), "", Tool::None);
|
||||
tool_button((1, -1), "eraser", Tool::SetTile(tile_to_char(' ')));
|
||||
|
||||
tool_button((0, 0), "block", Tool::SetTile(tile_to_char('#')));
|
||||
tool_button((0, 1), "bag_off", Tool::SetTile(tile_to_char('B')));
|
||||
tool_button((0, 2), "trigger_off", Tool::SetTile(tile_to_char('*')));
|
||||
|
||||
tool_button(
|
||||
(1, 0),
|
||||
"wire_horizontal_off",
|
||||
Tool::SetTile(tile_to_char('-')),
|
||||
);
|
||||
tool_button(
|
||||
(1, 1),
|
||||
"wire_vertical_off",
|
||||
Tool::SetTile(tile_to_char('|')),
|
||||
);
|
||||
tool_button((1, 2), "wire_cross_off", Tool::SetTile(tile_to_char('+')));
|
||||
}
|
||||
}
|
||||
|
|
44
src/util.rs
44
src/util.rs
|
@ -18,7 +18,7 @@ pub fn text_input(
|
|||
text,
|
||||
bounds.x as i32 + 4,
|
||||
bounds.y as i32 + 4,
|
||||
10,
|
||||
20,
|
||||
Color::WHITE,
|
||||
);
|
||||
let mouse_pos = d.get_mouse_position();
|
||||
|
@ -50,6 +50,48 @@ pub fn text_input(
|
|||
changed
|
||||
}
|
||||
|
||||
pub fn texture_button<T>(
|
||||
d: &mut RaylibDrawHandle,
|
||||
pos: Vector2,
|
||||
texture: Option<&Texture2D>,
|
||||
option: T,
|
||||
current: &mut T,
|
||||
tex_size: f32,
|
||||
border: f32,
|
||||
// tooltip
|
||||
) where
|
||||
T: PartialEq,
|
||||
{
|
||||
let color = if &option == current {
|
||||
Color::DARKCYAN
|
||||
} else {
|
||||
Color::GRAY
|
||||
};
|
||||
let bounds = Rectangle {
|
||||
x: pos.x,
|
||||
y: pos.y,
|
||||
width: tex_size + border * 2.,
|
||||
height: tex_size + border * 2.,
|
||||
};
|
||||
d.draw_rectangle_rec(bounds, color);
|
||||
if let Some(texture) = texture {
|
||||
d.draw_texture_ex(
|
||||
texture,
|
||||
pos + Vector2::new(border, border),
|
||||
0.,
|
||||
tex_size / texture.width as f32,
|
||||
Color::WHITE,
|
||||
);
|
||||
}
|
||||
|
||||
let mouse_pos = d.get_mouse_position();
|
||||
if d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT)
|
||||
&& bounds.check_collision_point_rec(mouse_pos)
|
||||
{
|
||||
*current = option;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn shrink_rec(rec: Rectangle, a: f32) -> Rectangle {
|
||||
Rectangle {
|
||||
x: rec.x + a,
|
||||
|
|
Loading…
Reference in a new issue