2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file subsidy_gui.cpp GUI for subsidies. */
|
2007-04-04 01:35:16 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "industry.h"
|
|
|
|
#include "town.h"
|
2007-12-19 19:44:29 +00:00
|
|
|
#include "window_gui.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2007-12-26 13:50:40 +00:00
|
|
|
#include "date_func.h"
|
2008-01-09 09:45:45 +00:00
|
|
|
#include "viewport_func.h"
|
|
|
|
#include "gfx_func.h"
|
2008-05-05 11:36:43 +00:00
|
|
|
#include "gui.h"
|
2009-05-23 15:46:00 +00:00
|
|
|
#include "subsidy_func.h"
|
2009-07-01 18:45:05 +00:00
|
|
|
#include "subsidy_base.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "core/geometry_func.hpp"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2009-04-17 19:47:21 +00:00
|
|
|
/** Widget numbers for the subsidy list window. */
|
|
|
|
enum SubsidyListWidgets {
|
|
|
|
SLW_PANEL,
|
|
|
|
SLW_SCROLLBAR,
|
|
|
|
};
|
|
|
|
|
2008-05-18 08:12:29 +00:00
|
|
|
struct SubsidyListWindow : Window {
|
2010-08-12 08:37:01 +00:00
|
|
|
Scrollbar *vscroll;
|
|
|
|
|
2009-09-01 19:29:16 +00:00
|
|
|
SubsidyListWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
|
2008-05-18 08:12:29 +00:00
|
|
|
{
|
2010-08-12 08:37:01 +00:00
|
|
|
this->CreateNestedTree(desc);
|
|
|
|
this->vscroll = this->GetScrollbar(SLW_SCROLLBAR);
|
|
|
|
this->FinishInitNested(desc, window_number);
|
2009-09-01 20:42:12 +00:00
|
|
|
this->OnInvalidateData(0);
|
2008-05-18 08:12:29 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-01-30 18:34:48 +00:00
|
|
|
virtual void OnClick(Point pt, int widget, int click_count)
|
2008-05-18 08:12:29 +00:00
|
|
|
{
|
2009-04-17 19:47:21 +00:00
|
|
|
if (widget != SLW_PANEL) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-08-12 08:37:01 +00:00
|
|
|
int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, SLW_PANEL, WD_FRAMERECT_TOP);
|
2009-09-01 20:42:12 +00:00
|
|
|
int num = 0;
|
2009-07-01 17:43:26 +00:00
|
|
|
const Subsidy *s;
|
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
2009-07-18 19:54:35 +00:00
|
|
|
if (!s->IsAwarded()) {
|
2009-09-01 20:42:12 +00:00
|
|
|
y--;
|
|
|
|
if (y == 0) {
|
2008-06-03 00:23:54 +00:00
|
|
|
this->HandleClick(s);
|
|
|
|
return;
|
|
|
|
}
|
2008-05-18 08:12:29 +00:00
|
|
|
num++;
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-18 08:12:29 +00:00
|
|
|
if (num == 0) {
|
2009-09-01 20:42:12 +00:00
|
|
|
y--; // "None"
|
2008-05-18 08:12:29 +00:00
|
|
|
if (y < 0) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-09-01 20:42:12 +00:00
|
|
|
y -= 2; // "Services already subsidised:"
|
2008-05-18 08:12:29 +00:00
|
|
|
if (y < 0) return;
|
2007-03-18 21:58:03 +00:00
|
|
|
|
2009-07-01 17:43:26 +00:00
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
2009-07-18 19:54:35 +00:00
|
|
|
if (s->IsAwarded()) {
|
2009-09-01 20:42:12 +00:00
|
|
|
y--;
|
|
|
|
if (y == 0) {
|
2008-06-03 00:23:54 +00:00
|
|
|
this->HandleClick(s);
|
|
|
|
return;
|
|
|
|
}
|
2008-05-18 08:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-03-18 21:58:03 +00:00
|
|
|
|
2008-05-18 08:12:29 +00:00
|
|
|
void HandleClick(const Subsidy *s)
|
|
|
|
{
|
2009-08-07 22:23:34 +00:00
|
|
|
/* determine src coordinate for subsidy and try to scroll to it */
|
2008-05-18 08:12:29 +00:00
|
|
|
TileIndex xy;
|
2009-08-07 22:23:34 +00:00
|
|
|
switch (s->src_type) {
|
2010-01-04 18:21:07 +00:00
|
|
|
case ST_INDUSTRY: xy = Industry::Get(s->src)->location.tile; break;
|
2009-08-07 22:23:34 +00:00
|
|
|
case ST_TOWN: xy = Town::Get(s->src)->xy; break;
|
|
|
|
default: NOT_REACHED();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-05-05 11:36:43 +00:00
|
|
|
|
2008-05-18 08:12:29 +00:00
|
|
|
if (_ctrl_pressed || !ScrollMainWindowToTile(xy)) {
|
|
|
|
if (_ctrl_pressed) ShowExtraViewPortWindow(xy);
|
|
|
|
|
2009-08-07 22:23:34 +00:00
|
|
|
/* otherwise determine dst coordinate for subsidy and scroll to it */
|
|
|
|
switch (s->dst_type) {
|
2010-01-04 18:21:07 +00:00
|
|
|
case ST_INDUSTRY: xy = Industry::Get(s->dst)->location.tile; break;
|
2009-08-07 22:23:34 +00:00
|
|
|
case ST_TOWN: xy = Town::Get(s->dst)->xy; break;
|
|
|
|
default: NOT_REACHED();
|
2008-05-18 08:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_ctrl_pressed) {
|
|
|
|
ShowExtraViewPortWindow(xy);
|
|
|
|
} else {
|
|
|
|
ScrollMainWindowToTile(xy);
|
|
|
|
}
|
2008-05-05 11:36:43 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-09-01 20:42:12 +00:00
|
|
|
/**
|
|
|
|
* Count the number of lines in this window.
|
|
|
|
* @return the number of lines
|
|
|
|
*/
|
|
|
|
uint CountLines()
|
2009-09-01 20:06:10 +00:00
|
|
|
{
|
|
|
|
/* Count number of (non) awarded subsidies */
|
|
|
|
uint num_awarded = 0;
|
|
|
|
uint num_not_awarded = 0;
|
|
|
|
const Subsidy *s;
|
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
|
|
|
if (!s->IsAwarded()) {
|
|
|
|
num_not_awarded++;
|
|
|
|
} else {
|
|
|
|
num_awarded++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Count the 'none' lines */
|
|
|
|
if (num_awarded == 0) num_awarded = 1;
|
|
|
|
if (num_not_awarded == 0) num_not_awarded = 1;
|
|
|
|
|
2009-09-01 20:42:12 +00:00
|
|
|
/* Offered, accepted and an empty line before the accepted ones. */
|
|
|
|
return 3 + num_awarded + num_not_awarded;
|
|
|
|
}
|
2009-09-01 20:06:10 +00:00
|
|
|
|
2009-11-22 18:28:14 +00:00
|
|
|
virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
|
2009-09-01 20:42:12 +00:00
|
|
|
{
|
|
|
|
if (widget != SLW_PANEL) return;
|
|
|
|
Dimension d = maxdim(GetStringBoundingBox(STR_SUBSIDIES_OFFERED_TITLE), GetStringBoundingBox(STR_SUBSIDIES_SUBSIDISED_TITLE));
|
|
|
|
|
|
|
|
resize->height = d.height;
|
|
|
|
|
|
|
|
d.height *= 5;
|
2009-09-01 20:06:10 +00:00
|
|
|
d.width += padding.width + WD_FRAMERECT_RIGHT + WD_FRAMERECT_LEFT;
|
|
|
|
d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
|
|
|
|
*size = maxdim(*size, d);
|
|
|
|
}
|
|
|
|
|
2009-09-01 19:29:16 +00:00
|
|
|
virtual void DrawWidget(const Rect &r, int widget) const
|
|
|
|
{
|
|
|
|
if (widget != SLW_PANEL) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-01 19:29:16 +00:00
|
|
|
YearMonthDay ymd;
|
2008-05-18 08:12:29 +00:00
|
|
|
ConvertDateToYMD(_date, &ymd);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-01 19:29:16 +00:00
|
|
|
int right = r.right - WD_FRAMERECT_RIGHT;
|
|
|
|
int y = r.top + WD_FRAMERECT_TOP;
|
|
|
|
int x = r.left + WD_FRAMERECT_LEFT;
|
2008-04-07 17:02:39 +00:00
|
|
|
|
2010-08-12 08:37:01 +00:00
|
|
|
int pos = -this->vscroll->GetPosition();
|
|
|
|
const int cap = this->vscroll->GetCapacity();
|
2009-09-01 20:42:12 +00:00
|
|
|
|
2008-05-18 08:12:29 +00:00
|
|
|
/* Section for drawing the offered subisidies */
|
2009-09-02 08:40:31 +00:00
|
|
|
if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_TITLE);
|
2009-09-01 20:42:12 +00:00
|
|
|
pos++;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-01 20:42:12 +00:00
|
|
|
uint num = 0;
|
2009-09-01 19:29:16 +00:00
|
|
|
const Subsidy *s;
|
2009-07-01 17:43:26 +00:00
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
2009-07-18 19:54:35 +00:00
|
|
|
if (!s->IsAwarded()) {
|
2009-09-02 08:40:31 +00:00
|
|
|
if (IsInsideMM(pos, 0, cap)) {
|
2009-09-01 20:42:12 +00:00
|
|
|
/* Displays the two offered towns */
|
2010-11-24 01:08:49 +00:00
|
|
|
SetupSubsidyDecodeParam(s, true);
|
2009-09-01 20:42:12 +00:00
|
|
|
SetDParam(7, _date - ymd.day + s->remaining * 32);
|
|
|
|
DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_OFFERED_FROM_TO);
|
|
|
|
}
|
|
|
|
pos++;
|
2008-05-18 08:12:29 +00:00
|
|
|
num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num == 0) {
|
2009-09-02 08:40:31 +00:00
|
|
|
if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE);
|
2009-09-01 20:42:12 +00:00
|
|
|
pos++;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 08:12:29 +00:00
|
|
|
/* Section for drawing the already granted subisidies */
|
2009-09-01 20:42:12 +00:00
|
|
|
pos++;
|
2009-09-02 08:40:31 +00:00
|
|
|
if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_TITLE);
|
2009-09-01 20:42:12 +00:00
|
|
|
pos++;
|
2008-05-18 08:12:29 +00:00
|
|
|
num = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-07-01 17:43:26 +00:00
|
|
|
FOR_ALL_SUBSIDIES(s) {
|
2009-07-18 19:54:35 +00:00
|
|
|
if (s->IsAwarded()) {
|
2009-09-02 08:40:31 +00:00
|
|
|
if (IsInsideMM(pos, 0, cap)) {
|
2010-11-24 01:08:49 +00:00
|
|
|
SetupSubsidyDecodeParam(s, true);
|
2009-09-01 20:42:12 +00:00
|
|
|
SetDParam(7, s->awarded);
|
|
|
|
SetDParam(8, _date - ymd.day + s->remaining * 32);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-01 20:42:12 +00:00
|
|
|
/* Displays the two connected stations */
|
|
|
|
DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_SUBSIDISED_FROM_TO);
|
|
|
|
}
|
|
|
|
pos++;
|
2008-05-18 08:12:29 +00:00
|
|
|
num++;
|
2008-04-04 02:24:44 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-09-01 20:42:12 +00:00
|
|
|
if (num == 0) {
|
2009-09-02 08:40:31 +00:00
|
|
|
if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_SUBSIDIES_NONE);
|
2009-09-01 20:42:12 +00:00
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-24 14:53:55 +00:00
|
|
|
virtual void OnResize()
|
2009-09-01 20:42:12 +00:00
|
|
|
{
|
2010-08-12 08:37:01 +00:00
|
|
|
this->vscroll->SetCapacityFromWidget(this, SLW_PANEL);
|
2009-09-01 20:42:12 +00:00
|
|
|
}
|
|
|
|
|
2011-03-13 21:31:29 +00:00
|
|
|
/**
|
|
|
|
* Some data on this window has become invalid.
|
|
|
|
* @param data Information about the changed data.
|
|
|
|
* @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
|
|
|
|
*/
|
|
|
|
virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
|
2009-09-01 20:42:12 +00:00
|
|
|
{
|
2011-03-13 21:31:29 +00:00
|
|
|
if (!gui_scope) return;
|
2010-08-12 08:37:01 +00:00
|
|
|
this->vscroll->SetCount(this->CountLines());
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-05-18 08:12:29 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-04-17 20:03:44 +00:00
|
|
|
static const NWidgetPart _nested_subsidies_list_widgets[] = {
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
2009-11-24 18:05:55 +00:00
|
|
|
NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
|
|
|
|
NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_SUBSIDIES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
|
2009-12-21 16:24:29 +00:00
|
|
|
NWidget(WWT_SHADEBOX, COLOUR_BROWN),
|
2009-11-24 18:05:55 +00:00
|
|
|
NWidget(WWT_STICKYBOX, COLOUR_BROWN),
|
2009-04-17 20:03:44 +00:00
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
2010-08-12 06:44:45 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_BROWN, SLW_PANEL), SetDataTip(0x0, STR_SUBSIDIES_TOOLTIP_CLICK_ON_SERVICE_TO_CENTER), SetResize(1, 1), SetScrollbar(SLW_SCROLLBAR), EndContainer(),
|
2009-04-17 20:03:44 +00:00
|
|
|
NWidget(NWID_VERTICAL),
|
2010-08-12 09:16:43 +00:00
|
|
|
NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, SLW_SCROLLBAR),
|
2009-11-24 18:05:55 +00:00
|
|
|
NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
|
2009-04-17 20:03:44 +00:00
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
|
|
|
};
|
|
|
|
|
2009-03-15 15:12:06 +00:00
|
|
|
static const WindowDesc _subsidies_list_desc(
|
2009-11-28 14:42:35 +00:00
|
|
|
WDP_AUTO, 500, 127,
|
2007-02-01 15:49:12 +00:00
|
|
|
WC_SUBSIDIES_LIST, WC_NONE,
|
2009-11-24 17:28:29 +00:00
|
|
|
0,
|
2009-11-15 10:26:01 +00:00
|
|
|
_nested_subsidies_list_widgets, lengthof(_nested_subsidies_list_widgets)
|
2009-03-15 15:12:06 +00:00
|
|
|
);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void ShowSubsidiesList()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-05-18 08:12:29 +00:00
|
|
|
AllocateWindowDescFront<SubsidyListWindow>(&_subsidies_list_desc, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|