diff options
author | Rubidium <rubidium@openttd.org> | 2021-04-18 20:29:46 +0200 |
---|---|---|
committer | rubidium42 <rubidium42@users.noreply.github.com> | 2021-04-21 21:12:08 +0200 |
commit | bf4fe19a6684231d04764c3bee5ed51e4124a925 (patch) | |
tree | c8b7d662f3ed0ecc236ea894c9a51279e80dfbc2 /src/network | |
parent | b3495f1a1323eb6d236c6fecb1127a0bd716a4d5 (diff) | |
download | openttd-bf4fe19a6684231d04764c3bee5ed51e4124a925.tar.xz |
Codechange: merge duplicated logic to scroll in lists by key into a single function
Diffstat (limited to 'src/network')
-rw-r--r-- | src/network/network_content_gui.cpp | 64 | ||||
-rw-r--r-- | src/network/network_gui.cpp | 39 |
2 files changed, 24 insertions, 79 deletions
diff --git a/src/network/network_content_gui.cpp b/src/network/network_content_gui.cpp index 466279125..0cd711877 100644 --- a/src/network/network_content_gui.cpp +++ b/src/network/network_content_gui.cpp @@ -863,55 +863,31 @@ public: EventState OnKeyPress(WChar key, uint16 keycode) override { - switch (keycode) { - case WKC_UP: - /* scroll up by one */ - if (this->list_pos > 0) this->list_pos--; - break; - case WKC_DOWN: - /* scroll down by one */ - if (this->list_pos < (int)this->content.size() - 1) this->list_pos++; - break; - case WKC_PAGEUP: - /* scroll up a page */ - this->list_pos = (this->list_pos < this->vscroll->GetCapacity()) ? 0 : this->list_pos - this->vscroll->GetCapacity(); - break; - case WKC_PAGEDOWN: - /* scroll down a page */ - this->list_pos = std::min(this->list_pos + this->vscroll->GetCapacity(), (int)this->content.size() - 1); - break; - case WKC_HOME: - /* jump to beginning */ - this->list_pos = 0; - break; - case WKC_END: - /* jump to end */ - this->list_pos = (int)this->content.size() - 1; - break; - - case WKC_SPACE: - case WKC_RETURN: - if (keycode == WKC_RETURN || !IsWidgetFocused(WID_NCL_FILTER)) { - if (this->selected != nullptr) { - _network_content_client.ToggleSelectedState(this->selected); - this->content.ForceResort(); - this->InvalidateData(); - } - if (this->filter_data.types.any()) { - this->content.ForceRebuild(); - this->InvalidateData(); + if (this->vscroll->UpdateListPositionOnKeyPress(this->list_pos, keycode) == ES_NOT_HANDLED) { + switch (keycode) { + case WKC_SPACE: + case WKC_RETURN: + if (keycode == WKC_RETURN || !IsWidgetFocused(WID_NCL_FILTER)) { + if (this->selected != nullptr) { + _network_content_client.ToggleSelectedState(this->selected); + this->content.ForceResort(); + this->InvalidateData(); + } + if (this->filter_data.types.any()) { + this->content.ForceRebuild(); + this->InvalidateData(); + } + return ES_HANDLED; } - return ES_HANDLED; - } - /* space is pressed and filter is focused. */ - FALLTHROUGH; + /* space is pressed and filter is focused. */ + FALLTHROUGH; - default: - return ES_NOT_HANDLED; + default: + return ES_NOT_HANDLED; + } } if (this->content.size() == 0) { - this->list_pos = 0; // above stuff may result in "-1". if (this->UpdateFilterState()) { this->content.ForceRebuild(); this->InvalidateData(); diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index 78b0d9dfc..1c80f7f3c 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -66,8 +66,8 @@ void UpdateNetworkGameWindow() } typedef GUIList<NetworkGameList*, StringFilter&> GUIGameServerList; -typedef uint16 ServerListPosition; -static const ServerListPosition SLP_INVALID = 0xFFFF; +typedef int ServerListPosition; +static const ServerListPosition SLP_INVALID = -1; /** Full blown container to make it behave exactly as we want :) */ class NWidgetServerListHeader : public NWidgetContainer { @@ -771,39 +771,8 @@ public: EventState state = ES_NOT_HANDLED; /* handle up, down, pageup, pagedown, home and end */ - if (keycode == WKC_UP || keycode == WKC_DOWN || keycode == WKC_PAGEUP || keycode == WKC_PAGEDOWN || keycode == WKC_HOME || keycode == WKC_END) { - if (this->servers.size() == 0) return ES_HANDLED; - switch (keycode) { - case WKC_UP: - /* scroll up by one */ - if (this->list_pos == SLP_INVALID) return ES_HANDLED; - if (this->list_pos > 0) this->list_pos--; - break; - case WKC_DOWN: - /* scroll down by one */ - if (this->list_pos == SLP_INVALID) return ES_HANDLED; - if (this->list_pos < this->servers.size() - 1) this->list_pos++; - break; - case WKC_PAGEUP: - /* scroll up a page */ - if (this->list_pos == SLP_INVALID) return ES_HANDLED; - this->list_pos = (this->list_pos < this->vscroll->GetCapacity()) ? 0 : this->list_pos - this->vscroll->GetCapacity(); - break; - case WKC_PAGEDOWN: - /* scroll down a page */ - if (this->list_pos == SLP_INVALID) return ES_HANDLED; - this->list_pos = std::min(this->list_pos + this->vscroll->GetCapacity(), (int)this->servers.size() - 1); - break; - case WKC_HOME: - /* jump to beginning */ - this->list_pos = 0; - break; - case WKC_END: - /* jump to end */ - this->list_pos = (ServerListPosition)this->servers.size() - 1; - break; - default: NOT_REACHED(); - } + if (this->vscroll->UpdateListPositionOnKeyPress(this->list_pos, keycode) == ES_HANDLED) { + if (this->list_pos == SLP_INVALID) return ES_HANDLED; this->server = this->servers[this->list_pos]; |