Implement checkbox::Renderer
in ggez
example
This commit is contained in:
parent
ccb87b12da
commit
eb45c51a7b
@ -2,7 +2,7 @@ mod renderer;
|
||||
mod widget;
|
||||
|
||||
use renderer::Renderer;
|
||||
use widget::{button, Button, Column, Text};
|
||||
use widget::{button, Button, Checkbox, Column, Text};
|
||||
|
||||
use ggez;
|
||||
use ggez::event;
|
||||
@ -43,24 +43,33 @@ impl event::EventHandler for Game {
|
||||
let screen = graphics::screen_coordinates(context);
|
||||
|
||||
let cursor = {
|
||||
let hello = Text::new("Hello, iced!")
|
||||
.horizontal_alignment(iced::text::HorizontalAlignment::Center);
|
||||
let hello = Text::new("Hello, iced!");
|
||||
|
||||
let button = Button::new(&mut self.button, "Press me!").width(200);
|
||||
let checkbox =
|
||||
Checkbox::new(true, "Check me!", Message::CheckboxToggled);
|
||||
|
||||
let button = Button::new(&mut self.button, "Press me!")
|
||||
.width(200)
|
||||
.align_self(iced::Align::End);
|
||||
|
||||
let widgets = Column::new()
|
||||
.max_width(600)
|
||||
.spacing(20)
|
||||
.push(hello)
|
||||
.push(checkbox)
|
||||
.push(button);
|
||||
|
||||
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);
|
||||
.push(widgets);
|
||||
|
||||
let renderer =
|
||||
&mut Renderer::new(context, self.spritesheet.clone());
|
||||
|
||||
let ui: Interface<(), Renderer> =
|
||||
let ui: Interface<Message, Renderer> =
|
||||
Interface::compute(content.into(), renderer);
|
||||
|
||||
let cursor = ui.draw(renderer, iced::Point::new(0.0, 0.0));
|
||||
@ -77,6 +86,11 @@ impl event::EventHandler for Game {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum Message {
|
||||
CheckboxToggled(bool),
|
||||
}
|
||||
|
||||
fn into_cursor_type(cursor: iced::MouseCursor) -> mouse::MouseCursor {
|
||||
match cursor {
|
||||
iced::MouseCursor::OutOfBounds => mouse::MouseCursor::Default,
|
||||
|
@ -1,4 +1,5 @@
|
||||
mod button;
|
||||
mod checkbox;
|
||||
mod text;
|
||||
|
||||
use ggez::graphics::{self, spritebatch::SpriteBatch, Color, Image};
|
||||
@ -18,6 +19,7 @@ impl Renderer<'_> {
|
||||
spritesheet,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn flush(&mut self) {
|
||||
graphics::draw(
|
||||
self.context,
|
||||
|
63
examples/ggez/renderer/checkbox.rs
Normal file
63
examples/ggez/renderer/checkbox.rs
Normal file
@ -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
|
||||
}
|
||||
}
|
||||
}
|
@ -3,3 +3,4 @@ use ggez::graphics::Color;
|
||||
pub use iced::{button, Button, Column, Row};
|
||||
|
||||
pub type Text = iced::Text<Color>;
|
||||
pub type Checkbox<Message> = iced::Checkbox<Color, Message>;
|
||||
|
Loading…
Reference in New Issue
Block a user