town_gui.cpp

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

Generated on Thu Feb 4 17:20:30 2010 for OpenTTD by  doxygen 1.5.6