1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
#![doc(html_favicon_url = "https://raw.githubusercontent.com/zng-ui/zng/main/examples/image/res/zng-logo-icon.png")]
#![doc(html_logo_url = "https://raw.githubusercontent.com/zng-ui/zng/main/examples/image/res/zng-logo.png")]
//!
//! Transform properties, [`scale`](fn@scale), [`rotate`](fn@rotate), [`transform`](fn@transform) and more.
//!
//! # Crate
//!
#![doc = include_str!(concat!("../", std::env!("CARGO_PKG_README")))]
#![warn(unused_extern_crates)]
#![warn(missing_docs)]
use zng_wgt::prelude::*;
/// Custom transform.
///
/// See [`Transform`] for how to initialize a custom transform. The [`transform_origin`] is applied using the widget's inner size
/// for relative values.
///
/// [`transform_origin`]: fn@transform_origin
/// [`Transform`]: zng_wgt::prelude::Transform
#[property(LAYOUT, default(Transform::identity()))]
pub fn transform(child: impl UiNode, transform: impl IntoVar<Transform>) -> impl UiNode {
let binding_key = FrameValueKey::new_unique();
let transform = transform.into_var();
let mut render_transform = PxTransform::identity();
match_node(child, move |child, op| match op {
UiNodeOp::Init => {
WIDGET.sub_var_layout(&transform);
}
UiNodeOp::Layout { wl, final_size } => {
let size = child.layout(wl);
let transform = transform.layout();
let default_origin = PxPoint::new(size.width / 2.0, size.height / 2.0);
let origin = LAYOUT.with_constraints(PxConstraints2d::new_fill_size(size), || {
TRANSFORM_ORIGIN_VAR.layout_dft(default_origin)
});
let x = origin.x.0 as f32;
let y = origin.y.0 as f32;
let transform = PxTransform::translation(-x, -y).then(&transform).then_translate(euclid::vec2(x, y));
if transform != render_transform {
render_transform = transform;
WIDGET.render_update();
}
*final_size = size;
}
UiNodeOp::Render { frame } => {
if frame.is_outer() {
frame.push_inner_transform(&render_transform, |frame| child.render(frame));
} else {
frame.push_reference_frame(
binding_key.into(),
binding_key.bind_var_mapped(&transform, render_transform),
false,
false,
|frame| child.render(frame),
);
}
}
UiNodeOp::RenderUpdate { update } => {
if update.is_outer() {
update.with_inner_transform(&render_transform, |update| child.render_update(update));
} else {
update.with_transform_opt(binding_key.update_var_mapped(&transform, render_transform), false, |update| {
child.render_update(update)
})
}
}
_ => {}
})
}
/// Rotate transform.
///
/// This property is a shorthand way of setting [`transform`] to [`new_rotate(angle)`](Transform::new_rotate) using variable mapping.
///
/// The rotation is done *around* the [`transform_origin`] in 2D.
///
/// [`transform`]: fn@transform
/// [`transform_origin`]: fn@transform_origin
#[property(LAYOUT, default(0.rad()))]
pub fn rotate(child: impl UiNode, angle: impl IntoVar<AngleRadian>) -> impl UiNode {
transform(child, angle.into_var().map(|&a| Transform::new_rotate(a)))
}
/// Rotate transform.
///
/// This property is a shorthand way of setting [`transform`] to [`new_rotate_x(angle)`](Transform::new_rotate_x) using variable mapping.
///
/// The rotation is done *around* the ***x*** axis that passes trough the [`transform_origin`] in 3D.
///
/// [`transform`]: fn@transform
/// [`transform_origin`]: fn@transform_origin
#[property(LAYOUT, default(0.rad()))]
pub fn rotate_x(child: impl UiNode, angle: impl IntoVar<AngleRadian>) -> impl UiNode {
transform(child, angle.into_var().map(|&a| Transform::new_rotate_x(a)))
}
/// Rotate transform.
///
/// This property is a shorthand way of setting [`transform`] to [`new_rotate_y(angle)`](Transform::new_rotate_y) using variable mapping.
///
/// The rotation is done *around* the ***y*** axis that passes trough the [`transform_origin`] in 3D.
///
/// [`transform`]: fn@transform
/// [`transform_origin`]: fn@transform_origin
#[property(LAYOUT, default(0.rad()))]
pub fn rotate_y(child: impl UiNode, angle: impl IntoVar<AngleRadian>) -> impl UiNode {
transform(child, angle.into_var().map(|&a| Transform::new_rotate_y(a)))
}
/// Same as [`rotate`].
///
/// [`rotate`]: fn@rotate
#[property(LAYOUT, default(0.rad()))]
pub fn rotate_z(child: impl UiNode, angle: impl IntoVar<AngleRadian>) -> impl UiNode {
transform(child, angle.into_var().map(|&a| Transform::new_rotate_z(a)))
}
/// Scale transform.
///
/// This property is a shorthand way of setting [`transform`] to [`new_scale(s)`](Transform::new_scale) using variable mapping.
///
/// [`transform`]: fn@transform
#[property(LAYOUT, default(1.0))]
pub fn scale(child: impl UiNode, s: impl IntoVar<Factor>) -> impl UiNode {
transform(child, s.into_var().map(|&x| Transform::new_scale(x)))
}
/// Scale X and Y transform.
///
/// This property is a shorthand way of setting [`transform`] to [`new_scale_xy(x, y)`](Transform::new_scale) using variable merging.
///
/// [`transform`]: fn@transform
#[property(LAYOUT, default(1.0, 1.0))]
pub fn scale_xy(child: impl UiNode, x: impl IntoVar<Factor>, y: impl IntoVar<Factor>) -> impl UiNode {
transform(
child,
merge_var!(x.into_var(), y.into_var(), |&x, &y| Transform::new_scale_xy(x, y)),
)
}
/// Scale X transform.
///
/// This property is a shorthand way of setting [`transform`] to [`new_scale_x(x)`](Transform::new_scale_x) using variable mapping.
///
/// [`transform`]: fn@transform
#[property(LAYOUT, default(1.0))]
pub fn scale_x(child: impl UiNode, x: impl IntoVar<Factor>) -> impl UiNode {
transform(child, x.into_var().map(|&x| Transform::new_scale_x(x)))
}
/// Scale Y transform.
///
/// This property is a shorthand way of setting [`transform`] to [`new_scale_y(y)`](Transform::new_scale_y) using variable mapping.
///
/// [`transform`]: fn@transform
#[property(LAYOUT, default(1.0))]
pub fn scale_y(child: impl UiNode, y: impl IntoVar<Factor>) -> impl UiNode {
transform(child, y.into_var().map(|&y| Transform::new_scale_y(y)))
}
/// Skew transform.
///
/// This property is a shorthand way of setting [`transform`] to [`new_skew(x, y)`](Transform::new_skew) using variable merging.
///
/// [`transform`]: fn@transform
#[property(LAYOUT, default(0.rad(), 0.rad()))]
pub fn skew(child: impl UiNode, x: impl IntoVar<AngleRadian>, y: impl IntoVar<AngleRadian>) -> impl UiNode {
transform(child, merge_var!(x.into_var(), y.into_var(), |&x, &y| Transform::new_skew(x, y)))
}
/// Skew X transform.
///
/// This property is a shorthand way of setting [`transform`] to [`new_skew_x(x)`](Transform::new_skew_x) using variable mapping.
///
/// [`transform`]: fn@transform
#[property(LAYOUT, default(0.rad()))]
pub fn skew_x(child: impl UiNode, x: impl IntoVar<AngleRadian>) -> impl UiNode {
transform(child, x.into_var().map(|&x| Transform::new_skew_x(x)))
}
/// Skew Y transform.
///
/// This property is a shorthand way of setting [`transform`] to [`new_skew_y(y)`](Transform::new_skew_y) using variable mapping.
///
/// [`transform`]: fn@transform
#[property(LAYOUT)]
pub fn skew_y(child: impl UiNode, y: impl IntoVar<AngleRadian>) -> impl UiNode {
transform(child, y.into_var().map(|&y| Transform::new_skew_y(y)))
}
/// Translate transform.
///
/// This property is a shorthand way of setting [`transform`] to [`new_translate(x, y)`](Transform::new_translate) using variable merging.
///
/// [`transform`]: fn@transform
#[property(LAYOUT, default(0, 0))]
pub fn translate(child: impl UiNode, x: impl IntoVar<Length>, y: impl IntoVar<Length>) -> impl UiNode {
transform(
child,
merge_var!(x.into_var(), y.into_var(), |x, y| Transform::new_translate(x.clone(), y.clone())),
)
}
/// Translate X transform.
///
/// This property is a shorthand way of setting [`transform`] to [`new_translate_x(x)`](Transform::new_translate_x) using variable mapping.
///
/// [`transform`]: fn@transform
#[property(LAYOUT, default(0))]
pub fn translate_x(child: impl UiNode, x: impl IntoVar<Length>) -> impl UiNode {
transform(child, x.into_var().map(|x| Transform::new_translate_x(x.clone())))
}
/// Translate Y transform.
///
/// This property is a shorthand way of setting [`transform`] to [`new_translate_y(y)`](Transform::new_translate_y) using variable mapping.
///
/// [`transform`]: fn@transform
#[property(LAYOUT, default(0))]
pub fn translate_y(child: impl UiNode, y: impl IntoVar<Length>) -> impl UiNode {
transform(child, y.into_var().map(|y| Transform::new_translate_y(y.clone())))
}
/// Translate Z transform.
///
/// This property is a shorthand way of setting [`transform`] to [`new_translate_z(z)`](Transform::new_translate_z) using variable mapping.
///
/// [`transform`]: fn@transform
#[property(LAYOUT, default(0))]
pub fn translate_z(child: impl UiNode, z: impl IntoVar<Length>) -> impl UiNode {
transform(child, z.into_var().map(|z| Transform::new_translate_z(z.clone())))
}
/// Point relative to the widget inner bounds around which the [`transform`] is applied.
///
/// This property sets the [`TRANSFORM_ORIGIN_VAR`] context variable.
///
/// [`transform`]: fn@transform
#[property(CONTEXT, default(TRANSFORM_ORIGIN_VAR))]
pub fn transform_origin(child: impl UiNode, origin: impl IntoVar<Point>) -> impl UiNode {
with_context_var(child, TRANSFORM_ORIGIN_VAR, origin)
}
///Distance from the Z plane (0) the viewer is, affects 3D transform on the widget's children.
///
/// [`Length::Default`] is an infinite distance, the lower the value the *closest* the viewer is and therefore
/// the 3D transforms are more noticeable. Distances less then `1.px()` are coerced to it.
///
/// [`Length::Default`]: zng_wgt::prelude::Length::Default
#[property(LAYOUT-20, default(Length::Default))]
pub fn perspective(child: impl UiNode, distance: impl IntoVar<Length>) -> impl UiNode {
let distance = distance.into_var();
match_node(child, move |_, op| match op {
UiNodeOp::Init => {
WIDGET.sub_var_layout(&distance);
}
UiNodeOp::Layout { wl, .. } => {
let d = distance.layout_dft_z(Px::MAX);
let d = LAYOUT.z_constraints().clamp(d).max(Px(1));
wl.set_perspective(d.0 as f32);
}
_ => {}
})
}
/// Vanishing point used by 3D transforms in the widget's children.
///
/// Is the widget center by default.
///
/// [`transform`]: fn@transform
#[property(LAYOUT-20, default(Point::default()))]
pub fn perspective_origin(child: impl UiNode, origin: impl IntoVar<Point>) -> impl UiNode {
let origin = origin.into_var();
match_node(child, move |c, op| match op {
UiNodeOp::Init => {
WIDGET.sub_var_layout(&origin);
}
UiNodeOp::Layout { wl, final_size } => {
let size = c.layout(wl);
let default_origin = PxPoint::new(size.width / 2.0, size.height / 2.0);
let origin = LAYOUT.with_constraints(PxConstraints2d::new_fill_size(size), || origin.layout_dft(default_origin));
wl.set_perspective_origin(origin);
*final_size = size;
}
_ => {}
})
}
/// Defines how the widget and children are positioned in 3D space.
///
/// This sets the style for the widget and children layout transform, the [`transform`] and other properties derived from [`transform`].
/// It does not affect any other descendant, only the widget and immediate children.
///
/// [`transform`]: fn@transform
#[property(CONTEXT, default(TransformStyle::Flat))]
pub fn transform_style(child: impl UiNode, style: impl IntoVar<TransformStyle>) -> impl UiNode {
let style = style.into_var();
match_node(child, move |_, op| match op {
UiNodeOp::Init => {
WIDGET.sub_var_layout(&style);
}
UiNodeOp::Layout { wl, .. } => {
wl.set_transform_style(style.get());
}
_ => {}
})
}
/// Sets if the widget is still visible when it is turned back towards the viewport due to rotations in X or Y axis in
/// the widget or in parent widgets.
///
/// Widget back face is visible by default, the back face is a mirror image of the front face, if `visible` is set
/// to `false` the widget is still layout and rendered, but it is not displayed on screen by the view-process if
/// the final global transform of the widget turns the backface towards the viewport.
///
/// This property affects any descendant widgets too, unless they also set `backface_visibility`.
#[property(CONTEXT, default(true))]
pub fn backface_visibility(child: impl UiNode, visible: impl IntoVar<bool>) -> impl UiNode {
let visible = visible.into_var();
match_node(child, move |c, op| match op {
UiNodeOp::Init => {
WIDGET.sub_var_render(&visible);
}
UiNodeOp::Render { frame } => {
frame.with_backface_visibility(visible.get(), |frame| c.render(frame));
}
_ => {}
})
}
context_var! {
/// Point relative to the widget inner bounds around which the [`transform`] is applied.
///
/// Default origin is `Point::center`.
///
/// [`transform`]: fn@transform
pub static TRANSFORM_ORIGIN_VAR: Point = Point::center();
/// Vanishing point used by [`transform`] when it is 3D.
///
/// Default origin is `Point::center`.
///
/// [`transform`]: fn@transform
pub static PERSPECTIVE_ORIGIN_VAR: Point = Point::center();
}