170 lines
5.5 KiB
Rust
170 lines
5.5 KiB
Rust
// Stiletto
|
|
// Copyright (C) 2020 Stiletto Authors
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
use druid::im::{vector, Vector};
|
|
use druid::kurbo::BezPath;
|
|
use druid::widget::prelude::*;
|
|
use druid::{AppLauncher, Color, Data, Event, LocalizedString, WindowDesc};
|
|
|
|
use stiletto::CanvasElement;
|
|
|
|
#[derive(Clone, Data)]
|
|
struct CanvasData {
|
|
current_element: Option<CanvasElement>,
|
|
elements: Vector<CanvasElement>,
|
|
}
|
|
|
|
impl CanvasData {
|
|
fn is_drawing(&self) -> bool {
|
|
self.current_element.is_some()
|
|
}
|
|
}
|
|
|
|
struct CanvasWidget;
|
|
|
|
impl Widget<CanvasData> for CanvasWidget {
|
|
fn event(&mut self, _ctx: &mut EventCtx, event: &Event, data: &mut CanvasData, _env: &Env) {
|
|
match event {
|
|
Event::MouseDown(mouse_event) => {
|
|
let mut kurbo_path = BezPath::new();
|
|
kurbo_path.move_to((mouse_event.pos.x, mouse_event.pos.y));
|
|
data.current_element = Some(CanvasElement::Freehand{
|
|
path: stiletto::Path { kurbo_path },
|
|
thickness: 2.0,
|
|
});
|
|
}
|
|
Event::MouseMove(mouse_event) => {
|
|
if data.is_drawing() {
|
|
if let Some(current_element) = data.current_element.as_mut() {
|
|
current_element
|
|
.get_path_mut()
|
|
.unwrap()
|
|
.kurbo_path
|
|
.line_to((mouse_event.pos.x, mouse_event.pos.y));
|
|
}
|
|
}
|
|
}
|
|
Event::MouseUp(_) => {
|
|
if data.is_drawing() {
|
|
if let Some(current_element) = data.current_element.take() {
|
|
data.elements.push_back(current_element);
|
|
}
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
fn lifecycle(
|
|
&mut self,
|
|
_ctx: &mut LifeCycleCtx,
|
|
_event: &LifeCycle,
|
|
_data: &CanvasData,
|
|
_env: &Env,
|
|
) {
|
|
}
|
|
|
|
fn update(
|
|
&mut self,
|
|
ctx: &mut UpdateCtx,
|
|
old_data: &CanvasData,
|
|
data: &CanvasData,
|
|
_env: &Env,
|
|
) {
|
|
// the current_element is moved to the elements array, no need to repaint
|
|
if old_data.is_drawing() && !data.is_drawing() {
|
|
return;
|
|
}
|
|
if data.is_drawing() {
|
|
if let Some(e) = data.current_element.as_ref() {
|
|
ctx.request_paint_rect(e.bounding_box());
|
|
}
|
|
} else {
|
|
ctx.request_paint();
|
|
}
|
|
}
|
|
|
|
fn layout(
|
|
&mut self,
|
|
_layout_ctx: &mut LayoutCtx,
|
|
bc: &BoxConstraints,
|
|
_data: &CanvasData,
|
|
_env: &Env,
|
|
) -> Size {
|
|
// BoxConstraints are passed by the parent widget.
|
|
// This method can return any Size within those constraints:
|
|
// bc.constrain(my_size)
|
|
//
|
|
// To check if a dimension is infinite or not (e.g. scrolling):
|
|
// bc.is_width_bounded() / bc.is_height_bounded()
|
|
bc.max()
|
|
}
|
|
|
|
// The paint method gets called last, after an event flow.
|
|
// It goes event -> update -> layout -> paint, and each method can influence the next.
|
|
// Basically, anything that changes the appearance of a widget causes a paint.
|
|
fn paint(&mut self, ctx: &mut PaintCtx, data: &CanvasData, _env: &Env) {
|
|
// Let's draw a picture with Piet!
|
|
|
|
// Clear the whole widget with the color of your choice
|
|
// (ctx.size() returns the size of the layout rect we're painting in)
|
|
let size = ctx.size();
|
|
let rect = size.to_rect();
|
|
ctx.fill(rect, &Color::WHITE);
|
|
// Note: ctx also has a `clear` method, but that clears the whole context,
|
|
// and we only want to clear this widget's area.
|
|
for element in &data.elements {
|
|
element.draw(ctx);
|
|
}
|
|
if let Some(element) = &data.current_element {
|
|
element.draw(ctx);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn build_ui() -> impl Widget<CanvasData> {
|
|
use druid::widget::{Align, Button, Flex, CrossAxisAlignment, SizedBox};
|
|
let toolbar = Flex::row()
|
|
.cross_axis_alignment(CrossAxisAlignment::Center)
|
|
.with_spacer(30.0)
|
|
.with_child(
|
|
Button::new("Undo"))
|
|
.with_child(
|
|
Button::new("Redo"));
|
|
|
|
Flex::column()
|
|
.cross_axis_alignment(CrossAxisAlignment::Center)
|
|
.must_fill_main_axis(true)
|
|
.with_child(SizedBox::new(Align::left(toolbar)).height(50.0))
|
|
.with_flex_child(CanvasWidget {}, 1.0)
|
|
}
|
|
|
|
pub fn main() {
|
|
let window = WindowDesc::new(build_ui)
|
|
.window_size((1024.0, 1400.0))
|
|
.title(
|
|
LocalizedString::new("custom-widget-demo-window-title").with_placeholder("Stiletto"),
|
|
);
|
|
let canvas_data = CanvasData {
|
|
current_element: None,
|
|
elements: vector![],
|
|
};
|
|
AppLauncher::with_window(window)
|
|
.use_simple_logger()
|
|
.launch(canvas_data)
|
|
.expect("launch failed");
|
|
}
|