tron@2186: /* $Id$ */ tron@2186: rubidium@10429: /** @file town_gui.cpp GUI for towns. */ belugas@6918: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" truelight@0: #include "town.h" rubidium@8720: #include "viewport_func.h" rubidium@8720: #include "gfx_func.h" truelight@0: #include "gui.h" rubidium@8603: #include "window_gui.h" rubidium@8603: #include "textbuf_gui.h" rubidium@8612: #include "command_func.h" rubidium@8750: #include "player_func.h" rubidium@8750: #include "player_base.h" rubidium@8750: #include "player_gui.h" rubidium@5720: #include "network/network.h" tron@2159: #include "variables.h" rubidium@8610: #include "strings_func.h" rubidium@9219: #include "sound_func.h" rubidium@8612: #include "economy_func.h" rubidium@8626: #include "core/alloc_func.hpp" rubidium@8766: #include "settings_type.h" rubidium@10445: #include "tilehighlight_func.h" rubidium@10484: #include "string_func.h" peter1138@10747: #include "sortlist_type.h" truelight@0: rubidium@8760: #include "table/sprites.h" rubidium@8760: #include "table/strings.h" rubidium@8760: peter1138@10747: typedef GUIList GUITownList; peter1138@10747: rubidium@9219: extern bool GenerateTowns(); rubidium@9219: static int _scengen_town_size = 1; // depress medium-sized towns per default rubidium@9219: truelight@0: static const Widget _town_authority_widgets[] = { rubidium@8153: { WWT_CLOSEBOX, RESIZE_NONE, 13, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, // TWA_CLOSEBOX rubidium@8153: { WWT_CAPTION, RESIZE_NONE, 13, 11, 316, 0, 13, STR_2022_LOCAL_AUTHORITY, STR_018C_WINDOW_TITLE_DRAG_THIS}, // TWA_CAPTION rubidium@8153: { WWT_PANEL, RESIZE_NONE, 13, 0, 316, 14, 105, 0x0, STR_NULL}, // TWA_RATING_INFO rubidium@8153: { WWT_PANEL, RESIZE_NONE, 13, 0, 304, 106, 157, 0x0, STR_2043_LIST_OF_THINGS_TO_DO_AT}, // TWA_COMMAND_LIST rubidium@8153: { WWT_SCROLLBAR, RESIZE_NONE, 13, 305, 316, 106, 157, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, // TWA_SCROLLBAR rubidium@8153: { WWT_PANEL, RESIZE_NONE, 13, 0, 316, 158, 209, 0x0, STR_NULL}, // TWA_ACTION_INFO rubidium@8153: { WWT_PUSHTXTBTN, RESIZE_NONE, 13, 0, 316, 210, 221, STR_2042_DO_IT, STR_2044_CARRY_OUT_THE_HIGHLIGHTED}, // TWA_EXECUTE darkvater@176: { WIDGETS_END}, truelight@0: }; truelight@0: truelight@0: extern const byte _town_action_costs[8]; truelight@0: rubidium@8153: enum TownActions { rubidium@8153: TACT_NONE = 0x00, rubidium@8153: rubidium@8153: TACT_ADVERTISE_SMALL = 0x01, rubidium@8153: TACT_ADVERTISE_MEDIUM = 0x02, rubidium@8153: TACT_ADVERTISE_LARGE = 0x04, rubidium@8153: TACT_ROAD_REBUILD = 0x08, rubidium@8153: TACT_BUILD_STATUE = 0x10, rubidium@8153: TACT_FOUND_BUILDINGS = 0x20, rubidium@8153: TACT_BUY_RIGHTS = 0x40, rubidium@8153: TACT_BRIBE = 0x80, rubidium@8153: rubidium@8153: TACT_ADVERTISE = TACT_ADVERTISE_SMALL | TACT_ADVERTISE_MEDIUM | TACT_ADVERTISE_LARGE, rubidium@8153: TACT_CONSTRUCTION = TACT_ROAD_REBUILD | TACT_BUILD_STATUE | TACT_FOUND_BUILDINGS, rubidium@8153: TACT_FUNDS = TACT_BUY_RIGHTS | TACT_BRIBE, rubidium@8153: TACT_ALL = TACT_ADVERTISE | TACT_CONSTRUCTION | TACT_FUNDS, rubidium@8153: }; rubidium@8153: rubidium@8153: DECLARE_ENUM_AS_BIT_SET(TownActions); rubidium@8153: Darkvater@1793: /** Get a list of available actions to do at a town. belugas@6918: * @param nump if not NULL add put the number of available actions in it Darkvater@1793: * @param pid the player that is querying the town belugas@6918: * @param t the town that is queried Darkvater@1793: * @return bitmasked value of enabled actions Darkvater@1793: */ Darkvater@1793: uint GetMaskOfTownActions(int *nump, PlayerID pid, const Town *t) truelight@0: { Darkvater@1793: int num = 0; rubidium@8153: TownActions buttons = TACT_NONE; tron@2639: rubidium@8153: /* Spectators and unwanted have no options */ rubidium@10775: if (pid != PLAYER_SPECTATOR && !(_settings_game.economy.bribe && t->unwanted[pid])) { truelight@0: belugas@6918: /* Things worth more than this are not shown */ rubidium@8153: Money avail = GetPlayer(pid)->player_money + _price.station_value * 200; rubidium@8153: Money ref = _price.build_industry >> 8; truelight@0: rubidium@8153: /* Check the action bits for validity and rubidium@8153: * if they are valid add them */ rubidium@8153: for (uint i = 0; i != lengthof(_town_action_costs); i++) { rubidium@8153: const TownActions cur = (TownActions)(1 << i); rubidium@8153: rubidium@8153: /* Is the player not able to bribe ? */ rubidium@10775: if (cur == TACT_BRIBE && (!_settings_game.economy.bribe || t->ratings[pid] >= RATING_BRIBE_MAXIMUM)) rubidium@8153: continue; rubidium@8153: rubidium@8153: /* Is the player not able to buy exclusive rights ? */ rubidium@10775: if (cur == TACT_BUY_RIGHTS && !_settings_game.economy.exclusive_rights) rubidium@8153: continue; rubidium@8153: rubidium@8153: /* Is the player not able to build a statue ? */ skidd13@8424: if (cur == TACT_BUILD_STATUE && HasBit(t->statues, pid)) rubidium@8153: continue; rubidium@8153: rubidium@8153: if (avail >= _town_action_costs[i] * ref) { rubidium@8153: buttons |= cur; truelight@0: num++; truelight@0: } truelight@0: } truelight@0: } truelight@0: Darkvater@1793: if (nump != NULL) *nump = num; truelight@0: return buttons; truelight@0: } truelight@0: rubidium@10569: struct TownAuthorityWindow : Window { belugas@10665: private: belugas@10666: Town *town; rubidium@10569: int sel_index; belugas@10173: belugas@10665: enum TownAuthorityWidget { belugas@10665: TWA_CLOSEBOX = 0, belugas@10665: TWA_CAPTION, belugas@10665: TWA_RATING_INFO, belugas@10665: TWA_COMMAND_LIST, belugas@10665: TWA_SCROLLBAR, belugas@10665: TWA_ACTION_INFO, belugas@10665: TWA_EXECUTE, belugas@10665: }; belugas@10665: belugas@11125: /** belugas@11125: * Get the position of the Nth set bit. belugas@11125: * belugas@11125: * If there is no Nth bit set return -1 belugas@11125: * belugas@11125: * @param bits The value to search in belugas@11125: * @param n The Nth set bit from which we want to know the position belugas@11125: * @return The position of the Nth set bit belugas@11125: */ belugas@11125: static int GetNthSetBit(uint32 bits, int n) belugas@11125: { belugas@11125: if (n >= 0) { belugas@11125: uint i; belugas@11125: FOR_EACH_SET_BIT(i, bits) { belugas@11125: n--; belugas@11125: if (n < 0) return i; belugas@11125: } belugas@11125: } belugas@11125: return -1; belugas@11125: } belugas@11125: belugas@10665: public: rubidium@10569: TownAuthorityWindow(const WindowDesc *desc, WindowNumber window_number) : rubidium@10569: Window(desc, window_number), sel_index(-1) rubidium@10569: { belugas@10666: this->town = GetTown(this->window_number); rubidium@10569: this->vscroll.cap = 5; belugas@10173: rubidium@10569: this->FindWindowPlacementAndResize(desc); rubidium@10569: } belugas@10173: rubidium@10569: virtual void OnPaint() rubidium@10569: { rubidium@10569: int numact; belugas@10666: uint buttons = GetMaskOfTownActions(&numact, _local_player, this->town); belugas@10173: rubidium@10569: SetVScrollCount(this, numact + 1); rubidium@10569: rubidium@10569: if (this->sel_index != -1 && !HasBit(buttons, this->sel_index)) { rubidium@10569: this->sel_index = -1; rubidium@10569: } rubidium@10569: rubidium@10569: this->SetWidgetDisabledState(6, this->sel_index == -1); rubidium@10569: rubidium@10569: SetDParam(0, this->window_number); rubidium@10595: this->DrawWidgets(); rubidium@10569: rubidium@10569: DrawString(2, 15, STR_2023_TRANSPORT_COMPANY_RATINGS, TC_FROMSTRING); rubidium@10569: rubidium@10569: /* Draw list of players */ rubidium@10569: int y = 25; rubidium@10569: rubidium@10569: const Player *p; rubidium@10569: FOR_ALL_PLAYERS(p) { belugas@10666: if (p->is_active && (HasBit(this->town->have_ratings, p->index) || this->town->exclusivity == p->index)) { rubidium@10569: DrawPlayerIcon(p->index, 2, y); rubidium@10569: rubidium@10569: SetDParam(0, p->index); rubidium@10569: SetDParam(1, p->index); rubidium@10569: belugas@10666: int r = this->town->ratings[p->index]; rubidium@10569: StringID str; rubidium@10569: (str = STR_3035_APPALLING, r <= RATING_APPALLING) || // Apalling rubidium@10569: (str++, r <= RATING_VERYPOOR) || // Very Poor rubidium@10569: (str++, r <= RATING_POOR) || // Poor rubidium@10569: (str++, r <= RATING_MEDIOCRE) || // Mediocore rubidium@10569: (str++, r <= RATING_GOOD) || // Good rubidium@10569: (str++, r <= RATING_VERYGOOD) || // Very Good rubidium@10569: (str++, r <= RATING_EXCELLENT) || // Excellent rubidium@10569: (str++, true); // Outstanding rubidium@10569: rubidium@10569: SetDParam(2, str); belugas@10666: if (this->town->exclusivity == p->index) { // red icon for player with exclusive rights rubidium@10569: DrawSprite(SPR_BLOT, PALETTE_TO_RED, 18, y); truelight@0: } rubidium@10569: rubidium@10569: DrawString(28, y, STR_2024, TC_FROMSTRING); rubidium@10569: y += 10; rubidium@10569: } rubidium@10569: } rubidium@10569: y = 107; rubidium@10569: int pos = this->vscroll.pos; rubidium@10569: rubidium@10569: if (--pos < 0) { rubidium@10569: DrawString(2, y, STR_2045_ACTIONS_AVAILABLE, TC_FROMSTRING); rubidium@10569: y += 10; rubidium@10569: } rubidium@10569: rubidium@10569: for (int i = 0; buttons; i++, buttons >>= 1) { rubidium@10569: if (pos <= -5) break; ///< Draw only the 5 fitting lines rubidium@10569: rubidium@10569: if ((buttons & 1) && --pos < 0) { rubidium@10569: DrawString(3, y, STR_2046_SMALL_ADVERTISING_CAMPAIGN + i, TC_ORANGE); rubidium@10569: y += 10; rubidium@10569: } rubidium@10569: } rubidium@10569: rubidium@10569: if (this->sel_index != -1) { rubidium@10569: SetDParam(1, (_price.build_industry >> 8) * _town_action_costs[this->sel_index]); rubidium@10569: SetDParam(0, STR_2046_SMALL_ADVERTISING_CAMPAIGN + this->sel_index); rubidium@10569: DrawStringMultiLine(2, 159, STR_204D_INITIATE_A_SMALL_LOCAL + this->sel_index, 313); rubidium@10569: } rubidium@10569: } rubidium@10569: rubidium@10569: virtual void OnDoubleClick(Point pt, int widget) { HandleClick(pt, widget, true); } rubidium@10569: virtual void OnClick(Point pt, int widget) { HandleClick(pt, widget, false); } rubidium@10569: rubidium@10569: void HandleClick(Point pt, int widget, bool double_click) rubidium@10569: { rubidium@10569: switch (widget) { rubidium@10569: case TWA_COMMAND_LIST: { rubidium@10569: int y = (pt.y - 0x6B) / 10; rubidium@10569: rubidium@10569: if (!IsInsideMM(y, 0, 5)) return; rubidium@10569: belugas@10666: y = GetNthSetBit(GetMaskOfTownActions(NULL, _local_player, this->town), y + this->vscroll.pos - 1); rubidium@10569: if (y >= 0) { rubidium@10569: this->sel_index = y; rubidium@10569: this->SetDirty(); rubidium@10569: } rubidium@10569: /* Fall through to clicking in case we are double-clicked */ rubidium@10569: if (!double_click || y < 0) break; truelight@0: } truelight@0: rubidium@10569: case TWA_EXECUTE: belugas@10666: DoCommandP(this->town->xy, this->window_number, this->sel_index, NULL, CMD_DO_TOWN_ACTION | CMD_MSG(STR_00B4_CAN_T_DO_THIS)); rubidium@10569: break; rubidium@10569: } rubidium@10569: } truelight@0: rubidium@10569: virtual void OnHundredthTick() rubidium@10569: { rubidium@10569: this->SetDirty(); truelight@0: } rubidium@10569: }; truelight@0: truelight@0: static const WindowDesc _town_authority_desc = { rubidium@7837: WDP_AUTO, WDP_AUTO, 317, 222, 317, 222, rubidium@6144: WC_TOWN_AUTHORITY, WC_NONE, truelight@0: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS, truelight@0: _town_authority_widgets, truelight@0: }; truelight@0: tron@410: static void ShowTownAuthorityWindow(uint town) truelight@0: { rubidium@10569: AllocateWindowDescFront(&_town_authority_desc, town); truelight@0: } truelight@0: belugas@10665: struct TownViewWindow : Window { belugas@10665: private: belugas@10666: Town *town; belugas@10174: belugas@10665: enum TownViewWidget { belugas@10665: TVW_CAPTION = 1, belugas@10665: TVW_STICKY, belugas@10682: TVW_VIEWPORTPANEL, belugas@10682: TVW_INFOPANEL = 5, belugas@10682: TVW_CENTERVIEW, belugas@10665: TVW_SHOWAUTORITY, belugas@10665: TVW_CHANGENAME, belugas@10665: TVW_EXPAND, belugas@10665: TVW_DELETE, belugas@10665: }; belugas@10174: belugas@10665: public: rubidium@10609: TownViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number) rubidium@10609: { belugas@10666: this->town = GetTown(this->window_number); belugas@10666: bool ingame = _game_mode != GM_EDITOR; belugas@10173: rubidium@10609: this->flags4 |= WF_DISABLE_VP_SCROLL; belugas@10666: InitializeWindowViewport(this, 3, 17, 254, 86, this->town->xy, ZOOM_LVL_TOWN); belugas@10173: belugas@10666: if (this->town->larger_town) this->widget[TVW_CAPTION].data = STR_CITY; rubidium@10609: this->SetWidgetHiddenState(TVW_DELETE, ingame); // hide delete button on game mode rubidium@10609: this->SetWidgetHiddenState(TVW_EXPAND, ingame); // hide expand button on game mode rubidium@10609: this->SetWidgetHiddenState(TVW_SHOWAUTORITY, !ingame); // hide autority button on editor mode belugas@10173: rubidium@10609: if (ingame) { rubidium@10609: /* resize caption bar */ rubidium@10609: this->widget[TVW_CAPTION].right = this->widget[TVW_STICKY].left -1; rubidium@10609: /* move the rename from top on scenario to bottom in game */ rubidium@10609: this->widget[TVW_CHANGENAME].top = this->widget[TVW_EXPAND].top; rubidium@10609: this->widget[TVW_CHANGENAME].bottom = this->widget[TVW_EXPAND].bottom; rubidium@10609: this->widget[TVW_CHANGENAME].right = this->widget[TVW_STICKY].right; rubidium@10609: } smatz@10632: belugas@10682: /* Space required for showing noise level information */ rubidium@10775: if (_settings_game.economy.station_noise_level) { belugas@10682: ResizeWindowForWidget(this, TVW_INFOPANEL, 0, 10); belugas@10682: } belugas@10682: smatz@10632: this->FindWindowPlacementAndResize(desc); rubidium@10609: } belugas@10173: rubidium@10609: virtual void OnPaint() rubidium@10609: { rubidium@10609: /* disable renaming town in network games if you are not the server */ rubidium@10609: this->SetWidgetDisabledState(TVW_CHANGENAME, _networking && !_network_server); rubidium@10609: belugas@10666: SetDParam(0, this->town->index); rubidium@10609: this->DrawWidgets(); rubidium@10609: belugas@10666: SetDParam(0, this->town->population); belugas@10666: SetDParam(1, this->town->num_houses); rubidium@10609: DrawString(2, 107, STR_2006_POPULATION, TC_FROMSTRING); rubidium@10609: belugas@10666: SetDParam(0, this->town->act_pass); belugas@10666: SetDParam(1, this->town->max_pass); rubidium@10609: DrawString(2, 117, STR_200D_PASSENGERS_LAST_MONTH_MAX, TC_FROMSTRING); rubidium@10609: belugas@10666: SetDParam(0, this->town->act_mail); belugas@10666: SetDParam(1, this->town->max_mail); rubidium@10609: DrawString(2, 127, STR_200E_MAIL_LAST_MONTH_MAX, TC_FROMSTRING); rubidium@10609: rubidium@10609: this->DrawViewport(); belugas@10682: belugas@10682: /* only show the town noise, if the noise option is activated. */ rubidium@10775: if (_settings_game.economy.station_noise_level) { belugas@10682: SetDParam(0, this->town->noise_reached); belugas@10682: SetDParam(1, this->town->MaxTownNoise()); belugas@10682: DrawString(2, 137, STR_NOISE_IN_TOWN, 0); belugas@10682: } truelight@0: } rubidium@10609: rubidium@10609: virtual void OnClick(Point pt, int widget) rubidium@10609: { rubidium@10609: switch (widget) { rubidium@10609: case TVW_CENTERVIEW: /* scroll to location */ rubidium@10609: if (_ctrl_pressed) { belugas@10666: ShowExtraViewPortWindow(this->town->xy); rubidium@10609: } else { belugas@10666: ScrollMainWindowToTile(this->town->xy); rubidium@10609: } rubidium@10609: break; rubidium@10609: rubidium@10609: case TVW_SHOWAUTORITY: /* town authority */ rubidium@10609: ShowTownAuthorityWindow(this->window_number); rubidium@10609: break; rubidium@10609: rubidium@10609: case TVW_CHANGENAME: /* rename */ rubidium@10609: SetDParam(0, this->window_number); rubidium@10609: ShowQueryString(STR_TOWN, STR_2007_RENAME_TOWN, 31, 130, this, CS_ALPHANUMERAL); rubidium@10609: break; rubidium@10609: rubidium@10609: case TVW_EXPAND: /* expand town - only available on Scenario editor */ belugas@10666: ExpandTown(this->town); rubidium@10609: break; rubidium@10609: rubidium@10609: case TVW_DELETE: /* delete town - only available on Scenario editor */ belugas@10666: delete this->town; rubidium@10609: break; rubidium@10609: } rubidium@10609: } rubidium@10609: belugas@10682: virtual void OnInvalidateData(int data = 0) belugas@10682: { belugas@10682: /* Called when setting station noise have changed, in order to resize the window */ belugas@10682: this->SetDirty(); // refresh display for current size. This will allow to avoid glitches when downgrading belugas@10682: rubidium@10775: if (_settings_game.economy.station_noise_level) { // adjust depending belugas@10682: if (this->height == 150) { // window is smaller, needs to be bigger belugas@10682: ResizeWindowForWidget(this, TVW_INFOPANEL, 0, 10); belugas@10682: } belugas@10682: } else { belugas@10682: if (this->height != 150) { // window is bigger, needs to be smaller belugas@10682: ResizeWindowForWidget(this, TVW_INFOPANEL, 0, -10); belugas@10682: } belugas@10682: } belugas@10682: } belugas@10682: rubidium@10609: virtual void OnQueryTextFinished(char *str) rubidium@10609: { rubidium@10609: if (!StrEmpty(str)) { rubidium@10609: _cmd_text = str; rubidium@10609: DoCommandP(0, this->window_number, 0, NULL, rubidium@10609: CMD_RENAME_TOWN | CMD_MSG(STR_2008_CAN_T_RENAME_TOWN)); rubidium@10609: } rubidium@10609: } rubidium@10609: }; truelight@0: truelight@0: truelight@0: static const Widget _town_view_widgets[] = { rubidium@4344: { WWT_CLOSEBOX, RESIZE_NONE, 13, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, belugas@10201: { WWT_CAPTION, RESIZE_NONE, 13, 11, 172, 0, 13, STR_2005, STR_018C_WINDOW_TITLE_DRAG_THIS}, rubidium@4344: { WWT_STICKYBOX, RESIZE_NONE, 13, 248, 259, 0, 13, 0x0, STR_STICKY_BUTTON}, Darkvater@4938: { WWT_PANEL, RESIZE_NONE, 13, 0, 259, 14, 105, 0x0, STR_NULL}, Darkvater@4939: { WWT_INSET, RESIZE_NONE, 13, 2, 257, 16, 103, 0x0, STR_NULL}, Darkvater@4938: { WWT_PANEL, RESIZE_NONE, 13, 0, 259, 106, 137, 0x0, STR_NULL}, rubidium@4344: { WWT_PUSHTXTBTN, RESIZE_NONE, 13, 0, 85, 138, 149, STR_00E4_LOCATION, STR_200B_CENTER_THE_MAIN_VIEW_ON}, rubidium@4344: { WWT_PUSHTXTBTN, RESIZE_NONE, 13, 86, 171, 138, 149, STR_2020_LOCAL_AUTHORITY, STR_2021_SHOW_INFORMATION_ON_LOCAL}, belugas@10201: { WWT_PUSHTXTBTN, RESIZE_NONE, 13, 172, 247, 0, 13, STR_0130_RENAME, STR_200C_CHANGE_TOWN_NAME}, belugas@10201: { WWT_PUSHTXTBTN, RESIZE_NONE, 13, 86, 171, 138, 149, STR_023C_EXPAND, STR_023B_INCREASE_SIZE_OF_TOWN}, belugas@10201: { WWT_PUSHTXTBTN, RESIZE_NONE, 13, 172, 259, 138, 149, STR_0290_DELETE, STR_0291_DELETE_THIS_TOWN_COMPLETELY}, darkvater@176: { WIDGETS_END}, truelight@0: }; truelight@0: truelight@0: static const WindowDesc _town_view_desc = { rubidium@7837: WDP_AUTO, WDP_AUTO, 260, 150, 260, 150, rubidium@6144: WC_TOWN_VIEW, WC_NONE, darkvater@758: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON, truelight@0: _town_view_widgets, truelight@0: }; truelight@0: Darkvater@3349: void ShowTownViewWindow(TownID town) truelight@0: { rubidium@10609: AllocateWindowDescFront(&_town_view_desc, town); truelight@0: } truelight@0: truelight@0: static const Widget _town_directory_widgets[] = { rubidium@4344: { WWT_CLOSEBOX, RESIZE_NONE, 13, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, rubidium@4344: { WWT_CAPTION, RESIZE_NONE, 13, 11, 195, 0, 13, STR_2000_TOWNS, STR_018C_WINDOW_TITLE_DRAG_THIS}, rubidium@4344: { WWT_STICKYBOX, RESIZE_NONE, 13, 196, 207, 0, 13, 0x0, STR_STICKY_BUTTON}, rubidium@4344: { WWT_PUSHTXTBTN, RESIZE_NONE, 13, 0, 98, 14, 25, STR_SORT_BY_NAME, STR_SORT_ORDER_TIP}, rubidium@4344: { WWT_PUSHTXTBTN, RESIZE_NONE, 13, 99, 195, 14, 25, STR_SORT_BY_POPULATION, STR_SORT_ORDER_TIP}, Darkvater@4938: { WWT_PANEL, RESIZE_BOTTOM, 13, 0, 195, 26, 189, 0x0, STR_200A_TOWN_NAMES_CLICK_ON_NAME}, rubidium@4344: { WWT_SCROLLBAR, RESIZE_BOTTOM, 13, 196, 207, 14, 189, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, Darkvater@4938: { WWT_PANEL, RESIZE_TB, 13, 0, 195, 190, 201, 0x0, STR_NULL}, rubidium@4344: { WWT_RESIZEBOX, RESIZE_TB, 13, 196, 207, 190, 201, 0x0, STR_RESIZE_BUTTON}, darkvater@176: { WIDGETS_END}, truelight@0: }; truelight@0: truelight@0: peter1138@10636: struct TownDirectoryWindow : public Window { belugas@10665: private: peter1138@10636: enum TownDirectoryWidget { peter1138@10636: TDW_SORTNAME = 3, peter1138@10636: TDW_SORTPOPULATION, peter1138@10636: TDW_CENTERTOWN, peter1138@10636: }; peter1138@10636: peter1138@10747: /* Runtime saved values */ peter1138@10747: static Listing last_sorting; peter1138@10747: static const Town *last_town; peter1138@10747: peter1138@10747: /* Constants for sorting towns */ rubidium@10751: static GUITownList::SortFunction * const sorter_funcs[]; peter1138@10747: peter1138@10747: GUITownList towns; peter1138@10747: peter1138@10747: void BuildTownList() peter1138@10747: { peter1138@10747: if (!this->towns.NeedRebuild()) return; peter1138@10747: peter1138@10747: this->towns.Clear(); peter1138@10747: peter1138@10747: const Town *t; peter1138@10747: FOR_ALL_TOWNS(t) { peter1138@10747: *this->towns.Append() = t; peter1138@10747: } peter1138@10747: peter1138@10747: this->towns.Compact(); peter1138@10747: this->towns.RebuildDone(); peter1138@10747: } peter1138@10747: peter1138@10747: void SortTownList() peter1138@10747: { peter1138@10747: last_town = NULL; peter1138@10747: this->towns.Sort(); peter1138@10747: } peter1138@10747: peter1138@10747: /** Sort by town name */ peter1138@10747: static int CDECL TownNameSorter(const Town * const *a, const Town * const *b) peter1138@10747: { peter1138@10747: static char buf_cache[64]; peter1138@10747: const Town *ta = *a; peter1138@10747: const Town *tb = *b; peter1138@10747: char buf[64]; peter1138@10747: peter1138@10747: SetDParam(0, ta->index); peter1138@10747: GetString(buf, STR_TOWN, lastof(buf)); peter1138@10747: peter1138@10747: /* If 'b' is the same town as in the last round, use the cached value peter1138@10747: * We do this to speed stuff up ('b' is called with the same value a lot of peter1138@10747: * times after eachother) */ peter1138@10747: if (tb != last_town) { peter1138@10747: last_town = tb; peter1138@10747: SetDParam(0, tb->index); peter1138@10747: GetString(buf_cache, STR_TOWN, lastof(buf_cache)); peter1138@10747: } peter1138@10747: peter1138@10747: return strcmp(buf, buf_cache); peter1138@10747: } peter1138@10747: peter1138@10747: /** Sort by population */ peter1138@10747: static int CDECL TownPopulationSorter(const Town * const *a, const Town * const *b) peter1138@10747: { peter1138@10747: return (*a)->population - (*b)->population; peter1138@10747: } peter1138@10747: belugas@10665: public: peter1138@10636: TownDirectoryWindow(const WindowDesc *desc) : Window(desc, 0) peter1138@10636: { peter1138@10636: this->vscroll.cap = 16; peter1138@10636: this->resize.step_height = 10; peter1138@10636: this->resize.height = this->height - 10 * 6; // minimum of 10 items in the list, each item 10 high peter1138@10636: peter1138@10747: this->towns.SetListing(this->last_sorting); peter1138@10747: this->towns.SetSortFuncs(this->sorter_funcs); peter1138@10747: this->towns.ForceRebuild(); peter1138@10747: peter1138@10636: this->FindWindowPlacementAndResize(desc); peter1138@10636: } peter1138@10636: peter1138@10747: ~TownDirectoryWindow() peter1138@10747: { peter1138@10747: this->last_sorting = this->towns.GetListing(); peter1138@10747: } peter1138@10747: peter1138@10636: virtual void OnPaint() peter1138@10636: { peter1138@10747: this->BuildTownList(); peter1138@10747: this->SortTownList(); peter1138@10636: peter1138@10747: SetVScrollCount(this, this->towns.Length()); peter1138@10636: peter1138@10636: this->DrawWidgets(); skidd13@10979: this->DrawSortButtonState(this->towns.SortType() == 0 ? TDW_SORTNAME : TDW_SORTPOPULATION, this->towns.IsDescSortOrder() ? SBS_DOWN : SBS_UP); peter1138@10636: peter1138@10636: { peter1138@10636: int n = 0; peter1138@10636: uint16 i = this->vscroll.pos; peter1138@10636: int y = 28; peter1138@10636: peter1138@10747: while (i < this->towns.Length()) { peter1138@10747: const Town *t = this->towns[i]; peter1138@10636: peter1138@10636: assert(t->xy); peter1138@10636: peter1138@10636: SetDParam(0, t->index); peter1138@10636: SetDParam(1, t->population); peter1138@10636: DrawString(2, y, STR_2057, TC_FROMSTRING); peter1138@10636: peter1138@10636: y += 10; peter1138@10636: i++; peter1138@10636: if (++n == this->vscroll.cap) break; // max number of towns in 1 window darkvater@174: } truelight@0: peter1138@10636: SetDParam(0, GetWorldPopulation()); peter1138@10636: DrawString(3, this->height - 12 + 2, STR_TOWN_POPULATION, TC_FROMSTRING); peter1138@10636: } peter1138@10636: } belugas@10173: peter1138@10636: virtual void OnClick(Point pt, int widget) peter1138@10636: { peter1138@10636: switch (widget) { peter1138@10636: case TDW_SORTNAME: /* Sort by Name ascending/descending */ peter1138@10747: if (this->towns.SortType() == 0) { peter1138@10747: this->towns.ToggleSortOrder(); peter1138@10747: } else { peter1138@10747: this->towns.SetSortType(0); peter1138@10747: } peter1138@10636: this->SetDirty(); peter1138@10636: break; belugas@10173: peter1138@10636: case TDW_SORTPOPULATION: /* Sort by Population ascending/descending */ peter1138@10747: if (this->towns.SortType() == 1) { peter1138@10747: this->towns.ToggleSortOrder(); peter1138@10747: } else { peter1138@10747: this->towns.SetSortType(1); peter1138@10747: } peter1138@10636: this->SetDirty(); peter1138@10636: break; peter1138@10636: peter1138@10636: case TDW_CENTERTOWN: { /* Click on Town Matrix */ peter1138@10636: uint16 id_v = (pt.y - 28) / 10; peter1138@10636: peter1138@10636: if (id_v >= this->vscroll.cap) return; // click out of bounds peter1138@10636: peter1138@10636: id_v += this->vscroll.pos; peter1138@10636: peter1138@10747: if (id_v >= this->towns.Length()) return; // click out of town bounds peter1138@10636: peter1138@10747: const Town *t = this->towns[id_v]; peter1138@10636: assert(t->xy); peter1138@10636: if (_ctrl_pressed) { peter1138@10636: ShowExtraViewPortWindow(t->xy); peter1138@10636: } else { peter1138@10636: ScrollMainWindowToTile(t->xy); peter1138@10636: } peter1138@10636: break; belugas@10173: } peter1138@10636: } peter1138@10636: } belugas@10173: peter1138@10636: virtual void OnHundredthTick() peter1138@10636: { peter1138@10636: this->SetDirty(); peter1138@10636: } belugas@10173: peter1138@10636: virtual void OnResize(Point new_size, Point delta) peter1138@10636: { peter1138@10636: this->vscroll.cap += delta.y / 10; truelight@193: } peter1138@10747: peter1138@10747: virtual void OnInvalidateData(int data) peter1138@10747: { peter1138@10747: if (data == 0) { peter1138@10747: this->towns.ForceRebuild(); peter1138@10747: } else { peter1138@10747: this->towns.ForceResort(); peter1138@10747: } peter1138@10747: } peter1138@10747: }; peter1138@10747: peter1138@10747: Listing TownDirectoryWindow::last_sorting = {false, 0}; peter1138@10747: const Town *TownDirectoryWindow::last_town = NULL; peter1138@10747: peter1138@10747: /* Available town directory sorting functions */ rubidium@10751: GUITownList::SortFunction * const TownDirectoryWindow::sorter_funcs[] = { peter1138@10747: &TownNameSorter, peter1138@10747: &TownPopulationSorter, peter1138@10636: }; truelight@0: truelight@0: static const WindowDesc _town_directory_desc = { rubidium@7837: WDP_AUTO, WDP_AUTO, 208, 202, 208, 202, rubidium@6144: WC_TOWN_DIRECTORY, WC_NONE, truelight@867: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE, truelight@0: _town_directory_widgets, truelight@0: }; truelight@0: rubidium@6573: void ShowTownDirectory() truelight@0: { smatz@10696: if (BringWindowToFrontById(WC_TOWN_DIRECTORY, 0)) return; peter1138@10636: new TownDirectoryWindow(&_town_directory_desc); truelight@0: } rubidium@9219: rubidium@9219: void CcBuildTown(bool success, TileIndex tile, uint32 p1, uint32 p2) rubidium@9219: { rubidium@9219: if (success) { rubidium@9219: SndPlayTileFx(SND_1F_SPLAT, tile); rubidium@9219: ResetObjectToPlace(); rubidium@9219: } rubidium@9219: } rubidium@9219: rubidium@9219: static void PlaceProc_Town(TileIndex tile) rubidium@9219: { rubidium@9219: uint32 size = min(_scengen_town_size, (int)TSM_CITY); rubidium@9219: uint32 mode = _scengen_town_size > TSM_CITY ? TSM_CITY : TSM_FIXED; rubidium@9219: DoCommandP(tile, size, mode, CcBuildTown, CMD_BUILD_TOWN | CMD_MSG(STR_0236_CAN_T_BUILD_TOWN_HERE)); rubidium@9219: } rubidium@9219: rubidium@9219: static const Widget _scen_edit_town_gen_widgets[] = { rubidium@9219: { WWT_CLOSEBOX, RESIZE_NONE, 7, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, rubidium@9219: { WWT_CAPTION, RESIZE_NONE, 7, 11, 147, 0, 13, STR_0233_TOWN_GENERATION, STR_018C_WINDOW_TITLE_DRAG_THIS}, rubidium@9219: { WWT_STICKYBOX, RESIZE_NONE, 7, 148, 159, 0, 13, 0x0, STR_STICKY_BUTTON}, rubidium@9219: { WWT_PANEL, RESIZE_NONE, 7, 0, 159, 14, 94, 0x0, STR_NULL}, rubidium@9219: { WWT_TEXTBTN, RESIZE_NONE, 14, 2, 157, 16, 27, STR_0234_NEW_TOWN, STR_0235_CONSTRUCT_NEW_TOWN}, rubidium@9219: { WWT_TEXTBTN, RESIZE_NONE, 14, 2, 157, 29, 40, STR_023D_RANDOM_TOWN, STR_023E_BUILD_TOWN_IN_RANDOM_LOCATION}, rubidium@9219: { WWT_TEXTBTN, RESIZE_NONE, 14, 2, 157, 42, 53, STR_MANY_RANDOM_TOWNS, STR_RANDOM_TOWNS_TIP}, rubidium@9219: { WWT_TEXTBTN, RESIZE_NONE, 14, 2, 53, 68, 79, STR_02A1_SMALL, STR_02A4_SELECT_TOWN_SIZE}, rubidium@9219: { WWT_TEXTBTN, RESIZE_NONE, 14, 54, 105, 68, 79, STR_02A2_MEDIUM, STR_02A4_SELECT_TOWN_SIZE}, rubidium@9219: { WWT_TEXTBTN, RESIZE_NONE, 14, 106, 157, 68, 79, STR_02A3_LARGE, STR_02A4_SELECT_TOWN_SIZE}, rubidium@9219: { WWT_TEXTBTN, RESIZE_NONE, 14, 2, 157, 81, 92, STR_SCENARIO_EDITOR_CITY, STR_02A4_SELECT_TOWN_SIZE}, rubidium@9219: { WWT_LABEL, RESIZE_NONE, 7, 0, 147, 54, 67, STR_02A5_TOWN_SIZE, STR_NULL}, rubidium@9219: { WIDGETS_END}, rubidium@9219: }; rubidium@9219: rubidium@10609: struct ScenarioEditorTownGenerationWindow : Window rubidium@9219: { belugas@10665: private: belugas@10665: enum TownScenarioEditorWidget { belugas@10665: TSEW_NEWTOWN = 4, belugas@10665: TSEW_RANDOMTOWN, belugas@10665: TSEW_MANYRANDOMTOWNS, belugas@10665: TSEW_SMALLTOWN, belugas@10665: TSEW_MEDIUMTOWN, belugas@10665: TSEW_LARGETOWN, belugas@10665: TSEW_CITY, belugas@10665: }; belugas@10665: belugas@10665: public: rubidium@10609: ScenarioEditorTownGenerationWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number) rubidium@10609: { rubidium@10609: this->LowerWidget(_scengen_town_size + TSEW_SMALLTOWN); peter1138@10681: this->FindWindowPlacementAndResize(desc); rubidium@10609: } belugas@10173: rubidium@10609: virtual void OnPaint() rubidium@10609: { rubidium@10609: this->DrawWidgets(); rubidium@10609: } belugas@10173: rubidium@10609: virtual void OnClick(Point pt, int widget) rubidium@10609: { rubidium@10609: switch (widget) { rubidium@10609: case TSEW_NEWTOWN: rubidium@10609: HandlePlacePushButton(this, TSEW_NEWTOWN, SPR_CURSOR_TOWN, VHM_RECT, PlaceProc_Town); rubidium@10609: break; belugas@10173: rubidium@10609: case TSEW_RANDOMTOWN: { rubidium@10609: Town *t; rubidium@10609: uint size = min(_scengen_town_size, (int)TSM_CITY); rubidium@10609: TownSizeMode mode = _scengen_town_size > TSM_CITY ? TSM_CITY : TSM_FIXED; belugas@10173: rubidium@10609: this->HandleButtonClick(TSEW_RANDOMTOWN); rubidium@10609: _generating_world = true; rubidium@10609: t = CreateRandomTown(20, mode, size); rubidium@10609: _generating_world = false; rubidium@10609: rubidium@10609: if (t == NULL) { rubidium@10609: ShowErrorMessage(STR_NO_SPACE_FOR_TOWN, STR_CANNOT_GENERATE_TOWN, 0, 0); rubidium@10609: } else { rubidium@10609: ScrollMainWindowToTile(t->xy); rubidium@10609: } belugas@10173: } break; belugas@10173: rubidium@10609: case TSEW_MANYRANDOMTOWNS: rubidium@10609: this->HandleButtonClick(TSEW_MANYRANDOMTOWNS); rubidium@9219: rubidium@10609: _generating_world = true; rubidium@10609: if (!GenerateTowns()) ShowErrorMessage(STR_NO_SPACE_FOR_TOWN, STR_CANNOT_GENERATE_TOWN, 0, 0); rubidium@10609: _generating_world = false; rubidium@10609: break; belugas@10173: rubidium@10609: case TSEW_SMALLTOWN: case TSEW_MEDIUMTOWN: case TSEW_LARGETOWN: case TSEW_CITY: rubidium@10609: this->RaiseWidget(_scengen_town_size + TSEW_SMALLTOWN); rubidium@10609: _scengen_town_size = widget - TSEW_SMALLTOWN; rubidium@10609: this->LowerWidget(_scengen_town_size + TSEW_SMALLTOWN); rubidium@10609: this->SetDirty(); rubidium@10609: break; rubidium@10609: } rubidium@9219: } rubidium@10609: rubidium@10609: virtual void OnTimeout() rubidium@10609: { rubidium@10609: this->RaiseWidget(TSEW_RANDOMTOWN); rubidium@10609: this->RaiseWidget(TSEW_MANYRANDOMTOWNS); rubidium@10609: this->SetDirty(); rubidium@10609: } rubidium@10609: rubidium@10609: virtual void OnPlaceObject(Point pt, TileIndex tile) rubidium@10609: { rubidium@10609: _place_proc(tile); rubidium@10609: } rubidium@10609: rubidium@10609: virtual void OnPlaceObjectAbort() rubidium@10609: { rubidium@10609: this->RaiseButtons(); rubidium@10609: this->LowerWidget(_scengen_town_size + TSEW_SMALLTOWN); rubidium@10609: this->SetDirty(); rubidium@10609: } rubidium@10609: }; rubidium@9219: rubidium@9219: static const WindowDesc _scen_edit_town_gen_desc = { rubidium@9219: WDP_AUTO, WDP_AUTO, 160, 95, 160, 95, rubidium@9219: WC_SCEN_TOWN_GEN, WC_NONE, rubidium@9219: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON, rubidium@9219: _scen_edit_town_gen_widgets, rubidium@9219: }; rubidium@9219: rubidium@9219: void ShowBuildTownWindow() rubidium@9219: { rubidium@11161: if (_game_mode != GM_EDITOR && !IsValidPlayerID(_current_player)) return; rubidium@10609: AllocateWindowDescFront(&_scen_edit_town_gen_desc, 0); rubidium@9219: } rubidium@9219: