summaryrefslogtreecommitdiffstats
path: root/examples/ggez/main.rs
blob: 3c02157a1689b7bb5dcfb0da2988e9c57bba753f (plain) (blame)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
mod renderer;
mod widget;

use renderer::Renderer;
use widget::{button, Button, Checkbox, Column, Text};

use ggez;
use ggez::event;
use ggez::graphics;
use ggez::input::mouse;

pub fn main() -> ggez::GameResult {
    let cb = ggez::ContextBuilder::new("iced", "ggez");
    let (ctx, event_loop) = &mut cb.build()?;
    let state = &mut Game::new(ctx)?;
    event::run(ctx, event_loop, state)
}

struct Game {
    spritesheet: graphics::Image,

    runtime: iced::Runtime,
    button: button::State,
}

impl Game {
    fn new(context: &mut ggez::Context) -> ggez::GameResult<Game> {
        Ok(Game {
            spritesheet: graphics::Image::new(context, "/ui.png").unwrap(),

            runtime: iced::Runtime::new(),
            button: button::State::new(),
        })
    }
}

impl event::EventHandler for Game {
    fn update(&mut self, _ctx: &mut ggez::Context) -> ggez::GameResult {
        Ok(())
    }

    fn mouse_motion_event(
        &mut self,
        _context: &mut ggez::Context,
        x: f32,
        y: f32,
        _dx: f32,
        _dy: f32,
    ) {
        self.runtime.on_event(iced::Event::Mouse(
            iced::input::mouse::Event::CursorMoved { x, y },
        ));
    }

    fn draw(&mut self, context: &mut ggez::Context) -> ggez::GameResult {
        graphics::clear(context, [0.1, 0.2, 0.3, 1.0].into());

        let screen = graphics::screen_coordinates(context);

        let cursor = {
            let hello = Text::new("Hello, iced!");

            let checkbox =
                Checkbox::new(true, "Check me!", Message::CheckboxToggled);

            let button = Button::new(&mut self.button, "Press me!")
                .width(200)
                .align_self(iced::Align::End);

            let widgets = Column::new()
                .max_width(600)
                .spacing(20)
                .push(hello)
                .push(checkbox)
                .push(button);

            let content = Column::new()
                .width(screen.w as u32)
                .height(screen.h as u32)
                .align_items(iced::Align::Center)
                .justify_content(iced::Justify::Center)
                .push(widgets);

            let renderer =
                &mut Renderer::new(context, self.spritesheet.clone());

            let mut ui = self.runtime.compute(content.into(), renderer);

            let messages = ui.update();
            let cursor = ui.draw(renderer);

            renderer.flush();

            cursor
        };

        mouse::set_cursor_type(context, into_cursor_type(cursor));

        graphics::present(context)?;
        Ok(())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Message {
    CheckboxToggled(bool),
}

fn into_cursor_type(cursor: iced::MouseCursor) -> mouse::MouseCursor {
    match cursor {
        iced::MouseCursor::OutOfBounds => mouse::MouseCursor::Default,
        iced::MouseCursor::Idle => mouse::MouseCursor::Default,
        iced::MouseCursor::Pointer => mouse::MouseCursor::Hand,
        iced::MouseCursor::Working => mouse::MouseCursor::Progress,
        iced::MouseCursor::Grab => mouse::MouseCursor::Grab,
        iced::MouseCursor::Grabbing => mouse::MouseCursor::Grabbing,
    }
}