shape_maker/src/lib.rs

140 lines
3.1 KiB
Rust

#![feature(let_chains)]
use bevy::prelude::*;
use bevy_egui::egui;
pub mod create;
pub mod helpers;
pub mod modify;
pub mod ui;
pub mod infinite_grid;
pub mod export;
pub mod undo;
pub use undo::UndoItem;
pub use modify::modify_sys;
pub use create::create_sys;
pub use helpers::*;
#[derive(Debug, Clone, Deref, DerefMut)]
pub struct DefaultColor(pub Color);
#[derive(Debug, Clone)]
pub struct SnapGrid {
pub width: f32,
pub height: f32,
pub snap: bool,
pub offset: Vec2,
pub visible: bool,
}
impl SnapGrid {
pub fn snap_to_grid(&self, v: Vec2) -> Vec2 {
if self.snap {
let w = v - self.offset + 0.5 * Vec2::new(self.width.copysign(v.x), self.height.copysign(v.y));
let snapped = Vec2::new(w.x - w.x % self.width, w.y - w.y % self.height);
snapped + self.offset
}
else {
v
}
}
}
impl Default for SnapGrid {
fn default() -> Self {
Self { width: 50.0, height: 50.0, snap: false, offset: Vec2::ZERO, visible: true }
}
}
#[derive(Clone, Debug)]
pub struct PointSize(pub f32);
#[derive(Component, Debug, Default, Clone)]
pub struct ImageData {
pub name: String,
pub note: String,
pub path: String,
}
#[derive(Component, Debug, Clone)]
pub struct ShapeData {
pub name: String,
pub shape: CreateShape,
pub main_shape: Entity,
pub edges: Vec<Entity>,
pub center: Option<Entity>,
pub note: String,
}
impl ShapeData {
pub fn shallow_copy(&self) -> Self {
Self {
name: Default::default(),
shape: self.shape,
main_shape: self.main_shape,
edges: Default::default(),
center: Default::default(),
note: Default::default()
}
}
}
#[derive(Component)]
pub struct MainCamera;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum CreateShape {
Triangle, Square, Circle, Capsule
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Action {
Create, Modify
}
pub struct UiState {
pub current_action: Action,
pub create_shape: CreateShape,
}
impl Default for UiState {
fn default() -> Self {
Self {
current_action: Action::Modify,
create_shape: CreateShape::Square,
}
}
}
impl UiState {
pub fn create_shape_color(&self, colors: &ButtonsColors, shape: CreateShape) -> egui::Rgba {
if self.create_shape == shape {
colors.clicked
}
else {
colors.regular
}
}
pub fn current_action_color(&self, colors: &ButtonsColors, action: Action) -> egui::Rgba {
if self.current_action == action {
colors.clicked
}
else {
colors.regular
}
}
}
#[derive(Default, Clone, Debug)]
pub struct ShouldImportExport {
pub import: bool,
pub export: bool,
}
#[derive(Debug)]
pub struct ButtonsColors {
pub regular: egui::Rgba,
pub clicked: egui::Rgba,
}
impl Default for ButtonsColors {
fn default() -> Self {
Self {
regular: egui::Rgba::from_rgb(0.3, 0.3, 0.3),
clicked: egui::Rgba::from_rgb(1.0, 1.0, 1.0)
}
}
}