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