Skip to content

Commit

Permalink
Add spin mode to ferris example 🦀
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed May 3, 2024
1 parent eac5bcb commit 4010e39
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 16 deletions.
26 changes: 25 additions & 1 deletion core/src/angle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{Point, Rectangle, Vector};

use std::f32::consts::{FRAC_PI_2, PI};
use std::ops::{Add, AddAssign, Div, Mul, RangeInclusive, Sub, SubAssign};
use std::ops::{Add, AddAssign, Div, Mul, RangeInclusive, Rem, Sub, SubAssign};

/// Degrees
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
Expand Down Expand Up @@ -48,6 +48,14 @@ impl From<Degrees> for f64 {
}
}

impl Mul<f32> for Degrees {
type Output = Degrees;

fn mul(self, rhs: f32) -> Self::Output {
Self(self.0 * rhs)
}
}

impl num_traits::FromPrimitive for Degrees {
fn from_i64(n: i64) -> Option<Self> {
Some(Self(n as f32))
Expand Down Expand Up @@ -156,6 +164,14 @@ impl Add for Radians {
}
}

impl Add<Degrees> for Radians {
type Output = Self;

fn add(self, rhs: Degrees) -> Self::Output {
Self(self.0 + rhs.0.to_radians())
}
}

impl AddAssign for Radians {
fn add_assign(&mut self, rhs: Radians) {
self.0 = self.0 + rhs.0;
Expand Down Expand Up @@ -202,6 +218,14 @@ impl Div for Radians {
}
}

impl Rem for Radians {
type Output = Self;

fn rem(self, rhs: Self) -> Self::Output {
Self(self.0 % rhs.0)
}
}

impl PartialEq<f32> for Radians {
fn eq(&self, other: &f32) -> bool {
self.0.eq(other)
Expand Down
7 changes: 7 additions & 0 deletions core/src/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ impl Rotation {
}
}

/// Returns a mutable reference to the angle of the [`Rotation`] in [`Radians`].
pub fn radians_mut(&mut self) -> &mut Radians {
match self {
Rotation::Floating(radians) | Rotation::Solid(radians) => radians,
}
}

/// Returns the angle of the [`Rotation`] in [`Degrees`].
pub fn degrees(self) -> Degrees {
Degrees(self.radians().0.to_degrees())
Expand Down
2 changes: 1 addition & 1 deletion examples/ferris/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ publish = false

[dependencies]
iced.workspace = true
iced.features = ["image", "debug"]
iced.features = ["image", "tokio", "debug"]
68 changes: 54 additions & 14 deletions examples/ferris/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use iced::widget::{column, container, image, pick_list, row, slider, text};
use iced::time::Instant;
use iced::widget::{
checkbox, column, container, image, pick_list, row, slider, text,
};
use iced::window;
use iced::{
Alignment, Color, ContentFit, Degrees, Element, Length, Rotation, Theme,
Alignment, Color, ContentFit, Degrees, Element, Length, Radians, Rotation,
Subscription, Theme,
};

pub fn main() -> iced::Result {
iced::program("Ferris - Iced", Image::update, Image::view)
.subscription(Image::subscription)
.theme(|_| Theme::TokyoNight)
.run()
}
Expand All @@ -13,6 +19,8 @@ struct Image {
width: f32,
rotation: Rotation,
content_fit: ContentFit,
spin: bool,
last_tick: Instant,
}

#[derive(Debug, Clone, Copy)]
Expand All @@ -21,6 +29,8 @@ enum Message {
RotationStrategyChanged(RotationStrategy),
RotationChanged(Degrees),
ContentFitChanged(ContentFit),
SpinToggled(bool),
RedrawRequested(Instant),
}

impl Image {
Expand Down Expand Up @@ -50,6 +60,29 @@ impl Image {
Message::ContentFitChanged(content_fit) => {
self.content_fit = content_fit;
}
Message::SpinToggled(spin) => {
self.spin = spin;
self.last_tick = Instant::now();
}
Message::RedrawRequested(now) => {
const ROTATION_SPEED: Degrees = Degrees(360.0);

let delta = (now - self.last_tick).as_millis() as f32 / 1_000.0;

*self.rotation.radians_mut() = (self.rotation.radians()
+ ROTATION_SPEED * delta)
% (2.0 * Radians::PI);

self.last_tick = now;
}
}
}

fn subscription(&self) -> Subscription<Message> {
if self.spin {
window::frames().map(Message::RedrawRequested)
} else {
Subscription::none()
}
}

Expand Down Expand Up @@ -111,18 +144,23 @@ impl Image {
Message::RotationStrategyChanged,
)
.width(Length::Fill),
column![
slider(
Degrees::RANGE,
self.rotation.degrees(),
Message::RotationChanged
),
text(format!(
"Rotation: {:.0}°",
f32::from(self.rotation.degrees())
))
.size(14)
.line_height(1.0)
row![
column![
slider(
Degrees::RANGE,
self.rotation.degrees(),
Message::RotationChanged
),
text(format!(
"Rotation: {:.0}°",
f32::from(self.rotation.degrees())
))
.size(14)
.line_height(1.0)
]
.spacing(5)
.align_items(Alignment::Center),
checkbox("Spin!", self.spin).on_toggle(Message::SpinToggled)
]
.spacing(5)
.align_items(Alignment::Center)
Expand All @@ -141,6 +179,8 @@ impl Default for Image {
width: 300.0,
rotation: Rotation::default(),
content_fit: ContentFit::default(),
spin: false,
last_tick: Instant::now(),
}
}
}
Expand Down

0 comments on commit 4010e39

Please sign in to comment.