Show Coffee's UI tour in ggez example
This commit is contained in:
parent
8f6ea4bdc9
commit
666e6761bc
2 changed files with 618 additions and 28 deletions
|
|
@ -1,8 +1,10 @@
|
|||
mod renderer;
|
||||
mod tour;
|
||||
mod widget;
|
||||
|
||||
use renderer::Renderer;
|
||||
use widget::{button, Button, Checkbox, Column, Text};
|
||||
use tour::Tour;
|
||||
use widget::Column;
|
||||
|
||||
use ggez;
|
||||
use ggez::event;
|
||||
|
|
@ -10,7 +12,13 @@ use ggez::graphics;
|
|||
use ggez::input::mouse;
|
||||
|
||||
pub fn main() -> ggez::GameResult {
|
||||
let cb = ggez::ContextBuilder::new("iced", "ggez");
|
||||
let cb = ggez::ContextBuilder::new("iced", "ggez").window_mode(
|
||||
ggez::conf::WindowMode {
|
||||
width: 1280.0,
|
||||
height: 1024.0,
|
||||
..ggez::conf::WindowMode::default()
|
||||
},
|
||||
);
|
||||
let (ctx, event_loop) = &mut cb.build()?;
|
||||
let state = &mut Game::new(ctx)?;
|
||||
event::run(ctx, event_loop, state)
|
||||
|
|
@ -20,16 +28,18 @@ struct Game {
|
|||
spritesheet: graphics::Image,
|
||||
|
||||
runtime: iced::Runtime,
|
||||
button: button::State,
|
||||
tour: Tour,
|
||||
}
|
||||
|
||||
impl Game {
|
||||
fn new(context: &mut ggez::Context) -> ggez::GameResult<Game> {
|
||||
graphics::set_default_filter(context, graphics::FilterMode::Nearest);
|
||||
|
||||
Ok(Game {
|
||||
spritesheet: graphics::Image::new(context, "/ui.png").unwrap(),
|
||||
|
||||
runtime: iced::Runtime::new(),
|
||||
button: button::State::new(),
|
||||
tour: Tour::new(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -39,6 +49,36 @@ impl event::EventHandler for Game {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn mouse_button_down_event(
|
||||
&mut self,
|
||||
_context: &mut ggez::Context,
|
||||
button: mouse::MouseButton,
|
||||
_x: f32,
|
||||
_y: f32,
|
||||
) {
|
||||
self.runtime.on_event(iced::Event::Mouse(
|
||||
iced::input::mouse::Event::Input {
|
||||
state: iced::input::ButtonState::Pressed,
|
||||
button: iced::input::mouse::Button::Left,
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
fn mouse_button_up_event(
|
||||
&mut self,
|
||||
_context: &mut ggez::Context,
|
||||
button: mouse::MouseButton,
|
||||
_x: f32,
|
||||
_y: f32,
|
||||
) {
|
||||
self.runtime.on_event(iced::Event::Mouse(
|
||||
iced::input::mouse::Event::Input {
|
||||
state: iced::input::ButtonState::Released,
|
||||
button: iced::input::mouse::Button::Left,
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
fn mouse_motion_event(
|
||||
&mut self,
|
||||
_context: &mut ggez::Context,
|
||||
|
|
@ -52,34 +92,40 @@ impl event::EventHandler for Game {
|
|||
));
|
||||
}
|
||||
|
||||
fn resize_event(
|
||||
&mut self,
|
||||
context: &mut ggez::Context,
|
||||
width: f32,
|
||||
height: f32,
|
||||
) {
|
||||
graphics::set_screen_coordinates(
|
||||
context,
|
||||
graphics::Rect {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
w: width,
|
||||
h: height,
|
||||
},
|
||||
)
|
||||
.expect("Set screen coordinates");
|
||||
}
|
||||
|
||||
fn draw(&mut self, context: &mut ggez::Context) -> ggez::GameResult {
|
||||
graphics::clear(context, [0.1, 0.2, 0.3, 1.0].into());
|
||||
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 cursor = {
|
||||
let hello = Text::new("Hello, iced!");
|
||||
|
||||
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 (messages, cursor) = {
|
||||
let layout = self.tour.layout();
|
||||
|
||||
let content = Column::new()
|
||||
.width(screen.w as u32)
|
||||
.height(screen.h as u32)
|
||||
.align_items(iced::Align::Center)
|
||||
.justify_content(iced::Justify::Center)
|
||||
.push(widgets);
|
||||
.push(layout);
|
||||
|
||||
let renderer =
|
||||
&mut Renderer::new(context, self.spritesheet.clone());
|
||||
|
|
@ -91,9 +137,13 @@ impl event::EventHandler for Game {
|
|||
|
||||
renderer.flush();
|
||||
|
||||
cursor
|
||||
(messages, cursor)
|
||||
};
|
||||
|
||||
for message in messages {
|
||||
self.tour.react(message);
|
||||
}
|
||||
|
||||
mouse::set_cursor_type(context, into_cursor_type(cursor));
|
||||
|
||||
graphics::present(context)?;
|
||||
|
|
@ -101,11 +151,6 @@ 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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue