-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from jacekpoz/circle
add ellipse tool
- Loading branch information
Showing
5 changed files
with
161 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
use anyhow::Result; | ||
use femtovg::{FontId, Path}; | ||
use relm4::gtk::gdk::{Key, ModifierType}; | ||
|
||
use crate::{ | ||
math::Vec2D, | ||
sketch_board::{MouseEventMsg, MouseEventType}, | ||
style::Style, | ||
}; | ||
|
||
use super::{Drawable, DrawableClone, Tool, ToolUpdateResult}; | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
pub struct Ellipse { | ||
middle: Vec2D, | ||
radii: Option<Vec2D>, | ||
style: Style, | ||
} | ||
|
||
impl Drawable for Ellipse { | ||
fn draw( | ||
&self, | ||
canvas: &mut femtovg::Canvas<femtovg::renderer::OpenGl>, | ||
_font: FontId, | ||
) -> Result<()> { | ||
let radii = match self.radii { | ||
Some(s) => s, | ||
None => return Ok(()), // early exit if none | ||
}; | ||
|
||
canvas.save(); | ||
let mut path = Path::new(); | ||
path.ellipse(self.middle.x, self.middle.y, radii.x, radii.y); | ||
|
||
canvas.stroke_path(&path, &self.style.into()); | ||
canvas.restore(); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct EllipseTool { | ||
ellipse: Option<Ellipse>, | ||
style: Style, | ||
} | ||
|
||
impl Tool for EllipseTool { | ||
fn handle_mouse_event(&mut self, event: MouseEventMsg) -> ToolUpdateResult { | ||
let shift_pressed = event.modifier.intersects(ModifierType::SHIFT_MASK); | ||
match event.type_ { | ||
MouseEventType::BeginDrag => { | ||
// start new | ||
self.ellipse = Some(Ellipse { | ||
middle: event.pos, | ||
radii: None, | ||
style: self.style, | ||
}); | ||
|
||
ToolUpdateResult::Redraw | ||
} | ||
MouseEventType::EndDrag => { | ||
if let Some(ellipse) = &mut self.ellipse { | ||
if event.pos == Vec2D::zero() { | ||
self.ellipse = None; | ||
|
||
ToolUpdateResult::Redraw | ||
} else { | ||
if shift_pressed { | ||
let max_size = event.pos.x.abs().max(event.pos.y.abs()); | ||
ellipse.radii = Some(Vec2D { | ||
x: max_size * event.pos.x.signum(), | ||
y: max_size * event.pos.y.signum(), | ||
}); | ||
} else { | ||
ellipse.radii = Some(event.pos); | ||
} | ||
let result = ellipse.clone_box(); | ||
self.ellipse = None; | ||
|
||
ToolUpdateResult::Commit(result) | ||
} | ||
} else { | ||
ToolUpdateResult::Unmodified | ||
} | ||
} | ||
MouseEventType::UpdateDrag => { | ||
if let Some(ellipse) = &mut self.ellipse { | ||
if event.pos == Vec2D::zero() { | ||
return ToolUpdateResult::Unmodified; | ||
} | ||
if shift_pressed { | ||
let max_size = event.pos.x.abs().max(event.pos.y.abs()); | ||
ellipse.radii = Some(Vec2D { | ||
x: max_size * event.pos.x.signum(), | ||
y: max_size * event.pos.y.signum(), | ||
}); | ||
} else { | ||
ellipse.radii = Some(event.pos); | ||
} | ||
|
||
ToolUpdateResult::Redraw | ||
} else { | ||
ToolUpdateResult::Unmodified | ||
} | ||
} | ||
_ => ToolUpdateResult::Unmodified, | ||
} | ||
} | ||
|
||
fn handle_key_event(&mut self, event: crate::sketch_board::KeyEventMsg) -> ToolUpdateResult { | ||
if event.key == Key::Escape && self.ellipse.is_some() { | ||
self.ellipse = None; | ||
ToolUpdateResult::Redraw | ||
} else { | ||
ToolUpdateResult::Unmodified | ||
} | ||
} | ||
|
||
fn handle_style_event(&mut self, style: Style) -> ToolUpdateResult { | ||
self.style = style; | ||
ToolUpdateResult::Unmodified | ||
} | ||
|
||
fn get_drawable(&self) -> Option<&dyn Drawable> { | ||
match &self.ellipse { | ||
Some(d) => Some(d), | ||
None => None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters