town_gui.cpp

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

Generated on Wed Dec 23 23:27:56 2009 for OpenTTD by  doxygen 1.5.6