From a57cecaf9849c8437d93b0442de28b2f9a91be02 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 19 Feb 2019 22:58:12 +0100 Subject: [PATCH] input: remove a lot of unnecessarily convoluted code. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In particular, get rid of the enum magic; we can use map if we really need some named enums. Which we usually don’t. --- doc/samples/btphystest.cpp | 38 +++------- doc/samples/btphystest.h | 20 +++--- src/Makefile.am | 2 +- src/input/controller.h | 41 ++--------- src/input/input.cpp | 43 +++++++++++- src/input/input.h | 26 +++++-- src/input/{keys.h => keys.inc} | 8 +-- src/input/sdl-input.cpp | 39 +++++----- src/lol-core.vcxproj | 2 +- src/lol-core.vcxproj.filter | 2 +- src/lolimgui.cpp | 125 ++++++++++++++------------------- src/lolimgui.h | 98 -------------------------- 12 files changed, 161 insertions(+), 283 deletions(-) rename src/input/{keys.h => keys.inc} (96%) diff --git a/doc/samples/btphystest.cpp b/doc/samples/btphystest.cpp index 2e46a243..29405073 100644 --- a/doc/samples/btphystest.cpp +++ b/doc/samples/btphystest.cpp @@ -1,8 +1,8 @@ // // Lol Engine — BtPhys tutorial // -// Copyright © 2009—2015 Benjamin “Touky” Huet -// © 2012—2019 Sam Hocevar +// Copyright © 2012—2019 Sam Hocevar +// © 2009—2015 Benjamin “Touky” Huet // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -77,26 +77,10 @@ void BtPhysTest::InitApp() m_loc_dp = .0f; #endif //CAT_MODE -#if 1 //HAS_INPUT - InputProfile& ip = m_profile; - ip.AddBindings(InputProfileType::Keyboard); - + m_profile.register_default_keys(); m_controller = new Controller("Default"); m_controller->Init(m_profile); Ticker::Ref(m_controller); -#else - /* Register an input controller for the keyboard */ - m_controller = new Controller("Default"); - m_controller->SetInputCount(KEY_MAX, 0); - m_controller->GetKey(KEY_MOVE_FORWARD).Bind("Keyboard", "Up"); - m_controller->GetKey(KEY_MOVE_BACK).Bind("Keyboard", "Down"); - m_controller->GetKey(KEY_MOVE_LEFT).Bind("Keyboard", "Left"); - m_controller->GetKey(KEY_MOVE_RIGHT).Bind("Keyboard", "Right"); - m_controller->GetKey(KEY_MOVE_JUMP).Bind("Keyboard", "Space"); - m_controller->GetKey(KEY_MOVE_UP).Bind("Keyboard", "PageUp"); - m_controller->GetKey(KEY_MOVE_DOWN).Bind("Keyboard", "PageDown"); - m_controller->GetKey(KEY_QUIT).Bind("Keyboard", "Escape"); -#endif /* Create a camera that matches the settings of XNA BtPhysTest */ m_camera = new Camera(); @@ -417,7 +401,7 @@ void BtPhysTest::tick_game(float seconds) auto context = Debug::DrawContext::New(Color::white, 1.f); Debug::DrawGrid(vec3::zero, vec3::axis_x, vec3::axis_y, vec3::axis_z, 10.f); - if (m_controller->WasKeyReleasedThisFrame(BtPhysTestKeyInput::KEY_QUIT)) + if (m_controller->WasKeyReleasedThisFrame((int)input::key::SC_Escape)) Ticker::Shutdown(); m_loop_value += seconds; @@ -601,15 +585,15 @@ void BtPhysTest::tick_game(float seconds) mat4 CtlrMx = Character->GetTransform(); vec3 movement(0.f); - movement.z = (m_controller->IsKeyPressed(KEY_MOVE_RIGHT) ? 1.f : 0.f) - - (m_controller->IsKeyPressed(KEY_MOVE_LEFT) ? 1.f : 0.f); - movement.x = (m_controller->IsKeyPressed(KEY_MOVE_FORWARD) ? 1.f : 0.f) - - (m_controller->IsKeyPressed(KEY_MOVE_BACK) ? 1.f : 0.f); - movement.y = (m_controller->IsKeyPressed(KEY_MOVE_UP) ? 1.f : 0.f) - - (m_controller->IsKeyPressed(KEY_MOVE_DOWN) ? 1.f : 0.f); + movement.z = (m_controller->IsKeyPressed(input::key::SC_Right) ? 1.f : 0.f) + - (m_controller->IsKeyPressed(input::key::SC_Left) ? 1.f : 0.f); + movement.x = (m_controller->IsKeyPressed(input::key::SC_Up) ? 1.f : 0.f) + - (m_controller->IsKeyPressed(input::key::SC_Down) ? 1.f : 0.f); + movement.y = (m_controller->IsKeyPressed(input::key::SC_PageUp) ? 1.f : 0.f) + - (m_controller->IsKeyPressed(input::key::SC_PageDown) ? 1.f : 0.f); vec3 CharMove = movement * seconds * vec3(4.f, 10.f, 4.f); - if (m_controller->WasKeyReleasedThisFrame(KEY_MOVE_JUMP)) + if (m_controller->WasKeyReleasedThisFrame(input::key::SC_Space)) Character->Jump(); Character->SetMovementForFrame(CharMove); diff --git a/doc/samples/btphystest.h b/doc/samples/btphystest.h index 5251da8c..0aec8361 100644 --- a/doc/samples/btphystest.h +++ b/doc/samples/btphystest.h @@ -1,8 +1,8 @@ // // Lol Engine — Bullet physics test // -// Copyright © 2009—2013 Benjamin “Touky” Huet -// © 2012—2019 Sam Hocevar +// Copyright © 2012—2019 Sam Hocevar +// © 2009—2013 Benjamin “Touky” Huet // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -74,14 +74,14 @@ private: protected: virtual bool BuildEnumMap(std::map& enum_map) { - enum_map[KEY_MOVE_FORWARD] = g_name_key_Up; - enum_map[KEY_MOVE_BACK] = g_name_key_Down; - enum_map[KEY_MOVE_LEFT] = g_name_key_Left; - enum_map[KEY_MOVE_RIGHT] = g_name_key_Right; - enum_map[KEY_MOVE_UP] = g_name_key_PageUp; - enum_map[KEY_MOVE_DOWN] = g_name_key_PageDown; - enum_map[KEY_MOVE_JUMP] = g_name_key_Space; - enum_map[KEY_QUIT] = g_name_key_Escape; + enum_map[KEY_MOVE_FORWARD] = input::key_to_name(input::key::SC_Up); + enum_map[KEY_MOVE_BACK] = input::key_to_name(input::key::SC_Down); + enum_map[KEY_MOVE_LEFT] = input::key_to_name(input::key::SC_Left); + enum_map[KEY_MOVE_RIGHT] = input::key_to_name(input::key::SC_Right); + enum_map[KEY_MOVE_UP] = input::key_to_name(input::key::SC_PageUp); + enum_map[KEY_MOVE_DOWN] = input::key_to_name(input::key::SC_PageDown); + enum_map[KEY_MOVE_JUMP] = input::key_to_name(input::key::SC_Space); + enum_map[KEY_QUIT] = input::key_to_name(input::key::SC_Escape); return true; } diff --git a/src/Makefile.am b/src/Makefile.am index b4229e31..a602d05e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -99,7 +99,7 @@ liblol_core_sources = \ \ audio/audio.cpp audio/sample.cpp \ \ - input/input.cpp input/input.h input/input_internal.h input/keys.h \ + input/input.cpp input/input.h input/input_internal.h input/keys.inc \ input/controller.cpp input/controller.h \ \ gpu/default-material.lolfx \ diff --git a/src/input/controller.h b/src/input/controller.h index 27401239..3d69d103 100644 --- a/src/input/controller.h +++ b/src/input/controller.h @@ -1,8 +1,8 @@ // // Lol Engine // -// Copyright © 2010—2015 Benjamin Litzelmann -// © 2017—2018 Sam Hocevar +// Copyright © 2017—2019 Sam Hocevar +// © 2010—2015 Benjamin Litzelmann // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -318,40 +318,10 @@ public: return *this; } - //BindingType ------------------------------------------------------------- - struct InputTypeBase : public StructSafeEnum + void register_default_keys() { - enum Type - { - Keyboard = 0, - MouseKey, - JoystickKey, - MouseAxis, - JoystickAxis, - - MAX, - }; - protected: - virtual bool BuildEnumMap(std::map& enum_map) { UNUSED(enum_map); return true; } - }; - typedef SafeEnum InputType; - - //Template helper --------------------------------------------------------- - template - void AddBindings(InputType const& type, uint64_t joy = 0) - { - for (int i = N_BEGIN; i < N_END; ++i) - { - switch (type.ToScalar()) - { - case InputType::Keyboard:/******/*this << InputProfile::Keyboard/******/(/***/i, T(i).tostring()); break; - case InputType::MouseKey:/******/*this << InputProfile::MouseKey/******/(/***/i, T(i).tostring()); break; - case InputType::JoystickKey:/***/*this << InputProfile::JoystickKey/***/(joy, i, T(i).tostring()); break; - case InputType::MouseAxis:/*****/*this << InputProfile::MouseAxis/*****/(/***/i, T(i).tostring()); break; - case InputType::JoystickAxis:/**/*this << InputProfile::JoystickAxis/**/(joy, i, T(i).tostring()); break; - default: break; - } - } +#define _SC(id, str, name) *this << InputProfile::Keyboard(id, #name); +#include "input/keys.inc" } private: @@ -362,7 +332,6 @@ private: array m_joystick_keys; array m_joystick_axis; }; -typedef InputProfile::InputType InputProfileType; //----------------------------------------------------------------------------- //TODO: Add mask|layer system to prevent several controllers from interfering with another. (input overlay in menus) diff --git a/src/input/input.cpp b/src/input/input.cpp index b0dae634..0c60e54c 100644 --- a/src/input/input.cpp +++ b/src/input/input.cpp @@ -1,8 +1,8 @@ // // Lol Engine // -// Copyright © 2010—2015 Benjamin Litzelmann -// © 2017—2018 Sam Hocevar +// Copyright © 2017—2019 Sam Hocevar +// © 2010—2015 Benjamin Litzelmann // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -14,12 +14,49 @@ #include #include +#include #include "input/input_internal.h" namespace lol { +// Lookup tables for scancode and key name lookups +static std::vector g_all_keys +{ +#define _SC(code, str, name) input::key::SC_##name, +#include "input/keys.inc" +}; + +static std::map g_key_to_name +{ +#define _SC(code, str, name) { input::key::SC_##name, #name }, +#include "input/keys.inc" +}; + +static std::map g_name_to_key +{ +#define _SC(code, str, name) { #name, input::key::SC_##name }, +#include "input/keys.inc" +}; + +std::vector const &input::all_keys() +{ + return g_all_keys; +} + +std::string const &input::key_to_name(input::key k) +{ + auto it = g_key_to_name.find(k); + return it == g_key_to_name.end() ? "Unknown" : it->second; +} + +input::key input::name_to_key(std::string const &name) +{ + auto it = g_name_to_key.find(name); + return it == g_name_to_key.end() ? key::SC_Unknown : it->second; +} + array InputDevice::devices; int InputDevice::joystick_count = 0; bool InputDevice::m_capturemouse; @@ -99,7 +136,7 @@ InputDeviceInternal* InputDeviceInternal::CreateStandardKeyboard() /* Register all scancodes known to SDL (from the USB standard) */ # define _SC(id, str, name) keyboard->AddKey(id, #name); -# include "input/keys.h" +# include "input/keys.inc" return keyboard; } diff --git a/src/input/input.h b/src/input/input.h index 57da07b5..8c386b49 100644 --- a/src/input/input.h +++ b/src/input/input.h @@ -1,8 +1,8 @@ // // Lol Engine // -// Copyright © 2010—2015 Benjamin Litzelmann -// © 2017—2018 Sam Hocevar +// Copyright © 2017—2019 Sam Hocevar +// © 2010—2015 Benjamin Litzelmann // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -14,10 +14,25 @@ #pragma once #include +#include namespace lol { +class input +{ +public: + enum class key : uint16_t + { +#define _SC(id, str, name) SC_##name = id, +#include "input/keys.inc" + }; + + static std::vector const &all_keys(); + static std::string const &key_to_name(key k); + static key name_to_key(std::string const &name); +}; + const std::string g_name_max("MAX"); const std::string g_name_mouse("Mouse"); const std::string g_name_keyboard("Keyboard"); @@ -28,10 +43,7 @@ static std::string g_name_joystick(const uint64_t num) return format("Joystick%d", (int)num); } -# define _SC(id, str, name) const std::string g_name_key_##name(#name); -# include "input/keys.h" - -//Mouse default buttons/axis +// Mouse default buttons/axis const std::string g_name_mouse_key_left("Left"); const std::string g_name_mouse_key_middle("Middle"); const std::string g_name_mouse_key_right("Right"); @@ -43,7 +55,7 @@ const std::string g_name_mouse_axis_ypixel("YPixel"); const std::string g_name_mouse_axis_scroll("Scroll"); const std::string g_name_mouse_cursor("Cursor"); -//Xbox default buttons/axis +// Xbox default buttons/axis const std::string g_name_xbox_key_dpad_up("DPadUp"); const std::string g_name_xbox_key_dpad_down("DPadDown"); const std::string g_name_xbox_key_dpad_left("DPadLeft"); diff --git a/src/input/keys.h b/src/input/keys.inc similarity index 96% rename from src/input/keys.h rename to src/input/keys.inc index de452b38..5fd01328 100644 --- a/src/input/keys.h +++ b/src/input/keys.inc @@ -1,8 +1,8 @@ // // Lol Engine // -// Copyright © 2010—2013 Benjamin Litzelmann -// © 2010—2015 Sam Hocevar +// Copyright © 2010—2019 Sam Hocevar +// © 2010—2013 Benjamin Litzelmann // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -15,7 +15,7 @@ * name and, if possible, a string representation. */ #if !defined _SC -# error scancode.h included without the _SC macro +# error keys.inc included without the _SC macro #endif _SC(0, "", Unknown) @@ -285,7 +285,7 @@ _SC(282, "", Sleep) _SC(283, "", App1) _SC(284, "", App2) -//Custom Lock management +/* Custom Lock management */ _SC(285, "", CapsLockStatus) _SC(286, "", ScrollLockStatus) _SC(287, "", NumLockClearStatus) diff --git a/src/input/sdl-input.cpp b/src/input/sdl-input.cpp index 096f0fed..8925bf54 100644 --- a/src/input/sdl-input.cpp +++ b/src/input/sdl-input.cpp @@ -39,9 +39,6 @@ namespace lol { -#define _SC(id, str, name) static const uint16_t SDLOL_##name = id; -#include "input/keys.h" - //------------------------------------------------------------------------- /* DEBUG STUFF static String ScanCodeToText(int sc) @@ -50,7 +47,7 @@ static String ScanCodeToText(int sc) { #define _SC(id, str, name) \ case id: return String(str); -#include "input/keys.h" +#include "input/keys.inc" default: msg::error("ScanCodeToText unknown scancode %0d\n", sc); } @@ -65,7 +62,7 @@ static String ScanCodeToName(int sc) { #define _SC(id, str, name) \ case id: return String(#name); -#include "input/keys.h" +#include "input/keys.inc" default: msg::error("ScanCodeToText unknown scancode %0d\n", sc); } @@ -95,9 +92,8 @@ SdlInput::SdlInput(int app_w, int app_h, int screen_w, int screen_h) m_keyboard = InputDeviceInternal::CreateStandardKeyboard(); m_mouse = InputDeviceInternal::CreateStandardMouse(); -#if LOL_USE_SDL - // XXX: another option is to properly handle gamepad support -# if !__EMSCRIPTEN__ + // XXX: another option for emscripten is to properly support gamepads +#if LOL_USE_SDL && !__EMSCRIPTEN__ SDL_JoystickEventState(SDL_FORCE_POLL_JOYSTICK ? SDL_QUERY : SDL_ENABLE); /* Register all the joysticks we can find, and let the input @@ -130,7 +126,6 @@ SdlInput::SdlInput(int app_w, int app_h, int screen_w, int screen_h) m_joysticks.push(sdlstick, stick); } -# endif // __EMSCRIPTEN__ #endif m_gamegroup = GAMEGROUP_INPUT; @@ -204,35 +199,33 @@ void SdlInput::tick(float seconds) case SDL_KEYUP: switch (int sc = event.key.keysym.scancode) { - //Lock management - case SDLOL_CapsLock: - case SDLOL_ScrollLock: - case SDLOL_NumLockClear: -# if defined SDLOL_CapsLock && defined SDLOL_ScrollLock && defined SDLOL_NumLockClear - //Update status on key down only + // Lock management + case (int)input::key::SC_CapsLock: + case (int)input::key::SC_ScrollLock: + case (int)input::key::SC_NumLockClear: + // Update status on key down only if (event.type == SDL_KEYDOWN) { int sc2 = sc; switch (sc) { - case SDLOL_CapsLock: - sc2 = SDLOL_CapsLockStatus; + case (int)input::key::SC_CapsLock: + sc2 = (int)input::key::SC_CapsLockStatus; break; - case SDLOL_ScrollLock: - sc2 = SDLOL_ScrollLockStatus; + case (int)input::key::SC_ScrollLock: + sc2 = (int)input::key::SC_ScrollLockStatus; break; - case SDLOL_NumLockClear: - sc2 = SDLOL_NumLockClearStatus; + case (int)input::key::SC_NumLockClear: + sc2 = (int)input::key::SC_NumLockClearStatus; break; } - m_keyboard->internal_set_key(sc2, !m_keyboard->GetKey(sc2)); + m_keyboard->internal_set_key(sc2, !m_keyboard->key((int)sc2)); /* DEBUG STUFF msg::debug("Repeat: 0x%02x : %s/%s/%s/%i\n", (int)m_keyboard, ScanCodeToText(sc2).C(), ScanCodeToName(sc2).C(), m_keyboard->GetKey(sc2) ? "up" : "down", event.key.repeat); */ } -# endif default: // Set key updates the corresponding key m_keyboard->internal_set_key(sc, event.type == SDL_KEYDOWN); diff --git a/src/lol-core.vcxproj b/src/lol-core.vcxproj index 35138b77..c618c966 100644 --- a/src/lol-core.vcxproj +++ b/src/lol-core.vcxproj @@ -208,7 +208,7 @@ - + true diff --git a/src/lol-core.vcxproj.filter b/src/lol-core.vcxproj.filter index 922c2998..80638a63 100644 --- a/src/lol-core.vcxproj.filter +++ b/src/lol-core.vcxproj.filter @@ -666,7 +666,7 @@ lol\image - + input diff --git a/src/lolimgui.cpp b/src/lolimgui.cpp index c59cbf4b..ad4647e6 100644 --- a/src/lolimgui.cpp +++ b/src/lolimgui.cpp @@ -22,6 +22,22 @@ using namespace lol; +namespace +{ + enum key_enum + { + LeftClick = 300, + RightClick, + MiddleClick, + Focus, + }; + + enum axis_enum + { + Scroll, + }; +} + static gui* g_gui = nullptr; //----------------------------------------------------------------------------- @@ -74,27 +90,19 @@ gui::gui(ImFontAtlas *shared_font_atlas) << ShaderProgram::Pixel << imgui_pixel; // Input Setup ------------------------------------------------------------- - InputProfile& ip = m_profile; - ip.AddBindings(InputProfileType::Keyboard); - //for (int i = key_enum::KEY_START; i < key_enum::KEY_END; ++i) - // m_profile << InputProfile::Keyboard(i, key_enum(i).tostring()); - for (int i = key_enum::MOUSE_KEY_START; i < key_enum::MOUSE_KEY_END; ++i) - m_profile << InputProfile::MouseKey(i, key_enum(i).tostring()); + m_profile.register_default_keys(); + + m_profile << InputProfile::MouseKey(key_enum::LeftClick, g_name_mouse_key_left); + m_profile << InputProfile::MouseKey(key_enum::RightClick, g_name_mouse_key_right); + m_profile << InputProfile::MouseKey(key_enum::MiddleClick, g_name_mouse_key_middle); + m_profile << InputProfile::MouseKey(key_enum::Focus, g_name_mouse_key_in_screen); - for (int i = axis_enum::MOUSE_AXIS_START; i < axis_enum::MOUSE_AXIS_END; ++i) - m_profile << InputProfile::MouseAxis(i, axis_enum(i).tostring()); + m_profile << InputProfile::MouseAxis(axis_enum::Scroll, g_name_mouse_axis_scroll); Ticker::Ref(m_controller = new Controller("ImGui_Controller")); m_controller->Init(m_profile); m_mouse = InputDevice::GetMouse(); m_keyboard = InputDevice::GetKeyboard(); - - //m_controller->Get - //# define KB InputProfile::Keyboard -// m_profile -// << InputProfile::Keyboard(idx, g_name_key_Left); -//# undef KB - } gui::~gui() @@ -114,24 +122,25 @@ void gui::init(ImFontAtlas *shared_font_atlas) ImGuiIO& io = ImGui::GetIO(); //ImFont* font0 = io.Fonts->AddFontDefault(); - // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array. - io.KeyMap[ImGuiKey_Tab] = key_enum::Tab; - io.KeyMap[ImGuiKey_LeftArrow] = key_enum::LeftArrow; - io.KeyMap[ImGuiKey_RightArrow] = key_enum::RightArrow; - io.KeyMap[ImGuiKey_UpArrow] = key_enum::UpArrow; - io.KeyMap[ImGuiKey_DownArrow] = key_enum::DownArrow; - io.KeyMap[ImGuiKey_Home] = key_enum::Home; - io.KeyMap[ImGuiKey_End] = key_enum::End; - io.KeyMap[ImGuiKey_Delete] = key_enum::Delete; - io.KeyMap[ImGuiKey_Backspace] = key_enum::Backspace; - io.KeyMap[ImGuiKey_Enter] = key_enum::Enter; - io.KeyMap[ImGuiKey_Escape] = key_enum::Escape; - io.KeyMap[ImGuiKey_A] = key_enum::A; - io.KeyMap[ImGuiKey_C] = key_enum::C; - io.KeyMap[ImGuiKey_V] = key_enum::V; - io.KeyMap[ImGuiKey_X] = key_enum::X; - io.KeyMap[ImGuiKey_Y] = key_enum::Y; - io.KeyMap[ImGuiKey_Z] = key_enum::Z; + // Keyboard mapping; these are the only ones ImGui cares about, the + // rest is just handled by the application. + io.KeyMap[ImGuiKey_Tab] = (int)input::key::SC_Tab; + io.KeyMap[ImGuiKey_LeftArrow] = (int)input::key::SC_Left; + io.KeyMap[ImGuiKey_RightArrow] = (int)input::key::SC_Right; + io.KeyMap[ImGuiKey_UpArrow] = (int)input::key::SC_Up; + io.KeyMap[ImGuiKey_DownArrow] = (int)input::key::SC_Down; + io.KeyMap[ImGuiKey_Home] = (int)input::key::SC_Home; + io.KeyMap[ImGuiKey_End] = (int)input::key::SC_End; + io.KeyMap[ImGuiKey_Delete] = (int)input::key::SC_Delete; + io.KeyMap[ImGuiKey_Backspace] = (int)input::key::SC_Backspace; + io.KeyMap[ImGuiKey_Enter] = (int)input::key::SC_Return; + io.KeyMap[ImGuiKey_Escape] = (int)input::key::SC_Escape; + io.KeyMap[ImGuiKey_A] = (int)input::key::SC_A; + io.KeyMap[ImGuiKey_C] = (int)input::key::SC_C; + io.KeyMap[ImGuiKey_V] = (int)input::key::SC_V; + io.KeyMap[ImGuiKey_X] = (int)input::key::SC_X; + io.KeyMap[ImGuiKey_Y] = (int)input::key::SC_Y; + io.KeyMap[ImGuiKey_Z] = (int)input::key::SC_Z; // Func pointer io.RenderDrawListsFn = gui::static_render_draw_lists; @@ -223,25 +232,13 @@ void gui::tick_game(float seconds) io.MouseDrawCursor = true; // Update Keyboard - io.KeyCtrl = false; - io.KeyShift = false; - for (int i = key_enum::KEY_START; i < key_enum::KEY_END; ++i) - { - switch (i) - { - case key_enum::LShift: - case key_enum::RShift: - io.KeyShift = (io.KeyShift || m_controller->IsKeyPressed(i)); - break; - case key_enum::LCtrl: - case key_enum::RCtrl: - io.KeyCtrl = (io.KeyCtrl || m_controller->IsKeyPressed(i)); - break; - default: - io.KeysDown[i] = m_controller->IsKeyPressed(i); - break; - } - } + io.KeyCtrl = m_controller->IsKeyPressed((int)input::key::SC_LCtrl) + || m_controller->IsKeyPressed((int)input::key::SC_RCtrl); + io.KeyShift = m_controller->IsKeyPressed((int)input::key::SC_LShift) + || m_controller->IsKeyPressed((int)input::key::SC_RShift); + + for (input::key k : input::all_keys()) + io.KeysDown[(int)k] = m_controller->IsKeyPressed((int)k); m_keyboard->SetTextInputActive(io.WantTextInput); @@ -261,26 +258,10 @@ void gui::tick_game(float seconds) //msg::debug("%.2f/%.2f\n", io.MousePos.x, io.MousePos.y); io.MouseWheel = m_controller->GetAxisValue(axis_enum::Scroll); - for (int i = key_enum::MOUSE_KEY_START; i < key_enum::MOUSE_KEY_END; ++i) - { - switch (i) - { - default: - io.MouseDown[i - key_enum::MOUSE_KEY_START] = m_controller->IsKeyPressed(i); - break; - case key_enum::Focus: - if (m_controller->IsKeyReleased(i)) - { - //msg::debug("Not focused .....\n"); - //io.MousePos = vec2(-1.f); - } - else - { - //msg::debug("Focused !!\n"); - } - break; - } - } + io.MouseDown[0] = m_controller->IsKeyPressed(key_enum::LeftClick); + io.MouseDown[1] = m_controller->IsKeyPressed(key_enum::RightClick); + io.MouseDown[2] = m_controller->IsKeyPressed(key_enum::MiddleClick); + // FIXME: handle key_enum::Focus? } // Start the frame diff --git a/src/lolimgui.h b/src/lolimgui.h index 99460efe..2529b64d 100644 --- a/src/lolimgui.h +++ b/src/lolimgui.h @@ -56,103 +56,6 @@ public: private: typedef Entity super; - //ImGuiKeyBase ------------------------------------------------------------ - struct key_base : public StructSafeEnum - { - enum Type - { - KEY_START, - - Tab = KEY_START, - LeftArrow, - RightArrow, - UpArrow, - DownArrow, - Home, - End, - Delete, - Backspace, - Enter, - Escape, - - A, C, V, X, Y, Z, - - LShift, RShift, LCtrl, RCtrl, - - KEY_END, - - MOUSE_KEY_START = KEY_END, - - LeftClick = MOUSE_KEY_START, - RightClick, - MiddleClick, - Focus, - - MOUSE_KEY_END, - - MAX = MOUSE_KEY_END, - }; - - protected: - virtual bool BuildEnumMap(std::map& enum_map) - { - enum_map[Tab] = g_name_key_Tab; - enum_map[LeftArrow] = g_name_key_Left; - enum_map[RightArrow] = g_name_key_Right; - enum_map[UpArrow] = g_name_key_Up; - enum_map[DownArrow] = g_name_key_Down; - enum_map[Home] = g_name_key_Home; - enum_map[End] = g_name_key_End; - enum_map[Delete] = g_name_key_Delete; - enum_map[Backspace] = g_name_key_Backspace; - enum_map[Enter] = g_name_key_Return; - enum_map[Escape] = g_name_key_Escape; - - enum_map[A] = g_name_key_A; - enum_map[C] = g_name_key_C; - enum_map[V] = g_name_key_V; - enum_map[X] = g_name_key_X; - enum_map[Y] = g_name_key_Y; - enum_map[Z] = g_name_key_Z; - - enum_map[LShift] = g_name_key_LShift; - enum_map[RShift] = g_name_key_RShift; - enum_map[LCtrl] = g_name_key_LCtrl; - enum_map[RCtrl] = g_name_key_RCtrl; - - enum_map[LeftClick] = g_name_mouse_key_left; - enum_map[RightClick] = g_name_mouse_key_right; - enum_map[MiddleClick] = g_name_mouse_key_middle; - enum_map[Focus] = g_name_mouse_key_in_screen; - - return true; - } - }; - typedef SafeEnum key_enum; - - //ImGuiKeyBase ------------------------------------------------------------ - struct axis_base : public StructSafeEnum - { - enum Type - { - MOUSE_AXIS_START = 0, - - Scroll = MOUSE_AXIS_START, - - MOUSE_AXIS_END, - - MAX = MOUSE_AXIS_END, - }; - protected: - virtual bool BuildEnumMap(std::map& enum_map) - { - enum_map[Scroll] = g_name_mouse_axis_scroll; - - return true; - } - }; - typedef SafeEnum axis_enum; - protected: virtual void tick_game(float seconds); virtual void tick_draw(float seconds, Scene &scene); @@ -186,7 +89,6 @@ protected: InputDevice* m_mouse = nullptr; InputDevice* m_keyboard = nullptr; InputProfile m_profile; - //std::map m_keys; std::string m_clipboard; class primitive : public PrimitiveRenderer