Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Erase faster to increase eraser size #1235

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 55 additions & 11 deletions crates/rnote-engine/src/pens/eraser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,48 @@ pub enum EraserState {
Down(Element),
}

#[derive(Debug, Default, Clone, Copy)]
pub struct EraserMotion {
last_element: Option<Element>,
last_element_time: Option<Instant>,
Doublonmousse marked this conversation as resolved.
Show resolved Hide resolved
pub speed: f64,
}

impl EraserMotion {
pub const SMOOTHING_FACTOR: f64 = 3.0;

fn update(&mut self, element: Element) {
if let Some(last_element) = self.last_element {
if let Some(last_element_time) = self.last_element_time {
let delta = element.pos - last_element.pos;
let delta_time = Instant::now() - last_element_time;
let new_speed = delta.norm() / delta_time.as_secs_f64();
self.speed = (self.speed * Self::SMOOTHING_FACTOR + new_speed)
/ (Self::SMOOTHING_FACTOR + 1.0);
}
}
self.last_element = Some(element);
self.last_element_time = Some(Instant::now());
}

fn reset(&mut self) {
self.last_element = None;
self.last_element_time = None;
self.speed = 0.0;
}
}

#[derive(Clone, Debug)]
pub struct Eraser {
pub(crate) state: EraserState,
pub(crate) motion: EraserMotion,
}

impl Default for Eraser {
fn default() -> Self {
Self {
state: EraserState::Up,
motion: EraserMotion::default(),
}
}
}
Expand Down Expand Up @@ -57,10 +90,10 @@ impl PenBehaviour for Eraser {
engine_view: &mut EngineViewMut,
) -> (EventResult<PenProgress>, WidgetFlags) {
let mut widget_flags = WidgetFlags::default();

let event_result = match (&mut self.state, event) {
(EraserState::Up | EraserState::Proximity { .. }, PenEvent::Down { element, .. }) => {
widget_flags |= erase(element, engine_view);
self.motion.update(element);
widget_flags |= erase(element, self.motion.speed, engine_view);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Speed instead of added_width here

self.state = EraserState::Down(element);
EventResult {
handled: true,
Expand All @@ -69,6 +102,7 @@ impl PenBehaviour for Eraser {
}
}
(EraserState::Up | EraserState::Down { .. }, PenEvent::Proximity { element, .. }) => {
self.motion.update(element);
self.state = EraserState::Proximity(element);
EventResult {
handled: false,
Expand All @@ -85,7 +119,8 @@ impl PenBehaviour for Eraser {
progress: PenProgress::Idle,
},
(EraserState::Down(current_element), PenEvent::Down { element, .. }) => {
widget_flags |= erase(element, engine_view);
self.motion.update(element);
widget_flags |= erase(element, self.motion.speed, engine_view);
*current_element = element;
EventResult {
handled: true,
Expand All @@ -94,8 +129,9 @@ impl PenBehaviour for Eraser {
}
}
(EraserState::Down { .. }, PenEvent::Up { element, .. }) => {
widget_flags |=
erase(element, engine_view) | engine_view.store.record(Instant::now());
self.motion.reset();
widget_flags |= erase(element, self.motion.speed, engine_view)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing, speed instead of added_width

| engine_view.store.record(Instant::now());
self.state = EraserState::Up;
EventResult {
handled: true,
Expand All @@ -109,6 +145,7 @@ impl PenBehaviour for Eraser {
progress: PenProgress::InProgress,
},
(EraserState::Proximity(_), PenEvent::Up { .. }) => {
self.motion.reset();
self.state = EraserState::Up;
EventResult {
handled: false,
Expand All @@ -117,6 +154,7 @@ impl PenBehaviour for Eraser {
}
}
(EraserState::Proximity(current_element), PenEvent::Proximity { element, .. }) => {
self.motion.update(element);
*current_element = element;
EventResult {
handled: false,
Expand Down Expand Up @@ -167,7 +205,7 @@ impl DrawableOnDoc for Eraser {
engine_view
.pens_config
.eraser_config
.eraser_bounds(*current_element),
.eraser_bounds(*current_element, self.motion.speed),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing

),
}
}
Expand All @@ -190,7 +228,7 @@ impl DrawableOnDoc for Eraser {
let bounds = engine_view
.pens_config
.eraser_config
.eraser_bounds(*current_element);
.eraser_bounds(*current_element, self.motion.speed);

let fill_rect = bounds.to_kurbo_rect();
let outline_rect = bounds.tightened(outline_width * 0.5).to_kurbo_rect();
Expand All @@ -202,7 +240,7 @@ impl DrawableOnDoc for Eraser {
let bounds = engine_view
.pens_config
.eraser_config
.eraser_bounds(*current_element);
.eraser_bounds(*current_element, self.motion.speed);

let fill_rect = bounds.to_kurbo_rect();
let outline_rect = bounds.tightened(outline_width * 0.5).to_kurbo_rect();
Expand All @@ -217,20 +255,26 @@ impl DrawableOnDoc for Eraser {
}
}

fn erase(element: Element, engine_view: &mut EngineViewMut) -> WidgetFlags {
fn erase(element: Element, speed: f64, engine_view: &mut EngineViewMut) -> WidgetFlags {
// the widget_flags.store_modified flag is set in the `.trash_..()` methods
let mut widget_flags = WidgetFlags::default();

match &engine_view.pens_config.eraser_config.style {
EraserStyle::TrashCollidingStrokes => {
widget_flags |= engine_view.store.trash_colliding_strokes(
engine_view.pens_config.eraser_config.eraser_bounds(element),
engine_view
.pens_config
.eraser_config
.eraser_bounds(element, speed),
engine_view.camera.viewport(),
);
}
EraserStyle::SplitCollidingStrokes => {
let (modified_strokes, wf) = engine_view.store.split_colliding_strokes(
engine_view.pens_config.eraser_config.eraser_bounds(element),
engine_view
.pens_config
.eraser_config
.eraser_bounds(element, speed),
engine_view.camera.viewport(),
);
widget_flags |= wf;
Expand Down
16 changes: 14 additions & 2 deletions crates/rnote-engine/src/pens/pensconfig/eraserconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ pub struct EraserConfig {
pub width: f64,
#[serde(rename = "style")]
pub style: EraserStyle,
#[serde(rename = "speed_scale")]
pub speed_scaling: bool,
}

impl Default for EraserConfig {
fn default() -> Self {
Self {
width: Self::WIDTH_DEFAULT,
style: EraserStyle::default(),
speed_scaling: true,
}
}
}
Expand All @@ -52,8 +55,17 @@ impl EraserConfig {
pub const WIDTH_MIN: f64 = 1.0;
pub const WIDTH_MAX: f64 = 500.0;
pub const WIDTH_DEFAULT: f64 = 12.0;
pub const SPEED_SCALING: f64 = 0.001;

pub(crate) fn eraser_bounds(&self, element: Element) -> Aabb {
Aabb::from_half_extents(element.pos.into(), na::Vector2::repeat(self.width * 0.5))
pub(crate) fn eraser_bounds(&self, element: Element, speed: f64) -> Aabb {
if self.speed_scaling {
let speed_scale = 1.0 + speed * Self::SPEED_SCALING;
Aabb::from_half_extents(
element.pos.into(),
na::Vector2::repeat(self.width * speed_scale * 0.5),
)
} else {
Aabb::from_half_extents(element.pos.into(), na::Vector2::repeat(self.width * 0.5))
}
}
}
12 changes: 11 additions & 1 deletion crates/rnote-ui/data/ui/penssidebar/eraserpage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,15 @@
<property name="preview-style">rounded-rect</property>
</object>
</child>
<child>
<object class="GtkToggleButton" id="speed_scaling_toggle">
<property name="group">speed_scaling_toggle</property>
<property name="tooltip_text" translatable="yes">Scale eraser size with speed</property>
<property name="icon_name">workspacelistentryicon-speedometer-symbolic</property>
<style>
<class name="sidebar_action_button" />
</style>
</object>
</child>
</template>
</interface>
</interface>
16 changes: 16 additions & 0 deletions crates/rnote-ui/src/penssidebar/eraserpage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ mod imp {
pub(crate) eraserstyle_split_colliding_strokes_toggle: TemplateChild<ToggleButton>,
#[template_child]
pub(crate) stroke_width_picker: TemplateChild<RnStrokeWidthPicker>,
#[template_child]
pub(crate) speed_scaling_toggle: TemplateChild<ToggleButton>,
}

#[glib::object_subclass]
Expand Down Expand Up @@ -170,6 +172,20 @@ impl RnEraserPage {
}
),
);

imp.speed_scaling_toggle.connect_toggled(clone!(
#[weak]
appwindow,
move |speed_scaling_toggle| {
appwindow
.active_tab_wrapper()
.canvas()
.engine_mut()
.pens_config
.eraser_config
.speed_scaling = speed_scaling_toggle.is_active();
}
));
}

pub(crate) fn refresh_ui(&self, active_tab: &RnCanvasWrapper) {
Expand Down