(svn r25414) -Codechange: Move handling of global hotkeys to HotkeyList.

replace/41b28d7194a279bdc17475d4fbe2ea6ec885a466
frosch 11 years ago
parent ee4e68bd5e
commit fef30983e6

@ -1402,6 +1402,19 @@ NWidgetBase *MakeCompanyButtonRowsAIDebug(int *biggest_index)
return MakeCompanyButtonRows(biggest_index, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP);
}
/**
* Handler for global hotkeys of the AIDebugWindow.
* @param hotkey Hotkey
* @return ES_HANDLED if hotkey was accepted.
*/
static EventState AIDebugGlobalHotkeys(int hotkey)
{
if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED;
Window *w = ShowAIDebugWindow(INVALID_COMPANY);
if (w == NULL) return ES_NOT_HANDLED;
return w->OnHotkey(hotkey);
}
static Hotkey aidebug_hotkeys[] = {
Hotkey('1', "company_1", WID_AID_COMPANY_BUTTON_START),
Hotkey('2', "company_2", WID_AID_COMPANY_BUTTON_START + 1),
@ -1427,7 +1440,7 @@ static Hotkey aidebug_hotkeys[] = {
Hotkey(WKC_RETURN, "continue", WID_AID_CONTINUE_BTN),
HOTKEY_LIST_END
};
HotkeyList AIDebugWindow::hotkeys("aidebug", aidebug_hotkeys);
HotkeyList AIDebugWindow::hotkeys("aidebug", aidebug_hotkeys, AIDebugGlobalHotkeys);
/** Widgets for the AI debug window. */
static const NWidgetPart _nested_ai_debug_widgets[] = {
@ -1501,18 +1514,6 @@ Window *ShowAIDebugWindow(CompanyID show_company)
return NULL;
}
/**
* Handler for global AI debug window hotkeys.
*/
EventState AIDebugGlobalHotkeys(uint16 key, uint16 keycode)
{
int num = AIDebugWindow::hotkeys.CheckMatch(keycode, true);
if (num == -1) return ES_NOT_HANDLED;
Window *w = ShowAIDebugWindow(INVALID_COMPANY);
if (w == NULL) return ES_NOT_HANDLED;
return w->OnHotkey(num);
}
/**
* Reset the AI windows to their initial state.
*/

@ -138,12 +138,25 @@ struct BuildAirToolbarWindow : Window {
static HotkeyList hotkeys;
};
/**
* Handler for global hotkeys of the BuildAirToolbarWindow.
* @param hotkey Hotkey
* @return ES_HANDLED if hotkey was accepted.
*/
static EventState AirportToolbarGlobalHotkeys(int hotkey)
{
if (_game_mode != GM_NORMAL || !CanBuildVehicleInfrastructure(VEH_AIRCRAFT)) return ES_NOT_HANDLED;
Window *w = ShowBuildAirToolbar();
if (w == NULL) return ES_NOT_HANDLED;
return w->OnHotkey(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);
HotkeyList BuildAirToolbarWindow::hotkeys("airtoolbar", airtoolbar_hotkeys, AirportToolbarGlobalHotkeys);
static const NWidgetPart _nested_air_toolbar_widgets[] = {
NWidget(NWID_HORIZONTAL),
@ -181,16 +194,6 @@ Window *ShowBuildAirToolbar()
return AllocateWindowDescFront<BuildAirToolbarWindow>(&_air_toolbar_desc, TRANSPORT_AIR);
}
EventState AirportToolbarGlobalHotkeys(uint16 key, uint16 keycode)
{
if (!CanBuildVehicleInfrastructure(VEH_AIRCRAFT)) return ES_NOT_HANDLED;
int num = BuildAirToolbarWindow::hotkeys.CheckMatch(keycode, true);
if (num == -1) return ES_NOT_HANDLED;
Window *w = ShowBuildAirToolbar();
if (w == NULL) return ES_NOT_HANDLED;
return w->OnHotkey(num);
}
class BuildAirportWindow : public PickerWindowBase {
SpriteID preview_sprite; ///< Cached airport preview sprite.
int line_height;

@ -269,6 +269,19 @@ struct BuildDocksToolbarWindow : Window {
static HotkeyList hotkeys;
};
/**
* Handler for global hotkeys of the BuildDocksToolbarWindow.
* @param hotkey Hotkey
* @return ES_HANDLED if hotkey was accepted.
*/
static EventState DockToolbarGlobalHotkeys(int hotkey)
{
if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED;
Window *w = ShowBuildDocksToolbar();
if (w == NULL) return ES_NOT_HANDLED;
return w->OnHotkey(hotkey);
}
const uint16 _dockstoolbar_aqueduct_keys[] = {'B', '8', 0};
static Hotkey dockstoolbar_hotkeys[] = {
@ -282,7 +295,7 @@ static Hotkey dockstoolbar_hotkeys[] = {
Hotkey(_dockstoolbar_aqueduct_keys, "aqueduct", WID_DT_BUILD_AQUEDUCT),
HOTKEY_LIST_END
};
HotkeyList BuildDocksToolbarWindow::hotkeys("dockstoolbar", dockstoolbar_hotkeys);
HotkeyList BuildDocksToolbarWindow::hotkeys("dockstoolbar", dockstoolbar_hotkeys, DockToolbarGlobalHotkeys);
/**
* Nested widget parts of docks toolbar, game version.
@ -329,15 +342,6 @@ Window *ShowBuildDocksToolbar()
return AllocateWindowDescFront<BuildDocksToolbarWindow>(&_build_docks_toolbar_desc, TRANSPORT_WATER);
}
EventState DockToolbarGlobalHotkeys(uint16 key, uint16 keycode)
{
int num = BuildDocksToolbarWindow::hotkeys.CheckMatch(keycode, true);
if (num == -1) return ES_NOT_HANDLED;
Window *w = ShowBuildDocksToolbar();
if (w == NULL) return ES_NOT_HANDLED;
return w->OnHotkey(num);
}
/**
* Nested widget parts of docks toolbar, scenario editor version.
* Positions of #WID_DT_DEPOT, #WID_DT_STATION, and #WID_DT_BUOY widgets have changed.

@ -249,8 +249,8 @@ void Hotkey::AddKeycode(uint16 keycode)
this->keycodes.Include(keycode);
}
HotkeyList::HotkeyList(const char *ini_group, Hotkey *items) :
ini_group(ini_group), items(items)
HotkeyList::HotkeyList(const char *ini_group, Hotkey *items, GlobalHotkeyHandlerFunc global_hotkey_handler) :
global_hotkey_handler(global_hotkey_handler), ini_group(ini_group), items(items)
{
if (_hotkey_lists == NULL) _hotkey_lists = new SmallVector<HotkeyList*, 16>();
*_hotkey_lists->Append() = this;
@ -337,45 +337,13 @@ void SaveHotkeysToConfig()
SaveLoadHotkeys(true);
}
typedef EventState GlobalHotkeyHandler(uint16, uint16);
GlobalHotkeyHandler RailToolbarGlobalHotkeys;
GlobalHotkeyHandler DockToolbarGlobalHotkeys;
GlobalHotkeyHandler AirportToolbarGlobalHotkeys;
GlobalHotkeyHandler TerraformToolbarGlobalHotkeys;
GlobalHotkeyHandler TerraformToolbarEditorGlobalHotkeys;
GlobalHotkeyHandler RoadToolbarGlobalHotkeys;
GlobalHotkeyHandler RoadToolbarEditorGlobalHotkeys;
GlobalHotkeyHandler SignListGlobalHotkeys;
GlobalHotkeyHandler AIDebugGlobalHotkeys;
GlobalHotkeyHandler *_global_hotkey_handlers[] = {
RailToolbarGlobalHotkeys,
DockToolbarGlobalHotkeys,
AirportToolbarGlobalHotkeys,
TerraformToolbarGlobalHotkeys,
RoadToolbarGlobalHotkeys,
SignListGlobalHotkeys,
AIDebugGlobalHotkeys,
};
GlobalHotkeyHandler *_global_hotkey_handlers_editor[] = {
TerraformToolbarEditorGlobalHotkeys,
RoadToolbarEditorGlobalHotkeys,
};
void HandleGlobalHotkeys(uint16 key, uint16 keycode)
{
if (_game_mode == GM_NORMAL) {
for (uint i = 0; i < lengthof(_global_hotkey_handlers); i++) {
if (_global_hotkey_handlers[i](key, keycode) == ES_HANDLED) return;
}
} else if (_game_mode == GM_EDITOR) {
for (uint i = 0; i < lengthof(_global_hotkey_handlers_editor); i++) {
if (_global_hotkey_handlers_editor[i](key, keycode) == ES_HANDLED) return;
}
for (HotkeyList **list = _hotkey_lists->Begin(); list != _hotkey_lists->End(); ++list) {
if ((*list)->global_hotkey_handler == NULL) continue;
int hotkey = (*list)->CheckMatch(keycode, true);
if (hotkey >= 0 && ((*list)->global_hotkey_handler(hotkey) == ES_HANDLED)) return;
}
}

@ -14,6 +14,7 @@
#include "core/smallvec_type.hpp"
#include "gfx_type.h"
#include "window_type.h"
/**
* All data for a single hotkey. The name (for saving/loading a configfile),
@ -38,7 +39,9 @@ struct IniFile;
* List of hotkeys for a window.
*/
struct HotkeyList {
HotkeyList(const char *ini_group, Hotkey *items);
typedef EventState (*GlobalHotkeyHandlerFunc)(int hotkey);
HotkeyList(const char *ini_group, Hotkey *items, GlobalHotkeyHandlerFunc global_hotkey_handler = NULL);
~HotkeyList();
void Load(IniFile *ini);
@ -46,6 +49,7 @@ struct HotkeyList {
int CheckMatch(uint16 keycode, bool global_only = false) const;
GlobalHotkeyHandlerFunc global_hotkey_handler;
private:
const char *ini_group;
Hotkey *items;

@ -755,6 +755,20 @@ struct BuildRailToolbarWindow : Window {
static HotkeyList hotkeys;
};
/**
* Handler for global hotkeys of the BuildRailToolbarWindow.
* @param hotkey Hotkey
* @return ES_HANDLED if hotkey was accepted.
*/
static EventState RailToolbarGlobalHotkeys(int hotkey)
{
if (_game_mode != GM_NORMAL || !CanBuildVehicleInfrastructure(VEH_TRAIN)) return ES_NOT_HANDLED;
extern RailType _last_built_railtype;
Window *w = ShowBuildRailToolbar(_last_built_railtype);
if (w == NULL) return ES_NOT_HANDLED;
return w->OnHotkey(hotkey);
}
const uint16 _railtoolbar_autorail_keys[] = {'5', 'A' | WKC_GLOBAL_HOTKEY, 0};
static Hotkey railtoolbar_hotkeys[] = {
@ -774,7 +788,7 @@ static Hotkey railtoolbar_hotkeys[] = {
Hotkey('C', "convert", WID_RAT_CONVERT_RAIL),
HOTKEY_LIST_END
};
HotkeyList BuildRailToolbarWindow::hotkeys("railtoolbar", railtoolbar_hotkeys);
HotkeyList BuildRailToolbarWindow::hotkeys("railtoolbar", railtoolbar_hotkeys, RailToolbarGlobalHotkeys);
static const NWidgetPart _nested_build_rail_widgets[] = {
NWidget(NWID_HORIZONTAL),
@ -845,17 +859,6 @@ Window *ShowBuildRailToolbar(RailType railtype)
return new BuildRailToolbarWindow(&_build_rail_desc, railtype);
}
EventState RailToolbarGlobalHotkeys(uint16 key, uint16 keycode)
{
if (!CanBuildVehicleInfrastructure(VEH_TRAIN)) return ES_NOT_HANDLED;
extern RailType _last_built_railtype;
int num = BuildRailToolbarWindow::hotkeys.CheckMatch(keycode, true);
if (num == -1) return ES_NOT_HANDLED;
Window *w = ShowBuildRailToolbar(_last_built_railtype);
if (w == NULL) return ES_NOT_HANDLED;
return w->OnHotkey(num);
}
/* TODO: For custom stations, respect their allowed platforms/lengths bitmasks!
* --pasky */

@ -660,6 +660,33 @@ struct BuildRoadToolbarWindow : Window {
static HotkeyList hotkeys;
};
/**
* Handler for global hotkeys of the BuildRoadToolbarWindow.
* @param hotkey Hotkey
* @return ES_HANDLED if hotkey was accepted.
*/
static EventState RoadToolbarGlobalHotkeys(int hotkey)
{
Window *w = NULL;
switch (_game_mode) {
case GM_NORMAL: {
extern RoadType _last_built_roadtype;
w = ShowBuildRoadToolbar(_last_built_roadtype);
break;
}
case GM_EDITOR:
w = ShowBuildRoadScenToolbar();
break;
default:
break;
}
if (w == NULL) return ES_NOT_HANDLED;
return w->OnHotkey(hotkey);
}
static Hotkey roadtoolbar_hotkeys[] = {
Hotkey('1', "build_x", WID_ROT_ROAD_X),
Hotkey('2', "build_y", WID_ROT_ROAD_Y),
@ -674,7 +701,7 @@ static Hotkey roadtoolbar_hotkeys[] = {
Hotkey('R', "remove", WID_ROT_REMOVE),
HOTKEY_LIST_END
};
HotkeyList BuildRoadToolbarWindow::hotkeys("roadtoolbar", roadtoolbar_hotkeys);
HotkeyList BuildRoadToolbarWindow::hotkeys("roadtoolbar", roadtoolbar_hotkeys, RoadToolbarGlobalHotkeys);
static const NWidgetPart _nested_build_road_widgets[] = {
@ -774,16 +801,6 @@ Window *ShowBuildRoadToolbar(RoadType roadtype)
return AllocateWindowDescFront<BuildRoadToolbarWindow>(roadtype == ROADTYPE_ROAD ? &_build_road_desc : &_build_tramway_desc, TRANSPORT_ROAD);
}
EventState RoadToolbarGlobalHotkeys(uint16 key, uint16 keycode)
{
extern RoadType _last_built_roadtype;
int num = BuildRoadToolbarWindow::hotkeys.CheckMatch(keycode, true);
if (num == -1) return ES_NOT_HANDLED;
Window *w = ShowBuildRoadToolbar(_last_built_roadtype);
if (w == NULL) return ES_NOT_HANDLED;
return w->OnHotkey(num);
}
static const NWidgetPart _nested_build_road_scen_widgets[] = {
NWidget(NWID_HORIZONTAL),
NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
@ -829,15 +846,6 @@ Window *ShowBuildRoadScenToolbar()
return AllocateWindowDescFront<BuildRoadToolbarWindow>(&_build_road_scen_desc, TRANSPORT_ROAD);
}
EventState RoadToolbarEditorGlobalHotkeys(uint16 key, uint16 keycode)
{
int num = BuildRoadToolbarWindow::hotkeys.CheckMatch(keycode, true);
if (num == -1) return ES_NOT_HANDLED;
Window *w = ShowBuildRoadScenToolbar();
if (w == NULL) return ES_NOT_HANDLED;
return w->OnHotkey(num);
}
struct BuildRoadDepotWindow : public PickerWindowBase {
BuildRoadDepotWindow(WindowDesc *desc, Window *parent) : PickerWindowBase(desc, parent)
{

@ -338,11 +338,24 @@ struct SignListWindow : Window, SignList {
static HotkeyList hotkeys;
};
/**
* Handler for global hotkeys of the SignListWindow.
* @param hotkey Hotkey
* @return ES_HANDLED if hotkey was accepted.
*/
static EventState SignListGlobalHotkeys(int hotkey)
{
if (_game_mode == GM_MENU) return ES_NOT_HANDLED;
Window *w = ShowSignList();
if (w == NULL) return ES_NOT_HANDLED;
return w->OnHotkey(hotkey);
}
static Hotkey signlist_hotkeys[] = {
Hotkey('F', "focus_filter_box", SLHK_FOCUS_FILTER_BOX),
HOTKEY_LIST_END
};
HotkeyList SignListWindow::hotkeys("signlist", signlist_hotkeys);
HotkeyList SignListWindow::hotkeys("signlist", signlist_hotkeys, SignListGlobalHotkeys);
static const NWidgetPart _nested_sign_list_widgets[] = {
NWidget(NWID_HORIZONTAL),
@ -391,15 +404,6 @@ Window *ShowSignList()
return AllocateWindowDescFront<SignListWindow>(&_sign_list_desc, 0);
}
EventState SignListGlobalHotkeys(uint16 key, uint16 keycode)
{
int num = SignListWindow::hotkeys.CheckMatch(keycode, true);
if (num == -1) return ES_NOT_HANDLED;
Window *w = ShowSignList();
if (w == NULL) return ES_NOT_HANDLED;
return w->OnHotkey(num);
}
/**
* Actually rename the sign.
* @param index the sign to rename.

@ -290,6 +290,19 @@ struct TerraformToolbarWindow : Window {
static HotkeyList hotkeys;
};
/**
* Handler for global hotkeys of the TerraformToolbarWindow.
* @param hotkey Hotkey
* @return ES_HANDLED if hotkey was accepted.
*/
static EventState TerraformToolbarGlobalHotkeys(int hotkey)
{
if (_game_mode != GM_NORMAL) return ES_NOT_HANDLED;
Window *w = ShowTerraformToolbar(NULL);
if (w == NULL) return ES_NOT_HANDLED;
return w->OnHotkey(hotkey);
}
static Hotkey terraform_hotkeys[] = {
Hotkey('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_TT_LOWER_LAND),
Hotkey('W' | WKC_GLOBAL_HOTKEY, "raise", WID_TT_RAISE_LAND),
@ -301,7 +314,7 @@ static Hotkey terraform_hotkeys[] = {
Hotkey('P', "placeobject", WID_TT_PLACE_OBJECT),
HOTKEY_LIST_END
};
HotkeyList TerraformToolbarWindow::hotkeys("terraform", terraform_hotkeys);
HotkeyList TerraformToolbarWindow::hotkeys("terraform", terraform_hotkeys, TerraformToolbarGlobalHotkeys);
static const NWidgetPart _nested_terraform_widgets[] = {
NWidget(NWID_HORIZONTAL),
@ -371,15 +384,6 @@ Window *ShowTerraformToolbar(Window *link)
return w;
}
EventState TerraformToolbarGlobalHotkeys(uint16 key, uint16 keycode)
{
int num = TerraformToolbarWindow::hotkeys.CheckMatch(keycode, true);
if (num == -1) return ES_NOT_HANDLED;
Window *w = ShowTerraformToolbar(NULL);
if (w == NULL) return ES_NOT_HANDLED;
return w->OnHotkey(num);
}
static byte _terraform_size = 1;
/**
@ -712,6 +716,19 @@ struct ScenarioEditorLandscapeGenerationWindow : Window {
static HotkeyList hotkeys;
};
/**
* Handler for global hotkeys of the ScenarioEditorLandscapeGenerationWindow.
* @param hotkey Hotkey
* @return ES_HANDLED if hotkey was accepted.
*/
static EventState TerraformToolbarEditorGlobalHotkeys(int hotkey)
{
if (_game_mode != GM_EDITOR) return ES_NOT_HANDLED;
Window *w = ShowEditorTerraformToolbar();
if (w == NULL) return ES_NOT_HANDLED;
return w->OnHotkey(hotkey);
}
static Hotkey terraform_editor_hotkeys[] = {
Hotkey('D' | WKC_GLOBAL_HOTKEY, "dynamite", WID_ETT_DEMOLISH),
Hotkey('Q' | WKC_GLOBAL_HOTKEY, "lower", WID_ETT_LOWER_LAND),
@ -723,7 +740,7 @@ static Hotkey terraform_editor_hotkeys[] = {
HOTKEY_LIST_END
};
HotkeyList ScenarioEditorLandscapeGenerationWindow::hotkeys("terraform_editor", terraform_editor_hotkeys);
HotkeyList ScenarioEditorLandscapeGenerationWindow::hotkeys("terraform_editor", terraform_editor_hotkeys, TerraformToolbarEditorGlobalHotkeys);
static WindowDesc _scen_edit_land_gen_desc(
WDP_AUTO, "toolbar_landscape_scen", 0, 0,
@ -741,12 +758,3 @@ Window *ShowEditorTerraformToolbar()
{
return AllocateWindowDescFront<ScenarioEditorLandscapeGenerationWindow>(&_scen_edit_land_gen_desc, 0);
}
EventState TerraformToolbarEditorGlobalHotkeys(uint16 key, uint16 keycode)
{
int num = ScenarioEditorLandscapeGenerationWindow::hotkeys.CheckMatch(keycode, true);
if (num == -1) return ES_NOT_HANDLED;
Window *w = ShowEditorTerraformToolbar();
if (w == NULL) return ES_NOT_HANDLED;
return w->OnHotkey(num);
}

@ -20,12 +20,6 @@
#include "core/smallvec_type.hpp"
#include "core/smallmap_type.hpp"
/** State of handling an event. */
enum EventState {
ES_HANDLED, ///< The passed event is handled.
ES_NOT_HANDLED, ///< The passed event is not handled.
};
/**
* Flags to describe the look of the frame
*/

@ -691,4 +691,10 @@ struct Window;
/** Number to differentiate different windows of the same class */
typedef int32 WindowNumber;
/** State of handling an event. */
enum EventState {
ES_HANDLED, ///< The passed event is handled.
ES_NOT_HANDLED, ///< The passed event is not handled.
};
#endif /* WINDOW_TYPE_H */

Loading…
Cancel
Save