Skip to main content

blockworx_tools/
icon.rs

1use crate::theme::Style;
2use blockworx_paint::{Canvas, Cursor, Event, Interaction};
3
4use crate::{
5    names::ToolName,
6    shape::ShapeId,
7    tool::{Action, ImageTarget, ToolTrait},
8    widget::drawing::Drawing,
9};
10
11/// Attach an icon to a block by clicking it (finger cursor); with a block
12/// already selected the toolbar button and the selection overlay's icon
13/// button ask for the image directly instead of arming this. The picked
14/// image becomes the block's foreground
15/// [`icon`](blockworx_doc::block_model::Block::icon), left selected for
16/// repositioning — which [`Action::SetIcon`] does.
17pub struct IconTool;
18
19impl ToolTrait for IconTool {
20    fn name(&self) -> ToolName {
21        ToolName::Icon
22    }
23
24    fn widget<C: Canvas>(
25        &mut self,
26        data: &mut Drawing,
27        interaction: &Interaction,
28        painter: &mut Style<'_, C>,
29    ) -> Option<Action> {
30        crate::widget::display::widget(data, interaction, painter);
31        painter.set_cursor(Cursor::PointingHand);
32        if let Some(Event::Clicked { pos }) = interaction.event
33            && let Some(ShapeId::Rect(rid)) = data.shape_at_pos(pos)
34        {
35            return Some(Action::ImageWanted(ImageTarget::Icon(rid)));
36        }
37        None
38    }
39}