00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "town.h"
00014 #include "viewport_func.h"
00015 #include "error.h"
00016 #include "gui.h"
00017 #include "command_func.h"
00018 #include "company_func.h"
00019 #include "company_base.h"
00020 #include "company_gui.h"
00021 #include "network/network.h"
00022 #include "string_func.h"
00023 #include "strings_func.h"
00024 #include "sound_func.h"
00025 #include "tilehighlight_func.h"
00026 #include "sortlist_type.h"
00027 #include "road_cmd.h"
00028 #include "landscape.h"
00029 #include "querystring_gui.h"
00030 #include "window_func.h"
00031 #include "townname_func.h"
00032 #include "core/geometry_func.hpp"
00033 #include "genworld.h"
00034
00035 #include "widgets/town_widget.h"
00036
00037 #include "table/strings.h"
00038
00039 typedef GUIList<const Town*> GUITownList;
00040
00041 static const NWidgetPart _nested_town_authority_widgets[] = {
00042 NWidget(NWID_HORIZONTAL),
00043 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00044 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TA_CAPTION), SetDataTip(STR_LOCAL_AUTHORITY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00045 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00046 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00047 EndContainer(),
00048 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_RATING_INFO), SetMinimalSize(317, 92), SetResize(1, 1), EndContainer(),
00049 NWidget(NWID_HORIZONTAL),
00050 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_COMMAND_LIST), SetMinimalSize(305, 52), SetResize(1, 0), SetDataTip(0x0, STR_LOCAL_AUTHORITY_ACTIONS_TOOLTIP), SetScrollbar(WID_TA_SCROLLBAR), EndContainer(),
00051 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_TA_SCROLLBAR),
00052 EndContainer(),
00053 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TA_ACTION_INFO), SetMinimalSize(317, 52), SetResize(1, 0), EndContainer(),
00054 NWidget(NWID_HORIZONTAL),
00055 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TA_EXECUTE), SetMinimalSize(317, 12), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_LOCAL_AUTHORITY_DO_IT_BUTTON, STR_LOCAL_AUTHORITY_DO_IT_TOOLTIP),
00056 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00057 EndContainer()
00058 };
00059
00061 struct TownAuthorityWindow : Window {
00062 private:
00063 Town *town;
00064 int sel_index;
00065 Scrollbar *vscroll;
00066 uint displayed_actions_on_previous_painting;
00067
00077 static int GetNthSetBit(uint32 bits, int n)
00078 {
00079 if (n >= 0) {
00080 uint i;
00081 FOR_EACH_SET_BIT(i, bits) {
00082 n--;
00083 if (n < 0) return i;
00084 }
00085 }
00086 return -1;
00087 }
00088
00089 public:
00090 TownAuthorityWindow(const WindowDesc *desc, WindowNumber window_number) : Window(), sel_index(-1), displayed_actions_on_previous_painting(0)
00091 {
00092 this->town = Town::Get(window_number);
00093 this->InitNested(desc, window_number);
00094 this->vscroll = this->GetScrollbar(WID_TA_SCROLLBAR);
00095 this->vscroll->SetCapacity((this->GetWidget<NWidgetBase>(WID_TA_COMMAND_LIST)->current_y - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM) / FONT_HEIGHT_NORMAL);
00096 }
00097
00098 virtual void OnPaint()
00099 {
00100 int numact;
00101 uint buttons = GetMaskOfTownActions(&numact, _local_company, this->town);
00102 if (buttons != displayed_actions_on_previous_painting) this->SetDirty();
00103 displayed_actions_on_previous_painting = buttons;
00104
00105 this->vscroll->SetCount(numact + 1);
00106
00107 if (this->sel_index != -1 && !HasBit(buttons, this->sel_index)) {
00108 this->sel_index = -1;
00109 }
00110
00111 this->SetWidgetDisabledState(WID_TA_EXECUTE, this->sel_index == -1);
00112
00113 this->DrawWidgets();
00114 if (!this->IsShaded()) this->DrawRatings();
00115 }
00116
00118 void DrawRatings()
00119 {
00120 NWidgetBase *nwid = this->GetWidget<NWidgetBase>(WID_TA_RATING_INFO);
00121 uint left = nwid->pos_x + WD_FRAMERECT_LEFT;
00122 uint right = nwid->pos_x + nwid->current_x - 1 - WD_FRAMERECT_RIGHT;
00123
00124 uint y = nwid->pos_y + WD_FRAMERECT_TOP;
00125
00126 DrawString(left, right, y, STR_LOCAL_AUTHORITY_COMPANY_RATINGS);
00127 y += FONT_HEIGHT_NORMAL;
00128
00129 Dimension icon_size = GetSpriteSize(SPR_COMPANY_ICON);
00130 int icon_width = icon_size.width;
00131 int icon_y_offset = (FONT_HEIGHT_NORMAL - icon_size.height) / 2;
00132
00133 Dimension exclusive_size = GetSpriteSize(SPR_EXCLUSIVE_TRANSPORT);
00134 int exclusive_width = exclusive_size.width;
00135 int exclusive_y_offset = (FONT_HEIGHT_NORMAL - exclusive_size.height) / 2;
00136
00137 bool rtl = _current_text_dir == TD_RTL;
00138 uint text_left = left + (rtl ? 0 : icon_width + exclusive_width + 4);
00139 uint text_right = right - (rtl ? icon_width + exclusive_width + 4 : 0);
00140 uint icon_left = rtl ? right - icon_width : left;
00141 uint exclusive_left = rtl ? right - icon_width - exclusive_width - 2 : left + icon_width + 2;
00142
00143
00144 const Company *c;
00145 FOR_ALL_COMPANIES(c) {
00146 if ((HasBit(this->town->have_ratings, c->index) || this->town->exclusivity == c->index)) {
00147 DrawCompanyIcon(c->index, icon_left, y + icon_y_offset);
00148
00149 SetDParam(0, c->index);
00150 SetDParam(1, c->index);
00151
00152 int r = this->town->ratings[c->index];
00153 StringID str;
00154 (str = STR_CARGO_RATING_APPALLING, r <= RATING_APPALLING) ||
00155 (str++, r <= RATING_VERYPOOR) ||
00156 (str++, r <= RATING_POOR) ||
00157 (str++, r <= RATING_MEDIOCRE) ||
00158 (str++, r <= RATING_GOOD) ||
00159 (str++, r <= RATING_VERYGOOD) ||
00160 (str++, r <= RATING_EXCELLENT) ||
00161 (str++, true);
00162
00163 SetDParam(2, str);
00164 if (this->town->exclusivity == c->index) {
00165 DrawSprite(SPR_EXCLUSIVE_TRANSPORT, COMPANY_SPRITE_COLOUR(c->index), exclusive_left, y + exclusive_y_offset);
00166 }
00167
00168 DrawString(text_left, text_right, y, STR_LOCAL_AUTHORITY_COMPANY_RATING);
00169 y += FONT_HEIGHT_NORMAL;
00170 }
00171 }
00172
00173 y = y + WD_FRAMERECT_BOTTOM - nwid->pos_y;
00174 if (y > nwid->current_y) {
00175
00176 ResizeWindow(this, 0, y - nwid->current_y);
00177 }
00178 }
00179
00180 virtual void SetStringParameters(int widget) const
00181 {
00182 if (widget == WID_TA_CAPTION) SetDParam(0, this->window_number);
00183 }
00184
00185 virtual void DrawWidget(const Rect &r, int widget) const
00186 {
00187 switch (widget) {
00188 case WID_TA_ACTION_INFO:
00189 if (this->sel_index != -1) {
00190 SetDParam(0, _price[PR_TOWN_ACTION] * _town_action_costs[this->sel_index] >> 8);
00191 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
00192 STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING + this->sel_index);
00193 }
00194 break;
00195 case WID_TA_COMMAND_LIST: {
00196 int numact;
00197 uint buttons = GetMaskOfTownActions(&numact, _local_company, this->town);
00198 int y = r.top + WD_FRAMERECT_TOP;
00199 int pos = this->vscroll->GetPosition();
00200
00201 if (--pos < 0) {
00202 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_LOCAL_AUTHORITY_ACTIONS_TITLE);
00203 y += FONT_HEIGHT_NORMAL;
00204 }
00205
00206 for (int i = 0; buttons; i++, buttons >>= 1) {
00207 if (pos <= -5) break;
00208
00209 if ((buttons & 1) && --pos < 0) {
00210 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y,
00211 STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + i, this->sel_index == i ? TC_WHITE : TC_ORANGE);
00212 y += FONT_HEIGHT_NORMAL;
00213 }
00214 }
00215 break;
00216 }
00217 }
00218 }
00219
00220 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00221 {
00222 switch (widget) {
00223 case WID_TA_ACTION_INFO: {
00224 assert(size->width > padding.width && size->height > padding.height);
00225 size->width -= WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00226 size->height -= WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00227 Dimension d = {0, 0};
00228 for (int i = 0; i < TACT_COUNT; i++) {
00229 SetDParam(0, _price[PR_TOWN_ACTION] * _town_action_costs[i] >> 8);
00230 d = maxdim(d, GetStringMultiLineBoundingBox(STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING + i, *size));
00231 }
00232 *size = maxdim(*size, d);
00233 size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00234 size->height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00235 break;
00236 }
00237
00238 case WID_TA_COMMAND_LIST:
00239 size->height = WD_FRAMERECT_TOP + 5 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
00240 size->width = GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTIONS_TITLE).width;
00241 for (uint i = 0; i < TACT_COUNT; i++ ) {
00242 size->width = max(size->width, GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + i).width);
00243 }
00244 size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00245 break;
00246
00247 case WID_TA_RATING_INFO:
00248 resize->height = FONT_HEIGHT_NORMAL;
00249 size->height = WD_FRAMERECT_TOP + 9 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
00250 break;
00251 }
00252 }
00253
00254 virtual void OnClick(Point pt, int widget, int click_count)
00255 {
00256 switch (widget) {
00257 case WID_TA_COMMAND_LIST: {
00258 int y = this->GetRowFromWidget(pt.y, WID_TA_COMMAND_LIST, 1, FONT_HEIGHT_NORMAL);
00259 if (!IsInsideMM(y, 0, 5)) return;
00260
00261 y = GetNthSetBit(GetMaskOfTownActions(NULL, _local_company, this->town), y + this->vscroll->GetPosition() - 1);
00262 if (y >= 0) {
00263 this->sel_index = y;
00264 this->SetDirty();
00265 }
00266
00267 if (click_count == 1 || y < 0) break;
00268 }
00269
00270 case WID_TA_EXECUTE:
00271 DoCommandP(this->town->xy, this->window_number, this->sel_index, CMD_DO_TOWN_ACTION | CMD_MSG(STR_ERROR_CAN_T_DO_THIS));
00272 break;
00273 }
00274 }
00275
00276 virtual void OnHundredthTick()
00277 {
00278 this->SetDirty();
00279 }
00280 };
00281
00282 static const WindowDesc _town_authority_desc(
00283 WDP_AUTO, 317, 222,
00284 WC_TOWN_AUTHORITY, WC_NONE,
00285 WDF_UNCLICK_BUTTONS,
00286 _nested_town_authority_widgets, lengthof(_nested_town_authority_widgets)
00287 );
00288
00289 static void ShowTownAuthorityWindow(uint town)
00290 {
00291 AllocateWindowDescFront<TownAuthorityWindow>(&_town_authority_desc, town);
00292 }
00293
00294
00295
00296 struct TownViewWindow : Window {
00297 private:
00298 Town *town;
00299
00300 public:
00301 static const int WID_TV_HEIGHT_NORMAL = 150;
00302
00303 TownViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
00304 {
00305 this->CreateNestedTree(desc);
00306
00307 this->town = Town::Get(window_number);
00308 if (this->town->larger_town) this->GetWidget<NWidgetCore>(WID_TV_CAPTION)->widget_data = STR_TOWN_VIEW_CITY_CAPTION;
00309
00310 this->FinishInitNested(desc, window_number);
00311
00312 this->flags |= WF_DISABLE_VP_SCROLL;
00313 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_TV_VIEWPORT);
00314 nvp->InitializeViewport(this, this->town->xy, ZOOM_LVL_NEWS);
00315
00316
00317 this->SetWidgetDisabledState(WID_TV_CHANGE_NAME, _networking && !_network_server);
00318 }
00319
00320 virtual void SetStringParameters(int widget) const
00321 {
00322 if (widget == WID_TV_CAPTION) SetDParam(0, this->town->index);
00323 }
00324
00325 virtual void DrawWidget(const Rect &r, int widget) const
00326 {
00327 if (widget != WID_TV_INFO) return;
00328
00329 uint y = r.top + WD_FRAMERECT_TOP;
00330
00331 SetDParam(0, this->town->cache.population);
00332 SetDParam(1, this->town->cache.num_houses);
00333 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y, STR_TOWN_VIEW_POPULATION_HOUSES);
00334
00335 SetDParam(0, this->town->supplied[CT_PASSENGERS].old_act);
00336 SetDParam(1, this->town->supplied[CT_PASSENGERS].old_max);
00337 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_PASSENGERS_LAST_MONTH_MAX);
00338
00339 SetDParam(0, this->town->supplied[CT_MAIL].old_act);
00340 SetDParam(1, this->town->supplied[CT_MAIL].old_max);
00341 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_MAIL_LAST_MONTH_MAX);
00342
00343 bool first = true;
00344 for (int i = TE_BEGIN; i < TE_END; i++) {
00345 if (this->town->goal[i] == 0) continue;
00346 if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
00347 if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
00348
00349 if (first) {
00350 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH);
00351 first = false;
00352 }
00353
00354 bool rtl = _current_text_dir == TD_RTL;
00355 uint cargo_text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : 20);
00356 uint cargo_text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? 20 : 0);
00357
00358 const CargoSpec *cargo = FindFirstCargoWithTownEffect((TownEffect)i);
00359 assert(cargo != NULL);
00360
00361 StringID string;
00362
00363 if (this->town->goal[i] == TOWN_GROWTH_DESERT || this->town->goal[i] == TOWN_GROWTH_WINTER) {
00364
00365 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED_GENERAL;
00366 if (this->town->received[i].old_act == 0) {
00367 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_GENERAL;
00368
00369 if (this->town->goal[i] == TOWN_GROWTH_WINTER && TileHeight(this->town->xy) < GetSnowLine()) {
00370 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_WINTER;
00371 }
00372 }
00373
00374 SetDParam(0, cargo->name);
00375 } else {
00376 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED;
00377 if (this->town->received[i].old_act < this->town->goal[i]) {
00378 string = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED;
00379 }
00380
00381 SetDParam(0, cargo->Index());
00382 SetDParam(1, this->town->received[i].old_act);
00383 SetDParam(2, cargo->Index());
00384 SetDParam(3, this->town->goal[i]);
00385 }
00386 DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, string);
00387 }
00388
00389 if (HasBit(this->town->flags, TOWN_IS_FUNDED)) {
00390 SetDParam(0, ((this->town->growth_rate & (~TOWN_GROW_RATE_CUSTOM)) * TOWN_GROWTH_TICKS + DAY_TICKS) / DAY_TICKS);
00391 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, this->town->fund_buildings_months == 0 ? STR_TOWN_VIEW_TOWN_GROWS_EVERY : STR_TOWN_VIEW_TOWN_GROWS_EVERY_FUNDED);
00392 } else {
00393 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_TOWN_GROW_STOPPED);
00394 }
00395
00396
00397 if (_settings_game.economy.station_noise_level) {
00398 SetDParam(0, this->town->noise_reached);
00399 SetDParam(1, this->town->MaxTownNoise());
00400 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_NOISE_IN_TOWN);
00401 }
00402
00403 if (this->town->text != NULL) {
00404 SetDParamStr(0, this->town->text);
00405 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, UINT16_MAX, STR_JUST_RAW_STRING, TC_BLACK);
00406 }
00407 }
00408
00409 virtual void OnClick(Point pt, int widget, int click_count)
00410 {
00411 switch (widget) {
00412 case WID_TV_CENTER_VIEW:
00413 if (_ctrl_pressed) {
00414 ShowExtraViewPortWindow(this->town->xy);
00415 } else {
00416 ScrollMainWindowToTile(this->town->xy);
00417 }
00418 break;
00419
00420 case WID_TV_SHOW_AUTHORITY:
00421 ShowTownAuthorityWindow(this->window_number);
00422 break;
00423
00424 case WID_TV_CHANGE_NAME:
00425 SetDParam(0, this->window_number);
00426 ShowQueryString(STR_TOWN_NAME, STR_TOWN_VIEW_RENAME_TOWN_BUTTON, MAX_LENGTH_TOWN_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
00427 break;
00428
00429 case WID_TV_EXPAND: {
00430
00431 static bool _warn_town_no_roads = false;
00432
00433 if (!_settings_game.economy.allow_town_roads && !_warn_town_no_roads) {
00434 ShowErrorMessage(STR_ERROR_TOWN_EXPAND_WARN_NO_ROADS, INVALID_STRING_ID, WL_WARNING);
00435 _warn_town_no_roads = true;
00436 }
00437
00438 DoCommandP(0, this->window_number, 0, CMD_EXPAND_TOWN | CMD_MSG(STR_ERROR_CAN_T_EXPAND_TOWN));
00439 break;
00440 }
00441
00442 case WID_TV_DELETE:
00443 DoCommandP(0, this->window_number, 0, CMD_DELETE_TOWN | CMD_MSG(STR_ERROR_TOWN_CAN_T_DELETE));
00444 break;
00445 }
00446 }
00447
00448 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00449 {
00450 switch (widget) {
00451 case WID_TV_INFO:
00452 size->height = GetDesiredInfoHeight(size->width);
00453 break;
00454 }
00455 }
00456
00461 uint GetDesiredInfoHeight(int width) const
00462 {
00463 uint aimed_height = 3 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00464
00465 bool first = true;
00466 for (int i = TE_BEGIN; i < TE_END; i++) {
00467 if (this->town->goal[i] == 0) continue;
00468 if (this->town->goal[i] == TOWN_GROWTH_WINTER && (TileHeight(this->town->xy) < LowestSnowLine() || this->town->cache.population <= 90)) continue;
00469 if (this->town->goal[i] == TOWN_GROWTH_DESERT && (GetTropicZone(this->town->xy) != TROPICZONE_DESERT || this->town->cache.population <= 60)) continue;
00470
00471 if (first) {
00472 aimed_height += FONT_HEIGHT_NORMAL;
00473 first = false;
00474 }
00475 aimed_height += FONT_HEIGHT_NORMAL;
00476 }
00477 aimed_height += FONT_HEIGHT_NORMAL;
00478
00479 if (_settings_game.economy.station_noise_level) aimed_height += FONT_HEIGHT_NORMAL;
00480
00481 if (this->town->text != NULL) {
00482 SetDParamStr(0, this->town->text);
00483 aimed_height += GetStringHeight(STR_JUST_RAW_STRING, width);
00484 }
00485
00486 return aimed_height;
00487 }
00488
00489 void ResizeWindowAsNeeded()
00490 {
00491 const NWidgetBase *nwid_info = this->GetWidget<NWidgetBase>(WID_TV_INFO);
00492 uint aimed_height = GetDesiredInfoHeight(nwid_info->current_x);
00493 if (aimed_height > nwid_info->current_y || (aimed_height < nwid_info->current_y && nwid_info->current_y > nwid_info->smallest_y)) {
00494 this->ReInit();
00495 }
00496 }
00497
00498 virtual void OnResize()
00499 {
00500 if (this->viewport != NULL) {
00501 NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(WID_TV_VIEWPORT);
00502 nvp->UpdateViewportCoordinates(this);
00503
00504 ScrollWindowToTile(this->town->xy, this, true);
00505 }
00506 }
00507
00513 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00514 {
00515 if (!gui_scope) return;
00516
00517 this->SetDirty();
00518 this->ResizeWindowAsNeeded();
00519 }
00520
00521 virtual void OnQueryTextFinished(char *str)
00522 {
00523 if (str == NULL) return;
00524
00525 DoCommandP(0, this->window_number, 0, CMD_RENAME_TOWN | CMD_MSG(STR_ERROR_CAN_T_RENAME_TOWN), NULL, str);
00526 }
00527 };
00528
00529 static const NWidgetPart _nested_town_game_view_widgets[] = {
00530 NWidget(NWID_HORIZONTAL),
00531 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00532 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00533 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00534 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00535 EndContainer(),
00536 NWidget(WWT_PANEL, COLOUR_BROWN),
00537 NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
00538 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_TV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 0), SetResize(1, 1), SetPadding(1, 1, 1, 1),
00539 EndContainer(),
00540 EndContainer(),
00541 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TV_INFO), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
00542 NWidget(NWID_HORIZONTAL),
00543 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00544 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_CENTER_VIEW), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_LOCATION, STR_TOWN_VIEW_CENTER_TOOLTIP),
00545 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_SHOW_AUTHORITY), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_LOCAL_AUTHORITY_BUTTON, STR_TOWN_VIEW_LOCAL_AUTHORITY_TOOLTIP),
00546 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_CHANGE_NAME), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
00547 EndContainer(),
00548 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00549 EndContainer(),
00550 };
00551
00552 static const WindowDesc _town_game_view_desc(
00553 WDP_AUTO, 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
00554 WC_TOWN_VIEW, WC_NONE,
00555 WDF_UNCLICK_BUTTONS,
00556 _nested_town_game_view_widgets, lengthof(_nested_town_game_view_widgets)
00557 );
00558
00559 static const NWidgetPart _nested_town_editor_view_widgets[] = {
00560 NWidget(NWID_HORIZONTAL),
00561 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00562 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_TV_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00563 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_CHANGE_NAME), SetMinimalSize(76, 14), SetDataTip(STR_BUTTON_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
00564 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00565 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00566 EndContainer(),
00567 NWidget(WWT_PANEL, COLOUR_BROWN),
00568 NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
00569 NWidget(NWID_VIEWPORT, INVALID_COLOUR, WID_TV_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 1), SetResize(1, 1), SetPadding(1, 1, 1, 1),
00570 EndContainer(),
00571 EndContainer(),
00572 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TV_INFO), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
00573 NWidget(NWID_HORIZONTAL),
00574 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00575 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_CENTER_VIEW), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_LOCATION, STR_TOWN_VIEW_CENTER_TOOLTIP),
00576 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_EXPAND), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_EXPAND_BUTTON, STR_TOWN_VIEW_EXPAND_TOOLTIP),
00577 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_DELETE), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_DELETE_BUTTON, STR_TOWN_VIEW_DELETE_TOOLTIP),
00578 EndContainer(),
00579 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00580 EndContainer(),
00581 };
00582
00583 static const WindowDesc _town_editor_view_desc(
00584 WDP_AUTO, 260, TownViewWindow::WID_TV_HEIGHT_NORMAL,
00585 WC_TOWN_VIEW, WC_NONE,
00586 WDF_UNCLICK_BUTTONS,
00587 _nested_town_editor_view_widgets, lengthof(_nested_town_editor_view_widgets)
00588 );
00589
00590 void ShowTownViewWindow(TownID town)
00591 {
00592 if (_game_mode == GM_EDITOR) {
00593 AllocateWindowDescFront<TownViewWindow>(&_town_editor_view_desc, town);
00594 } else {
00595 AllocateWindowDescFront<TownViewWindow>(&_town_game_view_desc, town);
00596 }
00597 }
00598
00599 static const NWidgetPart _nested_town_directory_widgets[] = {
00600 NWidget(NWID_HORIZONTAL),
00601 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00602 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_TOWN_DIRECTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00603 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00604 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00605 EndContainer(),
00606 NWidget(NWID_HORIZONTAL),
00607 NWidget(NWID_VERTICAL),
00608 NWidget(NWID_HORIZONTAL),
00609 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TD_SORT_NAME), SetMinimalSize(99, 12), SetDataTip(STR_SORT_BY_CAPTION_NAME, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0),
00610 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TD_SORT_POPULATION), SetMinimalSize(97, 12), SetDataTip(STR_SORT_BY_CAPTION_POPULATION, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0),
00611 EndContainer(),
00612 NWidget(WWT_PANEL, COLOUR_BROWN, WID_TD_LIST), SetMinimalSize(196, 0), SetDataTip(0x0, STR_TOWN_DIRECTORY_LIST_TOOLTIP),
00613 SetFill(1, 0), SetResize(0, 10), SetScrollbar(WID_TD_SCROLLBAR), EndContainer(),
00614 NWidget(WWT_PANEL, COLOUR_BROWN),
00615 NWidget(WWT_TEXT, COLOUR_BROWN, WID_TD_WORLD_POPULATION), SetPadding(2, 0, 0, 2), SetMinimalSize(196, 12), SetFill(1, 0), SetDataTip(STR_TOWN_POPULATION, STR_NULL),
00616 EndContainer(),
00617 EndContainer(),
00618 NWidget(NWID_VERTICAL),
00619 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_TD_SCROLLBAR),
00620 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00621 EndContainer(),
00622 EndContainer(),
00623 };
00624
00626 struct TownDirectoryWindow : public Window {
00627 private:
00628
00629 static Listing last_sorting;
00630 static const Town *last_town;
00631
00632
00633 static GUITownList::SortFunction * const sorter_funcs[];
00634
00635 GUITownList towns;
00636
00637 Scrollbar *vscroll;
00638
00639 void BuildSortTownList()
00640 {
00641 if (this->towns.NeedRebuild()) {
00642 this->towns.Clear();
00643
00644 const Town *t;
00645 FOR_ALL_TOWNS(t) {
00646 *this->towns.Append() = t;
00647 }
00648
00649 this->towns.Compact();
00650 this->towns.RebuildDone();
00651 this->vscroll->SetCount(this->towns.Length());
00652 }
00653
00654 this->last_town = NULL;
00655 this->towns.Sort();
00656 }
00657
00659 static int CDECL TownNameSorter(const Town * const *a, const Town * const *b)
00660 {
00661 static char buf_cache[64];
00662 const Town *ta = *a;
00663 const Town *tb = *b;
00664 char buf[64];
00665
00666 SetDParam(0, ta->index);
00667 GetString(buf, STR_TOWN_NAME, lastof(buf));
00668
00669
00670
00671
00672 if (tb != last_town) {
00673 last_town = tb;
00674 SetDParam(0, tb->index);
00675 GetString(buf_cache, STR_TOWN_NAME, lastof(buf_cache));
00676 }
00677
00678 return strnatcmp(buf, buf_cache);
00679 }
00680
00682 static int CDECL TownPopulationSorter(const Town * const *a, const Town * const *b)
00683 {
00684 return (*a)->cache.population - (*b)->cache.population;
00685 }
00686
00687 public:
00688 TownDirectoryWindow(const WindowDesc *desc) : Window()
00689 {
00690 this->CreateNestedTree(desc);
00691
00692 this->vscroll = this->GetScrollbar(WID_TD_SCROLLBAR);
00693
00694 this->towns.SetListing(this->last_sorting);
00695 this->towns.SetSortFuncs(TownDirectoryWindow::sorter_funcs);
00696 this->towns.ForceRebuild();
00697 this->BuildSortTownList();
00698
00699 this->FinishInitNested(desc, 0);
00700 }
00701
00702 ~TownDirectoryWindow()
00703 {
00704 this->last_sorting = this->towns.GetListing();
00705 }
00706
00707 virtual void SetStringParameters(int widget) const
00708 {
00709 if (widget == WID_TD_WORLD_POPULATION) SetDParam(0, GetWorldPopulation());
00710 }
00711
00712 virtual void DrawWidget(const Rect &r, int widget) const
00713 {
00714 switch (widget) {
00715 case WID_TD_SORT_NAME:
00716 if (this->towns.SortType() == 0) this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
00717 break;
00718
00719 case WID_TD_SORT_POPULATION:
00720 if (this->towns.SortType() != 0) this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
00721 break;
00722
00723 case WID_TD_LIST: {
00724 int n = 0;
00725 int y = r.top + WD_FRAMERECT_TOP;
00726 if (this->towns.Length() == 0) {
00727 DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y, STR_TOWN_DIRECTORY_NONE);
00728 break;
00729 }
00730
00731
00732 bool rtl = _current_text_dir == TD_RTL;
00733 Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
00734 int text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : icon_size.width + 2);
00735 int text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? icon_size.width + 2 : 0);
00736 int icon_x = rtl ? r.right - WD_FRAMERECT_RIGHT - icon_size.width : r.left + WD_FRAMERECT_LEFT;
00737
00738 for (uint i = this->vscroll->GetPosition(); i < this->towns.Length(); i++) {
00739 const Town *t = this->towns[i];
00740 assert(t->xy != INVALID_TILE);
00741
00742
00743 if (_game_mode == GM_EDITOR || !HasBit(t->have_ratings, _local_company)) {
00744 DrawSprite(SPR_TOWN_RATING_NA, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
00745 } else {
00746 SpriteID icon = SPR_TOWN_RATING_APALLING;
00747 if (t->ratings[_local_company] > RATING_VERYPOOR) icon = SPR_TOWN_RATING_MEDIOCRE;
00748 if (t->ratings[_local_company] > RATING_GOOD) icon = SPR_TOWN_RATING_GOOD;
00749 DrawSprite(icon, PAL_NONE, icon_x, y + (this->resize.step_height - icon_size.height) / 2);
00750 }
00751
00752 SetDParam(0, t->index);
00753 SetDParam(1, t->cache.population);
00754 DrawString(text_left, text_right, y + (this->resize.step_height - FONT_HEIGHT_NORMAL) / 2, STR_TOWN_DIRECTORY_TOWN);
00755
00756 y += this->resize.step_height;
00757 if (++n == this->vscroll->GetCapacity()) break;
00758 }
00759 break;
00760 }
00761 }
00762 }
00763
00764 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00765 {
00766 switch (widget) {
00767 case WID_TD_SORT_NAME:
00768 case WID_TD_SORT_POPULATION: {
00769 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
00770 d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2;
00771 d.height += padding.height;
00772 *size = maxdim(*size, d);
00773 break;
00774 }
00775 case WID_TD_LIST: {
00776 Dimension d = GetStringBoundingBox(STR_TOWN_DIRECTORY_NONE);
00777 for (uint i = 0; i < this->towns.Length(); i++) {
00778 const Town *t = this->towns[i];
00779
00780 assert(t != NULL);
00781
00782 SetDParam(0, t->index);
00783 SetDParam(1, 10000000);
00784 d = maxdim(d, GetStringBoundingBox(STR_TOWN_DIRECTORY_TOWN));
00785 }
00786 Dimension icon_size = GetSpriteSize(SPR_TOWN_RATING_GOOD);
00787 d.width += icon_size.width + 2;
00788 d.height = max(d.height, icon_size.height);
00789 resize->height = d.height;
00790 d.height *= 5;
00791 d.width += padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00792 d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00793 *size = maxdim(*size, d);
00794 break;
00795 }
00796 case WID_TD_WORLD_POPULATION: {
00797 SetDParam(0, 1000000000);
00798 Dimension d = GetStringBoundingBox(STR_TOWN_POPULATION);
00799 d.width += padding.width;
00800 d.height += padding.height;
00801 *size = maxdim(*size, d);
00802 break;
00803 }
00804 }
00805 }
00806
00807 virtual void OnClick(Point pt, int widget, int click_count)
00808 {
00809 switch (widget) {
00810 case WID_TD_SORT_NAME:
00811 if (this->towns.SortType() == 0) {
00812 this->towns.ToggleSortOrder();
00813 } else {
00814 this->towns.SetSortType(0);
00815 }
00816 this->BuildSortTownList();
00817 this->SetDirty();
00818 break;
00819
00820 case WID_TD_SORT_POPULATION:
00821 if (this->towns.SortType() == 1) {
00822 this->towns.ToggleSortOrder();
00823 } else {
00824 this->towns.SetSortType(1);
00825 }
00826 this->BuildSortTownList();
00827 this->SetDirty();
00828 break;
00829
00830 case WID_TD_LIST: {
00831 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_TD_LIST, WD_FRAMERECT_TOP);
00832 if (id_v >= this->towns.Length()) return;
00833
00834 const Town *t = this->towns[id_v];
00835 assert(t != NULL);
00836 if (_ctrl_pressed) {
00837 ShowExtraViewPortWindow(t->xy);
00838 } else {
00839 ScrollMainWindowToTile(t->xy);
00840 }
00841 break;
00842 }
00843 }
00844 }
00845
00846 virtual void OnPaint()
00847 {
00848 if (this->towns.NeedRebuild()) this->BuildSortTownList();
00849 this->DrawWidgets();
00850 }
00851
00852 virtual void OnHundredthTick()
00853 {
00854 this->BuildSortTownList();
00855 this->SetDirty();
00856 }
00857
00858 virtual void OnResize()
00859 {
00860 this->vscroll->SetCapacityFromWidget(this, WID_TD_LIST);
00861 }
00862
00868 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00869 {
00870 if (data == 0) {
00871
00872 this->towns.ForceRebuild();
00873 } else {
00874 this->towns.ForceResort();
00875 }
00876 }
00877 };
00878
00879 Listing TownDirectoryWindow::last_sorting = {false, 0};
00880 const Town *TownDirectoryWindow::last_town = NULL;
00881
00882
00883 GUITownList::SortFunction * const TownDirectoryWindow::sorter_funcs[] = {
00884 &TownNameSorter,
00885 &TownPopulationSorter,
00886 };
00887
00888 static const WindowDesc _town_directory_desc(
00889 WDP_AUTO, 208, 202,
00890 WC_TOWN_DIRECTORY, WC_NONE,
00891 WDF_UNCLICK_BUTTONS,
00892 _nested_town_directory_widgets, lengthof(_nested_town_directory_widgets)
00893 );
00894
00895 void ShowTownDirectory()
00896 {
00897 if (BringWindowToFrontById(WC_TOWN_DIRECTORY, 0)) return;
00898 new TownDirectoryWindow(&_town_directory_desc);
00899 }
00900
00901 void CcFoundTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00902 {
00903 if (result.Failed()) return;
00904
00905 SndPlayTileFx(SND_1F_SPLAT, tile);
00906 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
00907 }
00908
00909 void CcFoundRandomTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00910 {
00911 if (result.Succeeded()) ScrollMainWindowToTile(Town::Get(_new_town_id)->xy);
00912 }
00913
00914 static const NWidgetPart _nested_found_town_widgets[] = {
00915 NWidget(NWID_HORIZONTAL),
00916 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00917 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FOUND_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00918 NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
00919 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
00920 EndContainer(),
00921
00922 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00923 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00924 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_NEW_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
00925 SetDataTip(STR_FOUND_TOWN_NEW_TOWN_BUTTON, STR_FOUND_TOWN_NEW_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
00926 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_RANDOM_TOWN), SetMinimalSize(156, 12), SetFill(1, 0),
00927 SetDataTip(STR_FOUND_TOWN_RANDOM_TOWN_BUTTON, STR_FOUND_TOWN_RANDOM_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
00928 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_MANY_RANDOM_TOWNS), SetMinimalSize(156, 12), SetFill(1, 0),
00929 SetDataTip(STR_FOUND_TOWN_MANY_RANDOM_TOWNS, STR_FOUND_TOWN_RANDOM_TOWNS_TOOLTIP), SetPadding(0, 2, 0, 2),
00930
00931 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(156, 14), SetPadding(0, 2, 0, 2), SetDataTip(STR_FOUND_TOWN_NAME_TITLE, STR_NULL),
00932 NWidget(WWT_EDITBOX, COLOUR_WHITE, WID_TF_TOWN_NAME_EDITBOX), SetMinimalSize(156, 12), SetPadding(0, 2, 3, 2),
00933 SetDataTip(STR_FOUND_TOWN_NAME_EDITOR_TITLE, STR_FOUND_TOWN_NAME_EDITOR_HELP),
00934 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_TOWN_NAME_RANDOM), SetMinimalSize(78, 12), SetPadding(0, 2, 0, 2), SetFill(1, 0),
00935 SetDataTip(STR_FOUND_TOWN_NAME_RANDOM_BUTTON, STR_FOUND_TOWN_NAME_RANDOM_TOOLTIP),
00936
00937 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
00938 NWidget(NWID_SPACER), SetFill(1, 0),
00939 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_TITLE, STR_NULL),
00940 NWidget(NWID_SPACER), SetFill(1, 0),
00941 EndContainer(),
00942 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
00943 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_SMALL), SetMinimalSize(78, 12), SetFill(1, 0),
00944 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_SMALL_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
00945 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_MEDIUM), SetMinimalSize(78, 12), SetFill(1, 0),
00946 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_MEDIUM_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
00947 EndContainer(),
00948 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00949 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
00950 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_LARGE), SetMinimalSize(78, 12), SetFill(1, 0),
00951 SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_LARGE_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
00952 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_SIZE_RANDOM), SetMinimalSize(78, 12), SetFill(1, 0),
00953 SetDataTip(STR_FOUND_TOWN_SIZE_RANDOM, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
00954 EndContainer(),
00955 NWidget(NWID_SPACER), SetMinimalSize(0, 3),
00956 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_CITY), SetPadding(0, 2, 0, 2), SetMinimalSize(156, 12), SetFill(1, 0),
00957 SetDataTip(STR_FOUND_TOWN_CITY, STR_FOUND_TOWN_CITY_TOOLTIP), SetFill(1, 0),
00958
00959 NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
00960 NWidget(NWID_SPACER), SetFill(1, 0),
00961 NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_ROAD_LAYOUT, STR_NULL),
00962 NWidget(NWID_SPACER), SetFill(1, 0),
00963 EndContainer(),
00964 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
00965 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_ORIGINAL), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_ORIGINAL, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT),
00966 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_BETTER), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_BETTER_ROADS, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT),
00967 EndContainer(),
00968 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00969 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
00970 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_GRID2), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_2X2_GRID, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT),
00971 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_GRID3), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_3X3_GRID, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT),
00972 EndContainer(),
00973 NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00974 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_TF_LAYOUT_RANDOM), SetPadding(0, 2, 0, 2), SetMinimalSize(0, 12), SetFill(1, 0),
00975 SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_RANDOM, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT), SetFill(1, 0),
00976 NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00977 EndContainer(),
00978 };
00979
00981 struct FoundTownWindow : QueryStringBaseWindow {
00982 private:
00983 TownSize town_size;
00984 TownLayout town_layout;
00985 bool city;
00986 bool townnamevalid;
00987 uint32 townnameparts;
00988 TownNameParams params;
00989
00990 public:
00991 FoundTownWindow(const WindowDesc *desc, WindowNumber window_number) :
00992 QueryStringBaseWindow(MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH, MAX_LENGTH_TOWN_NAME_CHARS),
00993 town_size(TSZ_MEDIUM),
00994 town_layout(_settings_game.economy.town_layout),
00995 params(_settings_game.game_creation.town_name)
00996 {
00997 this->InitNested(desc, window_number);
00998 this->text.Initialize(this->edit_str_buf, this->edit_str_size, this->max_chars);
00999 this->RandomTownName();
01000 this->UpdateButtons(true);
01001 }
01002
01003 void RandomTownName()
01004 {
01005 this->townnamevalid = GenerateTownName(&this->townnameparts);
01006
01007 if (!this->townnamevalid) {
01008 this->edit_str_buf[0] = '\0';
01009 } else {
01010 GetTownName(this->edit_str_buf, &this->params, this->townnameparts, &this->edit_str_buf[this->edit_str_size - 1]);
01011 }
01012 this->text.UpdateSize();
01013 UpdateOSKOriginalText(this, WID_TF_TOWN_NAME_EDITBOX);
01014
01015 this->SetWidgetDirty(WID_TF_TOWN_NAME_EDITBOX);
01016 }
01017
01018 void UpdateButtons(bool check_availability)
01019 {
01020 if (check_availability && _game_mode != GM_EDITOR) {
01021 this->SetWidgetsDisabledState(true, WID_TF_RANDOM_TOWN, WID_TF_MANY_RANDOM_TOWNS, WID_TF_SIZE_LARGE, WIDGET_LIST_END);
01022 this->SetWidgetsDisabledState(_settings_game.economy.found_town != TF_CUSTOM_LAYOUT,
01023 WID_TF_LAYOUT_ORIGINAL, WID_TF_LAYOUT_BETTER, WID_TF_LAYOUT_GRID2, WID_TF_LAYOUT_GRID3, WID_TF_LAYOUT_RANDOM, WIDGET_LIST_END);
01024 if (_settings_game.economy.found_town != TF_CUSTOM_LAYOUT) town_layout = _settings_game.economy.town_layout;
01025 }
01026
01027 for (int i = WID_TF_SIZE_SMALL; i <= WID_TF_SIZE_RANDOM; i++) {
01028 this->SetWidgetLoweredState(i, i == WID_TF_SIZE_SMALL + this->town_size);
01029 }
01030
01031 this->SetWidgetLoweredState(WID_TF_CITY, this->city);
01032
01033 for (int i = WID_TF_LAYOUT_ORIGINAL; i <= WID_TF_LAYOUT_RANDOM; i++) {
01034 this->SetWidgetLoweredState(i, i == WID_TF_LAYOUT_ORIGINAL + this->town_layout);
01035 }
01036
01037 this->SetDirty();
01038 }
01039
01040 void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, CommandCallback cc)
01041 {
01042 const char *name = NULL;
01043
01044 if (!this->townnamevalid) {
01045 name = this->edit_str_buf;
01046 } else {
01047
01048 char buf[MAX_LENGTH_TOWN_NAME_CHARS * MAX_CHAR_LENGTH];
01049 GetTownName(buf, &this->params, this->townnameparts, lastof(buf));
01050 if (strcmp(buf, this->edit_str_buf) != 0) name = this->edit_str_buf;
01051 }
01052
01053 bool success = DoCommandP(tile, this->town_size | this->city << 2 | this->town_layout << 3 | random << 6,
01054 townnameparts, CMD_FOUND_TOWN | CMD_MSG(errstr), cc, name);
01055
01056 if (success) this->RandomTownName();
01057 }
01058
01059 virtual void OnPaint()
01060 {
01061 this->DrawWidgets();
01062 if (!this->IsShaded()) this->DrawEditBox(WID_TF_TOWN_NAME_EDITBOX);
01063 }
01064
01065 virtual void OnClick(Point pt, int widget, int click_count)
01066 {
01067 switch (widget) {
01068 case WID_TF_NEW_TOWN:
01069 HandlePlacePushButton(this, WID_TF_NEW_TOWN, SPR_CURSOR_TOWN, HT_RECT);
01070 break;
01071
01072 case WID_TF_RANDOM_TOWN:
01073 this->HandleButtonClick(WID_TF_RANDOM_TOWN);
01074 this->ExecuteFoundTownCommand(0, true, STR_ERROR_CAN_T_GENERATE_TOWN, CcFoundRandomTown);
01075 break;
01076
01077 case WID_TF_TOWN_NAME_RANDOM:
01078 this->RandomTownName();
01079 this->SetFocusedWidget(WID_TF_TOWN_NAME_EDITBOX);
01080 break;
01081
01082 case WID_TF_MANY_RANDOM_TOWNS:
01083 this->HandleButtonClick(WID_TF_MANY_RANDOM_TOWNS);
01084
01085 _generating_world = true;
01086 UpdateNearestTownForRoadTiles(true);
01087 if (!GenerateTowns(this->town_layout)) {
01088 ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_TOWN, STR_ERROR_NO_SPACE_FOR_TOWN, WL_INFO);
01089 }
01090 UpdateNearestTownForRoadTiles(false);
01091 _generating_world = false;
01092 break;
01093
01094 case WID_TF_SIZE_SMALL: case WID_TF_SIZE_MEDIUM: case WID_TF_SIZE_LARGE: case WID_TF_SIZE_RANDOM:
01095 this->town_size = (TownSize)(widget - WID_TF_SIZE_SMALL);
01096 this->UpdateButtons(false);
01097 break;
01098
01099 case WID_TF_CITY:
01100 this->city ^= true;
01101 this->SetWidgetLoweredState(WID_TF_CITY, this->city);
01102 this->SetDirty();
01103 break;
01104
01105 case WID_TF_LAYOUT_ORIGINAL: case WID_TF_LAYOUT_BETTER: case WID_TF_LAYOUT_GRID2:
01106 case WID_TF_LAYOUT_GRID3: case WID_TF_LAYOUT_RANDOM:
01107 this->town_layout = (TownLayout)(widget - WID_TF_LAYOUT_ORIGINAL);
01108 this->UpdateButtons(false);
01109 break;
01110 }
01111 }
01112
01113 virtual void OnTimeout()
01114 {
01115 this->RaiseWidget(WID_TF_RANDOM_TOWN);
01116 this->RaiseWidget(WID_TF_MANY_RANDOM_TOWNS);
01117 this->SetDirty();
01118 }
01119
01120 virtual void OnMouseLoop()
01121 {
01122 this->HandleEditBox(WID_TF_TOWN_NAME_EDITBOX);
01123 }
01124
01125 virtual EventState OnKeyPress(uint16 key, uint16 keycode)
01126 {
01127 EventState state = ES_NOT_HANDLED;
01128 if (this->HandleEditBoxKey(WID_TF_TOWN_NAME_EDITBOX, key, keycode, state) == HEBR_CANCEL) {
01129 this->UnfocusFocusedWidget();
01130 }
01131 return state;
01132 }
01133
01134 virtual void OnPlaceObject(Point pt, TileIndex tile)
01135 {
01136 this->ExecuteFoundTownCommand(tile, false, STR_ERROR_CAN_T_FOUND_TOWN_HERE, CcFoundTown);
01137 }
01138
01139 virtual void OnPlaceObjectAbort()
01140 {
01141 this->RaiseButtons();
01142 this->UpdateButtons(false);
01143 }
01144
01150 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01151 {
01152 if (!gui_scope) return;
01153 this->UpdateButtons(true);
01154 }
01155 };
01156
01157 static const WindowDesc _found_town_desc(
01158 WDP_AUTO, 160, 162,
01159 WC_FOUND_TOWN, WC_NONE,
01160 WDF_CONSTRUCTION,
01161 _nested_found_town_widgets, lengthof(_nested_found_town_widgets)
01162 );
01163
01164 void ShowFoundTownWindow()
01165 {
01166 if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
01167 AllocateWindowDescFront<FoundTownWindow>(&_found_town_desc, 0);
01168 }