basic tile drawing

This commit is contained in:
Crispy 2024-10-05 15:34:58 +02:00
parent 1257388168
commit a0f11b60fb
3 changed files with 43 additions and 5 deletions

BIN
assets/selection.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 B

View file

@ -41,13 +41,18 @@ enum SimState {
Stepping,
}
fn load_textures_from(folder: &str, rl: &mut RaylibHandle, thread:&RaylibThread,textures:&mut HashMap<String,Texture2D>){
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"))
.load_texture(thread, &format!("{folder}/{name}.png"))
.unwrap();
textures.insert(name.to_string(), texture);
}
@ -233,8 +238,8 @@ impl Game {
border,
);
};
tool_button((0, -1), "", Tool::None);
tool_button((1, -1), "eraser", Tool::SetTile(tile_to_char(' ')));
tool_button((0, -1), "eraser", Tool::SetTile(tile_to_char(' ')));
tool_button((1, -1), "", Tool::None);
tool_button((0, 0), "block", Tool::SetTile(tile_to_char('#')));
tool_button((0, 1), "bag_off", Tool::SetTile(tile_to_char('B')));
@ -251,5 +256,27 @@ impl Game {
Tool::SetTile(tile_to_char('|')),
);
tool_button((1, 2), "wire_cross_off", Tool::SetTile(tile_to_char('+')));
let mouse_pos = d.get_mouse_position();
if self.sim_state == SimState::Editing && mouse_pos.y < footer_top {
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 let Tool::SetTile(tile) = self.active_tool {
d.draw_texture_ex(
textures.get("selection").unwrap(),
tile_screen_pos,
0.,
(1 << self.zoom) as f32,
Color::new(255, 255, 255, 150),
);
if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT) {
self.source_board.set(tile_pos.into(), tile)
}
}
}
}
}

View file

@ -18,6 +18,15 @@ impl From<(usize, usize)> for Pos {
}
}
impl From<Vector2> for Pos {
fn from(vec: Vector2) -> Self {
Self {
x: vec.x as isize,
y: vec.y as isize,
}
}
}
#[derive(Debug, Clone)]
pub struct Board {
rows: Vec<Vec<Tile>>,
@ -101,7 +110,9 @@ impl Board {
if let Some(tile) = self.get((x, y).into()) {
let px = x as i32 * tile_size + offset.x as i32 + tile_size / 2;
let py = y as i32 * tile_size + offset.y as i32 + tile_size / 2;
let texture = textures.get(&tile.texture()).unwrap_or_else(||textures.get("missing").unwrap());
let texture = textures
.get(&tile.texture())
.unwrap_or_else(|| textures.get("missing").unwrap());
d.draw_texture_ex(
texture,
Vector2::new((px - tile_size / 2) as f32, (py - tile_size / 2) as f32),