Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cont/LuaUI/callins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ CallInsList = {
"MiniMapGeometryChanged",
"CommandNotify",

"KeyBindingsChanged",
"KeyMapChanged",
"KeyPress",
"KeyRelease",
Expand Down
7 changes: 7 additions & 0 deletions cont/LuaUI/widgets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ local callInLists = {
'AddConsoleLine',
'ViewResize',
'DrawScreen',
'KeyBindingsChanged',
'KeyMapChanged',
'KeyPress',
'KeyRelease',
Expand Down Expand Up @@ -1416,6 +1417,12 @@ end
-- Keyboard call-ins
--

function widgetHandler:KeyBindingsChanged()
for _,w in ipairs(self.KeyBindingsChangedList) do
w:KeyBindingsChanged()
end
end

function widgetHandler:KeyMapChanged()
for _,w in ipairs(self.KeyMapChangedList) do
w:KeyMapChanged()
Expand Down
14 changes: 10 additions & 4 deletions rts/Game/UI/KeyBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "KeySet.h"
#include "Sim/Units/UnitDef.h"
#include "Sim/Units/UnitDefHandler.h"
#include "System/EventHandler.h"
#include "System/FileSystem/FileHandler.h"
#include "System/FileSystem/SimpleParser.h"
#include "System/Log/ILog.h"
Expand Down Expand Up @@ -858,7 +859,7 @@ void CKeyBindings::PushAction(const Action& action)
}
}

bool CKeyBindings::ExecuteCommand(const std::string& line)
bool CKeyBindings::ExecuteCommand(const std::string& line, bool sendEvents)
{
RECOIL_DETAILED_TRACY_ZONE;
const std::vector<std::string> words = CSimpleParser::Tokenize(line, 2);
Expand All @@ -867,6 +868,7 @@ bool CKeyBindings::ExecuteCommand(const std::string& line)
return false;

const std::string command = StringToLower(words[0]);
bool changedKeys = true;

if (command == "keydebug") {
if (words.size() == 1) {
Expand All @@ -876,6 +878,7 @@ bool CKeyBindings::ExecuteCommand(const std::string& line)
// set
debugEnabled = atoi(words[1].c_str());
}
changedKeys = false; // keydebug only toggles debug logging; it never changes bindings
}
else if (command == "keyload") {
const std::string& filename = words.size() > 1 ? words[1] : DEFAULT_FILENAME;
Expand All @@ -895,8 +898,8 @@ bool CKeyBindings::ExecuteCommand(const std::string& line)
if (debugEnabled)
LOG("[CKeyBindings::%s] line=%s", __func__, line.c_str());

ExecuteCommand("unbindall");
ExecuteCommand("unbind enter chat");
ExecuteCommand("unbindall", false);
ExecuteCommand("unbind enter chat", false);

if (loadStack.empty() && words.size() == 1)
LoadDefaults();
Expand Down Expand Up @@ -942,6 +945,9 @@ bool CKeyBindings::ExecuteCommand(const std::string& line)
if (buildHotkeyMap)
BuildHotkeyMap();

if (changedKeys && sendEvents)
eventHandler.KeyBindingsChanged();

return false;
}

Expand Down Expand Up @@ -973,7 +979,7 @@ bool CKeyBindings::Load(const std::string& filename)
CSimpleParser parser(ifs);

while (!parser.Eof()) {
ExecuteCommand(parser.GetCleanLine());
ExecuteCommand(parser.GetCleanLine(), false);
}

loadStack.pop_back();
Expand Down
2 changes: 1 addition & 1 deletion rts/Game/UI/KeyBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CKeyBindings : public CommandReceiver
const HotkeyList& GetHotkeys(const std::string& action) const;

virtual void PushAction(const Action&);
bool ExecuteCommand(const std::string& line);
bool ExecuteCommand(const std::string& line, bool sendEvents = true);

// Receive configuration notifications (for KeyChainTimeout)
void ConfigNotify(const std::string& key, const std::string& value);
Expand Down
25 changes: 25 additions & 0 deletions rts/Lua/LuaHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3103,6 +3103,31 @@ void CLuaHandle::Pong(uint8_t pingTag, const spring_time pktSendTime, const spri
RunCallIn(L, cmdStr, 3, 0);
}

/*** Called when keybindings change.
*
* Called when:
*
* - An operation that changed current keybindings occurred, e.g. `bind k action`. If the operation operated on multiple keybindings, just a single event is called, at the end of it, e.g. `keyreload`.
* - Any operation that changes how actions are retrieved from input triggers happened, e.g. `fakemeta space`.
*
* Nothing is passed; call `Spring.GetKeyBindings` to read the current state.
Comment thread
burnhamrobertp marked this conversation as resolved.
*
* @function Callins:KeyBindingsChanged
*/
void CLuaHandle::KeyBindingsChanged()
{
RECOIL_DETAILED_TRACY_ZONE;
LUA_CALL_IN_CHECK(L);
luaL_checkstack(L, 2, __func__);

static const LuaHashString cmdStr(__func__);

if (!cmdStr.GetGlobalFunc(L))
return;

RunCallIn(L, cmdStr, 0, 0);
}


/*** Called when the keymap changes
*
Expand Down
1 change: 1 addition & 0 deletions rts/Lua/LuaHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ class CLuaHandle : public CEventClient
void UnsyncedHeightMapUpdate(const SRectangle& rect) override;
void Update() override;

void KeyBindingsChanged() override;
bool KeyMapChanged() override;
bool KeyPress(int keyCode, int scanCode, bool isRepeat) override;
bool KeyRelease(int keyCode, int scanCode) override;
Expand Down
1 change: 1 addition & 0 deletions rts/System/EventClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void CEventClient::DrawLoadScreen() {}
void CEventClient::LoadProgress(const std::string& msg, const bool replace_lastline) {}

// from LuaUI
void CEventClient::KeyBindingsChanged() {}
bool CEventClient::KeyMapChanged() { return false; }
bool CEventClient::KeyPress(int keyCode, int scanCode, bool isRepeat) { return false; }
bool CEventClient::KeyRelease(int keyCode, int scanCode) { return false; }
Expand Down
1 change: 1 addition & 0 deletions rts/System/EventClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ class CEventClient
virtual void Update();
virtual void UnsyncedHeightMapUpdate(const SRectangle& rect);

virtual void KeyBindingsChanged();
virtual bool KeyMapChanged();
virtual bool KeyPress(int keyCode, int scanCode, bool isRepeat);
virtual bool KeyRelease(int keyCode, int scanCode);
Expand Down
6 changes: 6 additions & 0 deletions rts/System/EventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,12 @@ bool CEventHandler::CommandNotify(const Command& cmd)
return ControlReverseIterateDefTrue(listCommandNotify, &CEventClient::CommandNotify, cmd);
}

void CEventHandler::KeyBindingsChanged()
{
ZoneScoped;
ITERATE_EVENTCLIENTLIST_NA(KeyBindingsChanged);
}

bool CEventHandler::KeyMapChanged()
{
ZoneScoped;
Expand Down
1 change: 1 addition & 0 deletions rts/System/EventHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ class CEventHandler
void UnsyncedHeightMapUpdate(const SRectangle& rect);
void Update();

void KeyBindingsChanged();
bool KeyMapChanged();
bool KeyPress(int keyCode, int scanCode, bool isRepeat);
bool KeyRelease(int keyCode, int scanCode);
Expand Down
1 change: 1 addition & 0 deletions rts/System/Events.def
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@

SETUP_EVENT(Update, MANAGED_BIT | UNSYNCED_BIT)

SETUP_EVENT(KeyBindingsChanged, MANAGED_BIT | UNSYNCED_BIT)
SETUP_EVENT(KeyMapChanged, MANAGED_BIT | UNSYNCED_BIT | CONTROL_BIT)
SETUP_EVENT(KeyPress, MANAGED_BIT | UNSYNCED_BIT | CONTROL_BIT)
SETUP_EVENT(KeyRelease, MANAGED_BIT | UNSYNCED_BIT | CONTROL_BIT)
Expand Down
Loading