tron@2186: /* $Id$ */ tron@2186: rubidium@10455: /** @file subsidy_gui.cpp GUI for subsidies. */ glx@9574: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" rubidium@9837: #include "station_base.h" truelight@0: #include "industry.h" truelight@0: #include "town.h" rubidium@9723: #include "economy_func.h" tron@2159: #include "variables.h" truelight@9476: #include "cargotype.h" rubidium@9723: #include "window_gui.h" rubidium@9723: #include "strings_func.h" rubidium@9723: #include "date_func.h" rubidium@9723: #include "viewport_func.h" rubidium@9723: #include "gfx_func.h" rubidium@10455: #include "gui.h" truelight@0: rubidium@9724: #include "table/strings.h" rubidium@9724: glx@10645: struct SubsidyListWindow : Window { glx@10645: SubsidyListWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number) glx@10645: { rubidium@10715: this->FindWindowPlacementAndResize(desc); glx@10645: } truelight@0: glx@10645: virtual void OnClick(Point pt, int widget) glx@10645: { glx@10645: if (widget != 3) return; truelight@0: glx@10645: int y = pt.y - 25; glx@10645: glx@10645: if (y < 0) return; glx@10645: glx@10645: uint num = 0; glx@10645: for (const Subsidy *s = _subsidies; s != endof(_subsidies); s++) { glx@10645: if (s->cargo_type != CT_INVALID && s->age < 12) { glx@10645: y -= 10; glx@10829: if (y < 0) { glx@10829: this->HandleClick(s); glx@10829: return; glx@10829: } glx@10645: num++; glx@10645: } glx@10645: } glx@10645: glx@10645: if (num == 0) { glx@10829: y -= 10; // "None" glx@10645: if (y < 0) return; glx@10645: } glx@10645: glx@10829: y -= 11; // "Services already subsidised:" glx@10645: if (y < 0) return; glx@10645: glx@10645: for (const Subsidy *s = _subsidies; s != endof(_subsidies); s++) { glx@10645: if (s->cargo_type != CT_INVALID && s->age >= 12) { glx@10645: y -= 10; glx@10829: if (y < 0) { glx@10829: this->HandleClick(s); glx@10829: return; glx@10829: } glx@10645: } truelight@0: } truelight@0: } truelight@0: glx@10645: void HandleClick(const Subsidy *s) glx@10645: { glx@10645: TownEffect te = GetCargo(s->cargo_type)->town_effect; glx@10645: TileIndex xy; truelight@193: glx@10645: /* determine from coordinate for subsidy and try to scroll to it */ glx@10645: uint offs = s->from; truelight@0: if (s->age >= 12) { truelight@919: xy = GetStation(offs)->xy; glx@10645: } else if (te == TE_PASSENGERS || te == TE_MAIL) { truelight@919: xy = GetTown(offs)->xy; truelight@0: } else { truelight@919: xy = GetIndustry(offs)->xy; truelight@0: } rubidium@10455: glx@10645: if (_ctrl_pressed || !ScrollMainWindowToTile(xy)) { glx@10645: if (_ctrl_pressed) ShowExtraViewPortWindow(xy); truelight@0: glx@10645: /* otherwise determine to coordinate for subsidy and scroll to it */ glx@10645: offs = s->to; glx@10645: if (s->age >= 12) { glx@10645: xy = GetStation(offs)->xy; glx@10645: } else if (te == TE_PASSENGERS || te == TE_MAIL || te == TE_GOODS || te == TE_FOOD) { glx@10645: xy = GetTown(offs)->xy; glx@10645: } else { glx@10645: xy = GetIndustry(offs)->xy; glx@10645: } truelight@0: glx@10645: if (_ctrl_pressed) { glx@10645: ShowExtraViewPortWindow(xy); glx@10645: } else { glx@10645: ScrollMainWindowToTile(xy); glx@10645: } truelight@0: } truelight@0: } truelight@0: glx@10645: virtual void OnPaint() glx@10645: { glx@10645: YearMonthDay ymd; glx@10645: const Subsidy *s; truelight@0: glx@10645: this->DrawWidgets(); truelight@0: glx@10645: ConvertDateToYMD(_date, &ymd); tron@2639: glx@10645: int width = this->width - 13; // scroll bar = 11 + pixel each side glx@10645: int y = 15; glx@10645: int x = 1; glx@10645: glx@10645: /* Section for drawing the offered subisidies */ glx@10645: DrawStringTruncated(x, y, STR_2026_SUBSIDIES_ON_OFFER_FOR, TC_FROMSTRING, width); glx@10645: y += 10; glx@10645: uint num = 0; glx@10645: glx@10645: for (s = _subsidies; s != endof(_subsidies); s++) { glx@10645: if (s->cargo_type != CT_INVALID && s->age < 12) { glx@10645: int x2; glx@10645: glx@10645: /* Displays the two offered towns */ glx@10645: SetupSubsidyDecodeParam(s, 1); rubidium@10715: x2 = DrawStringTruncated(x + 2, y, STR_2027_FROM_TO, TC_FROMSTRING, width - 2); glx@10645: rubidium@10715: if (width - x2 > 10) { rubidium@10715: /* Displays the deadline before voiding the proposal */ rubidium@10715: SetDParam(0, _date - ymd.day + 384 - s->age * 32); rubidium@10715: DrawStringTruncated(x2, y, STR_2028_BY, TC_FROMSTRING, width - x2); rubidium@10715: } rubidium@10715: glx@10645: y += 10; glx@10645: num++; tron@2639: } glx@10645: } glx@10645: glx@10645: if (num == 0) { glx@10645: DrawStringTruncated(x + 2, y, STR_202A_NONE, TC_FROMSTRING, width - 2); glx@10645: y += 10; glx@10645: } glx@10645: glx@10645: /* Section for drawing the already granted subisidies */ glx@10645: DrawStringTruncated(x, y + 1, STR_202B_SERVICES_ALREADY_SUBSIDISED, TC_FROMSTRING, width); glx@10645: y += 10; glx@10645: num = 0; glx@10645: glx@10645: for (s = _subsidies; s != endof(_subsidies); s++) { glx@10645: if (s->cargo_type != CT_INVALID && s->age >= 12) { glx@10645: int xt; glx@10645: glx@10645: SetupSubsidyDecodeParam(s, 1); glx@10645: glx@10645: PlayerID player = GetStation(s->to)->owner; glx@10645: SetDParam(3, player); glx@10645: glx@10645: /* Displays the two connected stations */ glx@10645: xt = DrawStringTruncated(x + 2, y, STR_202C_FROM_TO, TC_FROMSTRING, width - 2); glx@10645: glx@10645: /* Displays the date where the granted subsidy will end */ glx@10645: if ((xt > 3) && (width - xt) > 9 ) { // do not draw if previous drawing failed or if it will overlap on scrollbar glx@10645: SetDParam(0, _date - ymd.day + 768 - s->age * 32); glx@10645: DrawStringTruncated(xt, y, STR_202D_UNTIL, TC_FROMSTRING, width - xt); glx@10645: } glx@10645: y += 10; glx@10645: num++; glx@10645: } glx@10645: } glx@10645: glx@10645: if (num == 0) DrawStringTruncated(x + 2, y, STR_202A_NONE, TC_FROMSTRING, width - 2); truelight@0: } glx@10645: }; truelight@0: truelight@0: static const Widget _subsidies_list_widgets[] = { rubidium@9869: { WWT_CLOSEBOX, RESIZE_NONE, 13, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, rubidium@9869: { WWT_CAPTION, RESIZE_RIGHT, 13, 11, 307, 0, 13, STR_2025_SUBSIDIES, STR_018C_WINDOW_TITLE_DRAG_THIS}, rubidium@9869: { WWT_STICKYBOX, RESIZE_LR, 13, 308, 319, 0, 13, STR_NULL, STR_STICKY_BUTTON}, rubidium@9869: { WWT_PANEL, RESIZE_RB, 13, 0, 307, 14, 126, 0x0, STR_01FD_CLICK_ON_SERVICE_TO_CENTER}, rubidium@10142: { WWT_SCROLLBAR, RESIZE_LRB, 13, 308, 319, 14, 114, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, rubidium@10142: { WWT_RESIZEBOX, RESIZE_LRTB, 13, 308, 319, 115, 126, 0x0, STR_RESIZE_BUTTON}, rubidium@9869: darkvater@176: { WIDGETS_END}, truelight@0: }; truelight@0: truelight@0: static const WindowDesc _subsidies_list_desc = { rubidium@9869: WDP_AUTO, WDP_AUTO, 320, 127, 320, 127, rubidium@6144: WC_SUBSIDIES_LIST, WC_NONE, rubidium@9869: WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON | WDF_RESIZABLE, truelight@0: _subsidies_list_widgets, truelight@0: }; truelight@0: truelight@0: rubidium@6573: void ShowSubsidiesList() truelight@0: { glx@10645: AllocateWindowDescFront(&_subsidies_list_desc, 0); truelight@0: }