COMMAND: added constants for button down/up commands

master
Martin Gerhardy 2022-01-18 18:51:06 +01:00
parent 5936ee3d88
commit 2c73225326
4 changed files with 18 additions and 15 deletions

View File

@ -49,26 +49,26 @@ bool Command::unregisterCommand(const char* name) {
ActionButtonCommands Command::registerActionButton(const core::String& name, ActionButton& button) {
core::ScopedWriteLock lock(_lock);
const Command cPressed("+" + name, [&] (const command::CmdArgs& args) {
const Command cPressed(COMMAND_PRESSED + name, [&] (const command::CmdArgs& args) {
const int32_t key = args.size() >= 1 ? args[0].toInt() : 0;
const double seconds = args.size() >= 2 ? core::string::toDouble(args[1]) : 0.0;
button.handleDown(key, seconds);
});
_cmds.put(cPressed.name(), cPressed);
const Command cReleased("-" + name, [&] (const command::CmdArgs& args) {
const Command cReleased(COMMAND_RELEASED + name, [&] (const command::CmdArgs& args) {
const int32_t key = args.size() >= 1 ? args[0].toInt() : 0;
const double seconds = args.size() >= 2 ? core::string::toDouble(args[1]) : 0.0;
button.handleUp(key, seconds);
});
_cmds.put(cReleased.name(), cReleased);
updateSortedList();
return ActionButtonCommands("+" + name, "-" + name);
return ActionButtonCommands(COMMAND_PRESSED + name, COMMAND_RELEASED + name);
}
bool Command::unregisterActionButton(const core::String& name) {
core::ScopedWriteLock lock(_lock);
const core::String downB("+" + name);
const core::String upB("-" + name);
const core::String downB(COMMAND_PRESSED + name);
const core::String upB(COMMAND_RELEASED + name);
int amount = _cmds.remove(downB);
amount += _cmds.remove(upB);
updateSortedList();
@ -183,7 +183,7 @@ bool Command::execute(const core::String& command, const CmdArgs& args) {
}
return true;
}
if ((command[0] == '+' || command[0] == '-') && args.empty()) {
if ((command[0] == COMMAND_PRESSED[0] || command[0] == COMMAND_RELEASED[0]) && args.empty()) {
Log::warn("Skip execution of %s - no arguments provided", command.c_str());
return false;
}

View File

@ -17,6 +17,8 @@
namespace command {
typedef core::DynamicArray<core::String> CmdArgs;
#define COMMAND_PRESSED "+"
#define COMMAND_RELEASED "-"
struct ActionButtonCommands {
const core::String first;

View File

@ -76,7 +76,7 @@ static bool executeCommandsForBinding(const BindMap& bindings, int32_t key, int1
continue;
}
Log::trace("Execute the command %s for key %i", command.c_str(), key);
if (command[0] == '+') {
if (command[0] == COMMAND_PRESSED[0]) {
if (command::Command::execute("%s %i %f", command.c_str(), key, nowSeconds) == 1) {
Log::trace("The tracking command was executed");
handled = true;
@ -290,7 +290,7 @@ bool KeyBindingHandler::execute(int32_t key, int16_t modifier, bool pressed, dou
continue;
}
const int32_t commandKey = b.first;
if (pair.command[0] != '+') {
if (pair.command[0] != COMMAND_PRESSED[0]) {
// no action button command
continue;
}
@ -315,7 +315,7 @@ bool KeyBindingHandler::execute(int32_t key, int16_t modifier, bool pressed, dou
if (pair.modifier != 0) {
continue;
}
command::Command::execute("-%s %i %f", &(pair.command.c_str()[1]), checkKey, nowSeconds);
command::Command::execute(COMMAND_RELEASED "%s %i %f", &(pair.command.c_str()[1]), checkKey, nowSeconds);
}
}
}
@ -330,7 +330,7 @@ bool KeyBindingHandler::execute(int32_t key, int16_t modifier, bool pressed, dou
for (auto& b : _bindings) {
const CommandModifierPair& pair = b.second;
const int32_t commandKey = b.first;
if (pair.command[0] != '+') {
if (pair.command[0] != COMMAND_PRESSED[0]) {
// no action button command
continue;
}
@ -340,7 +340,7 @@ bool KeyBindingHandler::execute(int32_t key, int16_t modifier, bool pressed, dou
if (!isPressed(commandKey)) {
continue;
}
command::Command::execute("-%s %i %f", &(pair.command.c_str()[1]), commandKey, nowSeconds);
command::Command::execute(COMMAND_RELEASED "%s %i %f", &(pair.command.c_str()[1]), commandKey, nowSeconds);
executeCommands(commandKey, modifier, nowSeconds, 0u);
}
_pressedModifierMask &= ~(uint32_t)code;
@ -349,8 +349,8 @@ bool KeyBindingHandler::execute(int32_t key, int16_t modifier, bool pressed, dou
for (auto i = range.first; i != range.second; ++i) {
const CommandModifierPair& pair = i->second;
const core::String& command = pair.command;
if (command[0] == '+') {
command::Command::execute("-%s %i %f", &(command.c_str()[1]), key, nowSeconds);
if (command[0] == COMMAND_PRESSED[0]) {
command::Command::execute(COMMAND_RELEASED "%s %i %f", &(command.c_str()[1]), key, nowSeconds);
handled = true;
}
}

View File

@ -4,6 +4,7 @@
#include "KeybindingParser.h"
#include "CustomButtonNames.h"
#include "command/Command.h"
#include "core/ArrayLength.h"
#include "core/Tokenizer.h"
#include "core/collection/DynamicArray.h"
@ -14,7 +15,7 @@ namespace util {
void KeybindingParser::parseKeyAndCommand(core::String key, const core::String& command) {
int modifier = KMOD_NONE;
core::Tokenizer tok(true, key, "+");
core::Tokenizer tok(true, key, COMMAND_PRESSED);
const core::DynamicArray<core::String>& line = tok.tokens();
if (line.size() > 1) {
for (const core::String& token : line) {
@ -40,7 +41,7 @@ void KeybindingParser::parseKeyAndCommand(core::String key, const core::String&
} else {
key = token;
if (key.empty()) {
key = "+";
key = COMMAND_PRESSED;
}
}
}