use crate::ffi::system::sfTime;
use std::ops::{
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign,
};
#[derive(Copy, Clone, Debug)]
pub struct Time(sfTime);
impl PartialEq for Time {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl Eq for Time {}
impl PartialOrd for Time {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.0.partial_cmp(&other.0)
}
}
impl Ord for Time {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.cmp(&other.0)
}
}
impl Time {
#[must_use]
#[allow(clippy::cast_possible_truncation)]
pub fn seconds(seconds: f32) -> Self {
Time((seconds * 1000000.) as i64)
}
#[must_use]
pub const fn milliseconds(milliseconds: i32) -> Self {
Time(milliseconds as i64 * 1000)
}
#[must_use]
pub const fn microseconds(microseconds: i64) -> Self {
Time(microseconds)
}
#[must_use]
pub fn as_seconds(self) -> f32 {
self.0 as f32 / 1000000.
}
#[must_use]
#[allow(clippy::cast_possible_truncation)]
pub fn as_milliseconds(self) -> i32 {
(self.0 / 1000) as i32
}
#[must_use]
pub fn as_microseconds(self) -> i64 {
self.0
}
pub(crate) fn raw(self) -> sfTime {
self.0
}
pub(crate) fn from_raw(raw: sfTime) -> Self {
Time(raw)
}
pub const ZERO: Time = Time(0);
}
impl Neg for Time {
type Output = Self;
fn neg(self) -> Self {
Time(-self.0)
}
}
impl Add for Time {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Time::microseconds(self.0 + rhs.0)
}
}
impl AddAssign for Time {
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.0;
}
}
impl Sub for Time {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
Time::microseconds(self.0 - rhs.0)
}
}
impl SubAssign for Time {
fn sub_assign(&mut self, rhs: Self) {
self.0 -= rhs.0;
}
}
impl Mul<f32> for Time {
type Output = Self;
fn mul(self, rhs: f32) -> Self {
Time::seconds(self.as_seconds() * rhs)
}
}
impl Mul<i64> for Time {
type Output = Self;
fn mul(self, rhs: i64) -> Self {
Time::microseconds(self.as_microseconds() * rhs)
}
}
impl Mul<Time> for f32 {
type Output = Time;
fn mul(self, rhs: Time) -> Time {
rhs * self
}
}
impl Mul<Time> for i64 {
type Output = Time;
fn mul(self, rhs: Time) -> Time {
rhs * self
}
}
impl MulAssign<f32> for Time {
fn mul_assign(&mut self, rhs: f32) {
*self = *self * rhs;
}
}
impl MulAssign<i64> for Time {
fn mul_assign(&mut self, rhs: i64) {
*self = *self * rhs;
}
}
impl Div<f32> for Time {
type Output = Self;
fn div(self, rhs: f32) -> Self {
Time::seconds(self.as_seconds() / rhs)
}
}
impl Div<i64> for Time {
type Output = Self;
fn div(self, rhs: i64) -> Self {
Time::microseconds(self.as_microseconds() / rhs)
}
}
impl Div for Time {
type Output = f32;
fn div(self, rhs: Self) -> f32 {
self.as_seconds() / rhs.as_seconds()
}
}
impl DivAssign<f32> for Time {
fn div_assign(&mut self, rhs: f32) {
*self = *self / rhs;
}
}
impl DivAssign<i64> for Time {
fn div_assign(&mut self, rhs: i64) {
*self = *self / rhs;
}
}
impl Rem for Time {
type Output = Self;
fn rem(self, rhs: Self) -> Self {
Time::microseconds(self.0 % rhs.0)
}
}
impl RemAssign for Time {
fn rem_assign(&mut self, rhs: Self) {
self.0 %= rhs.0
}
}
impl Default for Time {
fn default() -> Self {
Self::ZERO
}
}