signs_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "company_gui.h"
00014 #include "company_func.h"
00015 #include "signs_base.h"
00016 #include "signs_func.h"
00017 #include "debug.h"
00018 #include "command_func.h"
00019 #include "strings_func.h"
00020 #include "window_func.h"
00021 #include "map_func.h"
00022 #include "viewport_func.h"
00023 #include "querystring_gui.h"
00024 #include "sortlist_type.h"
00025 #include "stringfilter_type.h"
00026 #include "string_func.h"
00027 #include "core/geometry_func.hpp"
00028 #include "hotkeys.h"
00029 #include "transparency.h"
00030 
00031 #include "widgets/sign_widget.h"
00032 
00033 #include "table/strings.h"
00034 #include "table/sprites.h"
00035 
00036 struct SignList {
00040   typedef GUIList<const Sign *, StringFilter &> GUISignList;
00041 
00042   static const Sign *last_sign;
00043   GUISignList signs;
00044 
00045   StringFilter string_filter;                                       
00046   static bool match_case;                                           
00047 
00051   SignList() : string_filter(&match_case)
00052   {
00053   }
00054 
00055   void BuildSignsList()
00056   {
00057     if (!this->signs.NeedRebuild()) return;
00058 
00059     DEBUG(misc, 3, "Building sign list");
00060 
00061     this->signs.Clear();
00062 
00063     const Sign *si;
00064     FOR_ALL_SIGNS(si) *this->signs.Append() = si;
00065 
00066     this->signs.SetFilterState(true);
00067     this->FilterSignList();
00068     this->signs.Compact();
00069     this->signs.RebuildDone();
00070   }
00071 
00073   static int CDECL SignNameSorter(const Sign * const *a, const Sign * const *b)
00074   {
00075     static char buf_cache[64];
00076     char buf[64];
00077 
00078     SetDParam(0, (*a)->index);
00079     GetString(buf, STR_SIGN_NAME, lastof(buf));
00080 
00081     if (*b != last_sign) {
00082       last_sign = *b;
00083       SetDParam(0, (*b)->index);
00084       GetString(buf_cache, STR_SIGN_NAME, lastof(buf_cache));
00085     }
00086 
00087     int r = strnatcmp(buf, buf_cache); // Sort by name (natural sorting).
00088 
00089     return r != 0 ? r : ((*a)->index - (*b)->index);
00090   }
00091 
00092   void SortSignsList()
00093   {
00094     if (!this->signs.Sort(&SignNameSorter)) return;
00095 
00096     /* Reset the name sorter sort cache */
00097     this->last_sign = NULL;
00098   }
00099 
00101   static bool CDECL SignNameFilter(const Sign * const *a, StringFilter &filter)
00102   {
00103     /* Get sign string */
00104     char buf1[MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH];
00105     SetDParam(0, (*a)->index);
00106     GetString(buf1, STR_SIGN_NAME, lastof(buf1));
00107 
00108     filter.ResetState();
00109     filter.AddLine(buf1);
00110     return filter.GetState();
00111   }
00112 
00114   static bool CDECL OwnerDeityFilter(const Sign * const *a, StringFilter &filter)
00115   {
00116     /* You should never be able to edit signs of owner DEITY */
00117     return (*a)->owner != OWNER_DEITY;
00118   }
00119 
00121   static bool CDECL OwnerVisibilityFilter(const Sign * const *a, StringFilter &filter)
00122   {
00123     assert(!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS));
00124     /* Hide sign if non-own signs are hidden in the viewport */
00125     return (*a)->owner == _local_company || (*a)->owner == OWNER_DEITY;
00126   }
00127 
00129   void FilterSignList()
00130   {
00131     this->signs.Filter(&SignNameFilter, this->string_filter);
00132     if (_game_mode != GM_EDITOR) this->signs.Filter(&OwnerDeityFilter, this->string_filter);
00133     if (!HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS)) {
00134       this->signs.Filter(&OwnerVisibilityFilter, this->string_filter);
00135     }
00136   }
00137 };
00138 
00139 const Sign *SignList::last_sign = NULL;
00140 bool SignList::match_case = false;
00141 
00143 enum SignListHotkeys {
00144   SLHK_FOCUS_FILTER_BOX, 
00145 };
00146 
00147 struct SignListWindow : Window, SignList {
00148   QueryString filter_editbox; 
00149   int text_offset; 
00150   Scrollbar *vscroll;
00151 
00152   SignListWindow(const WindowDesc *desc, WindowNumber window_number) : filter_editbox(MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_SIGN_NAME_CHARS)
00153   {
00154     this->CreateNestedTree(desc);
00155     this->vscroll = this->GetScrollbar(WID_SIL_SCROLLBAR);
00156     this->FinishInitNested(desc, window_number);
00157     this->SetWidgetLoweredState(WID_SIL_FILTER_MATCH_CASE_BTN, SignList::match_case);
00158 
00159     /* Initialize the text edit widget */
00160     this->querystrings[WID_SIL_FILTER_TEXT] = &this->filter_editbox;
00161     this->filter_editbox.ok_button = WID_SIL_FILTER_ENTER_BTN;
00162     this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
00163     this->filter_editbox.afilter = CS_ALPHANUMERAL;
00164 
00165     /* Initialize the filtering variables */
00166     this->SetFilterString("");
00167 
00168     /* Create initial list. */
00169     this->signs.ForceRebuild();
00170     this->signs.ForceResort();
00171     this->BuildSortSignList();
00172   }
00173 
00180   void SetFilterString(const char *new_filter_string)
00181   {
00182     /* check if there is a new filter string */
00183     this->string_filter.SetFilterTerm(new_filter_string);
00184 
00185     /* Rebuild the list of signs */
00186     this->InvalidateData();
00187   }
00188 
00189   virtual void OnPaint()
00190   {
00191     if (this->signs.NeedRebuild()) this->BuildSortSignList();
00192     this->DrawWidgets();
00193   }
00194 
00195   virtual void DrawWidget(const Rect &r, int widget) const
00196   {
00197     switch (widget) {
00198       case WID_SIL_LIST: {
00199         uint y = r.top + WD_FRAMERECT_TOP; // Offset from top of widget.
00200         /* No signs? */
00201         if (this->vscroll->GetCount() == 0) {
00202           DrawString(r.left + WD_FRAMETEXT_LEFT, r.right, y, STR_STATION_LIST_NONE);
00203           return;
00204         }
00205 
00206         bool rtl = _current_text_dir == TD_RTL;
00207         int sprite_offset_y = (FONT_HEIGHT_NORMAL - 10) / 2 + 1;
00208         uint icon_left  = 4 + (rtl ? r.right - this->text_offset : r.left);
00209         uint text_left  = r.left + (rtl ? WD_FRAMERECT_LEFT : this->text_offset);
00210         uint text_right = r.right - (rtl ? this->text_offset : WD_FRAMERECT_RIGHT);
00211 
00212         /* At least one sign available. */
00213         for (uint16 i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
00214           const Sign *si = this->signs[i];
00215 
00216           if (si->owner != OWNER_NONE) DrawCompanyIcon(si->owner, icon_left, y + sprite_offset_y);
00217 
00218           SetDParam(0, si->index);
00219           DrawString(text_left, text_right, y, STR_SIGN_NAME, TC_YELLOW);
00220           y += this->resize.step_height;
00221         }
00222         break;
00223       }
00224     }
00225   }
00226 
00227   virtual void SetStringParameters(int widget) const
00228   {
00229     if (widget == WID_SIL_CAPTION) SetDParam(0, this->vscroll->GetCount());
00230   }
00231 
00232   virtual void OnClick(Point pt, int widget, int click_count)
00233   {
00234     switch (widget) {
00235       case WID_SIL_LIST: {
00236         uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SIL_LIST, WD_FRAMERECT_TOP);
00237         if (id_v == INT_MAX) return;
00238 
00239         const Sign *si = this->signs[id_v];
00240         ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
00241         break;
00242       }
00243 
00244       case WID_SIL_FILTER_ENTER_BTN:
00245         if (this->signs.Length() >= 1) {
00246           const Sign *si = this->signs[0];
00247           ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
00248         }
00249         break;
00250 
00251       case WID_SIL_FILTER_MATCH_CASE_BTN:
00252         SignList::match_case = !SignList::match_case; // Toggle match case
00253         this->SetWidgetLoweredState(WID_SIL_FILTER_MATCH_CASE_BTN, SignList::match_case); // Toggle button pushed state
00254         this->InvalidateData(); // Rebuild the list of signs
00255         break;
00256     }
00257   }
00258 
00259   virtual void OnResize()
00260   {
00261     this->vscroll->SetCapacityFromWidget(this, WID_SIL_LIST, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
00262   }
00263 
00264   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00265   {
00266     switch (widget) {
00267       case WID_SIL_LIST: {
00268         Dimension spr_dim = GetSpriteSize(SPR_COMPANY_ICON);
00269         this->text_offset = WD_FRAMETEXT_LEFT + spr_dim.width + 2; // 2 pixels space between icon and the sign text.
00270         resize->height = max<uint>(FONT_HEIGHT_NORMAL, spr_dim.height);
00271         Dimension d = {this->text_offset + WD_FRAMETEXT_RIGHT, WD_FRAMERECT_TOP + 5 * resize->height + WD_FRAMERECT_BOTTOM};
00272         *size = maxdim(*size, d);
00273         break;
00274       }
00275 
00276       case WID_SIL_CAPTION:
00277         SetDParam(0, max<size_t>(1000, Sign::GetPoolSize()));
00278         *size = GetStringBoundingBox(STR_SIGN_LIST_CAPTION);
00279         size->height += padding.height;
00280         size->width  += padding.width;
00281         break;
00282     }
00283   }
00284 
00285   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00286   {
00287     EventState state = ES_NOT_HANDLED;
00288     if (CheckHotkeyMatch(signlist_hotkeys, keycode, this) == SLHK_FOCUS_FILTER_BOX) {
00289       this->SetFocusedWidget(WID_SIL_FILTER_TEXT);
00290       SetFocusedWindow(this); // The user has asked to give focus to the text box, so make sure this window is focused.
00291       state = ES_HANDLED;
00292     }
00293 
00294     return state;
00295   }
00296 
00297   virtual void OnEditboxChanged(int widget)
00298   {
00299     if (widget == WID_SIL_FILTER_TEXT) this->SetFilterString(this->filter_editbox.text.buf);
00300   }
00301 
00302   void BuildSortSignList()
00303   {
00304     if (this->signs.NeedRebuild()) {
00305       this->BuildSignsList();
00306       this->vscroll->SetCount(this->signs.Length());
00307       this->SetWidgetDirty(WID_SIL_CAPTION);
00308     }
00309     this->SortSignsList();
00310   }
00311 
00312   virtual void OnHundredthTick()
00313   {
00314     this->BuildSortSignList();
00315     this->SetDirty();
00316   }
00317 
00323   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00324   {
00325     /* When there is a filter string, we always need to rebuild the list even if
00326      * the amount of signs in total is unchanged, as the subset of signs that is
00327      * accepted by the filter might has changed. */
00328     if (data == 0 || data == -1 || !this->string_filter.IsEmpty()) { // New or deleted sign, changed visibility setting or there is a filter string
00329       /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
00330       this->signs.ForceRebuild();
00331     } else { // Change of sign contents while there is no filter string
00332       this->signs.ForceResort();
00333     }
00334   }
00335 
00336   static Hotkey<SignListWindow> signlist_hotkeys[];
00337 };
00338 
00339 Hotkey<SignListWindow> SignListWindow::signlist_hotkeys[] = {
00340   Hotkey<SignListWindow>('F', "focus_filter_box", SLHK_FOCUS_FILTER_BOX),
00341   HOTKEY_LIST_END(SignListWindow)
00342 };
00343 Hotkey<SignListWindow> *_signlist_hotkeys = SignListWindow::signlist_hotkeys;
00344 
00345 static const NWidgetPart _nested_sign_list_widgets[] = {
00346   NWidget(NWID_HORIZONTAL),
00347     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00348     NWidget(WWT_CAPTION, COLOUR_GREY, WID_SIL_CAPTION), SetDataTip(STR_SIGN_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00349     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00350     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00351   EndContainer(),
00352   NWidget(NWID_HORIZONTAL),
00353     NWidget(NWID_VERTICAL),
00354       NWidget(WWT_PANEL, COLOUR_GREY, WID_SIL_LIST), SetMinimalSize(WD_FRAMETEXT_LEFT + 16 + 255 + WD_FRAMETEXT_RIGHT, 50),
00355                 SetResize(1, 10), SetFill(1, 0), SetScrollbar(WID_SIL_SCROLLBAR), EndContainer(),
00356       NWidget(NWID_HORIZONTAL),
00357         NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1),
00358           NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SIL_FILTER_TEXT), SetMinimalSize(80, 12), SetResize(1, 0), SetFill(1, 0), SetPadding(2, 2, 2, 2),
00359               SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
00360         EndContainer(),
00361         NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SIL_FILTER_MATCH_CASE_BTN), SetDataTip(STR_SIGN_LIST_MATCH_CASE, STR_SIGN_LIST_MATCH_CASE_TOOLTIP),
00362       EndContainer(),
00363     EndContainer(),
00364     NWidget(NWID_VERTICAL),
00365       NWidget(NWID_VERTICAL), SetFill(0, 1),
00366         NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SIL_SCROLLBAR),
00367       EndContainer(),
00368       NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00369     EndContainer(),
00370   EndContainer(),
00371 };
00372 
00373 static const WindowDesc _sign_list_desc(
00374   WDP_AUTO, 358, 138,
00375   WC_SIGN_LIST, WC_NONE,
00376   0,
00377   _nested_sign_list_widgets, lengthof(_nested_sign_list_widgets)
00378 );
00379 
00385 Window *ShowSignList()
00386 {
00387   return AllocateWindowDescFront<SignListWindow>(&_sign_list_desc, 0);
00388 }
00389 
00390 EventState SignListGlobalHotkeys(uint16 key, uint16 keycode)
00391 {
00392   int num = CheckHotkeyMatch<SignListWindow>(_signlist_hotkeys, keycode, NULL, true);
00393   if (num == -1) return ES_NOT_HANDLED;
00394   Window *w = ShowSignList();
00395   if (w == NULL) return ES_NOT_HANDLED;
00396   return w->OnKeyPress(key, keycode);
00397 }
00398 
00405 static bool RenameSign(SignID index, const char *text)
00406 {
00407   bool remove = StrEmpty(text);
00408   DoCommandP(0, index, 0, CMD_RENAME_SIGN | (StrEmpty(text) ? CMD_MSG(STR_ERROR_CAN_T_DELETE_SIGN) : CMD_MSG(STR_ERROR_CAN_T_CHANGE_SIGN_NAME)), NULL, text);
00409   return remove;
00410 }
00411 
00412 struct SignWindow : Window, SignList {
00413   QueryString name_editbox;
00414   SignID cur_sign;
00415 
00416   SignWindow(const WindowDesc *desc, const Sign *si) : name_editbox(MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_SIGN_NAME_CHARS)
00417   {
00418     this->querystrings[WID_QES_TEXT] = &this->name_editbox;
00419     this->name_editbox.caption = STR_EDIT_SIGN_CAPTION;
00420     this->name_editbox.cancel_button = WID_QES_CANCEL;
00421     this->name_editbox.ok_button = WID_QES_OK;
00422     this->name_editbox.afilter = CS_ALPHANUMERAL;
00423 
00424     this->InitNested(desc, WN_QUERY_STRING_SIGN);
00425 
00426     UpdateSignEditWindow(si);
00427     this->SetFocusedWidget(WID_QES_TEXT);
00428   }
00429 
00430   void UpdateSignEditWindow(const Sign *si)
00431   {
00432     /* Display an empty string when the sign hasnt been edited yet */
00433     if (si->name != NULL) {
00434       SetDParam(0, si->index);
00435       this->name_editbox.text.Assign(STR_SIGN_NAME);
00436     } else {
00437       this->name_editbox.text.DeleteAll();
00438     }
00439 
00440     this->cur_sign = si->index;
00441 
00442     this->SetWidgetDirty(WID_QES_TEXT);
00443     this->SetFocusedWidget(WID_QES_TEXT);
00444   }
00445 
00451   const Sign *PrevNextSign(bool next)
00452   {
00453     /* Rebuild the sign list */
00454     this->signs.ForceRebuild();
00455     this->signs.NeedResort();
00456     this->BuildSignsList();
00457     this->SortSignsList();
00458 
00459     /* Search through the list for the current sign, excluding
00460      * - the first sign if we want the previous sign or
00461      * - the last sign if we want the next sign */
00462     uint end = this->signs.Length() - (next ? 1 : 0);
00463     for (uint i = next ? 0 : 1; i < end; i++) {
00464       if (this->cur_sign == this->signs[i]->index) {
00465         /* We've found the current sign, so return the sign before/after it */
00466         return this->signs[i + (next ? 1 : -1)];
00467       }
00468     }
00469     /* If we haven't found the current sign by now, return the last/first sign */
00470     return this->signs[next ? 0 : this->signs.Length() - 1];
00471   }
00472 
00473   virtual void SetStringParameters(int widget) const
00474   {
00475     switch (widget) {
00476       case WID_QES_CAPTION:
00477         SetDParam(0, this->name_editbox.caption);
00478         break;
00479     }
00480   }
00481 
00482   virtual void OnClick(Point pt, int widget, int click_count)
00483   {
00484     switch (widget) {
00485       case WID_QES_PREVIOUS:
00486       case WID_QES_NEXT: {
00487         const Sign *si = this->PrevNextSign(widget == WID_QES_NEXT);
00488 
00489         /* Rebuild the sign list */
00490         this->signs.ForceRebuild();
00491         this->signs.NeedResort();
00492         this->BuildSignsList();
00493         this->SortSignsList();
00494 
00495         /* Scroll to sign and reopen window */
00496         ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
00497         UpdateSignEditWindow(si);
00498         break;
00499       }
00500 
00501       case WID_QES_DELETE:
00502         /* Only need to set the buffer to null, the rest is handled as the OK button */
00503         RenameSign(this->cur_sign, "");
00504         /* don't delete this, we are deleted in Sign::~Sign() -> DeleteRenameSignWindow() */
00505         break;
00506 
00507       case WID_QES_OK:
00508         if (RenameSign(this->cur_sign, this->name_editbox.text.buf)) break;
00509         /* FALL THROUGH */
00510 
00511       case WID_QES_CANCEL:
00512         delete this;
00513         break;
00514     }
00515   }
00516 };
00517 
00518 static const NWidgetPart _nested_query_sign_edit_widgets[] = {
00519   NWidget(NWID_HORIZONTAL),
00520     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00521     NWidget(WWT_CAPTION, COLOUR_GREY, WID_QES_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00522   EndContainer(),
00523   NWidget(WWT_PANEL, COLOUR_GREY),
00524     NWidget(WWT_EDITBOX, COLOUR_GREY, WID_QES_TEXT), SetMinimalSize(256, 12), SetDataTip(STR_EDIT_SIGN_SIGN_OSKTITLE, STR_NULL), SetPadding(2, 2, 2, 2),
00525   EndContainer(),
00526   NWidget(NWID_HORIZONTAL),
00527     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_OK), SetMinimalSize(61, 12), SetDataTip(STR_BUTTON_OK, STR_NULL),
00528     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_CANCEL), SetMinimalSize(60, 12), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
00529     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_DELETE), SetMinimalSize(60, 12), SetDataTip(STR_TOWN_VIEW_DELETE_BUTTON, STR_NULL),
00530     NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), EndContainer(),
00531     NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_QES_PREVIOUS), SetMinimalSize(11, 12), SetDataTip(AWV_DECREASE, STR_EDIT_SIGN_PREVIOUS_SIGN_TOOLTIP),
00532     NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_QES_NEXT), SetMinimalSize(11, 12), SetDataTip(AWV_INCREASE, STR_EDIT_SIGN_NEXT_SIGN_TOOLTIP),
00533   EndContainer(),
00534 };
00535 
00536 static const WindowDesc _query_sign_edit_desc(
00537   WDP_AUTO, 0, 0,
00538   WC_QUERY_STRING, WC_NONE,
00539   WDF_CONSTRUCTION,
00540   _nested_query_sign_edit_widgets, lengthof(_nested_query_sign_edit_widgets)
00541 );
00542 
00547 void HandleClickOnSign(const Sign *si)
00548 {
00549   if (_ctrl_pressed && (si->owner == _local_company || (si->owner == OWNER_DEITY && _game_mode == GM_EDITOR))) {
00550     RenameSign(si->index, NULL);
00551     return;
00552   }
00553   ShowRenameSignWindow(si);
00554 }
00555 
00560 void ShowRenameSignWindow(const Sign *si)
00561 {
00562   /* Delete all other edit windows */
00563   DeleteWindowByClass(WC_QUERY_STRING);
00564 
00565   new SignWindow(&_query_sign_edit_desc, si);
00566 }
00567 
00572 void DeleteRenameSignWindow(SignID sign)
00573 {
00574   SignWindow *w = dynamic_cast<SignWindow *>(FindWindowById(WC_QUERY_STRING, WN_QUERY_STRING_SIGN));
00575 
00576   if (w != NULL && w->cur_sign == sign) delete w;
00577 }