signs_gui.cpp

Go to the documentation of this file.
00001 /* $Id: signs_gui.cpp 26025 2013-11-17 13:53:33Z rubidium $ */
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 
00164     /* Initialize the filtering variables */
00165     this->SetFilterString("");
00166 
00167     /* Create initial list. */
00168     this->signs.ForceRebuild();
00169     this->signs.ForceResort();
00170     this->BuildSortSignList();
00171   }
00172 
00179   void SetFilterString(const char *new_filter_string)
00180   {
00181     /* check if there is a new filter string */
00182     this->string_filter.SetFilterTerm(new_filter_string);
00183 
00184     /* Rebuild the list of signs */
00185     this->InvalidateData();
00186   }
00187 
00188   virtual void OnPaint()
00189   {
00190     if (this->signs.NeedRebuild()) this->BuildSortSignList();
00191     this->DrawWidgets();
00192   }
00193 
00194   virtual void DrawWidget(const Rect &r, int widget) const
00195   {
00196     switch (widget) {
00197       case WID_SIL_LIST: {
00198         uint y = r.top + WD_FRAMERECT_TOP; // Offset from top of widget.
00199         /* No signs? */
00200         if (this->vscroll->GetCount() == 0) {
00201           DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_STATION_LIST_NONE);
00202           return;
00203         }
00204 
00205         bool rtl = _current_text_dir == TD_RTL;
00206         int sprite_offset_y = (FONT_HEIGHT_NORMAL - 10) / 2 + 1;
00207         uint icon_left  = 4 + (rtl ? r.right - this->text_offset : r.left);
00208         uint text_left  = r.left + (rtl ? WD_FRAMERECT_LEFT : this->text_offset);
00209         uint text_right = r.right - (rtl ? this->text_offset : WD_FRAMERECT_RIGHT);
00210 
00211         /* At least one sign available. */
00212         for (uint16 i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
00213           const Sign *si = this->signs[i];
00214 
00215           if (si->owner != OWNER_NONE) DrawCompanyIcon(si->owner, icon_left, y + sprite_offset_y);
00216 
00217           SetDParam(0, si->index);
00218           DrawString(text_left, text_right, y, STR_SIGN_NAME, TC_YELLOW);
00219           y += this->resize.step_height;
00220         }
00221         break;
00222       }
00223     }
00224   }
00225 
00226   virtual void SetStringParameters(int widget) const
00227   {
00228     if (widget == WID_SIL_CAPTION) SetDParam(0, this->vscroll->GetCount());
00229   }
00230 
00231   virtual void OnClick(Point pt, int widget, int click_count)
00232   {
00233     switch (widget) {
00234       case WID_SIL_LIST: {
00235         uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SIL_LIST, WD_FRAMERECT_TOP);
00236         if (id_v == INT_MAX) return;
00237 
00238         const Sign *si = this->signs[id_v];
00239         ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
00240         break;
00241       }
00242 
00243       case WID_SIL_FILTER_ENTER_BTN:
00244         if (this->signs.Length() >= 1) {
00245           const Sign *si = this->signs[0];
00246           ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
00247         }
00248         break;
00249 
00250       case WID_SIL_FILTER_MATCH_CASE_BTN:
00251         SignList::match_case = !SignList::match_case; // Toggle match case
00252         this->SetWidgetLoweredState(WID_SIL_FILTER_MATCH_CASE_BTN, SignList::match_case); // Toggle button pushed state
00253         this->InvalidateData(); // Rebuild the list of signs
00254         break;
00255     }
00256   }
00257 
00258   virtual void OnResize()
00259   {
00260     this->vscroll->SetCapacityFromWidget(this, WID_SIL_LIST, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
00261   }
00262 
00263   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00264   {
00265     switch (widget) {
00266       case WID_SIL_LIST: {
00267         Dimension spr_dim = GetSpriteSize(SPR_COMPANY_ICON);
00268         this->text_offset = WD_FRAMETEXT_LEFT + spr_dim.width + 2; // 2 pixels space between icon and the sign text.
00269         resize->height = max<uint>(FONT_HEIGHT_NORMAL, spr_dim.height);
00270         Dimension d = {this->text_offset + WD_FRAMETEXT_RIGHT, WD_FRAMERECT_TOP + 5 * resize->height + WD_FRAMERECT_BOTTOM};
00271         *size = maxdim(*size, d);
00272         break;
00273       }
00274 
00275       case WID_SIL_CAPTION:
00276         SetDParamMaxValue(0, Sign::GetPoolSize(), 3);
00277         *size = GetStringBoundingBox(STR_SIGN_LIST_CAPTION);
00278         size->height += padding.height;
00279         size->width  += padding.width;
00280         break;
00281     }
00282   }
00283 
00284   virtual EventState OnKeyPress(WChar key, uint16 keycode)
00285   {
00286     EventState state = ES_NOT_HANDLED;
00287     if (CheckHotkeyMatch(signlist_hotkeys, keycode, this) == SLHK_FOCUS_FILTER_BOX) {
00288       this->SetFocusedWidget(WID_SIL_FILTER_TEXT);
00289       SetFocusedWindow(this); // The user has asked to give focus to the text box, so make sure this window is focused.
00290       state = ES_HANDLED;
00291     }
00292 
00293     return state;
00294   }
00295 
00296   virtual void OnEditboxChanged(int widget)
00297   {
00298     if (widget == WID_SIL_FILTER_TEXT) this->SetFilterString(this->filter_editbox.text.buf);
00299   }
00300 
00301   void BuildSortSignList()
00302   {
00303     if (this->signs.NeedRebuild()) {
00304       this->BuildSignsList();
00305       this->vscroll->SetCount(this->signs.Length());
00306       this->SetWidgetDirty(WID_SIL_CAPTION);
00307     }
00308     this->SortSignsList();
00309   }
00310 
00311   virtual void OnHundredthTick()
00312   {
00313     this->BuildSortSignList();
00314     this->SetDirty();
00315   }
00316 
00322   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00323   {
00324     /* When there is a filter string, we always need to rebuild the list even if
00325      * the amount of signs in total is unchanged, as the subset of signs that is
00326      * accepted by the filter might has changed. */
00327     if (data == 0 || data == -1 || !this->string_filter.IsEmpty()) { // New or deleted sign, changed visibility setting or there is a filter string
00328       /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
00329       this->signs.ForceRebuild();
00330     } else { // Change of sign contents while there is no filter string
00331       this->signs.ForceResort();
00332     }
00333   }
00334 
00335   static Hotkey<SignListWindow> signlist_hotkeys[];
00336 };
00337 
00338 Hotkey<SignListWindow> SignListWindow::signlist_hotkeys[] = {
00339   Hotkey<SignListWindow>('F', "focus_filter_box", SLHK_FOCUS_FILTER_BOX),
00340   HOTKEY_LIST_END(SignListWindow)
00341 };
00342 Hotkey<SignListWindow> *_signlist_hotkeys = SignListWindow::signlist_hotkeys;
00343 
00344 static const NWidgetPart _nested_sign_list_widgets[] = {
00345   NWidget(NWID_HORIZONTAL),
00346     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00347     NWidget(WWT_CAPTION, COLOUR_GREY, WID_SIL_CAPTION), SetDataTip(STR_SIGN_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00348     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00349     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00350   EndContainer(),
00351   NWidget(NWID_HORIZONTAL),
00352     NWidget(NWID_VERTICAL),
00353       NWidget(WWT_PANEL, COLOUR_GREY, WID_SIL_LIST), SetMinimalSize(WD_FRAMETEXT_LEFT + 16 + 255 + WD_FRAMETEXT_RIGHT, 50),
00354                 SetResize(1, 10), SetFill(1, 0), SetScrollbar(WID_SIL_SCROLLBAR), EndContainer(),
00355       NWidget(NWID_HORIZONTAL),
00356         NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1),
00357           NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SIL_FILTER_TEXT), SetMinimalSize(80, 12), SetResize(1, 0), SetFill(1, 0), SetPadding(2, 2, 2, 2),
00358               SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
00359         EndContainer(),
00360         NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SIL_FILTER_MATCH_CASE_BTN), SetDataTip(STR_SIGN_LIST_MATCH_CASE, STR_SIGN_LIST_MATCH_CASE_TOOLTIP),
00361       EndContainer(),
00362     EndContainer(),
00363     NWidget(NWID_VERTICAL),
00364       NWidget(NWID_VERTICAL), SetFill(0, 1),
00365         NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SIL_SCROLLBAR),
00366       EndContainer(),
00367       NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00368     EndContainer(),
00369   EndContainer(),
00370 };
00371 
00372 static const WindowDesc _sign_list_desc(
00373   WDP_AUTO, 358, 138,
00374   WC_SIGN_LIST, WC_NONE,
00375   0,
00376   _nested_sign_list_widgets, lengthof(_nested_sign_list_widgets)
00377 );
00378 
00384 Window *ShowSignList()
00385 {
00386   return AllocateWindowDescFront<SignListWindow>(&_sign_list_desc, 0);
00387 }
00388 
00389 EventState SignListGlobalHotkeys(WChar key, uint16 keycode)
00390 {
00391   int num = CheckHotkeyMatch<SignListWindow>(_signlist_hotkeys, keycode, NULL, true);
00392   if (num == -1) return ES_NOT_HANDLED;
00393   Window *w = ShowSignList();
00394   if (w == NULL) return ES_NOT_HANDLED;
00395   return w->OnKeyPress(key, keycode);
00396 }
00397 
00404 static bool RenameSign(SignID index, const char *text)
00405 {
00406   bool remove = StrEmpty(text);
00407   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);
00408   return remove;
00409 }
00410 
00411 struct SignWindow : Window, SignList {
00412   QueryString name_editbox;
00413   SignID cur_sign;
00414 
00415   SignWindow(const WindowDesc *desc, const Sign *si) : name_editbox(MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_SIGN_NAME_CHARS)
00416   {
00417     this->querystrings[WID_QES_TEXT] = &this->name_editbox;
00418     this->name_editbox.caption = STR_EDIT_SIGN_CAPTION;
00419     this->name_editbox.cancel_button = WID_QES_CANCEL;
00420     this->name_editbox.ok_button = WID_QES_OK;
00421 
00422     this->InitNested(desc, WN_QUERY_STRING_SIGN);
00423 
00424     UpdateSignEditWindow(si);
00425     this->SetFocusedWidget(WID_QES_TEXT);
00426   }
00427 
00428   void UpdateSignEditWindow(const Sign *si)
00429   {
00430     /* Display an empty string when the sign hasn't been edited yet */
00431     if (si->name != NULL) {
00432       SetDParam(0, si->index);
00433       this->name_editbox.text.Assign(STR_SIGN_NAME);
00434     } else {
00435       this->name_editbox.text.DeleteAll();
00436     }
00437 
00438     this->cur_sign = si->index;
00439 
00440     this->SetWidgetDirty(WID_QES_TEXT);
00441     this->SetFocusedWidget(WID_QES_TEXT);
00442   }
00443 
00449   const Sign *PrevNextSign(bool next)
00450   {
00451     /* Rebuild the sign list */
00452     this->signs.ForceRebuild();
00453     this->signs.NeedResort();
00454     this->BuildSignsList();
00455     this->SortSignsList();
00456 
00457     /* Search through the list for the current sign, excluding
00458      * - the first sign if we want the previous sign or
00459      * - the last sign if we want the next sign */
00460     uint end = this->signs.Length() - (next ? 1 : 0);
00461     for (uint i = next ? 0 : 1; i < end; i++) {
00462       if (this->cur_sign == this->signs[i]->index) {
00463         /* We've found the current sign, so return the sign before/after it */
00464         return this->signs[i + (next ? 1 : -1)];
00465       }
00466     }
00467     /* If we haven't found the current sign by now, return the last/first sign */
00468     return this->signs[next ? 0 : this->signs.Length() - 1];
00469   }
00470 
00471   virtual void SetStringParameters(int widget) const
00472   {
00473     switch (widget) {
00474       case WID_QES_CAPTION:
00475         SetDParam(0, this->name_editbox.caption);
00476         break;
00477     }
00478   }
00479 
00480   virtual void OnClick(Point pt, int widget, int click_count)
00481   {
00482     switch (widget) {
00483       case WID_QES_PREVIOUS:
00484       case WID_QES_NEXT: {
00485         const Sign *si = this->PrevNextSign(widget == WID_QES_NEXT);
00486 
00487         /* Rebuild the sign list */
00488         this->signs.ForceRebuild();
00489         this->signs.NeedResort();
00490         this->BuildSignsList();
00491         this->SortSignsList();
00492 
00493         /* Scroll to sign and reopen window */
00494         ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
00495         UpdateSignEditWindow(si);
00496         break;
00497       }
00498 
00499       case WID_QES_DELETE:
00500         /* Only need to set the buffer to null, the rest is handled as the OK button */
00501         RenameSign(this->cur_sign, "");
00502         /* don't delete this, we are deleted in Sign::~Sign() -> DeleteRenameSignWindow() */
00503         break;
00504 
00505       case WID_QES_OK:
00506         if (RenameSign(this->cur_sign, this->name_editbox.text.buf)) break;
00507         /* FALL THROUGH */
00508 
00509       case WID_QES_CANCEL:
00510         delete this;
00511         break;
00512     }
00513   }
00514 };
00515 
00516 static const NWidgetPart _nested_query_sign_edit_widgets[] = {
00517   NWidget(NWID_HORIZONTAL),
00518     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00519     NWidget(WWT_CAPTION, COLOUR_GREY, WID_QES_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00520   EndContainer(),
00521   NWidget(WWT_PANEL, COLOUR_GREY),
00522     NWidget(WWT_EDITBOX, COLOUR_GREY, WID_QES_TEXT), SetMinimalSize(256, 12), SetDataTip(STR_EDIT_SIGN_SIGN_OSKTITLE, STR_NULL), SetPadding(2, 2, 2, 2),
00523   EndContainer(),
00524   NWidget(NWID_HORIZONTAL),
00525     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_OK), SetMinimalSize(61, 12), SetDataTip(STR_BUTTON_OK, STR_NULL),
00526     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_CANCEL), SetMinimalSize(60, 12), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
00527     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_DELETE), SetMinimalSize(60, 12), SetDataTip(STR_TOWN_VIEW_DELETE_BUTTON, STR_NULL),
00528     NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), EndContainer(),
00529     NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_QES_PREVIOUS), SetMinimalSize(11, 12), SetDataTip(AWV_DECREASE, STR_EDIT_SIGN_PREVIOUS_SIGN_TOOLTIP),
00530     NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_QES_NEXT), SetMinimalSize(11, 12), SetDataTip(AWV_INCREASE, STR_EDIT_SIGN_NEXT_SIGN_TOOLTIP),
00531   EndContainer(),
00532 };
00533 
00534 static const WindowDesc _query_sign_edit_desc(
00535   WDP_CENTER, 0, 0,
00536   WC_QUERY_STRING, WC_NONE,
00537   WDF_CONSTRUCTION,
00538   _nested_query_sign_edit_widgets, lengthof(_nested_query_sign_edit_widgets)
00539 );
00540 
00545 void HandleClickOnSign(const Sign *si)
00546 {
00547   if (_ctrl_pressed && (si->owner == _local_company || (si->owner == OWNER_DEITY && _game_mode == GM_EDITOR))) {
00548     RenameSign(si->index, NULL);
00549     return;
00550   }
00551   ShowRenameSignWindow(si);
00552 }
00553 
00558 void ShowRenameSignWindow(const Sign *si)
00559 {
00560   /* Delete all other edit windows */
00561   DeleteWindowByClass(WC_QUERY_STRING);
00562 
00563   new SignWindow(&_query_sign_edit_desc, si);
00564 }
00565 
00570 void DeleteRenameSignWindow(SignID sign)
00571 {
00572   SignWindow *w = dynamic_cast<SignWindow *>(FindWindowById(WC_QUERY_STRING, WN_QUERY_STRING_SIGN));
00573 
00574   if (w != NULL && w->cur_sign == sign) delete w;
00575 }