Add plumbing for event system (#52)

* Add plumbing for event system

* Apply review suggestions

* Add swap and reset color functions
This commit is contained in:
T0mstone
2021-03-28 23:39:33 +02:00
committed by GitHub
parent ea1f9f30f0
commit c437f1bd22
9 changed files with 352 additions and 210 deletions
+28 -8
View File
@@ -1,8 +1,10 @@
use crate::events::{ModKeys, MouseState, TracePoint};
use crate::{events::Trace, Color};
use std::collections::HashMap;
pub struct ToolState {
pub mouse_is_clicked: bool,
pub mouse_state: MouseState,
pub mod_keys: ModKeys,
pub trace: Trace,
pub primary_color: Color,
pub secondary_color: Color,
@@ -10,10 +12,11 @@ pub struct ToolState {
tool_settings: HashMap<ToolType, ToolSettings>,
}
impl ToolState {
pub fn new() -> Self {
impl Default for ToolState {
fn default() -> Self {
ToolState {
mouse_is_clicked: false,
mouse_state: MouseState::default(),
mod_keys: ModKeys::default(),
trace: Trace::new(),
primary_color: Color::BLACK,
secondary_color: Color::WHITE,
@@ -23,14 +26,29 @@ impl ToolState {
}
}
impl ToolState {
pub fn new() -> Self {
Self::default()
}
pub fn record_trace_point(&mut self) {
self.trace.push(TracePoint {
mouse_state: self.mouse_state,
mod_keys: self.mod_keys,
})
}
}
fn default_tool_settings() -> HashMap<ToolType, ToolSettings> {
let tool_init = |tool: &ToolType| (*tool, tool.default_settings());
// TODO: when 1.51 is more common, change this to use array::IntoIter
[
tool_init(&ToolType::Select),
tool_init(&ToolType::Ellipse),
tool_init(&ToolType::Shape), // TODO: Add more tool defaults
]
.iter()
.cloned()
.copied()
.collect()
}
@@ -54,6 +72,7 @@ impl ToolType {
fn default_settings(&self) -> ToolSettings {
match self {
ToolType::Select => ToolSettings::Select { append_mode: SelectAppendMode::New },
ToolType::Ellipse => ToolSettings::Ellipse,
ToolType::Shape => ToolSettings::Shape {
shape: Shape::Polygon { vertices: 3 },
},
@@ -62,13 +81,14 @@ impl ToolType {
}
}
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ToolSettings {
Select { append_mode: SelectAppendMode },
Ellipse,
Shape { shape: Shape },
}
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum SelectAppendMode {
New,
Add,
@@ -76,7 +96,7 @@ pub enum SelectAppendMode {
Intersect,
}
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Shape {
Star { vertices: u32 },
Polygon { vertices: u32 },