From 8c5c9b4ec625c8bcae52c6fa0018fe9b781f1b79 Mon Sep 17 00:00:00 2001 From: Dietrich Date: Wed, 11 Jan 2023 10:20:15 +0100 Subject: [PATCH] Upgrade `inspector-egui` and add first `pen_up` Unfortunately `pen_up` does not seem to work even though it compiles --- Cargo.toml | 2 +- src/builders.rs | 134 +++++++++++++++++++++++++++++++++++ src/commands.rs | 18 ++--- src/debug.rs | 4 +- src/drawing.rs | 4 +- src/drawing/line_segments.rs | 18 ++--- src/general/angle.rs | 65 ++++++++++++----- src/general/length.rs | 4 +- src/lib.rs | 5 +- src/shapes.rs | 10 +-- src/state.rs | 8 ++- src/turtle_bundle.rs | 12 +++- 12 files changed, 234 insertions(+), 50 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8d0ed16..cbe57cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,6 @@ license = "MIT OR Apache-2.0" bevy = { version = "0.9" } bevy_prototype_lyon = {version="0.7"} -bevy-inspector-egui = "0.14" +bevy-inspector-egui = "0.16" num-traits = "0.2" bevy_tweening = {version="0.6"} \ No newline at end of file diff --git a/src/builders.rs b/src/builders.rs index ebaba36..de71746 100644 --- a/src/builders.rs +++ b/src/builders.rs @@ -7,6 +7,10 @@ use crate::{ #[derive(Default, Debug)] pub struct TurtlePlan { + /** + * A turtle Plan contains the segments of a turtle drawing. + * The segments in turn contain the commands to draw the graph. + */ commands: Vec, } @@ -119,3 +123,133 @@ pub trait CurvedMovement: WithCommands { } impl CurvedMovement for TurtlePlan {} + +pub trait StopLine +where + T: WithCommands, +{ + fn pen_up(self) -> InvisibleLinesPlan; +} + +impl StopLine for TurtlePlan { + fn pen_up(self) -> InvisibleLinesPlan { + { + InvisibleLinesPlan { + before: self, + commands: vec![], + } + } + } +} + +pub trait StartLine { + fn pen_down(self) -> T; +} + +impl StartLine for InvisibleLinesPlan +where + T: WithCommands, +{ + fn pen_down(mut self) -> T { + self.before.get_mut_commands().append(&mut self.commands); + self.before + } +} + +pub struct InvisibleLinesPlan { + before: T, + commands: Vec, +} + +impl WithCommands for InvisibleLinesPlan +where + T: WithCommands, +{ + fn get_mut_commands(&mut self) -> &mut Vec { + &mut self.commands + } + + fn get_commands(self) -> Vec { + self.commands + } +} + +impl InvisibleLinesPlan +where + T: WithCommands, +{ + pub fn new(before: T) -> Self { + InvisibleLinesPlan { + before, + commands: vec![], + } + } +} + +impl Turnable for InvisibleLinesPlan {} + +impl DirectionalMovement for InvisibleLinesPlan +where + T: WithCommands, +{ + fn forward(&mut self, length: IntoDistance) -> &mut Self + where + Length: From, + { + let length: Length = length.into(); + self.get_mut_commands() + .push(TurtleSegment::Single(DrawElement::Move( + crate::commands::MoveCommand::Forward(length), + ))); + self + } + + fn backward(&mut self, length: IntoDistance) -> &mut Self + where + Length: From, + { + let length: Length = length.into(); + self.get_mut_commands() + .push(TurtleSegment::Single(DrawElement::Move( + crate::commands::MoveCommand::Backward(length), + ))); + self + } +} + +impl CurvedMovement for InvisibleLinesPlan +where + T: WithCommands, +{ + fn circle( + &mut self, + radius: IntoDistance, + extend: IntoAngle, + ) -> &mut Self + where + Angle: From, + Length: From, + { + let angle: Angle = extend.into(); + let radius: Length = radius.into(); + self.get_mut_commands() + .push(TurtleSegment::Single(DrawElement::Move( + MoveCommand::Circle { radius, angle }, + ))); + self + } + + fn circle_right>( + &mut self, + radius: IntoDistance, + extend: IntoAngle, + ) -> &mut Self + where + Angle: From, + Length: From, + { + self.circle(-radius, extend); + println!("Warning: circle with right arc not working yet..."); + self + } +} diff --git a/src/commands.rs b/src/commands.rs index b67e0d1..dc355b6 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,5 +1,7 @@ -use bevy::prelude::Component; -use bevy_inspector_egui::Inspectable; +use bevy::{ + prelude::Component, + reflect::{FromReflect, Reflect}, +}; use crate::{ builders::WithCommands, @@ -17,7 +19,7 @@ use crate::{ * All the possibilities to draw something with turtle. All the commands can get the position, heading, * color and fill_color from the turtles state. */ -#[derive(Component, Inspectable, Debug)] +#[derive(Component, Reflect, FromReflect, Debug, Clone)] pub enum MoveCommand { Forward(Length), Backward(Length), @@ -35,7 +37,7 @@ impl Default for MoveCommand { } /// Different ways to drop breadcrumbs on the way like a dot or a stamp of the turtles shape. -#[derive(Component, Inspectable, Default, Debug)] +#[derive(Component, Reflect, FromReflect, Default, Debug, Clone)] pub enum Breadcrumb { Dot, #[default] @@ -43,7 +45,7 @@ pub enum Breadcrumb { } /// Different ways that change the orientation of the turtle. -#[derive(Component, Inspectable, Debug)] +#[derive(Component, Reflect, FromReflect, Debug, Clone)] pub enum OrientationCommand { Left(Angle), Right(Angle), @@ -58,7 +60,7 @@ impl Default for OrientationCommand { } /// A combination of all commands that can be used while drawing. -#[derive(Component, Inspectable, Debug)] +#[derive(Component, Reflect, FromReflect, Debug, Clone)] pub enum DrawElement { Draw(MoveCommand), Move(MoveCommand), @@ -102,7 +104,7 @@ impl ToAnimationSegment for DrawElement { } } -#[derive(Component, Inspectable, Debug)] +#[derive(Component, Reflect, FromReflect, Debug, Clone)] pub enum TurtleSegment { Single(DrawElement), Outline(Vec), @@ -126,7 +128,7 @@ impl ToAnimationSegment for TurtleSegment { } } } -#[derive(Component, Inspectable, Debug)] +#[derive(Component, Reflect, Debug)] pub struct TurtleCommands { animation_state: usize, commands: Vec, diff --git a/src/debug.rs b/src/debug.rs index 214360c..ae56520 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -1,12 +1,12 @@ use bevy::prelude::Plugin; -use bevy_inspector_egui::WorldInspectorPlugin; +use bevy_inspector_egui::quick::WorldInspectorPlugin; pub struct DebugPlugin; impl Plugin for DebugPlugin { fn build(&self, app: &mut bevy::prelude::App) { if cfg!(debug_assertions) { - app.add_plugin(WorldInspectorPlugin::new()); + app.add_plugin(WorldInspectorPlugin); } } } diff --git a/src/drawing.rs b/src/drawing.rs index fa6c2a4..f3819b0 100644 --- a/src/drawing.rs +++ b/src/drawing.rs @@ -1,4 +1,4 @@ -use bevy_inspector_egui::Inspectable; +use bevy::reflect::{FromReflect, Reflect}; pub use self::line_segments::{TurtleDrawCircle, TurtleDrawLine}; @@ -6,7 +6,7 @@ pub mod animation; mod line_segments; pub(crate) mod run_step; -#[derive(Inspectable, Default, Debug)] +#[derive(Reflect, FromReflect, Default, Debug)] pub enum TurtleGraphElement { TurtleLine(TurtleDrawLine), TurtleCircle(TurtleDrawCircle), diff --git a/src/drawing/line_segments.rs b/src/drawing/line_segments.rs index 9447207..0300b73 100644 --- a/src/drawing/line_segments.rs +++ b/src/drawing/line_segments.rs @@ -1,5 +1,7 @@ -use bevy::prelude::{Bundle, Color, Component, Name, Transform, Vec2}; -use bevy_inspector_egui::Inspectable; +use bevy::{ + prelude::{Bundle, Color, Component, Name, Transform, Vec2}, + reflect::{FromReflect, Reflect}, +}; use bevy_prototype_lyon::{ entity::ShapeBundle, prelude::{DrawMode, FillMode, GeometryBuilder, PathBuilder, StrokeMode}, @@ -8,9 +10,9 @@ use bevy_prototype_lyon::{ use crate::general::{angle::Angle, Precision}; -#[derive(Bundle, Inspectable, Default)] +#[derive(Bundle, Reflect, FromReflect, Default)] pub struct TurtleDrawLine { - #[inspectable(ignore)] + #[reflect(ignore)] line: ShapeBundle, name: Name, marker: LineMarker, @@ -25,7 +27,7 @@ impl std::fmt::Debug for TurtleDrawLine { } } -#[derive(Component, Default, Inspectable, Debug)] +#[derive(Component, Default, Reflect, FromReflect, Debug, Clone, Copy)] struct LineMarker; impl TurtleDrawLine { @@ -45,10 +47,10 @@ impl TurtleDrawLine { } } -#[derive(Bundle, Inspectable, Default)] +#[derive(Bundle, Reflect, FromReflect, Default)] pub struct TurtleDrawCircle { - #[inspectable(ignore)] + #[reflect(ignore)] line: ShapeBundle, name: Name, marker: CircleMarker, @@ -63,7 +65,7 @@ impl std::fmt::Debug for TurtleDrawCircle { } } -#[derive(Component, Default, Inspectable, Debug)] +#[derive(Component, Default, Reflect, FromReflect, Debug, Clone)] struct CircleMarker; impl TurtleDrawCircle { diff --git a/src/general/angle.rs b/src/general/angle.rs index 18a933d..e2e3dfc 100644 --- a/src/general/angle.rs +++ b/src/general/angle.rs @@ -1,29 +1,30 @@ -use bevy_inspector_egui::Inspectable; use std::{ f32::consts::PI, ops::{Add, Div, Mul, Neg, Rem, Sub}, }; +use bevy::reflect::{FromReflect, Reflect}; + use super::Precision; -#[derive(Inspectable, Copy, Clone, Debug, PartialEq, Eq)] -pub enum AngleUnit { +#[derive(Reflect, FromReflect, Copy, Clone, Debug, PartialEq, Eq)] +pub enum AngleUnit { Degrees(T), Radians(T), } -impl Default for AngleUnit { +impl Default for AngleUnit { fn default() -> Self { Self::Degrees(Default::default()) } } -#[derive(Inspectable, Copy, Default, Clone, Debug, PartialEq, Eq)] -pub struct Angle { +#[derive(Reflect, FromReflect, Copy, Default, Clone, Debug, PartialEq, Eq)] +pub struct Angle { value: AngleUnit, } -impl + Default> From for Angle { +impl + Default + Send + Sync + Reflect + Copy + FromReflect> From for Angle { fn from(i: i16) -> Self { Self { value: AngleUnit::Degrees(T::from(i)), @@ -31,7 +32,9 @@ impl + Default> From for Angle { } } -impl> Rem for Angle { +impl> Rem + for Angle +{ type Output = Self; fn rem(self, rhs: T) -> Self::Output { @@ -42,7 +45,9 @@ impl> Rem for Angle { } } -impl> Mul for Angle { +impl> Mul + for Angle +{ type Output = Self; fn mul(self, rhs: T) -> Self::Output { @@ -65,7 +70,9 @@ impl Angle { } } } -impl> Div for Angle { +impl> Div + for Angle +{ type Output = Self; fn div(self, rhs: T) -> Self::Output { @@ -76,7 +83,10 @@ impl> Div for Angle { } } -impl> Neg for Angle { +impl< + T: Default + Clone + Send + Sync + Reflect + Copy + FromReflect + std::ops::Neg, + > Neg for Angle +{ type Output = Self; fn neg(self) -> Self::Output { @@ -87,7 +97,10 @@ impl> Neg for Angle { } } -impl> Neg for &Angle { +impl< + T: Default + Clone + Send + Sync + Reflect + Copy + FromReflect + std::ops::Neg, + > Neg for &Angle +{ type Output = Angle; fn neg(self) -> Self::Output { @@ -98,7 +111,7 @@ impl> Neg for &Angle { } } -impl Angle { +impl Angle { pub fn degrees(value: T) -> Angle { Self { value: AngleUnit::Degrees(value), @@ -117,7 +130,7 @@ impl Angle { } } -impl Angle { +impl Angle { pub fn to_radians(self) -> Self { match self.value { AngleUnit::Degrees(v) => Self { @@ -136,7 +149,17 @@ impl Angle { } } -impl + Default + num_traits::float::Float> Add for Angle { +impl< + T: Add + + Send + + Sync + + Reflect + + Copy + + FromReflect + + Default + + num_traits::float::Float, + > Add for Angle +{ type Output = Angle; fn add(self, rhs: Self) -> Self::Output { @@ -157,7 +180,17 @@ impl + Default + num_traits::float::Float> Add for Angle { } } -impl + Default + num_traits::float::Float> Sub for Angle { +impl< + T: Sub + + Default + + Send + + Sync + + Reflect + + Copy + + FromReflect + + num_traits::float::Float, + > Sub for Angle +{ type Output = Angle; fn sub(self, rhs: Self) -> Self::Output { diff --git a/src/general/length.rs b/src/general/length.rs index 351560a..8c3f62f 100644 --- a/src/general/length.rs +++ b/src/general/length.rs @@ -1,8 +1,8 @@ -use bevy_inspector_egui::Inspectable; +use bevy::reflect::{FromReflect, Reflect}; use super::Precision; -#[derive(Inspectable, Default, Copy, Clone, Debug)] +#[derive(Reflect, FromReflect, Default, Copy, Clone, Debug)] pub struct Length(pub Precision); impl From for Length { diff --git a/src/lib.rs b/src/lib.rs index 96a932e..5efdaa5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,6 @@ use std::time::Duration; use bevy::{core_pipeline::clear_color::ClearColorConfig, prelude::*, window::close_on_esc}; -use bevy_inspector_egui::RegisterInspectable; use bevy_prototype_lyon::prelude::{Path, ShapePlugin}; use bevy_tweening::{ component_animator_system, lens::TransformScaleLens, Animator, EaseFunction, Tween, @@ -57,8 +56,8 @@ impl Plugin for TurtlePlugin { .add_system(component_animator_system::) .add_system(close_on_esc) .add_system(draw_lines) - .register_inspectable::() - .register_inspectable::(); + .register_type::() + .register_type::(); } } diff --git a/src/shapes.rs b/src/shapes.rs index 6474ddb..d08f3f2 100644 --- a/src/shapes.rs +++ b/src/shapes.rs @@ -1,12 +1,14 @@ mod turtle; -use bevy::prelude::{Color, Component}; -use bevy_inspector_egui::Inspectable; +use bevy::{ + prelude::{Color, Component}, + reflect::Reflect, +}; pub use turtle::turtle; -#[derive(Clone, Component, Inspectable)] +#[derive(Clone, Component, Reflect)] pub struct TurtleShape; -#[derive(Clone, Component, Inspectable, Default, Debug)] +#[derive(Clone, Component, Reflect, Default, Debug)] pub struct TurtleColors { color: Color, fill_color: Color, diff --git a/src/state.rs b/src/state.rs index 542c2a0..8707e80 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,7 +1,9 @@ use std::{cmp::max, time::Duration}; -use bevy::prelude::{Color, Component, Transform}; -use bevy_inspector_egui::Inspectable; +use bevy::{ + prelude::{Component, Transform}, + reflect::Reflect, +}; use crate::{ commands::TurtleSegment, @@ -10,7 +12,7 @@ use crate::{ }; /// Describing the full state of a turtle. -#[derive(Component, Inspectable, Default, Debug)] +#[derive(Component, Reflect, Default, Debug, Clone)] pub struct TurtleState { drawing: Vec, position: Coordinate, diff --git a/src/turtle_bundle.rs b/src/turtle_bundle.rs index 00e13c4..b7b069f 100644 --- a/src/turtle_bundle.rs +++ b/src/turtle_bundle.rs @@ -7,7 +7,10 @@ use bevy_prototype_lyon::{ }; use crate::{ - builders::{CurvedMovement, DirectionalMovement, Turnable, TurtlePlan, WithCommands}, + builders::{ + CurvedMovement, DirectionalMovement, InvisibleLinesPlan, StopLine, Turnable, TurtlePlan, + WithCommands, + }, commands::{TurtleCommands, TurtleSegment}, general::Speed, shapes::{self, TurtleColors}, @@ -87,6 +90,13 @@ impl WithCommands for TurtleBundle { self.commands.get_commands() } } +impl StopLine for TurtleBundle { + fn pen_up(self) -> crate::builders::InvisibleLinesPlan { + { + InvisibleLinesPlan::new(self) + } + } +} impl DirectionalMovement for TurtleBundle {} impl Turnable for TurtleBundle {}