diff --git a/src/Makefile.am b/src/Makefile.am index 39339cca..a95d173a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -101,7 +101,7 @@ liblol_core_sources = \ \ audio/audio.cpp audio/sample.cpp \ \ - ui/input.cpp ui/input.h ui/keys.inc \ + ui/input.cpp ui/input.h ui/keys.inc ui/buttons.inc \ ui/controller.cpp ui/controller.h \ ui/gui.cpp ui/gui.h \ \ diff --git a/src/lol-core.vcxproj b/src/lol-core.vcxproj index 1a1b189b..84d779cd 100644 --- a/src/lol-core.vcxproj +++ b/src/lol-core.vcxproj @@ -287,6 +287,7 @@ + diff --git a/src/lol-core.vcxproj.filters b/src/lol-core.vcxproj.filters index 34c5712c..4bfc9f72 100644 --- a/src/lol-core.vcxproj.filters +++ b/src/lol-core.vcxproj.filters @@ -521,6 +521,9 @@ + + ui + ui diff --git a/src/ui/buttons.inc b/src/ui/buttons.inc new file mode 100644 index 00000000..14339b12 --- /dev/null +++ b/src/ui/buttons.inc @@ -0,0 +1,42 @@ +// +// Lol Engine +// +// Copyright © 2010—2019 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. +// + +#if !defined _BTN +# error buttons.inc included without the _BTN macro +#endif + +_BTN(0, Unknown) + +// Mouse buttons (FIXME: rename to LeftClick etc.?) +_BTN(1, Left) +_BTN(2, Right) +_BTN(3, Middle) +_BTN(4, Focus) + +// Typical game controller buttons +_BTN(5, A) +_BTN(6, B) +_BTN(7, X) +_BTN(8, Y) +_BTN(9, DpadUp) +_BTN(10, DpadDown) +_BTN(11, DpadLeft) +_BTN(12, DpadRight) +_BTN(13, Start) +_BTN(14, Back) +_BTN(15, LeftThumb) +_BTN(16, RightThumb) +_BTN(17, LeftShoulder) +_BTN(18, RightShoulder) + +#undef _BTN + diff --git a/src/ui/d3d9-input.cpp b/src/ui/d3d9-input.cpp index eb934391..b284dec9 100644 --- a/src/ui/d3d9-input.cpp +++ b/src/ui/d3d9-input.cpp @@ -26,20 +26,6 @@ namespace lol { -static const std::string g_name_xbox_key_dpad_up("DPadUp"); -static const std::string g_name_xbox_key_dpad_down("DPadDown"); -static const std::string g_name_xbox_key_dpad_left("DPadLeft"); -static const std::string g_name_xbox_key_dpad_right("DPadRight"); -static const std::string g_name_xbox_key_left_thumb("LeftThumb"); -static const std::string g_name_xbox_key_right_thumb("RightThumb"); -static const std::string g_name_xbox_key_left_shoulder("LeftShoulder"); -static const std::string g_name_xbox_key_right_shoulder("Rightshoulder"); -static const std::string g_name_xbox_key_a("A"); -static const std::string g_name_xbox_key_b("B"); -static const std::string g_name_xbox_key_x("X"); -static const std::string g_name_xbox_key_y("Y"); -static const std::string g_name_xbox_key_start("Start"); -static const std::string g_name_xbox_key_back("Back"); static const std::string g_name_xbox_axis_left_x("Axis1"); static const std::string g_name_xbox_axis_left_y("Axis2"); static const std::string g_name_xbox_axis_right_x("Axis3"); @@ -84,20 +70,8 @@ D3d9Input::D3d9Input() stick->AddAxis(g_name_xbox_axis_left_trigger.c_str()); stick->AddAxis(g_name_xbox_axis_right_trigger.c_str()); - stick->AddKey(g_name_xbox_key_dpad_up.c_str()); - stick->AddKey(g_name_xbox_key_dpad_down.c_str()); - stick->AddKey(g_name_xbox_key_dpad_left.c_str()); - stick->AddKey(g_name_xbox_key_dpad_right.c_str()); - stick->AddKey(g_name_xbox_key_start.c_str()); - stick->AddKey(g_name_xbox_key_back.c_str()); - stick->AddKey(g_name_xbox_key_left_thumb.c_str()); - stick->AddKey(g_name_xbox_key_right_thumb.c_str()); - stick->AddKey(g_name_xbox_key_left_shoulder.c_str()); - stick->AddKey(g_name_xbox_key_right_shoulder.c_str()); - stick->AddKey(g_name_xbox_key_a.c_str()); - stick->AddKey(g_name_xbox_key_b.c_str()); - stick->AddKey(g_name_xbox_key_x.c_str()); - stick->AddKey(g_name_xbox_key_y.c_str()); + #define _BTN(id, name) stick->internal_add_button(input::button::BTN_##name, #name); + #include "ui/buttons.inc" // FIXME: ignore mouse buttons here m_data->m_joysticks.push(i, stick); } diff --git a/src/ui/input.cpp b/src/ui/input.cpp index ffa2c655..10b0bfd0 100644 --- a/src/ui/input.cpp +++ b/src/ui/input.cpp @@ -42,16 +42,13 @@ input::input() // Create default keyboard device m_keyboard = std::make_shared(g_name_keyboard.c_str()); /* Register all scancodes known to SDL (from the USB standard) */ -# define _SC(id, str, name) m_keyboard->AddKey(id, #name); -# include "ui/keys.inc" + #define _SC(id, str, name) m_keyboard->internal_add_key(input::key::SC_##name, #name); + #include "ui/keys.inc" // Create default mouse device m_mouse = std::make_shared(g_name_mouse.c_str()); - m_mouse->AddButton((int)input::button::BTN_Left, g_name_mouse_key_left.c_str()); - m_mouse->AddButton((int)input::button::BTN_Middle, g_name_mouse_key_middle.c_str()); - m_mouse->AddButton((int)input::button::BTN_Right, g_name_mouse_key_right.c_str()); - m_mouse->AddButton((int)input::button::BTN_Focus, g_name_mouse_key_in_screen.c_str()); - // Added to manage if mouse is in the screen or not. + #define _BTN(id, name) m_mouse->internal_add_button(input::button::BTN_##name, #name); + #include "ui/buttons.inc" // FIXME: this will also add joystick buttons! m_mouse->AddAxis(g_name_mouse_axis_x.c_str()); m_mouse->AddAxis(g_name_mouse_axis_y.c_str()); m_mouse->AddAxis(g_name_mouse_axis_xpixel.c_str()); @@ -116,32 +113,26 @@ void InputDevice::capture_text(bool status) m_input_active = status; } -void InputDevice::AddKey(int index, const char* name) +void InputDevice::internal_add_key(input::key key, const char* name) { - if (index == -1) - index = (int)m_key_names.size(); - - while (index >= (int)m_key_names.size()) + while ((int)key >= (int)m_key_names.size()) { m_key_names.push_back(""); m_keys.push_back(false); } - m_key_names[index] = name; + m_key_names[(int)key] = name; } -void InputDevice::AddButton(int index, const char* name) +void InputDevice::internal_add_button(input::button button, const char* name) { - if (index == -1) - index = (int)m_button_names.size(); - - while (index >= (int)m_button_names.size()) + while ((int)button >= (int)m_button_names.size()) { m_button_names.push_back(""); m_buttons.push_back(false); } - m_button_names[index] = name; + m_button_names[(int)button] = name; } void InputDevice::AddAxis(int index, const char* name, float sensitivity) diff --git a/src/ui/input.h b/src/ui/input.h index 5cedbb7c..86cfe050 100644 --- a/src/ui/input.h +++ b/src/ui/input.h @@ -58,10 +58,8 @@ public: enum class button : uint16_t { - BTN_Left, - BTN_Right, - BTN_Middle, - BTN_Focus, + #define _BTN(id, name) BTN_##name = id, + #include "ui/buttons.inc" }; static std::vector const &all_keys(); @@ -245,19 +243,8 @@ public: /** Internal functions that allow to construct an InputDevice * dynamically, when the keys, axis and cursors are not known at * compile time. */ - void AddKey(int id, char const * name); - - inline void AddKey(char const * name) - { - AddKey(-1, name); - } - - void AddButton(int id, char const * name); - - inline void AddButton(char const * name) - { - AddButton(-1, name); - } + void internal_add_key(input::key, char const * name); + void internal_add_button(input::button, char const * name); void AddAxis(int id, char const * name, float sensitivity = 1.0f); diff --git a/src/ui/sdl-input.cpp b/src/ui/sdl-input.cpp index e80cbd98..58695ef6 100644 --- a/src/ui/sdl-input.cpp +++ b/src/ui/sdl-input.cpp @@ -87,7 +87,7 @@ SdlInput::SdlInput(int app_w, int app_h, int screen_w, int screen_h) for (int j = 0; j < SDL_JoystickNumAxes(sdlstick); ++j) stick->AddAxis(format("Axis%d", j + 1).c_str()); for (int j = 0; j < SDL_JoystickNumButtons(sdlstick); ++j) - stick->AddKey(format("Button%d", j + 1).c_str()); + stick->internal_add_button((input::button)(j + 1), format("Button%d", j + 1).c_str()); m_joysticks.push(sdlstick, stick); } @@ -200,7 +200,8 @@ void SdlInput::tick(float seconds) case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: //event.button.which - mouse->internal_set_button((input::button)(event.button.button - 1), event.type == SDL_MOUSEBUTTONDOWN); + mouse->internal_set_button((input::button)((int)input::button::BTN_Left + event.button.button - 1), + event.type == SDL_MOUSEBUTTONDOWN); break; case SDL_MOUSEWHEEL: mouse->internal_set_axis(4, (float)event.button.y);