Changeset - r25206:136702761466
[Not reviewed]
master
0 5 0
Rubidium - 4 years ago 2021-04-18 18:29:46
rubidium@openttd.org
Codechange: merge duplicated logic to scroll in lists by key into a single function
5 files changed with 67 insertions and 95 deletions:
0 comments (0 inline, 0 general)
src/network/network_content_gui.cpp
Show inline comments
 
@@ -863,32 +863,8 @@ public:
 

	
 
	EventState OnKeyPress(WChar key, uint16 keycode) override
 
	{
 
		if (this->vscroll->UpdateListPositionOnKeyPress(this->list_pos, keycode) == ES_NOT_HANDLED) {
 
		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)) {
 
@@ -909,9 +885,9 @@ public:
 
			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();
src/network/network_gui.cpp
Show inline comments
 
@@ -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->vscroll->UpdateListPositionOnKeyPress(this->list_pos, keycode) == ES_HANDLED) {
 
					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();
 
			}
 

	
 
			this->server = this->servers[this->list_pos];
 

	
src/newgrf_gui.cpp
Show inline comments
 
@@ -1318,42 +1318,8 @@ struct NewGRFWindow : public Window, New
 
	{
 
		if (!this->editable) return ES_NOT_HANDLED;
 

	
 
		switch (keycode) {
 
			case WKC_UP:
 
				/* scroll up by one */
 
				if (this->avail_pos > 0) this->avail_pos--;
 
				break;
 

	
 
			case WKC_DOWN:
 
				/* scroll down by one */
 
				if (this->avail_pos < (int)this->avails.size() - 1) this->avail_pos++;
 
				break;
 

	
 
			case WKC_PAGEUP:
 
				/* scroll up a page */
 
				this->avail_pos = (this->avail_pos < this->vscroll2->GetCapacity()) ? 0 : this->avail_pos - this->vscroll2->GetCapacity();
 
				break;
 
		if (this->vscroll2->UpdateListPositionOnKeyPress(this->avail_pos, keycode) == ES_NOT_HANDLED) return ES_NOT_HANDLED;
 

	
 
			case WKC_PAGEDOWN:
 
				/* scroll down a page */
 
				this->avail_pos = std::min(this->avail_pos + this->vscroll2->GetCapacity(), (int)this->avails.size() - 1);
 
				break;
 

	
 
			case WKC_HOME:
 
				/* jump to beginning */
 
				this->avail_pos = 0;
 
				break;
 

	
 
			case WKC_END:
 
				/* jump to end */
 
				this->avail_pos = (uint)this->avails.size() - 1;
 
				break;
 

	
 
			default:
 
				return ES_NOT_HANDLED;
 
		}
 

	
 
		if (this->avails.size() == 0) this->avail_pos = -1;
 
		if (this->avail_pos >= 0) {
 
			this->active_sel = nullptr;
 
			DeleteWindowByClass(WC_GRF_PARAMETERS);
src/widget.cpp
Show inline comments
 
@@ -1975,6 +1975,66 @@ int Scrollbar::GetScrolledRowFromWidget(
 
}
 

	
 
/**
 
 * Update the given list position as if it were on this scroll bar when the given keycode was pressed.
 
 * This does not update the actual position of this scroll bar, that is left to the caller. It does,
 
 * however use the capacity and count of the scroll bar for the bounds and amount to scroll.
 
 *
 
 * When the count is 0 or the return is ES_NOT_HANDLED, then the position is not updated.
 
 * With WKC_UP and WKC_DOWN the position goes one up or down respectively.
 
 * With WKC_PAGEUP and WKC_PAGEDOWN the position goes one capacity up or down respectively.
 
 * With WKC_HOME the first position is selected and with WKC_END the last position is selected.
 
 * This function ensures that pos is in the range [0..count).
 
 * @param list_position The current position in the list.
 
 * @param key_code      The pressed key code.
 
 * @return ES_NOT_HANDLED when another key than the 6 specific keys was pressed, otherwise ES_HANDLED.
 
 */
 
EventState Scrollbar::UpdateListPositionOnKeyPress(int &list_position, uint16 keycode) const
 
{
 
	int new_pos = list_position;
 
	switch (keycode) {
 
		case WKC_UP:
 
			/* scroll up by one */
 
			new_pos--;
 
			break;
 

	
 
		case WKC_DOWN:
 
			/* scroll down by one */
 
			new_pos++;
 
			break;
 

	
 
		case WKC_PAGEUP:
 
			/* scroll up a page */
 
			new_pos -= this->GetCapacity();
 
			break;
 

	
 
		case WKC_PAGEDOWN:
 
			/* scroll down a page */
 
			new_pos += this->GetCapacity();
 
			break;
 

	
 
		case WKC_HOME:
 
			/* jump to beginning */
 
			new_pos = 0;
 
			break;
 

	
 
		case WKC_END:
 
			/* jump to end */
 
			new_pos = this->GetCount() - 1;
 
			break;
 

	
 
		default:
 
			return ES_NOT_HANDLED;
 
	}
 

	
 
	/* If there are no elements, there is nothing to scroll/update. */
 
	if (this->GetCount() != 0) {
 
		list_position = Clamp(new_pos, 0, this->GetCount() - 1);
 
	}
 
	return ES_HANDLED;
 
}
 

	
 

	
 
/**
 
 * Set capacity of visible elements from the size and resize properties of a widget.
 
 * @param w       Window.
 
 * @param widget  Widget with size and resize properties.
src/widget_type.h
Show inline comments
 
@@ -749,6 +749,7 @@ public:
 
	}
 

	
 
	int GetScrolledRowFromWidget(int clickpos, const Window * const w, int widget, int padding = 0, int line_height = -1) const;
 
	EventState UpdateListPositionOnKeyPress(int &list_position, uint16 keycode) const;
 
};
 

	
 
/**
0 comments (0 inline, 0 general)