Codechange: Reorganise hotkey initialisation. (#10951)

Hotkeys are now initialized inline, and use std::vector instead of
separate static C-arrays and std::string instead of char *. The list end
marker is no longer required.
This commit is contained in:
PeterN
2023-06-05 18:12:30 +01:00
committed by GitHub
parent 921f5afc4d
commit f814c86389
15 changed files with 410 additions and 502 deletions

View File

@@ -149,14 +149,14 @@ static uint16 ParseKeycode(const char *start, const char *end)
* @param hotkey The hotkey object to add the keycodes to
* @param value The string to parse
*/
static void ParseHotkeys(Hotkey *hotkey, const char *value)
static void ParseHotkeys(Hotkey &hotkey, const char *value)
{
const char *start = value;
while (*start != '\0') {
const char *end = start;
while (*end != '\0' && *end != ',') end++;
uint16 keycode = ParseKeycode(start, end);
if (keycode != 0) hotkey->AddKeycode(keycode);
if (keycode != 0) hotkey.AddKeycode(keycode);
start = (*end == ',') ? end + 1: end;
}
}
@@ -210,10 +210,10 @@ static std::string KeycodeToString(uint16 keycode)
* @param hotkey The keycodes of this hotkey need to be converted to a string.
* @return A string representation of all keycodes.
*/
std::string SaveKeycodes(const Hotkey *hotkey)
std::string SaveKeycodes(const Hotkey &hotkey)
{
std::string str;
for (auto keycode : hotkey->keycodes) {
for (auto keycode : hotkey.keycodes) {
if (!str.empty()) str += ",";
str += KeycodeToString(keycode);
}
@@ -226,7 +226,7 @@ std::string SaveKeycodes(const Hotkey *hotkey)
* @param name The name of this hotkey.
* @param num Number of this hotkey, should be unique within the hotkey list.
*/
Hotkey::Hotkey(uint16 default_keycode, const char *name, int num) :
Hotkey::Hotkey(uint16 default_keycode, const std::string &name, int num) :
name(name),
num(num)
{
@@ -239,14 +239,12 @@ Hotkey::Hotkey(uint16 default_keycode, const char *name, int num) :
* @param name The name of this hotkey.
* @param num Number of this hotkey, should be unique within the hotkey list.
*/
Hotkey::Hotkey(const uint16 *default_keycodes, const char *name, int num) :
Hotkey::Hotkey(const std::vector<uint16> &default_keycodes, const std::string &name, int num) :
name(name),
num(num)
{
const uint16 *keycode = default_keycodes;
while (*keycode != 0) {
this->AddKeycode(*keycode);
keycode++;
for (uint16 keycode : default_keycodes) {
this->AddKeycode(keycode);
}
}
@@ -260,7 +258,7 @@ void Hotkey::AddKeycode(uint16 keycode)
this->keycodes.insert(keycode);
}
HotkeyList::HotkeyList(const char *ini_group, Hotkey *items, GlobalHotkeyHandlerFunc global_hotkey_handler) :
HotkeyList::HotkeyList(const std::string &ini_group, const std::vector<Hotkey> &items, GlobalHotkeyHandlerFunc global_hotkey_handler) :
global_hotkey_handler(global_hotkey_handler), ini_group(ini_group), items(items)
{
if (_hotkey_lists == nullptr) _hotkey_lists = new std::vector<HotkeyList*>();
@@ -279,10 +277,10 @@ HotkeyList::~HotkeyList()
void HotkeyList::Load(IniFile *ini)
{
IniGroup *group = ini->GetGroup(this->ini_group);
for (Hotkey *hotkey = this->items; hotkey->name != nullptr; ++hotkey) {
IniItem *item = group->GetItem(hotkey->name, false);
for (Hotkey &hotkey : this->items) {
IniItem *item = group->GetItem(hotkey.name, false);
if (item != nullptr) {
hotkey->keycodes.clear();
hotkey.keycodes.clear();
if (item->value.has_value()) ParseHotkeys(hotkey, item->value->c_str());
}
}
@@ -295,8 +293,8 @@ void HotkeyList::Load(IniFile *ini)
void HotkeyList::Save(IniFile *ini) const
{
IniGroup *group = ini->GetGroup(this->ini_group);
for (const Hotkey *hotkey = this->items; hotkey->name != nullptr; ++hotkey) {
IniItem *item = group->GetItem(hotkey->name, true);
for (const Hotkey &hotkey : this->items) {
IniItem *item = group->GetItem(hotkey.name, true);
item->SetValue(SaveKeycodes(hotkey));
}
}
@@ -309,11 +307,11 @@ void HotkeyList::Save(IniFile *ini) const
*/
int HotkeyList::CheckMatch(uint16 keycode, bool global_only) const
{
for (const Hotkey *list = this->items; list->name != nullptr; ++list) {
auto begin = list->keycodes.begin();
auto end = list->keycodes.end();
for (const Hotkey &hotkey : this->items) {
auto begin = hotkey.keycodes.begin();
auto end = hotkey.keycodes.end();
if (std::find(begin, end, keycode | WKC_GLOBAL_HOTKEY) != end || (!global_only && std::find(begin, end, keycode) != end)) {
return list->num;
return hotkey.num;
}
}
return -1;