Leviathan  0.8.0.0
Leviathan game engine
InputController.cpp
Go to the documentation of this file.
1 // ------------------------------------ //
2 #include "InputController.h"
3 
4 using namespace Leviathan;
5 // ------------------------------------ //
7 
9 {
10  // Unlink all children //
11  for(size_t i = 0; i < ConnectedReceivers.size(); i++) {
12  // Call disconnect function first //
13  ConnectedReceivers[i]->_OnDisconnect(this);
14  }
15 
16  ConnectedReceivers.clear();
17 }
18 // ------------------------------------ //
20 {
21  GUARD_LOCK();
22  // reset internal state //
23 
24  // tell listeners that new frame begins //
25  for(size_t i = 0; i < ConnectedReceivers.size(); i++) {
26 
27  ConnectedReceivers[i]->BeginNewReceiveQueue();
28  }
29 }
30 
31 DLLEXPORT void InputController::OnInputGet(int32_t key, int specialmodifiers, bool down)
32 {
33  // Call on first that wants it //
34  bool received = false;
35 
36  GUARD_LOCK();
37 
38  for(size_t i = 0; i < ConnectedReceivers.size(); i++) {
39 
40  if(received) {
41 
42  ConnectedReceivers[i]->ReceiveBlockedInput(key, specialmodifiers, down);
43  continue;
44  }
45 
46  if(ConnectedReceivers[i]->ReceiveInput(key, specialmodifiers, down))
47  received = true;
48  }
49 }
50 
51 DLLEXPORT void InputController::OnBlockedInput(int32_t key, int specialmodifiers, bool down)
52 {
53  GUARD_LOCK();
54 
55  // call on all //
56  for(size_t i = 0; i < ConnectedReceivers.size(); i++) {
57 
58  ConnectedReceivers[i]->ReceiveBlockedInput(key, specialmodifiers, down);
59  }
60 }
61 
62 DLLEXPORT void InputController::OnScroll(int x, int y, int modifiers)
63 {
64  // Call on first that wants it //
65  GUARD_LOCK();
66 
67  for(size_t i = 0; i < ConnectedReceivers.size(); i++) {
68 
69  if(ConnectedReceivers[i]->OnScroll(x, y, modifiers))
70  return;
71  }
72 }
73 
74 
75 DLLEXPORT void InputController::SendMouseMovement(int xmoved, int ymoved)
76 {
77 
78  GUARD_LOCK();
79 
80  // call on first that gets it //
81  for(size_t i = 0; i < ConnectedReceivers.size(); i++) {
82 
83  if(ConnectedReceivers[i]->OnMouseMove(xmoved, ymoved))
84  return;
85  }
86 }
87 // ------------------------------------ //
88 DLLEXPORT void InputController::LinkReceiver(std::shared_ptr<InputReceiver> object)
89 {
90 
91  GUARD_LOCK();
92 
93  // Just add to list and call link function //
94  object->_OnConnected(this);
95 
96  ConnectedReceivers.push_back(object);
97 }
98 // ------------------------------------ //
100 {
101  for(size_t i = 0; i < ConnectedReceivers.size(); i++) {
102  if(ConnectedReceivers[i].get() == child) {
103  // call disconnect function first //
104  ConnectedReceivers[i]->_OnDisconnect(this);
105  ConnectedReceivers.erase(ConnectedReceivers.begin() + i);
106  return;
107  }
108  }
109 }
110 // ------------------ InputReceiver ------------------ //
111 DLLEXPORT InputReceiver::InputReceiver() : ConnectedTo(nullptr) {}
112 
114 {
115  // tell owner that we no longer exist //
116  LEVIATHAN_ASSERT(!ConnectedTo, "InputReceiver not _UnConnectParent called");
117 }
118 // ------------------------------------ //
119 DLLEXPORT bool InputReceiver::OnScroll(int x, int y, int modifiers)
120 {
121  return false;
122 }
123 // ------------------------------------ //
125 {
126 
127  if(ConnectedTo)
128  ConnectedTo->_OnChildUnlink(ownerlock, this);
129 
130  ConnectedTo = NULL;
131 }
132 // ------------------------------------ //
134 {
135  ConnectedTo = owner;
136 }
137 
139 {
140  ConnectedTo = NULL;
141 }
142 // ------------------------------------ //
DLLEXPORT void LinkReceiver(std::shared_ptr< InputReceiver > object)
Manages registering multiple InputReceiver objects to a window to easily control where input is sent.
virtual DLLEXPORT void _OnChildUnlink(Lock &guard, InputReceiver *child)
Called by input controllers if they unlink.
class to derive from when wanting to connect to input
virtual DLLEXPORT ~InputReceiver()
virtual DLLEXPORT void BeginNewReceiveQueue()
virtual DLLEXPORT void OnBlockedInput(int32_t key, int specialmodifiers, bool down)
This is called when input is not received.
#define LEVIATHAN_ASSERT(x, msg)
Definition: Define.h:100
virtual DLLEXPORT void StartInputGather()
virtual DLLEXPORT void OnInputGet(int32_t key, int specialmodifiers, bool down)
Called when the GUI didn't eat the whole keypress (it only ate half of the A-press)
#define DLLEXPORT
Definition: Include.h:84
virtual DLLEXPORT void OnScroll(int x, int y, int modifiers)
InputController * ConnectedTo
virtual DLLEXPORT bool OnScroll(int x, int y, int modifiers)
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
void _OnDisconnect(InputController *owner)
void _OnConnected(InputController *owner)
DLLEXPORT void _UnConnectParent(Lock &ownerlock)
If ConnectedTo is not NULL notifies it that we are no longer valid.
std::vector< std::shared_ptr< InputReceiver > > ConnectedReceivers
#define GUARD_LOCK()
Definition: ThreadSafe.h:111
std::unique_lock< std::mutex > Lock
Definition: ThreadSafe.h:18
virtual DLLEXPORT void SendMouseMovement(int xmoved, int ymoved)
When the mouse input is captured it is passed here.