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

summaryrefslogtreecommitdiff
path: root/source/windowing/StarKeyBindings.cpp
blob: d082a3eb364b79b8e38858bf8fdcb8ad6dc81cc8 (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
208
209
210
211
212
#include "StarKeyBindings.hpp"
#include "StarRoot.hpp"
#include "StarConfiguration.hpp"
#include "StarLogging.hpp"
#include "StarJsonExtra.hpp"

#include <bitset>

namespace Star {

HashMap<Key, KeyMod> const KeyChordMods{
  {Key::LShift, KeyMod::LShift},
  {Key::RShift, KeyMod::RShift},
  {Key::LCtrl, KeyMod::LCtrl},
  {Key::RCtrl, KeyMod::RCtrl},
  {Key::LAlt, KeyMod::LAlt},
  {Key::RAlt, KeyMod::RAlt},
  {Key::LGui, KeyMod::LGui},
  {Key::RGui, KeyMod::RGui},
  {Key::AltGr, KeyMod::AltGr}
};

EnumMap<InterfaceAction> const InterfaceActionNames{
    {InterfaceAction::None, "None"},
    {InterfaceAction::PlayerUp, "PlayerUp"},
    {InterfaceAction::PlayerDown, "PlayerDown"},
    {InterfaceAction::PlayerLeft, "PlayerLeft"},
    {InterfaceAction::PlayerRight, "PlayerRight"},
    {InterfaceAction::PlayerJump, "PlayerJump"},
    {InterfaceAction::PlayerMainItem, "PlayerMainItem"},
    {InterfaceAction::PlayerAltItem, "PlayerAltItem"},
    {InterfaceAction::PlayerDropItem, "PlayerDropItem"},
    {InterfaceAction::PlayerInteract, "PlayerInteract"},
    {InterfaceAction::PlayerShifting, "PlayerShifting"},
    {InterfaceAction::PlayerTechAction1, "PlayerTechAction1"},
    {InterfaceAction::PlayerTechAction2, "PlayerTechAction2"},
    {InterfaceAction::PlayerTechAction3, "PlayerTechAction3"},
    {InterfaceAction::EmoteBlabbering, "EmoteBlabbering"},
    {InterfaceAction::EmoteShouting, "EmoteShouting"},
    {InterfaceAction::EmoteHappy, "EmoteHappy"},
    {InterfaceAction::EmoteSad, "EmoteSad"},
    {InterfaceAction::EmoteNeutral, "EmoteNeutral"},
    {InterfaceAction::EmoteLaugh, "EmoteLaugh"},
    {InterfaceAction::EmoteAnnoyed, "EmoteAnnoyed"},
    {InterfaceAction::EmoteOh, "EmoteOh"},
    {InterfaceAction::EmoteOooh, "EmoteOooh"},
    {InterfaceAction::EmoteBlink, "EmoteBlink"},
    {InterfaceAction::EmoteWink, "EmoteWink"},
    {InterfaceAction::EmoteEat, "EmoteEat"},
    {InterfaceAction::EmoteSleep, "EmoteSleep"},
    {InterfaceAction::ShowLabels, "ShowLabels"},
    {InterfaceAction::CameraShift, "CameraShift"},
    {InterfaceAction::TitleBack, "TitleBack"},
    {InterfaceAction::CinematicSkip, "CinematicSkip"},
    {InterfaceAction::CinematicNext, "CinematicNext"},
    {InterfaceAction::GuiClose, "GuiClose"},
    {InterfaceAction::GuiShifting, "GuiShifting"},
    {InterfaceAction::KeybindingClear, "KeybindingClear"},
    {InterfaceAction::KeybindingCancel, "KeybindingCancel"},
    {InterfaceAction::ChatPageUp, "ChatPageUp"},
    {InterfaceAction::ChatPageDown, "ChatPageDown"},
    {InterfaceAction::ChatPreviousLine, "ChatPreviousLine"},
    {InterfaceAction::ChatNextLine, "ChatNextLine"},
    {InterfaceAction::ChatSendLine, "ChatSendLine"},
    {InterfaceAction::ChatBegin, "ChatBegin"},
    {InterfaceAction::ChatBeginCommand, "ChatBeginCommand"},
    {InterfaceAction::ChatStop, "ChatStop"},
    {InterfaceAction::InterfaceShowHelp, "InterfaceShowHelp"},
    {InterfaceAction::InterfaceHideHud, "InterfaceHideHud"},
    {InterfaceAction::InterfaceChangeBarGroup, "InterfaceChangeBarGroup"},
    {InterfaceAction::InterfaceDeselectHands, "InterfaceDeselectHands"},
    {InterfaceAction::InterfaceBar1, "InterfaceBar1"},
    {InterfaceAction::InterfaceBar2, "InterfaceBar2"},
    {InterfaceAction::InterfaceBar3, "InterfaceBar3"},
    {InterfaceAction::InterfaceBar4, "InterfaceBar4"},
    {InterfaceAction::InterfaceBar5, "InterfaceBar5"},
    {InterfaceAction::InterfaceBar6, "InterfaceBar6"},
    {InterfaceAction::InterfaceBar7, "InterfaceBar7"},
    {InterfaceAction::InterfaceBar8, "InterfaceBar8"},
    {InterfaceAction::InterfaceBar9, "InterfaceBar9"},
    {InterfaceAction::InterfaceBar10, "InterfaceBar10"},
    {InterfaceAction::EssentialBar1, "EssentialBar1"},
    {InterfaceAction::EssentialBar2, "EssentialBar2"},
    {InterfaceAction::EssentialBar3, "EssentialBar3"},
    {InterfaceAction::EssentialBar4, "EssentialBar4"},
    {InterfaceAction::InterfaceRepeatCommand, "InterfaceRepeatCommand"},
    {InterfaceAction::InterfaceToggleFullscreen, "InterfaceToggleFullscreen"},
    {InterfaceAction::InterfaceReload, "InterfaceReload"},
    {InterfaceAction::InterfaceEscapeMenu, "InterfaceEscapeMenu"},
    {InterfaceAction::InterfaceInventory, "InterfaceInventory"},
    {InterfaceAction::InterfaceCodex, "InterfaceCodex"},
    {InterfaceAction::InterfaceQuest, "InterfaceQuest"},
    {InterfaceAction::InterfaceCrafting, "InterfaceCrafting"},
};

bool KeyChord::operator<(KeyChord const& rhs) const {
  return tie(key, mods) < tie(rhs.key, rhs.mods);
}

KeyChord inputDescriptorFromJson(Json const& json) {
  Key key;
  auto type = json.getString("type");
  if (type == "key") {
    auto value = json.get("value");
    if (value.isType(Json::Type::String)) {
      key = KeyNames.getLeft(value.toString());
    } else if (value.canConvert(Json::Type::Int)) {
      key = (Key)value.toUInt();
    } else {
      throw StarException::format("Improper key value '{}'", value);
    }
  } else {
    throw StarException::format("Improper bindings type '{}'", type);
  }

  KeyMod mods = KeyMod::NoMod;
  for (auto mod : json.get("mods").iterateArray())
    mods |= KeyModNames.getLeft(mod.toString());

  return {key, mods};
}

Json inputDescriptorToJson(KeyChord const& chord) {
  JsonArray modNames;
  for (auto const& p : KeyModNames) {
    if ((chord.mods & p.first) != KeyMod::NoMod)
      modNames.append(p.second);
  }
  return JsonObject{
    {"type", "key"},
    {"value", KeyNames.getRight(chord.key)},
    {"mods", modNames}
  };
}

String printInputDescriptor(KeyChord chord) {
  StringList modNames;
  for (auto const& p : KeyModNames) {
    if ((chord.mods & p.first) != KeyMod::NoMod)
      modNames.append(p.second);
  }

  return String::joinWith(" + ", modNames.join(" + "), KeyNames.getRight(chord.key));
}

KeyBindings::KeyBindings() {}

KeyBindings::KeyBindings(Json const& json) {
  Map<Key, List<pair<KeyMod, InterfaceAction>>> actions;
  try {
    for (auto const& kvpair : json.iterateObject()) {
      InterfaceAction action = InterfaceActionNames.getLeft(kvpair.first);

      for (auto const& input : kvpair.second.iterateArray()) {
        try {
          auto chord = inputDescriptorFromJson(input);
          actions[chord.key].append({chord.mods, action});
        } catch (StarException const& e) {
          Logger::warn("Could not load keybinding for {}: {}\n",
              InterfaceActionNames.getRight(action),
              outputException(e, false));
        }
      }
    }

    m_actions = std::move(actions);
  } catch (StarException const& e) {
    throw StarException(strf("Could not set keybindings from configuration. {}", outputException(e, false)));
  }
}

Set<InterfaceAction> KeyBindings::actions(Key key) const {
  return actions(KeyChord{key, KeyMod::NoMod});
}

Set<InterfaceAction> KeyBindings::actions(InputEvent const& event) const {
  if (auto keyDown = event.ptr<KeyDownEvent>())
    return actions(KeyChord{keyDown->key, keyDown->mods});
  return {};
}

Set<InterfaceAction> KeyBindings::actions(KeyChord chord) const {
  size_t mostMatchedMods = 0;
  Set<InterfaceAction> matching;
  for (auto const& pair : m_actions.value(chord.key)) {
    // first make sure that all required mods for the binding are held
    if ((pair.first & chord.mods) == pair.first) {
      // now count the number of mods in the binding
      size_t matchedMods = 0;
      for (auto modPair : KeyChordMods) {
        if ((modPair.second & pair.first) == modPair.second)
          ++matchedMods;
      }

      if (matchedMods > mostMatchedMods) {
        matching.clear();
        mostMatchedMods = matchedMods;
      }

      // only activate the binding(s) with the most mods
      if (matchedMods == mostMatchedMods)
        matching.add(pair.second);
    }
  }
  return matching;
}

Set<InterfaceAction> KeyBindings::actionsForKey(Key key) const {
  return Set<InterfaceAction>::from(m_actions.value(key).transformed([](auto p){ return p.second; }));
}

}