From 9fe35a49a16d298ffaa8edde24274fab3b193d14 Mon Sep 17 00:00:00 2001 From: dP Date: Wed, 20 May 2020 00:19:52 +0300 Subject: [PATCH] Add hotkeys to remember locations and scroll back to them --- cm_changelog.txt | 1 + source.list | 2 ++ src/citymania/locations.cpp | 65 +++++++++++++++++++++++++++++++++++++ src/citymania/locations.hpp | 45 +++++++++++++++++++++++++ src/rev.cpp | 2 +- src/toolbar_gui.cpp | 6 +++- 6 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 src/citymania/locations.cpp create mode 100644 src/citymania/locations.hpp diff --git a/cm_changelog.txt b/cm_changelog.txt index 612a6d1d05..8fc4e693c7 100644 --- a/cm_changelog.txt +++ b/cm_changelog.txt @@ -80,6 +80,7 @@ This is usable for any OpenTTD servers - Rename "New CB borders" zone to "CB town limit" and switch it to a new colored highlight. Remove old "CB borders" zone. - In CB modes added CB town limit to outer town zones highlight (red border). - Show engine id in build window in newgrf developer mode. +- Add hotkeys (unset by default) to remember locations and scroll back to them. - Fixed automatic funding and advertisement wasting money. - Fixed crash on selecting tram station build tool. - Fixed zoning hotkeys. diff --git a/source.list b/source.list index c5094e8986..e705635ede 100644 --- a/source.list +++ b/source.list @@ -620,6 +620,8 @@ citymania/base64.h citymania/base64.cpp citymania/highlight.hpp citymania/highlight.cpp +citymania/locations.hpp +citymania/locations.cpp citymania/minimap.hpp citymania/minimap.cpp citymania/station_ui.hpp diff --git a/src/citymania/locations.cpp b/src/citymania/locations.cpp new file mode 100644 index 0000000000..1fa0cf06e4 --- /dev/null +++ b/src/citymania/locations.cpp @@ -0,0 +1,65 @@ +#include "../stdafx.h" + +#include "../viewport_func.h" +#include "../window_func.h" +#include "../window_gui.h" +#include "../window_type.h" +#include "../zoom_func.h" +#include "../zoom_type.h" + +#include "locations.hpp" + +#include "../safeguards.h" + +namespace citymania { + +struct Location { + ZoomLevel zoom; + int32 scrollpos_x = 0; + int32 scrollpos_y = 0; +}; + +Location _locations[NUM_LOCATIONS]; + +void SaveLocation(uint slot) { + assert(slot < NUM_LOCATIONS); + auto &loc = _locations[slot]; + + Window *w = FindWindowById(WC_MAIN_WINDOW, 0); + auto vp = w->viewport; + + loc.zoom = vp->zoom; + loc.scrollpos_x = vp->scrollpos_x; + loc.scrollpos_y = vp->scrollpos_y; +} + +void LoadLocation(uint slot) { + assert(slot < NUM_LOCATIONS); + auto &loc = _locations[slot]; + if (!loc.scrollpos_y && !loc.scrollpos_x) return; + + Window *w = FindWindowById(WC_MAIN_WINDOW, 0); + auto vp = w->viewport; + + vp->zoom = loc.zoom; + vp->follow_vehicle = INVALID_VEHICLE; + vp->dest_scrollpos_x = loc.scrollpos_x; + vp->dest_scrollpos_y = loc.scrollpos_y; + vp->virtual_width = ScaleByZoom(vp->width, vp->zoom); + vp->virtual_height = ScaleByZoom(vp->height, vp->zoom); +} + +EventState HandleToolbarHotkey(int hotkey) { + if (hotkey >= CM_MTHK_LOCATIONS_GOTO_START && hotkey < CM_MTHK_LOCATIONS_GOTO_END) { + LoadLocation(hotkey - CM_MTHK_LOCATIONS_GOTO_START); + return ES_HANDLED; + } + if (hotkey >= CM_MTHK_LOCATIONS_SET_START && hotkey < CM_MTHK_LOCATIONS_SET_END) { + SaveLocation(hotkey - CM_MTHK_LOCATIONS_SET_START); + return ES_HANDLED; + } + return ES_NOT_HANDLED; +} + + +} // namespace citymania diff --git a/src/citymania/locations.hpp b/src/citymania/locations.hpp new file mode 100644 index 0000000000..ac33ec6574 --- /dev/null +++ b/src/citymania/locations.hpp @@ -0,0 +1,45 @@ +#ifndef CITYMANIA_LOCATIONS_HPP +#define CITYMANIA_LOCATIONS_HPP + +namespace citymania { + +static const uint NUM_LOCATIONS = 9; + +#define CM_MTHK_LOCATIONS_START 240 +static const int CM_MTHK_LOCATIONS_GOTO_START = CM_MTHK_LOCATIONS_START; +static const int CM_MTHK_LOCATIONS_GOTO_END = CM_MTHK_LOCATIONS_GOTO_START + NUM_LOCATIONS; +static const int CM_MTHK_LOCATIONS_SET_START = CM_MTHK_LOCATIONS_GOTO_END; +static const int CM_MTHK_LOCATIONS_SET_END = CM_MTHK_LOCATIONS_SET_START + NUM_LOCATIONS; + + +// #define CM_MTHK_LOCATIONS_GOTO_START (CM_MTHK_LOCATIONS_START) +// #define CM_MTHK_LOCATIONS_GOTO_END (CM_MTHK_LOCATIONS_GOTO_START + NUM_LOCATIONS) +// #define CM_MTHK_LOCATIONS_SET_START (CM_MTHK_LOCATIONS_GOTO_END) +// #define CM_MTHK_LOCATIONS_SET_END (CM_MTHK_LOCATIONS_SET_START + NUM_LOCATIONS) + + +#define CM_LOCATION_HOTKEYS_DECL \ + Hotkey((uint16)0, "cm_goto_location_1", CM_MTHK_LOCATIONS_START), \ + Hotkey((uint16)0, "cm_goto_location_2", CM_MTHK_LOCATIONS_START + 1), \ + Hotkey((uint16)0, "cm_goto_location_3", CM_MTHK_LOCATIONS_START + 2), \ + Hotkey((uint16)0, "cm_goto_location_4", CM_MTHK_LOCATIONS_START + 3), \ + Hotkey((uint16)0, "cm_goto_location_5", CM_MTHK_LOCATIONS_START + 4), \ + Hotkey((uint16)0, "cm_goto_location_6", CM_MTHK_LOCATIONS_START + 5), \ + Hotkey((uint16)0, "cm_goto_location_7", CM_MTHK_LOCATIONS_START + 6), \ + Hotkey((uint16)0, "cm_goto_location_8", CM_MTHK_LOCATIONS_START + 7), \ + Hotkey((uint16)0, "cm_goto_location_9", CM_MTHK_LOCATIONS_START + 8), \ + Hotkey((uint16)0, "cm_set_location_1", CM_MTHK_LOCATIONS_START + 9), \ + Hotkey((uint16)0, "cm_set_location_2", CM_MTHK_LOCATIONS_START + 10), \ + Hotkey((uint16)0, "cm_set_location_3", CM_MTHK_LOCATIONS_START + 11), \ + Hotkey((uint16)0, "cm_set_location_4", CM_MTHK_LOCATIONS_START + 12), \ + Hotkey((uint16)0, "cm_set_location_5", CM_MTHK_LOCATIONS_START + 13), \ + Hotkey((uint16)0, "cm_set_location_6", CM_MTHK_LOCATIONS_START + 14), \ + Hotkey((uint16)0, "cm_set_location_7", CM_MTHK_LOCATIONS_START + 15), \ + Hotkey((uint16)0, "cm_set_location_8", CM_MTHK_LOCATIONS_START + 16), \ + Hotkey((uint16)0, "cm_set_location_9", CM_MTHK_LOCATIONS_START + 17) + +EventState HandleToolbarHotkey(int hotkey); + +} // namespace citymania + +#endif /* CITYMANIA_MINIMAP_HPP */ diff --git a/src/rev.cpp b/src/rev.cpp index 3efa0d7e77..864fa45167 100644 --- a/src/rev.cpp +++ b/src/rev.cpp @@ -83,4 +83,4 @@ const byte _openttd_revision_tagged = 1; const uint32 _openttd_newgrf_version = 1 << 28 | 10 << 24 | 0 << 20 | 1 << 19 | 28004; -const char _citymania_version[] = "20200519-master-mf9699294a9 19.05.20"; +const char _citymania_version[] = "20200519-master-m1d2c2925d1 19.05.20"; diff --git a/src/toolbar_gui.cpp b/src/toolbar_gui.cpp index 6215ceebe3..d4ee6054c5 100644 --- a/src/toolbar_gui.cpp +++ b/src/toolbar_gui.cpp @@ -60,6 +60,8 @@ #include "zoning.h" #include "watch_gui.h" +#include "citymania/locations.hpp" + #include "safeguards.h" @@ -2100,6 +2102,7 @@ enum MainToolbarHotkeys { MTHK_SETTINGS_ADV, MTHK_NEWGRF, MTHK_LANDINFO, + CM_LOCATION_HOTKEYS, }; /** Main toolbar. */ @@ -2215,7 +2218,7 @@ struct MainToolbarWindow : Window { case MTHK_SETTINGS_ADV: ShowGameSettings(); break; case MTHK_NEWGRF: ShowNewGRFSettings(!_networking && _settings_client.gui.UserIsAllowedToChangeNewGRFs(), true, true, &_grfconfig); break; case MTHK_LANDINFO: _last_started_action = PlaceLandBlockInfo(); break; - default: return ES_NOT_HANDLED; + default: return citymania::HandleToolbarHotkey(hotkey); } return ES_HANDLED; } @@ -2367,6 +2370,7 @@ static Hotkey maintoolbar_hotkeys[] = { Hotkey((uint16)0, "newgrf_window", MTHK_NEWGRF), Hotkey((uint16)0, "sign_list", MTHK_SIGN_LIST), Hotkey((uint16)0, "land_info", MTHK_LANDINFO), + CM_LOCATION_HOTKEYS_DECL, HOTKEY_LIST_END }; HotkeyList MainToolbarWindow::hotkeys("maintoolbar", maintoolbar_hotkeys);