town_gui.cpp

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

Generated on Fri Feb 4 20:53:49 2011 for OpenTTD by  doxygen 1.6.1