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] Show images in tree view
- [x] Show/Hide images
- [ ] Move/Drag images around
- [x] Move/Drag images around
- [ ] Control images Z value(so we could reorder them)
- [x] Delete images
- [x] Name images
- [ ] Duplicate images(and also shapes maybe?)?
- [ ] Scale images
- [x] Show hide shapes
- [ ] Control shape Z value
- [x] Control shape Z value
- [x] Snap to grid
- [x] Change grid size
- [ ] 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);
}
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() {
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,
// then it should be the entity we just inserted, and we can just override its transform
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(
&shape,
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 })
.id();
@ -188,7 +188,7 @@ fn insert_edge_to_shape_creation(
let np = coms.spawn_bundle(GeometryBuilder::build_as(
&shape,
DrawMode::Fill(FillMode::color(Color::RED)),
Transform::from_translation(mpos.extend(1.0))
Transform::from_translation(mpos.extend(0.5))
))
.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(
&shape,
DrawMode::Fill(FillMode::color(Color::RED)),
Transform::from_translation(pos.extend(1.0))
Transform::from_translation(pos.extend(0.5))
))
.id();
// Spawn main shape!

View File

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

View File

@ -3,32 +3,44 @@ use bevy::prelude::*;
use crate::*;
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(
// 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>>,
wnds: Res<Windows>,
scale: Query<&OrthographicProjection, With<MainCamera>>,
p_size: Res<PointSize>,
assets: Res<Assets<Image>>,
mut paths: Query<&mut Path>,
mut transforms: Query<&mut Transform>,
gtransforms: Query<&GlobalTransform>,
q_cam: Query<(&Camera, &GlobalTransform), With<MainCamera>>,
shapes: Query<(Entity, &ShapeData)>,
images: Query<(Entity, &Handle<Image>)>,
snap: Res<SnapGrid>,
) {
let scale = scale.get_single().map(|s| s.scale).unwrap_or(1.0);
let mouse_pos = get_mouse_pos(&q_cam, &wnds);
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
for (e, sd) in shapes.iter() {
if let Ok(t) = gtransforms.get(sd.center.unwrap()) {
let t = t.translation().xy();
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;
}
}
@ -37,17 +49,32 @@ pub fn modify_sys(
let t = t.translation().xy();
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;
}
}
}
}
}
else if mouse.just_released(MouseButton::Left) && holding.is_some() {
*holding = None; // We just released our sad little dot/shape
else if mouse.just_released(MouseButton::Left) && *holding != Holding::None {
*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) {
// Middle of a drag :D
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>,
) {
egui::Window::new("Snap Grid")
.default_pos((200.0, 20.0))
.default_pos((500.0, 20.0))
.title_bar(true)
.resizable(false)
.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.y));
});
ui.horizontal(|hui| {
hui.label("Z:");
hui.add(egui::DragValue::new(&mut t.translation.z).clamp_range(0..=i32::MAX));
});
ui.horizontal(|hui| {
hui.label("Rotation:");
let mut rot = t.rotation.to_euler(EulerRot::XYZ).2.to_degrees();