town_gui.cpp

Go to the documentation of this file.
00001 /* $Id: town_gui.cpp 14902 2009-01-07 17:40:17Z frosch $ */
00002 
00005 #include "stdafx.h"
00006 #include "openttd.h"
00007 #include "debug.h"
00008 #include "town.h"
00009 #include "viewport_func.h"
00010 #include "gfx_func.h"
00011 #include "gui.h"
00012 #include "window_gui.h"
00013 #include "textbuf_gui.h"
00014 #include "command_func.h"
00015 #include "company_func.h"
00016 #include "company_base.h"
00017 #include "company_gui.h"
00018 #include "network/network.h"
00019 #include "variables.h"
00020 #include "strings_func.h"
00021 #include "sound_func.h"
00022 #include "economy_func.h"
00023 #include "core/alloc_func.hpp"
00024 #include "settings_type.h"
00025 #include "tilehighlight_func.h"
00026 #include "string_func.h"
00027 #include "sortlist_type.h"
00028 #include "road_cmd.h"
00029 
00030 #include "table/sprites.h"
00031 #include "table/strings.h"
00032 
00033 typedef GUIList<const Town*> GUITownList;
00034 
00035 extern bool GenerateTowns();
00036 static int _scengen_town_size = 1; // depress medium-sized towns per default
00037 
00038 static const Widget _town_authority_widgets[] = {
00039 {   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_BROWN,     0,    10,     0,    13, STR_00C5,                 STR_018B_CLOSE_WINDOW},              // TWA_CLOSEBOX
00040 {    WWT_CAPTION,   RESIZE_NONE,  COLOUR_BROWN,    11,   316,     0,    13, STR_2022_LOCAL_AUTHORITY, STR_018C_WINDOW_TITLE_DRAG_THIS},    // TWA_CAPTION
00041 {      WWT_PANEL,   RESIZE_NONE,  COLOUR_BROWN,     0,   316,    14,   105, 0x0,                      STR_NULL},                           // TWA_RATING_INFO
00042 {      WWT_PANEL,   RESIZE_NONE,  COLOUR_BROWN,     0,   304,   106,   157, 0x0,                      STR_2043_LIST_OF_THINGS_TO_DO_AT},   // TWA_COMMAND_LIST
00043 {  WWT_SCROLLBAR,   RESIZE_NONE,  COLOUR_BROWN,   305,   316,   106,   157, 0x0,                      STR_0190_SCROLL_BAR_SCROLLS_LIST},   // TWA_SCROLLBAR
00044 {      WWT_PANEL,   RESIZE_NONE,  COLOUR_BROWN,     0,   316,   158,   209, 0x0,                      STR_NULL},                           // TWA_ACTION_INFO
00045 { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_BROWN,     0,   316,   210,   221, STR_2042_DO_IT,           STR_2044_CARRY_OUT_THE_HIGHLIGHTED}, // TWA_EXECUTE
00046 {   WIDGETS_END},
00047 };
00048 
00049 extern const byte _town_action_costs[8];
00050 
00051 enum TownActions {
00052   TACT_NONE             = 0x00,
00053 
00054   TACT_ADVERTISE_SMALL  = 0x01,
00055   TACT_ADVERTISE_MEDIUM = 0x02,
00056   TACT_ADVERTISE_LARGE  = 0x04,
00057   TACT_ROAD_REBUILD     = 0x08,
00058   TACT_BUILD_STATUE     = 0x10,
00059   TACT_FOUND_BUILDINGS  = 0x20,
00060   TACT_BUY_RIGHTS       = 0x40,
00061   TACT_BRIBE            = 0x80,
00062 
00063   TACT_ADVERTISE        = TACT_ADVERTISE_SMALL | TACT_ADVERTISE_MEDIUM | TACT_ADVERTISE_LARGE,
00064   TACT_CONSTRUCTION     = TACT_ROAD_REBUILD | TACT_BUILD_STATUE | TACT_FOUND_BUILDINGS,
00065   TACT_FUNDS            = TACT_BUY_RIGHTS | TACT_BRIBE,
00066   TACT_ALL              = TACT_ADVERTISE | TACT_CONSTRUCTION | TACT_FUNDS,
00067 };
00068 
00069 DECLARE_ENUM_AS_BIT_SET(TownActions);
00070 
00077 uint GetMaskOfTownActions(int *nump, CompanyID cid, const Town *t)
00078 {
00079   int num = 0;
00080   TownActions buttons = TACT_NONE;
00081 
00082   /* Spectators and unwanted have no options */
00083   if (cid != COMPANY_SPECTATOR && !(_settings_game.economy.bribe && t->unwanted[cid])) {
00084 
00085     /* Things worth more than this are not shown */
00086     Money avail = GetCompany(cid)->money + _price.station_value * 200;
00087     Money ref = _price.build_industry >> 8;
00088 
00089     /* Check the action bits for validity and
00090      * if they are valid add them */
00091     for (uint i = 0; i != lengthof(_town_action_costs); i++) {
00092       const TownActions cur = (TownActions)(1 << i);
00093 
00094       /* Is the company not able to bribe ? */
00095       if (cur == TACT_BRIBE && (!_settings_game.economy.bribe || t->ratings[cid] >= RATING_BRIBE_MAXIMUM))
00096         continue;
00097 
00098       /* Is the company not able to buy exclusive rights ? */
00099       if (cur == TACT_BUY_RIGHTS && !_settings_game.economy.exclusive_rights)
00100         continue;
00101 
00102       /* Is the company not able to build a statue ? */
00103       if (cur == TACT_BUILD_STATUE && HasBit(t->statues, cid))
00104         continue;
00105 
00106       if (avail >= _town_action_costs[i] * ref) {
00107         buttons |= cur;
00108         num++;
00109       }
00110     }
00111   }
00112 
00113   if (nump != NULL) *nump = num;
00114   return buttons;
00115 }
00116 
00117 struct TownAuthorityWindow : Window {
00118 private:
00119   Town *town;
00120   int sel_index;
00121 
00122   enum TownAuthorityWidget {
00123     TWA_CLOSEBOX = 0,
00124     TWA_CAPTION,
00125     TWA_RATING_INFO,
00126     TWA_COMMAND_LIST,
00127     TWA_SCROLLBAR,
00128     TWA_ACTION_INFO,
00129     TWA_EXECUTE,
00130   };
00131 
00141   static int GetNthSetBit(uint32 bits, int n)
00142   {
00143     if (n >= 0) {
00144       uint i;
00145       FOR_EACH_SET_BIT(i, bits) {
00146         n--;
00147         if (n < 0) return i;
00148       }
00149     }
00150     return -1;
00151   }
00152 
00153 public:
00154   TownAuthorityWindow(const WindowDesc *desc, WindowNumber window_number) :
00155       Window(desc, window_number), sel_index(-1)
00156   {
00157     this->town = GetTown(this->window_number);
00158     this->vscroll.cap = 5;
00159 
00160     this->FindWindowPlacementAndResize(desc);
00161   }
00162 
00163   virtual void OnPaint()
00164   {
00165     int numact;
00166     uint buttons = GetMaskOfTownActions(&numact, _local_company, this->town);
00167 
00168     SetVScrollCount(this, numact + 1);
00169 
00170     if (this->sel_index != -1 && !HasBit(buttons, this->sel_index)) {
00171       this->sel_index = -1;
00172     }
00173 
00174     this->SetWidgetDisabledState(6, this->sel_index == -1);
00175 
00176     SetDParam(0, this->window_number);
00177     this->DrawWidgets();
00178 
00179     DrawString(2, 15, STR_2023_TRANSPORT_COMPANY_RATINGS, TC_FROMSTRING);
00180 
00181     /* Draw list of companies */
00182     int y = 25;
00183 
00184     const Company *c;
00185     FOR_ALL_COMPANIES(c) {
00186       if ((HasBit(this->town->have_ratings, c->index) || this->town->exclusivity == c->index)) {
00187         DrawCompanyIcon(c->index, 2, y);
00188 
00189         SetDParam(0, c->index);
00190         SetDParam(1, c->index);
00191 
00192         int r = this->town->ratings[c->index];
00193         StringID str;
00194         (str = STR_3035_APPALLING, r <= RATING_APPALLING) || // Apalling
00195         (str++,                    r <= RATING_VERYPOOR)  || // Very Poor
00196         (str++,                    r <= RATING_POOR)      || // Poor
00197         (str++,                    r <= RATING_MEDIOCRE)  || // Mediocore
00198         (str++,                    r <= RATING_GOOD)      || // Good
00199         (str++,                    r <= RATING_VERYGOOD)  || // Very Good
00200         (str++,                    r <= RATING_EXCELLENT) || // Excellent
00201         (str++,                    true);                    // Outstanding
00202 
00203         SetDParam(2, str);
00204         if (this->town->exclusivity == c->index) { // red icon for company with exclusive rights
00205           DrawSprite(SPR_BLOT, PALETTE_TO_RED, 18, y);
00206         }
00207 
00208         DrawString(28, y, STR_2024, TC_FROMSTRING);
00209         y += 10;
00210       }
00211     }
00212     y = 107;
00213     int pos = this->vscroll.pos;
00214 
00215     if (--pos < 0) {
00216       DrawString(2, y, STR_2045_ACTIONS_AVAILABLE, TC_FROMSTRING);
00217       y += 10;
00218     }
00219 
00220     for (int i = 0; buttons; i++, buttons >>= 1) {
00221       if (pos <= -5) break; 
00222 
00223       if ((buttons & 1) && --pos < 0) {
00224         DrawString(3, y, STR_2046_SMALL_ADVERTISING_CAMPAIGN + i, TC_ORANGE);
00225         y += 10;
00226       }
00227     }
00228 
00229     if (this->sel_index != -1) {
00230       SetDParam(1, (_price.build_industry >> 8) * _town_action_costs[this->sel_index]);
00231       SetDParam(0, STR_2046_SMALL_ADVERTISING_CAMPAIGN + this->sel_index);
00232       DrawStringMultiLine(2, 159, STR_204D_INITIATE_A_SMALL_LOCAL + this->sel_index, 313);
00233     }
00234   }
00235 
00236   virtual void OnDoubleClick(Point pt, int widget) { HandleClick(pt, widget, true); }
00237   virtual void OnClick(Point pt, int widget) { HandleClick(pt, widget, false); }
00238 
00239   void HandleClick(Point pt, int widget, bool double_click)
00240   {
00241     switch (widget) {
00242       case TWA_COMMAND_LIST: {
00243         int y = (pt.y - 0x6B) / 10;
00244 
00245         if (!IsInsideMM(y, 0, 5)) return;
00246 
00247         y = GetNthSetBit(GetMaskOfTownActions(NULL, _local_company, this->town), y + this->vscroll.pos - 1);
00248         if (y >= 0) {
00249           this->sel_index = y;
00250           this->SetDirty();
00251         }
00252         /* Fall through to clicking in case we are double-clicked */
00253         if (!double_click || y < 0) break;
00254       }
00255 
00256       case TWA_EXECUTE:
00257         DoCommandP(this->town->xy, this->window_number, this->sel_index, CMD_DO_TOWN_ACTION | CMD_MSG(STR_00B4_CAN_T_DO_THIS));
00258         break;
00259     }
00260   }
00261 
00262   virtual void OnHundredthTick()
00263   {
00264     this->SetDirty();
00265   }
00266 };
00267 
00268 static const WindowDesc _town_authority_desc = {
00269   WDP_AUTO, WDP_AUTO, 317, 222, 317, 222,
00270   WC_TOWN_AUTHORITY, WC_NONE,
00271   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
00272   _town_authority_widgets,
00273 };
00274 
00275 static void ShowTownAuthorityWindow(uint town)
00276 {
00277   AllocateWindowDescFront<TownAuthorityWindow>(&_town_authority_desc, town);
00278 }
00279 
00280 struct TownViewWindow : Window {
00281 private:
00282   Town *town;
00283 
00284   enum TownViewWidget {
00285     TVW_CAPTION = 1,
00286     TVW_STICKY,
00287     TVW_VIEWPORTPANEL,
00288     TVW_INFOPANEL = 5,
00289     TVW_CENTERVIEW,
00290     TVW_SHOWAUTORITY,
00291     TVW_CHANGENAME,
00292     TVW_EXPAND,
00293     TVW_DELETE,
00294   };
00295 
00296 public:
00297   TownViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
00298   {
00299     this->town = GetTown(this->window_number);
00300     bool ingame = _game_mode != GM_EDITOR;
00301 
00302     this->flags4 |= WF_DISABLE_VP_SCROLL;
00303     InitializeWindowViewport(this, 3, 17, 254, 86, this->town->xy, ZOOM_LVL_TOWN);
00304 
00305     if (this->town->larger_town) this->widget[TVW_CAPTION].data = STR_CITY;
00306     this->SetWidgetHiddenState(TVW_DELETE, ingame);  // hide delete button on game mode
00307     this->SetWidgetHiddenState(TVW_EXPAND, ingame);  // hide expand button on game mode
00308     this->SetWidgetHiddenState(TVW_SHOWAUTORITY, !ingame); // hide autority button on editor mode
00309 
00310     if (ingame) {
00311       /* resize caption bar */
00312       this->widget[TVW_CAPTION].right = this->widget[TVW_STICKY].left -1;
00313       /* move the rename from top on scenario to bottom in game */
00314       this->widget[TVW_CHANGENAME].top = this->widget[TVW_EXPAND].top;
00315       this->widget[TVW_CHANGENAME].bottom = this->widget[TVW_EXPAND].bottom;
00316       this->widget[TVW_CHANGENAME].right = this->widget[TVW_STICKY].right;
00317     }
00318 
00319     /* Space required for showing noise level information */
00320     if (_settings_game.economy.station_noise_level) {
00321       ResizeWindowForWidget(this, TVW_INFOPANEL, 0, 10);
00322     }
00323 
00324     this->FindWindowPlacementAndResize(desc);
00325   }
00326 
00327   virtual void OnPaint()
00328   {
00329     /* disable renaming town in network games if you are not the server */
00330     this->SetWidgetDisabledState(TVW_CHANGENAME, _networking && !_network_server);
00331 
00332     SetDParam(0, this->town->index);
00333     this->DrawWidgets();
00334 
00335     SetDParam(0, this->town->population);
00336     SetDParam(1, this->town->num_houses);
00337     DrawString(2, 107, STR_2006_POPULATION, TC_FROMSTRING);
00338 
00339     SetDParam(0, this->town->act_pass);
00340     SetDParam(1, this->town->max_pass);
00341     DrawString(2, 117, STR_200D_PASSENGERS_LAST_MONTH_MAX, TC_FROMSTRING);
00342 
00343     SetDParam(0, this->town->act_mail);
00344     SetDParam(1, this->town->max_mail);
00345     DrawString(2, 127, STR_200E_MAIL_LAST_MONTH_MAX, TC_FROMSTRING);
00346 
00347     this->DrawViewport();
00348 
00349     /* only show the town noise, if the noise option is activated. */
00350     if (_settings_game.economy.station_noise_level) {
00351       SetDParam(0, this->town->noise_reached);
00352       SetDParam(1, this->town->MaxTownNoise());
00353       DrawString(2, 137, STR_NOISE_IN_TOWN, 0);
00354     }
00355   }
00356 
00357   virtual void OnClick(Point pt, int widget)
00358   {
00359     switch (widget) {
00360       case TVW_CENTERVIEW: /* scroll to location */
00361         if (_ctrl_pressed) {
00362           ShowExtraViewPortWindow(this->town->xy);
00363         } else {
00364           ScrollMainWindowToTile(this->town->xy);
00365         }
00366         break;
00367 
00368       case TVW_SHOWAUTORITY: /* town authority */
00369         ShowTownAuthorityWindow(this->window_number);
00370         break;
00371 
00372       case TVW_CHANGENAME: /* rename */
00373         SetDParam(0, this->window_number);
00374         ShowQueryString(STR_TOWN, STR_2007_RENAME_TOWN, MAX_LENGTH_TOWN_NAME_BYTES, MAX_LENGTH_TOWN_NAME_PIXELS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT);
00375         break;
00376 
00377       case TVW_EXPAND: /* expand town - only available on Scenario editor */
00378         ExpandTown(this->town);
00379         break;
00380 
00381       case TVW_DELETE: /* delete town - only available on Scenario editor */
00382         delete this->town;
00383         break;
00384     }
00385   }
00386 
00387   virtual void OnInvalidateData(int data = 0)
00388   {
00389     /* Called when setting station noise have changed, in order to resize the window */
00390     this->SetDirty(); // refresh display for current size. This will allow to avoid glitches when downgrading
00391 
00392     if (_settings_game.economy.station_noise_level) { // adjust depending
00393       if (this->height == 150) { // window is smaller, needs to be bigger
00394         ResizeWindowForWidget(this, TVW_INFOPANEL, 0, 10);
00395       }
00396     } else {
00397       if (this->height != 150) { // window is bigger, needs to be smaller
00398         ResizeWindowForWidget(this, TVW_INFOPANEL, 0, -10);
00399       }
00400     }
00401   }
00402 
00403   virtual void OnQueryTextFinished(char *str)
00404   {
00405     if (str == NULL) return;
00406 
00407     DoCommandP(0, this->window_number, 0, CMD_RENAME_TOWN | CMD_MSG(STR_2008_CAN_T_RENAME_TOWN), NULL, str);
00408   }
00409 };
00410 
00411 
00412 static const Widget _town_view_widgets[] = {
00413 {   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_BROWN,     0,    10,     0,    13, STR_00C5,                 STR_018B_CLOSE_WINDOW},
00414 {    WWT_CAPTION,   RESIZE_NONE,  COLOUR_BROWN,    11,   172,     0,    13, STR_2005,                 STR_018C_WINDOW_TITLE_DRAG_THIS},
00415 {  WWT_STICKYBOX,   RESIZE_NONE,  COLOUR_BROWN,   248,   259,     0,    13, 0x0,                      STR_STICKY_BUTTON},
00416 {      WWT_PANEL,   RESIZE_NONE,  COLOUR_BROWN,     0,   259,    14,   105, 0x0,                      STR_NULL},
00417 {      WWT_INSET,   RESIZE_NONE,  COLOUR_BROWN,     2,   257,    16,   103, 0x0,                      STR_NULL},
00418 {      WWT_PANEL,   RESIZE_NONE,  COLOUR_BROWN,     0,   259,   106,   137, 0x0,                      STR_NULL},
00419 { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_BROWN,     0,    85,   138,   149, STR_00E4_LOCATION,        STR_200B_CENTER_THE_MAIN_VIEW_ON},
00420 { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_BROWN,    86,   171,   138,   149, STR_2020_LOCAL_AUTHORITY, STR_2021_SHOW_INFORMATION_ON_LOCAL},
00421 { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_BROWN,   172,   247,     0,    13, STR_0130_RENAME,          STR_200C_CHANGE_TOWN_NAME},
00422 { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_BROWN,    86,   171,   138,   149, STR_023C_EXPAND,          STR_023B_INCREASE_SIZE_OF_TOWN},
00423 { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_BROWN,   172,   259,   138,   149, STR_0290_DELETE,          STR_0291_DELETE_THIS_TOWN_COMPLETELY},
00424 {   WIDGETS_END},
00425 };
00426 
00427 static const WindowDesc _town_view_desc = {
00428   WDP_AUTO, WDP_AUTO, 260, 150, 260, 150,
00429   WC_TOWN_VIEW, WC_NONE,
00430   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON,
00431   _town_view_widgets,
00432 };
00433 
00434 void ShowTownViewWindow(TownID town)
00435 {
00436   AllocateWindowDescFront<TownViewWindow>(&_town_view_desc, town);
00437 }
00438 
00439 static const Widget _town_directory_widgets[] = {
00440 {   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_BROWN,     0,    10,     0,    13, STR_00C5,               STR_018B_CLOSE_WINDOW},
00441 {    WWT_CAPTION,   RESIZE_NONE,  COLOUR_BROWN,    11,   195,     0,    13, STR_2000_TOWNS,         STR_018C_WINDOW_TITLE_DRAG_THIS},
00442 {  WWT_STICKYBOX,   RESIZE_NONE,  COLOUR_BROWN,   196,   207,     0,    13, 0x0,                    STR_STICKY_BUTTON},
00443 { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_BROWN,     0,    98,    14,    25, STR_SORT_BY_NAME,       STR_SORT_ORDER_TIP},
00444 { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_BROWN,    99,   195,    14,    25, STR_SORT_BY_POPULATION, STR_SORT_ORDER_TIP},
00445 {      WWT_PANEL, RESIZE_BOTTOM,  COLOUR_BROWN,     0,   195,    26,   189, 0x0,                    STR_200A_TOWN_NAMES_CLICK_ON_NAME},
00446 {  WWT_SCROLLBAR, RESIZE_BOTTOM,  COLOUR_BROWN,   196,   207,    14,   189, 0x0,                    STR_0190_SCROLL_BAR_SCROLLS_LIST},
00447 {      WWT_PANEL,     RESIZE_TB,  COLOUR_BROWN,     0,   195,   190,   201, 0x0,                    STR_NULL},
00448 {  WWT_RESIZEBOX,     RESIZE_TB,  COLOUR_BROWN,   196,   207,   190,   201, 0x0,                    STR_RESIZE_BUTTON},
00449 {   WIDGETS_END},
00450 };
00451 
00452 
00453 struct TownDirectoryWindow : public Window {
00454 private:
00455   enum TownDirectoryWidget {
00456     TDW_SORTNAME = 3,
00457     TDW_SORTPOPULATION,
00458     TDW_CENTERTOWN,
00459   };
00460 
00461   /* Runtime saved values */
00462   static Listing last_sorting;
00463   static const Town *last_town;
00464 
00465   /* Constants for sorting towns */
00466   static GUITownList::SortFunction * const sorter_funcs[];
00467 
00468   GUITownList towns;
00469 
00470   void BuildTownList()
00471   {
00472     if (!this->towns.NeedRebuild()) return;
00473 
00474     this->towns.Clear();
00475 
00476     const Town *t;
00477     FOR_ALL_TOWNS(t) {
00478       *this->towns.Append() = t;
00479     }
00480 
00481     this->towns.Compact();
00482     this->towns.RebuildDone();
00483   }
00484 
00485   void SortTownList()
00486   {
00487     last_town = NULL;
00488     this->towns.Sort();
00489   }
00490 
00492   static int CDECL TownNameSorter(const Town * const *a, const Town * const *b)
00493   {
00494     static char buf_cache[64];
00495     const Town *ta = *a;
00496     const Town *tb = *b;
00497     char buf[64];
00498 
00499     SetDParam(0, ta->index);
00500     GetString(buf, STR_TOWN, lastof(buf));
00501 
00502     /* If 'b' is the same town as in the last round, use the cached value
00503      * We do this to speed stuff up ('b' is called with the same value a lot of
00504      * times after eachother) */
00505     if (tb != last_town) {
00506       last_town = tb;
00507       SetDParam(0, tb->index);
00508       GetString(buf_cache, STR_TOWN, lastof(buf_cache));
00509     }
00510 
00511     return strcmp(buf, buf_cache);
00512   }
00513 
00515   static int CDECL TownPopulationSorter(const Town * const *a, const Town * const *b)
00516   {
00517     return (*a)->population - (*b)->population;
00518   }
00519 
00520 public:
00521   TownDirectoryWindow(const WindowDesc *desc) : Window(desc, 0)
00522   {
00523     this->vscroll.cap = 16;
00524     this->resize.step_height = 10;
00525     this->resize.height = this->height - 10 * 6; // minimum of 10 items in the list, each item 10 high
00526 
00527     this->towns.SetListing(this->last_sorting);
00528     this->towns.SetSortFuncs(this->sorter_funcs);
00529     this->towns.ForceRebuild();
00530 
00531     this->FindWindowPlacementAndResize(desc);
00532   }
00533 
00534   ~TownDirectoryWindow()
00535   {
00536     this->last_sorting = this->towns.GetListing();
00537   }
00538 
00539   virtual void OnPaint()
00540   {
00541     this->BuildTownList();
00542     this->SortTownList();
00543 
00544     SetVScrollCount(this, this->towns.Length());
00545 
00546     this->DrawWidgets();
00547     this->DrawSortButtonState(this->towns.SortType() == 0 ? TDW_SORTNAME : TDW_SORTPOPULATION, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
00548 
00549     {
00550       int n = 0;
00551       uint16 i = this->vscroll.pos;
00552       int y = 28;
00553 
00554       while (i < this->towns.Length()) {
00555         const Town *t = this->towns[i];
00556 
00557         assert(t->xy != INVALID_TILE);
00558 
00559         SetDParam(0, t->index);
00560         SetDParam(1, t->population);
00561         DrawString(2, y, STR_2057, TC_FROMSTRING);
00562 
00563         y += 10;
00564         i++;
00565         if (++n == this->vscroll.cap) break; // max number of towns in 1 window
00566       }
00567 
00568       SetDParam(0, GetWorldPopulation());
00569       DrawString(3, this->height - 12 + 2, STR_TOWN_POPULATION, TC_FROMSTRING);
00570     }
00571   }
00572 
00573   virtual void OnClick(Point pt, int widget)
00574   {
00575     switch (widget) {
00576       case TDW_SORTNAME: /* Sort by Name ascending/descending */
00577         if (this->towns.SortType() == 0) {
00578           this->towns.ToggleSortOrder();
00579         } else {
00580           this->towns.SetSortType(0);
00581         }
00582         this->SetDirty();
00583         break;
00584 
00585       case TDW_SORTPOPULATION: /* Sort by Population ascending/descending */
00586         if (this->towns.SortType() == 1) {
00587           this->towns.ToggleSortOrder();
00588         } else {
00589           this->towns.SetSortType(1);
00590         }
00591         this->SetDirty();
00592         break;
00593 
00594       case TDW_CENTERTOWN: { /* Click on Town Matrix */
00595         uint16 id_v = (pt.y - 28) / 10;
00596 
00597         if (id_v >= this->vscroll.cap) return; // click out of bounds
00598 
00599         id_v += this->vscroll.pos;
00600 
00601         if (id_v >= this->towns.Length()) return; // click out of town bounds
00602 
00603         const Town *t = this->towns[id_v];
00604         assert(t->xy != INVALID_TILE);
00605         if (_ctrl_pressed) {
00606           ShowExtraViewPortWindow(t->xy);
00607         } else {
00608           ScrollMainWindowToTile(t->xy);
00609         }
00610         break;
00611       }
00612     }
00613   }
00614 
00615   virtual void OnHundredthTick()
00616   {
00617     this->SetDirty();
00618   }
00619 
00620   virtual void OnResize(Point new_size, Point delta)
00621   {
00622     this->vscroll.cap += delta.y / 10;
00623   }
00624 
00625   virtual void OnInvalidateData(int data)
00626   {
00627     if (data == 0) {
00628       this->towns.ForceRebuild();
00629     } else {
00630       this->towns.ForceResort();
00631     }
00632   }
00633 };
00634 
00635 Listing TownDirectoryWindow::last_sorting = {false, 0};
00636 const Town *TownDirectoryWindow::last_town = NULL;
00637 
00638 /* Available town directory sorting functions */
00639 GUITownList::SortFunction * const TownDirectoryWindow::sorter_funcs[] = {
00640   &TownNameSorter,
00641   &TownPopulationSorter,
00642 };
00643 
00644 static const WindowDesc _town_directory_desc = {
00645   WDP_AUTO, WDP_AUTO, 208, 202, 208, 202,
00646   WC_TOWN_DIRECTORY, WC_NONE,
00647   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
00648   _town_directory_widgets,
00649 };
00650 
00651 void ShowTownDirectory()
00652 {
00653   if (BringWindowToFrontById(WC_TOWN_DIRECTORY, 0)) return;
00654   new TownDirectoryWindow(&_town_directory_desc);
00655 }
00656 
00657 void CcBuildTown(bool success, TileIndex tile, uint32 p1, uint32 p2)
00658 {
00659   if (success) {
00660     SndPlayTileFx(SND_1F_SPLAT, tile);
00661     if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
00662   }
00663 }
00664 
00665 static void PlaceProc_Town(TileIndex tile)
00666 {
00667   uint32 size = min(_scengen_town_size, (int)TSM_CITY);
00668   uint32 mode = _scengen_town_size > TSM_CITY ? TSM_CITY : TSM_FIXED;
00669   DoCommandP(tile, size, mode, CMD_BUILD_TOWN | CMD_MSG(STR_0236_CAN_T_BUILD_TOWN_HERE), CcBuildTown);
00670 }
00671 
00672 static const Widget _scen_edit_town_gen_widgets[] = {
00673 {   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_DARK_GREEN,    0,    10,     0,    13, STR_00C5,                 STR_018B_CLOSE_WINDOW},
00674 {    WWT_CAPTION,   RESIZE_NONE,  COLOUR_DARK_GREEN,   11,   147,     0,    13, STR_0233_TOWN_GENERATION, STR_018C_WINDOW_TITLE_DRAG_THIS},
00675 {  WWT_STICKYBOX,   RESIZE_NONE,  COLOUR_DARK_GREEN,  148,   159,     0,    13, 0x0,                      STR_STICKY_BUTTON},
00676 {      WWT_PANEL,   RESIZE_NONE,  COLOUR_DARK_GREEN,    0,   159,    14,    94, 0x0,                      STR_NULL},
00677 {    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,          2,   157,    16,    27, STR_0234_NEW_TOWN,        STR_0235_CONSTRUCT_NEW_TOWN},
00678 {    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,          2,   157,    29,    40, STR_023D_RANDOM_TOWN,     STR_023E_BUILD_TOWN_IN_RANDOM_LOCATION},
00679 {    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,          2,   157,    42,    53, STR_MANY_RANDOM_TOWNS,    STR_RANDOM_TOWNS_TIP},
00680 {    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,          2,    53,    68,    79, STR_02A1_SMALL,           STR_02A4_SELECT_TOWN_SIZE},
00681 {    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,         54,   105,    68,    79, STR_02A2_MEDIUM,          STR_02A4_SELECT_TOWN_SIZE},
00682 {    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,        106,   157,    68,    79, STR_02A3_LARGE,           STR_02A4_SELECT_TOWN_SIZE},
00683 {    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,          2,   157,    81,    92, STR_SCENARIO_EDITOR_CITY, STR_02A4_SELECT_TOWN_SIZE},
00684 {      WWT_LABEL,   RESIZE_NONE,  COLOUR_DARK_GREEN,    0,   147,    54,    67, STR_02A5_TOWN_SIZE,       STR_NULL},
00685 {   WIDGETS_END},
00686 };
00687 
00688 struct ScenarioEditorTownGenerationWindow : Window
00689 {
00690 private:
00691   enum TownScenarioEditorWidget {
00692     TSEW_NEWTOWN = 4,
00693     TSEW_RANDOMTOWN,
00694     TSEW_MANYRANDOMTOWNS,
00695     TSEW_SMALLTOWN,
00696     TSEW_MEDIUMTOWN,
00697     TSEW_LARGETOWN,
00698     TSEW_CITY,
00699   };
00700 
00701 public:
00702   ScenarioEditorTownGenerationWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
00703   {
00704     this->LowerWidget(_scengen_town_size + TSEW_SMALLTOWN);
00705     this->FindWindowPlacementAndResize(desc);
00706   }
00707 
00708   virtual void OnPaint()
00709   {
00710     this->DrawWidgets();
00711   }
00712 
00713   virtual void OnClick(Point pt, int widget)
00714   {
00715     switch (widget) {
00716       case TSEW_NEWTOWN:
00717         HandlePlacePushButton(this, TSEW_NEWTOWN, SPR_CURSOR_TOWN, VHM_RECT, PlaceProc_Town);
00718         break;
00719 
00720       case TSEW_RANDOMTOWN: {
00721         Town *t;
00722         uint size = min(_scengen_town_size, (int)TSM_CITY);
00723         TownSizeMode mode = _scengen_town_size > TSM_CITY ? TSM_CITY : TSM_FIXED;
00724 
00725         this->HandleButtonClick(TSEW_RANDOMTOWN);
00726         _generating_world = true;
00727         UpdateNearestTownForRoadTiles(true);
00728         t = CreateRandomTown(20, mode, size);
00729         UpdateNearestTownForRoadTiles(false);
00730         _generating_world = false;
00731 
00732         if (t == NULL) {
00733           ShowErrorMessage(STR_NO_SPACE_FOR_TOWN, STR_CANNOT_GENERATE_TOWN, 0, 0);
00734         } else {
00735           ScrollMainWindowToTile(t->xy);
00736         }
00737       } break;
00738 
00739       case TSEW_MANYRANDOMTOWNS:
00740         this->HandleButtonClick(TSEW_MANYRANDOMTOWNS);
00741 
00742         _generating_world = true;
00743         UpdateNearestTownForRoadTiles(true);
00744         if (!GenerateTowns()) {
00745           ShowErrorMessage(STR_NO_SPACE_FOR_TOWN, STR_CANNOT_GENERATE_TOWN, 0, 0);
00746         }
00747         UpdateNearestTownForRoadTiles(false);
00748         _generating_world = false;
00749         break;
00750 
00751       case TSEW_SMALLTOWN: case TSEW_MEDIUMTOWN: case TSEW_LARGETOWN: case TSEW_CITY:
00752         this->RaiseWidget(_scengen_town_size + TSEW_SMALLTOWN);
00753         _scengen_town_size = widget - TSEW_SMALLTOWN;
00754         this->LowerWidget(_scengen_town_size + TSEW_SMALLTOWN);
00755         this->SetDirty();
00756         break;
00757     }
00758   }
00759 
00760   virtual void OnTimeout()
00761   {
00762     this->RaiseWidget(TSEW_RANDOMTOWN);
00763     this->RaiseWidget(TSEW_MANYRANDOMTOWNS);
00764     this->SetDirty();
00765   }
00766 
00767   virtual void OnPlaceObject(Point pt, TileIndex tile)
00768   {
00769     _place_proc(tile);
00770   }
00771 
00772   virtual void OnPlaceObjectAbort()
00773   {
00774     this->RaiseButtons();
00775     this->LowerWidget(_scengen_town_size + TSEW_SMALLTOWN);
00776     this->SetDirty();
00777   }
00778 };
00779 
00780 static const WindowDesc _scen_edit_town_gen_desc = {
00781   WDP_AUTO, WDP_AUTO, 160, 95, 160, 95,
00782   WC_SCEN_TOWN_GEN, WC_NONE,
00783   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON,
00784   _scen_edit_town_gen_widgets,
00785 };
00786 
00787 void ShowBuildTownWindow()
00788 {
00789   if (_game_mode != GM_EDITOR && !IsValidCompanyID(_current_company)) return;
00790   AllocateWindowDescFront<ScenarioEditorTownGenerationWindow>(&_scen_edit_town_gen_desc, 0);
00791 }

Generated on Fri Jan 9 19:01:52 2009 for openttd by  doxygen 1.5.6