Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/game/StarInput.cpp
blob: 59b959327109199b0ef6cba30b5936ba0c3d6d06 (plain)
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
#include "StarInput.hpp"
#include "StarAssets.hpp"
#include "StarRoot.hpp"

namespace Star {

Json keyModsToJson(KeyMod mod) {
  JsonArray array;
  
  if ((bool)(mod & KeyMod::LShift)) array.emplace_back("LShift");
  if ((bool)(mod & KeyMod::RShift)) array.emplace_back("RShift");
  if ((bool)(mod & KeyMod::LCtrl )) array.emplace_back("LCtrl" );
  if ((bool)(mod & KeyMod::RCtrl )) array.emplace_back("RCtrl" );
  if ((bool)(mod & KeyMod::LAlt  )) array.emplace_back("LAlt"  );
  if ((bool)(mod & KeyMod::RAlt  )) array.emplace_back("RAlt"  );
  if ((bool)(mod & KeyMod::LGui  )) array.emplace_back("LGui"  );
  if ((bool)(mod & KeyMod::RGui  )) array.emplace_back("RGui"  );
  if ((bool)(mod & KeyMod::Num   )) array.emplace_back("Num"   );
  if ((bool)(mod & KeyMod::Caps  )) array.emplace_back("Caps"  );
  if ((bool)(mod & KeyMod::AltGr )) array.emplace_back("AltGr" );
  if ((bool)(mod & KeyMod::Scroll)) array.emplace_back("Scroll");

  return move(array);
}

// Optional pointer argument to output calculated priority
KeyMod keyModsFromJson(Json const& json, uint8_t* priority = nullptr) {
  KeyMod mod = KeyMod::NoMod;
  if (!json.isType(Json::Type::Array))
    return mod;

  for (Json const& jMod : json.toArray()) {
    if (mod != (mod |= KeyModNames.getLeft(jMod.toString())) && priority)
      ++*priority;
  }

  return mod;
}

size_t hash<InputVariant>::operator()(InputVariant const& v) const {
  size_t indexHash = hashOf(v.typeIndex());
  if (auto key = v.ptr<Key>())
    hashCombine(indexHash, hashOf(*key));
  else if (auto mButton = v.ptr<MouseButton>())
    hashCombine(indexHash, hashOf(*mButton));
  else if (auto cButton = v.ptr<ControllerButton>())
    hashCombine(indexHash, hashOf(*cButton));

  return indexHash;
}

Input::Bind Input::bindFromJson(Json const& json) {
  Bind bind;

  String type = json.getString("type");
  Json value = json.get("value");

  if (type == "key") {
    KeyBind keyBind;
    keyBind.key = KeyNames.getLeft(value.toString());
    keyBind.mods = keyModsFromJson(json.getArray("mods", {}), &keyBind.priority);
    bind = move(keyBind);
  }
  else if (type == "mouse") {
    MouseBind mouseBind;
    mouseBind.button = MouseButtonNames.getLeft(value.toString());
    mouseBind.mods = keyModsFromJson(json.getArray("mods", {}), &mouseBind.priority);
    bind = move(mouseBind);
  }
  else if (type == "controller") {
    ControllerBind controllerBind;
    controllerBind.button = ControllerButtonNames.getLeft(value.toString());
    controllerBind.controller = json.getUInt("controller", 0);
    bind = move(controllerBind);
  }

  return bind;
}

Json Input::bindToJson(Bind const& bind) {
  if (auto keyBind = bind.ptr<KeyBind>()) {
    return JsonObject{
      {"type", "key"},
      {"value", KeyNames.getRight(keyBind->key)},
      {"mods", keyModsToJson(keyBind->mods)}
    };
  }
  else if (auto mouseBind = bind.ptr<MouseBind>()) {
    return JsonObject{
      {"type", "mouse"},
      {"value", MouseButtonNames.getRight(mouseBind->button)},
      {"mods", keyModsToJson(mouseBind->mods)}
    };
  }
  else if (auto controllerBind = bind.ptr<ControllerBind>()) {
    return JsonObject{
      {"type", "controller"},
      {"value", ControllerButtonNames.getRight(controllerBind->button)}
    };
  }

  return Json();
}

Input::BindEntry::BindEntry(String entryId, Json const& config, BindCategory const& parentCategory) {
  category = &parentCategory;
  id = move(entryId);
  name = config.getString("name", id);

  for (Json const& jBind : config.getArray("default", {})) {
    try
      { defaultBinds.emplace_back(bindFromJson(jBind)); }
    catch (JsonException const& e)
      { Logger::error("Binds: Error loading default bind in {}.{}: {}", parentCategory.id, id, e.what()); }
  }
}

Input::BindCategory::BindCategory(String categoryId, Json const& categoryConfig) {
  id = move(categoryId);
  name = config.getString("name", id);
  config = categoryConfig;

  ConfigurationPtr userConfig = Root::singletonPtr()->configuration();
  auto userBindings = userConfig->get("modBindings");

  for (auto& pair : config.getObject("binds", {})) {
    String const& bindId = pair.first;
    Json const& bindConfig = pair.second;
    if (!bindConfig.isType(Json::Type::Object))
      continue;

    BindEntry& entry = entries.insert(bindId, BindEntry(bindId, bindConfig, *this)).first->second;

    if (userBindings.isType(Json::Type::Object)) {
      for (auto& jBind : userBindings.queryArray(strf("{}.{}", id, bindId), {})) {
        try
          { entry.customBinds.emplace_back(bindFromJson(jBind)); }
        catch (JsonException const& e)
          { Logger::error("Binds: Error loading user bind in {}.{}: {}", id, bindId, e.what()); }
      }
    }
  }
}

Input* Input::s_singleton;

Input* Input::singletonPtr() {
  return s_singleton;
}

Input& Input::singleton() {
  if (!s_singleton)
    throw InputException("Input::singleton() called with no Input instance available");
  else
    return *s_singleton;
}

Input::Input() {
  if (s_singleton)
    throw InputException("Singleton Input has been constructed twice");

  s_singleton = this;

  reload();

  m_rootReloadListener = make_shared<CallbackListener>([&]() {
    reload();
  });

  Root::singletonPtr()->registerReloadListener(m_rootReloadListener);
}

Input::~Input() {
  s_singleton = nullptr;
}

void Input::reset() {
  m_inputStates.clear();
  m_bindStates.clear();
}

void Input::reload() {
  reset();
  m_bindCategories.clear();
  m_inputsToBinds.clear();

  auto assets = Root::singleton().assets();

  for (auto& bindPath : assets->scanExtension("binds")) {
    for (auto& pair : assets->json(bindPath).toObject()) {
      String const& categoryId = pair.first;
      Json const& categoryConfig = pair.second;
      if (!categoryConfig.isType(Json::Type::Object))
        continue;

      m_bindCategories.insert(categoryId, BindCategory(categoryId, categoryConfig));
    }
  }

  size_t count = 0;
  for (auto& pair : m_bindCategories)
    count += pair.second.entries.size();

  Logger::info("Binds: Loaded {} bind{}", count, count == 1 ? "" : "s");
}

}