Sleeping Wombat GUI  0.100
swGUI
InputDispatcher.h
Go to the documentation of this file.
1 #pragma once
2 
11 
12 
13 namespace sw {
14 namespace input
15 {
16 
20 {
21 public:
22 
24  {
27 
28  // ================================ //
29  //
31  : Event( evt )
32  , ProducerDevice( device )
33  {}
34  };
35 
36 private:
37 
38  std::vector< KeyboardDeviceOPtr >& m_keyboards;
39  std::vector< MouseDeviceOPtr >& m_mouses;
40  std::vector< JoystickDeviceOPtr >& m_joysticks;
41 
44 
45 protected:
46 public:
47  explicit InputDispatcher ( IInput* input );
48  ~InputDispatcher () = default;
49 
50 
52  bool NoEventsLeft ();
53 
54 
55  std::vector< KeyboardDeviceOPtr >& GetKeyboards () { return m_keyboards; }
56  std::vector< MouseDeviceOPtr >& GetMouses () { return m_mouses; }
57  std::vector< JoystickDeviceOPtr >& GetJoysticks () { return m_joysticks; }
58 
59 private:
60 
61  DispatchedEvent FindEvent ( Timestamp timestamp );
63 };
64 
65 
66 //====================================================================================//
67 // Inline implementation
68 //====================================================================================//
69 
70 
72  : m_keyboards( input->GetKeyboardDevice() )
73  , m_mouses( input->GetMouseDevice() )
74  , m_joysticks( input->GetJoystickDevice() )
75  , m_nextTimestamp( 0 )
76  , m_eventLeft( 0 )
77 {
78  for( auto & mouse : m_mouses )
79  m_eventLeft += mouse->GetEventsQueue().EventsLeft();
80 
81  for( auto & keyboard : m_keyboards )
82  m_eventLeft += keyboard->GetEventsQueue().EventsLeft();
83 
84  for( auto & joystick : m_joysticks )
85  m_eventLeft += joystick->GetEventsQueue().EventsLeft();
86 }
87 
88 // ================================ //
89 //
91 {
92  if( NoEventsLeft() )
93  return InputDispatcher::DispatchedEvent( nullptr, DeviceEvent() );
94 
95  m_eventLeft--;
96 
97  auto dispatched = FindEvent( m_nextTimestamp );
98  if( dispatched.ProducerDevice == nullptr )
99  {
100  // Only informational assert. You can remove in future.
101  assert( !"Lost event ID" );
102 
103  // If event with m_nextTimestamp not found.
105  return FindEvent( m_nextTimestamp++ );
106  }
107 
108  m_nextTimestamp++;
109  return dispatched;
110 }
111 
112 // ================================ //
113 //
115 {
116  return m_eventLeft == 0;
117 }
118 
119 // ================================ //
120 //
122 {
123  // Mouse events are will be the most often.
124  for( int i = 0; i < m_mouses.size(); i++ )
125  {
126  if( m_mouses[ i ]->GetNextEvtTimestamp() == timestamp )
127  return DispatchedEvent( m_mouses[ i ].get(), m_mouses[ i ]->ApplyNextEvent() );
128  }
129 
130  for( int i = 0; i < m_keyboards.size(); i++ )
131  {
132  if( m_keyboards[ i ]->GetNextEvtTimestamp() == timestamp )
133  return DispatchedEvent( m_keyboards[ i ].get(), m_keyboards[ i ]->ApplyNextEvent() );
134  }
135 
136  for( int i = 0; i < m_joysticks.size(); i++ )
137  {
138  if( m_joysticks[ i ]->GetNextEvtTimestamp() == timestamp )
139  return DispatchedEvent( m_joysticks[ i ].get(), m_joysticks[ i ]->ApplyNextEvent() );
140  }
141 
142  return InputDispatcher::DispatchedEvent( nullptr, DeviceEvent() );
143 }
144 
145 // ================================ //
146 //
148 {
149  Timestamp minTimestamp = std::numeric_limits< Timestamp >::max();
150 
151  // Mouse events are will be the most often.
152  for( int i = 0; i < m_mouses.size(); i++ )
153  {
154  if( m_mouses[ i ]->GetNextEvtTimestamp() < minTimestamp )
155  minTimestamp = m_mouses[ i ]->GetNextEvtTimestamp();
156  }
157 
158  for( int i = 0; i < m_keyboards.size(); i++ )
159  {
160  if( m_keyboards[ i ]->GetNextEvtTimestamp() < minTimestamp )
161  minTimestamp = m_keyboards[ i ]->GetNextEvtTimestamp();
162  }
163 
164  for( int i = 0; i < m_joysticks.size(); i++ )
165  {
166  if( m_joysticks[ i ]->GetNextEvtTimestamp() < minTimestamp )
167  minTimestamp = m_joysticks[ i ]->GetNextEvtTimestamp();
168  }
169 
170  return minTimestamp;
171 }
172 
173 } // input
174 } // sw
175 
Device * ProducerDevice
Definition: InputDispatcher.h:26
Timestamp FindMinTimestamp()
Definition: InputDispatcher.h:147
uint16 Timestamp
Definition: InputDeviceEvent.h:19
std::vector< JoystickDeviceOPtr > & GetJoysticks()
Definition: InputDispatcher.h:57
Size m_eventLeft
Definition: InputDispatcher.h:43
std::vector< MouseDeviceOPtr > & m_mouses
Definition: InputDispatcher.h:39
DispatchedEvent FindEvent(Timestamp timestamp)
Definition: InputDispatcher.h:121
Definition: DirectInputModule.cpp:11
std::vector< MouseDeviceOPtr > & GetMouses()
Definition: InputDispatcher.h:56
Provides input events in order of creation. This class uses timestamp to sort events.
Definition: InputDispatcher.h:19
KeyStates changed events.
Definition: InputDeviceEvent.h:313
Definition: InputDispatcher.h:23
std::vector< JoystickDeviceOPtr > & m_joysticks
Definition: InputDispatcher.h:40
InputDispatcher(IInput *input)
Definition: InputDispatcher.h:71
Interface class for input devices.
Definition: Device.h:21
DispatchedEvent NextEvent()
Definition: InputDispatcher.h:90
Timestamp m_nextTimestamp
Definition: InputDispatcher.h:42
size_t Size
Definition: TypesDefinitions.h:35
bool NoEventsLeft()
Definition: InputDispatcher.h:114
std::vector< KeyboardDeviceOPtr > & m_keyboards
Definition: InputDispatcher.h:38
Interface for input classes for capturing user input.
Definition: IInput.h:65
DispatchedEvent(Device *device, DeviceEvent evt)
Definition: InputDispatcher.h:30
DeviceEvent Event
Definition: InputDispatcher.h:25
std::vector< KeyboardDeviceOPtr > & GetKeyboards()
Definition: InputDispatcher.h:55