town_gui.cpp

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

Generated on Mon May 9 05:19:02 2011 for OpenTTD by  doxygen 1.6.1