-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathInputDispatcher.cpp
More file actions
147 lines (143 loc) · 3.77 KB
/
Copy pathInputDispatcher.cpp
File metadata and controls
147 lines (143 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "InputDispatcher.h"
#include "CoreLib/Tokenizer.h"
#include "CoreLib/LibIO.h"
namespace GameEngine
{
using namespace CoreLib;
using namespace CoreLib::Text;
using namespace CoreLib::IO;
InputDispatcher::InputDispatcher(const RefPtr<HardwareInputInterface>& pInputInterface)
{
inputInterface = pInputInterface;
}
void InputDispatcher::LoadMapping(const CoreLib::String & fileName)
{
TokenReader parser(File::ReadAllText(fileName));
while (!parser.IsEnd())
{
bool isAxis = true;
InputMappingValue mapVal;
if (parser.LookAhead("action"))
{
parser.Read("action");
isAxis = false;
}
else
parser.Read("axis");
mapVal.ActionName = parser.ReadWord();
parser.Read(":");
auto ch = parser.ReadStringLiteral().ToUpper();
if (isAxis)
{
if (parser.LookAhead(","))
{
parser.Read(",");
mapVal.Value = (float)parser.ReadDouble();
}
}
wchar_t key = ch[0];
if (ch == "SPACE")
key = SpecialKeys::Space;
else if (ch == "MBLEFT")
key = SpecialKeys::MouseLeftButton;
else if (ch == "MBRIGHT")
key = SpecialKeys::MouseRightButton;
else if (ch == "MBMIDDLE")
key = SpecialKeys::MouseMiddleButton;
else if (ch == "CTRL")
key = SpecialKeys::Control;
else if (ch == "UP")
key = SpecialKeys::UpArrow;
else if (ch == "DOWN")
key = SpecialKeys::DownArrow;
else if (ch == "LEFT")
key = SpecialKeys::LeftArrow;
else if (ch == "RIGHT")
key = SpecialKeys::RightArrow;
else if (ch == "ESCAPE")
key = SpecialKeys::Escape;
else if (ch == "ENTER")
key = SpecialKeys::Enter;
else if (ch == "SHIFT")
key = SpecialKeys::Shift;
else if (ch == "`")
key = SpecialKeys::Tilda;
if (isAxis)
keyboardAxisMappings[key] = mapVal;
else
keyboardActionMappings[key] = mapVal;
actionHandlers[mapVal.ActionName] = List<ActionInputHandlerFunc>();
}
}
void InputDispatcher::BindActionHandler(const CoreLib::String & axisName, ActionInputHandlerFunc handlerFunc)
{
auto list = actionHandlers.TryGetValue(axisName);
if (!list)
{
actionHandlers.Add(axisName, List<ActionInputHandlerFunc>());
list = actionHandlers.TryGetValue(axisName);
}
list->Add(handlerFunc);
}
void InputDispatcher::UnbindActionHandler(const CoreLib::String & axisName, ActionInputHandlerFunc func)
{
auto list = actionHandlers.TryGetValue(axisName);
if (list)
{
for (int i = list->Count() - 1; i >= 0; i--)
{
if ((*list)[i] == func)
{
list->RemoveAt(i);
break;
}
}
}
}
void InputDispatcher::BindMouseInputHandler(ActorMouseInputHandlerFunc handlerFunc)
{
mouseActionHandlers.Add(handlerFunc);
}
void InputDispatcher::UnbindMouseInputHandler(ActorMouseInputHandlerFunc handlerFunc)
{
for (int i = mouseActionHandlers.Count() - 1; i >= 0; i--)
{
if (mouseActionHandlers[i] == handlerFunc)
{
mouseActionHandlers.RemoveAt(i);
break;
}
}
}
void InputDispatcher::DispatchInput(int channel)
{
for (auto & binding : keyboardActionMappings)
{
if (inputInterface->QueryKeyState(binding.Key).HasPressed)
{
DispatchAction(binding.Value.ActionName, ArrayView<String>(), binding.Value.Value, channel);
}
}
for (auto & binding : keyboardAxisMappings)
{
if (inputInterface->QueryKeyState(binding.Key).IsDown)
{
DispatchAction(binding.Value.ActionName, ArrayView<String>(), binding.Value.Value, channel);
}
}
}
void InputDispatcher::DispatchAction(CoreLib::String actionName, ArrayView<String> args, float actionValue, int channel)
{
auto handlers = actionHandlers.TryGetValue(actionName);
if (handlers)
{
ActionInput input;
input.AxisValue = actionValue;
input.Arguments = args;
input.Channel = channel;
for (auto & handler : *handlers)
if (handler(actionName, input))
break;
}
}
}