-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInputActionManager.cpp
More file actions
247 lines (211 loc) · 5.91 KB
/
Copy pathInputActionManager.cpp
File metadata and controls
247 lines (211 loc) · 5.91 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "InputActionManager.h"
#include "SceneManager.h"
#include "PathFinder.h"
#include "Interfaces/AssetAuthoringPort.h"
InputActionManager* InputActionManagers = nullptr;
void InputActionManager::Update(float tick)
{
if (SceneManagers->m_isGameStart == false) return;
if (!m_actionMaps.empty())
{
for (auto& actionMap : m_actionMaps)
{
actionMap->CheckAction();
}
}
}
void InputActionManager::AddActionMap()
{
std::string baseName = "NewActionMap";
std::string finalName = baseName;
int uniqueIndex = 0;
// �̹� �����ϴ� �̸��� �ִ� ���� �ݺ�
while (FindActionMap(finalName) != nullptr)
{
finalName = baseName + std::to_string(uniqueIndex);
uniqueIndex++;
}
ActionMap* newActionMap = new ActionMap();
newActionMap->m_name = finalName;
m_actionMaps.push_back(newActionMap);
}
ActionMap* InputActionManager::AddActionMap(std::string name)
{
for (auto& actionMap : m_actionMaps)
{
if (actionMap->m_name == name)
{
return actionMap;
}
}
ActionMap* newActionMap = new ActionMap();
newActionMap->m_name = name;
m_actionMaps.push_back(newActionMap);
return newActionMap;
}
void InputActionManager::DeleteActionMap(std::string name)
{
auto deleteMap = FindActionMap(name);
if (deleteMap != nullptr)
{
auto it = std::find(m_actionMaps.begin(), m_actionMaps.end(), deleteMap);
if (it != m_actionMaps.end())
{
delete* it;
m_actionMaps.erase(it);
}
}
}
ActionMap* InputActionManager::FindActionMap(std::string name)
{
for (auto& actionMap : m_actionMaps)
{
if (actionMap->m_name == name)
{
return actionMap;
}
}
std::cout << "ActionMap not found: " << name << std::endl;
return nullptr;
}
bool InputActionManager::SaveManager()
{
if (m_actionMaps.empty()) return true;
// 한 맵이 실패해도 나머지는 계속 쓴다 — 기존 동작(전부 시도)을 유지하되 실패를
// 삼키지 않고 호출자에게 돌려준다.
bool allSaved = true;
for (auto& actionMap : m_actionMaps)
{
if (!SerializeMap(actionMap)) allSaved = false;
}
return allSaved;
}
void InputActionManager::LoadManager()
{
ClearActionMaps();
namespace fs = std::filesystem;
fs::path dirPath = PathFinder::InputMapPath();
if (!fs::exists(dirPath) || !fs::is_directory(dirPath))
{
std::cerr << "Directory does not exist: " << dirPath << std::endl;
return;
}
for (const auto& entry : fs::directory_iterator(dirPath))
{
if (entry.is_regular_file() && entry.path().extension() == ".json")
{
std::string filePath = entry.path().string();
m_actionMaps.push_back(DeSerializeMap(filePath));
}
}
}
bool InputActionManager::SerializeMap(ActionMap* _actionMap)
{
if (nullptr == _actionMap) return false;
nlohmann::json json;
nlohmann::json actionArray = nlohmann::json::array();
json["mapName"] = _actionMap->m_name;
for (auto& action : _actionMap->m_actions)
{
nlohmann::json actionJson;
actionJson["actionName"] = action->actionName;
actionJson["inputType"] = InputTypeString(action->inputType);
actionJson["actionType"] = ActionTypeString(action->actionType);
actionJson["keystate"] = KeyStateString(action->keystate);
int i = 0;
for (auto& key : action->key)
{
if (action->inputType == InputType::KeyBoard)
{
actionJson["key" + std::to_string(i)] = KeyBoardString(static_cast<KeyBoard>(key));
}
else if (action->inputType == InputType::GamePad)
{
actionJson["key" + std::to_string(i)] = ControllerButtonString(static_cast<ControllerButton>(key));
}
else
{
}
i++;
}
actionJson["scpritName"] = action->m_scriptName;
actionJson["funName"] = action->funName;
actionArray.push_back(actionJson);
}
json["actions"] = actionArray;
// replace_extension은 이름에 '.'이 있으면 그 뒤를 통째로 잘라낸다("Player.v2" →
// "Player.json"). 문자열로 붙여 이름을 보존한다.
UncatalogedAuthoringRequest request{};
request.destinationPath = PathFinder::InputMapPath(_actionMap->m_name + ".json");
request.payload = json.dump(4);
if (!AssetAuthoringPort::WriteInputActionMap(request))
{
Debug->LogError(
"Input action map save requires a complete Editor authoring "
"transaction: " + _actionMap->m_name);
return false;
}
return true;
}
ActionMap* InputActionManager::DeSerializeMap(std::string _filepath)
{
std::ifstream file(_filepath);
if (!file.is_open())
{
std::cerr << "Failed to open: " << _filepath << std::endl;
return nullptr;
}
nlohmann::json json;
try {
file >> json;
}
catch (std::exception& e) {
std::cerr << "JSON parsing error: " << e.what() << std::endl;
return nullptr;
}
ActionMap* newMap = new ActionMap();
newMap->m_name = json["mapName"];
for (auto& actionJson : json["actions"])
{
auto action = newMap->AddAction();
action->actionName = actionJson["actionName"];
action->inputType = ParseInputType(actionJson["inputType"]);
action->actionType = ParseActionType(actionJson["actionType"]);
action->keystate = ParseKeyState(actionJson["keystate"]);
action->funName = actionJson["funName"];
action->m_scriptName = actionJson["scpritName"];
action->key.resize(4);
// key0, key1, key2... ������ Ű���� �Ľ�
for (int i = 0; ; ++i)
{
std::string keyName = "key" + std::to_string(i);
if (actionJson.contains(keyName))
{
std::string keyname = actionJson[keyName];
if (action->inputType == InputType::KeyBoard)
{
action->key[i] = static_cast<size_t>(ParseKeyBoard(keyname));
}
else if (action->inputType == InputType::GamePad)
{
if (ParseControllerButton(keyname) == ControllerButton::LEFT_THUMB ||
ParseControllerButton(keyname) == ControllerButton::RIGHT_THUMB
)
{
action->SetControllerButton(ParseControllerButton(keyname));
}
else
{
action->key[i] = static_cast<size_t>(ParseControllerButton(keyname));
}
}
else
{
}
}
else
break;
}
}
return newMap;
}