From 3e3ebc9be09d016c082e6625a6c9b2261a4215d8 Mon Sep 17 00:00:00 2001 From: nertsal Date: Wed, 23 Oct 2024 21:44:49 +0300 Subject: [PATCH] convert timings (it compiles) --- crates/ctl-core/src/legacy/v1.rs | 4 +- crates/ctl-core/src/model/level.rs | 10 ++-- crates/ctl-core/src/model/movement.rs | 8 +-- crates/ctl-core/src/types.rs | 47 +++++++++++++---- crates/ctl-server/src/server/group.rs | 2 +- src/editor/action/level.rs | 34 +++++++------ src/editor/action/state.rs | 4 +- src/editor/config.rs | 4 +- src/editor/handle_event.rs | 6 +-- src/editor/mod.rs | 69 ++++++++++++------------- src/editor/ui/config.rs | 72 ++++++++++++++------------- src/editor/ui/edit.rs | 40 +++++++++------ src/game.rs | 6 +-- src/local/cache.rs | 2 +- src/local/fs.rs | 16 ++++-- src/local/fs/native.rs | 7 ++- src/media.rs | 6 +-- src/menu/level.rs | 20 ++++---- src/menu/main.rs | 10 ++-- src/menu/splash.rs | 6 +-- src/model/level/config.rs | 14 +++--- src/model/level/state.rs | 26 +++++----- src/model/logic/event.rs | 2 +- src/model/logic/mod.rs | 35 +++++++------ src/model/mod.rs | 34 ++++++------- src/model/player.rs | 10 ++-- src/model/score.rs | 6 +-- src/render/dither.rs | 2 +- src/render/editor/game.rs | 20 ++++---- src/render/editor/ui.rs | 8 +-- src/render/game.rs | 2 +- src/render/mod.rs | 4 +- src/render/util.rs | 4 +- src/ui/widget/timeline.rs | 35 ++++++------- 34 files changed, 313 insertions(+), 262 deletions(-) diff --git a/crates/ctl-core/src/legacy/v1.rs b/crates/ctl-core/src/legacy/v1.rs index c57b14b9..ad2a08f6 100644 --- a/crates/ctl-core/src/legacy/v1.rs +++ b/crates/ctl-core/src/legacy/v1.rs @@ -186,7 +186,7 @@ fn convert_level(beat_time: crate::FloatTime, value: Level) -> crate::Level { .events .into_iter() .map(|event| crate::TimedEvent { - beat: convert_time(beat_time, event.beat), + time: convert_time(beat_time, event.beat), event: match event.event { Event::Light(light) => crate::Event::Light(crate::LightEvent { light: crate::LightSerde { @@ -245,5 +245,5 @@ fn convert_frame(beat_time: crate::FloatTime, value: MoveFrame) -> crate::MoveFr } fn convert_time(beat_time: crate::FloatTime, time: crate::FloatTime) -> crate::Time { - crate::convert_time(beat_time * time) + crate::seconds_to_time(beat_time * time) } diff --git a/crates/ctl-core/src/model/level.rs b/crates/ctl-core/src/model/level.rs index 65fe2e29..33c1e4db 100644 --- a/crates/ctl-core/src/model/level.rs +++ b/crates/ctl-core/src/model/level.rs @@ -10,7 +10,7 @@ pub struct Level { #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct TimedEvent { /// The beat on which the event should happen. - pub beat: Time, + pub time: Time, pub event: Event, } @@ -40,7 +40,7 @@ pub struct LightEvent { #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub struct Telegraph { - /// How long (in beats) before the event should the telegraph occur. + /// How long (in milliseconds) before the event should the telegraph occur. pub precede_time: Time, /// How fast the telegraph is. pub speed: Coord, @@ -51,11 +51,11 @@ impl Level { Self { events: Vec::new() } } - /// Calculate the last beat when anything happens. - pub fn last_beat(&self) -> Time { + /// Calculate the last time when anything happens. + pub fn last_time(&self) -> Time { self.events .iter() - .map(|event| event.beat + event.duration()) + .map(|event| event.time + event.duration()) .max() .unwrap_or(Time::ZERO) } diff --git a/crates/ctl-core/src/model/movement.rs b/crates/ctl-core/src/model/movement.rs index 3ff84721..6849079d 100644 --- a/crates/ctl-core/src/model/movement.rs +++ b/crates/ctl-core/src/model/movement.rs @@ -2,9 +2,9 @@ use super::*; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] pub struct Movement { - /// Time (in beats) to spend fading into the initial position. + /// Time (in milliseconds) to spend fading into the initial position. pub fade_in: Time, - /// Time (in beats) to spend fading out of the last keyframe. + /// Time (in milliseconds) to spend fading out of the last keyframe. pub fade_out: Time, pub initial: Transform, #[serde(default)] @@ -126,9 +126,9 @@ impl Interpolatable for Transform { } impl MoveFrame { - pub fn scale(lerp_time: Time, scale: impl Float) -> Self { + pub fn scale(lerp_time: impl Float, scale: impl Float) -> Self { Self { - lerp_time, + lerp_time: seconds_to_time(FloatTime::new(lerp_time.as_f32())), interpolation: MoveInterpolation::default(), change_curve: None, transform: Transform::scale(scale), diff --git a/crates/ctl-core/src/types.rs b/crates/ctl-core/src/types.rs index c26fb039..2a8d2a6f 100644 --- a/crates/ctl-core/src/types.rs +++ b/crates/ctl-core/src/types.rs @@ -10,10 +10,14 @@ pub type Name = Arc; /// 1000 means that each time unit is a millisecond. pub const TIME_IN_FLOAT_TIME: Time = 1000; -pub fn convert_time(time: FloatTime) -> Time { +pub fn seconds_to_time(time: FloatTime) -> Time { (time.as_f32() / TIME_IN_FLOAT_TIME as f32).round() as Time } +pub fn time_to_seconds(time: Time) -> FloatTime { + FloatTime::new(time as f32 * TIME_IN_FLOAT_TIME as f32) +} + #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct BeatTime { /// 1 unit is 1/16 of a beat (typically a 1/64th note). @@ -21,15 +25,35 @@ pub struct BeatTime { } impl BeatTime { + const UNITS_PER_BEAT: Time = 16; + /// A whole beat (typically a 1/4th note) + pub const WHOLE: Self = Self { + units: Self::UNITS_PER_BEAT, + }; + /// A half beat (typically a 1/8th note) + pub const HALF: Self = Self { + units: Self::UNITS_PER_BEAT / 2, + }; + /// A quarter beat (typically a 1/16th note) + pub const QUARTER: Self = Self { + units: Self::UNITS_PER_BEAT / 4, + }; + /// An eighth beat (typically a 1/32th note) + pub const EIGHTH: Self = Self { + units: Self::UNITS_PER_BEAT / 8, + }; + /// From whole beats. pub fn from_beats(beats: Time) -> Self { - Self { units: beats * 64 } + Self { + units: beats * Self::UNITS_PER_BEAT, + } } /// From quarter beats. pub fn from_4ths(quarters: Time) -> Self { Self { - units: quarters * 16, + units: quarters * (Self::UNITS_PER_BEAT / 4), } } @@ -39,7 +63,17 @@ impl BeatTime { } pub fn as_millis(&self, beat_time: Time) -> Time { - self.units * beat_time / 16 + self.units * beat_time / Self::UNITS_PER_BEAT + } +} + +impl Mul