ruffle/core/src/transform.rs

57 lines
1.3 KiB
Rust
Raw Normal View History

2019-04-29 05:55:44 +00:00
use crate::prelude::*;
2019-05-24 17:25:03 +00:00
use gc_arena::Collect;
2019-04-29 05:55:44 +00:00
/// Represents the transform for a DisplayObject.
/// This includes both the transformation matrix and the color transform.
2019-05-04 18:45:11 +00:00
///
#[derive(Clone, Collect, Debug)]
2019-05-24 17:25:03 +00:00
#[collect(require_static)]
2019-04-29 05:55:44 +00:00
pub struct Transform {
pub matrix: Matrix,
pub color_transform: ColorTransform,
}
impl Default for Transform {
fn default() -> Self {
Self {
matrix: Default::default(),
color_transform: Default::default(),
}
}
}
2019-05-01 16:55:54 +00:00
pub struct TransformStack(Vec<Transform>);
impl TransformStack {
pub fn new() -> Self {
Self(vec![Transform::default()])
}
pub fn push(&mut self, transform: &Transform) {
let cur_transform = self.transform();
2019-05-24 17:25:03 +00:00
let matrix = cur_transform.matrix * transform.matrix;
let color_transform = cur_transform.color_transform * transform.color_transform;
self.0.push(Transform {
matrix,
color_transform,
});
2019-05-01 16:55:54 +00:00
}
pub fn pop(&mut self) {
if self.0.len() <= 1 {
panic!("Transform stack underflow");
}
self.0.pop();
}
pub fn transform(&self) -> &Transform {
&self.0[self.0.len() - 1]
}
}
2019-05-24 17:25:03 +00:00
impl Default for TransformStack {
fn default() -> Self {
TransformStack::new()
}
}