Skip to main content

cranpose_render_common/
layer_transform.rs

1use cranpose_ui_graphics::{GraphicsLayer, Point, Rect};
2
3use crate::graph::{ProjectiveTransform, quad_bounds};
4
5pub(crate) fn layer_scale_x(layer: &GraphicsLayer) -> f32 {
6    layer.scale * layer.scale_x
7}
8
9pub(crate) fn layer_scale_y(layer: &GraphicsLayer) -> f32 {
10    layer.scale * layer.scale_y
11}
12
13pub fn layer_uniform_scale(layer: &GraphicsLayer) -> f32 {
14    layer_scale_x(layer).min(layer_scale_y(layer))
15}
16
17pub(crate) fn layer_affine_is_identity(layer: &GraphicsLayer) -> bool {
18    layer.scale == 1.0
19        && layer.scale_x == 1.0
20        && layer.scale_y == 1.0
21        && layer.translation_x == 0.0
22        && layer.translation_y == 0.0
23}
24
25pub fn apply_layer_affine_to_rect(rect: Rect, layer_bounds: Rect, layer: &GraphicsLayer) -> Rect {
26    if layer_affine_is_identity(layer) {
27        return rect;
28    }
29    let scale_x = layer_scale_x(layer);
30    let scale_y = layer_scale_y(layer);
31    let (pivot_x, pivot_y) = layer_rotation_pivot(layer_bounds, layer);
32    Rect {
33        x: pivot_x + (rect.x - pivot_x) * scale_x + layer.translation_x,
34        y: pivot_y + (rect.y - pivot_y) * scale_y + layer.translation_y,
35        width: rect.width * scale_x,
36        height: rect.height * scale_y,
37    }
38}
39
40/// Point counterpart of [`apply_layer_affine_to_rect`]: scale about the layer
41/// pivot, then translate. Rotation/perspective are deliberately excluded — the
42/// affine space is the one SDF shapes are evaluated in, with rotation carried
43/// by the deformed quad instead.
44pub fn apply_layer_affine_to_point(
45    point: Point,
46    layer_bounds: Rect,
47    layer: &GraphicsLayer,
48) -> Point {
49    if layer_affine_is_identity(layer) {
50        return point;
51    }
52    let (pivot_x, pivot_y) = layer_rotation_pivot(layer_bounds, layer);
53    Point::new(
54        pivot_x + (point.x - pivot_x) * layer_scale_x(layer) + layer.translation_x,
55        pivot_y + (point.y - pivot_y) * layer_scale_y(layer) + layer.translation_y,
56    )
57}
58
59fn layer_rotation_pivot(layer_bounds: Rect, layer: &GraphicsLayer) -> (f32, f32) {
60    (
61        layer_bounds.x + layer_bounds.width * layer.transform_origin.pivot_fraction_x,
62        layer_bounds.y + layer_bounds.height * layer.transform_origin.pivot_fraction_y,
63    )
64}
65
66pub(crate) fn layer_scales_or_rotates(layer: &GraphicsLayer) -> bool {
67    layer_scale_x(layer) != 1.0 || layer_scale_y(layer) != 1.0 || layer_has_rotation(layer)
68}
69
70fn layer_has_rotation(layer: &GraphicsLayer) -> bool {
71    layer.rotation_x.abs() > f32::EPSILON
72        || layer.rotation_y.abs() > f32::EPSILON
73        || layer.rotation_z.abs() > f32::EPSILON
74}
75
76fn apply_rotation_and_perspective(
77    point: [f32; 2],
78    pivot: (f32, f32),
79    layer: &GraphicsLayer,
80) -> [f32; 2] {
81    if !layer_has_rotation(layer) {
82        return point;
83    }
84
85    let mut x = point[0] - pivot.0;
86    let mut y = point[1] - pivot.1;
87    let mut z = 0.0f32;
88
89    let (sin_x, cos_x) = layer.rotation_x.to_radians().sin_cos();
90    let (sin_y, cos_y) = layer.rotation_y.to_radians().sin_cos();
91    let (sin_z, cos_z) = layer.rotation_z.to_radians().sin_cos();
92
93    let y_rot_x = y * cos_x - z * sin_x;
94    let z_rot_x = y * sin_x + z * cos_x;
95    y = y_rot_x;
96    z = z_rot_x;
97
98    let x_rot_y = x * cos_y + z * sin_y;
99    let z_rot_y = -x * sin_y + z * cos_y;
100    x = x_rot_y;
101    z = z_rot_y;
102
103    let x_rot_z = x * cos_z - y * sin_z;
104    let y_rot_z = x * sin_z + y * cos_z;
105    x = x_rot_z;
106    y = y_rot_z;
107
108    const CAMERA_DISTANCE_SCALE: f32 = 72.0;
109    let camera_distance = (layer.camera_distance * CAMERA_DISTANCE_SCALE).max(1.0);
110    let denom = (camera_distance - z).max(1.0);
111    let perspective = camera_distance / denom;
112
113    [pivot.0 + x * perspective, pivot.1 + y * perspective]
114}
115
116fn apply_layer_to_point(point: [f32; 2], pivot: (f32, f32), layer: &GraphicsLayer) -> [f32; 2] {
117    let scaled = [
118        pivot.0 + (point[0] - pivot.0) * layer_scale_x(layer),
119        pivot.1 + (point[1] - pivot.1) * layer_scale_y(layer),
120    ];
121    let rotated = apply_rotation_and_perspective(scaled, pivot, layer);
122    [
123        rotated[0] + layer.translation_x,
124        rotated[1] + layer.translation_y,
125    ]
126}
127
128pub fn apply_layer_to_quad(rect: Rect, layer_bounds: Rect, layer: &GraphicsLayer) -> [[f32; 2]; 4] {
129    let quad = [
130        [rect.x, rect.y],
131        [rect.x + rect.width, rect.y],
132        [rect.x, rect.y + rect.height],
133        [rect.x + rect.width, rect.y + rect.height],
134    ];
135    if layer_affine_is_identity(layer) && !layer_has_rotation(layer) {
136        return quad;
137    }
138    let pivot = layer_rotation_pivot(layer_bounds, layer);
139    quad.map(|point| apply_layer_to_point(point, pivot, layer))
140}
141
142pub fn apply_layer_to_rect(rect: Rect, layer_bounds: Rect, layer: &GraphicsLayer) -> Rect {
143    quad_bounds(apply_layer_to_quad(rect, layer_bounds, layer))
144}
145
146pub fn layer_transform_to_parent(
147    local_bounds: Rect,
148    placement: Point,
149    layer: &GraphicsLayer,
150) -> ProjectiveTransform {
151    let placement_rect = Rect {
152        x: placement.x,
153        y: placement.y,
154        width: local_bounds.width,
155        height: local_bounds.height,
156    };
157    ProjectiveTransform::from_rect_to_quad(
158        local_bounds,
159        apply_layer_to_quad(placement_rect, placement_rect, layer),
160    )
161}
162
163#[cfg(test)]
164#[path = "tests/layer_transform_tests.rs"]
165mod tests;