ui/
example-ui.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
extern crate onyx;

#[derive(Copy, Clone)]
pub enum Action {
    Increment,
    Decrement,
}

struct Scene {
    count: i32,
}

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

    fn new(state: &mut onyx::State<Action>) -> Self {
        let scene = Scene { count: 0 };
        state.ui().build(onyx::ui::panel()
            .layout(onyx::ui::Layout::Vertical)
            .child(onyx::ui::button(|| Action::Increment)
                .child(onyx::ui::label("Increment"))
            )
            .child(onyx::ui::button(|| Action::Decrement)
                .child(onyx::ui::label("Decrement"))
            )
            .child(onyx::ui::label("0").id("count"))
        );
        scene
    }
    
    fn action(&mut self, action: Action, state: &mut onyx::State<Action>) -> onyx::Transition {
        match action {
            Action::Increment => self.count += 1,
            Action::Decrement => self.count -= 1,
        }
        state.ui().set_text("count", &self.count.to_string());
        state.none()
    }
}

fn main() {
    onyx::run::<Scene>("UI", (800, 600));
}