1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00
This commit is contained in:
y5nw 2025-06-01 11:14:04 +02:00
parent c58814e703
commit 97c34ecd22
8 changed files with 68 additions and 84 deletions

View file

@ -88,6 +88,17 @@ enum EEVENT_TYPE
}; };
//! Enumeration for "user" event types.
enum UserEventType: s32
{
//! In-game touch buttons.
/** This event is currently only fired by the in-game touch controller.
UserData1: The GameKeyType of the button.
UserData2: Whether the button is pressed or released.
*/
USER_EVENT_GAME_KEY = 1
};
//! Enumeration for all mouse input events //! Enumeration for all mouse input events
enum EMOUSE_INPUT_EVENT enum EMOUSE_INPUT_EVENT
{ {
@ -512,6 +523,9 @@ struct SEvent
//! Any kind of user event. //! Any kind of user event.
struct SUserEvent struct SUserEvent
{ {
//! Event code
s32 code;
//! Some user specified data as int //! Some user specified data as int
size_t UserData1; size_t UserData1;

View file

@ -914,6 +914,7 @@ bool CIrrDeviceLinux::run()
} else { } else {
// we assume it's a user message // we assume it's a user message
irrevent.EventType = irr::EET_USER_EVENT; irrevent.EventType = irr::EET_USER_EVENT;
irrevent.UserEvent.code = 0;
irrevent.UserEvent.UserData1 = static_cast<size_t>(event.xclient.data.l[0]); irrevent.UserEvent.UserData1 = static_cast<size_t>(event.xclient.data.l[0]);
irrevent.UserEvent.UserData2 = static_cast<size_t>(event.xclient.data.l[1]); irrevent.UserEvent.UserData2 = static_cast<size_t>(event.xclient.data.l[1]);
postEventFromUser(irrevent); postEventFromUser(irrevent);

View file

@ -947,6 +947,7 @@ bool CIrrDeviceSDL::run()
case SDL_USEREVENT: case SDL_USEREVENT:
irrevent.EventType = irr::EET_USER_EVENT; irrevent.EventType = irr::EET_USER_EVENT;
irrevent.UserEvent.code = SDL_event.user.code;
irrevent.UserEvent.UserData1 = reinterpret_cast<uintptr_t>(SDL_event.user.data1); irrevent.UserEvent.UserData1 = reinterpret_cast<uintptr_t>(SDL_event.user.data1);
irrevent.UserEvent.UserData2 = reinterpret_cast<uintptr_t>(SDL_event.user.data2); irrevent.UserEvent.UserData2 = reinterpret_cast<uintptr_t>(SDL_event.user.data2);

View file

@ -678,6 +678,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case WM_USER: case WM_USER:
event.EventType = irr::EET_USER_EVENT; event.EventType = irr::EET_USER_EVENT;
event.UserEvent.code = 0;
event.UserEvent.UserData1 = static_cast<size_t>(wParam); event.UserEvent.UserData1 = static_cast<size_t>(wParam);
event.UserEvent.UserData2 = static_cast<size_t>(lParam); event.UserEvent.UserData2 = static_cast<size_t>(lParam);
dev = getDeviceFromHWnd(hWnd); dev = getDeviceFromHWnd(hWnd);

View file

@ -76,9 +76,11 @@ void MyEventReceiver::reloadKeybindings()
// First clear all keys, then re-add the ones we listen for // First clear all keys, then re-add the ones we listen for
keysListenedFor.clear(); keysListenedFor.clear();
for (int i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) { for (int i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) {
GameKeyType game_key = static_cast<GameKeyType>(i);
for (auto key: keybindings[i]) { for (auto key: keybindings[i]) {
listenForKey(key, static_cast<GameKeyType>(i)); listenForKey(key, game_key);
} }
listenForKey(game_key, game_key);
} }
} }
@ -217,6 +219,10 @@ bool MyEventReceiver::OnEvent(const SEvent &event)
default: default:
break; break;
} }
} else if (event.EventType == irr::EET_USER_EVENT && event.UserEvent.code == irr::USER_EVENT_GAME_KEY) {
KeyPress keyCode(static_cast<GameKeyType>(event.UserEvent.UserData1));
setKeyDown(keyCode, event.UserEvent.UserData2);
return true;
} }
// tell Irrlicht to continue processing this event // tell Irrlicht to continue processing this event

View file

@ -30,6 +30,8 @@ public:
KeyPress(const irr::SEvent::SKeyInput &in); KeyPress(const irr::SEvent::SKeyInput &in);
KeyPress(GameKeyType key) : scancode(key) {}
// Get a string representation that is suitable for use in minetest.conf // Get a string representation that is suitable for use in minetest.conf
std::string sym() const; std::string sym() const;

View file

@ -31,16 +31,13 @@
TouchControls *g_touchcontrols; TouchControls *g_touchcontrols;
void TouchControls::emitKeyboardEvent(KeyPress key, bool pressed) void TouchControls::emitGameKeyEvent(GameKeyType key, bool pressed)
{ {
SEvent e{}; SEvent e{};
e.EventType = EET_KEY_INPUT_EVENT; e.EventType = EET_USER_EVENT;
e.KeyInput.Key = key.getKeycode(); e.UserEvent.code = irr::USER_EVENT_GAME_KEY;
e.KeyInput.Control = false; e.UserEvent.UserData1 = static_cast<size_t>(key);
e.KeyInput.Shift = false; e.UserEvent.UserData2 = pressed;
e.KeyInput.Char = key.getKeychar();
e.KeyInput.SystemKeyCode = key.getScancode();
e.KeyInput.PressedDown = pressed;
m_receiver->OnEvent(e); m_receiver->OnEvent(e);
} }
@ -55,10 +52,10 @@ void TouchControls::loadButtonTexture(IGUIImage *gui_button, const std::string &
void TouchControls::buttonEmitAction(button_info &btn, bool action) void TouchControls::buttonEmitAction(button_info &btn, bool action)
{ {
if (!btn.keypress) if (!btn.game_key)
return; return;
emitKeyboardEvent(btn.keypress, action); emitGameKeyEvent(btn.game_key, action);
if (action) { if (action) {
if (btn.toggleable == button_info::FIRST_TEXTURE) { if (btn.toggleable == button_info::FIRST_TEXTURE) {
@ -142,81 +139,48 @@ bool TouchControls::buttonsStep(std::vector<button_info> &buttons, float dtime)
return has_pointers; return has_pointers;
} }
static std::string id_to_setting(touch_gui_button_id id) static GameKeyType id_to_action(touch_gui_button_id id)
{ {
std::string key = "";
switch (id) { switch (id) {
case exit_id:
return KeyType::ESC;
case dig_id: case dig_id:
key = "dig"; return KeyType::DIG;
break;
case place_id: case place_id:
key = "place"; return KeyType::PLACE;
break;
case jump_id: case jump_id:
key = "jump"; return KeyType::JUMP;
break;
case sneak_id: case sneak_id:
key = "sneak"; return KeyType::SNEAK;
break;
case zoom_id: case zoom_id:
key = "zoom"; return KeyType::ZOOM;
break;
case aux1_id: case aux1_id:
key = "aux1"; return KeyType::AUX1;
break;
case fly_id: case fly_id:
key = "freemove"; return KeyType::FREEMOVE;
break;
case noclip_id: case noclip_id:
key = "noclip"; return KeyType::NOCLIP;
break;
case fast_id: case fast_id:
key = "fastmove"; return KeyType::FASTMOVE;
break;
case debug_id: case debug_id:
key = "toggle_debug"; return KeyType::TOGGLE_DEBUG;
break;
case camera_id: case camera_id:
key = "camera_mode"; return KeyType::CAMERA_MODE;
break;
case range_id: case range_id:
key = "rangeselect"; return KeyType::RANGESELECT;
break;
case minimap_id: case minimap_id:
key = "minimap"; return KeyType::MINIMAP;
break;
case toggle_chat_id: case toggle_chat_id:
key = "toggle_chat"; return KeyType::TOGGLE_CHAT;
break;
case chat_id: case chat_id:
key = "chat"; return KeyType::CHAT;
break;
case inventory_id: case inventory_id:
key = "inventory"; return KeyType::INVENTORY;
break;
case drop_id: case drop_id:
key = "drop"; return KeyType::DROP;
break;
default: default:
break; return KeyType::INTERNAL_ENUM_COUNT;
} }
return key.empty() ? key : "keymap_" + key;
}
static KeyPress id_to_keypress(touch_gui_button_id id)
{
// ESC isn't part of the keymap.
if (id == exit_id)
return EscapeKey;
auto setting_name = id_to_setting(id);
assert(!setting_name.empty());
const auto &keylist = getKeySetting(setting_name);
if (keylist.empty())
warningstream << "TouchControls: Unbound or invalid key for "
<< setting_name << ", hiding button." << std::endl;
return keylist[0];
} }
@ -239,11 +203,6 @@ TouchControls::TouchControls(IrrlichtDevice *device, ISimpleTextureSource *tsrc)
readSettings(); readSettings();
for (auto name : setting_names) for (auto name : setting_names)
g_settings->registerChangedCallback(name, settingChangedCallback, this); g_settings->registerChangedCallback(name, settingChangedCallback, this);
// Also update layout when keybindings change (e.g. for convertibles)
for (u8 id = 0; id < touch_gui_button_id_END; id++)
if (auto name = id_to_setting((touch_gui_button_id)id); !name.empty())
g_settings->registerChangedCallback(name, settingChangedCallback, this);
} }
void TouchControls::settingChangedCallback(const std::string &name, void *data) void TouchControls::settingChangedCallback(const std::string &name, void *data)
@ -376,7 +335,7 @@ bool TouchControls::mayAddButton(touch_gui_button_id id)
assert(ButtonLayout::isButtonValid(id)); assert(ButtonLayout::isButtonValid(id));
assert(ButtonLayout::isButtonAllowed(id)); assert(ButtonLayout::isButtonAllowed(id));
// The overflow button doesn't need a keycode to be valid. // The overflow button doesn't need a keycode to be valid.
return id == overflow_id || id_to_keypress(id); return id == overflow_id || id_to_action(id) >= KeyType::INTERNAL_ENUM_COUNT;
} }
void TouchControls::addButton(std::vector<button_info> &buttons, touch_gui_button_id id, void TouchControls::addButton(std::vector<button_info> &buttons, touch_gui_button_id id,
@ -388,7 +347,7 @@ void TouchControls::addButton(std::vector<button_info> &buttons, touch_gui_butto
button_info &btn = buttons.emplace_back(); button_info &btn = buttons.emplace_back();
btn.id = id; btn.id = id;
btn.keypress = id_to_keypress(id); btn.game_key = id_to_action(id);
btn.gui_button = grab_gui_element<IGUIImage>(btn_gui_button); btn.gui_button = grab_gui_element<IGUIImage>(btn_gui_button);
} }
@ -655,10 +614,10 @@ void TouchControls::translateEvent(const SEvent &event)
void TouchControls::applyJoystickStatus() void TouchControls::applyJoystickStatus()
{ {
if (m_joystick_triggers_aux1) { if (m_joystick_triggers_aux1) {
auto key = id_to_keypress(aux1_id); auto key = id_to_action(aux1_id);
emitKeyboardEvent(key, false); emitGameKeyEvent(key, false);
if (m_joystick_status_aux1) if (m_joystick_status_aux1)
emitKeyboardEvent(key, true); emitGameKeyEvent(key, true);
} }
} }
@ -764,11 +723,11 @@ void TouchControls::releaseAll()
// Release those manually too since the change initiated by // Release those manually too since the change initiated by
// handleReleaseEvent will only be applied later by applyContextControls. // handleReleaseEvent will only be applied later by applyContextControls.
if (m_dig_pressed) { if (m_dig_pressed) {
emitKeyboardEvent(id_to_keypress(dig_id), false); emitGameKeyEvent(id_to_action(dig_id), false);
m_dig_pressed = false; m_dig_pressed = false;
} }
if (m_place_pressed) { if (m_place_pressed) {
emitKeyboardEvent(id_to_keypress(place_id), false); emitGameKeyEvent(id_to_action(place_id), false);
m_place_pressed = false; m_place_pressed = false;
} }
} }
@ -852,20 +811,20 @@ void TouchControls::applyContextControls(const TouchInteractionMode &mode)
target_place_pressed |= now < m_place_pressed_until; target_place_pressed |= now < m_place_pressed_until;
if (target_dig_pressed && !m_dig_pressed) { if (target_dig_pressed && !m_dig_pressed) {
emitKeyboardEvent(id_to_keypress(dig_id), true); emitGameKeyEvent(id_to_action(dig_id), true);
m_dig_pressed = true; m_dig_pressed = true;
} else if (!target_dig_pressed && m_dig_pressed) { } else if (!target_dig_pressed && m_dig_pressed) {
emitKeyboardEvent(id_to_keypress(dig_id), false); emitGameKeyEvent(id_to_action(dig_id), false);
m_dig_pressed = false; m_dig_pressed = false;
} }
if (target_place_pressed && !m_place_pressed) { if (target_place_pressed && !m_place_pressed) {
emitKeyboardEvent(id_to_keypress(place_id), true); emitGameKeyEvent(id_to_action(place_id), true);
m_place_pressed = true; m_place_pressed = true;
} else if (!target_place_pressed && m_place_pressed) { } else if (!target_place_pressed && m_place_pressed) {
emitKeyboardEvent(id_to_keypress(place_id), false); emitGameKeyEvent(id_to_action(place_id), false);
m_place_pressed = false; m_place_pressed = false;
} }
} }

View file

@ -16,7 +16,7 @@
#include "itemdef.h" #include "itemdef.h"
#include "touchscreenlayout.h" #include "touchscreenlayout.h"
#include "util/basic_macros.h" #include "util/basic_macros.h"
#include "client/keycode.h" #include "client/keys.h"
namespace irr namespace irr
{ {
@ -59,7 +59,7 @@ struct button_info
{ {
touch_gui_button_id id; touch_gui_button_id id;
float repeat_counter; float repeat_counter;
KeyPress keypress; GameKeyType game_key;
std::vector<size_t> pointer_ids; std::vector<size_t> pointer_ids;
std::shared_ptr<IGUIImage> gui_button = nullptr; std::shared_ptr<IGUIImage> gui_button = nullptr;
@ -204,7 +204,7 @@ private:
// for its buttons. We only want static image display, not interactivity, // for its buttons. We only want static image display, not interactivity,
// from Irrlicht. // from Irrlicht.
void emitKeyboardEvent(KeyPress keycode, bool pressed); void emitGameKeyEvent(GameKeyType, bool pressed);
void loadButtonTexture(IGUIImage *gui_button, const std::string &path); void loadButtonTexture(IGUIImage *gui_button, const std::string &path);
void buttonEmitAction(button_info &btn, bool action); void buttonEmitAction(button_info &btn, bool action);