00001
00002
00003
00004
00005
00006
00007
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);
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
00097 this->last_sign = NULL;
00098 }
00099
00101 static bool CDECL SignNameFilter(const Sign * const *a, StringFilter &filter)
00102 {
00103
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
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
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(WindowDesc *desc, WindowNumber window_number) : Window(desc), filter_editbox(MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_SIGN_NAME_CHARS)
00153 {
00154 this->CreateNestedTree();
00155 this->vscroll = this->GetScrollbar(WID_SIL_SCROLLBAR);
00156 this->FinishInitNested(window_number);
00157 this->SetWidgetLoweredState(WID_SIL_FILTER_MATCH_CASE_BTN, SignList::match_case);
00158
00159
00160 this->querystrings[WID_SIL_FILTER_TEXT] = &this->filter_editbox;
00161 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
00162
00163
00164 this->SetFilterString("");
00165
00166
00167 this->signs.ForceRebuild();
00168 this->signs.ForceResort();
00169 this->BuildSortSignList();
00170 }
00171
00178 void SetFilterString(const char *new_filter_string)
00179 {
00180
00181 this->string_filter.SetFilterTerm(new_filter_string);
00182
00183
00184 this->InvalidateData();
00185 }
00186
00187 virtual void OnPaint()
00188 {
00189 if (this->signs.NeedRebuild()) this->BuildSortSignList();
00190 this->DrawWidgets();
00191 }
00192
00193 virtual void DrawWidget(const Rect &r, int widget) const
00194 {
00195 switch (widget) {
00196 case WID_SIL_LIST: {
00197 uint y = r.top + WD_FRAMERECT_TOP;
00198
00199 if (this->vscroll->GetCount() == 0) {
00200 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, STR_STATION_LIST_NONE);
00201 return;
00202 }
00203
00204 bool rtl = _current_text_dir == TD_RTL;
00205 int sprite_offset_y = (FONT_HEIGHT_NORMAL - 10) / 2 + 1;
00206 uint icon_left = 4 + (rtl ? r.right - this->text_offset : r.left);
00207 uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : this->text_offset);
00208 uint text_right = r.right - (rtl ? this->text_offset : WD_FRAMERECT_RIGHT);
00209
00210
00211 for (uint16 i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
00212 const Sign *si = this->signs[i];
00213
00214 if (si->owner != OWNER_NONE) DrawCompanyIcon(si->owner, icon_left, y + sprite_offset_y);
00215
00216 SetDParam(0, si->index);
00217 DrawString(text_left, text_right, y, STR_SIGN_NAME, TC_YELLOW);
00218 y += this->resize.step_height;
00219 }
00220 break;
00221 }
00222 }
00223 }
00224
00225 virtual void SetStringParameters(int widget) const
00226 {
00227 if (widget == WID_SIL_CAPTION) SetDParam(0, this->vscroll->GetCount());
00228 }
00229
00230 virtual void OnClick(Point pt, int widget, int click_count)
00231 {
00232 switch (widget) {
00233 case WID_SIL_LIST: {
00234 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SIL_LIST, WD_FRAMERECT_TOP);
00235 if (id_v == INT_MAX) return;
00236
00237 const Sign *si = this->signs[id_v];
00238 ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
00239 break;
00240 }
00241
00242 case WID_SIL_FILTER_ENTER_BTN:
00243 if (this->signs.Length() >= 1) {
00244 const Sign *si = this->signs[0];
00245 ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
00246 }
00247 break;
00248
00249 case WID_SIL_FILTER_MATCH_CASE_BTN:
00250 SignList::match_case = !SignList::match_case;
00251 this->SetWidgetLoweredState(WID_SIL_FILTER_MATCH_CASE_BTN, SignList::match_case);
00252 this->InvalidateData();
00253 break;
00254 }
00255 }
00256
00257 virtual void OnResize()
00258 {
00259 this->vscroll->SetCapacityFromWidget(this, WID_SIL_LIST, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
00260 }
00261
00262 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00263 {
00264 switch (widget) {
00265 case WID_SIL_LIST: {
00266 Dimension spr_dim = GetSpriteSize(SPR_COMPANY_ICON);
00267 this->text_offset = WD_FRAMETEXT_LEFT + spr_dim.width + 2;
00268 resize->height = max<uint>(FONT_HEIGHT_NORMAL, spr_dim.height);
00269 Dimension d = {this->text_offset + WD_FRAMETEXT_RIGHT, WD_FRAMERECT_TOP + 5 * resize->height + WD_FRAMERECT_BOTTOM};
00270 *size = maxdim(*size, d);
00271 break;
00272 }
00273
00274 case WID_SIL_CAPTION:
00275 SetDParamMaxValue(0, Sign::GetPoolSize(), 3);
00276 *size = GetStringBoundingBox(STR_SIGN_LIST_CAPTION);
00277 size->height += padding.height;
00278 size->width += padding.width;
00279 break;
00280 }
00281 }
00282
00283 virtual EventState OnHotkey(int hotkey)
00284 {
00285 switch (hotkey) {
00286 case SLHK_FOCUS_FILTER_BOX:
00287 this->SetFocusedWidget(WID_SIL_FILTER_TEXT);
00288 SetFocusedWindow(this);
00289 break;
00290
00291 default:
00292 return ES_NOT_HANDLED;
00293 }
00294
00295 return ES_HANDLED;
00296 }
00297
00298 virtual void OnEditboxChanged(int widget)
00299 {
00300 if (widget == WID_SIL_FILTER_TEXT) this->SetFilterString(this->filter_editbox.text.buf);
00301 }
00302
00303 void BuildSortSignList()
00304 {
00305 if (this->signs.NeedRebuild()) {
00306 this->BuildSignsList();
00307 this->vscroll->SetCount(this->signs.Length());
00308 this->SetWidgetDirty(WID_SIL_CAPTION);
00309 }
00310 this->SortSignsList();
00311 }
00312
00313 virtual void OnHundredthTick()
00314 {
00315 this->BuildSortSignList();
00316 this->SetDirty();
00317 }
00318
00324 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00325 {
00326
00327
00328
00329 if (data == 0 || data == -1 || !this->string_filter.IsEmpty()) {
00330
00331 this->signs.ForceRebuild();
00332 } else {
00333 this->signs.ForceResort();
00334 }
00335 }
00336
00337 static HotkeyList hotkeys;
00338 };
00339
00345 static EventState SignListGlobalHotkeys(int hotkey)
00346 {
00347 if (_game_mode == GM_MENU) return ES_NOT_HANDLED;
00348 Window *w = ShowSignList();
00349 if (w == NULL) return ES_NOT_HANDLED;
00350 return w->OnHotkey(hotkey);
00351 }
00352
00353 static Hotkey signlist_hotkeys[] = {
00354 Hotkey('F', "focus_filter_box", SLHK_FOCUS_FILTER_BOX),
00355 HOTKEY_LIST_END
00356 };
00357 HotkeyList SignListWindow::hotkeys("signlist", signlist_hotkeys, SignListGlobalHotkeys);
00358
00359 static const NWidgetPart _nested_sign_list_widgets[] = {
00360 NWidget(NWID_HORIZONTAL),
00361 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00362 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SIL_CAPTION), SetDataTip(STR_SIGN_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00363 NWidget(WWT_SHADEBOX, COLOUR_GREY),
00364 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
00365 NWidget(WWT_STICKYBOX, COLOUR_GREY),
00366 EndContainer(),
00367 NWidget(NWID_HORIZONTAL),
00368 NWidget(NWID_VERTICAL),
00369 NWidget(WWT_PANEL, COLOUR_GREY, WID_SIL_LIST), SetMinimalSize(WD_FRAMETEXT_LEFT + 16 + 255 + WD_FRAMETEXT_RIGHT, 50),
00370 SetResize(1, 10), SetFill(1, 0), SetScrollbar(WID_SIL_SCROLLBAR), EndContainer(),
00371 NWidget(NWID_HORIZONTAL),
00372 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1),
00373 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SIL_FILTER_TEXT), SetMinimalSize(80, 12), SetResize(1, 0), SetFill(1, 0), SetPadding(2, 2, 2, 2),
00374 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
00375 EndContainer(),
00376 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SIL_FILTER_MATCH_CASE_BTN), SetDataTip(STR_SIGN_LIST_MATCH_CASE, STR_SIGN_LIST_MATCH_CASE_TOOLTIP),
00377 EndContainer(),
00378 EndContainer(),
00379 NWidget(NWID_VERTICAL),
00380 NWidget(NWID_VERTICAL), SetFill(0, 1),
00381 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SIL_SCROLLBAR),
00382 EndContainer(),
00383 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00384 EndContainer(),
00385 EndContainer(),
00386 };
00387
00388 static WindowDesc _sign_list_desc(
00389 WDP_AUTO, "list_signs", 358, 138,
00390 WC_SIGN_LIST, WC_NONE,
00391 0,
00392 _nested_sign_list_widgets, lengthof(_nested_sign_list_widgets),
00393 &SignListWindow::hotkeys
00394 );
00395
00401 Window *ShowSignList()
00402 {
00403 return AllocateWindowDescFront<SignListWindow>(&_sign_list_desc, 0);
00404 }
00405
00412 static bool RenameSign(SignID index, const char *text)
00413 {
00414 bool remove = StrEmpty(text);
00415 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);
00416 return remove;
00417 }
00418
00419 struct SignWindow : Window, SignList {
00420 QueryString name_editbox;
00421 SignID cur_sign;
00422
00423 SignWindow(WindowDesc *desc, const Sign *si) : Window(desc), name_editbox(MAX_LENGTH_SIGN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_SIGN_NAME_CHARS)
00424 {
00425 this->querystrings[WID_QES_TEXT] = &this->name_editbox;
00426 this->name_editbox.caption = STR_EDIT_SIGN_CAPTION;
00427 this->name_editbox.cancel_button = WID_QES_CANCEL;
00428 this->name_editbox.ok_button = WID_QES_OK;
00429
00430 this->InitNested(WN_QUERY_STRING_SIGN);
00431
00432 UpdateSignEditWindow(si);
00433 this->SetFocusedWidget(WID_QES_TEXT);
00434 }
00435
00436 void UpdateSignEditWindow(const Sign *si)
00437 {
00438
00439 if (si->name != NULL) {
00440 SetDParam(0, si->index);
00441 this->name_editbox.text.Assign(STR_SIGN_NAME);
00442 } else {
00443 this->name_editbox.text.DeleteAll();
00444 }
00445
00446 this->cur_sign = si->index;
00447
00448 this->SetWidgetDirty(WID_QES_TEXT);
00449 this->SetFocusedWidget(WID_QES_TEXT);
00450 }
00451
00457 const Sign *PrevNextSign(bool next)
00458 {
00459
00460 this->signs.ForceRebuild();
00461 this->signs.NeedResort();
00462 this->BuildSignsList();
00463 this->SortSignsList();
00464
00465
00466
00467
00468 uint end = this->signs.Length() - (next ? 1 : 0);
00469 for (uint i = next ? 0 : 1; i < end; i++) {
00470 if (this->cur_sign == this->signs[i]->index) {
00471
00472 return this->signs[i + (next ? 1 : -1)];
00473 }
00474 }
00475
00476 return this->signs[next ? 0 : this->signs.Length() - 1];
00477 }
00478
00479 virtual void SetStringParameters(int widget) const
00480 {
00481 switch (widget) {
00482 case WID_QES_CAPTION:
00483 SetDParam(0, this->name_editbox.caption);
00484 break;
00485 }
00486 }
00487
00488 virtual void OnClick(Point pt, int widget, int click_count)
00489 {
00490 switch (widget) {
00491 case WID_QES_PREVIOUS:
00492 case WID_QES_NEXT: {
00493 const Sign *si = this->PrevNextSign(widget == WID_QES_NEXT);
00494
00495
00496 this->signs.ForceRebuild();
00497 this->signs.NeedResort();
00498 this->BuildSignsList();
00499 this->SortSignsList();
00500
00501
00502 ScrollMainWindowToTile(TileVirtXY(si->x, si->y));
00503 UpdateSignEditWindow(si);
00504 break;
00505 }
00506
00507 case WID_QES_DELETE:
00508
00509 RenameSign(this->cur_sign, "");
00510
00511 break;
00512
00513 case WID_QES_OK:
00514 if (RenameSign(this->cur_sign, this->name_editbox.text.buf)) break;
00515
00516
00517 case WID_QES_CANCEL:
00518 delete this;
00519 break;
00520 }
00521 }
00522 };
00523
00524 static const NWidgetPart _nested_query_sign_edit_widgets[] = {
00525 NWidget(NWID_HORIZONTAL),
00526 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00527 NWidget(WWT_CAPTION, COLOUR_GREY, WID_QES_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00528 EndContainer(),
00529 NWidget(WWT_PANEL, COLOUR_GREY),
00530 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_QES_TEXT), SetMinimalSize(256, 12), SetDataTip(STR_EDIT_SIGN_SIGN_OSKTITLE, STR_NULL), SetPadding(2, 2, 2, 2),
00531 EndContainer(),
00532 NWidget(NWID_HORIZONTAL),
00533 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_OK), SetMinimalSize(61, 12), SetDataTip(STR_BUTTON_OK, STR_NULL),
00534 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_CANCEL), SetMinimalSize(60, 12), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
00535 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_QES_DELETE), SetMinimalSize(60, 12), SetDataTip(STR_TOWN_VIEW_DELETE_BUTTON, STR_NULL),
00536 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), EndContainer(),
00537 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_QES_PREVIOUS), SetMinimalSize(11, 12), SetDataTip(AWV_DECREASE, STR_EDIT_SIGN_PREVIOUS_SIGN_TOOLTIP),
00538 NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_QES_NEXT), SetMinimalSize(11, 12), SetDataTip(AWV_INCREASE, STR_EDIT_SIGN_NEXT_SIGN_TOOLTIP),
00539 EndContainer(),
00540 };
00541
00542 static WindowDesc _query_sign_edit_desc(
00543 WDP_CENTER, "query_sign", 0, 0,
00544 WC_QUERY_STRING, WC_NONE,
00545 WDF_CONSTRUCTION,
00546 _nested_query_sign_edit_widgets, lengthof(_nested_query_sign_edit_widgets)
00547 );
00548
00553 void HandleClickOnSign(const Sign *si)
00554 {
00555 if (_ctrl_pressed && (si->owner == _local_company || (si->owner == OWNER_DEITY && _game_mode == GM_EDITOR))) {
00556 RenameSign(si->index, NULL);
00557 return;
00558 }
00559 ShowRenameSignWindow(si);
00560 }
00561
00566 void ShowRenameSignWindow(const Sign *si)
00567 {
00568
00569 DeleteWindowByClass(WC_QUERY_STRING);
00570
00571 new SignWindow(&_query_sign_edit_desc, si);
00572 }
00573
00578 void DeleteRenameSignWindow(SignID sign)
00579 {
00580 SignWindow *w = dynamic_cast<SignWindow *>(FindWindowById(WC_QUERY_STRING, WN_QUERY_STRING_SIGN));
00581
00582 if (w != NULL && w->cur_sign == sign) delete w;
00583 }