Skip to content

Commit

Permalink
more keybinds improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
malytomas committed Jan 27, 2025
1 parent ac259cb commit 51b0d01
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 34 deletions.
4 changes: 2 additions & 2 deletions sources/include/cage-core/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace cage
}
}

CAGE_FORCE_INLINE void clear() { del.b.clear(); }
CAGE_FORCE_INLINE void unbind() { del.b.clear(); }

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

Expand All @@ -87,7 +87,7 @@ namespace cage
{
Delegate<bool(Ts...)> b;
Delegate<void(Ts...)> v;
CAGE_FORCE_INLINE Del() : b(){};
CAGE_FORCE_INLINE Del() : b() {};
} del;
bool vd = false;

Expand Down
2 changes: 1 addition & 1 deletion sources/include/cage-engine/keybinds.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace cage
sint32 displayOrder = 0;
ModifiersFlags requiredFlags = ModifiersFlags::None;
ModifiersFlags forbiddenFlags = ModifiersFlags::Super;
KeybindDevicesFlags devices = KeybindDevicesFlags::Keyboard | KeybindDevicesFlags::Mouse | KeybindDevicesFlags::Wheel;
KeybindDevicesFlags devices = KeybindDevicesFlags::Keyboard | KeybindDevicesFlags::Mouse;
KeybindModesFlags modes = KeybindModesFlags::Press;
};

Expand Down
54 changes: 34 additions & 20 deletions sources/libengine/keybinds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ namespace cage
{
namespace
{
std::vector<class KeybindImpl *> global;
std::vector<class KeybindImpl *> &global()
{
static std::vector<class KeybindImpl *> g;
return g;
}

CAGE_FORCE_INLINE String finishName(String s)
{
Expand All @@ -40,7 +44,7 @@ namespace cage

CAGE_FORCE_INLINE KeybindModesFlags matchImpl(input::privat::BaseKey k, KeybindModesFlags activation) const
{
if (k.key == key && checkFlags(k.mods))
if (k.key == key && (checkFlags(k.mods) || activation == KeybindModesFlags::Release))
return activation;
return KeybindModesFlags::None;
}
Expand All @@ -64,7 +68,7 @@ namespace cage

CAGE_FORCE_INLINE KeybindModesFlags matchImpl(input::privat::BaseMouse k, KeybindModesFlags activation) const
{
if (any(k.buttons & button) && checkFlags(k.mods))
if (any(k.buttons & button) && (checkFlags(k.mods) || activation == KeybindModesFlags::Release))
return activation;
return KeybindModesFlags::None;
}
Expand Down Expand Up @@ -94,7 +98,7 @@ namespace cage
return KeybindModesFlags::None;
}

CAGE_FORCE_INLINE String value() const { return finishName(Stringizer() + getModifiersNames(requiredFlags) + " Scroll"); }
CAGE_FORCE_INLINE String value() const { return finishName(Stringizer() + getModifiersNames(requiredFlags) + " WHEEL"); }
};

using Matcher = std::variant<std::monostate, KeyboardMatcher, MouseMatcher, WheelMatcher>;
Expand Down Expand Up @@ -201,14 +205,14 @@ namespace cage
CAGE_ASSERT(any(config.modes));
reset(); // make matchers from the defaults
this->event = event;
global.push_back(this);
global().push_back(this);
}

~KeybindImpl()
{
auto it = std::find(global.begin(), global.end(), this);
if (it != global.end())
global.erase(it);
auto it = std::find(global().begin(), global().end(), this);
if (it != global().end())
global().erase(it);
}

bool process(const GenericInput &input) const
Expand All @@ -234,7 +238,11 @@ namespace cage
if (any(r & config.modes))
{
if (event)
return event(input);
{
const bool p = event(input);
if (none(r & KeybindModesFlags::Release)) // release always propagates
return p;
}
return false;
}
}
Expand Down Expand Up @@ -285,7 +293,7 @@ namespace cage

void cancel()
{
assignmentListener.clear();
assignmentListener.unbind();
assigningIndex = m;
makeGui();
}
Expand Down Expand Up @@ -380,7 +388,7 @@ namespace cage
[](const auto &mt) -> String
{
if constexpr (std::is_same_v<std::decay_t<decltype(mt)>, std::monostate>)
return "-----";
return "";
else
return mt.value();
},
Expand Down Expand Up @@ -459,7 +467,7 @@ namespace cage

Keybind *findKeybind(const String &id)
{
for (KeybindImpl *it : global)
for (KeybindImpl *it : global())
{
if (it->config.id == id)
return it;
Expand All @@ -470,9 +478,9 @@ namespace cage
void keybindsRegisterListeners(EventDispatcher<bool(const GenericInput &)> &dispatcher)
{
assignmentListener.attach(dispatcher, -328655984);
for (KeybindImpl *it : global)
for (KeybindImpl *it : global())
{
it->listener.clear();
it->listener.unbind();
it->listener.bind([it](const GenericInput &in) { return it->process(in); });
it->listener.attach(dispatcher, it->config.eventOrder);
}
Expand All @@ -481,7 +489,7 @@ namespace cage
void keybindsDispatchTick()
{
GenericInput g = input::Tick();
for (KeybindImpl *it : global)
for (KeybindImpl *it : global())
it->process(g);
}

Expand All @@ -495,13 +503,19 @@ namespace cage

void keybindsGuiTable(GuiBuilder *g, const String &filterPrefix)
{
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 }; });
std::vector<KeybindImpl *> tmp;
tmp.reserve(global().size());
for (KeybindImpl *k : global())
{
if (isPattern(k->config.id, filterPrefix, "", ""))
tmp.push_back(k);
}
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 }; });

auto _ = g->verticalTable(2);
for (KeybindImpl *k : global)
for (KeybindImpl *k : tmp)
{
if (!isPattern(k->config.id, filterPrefix, "", ""))
continue;
g->label().text(k->textId, k->config.id);
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);
keybindsGuiWidget(g, k);
}
}
Expand Down
48 changes: 38 additions & 10 deletions sources/libengine/window/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,18 +820,20 @@ namespace cage
{
switch (key)
{
case GLFW_KEY_SPACE:
return "_____";
case GLFW_KEY_ESCAPE:
return "ESC";
case GLFW_KEY_ENTER:
return "ENTER";
case GLFW_KEY_TAB:
return "TAB";
case GLFW_KEY_BACKSPACE:
return "BACKSPACE";
return "BACK";
case GLFW_KEY_INSERT:
return "INSERT";
return "INS";
case GLFW_KEY_DELETE:
return "DELETE";
return "DEL";
case GLFW_KEY_RIGHT:
return "RIGHT";
case GLFW_KEY_LEFT:
Expand All @@ -841,21 +843,21 @@ namespace cage
case GLFW_KEY_UP:
return "UP";
case GLFW_KEY_PAGE_UP:
return "PAGE_UP";
return "PGUP";
case GLFW_KEY_PAGE_DOWN:
return "PAGE_DOWN";
return "PGDN";
case GLFW_KEY_HOME:
return "HOME";
case GLFW_KEY_END:
return "END";
case GLFW_KEY_CAPS_LOCK:
return "CAPS_LOCK";
return "CAPS";
case GLFW_KEY_SCROLL_LOCK:
return "SCROLL_LOCK";
return "SCROLL";
case GLFW_KEY_NUM_LOCK:
return "NUM_LOCK";
return "NUM";
case GLFW_KEY_PRINT_SCREEN:
return "PRINT_SCREEN";
return "PRTSC";
case GLFW_KEY_PAUSE:
return "PAUSE";
case GLFW_KEY_F1:
Expand All @@ -882,8 +884,34 @@ namespace cage
return "F11";
case GLFW_KEY_F12:
return "F12";
case GLFW_KEY_F13:
return "F13";
case GLFW_KEY_F14:
return "F14";
case GLFW_KEY_F15:
return "F15";
case GLFW_KEY_F16:
return "F16";
case GLFW_KEY_F17:
return "F17";
case GLFW_KEY_F18:
return "F18";
case GLFW_KEY_F19:
return "F19";
case GLFW_KEY_F20:
return "F20";
case GLFW_KEY_F21:
return "F21";
case GLFW_KEY_F22:
return "F22";
case GLFW_KEY_F23:
return "F23";
case GLFW_KEY_F24:
return "F24";
case GLFW_KEY_F25:
return "F25";
case GLFW_KEY_KP_ENTER:
return "ENTER2";
return "ENT";
}
const auto s = glfwGetKeyName(key, 0);
return s ? toUpper(detail::StringBase<27>(s)) : "???";
Expand Down
2 changes: 1 addition & 1 deletion sources/test-core/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void testEvents()
CAGE_TEST(n == 1);
l1.detach();
l1.attach(d);
l1.clear();
l1.unbind();
}

{
Expand Down

0 comments on commit 51b0d01

Please sign in to comment.