summaryrefslogtreecommitdiffstats
path: root/examples/tour/src/iced_ggez/main.rs
blob: 72774d38b1b5191e39a43ed15a960a9e1d969cab (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use iced_tour::{iced_ggez, Tour};

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

pub fn main() -> ggez::GameResult {
    let (context, event_loop) = {
        &mut ggez::ContextBuilder::new("iced", "ggez")
            .window_mode(ggez::conf::WindowMode {
                width: 1280.0,
                height: 1024.0,
                resizable: true,
                ..ggez::conf::WindowMode::default()
            })
            .build()?
    };

    filesystem::mount(
        context,
        std::path::Path::new(env!("CARGO_MANIFEST_DIR")),
        true,
    );

    let state = &mut Game::new(context)?;

    event::run(context, event_loop, state)
}

struct Game {
    spritesheet: graphics::Image,
    font: graphics::Font,
    images: iced_ggez::ImageCache,
    tour: Tour,

    events: Vec<iced_native::Event>,
    cache: Option<iced_native::Cache>,
}

impl Game {
    fn new(context: &mut ggez::Context) -> ggez::GameResult<Game> {
        graphics::set_default_filter(context, graphics::FilterMode::Nearest);

        Ok(Game {
            spritesheet: graphics::Image::new(context, "/resources/ui.png")
                .unwrap(),
            font: graphics::Font::new(context, "/resources/Roboto-Regular.ttf")
                .unwrap(),
            images: iced_ggez::ImageCache::new(),
            tour: Tour::new(),

            events: Vec::new(),
            cache: Some(iced_native::Cache::default()),
        })
    }
}

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

    fn mouse_button_down_event(
        &mut self,
        _context: &mut ggez::Context,
        _button: mouse::MouseButton,
        _x: f32,
        _y: f32,
    ) {
        self.events.push(iced_native::Event::Mouse(
            iced_native::input::mouse::Event::Input {
                state: iced_native::input::ButtonState::Pressed,
                button: iced_native::input::mouse::Button::Left, // TODO: Map `button`
            },
        ));
    }

    fn mouse_button_up_event(
        &mut self,
        _context: &mut ggez::Context,
        _button: mouse::MouseButton,
        _x: f32,
        _y: f32,
    ) {
        self.events.push(iced_native::Event::Mouse(
            iced_native::input::mouse::Event::Input {
                state: iced_native::input::ButtonState::Released,
                button: iced_native::input::mouse::Button::Left, // TODO: Map `button`
            },
        ));
    }

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

    fn resize_event(
        &mut self,
        context: &mut ggez::Context,
        width: f32,
        height: f32,
    ) {
        graphics::set_screen_coordinates(
            context,
            graphics::Rect {
                x: 0.0,
                y: 0.0,
                w: width,
                h: height,
            },
        )
        .expect("Set screen coordinates");
    }

    fn draw(&mut self, context: &mut ggez::Context) -> ggez::GameResult {
        graphics::clear(context, graphics::WHITE);

        let screen = graphics::screen_coordinates(context);

        let (messages, cursor) = {
            let view = self.tour.view();

            let content = iced_ggez::Column::new()
                .width(iced_native::Length::Units(screen.w as u16))
                .height(iced_native::Length::Units(screen.h as u16))
                .padding(20)
                .align_items(iced_native::Align::Center)
                .justify_content(iced_native::Justify::Center)
                .push(view);

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

            let mut ui = iced_native::UserInterface::build(
                content,
                self.cache.take().unwrap(),
                renderer,
            );

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

            self.cache = Some(ui.into_cache());

            renderer.flush();

            (messages, cursor)
        };

        for message in messages {
            self.tour.update(message);
        }

        let cursor_type = into_cursor_type(cursor);

        if mouse::cursor_type(context) != cursor_type {
            mouse::set_cursor_type(context, cursor_type);
        }

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

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