diff --git a/examples/ggez/renderer.rs b/examples/ggez/renderer.rs index 74fc581d..2e2e4cb4 100644 --- a/examples/ggez/renderer.rs +++ b/examples/ggez/renderer.rs @@ -1,5 +1,7 @@ mod button; mod checkbox; +mod radio; +mod slider; mod text; use ggez::graphics::{self, spritebatch::SpriteBatch, Color, Image}; diff --git a/examples/ggez/renderer/checkbox.rs b/examples/ggez/renderer/checkbox.rs index eac90ac2..1930631d 100644 --- a/examples/ggez/renderer/checkbox.rs +++ b/examples/ggez/renderer/checkbox.rs @@ -1,4 +1,5 @@ use super::Renderer; + use ggez::graphics::{DrawParam, Rect}; use iced::{checkbox, MouseCursor}; diff --git a/examples/ggez/renderer/radio.rs b/examples/ggez/renderer/radio.rs new file mode 100644 index 00000000..64310f9b --- /dev/null +++ b/examples/ggez/renderer/radio.rs @@ -0,0 +1,63 @@ +use super::Renderer; + +use ggez::graphics::{DrawParam, Rect}; +use iced::{radio, MouseCursor, Point, Rectangle}; + +const SPRITE: Rect = Rect { + x: 98.0, + y: 28.0, + w: 28.0, + h: 28.0, +}; + +impl radio::Renderer for Renderer<'_> { + fn draw( + &mut self, + cursor_position: Point, + bounds: Rectangle, + bounds_with_label: Rectangle, + is_selected: bool, + ) -> MouseCursor { + let mouse_over = bounds_with_label.contains(cursor_position); + + let width = self.spritesheet.width() as f32; + let height = self.spritesheet.height() as f32; + + self.sprites.add(DrawParam { + src: Rect { + x: (SPRITE.x + (if mouse_over { SPRITE.w } else { 0.0 })) + / width, + y: SPRITE.y / height, + w: SPRITE.w / width, + h: SPRITE.h / height, + }, + dest: ggez::mint::Point2 { + x: bounds.x, + y: bounds.y, + }, + ..DrawParam::default() + }); + + if is_selected { + self.sprites.add(DrawParam { + src: Rect { + x: (SPRITE.x + SPRITE.w * 2.0) / width, + y: SPRITE.y / height, + w: SPRITE.w / width, + h: SPRITE.h / height, + }, + dest: ggez::mint::Point2 { + x: bounds.x, + y: bounds.y, + }, + ..DrawParam::default() + }); + } + + if mouse_over { + MouseCursor::Pointer + } else { + MouseCursor::OutOfBounds + } + } +} diff --git a/examples/ggez/renderer/slider.rs b/examples/ggez/renderer/slider.rs new file mode 100644 index 00000000..86757127 --- /dev/null +++ b/examples/ggez/renderer/slider.rs @@ -0,0 +1,82 @@ +use super::Renderer; + +use ggez::graphics::{DrawParam, Rect}; +use iced::{slider, MouseCursor, Point, Rectangle}; +use std::ops::RangeInclusive; + +const RAIL: Rect = Rect { + x: 98.0, + y: 56.0, + w: 1.0, + h: 4.0, +}; + +const MARKER: Rect = Rect { + x: RAIL.x + 28.0, + y: RAIL.y, + w: 16.0, + h: 24.0, +}; + +impl slider::Renderer for Renderer<'_> { + fn draw( + &mut self, + cursor_position: Point, + bounds: Rectangle, + state: &slider::State, + range: RangeInclusive, + value: f32, + ) -> MouseCursor { + let width = self.spritesheet.width() as f32; + let height = self.spritesheet.height() as f32; + + self.sprites.add(DrawParam { + src: Rect { + x: RAIL.x / width, + y: RAIL.y / height, + w: RAIL.w / width, + h: RAIL.h / height, + }, + dest: ggez::mint::Point2 { + x: bounds.x + MARKER.w as f32 / 2.0, + y: bounds.y + 12.5, + }, + scale: ggez::mint::Vector2 { + x: bounds.width - MARKER.w as f32, + y: 1.0, + }, + ..DrawParam::default() + }); + + let (range_start, range_end) = range.into_inner(); + + let marker_offset = (bounds.width - MARKER.w as f32) + * ((value - range_start) / (range_end - range_start).max(1.0)); + + let mouse_over = bounds.contains(cursor_position); + let is_active = state.is_dragging() || mouse_over; + + self.sprites.add(DrawParam { + src: Rect { + x: (MARKER.x + (if is_active { MARKER.w } else { 0.0 })) + / width, + y: MARKER.y / height, + w: MARKER.w / width, + h: MARKER.h / height, + }, + dest: ggez::mint::Point2 { + x: bounds.x + marker_offset.round(), + y: bounds.y + (if state.is_dragging() { 2.0 } else { 0.0 }), + }, + ..DrawParam::default() + }); + + if state.is_dragging() { + MouseCursor::Grabbing + } else if mouse_over { + MouseCursor::Grab + } else { + MouseCursor::OutOfBounds + } + } +} diff --git a/examples/ggez/widget.rs b/examples/ggez/widget.rs index 00c013d4..a5fcccc6 100644 --- a/examples/ggez/widget.rs +++ b/examples/ggez/widget.rs @@ -1,6 +1,13 @@ +use super::Renderer; + use ggez::graphics::Color; -pub use iced::{button, Button, Column, Row}; +pub use iced::{button, slider, Button, Slider}; pub type Text = iced::Text; pub type Checkbox = iced::Checkbox; +pub type Radio = iced::Radio; + +pub type Column<'a, Message> = iced::Column<'a, Message, Renderer<'a>>; +pub type Row<'a, Message> = iced::Row<'a, Message, Renderer<'a>>; +pub type Element<'a, Message> = iced::Element<'a, Message, Renderer<'a>>;