Skip to content

Implement Mul for Transform #346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/graphics/transform.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::{Mul, MulAssign};

use crate::{ffi::graphics as ffi, graphics::FloatRect, system::Vector2f};

/// Define a 3x3 transform matrix.
Expand Down Expand Up @@ -195,3 +197,63 @@ impl Default for Transform {
Self::IDENTITY
}
}

impl Mul for Transform {
type Output = Self;

/// Combines two transformations
///
/// Equivelant to calling `left.clone().combine(right)`
///
/// Returns new combined Transform
/// ```
/// # use sfml::graphics::Transform;
/// let test_transform = Transform::new(1., 2., 3., 4., 5., 6., 7., 8., 9.);
/// let result = Transform::new(30., 36., 42., 66., 81., 96., 102., 126., 150.);
/// assert_eq!(test_transform * test_transform, result);
/// ```
fn mul(self, rhs: Self) -> Self::Output {
let mut clone_self = self;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could just make self mut in the parameter list. But this is fine too I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, so the base SFML implementation has it where it clones itself like so. would that do the same here? I actually don't know. If it just clones self if i put mut there, then yeah I should have done it like that. Oh well. I don't know everything about rust :P

clone_self.combine(&rhs);
clone_self
}
}

impl MulAssign for Transform {
/// Combines two transformations
///
/// Equivelant to calling `left.combine(right)`
///
/// Returns new combined Transform
/// ```
/// # use sfml::graphics::Transform;
/// let mut test_transform = Transform::new(1., 2., 3., 4., 5., 6., 7., 8., 9.);
/// let result = Transform::new(30., 36., 42., 66., 81., 96., 102., 126., 150.);
/// test_transform *= test_transform;
/// assert_eq!(test_transform, result);
/// ```
fn mul_assign(&mut self, rhs: Self) {
self.combine(&rhs);
}
}

impl Mul<Vector2f> for Transform {
type Output = Vector2f;

/// Transforms a point
///
/// Equivalent to calling `left.transform_point(right)`
///
/// Returns a new transformed points
/// ```
/// # use sfml::graphics::Transform;
/// # use sfml::system::Vector2f;
/// let test_transform = Transform::new(1., 2., 3., 4., 5., 6., 7., 8., 9.);
/// let test_point = Vector2f::new(2., 3.);
/// let result = Vector2f::new(11., 29.);
/// assert_eq!(test_transform * test_point, result);
/// ```
fn mul(self, rhs: Vector2f) -> Self::Output {
self.transform_point(rhs)
}
}
Loading