transition/
example-transition.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
extern crate onyx;

struct Scene1;
struct Scene2;
struct Scene3;

enum Action1 {
    Push2,
    Switch3,
    Quit,
}

enum Action2 {
    Pop,
}

enum Action3 {
    Switch1
}

impl onyx::Scene for Scene1 {
    type Action = Action1;

    fn new(state: &mut onyx::State<Self::Action>) -> Self {
        state.ui().build(onyx::ui::panel()
            .layout(onyx::ui::Layout::Vertical)
            .child(onyx::ui::button(|| Action1::Push2)
                .child(onyx::ui::label("Push 2"))
            )
            .child(onyx::ui::button(|| Action1::Switch3)
                .child(onyx::ui::label("Switch 3"))
            )
            .child(onyx::ui::button(|| Action1::Quit)
                .child(onyx::ui::label("Quit"))
            )
        );
        Scene1
    }

    fn action(&mut self, action: Self::Action, state: &mut onyx::State<Self::Action>) -> onyx::Transition {
        match action {
            Action1::Push2 => state.push::<Scene2>(),
            Action1::Switch3 => state.switch::<Scene3>(),
            Action1::Quit => state.quit(),
        }
    }
}

impl onyx::Scene for Scene2 {
    type Action = Action2;

    fn new(state: &mut onyx::State<Self::Action>) -> Self {
        state.ui().build(onyx::ui::panel()
            .layout(onyx::ui::Layout::Vertical)
            .child(onyx::ui::button(|| Action2::Pop)
                .child(onyx::ui::label("Pop"))
            )
        );
        Scene2
    }

    fn action(&mut self, action: Self::Action, state: &mut onyx::State<Self::Action>) -> onyx::Transition {
        match action {
            Action2::Pop => state.pop(),
        }
    }
}

impl onyx::Scene for Scene3 {
    type Action = Action3;

    fn new(state: &mut onyx::State<Self::Action>) -> Self {
        state.ui().build(onyx::ui::panel()
            .layout(onyx::ui::Layout::Vertical)
            .child(onyx::ui::button(|| Action3::Switch1)
                .child(onyx::ui::label("Switch 1"))
            )
        );
        Scene3
    }

    fn action(&mut self, action: Self::Action, state: &mut onyx::State<Self::Action>) -> onyx::Transition {
        match action {
            Action3::Switch1 => state.switch::<Scene1>(),
        }
    }
}

fn main() {
    onyx::run::<Scene1>("Transition", (800, 600))
}