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