summaryrefslogtreecommitdiffstats
path: root/examples/ggez/renderer/checkbox.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-07-22 21:01:59 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-07-22 21:01:59 +0200
commiteb45c51a7b8b9a595e318048712362c8d65d77b3 (patch)
treea32b6449e907f23e031405646a8df1cbd6c020b6 /examples/ggez/renderer/checkbox.rs
parentccb87b12da9f4c8e65571d31da2952ff1b5b08d9 (diff)
downloadiced-eb45c51a7b8b9a595e318048712362c8d65d77b3.tar.gz
iced-eb45c51a7b8b9a595e318048712362c8d65d77b3.tar.bz2
iced-eb45c51a7b8b9a595e318048712362c8d65d77b3.zip
Implement `checkbox::Renderer` in `ggez` example
Diffstat (limited to '')
-rw-r--r--examples/ggez/renderer/checkbox.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/examples/ggez/renderer/checkbox.rs b/examples/ggez/renderer/checkbox.rs
new file mode 100644
index 00000000..eac90ac2
--- /dev/null
+++ b/examples/ggez/renderer/checkbox.rs
@@ -0,0 +1,63 @@
+use super::Renderer;
+use ggez::graphics::{DrawParam, Rect};
+use iced::{checkbox, MouseCursor};
+
+const SPRITE: Rect = Rect {
+ x: 98.0,
+ y: 0.0,
+ w: 28.0,
+ h: 28.0,
+};
+
+impl checkbox::Renderer for Renderer<'_> {
+ fn draw(
+ &mut self,
+ cursor_position: iced::Point,
+ bounds: iced::Rectangle<f32>,
+ text_bounds: iced::Rectangle<f32>,
+ is_checked: bool,
+ ) -> MouseCursor {
+ let mouse_over = bounds.contains(cursor_position)
+ || text_bounds.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_checked {
+ 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
+ }
+ }
+}