rename keycode, mousecode enumerations

This commit is contained in:
MihailRis 2025-04-02 17:39:31 +03:00
parent 9843a1fc27
commit 881edb7b28
22 changed files with 160 additions and 154 deletions

View File

@ -42,13 +42,13 @@ public:
: Setting(format), initial(value), value(value) { : Setting(format), initial(value), value(value) {
} }
observer_handler observe(consumer<T> callback, bool callOnStart = false) { ObserverHandler observe(consumer<T> callback, bool callOnStart = false) {
const int id = nextid++; const int id = nextid++;
observers.emplace(id, callback); observers.emplace(id, callback);
if (callOnStart) { if (callOnStart) {
callback(value); callback(value);
} }
return observer_handler([this, id]() { return ObserverHandler([this, id]() {
observers.erase(id); observers.erase(id);
}); });
} }

View File

@ -174,13 +174,13 @@ void Engine::loadControls() {
} }
void Engine::updateHotkeys() { void Engine::updateHotkeys() {
if (input->jpressed(keycode::F2)) { if (input->jpressed(Keycode::F2)) {
saveScreenshot(); saveScreenshot();
} }
if (input->jpressed(keycode::F8)) { if (input->jpressed(Keycode::F8)) {
gui->toggleDebug(); gui->toggleDebug();
} }
if (input->jpressed(keycode::F11)) { if (input->jpressed(Keycode::F11)) {
settings.display.fullscreen.toggle(); settings.display.fullscreen.toggle();
} }
} }

View File

@ -252,15 +252,15 @@ void Hud::updateHotbarControl() {
player.setChosenSlot(slot); player.setChosenSlot(slot);
} }
for ( for (
int i = static_cast<int>(keycode::NUM_1); int i = static_cast<int>(Keycode::NUM_1);
i <= static_cast<int>(keycode::NUM_9); i <= static_cast<int>(Keycode::NUM_9);
i++ i++
) { ) {
if (input.jpressed(static_cast<keycode>(i))) { if (input.jpressed(static_cast<Keycode>(i))) {
player.setChosenSlot(i - static_cast<int>(keycode::NUM_1)); player.setChosenSlot(i - static_cast<int>(Keycode::NUM_1));
} }
} }
if (input.jpressed(keycode::NUM_0)) { if (input.jpressed(Keycode::NUM_0)) {
player.setChosenSlot(9); player.setChosenSlot(9);
} }
} }

View File

@ -180,13 +180,13 @@ void LevelScreen::saveWorldPreview() {
void LevelScreen::updateHotkeys() { void LevelScreen::updateHotkeys() {
auto& settings = engine.getSettings(); auto& settings = engine.getSettings();
if (input.jpressed(keycode::O)) { if (input.jpressed(Keycode::O)) {
settings.graphics.frustumCulling.toggle(); settings.graphics.frustumCulling.toggle();
} }
if (input.jpressed(keycode::F1)) { if (input.jpressed(Keycode::F1)) {
hudVisible = !hudVisible; hudVisible = !hudVisible;
} }
if (input.jpressed(keycode::F3)) { if (input.jpressed(Keycode::F3)) {
debug = !debug; debug = !debug;
hud->setDebug(debug); hud->setDebug(debug);
renderer->setDebug(debug); renderer->setDebug(debug);

View File

@ -135,7 +135,7 @@ void GUI::actMouse(float delta, const CursorState& cursor) {
} }
this->hover = hover; this->hover = hover;
if (input.jclicked(mousecode::BUTTON_1)) { if (input.jclicked(Mousecode::BUTTON_1)) {
if (pressed == nullptr && this->hover) { if (pressed == nullptr && this->hover) {
pressed = hover; pressed = hover;
if (doubleClickTimer < doubleClickDelay) { if (doubleClickTimer < doubleClickDelay) {
@ -158,13 +158,13 @@ void GUI::actMouse(float delta, const CursorState& cursor) {
focus->defocus(); focus->defocus();
focus = nullptr; focus = nullptr;
} }
} else if (!input.clicked(mousecode::BUTTON_1) && pressed) { } else if (!input.clicked(Mousecode::BUTTON_1) && pressed) {
pressed->mouseRelease(cursor.pos.x, cursor.pos.y); pressed->mouseRelease(cursor.pos.x, cursor.pos.y);
pressed = nullptr; pressed = nullptr;
} }
if (hover) { if (hover) {
for (mousecode code : MOUSECODES_ALL) { for (Mousecode code : MOUSECODES_ALL) {
if (input.jclicked(code)) { if (input.jclicked(code)) {
hover->clicked(code); hover->clicked(code);
} }
@ -173,7 +173,7 @@ void GUI::actMouse(float delta, const CursorState& cursor) {
} }
void GUI::actFocused() { void GUI::actFocused() {
if (input.jpressed(keycode::ESCAPE)) { if (input.jpressed(Keycode::ESCAPE)) {
focus->defocus(); focus->defocus();
focus = nullptr; focus = nullptr;
return; return;
@ -187,8 +187,8 @@ void GUI::actFocused() {
const auto& cursor = input.getCursor(); const auto& cursor = input.getCursor();
if (!cursor.locked) { if (!cursor.locked) {
if (input.clicked(mousecode::BUTTON_1) && if (input.clicked(Mousecode::BUTTON_1) &&
(input.jclicked(mousecode::BUTTON_1) || cursor.delta.x || (input.jclicked(Mousecode::BUTTON_1) || cursor.delta.x ||
cursor.delta.y)) { cursor.delta.y)) {
if (!doubleClicked) { if (!doubleClicked) {
focus->mouseMove(cursor.pos.x, cursor.pos.y); focus->mouseMove(cursor.pos.x, cursor.pos.y);

View File

@ -25,15 +25,15 @@ void InputBindBox::drawBackground(const DrawContext& pctx, const Assets&) {
label->setText(util::str2wstr_utf8(binding.text())); label->setText(util::str2wstr_utf8(binding.text()));
} }
void InputBindBox::clicked(mousecode button) { void InputBindBox::clicked(Mousecode button) {
if (isFocused()) { if (isFocused()) {
binding.reset(button); binding.reset(button);
defocus(); defocus();
} }
} }
void InputBindBox::keyPressed(keycode key) { void InputBindBox::keyPressed(Keycode key) {
if (key != keycode::ESCAPE) { if (key != Keycode::ESCAPE) {
binding.reset(key); binding.reset(key);
} }
defocus(); defocus();

View File

@ -19,8 +19,8 @@ namespace gui {
const DrawContext& pctx, const Assets& assets const DrawContext& pctx, const Assets& assets
) override; ) override;
virtual void clicked(mousecode button) override; virtual void clicked(Mousecode button) override;
virtual void keyPressed(keycode key) override; virtual void keyPressed(Keycode key) override;
virtual bool isFocuskeeper() const override { virtual bool isFocuskeeper() const override {
return true; return true;
} }

View File

@ -281,7 +281,7 @@ bool SlotView::isHighlighted() const {
void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) { void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) {
const auto& input = gui.getInput(); const auto& input = gui.getInput();
if (layout.taking && input.pressed(keycode::LEFT_SHIFT)) { if (layout.taking && input.pressed(Keycode::LEFT_SHIFT)) {
if (layout.shareFunc) { if (layout.shareFunc) {
layout.shareFunc(layout.index, stack); layout.shareFunc(layout.index, stack);
} }
@ -348,7 +348,7 @@ void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) {
} }
} }
void SlotView::clicked(mousecode button) { void SlotView::clicked(Mousecode button) {
if (bound == nullptr) if (bound == nullptr)
return; return;
auto exchangeSlot = auto exchangeSlot =
@ -359,9 +359,9 @@ void SlotView::clicked(mousecode button) {
ItemStack& grabbed = exchangeSlot->getStack(); ItemStack& grabbed = exchangeSlot->getStack();
ItemStack& stack = *bound; ItemStack& stack = *bound;
if (button == mousecode::BUTTON_1) { if (button == Mousecode::BUTTON_1) {
performLeftClick(stack, grabbed); performLeftClick(stack, grabbed);
} else if (button == mousecode::BUTTON_2) { } else if (button == Mousecode::BUTTON_2) {
performRightClick(stack, grabbed); performRightClick(stack, grabbed);
} }
if (layout.updateFunc) { if (layout.updateFunc) {
@ -370,7 +370,7 @@ void SlotView::clicked(mousecode button) {
} }
void SlotView::onFocus() { void SlotView::onFocus() {
clicked(mousecode::BUTTON_1); clicked(Mousecode::BUTTON_1);
} }
const std::wstring& SlotView::getTooltip() const { const std::wstring& SlotView::getTooltip() const {

View File

@ -93,7 +93,7 @@ namespace gui {
void setHighlighted(bool flag); void setHighlighted(bool flag);
bool isHighlighted() const; bool isHighlighted() const;
virtual void clicked(mousecode) override; virtual void clicked(Mousecode) override;
virtual void onFocus() override; virtual void onFocus() override;
virtual const std::wstring& getTooltip() const override; virtual const std::wstring& getTooltip() const override;

View File

@ -824,10 +824,10 @@ void TextBox::onInput() {
refreshSyntax(); refreshSyntax();
} }
void TextBox::performEditingKeyboardEvents(keycode key) { void TextBox::performEditingKeyboardEvents(Keycode key) {
bool shiftPressed = gui.getInput().pressed(keycode::LEFT_SHIFT); bool shiftPressed = gui.getInput().pressed(Keycode::LEFT_SHIFT);
bool breakSelection = getSelectionLength() != 0 && !shiftPressed; bool breakSelection = getSelectionLength() != 0 && !shiftPressed;
if (key == keycode::BACKSPACE) { if (key == Keycode::BACKSPACE) {
if (!eraseSelected() && caret > 0 && input.length() > 0) { if (!eraseSelected() && caret > 0 && input.length() > 0) {
if (caret > input.length()) { if (caret > input.length()) {
caret = input.length(); caret = input.length();
@ -839,7 +839,7 @@ void TextBox::performEditingKeyboardEvents(keycode key) {
onInput(); onInput();
} }
} }
} else if (key == keycode::DELETE) { } else if (key == Keycode::DELETE) {
if (!eraseSelected() && caret < input.length()) { if (!eraseSelected() && caret < input.length()) {
historian->onErase(caret, input.substr(caret, 1)); historian->onErase(caret, input.substr(caret, 1));
input = input.substr(0, caret) + input.substr(caret + 1); input = input.substr(0, caret) + input.substr(caret + 1);
@ -847,7 +847,7 @@ void TextBox::performEditingKeyboardEvents(keycode key) {
onInput(); onInput();
} }
} }
} else if (key == keycode::ENTER) { } else if (key == Keycode::ENTER) {
if (multiline) { if (multiline) {
paste(L"\n"); paste(L"\n");
} else { } else {
@ -856,42 +856,42 @@ void TextBox::performEditingKeyboardEvents(keycode key) {
consumer(getText()); consumer(getText());
} }
} }
} else if (key == keycode::TAB) { } else if (key == Keycode::TAB) {
paste(L" "); paste(L" ");
} else if (key == keycode::LEFT) { } else if (key == Keycode::LEFT) {
stepLeft(shiftPressed, breakSelection); stepLeft(shiftPressed, breakSelection);
} else if (key == keycode::RIGHT) { } else if (key == Keycode::RIGHT) {
stepRight(shiftPressed, breakSelection); stepRight(shiftPressed, breakSelection);
} else if (key == keycode::UP && onUpPressed) { } else if (key == Keycode::UP && onUpPressed) {
onUpPressed(); onUpPressed();
} else if (key == keycode::DOWN && onDownPressed) { } else if (key == Keycode::DOWN && onDownPressed) {
onDownPressed(); onDownPressed();
} }
} }
void TextBox::keyPressed(keycode key) { void TextBox::keyPressed(Keycode key) {
const auto& inputEvents = gui.getInput(); const auto& inputEvents = gui.getInput();
if (editable) { if (editable) {
performEditingKeyboardEvents(key); performEditingKeyboardEvents(key);
} }
if (inputEvents.pressed(keycode::LEFT_CONTROL) && key != keycode::LEFT_CONTROL) { if (inputEvents.pressed(Keycode::LEFT_CONTROL) && key != Keycode::LEFT_CONTROL) {
if (controlCombinationsHandler) { if (controlCombinationsHandler) {
if (controlCombinationsHandler(static_cast<int>(key))) { if (controlCombinationsHandler(static_cast<int>(key))) {
return; return;
} }
} }
// Copy selected text to clipboard // Copy selected text to clipboard
if (key == keycode::C || key == keycode::X) { if (key == Keycode::C || key == Keycode::X) {
std::string text = util::wstr2str_utf8(getSelection()); std::string text = util::wstr2str_utf8(getSelection());
if (!text.empty()) { if (!text.empty()) {
gui.getInput().setClipboardText(text.c_str()); gui.getInput().setClipboardText(text.c_str());
} }
if (editable && key == keycode::X) { if (editable && key == Keycode::X) {
eraseSelected(); eraseSelected();
} }
} }
// Paste text from clipboard // Paste text from clipboard
if (key == keycode::V && editable) { if (key == Keycode::V && editable) {
const char* text = inputEvents.getClipboardText(); const char* text = inputEvents.getClipboardText();
if (text) { if (text) {
historian->sync(); // flush buffer before combination historian->sync(); // flush buffer before combination
@ -902,18 +902,18 @@ void TextBox::keyPressed(keycode key) {
} }
} }
// Select/deselect all // Select/deselect all
if (key == keycode::A) { if (key == Keycode::A) {
if (selectionStart == selectionEnd) { if (selectionStart == selectionEnd) {
select(0, input.length()); select(0, input.length());
} else { } else {
resetSelection(); resetSelection();
} }
} }
if (key == keycode::Z) { if (key == Keycode::Z) {
historian->undo(); historian->undo();
refreshSyntax(); refreshSyntax();
} }
if (key == keycode::Y) { if (key == Keycode::Y) {
historian->redo(); historian->redo();
refreshSyntax(); refreshSyntax();
} }
@ -948,11 +948,11 @@ std::shared_ptr<UINode> TextBox::getAt(const glm::vec2& pos) {
void TextBox::setOnUpPressed(const runnable& callback) { void TextBox::setOnUpPressed(const runnable& callback) {
if (callback == nullptr) { if (callback == nullptr) {
onUpPressed = [this]() { onUpPressed = [this]() {
if (inputEvents.pressed(keycode::LEFT_CONTROL)) { if (inputEvents.pressed(Keycode::LEFT_CONTROL)) {
scrolled(1); scrolled(1);
return; return;
} }
bool shiftPressed = inputEvents.pressed(keycode::LEFT_SHIFT); bool shiftPressed = inputEvents.pressed(Keycode::LEFT_SHIFT);
bool breakSelection = getSelectionLength() != 0 && !shiftPressed; bool breakSelection = getSelectionLength() != 0 && !shiftPressed;
stepDefaultUp(shiftPressed, breakSelection); stepDefaultUp(shiftPressed, breakSelection);
}; };
@ -964,11 +964,11 @@ void TextBox::setOnUpPressed(const runnable& callback) {
void TextBox::setOnDownPressed(const runnable& callback) { void TextBox::setOnDownPressed(const runnable& callback) {
if (callback == nullptr) { if (callback == nullptr) {
onDownPressed = [this]() { onDownPressed = [this]() {
if (inputEvents.pressed(keycode::LEFT_CONTROL)) { if (inputEvents.pressed(Keycode::LEFT_CONTROL)) {
scrolled(-1); scrolled(-1);
return; return;
} }
bool shiftPressed = inputEvents.pressed(keycode::LEFT_SHIFT); bool shiftPressed = inputEvents.pressed(Keycode::LEFT_SHIFT);
bool breakSelection = getSelectionLength() != 0 && !shiftPressed; bool breakSelection = getSelectionLength() != 0 && !shiftPressed;
stepDefaultDown(shiftPressed, breakSelection); stepDefaultDown(shiftPressed, breakSelection);
}; };

View File

@ -87,7 +87,7 @@ namespace gui {
/// @brief Set maxLocalCaret to local (line) caret position /// @brief Set maxLocalCaret to local (line) caret position
void resetMaxLocalCaret(); void resetMaxLocalCaret();
void performEditingKeyboardEvents(keycode key); void performEditingKeyboardEvents(Keycode key);
void refreshLabel(); void refreshLabel();
@ -238,7 +238,7 @@ namespace gui {
virtual void draw(const DrawContext& pctx, const Assets& assets) override; virtual void draw(const DrawContext& pctx, const Assets& assets) override;
virtual void drawBackground(const DrawContext& pctx, const Assets& assets) override; virtual void drawBackground(const DrawContext& pctx, const Assets& assets) override;
virtual void typed(unsigned int codepoint) override; virtual void typed(unsigned int codepoint) override;
virtual void keyPressed(keycode key) override; virtual void keyPressed(Keycode key) override;
virtual std::shared_ptr<UINode> getAt(const glm::vec2& pos) override; virtual std::shared_ptr<UINode> getAt(const glm::vec2& pos) override;
virtual void setOnUpPressed(const runnable &callback); virtual void setOnUpPressed(const runnable &callback);
virtual void setOnDownPressed(const runnable &callback); virtual void setOnDownPressed(const runnable &callback);

View File

@ -175,7 +175,7 @@ namespace gui {
virtual void onFocus() {focused = true;} virtual void onFocus() {focused = true;}
virtual void doubleClick(int x, int y); virtual void doubleClick(int x, int y);
virtual void click(int x, int y); virtual void click(int x, int y);
virtual void clicked(mousecode button) {} virtual void clicked(Mousecode button) {}
virtual void mouseMove(int x, int y) {}; virtual void mouseMove(int x, int y) {};
virtual void mouseRelease(int x, int y); virtual void mouseRelease(int x, int y);
virtual void scrolled(int value); virtual void scrolled(int value);
@ -188,7 +188,7 @@ namespace gui {
virtual bool isFocuskeeper() const {return false;} virtual bool isFocuskeeper() const {return false;}
virtual void typed(unsigned int codepoint) {}; virtual void typed(unsigned int codepoint) {};
virtual void keyPressed(keycode key) {}; virtual void keyPressed(Keycode key) {};
/// @brief Check if screen position is inside of the element /// @brief Check if screen position is inside of the element
/// @param pos screen position /// @param pos screen position

View File

@ -68,11 +68,11 @@ void guiutil::alert(
panel->refresh(); panel->refresh();
auto& input = engine.getInput(); auto& input = engine.getInput();
panel->keepAlive(input.addKeyCallback(keycode::ENTER, [on_hidden_final]() { panel->keepAlive(input.addKeyCallback(Keycode::ENTER, [on_hidden_final]() {
on_hidden_final(); on_hidden_final();
return true; return true;
})); }));
panel->keepAlive(input.addKeyCallback(keycode::ESCAPE, [on_hidden_final]() { panel->keepAlive(input.addKeyCallback(Keycode::ESCAPE, [on_hidden_final]() {
on_hidden_final(); on_hidden_final();
return true; return true;
})); }));
@ -138,11 +138,11 @@ void guiutil::confirm(
})); }));
panel->add(subpanel); panel->add(subpanel);
panel->keepAlive(input.addKeyCallback(keycode::ENTER, [=]() { panel->keepAlive(input.addKeyCallback(Keycode::ENTER, [=]() {
on_confirm_final(); on_confirm_final();
return true; return true;
})); }));
panel->keepAlive(input.addKeyCallback(keycode::ESCAPE, [=]() { panel->keepAlive(input.addKeyCallback(Keycode::ESCAPE, [=]() {
on_deny_final(); on_deny_final();
return true; return true;
})); }));

View File

@ -36,7 +36,7 @@ static int l_add_callback(lua::State* L) {
lua::pushvalue(L, 2); lua::pushvalue(L, 2);
auto actual_callback = lua::create_simple_handler(L); auto actual_callback = lua::create_simple_handler(L);
observer_handler handler; ObserverHandler handler;
auto& gui = engine->getGUI(); auto& gui = engine->getGUI();
auto& input = engine->getInput(); auto& input = engine->getInput();

View File

@ -5,7 +5,7 @@
using scriptenv = std::shared_ptr<int>; using scriptenv = std::shared_ptr<int>;
class observer_handler; class ObserverHandler;
/// @brief dynamic integer type (64 bit signed integer) /// @brief dynamic integer type (64 bit signed integer)
using integer_t = int64_t; using integer_t = int64_t;

View File

@ -31,12 +31,12 @@ namespace util {
nextid = o.nextid; nextid = o.nextid;
} }
observer_handler add(std::function<bool(Types...)> handler) { ObserverHandler add(std::function<bool(Types...)> handler) {
std::lock_guard lock(mutex); std::lock_guard lock(mutex);
int id = nextid++; int id = nextid++;
handlers[id] = std::move(handler); handlers[id] = std::move(handler);
order.push_back(id); order.push_back(id);
return observer_handler([this, id]() { return ObserverHandler([this, id]() {
std::lock_guard lock(mutex); std::lock_guard lock(mutex);
handlers.erase(id); handlers.erase(id);
order.erase( order.erase(

View File

@ -9,7 +9,7 @@ namespace util {
/// @brief Keeps shared pointers alive until destruction /// @brief Keeps shared pointers alive until destruction
class ObjectsKeeper { class ObjectsKeeper {
std::vector<std::shared_ptr<void>> ptrs; std::vector<std::shared_ptr<void>> ptrs;
std::vector<observer_handler> handlers; std::vector<ObserverHandler> handlers;
public: public:
ObjectsKeeper() = default; ObjectsKeeper() = default;
@ -29,7 +29,7 @@ namespace util {
ptrs.push_back(std::move(ptr)); ptrs.push_back(std::move(ptr));
} }
virtual void keepAlive(observer_handler&& ptr) { virtual void keepAlive(ObserverHandler&& ptr) {
handlers.emplace_back(std::move(ptr)); handlers.emplace_back(std::move(ptr));
} }

View File

@ -26,11 +26,11 @@ namespace util {
nextid = o.nextid; nextid = o.nextid;
} }
observer_handler add(runnable callback) { ObserverHandler add(runnable callback) {
std::lock_guard lock(mutex); std::lock_guard lock(mutex);
int id = nextid++; int id = nextid++;
runnables[id] = std::move(callback); runnables[id] = std::move(callback);
return observer_handler(new int(id), [this](int* id) { //-V508 return ObserverHandler(new int(id), [this](int* id) { //-V508
std::lock_guard lock(mutex); std::lock_guard lock(mutex);
runnables.erase(*id); runnables.erase(*id);
delete id; delete id;

View File

@ -2,21 +2,21 @@
#include <functional> #include <functional>
class observer_handler { class ObserverHandler {
public: public:
observer_handler() = default; ObserverHandler() = default;
observer_handler(std::function<void()> destructor) ObserverHandler(std::function<void()> destructor)
: destructor(std::move(destructor)) { : destructor(std::move(destructor)) {
} }
observer_handler(const observer_handler&) = delete; ObserverHandler(const ObserverHandler&) = delete;
observer_handler(observer_handler&& handler) noexcept ObserverHandler(ObserverHandler&& handler) noexcept
: destructor(std::move(handler.destructor)) { : destructor(std::move(handler.destructor)) {
} }
~observer_handler() { ~ObserverHandler() {
if (destructor) { if (destructor) {
destructor(); destructor();
} }
@ -26,9 +26,9 @@ public:
return destructor == nullptr; return destructor == nullptr;
} }
observer_handler& operator=(const observer_handler& handler) = delete; ObserverHandler& operator=(const ObserverHandler& handler) = delete;
observer_handler& operator=(observer_handler&& handler) noexcept { ObserverHandler& operator=(ObserverHandler&& handler) noexcept {
if (destructor) { if (destructor) {
destructor(); destructor();
} }

View File

@ -168,10 +168,10 @@ public:
uint currentFrame = 0; uint currentFrame = 0;
uint frames[KEYS_BUFFER_SIZE] {}; uint frames[KEYS_BUFFER_SIZE] {};
std::vector<uint> codepoints; std::vector<uint> codepoints;
std::vector<keycode> pressedKeys; std::vector<Keycode> pressedKeys;
Bindings bindings; Bindings bindings;
bool keys[KEYS_BUFFER_SIZE] {}; bool keys[KEYS_BUFFER_SIZE] {};
std::unordered_map<keycode, util::HandlersList<>> keyCallbacks; std::unordered_map<Keycode, util::HandlersList<>> keyCallbacks;
GLFWInput(GLFWwindow* window) GLFWInput(GLFWwindow* window)
: window(window) { : window(window) {
@ -196,11 +196,11 @@ public:
bool newstate = false; bool newstate = false;
switch (binding.type) { switch (binding.type) {
case inputtype::keyboard: case InputType::KEYBOARD:
newstate = pressed(static_cast<keycode>(binding.code)); newstate = pressed(static_cast<Keycode>(binding.code));
break; break;
case inputtype::mouse: case InputType::MOUSE:
newstate = clicked(static_cast<mousecode>(binding.code)); newstate = clicked(static_cast<Mousecode>(binding.code));
break; break;
} }
@ -224,13 +224,13 @@ public:
keys[key] = pressed; keys[key] = pressed;
frames[key] = currentFrame; frames[key] = currentFrame;
if (pressed && !prevPressed) { if (pressed && !prevPressed) {
const auto& callbacks = keyCallbacks.find(static_cast<keycode>(key)); const auto& callbacks = keyCallbacks.find(static_cast<Keycode>(key));
if (callbacks != keyCallbacks.end()) { if (callbacks != keyCallbacks.end()) {
callbacks->second.notify(); callbacks->second.notify();
} }
} }
if (pressed) { if (pressed) {
pressedKeys.push_back(static_cast<keycode>(key)); pressedKeys.push_back(static_cast<Keycode>(key));
} }
} }
@ -251,24 +251,24 @@ public:
return scroll; return scroll;
} }
bool pressed(keycode key) const override { bool pressed(Keycode key) const override {
int keycode = static_cast<int>(key); int keycode = static_cast<int>(key);
if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE) { if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE) {
return false; return false;
} }
return keys[keycode]; return keys[keycode];
} }
bool jpressed(keycode keycode) const override { bool jpressed(Keycode keycode) const override {
return pressed(keycode) && return pressed(keycode) &&
frames[static_cast<int>(keycode)] == currentFrame; frames[static_cast<int>(keycode)] == currentFrame;
} }
bool clicked(mousecode code) const override { bool clicked(Mousecode code) const override {
return pressed( return pressed(
static_cast<keycode>(MOUSE_KEYS_OFFSET + static_cast<int>(code)) static_cast<Keycode>(MOUSE_KEYS_OFFSET + static_cast<int>(code))
); );
} }
bool jclicked(mousecode code) const override { bool jclicked(Mousecode code) const override {
return clicked(code) && return clicked(code) &&
frames[static_cast<int>(code) + MOUSE_KEYS_OFFSET] == frames[static_cast<int>(code) + MOUSE_KEYS_OFFSET] ==
currentFrame; currentFrame;
@ -311,11 +311,11 @@ public:
return bindings; return bindings;
} }
observer_handler addKeyCallback(keycode key, KeyCallback callback) override { ObserverHandler addKeyCallback(Keycode key, KeyCallback callback) override {
return keyCallbacks[key].add(std::move(callback)); return keyCallbacks[key].add(std::move(callback));
} }
const std::vector<keycode>& getPressedKeys() const override { const std::vector<Keycode>& getPressedKeys() const override {
return pressedKeys; return pressedKeys;
} }

View File

@ -56,7 +56,7 @@ static std::unordered_map<std::string, int> mousecodes {
static std::unordered_map<int, std::string> keynames {}; static std::unordered_map<int, std::string> keynames {};
static std::unordered_map<int, std::string> buttonsnames{}; static std::unordered_map<int, std::string> buttonsnames{};
std::string input_util::get_name(mousecode code) { std::string input_util::get_name(Mousecode code) {
auto found = buttonsnames.find(static_cast<int>(code)); auto found = buttonsnames.find(static_cast<int>(code));
if (found == buttonsnames.end()) { if (found == buttonsnames.end()) {
return "unknown"; return "unknown";
@ -64,7 +64,7 @@ std::string input_util::get_name(mousecode code) {
return found->second; return found->second;
} }
std::string input_util::get_name(keycode code) { std::string input_util::get_name(Keycode code) {
auto found = keynames.find(static_cast<int>(code)); auto found = keynames.find(static_cast<int>(code));
if (found == keynames.end()) { if (found == keynames.end()) {
return "unknown"; return "unknown";
@ -72,17 +72,17 @@ std::string input_util::get_name(keycode code) {
return found->second; return found->second;
} }
void Binding::reset(inputtype type, int code) { void Binding::reset(InputType type, int code) {
this->type = type; this->type = type;
this->code = code; this->code = code;
} }
void Binding::reset(keycode code) { void Binding::reset(Keycode code) {
reset(inputtype::keyboard, static_cast<int>(code)); reset(InputType::KEYBOARD, static_cast<int>(code));
} }
void Binding::reset(mousecode code) { void Binding::reset(Mousecode code) {
reset(inputtype::mouse, static_cast<int>(code)); reset(InputType::MOUSE, static_cast<int>(code));
} }
void input_util::initialize() { void input_util::initialize() {
@ -103,23 +103,23 @@ void input_util::initialize() {
} }
} }
keycode input_util::keycode_from(const std::string& name) { Keycode input_util::keycode_from(const std::string& name) {
const auto& found = keycodes.find(name); const auto& found = keycodes.find(name);
if (found == keycodes.end()) { if (found == keycodes.end()) {
return keycode::UNKNOWN; return Keycode::UNKNOWN;
} }
return static_cast<keycode>(found->second); return static_cast<Keycode>(found->second);
} }
mousecode input_util::mousecode_from(const std::string& name) { Mousecode input_util::mousecode_from(const std::string& name) {
const auto& found = mousecodes.find(name); const auto& found = mousecodes.find(name);
if (found == mousecodes.end()) { if (found == mousecodes.end()) {
return mousecode::UNKNOWN; return Mousecode::UNKNOWN;
} }
return static_cast<mousecode>(found->second); return static_cast<Mousecode>(found->second);
} }
std::string input_util::to_string(keycode code) { std::string input_util::to_string(Keycode code) {
int icode_repr = static_cast<int>(code); int icode_repr = static_cast<int>(code);
const char* name = const char* name =
glfwGetKeyName(icode_repr, glfwGetKeyScancode(icode_repr)); glfwGetKeyName(icode_repr, glfwGetKeyScancode(icode_repr));
@ -212,21 +212,21 @@ std::string input_util::to_string(keycode code) {
return std::string(name); return std::string(name);
} }
std::string input_util::to_string(mousecode code) { std::string input_util::to_string(Mousecode code) {
switch (code) { switch (code) {
case mousecode::BUTTON_1: case Mousecode::BUTTON_1:
return "LMB"; return "LMB";
case mousecode::BUTTON_2: case Mousecode::BUTTON_2:
return "RMB"; return "RMB";
case mousecode::BUTTON_3: case Mousecode::BUTTON_3:
return "MMB"; return "MMB";
case mousecode::BUTTON_4: case Mousecode::BUTTON_4:
case mousecode::BUTTON_5: case Mousecode::BUTTON_5:
case mousecode::BUTTON_6: case Mousecode::BUTTON_6:
case mousecode::BUTTON_7: case Mousecode::BUTTON_7:
case mousecode::BUTTON_8: case Mousecode::BUTTON_8:
return "XButton " + std::to_string(static_cast<int>(code) - return "XButton " + std::to_string(static_cast<int>(code) -
static_cast<int>(mousecode::BUTTON_3)); static_cast<int>(Mousecode::BUTTON_3));
default: default:
return "unknown button"; return "unknown button";
} }
@ -251,13 +251,13 @@ void Bindings::read(const dv::value& map, BindType bindType) {
for (auto& [name, value] : section.asObject()) { for (auto& [name, value] : section.asObject()) {
auto key = sectionName + "." + name; auto key = sectionName + "." + name;
auto [prefix, codename] = util::split_at(value.asString(), ':'); auto [prefix, codename] = util::split_at(value.asString(), ':');
inputtype type; InputType type;
int code; int code;
if (prefix == "key") { if (prefix == "key") {
type = inputtype::keyboard; type = InputType::KEYBOARD;
code = static_cast<int>(input_util::keycode_from(codename)); code = static_cast<int>(input_util::keycode_from(codename));
} else if (prefix == "mouse") { } else if (prefix == "mouse") {
type = inputtype::mouse; type = InputType::MOUSE;
code = static_cast<int>(input_util::mousecode_from(codename)); code = static_cast<int>(input_util::mousecode_from(codename));
} else { } else {
logger.error() logger.error()
@ -282,15 +282,15 @@ std::string Bindings::write() const {
const auto& binding = entry.second; const auto& binding = entry.second;
std::string value; std::string value;
switch (binding.type) { switch (binding.type) {
case inputtype::keyboard: case InputType::KEYBOARD:
value = value =
"key:" + "key:" +
input_util::get_name(static_cast<keycode>(binding.code)); input_util::get_name(static_cast<Keycode>(binding.code));
break; break;
case inputtype::mouse: case InputType::MOUSE:
value = value =
"mouse:" + "mouse:" +
input_util::get_name(static_cast<mousecode>(binding.code)); input_util::get_name(static_cast<Mousecode>(binding.code));
break; break;
default: default:
throw std::runtime_error("unsupported control type"); throw std::runtime_error("unsupported control type");

View File

@ -15,7 +15,7 @@ enum class BindType {
}; };
/// @brief Represents glfw3 keycode values. /// @brief Represents glfw3 keycode values.
enum class keycode : int { enum class Keycode : int {
SPACE = 32, SPACE = 32,
APOSTROPHE = 39, APOSTROPHE = 39,
COMMA = 44, COMMA = 44,
@ -108,7 +108,7 @@ enum class keycode : int {
/// @brief Represents glfw3 mouse button IDs. /// @brief Represents glfw3 mouse button IDs.
/// @details There is a subset of glfw3 mouse button IDs. /// @details There is a subset of glfw3 mouse button IDs.
enum class mousecode : int { enum class Mousecode : int {
BUTTON_1 = 0, // Left mouse button BUTTON_1 = 0, // Left mouse button
BUTTON_2 = 1, // Right mouse button BUTTON_2 = 1, // Right mouse button
BUTTON_3 = 2, // Middle mouse button BUTTON_3 = 2, // Middle mouse button
@ -120,43 +120,49 @@ enum class mousecode : int {
UNKNOWN = -1, UNKNOWN = -1,
}; };
inline mousecode MOUSECODES_ALL[] { inline Mousecode MOUSECODES_ALL[] {
mousecode::BUTTON_1, mousecode::BUTTON_2, mousecode::BUTTON_3, mousecode::BUTTON_4, Mousecode::BUTTON_1,
mousecode::BUTTON_5, mousecode::BUTTON_6, mousecode::BUTTON_7, mousecode::BUTTON_8 }; Mousecode::BUTTON_2,
Mousecode::BUTTON_3,
Mousecode::BUTTON_4,
Mousecode::BUTTON_5,
Mousecode::BUTTON_6,
Mousecode::BUTTON_7,
Mousecode::BUTTON_8};
namespace input_util { namespace input_util {
void initialize(); void initialize();
keycode keycode_from(const std::string& name); Keycode keycode_from(const std::string& name);
mousecode mousecode_from(const std::string& name); Mousecode mousecode_from(const std::string& name);
/// @return Key label by keycode /// @return Key label by keycode
std::string to_string(keycode code); std::string to_string(Keycode code);
/// @return Mouse button label by keycode /// @return Mouse button label by keycode
std::string to_string(mousecode code); std::string to_string(Mousecode code);
/// @return Key name by keycode /// @return Key name by keycode
std::string get_name(keycode code); std::string get_name(Keycode code);
/// @return Mouse button name by keycode /// @return Mouse button name by keycode
std::string get_name(mousecode code); std::string get_name(Mousecode code);
} }
enum class inputtype { enum class InputType {
keyboard, KEYBOARD,
mouse, MOUSE,
}; };
struct Binding { struct Binding {
util::HandlersList<> onactived; util::HandlersList<> onactived;
inputtype type; InputType type;
int code; int code;
bool state = false; bool state = false;
bool justChange = false; bool justChange = false;
bool enabled = true; bool enabled = true;
Binding() = default; Binding() = default;
Binding(inputtype type, int code) : type(type), code(code) { Binding(InputType type, int code) : type(type), code(code) {
} }
bool active() const { bool active() const {
@ -167,17 +173,17 @@ struct Binding {
return state && justChange; return state && justChange;
} }
void reset(inputtype, int); void reset(InputType, int);
void reset(keycode); void reset(Keycode);
void reset(mousecode); void reset(Mousecode);
inline std::string text() const { inline std::string text() const {
switch (type) { switch (type) {
case inputtype::keyboard: { case InputType::KEYBOARD: {
return input_util::to_string(static_cast<keycode>(code)); return input_util::to_string(static_cast<Keycode>(code));
} }
case inputtype::mouse: { case InputType::MOUSE: {
return input_util::to_string(static_cast<mousecode>(code)); return input_util::to_string(static_cast<Mousecode>(code));
} }
} }
return "<unknown input type>"; return "<unknown input type>";
@ -223,11 +229,11 @@ public:
const Binding& require(const std::string& name) const; const Binding& require(const std::string& name) const;
void bind(const std::string& name, inputtype type, int code) { void bind(const std::string& name, InputType type, int code) {
bindings.try_emplace(name, Binding(type, code)); bindings.try_emplace(name, Binding(type, code));
} }
void rebind(const std::string& name, inputtype type, int code) { void rebind(const std::string& name, InputType type, int code) {
require(name) = Binding(type, code); require(name) = Binding(type, code);
} }
@ -262,11 +268,11 @@ public:
virtual int getScroll() = 0; virtual int getScroll() = 0;
virtual bool pressed(keycode keycode) const = 0; virtual bool pressed(Keycode keycode) const = 0;
virtual bool jpressed(keycode keycode) const = 0; virtual bool jpressed(Keycode keycode) const = 0;
virtual bool clicked(mousecode mousecode) const = 0; virtual bool clicked(Mousecode mousecode) const = 0;
virtual bool jclicked(mousecode mousecode) const = 0; virtual bool jclicked(Mousecode mousecode) const = 0;
virtual CursorState getCursor() const = 0; virtual CursorState getCursor() const = 0;
@ -277,12 +283,12 @@ public:
virtual const Bindings& getBindings() const = 0; virtual const Bindings& getBindings() const = 0;
virtual observer_handler addKeyCallback(keycode key, KeyCallback callback) = 0; virtual ObserverHandler addKeyCallback(Keycode key, KeyCallback callback) = 0;
virtual const std::vector<keycode>& getPressedKeys() const = 0; virtual const std::vector<Keycode>& getPressedKeys() const = 0;
virtual const std::vector<uint>& getCodepoints() const = 0; virtual const std::vector<uint>& getCodepoints() const = 0;
observer_handler addCallback(const std::string& name, KeyCallback callback) { ObserverHandler addCallback(const std::string& name, KeyCallback callback) {
return getBindings().require(name).onactived.add(callback); return getBindings().require(name).onactived.add(callback);
} }
}; };