town_gui.cpp

Go to the documentation of this file.
00001 /* $Id: town_gui.cpp 18809 2010-01-15 16:41:15Z 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 "openttd.h"
00014 #include "town.h"
00015 #include "viewport_func.h"
00016 #include "gfx_func.h"
00017 #include "gui.h"
00018 #include "command_func.h"
00019 #include "company_func.h"
00020 #include "company_base.h"
00021 #include "company_gui.h"
00022 #include "network/network.h"
00023 #include "variables.h"
00024 #include "strings_func.h"
00025 #include "sound_func.h"
00026 #include "economy_func.h"
00027 #include "tilehighlight_func.h"
00028 #include "sortlist_type.h"
00029 #include "road_cmd.h"
00030 #include "landscape.h"
00031 #include "cargotype.h"
00032 #include "querystring_gui.h"
00033 #include "window_func.h"
00034 #include "townname_func.h"
00035 #include "townname_type.h"
00036 #include "core/geometry_func.hpp"
00037 
00038 #include "table/sprites.h"
00039 #include "table/strings.h"
00040 
00041 typedef GUIList<const Town*> GUITownList;
00042 
00044 enum TownAuthorityWidgets {
00045   TWA_CAPTION,
00046   TWA_RATING_INFO,  
00047   TWA_COMMAND_LIST, 
00048   TWA_SCROLLBAR,
00049   TWA_ACTION_INFO,  
00050   TWA_EXECUTE,      
00051 };
00052 
00053 static const NWidgetPart _nested_town_authority_widgets[] = {
00054   NWidget(NWID_HORIZONTAL),
00055     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00056     NWidget(WWT_CAPTION, COLOUR_BROWN, TWA_CAPTION), SetDataTip(STR_LOCAL_AUTHORITY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00057     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00058     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00059   EndContainer(),
00060   NWidget(WWT_PANEL, COLOUR_BROWN, TWA_RATING_INFO), SetMinimalSize(317, 92), SetResize(1, 1), EndContainer(),
00061   NWidget(NWID_HORIZONTAL),
00062     NWidget(WWT_PANEL, COLOUR_BROWN, TWA_COMMAND_LIST), SetMinimalSize(305, 52), SetResize(1, 0), SetDataTip(0x0, STR_LOCAL_AUTHORITY_ACTIONS_TOOLTIP), EndContainer(),
00063     NWidget(WWT_SCROLLBAR, COLOUR_BROWN, TWA_SCROLLBAR),
00064   EndContainer(),
00065   NWidget(WWT_PANEL, COLOUR_BROWN, TWA_ACTION_INFO), SetMinimalSize(317, 52), SetResize(1, 0), EndContainer(),
00066   NWidget(NWID_HORIZONTAL),
00067     NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, TWA_EXECUTE),  SetMinimalSize(317, 12), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_LOCAL_AUTHORITY_DO_IT_BUTTON, STR_LOCAL_AUTHORITY_DO_IT_TOOLTIP),
00068     NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00069   EndContainer()
00070 };
00071 
00073 struct TownAuthorityWindow : Window {
00074 private:
00075   Town *town;    
00076   int sel_index; 
00077 
00087   static int GetNthSetBit(uint32 bits, int n)
00088   {
00089     if (n >= 0) {
00090       uint i;
00091       FOR_EACH_SET_BIT(i, bits) {
00092         n--;
00093         if (n < 0) return i;
00094       }
00095     }
00096     return -1;
00097   }
00098 
00099 public:
00100   TownAuthorityWindow(const WindowDesc *desc, WindowNumber window_number) : Window(), sel_index(-1)
00101   {
00102     this->town = Town::Get(window_number);
00103     this->InitNested(desc, window_number);
00104     this->vscroll.SetCapacity((this->GetWidget<NWidgetBase>(TWA_COMMAND_LIST)->current_y - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM) / FONT_HEIGHT_NORMAL);
00105   }
00106 
00107   virtual void OnPaint()
00108   {
00109     int numact;
00110     uint buttons = GetMaskOfTownActions(&numact, _local_company, this->town);
00111 
00112     this->vscroll.SetCount(numact + 1);
00113 
00114     if (this->sel_index != -1 && !HasBit(buttons, this->sel_index)) {
00115       this->sel_index = -1;
00116     }
00117 
00118     this->SetWidgetDisabledState(TWA_EXECUTE, this->sel_index == -1);
00119 
00120     this->DrawWidgets();
00121     if (!this->IsShaded()) this->DrawRatings();
00122   }
00123 
00125   void DrawRatings()
00126   {
00127     NWidgetBase *nwid = this->GetWidget<NWidgetBase>(TWA_RATING_INFO);
00128     uint left = nwid->pos_x + WD_FRAMERECT_LEFT;
00129     uint right = nwid->pos_x + nwid->current_x - 1 - WD_FRAMERECT_RIGHT;
00130 
00131     uint y = nwid->pos_y + WD_FRAMERECT_TOP;
00132 
00133     DrawString(left, right, y, STR_LOCAL_AUTHORITY_COMPANY_RATINGS);
00134     y += FONT_HEIGHT_NORMAL;
00135 
00136     int sprite_y_offset = (FONT_HEIGHT_NORMAL - 10) / 2;
00137     bool rtl = _dynlang.text_dir == TD_RTL;
00138     uint text_left  = left + (rtl ? 0 : 26);
00139     uint text_right = right - (rtl ? 26 : 0);
00140     uint icon_left  = rtl ? right - 14 : left;
00141     uint blob_left  = rtl ? right - 24 : left + 16;
00142 
00143     /* Draw list of companies */
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 + sprite_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) || // Apalling
00155         (str++,                    r <= RATING_VERYPOOR)  || // Very Poor
00156         (str++,                    r <= RATING_POOR)      || // Poor
00157         (str++,                    r <= RATING_MEDIOCRE)  || // Mediocore
00158         (str++,                    r <= RATING_GOOD)      || // Good
00159         (str++,                    r <= RATING_VERYGOOD)  || // Very Good
00160         (str++,                    r <= RATING_EXCELLENT) || // Excellent
00161         (str++,                    true);                    // Outstanding
00162 
00163         SetDParam(2, str);
00164         if (this->town->exclusivity == c->index) { // red icon for company with exclusive rights
00165           DrawSprite(SPR_BLOT, PALETTE_TO_RED, blob_left, y + sprite_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; // Compute needed size of the widget.
00174     if (y > nwid->current_y) {
00175       /* If the company list is too big to fit, mark ourself dirty and draw again. */
00176       ResizeWindow(this, 0, y - nwid->current_y);
00177     }
00178   }
00179 
00180   virtual void SetStringParameters(int widget) const
00181   {
00182     if (widget == TWA_CAPTION) SetDParam(0, this->window_number);
00183   }
00184 
00185   virtual void DrawWidget(const Rect &r, int widget) const
00186   {
00187     switch (widget) {
00188       case TWA_ACTION_INFO:
00189         if (this->sel_index != -1) {
00190           SetDParam(1, _price[PR_TOWN_ACTION] * _town_action_costs[this->sel_index] >> 8);
00191           SetDParam(0, STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + this->sel_index);
00192           DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
00193                 STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING + this->sel_index);
00194         }
00195         break;
00196       case TWA_COMMAND_LIST: {
00197         int numact;
00198         uint buttons = GetMaskOfTownActions(&numact, _local_company, this->town);
00199         int y = r.top + WD_FRAMERECT_TOP;
00200         int pos = this->vscroll.GetPosition();
00201 
00202         if (--pos < 0) {
00203           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_LOCAL_AUTHORITY_ACTIONS_TITLE);
00204           y += FONT_HEIGHT_NORMAL;
00205         }
00206 
00207         for (int i = 0; buttons; i++, buttons >>= 1) {
00208           if (pos <= -5) break; 
00209 
00210           if ((buttons & 1) && --pos < 0) {
00211             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + i, 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 TWA_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(1, _price[PR_TOWN_ACTION] * _town_action_costs[i] >> 8);
00230           SetDParam(0, STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + i);
00231           d = maxdim(d, GetStringMultiLineBoundingBox(STR_LOCAL_AUTHORITY_ACTION_TOOLTIP_SMALL_ADVERTISING + i, *size));
00232         }
00233         *size = maxdim(*size, d);
00234         size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00235         size->height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00236         break;
00237       }
00238 
00239       case TWA_COMMAND_LIST:
00240         size->height = WD_FRAMERECT_TOP + 5 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
00241         size->width = GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTIONS_TITLE).width;
00242         for (uint i = 0; i < TACT_COUNT; i++ ) {
00243           size->width = max(size->width, GetStringBoundingBox(STR_LOCAL_AUTHORITY_ACTION_SMALL_ADVERTISING_CAMPAIGN + i).width);
00244         }
00245         size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00246         break;
00247 
00248       case TWA_RATING_INFO:
00249         resize->height = FONT_HEIGHT_NORMAL;
00250         size->height = WD_FRAMERECT_TOP + 9 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
00251         break;
00252     }
00253   }
00254 
00255   virtual void OnDoubleClick(Point pt, int widget) { HandleClick(pt, widget, true); }
00256   virtual void OnClick(Point pt, int widget) { HandleClick(pt, widget, false); }
00257 
00258   void HandleClick(Point pt, int widget, bool double_click)
00259   {
00260     switch (widget) {
00261       case TWA_COMMAND_LIST: {
00262         int y = (pt.y - this->GetWidget<NWidgetBase>(TWA_COMMAND_LIST)->pos_y - 1) / FONT_HEIGHT_NORMAL;
00263 
00264         if (!IsInsideMM(y, 0, 5)) return;
00265 
00266         y = GetNthSetBit(GetMaskOfTownActions(NULL, _local_company, this->town), y + this->vscroll.GetPosition() - 1);
00267         if (y >= 0) {
00268           this->sel_index = y;
00269           this->SetDirty();
00270         }
00271         /* Fall through to clicking in case we are double-clicked */
00272         if (!double_click || y < 0) break;
00273       }
00274 
00275       case TWA_EXECUTE:
00276         DoCommandP(this->town->xy, this->window_number, this->sel_index, CMD_DO_TOWN_ACTION | CMD_MSG(STR_ERROR_CAN_T_DO_THIS));
00277         break;
00278     }
00279   }
00280 
00281   virtual void OnHundredthTick()
00282   {
00283     this->SetDirty();
00284   }
00285 };
00286 
00287 static const WindowDesc _town_authority_desc(
00288   WDP_AUTO, 317, 222,
00289   WC_TOWN_AUTHORITY, WC_NONE,
00290   WDF_UNCLICK_BUTTONS,
00291   _nested_town_authority_widgets, lengthof(_nested_town_authority_widgets)
00292 );
00293 
00294 static void ShowTownAuthorityWindow(uint town)
00295 {
00296   AllocateWindowDescFront<TownAuthorityWindow>(&_town_authority_desc, town);
00297 }
00298 
00300 enum TownViewWidgets {
00301   TVW_CAPTION,
00302   TVW_VIEWPORT,
00303   TVW_INFOPANEL,
00304   TVW_CENTERVIEW,
00305   TVW_SHOWAUTHORITY,
00306   TVW_CHANGENAME,
00307   TVW_EXPAND,
00308   TVW_DELETE,
00309 };
00310 
00311 /* Town view window. */
00312 struct TownViewWindow : Window {
00313 private:
00314   Town *town; 
00315 
00316 public:
00317   enum {
00318     TVW_HEIGHT_NORMAL = 150,
00319   };
00320 
00321   TownViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
00322   {
00323     this->CreateNestedTree(desc);
00324 
00325     this->town = Town::Get(window_number);
00326     if (this->town->larger_town) this->GetWidget<NWidgetCore>(TVW_CAPTION)->widget_data = STR_TOWN_VIEW_CITY_CAPTION;
00327 
00328     this->FinishInitNested(desc, window_number);
00329 
00330     this->flags4 |= WF_DISABLE_VP_SCROLL;
00331     NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(TVW_VIEWPORT);
00332     nvp->InitializeViewport(this, this->town->xy, ZOOM_LVL_NEWS);
00333 
00334     /* disable renaming town in network games if you are not the server */
00335     this->SetWidgetDisabledState(TVW_CHANGENAME, _networking && !_network_server);
00336   }
00337 
00338   virtual void OnPaint()
00339   {
00340     this->DrawWidgets();
00341   }
00342 
00343   virtual void SetStringParameters(int widget) const
00344   {
00345     if (widget == TVW_CAPTION) SetDParam(0, this->town->index);
00346   }
00347 
00353   const CargoSpec *FindFirstCargoWithTownEffect(TownEffect effect) const
00354   {
00355     const CargoSpec *cs;
00356     FOR_ALL_CARGOSPECS(cs) {
00357       if (cs->town_effect == effect) return cs;
00358     }
00359     return NULL;
00360   }
00361 
00362   virtual void DrawWidget(const Rect &r, int widget) const
00363   {
00364     if (widget != TVW_INFOPANEL) return;
00365 
00366     uint y = r.top + WD_FRAMERECT_TOP;
00367 
00368     SetDParam(0, this->town->population);
00369     SetDParam(1, this->town->num_houses);
00370     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y, STR_TOWN_VIEW_POPULATION_HOUSES);
00371 
00372     SetDParam(0, this->town->act_pass);
00373     SetDParam(1, this->town->max_pass);
00374     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_PASSENGERS_LAST_MONTH_MAX);
00375 
00376     SetDParam(0, this->town->act_mail);
00377     SetDParam(1, this->town->max_mail);
00378     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_MAIL_LAST_MONTH_MAX);
00379 
00380     StringID required_text = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED;
00381     uint cargo_needed_for_growth = 0;
00382     switch (_settings_game.game_creation.landscape) {
00383       case LT_ARCTIC:
00384         if (TilePixelHeight(this->town->xy) >= LowestSnowLine()) cargo_needed_for_growth = 1;
00385         if (TilePixelHeight(this->town->xy) < GetSnowLine()) required_text = STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_WINTER;
00386         break;
00387 
00388       case LT_TROPIC:
00389         if (GetTropicZone(this->town->xy) == TROPICZONE_DESERT) cargo_needed_for_growth = 2;
00390         break;
00391 
00392       default: break;
00393     }
00394 
00395     if (cargo_needed_for_growth > 0) {
00396       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH);
00397 
00398       bool rtl = _dynlang.text_dir == TD_RTL;
00399       uint cargo_text_left = r.left + WD_FRAMERECT_LEFT + (rtl ? 0 : 20);
00400       uint cargo_text_right = r.right - WD_FRAMERECT_RIGHT - (rtl ? 20 : 0);
00401 
00402       const CargoSpec *food = FindFirstCargoWithTownEffect(TE_FOOD);
00403       CargoID first_food_cargo = (food != NULL) ? food->Index() : (CargoID)CT_INVALID;
00404       StringID food_name       = (food != NULL) ? food->name    : STR_CARGO_PLURAL_FOOD;
00405 
00406       const CargoSpec *water = FindFirstCargoWithTownEffect(TE_WATER);
00407       CargoID first_water_cargo = (water != NULL) ? water->Index() : (CargoID)CT_INVALID;
00408       StringID water_name       = (water != NULL) ? water->name    : STR_CARGO_PLURAL_WATER;
00409 
00410       if (first_food_cargo != CT_INVALID && this->town->act_food > 0) {
00411         SetDParam(0, first_food_cargo);
00412         SetDParam(1, this->town->act_food);
00413         DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_LAST_MONTH);
00414       } else {
00415         SetDParam(0, food_name);
00416         DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, required_text);
00417       }
00418 
00419       if (cargo_needed_for_growth > 1) {
00420         if (first_water_cargo != CT_INVALID && this->town->act_water > 0) {
00421           SetDParam(0, first_water_cargo);
00422           SetDParam(1, this->town->act_water);
00423           DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_LAST_MONTH);
00424         } else {
00425           SetDParam(0, water_name);
00426           DrawString(cargo_text_left, cargo_text_right, y += FONT_HEIGHT_NORMAL, required_text);
00427         }
00428       }
00429     }
00430 
00431     /* only show the town noise, if the noise option is activated. */
00432     if (_settings_game.economy.station_noise_level) {
00433       SetDParam(0, this->town->noise_reached);
00434       SetDParam(1, this->town->MaxTownNoise());
00435       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_NOISE_IN_TOWN);
00436     }
00437   }
00438 
00439   virtual void OnClick(Point pt, int widget)
00440   {
00441     switch (widget) {
00442       case TVW_CENTERVIEW: // scroll to location
00443         if (_ctrl_pressed) {
00444           ShowExtraViewPortWindow(this->town->xy);
00445         } else {
00446           ScrollMainWindowToTile(this->town->xy);
00447         }
00448         break;
00449 
00450       case TVW_SHOWAUTHORITY: // town authority
00451         ShowTownAuthorityWindow(this->window_number);
00452         break;
00453 
00454       case TVW_CHANGENAME: // rename
00455         SetDParam(0, this->window_number);
00456         ShowQueryString(STR_TOWN_NAME, STR_TOWN_VIEW_RENAME_TOWN_BUTTON, MAX_LENGTH_TOWN_NAME_BYTES, MAX_LENGTH_TOWN_NAME_PIXELS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT);
00457         break;
00458 
00459       case TVW_EXPAND: // expand town - only available on Scenario editor
00460         ExpandTown(this->town);
00461         break;
00462 
00463       case TVW_DELETE: // delete town - only available on Scenario editor
00464         delete this->town;
00465         break;
00466     }
00467   }
00468 
00469   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00470   {
00471     switch (widget) {
00472       case TVW_INFOPANEL:
00473         size->height = GetDesiredInfoHeight();
00474         break;
00475     }
00476   }
00477 
00482   uint GetDesiredInfoHeight() const
00483   {
00484     uint aimed_height = 3 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00485 
00486     switch (_settings_game.game_creation.landscape) {
00487       case LT_ARCTIC:
00488         if (TilePixelHeight(this->town->xy) >= LowestSnowLine()) aimed_height += 2 * FONT_HEIGHT_NORMAL;
00489         break;
00490 
00491       case LT_TROPIC:
00492         if (GetTropicZone(this->town->xy) == TROPICZONE_DESERT) aimed_height += 3 * FONT_HEIGHT_NORMAL;
00493         break;
00494 
00495       default: break;
00496     }
00497 
00498     if (_settings_game.economy.station_noise_level) aimed_height += FONT_HEIGHT_NORMAL;
00499 
00500     return aimed_height;
00501   }
00502 
00503   void ResizeWindowAsNeeded()
00504   {
00505     const NWidgetBase *nwid_info = this->GetWidget<NWidgetBase>(TVW_INFOPANEL);
00506     uint aimed_height = GetDesiredInfoHeight();
00507     if (aimed_height > nwid_info->current_y || (aimed_height < nwid_info->current_y && nwid_info->current_y > nwid_info->smallest_y)) {
00508       this->ReInit();
00509     }
00510   }
00511 
00512   virtual void OnResize()
00513   {
00514     if (this->viewport != NULL) {
00515       NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(TVW_VIEWPORT);
00516       nvp->UpdateViewportCoordinates(this);
00517     }
00518   }
00519 
00520   virtual void OnInvalidateData(int data = 0)
00521   {
00522     /* Called when setting station noise have changed, in order to resize the window */
00523     this->SetDirty(); // refresh display for current size. This will allow to avoid glitches when downgrading
00524     this->ResizeWindowAsNeeded();
00525   }
00526 
00527   virtual void OnQueryTextFinished(char *str)
00528   {
00529     if (str == NULL) return;
00530 
00531     DoCommandP(0, this->window_number, 0, CMD_RENAME_TOWN | CMD_MSG(STR_ERROR_CAN_T_RENAME_TOWN), NULL, str);
00532   }
00533 };
00534 
00535 static const NWidgetPart _nested_town_game_view_widgets[] = {
00536   NWidget(NWID_HORIZONTAL),
00537     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00538     NWidget(WWT_CAPTION, COLOUR_BROWN, TVW_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00539     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00540     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00541   EndContainer(),
00542   NWidget(WWT_PANEL, COLOUR_BROWN),
00543     NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
00544       NWidget(NWID_VIEWPORT, INVALID_COLOUR, TVW_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 0), SetResize(1, 1), SetPadding(1, 1, 1, 1),
00545     EndContainer(),
00546   EndContainer(),
00547   NWidget(WWT_PANEL, COLOUR_BROWN, TVW_INFOPANEL), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
00548   NWidget(NWID_HORIZONTAL),
00549     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00550       NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, TVW_CENTERVIEW), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_LOCATION, STR_TOWN_VIEW_CENTER_TOOLTIP),
00551       NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, TVW_SHOWAUTHORITY), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_LOCAL_AUTHORITY_BUTTON, STR_TOWN_VIEW_LOCAL_AUTHORITY_TOOLTIP),
00552       NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, TVW_CHANGENAME), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
00553     EndContainer(),
00554     NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00555   EndContainer(),
00556 };
00557 
00558 static const WindowDesc _town_game_view_desc(
00559   WDP_AUTO, 260, TownViewWindow::TVW_HEIGHT_NORMAL,
00560   WC_TOWN_VIEW, WC_NONE,
00561   WDF_UNCLICK_BUTTONS,
00562   _nested_town_game_view_widgets, lengthof(_nested_town_game_view_widgets)
00563 );
00564 
00565 static const NWidgetPart _nested_town_editor_view_widgets[] = {
00566   NWidget(NWID_HORIZONTAL),
00567     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00568     NWidget(WWT_CAPTION, COLOUR_BROWN, TVW_CAPTION), SetDataTip(STR_TOWN_VIEW_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00569     NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, TVW_CHANGENAME), SetMinimalSize(76, 14), SetDataTip(STR_BUTTON_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
00570     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00571     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00572   EndContainer(),
00573   NWidget(WWT_PANEL, COLOUR_BROWN),
00574     NWidget(WWT_INSET, COLOUR_BROWN), SetPadding(2, 2, 2, 2),
00575       NWidget(NWID_VIEWPORT, INVALID_COLOUR, TVW_VIEWPORT), SetMinimalSize(254, 86), SetFill(1, 1), SetResize(1, 1), SetPadding(1, 1, 1, 1),
00576     EndContainer(),
00577   EndContainer(),
00578   NWidget(WWT_PANEL, COLOUR_BROWN, TVW_INFOPANEL), SetMinimalSize(260, 32), SetResize(1, 0), SetFill(1, 0), EndContainer(),
00579   NWidget(NWID_HORIZONTAL),
00580     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00581       NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, TVW_CENTERVIEW), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_LOCATION, STR_TOWN_VIEW_CENTER_TOOLTIP),
00582       NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, TVW_EXPAND), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_EXPAND_BUTTON, STR_TOWN_VIEW_EXPAND_TOOLTIP),
00583       NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, TVW_DELETE), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_DELETE_BUTTON, STR_TOWN_VIEW_DELETE_TOOLTIP),
00584     EndContainer(),
00585     NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00586   EndContainer(),
00587 };
00588 
00589 static const WindowDesc _town_editor_view_desc(
00590   WDP_AUTO, 260, TownViewWindow::TVW_HEIGHT_NORMAL,
00591   WC_TOWN_VIEW, WC_NONE,
00592   WDF_UNCLICK_BUTTONS,
00593   _nested_town_editor_view_widgets, lengthof(_nested_town_editor_view_widgets)
00594 );
00595 
00596 void ShowTownViewWindow(TownID town)
00597 {
00598   if (_game_mode == GM_EDITOR) {
00599     AllocateWindowDescFront<TownViewWindow>(&_town_editor_view_desc, town);
00600   } else {
00601     AllocateWindowDescFront<TownViewWindow>(&_town_game_view_desc, town);
00602   }
00603 }
00604 
00606 enum TownDirectoryWidgets {
00607   TDW_SORTNAME,
00608   TDW_SORTPOPULATION,
00609   TDW_CENTERTOWN,
00610   TDW_SCROLLBAR,
00611   TDW_BOTTOM_PANEL,
00612   TDW_BOTTOM_TEXT,
00613 };
00614 
00615 static const NWidgetPart _nested_town_directory_widgets[] = {
00616   NWidget(NWID_HORIZONTAL),
00617     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00618     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_TOWN_DIRECTORY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00619     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00620     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00621   EndContainer(),
00622   NWidget(NWID_HORIZONTAL),
00623     NWidget(NWID_VERTICAL),
00624       NWidget(NWID_HORIZONTAL),
00625         NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, TDW_SORTNAME), SetMinimalSize(99, 12), SetDataTip(STR_SORT_BY_CAPTION_NAME, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0),
00626         NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, TDW_SORTPOPULATION), SetMinimalSize(97, 12), SetDataTip(STR_SORT_BY_CAPTION_POPULATION, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0),
00627       EndContainer(),
00628       NWidget(WWT_PANEL, COLOUR_BROWN, TDW_CENTERTOWN), SetMinimalSize(196, 164), SetDataTip(0x0, STR_TOWN_DIRECTORY_LIST_TOOLTIP),
00629               SetFill(1, 0), SetResize(0, 10), EndContainer(),
00630       NWidget(WWT_PANEL, COLOUR_BROWN, TDW_BOTTOM_PANEL),
00631         NWidget(WWT_TEXT, COLOUR_BROWN, TDW_BOTTOM_TEXT), SetPadding(2, 0, 0, 2), SetMinimalSize(196, 12), SetFill(1, 0), SetDataTip(STR_TOWN_POPULATION, STR_NULL),
00632       EndContainer(),
00633     EndContainer(),
00634     NWidget(NWID_VERTICAL),
00635       NWidget(WWT_SCROLLBAR, COLOUR_BROWN, TDW_SCROLLBAR),
00636       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00637     EndContainer(),
00638   EndContainer(),
00639 };
00640 
00642 struct TownDirectoryWindow : public Window {
00643 private:
00644   /* Runtime saved values */
00645   static Listing last_sorting;
00646   static const Town *last_town;
00647 
00648   /* Constants for sorting towns */
00649   static GUITownList::SortFunction * const sorter_funcs[];
00650 
00651   GUITownList towns;
00652 
00653   void BuildSortTownList()
00654   {
00655     if (this->towns.NeedRebuild()) {
00656       this->towns.Clear();
00657 
00658       const Town *t;
00659       FOR_ALL_TOWNS(t) {
00660         *this->towns.Append() = t;
00661       }
00662 
00663       this->towns.Compact();
00664       this->towns.RebuildDone();
00665       this->vscroll.SetCount(this->towns.Length()); // Update scrollbar as well.
00666     }
00667     /* Always sort the towns. */
00668     this->last_town = NULL;
00669     this->towns.Sort();
00670   }
00671 
00673   static int CDECL TownNameSorter(const Town * const *a, const Town * const *b)
00674   {
00675     static char buf_cache[64];
00676     const Town *ta = *a;
00677     const Town *tb = *b;
00678     char buf[64];
00679 
00680     SetDParam(0, ta->index);
00681     GetString(buf, STR_TOWN_NAME, lastof(buf));
00682 
00683     /* If 'b' is the same town as in the last round, use the cached value
00684      * We do this to speed stuff up ('b' is called with the same value a lot of
00685      * times after eachother) */
00686     if (tb != last_town) {
00687       last_town = tb;
00688       SetDParam(0, tb->index);
00689       GetString(buf_cache, STR_TOWN_NAME, lastof(buf_cache));
00690     }
00691 
00692     return strcmp(buf, buf_cache);
00693   }
00694 
00696   static int CDECL TownPopulationSorter(const Town * const *a, const Town * const *b)
00697   {
00698     return (*a)->population - (*b)->population;
00699   }
00700 
00701 public:
00702   TownDirectoryWindow(const WindowDesc *desc) : Window()
00703   {
00704     this->towns.SetListing(this->last_sorting);
00705     this->towns.SetSortFuncs(TownDirectoryWindow::sorter_funcs);
00706     this->towns.ForceRebuild();
00707     this->BuildSortTownList();
00708 
00709     this->InitNested(desc, 0);
00710   }
00711 
00712   ~TownDirectoryWindow()
00713   {
00714     this->last_sorting = this->towns.GetListing();
00715   }
00716 
00717   virtual void OnPaint()
00718   {
00719     this->DrawWidgets();
00720   }
00721 
00722   virtual void SetStringParameters(int widget) const
00723   {
00724     if (widget == TDW_BOTTOM_TEXT) SetDParam(0, GetWorldPopulation());
00725   }
00726 
00727   virtual void DrawWidget(const Rect &r, int widget) const
00728   {
00729     switch (widget) {
00730       case TDW_SORTNAME:
00731         if (this->towns.SortType() == 0) this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
00732         break;
00733 
00734       case TDW_SORTPOPULATION:
00735         if (this->towns.SortType() != 0) this->DrawSortButtonState(widget, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
00736         break;
00737 
00738       case TDW_CENTERTOWN: {
00739         int n = 0;
00740         int y = r.top + WD_FRAMERECT_TOP;
00741         if (this->towns.Length() == 0) { // No towns available.
00742           DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y, STR_TOWN_DIRECTORY_NONE);
00743           break;
00744         }
00745         /* At least one town available. */
00746         for (uint i = this->vscroll.GetPosition(); i < this->towns.Length(); i++) {
00747           const Town *t = this->towns[i];
00748 
00749           assert(t->xy != INVALID_TILE);
00750 
00751           SetDParam(0, t->index);
00752           SetDParam(1, t->population);
00753           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TOWN_DIRECTORY_TOWN);
00754 
00755           y += this->resize.step_height;
00756           if (++n == this->vscroll.GetCapacity()) break; // max number of towns in 1 window
00757         }
00758       } break;
00759     }
00760   }
00761 
00762   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00763   {
00764     switch (widget) {
00765       case TDW_SORTNAME:
00766       case TDW_SORTPOPULATION: {
00767         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
00768         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the word is centered, also looks nice.
00769         d.height += padding.height;
00770         *size = maxdim(*size, d);
00771         break;
00772       }
00773       case TDW_CENTERTOWN: {
00774         Dimension d = GetStringBoundingBox(STR_TOWN_DIRECTORY_NONE);
00775         for (uint i = 0; i < this->towns.Length(); i++) {
00776           const Town *t = this->towns[i];
00777 
00778           assert(t != NULL);
00779 
00780           SetDParam(0, t->index);
00781           SetDParam(1, 10000000); // 10^7
00782           d = maxdim(d, GetStringBoundingBox(STR_TOWN_DIRECTORY_TOWN));
00783         }
00784         d.width += padding.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00785         d.height += padding.height;
00786         *size = maxdim(*size, d);
00787         resize->height = d.height;
00788         break;
00789       }
00790       case TDW_BOTTOM_TEXT: {
00791         SetDParam(0, 1000000000); // 10^9
00792         Dimension d = GetStringBoundingBox(STR_TOWN_POPULATION);
00793         d.width += padding.width;
00794         d.height += padding.height;
00795         *size = maxdim(*size, d);
00796         break;
00797       }
00798     }
00799   }
00800 
00801   virtual void OnClick(Point pt, int widget)
00802   {
00803     switch (widget) {
00804       case TDW_SORTNAME: // Sort by Name ascending/descending
00805         if (this->towns.SortType() == 0) {
00806           this->towns.ToggleSortOrder();
00807         } else {
00808           this->towns.SetSortType(0);
00809         }
00810         this->BuildSortTownList();
00811         this->SetDirty();
00812         break;
00813 
00814       case TDW_SORTPOPULATION: // Sort by Population ascending/descending
00815         if (this->towns.SortType() == 1) {
00816           this->towns.ToggleSortOrder();
00817         } else {
00818           this->towns.SetSortType(1);
00819         }
00820         this->BuildSortTownList();
00821         this->SetDirty();
00822         break;
00823 
00824       case TDW_CENTERTOWN: { // Click on Town Matrix
00825         uint16 id_v = (pt.y - this->GetWidget<NWidgetBase>(widget)->pos_y - WD_FRAMERECT_TOP) / this->resize.step_height;
00826 
00827         if (id_v >= this->vscroll.GetCapacity()) return; // click out of bounds
00828 
00829         id_v += this->vscroll.GetPosition();
00830 
00831         if (id_v >= this->towns.Length()) return; // click out of town bounds
00832 
00833         const Town *t = this->towns[id_v];
00834         assert(t != NULL);
00835         if (_ctrl_pressed) {
00836           ShowExtraViewPortWindow(t->xy);
00837         } else {
00838           ScrollMainWindowToTile(t->xy);
00839         }
00840         break;
00841       }
00842     }
00843   }
00844 
00845   virtual void OnHundredthTick()
00846   {
00847     this->BuildSortTownList();
00848     this->SetDirty();
00849   }
00850 
00851   virtual void OnResize()
00852   {
00853     this->vscroll.SetCapacityFromWidget(this, TDW_CENTERTOWN);
00854   }
00855 
00856   virtual void OnInvalidateData(int data)
00857   {
00858     if (data == 0) {
00859       this->towns.ForceRebuild();
00860     } else {
00861       this->towns.ForceResort();
00862     }
00863     this->BuildSortTownList();
00864   }
00865 };
00866 
00867 Listing TownDirectoryWindow::last_sorting = {false, 0};
00868 const Town *TownDirectoryWindow::last_town = NULL;
00869 
00870 /* Available town directory sorting functions */
00871 GUITownList::SortFunction * const TownDirectoryWindow::sorter_funcs[] = {
00872   &TownNameSorter,
00873   &TownPopulationSorter,
00874 };
00875 
00876 static const WindowDesc _town_directory_desc(
00877   WDP_AUTO, 208, 202,
00878   WC_TOWN_DIRECTORY, WC_NONE,
00879   WDF_UNCLICK_BUTTONS,
00880   _nested_town_directory_widgets, lengthof(_nested_town_directory_widgets)
00881 );
00882 
00883 void ShowTownDirectory()
00884 {
00885   if (BringWindowToFrontById(WC_TOWN_DIRECTORY, 0)) return;
00886   new TownDirectoryWindow(&_town_directory_desc);
00887 }
00888 
00889 void CcFoundTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00890 {
00891   if (result.Failed()) return;
00892 
00893   SndPlayTileFx(SND_1F_SPLAT, tile);
00894   if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
00895 }
00896 
00897 void CcFoundRandomTown(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00898 {
00899   if (result.Succeeded()) ScrollMainWindowToTile(Town::Get(_new_town_id)->xy);
00900 }
00901 
00903 enum TownScenarioEditorWidgets {
00904   TSEW_BACKGROUND,
00905   TSEW_NEWTOWN,
00906   TSEW_RANDOMTOWN,
00907   TSEW_MANYRANDOMTOWNS,
00908   TSEW_TOWNNAME_TEXT,
00909   TSEW_TOWNNAME_EDITBOX,
00910   TSEW_TOWNNAME_RANDOM,
00911   TSEW_TOWNSIZE,
00912   TSEW_SIZE_SMALL,
00913   TSEW_SIZE_MEDIUM,
00914   TSEW_SIZE_LARGE,
00915   TSEW_SIZE_RANDOM,
00916   TSEW_CITY,
00917   TSEW_TOWNLAYOUT,
00918   TSEW_LAYOUT_ORIGINAL,
00919   TSEW_LAYOUT_BETTER,
00920   TSEW_LAYOUT_GRID2,
00921   TSEW_LAYOUT_GRID3,
00922   TSEW_LAYOUT_RANDOM,
00923 };
00924 
00925 static const NWidgetPart _nested_found_town_widgets[] = {
00926   NWidget(NWID_HORIZONTAL),
00927     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00928     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_FOUND_TOWN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00929     NWidget(WWT_SHADEBOX, COLOUR_DARK_GREEN),
00930     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
00931   EndContainer(),
00932   /* Construct new town(s) buttons. */
00933   NWidget(WWT_PANEL, COLOUR_DARK_GREEN, TSEW_BACKGROUND),
00934     NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00935     NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_NEWTOWN), SetMinimalSize(156, 12), SetFill(1, 0),
00936                     SetDataTip(STR_FOUND_TOWN_NEW_TOWN_BUTTON, STR_FOUND_TOWN_NEW_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
00937     NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_RANDOMTOWN), SetMinimalSize(156, 12), SetFill(1, 0),
00938                     SetDataTip(STR_FOUND_TOWN_RANDOM_TOWN_BUTTON, STR_FOUND_TOWN_RANDOM_TOWN_TOOLTIP), SetPadding(0, 2, 1, 2),
00939     NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_MANYRANDOMTOWNS), SetMinimalSize(156, 12), SetFill(1, 0),
00940                     SetDataTip(STR_FOUND_TOWN_MANY_RANDOM_TOWNS, STR_FOUND_TOWN_RANDOM_TOWNS_TOOLTIP), SetPadding(0, 2, 0, 2),
00941     /* Town name selection. */
00942     NWidget(WWT_LABEL, COLOUR_DARK_GREEN, TSEW_TOWNSIZE), SetMinimalSize(156, 14), SetPadding(0, 2, 0, 2), SetDataTip(STR_FOUND_TOWN_NAME_TITLE, STR_NULL),
00943     NWidget(WWT_EDITBOX, COLOUR_WHITE, TSEW_TOWNNAME_EDITBOX), SetMinimalSize(156, 12), SetPadding(0, 2, 3, 2),
00944                     SetDataTip(STR_FOUND_TOWN_NAME_EDITOR_TITLE, STR_FOUND_TOWN_NAME_EDITOR_HELP),
00945     NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_TOWNNAME_RANDOM), SetMinimalSize(78, 12), SetPadding(0, 2, 0, 2), SetFill(1, 0),
00946                     SetDataTip(STR_FOUND_TOWN_NAME_RANDOM_BUTTON, STR_FOUND_TOWN_NAME_RANDOM_TOOLTIP),
00947     /* Town size selection. */
00948     NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
00949       NWidget(NWID_SPACER), SetFill(1, 0),
00950       NWidget(WWT_LABEL, COLOUR_DARK_GREEN, TSEW_TOWNSIZE), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_TITLE, STR_NULL),
00951       NWidget(NWID_SPACER), SetFill(1, 0),
00952     EndContainer(),
00953     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
00954       NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_SIZE_SMALL), SetMinimalSize(78, 12), SetFill(1, 0),
00955                     SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_SMALL_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
00956       NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_SIZE_MEDIUM), SetMinimalSize(78, 12), SetFill(1, 0),
00957                     SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_MEDIUM_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
00958     EndContainer(),
00959     NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00960     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
00961       NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_SIZE_LARGE), SetMinimalSize(78, 12), SetFill(1, 0),
00962                     SetDataTip(STR_FOUND_TOWN_INITIAL_SIZE_LARGE_BUTTON, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
00963       NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_SIZE_RANDOM), SetMinimalSize(78, 12), SetFill(1, 0),
00964                     SetDataTip(STR_FOUND_TOWN_SIZE_RANDOM, STR_FOUND_TOWN_INITIAL_SIZE_TOOLTIP),
00965     EndContainer(),
00966     NWidget(NWID_SPACER), SetMinimalSize(0, 3),
00967     NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_CITY), SetPadding(0, 2, 0, 2), SetMinimalSize(156, 12), SetFill(1, 0),
00968                     SetDataTip(STR_FOUND_TOWN_CITY, STR_FOUND_TOWN_CITY_TOOLTIP), SetFill(1, 0),
00969     /* Town roads selection. */
00970     NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
00971       NWidget(NWID_SPACER), SetFill(1, 0),
00972       NWidget(WWT_LABEL, COLOUR_DARK_GREEN, TSEW_TOWNLAYOUT), SetMinimalSize(148, 14), SetDataTip(STR_FOUND_TOWN_ROAD_LAYOUT, STR_NULL),
00973       NWidget(NWID_SPACER), SetFill(1, 0),
00974     EndContainer(),
00975     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
00976       NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_LAYOUT_ORIGINAL), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_ORIGINAL, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT),
00977       NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_LAYOUT_BETTER), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_BETTER_ROADS, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT),
00978     EndContainer(),
00979     NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00980     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(2, 0, 2),
00981       NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_LAYOUT_GRID2), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_2X2_GRID, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT),
00982       NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_LAYOUT_GRID3), SetMinimalSize(78, 12), SetFill(1, 0), SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_3X3_GRID, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT),
00983     EndContainer(),
00984     NWidget(NWID_SPACER), SetMinimalSize(0, 1),
00985     NWidget(WWT_TEXTBTN, COLOUR_GREY, TSEW_LAYOUT_RANDOM), SetPadding(0, 2, 0, 2), SetMinimalSize(0, 12), SetFill(1, 0),
00986                     SetDataTip(STR_FOUND_TOWN_SELECT_LAYOUT_RANDOM, STR_FOUND_TOWN_SELECT_TOWN_ROAD_LAYOUT), SetFill(1, 0),
00987     NWidget(NWID_SPACER), SetMinimalSize(0, 2),
00988   EndContainer(),
00989 };
00990 
00992 struct FoundTownWindow : QueryStringBaseWindow {
00993 private:
00994   TownSize town_size;     
00995   TownLayout town_layout; 
00996   bool city;              
00997   bool townnamevalid;     
00998   uint32 townnameparts;   
00999   TownNameParams params;  
01000 
01001 public:
01002   FoundTownWindow(const WindowDesc *desc, WindowNumber window_number) :
01003       QueryStringBaseWindow(MAX_LENGTH_TOWN_NAME_BYTES),
01004       town_size(TS_MEDIUM),
01005       town_layout(_settings_game.economy.town_layout),
01006       params(_settings_game.game_creation.town_name)
01007   {
01008     this->InitNested(desc, window_number);
01009     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, MAX_LENGTH_TOWN_NAME_PIXELS);
01010     this->RandomTownName();
01011     this->UpdateButtons(true);
01012     this->SetFocusedWidget(TSEW_TOWNNAME_EDITBOX);
01013   }
01014 
01015   void RandomTownName()
01016   {
01017     this->townnamevalid = GenerateTownName(&this->townnameparts);
01018 
01019     if (!this->townnamevalid) {
01020       this->edit_str_buf[0] = '\0';
01021     } else {
01022       GetTownName(this->edit_str_buf, &this->params, this->townnameparts, &this->edit_str_buf[this->edit_str_size - 1]);
01023     }
01024     UpdateTextBufferSize(&this->text);
01025     UpdateOSKOriginalText(this, TSEW_TOWNNAME_EDITBOX);
01026 
01027     this->SetWidgetDirty(TSEW_TOWNNAME_EDITBOX);
01028   }
01029 
01030   void UpdateButtons(bool check_availability)
01031   {
01032     if (check_availability && _game_mode != GM_EDITOR) {
01033       this->SetWidgetsDisabledState(true, TSEW_RANDOMTOWN, TSEW_MANYRANDOMTOWNS, TSEW_SIZE_LARGE, WIDGET_LIST_END);
01034       this->SetWidgetsDisabledState(_settings_game.economy.found_town != TF_CUSTOM_LAYOUT,
01035           TSEW_LAYOUT_ORIGINAL, TSEW_LAYOUT_BETTER, TSEW_LAYOUT_GRID2, TSEW_LAYOUT_GRID3, TSEW_LAYOUT_RANDOM, WIDGET_LIST_END);
01036       if (_settings_game.economy.found_town != TF_CUSTOM_LAYOUT) town_layout = _settings_game.economy.town_layout;
01037     }
01038 
01039     for (int i = TSEW_SIZE_SMALL; i <= TSEW_SIZE_RANDOM; i++) {
01040       this->SetWidgetLoweredState(i, i == TSEW_SIZE_SMALL + this->town_size);
01041     }
01042 
01043     this->SetWidgetLoweredState(TSEW_CITY, this->city);
01044 
01045     for (int i = TSEW_LAYOUT_ORIGINAL; i <= TSEW_LAYOUT_RANDOM; i++) {
01046       this->SetWidgetLoweredState(i, i == TSEW_LAYOUT_ORIGINAL + this->town_layout);
01047     }
01048 
01049     this->SetDirty();
01050   }
01051 
01052   void ExecuteFoundTownCommand(TileIndex tile, bool random, StringID errstr, CommandCallback cc)
01053   {
01054     const char *name = NULL;
01055 
01056     if (!this->townnamevalid) {
01057       name = this->edit_str_buf;
01058     } else {
01059       /* If user changed the name, send it */
01060       char buf[MAX_LENGTH_TOWN_NAME_BYTES];
01061       GetTownName(buf, &this->params, this->townnameparts, lastof(buf));
01062       if (strcmp(buf, this->edit_str_buf) != 0) name = this->edit_str_buf;
01063     }
01064 
01065     bool success = DoCommandP(tile, this->town_size | this->city << 2 | this->town_layout << 3 | random << 6,
01066         townnameparts, CMD_FOUND_TOWN | CMD_MSG(errstr), cc, name);
01067 
01068     if (success) this->RandomTownName();
01069   }
01070 
01071   virtual void OnPaint()
01072   {
01073     this->DrawWidgets();
01074     if (!this->IsShaded()) this->DrawEditBox(TSEW_TOWNNAME_EDITBOX);
01075   }
01076 
01077   virtual void OnClick(Point pt, int widget)
01078   {
01079     switch (widget) {
01080       case TSEW_NEWTOWN:
01081         HandlePlacePushButton(this, TSEW_NEWTOWN, SPR_CURSOR_TOWN, HT_RECT, NULL);
01082         break;
01083 
01084       case TSEW_RANDOMTOWN:
01085         this->HandleButtonClick(TSEW_RANDOMTOWN);
01086         this->ExecuteFoundTownCommand(0, true, STR_ERROR_CAN_T_GENERATE_TOWN, CcFoundRandomTown);
01087         break;
01088 
01089       case TSEW_TOWNNAME_RANDOM:
01090         this->RandomTownName();
01091         this->SetFocusedWidget(TSEW_TOWNNAME_EDITBOX);
01092         break;
01093 
01094       case TSEW_MANYRANDOMTOWNS:
01095         this->HandleButtonClick(TSEW_MANYRANDOMTOWNS);
01096 
01097         _generating_world = true;
01098         UpdateNearestTownForRoadTiles(true);
01099         if (!GenerateTowns(this->town_layout)) {
01100           ShowErrorMessage(STR_ERROR_CAN_T_GENERATE_TOWN, STR_ERROR_NO_SPACE_FOR_TOWN, 0, 0);
01101         }
01102         UpdateNearestTownForRoadTiles(false);
01103         _generating_world = false;
01104         break;
01105 
01106       case TSEW_SIZE_SMALL: case TSEW_SIZE_MEDIUM: case TSEW_SIZE_LARGE: case TSEW_SIZE_RANDOM:
01107         this->town_size = (TownSize)(widget - TSEW_SIZE_SMALL);
01108         this->UpdateButtons(false);
01109         break;
01110 
01111       case TSEW_CITY:
01112         this->city ^= true;
01113         this->SetWidgetLoweredState(TSEW_CITY, this->city);
01114         this->SetDirty();
01115         break;
01116 
01117       case TSEW_LAYOUT_ORIGINAL: case TSEW_LAYOUT_BETTER: case TSEW_LAYOUT_GRID2:
01118       case TSEW_LAYOUT_GRID3: case TSEW_LAYOUT_RANDOM:
01119         this->town_layout = (TownLayout)(widget - TSEW_LAYOUT_ORIGINAL);
01120         this->UpdateButtons(false);
01121         break;
01122     }
01123   }
01124 
01125   virtual void OnTimeout()
01126   {
01127     this->RaiseWidget(TSEW_RANDOMTOWN);
01128     this->RaiseWidget(TSEW_MANYRANDOMTOWNS);
01129     this->SetDirty();
01130   }
01131 
01132   virtual void OnMouseLoop()
01133   {
01134     this->HandleEditBox(TSEW_TOWNNAME_EDITBOX);
01135   }
01136 
01137   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
01138   {
01139     EventState state;
01140     if (this->HandleEditBoxKey(TSEW_TOWNNAME_EDITBOX, key, keycode, state) == HEBR_CANCEL) {
01141       this->UnfocusFocusedWidget();
01142     }
01143     return state;
01144   }
01145 
01146   virtual void OnPlaceObject(Point pt, TileIndex tile)
01147   {
01148     this->ExecuteFoundTownCommand(tile, false, STR_ERROR_CAN_T_FOUND_TOWN_HERE, CcFoundTown);
01149   }
01150 
01151   virtual void OnPlaceObjectAbort()
01152   {
01153     this->RaiseButtons();
01154     this->UpdateButtons(false);
01155   }
01156 
01157   virtual void OnInvalidateData(int)
01158   {
01159     this->UpdateButtons(true);
01160   }
01161 };
01162 
01163 static const WindowDesc _found_town_desc(
01164   WDP_AUTO, 160, 162,
01165   WC_FOUND_TOWN, WC_NONE,
01166   WDF_CONSTRUCTION,
01167   _nested_found_town_widgets, lengthof(_nested_found_town_widgets)
01168 );
01169 
01170 void ShowFoundTownWindow()
01171 {
01172   if (_game_mode != GM_EDITOR && !Company::IsValidID(_local_company)) return;
01173   AllocateWindowDescFront<FoundTownWindow>(&_found_town_desc, 0);
01174 }

Generated on Wed Jan 20 23:38:41 2010 for OpenTTD by  doxygen 1.5.6