mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 05:10:03 -04:00
[emath] RectTransform: transforms Pos2 from one Rect to another
Very useful for transforming coordinate systems, e.g. for painting
This commit is contained in:
@@ -34,6 +34,9 @@ impl super::View for DancingStrings {
|
||||
let desired_size = ui.available_width() * vec2(1.0, 0.35);
|
||||
let (_id, rect) = ui.allocate_space(desired_size);
|
||||
|
||||
let to_screen =
|
||||
math::RectTransform::from_to(Rect::from_x_y_ranges(0.0..=1.0, -1.0..=1.0), rect);
|
||||
|
||||
let mut shapes = vec![];
|
||||
|
||||
for &mode in &[2, 3, 5] {
|
||||
@@ -46,11 +49,7 @@ impl super::View for DancingStrings {
|
||||
let t = i as f32 / (n as f32);
|
||||
let amp = (time as f32 * speed * mode).sin() / mode;
|
||||
let y = amp * (t * std::f32::consts::TAU / 2.0 * mode).sin();
|
||||
|
||||
pos2(
|
||||
lerp(rect.x_range(), t),
|
||||
remap(y, -1.0..=1.0, rect.y_range()),
|
||||
)
|
||||
to_screen * pos2(t, y)
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
||||
@@ -3,7 +3,8 @@ use egui::*;
|
||||
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
|
||||
#[cfg_attr(feature = "persistence", serde(default))]
|
||||
pub struct Painting {
|
||||
lines: Vec<Vec<Vec2>>,
|
||||
/// in 0-1 normalized coordinates
|
||||
lines: Vec<Vec<Pos2>>,
|
||||
stroke: Stroke,
|
||||
}
|
||||
|
||||
@@ -32,6 +33,12 @@ impl Painting {
|
||||
ui.allocate_painter(ui.available_size_before_wrap_finite(), Sense::drag());
|
||||
let rect = response.rect;
|
||||
|
||||
let to_screen = emath::RectTransform::from_to(
|
||||
Rect::from_min_size(Pos2::ZERO, rect.square_proportions()),
|
||||
rect,
|
||||
);
|
||||
let from_screen = to_screen.inverse();
|
||||
|
||||
if self.lines.is_empty() {
|
||||
self.lines.push(vec![]);
|
||||
}
|
||||
@@ -39,7 +46,7 @@ impl Painting {
|
||||
let current_line = self.lines.last_mut().unwrap();
|
||||
|
||||
if let Some(pointer_pos) = response.interact_pointer_pos() {
|
||||
let canvas_pos = pointer_pos - rect.min;
|
||||
let canvas_pos = from_screen * pointer_pos;
|
||||
if current_line.last() != Some(&canvas_pos) {
|
||||
current_line.push(canvas_pos);
|
||||
}
|
||||
@@ -49,7 +56,7 @@ impl Painting {
|
||||
|
||||
for line in &self.lines {
|
||||
if line.len() >= 2 {
|
||||
let points: Vec<Pos2> = line.iter().map(|p| rect.min + *p).collect();
|
||||
let points: Vec<Pos2> = line.iter().map(|p| to_screen * *p).collect();
|
||||
painter.add(Shape::line(points, self.stroke));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,8 +100,6 @@ impl FractalClock {
|
||||
}
|
||||
|
||||
fn paint(&mut self, painter: &Painter) {
|
||||
let rect = painter.clip_rect();
|
||||
|
||||
struct Hand {
|
||||
length: f32,
|
||||
angle: f32,
|
||||
@@ -130,14 +128,18 @@ impl FractalClock {
|
||||
Hand::from_length_angle(0.5, angle_from_period(12.0 * 60.0 * 60.0)),
|
||||
];
|
||||
|
||||
let scale = self.zoom * rect.width().min(rect.height());
|
||||
let mut shapes: Vec<Shape> = Vec::new();
|
||||
let mut paint_line = |points: [Pos2; 2], color: Color32, width: f32| {
|
||||
let line = [
|
||||
rect.center() + scale * points[0].to_vec2(),
|
||||
rect.center() + scale * points[1].to_vec2(),
|
||||
];
|
||||
|
||||
let rect = painter.clip_rect();
|
||||
let to_screen = emath::RectTransform::from_to(
|
||||
Rect::from_center_size(Pos2::ZERO, rect.square_proportions() / self.zoom),
|
||||
rect,
|
||||
);
|
||||
|
||||
let mut paint_line = |points: [Pos2; 2], color: Color32, width: f32| {
|
||||
let line = [to_screen * points[0], to_screen * points[1]];
|
||||
|
||||
// culling
|
||||
if rect.intersects(Rect::from_two_pos(line[0], line[1])) {
|
||||
shapes.push(Shape::line_segment(line, (width, color)));
|
||||
}
|
||||
@@ -186,6 +188,9 @@ impl FractalClock {
|
||||
width *= self.width_factor;
|
||||
|
||||
let luminance_u8 = (255.0 * luminance).round() as u8;
|
||||
if luminance_u8 == 0 {
|
||||
break;
|
||||
}
|
||||
|
||||
for &rotor in &hand_rotors {
|
||||
for a in &nodes {
|
||||
|
||||
Reference in New Issue
Block a user