diff options
author | yexo <yexo@openttd.org> | 2010-07-04 10:57:31 +0000 |
---|---|---|
committer | yexo <yexo@openttd.org> | 2010-07-04 10:57:31 +0000 |
commit | 19f86951d9d463fdba12d3ccaa3438d61f7275e1 (patch) | |
tree | d216ae336d1d5e46ed14e90944eb7217838dfef0 | |
parent | 6c6d1e379270b3c1cf6403bbb69db61d076d26b9 (diff) | |
download | openttd-19f86951d9d463fdba12d3ccaa3438d61f7275e1.tar.xz |
(svn r20073) -Feature: customizable hotkeys for the road toolbar
-rw-r--r-- | src/hotkeys.cpp | 4 | ||||
-rw-r--r-- | src/road_gui.cpp | 88 | ||||
-rw-r--r-- | src/road_gui.h | 4 | ||||
-rw-r--r-- | src/toolbar_gui.cpp | 2 |
4 files changed, 62 insertions, 36 deletions
diff --git a/src/hotkeys.cpp b/src/hotkeys.cpp index 17029795f..47b88361a 100644 --- a/src/hotkeys.cpp +++ b/src/hotkeys.cpp @@ -293,6 +293,8 @@ GlobalHotkeyHandler DockToolbarGlobalHotkeys; GlobalHotkeyHandler AirportToolbarGlobalHotkeys; GlobalHotkeyHandler TerraformToolbarGlobalHotkeys; GlobalHotkeyHandler TerraformToolbarEditorGlobalHotkeys; +GlobalHotkeyHandler RoadToolbarGlobalHotkeys; +GlobalHotkeyHandler RoadToolbarEditorGlobalHotkeys; GlobalHotkeyHandler *_global_hotkey_handlers[] = { @@ -300,10 +302,12 @@ GlobalHotkeyHandler *_global_hotkey_handlers[] = { DockToolbarGlobalHotkeys, AirportToolbarGlobalHotkeys, TerraformToolbarGlobalHotkeys, + RoadToolbarGlobalHotkeys, }; GlobalHotkeyHandler *_global_hotkey_handlers_editor[] = { TerraformToolbarEditorGlobalHotkeys, + RoadToolbarEditorGlobalHotkeys, }; diff --git a/src/road_gui.cpp b/src/road_gui.cpp index 9fb423e35..38f23299d 100644 --- a/src/road_gui.cpp +++ b/src/road_gui.cpp @@ -28,6 +28,7 @@ #include "tunnelbridge_map.h" #include "tilehighlight_func.h" #include "company_base.h" +#include "hotkeys.h" #include "table/sprites.h" #include "table/strings.h" @@ -446,21 +447,6 @@ static OnButtonClick * const _build_road_button_proc[] = { BuildRoadClick_Remove }; -/** Array with the keycode of the button-clicks for the road-toolbar */ -static const uint16 _road_keycodes[] = { - '1', - '2', - '3', - '4', - '5', - '6', - '7', - '8', - 'B', - 'T', - 'R', -}; - struct BuildRoadToolbarWindow : Window { BuildRoadToolbarWindow(const WindowDesc *desc, WindowNumber window_number) : Window() { @@ -553,20 +539,11 @@ struct BuildRoadToolbarWindow : Window { virtual EventState OnKeyPress(uint16 key, uint16 keycode) { - EventState state = ES_NOT_HANDLED; - for (uint i = 0; i != lengthof(_road_keycodes); i++) { - if (keycode == _road_keycodes[i]) { - _remove_button_clicked = false; - _one_way_button_clicked = false; - _build_road_button_proc[i](this); - this->UpdateOptionWidgetStatus((RoadToolbarWidgets)(i + RTW_ROAD_X)); - if (_ctrl_pressed) RoadToolbar_CtrlChanged(this); - state = ES_HANDLED; - break; - } - } + int num = CheckHotkeyMatch(roadtoolbar_hotkeys, keycode, this); + if (num == -1 || this->GetWidget<NWidgetBase>(num) == NULL) return ES_NOT_HANDLED; + this->OnClick(Point(), num, 1); MarkTileDirtyByTile(TileVirtXY(_thd.pos.x, _thd.pos.y)); // redraw tile selection - return state; + return ES_HANDLED; } virtual void OnPlaceObject(Point pt, TileIndex tile) @@ -697,7 +674,26 @@ struct BuildRoadToolbarWindow : Window { if (RoadToolbar_CtrlChanged(this)) return ES_HANDLED; return ES_NOT_HANDLED; } + + static Hotkey<BuildRoadToolbarWindow> roadtoolbar_hotkeys[]; +}; + +Hotkey<BuildRoadToolbarWindow> BuildRoadToolbarWindow::roadtoolbar_hotkeys[] = { + Hotkey<BuildRoadToolbarWindow>('1', "build_x", RTW_ROAD_X), + Hotkey<BuildRoadToolbarWindow>('2', "build_y", RTW_ROAD_Y), + Hotkey<BuildRoadToolbarWindow>('3', "autoroad", RTW_AUTOROAD), + Hotkey<BuildRoadToolbarWindow>('4', "demolish", RTW_DEMOLISH), + Hotkey<BuildRoadToolbarWindow>('5', "depot", RTW_DEPOT), + Hotkey<BuildRoadToolbarWindow>('6', "bus_station", RTW_BUS_STATION), + Hotkey<BuildRoadToolbarWindow>('7', "truck_station", RTW_TRUCK_STATION), + Hotkey<BuildRoadToolbarWindow>('8', "oneway", RTW_ONE_WAY), + Hotkey<BuildRoadToolbarWindow>('B', "bridge", RTW_BUILD_BRIDGE), + Hotkey<BuildRoadToolbarWindow>('T', "tunnel", RTW_BUILD_TUNNEL), + Hotkey<BuildRoadToolbarWindow>('R', "remove", RTW_REMOVE), + HOTKEY_LIST_END(BuildRoadToolbarWindow) }; +Hotkey<BuildRoadToolbarWindow> *_roadtoolbar_hotkeys = BuildRoadToolbarWindow::roadtoolbar_hotkeys; + static const NWidgetPart _nested_build_road_widgets[] = { NWidget(NWID_HORIZONTAL), @@ -778,13 +774,30 @@ static const WindowDesc _build_tramway_desc( _nested_build_tramway_widgets, lengthof(_nested_build_tramway_widgets) ); -void ShowBuildRoadToolbar(RoadType roadtype) +/** + * Open the build road toolbar window + * + * If the terraform toolbar is linked to the toolbar, that window is also opened. + * + * @return newly opened road toolbar, or NULL if the toolbar could not be opened. + */ +Window *ShowBuildRoadToolbar(RoadType roadtype) { - if (!Company::IsValidID(_local_company)) return; + if (!Company::IsValidID(_local_company)) return NULL; _cur_roadtype = roadtype; DeleteWindowByClass(WC_BUILD_TOOLBAR); - AllocateWindowDescFront<BuildRoadToolbarWindow>(roadtype == ROADTYPE_ROAD ? &_build_road_desc : &_build_tramway_desc, TRANSPORT_ROAD); + 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 = CheckHotkeyMatch<BuildRoadToolbarWindow>(_roadtoolbar_hotkeys, keycode, NULL, true); + if (num == -1) return ES_NOT_HANDLED; + Window *w = ShowBuildRoadToolbar(_last_built_roadtype); + if (w == NULL) return ES_NOT_HANDLED; + return w->OnKeyPress(key, keycode); } static const NWidgetPart _nested_build_road_scen_widgets[] = { @@ -821,10 +834,19 @@ static const WindowDesc _build_road_scen_desc( _nested_build_road_scen_widgets, lengthof(_nested_build_road_scen_widgets) ); -void ShowBuildRoadScenToolbar() +Window *ShowBuildRoadScenToolbar() { _cur_roadtype = ROADTYPE_ROAD; - AllocateWindowDescFront<BuildRoadToolbarWindow>(&_build_road_scen_desc, 0); + return AllocateWindowDescFront<BuildRoadToolbarWindow>(&_build_road_scen_desc, 0); +} + +EventState RoadToolbarEditorGlobalHotkeys(uint16 key, uint16 keycode) +{ + int num = CheckHotkeyMatch<BuildRoadToolbarWindow>(_roadtoolbar_hotkeys, keycode, NULL, true); + if (num == -1) return ES_NOT_HANDLED; + Window *w = ShowBuildRoadScenToolbar(); + if (w == NULL) return ES_NOT_HANDLED; + return w->OnKeyPress(key, keycode); } /** Enum referring to the widgets of the build road depot window */ diff --git a/src/road_gui.h b/src/road_gui.h index 7948e3b04..8bf38a7ed 100644 --- a/src/road_gui.h +++ b/src/road_gui.h @@ -14,7 +14,7 @@ #include "road_type.h" -void ShowBuildRoadToolbar(RoadType roadtype); -void ShowBuildRoadScenToolbar(); +Window *ShowBuildRoadToolbar(RoadType roadtype); +Window *ShowBuildRoadScenToolbar(); #endif /* ROAD_GUI_H */ diff --git a/src/toolbar_gui.cpp b/src/toolbar_gui.cpp index 25b8adcd9..2cf124b7a 100644 --- a/src/toolbar_gui.cpp +++ b/src/toolbar_gui.cpp @@ -55,7 +55,7 @@ #include "table/sprites.h" RailType _last_built_railtype; -static RoadType _last_built_roadtype; +RoadType _last_built_roadtype; enum ToolbarMode { TB_NORMAL, |