dragging! and some other things, also bevy 0.8 now(which is annoying me currently due to a weird bug in exit

This commit is contained in:
RustyStriker 2022-08-01 18:33:21 +03:00
parent 509a11a6a9
commit 1da4c5b473
9 changed files with 509 additions and 478 deletions

View file

@ -1,4 +1,5 @@
use bevy::ecs::schedule::ShouldRun;
use bevy::input::mouse::{MouseMotion, MouseWheel};
use bevy::math::Vec3Swizzles;
use bevy::{prelude::*, window::PresentMode, winit::WinitSettings};
use bevy_egui::{egui, EguiContext, EguiPlugin};
@ -15,7 +16,7 @@ fn main() {
// Optimal power saving and present mode settings for desktop apps.
.insert_resource(WinitSettings::desktop_app())
.insert_resource(WindowDescriptor {
present_mode: PresentMode::Mailbox,
present_mode: PresentMode::Fifo,
title: "Shape Maker".to_string(),
..Default::default()
})
@ -39,13 +40,13 @@ fn main() {
.add_system(modify_sys.with_run_criteria(|state: Res<UiState>, mut ec: ResMut<EguiContext>|
if !ec.ctx_mut().is_pointer_over_area() && state.current_action == Action::Modify { ShouldRun::Yes } else { ShouldRun::No }
))
.add_system(rotate_sys.with_run_criteria(|state: Res<UiState>, mut ec: ResMut<EguiContext>|
if !ec.ctx_mut().is_pointer_over_area() && state.current_action == Action::Rotate { ShouldRun::Yes } else { ShouldRun::No }
))
.add_system(action_bar_sys)
.add_system(shape_tree_sys)
.add_system(show_hide_points)
.add_system(color_points)
.add_system(drag_camera_sys)
.add_system(zoom_camera_sys)
.add_system(scale_points)
;
app.run();
@ -54,7 +55,7 @@ fn main() {
fn basic_setup_sys(
mut coms: Commands,
) {
coms.spawn_bundle(OrthographicCameraBundle::new_2d())
coms.spawn_bundle(Camera2dBundle::default())
.insert(MainCamera);
}
@ -65,6 +66,70 @@ fn configure_visuals(mut egui_ctx: ResMut<EguiContext>) {
});
}
fn zoom_camera_sys(
mut mouse_scroll: EventReader<MouseWheel>,
mut cam: Query<&mut OrthographicProjection, With<MainCamera>>,
) {
let mut scroll = 0.0;
for m in mouse_scroll.iter() {
scroll += m.y * 0.1;
}
for mut op in cam.iter_mut() {
op.scale += scroll ;
if op.scale <= 0.0 {
op.scale = 0.1;
}
}
}
fn scale_points(
scale: Query<&OrthographicProjection, (With<MainCamera>, Changed<OrthographicProjection>)>,
shapes: Query<&ShapeData>,
p_size: Res<PointSize>,
mut paths: Query<&mut Path, With<Path>>,
) {
if let Ok(scale) = scale.get_single() {
let scale = scale.scale;
for sd in shapes.iter() {
if let Ok(mut p) = paths.get_mut(sd.center.unwrap()) {
let shape = shapes::Circle {
radius: p_size.0 * scale,
center: Vec2::ZERO,
};
*p = ShapePath::build_as(&shape);
}
for &edge in sd.edges.iter() {
if let Ok(mut p) = paths.get_mut(edge) {
let shape = shapes::Circle {
radius: p_size.0 * scale,
center: Vec2::ZERO,
};
*p = ShapePath::build_as(&shape);
}
}
}
}
}
fn drag_camera_sys(
mut mouse_move: EventReader<MouseMotion>,
mouse_input: Res<Input<MouseButton>>,
mut cam: Query<(&mut Transform, &OrthographicProjection), With<MainCamera>>,
) {
if mouse_input.pressed(MouseButton::Middle) {
let mut delta = Vec2::ZERO;
for m in mouse_move.iter() {
delta -= Vec2::new(m.delta.x, -m.delta.y) * 2.0;
}
for (mut t, op) in cam.iter_mut() {
t.translation += delta.extend(0.0) * op.scale;
}
}
}
fn show_hide_points(
state: Res<UiState>,
@ -74,7 +139,6 @@ fn show_hide_points(
let (center, edge) = match state.current_action {
Action::Create => (false, false),
Action::Modify => (true, true),
Action::Rotate => (true, true),
};
for sd in shapes.iter() {
@ -96,6 +160,7 @@ fn show_hide_points(
fn color_points(
mut current: Local<Option<Entity>>,
scale: Query<&OrthographicProjection, With<MainCamera>>,
wnds: Res<Windows>,
p_size: Res<PointSize>,
q_cam: Query<(&Camera, &GlobalTransform), With<MainCamera>>,
@ -103,16 +168,18 @@ fn color_points(
global_transforms: Query<&GlobalTransform, With<Path>>,
mut dots: Query<&mut DrawMode, With<Path>>,
) {
let normal = DrawMode::Outlined { fill_mode: FillMode::color(Color::RED), outline_mode: StrokeMode::new(Color::rgb(0.9,0.0,0.0), 1.0) };
let hover = DrawMode::Outlined { fill_mode: FillMode::color(Color::RED), outline_mode: StrokeMode::new(Color::ORANGE, 3.0) };
let scale = scale.get_single().map(|s| s.scale).unwrap_or(1.0);
let normal = DrawMode::Outlined { fill_mode: FillMode::color(Color::RED), outline_mode: StrokeMode::new(Color::rgb(0.9,0.0,0.0), 0.0) };
let hover = DrawMode::Outlined { fill_mode: FillMode::color(Color::RED), outline_mode: StrokeMode::new(Color::ORANGE, 3.0 * scale) };
let mouse_pos = get_mouse_pos(&q_cam, &wnds);
if let Some(mpos) = mouse_pos {
let mut hovering = false;
'shapes: for sd in shapes.iter() {
let e = sd.center.unwrap();
let p = global_transforms.get(e).unwrap().translation.xy();
if (mpos - p).length_squared() < p_size.0.powi(2) {
let p = global_transforms.get(e).unwrap().translation().xy();
if (mpos - p).length_squared() < (p_size.0 * scale).powi(2) {
if Some(e) != *current {
if let Some(c) = &*current {
if let Ok(mut md) = dots.get_mut(*c) {
@ -128,8 +195,8 @@ fn color_points(
break;
}
for edge in sd.edges.iter() {
let p = global_transforms.get(*edge).unwrap().translation.xy();
if (mpos - p).length_squared() < p_size.0.powi(2) {
let p = global_transforms.get(*edge).unwrap().translation().xy();
if (mpos - p).length_squared() < (p_size.0 * scale).powi(2) {
if Some(*edge) != *current {
if let Some(c) = &*current {
if let Ok(mut md) = dots.get_mut(*c) {