cook a bit
This commit is contained in:
parent
47bf9f1af3
commit
1b35c96f6e
@ -31,8 +31,70 @@ pub enum StepInstruction{
|
|||||||
ReplaceMouse,
|
ReplaceMouse,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
enum BufferState{
|
||||||
|
Unbuffered,
|
||||||
|
Initializing(SessionTime,MouseState<PhysicsTimeInner>),
|
||||||
|
Buffered(SessionTime,MouseState<PhysicsTimeInner>),
|
||||||
|
}
|
||||||
|
impl BufferState{
|
||||||
|
fn next_state(&self,ins:DoubleTimedUnbufferedInstruction,physics_timeline:&mut std::collections::VecDeque<TimedPhysicsInstruction>)->Self{
|
||||||
|
match self{
|
||||||
|
BufferState::Unbuffered=>{
|
||||||
|
if let UnbufferedInputInstruction::MoveMouse(pos)=ins.instruction.instruction{
|
||||||
|
return BufferState::Initializing(ins.time,MouseState{pos,time:ins.instruction.time});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
BufferState::Initializing(time,mouse_state)=>{
|
||||||
|
let timeout=*time+MOUSE_TIMEOUT;
|
||||||
|
if timeout<ins.time{
|
||||||
|
// duplicate the current mouse
|
||||||
|
physics_timeline.push_front(TimedInstruction{
|
||||||
|
// This should be simulation_timer.time(time)
|
||||||
|
// but the timer is not accessible from this scope
|
||||||
|
// and it's just here to say that the mouse isn't moving anyways.
|
||||||
|
time:ins.instruction.time,
|
||||||
|
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{pos:mouse_state.pos,time:ins.instruction.time}),
|
||||||
|
});
|
||||||
|
return BufferState::Unbuffered;
|
||||||
|
}
|
||||||
|
if let UnbufferedInputInstruction::MoveMouse(pos)=ins.instruction.instruction{
|
||||||
|
let next_mouse_state=MouseState{pos,time:ins.instruction.time};
|
||||||
|
physics_timeline.push_front(TimedInstruction{
|
||||||
|
time:mouse_state.time,
|
||||||
|
instruction:PhysicsInputInstruction::ReplaceMouse(mouse_state.clone(),next_mouse_state.clone()),
|
||||||
|
});
|
||||||
|
return BufferState::Buffered(ins.time,next_mouse_state);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
BufferState::Buffered(time,mouse_state)=>{
|
||||||
|
let timeout=*time+MOUSE_TIMEOUT;
|
||||||
|
if timeout<ins.time{
|
||||||
|
// duplicate the current mouse
|
||||||
|
physics_timeline.push_front(TimedInstruction{
|
||||||
|
// This should be simulation_timer.time(time)
|
||||||
|
// but the timer is not accessible from this scope
|
||||||
|
// and it's just here to say that the mouse isn't moving anyways.
|
||||||
|
time:ins.instruction.time,
|
||||||
|
instruction:PhysicsInputInstruction::SetNextMouse(MouseState{pos:mouse_state.pos,time:ins.instruction.time}),
|
||||||
|
});
|
||||||
|
return BufferState::Unbuffered;
|
||||||
|
}
|
||||||
|
if let UnbufferedInputInstruction::MoveMouse(pos)=ins.instruction.instruction{
|
||||||
|
let next_mouse_state=MouseState{pos,time:ins.instruction.time};
|
||||||
|
physics_timeline.push_front(TimedInstruction{
|
||||||
|
time:ins.instruction.time,
|
||||||
|
instruction:PhysicsInputInstruction::SetNextMouse(next_mouse_state.clone()),
|
||||||
|
});
|
||||||
|
return BufferState::Buffered(ins.time,next_mouse_state);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
self.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
pub struct MouseInterpolator{
|
pub struct MouseInterpolator{
|
||||||
last_mouse_state:MouseState<SessionTimeInner>,
|
buffer_state:BufferState,
|
||||||
// double timestamped timeline?
|
// double timestamped timeline?
|
||||||
physics_timeline:std::collections::VecDeque<TimedPhysicsInstruction>,
|
physics_timeline:std::collections::VecDeque<TimedPhysicsInstruction>,
|
||||||
}
|
}
|
||||||
@ -53,13 +115,23 @@ impl InstructionEmitter<StepInstruction> for MouseInterpolator{
|
|||||||
impl MouseInterpolator{
|
impl MouseInterpolator{
|
||||||
pub fn new()->MouseInterpolator{
|
pub fn new()->MouseInterpolator{
|
||||||
MouseInterpolator{
|
MouseInterpolator{
|
||||||
last_mouse_state:MouseState::default(),
|
buffer_state:BufferState::Unbuffered,
|
||||||
physics_timeline:std::collections::VecDeque::new(),
|
physics_timeline:std::collections::VecDeque::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn push_unbuffered_input(&mut self,ins:DoubleTimedUnbufferedInstruction){
|
pub fn push_unbuffered_input(&mut self,ins:DoubleTimedUnbufferedInstruction){
|
||||||
// new input
|
// new input
|
||||||
// if there is zero instruction buffered, it means the mouse is not moving
|
// 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
|
||||||
|
// TODO: remove clone
|
||||||
|
self.buffer_state=self.buffer_state.next_state(ins,&mut self.physics_timeline);
|
||||||
}
|
}
|
||||||
fn is_first_ready(&self)->bool{
|
fn is_first_ready(&self)->bool{
|
||||||
// if the last instruction is a ReplaceMouse instruction
|
// if the last instruction is a ReplaceMouse instruction
|
||||||
|
Loading…
Reference in New Issue
Block a user