1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-27 22:49:25 +02:00

key name capture

This commit is contained in:
Laserlicht
2025-07-19 13:09:15 +02:00
parent 6e61cb0cf3
commit 21df285e5a
10 changed files with 67 additions and 10 deletions

View File

@@ -262,7 +262,6 @@
"vcmi.shortcuts.button.hover" : "Shortcuts", "vcmi.shortcuts.button.hover" : "Shortcuts",
"vcmi.shortcuts.button.help" : "{Shortcuts}\n\nShow menu for viewing and adjusting shortcuts and keybindings", "vcmi.shortcuts.button.help" : "{Shortcuts}\n\nShow menu for viewing and adjusting shortcuts and keybindings",
"vcmi.shortcuts.editButton.help" : "Edit key binding", "vcmi.shortcuts.editButton.help" : "Edit key binding",
"vcmi.shortcuts.editButton.popup" : "You want to change keybinding for {%s}?\n\nSadly setting the key binding here is not supported yet. Bindings has to configured over {shortcutsConfig.json} in config folder. You need to add the key {%s}.",
"vcmi.shortcuts.group.keyboard" : "Keyboard", "vcmi.shortcuts.group.keyboard" : "Keyboard",
"vcmi.shortcuts.group.joystickAxes" : "Joystick Axes", "vcmi.shortcuts.group.joystickAxes" : "Joystick Axes",
"vcmi.shortcuts.group.joystickButtons" : "Joystick Buttons", "vcmi.shortcuts.group.joystickButtons" : "Joystick Buttons",

View File

@@ -262,7 +262,6 @@
"vcmi.shortcuts.button.hover" : "Tastenkürzel", "vcmi.shortcuts.button.hover" : "Tastenkürzel",
"vcmi.shortcuts.button.help" : "{Tastenkürzel}\n\nMenü zum Anzeigen und Anpassen von Tastenkürzeln und Tastenbelegungen anzeigen", "vcmi.shortcuts.button.help" : "{Tastenkürzel}\n\nMenü zum Anzeigen und Anpassen von Tastenkürzeln und Tastenbelegungen anzeigen",
"vcmi.shortcuts.editButton.help" : "Tastenbelegung bearbeiten", "vcmi.shortcuts.editButton.help" : "Tastenbelegung bearbeiten",
"vcmi.shortcuts.editButton.popup" : "Möchten Sie die Tastenbelegung für {%s} ändern?\n\nLeider wird das Festlegen der Tastenbelegung hier noch nicht unterstützt. Die Belegung muss über die {shortcutsConfig.json} im Konfigurationsordner eingestellt werden. Sie müssen den Schlüssel {%s} hinzufügen.",
"vcmi.shortcuts.group.keyboard" : "Tastatur", "vcmi.shortcuts.group.keyboard" : "Tastatur",
"vcmi.shortcuts.group.joystickAxes" : "Joystick-Achsen", "vcmi.shortcuts.group.joystickAxes" : "Joystick-Achsen",
"vcmi.shortcuts.group.joystickButtons" : "Joystick-Tasten", "vcmi.shortcuts.group.joystickButtons" : "Joystick-Tasten",

View File

@@ -140,12 +140,13 @@ double InputSourceGameController::getRealAxisValue(int value) const
return clampedValue; return clampedValue;
} }
void InputSourceGameController::dispatchAxisShortcuts(const std::vector<EShortcut> & shortcutsVector, SDL_GameControllerAxis axisID, int axisValue) void InputSourceGameController::dispatchAxisShortcuts(const std::vector<EShortcut> & shortcutsVector, SDL_GameControllerAxis axisID, int axisValue, std::string axisName)
{ {
if(getRealAxisValue(axisValue) > configTriggerThreshold) if(getRealAxisValue(axisValue) > configTriggerThreshold)
{ {
if(!pressedAxes.count(axisID)) if(!pressedAxes.count(axisID))
{ {
ENGINE->events().dispatchKeyPressed(axisName);
ENGINE->events().dispatchShortcutPressed(shortcutsVector); ENGINE->events().dispatchShortcutPressed(shortcutsVector);
pressedAxes.insert(axisID); pressedAxes.insert(axisID);
} }
@@ -154,6 +155,7 @@ void InputSourceGameController::dispatchAxisShortcuts(const std::vector<EShortcu
{ {
if(pressedAxes.count(axisID)) if(pressedAxes.count(axisID))
{ {
ENGINE->events().dispatchKeyReleased(axisName);
ENGINE->events().dispatchShortcutReleased(shortcutsVector); ENGINE->events().dispatchShortcutReleased(shortcutsVector);
pressedAxes.erase(axisID); pressedAxes.erase(axisID);
} }
@@ -189,7 +191,7 @@ void InputSourceGameController::handleEventAxisMotion(const SDL_ControllerAxisEv
} }
} }
dispatchAxisShortcuts(buttonActions, axisID, axis.value); dispatchAxisShortcuts(buttonActions, axisID, axis.value, axisName);
} }
void InputSourceGameController::tryToConvertCursor() void InputSourceGameController::tryToConvertCursor()
@@ -208,6 +210,8 @@ void InputSourceGameController::handleEventButtonDown(const SDL_ControllerButton
{ {
std::string buttonName = SDL_GameControllerGetStringForButton(static_cast<SDL_GameControllerButton>(button.button)); std::string buttonName = SDL_GameControllerGetStringForButton(static_cast<SDL_GameControllerButton>(button.button));
const auto & shortcutsVector = ENGINE->shortcuts().translateJoystickButton(buttonName); const auto & shortcutsVector = ENGINE->shortcuts().translateJoystickButton(buttonName);
ENGINE->events().dispatchKeyPressed(buttonName);
ENGINE->events().dispatchShortcutPressed(shortcutsVector); ENGINE->events().dispatchShortcutPressed(shortcutsVector);
} }
@@ -215,6 +219,7 @@ void InputSourceGameController::handleEventButtonUp(const SDL_ControllerButtonEv
{ {
std::string buttonName = SDL_GameControllerGetStringForButton(static_cast<SDL_GameControllerButton>(button.button)); std::string buttonName = SDL_GameControllerGetStringForButton(static_cast<SDL_GameControllerButton>(button.button));
const auto & shortcutsVector = ENGINE->shortcuts().translateJoystickButton(buttonName); const auto & shortcutsVector = ENGINE->shortcuts().translateJoystickButton(buttonName);
ENGINE->events().dispatchKeyReleased(buttonName);
ENGINE->events().dispatchShortcutReleased(shortcutsVector); ENGINE->events().dispatchShortcutReleased(shortcutsVector);
} }

View File

@@ -48,7 +48,7 @@ class InputSourceGameController
void openGameController(int index); void openGameController(int index);
int getJoystickIndex(SDL_GameController * controller); int getJoystickIndex(SDL_GameController * controller);
double getRealAxisValue(int value) const; double getRealAxisValue(int value) const;
void dispatchAxisShortcuts(const std::vector<EShortcut> & shortcutsVector, SDL_GameControllerAxis axisID, int axisValue); void dispatchAxisShortcuts(const std::vector<EShortcut> & shortcutsVector, SDL_GameControllerAxis axisID, int axisValue, std::string axisName);
void tryToConvertCursor(); void tryToConvertCursor();
void doCursorMove(int deltaX, int deltaY); void doCursorMove(int deltaX, int deltaY);
int getMoveDis(float planDis); int getMoveDis(float planDis);

View File

@@ -88,6 +88,8 @@ void InputSourceKeyboard::handleEventKeyDown(const SDL_KeyboardEvent & key)
auto shortcutsVector = ENGINE->shortcuts().translateKeycode(keyName); auto shortcutsVector = ENGINE->shortcuts().translateKeycode(keyName);
ENGINE->events().dispatchKeyPressed(keyName);
if (vstd::contains(shortcutsVector, EShortcut::MAIN_MENU_LOBBY)) if (vstd::contains(shortcutsVector, EShortcut::MAIN_MENU_LOBBY))
ENGINE->user().onGlobalLobbyInterfaceActivated(); ENGINE->user().onGlobalLobbyInterfaceActivated();
@@ -143,6 +145,8 @@ void InputSourceKeyboard::handleEventKeyUp(const SDL_KeyboardEvent & key)
auto shortcutsVector = ENGINE->shortcuts().translateKeycode(keyName); auto shortcutsVector = ENGINE->shortcuts().translateKeycode(keyName);
ENGINE->events().dispatchKeyReleased(keyName);
ENGINE->events().dispatchShortcutReleased(shortcutsVector); ENGINE->events().dispatchShortcutReleased(shortcutsVector);
} }

View File

@@ -43,6 +43,7 @@ void EventDispatcher::processLists(ui16 activityFlag, const Functor & cb)
processList(AEventsReceiver::TEXTINPUT, textInterested); processList(AEventsReceiver::TEXTINPUT, textInterested);
processList(AEventsReceiver::GESTURE, panningInterested); processList(AEventsReceiver::GESTURE, panningInterested);
processList(AEventsReceiver::INPUT_MODE_CHANGE, inputModeChangeInterested); processList(AEventsReceiver::INPUT_MODE_CHANGE, inputModeChangeInterested);
processList(AEventsReceiver::KEY_NAME, keyNameInterested);
} }
void EventDispatcher::activateElement(AEventsReceiver * elem, ui16 activityFlag) void EventDispatcher::activateElement(AEventsReceiver * elem, ui16 activityFlag)
@@ -133,6 +134,22 @@ void EventDispatcher::dispatchShortcutReleased(const std::vector<EShortcut> & sh
} }
} }
void EventDispatcher::dispatchKeyPressed(const std::string & keyName)
{
EventReceiversList miCopy = keyNameInterested;
for(auto & i : miCopy)
i->keyPressed(keyName);
}
void EventDispatcher::dispatchKeyReleased(const std::string & keyName)
{
EventReceiversList miCopy = keyNameInterested;
for(auto & i : miCopy)
i->keyReleased(keyName);
}
void EventDispatcher::dispatchMouseDoubleClick(const Point & position, int tolerance) void EventDispatcher::dispatchMouseDoubleClick(const Point & position, int tolerance)
{ {
handleDoubleButtonClick(position, tolerance); handleDoubleButtonClick(position, tolerance);

View File

@@ -37,6 +37,7 @@ class EventDispatcher
EventReceiversList textInterested; EventReceiversList textInterested;
EventReceiversList panningInterested; EventReceiversList panningInterested;
EventReceiversList inputModeChangeInterested; EventReceiversList inputModeChangeInterested;
EventReceiversList keyNameInterested;
void handleLeftButtonClick(const Point & position, int tolerance, bool isPressed); void handleLeftButtonClick(const Point & position, int tolerance, bool isPressed);
void handleDoubleButtonClick(const Point & position, int tolerance); void handleDoubleButtonClick(const Point & position, int tolerance);
@@ -59,6 +60,10 @@ public:
void dispatchShortcutPressed(const std::vector<EShortcut> & shortcuts); void dispatchShortcutPressed(const std::vector<EShortcut> & shortcuts);
void dispatchShortcutReleased(const std::vector<EShortcut> & shortcuts); void dispatchShortcutReleased(const std::vector<EShortcut> & shortcuts);
/// Key events (to get keyname of pressed key)
void dispatchKeyPressed(const std::string & keyName);
void dispatchKeyReleased(const std::string & keyName);
/// Mouse events /// Mouse events
void dispatchMouseLeftButtonPressed(const Point & position, int tolerance); void dispatchMouseLeftButtonPressed(const Point & position, int tolerance);
void dispatchMouseLeftButtonReleased(const Point & position, int tolerance); void dispatchMouseLeftButtonReleased(const Point & position, int tolerance);

View File

@@ -77,6 +77,9 @@ public:
virtual void keyPressed(EShortcut key) {} virtual void keyPressed(EShortcut key) {}
virtual void keyReleased(EShortcut key) {} virtual void keyReleased(EShortcut key) {}
virtual void keyPressed(const std::string & keyName) {}
virtual void keyReleased(const std::string & keyName) {}
virtual void tick(uint32_t msPassed) {} virtual void tick(uint32_t msPassed) {}
virtual void inputModeChanged(InputMode modi) {} virtual void inputModeChanged(InputMode modi) {}
@@ -101,7 +104,8 @@ public:
GESTURE = 1024, GESTURE = 1024,
DRAG = 2048, DRAG = 2048,
INPUT_MODE_CHANGE = 4096, INPUT_MODE_CHANGE = 4096,
DRAG_POPUP = 8192 DRAG_POPUP = 8192,
KEY_NAME = 16384
}; };
/// Returns true if element is currently hovered by mouse /// Returns true if element is currently hovered by mouse

View File

@@ -11,6 +11,7 @@
#include "StdInc.h" #include "StdInc.h"
#include "ShortcutsWindow.h" #include "ShortcutsWindow.h"
#include "../../GameEngine.h"
#include "../../gui/Shortcut.h" #include "../../gui/Shortcut.h"
#include "../../gui/WindowHandler.h" #include "../../gui/WindowHandler.h"
#include "../../widgets/Buttons.h" #include "../../widgets/Buttons.h"
@@ -110,10 +111,7 @@ ShortcutElement::ShortcutElement(std::string id, JsonNode keys, int elem)
buttonEdit = std::make_shared<CButton>(Point(422, 3), AnimationPath::builtin("settingsWindow/button32"), std::make_pair("", MetaString::createFromTextID("vcmi.shortcuts.editButton.help").toString())); buttonEdit = std::make_shared<CButton>(Point(422, 3), AnimationPath::builtin("settingsWindow/button32"), std::make_pair("", MetaString::createFromTextID("vcmi.shortcuts.editButton.help").toString()));
buttonEdit->setOverlay(std::make_shared<CPicture>(ImagePath::builtin("settingsWindow/gear"))); buttonEdit->setOverlay(std::make_shared<CPicture>(ImagePath::builtin("settingsWindow/gear")));
buttonEdit->addCallback([id](){ buttonEdit->addCallback([id](){
auto str = MetaString::createFromTextID("vcmi.shortcuts.editButton.popup"); ENGINE->windows().createAndPushWindow<ShortcutsEditWindow>(id);
str.replaceTextID("vcmi.shortcuts.shortcut." + id);
str.replaceRawString(id);
CInfoWindow::showInfoDialog(str.toString(), {});
}); });
if(elem < MAX_LINES - 1) if(elem < MAX_LINES - 1)
seperationLine = std::make_shared<TransparentFilledRectangle>(Rect(0, LINE_HEIGHT, 456, 1), ColorRGBA(0, 0, 0, 64), ColorRGBA(128, 100, 75), 1); seperationLine = std::make_shared<TransparentFilledRectangle>(Rect(0, LINE_HEIGHT, 456, 1), ColorRGBA(0, 0, 0, 64), ColorRGBA(128, 100, 75), 1);
@@ -133,3 +131,21 @@ ShortcutElement::ShortcutElement(std::string group, int elem)
if(elem < MAX_LINES - 1) if(elem < MAX_LINES - 1)
seperationLine = std::make_shared<TransparentFilledRectangle>(Rect(0, LINE_HEIGHT, 456, 1), ColorRGBA(0, 0, 0, 64), ColorRGBA(128, 100, 75), 1); seperationLine = std::make_shared<TransparentFilledRectangle>(Rect(0, LINE_HEIGHT, 456, 1), ColorRGBA(0, 0, 0, 64), ColorRGBA(128, 100, 75), 1);
} }
ShortcutsEditWindow::ShortcutsEditWindow(const std::string & id)
: CWindowObject(BORDERED)
{
OBJECT_CONSTRUCTION;
pos.w = 200;
pos.h = 100;
updateShadow();
center();
addUsedEvents(KEY_NAME);
}
void ShortcutsEditWindow::keyPressed(const std::string & keyName)
{
std::cout << keyName << "\n";
}

View File

@@ -53,3 +53,11 @@ public:
ShortcutsWindow(); ShortcutsWindow();
}; };
class ShortcutsEditWindow : public CWindowObject
{
private:
void keyPressed(const std::string & keyName) override;
public:
ShortcutsEditWindow(const std::string & id);
};