-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputManager.cpp
143 lines (119 loc) · 3.08 KB
/
InputManager.cpp
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
#include "precomp.h"
#include "InputManager.h"
#include "Input.h"
#include "Binding.h"
#include "PlayerData.h"
InputManager::InputManager() : data{ new PlayerData("input") }
{
for (auto& inp : inputs)
{
inp = make_unique<Input>();
}
// Make sure every bound input also has a default value.
assert(data->GetVariables().size() == data->GetVariables("default").size());
LoadKeyBindingsFromFile();
}
InputManager::~InputManager() {}
void InputManager::Tick()
{
while (!interactedWith.empty())
{
Input* inp = interactedWith.front();
interactedWith.pop();
// ResetTimeAlive the input state for the next frame.
inp->ResetForNextFrame();
}
mouseWheelChange = 0;
}
const Binding& InputManager::GetBinding(std::string_view name)
{
return *Inst().bindingsByName.at(name);
}
void InputManager::LoadKeyBindingsFromFile(bool loadDefaultValues)
{
bindings.clear();
bindingsByName.clear();
list<Data::Variable*> inputSettings = data->GetVariables((loadDefaultValues ? "default" : ""));
for (Data::Variable* var : inputSettings)
{
auto kb = make_unique<Binding>(var, this);
bindingsByName[kb->name] = kb.get();
bindings.push_back(move(kb));
}
}
//void InputManager::Unbind(std::string_view bindingName, InputID fromInput)
//{
// InputManager& instance = Inst();
// instance.bindingsByName.at(bindingName)->UnbindFrom(&instance.inputs.at(fromInput));
//}
//
//void InputManager::Bind(std::string_view bindingName, InputID toInput)
//{
// InputManager& instance = Inst();
// instance.bindingsByName.at(bindingName)->BindTo(&instance.inputs.at(toInput));
//}
//
//void InputManager::BindToNextInput(std::string_view bindingName)
//{
// InputManager& instance = Inst();
// instance.currentlyRebinding = instance.bindingsByName.at(bindingName);
//}
void InputManager::KeyDown(int key)
{
UpdateInputState(ToInputID(key, glfwType::key), InputManager::Interaction::pressed);
}
void InputManager::KeyUp(int key)
{
UpdateInputState(ToInputID(key, glfwType::key), Interaction::released);
}
void InputManager::MouseDown(int button)
{
UpdateInputState(ToInputID(button, glfwType::mousebutton), Interaction::pressed);
}
void InputManager::MouseUp(int button)
{
UpdateInputState(ToInputID(button, glfwType::mousebutton), Interaction::released);
}
void InputManager::MouseWheel(float f)
{
mouseWheelChange = static_cast<int>(f);
}
void InputManager::MouseMove(int x, int y)
{
mousePos = { x, y };
}
InputID InputManager::ToInputID(uint num, glfwType type)
{
switch (type)
{
case InputManager::glfwType::key:
return num;
break;
case InputManager::glfwType::mousebutton:
return num + GLFW_KEY_LAST;
break;
default:
static_assert("glfwType not supported");
break;
}
return InputID();
}
void InputManager::UpdateInputState(InputID keyCode, Interaction interaction)
{
Input* inp = inputs[keyCode].get();
//if (currentlyRebinding && interaction == Interaction::pressed)
//{
// currentlyRebinding->BindTo(inp);
// currentlyRebinding = nullptr;
//}
interactedWith.push(inp);
if (interaction == Interaction::pressed)
{
inp->Down();
LOGMESSAGE(keyCode << " pressed.");
}
else
{
inp->Up();
}
}