Skip to content

Commit 51b0d01

Browse files
committed
more keybinds improvements
1 parent ac259cb commit 51b0d01

File tree

5 files changed

+76
-34
lines changed

5 files changed

+76
-34
lines changed

sources/include/cage-core/events.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace cage
6363
}
6464
}
6565

66-
CAGE_FORCE_INLINE void clear() { del.b.clear(); }
66+
CAGE_FORCE_INLINE void unbind() { del.b.clear(); }
6767

6868
CAGE_FORCE_INLINE void attach(EventDispatcher<bool(Ts...)> &dispatcher, sint32 order = 0) { privat::EventLinker::attach(&dispatcher, order); }
6969

@@ -87,7 +87,7 @@ namespace cage
8787
{
8888
Delegate<bool(Ts...)> b;
8989
Delegate<void(Ts...)> v;
90-
CAGE_FORCE_INLINE Del() : b(){};
90+
CAGE_FORCE_INLINE Del() : b() {};
9191
} del;
9292
bool vd = false;
9393

sources/include/cage-engine/keybinds.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ namespace cage
5959
sint32 displayOrder = 0;
6060
ModifiersFlags requiredFlags = ModifiersFlags::None;
6161
ModifiersFlags forbiddenFlags = ModifiersFlags::Super;
62-
KeybindDevicesFlags devices = KeybindDevicesFlags::Keyboard | KeybindDevicesFlags::Mouse | KeybindDevicesFlags::Wheel;
62+
KeybindDevicesFlags devices = KeybindDevicesFlags::Keyboard | KeybindDevicesFlags::Mouse;
6363
KeybindModesFlags modes = KeybindModesFlags::Press;
6464
};
6565

sources/libengine/keybinds.cpp

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ namespace cage
1414
{
1515
namespace
1616
{
17-
std::vector<class KeybindImpl *> global;
17+
std::vector<class KeybindImpl *> &global()
18+
{
19+
static std::vector<class KeybindImpl *> g;
20+
return g;
21+
}
1822

1923
CAGE_FORCE_INLINE String finishName(String s)
2024
{
@@ -40,7 +44,7 @@ namespace cage
4044

4145
CAGE_FORCE_INLINE KeybindModesFlags matchImpl(input::privat::BaseKey k, KeybindModesFlags activation) const
4246
{
43-
if (k.key == key && checkFlags(k.mods))
47+
if (k.key == key && (checkFlags(k.mods) || activation == KeybindModesFlags::Release))
4448
return activation;
4549
return KeybindModesFlags::None;
4650
}
@@ -64,7 +68,7 @@ namespace cage
6468

6569
CAGE_FORCE_INLINE KeybindModesFlags matchImpl(input::privat::BaseMouse k, KeybindModesFlags activation) const
6670
{
67-
if (any(k.buttons & button) && checkFlags(k.mods))
71+
if (any(k.buttons & button) && (checkFlags(k.mods) || activation == KeybindModesFlags::Release))
6872
return activation;
6973
return KeybindModesFlags::None;
7074
}
@@ -94,7 +98,7 @@ namespace cage
9498
return KeybindModesFlags::None;
9599
}
96100

97-
CAGE_FORCE_INLINE String value() const { return finishName(Stringizer() + getModifiersNames(requiredFlags) + " Scroll"); }
101+
CAGE_FORCE_INLINE String value() const { return finishName(Stringizer() + getModifiersNames(requiredFlags) + " WHEEL"); }
98102
};
99103

100104
using Matcher = std::variant<std::monostate, KeyboardMatcher, MouseMatcher, WheelMatcher>;
@@ -201,14 +205,14 @@ namespace cage
201205
CAGE_ASSERT(any(config.modes));
202206
reset(); // make matchers from the defaults
203207
this->event = event;
204-
global.push_back(this);
208+
global().push_back(this);
205209
}
206210

207211
~KeybindImpl()
208212
{
209-
auto it = std::find(global.begin(), global.end(), this);
210-
if (it != global.end())
211-
global.erase(it);
213+
auto it = std::find(global().begin(), global().end(), this);
214+
if (it != global().end())
215+
global().erase(it);
212216
}
213217

214218
bool process(const GenericInput &input) const
@@ -234,7 +238,11 @@ namespace cage
234238
if (any(r & config.modes))
235239
{
236240
if (event)
237-
return event(input);
241+
{
242+
const bool p = event(input);
243+
if (none(r & KeybindModesFlags::Release)) // release always propagates
244+
return p;
245+
}
238246
return false;
239247
}
240248
}
@@ -285,7 +293,7 @@ namespace cage
285293

286294
void cancel()
287295
{
288-
assignmentListener.clear();
296+
assignmentListener.unbind();
289297
assigningIndex = m;
290298
makeGui();
291299
}
@@ -380,7 +388,7 @@ namespace cage
380388
[](const auto &mt) -> String
381389
{
382390
if constexpr (std::is_same_v<std::decay_t<decltype(mt)>, std::monostate>)
383-
return "-----";
391+
return "";
384392
else
385393
return mt.value();
386394
},
@@ -459,7 +467,7 @@ namespace cage
459467

460468
Keybind *findKeybind(const String &id)
461469
{
462-
for (KeybindImpl *it : global)
470+
for (KeybindImpl *it : global())
463471
{
464472
if (it->config.id == id)
465473
return it;
@@ -470,9 +478,9 @@ namespace cage
470478
void keybindsRegisterListeners(EventDispatcher<bool(const GenericInput &)> &dispatcher)
471479
{
472480
assignmentListener.attach(dispatcher, -328655984);
473-
for (KeybindImpl *it : global)
481+
for (KeybindImpl *it : global())
474482
{
475-
it->listener.clear();
483+
it->listener.unbind();
476484
it->listener.bind([it](const GenericInput &in) { return it->process(in); });
477485
it->listener.attach(dispatcher, it->config.eventOrder);
478486
}
@@ -481,7 +489,7 @@ namespace cage
481489
void keybindsDispatchTick()
482490
{
483491
GenericInput g = input::Tick();
484-
for (KeybindImpl *it : global)
492+
for (KeybindImpl *it : global())
485493
it->process(g);
486494
}
487495

@@ -495,13 +503,19 @@ namespace cage
495503

496504
void keybindsGuiTable(GuiBuilder *g, const String &filterPrefix)
497505
{
498-
std::sort(global.begin(), global.end(), [](const KeybindImpl *a, const KeybindImpl *b) -> bool { return std::pair{ a->config.displayOrder, a->config.id } < std::pair{ b->config.displayOrder, b->config.id }; });
506+
std::vector<KeybindImpl *> tmp;
507+
tmp.reserve(global().size());
508+
for (KeybindImpl *k : global())
509+
{
510+
if (isPattern(k->config.id, filterPrefix, "", ""))
511+
tmp.push_back(k);
512+
}
513+
std::sort(tmp.begin(), tmp.end(), [](const KeybindImpl *a, const KeybindImpl *b) -> bool { return std::tuple{ a->config.displayOrder, textsGet(a->textId), a->config.id } < std::tuple{ b->config.displayOrder, textsGet(b->textId), b->config.id }; });
514+
499515
auto _ = g->verticalTable(2);
500-
for (KeybindImpl *k : global)
516+
for (KeybindImpl *k : tmp)
501517
{
502-
if (!isPattern(k->config.id, filterPrefix, "", ""))
503-
continue;
504-
g->label().text(k->textId, k->config.id);
518+
g->label().update([k](Entity *e) { e->value<GuiTextFormatComponent>().color = k->active ? Vec3(0.5, 0.9, 1) : Vec3::Nan(); }).text(k->textId, k->config.id);
505519
keybindsGuiWidget(g, k);
506520
}
507521
}

sources/libengine/window/window.cpp

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -820,18 +820,20 @@ namespace cage
820820
{
821821
switch (key)
822822
{
823+
case GLFW_KEY_SPACE:
824+
return "_____";
823825
case GLFW_KEY_ESCAPE:
824826
return "ESC";
825827
case GLFW_KEY_ENTER:
826828
return "ENTER";
827829
case GLFW_KEY_TAB:
828830
return "TAB";
829831
case GLFW_KEY_BACKSPACE:
830-
return "BACKSPACE";
832+
return "BACK";
831833
case GLFW_KEY_INSERT:
832-
return "INSERT";
834+
return "INS";
833835
case GLFW_KEY_DELETE:
834-
return "DELETE";
836+
return "DEL";
835837
case GLFW_KEY_RIGHT:
836838
return "RIGHT";
837839
case GLFW_KEY_LEFT:
@@ -841,21 +843,21 @@ namespace cage
841843
case GLFW_KEY_UP:
842844
return "UP";
843845
case GLFW_KEY_PAGE_UP:
844-
return "PAGE_UP";
846+
return "PGUP";
845847
case GLFW_KEY_PAGE_DOWN:
846-
return "PAGE_DOWN";
848+
return "PGDN";
847849
case GLFW_KEY_HOME:
848850
return "HOME";
849851
case GLFW_KEY_END:
850852
return "END";
851853
case GLFW_KEY_CAPS_LOCK:
852-
return "CAPS_LOCK";
854+
return "CAPS";
853855
case GLFW_KEY_SCROLL_LOCK:
854-
return "SCROLL_LOCK";
856+
return "SCROLL";
855857
case GLFW_KEY_NUM_LOCK:
856-
return "NUM_LOCK";
858+
return "NUM";
857859
case GLFW_KEY_PRINT_SCREEN:
858-
return "PRINT_SCREEN";
860+
return "PRTSC";
859861
case GLFW_KEY_PAUSE:
860862
return "PAUSE";
861863
case GLFW_KEY_F1:
@@ -882,8 +884,34 @@ namespace cage
882884
return "F11";
883885
case GLFW_KEY_F12:
884886
return "F12";
887+
case GLFW_KEY_F13:
888+
return "F13";
889+
case GLFW_KEY_F14:
890+
return "F14";
891+
case GLFW_KEY_F15:
892+
return "F15";
893+
case GLFW_KEY_F16:
894+
return "F16";
895+
case GLFW_KEY_F17:
896+
return "F17";
897+
case GLFW_KEY_F18:
898+
return "F18";
899+
case GLFW_KEY_F19:
900+
return "F19";
901+
case GLFW_KEY_F20:
902+
return "F20";
903+
case GLFW_KEY_F21:
904+
return "F21";
905+
case GLFW_KEY_F22:
906+
return "F22";
907+
case GLFW_KEY_F23:
908+
return "F23";
909+
case GLFW_KEY_F24:
910+
return "F24";
911+
case GLFW_KEY_F25:
912+
return "F25";
885913
case GLFW_KEY_KP_ENTER:
886-
return "ENTER2";
914+
return "ENT";
887915
}
888916
const auto s = glfwGetKeyName(key, 0);
889917
return s ? toUpper(detail::StringBase<27>(s)) : "???";

sources/test-core/events.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void testEvents()
5353
CAGE_TEST(n == 1);
5454
l1.detach();
5555
l1.attach(d);
56-
l1.clear();
56+
l1.unbind();
5757
}
5858

5959
{

0 commit comments

Comments
 (0)