Skip to content

Commit

Permalink
Merge pull request #100 from jacekpoz/shift-square
Browse files Browse the repository at this point in the history
turn rectangle into a square when shift is pressed
  • Loading branch information
gabm authored Jul 14, 2024
2 parents c5e50bf + 3a5c830 commit a272230
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/tools/rectangle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use femtovg::{FontId, Path};
use relm4::gtk::gdk::Key;
use relm4::gtk::gdk::{Key, ModifierType};

use crate::{
math::Vec2D,
Expand Down Expand Up @@ -51,6 +51,7 @@ pub struct RectangleTool {

impl Tool for RectangleTool {
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
Expand All @@ -63,14 +64,22 @@ impl Tool for RectangleTool {
ToolUpdateResult::Redraw
}
MouseEventType::EndDrag => {
if let Some(a) = &mut self.rectangle {
if let Some(rectangle) = &mut self.rectangle {
if event.pos == Vec2D::zero() {
self.rectangle = None;

ToolUpdateResult::Redraw
} else {
a.size = Some(event.pos);
let result = a.clone_box();
if shift_pressed {
let max_size = event.pos.x.abs().max(event.pos.y.abs());
rectangle.size = Some(Vec2D {
x: max_size * event.pos.x.signum(),
y: max_size * event.pos.y.signum(),
});
} else {
rectangle.size = Some(event.pos);
}
let result = rectangle.clone_box();
self.rectangle = None;

ToolUpdateResult::Commit(result)
Expand All @@ -80,11 +89,19 @@ impl Tool for RectangleTool {
}
}
MouseEventType::UpdateDrag => {
if let Some(a) = &mut self.rectangle {
if let Some(rectangle) = &mut self.rectangle {
if event.pos == Vec2D::zero() {
return ToolUpdateResult::Unmodified;
}
a.size = Some(event.pos);
if shift_pressed {
let max_size = event.pos.x.abs().max(event.pos.y.abs());
rectangle.size = Some(Vec2D {
x: max_size * event.pos.x.signum(),
y: max_size * event.pos.y.signum(),
});
} else {
rectangle.size = Some(event.pos);
}

ToolUpdateResult::Redraw
} else {
Expand Down

0 comments on commit a272230

Please sign in to comment.