|
1 | 1 | use crate::PIXEL_SCALE; |
2 | 2 | use crate::circle::Circle; |
3 | 3 | use crate::world_data::{FullIndex, VoxelWorld, World}; |
| 4 | +use crate::world_image::PixelLength; |
4 | 5 | use bevy::camera::{Camera, Camera2d}; |
5 | 6 | use bevy::input::ButtonInput; |
6 | | -use bevy::prelude::{GlobalTransform, KeyCode, MouseButton, Res, ResMut, Single, With}; |
| 7 | +use bevy::math::Vec3; |
| 8 | +use bevy::prelude::{ |
| 9 | + Commands, Component, Entity, GlobalTransform, KeyCode, MouseButton, Res, ResMut, Single, |
| 10 | + Transform, With, |
| 11 | +}; |
| 12 | +use bevy::sprite::{Anchor, Text2d}; |
| 13 | +use bevy::text::{FontSize, TextFont}; |
7 | 14 | use bevy::window::Window; |
8 | 15 | pub fn spawn_cells( |
9 | 16 | pointer: Res<ButtonInput<MouseButton>>, |
@@ -41,3 +48,49 @@ pub fn spawn_cells( |
41 | 48 | } |
42 | 49 | } |
43 | 50 | } |
| 51 | +#[derive(Component)] |
| 52 | +pub struct DiagnosticUi; |
| 53 | +pub fn pointer_diagnostic( |
| 54 | + mut commands: Commands, |
| 55 | + kb: Res<ButtonInput<KeyCode>>, |
| 56 | + window: Single<&Window>, |
| 57 | + camera_single: Single<(&Camera, &GlobalTransform), With<Camera2d>>, |
| 58 | + world: Res<World>, |
| 59 | + ui: Option<Single<(Entity, &mut Transform, &mut Text2d), With<DiagnosticUi>>>, |
| 60 | + pixel_length: Res<PixelLength>, |
| 61 | +) { |
| 62 | + let (camera, camera_transform) = camera_single.into_inner(); |
| 63 | + let shift = kb.pressed(KeyCode::ShiftLeft); |
| 64 | + let ctrl = kb.pressed(KeyCode::ControlLeft); |
| 65 | + if (shift || ctrl) |
| 66 | + && let Some(pos) = window |
| 67 | + .cursor_position() |
| 68 | + .and_then(|cursor| camera.viewport_to_world_2d(camera_transform, cursor).ok()) |
| 69 | + { |
| 70 | + let px = (pos.x / PIXEL_SCALE).floor() as u16; |
| 71 | + let py = (pos.y / PIXEL_SCALE).floor() as u16; |
| 72 | + let index = FullIndex::from((px, py)); |
| 73 | + let new = Text2d(if ctrl && let Some(cell) = world.get(index) { |
| 74 | + format!("{} {index}", cell.cell_data.name) |
| 75 | + } else { |
| 76 | + format!("{index}") |
| 77 | + }); |
| 78 | + let scale = PIXEL_SCALE / (**pixel_length) as f32; |
| 79 | + let mut transform = Transform::from_xyz(pos.x + 10.0 * scale, pos.y - 10.0 * scale, 0.0); |
| 80 | + transform.scale = Vec3::splat(scale); |
| 81 | + if let Some((_, mut trans, mut text)) = ui.map(Single::into_inner) { |
| 82 | + *trans = transform; |
| 83 | + *text = new; |
| 84 | + } else { |
| 85 | + commands.spawn(( |
| 86 | + transform, |
| 87 | + new, |
| 88 | + TextFont::from_font_size(FontSize::Px(16.0)), |
| 89 | + Anchor::TOP_LEFT, |
| 90 | + DiagnosticUi, |
| 91 | + )); |
| 92 | + } |
| 93 | + } else if let Some((ent, _, _)) = ui.map(Single::into_inner) { |
| 94 | + commands.entity(ent).despawn(); |
| 95 | + } |
| 96 | +} |
0 commit comments