summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-16 16:52:21 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-16 16:52:21 +0100
commitbad3b1ac4777b4d9b57c6ba9473fb97753a77124 (patch)
treeb5675155923b3576df03a6ed67d0943807a38493
parent348e00e11cd976a16493f4ce693db614886c1ecd (diff)
downloadiced-bad3b1ac4777b4d9b57c6ba9473fb97753a77124.tar.gz
iced-bad3b1ac4777b4d9b57c6ba9473fb97753a77124.tar.bz2
iced-bad3b1ac4777b4d9b57c6ba9473fb97753a77124.zip
Show name of current `Theme` in `clock` example
-rw-r--r--core/src/angle.rs24
-rw-r--r--examples/clock/src/main.rs33
-rw-r--r--renderer/src/geometry.rs4
-rw-r--r--tiny_skia/src/geometry.rs12
-rw-r--r--wgpu/src/geometry.rs8
5 files changed, 67 insertions, 14 deletions
diff --git a/core/src/angle.rs b/core/src/angle.rs
index 30ddad83..dc3c0e93 100644
--- a/core/src/angle.rs
+++ b/core/src/angle.rs
@@ -7,6 +7,18 @@ use std::ops::{Add, AddAssign, Div, Mul, RangeInclusive, Sub, SubAssign};
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct Degrees(pub f32);
+impl PartialEq<f32> for Degrees {
+ fn eq(&self, other: &f32) -> bool {
+ self.0.eq(other)
+ }
+}
+
+impl PartialOrd<f32> for Degrees {
+ fn partial_cmp(&self, other: &f32) -> Option<std::cmp::Ordering> {
+ self.0.partial_cmp(other)
+ }
+}
+
/// Radians
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct Radians(pub f32);
@@ -140,3 +152,15 @@ impl Div for Radians {
Self(self.0 / rhs.0)
}
}
+
+impl PartialEq<f32> for Radians {
+ fn eq(&self, other: &f32) -> bool {
+ self.0.eq(other)
+ }
+}
+
+impl PartialOrd<f32> for Radians {
+ fn partial_cmp(&self, other: &f32) -> Option<std::cmp::Ordering> {
+ self.0.partial_cmp(other)
+ }
+}
diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs
index fcad5d34..5110c78e 100644
--- a/examples/clock/src/main.rs
+++ b/examples/clock/src/main.rs
@@ -1,8 +1,10 @@
+use iced::alignment;
use iced::mouse;
use iced::widget::canvas::{stroke, Cache, Geometry, LineCap, Path, Stroke};
use iced::widget::{canvas, container};
use iced::{
- Element, Length, Point, Rectangle, Renderer, Subscription, Theme, Vector,
+ Degrees, Element, Font, Length, Point, Rectangle, Renderer, Subscription,
+ Theme, Vector,
};
pub fn main() -> iced::Result {
@@ -133,8 +135,31 @@ impl<Message> canvas::Program<Message> for Clock {
});
frame.with_save(|frame| {
- frame.rotate(hand_rotation(self.now.second(), 60));
+ let rotation = hand_rotation(self.now.second(), 60);
+
+ frame.rotate(rotation);
frame.stroke(&long_hand, thin_stroke());
+
+ let rotate_factor = if rotation < 180.0 { 1.0 } else { -1.0 };
+
+ frame.rotate(Degrees(-90.0 * rotate_factor));
+ frame.fill_text(canvas::Text {
+ content: theme.to_string(),
+ size: 15.into(),
+ position: Point::new(
+ (0.8 * radius - 8.0) * rotate_factor,
+ -8.0,
+ ),
+ color: palette.primary.weak.text,
+ horizontal_alignment: if rotate_factor > 0.0 {
+ alignment::Horizontal::Right
+ } else {
+ alignment::Horizontal::Left
+ },
+ vertical_alignment: alignment::Vertical::Bottom,
+ font: Font::MONOSPACE,
+ ..canvas::Text::default()
+ });
});
});
@@ -142,8 +167,8 @@ impl<Message> canvas::Program<Message> for Clock {
}
}
-fn hand_rotation(n: u8, total: u8) -> f32 {
+fn hand_rotation(n: u8, total: u8) -> Degrees {
let turns = n as f32 / total as f32;
- 2.0 * std::f32::consts::PI * turns
+ Degrees(360.0 * turns)
}
diff --git a/renderer/src/geometry.rs b/renderer/src/geometry.rs
index f09ccfbf..36435148 100644
--- a/renderer/src/geometry.rs
+++ b/renderer/src/geometry.rs
@@ -2,7 +2,7 @@ mod cache;
pub use cache::Cache;
-use crate::core::{Point, Rectangle, Size, Transformation, Vector};
+use crate::core::{Point, Radians, Rectangle, Size, Transformation, Vector};
use crate::graphics::geometry::{Fill, Path, Stroke, Text};
use crate::Renderer;
@@ -184,7 +184,7 @@ impl Frame {
/// Applies a rotation in radians to the current transform of the [`Frame`].
#[inline]
- pub fn rotate(&mut self, angle: f32) {
+ pub fn rotate(&mut self, angle: impl Into<Radians>) {
delegate!(self, frame, frame.rotate(angle));
}
diff --git a/tiny_skia/src/geometry.rs b/tiny_skia/src/geometry.rs
index f7518731..16787f89 100644
--- a/tiny_skia/src/geometry.rs
+++ b/tiny_skia/src/geometry.rs
@@ -1,5 +1,7 @@
use crate::core::text::LineHeight;
-use crate::core::{Pixels, Point, Rectangle, Size, Transformation, Vector};
+use crate::core::{
+ Pixels, Point, Radians, Rectangle, Size, Transformation, Vector,
+};
use crate::graphics::geometry::fill::{self, Fill};
use crate::graphics::geometry::stroke::{self, Stroke};
use crate::graphics::geometry::{Path, Style, Text};
@@ -192,10 +194,10 @@ impl Frame {
self.transform.pre_translate(translation.x, translation.y);
}
- pub fn rotate(&mut self, angle: f32) {
- self.transform = self
- .transform
- .pre_concat(tiny_skia::Transform::from_rotate(angle.to_degrees()));
+ pub fn rotate(&mut self, angle: impl Into<Radians>) {
+ self.transform = self.transform.pre_concat(
+ tiny_skia::Transform::from_rotate(angle.into().0.to_degrees()),
+ );
}
pub fn scale(&mut self, scale: impl Into<f32>) {
diff --git a/wgpu/src/geometry.rs b/wgpu/src/geometry.rs
index 8cfcfff0..f4e0fbda 100644
--- a/wgpu/src/geometry.rs
+++ b/wgpu/src/geometry.rs
@@ -1,6 +1,8 @@
//! Build and draw geometry.
use crate::core::text::LineHeight;
-use crate::core::{Pixels, Point, Rectangle, Size, Transformation, Vector};
+use crate::core::{
+ Pixels, Point, Radians, Rectangle, Size, Transformation, Vector,
+};
use crate::graphics::color;
use crate::graphics::geometry::fill::{self, Fill};
use crate::graphics::geometry::{
@@ -475,12 +477,12 @@ impl Frame {
/// Applies a rotation in radians to the current transform of the [`Frame`].
#[inline]
- pub fn rotate(&mut self, angle: f32) {
+ pub fn rotate(&mut self, angle: impl Into<Radians>) {
self.transforms.current.0 = self
.transforms
.current
.0
- .pre_rotate(lyon::math::Angle::radians(angle));
+ .pre_rotate(lyon::math::Angle::radians(angle.into().0));
}
/// Applies a uniform scaling to the current transform of the [`Frame`].