diff options
author | 2019-07-23 12:07:32 +0200 | |
---|---|---|
committer | 2019-07-23 12:07:32 +0200 | |
commit | 666e6761bcabf454d0a5d8c35a6fefc1e49a16aa (patch) | |
tree | b426ed1e5c258bdcb9cf16cf0c3f3d20364b07a3 /examples/ggez/main.rs | |
parent | 8f6ea4bdc99ef5960316d2230676495f2d90c30e (diff) | |
download | iced-666e6761bcabf454d0a5d8c35a6fefc1e49a16aa.tar.gz iced-666e6761bcabf454d0a5d8c35a6fefc1e49a16aa.tar.bz2 iced-666e6761bcabf454d0a5d8c35a6fefc1e49a16aa.zip |
Show Coffee's UI tour in `ggez` example
Diffstat (limited to 'examples/ggez/main.rs')
-rw-r--r-- | examples/ggez/main.rs | 101 |
1 files changed, 73 insertions, 28 deletions
diff --git a/examples/ggez/main.rs b/examples/ggez/main.rs index 3c02157a..8f1393ab 100644 --- a/examples/ggez/main.rs +++ b/examples/ggez/main.rs @@ -1,8 +1,10 @@ mod renderer; +mod tour; mod widget; use renderer::Renderer; -use widget::{button, Button, Checkbox, Column, Text}; +use tour::Tour; +use widget::Column; use ggez; use ggez::event; @@ -10,7 +12,13 @@ use ggez::graphics; use ggez::input::mouse; pub fn main() -> ggez::GameResult { - let cb = ggez::ContextBuilder::new("iced", "ggez"); + let cb = ggez::ContextBuilder::new("iced", "ggez").window_mode( + ggez::conf::WindowMode { + width: 1280.0, + height: 1024.0, + ..ggez::conf::WindowMode::default() + }, + ); let (ctx, event_loop) = &mut cb.build()?; let state = &mut Game::new(ctx)?; event::run(ctx, event_loop, state) @@ -20,16 +28,18 @@ struct Game { spritesheet: graphics::Image, runtime: iced::Runtime, - button: button::State, + tour: Tour, } 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, "/ui.png").unwrap(), runtime: iced::Runtime::new(), - button: button::State::new(), + tour: Tour::new(), }) } } @@ -39,6 +49,36 @@ impl event::EventHandler for Game { Ok(()) } + fn mouse_button_down_event( + &mut self, + _context: &mut ggez::Context, + button: mouse::MouseButton, + _x: f32, + _y: f32, + ) { + self.runtime.on_event(iced::Event::Mouse( + iced::input::mouse::Event::Input { + state: iced::input::ButtonState::Pressed, + button: iced::input::mouse::Button::Left, + }, + )); + } + + fn mouse_button_up_event( + &mut self, + _context: &mut ggez::Context, + button: mouse::MouseButton, + _x: f32, + _y: f32, + ) { + self.runtime.on_event(iced::Event::Mouse( + iced::input::mouse::Event::Input { + state: iced::input::ButtonState::Released, + button: iced::input::mouse::Button::Left, + }, + )); + } + fn mouse_motion_event( &mut self, _context: &mut ggez::Context, @@ -52,34 +92,40 @@ impl event::EventHandler for Game { )); } - 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); + 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"); + } - let cursor = { - let hello = Text::new("Hello, iced!"); + fn draw(&mut self, context: &mut ggez::Context) -> ggez::GameResult { + graphics::clear(context, [0.3, 0.3, 0.6, 1.0].into()); - let checkbox = - Checkbox::new(true, "Check me!", Message::CheckboxToggled); + self.tour.draw(context).expect("Draw tour"); - let button = Button::new(&mut self.button, "Press me!") - .width(200) - .align_self(iced::Align::End); + let screen = graphics::screen_coordinates(context); - let widgets = Column::new() - .max_width(600) - .spacing(20) - .push(hello) - .push(checkbox) - .push(button); + let (messages, cursor) = { + let layout = self.tour.layout(); 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); + .push(layout); let renderer = &mut Renderer::new(context, self.spritesheet.clone()); @@ -91,9 +137,13 @@ impl event::EventHandler for Game { renderer.flush(); - cursor + (messages, cursor) }; + for message in messages { + self.tour.react(message); + } + mouse::set_cursor_type(context, into_cursor_type(cursor)); graphics::present(context)?; @@ -101,11 +151,6 @@ impl event::EventHandler for Game { } } -#[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, |