Hotkeys: Use std::vector, remove HOTKEY_LIST_END sentinel

This commit is contained in:
Jonathan G Rennison 2024-08-21 21:51:02 +01:00
parent 44dffb0e81
commit c21d6607dd
17 changed files with 26 additions and 41 deletions

View File

@ -183,7 +183,6 @@ static EventState AirportToolbarGlobalHotkeys(int hotkey)
static Hotkey airtoolbar_hotkeys[] = {
Hotkey('1', "airport", WID_AT_AIRPORT),
Hotkey('2', "demolish", WID_AT_DEMOLISH),
HOTKEY_LIST_END
};
HotkeyList BuildAirToolbarWindow::hotkeys("airtoolbar", airtoolbar_hotkeys, AirportToolbarGlobalHotkeys);

View File

@ -2285,7 +2285,6 @@ struct BuildVehicleWindow : BuildVehicleWindowBase {
static Hotkey buildvehicle_hotkeys[] = {
Hotkey('F', "focus_filter_box", BVHK_FOCUS_FILTER_BOX),
HOTKEY_LIST_END
};
HotkeyList BuildVehicleWindow::hotkeys("buildvehicle", buildvehicle_hotkeys);

View File

@ -318,7 +318,6 @@ static Hotkey dockstoolbar_hotkeys[] = {
Hotkey('6', "buoy", WID_DT_BUOY),
Hotkey('7', "river", WID_DT_RIVER),
Hotkey(_dockstoolbar_aqueduct_keys, "aqueduct", WID_DT_BUILD_AQUEDUCT),
HOTKEY_LIST_END
};
HotkeyList BuildDocksToolbarWindow::hotkeys("dockstoolbar", dockstoolbar_hotkeys, DockToolbarGlobalHotkeys);

View File

@ -152,14 +152,14 @@ static uint16_t 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_t keycode = ParseKeycode(start, end);
if (keycode != 0) hotkey->AddKeycode(keycode);
if (keycode != 0) hotkey.AddKeycode(keycode);
start = (*end == ',') ? end + 1: end;
}
}
@ -213,10 +213,10 @@ static std::string KeycodeToString(uint16_t 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);
}
@ -263,13 +263,21 @@ void Hotkey::AddKeycode(uint16_t keycode)
this->keycodes.insert(keycode);
}
HotkeyList::HotkeyList(const char *ini_group, Hotkey *items, GlobalHotkeyHandlerFunc global_hotkey_handler) :
global_hotkey_handler(global_hotkey_handler), ini_group(ini_group), items(items)
HotkeyList::HotkeyList(const char *ini_group, std::vector<Hotkey> items, GlobalHotkeyHandlerFunc global_hotkey_handler) :
global_hotkey_handler(global_hotkey_handler), ini_group(ini_group), items(std::move(items))
{
if (_hotkey_lists == nullptr) _hotkey_lists = new std::vector<HotkeyList*>();
_hotkey_lists->push_back(this);
}
HotkeyList::HotkeyList(const char *ini_group, std::span<const Hotkey> items, GlobalHotkeyHandlerFunc global_hotkey_handler) :
global_hotkey_handler(global_hotkey_handler), ini_group(ini_group)
{
this->items.assign(items.begin(), items.end());
if (_hotkey_lists == nullptr) _hotkey_lists = new std::vector<HotkeyList*>();
_hotkey_lists->push_back(this);
}
HotkeyList::~HotkeyList()
{
_hotkey_lists->erase(std::find(_hotkey_lists->begin(), _hotkey_lists->end(), this));
@ -283,10 +291,10 @@ void HotkeyList::Load(const IniFile &ini)
{
const IniGroup *group = ini.GetGroup(this->ini_group);
if (group == nullptr) return;
for (Hotkey *hotkey = this->items; hotkey->name != nullptr; ++hotkey) {
const IniItem *item = group->GetItem(hotkey->name);
for (Hotkey &hotkey : this->items) {
const IniItem *item = group->GetItem(hotkey.name);
if (item != nullptr) {
hotkey->keycodes.clear();
hotkey.keycodes.clear();
if (item->value.has_value()) ParseHotkeys(hotkey, item->value->c_str());
}
}
@ -299,8 +307,8 @@ void HotkeyList::Load(const IniFile &ini)
void HotkeyList::Save(IniFile &ini) const
{
IniGroup &group = ini.GetOrCreateGroup(this->ini_group);
for (const Hotkey *hotkey = this->items; hotkey->name != nullptr; ++hotkey) {
IniItem &item = group.GetOrCreateItem(hotkey->name);
for (const Hotkey &hotkey : this->items) {
IniItem &item = group.GetOrCreateItem(hotkey.name);
item.SetValue(SaveKeycodes(hotkey));
}
}
@ -313,11 +321,11 @@ void HotkeyList::Save(IniFile &ini) const
*/
int HotkeyList::CheckMatch(uint16_t 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 &list : this->items) {
auto begin = list.keycodes.begin();
auto end = list.keycodes.end();
if (std::find(begin, end, keycode | WKC_GLOBAL_HOTKEY) != end || (!global_only && std::find(begin, end, keycode) != end)) {
return list->num;
return list.num;
}
}
return -1;

View File

@ -30,8 +30,6 @@ struct Hotkey {
btree::btree_set<uint16_t> keycodes;
};
#define HOTKEY_LIST_END Hotkey((uint16_t)0, nullptr, -1)
struct IniFile;
/**
@ -40,7 +38,8 @@ struct IniFile;
struct HotkeyList {
typedef EventState (*GlobalHotkeyHandlerFunc)(int hotkey);
HotkeyList(const char *ini_group, Hotkey *items, GlobalHotkeyHandlerFunc global_hotkey_handler = nullptr);
HotkeyList(const char *ini_group, std::vector<Hotkey> items, GlobalHotkeyHandlerFunc global_hotkey_handler = nullptr);
HotkeyList(const char *ini_group, std::span<const Hotkey> items, GlobalHotkeyHandlerFunc global_hotkey_handler = nullptr);
~HotkeyList();
void Load(const IniFile &ini);
@ -51,7 +50,7 @@ struct HotkeyList {
GlobalHotkeyHandlerFunc global_hotkey_handler;
private:
const char *ini_group;
Hotkey *items;
std::vector<Hotkey> items;
/**
* Dummy private copy constructor to prevent compilers from

View File

@ -1959,7 +1959,6 @@ public:
static Hotkey industrydirectory_hotkeys[] = {
Hotkey('F', "focus_filter_box", IDHK_FOCUS_FILTER_BOX),
HOTKEY_LIST_END
};
HotkeyList IndustryDirectoryWindow::hotkeys("industrydirectory", industrydirectory_hotkeys);

View File

@ -609,7 +609,6 @@ static Hotkey global_hotkeys[] = {
Hotkey((uint16_t)0, "switch_viewport_route_overlay_mode", GHK_SWITCH_VIEWPORT_ROUTE_OVERLAY_MODE),
Hotkey((uint16_t)0, "switch_viewport_map_slope_mode", GHK_SWITCH_VIEWPORT_MAP_SLOPE_MODE),
Hotkey((uint16_t)0, "switch_viewport_map_height_mode", GHK_SWITCH_VIEWPORT_MAP_HEIGHT_MODE),
HOTKEY_LIST_END
};
HotkeyList MainWindow::hotkeys("global", global_hotkeys);

View File

@ -690,7 +690,6 @@ static EventState BuildObjectGlobalHotkeys(int hotkey)
static Hotkey buildobject_hotkeys[] = {
Hotkey('F', "focus_filter_box", BOHK_FOCUS_FILTER_BOX),
HOTKEY_LIST_END
};
HotkeyList BuildObjectWindow::hotkeys("buildobject", buildobject_hotkeys, BuildObjectGlobalHotkeys);

View File

@ -3816,7 +3816,6 @@ static Hotkey order_hotkeys[] = {
Hotkey((uint16_t)0, "duplicate", OHK_DUPLICATE),
Hotkey((uint16_t)0, "retarget_jump", OHK_RETARGET_JUMP),
Hotkey((uint16_t)0, "close", OHK_CLOSE),
HOTKEY_LIST_END
};
HotkeyList OrdersWindow::hotkeys("order", order_hotkeys);

View File

@ -1048,7 +1048,6 @@ static Hotkey railtoolbar_hotkeys[] = {
Hotkey('R', "remove", WID_RAT_REMOVE),
Hotkey('C', "convert", WID_RAT_CONVERT_RAIL),
Hotkey(WKC_CTRL | 'C', "convert_track", WID_RAT_CONVERT_RAIL_TRACK),
HOTKEY_LIST_END
};
HotkeyList BuildRailToolbarWindow::hotkeys("railtoolbar", railtoolbar_hotkeys, RailToolbarGlobalHotkeys);
@ -1781,7 +1780,6 @@ static EventState BuildRailStationGlobalHotkeys(int hotkey)
static Hotkey buildrailstation_hotkeys[] = {
Hotkey('F', "focus_filter_box", BRASHK_FOCUS_FILTER_BOX),
HOTKEY_LIST_END
};
HotkeyList BuildRailStationWindow::hotkeys("buildrailstation", buildrailstation_hotkeys, BuildRailStationGlobalHotkeys);
@ -2293,7 +2291,6 @@ static Hotkey signaltoolbar_hotkeys[] = {
Hotkey('H', "signal_pbs", WID_BS_ELECTRIC_PBS),
Hotkey('J', "signal_pbs_oneway", WID_BS_ELECTRIC_PBS_OWAY),
Hotkey((uint16_t)0, "signal_no_entry", WID_BS_ELECTRIC_NO_ENTRY),
HOTKEY_LIST_END
};
HotkeyList BuildSignalWindow::hotkeys("signaltoolbar", signaltoolbar_hotkeys);

View File

@ -917,7 +917,6 @@ static Hotkey roadtoolbar_hotkeys[] = {
Hotkey('R', "remove", WID_ROT_REMOVE),
Hotkey('C', "convert", WID_ROT_CONVERT_ROAD),
Hotkey('9', "waypoint", WID_ROT_BUILD_WAYPOINT),
HOTKEY_LIST_END
};
HotkeyList BuildRoadToolbarWindow::road_hotkeys("roadtoolbar", roadtoolbar_hotkeys, RoadToolbarGlobalHotkeys);
@ -934,7 +933,6 @@ static Hotkey tramtoolbar_hotkeys[] = {
Hotkey('R', "remove", WID_ROT_REMOVE),
Hotkey('C', "convert", WID_ROT_CONVERT_ROAD),
Hotkey('9', "waypoint", WID_ROT_BUILD_WAYPOINT),
HOTKEY_LIST_END
};
HotkeyList BuildRoadToolbarWindow::tram_hotkeys("tramtoolbar", tramtoolbar_hotkeys, TramToolbarGlobalHotkeys);
@ -1762,13 +1760,11 @@ public:
static Hotkey buildroadstop_hotkeys[] = {
Hotkey('F', "focus_filter_box", BROSHK_FOCUS_FILTER_BOX),
HOTKEY_LIST_END
};
HotkeyList BuildRoadStationWindow::road_hotkeys("buildroadstop", buildroadstop_hotkeys);
static Hotkey buildtramstop_hotkeys[] = {
Hotkey('F', "focus_filter_box", BROSHK_FOCUS_FILTER_BOX),
HOTKEY_LIST_END
};
HotkeyList BuildRoadStationWindow::tram_hotkeys("buildtramstop", buildtramstop_hotkeys);

View File

@ -1265,7 +1265,6 @@ static Hotkey scriptdebug_hotkeys[] = {
Hotkey('F', "break_string", WID_SCRD_BREAK_STR_EDIT_BOX),
Hotkey('C', "match_case", WID_SCRD_MATCH_CASE_BTN),
Hotkey(WKC_RETURN, "continue", WID_SCRD_CONTINUE_BTN),
HOTKEY_LIST_END
};
HotkeyList ScriptDebugWindow::hotkeys("aidebug", scriptdebug_hotkeys, ScriptDebugGlobalHotkeys);

View File

@ -350,7 +350,6 @@ static EventState SignListGlobalHotkeys(int hotkey)
static Hotkey signlist_hotkeys[] = {
Hotkey('F', "focus_filter_box", SLHK_FOCUS_FILTER_BOX),
HOTKEY_LIST_END
};
HotkeyList SignListWindow::hotkeys("signlist", signlist_hotkeys, SignListGlobalHotkeys);

View File

@ -383,7 +383,6 @@ static Hotkey terraform_hotkeys[] = {
Hotkey('R' | WKC_SHIFT, "ruler", WID_TT_MEASUREMENT_TOOL),
Hotkey('O', "placesign", WID_TT_PLACE_SIGN),
Hotkey('P', "placeobject", WID_TT_PLACE_OBJECT),
HOTKEY_LIST_END
};
HotkeyList TerraformToolbarWindow::hotkeys("terraform", terraform_hotkeys, TerraformToolbarGlobalHotkeys);
@ -844,7 +843,6 @@ static Hotkey terraform_editor_hotkeys[] = {
Hotkey('T', "desert", WID_ETT_PLACE_DESERT),
Hotkey('O', "object", WID_ETT_PLACE_OBJECT),
Hotkey('H', "house", WID_ETT_PLACE_HOUSE),
HOTKEY_LIST_END
};
HotkeyList ScenarioEditorLandscapeGenerationWindow::hotkeys("terraform_editor", terraform_editor_hotkeys, TerraformToolbarEditorGlobalHotkeys);

View File

@ -2339,7 +2339,6 @@ static Hotkey maintoolbar_hotkeys[] = {
Hotkey((uint16_t)0, "template_replacement", MTHK_TEMPLATE_REPLACEMENT),
Hotkey((uint16_t)0, "train_slots", MTHK_TRAIN_SLOTS),
Hotkey((uint16_t)0, "train_counters", MTHK_TRAIN_COUNTERS),
HOTKEY_LIST_END
};
HotkeyList MainToolbarWindow::hotkeys("maintoolbar", maintoolbar_hotkeys);
@ -2724,7 +2723,6 @@ static Hotkey scenedit_maintoolbar_hotkeys[] = {
Hotkey('L', "terraform", MTEHK_TERRAFORM),
Hotkey('M', "smallmap", MTEHK_SMALLMAP),
Hotkey('V', "extra_viewport", MTEHK_EXTRA_VIEWPORT),
HOTKEY_LIST_END
};
HotkeyList ScenarioEditorToolbarWindow::hotkeys("scenedit_maintoolbar", scenedit_maintoolbar_hotkeys);

View File

@ -1253,7 +1253,6 @@ public:
static Hotkey towndirectory_hotkeys[] = {
Hotkey('F', "focus_filter_box", TDHK_FOCUS_FILTER_BOX),
HOTKEY_LIST_END
};
HotkeyList TownDirectoryWindow::hotkeys("towndirectory", towndirectory_hotkeys);

View File

@ -4473,7 +4473,6 @@ public:
static Hotkey vehicleview_hotkeys[] = {
Hotkey('H', "honk", WID_VV_HONK_HORN),
HOTKEY_LIST_END
};
HotkeyList VehicleViewWindow::hotkeys("vehicleview", vehicleview_hotkeys);