summaryrefslogtreecommitdiff
path: root/src/subsidy_gui.cpp
blob: bf1a67181675dd3010cde870e5646550d5d106ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* $Id$ */

/** @file subsidy_gui.cpp GUI for subsidies. */

#include "stdafx.h"
#include "openttd.h"
#include "station_base.h"
#include "industry.h"
#include "town.h"
#include "economy_func.h"
#include "variables.h"
#include "cargotype.h"
#include "window_gui.h"
#include "strings_func.h"
#include "date_func.h"
#include "viewport_func.h"
#include "gfx_func.h"
#include "gui.h"

#include "table/strings.h"

static void HandleSubsidyClick(int y)
{
	const Subsidy *s;
	uint num;
	int offs;
	TileIndex xy;

	if (y < 0) return;

	num = 0;
	for (s = _subsidies; s != endof(_subsidies); s++) {
		if (s->cargo_type != CT_INVALID && s->age < 12) {
			y -= 10;
			if (y < 0) goto handle_click;
			num++;
		}
	}

	if (num == 0) {
		y -= 10;
		if (y < 0) return;
	}

	y -= 11;
	if (y < 0) return;

	for (s = _subsidies; s != endof(_subsidies); s++) {
		if (s->cargo_type != CT_INVALID && s->age >= 12) {
			y -= 10;
			if (y < 0) goto handle_click;
		}
	}
	return;

handle_click:

	TownEffect te = GetCargo(s->cargo_type)->town_effect;

	/* determine from coordinate for subsidy and try to scroll to it */
	offs = s->from;
	if (s->age >= 12) {
		xy = GetStation(offs)->xy;
	} else if (te == TE_PASSENGERS || te == TE_MAIL) {
		xy = GetTown(offs)->xy;
	} else {
		xy = GetIndustry(offs)->xy;
	}

	if (_ctrl_pressed || !ScrollMainWindowToTile(xy)) {
		if (_ctrl_pressed) ShowExtraViewPortWindow(xy);

		/* otherwise determine to coordinate for subsidy and scroll to it */
		offs = s->to;
		if (s->age >= 12) {
			xy = GetStation(offs)->xy;
		} else if (te == TE_PASSENGERS || te == TE_MAIL || te == TE_GOODS || te == TE_FOOD) {
			xy = GetTown(offs)->xy;
		} else {
			xy = GetIndustry(offs)->xy;
		}

		if (_ctrl_pressed) {
			ShowExtraViewPortWindow(xy);
		} else {
			ScrollMainWindowToTile(xy);
		}
	}
}

static void DrawSubsidiesWindow(const Window *w)
{
	YearMonthDay ymd;
	const Subsidy *s;
	uint num;
	int x;
	int y;

	w->DrawWidgets();

	ConvertDateToYMD(_date, &ymd);

	int width = w->width - 13;  // scroll bar = 11 + pixel each side
	y = 15;
	x = 1;

	/* Section for drawing the offered subisidies */
	DrawStringTruncated(x, y, STR_2026_SUBSIDIES_ON_OFFER_FOR, TC_FROMSTRING, width);
	y += 10;
	num = 0;

	for (s = _subsidies; s != endof(_subsidies); s++) {
		if (s->cargo_type != CT_INVALID && s->age < 12) {
			int x2;

			/* Displays the two offered towns */
			SetupSubsidyDecodeParam(s, 1);
			x2 = DrawStringTruncated(x + 2, y, STR_2027_FROM_TO, TC_FROMSTRING, width);

			/* Displays the deadline before voiding the proposal */
			SetDParam(0, _date - ymd.day + 384 - s->age * 32);
			DrawStringTruncated(x2, y, STR_2028_BY, TC_FROMSTRING, width - x2);
			y += 10;
			num++;
		}
	}

	if (num == 0) {
		DrawStringTruncated(x + 2, y, STR_202A_NONE, TC_FROMSTRING, width - 2);
		y += 10;
	}

	/* Section for drawing the already granted subisidies */
	DrawStringTruncated(x, y + 1, STR_202B_SERVICES_ALREADY_SUBSIDISED, TC_FROMSTRING, width);
	y += 10;
	num = 0;

	for (s = _subsidies; s != endof(_subsidies); s++) {
		if (s->cargo_type != CT_INVALID && s->age >= 12) {
			int xt;

			SetupSubsidyDecodeParam(s, 1);

			PlayerID player = GetStation(s->to)->owner;
			SetDParam(3, player);

			/* Displays the two connected stations */
			xt = DrawStringTruncated(x + 2, y, STR_202C_FROM_TO, TC_FROMSTRING, width - 2);

			/* Displays the date where the granted subsidy will end */
			if ((xt > 3) && (width - xt) > 9 ) { // do not draw if previous drawing failed or if it will overlap on scrollbar
				SetDParam(0, _date - ymd.day + 768 - s->age * 32);
				DrawStringTruncated(xt, y, STR_202D_UNTIL, TC_FROMSTRING, width - xt);
			}
			y += 10;
			num++;
		}
	}

	if (num == 0) DrawStringTruncated(x + 2, y, STR_202A_NONE, TC_FROMSTRING, width - 2);
}

static void SubsidiesListWndProc(Window *w, WindowEvent *e)
{
	switch (e->event) {
		case WE_PAINT: DrawSubsidiesWindow(w); break;

		case WE_CLICK:
			switch (e->we.click.widget) {
				case 3:
					HandleSubsidyClick(e->we.click.pt.y - 25);
					break;
			}
		break;
	}
}

static const Widget _subsidies_list_widgets[] = {
{   WWT_CLOSEBOX, RESIZE_NONE,   13,   0,  10,   0,  13, STR_00C5,           STR_018B_CLOSE_WINDOW},
{    WWT_CAPTION, RESIZE_RIGHT,  13,  11, 307,   0,  13, STR_2025_SUBSIDIES, STR_018C_WINDOW_TITLE_DRAG_THIS},
{  WWT_STICKYBOX, RESIZE_LR,     13, 308, 319,   0,  13, STR_NULL,           STR_STICKY_BUTTON},
{      WWT_PANEL, RESIZE_RB,     13,   0, 307,  14, 126, 0x0,                STR_01FD_CLICK_ON_SERVICE_TO_CENTER},
{  WWT_SCROLLBAR, RESIZE_LRB,    13, 308, 319,  14, 114, 0x0,                STR_0190_SCROLL_BAR_SCROLLS_LIST},
{  WWT_RESIZEBOX, RESIZE_LRTB,   13, 308, 319, 115, 126, 0x0,                STR_RESIZE_BUTTON},

{   WIDGETS_END},
};

static const WindowDesc _subsidies_list_desc = {
	WDP_AUTO, WDP_AUTO, 320, 127, 320, 127,
	WC_SUBSIDIES_LIST, WC_NONE,
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON | WDF_RESIZABLE,
	_subsidies_list_widgets,
	SubsidiesListWndProc
};


void ShowSubsidiesList()
{
	AllocateWindowDescFront<Window>(&_subsidies_list_desc, 0);
}