Skip to content
Snippets Groups Projects
Commit 2f1b5052 authored by Per Lindgren's avatar Per Lindgren
Browse files

draw (scaling broken)

parents
No related branches found
No related tags found
No related merge requests found
/target
**/*.rs.bk
[package]
name = "envelop_edit"
version = "0.1.0"
authors = ["Per Lindgren <per.lindgren@ltu.se>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies.druid]
version = "0.4.0"
path = "../druid/druid"
[dependencies]
log = "0.4.8"
// Copyright 2019 The xi-editor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! An example of an animating widget.
use std::f64::consts::PI;
use druid::kurbo::Line;
use druid::widget::{Flex, Label, WidgetExt};
use druid::{
AppLauncher, BoxConstraints, Color, Env, Event, EventCtx, LayoutCtx, LinearGradient, PaintCtx,
PlatformError, Point, RenderContext, Size, UnitPoint, UpdateCtx, Vec2, Widget, WindowDesc,
};
use log::info;
enum MouseState {
Up,
Down,
}
struct DrawWidget {
state: MouseState,
points: Vec<Point>,
t: f64,
}
impl Widget<()> for DrawWidget {
fn event(&mut self, ctx: &mut EventCtx, event: &Event, _data: &mut (), _env: &Env) {
match event {
Event::MouseDown(m) => {
info!("down: pos {:?}", m.pos);
self.state = MouseState::Down;
self.t = 0.0;
self.points.push(m.pos);
ctx.invalidate();
}
Event::MouseMoved(m) => match self.state {
MouseState::Down => {
info!("pos {:?}", m.pos);
}
_ => {}
},
Event::MouseUp(m) => {
info!("up:pos {:?}", m.pos);
self.state = MouseState::Up;
self.t = 0.0;
}
Event::AnimFrame(interval) => {}
_ => (),
}
}
fn update(&mut self, _ctx: &mut UpdateCtx, _old_data: Option<&()>, _data: &(), _env: &Env) {}
fn layout(
&mut self,
_layout_ctx: &mut LayoutCtx,
bc: &BoxConstraints,
_data: &(),
_env: &Env,
) -> Size {
bc.constrain((100.0, 100.0))
}
fn paint(&mut self, paint_ctx: &mut PaintCtx, _data: &(), _env: &Env) {
// let center = Point::new(50.0, 50.0);
// let ambit = center + 45.0 * Vec2::from_angle((0.75 + self.t) * 2.0 * PI);
// paint_ctx.stroke(Line::new(center, ambit), &Color::WHITE, 10.0);
info!("paint");
if let Some(mut old) = self.points.iter().next() {
for new in self.points.iter() {
let p1 = Point::new(1.0, 2.0);
let line = Line::new(*old, *new);
paint_ctx.stroke(line, &Color::WHITE, 1.0);
old = new;
}
}
}
}
// fn main() {
// let window = WindowDesc::new(|| AnimWidget {
// state: MouseState::Up,
// points: Vec::new(),
// t: 0.0,
// });
// AppLauncher::with_window(window)
// .use_simple_logger()
// .launch(0)
// .expect("launch failed");
// }
fn build_app() -> impl Widget<()> {
let solid = Color::rgb8(0x3a, 0x3a, 0x3a);
let gradient = LinearGradient::new(
UnitPoint::TOP_LEFT,
UnitPoint::BOTTOM_RIGHT,
(Color::rgb8(0x11, 0x11, 0x11), Color::rgb8(0xbb, 0xbb, 0xbb)),
);
Flex::column()
.with_child(
Flex::row()
.with_child(
Label::new("top left")
.border(gradient.clone(), 4.0)
.padding(10.0),
1.0,
)
.with_child(
DrawWidget {
state: MouseState::Up,
points: Vec::new(),
t: 0.0,
}
.border(gradient.clone(), 4.0)
.padding(10.0),
1.0,
)
.with_child(
Label::new("top right")
.background(solid.clone())
.padding(10.0),
1.0,
),
1.0,
)
.with_child(
Flex::row()
.with_child(
Label::new("bottom left")
.background(gradient.clone())
.rounded(10.0)
.padding(10.0),
1.0,
)
.with_child(
Label::new("bottom right")
.border(solid.clone(), 4.0)
.rounded(10.0)
.padding(10.0),
1.0,
),
1.0,
)
}
fn main() -> Result<(), PlatformError> {
AppLauncher::with_window(WindowDesc::new(build_app))
.use_simple_logger()
.launch(())?;
Ok(())
}
use druid::widget::{Align, Button, Flex, Label, Padding, WidgetExt};
use druid::{AppLauncher, LocalizedString, Widget, WindowDesc};
use log::info;
fn main() {
let main_window = WindowDesc::new(ui_builder);
let data = 0_u32;
AppLauncher::with_window(main_window)
.use_simple_logger()
.launch(data)
.expect("launch failed");
}
fn ui_builder() -> impl Widget<u32> {
// The label text will be computed dynamically based on the current locale and count
let text =
LocalizedString::new("hello-counter").with_arg("count", |data: &u32, _env| (*data).into());
let label = Label::new(text).padding(5.0).center();
let button = Button::new("increment", |_ctx, data, _env| {
*data += 1;
info!("Button Pressed");
})
.padding(5.0);
Flex::column()
.with_child(label, 1.0)
.with_child(button, 1.0)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment