move images and z order shapes :D

This commit is contained in:
RustyStriker 2022-08-11 19:06:34 +03:00
parent 2a1614b004
commit a529133698
5 changed files with 54 additions and 15 deletions

View file

@ -14,13 +14,14 @@ Eventually, this will be a level editor... But until then, I guess it's just fun
- [x] Insert images - [x] Insert images
- [x] Show images in tree view - [x] Show images in tree view
- [x] Show/Hide images - [x] Show/Hide images
- [ ] Move/Drag images around - [x] Move/Drag images around
- [ ] Control images Z value(so we could reorder them) - [ ] Control images Z value(so we could reorder them)
- [x] Delete images - [x] Delete images
- [x] Name images - [x] Name images
- [ ] Duplicate images(and also shapes maybe?)? - [ ] Duplicate images(and also shapes maybe?)?
- [ ] Scale images
- [x] Show hide shapes - [x] Show hide shapes
- [ ] Control shape Z value - [x] Control shape Z value
- [x] Snap to grid - [x] Snap to grid
- [x] Change grid size - [x] Change grid size
- [ ] Show/Hide grid(also make a visible grid in the first place) - [ ] Show/Hide grid(also make a visible grid in the first place)

View file

@ -34,7 +34,7 @@ pub fn create_sys(
t.translation = center.extend(0.0); t.translation = center.extend(0.0);
} }
if let Ok(mut t) = transforms.get_mut(sd.center.unwrap()) { if let Ok(mut t) = transforms.get_mut(sd.center.unwrap()) {
t.translation = Vec3::new(0.0, 0.0, 1.0); // Just to make sure the center is centered t.translation = Vec3::new(0.0, 0.0, 0.5); // Just to make sure the center is centered
} }
for edge in sd.edges.iter() { for edge in sd.edges.iter() {
if let Ok(mut t) = transforms.get_mut(*edge) { if let Ok(mut t) = transforms.get_mut(*edge) {
@ -44,7 +44,7 @@ pub fn create_sys(
// If we dont have a transform in the query for the relevant entity, // If we dont have a transform in the query for the relevant entity,
// then it should be the entity we just inserted, and we can just override its transform // then it should be the entity we just inserted, and we can just override its transform
coms.entity(*edge) coms.entity(*edge)
.insert(Transform::from_translation((mouse_pos - center).extend(1.0))); .insert(Transform::from_translation((mouse_pos - center).extend(0.5)));
} }
} }
@ -163,7 +163,7 @@ fn add_center_point(
let np = coms.spawn_bundle(GeometryBuilder::build_as( let np = coms.spawn_bundle(GeometryBuilder::build_as(
&shape, &shape,
DrawMode::Fill(FillMode::color(Color::RED)), DrawMode::Fill(FillMode::color(Color::RED)),
Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)) Transform::from_translation(Vec3::new(0.0, 0.0, 0.5))
)) ))
.insert(Visibility { is_visible: false }) .insert(Visibility { is_visible: false })
.id(); .id();
@ -188,7 +188,7 @@ fn insert_edge_to_shape_creation(
let np = coms.spawn_bundle(GeometryBuilder::build_as( let np = coms.spawn_bundle(GeometryBuilder::build_as(
&shape, &shape,
DrawMode::Fill(FillMode::color(Color::RED)), DrawMode::Fill(FillMode::color(Color::RED)),
Transform::from_translation(mpos.extend(1.0)) Transform::from_translation(mpos.extend(0.5))
)) ))
.id(); .id();
@ -290,7 +290,7 @@ fn create_new_shape(coms: &mut Commands, pos: Vec2, create_shape: CreateShape, p
let fp = coms.spawn_bundle(GeometryBuilder::build_as( let fp = coms.spawn_bundle(GeometryBuilder::build_as(
&shape, &shape,
DrawMode::Fill(FillMode::color(Color::RED)), DrawMode::Fill(FillMode::color(Color::RED)),
Transform::from_translation(pos.extend(1.0)) Transform::from_translation(pos.extend(0.5))
)) ))
.id(); .id();
// Spawn main shape! // Spawn main shape!

View file

@ -42,6 +42,8 @@ pub struct PointSize(pub f32);
#[derive(Component, Debug, Deref, DerefMut)] #[derive(Component, Debug, Deref, DerefMut)]
pub struct ImageName(pub String); pub struct ImageName(pub String);
#[derive(Component, Debug, Deref, DerefMut)]
pub struct ImageSize(pub Vec2);
#[derive(Component, Debug)] #[derive(Component, Debug)]
pub struct ShapeData { pub struct ShapeData {

View file

@ -3,32 +3,44 @@ use bevy::prelude::*;
use crate::*; use crate::*;
use bevy_prototype_lyon::prelude::*; use bevy_prototype_lyon::prelude::*;
#[derive(Default, PartialEq)]
pub enum Holding {
/// (ShapeData, Path)
Shape(Entity, Entity),
/// (Sprite, Offset)
Image(Entity, Vec2),
#[default]
None,
}
pub fn modify_sys( pub fn modify_sys(
// Which entity the user currently drags, and which specific part of it(ShapeData entity, path entity) // Which entity the user currently drags, and which specific part of it(ShapeData entity, path entity)
mut holding: Local<Option<(Entity, Entity)>>, mut holding: Local<Holding>,
mouse: Res<Input<MouseButton>>, mouse: Res<Input<MouseButton>>,
wnds: Res<Windows>, wnds: Res<Windows>,
scale: Query<&OrthographicProjection, With<MainCamera>>, scale: Query<&OrthographicProjection, With<MainCamera>>,
p_size: Res<PointSize>, p_size: Res<PointSize>,
assets: Res<Assets<Image>>,
mut paths: Query<&mut Path>, mut paths: Query<&mut Path>,
mut transforms: Query<&mut Transform>, mut transforms: Query<&mut Transform>,
gtransforms: Query<&GlobalTransform>, gtransforms: Query<&GlobalTransform>,
q_cam: Query<(&Camera, &GlobalTransform), With<MainCamera>>, q_cam: Query<(&Camera, &GlobalTransform), With<MainCamera>>,
shapes: Query<(Entity, &ShapeData)>, shapes: Query<(Entity, &ShapeData)>,
images: Query<(Entity, &Handle<Image>)>,
snap: Res<SnapGrid>, snap: Res<SnapGrid>,
) { ) {
let scale = scale.get_single().map(|s| s.scale).unwrap_or(1.0); let scale = scale.get_single().map(|s| s.scale).unwrap_or(1.0);
let mouse_pos = get_mouse_pos(&q_cam, &wnds); let mouse_pos = get_mouse_pos(&q_cam, &wnds);
if let Some(mouse_pos) = mouse_pos { if let Some(mouse_pos) = mouse_pos {
if mouse.just_pressed(MouseButton::Left) && holding.is_none() { if mouse.just_pressed(MouseButton::Left) && *holding == Holding::None {
// Grab attempt - search if we are holding a shape or something // Grab attempt - search if we are holding a shape or something
for (e, sd) in shapes.iter() { for (e, sd) in shapes.iter() {
if let Ok(t) = gtransforms.get(sd.center.unwrap()) { if let Ok(t) = gtransforms.get(sd.center.unwrap()) {
let t = t.translation().xy(); let t = t.translation().xy();
if (mouse_pos - t).length_squared() < (p_size.0 * scale).powi(2) { if (mouse_pos - t).length_squared() < (p_size.0 * scale).powi(2) {
*holding = Some((e, sd.center.unwrap())); *holding = Holding::Shape(e, sd.center.unwrap());
break; break;
} }
} }
@ -37,17 +49,32 @@ pub fn modify_sys(
let t = t.translation().xy(); let t = t.translation().xy();
if (mouse_pos - t).length_squared() < (p_size.0 * scale).powi(2) { if (mouse_pos - t).length_squared() < (p_size.0 * scale).powi(2) {
*holding = Some((e, *edge)); *holding = Holding::Shape(e, *edge);
break;
}
}
}
}
// Only proceed if holding is still None
if *holding == Holding::None {
for (e, h) in images.iter() {
if let Some(size) = assets.get(h).map(|x| x.size()) {
let size = size * 0.5;
let t = gtransforms.get(e).unwrap();
// Disregard rotations for now plz
let diff = (mouse_pos - t.translation().xy()).abs();
if diff.x < size.x && diff.y < size.y {
*holding = Holding::Image(e, mouse_pos - t.translation().xy());
break; break;
} }
} }
} }
} }
} }
else if mouse.just_released(MouseButton::Left) && holding.is_some() { else if mouse.just_released(MouseButton::Left) && *holding != Holding::None {
*holding = None; // We just released our sad little dot/shape *holding = Holding::None; // We just released our sad little dot/shape
} }
else if let Some((se, pe)) = *holding { else if let Holding::Shape(se, pe) = *holding {
if let Ok(sd) = shapes.get_component::<ShapeData>(se) { if let Ok(sd) = shapes.get_component::<ShapeData>(se) {
// Middle of a drag :D // Middle of a drag :D
if let Some(ce) = sd.center && ce == pe { if let Some(ce) = sd.center && ce == pe {
@ -79,6 +106,11 @@ pub fn modify_sys(
} }
} }
} }
else if let Holding::Image(e, offset) = *holding {
if let Ok(mut t) = transforms.get_mut(e) {
t.translation = snap.snap_to_grid(mouse_pos - offset).extend(t.translation.z);
}
}
} }
} }

View file

@ -81,7 +81,7 @@ pub fn grid_window_sys(
mut egui_ctx: ResMut<EguiContext>, mut egui_ctx: ResMut<EguiContext>,
) { ) {
egui::Window::new("Snap Grid") egui::Window::new("Snap Grid")
.default_pos((200.0, 20.0)) .default_pos((500.0, 20.0))
.title_bar(true) .title_bar(true)
.resizable(false) .resizable(false)
.show(egui_ctx.ctx_mut(), |ui| { .show(egui_ctx.ctx_mut(), |ui| {
@ -131,6 +131,10 @@ pub fn shape_tree_sys(
hui.add(egui::DragValue::new(&mut t.translation.x)); hui.add(egui::DragValue::new(&mut t.translation.x));
hui.add(egui::DragValue::new(&mut t.translation.y)); hui.add(egui::DragValue::new(&mut t.translation.y));
}); });
ui.horizontal(|hui| {
hui.label("Z:");
hui.add(egui::DragValue::new(&mut t.translation.z).clamp_range(0..=i32::MAX));
});
ui.horizontal(|hui| { ui.horizontal(|hui| {
hui.label("Rotation:"); hui.label("Rotation:");
let mut rot = t.rotation.to_euler(EulerRot::XYZ).2.to_degrees(); let mut rot = t.rotation.to_euler(EulerRot::XYZ).2.to_degrees();