diff options
Diffstat (limited to 'examples/ggez')
-rw-r--r-- | examples/ggez/main.rs | 63 | ||||
-rw-r--r-- | examples/ggez/renderer.rs | 8 | ||||
-rw-r--r-- | examples/ggez/renderer/button.rs | 2 | ||||
-rw-r--r-- | examples/ggez/renderer/checkbox.rs | 4 | ||||
-rw-r--r-- | examples/ggez/renderer/radio.rs | 4 | ||||
-rw-r--r-- | examples/ggez/renderer/slider.rs | 2 | ||||
-rw-r--r-- | examples/ggez/renderer/text.rs | 22 | ||||
-rw-r--r-- | examples/ggez/tour.rs | 4 |
8 files changed, 59 insertions, 50 deletions
diff --git a/examples/ggez/main.rs b/examples/ggez/main.rs index 4719418e..329bde81 100644 --- a/examples/ggez/main.rs +++ b/examples/ggez/main.rs @@ -8,27 +8,41 @@ use widget::Column; use ggez; use ggez::event; +use ggez::filesystem; use ggez::graphics; use ggez::input::mouse; pub fn main() -> ggez::GameResult { - let cb = ggez::ContextBuilder::new("iced", "ggez").window_mode( - ggez::conf::WindowMode { - width: 1280.0, - height: 1024.0, - ..ggez::conf::WindowMode::default() - }, + let (context, event_loop) = { + &mut ggez::ContextBuilder::new("iced", "ggez") + .window_mode(ggez::conf::WindowMode { + width: 1280.0, + height: 1024.0, + ..ggez::conf::WindowMode::default() + }) + .build()? + }; + + filesystem::mount( + context, + std::path::Path::new(&format!( + "{}/examples/resources", + env!("CARGO_MANIFEST_DIR") + )), + true, ); - let (ctx, event_loop) = &mut cb.build()?; - let state = &mut Game::new(ctx)?; - event::run(ctx, event_loop, state) + + let state = &mut Game::new(context)?; + + event::run(context, event_loop, state) } struct Game { spritesheet: graphics::Image, - - runtime: iced::Runtime, tour: Tour, + + events: Vec<iced::Event>, + cache: Option<iced::Cache>, } impl Game { @@ -37,9 +51,10 @@ impl Game { Ok(Game { spritesheet: graphics::Image::new(context, "/ui.png").unwrap(), - - runtime: iced::Runtime::new(), tour: Tour::new(), + + events: Vec::new(), + cache: Some(iced::Cache::default()), }) } } @@ -52,11 +67,11 @@ impl event::EventHandler for Game { fn mouse_button_down_event( &mut self, _context: &mut ggez::Context, - button: mouse::MouseButton, + _button: mouse::MouseButton, _x: f32, _y: f32, ) { - self.runtime.on_event(iced::Event::Mouse( + self.events.push(iced::Event::Mouse( iced::input::mouse::Event::Input { state: iced::input::ButtonState::Pressed, button: iced::input::mouse::Button::Left, // TODO: Map `button` @@ -67,11 +82,11 @@ impl event::EventHandler for Game { fn mouse_button_up_event( &mut self, _context: &mut ggez::Context, - button: mouse::MouseButton, + _button: mouse::MouseButton, _x: f32, _y: f32, ) { - self.runtime.on_event(iced::Event::Mouse( + self.events.push(iced::Event::Mouse( iced::input::mouse::Event::Input { state: iced::input::ButtonState::Released, button: iced::input::mouse::Button::Left, // TODO: Map `button` @@ -87,7 +102,7 @@ impl event::EventHandler for Game { _dx: f32, _dy: f32, ) { - self.runtime.on_event(iced::Event::Mouse( + self.events.push(iced::Event::Mouse( iced::input::mouse::Event::CursorMoved { x, y }, )); } @@ -113,8 +128,6 @@ impl event::EventHandler for Game { fn draw(&mut self, context: &mut ggez::Context) -> ggez::GameResult { graphics::clear(context, [0.3, 0.3, 0.6, 1.0].into()); - self.tour.draw(context).expect("Draw tour"); - let screen = graphics::screen_coordinates(context); let (messages, cursor) = { @@ -130,11 +143,17 @@ impl event::EventHandler for Game { let renderer = &mut Renderer::new(context, self.spritesheet.clone()); - let mut ui = self.runtime.compute(content.into(), renderer); + let mut ui = iced::UserInterface::build( + content, + self.cache.take().unwrap(), + renderer, + ); - let messages = ui.update(); + let messages = ui.update(self.events.drain(..)); let cursor = ui.draw(renderer); + self.cache = Some(ui.into_cache()); + renderer.flush(); (messages, cursor) diff --git a/examples/ggez/renderer.rs b/examples/ggez/renderer.rs index 2e2e4cb4..ccf12aba 100644 --- a/examples/ggez/renderer.rs +++ b/examples/ggez/renderer.rs @@ -4,7 +4,7 @@ mod radio; mod slider; mod text; -use ggez::graphics::{self, spritebatch::SpriteBatch, Color, Image}; +use ggez::graphics::{self, spritebatch::SpriteBatch, Image}; use ggez::Context; pub struct Renderer<'a> { @@ -39,9 +39,3 @@ impl Renderer<'_> { .expect("Draw text"); } } - -impl iced::Renderer for Renderer<'_> { - type Color = Color; - - fn explain(&mut self, layout: &iced::Layout<'_>, color: Color) {} -} diff --git a/examples/ggez/renderer/button.rs b/examples/ggez/renderer/button.rs index 2423efe1..fc3ea7ca 100644 --- a/examples/ggez/renderer/button.rs +++ b/examples/ggez/renderer/button.rs @@ -29,7 +29,7 @@ impl button::Renderer for Renderer<'_> { fn draw( &mut self, cursor_position: iced::Point, - mut bounds: iced::Rectangle<f32>, + mut bounds: iced::Rectangle, state: &button::State, label: &str, class: button::Class, diff --git a/examples/ggez/renderer/checkbox.rs b/examples/ggez/renderer/checkbox.rs index 1930631d..20a91be5 100644 --- a/examples/ggez/renderer/checkbox.rs +++ b/examples/ggez/renderer/checkbox.rs @@ -14,8 +14,8 @@ impl checkbox::Renderer for Renderer<'_> { fn draw( &mut self, cursor_position: iced::Point, - bounds: iced::Rectangle<f32>, - text_bounds: iced::Rectangle<f32>, + bounds: iced::Rectangle, + text_bounds: iced::Rectangle, is_checked: bool, ) -> MouseCursor { let mouse_over = bounds.contains(cursor_position) diff --git a/examples/ggez/renderer/radio.rs b/examples/ggez/renderer/radio.rs index 64310f9b..0f7815d6 100644 --- a/examples/ggez/renderer/radio.rs +++ b/examples/ggez/renderer/radio.rs @@ -14,8 +14,8 @@ impl radio::Renderer for Renderer<'_> { fn draw( &mut self, cursor_position: Point, - bounds: Rectangle<f32>, - bounds_with_label: Rectangle<f32>, + bounds: Rectangle, + bounds_with_label: Rectangle, is_selected: bool, ) -> MouseCursor { let mouse_over = bounds_with_label.contains(cursor_position); diff --git a/examples/ggez/renderer/slider.rs b/examples/ggez/renderer/slider.rs index 86757127..146cee18 100644 --- a/examples/ggez/renderer/slider.rs +++ b/examples/ggez/renderer/slider.rs @@ -22,7 +22,7 @@ impl slider::Renderer for Renderer<'_> { fn draw( &mut self, cursor_position: Point, - bounds: Rectangle<f32>, + bounds: Rectangle, state: &slider::State, range: RangeInclusive<f32>, value: f32, diff --git a/examples/ggez/renderer/text.rs b/examples/ggez/renderer/text.rs index bfdedf74..a6f782fc 100644 --- a/examples/ggez/renderer/text.rs +++ b/examples/ggez/renderer/text.rs @@ -9,16 +9,16 @@ impl text::Renderer<Color> for Renderer<'_> { fn node(&self, style: iced::Style, content: &str, size: f32) -> iced::Node { let font_cache = graphics::font_cache(self.context); let content = String::from(content); + + // TODO: Investigate why stretch tries to measure this MANY times + // with every ancestor's bounds. + // Bug? Using the library wrong? I should probably open an issue on + // the stretch repository. + // I noticed that the first measure is the one that matters in + // practice. Here, we use a RefCell to store the cached measurement. let measure = RefCell::new(None); iced::Node::with_measure(style, move |bounds| { - // TODO: Investigate why stretch tries to measure this MANY times - // with every ancestor's bounds. - // Bug? Using the library wrong? I should probably open an issue on - // the stretch repository. - // I noticed that the first measure is the one that matters in - // practice. Here, we use a RefCell to store the cached - // measurement. let mut measure = measure.borrow_mut(); if measure.is_none() { @@ -47,7 +47,7 @@ impl text::Renderer<Color> for Renderer<'_> { Align::Left, ); - let (width, height) = text.dimensions(&font_cache); + let (width, height) = font_cache.dimensions(&text); let size = iced::Size { width: width as f32, @@ -69,10 +69,10 @@ impl text::Renderer<Color> for Renderer<'_> { fn draw( &mut self, - bounds: iced::Rectangle<f32>, + bounds: iced::Rectangle, content: &str, size: f32, - color: Color, + color: Option<Color>, horizontal_alignment: text::HorizontalAlignment, _vertical_alignment: text::VerticalAlignment, ) { @@ -101,7 +101,7 @@ impl text::Renderer<Color> for Renderer<'_> { x: bounds.x, y: bounds.y, }, - Some(color), + color, ); } } diff --git a/examples/ggez/tour.rs b/examples/ggez/tour.rs index 3808fdda..19982aa4 100644 --- a/examples/ggez/tour.rs +++ b/examples/ggez/tour.rs @@ -20,10 +20,6 @@ impl Tour { } } - pub fn draw(&mut self, context: &mut ggez::Context) -> ggez::GameResult { - Ok(()) - } - pub fn react(&mut self, event: Message) { match event { Message::BackPressed => { |