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) {
}
observer_handler observe(consumer<T> callback, bool callOnStart = false) {
ObserverHandler observe(consumer<T> callback, bool callOnStart = false) {
const int id = nextid++;
observers.emplace(id, callback);
if (callOnStart) {
callback(value);
}
return observer_handler([this, id]() {
return ObserverHandler([this, id]() {
observers.erase(id);
});
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -87,7 +87,7 @@ namespace gui {
/// @brief Set maxLocalCaret to local (line) caret position
void resetMaxLocalCaret();
void performEditingKeyboardEvents(keycode key);
void performEditingKeyboardEvents(Keycode key);
void refreshLabel();
@ -238,7 +238,7 @@ namespace gui {
virtual void draw(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 keyPressed(keycode key) override;
virtual void keyPressed(Keycode key) override;
virtual std::shared_ptr<UINode> getAt(const glm::vec2& pos) override;
virtual void setOnUpPressed(const runnable &callback);
virtual void setOnDownPressed(const runnable &callback);

View File

@ -175,7 +175,7 @@ namespace gui {
virtual void onFocus() {focused = true;}
virtual void doubleClick(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 mouseRelease(int x, int y);
virtual void scrolled(int value);
@ -188,7 +188,7 @@ namespace gui {
virtual bool isFocuskeeper() const {return false;}
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
/// @param pos screen position

View File

@ -68,11 +68,11 @@ void guiutil::alert(
panel->refresh();
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();
return true;
}));
panel->keepAlive(input.addKeyCallback(keycode::ESCAPE, [on_hidden_final]() {
panel->keepAlive(input.addKeyCallback(Keycode::ESCAPE, [on_hidden_final]() {
on_hidden_final();
return true;
}));
@ -138,11 +138,11 @@ void guiutil::confirm(
}));
panel->add(subpanel);
panel->keepAlive(input.addKeyCallback(keycode::ENTER, [=]() {
panel->keepAlive(input.addKeyCallback(Keycode::ENTER, [=]() {
on_confirm_final();
return true;
}));
panel->keepAlive(input.addKeyCallback(keycode::ESCAPE, [=]() {
panel->keepAlive(input.addKeyCallback(Keycode::ESCAPE, [=]() {
on_deny_final();
return true;
}));

View File

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

View File

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

View File

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

View File

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

View File

@ -26,11 +26,11 @@ namespace util {
nextid = o.nextid;
}
observer_handler add(runnable callback) {
ObserverHandler add(runnable callback) {
std::lock_guard lock(mutex);
int id = nextid++;
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);
runnables.erase(*id);
delete id;

View File

@ -2,21 +2,21 @@
#include <functional>
class observer_handler {
class ObserverHandler {
public:
observer_handler() = default;
ObserverHandler() = default;
observer_handler(std::function<void()> destructor)
ObserverHandler(std::function<void()> 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)) {
}
~observer_handler() {
~ObserverHandler() {
if (destructor) {
destructor();
}
@ -26,9 +26,9 @@ public:
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) {
destructor();
}

View File

@ -168,10 +168,10 @@ public:
uint currentFrame = 0;
uint frames[KEYS_BUFFER_SIZE] {};
std::vector<uint> codepoints;
std::vector<keycode> pressedKeys;
std::vector<Keycode> pressedKeys;
Bindings bindings;
bool keys[KEYS_BUFFER_SIZE] {};
std::unordered_map<keycode, util::HandlersList<>> keyCallbacks;
std::unordered_map<Keycode, util::HandlersList<>> keyCallbacks;
GLFWInput(GLFWwindow* window)
: window(window) {
@ -196,11 +196,11 @@ public:
bool newstate = false;
switch (binding.type) {
case inputtype::keyboard:
newstate = pressed(static_cast<keycode>(binding.code));
case InputType::KEYBOARD:
newstate = pressed(static_cast<Keycode>(binding.code));
break;
case inputtype::mouse:
newstate = clicked(static_cast<mousecode>(binding.code));
case InputType::MOUSE:
newstate = clicked(static_cast<Mousecode>(binding.code));
break;
}
@ -224,13 +224,13 @@ public:
keys[key] = pressed;
frames[key] = currentFrame;
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()) {
callbacks->second.notify();
}
}
if (pressed) {
pressedKeys.push_back(static_cast<keycode>(key));
pressedKeys.push_back(static_cast<Keycode>(key));
}
}
@ -251,24 +251,24 @@ public:
return scroll;
}
bool pressed(keycode key) const override {
bool pressed(Keycode key) const override {
int keycode = static_cast<int>(key);
if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE) {
return false;
}
return keys[keycode];
}
bool jpressed(keycode keycode) const override {
bool jpressed(Keycode keycode) const override {
return pressed(keycode) &&
frames[static_cast<int>(keycode)] == currentFrame;
}
bool clicked(mousecode code) const override {
bool clicked(Mousecode code) const override {
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) &&
frames[static_cast<int>(code) + MOUSE_KEYS_OFFSET] ==
currentFrame;
@ -311,11 +311,11 @@ public:
return bindings;
}
observer_handler addKeyCallback(keycode key, KeyCallback callback) override {
ObserverHandler addKeyCallback(Keycode key, KeyCallback callback) override {
return keyCallbacks[key].add(std::move(callback));
}
const std::vector<keycode>& getPressedKeys() const override {
const std::vector<Keycode>& getPressedKeys() const override {
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> 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));
if (found == buttonsnames.end()) {
return "unknown";
@ -64,7 +64,7 @@ std::string input_util::get_name(mousecode code) {
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));
if (found == keynames.end()) {
return "unknown";
@ -72,17 +72,17 @@ std::string input_util::get_name(keycode code) {
return found->second;
}
void Binding::reset(inputtype type, int code) {
void Binding::reset(InputType type, int code) {
this->type = type;
this->code = code;
}
void Binding::reset(keycode code) {
reset(inputtype::keyboard, static_cast<int>(code));
void Binding::reset(Keycode code) {
reset(InputType::KEYBOARD, static_cast<int>(code));
}
void Binding::reset(mousecode code) {
reset(inputtype::mouse, static_cast<int>(code));
void Binding::reset(Mousecode code) {
reset(InputType::MOUSE, static_cast<int>(code));
}
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);
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);
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);
const char* name =
glfwGetKeyName(icode_repr, glfwGetKeyScancode(icode_repr));
@ -212,21 +212,21 @@ std::string input_util::to_string(keycode code) {
return std::string(name);
}
std::string input_util::to_string(mousecode code) {
std::string input_util::to_string(Mousecode code) {
switch (code) {
case mousecode::BUTTON_1:
case Mousecode::BUTTON_1:
return "LMB";
case mousecode::BUTTON_2:
case Mousecode::BUTTON_2:
return "RMB";
case mousecode::BUTTON_3:
case Mousecode::BUTTON_3:
return "MMB";
case mousecode::BUTTON_4:
case mousecode::BUTTON_5:
case mousecode::BUTTON_6:
case mousecode::BUTTON_7:
case mousecode::BUTTON_8:
case Mousecode::BUTTON_4:
case Mousecode::BUTTON_5:
case Mousecode::BUTTON_6:
case Mousecode::BUTTON_7:
case Mousecode::BUTTON_8:
return "XButton " + std::to_string(static_cast<int>(code) -
static_cast<int>(mousecode::BUTTON_3));
static_cast<int>(Mousecode::BUTTON_3));
default:
return "unknown button";
}
@ -251,13 +251,13 @@ void Bindings::read(const dv::value& map, BindType bindType) {
for (auto& [name, value] : section.asObject()) {
auto key = sectionName + "." + name;
auto [prefix, codename] = util::split_at(value.asString(), ':');
inputtype type;
InputType type;
int code;
if (prefix == "key") {
type = inputtype::keyboard;
type = InputType::KEYBOARD;
code = static_cast<int>(input_util::keycode_from(codename));
} else if (prefix == "mouse") {
type = inputtype::mouse;
type = InputType::MOUSE;
code = static_cast<int>(input_util::mousecode_from(codename));
} else {
logger.error()
@ -282,15 +282,15 @@ std::string Bindings::write() const {
const auto& binding = entry.second;
std::string value;
switch (binding.type) {
case inputtype::keyboard:
case InputType::KEYBOARD:
value =
"key:" +
input_util::get_name(static_cast<keycode>(binding.code));
input_util::get_name(static_cast<Keycode>(binding.code));
break;
case inputtype::mouse:
case InputType::MOUSE:
value =
"mouse:" +
input_util::get_name(static_cast<mousecode>(binding.code));
input_util::get_name(static_cast<Mousecode>(binding.code));
break;
default:
throw std::runtime_error("unsupported control type");

View File

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