signs_gui.cpp

Go to the documentation of this file.
00001 /* $Id: signs_gui.cpp 20092 2010-07-08 19:44:00Z 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 "gfx_func.h"
00023 #include "viewport_func.h"
00024 #include "querystring_gui.h"
00025 #include "sortlist_type.h"
00026 #include "string_func.h"
00027 #include "core/geometry_func.hpp"
00028 
00029 #include "table/strings.h"
00030 #include "table/sprites.h"
00031 
00032 struct SignList {
00033   typedef GUIList<const Sign *> GUISignList;
00034 
00035   static const Sign *last_sign;
00036   GUISignList signs;
00037 
00038   void BuildSignsList()
00039   {
00040     if (!this->signs.NeedRebuild()) return;
00041 
00042     DEBUG(misc, 3, "Building sign list");
00043 
00044     this->signs.Clear();
00045 
00046     const Sign *si;
00047     FOR_ALL_SIGNS(si) *this->signs.Append() = si;
00048 
00049     this->signs.Compact();
00050     this->signs.RebuildDone();
00051   }
00052 
00054   static int CDECL SignNameSorter(const Sign * const *a, const Sign * const *b)
00055   {
00056     static char buf_cache[64];
00057     char buf[64];
00058 
00059     SetDParam(0, (*a)->index);
00060     GetString(buf, STR_SIGN_NAME, lastof(buf));
00061 
00062     if (*b != last_sign) {
00063       last_sign = *b;
00064       SetDParam(0, (*b)->index);
00065       GetString(buf_cache, STR_SIGN_NAME, lastof(buf_cache));
00066     }
00067 
00068     int r = strcasecmp(buf, buf_cache);
00069 
00070     return r != 0 ? r : ((*a)->index - (*b)->index);
00071   }
00072 
00073   void SortSignsList()
00074   {
00075     if (!this->signs.Sort(&SignNameSorter)) return;
00076 
00077     /* Reset the name sorter sort cache */
00078     this->last_sign = NULL;
00079   }
00080 };
00081 
00082 const Sign *SignList::last_sign = NULL;
00083 
00085 enum SignListWidgets {
00086   SLW_CAPTION,
00087   SLW_LIST,
00088   SLW_SCROLLBAR,
00089 };
00090 
00091 struct SignListWindow : Window, SignList {
00092   int text_offset; // Offset of the sign text relative to the left edge of the SLW_LIST widget.
00093 
00094   SignListWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
00095   {
00096     this->InitNested(desc, window_number);
00097 
00098     /* Create initial list. */
00099     this->signs.ForceRebuild();
00100     this->signs.ForceResort();
00101     this->BuildSignsList();
00102     this->SortSignsList();
00103     this->vscroll.SetCount(this->signs.Length());
00104   }
00105 
00106   virtual void OnPaint()
00107   {
00108     this->DrawWidgets();
00109   }
00110 
00111   virtual void DrawWidget(const Rect &r, int widget) const
00112   {
00113     switch (widget) {
00114       case SLW_LIST: {
00115         uint y = r.top + WD_FRAMERECT_TOP; // Offset from top of widget.
00116         /* No signs? */
00117         if (this->vscroll.GetCount() == 0) {
00118           DrawString(r.left + WD_FRAMETEXT_LEFT, r.right, y, STR_STATION_LIST_NONE);
00119           return;
00120         }
00121 
00122         bool rtl = _dynlang.text_dir == TD_RTL;
00123         int sprite_offset_y = (FONT_HEIGHT_NORMAL - 10) / 2 + 1;
00124         uint icon_left  = 4 + (rtl ? r.right - this->text_offset : r.left);
00125         uint text_left  = r.left + (rtl ? WD_FRAMERECT_LEFT : this->text_offset);
00126         uint text_right = r.right - (rtl ? this->text_offset : WD_FRAMERECT_RIGHT);
00127 
00128         /* At least one sign available. */
00129         for (uint16 i = this->vscroll.GetPosition(); this->vscroll.IsVisible(i) && i < this->vscroll.GetCount(); i++) {
00130           const Sign *si = this->signs[i];
00131 
00132           if (si->owner != OWNER_NONE) DrawCompanyIcon(si->owner, icon_left, y + sprite_offset_y);
00133 
00134           SetDParam(0, si->index);
00135           DrawString(text_left, text_right, y, STR_SIGN_NAME, TC_YELLOW);
00136           y += this->resize.step_height;
00137         }
00138         break;
00139       }
00140     }
00141   }
00142 
00143   virtual void SetStringParameters(int widget) const
00144   {
00145     if (widget == SLW_CAPTION) SetDParam(0, this->vscroll.GetCount());
00146   }
00147 
00148   virtual void OnClick(Point pt, int widget, int click_count)
00149   {
00150     if (widget == SLW_LIST) {
00151       uint id_v = (pt.y - this->GetWidget<NWidgetBase>(SLW_LIST)->pos_y - WD_FRAMERECT_TOP) / this->resize.step_height;
00152 
00153       if (id_v >= this->vscroll.GetCapacity()) return;
00154       id_v += this->vscroll.GetPosition();
00155       if (id_v >= this->vscroll.GetCount()) return;
00156 
00157       const Sign *si = this->signs[id_v];
00158       ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
00159     }
00160   }
00161 
00162   virtual void OnResize()
00163   {
00164     this->vscroll.SetCapacityFromWidget(this, SLW_LIST, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
00165   }
00166 
00167   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00168   {
00169     switch (widget) {
00170       case SLW_LIST: {
00171         Dimension spr_dim = GetSpriteSize(SPR_COMPANY_ICON);
00172         this->text_offset = WD_FRAMETEXT_LEFT + spr_dim.width + 2; // 2 pixels space between icon and the sign text.
00173         resize->height = max<uint>(FONT_HEIGHT_NORMAL, GetSpriteSize(SPR_COMPANY_ICON).height);
00174         Dimension d = {this->text_offset + MAX_LENGTH_SIGN_NAME_PIXELS + WD_FRAMETEXT_RIGHT, WD_FRAMERECT_TOP + 5 * resize->height + WD_FRAMERECT_BOTTOM};
00175         *size = maxdim(*size, d);
00176       } break;
00177 
00178       case SLW_CAPTION:
00179         SetDParam(0, max<size_t>(1000, Sign::GetPoolSize()));
00180         *size = GetStringBoundingBox(STR_SIGN_LIST_CAPTION);
00181         size->height += padding.height;
00182         size->width  += padding.width;
00183         break;
00184     }
00185   }
00186 
00187   virtual void OnInvalidateData(int data)
00188   {
00189     if (data == 0) { // New or deleted sign.
00190       this->signs.ForceRebuild();
00191       this->BuildSignsList();
00192       this->SetWidgetDirty(SLW_CAPTION);
00193       this->vscroll.SetCount(this->signs.Length());
00194     } else { // Change of sign contents.
00195       this->signs.ForceResort();
00196     }
00197 
00198     this->SortSignsList();
00199   }
00200 };
00201 
00202 static const NWidgetPart _nested_sign_list_widgets[] = {
00203   NWidget(NWID_HORIZONTAL),
00204     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00205     NWidget(WWT_CAPTION, COLOUR_GREY, SLW_CAPTION), SetDataTip(STR_SIGN_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00206     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00207     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00208   EndContainer(),
00209   NWidget(NWID_HORIZONTAL),
00210     NWidget(WWT_PANEL, COLOUR_GREY, SLW_LIST), SetMinimalSize(WD_FRAMETEXT_LEFT + 16 + MAX_LENGTH_SIGN_NAME_PIXELS + WD_FRAMETEXT_RIGHT, 50),
00211               SetResize(1, 10), SetFill(1, 0), EndContainer(),
00212     NWidget(NWID_VERTICAL),
00213       NWidget(WWT_SCROLLBAR, COLOUR_GREY, SLW_SCROLLBAR),
00214       NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00215     EndContainer(),
00216   EndContainer(),
00217 };
00218 
00219 static const WindowDesc _sign_list_desc(
00220   WDP_AUTO, 358, 138,
00221   WC_SIGN_LIST, WC_NONE,
00222   0,
00223   _nested_sign_list_widgets, lengthof(_nested_sign_list_widgets)
00224 );
00225 
00226 
00227 void ShowSignList()
00228 {
00229   AllocateWindowDescFront<SignListWindow>(&_sign_list_desc, 0);
00230 }
00231 
00238 static bool RenameSign(SignID index, const char *text)
00239 {
00240   bool remove = StrEmpty(text);
00241   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);
00242   return remove;
00243 }
00244 
00246 enum QueryEditSignWidgets {
00247   QUERY_EDIT_SIGN_WIDGET_CAPTION,
00248   QUERY_EDIT_SIGN_WIDGET_TEXT,
00249   QUERY_EDIT_SIGN_WIDGET_OK,
00250   QUERY_EDIT_SIGN_WIDGET_CANCEL,
00251   QUERY_EDIT_SIGN_WIDGET_DELETE,
00252   QUERY_EDIT_SIGN_WIDGET_PREVIOUS,
00253   QUERY_EDIT_SIGN_WIDGET_NEXT,
00254 };
00255 
00256 struct SignWindow : QueryStringBaseWindow, SignList {
00257   SignID cur_sign;
00258 
00259   SignWindow(const WindowDesc *desc, const Sign *si) : QueryStringBaseWindow(MAX_LENGTH_SIGN_NAME_BYTES)
00260   {
00261     this->caption = STR_EDIT_SIGN_CAPTION;
00262     this->afilter = CS_ALPHANUMERAL;
00263 
00264     this->InitNested(desc);
00265 
00266     this->LowerWidget(QUERY_EDIT_SIGN_WIDGET_TEXT);
00267     UpdateSignEditWindow(si);
00268     this->SetFocusedWidget(QUERY_EDIT_SIGN_WIDGET_TEXT);
00269   }
00270 
00271   void UpdateSignEditWindow(const Sign *si)
00272   {
00273     char *last_of = &this->edit_str_buf[this->edit_str_size - 1]; // points to terminating '\0'
00274 
00275     /* Display an empty string when the sign hasnt been edited yet */
00276     if (si->name != NULL) {
00277       SetDParam(0, si->index);
00278       GetString(this->edit_str_buf, STR_SIGN_NAME, last_of);
00279     } else {
00280       GetString(this->edit_str_buf, STR_EMPTY, last_of);
00281     }
00282     *last_of = '\0';
00283 
00284     this->cur_sign = si->index;
00285     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, MAX_LENGTH_SIGN_NAME_PIXELS);
00286 
00287     this->SetWidgetDirty(QUERY_EDIT_SIGN_WIDGET_TEXT);
00288     this->SetFocusedWidget(QUERY_EDIT_SIGN_WIDGET_TEXT);
00289   }
00290 
00296   const Sign *PrevNextSign(bool next)
00297   {
00298     /* Rebuild the sign list */
00299     this->signs.ForceRebuild();
00300     this->signs.NeedResort();
00301     this->BuildSignsList();
00302     this->SortSignsList();
00303 
00304     /* Search through the list for the current sign, excluding
00305      * - the first sign if we want the previous sign or
00306      * - the last sign if we want the next sign */
00307     uint end = this->signs.Length() - (next ? 1 : 0);
00308     for (uint i = next ? 0 : 1; i < end; i++) {
00309       if (this->cur_sign == this->signs[i]->index) {
00310         /* We've found the current sign, so return the sign before/after it */
00311         return this->signs[i + (next ? 1 : -1)];
00312       }
00313     }
00314     /* If we haven't found the current sign by now, return the last/first sign */
00315     return this->signs[next ? 0 : this->signs.Length() - 1];
00316   }
00317 
00318   virtual void SetStringParameters(int widget) const
00319   {
00320     switch (widget) {
00321       case QUERY_EDIT_SIGN_WIDGET_CAPTION:
00322         SetDParam(0, this->caption);
00323         break;
00324     }
00325   }
00326 
00327   virtual void OnPaint()
00328   {
00329     this->DrawWidgets();
00330     if (!this->IsShaded()) this->DrawEditBox(QUERY_EDIT_SIGN_WIDGET_TEXT);
00331   }
00332 
00333   virtual void OnClick(Point pt, int widget, int click_count)
00334   {
00335     switch (widget) {
00336       case QUERY_EDIT_SIGN_WIDGET_PREVIOUS:
00337       case QUERY_EDIT_SIGN_WIDGET_NEXT: {
00338         const Sign *si = this->PrevNextSign(widget == QUERY_EDIT_SIGN_WIDGET_NEXT);
00339 
00340         /* Rebuild the sign list */
00341         this->signs.ForceRebuild();
00342         this->signs.NeedResort();
00343         this->BuildSignsList();
00344         this->SortSignsList();
00345 
00346         /* Scroll to sign and reopen window */
00347         ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
00348         UpdateSignEditWindow(si);
00349         break;
00350       }
00351 
00352       case QUERY_EDIT_SIGN_WIDGET_DELETE:
00353         /* Only need to set the buffer to null, the rest is handled as the OK button */
00354         RenameSign(this->cur_sign, "");
00355         /* don't delete this, we are deleted in Sign::~Sign() -> DeleteRenameSignWindow() */
00356         break;
00357 
00358       case QUERY_EDIT_SIGN_WIDGET_OK:
00359         if (RenameSign(this->cur_sign, this->text.buf)) break;
00360         /* FALL THROUGH */
00361 
00362       case QUERY_EDIT_SIGN_WIDGET_CANCEL:
00363         delete this;
00364         break;
00365     }
00366   }
00367 
00368   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00369   {
00370     EventState state = ES_NOT_HANDLED;
00371     switch (this->HandleEditBoxKey(QUERY_EDIT_SIGN_WIDGET_TEXT, key, keycode, state)) {
00372       default: break;
00373 
00374       case HEBR_CONFIRM:
00375         if (RenameSign(this->cur_sign, this->text.buf)) break;
00376         /* FALL THROUGH */
00377 
00378       case HEBR_CANCEL: // close window, abandon changes
00379         delete this;
00380         break;
00381     }
00382     return state;
00383   }
00384 
00385   virtual void OnMouseLoop()
00386   {
00387     this->HandleEditBox(QUERY_EDIT_SIGN_WIDGET_TEXT);
00388   }
00389 
00390   virtual void OnOpenOSKWindow(int wid)
00391   {
00392     ShowOnScreenKeyboard(this, wid, QUERY_EDIT_SIGN_WIDGET_CANCEL, QUERY_EDIT_SIGN_WIDGET_OK);
00393   }
00394 };
00395 
00396 static const NWidgetPart _nested_query_sign_edit_widgets[] = {
00397   NWidget(NWID_HORIZONTAL),
00398     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00399     NWidget(WWT_CAPTION, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00400   EndContainer(),
00401   NWidget(WWT_PANEL, COLOUR_GREY),
00402     NWidget(WWT_EDITBOX, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_TEXT), SetMinimalSize(256, 12), SetDataTip(STR_EDIT_SIGN_SIGN_OSKTITLE, STR_NULL), SetPadding(2, 2, 2, 2),
00403   EndContainer(),
00404   NWidget(NWID_HORIZONTAL),
00405     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_OK), SetMinimalSize(61, 12), SetDataTip(STR_BUTTON_OK, STR_NULL),
00406     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_CANCEL), SetMinimalSize(60, 12), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
00407     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_DELETE), SetMinimalSize(60, 12), SetDataTip(STR_TOWN_VIEW_DELETE_BUTTON, STR_NULL),
00408     NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), EndContainer(),
00409     NWidget(NWID_BUTTON_ARROW, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_PREVIOUS), SetMinimalSize(11, 12), SetDataTip(AWV_DECREASE, STR_EDIT_SIGN_PREVIOUS_SIGN_TOOLTIP),
00410     NWidget(NWID_BUTTON_ARROW, COLOUR_GREY, QUERY_EDIT_SIGN_WIDGET_NEXT), SetMinimalSize(11, 12), SetDataTip(AWV_INCREASE, STR_EDIT_SIGN_NEXT_SIGN_TOOLTIP),
00411   EndContainer(),
00412 };
00413 
00414 static const WindowDesc _query_sign_edit_desc(
00415   WDP_AUTO, 0, 0,
00416   WC_QUERY_STRING, WC_NONE,
00417   WDF_CONSTRUCTION | WDF_UNCLICK_BUTTONS,
00418   _nested_query_sign_edit_widgets, lengthof(_nested_query_sign_edit_widgets)
00419 );
00420 
00421 void HandleClickOnSign(const Sign *si)
00422 {
00423   if (_ctrl_pressed && si->owner == _local_company) {
00424     RenameSign(si->index, NULL);
00425     return;
00426   }
00427   ShowRenameSignWindow(si);
00428 }
00429 
00430 void ShowRenameSignWindow(const Sign *si)
00431 {
00432   /* Delete all other edit windows */
00433   DeleteWindowById(WC_QUERY_STRING, 0);
00434 
00435   new SignWindow(&_query_sign_edit_desc, si);
00436 }
00437 
00438 void DeleteRenameSignWindow(SignID sign)
00439 {
00440   SignWindow *w = dynamic_cast<SignWindow *>(FindWindowById(WC_QUERY_STRING, 0));
00441 
00442   if (w != NULL && w->cur_sign == sign) delete w;
00443 }

Generated on Sat Nov 20 20:59:09 2010 for OpenTTD by  doxygen 1.6.1