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

dial envelope (obsolete tests removed)

parent 76975ed2
No related branches found
No related tags found
No related merge requests found
// 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::{BezPath, 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, WheelEvent, Widget,
WindowDesc,
};
use log::info;
enum MouseState {
Up,
Down,
}
struct DrawWidget {
state: MouseState,
points: Vec<Point>,
}
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);
//info!("down: pos {:?}, pos_rel {:?}", m.pos);
self.state = MouseState::Down;
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;
}
_ => {}
}
}
fn update(&mut self, ctx: &mut UpdateCtx, _old_data: Option<&()>, _data: &(), env: &Env) {
//info!("update {:?}", env.get());
ctx.invalidate();
}
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) {
info!("paint");
if let Some(p) = self.points.iter().next() {
let mut path = BezPath::new();
path.move_to(*p);
for p in self.points.iter() {
path.line_to(*p);
}
paint_ctx.stroke(path, &Color::WHITE, 1.0);
}
}
}
#[derive(Debug)]
struct DialWidget {
size: Size,
down_pos: Option<Point>,
cur_pos: Point,
value: f64, // in range 0.0..= 1.0
}
impl Widget<()> for DialWidget {
fn event(&mut self, ctx: &mut EventCtx, event: &Event, _data: &mut (), _env: &Env) {
match event {
Event::MouseDown(m) => {
info!("down: pos {:?}", m.pos);
//info!("down: pos {:?}, pos_rel {:?}", m.pos);
self.down_pos = Some(m.pos);
}
Event::MouseMoved(m) => match self.down_pos {
Some(down_pos) => {
self.cur_pos = m.pos;
info!("down_pos {:?}, cur_pos {:?}", down_pos, m.pos);
}
_ => {}
},
Event::MouseUp(m) => {
info!("up:pos {:?}", m.pos);
self.value = self.value + 1.0;
}
Event::Wheel(w) => {
info!("Event {:?}", w.delta);
let mut delta = w.delta.y / (120 * 10) as f64; // 1% for my machine
if w.mods.shift {
delta = delta / 10.0;
};
if w.mods.ctrl {
delta = delta / 10.0;
};
self.value -= delta;
self.value = f64::max(0.0, f64::min(1.0, self.value));
ctx.invalidate();
}
_ => (),
}
}
fn update(&mut self, ctx: &mut UpdateCtx, _old_data: Option<&()>, _data: &(), env: &Env) {
//info!("update {:?}", env.get());
ctx.invalidate();
}
fn layout(
&mut self,
_layout_ctx: &mut LayoutCtx,
bc: &BoxConstraints,
_data: &(),
_env: &Env,
) -> Size {
bc.constrain(self.size) // size of dial
}
fn paint(&mut self, paint_ctx: &mut PaintCtx, _data: &(), _env: &Env) {
info!("dial paint");
// far some reason origin (0,0) seems not to point to the border not the interior
let center = Point::new(self.size.width / 2.0, self.size.height / 2.0);
let diameter = 0.8 * self.size.width / 2.0;
let mut path = BezPath::new();
let ambit0 = center + diameter * Vec2::from_angle(0.0);
path.move_to(ambit0);
for v in (0..360).step_by(10) {
let ambit = center + diameter * Vec2::from_angle(v as f64 / 360.0 * 2.0 * PI);
path.line_to(ambit);
}
path.close_path();
paint_ctx.stroke(path, &Color::WHITE, 1.0);
let mut path = BezPath::new();
path.move_to(center);
path.line_to(
center + diameter * Vec2::from_angle(self.value * 2.0 * 0.75 * PI + 2.0 * PI * 0.375),
);
paint_ctx.stroke(path, &Color::WHITE, 1.0);
}
}
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(
DialWidget {
size: Size::new(100.0, 100.0),
down_pos: None,
cur_pos: Point::new(0.0, 0.0),
value: 0.0,
}
.border(gradient.clone(), 4.0)
.padding(10.0),
0.0, // horizontal
)
.with_child(
DrawWidget {
state: MouseState::Up,
points: Vec::new(),
}
.border(gradient.clone(), 4.0)
.padding(10.0),
0.0,
)
.with_child(
Label::new("top right")
.background(solid.clone())
.padding(10.0),
1.0,
),
0.0, // vertical
)
.with_child(
Flex::row()
.with_child(
Label::new("bottom left")
.background(gradient.clone())
.rounded(10.0)
.padding(10.0),
0.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(())
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment