diff options
author | smatz <smatz@openttd.org> | 2008-09-16 19:18:22 +0000 |
---|---|---|
committer | smatz <smatz@openttd.org> | 2008-09-16 19:18:22 +0000 |
commit | 94c5c9f21d3c8053b6a94a4f658b0079a7e34384 (patch) | |
tree | 39b02eff17d2625a4796d83c495e40e60c1dc032 | |
parent | b5fe06f43082608fd41d7fa15b18151e5f8823f4 (diff) | |
download | openttd-94c5c9f21d3c8053b6a94a4f658b0079a7e34384.tar.xz |
(svn r14346) -Codechange [FS#2184]: reduce code duplication when jumping to next/previous sign in signs_gui.cpp (Roujin)
-rw-r--r-- | src/signs_gui.cpp | 63 |
1 files changed, 30 insertions, 33 deletions
diff --git a/src/signs_gui.cpp b/src/signs_gui.cpp index 8b319a721..f687e54c9 100644 --- a/src/signs_gui.cpp +++ b/src/signs_gui.cpp @@ -224,6 +224,33 @@ struct SignWindow : QueryStringBaseWindow, SignList { 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 : QueryStringBaseWindow, SignList { virtual void OnClick(Point pt, int widget) { switch (widget) { - case QUERY_EDIT_SIGN_WIDGET_PREVIOUS: { - /* 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_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 = 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); |