Skip to content

Commit

Permalink
chore: fix pong example
Browse files Browse the repository at this point in the history
  • Loading branch information
wash2 committed Feb 5, 2024
1 parent 767066f commit f6ce981
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 41 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ libcosmic = ["dep:libcosmic"]
members = [
"examples/*"
]
exclude = [
"examples/stopwatch"
]

[dependencies]
iced = { git = "https://github.com/iced-rs/iced", rev = "5540ac0", features = ["tokio"], optional = true }
Expand Down
42 changes: 18 additions & 24 deletions examples/pong/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ use iced_core::{
Point, Rectangle, Shell, Size, Widget,
};

use crate::theme::Theme;

/// A simple widget that layers one above another.
pub struct Layer<'a, Message, Renderer> {
base: Element<'a, Message, Renderer>,
layer: Element<'a, Message, Renderer>,
base: Element<'a, Message, Theme, Renderer>,
layer: Element<'a, Message, Theme, Renderer>,
}

impl<'a, Message, Renderer> Layer<'a, Message, Renderer> {
/// Returns a new [`Layer`]
pub fn new(
base: impl Into<Element<'a, Message, Renderer>>,
layer: impl Into<Element<'a, Message, Renderer>>,
base: impl Into<Element<'a, Message, Theme, Renderer>>,
layer: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Self {
Self {
base: base.into(),
Expand All @@ -31,7 +33,7 @@ impl<'a, Message, Renderer> Layer<'a, Message, Renderer> {
}
}

impl<'a, Message, Renderer> Widget<Message, Renderer> for Layer<'a, Message, Renderer>
impl<'a, Message, Renderer> Widget<Message, Theme, Renderer> for Layer<'a, Message, Renderer>
where
Renderer: iced_core::Renderer,
Message: Clone,
Expand All @@ -44,12 +46,8 @@ where
tree.diff_children(&[&self.base, &self.layer]);
}

fn width(&self) -> Length {
self.base.as_widget().width()
}

fn height(&self) -> Length {
self.base.as_widget().height()
fn size(&self) -> Size<Length> {
self.base.as_widget().size()
}

fn layout(
Expand Down Expand Up @@ -90,7 +88,7 @@ where
&self,
state: &Tree,
renderer: &mut Renderer,
theme: &<Renderer as iced_core::Renderer>::Theme,
theme: &Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor_position: mouse::Cursor,
Expand All @@ -112,7 +110,7 @@ where
state: &'b mut Tree,
layout: Layout<'_>,
_renderer: &Renderer,
) -> Option<overlay::Element<'b, Message, Renderer>> {
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
Some(overlay::Element::new(
layout.position(),
Box::new(Overlay {
Expand Down Expand Up @@ -154,12 +152,12 @@ where
}

struct Overlay<'a, 'b, Message, Renderer> {
content: &'b mut Element<'a, Message, Renderer>,
content: &'b mut Element<'a, Message, Theme, Renderer>,
tree: &'b mut Tree,
size: Size,
}

impl<'a, 'b, Message, Renderer> overlay::Overlay<Message, Renderer>
impl<'a, 'b, Message, Renderer> overlay::Overlay<Message, Theme, Renderer>
for Overlay<'a, 'b, Message, Renderer>
where
Renderer: iced_core::Renderer,
Expand All @@ -180,10 +178,7 @@ where
.content
.as_widget()
.layout(&mut self.tree.children[0], renderer, &limits);
let mut node = layout::Node::with_children(self.size, vec![child]);
node.move_to(position);

node
layout::Node::with_children(self.size, vec![child]).move_to(position)
}

fn on_event(
Expand All @@ -210,17 +205,15 @@ where
fn draw(
&self,
renderer: &mut Renderer,
theme: &Renderer::Theme,
theme: &Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor_position: mouse::Cursor,
) {
renderer.fill_quad(
renderer::Quad {
bounds: layout.bounds(),
border_radius: iced_core::BorderRadius::from(0.0),
border_width: 0.0,
border_color: Color::TRANSPARENT,
..Default::default()
},
Color {
a: 0.,
Expand Down Expand Up @@ -270,7 +263,8 @@ where
}
}

impl<'a, Message, Renderer> From<Layer<'a, Message, Renderer>> for Element<'a, Message, Renderer>
impl<'a, Message, Renderer> From<Layer<'a, Message, Renderer>>
for Element<'a, Message, Theme, Renderer>
where
Renderer: 'a + iced_core::Renderer,
Message: 'a + Clone,
Expand Down
26 changes: 15 additions & 11 deletions examples/pong/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cosmic_time::reexports::iced_core::keyboard::key::Named;
use cosmic_time::reexports::iced_futures::event::listen_raw;
use iced::event;
use iced::keyboard::{self, KeyCode};
use iced::keyboard;

use iced::widget::{column, container, row, Space};
use iced::{executor, Application, Command, Event, Length, Settings, Subscription};
Expand Down Expand Up @@ -96,20 +97,23 @@ impl Application for Pong {
self.timeline.as_subscription::<Event>().map(Message::Tick),
listen_raw(|event, status| match (event, status) {
(
Event::Keyboard(keyboard::Event::KeyPressed {
key_code,
modifiers: _,
}),
Event::Keyboard(keyboard::Event::KeyPressed { key, .. }),
event::Status::Ignored,
) => match key_code {
KeyCode::W => Some(Message::Paddle(Paddle::LeftUp)),
KeyCode::S => Some(Message::Paddle(Paddle::LeftDown)),
KeyCode::Up => Some(Message::Paddle(Paddle::RightUp)),
KeyCode::Down => Some(Message::Paddle(Paddle::RightDown)),
) => match key {
keyboard::Key::Character(c) if c == "w" => {
Some(Message::Paddle(Paddle::LeftUp))
}
keyboard::Key::Character(c) if c == "s" => {
Some(Message::Paddle(Paddle::LeftDown))
}
keyboard::Key::Named(Named::ArrowUp) => Some(Message::Paddle(Paddle::RightUp)),
keyboard::Key::Named(Named::ArrowDown) => {
Some(Message::Paddle(Paddle::RightDown))
}
_ => None,
},
(
Event::Window(window::Event::Resized { width, height }),
Event::Window(_, window::Event::Resized { width, height }),
event::Status::Ignored,
) => Some(Message::WindowResized(width, height)),
_ => None,
Expand Down
10 changes: 7 additions & 3 deletions examples/pong/src/theme.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cosmic_time::reexports::iced;
use cosmic_time::reexports::iced_core::Border;
use iced::widget::{container, text};
use iced::{application, color};

Expand Down Expand Up @@ -49,7 +50,10 @@ impl container::StyleSheet for Theme {
},
Container::Ball => container::Appearance {
background: Some(color!(0xff, 0xff, 0xff).into()),
border_radius: 100000.0.into(),
border: Border {
radius: 100000.0.into(),
..Default::default()
},
..Default::default()
},
}
Expand All @@ -62,7 +66,7 @@ pub mod widget {

use crate::theme::Theme;

pub type Renderer = iced::Renderer<Theme>;
pub type Element<'a, Message> = iced::Element<'a, Message, Renderer>;
pub type Renderer = iced::Renderer;
pub type Element<'a, Message> = iced::Element<'a, Message, Theme, Renderer>;
pub type Container<'a, Message> = iced::widget::Container<'a, Message, Renderer>;
}

0 comments on commit f6ce981

Please sign in to comment.