@@ -54,13 +54,16 @@ pub struct RectShape {
5454 /// Since most rectangles do not have a texture, this is optional and in an `Arc`,
5555 /// so that [`RectShape`] is kept small..
5656 pub brush : Option < Arc < Brush > > ,
57+
58+ /// Rotate rectangle by this many radians clockwise around its center.
59+ pub angle : f32 ,
5760}
5861
5962#[ test]
6063fn rect_shape_size ( ) {
6164 assert_eq ! (
6265 std:: mem:: size_of:: <RectShape >( ) ,
63- 48 ,
66+ 52 ,
6467 "RectShape changed size! If it shrank - good! Update this test. If it grew - bad! Try to find a way to avoid it."
6568 ) ;
6669 assert ! (
@@ -88,6 +91,7 @@ impl RectShape {
8891 round_to_pixels : None ,
8992 blur_width : 0.0 ,
9093 brush : Default :: default ( ) ,
94+ angle : 0.0 ,
9195 }
9296 }
9397
@@ -157,6 +161,25 @@ impl RectShape {
157161 self
158162 }
159163
164+ /// Set the rotation of the rectangle (in radians, clockwise).
165+ /// The rectangle rotates around its center.
166+ #[ inline]
167+ pub fn with_angle ( mut self , angle : f32 ) -> Self {
168+ self . angle = angle;
169+ self
170+ }
171+
172+ /// Set the rotation of the rectangle (in radians, clockwise) around a custom pivot point.
173+ #[ inline]
174+ pub fn with_angle_and_pivot ( mut self , angle : f32 , pivot : Pos2 ) -> Self {
175+ self . angle = angle;
176+ let rot = emath:: Rot2 :: from_angle ( angle) ;
177+ let center = self . rect . center ( ) ;
178+ let new_center = pivot + rot * ( center - pivot) ;
179+ self . rect = self . rect . translate ( new_center - center) ;
180+ self
181+ }
182+
160183 /// The visual bounding rectangle (includes stroke width)
161184 #[ inline]
162185 pub fn visual_bounding_rect ( & self ) -> Rect {
@@ -168,7 +191,17 @@ impl RectShape {
168191 StrokeKind :: Middle => self . stroke . width / 2.0 ,
169192 StrokeKind :: Outside => self . stroke . width ,
170193 } ;
171- self . rect . expand ( expand + self . blur_width / 2.0 )
194+ let expanded = self . rect . expand ( expand + self . blur_width / 2.0 ) ;
195+ if self . angle == 0.0 {
196+ expanded
197+ } else {
198+ // Rotate around the rectangle's center and compute bounding box
199+ let center = self . rect . center ( ) ;
200+ let rect_relative = Rect :: from_center_size ( Pos2 :: ZERO , expanded. size ( ) ) ;
201+ rect_relative
202+ . rotate_bb ( emath:: Rot2 :: from_angle ( self . angle ) )
203+ . translate ( center. to_vec2 ( ) )
204+ }
172205 }
173206 }
174207
0 commit comments