1 Commits

Author SHA1 Message Date
c63babb599 Make pencil the default tool 2025-11-07 16:43:08 +01:00

View File

@ -27,7 +27,7 @@ use zerocopy::{FromBytes, IntoBytes};
use crate::{custom_code_block::try_from_custom_code_block, rasterizer}; use crate::{custom_code_block::try_from_custom_code_block, rasterizer};
use crate::{custom_code_block::write_custom_code_block, util::random_id}; use crate::{custom_code_block::write_custom_code_block, util::random_id};
use self::tool::{ToolEvent, Tool}; use self::tool::{Tool, ToolEvent};
const HANDWRITING_MIN_HEIGHT: f32 = 100.0; const HANDWRITING_MIN_HEIGHT: f32 = 100.0;
const HANDWRITING_BOTTOM_PADDING: f32 = 80.0; const HANDWRITING_BOTTOM_PADDING: f32 = 80.0;
@ -89,9 +89,6 @@ struct Ephemeral {
/// Tool position last frame, in canvas space. /// Tool position last frame, in canvas space.
last_tool_position: Option<Pos2>, last_tool_position: Option<Pos2>,
/// Whether the handwriting element is being interacted with.
is_focused: bool,
/// The stroke that is currently being drawed. /// The stroke that is currently being drawed.
current_stroke: Vec<Pos2>, current_stroke: Vec<Pos2>,
@ -142,10 +139,9 @@ impl Default for Ephemeral {
Self { Self {
id: random_id(), id: random_id(),
canvas_rasterizer: Default::default(), canvas_rasterizer: Default::default(),
tool: Tool::Eraser, tool: Tool::Pencil,
tool_position: None, tool_position: None,
last_tool_position: None, last_tool_position: None,
is_focused: false,
current_stroke: Default::default(), current_stroke: Default::default(),
tessellator: None, tessellator: None,
mesh: Default::default(), mesh: Default::default(),
@ -242,16 +238,9 @@ impl Handwriting {
// Was the user in the process of drawing a stroke last frame? // Was the user in the process of drawing a stroke last frame?
let was_drawing = !self.e.current_stroke.is_empty(); let was_drawing = !self.e.current_stroke.is_empty();
// Is the user in the process of drawing a stroke now?; // Is the user in the process of drawing a stroke now?
let is_drawing = response.interact_pointer_pos().is_some(); let is_drawing = response.interact_pointer_pos().is_some();
if is_drawing {
self.e.is_focused = true;
} else if ui.ctx().input(|i| i.pointer.any_down()) {
// if the user starts interactive with something else, unfocus the handwriting area.
self.e.is_focused = false;
}
if !is_drawing { if !is_drawing {
if was_drawing { if was_drawing {
// commit current line // commit current line
@ -259,18 +248,12 @@ impl Handwriting {
response.mark_changed(); response.mark_changed();
} }
let padding = if self.e.is_focused {
HANDWRITING_BOTTOM_PADDING
} else {
0.0
};
// recalculate how tall the widget should be // recalculate how tall the widget should be
let lines_max_y = self let lines_max_y = self
.strokes .strokes
.iter() .iter()
.flatten() .flatten()
.map(|p| p.y + padding) .map(|p| p.y + HANDWRITING_BOTTOM_PADDING)
.fold(HANDWRITING_MIN_HEIGHT, |max, y| max.max(y)); .fold(HANDWRITING_MIN_HEIGHT, |max, y| max.max(y));
// Change the height of the handwriting item. // Change the height of the handwriting item.
@ -316,9 +299,7 @@ impl Handwriting {
self.e.tool_position = tool_event.position(); self.e.tool_position = tool_event.position();
match self.e.tool { match self.e.tool {
Tool::Pencil => { Tool::Pencil => {
hw_response.changed |= hw_response.changed |= tool::pencil::on_tool_event(self, tool_event);
tool::pencil::on_tool_event(self, tool_event);
} }
Tool::Eraser => { Tool::Eraser => {
if tool::eraser::on_tool_event(self, tool_event) { if tool::eraser::on_tool_event(self, tool_event) {
@ -384,7 +365,9 @@ impl Handwriting {
// Draw the texture // Draw the texture
self.e.canvas_rasterizer.show(ui.ctx(), &painter, mesh_rect); self.e.canvas_rasterizer.show(ui.ctx(), &painter, mesh_rect);
if let Some(tool_position) = self.e.tool_position && let Tool::Eraser = self.e.tool { if let Some(tool_position) = self.e.tool_position
&& let Tool::Eraser = self.e.tool
{
let pos = to_screen * tool_position; let pos = to_screen * tool_position;
let shape = Shape::circle_stroke(pos, tool::eraser::RADIUS, style.stroke); let shape = Shape::circle_stroke(pos, tool::eraser::RADIUS, style.stroke);
painter.add(shape); painter.add(shape);