1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 13:20:05 -04:00

Add Image::rotate and Mesh::rotate (#1371)

Co-authored-by: Hunter Morgan <hmorgan@bellflight.com>
This commit is contained in:
Hunter522
2022-03-22 02:44:23 -05:00
committed by GitHub
parent 805539b50d
commit 0a400a5bcc
3 changed files with 38 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
use crate::*;
use emath::Rot2;
/// An widget to show an image of a given size.
///
@@ -36,6 +37,7 @@ pub struct Image {
bg_fill: Color32,
tint: Color32,
sense: Sense,
rotation: Option<(Rot2, Vec2)>,
}
impl Image {
@@ -47,6 +49,7 @@ impl Image {
bg_fill: Default::default(),
tint: Color32::WHITE,
sense: Sense::hover(),
rotation: None,
}
}
@@ -75,6 +78,17 @@ impl Image {
self.sense = sense;
self
}
/// Rotate the image about an origin by some angle
///
/// Positive angle is clockwise.
/// Origin is a vector in normalized UV space ((0,0) in top-left, (1,1) bottom right).
///
/// To rotate about the center you can pass `Vec2::splat(0.5)` as the origin.
pub fn rotate(mut self, angle: f32, origin: Vec2) -> Self {
self.rotation = Some((Rot2::from_angle(angle), origin));
self
}
}
impl Image {
@@ -88,10 +102,11 @@ impl Image {
let Self {
texture_id,
uv,
size: _,
size,
bg_fill,
tint,
sense: _,
rotation,
} = self;
if *bg_fill != Default::default() {
@@ -104,6 +119,9 @@ impl Image {
// TODO: builder pattern for Mesh
let mut mesh = Mesh::with_texture(*texture_id);
mesh.add_rect_with_uv(rect, *uv, *tint);
if let Some((rot, origin)) = rotation {
mesh.rotate(*rot, rect.min + *origin * *size);
}
ui.painter().add(Shape::mesh(mesh));
}
}