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
47 changes: 29 additions & 18 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 @@ -677,19 +678,17 @@ bool CKeyBindings::UnBind(const std::string& keystr, const std::string& command)
KeyMap& bindings = ks.IsKeyCode() ? codeBindings : scanBindings;
const auto it = bindings.find(ks);

if (it == bindings.end())
return false;

ActionList& al = it->second;
const bool success = RemoveCommandFromList(al, command);
if (it != bindings.end()) {
ActionList& al = it->second;

if (al.empty())
bindings.erase(it);
if (RemoveCommandFromList(al, command))
buildHotkeyMap = true;

if (success)
buildHotkeyMap = true;
if (al.empty())
bindings.erase(it);
}

return success;
return true;
}


Expand All @@ -709,11 +708,11 @@ bool CKeyBindings::UnBindKeyset(const std::string& keystr)

const auto it = bindings.find(ks);

if (it == bindings.end())
return false;
if (it != bindings.end()) {
bindings.erase(it);
buildHotkeyMap = true;
}

bindings.erase(it);
buildHotkeyMap = true;
return true;
}

Expand Down Expand Up @@ -756,7 +755,7 @@ bool CKeyBindings::UnBindAction(const std::string& command)
if (changed)
buildHotkeyMap = true;

return changed;
return true;
}


Expand Down Expand Up @@ -880,6 +879,9 @@ bool CKeyBindings::ExecuteCommandInternal(const std::string& line)

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

// emit even on a no-op command so clients can tell it ran; keydebug is logging-only, so skip it
bool emitEvent = true;

if (command == "keydebug") {
if (words.size() == 1) {
// toggle
Expand All @@ -888,6 +890,7 @@ bool CKeyBindings::ExecuteCommandInternal(const std::string& line)
// set
debugEnabled = atoi(words[1].c_str());
}
emitEvent = false;
}
else if (command == "keyload") {
const std::string& filename = words.size() > 1 ? words[1] : DEFAULT_FILENAME;
Expand Down Expand Up @@ -952,15 +955,19 @@ bool CKeyBindings::ExecuteCommandInternal(const std::string& line)
return false;
}

return false;
return emitEvent;
}


bool CKeyBindings::ExecuteCommand(const std::string& line)
{
const bool ret = ExecuteCommandInternal(line);
const bool emitEvent = ExecuteCommandInternal(line);
MaybeBuildHotkeyMap();
return ret;

if (emitEvent)
eventHandler.KeyBindingsChanged();

return emitEvent;
}


Expand Down Expand Up @@ -1001,6 +1008,10 @@ bool CKeyBindings::Load(const std::string& filename)
{
const bool ret = LoadInternal(filename);
MaybeBuildHotkeyMap();

if (ret)
eventHandler.KeyBindingsChanged();

return ret;
}

Expand Down
25 changes: 25 additions & 0 deletions rts/Lua/LuaHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3129,6 +3129,31 @@ void CLuaHandle::Pong(uint8_t pingTag, const spring_time pktSendTime, const spri
RunCallIn(L, cmdStr, 3, 0);
}

/*** Called after a keybinding command runs.
*
* Called when:
*
* - A keybinding command runs, e.g. `bind k action`. This may fire even when nothing actually changed (e.g. a redundant bind), so treat it as "a binding command ran", not a guarantee that bindings differ. An operation covering multiple keybindings fires a single event at the end, 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 @@ -287,6 +287,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 @@ -811,6 +811,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 @@ -223,6 +223,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 @@ -101,6 +101,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