Changeset - r10157:85398815e83e
[Not reviewed]
master
0 1 0
smatz - 16 years ago 2008-09-16 19:18:22
smatz@openttd.org
(svn r14346) -Codechange [FS#2184]: reduce code duplication when jumping to next/previous sign in signs_gui.cpp (Roujin)
1 file changed with 31 insertions and 34 deletions:
0 comments (0 inline, 0 general)
src/signs_gui.cpp
Show inline comments
 
@@ -224,6 +224,33 @@ struct SignWindow : QueryStringBaseWindo
 
		this->InvalidateWidget(QUERY_EDIT_SIGN_WIDGET_TEXT);
 
	}
 

	
 
	/**
 
	 * Returns a pointer to the (alphabetically) previous or next sign of the current sign.
 
	 * @param next false if the previous sign is wanted, true if the next sign is wanted
 
	 * @return pointer to the previous/next sign
 
	 */
 
	const Sign *PrevNextSign(bool next)
 
	{
 
		/* Rebuild the sign list */
 
		this->signs.ForceRebuild();
 
		this->signs.NeedResort();
 
		this->BuildSignsList();
 
		this->SortSignsList();
 

	
 
		/* Search through the list for the current sign, excluding
 
		 * - the first sign if we want the previous sign or
 
		 * - the last sign if we want the next sign */
 
		uint end = this->signs.Length() - (next ? 1 : 0);
 
		for (uint i = next ? 0 : 1; i < end; i++) {
 
			if (this->cur_sign == this->signs[i]->index) {
 
				/* We've found the current sign, so return the sign before/after it */
 
				return this->signs[i + (next ? 1 : -1)];
 
			}
 
		}
 
		/* If we haven't found the current sign by now, return the last/first sign */
 
		return this->signs[next ? 0 : this->signs.Length() - 1];
 
	}
 

	
 
	virtual void OnPaint()
 
	{
 
		SetDParam(0, this->caption);
 
@@ -234,46 +261,16 @@ struct SignWindow : QueryStringBaseWindo
 
	virtual void OnClick(Point pt, int widget)
 
	{
 
		switch (widget) {
 
			case QUERY_EDIT_SIGN_WIDGET_PREVIOUS: {
 
			case QUERY_EDIT_SIGN_WIDGET_PREVIOUS:
 
			case QUERY_EDIT_SIGN_WIDGET_NEXT: {
 
				const Sign *si = this->PrevNextSign(widget == QUERY_EDIT_SIGN_WIDGET_NEXT);
 

	
 
				/* Rebuild the sign list */
 
				this->signs.ForceRebuild();
 
				this->signs.NeedResort();
 
				this->BuildSignsList();
 
				this->SortSignsList();
 

	
 
				/* By default pick the last entry */
 
				const Sign *si = this->signs[this->signs.Length() - 1];
 

	
 
				for (uint i = 1; i < this->signs.Length(); i++) {
 
					if (this->cur_sign == this->signs[i]->index) {
 
						si = this->signs[i - 1];
 
						break;
 
					}
 
				}
 

	
 
				/* Scroll to sign and reopen window */
 
				ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
 
				UpdateSignEditWindow(si);
 
				break;
 
			}
 

	
 
			case QUERY_EDIT_SIGN_WIDGET_NEXT: {
 
				/* Rebuild the sign list */
 
				this->signs.ForceRebuild();
 
				this->signs.NeedResort();
 
				this->BuildSignsList();
 
				this->SortSignsList();
 

	
 
				/* By default pick the last entry */
 
				const Sign *si = this->signs[this->signs.Length() - 1];
 

	
 
				for (uint i = 0; i < this->signs.Length() - 1; i++) {
 
					if (this->cur_sign == this->signs[i]->index) {
 
						si = this->signs[i + 1];
 
						break;
 
					}
 
				}
 

	
 
				/* Scroll to sign and reopen window */
 
				ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
 
				UpdateSignEditWindow(si);
0 comments (0 inline, 0 general)