Skip to content

Commit a2dfc8d

Browse files
committed
add cursor index/cell string text
1 parent fce3cea commit a2dfc8d

5 files changed

Lines changed: 95 additions & 247 deletions

File tree

src/app.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use crate::APP_NAME;
22
use crate::camera::{move_camera, zoom_camera};
33
use crate::plugin::WorldPlugin;
4-
use crate::pointer::spawn_cells;
4+
use crate::pointer::{pointer_diagnostic, spawn_cells};
55
use crate::startup::startup;
66
use avian2d::PhysicsPlugins;
77
use bevy::DefaultPlugins;
88
use bevy::app::{
99
App, AppExit, FixedUpdate, PluginGroup as _, Startup, TaskPoolOptions, TaskPoolPlugin,
10-
TaskPoolThreadAssignmentPolicy,
10+
TaskPoolThreadAssignmentPolicy, Update,
1111
};
1212
use bevy::asset::{AssetMetaCheck, AssetPlugin};
1313
#[cfg(feature = "colliders")]
@@ -80,6 +80,7 @@ pub fn app_run() -> AppExit {
8080
bevy::gizmos::config::GizmoConfig::default(),
8181
);
8282
app.add_systems(Startup, startup);
83+
app.add_systems(Update, pointer_diagnostic);
8384
app.add_systems(FixedUpdate, (spawn_cells, move_camera, zoom_camera));
8485
app.run()
8586
}

src/pointer.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
use crate::PIXEL_SCALE;
22
use crate::circle::Circle;
33
use crate::world_data::{FullIndex, VoxelWorld, World};
4+
use crate::world_image::PixelLength;
45
use bevy::camera::{Camera, Camera2d};
56
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};
714
use bevy::window::Window;
815
pub fn spawn_cells(
916
pointer: Res<ButtonInput<MouseButton>>,
@@ -41,3 +48,49 @@ pub fn spawn_cells(
4148
}
4249
}
4350
}
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+
}

src/world/chunk.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::ops::{Index, IndexMut};
99
pub struct Chunk {
1010
pub cells: Box<Matrix<Cell>>,
1111
pub skip_simulation: bool,
12-
pub skip_edge_simulation: bool,
1312
}
1413
#[derive(Clone)]
1514
pub struct VoxelChunk {
@@ -41,7 +40,6 @@ impl Chunk {
4140
Self {
4241
cells: cells.assume_init(),
4342
skip_simulation: false,
44-
skip_edge_simulation: false,
4543
},
4644
VoxelChunk {
4745
voxels_modified: true,

0 commit comments

Comments
 (0)