diff --git a/strafe-client/src/mouse_interpolator.rs b/strafe-client/src/mouse_interpolator.rs index 0c61089..d225bae 100644 --- a/strafe-client/src/mouse_interpolator.rs +++ b/strafe-client/src/mouse_interpolator.rs @@ -75,6 +75,37 @@ impl MouseInterpolator{ self.output.append(&mut self.buffer); } } + fn get_timedout_at(&self,time_limit:SessionTime)->Option{ + match &self.buffer_state{ + BufferState::Unbuffered=>None, + BufferState::Initializing(time,_mouse_state) + |BufferState::Buffered(time,_mouse_state)=>{ + let timeout=*time+MOUSE_TIMEOUT; + (timeout(), + BufferState::Initializing(_time,mouse_state) + |BufferState::Buffered(_time,mouse_state)=>{ + // convert to BufferState::Unbuffered + // use the first instruction which should be a mouse instruction + // to push a ReplaceMouse instruction + // duplicate the current mouse + self.push_mouse(TimedInstruction{ + // This should be simulation_timer.time(timeout) + // but the timer is not accessible from this scope + // and it's just here to say that the mouse isn't moving anyways. + // I think this is a divide by zero bug, two identical mouse_states will occupy the interpolation state + time:mouse_state.time, + instruction:MouseInstruction::SetNextMouse(mouse_state), + }); + }, + } + } pub fn push_unbuffered_input(&mut self,ins:DoubleTimedUnbufferedInstruction){ // new input // if there is zero instruction buffered, it means the mouse is not moving @@ -86,6 +117,11 @@ impl MouseInterpolator{ // 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 + + // push buffered mouse instruction and flush buffer to output + if self.get_timedout_at(ins.time).is_some(){ + self.timeout(); + } // 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{ @@ -96,18 +132,6 @@ impl MouseInterpolator{ BufferState::Unbuffered }, BufferState::Initializing(time,mouse_state)=>{ - let timeout=time+MOUSE_TIMEOUT; - if timeout{ - // TODO: deduplicate code with above case - let timeout=time+MOUSE_TIMEOUT; - if timeoutOption>{ - match &self.buffer_state{ - BufferState::Unbuffered=>self.has_output().then_some(TimedInstruction{ + match self.get_timedout_at(time_limit){ + Some(timeout)=>Some(TimedInstruction{ + time:timeout, + instruction:StepInstruction::Timeout, + }), + None=>self.has_output().then_some(TimedInstruction{ + // this timestamp should not matter time:time_limit, instruction:StepInstruction::Pop, }), - BufferState::Initializing(time,_mouse_state) - |BufferState::Buffered(time,_mouse_state)=>{ - let timeout=*time+MOUSE_TIMEOUT; - if timeoutOption>{ match ins{ - // could check if self.is_first_ready() StepInstruction::Pop=>(), - StepInstruction::Timeout=>{ - let buffer_state=core::mem::replace(&mut self.buffer_state,BufferState::Unbuffered); - match buffer_state{ - BufferState::Unbuffered=>(), - BufferState::Initializing(_time,mouse_state) - |BufferState::Buffered(_time,mouse_state)=>{ - // convert to BufferState::Unbuffered - // use the first instruction which should be a mouse instruction - // to push a ReplaceMouse instruction - // duplicate the current mouse - self.push_mouse(TimedInstruction{ - // This should be simulation_timer.time(timeout) - // but the timer is not accessible from this scope - // and it's just here to say that the mouse isn't moving anyways. - // I think this is a divide by zero bug, two identical mouse_states will occupy the interpolation state - time:mouse_state.time, - instruction:MouseInstruction::SetNextMouse(mouse_state), - }); - }, - } - }, + StepInstruction::Timeout=>self.timeout(), } self.output.pop_front() }