From 30ef27e06fa2349098ea126b255e5e51872d324f Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Tue, 15 Aug 2023 20:09:13 +0200 Subject: [PATCH] Added block-hover highlight to show which block is being focused right now --- src/screens/build.rs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/screens/build.rs b/src/screens/build.rs index e3d4cf7..691a235 100644 --- a/src/screens/build.rs +++ b/src/screens/build.rs @@ -250,6 +250,37 @@ impl GameComponent for BuildScreen { let mut render_order: Vec<(&Pos3, &(Block, Direction))> = world.grid.iter().collect(); render_order.sort_by(|(p1, _), (p2, _)| p1.cmp(p2)); + + let (mx, my) = mouse_position(); + + // list of positions in render que for given pixel + // (mx, my) + let mut in_path: Vec<&Pos3> = Vec::new(); + + for (pos, _) in &render_order { + let (x, y) = get_screen_coords(pos, world.cam.scale, world.cam.center); + + if mx >= x + && mx <= TEXTURE_WIDTH.mul_add(world.cam.scale, x) + && my >= TEXTURE_Y_WHITESPACE.mul_add(world.cam.scale, y) + && my <= TEXTURE_HEIGHT.mul_add(world.cam.scale, y) + { + // check if mouse is above transparent area + // and skip block if necessary + if Face::from_xy(mx - x, my - y, world.cam.scale).is_none() { + continue; + } + + // block in mouse path + in_path.push(pos); + } + } + // weight axis + //in_path.sort_by(|p1, p2| p1.cmp(p2)); + let focused = in_path.last(); + + + // draw world for (pos, (block, dir)) in &render_order { let texture = block.get_texture().await.get_dir(dir); let width = TEXTURE_WIDTH * world.cam.scale; @@ -258,12 +289,18 @@ impl GameComponent for BuildScreen { let (x, y) = get_screen_coords(pos, world.cam.scale, world.cam.center); if x >= -width && x <= screen_width() && y >= -height && y <= screen_height() { + let mut color = WHITE; + if let Some(focused_block_pos) = focused { + if focused_block_pos == pos { + color = LIGHTGRAY; + } + } // render block draw_texture_ex( texture, x, y, - WHITE, + color, DrawTextureParams { dest_size: Some(Vec2::new(width, height)), source: None, -- 2.38.5