From 4ea7bda2494d9f10ab6bb7a5f82c635fca095860 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 20 Nov 2023 03:31:18 +0300 Subject: [PATCH] PagesControl, refactor --- src/engine.cpp | 4 +- src/frontend/gui/GUI.cpp | 19 ++++-- src/frontend/gui/UINode.cpp | 3 +- src/frontend/gui/UINode.h | 2 + src/frontend/gui/controls.cpp | 47 +++++++++++++- src/frontend/gui/controls.h | 18 ++++++ src/frontend/gui/panels.cpp | 62 +++++++++++++++++-- src/frontend/gui/panels.h | 24 ++++++++ src/frontend/hud.cpp | 74 +++++++++++----------- src/frontend/hud.h | 3 +- src/frontend/screens.cpp | 113 +++++++++++++++++++++------------- src/frontend/screens.h | 3 +- src/window/Events.h | 21 +------ src/window/input.cpp | 30 +++++++++ src/window/input.h | 33 ++++++++++ 15 files changed, 339 insertions(+), 117 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index f9be1b35..572db177 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -59,8 +59,6 @@ Engine::Engine(EngineSettings& settings) : settings(settings) { Audio::initialize(); gui = new GUI(); std::cout << "-- initializing finished" << std::endl; - - setScreen(shared_ptr(new MenuScreen(this))); } void Engine::updateTimers() { @@ -81,6 +79,8 @@ void Engine::updateHotkeys() { } void Engine::mainloop() { + setScreen(shared_ptr(new MenuScreen(this))); + std::cout << "-- preparing systems" << std::endl; Batch2D batch(1024); diff --git a/src/frontend/gui/GUI.cpp b/src/frontend/gui/GUI.cpp index 2ccf4895..f28ad67d 100644 --- a/src/frontend/gui/GUI.cpp +++ b/src/frontend/gui/GUI.cpp @@ -45,6 +45,7 @@ void GUI::act(float delta) { } this->hover = hover; + auto prevfocus = focus; if (Events::jclicked(0)) { if (pressed == nullptr && this->hover) { pressed = hover; @@ -52,7 +53,10 @@ void GUI::act(float delta) { if (focus && focus != pressed) { focus->defocus(); } - focus = pressed; + if (focus != pressed) { + focus = pressed; + focus->focus(this); + } } if (this->hover == nullptr && focus) { focus->defocus(); @@ -63,9 +67,7 @@ void GUI::act(float delta) { pressed = nullptr; } if (focus) { - if (!focus->isfocused()){ - focus = nullptr; - } else if (Events::jpressed(keycode::ESCAPE)) { + if (Events::jpressed(keycode::ESCAPE)) { focus->defocus(); focus = nullptr; } else { @@ -78,8 +80,17 @@ void GUI::act(float delta) { if (Events::clicked(mousecode::BUTTON_1)) { focus->mouseMove(this, mx, my); } + if (prevfocus == focus) + for (int i = mousecode::BUTTON_1; i < mousecode::BUTTON_1+12; i++) { + if (Events::jclicked(i)) { + focus->clicked(this, i); + } + } } } + if (focus && !focus->isfocused()) { + focus = nullptr; + } } void GUI::draw(Batch2D* batch, Assets* assets) { diff --git a/src/frontend/gui/UINode.cpp b/src/frontend/gui/UINode.cpp index cd4c9477..751c341a 100644 --- a/src/frontend/gui/UINode.cpp +++ b/src/frontend/gui/UINode.cpp @@ -52,7 +52,6 @@ UINode* UINode::getParent() const { void UINode::click(GUI*, int x, int y) { pressed_ = true; - focused_ = true; } void UINode::mouseRelease(GUI*, int x, int y) { @@ -103,7 +102,7 @@ void UINode::size(vec2 size) { this->size_ = size; if (parent) { sizelock = true; - parent->refresh(); + //parent->refresh(); sizelock = false; } } diff --git a/src/frontend/gui/UINode.h b/src/frontend/gui/UINode.h index e9699118..1620fc5a 100644 --- a/src/frontend/gui/UINode.h +++ b/src/frontend/gui/UINode.h @@ -56,7 +56,9 @@ namespace gui { virtual void margin(glm::vec4 margin); glm::vec4 margin() const; + virtual void focus(GUI*) {focused_ = true;} virtual void click(GUI*, int x, int y); + virtual void clicked(GUI*, int button) {} virtual void mouseMove(GUI*, int x, int y) {}; virtual void mouseRelease(GUI*, int x, int y); diff --git a/src/frontend/gui/controls.cpp b/src/frontend/gui/controls.cpp index c0293703..295073e5 100644 --- a/src/frontend/gui/controls.cpp +++ b/src/frontend/gui/controls.cpp @@ -5,6 +5,7 @@ #include "../../assets/Assets.h" #include "../../graphics/Batch2D.h" #include "../../graphics/Font.h" +#include "../../util/stringutil.h" using std::string; using std::wstring; @@ -36,7 +37,7 @@ void Label::draw(Batch2D* batch, Assets* assets) { } batch->color = color_; Font* font = assets->getFont(fontName_); - vec2 size = this->size(); + vec2 size = UINode::size(); vec2 newsize = vec2(font->calcWidth(text_), font->lineHeight()); if (newsize.x > size.x) { this->size(newsize); @@ -51,6 +52,11 @@ Label* Label::textSupplier(wstringsupplier supplier) { return this; } +void Label::size(vec2 sizenew) { + UINode::size(vec2(UINode::size().x, sizenew.y)); +} + +// ================================= Button =================================== Button::Button(shared_ptr content, glm::vec4 padding) : Panel(vec2(32,32), padding, 0) { add(content); } @@ -86,12 +92,12 @@ Button* Button::listenAction(onaction action) { return this; } +// ================================ TextBox =================================== TextBox::TextBox(wstring placeholder, vec4 padding) : Panel(vec2(200,32), padding, 0, false), input(L""), placeholder(placeholder) { label = new Label(L""); - label->align(Align::center); add(shared_ptr(label)); } @@ -151,6 +157,42 @@ wstring TextBox::text() const { return input; } +// ============================== InputBindBox ================================ +InputBindBox::InputBindBox(Binding& binding, vec4 padding) + : Panel(vec2(100,32), padding, 0, false), + binding(binding) { + label = new Label(L""); + //label->align(Align::center); + add(shared_ptr(label)); +} + +shared_ptr InputBindBox::getAt(vec2 pos, shared_ptr self) { + return UINode::getAt(pos, self); +} + +void InputBindBox::drawBackground(Batch2D* batch, Assets* assets) { + vec2 coord = calcCoord(); + batch->texture(nullptr); + batch->color = (isfocused() ? focusedColor : (hover_ ? hoverColor : color_)); + batch->rect(coord.x, coord.y, size_.x, size_.y); + label->text(util::str2wstr_utf8(binding.text())); +} + +void InputBindBox::clicked(GUI*, int button) { + binding.type = inputtype::button; + binding.code = button; + defocus(); +} + +void InputBindBox::keyPressed(int key) { + if (key != keycode::ESCAPE) { + binding.type = inputtype::keyboard; + binding.code = key; + } + defocus(); +} + +// ================================ TrackBar ================================== TrackBar::TrackBar(double min, double max, double value, double step, int trackWidth) : UINode(vec2(), vec2(32)), min(min), max(max), value(value), step(step), trackWidth(trackWidth) { color(vec4(0.f, 0.f, 0.f, 0.4f)); @@ -194,6 +236,7 @@ void TrackBar::mouseMove(GUI*, int x, int y) { } } +// ================================ CheckBox ================================== CheckBox::CheckBox(bool checked) : UINode(vec2(), vec2(32.0f)), checked_(checked) { color(vec4(0.0f, 0.0f, 0.0f, 0.5f)); } diff --git a/src/frontend/gui/controls.h b/src/frontend/gui/controls.h index 8bbe6f1e..0ef15269 100644 --- a/src/frontend/gui/controls.h +++ b/src/frontend/gui/controls.h @@ -8,6 +8,7 @@ #include #include "UINode.h" #include "panels.h" +#include "../../window/input.h" class Batch2D; class Assets; @@ -36,6 +37,7 @@ namespace gui { virtual void draw(Batch2D* batch, Assets* assets) override; virtual Label* textSupplier(wstringsupplier supplier); + virtual void size(glm::vec2 size) override; }; class Button : public Panel { @@ -79,6 +81,22 @@ namespace gui { virtual std::wstring text() const; }; + class InputBindBox : public Panel { + protected: + glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f}; + glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f}; + Label* label; + Binding& binding; + public: + InputBindBox(Binding& binding, glm::vec4 padding=glm::vec4(6.0f)); + virtual void drawBackground(Batch2D* batch, Assets* assets) override; + virtual std::shared_ptr getAt(glm::vec2 pos, std::shared_ptr self) override; + + virtual void clicked(GUI*, int button) override; + virtual void keyPressed(int key) override; + virtual bool isfocuskeeper() const override {return true;} + }; + class TrackBar : public UINode { protected: glm::vec4 hoverColor {0.01f, 0.02f, 0.03f, 0.5f}; diff --git a/src/frontend/gui/panels.cpp b/src/frontend/gui/panels.cpp index 85bedbbb..871bc0d4 100644 --- a/src/frontend/gui/panels.cpp +++ b/src/frontend/gui/panels.cpp @@ -1,15 +1,14 @@ #include "panels.h" +#include + #include "../../window/Window.h" #include "../../assets/Assets.h" #include "../../graphics/Batch2D.h" using std::shared_ptr; -using gui::UINode; -using gui::Container; -using gui::Panel; -using gui::Orientation; +using namespace gui; using glm::vec2; using glm::vec4; @@ -138,7 +137,8 @@ void Panel::refresh() { y += nodesize.y + margin.w + interval; float width = size.x - padding.x - padding.z - margin.x - margin.z; - node->size(vec2(width, nodesize.y)); + node->size(vec2(width, nodesize.y));; + node->refresh(); maxw = fmax(maxw, ex+node->size().x+margin.z+padding.z); } if (resizing_) @@ -154,6 +154,7 @@ void Panel::refresh() { float height = size.y - padding.y - padding.w - margin.y - margin.w; node->size(vec2(nodesize.x, height)); + node->refresh(); maxh = fmax(maxh, y+margin.y+node->size().y+margin.w+padding.w); } bool increased = maxh > size.y; @@ -177,4 +178,55 @@ void Panel::lock(){ node->lock(); } resizing_ = false; +} + +PagesControl::PagesControl() : Container(vec2(), vec2(1)){ +} + +void PagesControl::add(std::string name, std::shared_ptr panel) { + panel->visible(false); + pages[name] = Page{panel}; + Container::add(panel); +} + +void PagesControl::set(std::string name, bool history) { + auto found = pages.find(name); + if (found == pages.end()) { + throw std::runtime_error("no page found"); + } + if (current_.panel) { + current_.panel->visible(false); + } + if (history) { + pageStack.push(curname_); + } + curname_ = name; + current_ = found->second; + current_.panel->visible(true); + size(current_.panel->size()); +} + +void PagesControl::back() { + if (pageStack.empty()) + return; + std::string name = pageStack.top(); + pageStack.pop(); + set(name, false); +} + +Page PagesControl::current() { + return current_; +} + +void PagesControl::clearHistory() { + pageStack = std::stack(); +} + +void PagesControl::reset() { + clearHistory(); + if (current_.panel) { + curname_ = ""; + current_.panel->visible(false); + current_ = Page{nullptr}; + } } \ No newline at end of file diff --git a/src/frontend/gui/panels.h b/src/frontend/gui/panels.h index 7441f211..174180c7 100644 --- a/src/frontend/gui/panels.h +++ b/src/frontend/gui/panels.h @@ -3,6 +3,8 @@ #include #include +#include +#include #include #include "UINode.h" @@ -56,5 +58,27 @@ namespace gui { virtual void refresh() override; virtual void lock() override; }; + + struct Page { + std::shared_ptr panel = nullptr; + }; + + class PagesControl : public Container { + protected: + std::unordered_map pages; + std::stack pageStack; + Page current_; + std::string curname_ = ""; + public: + PagesControl(); + + void set(std::string name, bool history=true); + void add(std::string name, std::shared_ptr panel); + void back(); + void clearHistory(); + void reset(); + + Page current(); + }; } #endif // FRONTEND_GUI_PANELS_H_ \ No newline at end of file diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index 8fe79b8e..0d0b04cb 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -51,6 +51,9 @@ HudRenderer::HudRenderer(Engine* engine, Level* level) : level(level), assets(en uicamera->perspective = false; uicamera->flipped = true; + auto pagesptr = gui->get("pages"); + PagesControl* pages = (PagesControl*)(pagesptr.get()); + Panel* panel = new Panel(vec2(250, 200), vec4(5.0f), 1.0f); debugPanel = shared_ptr(panel); panel->listenInterval(1.0f, [this]() { @@ -116,37 +119,35 @@ HudRenderer::HudRenderer(Engine* engine, Level* level) : level(level), assets(en panel->refresh(); panel = new Panel(vec2(350, 200)); - pauseMenu = shared_ptr(panel); + auto pauseMenu = shared_ptr(panel); panel->color(vec4(0.0f)); { Button* button = new Button(L"Continue", vec4(10.0f)); - button->listenAction([this](GUI*){ - this->pause = false; - pauseMenu->visible(false); + button->listenAction([=](GUI*){ + pages->reset(); + pause = false; }); panel->add(shared_ptr(button)); } panel->add((new Button(L"Settings", vec4(10.f)))->listenAction([=](GUI* gui) { - pauseMenu->visible(false); - gui->store("back", pauseMenu); - gui->get("settings")->visible(true); + pages->set("settings"); })); { Button* button = new Button(L"Save and Quit to Menu", vec4(10.f)); button->listenAction([this, engine](GUI*){ - this->pauseMenu->visible(false); engine->setScreen(shared_ptr(new MenuScreen(engine))); }); panel->add(shared_ptr(button)); } - panel->visible(false); + + pages->reset(); + pages->add("pause", pauseMenu); gui->add(this->debugPanel); - gui->add(this->pauseMenu); } HudRenderer::~HudRenderer() { gui->remove(debugPanel); - gui->remove(pauseMenu); + //gui->remove(gui->get("pages")); delete batch; delete uicamera; } @@ -226,8 +227,7 @@ void HudRenderer::drawInventory(const GfxContext& ctx, Player* player) { if (Events::jclicked(GLFW_MOUSE_BUTTON_LEFT)) { player->choosenBlock = i+1; } - } else - { + } else { tint = vec4(1.0f); } @@ -239,16 +239,36 @@ void HudRenderer::drawInventory(const GfxContext& ctx, Player* player) { } } +void HudRenderer::update() { + PagesControl* pages = (PagesControl*)(gui->get("pages").get()); + if (Events::jpressed(keycode::ESCAPE) && !gui->isFocusCaught()) { + if (pause) { + pause = false; + pages->reset(); + } else if (inventoryOpen) { + inventoryOpen = false; + } else { + pause = true; + pages->set("pause"); + } + } + if (Events::jactive(BIND_HUD_INVENTORY)) { + if (!pause) { + inventoryOpen = !inventoryOpen; + } + } + if ((pause || inventoryOpen) == Events::_cursor_locked) + Events::toggleCursor(); +} + void HudRenderer::draw(const GfxContext& ctx){ const Viewport& viewport = ctx.getViewport(); const uint width = viewport.getWidth(); const uint height = viewport.getHeight(); + auto pages = gui->get("pages"); debugPanel->visible(level->player->debug); - pauseMenu->setCoord((viewport.size() - pauseMenu->size()) / 2.0f); - - auto settingsPanel = gui->get("settings"); - settingsPanel->setCoord((viewport.size() - settingsPanel->size()) / 2.0f); + pages->setCoord((viewport.size() - pages->size()) / 2.0f); uicamera->fov = height; @@ -298,26 +318,6 @@ void HudRenderer::draw(const GfxContext& ctx){ } } - if (Events::jpressed(keycode::ESCAPE) && !gui->isFocusCaught()) { - if (pause) { - pause = false; - pauseMenu->visible(false); - settingsPanel->visible(false); - } else if (inventoryOpen) { - inventoryOpen = false; - } else { - pause = true; - pauseMenu->visible(true); - } - } - if (Events::jactive(BIND_HUD_INVENTORY)) { - if (!pause) { - inventoryOpen = !inventoryOpen; - } - } - if ((pause || inventoryOpen) == Events::_cursor_locked) - Events::toggleCursor(); - if (pause || inventoryOpen) { batch->texture(nullptr); batch->color = vec4(0.0f, 0.0f, 0.0f, 0.5f); diff --git a/src/frontend/hud.h b/src/frontend/hud.h index 0dd0466d..2da61830 100644 --- a/src/frontend/hud.h +++ b/src/frontend/hud.h @@ -34,11 +34,12 @@ class HudRenderer { bool pause = false; std::shared_ptr debugPanel; - std::shared_ptr pauseMenu; gui::GUI* gui; public: HudRenderer(Engine* engine, Level* level); ~HudRenderer(); + + void update(); void drawInventory(const GfxContext& ctx, Player* player); void draw(const GfxContext& context); void drawDebug(int fps, bool occlusion); diff --git a/src/frontend/screens.cpp b/src/frontend/screens.cpp index e6d13241..09a00844 100644 --- a/src/frontend/screens.cpp +++ b/src/frontend/screens.cpp @@ -29,6 +29,7 @@ #include "../engine.h" #include "../files/engine_files.h" #include "../util/stringutil.h" +#include "../core_defs.h" using std::string; using std::wstring; @@ -40,15 +41,13 @@ using std::filesystem::u8path; using std::filesystem::directory_iterator; using namespace gui; -shared_ptr create_main_menu_panel(Engine* engine) { +shared_ptr create_main_menu_panel(Engine* engine, PagesControl* pages) { Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f); shared_ptr panelptr(panel); panel->color(vec4(0.0f)); - panel->setCoord(vec2(10, 10)); panel->add((new Button(L"New World", vec4(10.f)))->listenAction([=](GUI* gui) { - panel->visible(false); - gui->get("new-world")->visible(true); + pages->set("new-world"); })); Panel* worldsPanel = new Panel(vec2(390, 200), vec4(5.0f)); @@ -74,22 +73,20 @@ shared_ptr create_main_menu_panel(Engine* engine) { panel->add(worldsPanel); panel->add((new Button(L"Settings", vec4(10.f)))->listenAction([=](GUI* gui) { - panel->visible(false); - gui->store("back", panelptr); - gui->get("settings")->visible(true); + pages->set("settings"); })); panel->add((new Button(L"Quit", vec4(10.f)))->listenAction([](GUI*) { Window::setShouldClose(true); })); + panel->refresh(); return panelptr; } -shared_ptr create_new_world_panel(Engine* engine) { +shared_ptr create_new_world_panel(Engine* engine, PagesControl* pages) { Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f); shared_ptr panelptr(panel); panel->color(vec4(0.0f)); - panel->setCoord(vec2(10, 10)); TextBox* worldNameInput; { @@ -167,17 +164,44 @@ shared_ptr create_new_world_panel(Engine* engine) { } panel->add((new Button(L"Back", vec4(10.f)))->listenAction([=](GUI* gui) { - panel->visible(false); - gui->get("main-menu")->visible(true); + pages->back(); })); - + panel->refresh(); return panelptr; } -Panel* create_settings_panel(Engine* engine) { +Panel* create_controls_panel(Engine* engine, PagesControl* pages) { Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f); panel->color(vec4(0.0f)); - panel->setCoord(vec2(10, 10)); + + for (auto& entry : Events::bindings){ + string bindname = entry.first; + std::cout << bindname << std::endl; + + Panel* subpanel = new Panel(vec2(400, 45), vec4(5.0f), 1.0f); + subpanel->color(vec4(0.0f)); + subpanel->orientation(Orientation::horizontal); + + InputBindBox* bindbox = new InputBindBox(entry.second); + subpanel->add(bindbox); + Label* label = new Label(util::str2wstr_utf8(bindname)); + label->margin(vec4(6.0f)); + subpanel->add(label); + panel->add(subpanel); + } + + panel->add((new Button(L"Back", vec4(10.f)))->listenAction([=](GUI* gui) { + pages->back(); + })); + panel->refresh(); + return panel; +} + +shared_ptr create_settings_panel(Engine* engine, PagesControl* pages) { + Panel* panel = new Panel(vec2(400, 200), vec4(5.0f), 1.0f); + panel->color(vec4(0.0f)); + + shared_ptr panelptr(panel); /* Load Distance setting track bar */{ panel->add((new Label(L""))->textSupplier([=]() { @@ -232,30 +256,44 @@ Panel* create_settings_panel(Engine* engine) { panel->add(checkpanel); } - panel->add((new Button(L"Back", vec4(10.f)))->listenAction([=](GUI* gui) { - panel->visible(false); - gui->get("back")->visible(true); + panel->add((new Button(L"Controls", vec4(10.f)))->listenAction([=](GUI* gui) { + pages->set("controls"); })); - return panel; + + panel->add((new Button(L"Back", vec4(10.f)))->listenAction([=](GUI* gui) { + pages->back(); + })); + panel->refresh(); + return panelptr; } MenuScreen::MenuScreen(Engine* engine_) : Screen(engine_) { GUI* gui = engine->getGUI(); - panel = create_main_menu_panel(engine); - newWorldPanel = create_new_world_panel(engine); - newWorldPanel->visible(false); - auto settingsPanel = shared_ptr(create_settings_panel(engine)); - settingsPanel->visible(false); + auto pagesptr = gui->get("pages"); + PagesControl* pages; + if (pagesptr == nullptr) { + pages = new PagesControl(); + auto newWorldPanel = create_new_world_panel(engine, pages); - gui->store("main-menu", panel); - gui->store("new-world", newWorldPanel); - if (gui->get("settings") == nullptr) { - gui->store("settings", settingsPanel); + auto settingsPanel = shared_ptr(create_settings_panel(engine, pages)); + auto controlsPanel = shared_ptr(create_controls_panel(engine, pages)); + + pages->add("new-world", newWorldPanel); + pages->add("settings", settingsPanel); + pages->add("controls", controlsPanel); + + this->pages = shared_ptr(pages); + gui->add(this->pages); + gui->store("pages", this->pages); + } else { + this->pages = pagesptr; + pages = (PagesControl*)(pagesptr.get()); + pages->reset(); } - gui->add(panel); - gui->add(newWorldPanel); - gui->add(settingsPanel); + auto mainMenuPanel = create_main_menu_panel(engine, pages); + pages->add("main", mainMenuPanel); + pages->set("main"); batch = new Batch2D(1024); uicamera = new Camera(vec3(), Window::height); @@ -264,13 +302,6 @@ MenuScreen::MenuScreen(Engine* engine_) : Screen(engine_) { } MenuScreen::~MenuScreen() { - GUI* gui = engine->getGUI(); - - gui->remove("main-menu"); - gui->remove("new-world"); - - gui->remove(newWorldPanel); - gui->remove(panel); delete batch; delete uicamera; } @@ -279,11 +310,7 @@ void MenuScreen::update(float delta) { } void MenuScreen::draw(float delta) { - panel->setCoord((Window::size() - panel->size()) / 2.0f); - newWorldPanel->setCoord((Window::size() - newWorldPanel->size()) / 2.0f); - - auto settingsPanel = engine->getGUI()->get("settings"); - settingsPanel->setCoord((Window::size() - settingsPanel->size()) / 2.0f); + pages->setCoord((Window::size() - pages->size()) / 2.0f); Window::clear(); Window::setBgColor(vec3(0.2f, 0.2f, 0.2f)); @@ -352,6 +379,8 @@ void LevelScreen::update(float delta) { level->update(); level->chunksController->update(settings.chunks.loadSpeed); + + hud->update(); } void LevelScreen::draw(float delta) { diff --git a/src/frontend/screens.h b/src/frontend/screens.h index 37c72b7f..c29299c8 100644 --- a/src/frontend/screens.h +++ b/src/frontend/screens.h @@ -28,8 +28,7 @@ public: }; class MenuScreen : public Screen { - std::shared_ptr panel; - std::shared_ptr newWorldPanel; + std::shared_ptr pages; Batch2D* batch; Camera* uicamera; public: diff --git a/src/window/Events.h b/src/window/Events.h index 2dd6885c..c4d4813f 100644 --- a/src/window/Events.h +++ b/src/window/Events.h @@ -2,6 +2,7 @@ #define WINDOW_EVENTS_H_ #include "Window.h" +#include "input.h" #include #include @@ -9,26 +10,6 @@ typedef unsigned int uint; -enum class inputtype { - keyboard, - button, -}; - -struct Binding { - inputtype type; - int code; - bool state = false; - bool justChange = false; - - bool active() const { - return state; - } - - bool jactive() const { - return state && justChange; - } -}; - class Events { public: static bool* _keys; diff --git a/src/window/input.cpp b/src/window/input.cpp index 1f5e50ec..93cb08b2 100644 --- a/src/window/input.cpp +++ b/src/window/input.cpp @@ -66,6 +66,36 @@ int keycode::NUM_7 = GLFW_KEY_7; int keycode::NUM_8 = GLFW_KEY_8; int keycode::NUM_9 = GLFW_KEY_9; +const char* keycode::name(int code) { + const char* name = glfwGetKeyName(code, glfwGetKeyScancode(code)); + if (name == nullptr) { + switch (code) { + case GLFW_KEY_TAB: return "Tab"; + case GLFW_KEY_LEFT_CONTROL: return "Left Ctrl"; + case GLFW_KEY_RIGHT_CONTROL: return "Right Ctrl"; + case GLFW_KEY_LEFT_ALT: return "Left Alt"; + case GLFW_KEY_RIGHT_ALT: return "Right Alt"; + case GLFW_KEY_LEFT_SHIFT: return "Left Shift"; + case GLFW_KEY_RIGHT_SHIFT: return "Right Shift"; + case GLFW_KEY_CAPS_LOCK: return "Caps-Lock"; + case GLFW_KEY_SPACE: return "Space"; + case GLFW_KEY_ESCAPE: return "Esc"; + default: + return "Unknown"; + } + } + return name; +} + int mousecode::BUTTON_1 = GLFW_MOUSE_BUTTON_1; int mousecode::BUTTON_2 = GLFW_MOUSE_BUTTON_2; int mousecode::BUTTON_3 = GLFW_MOUSE_BUTTON_3; + +const char* mousecode::name(int code) { + switch (code) { + case GLFW_MOUSE_BUTTON_1: return "LMB"; + case GLFW_MOUSE_BUTTON_2: return "RMB"; + case GLFW_MOUSE_BUTTON_3: return "MMB"; + } + return "unknown button"; +} diff --git a/src/window/input.h b/src/window/input.h index 9cb9ce3b..33469fdc 100644 --- a/src/window/input.h +++ b/src/window/input.h @@ -66,12 +66,45 @@ namespace keycode { extern int NUM_7; extern int NUM_8; extern int NUM_9; + + extern const char* name(int code); } namespace mousecode { extern int BUTTON_1; extern int BUTTON_2; extern int BUTTON_3; + + extern const char* name(int code); } +enum class inputtype { + keyboard, + button, +}; + +struct Binding { + inputtype type; + int code; + bool state = false; + bool justChange = false; + + bool active() const { + return state; + } + + bool jactive() const { + return state && justChange; + } + + const char* text() const { + switch (type) { + case inputtype::keyboard: return keycode::name(code); + case inputtype::button: return mousecode::name(code); + } + return ""; + } +}; + + #endif // WINDOW_INPUT_H_ \ No newline at end of file