diff options
Diffstat (limited to '')
-rw-r--r-- | examples/ggez/renderer.rs | 26 | ||||
-rw-r--r-- | examples/ggez/renderer/button.rs | 1 | ||||
-rw-r--r-- | examples/ggez/renderer/debugger.rs | 30 | ||||
-rw-r--r-- | examples/ggez/renderer/image.rs | 51 | ||||
-rw-r--r-- | examples/ggez/renderer/text.rs | 17 |
5 files changed, 120 insertions, 5 deletions
diff --git a/examples/ggez/renderer.rs b/examples/ggez/renderer.rs index ccf12aba..8746dd96 100644 --- a/examples/ggez/renderer.rs +++ b/examples/ggez/renderer.rs @@ -1,24 +1,38 @@ mod button; mod checkbox; +mod debugger; +mod image; mod radio; mod slider; mod text; -use ggez::graphics::{self, spritebatch::SpriteBatch, Image}; +use ggez::graphics::{ + self, spritebatch::SpriteBatch, Font, Image, MeshBuilder, +}; use ggez::Context; pub struct Renderer<'a> { pub context: &'a mut Context, pub sprites: SpriteBatch, pub spritesheet: Image, + pub font: Font, + font_size: f32, + debug_mesh: Option<MeshBuilder>, } impl Renderer<'_> { - pub fn new(context: &mut Context, spritesheet: Image) -> Renderer { + pub fn new( + context: &mut Context, + spritesheet: Image, + font: Font, + ) -> Renderer { Renderer { context, sprites: SpriteBatch::new(spritesheet.clone()), spritesheet, + font, + font_size: 20.0, + debug_mesh: None, } } @@ -37,5 +51,13 @@ impl Renderer<'_> { graphics::FilterMode::Linear, ) .expect("Draw text"); + + if let Some(debug_mesh) = self.debug_mesh.take() { + let mesh = + debug_mesh.build(self.context).expect("Build debug mesh"); + + graphics::draw(self.context, &mesh, graphics::DrawParam::default()) + .expect("Draw debug mesh"); + } } } diff --git a/examples/ggez/renderer/button.rs b/examples/ggez/renderer/button.rs index fc3ea7ca..486e07ed 100644 --- a/examples/ggez/renderer/button.rs +++ b/examples/ggez/renderer/button.rs @@ -104,6 +104,7 @@ impl button::Renderer for Renderer<'_> { let mut text = Text::new(TextFragment { text: String::from(label), + font: Some(self.font), scale: Some(Scale { x: 20.0, y: 20.0 }), ..Default::default() }); diff --git a/examples/ggez/renderer/debugger.rs b/examples/ggez/renderer/debugger.rs new file mode 100644 index 00000000..98124795 --- /dev/null +++ b/examples/ggez/renderer/debugger.rs @@ -0,0 +1,30 @@ +use super::Renderer; +use ggez::graphics::{Color, DrawMode, MeshBuilder, Rect}; + +impl iced::renderer::Debugger for Renderer<'_> { + type Color = Color; + + fn explain(&mut self, layout: &iced::Layout<'_>, color: Color) { + let bounds = layout.bounds(); + + let mut debug_mesh = + self.debug_mesh.take().unwrap_or(MeshBuilder::new()); + + debug_mesh.rectangle( + DrawMode::stroke(1.0), + Rect { + x: bounds.x, + y: bounds.y, + w: bounds.width, + h: bounds.height, + }, + color, + ); + + self.debug_mesh = Some(debug_mesh); + + for child in layout.children() { + self.explain(&child, color); + } + } +} diff --git a/examples/ggez/renderer/image.rs b/examples/ggez/renderer/image.rs new file mode 100644 index 00000000..c3ead5c9 --- /dev/null +++ b/examples/ggez/renderer/image.rs @@ -0,0 +1,51 @@ +use super::Renderer; + +use ggez::{graphics, nalgebra}; +use iced::image; + +impl image::Renderer<graphics::Image> for Renderer<'_> { + fn node( + &self, + style: iced::Style, + image: &graphics::Image, + width: Option<u16>, + height: Option<u16>, + _source: Option<iced::Rectangle<u16>>, + ) -> iced::Node { + let aspect_ratio = image.width() as f32 / image.height() as f32; + + let style = match (width, height) { + (Some(width), Some(height)) => style.width(width).height(height), + (Some(width), None) => style + .width(width) + .height((width as f32 / aspect_ratio).round() as u16), + (None, Some(height)) => style + .height(height) + .width((height as f32 * aspect_ratio).round() as u16), + (None, None) => style.width(image.width()).height(image.height()), + }; + + iced::Node::new(style) + } + + fn draw( + &mut self, + image: &graphics::Image, + bounds: iced::Rectangle, + _source: Option<iced::Rectangle<u16>>, + ) { + // We should probably use batches to draw images efficiently and keep + // draw side-effect free, but this is good enough for the example. + graphics::draw( + self.context, + image, + graphics::DrawParam::new() + .dest(nalgebra::Point2::new(bounds.x, bounds.y)) + .scale(nalgebra::Vector2::new( + bounds.width / image.width() as f32, + bounds.height / image.height() as f32, + )), + ) + .expect("Draw image"); + } +} diff --git a/examples/ggez/renderer/text.rs b/examples/ggez/renderer/text.rs index a6f782fc..ecf1481e 100644 --- a/examples/ggez/renderer/text.rs +++ b/examples/ggez/renderer/text.rs @@ -6,7 +6,13 @@ use std::cell::RefCell; use std::f32; impl text::Renderer<Color> for Renderer<'_> { - fn node(&self, style: iced::Style, content: &str, size: f32) -> iced::Node { + fn node( + &self, + style: iced::Style, + content: &str, + size: Option<u16>, + ) -> iced::Node { + let font = self.font; let font_cache = graphics::font_cache(self.context); let content = String::from(content); @@ -17,6 +23,7 @@ impl text::Renderer<Color> for Renderer<'_> { // 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); + let size = size.map(f32::from).unwrap_or(self.font_size); iced::Node::with_measure(style, move |bounds| { let mut measure = measure.borrow_mut(); @@ -35,6 +42,7 @@ impl text::Renderer<Color> for Renderer<'_> { let mut text = Text::new(TextFragment { text: content.clone(), + font: Some(font), scale: Some(Scale { x: size, y: size }), ..Default::default() }); @@ -71,13 +79,16 @@ impl text::Renderer<Color> for Renderer<'_> { &mut self, bounds: iced::Rectangle, content: &str, - size: f32, + size: Option<u16>, color: Option<Color>, horizontal_alignment: text::HorizontalAlignment, _vertical_alignment: text::VerticalAlignment, ) { + let size = size.map(f32::from).unwrap_or(self.font_size); + let mut text = Text::new(TextFragment { text: String::from(content), + font: Some(self.font), scale: Some(Scale { x: size, y: size }), ..Default::default() }); @@ -101,7 +112,7 @@ impl text::Renderer<Color> for Renderer<'_> { x: bounds.x, y: bounds.y, }, - color, + color.or(Some(graphics::BLACK)), ); } } |