move code to more relevant location

This commit is contained in:
Quaternions 2025-01-13 23:22:18 -08:00
parent 52bbaaddc7
commit 6898302fa5

View File

@ -27,9 +27,47 @@ enum BufferState{
Initializing(SessionTime,MouseState<PhysicsTimeInner>),
Buffered(SessionTime,MouseState<PhysicsTimeInner>),
}
impl BufferState{
fn next_state(self,ins:DoubleTimedUnbufferedInstruction)->((Option<TimedInstruction<MouseInstruction,PhysicsTimeInner>>,Option<TimedInstruction<OtherInstruction,PhysicsTimeInner>>),Self){
let next_state=match self{
pub struct MouseInterpolator{
buffer_state:BufferState,
// double timestamped timeline?
physics_timeline:std::collections::VecDeque<TimedPhysicsInstruction>,
}
// Maybe MouseInterpolator manipulation is better expressed using impls
// and called from Instruction trait impls in session
impl InstructionConsumer<PhysicsTimedUnbufferedInstruction> for MouseInterpolator{
type TimeInner=SessionTimeInner;
fn process_instruction(&mut self,ins:DoubleTimedUnbufferedInstruction){
self.push_unbuffered_input(ins)
}
}
impl InstructionEmitter<StepInstruction> for MouseInterpolator{
type TimeInner=SessionTimeInner;
fn next_instruction(&self,time_limit:SessionTime)->Option<TimedInstruction<StepInstruction,Self::TimeInner>>{
self.buffered_instruction_with_timeout(time_limit)
}
}
impl MouseInterpolator{
pub fn new()->MouseInterpolator{
MouseInterpolator{
buffer_state:BufferState::Unbuffered,
physics_timeline:std::collections::VecDeque::new(),
}
}
pub fn push_unbuffered_input(&mut self,ins:DoubleTimedUnbufferedInstruction){
// new input
// if there is zero instruction buffered, it means the mouse is not moving
// case 1: unbuffered
// no mouse event is buffered
// - ins is mouse event? change to buffered
// - ins other -> write to timeline
// case 2: buffered
// a mouse event is buffered, and exists within the last 10ms
// case 3: stop
// a mouse event is buffered, but no mouse events have transpired within 10ms
// replace_with allows the enum variant to safely be replaced from behind a mutable reference
let (ins_mouse,ins_other)=replace_with::replace_with_or_abort_and_return(&mut self.buffer_state,|buffer_state|{
let next_state=match buffer_state{
BufferState::Unbuffered=>{
if let PhysicsUnbufferedInstruction::MoveMouse(pos)=ins.instruction.instruction{
return ((None,None),BufferState::Initializing(ins.time,MouseState{pos,time:ins.instruction.time}));
@ -95,48 +133,6 @@ impl BufferState{
}),
};
((None,ins_other),next_state)
}
}
pub struct MouseInterpolator{
buffer_state:BufferState,
// double timestamped timeline?
physics_timeline:std::collections::VecDeque<TimedPhysicsInstruction>,
}
// Maybe MouseInterpolator manipulation is better expressed using impls
// and called from Instruction trait impls in session
impl InstructionConsumer<PhysicsTimedUnbufferedInstruction> for MouseInterpolator{
type TimeInner=SessionTimeInner;
fn process_instruction(&mut self,ins:DoubleTimedUnbufferedInstruction){
self.push_unbuffered_input(ins)
}
}
impl InstructionEmitter<StepInstruction> for MouseInterpolator{
type TimeInner=SessionTimeInner;
fn next_instruction(&self,time_limit:SessionTime)->Option<TimedInstruction<StepInstruction,Self::TimeInner>>{
self.buffered_instruction_with_timeout(time_limit)
}
}
impl MouseInterpolator{
pub fn new()->MouseInterpolator{
MouseInterpolator{
buffer_state:BufferState::Unbuffered,
physics_timeline:std::collections::VecDeque::new(),
}
}
pub fn push_unbuffered_input(&mut self,ins:DoubleTimedUnbufferedInstruction){
// new input
// if there is zero instruction buffered, it means the mouse is not moving
// case 1: unbuffered
// no mouse event is buffered
// - ins is mouse event? change to buffered
// - ins other -> write to timeline
// case 2: buffered
// a mouse event is buffered, and exists within the last 10ms
// case 3: stop
// a mouse event is buffered, but no mouse events have transpired within 10ms
// replace_with allows the enum variant to safely be replaced from behind a mutable reference
let (ins_mouse,ins_other)=replace_with::replace_with_or_abort_and_return(&mut self.buffer_state,|buffer_state|{
buffer_state.next_state(ins)
});
if let Some(ins)=ins_mouse{
self.physics_timeline.push_front(TimedInstruction{