diff options
author | 2019-07-22 20:14:23 +0200 | |
---|---|---|
committer | 2019-07-22 20:14:23 +0200 | |
commit | ccb87b12da9f4c8e65571d31da2952ff1b5b08d9 (patch) | |
tree | 098ad52c5da98bc76e30f7ab7d0dd391e8ae581d /examples/ggez/main.rs | |
parent | b4eb0df5a0067d663d09b418246d25a378b29444 (diff) | |
download | iced-ccb87b12da9f4c8e65571d31da2952ff1b5b08d9.tar.gz iced-ccb87b12da9f4c8e65571d31da2952ff1b5b08d9.tar.bz2 iced-ccb87b12da9f4c8e65571d31da2952ff1b5b08d9.zip |
Implement `button::Renderer` in `ggez` example
Diffstat (limited to 'examples/ggez/main.rs')
-rw-r--r-- | examples/ggez/main.rs | 60 |
1 files changed, 50 insertions, 10 deletions
diff --git a/examples/ggez/main.rs b/examples/ggez/main.rs index 87949dee..7deef072 100644 --- a/examples/ggez/main.rs +++ b/examples/ggez/main.rs @@ -2,26 +2,33 @@ mod renderer; mod widget; use renderer::Renderer; -use widget::Text; +use widget::{button, Button, Column, Text}; use ggez; use ggez::event; use ggez::graphics; +use ggez::input::mouse; use iced::Interface; pub fn main() -> ggez::GameResult { let cb = ggez::ContextBuilder::new("iced", "ggez"); let (ctx, event_loop) = &mut cb.build()?; - let state = &mut Game::new()?; + let state = &mut Game::new(ctx)?; event::run(ctx, event_loop, state) } -struct Game {} +struct Game { + spritesheet: graphics::Image, + button: button::State, +} impl Game { - fn new() -> ggez::GameResult<Game> { - Ok(Game {}) + fn new(context: &mut ggez::Context) -> ggez::GameResult<Game> { + Ok(Game { + spritesheet: graphics::Image::new(context, "/ui.png").unwrap(), + button: button::State::new(), + }) } } @@ -33,17 +40,50 @@ 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 renderer = &mut Renderer { context }; + let screen = graphics::screen_coordinates(context); + + let cursor = { + let hello = Text::new("Hello, iced!") + .horizontal_alignment(iced::text::HorizontalAlignment::Center); + + let button = Button::new(&mut self.button, "Press me!").width(200); + + let content = Column::new() + .width(screen.w as u32) + .height(screen.h as u32) + .align_items(iced::Align::Center) + .justify_content(iced::Justify::Center) + .spacing(20) + .push(hello) + .push(button); + + let renderer = + &mut Renderer::new(context, self.spritesheet.clone()); + let ui: Interface<(), Renderer> = - Interface::compute(Text::new("Hello, iced!").into(), renderer); + Interface::compute(content.into(), renderer); - let mouse_cursor = ui.draw(renderer, iced::Point::new(0.0, 0.0)); + let cursor = ui.draw(renderer, iced::Point::new(0.0, 0.0)); renderer.flush(); - } + + cursor + }; + + mouse::set_cursor_type(context, into_cursor_type(cursor)); graphics::present(context)?; Ok(()) } } + +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, + } +} |