anim/
anim.rs

1// Copyright 2019 The Druid Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! An example of an animating widget. It is just a widget that
16//! requests an animation frame when it needs to, and draws the frame in the
17//! `paint` method.
18//! Once the animation is over it simply stops requesting animation frames.
19//! Usually we would put the state in the `Data`, but for things like animation
20//! we don't. This is because the animation state is not useful to know for the
21//! rest of the app. If this is something the rest of your widgets should know
22//! about, you could put it in the `data`.
23
24// On Windows platform, don't show a console when opening the app.
25#![windows_subsystem = "windows"]
26
27use std::f64::consts::PI;
28
29use druid::kurbo::{Circle, Line};
30use druid::widget::prelude::*;
31use druid::{AppLauncher, Color, LocalizedString, Point, Vec2, WindowDesc};
32
33struct AnimWidget {
34    t: f64,
35}
36
37impl Widget<()> for AnimWidget {
38    fn event(&mut self, ctx: &mut EventCtx, event: &Event, _data: &mut (), _env: &Env) {
39        match event {
40            Event::MouseDown(_) => {
41                self.t = 0.0;
42                ctx.request_anim_frame();
43            }
44            Event::AnimFrame(interval) => {
45                ctx.request_paint();
46                self.t += (*interval as f64) * 1e-9;
47                if self.t < 1.0 {
48                    ctx.request_anim_frame();
49                } else {
50                    // We might have t>1.0 at the end of the animation,
51                    // we want to make sure the line points up at the
52                    // end of the animation.
53                    self.t = 0.0;
54                }
55            }
56            _ => (),
57        }
58    }
59
60    fn lifecycle(&mut self, _ctx: &mut LifeCycleCtx, _event: &LifeCycle, _data: &(), _env: &Env) {}
61
62    fn update(&mut self, _ctx: &mut UpdateCtx, _old_data: &(), _data: &(), _env: &Env) {}
63
64    fn layout(
65        &mut self,
66        _layout_ctx: &mut LayoutCtx,
67        bc: &BoxConstraints,
68        _data: &(),
69        _env: &Env,
70    ) -> Size {
71        bc.constrain((100.0, 100.0))
72    }
73
74    fn paint(&mut self, ctx: &mut PaintCtx, _data: &(), _env: &Env) {
75        let t = self.t;
76        let center = Point::new(50.0, 50.0);
77        ctx.paint_with_z_index(1, move |ctx| {
78            let ambit = center + 45.0 * Vec2::from_angle((0.75 + t) * 2.0 * PI);
79            ctx.stroke(Line::new(center, ambit), &Color::WHITE, 1.0);
80        });
81
82        ctx.fill(Circle::new(center, 50.0), &Color::BLACK);
83    }
84}
85
86pub fn main() {
87    let window = WindowDesc::new(AnimWidget { t: 0.0 }).title(
88        LocalizedString::new("anim-demo-window-title")
89            .with_placeholder("You spin me right round..."),
90    );
91    AppLauncher::with_window(window)
92        .log_to_console()
93        .launch(())
94        .expect("launch failed");
95}