Skip to content

Commit

Permalink
Manually rotate rect
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Jul 8, 2024
1 parent 810ad07 commit ab4648f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 51 deletions.
22 changes: 10 additions & 12 deletions crates/egui/src/widgets/theme_switch/cogwheel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::painter_ext::PainterExt;
use super::rotated_rect::draw_rotated_rect;
use crate::Painter;
use emath::{vec2, Pos2, Rect, Rot2};
use epaint::{Color32, RectShape, Rounding};
use epaint::{Color32, Rounding};
use std::f32::consts::TAU;

pub(crate) fn cogwheel(painter: &Painter, center: Pos2, radius: f32, color: Color32) {
Expand All @@ -24,15 +24,13 @@ pub(crate) fn cogwheel(painter: &Painter, center: Pos2, radius: f32, color: Colo
for n in 0..cogs {
let cog_center = center - vec2(0., outer_radius + cog_length / 2. - thickness / 2.);
let cog_size = vec2(cog_width, cog_length);
let cog = RectShape::filled(
Rect::from_center_size(cog_center, cog_size),
Rounding {
nw: cog_rounding,
ne: cog_rounding,
..Default::default()
},
color,
);
painter.add_rotated(cog, Rot2::from_angle(TAU / cogs as f32 * n as f32), center);
let rotation = Rot2::from_angle(TAU / cogs as f32 * n as f32);
let rect = Rect::from_center_size(cog_center, cog_size);
let rounding = Rounding {
nw: cog_rounding,
ne: cog_rounding,
..Default::default()
};
draw_rotated_rect(painter, rect, rounding, color, rotation, center);
}
}
2 changes: 1 addition & 1 deletion crates/egui/src/widgets/theme_switch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{Painter, Response, ThemePreference, Ui, Widget};
mod arc;
mod cogwheel;
mod moon;
mod painter_ext;
mod rotated_rect;
mod sun;

#[must_use = "You should put this widget in an ui with `ui.add(widget);`"]
Expand Down
30 changes: 0 additions & 30 deletions crates/egui/src/widgets/theme_switch/painter_ext.rs

This file was deleted.

44 changes: 44 additions & 0 deletions crates/egui/src/widgets/theme_switch/rotated_rect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::Painter;
use emath::{vec2, Pos2, Rect, Rot2};
use epaint::{Color32, PathShape, Rounding, Stroke};

/// Draws a rectangle with rounded corners, rotated around an origin.
pub(crate) fn draw_rotated_rect(
painter: &Painter,
rect: Rect,
rounding: impl Into<Rounding>,
fill: impl Into<Color32>,
rot: impl Into<Rot2>,
origin: Pos2,
) {
let rounding = rounding.into();
let fill = fill.into();
let rot = rot.into();
let safe_inset_points = safe_inset_points(rect, rot, origin, rounding);

painter.add(PathShape::convex_polygon(
safe_inset_points.to_vec(),
fill,
Stroke::NONE,
));

// Nobody will notice that we're not actually drawing
// the round bits... 🤫
}

fn safe_inset_points(rect: Rect, rot: Rot2, origin: Pos2, rounding: Rounding) -> [Pos2; 8] {
[
rotate(rect.left_top() + vec2(0.0, rounding.nw), rot, origin),
rotate(rect.left_top() + vec2(rounding.nw, 0.0), rot, origin),
rotate(rect.right_top() - vec2(rounding.ne, 0.0), rot, origin),
rotate(rect.right_top() + vec2(0.0, rounding.ne), rot, origin),
rotate(rect.right_bottom() - vec2(0.0, rounding.se), rot, origin),
rotate(rect.right_bottom() - vec2(rounding.se, 0.0), rot, origin),
rotate(rect.left_bottom() + vec2(rounding.sw, 0.0), rot, origin),
rotate(rect.left_bottom() - vec2(0.0, rounding.sw), rot, origin),
]
}

fn rotate(point: Pos2, rot: Rot2, origin: Pos2) -> Pos2 {
origin + rot * (point - origin)
}
13 changes: 5 additions & 8 deletions crates/egui/src/widgets/theme_switch/sun.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::painter_ext::PainterExt;
use super::rotated_rect::draw_rotated_rect;
use crate::Painter;
use emath::{vec2, Pos2, Rect, Rot2, Vec2};
use epaint::{Color32, RectShape, Stroke};
use epaint::{Color32, Stroke};
use std::f32::consts::TAU;

pub(crate) fn sun(painter: &Painter, center: Pos2, radius: f32, color: Color32) {
Expand All @@ -18,11 +18,8 @@ pub(crate) fn sun(painter: &Painter, center: Pos2, radius: f32, color: Color32)
for n in 0..rays {
let ray_center = center - vec2(0., sun_radius + ray_spacing + ray_length / 2.);
let ray_size = vec2(ray_radius, ray_length);
let ray = RectShape::filled(
Rect::from_center_size(ray_center, ray_size),
ray_radius,
color,
);
clipped.add_rotated(ray, Rot2::from_angle(TAU / rays as f32 * n as f32), center);
let rect = Rect::from_center_size(ray_center, ray_size);
let rotation = Rot2::from_angle(TAU / rays as f32 * n as f32);
draw_rotated_rect(painter, rect, ray_radius / 2.0, color, rotation, center);
}
}

0 comments on commit ab4648f

Please sign in to comment.