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"
|
2009-10-04 17:16:41 +00:00
|
|
|
#include "strings_type.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#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"
|
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_CLOSEBOX,
|
|
|
|
SLW_CAPTION,
|
|
|
|
SLW_STICKYBOX,
|
|
|
|
SLW_PANEL,
|
|
|
|
SLW_SCROLLBAR,
|
|
|
|
SLW_RESIZEBOX,
|
|
|
|
};
|
|
|
|
|
2008-05-18 08:12:29 +00:00
|
|
|
struct SubsidyListWindow : Window {
|
2009-09-01 19:29:16 +00:00
|
|
|
SubsidyListWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
|
2008-05-18 08:12:29 +00:00
|
|
|
{
|
2009-09-01 19:29:16 +00:00
|
|
|
this->InitNested(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
|
|
|
|
2008-05-18 08:12:29 +00:00
|
|
|
virtual void OnClick(Point pt, int widget)
|
|
|
|
{
|
2009-04-17 19:47:21 +00:00
|
|
|
if (widget != SLW_PANEL) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-19 11:31:12 +00:00
|
|
|
int y = (pt.y - this->GetWidget<NWidgetBase>(SLW_PANEL)->pos_y - WD_FRAMERECT_TOP) / this->resize.step_height;
|
2009-09-02 08:40:31 +00:00
|
|
|
if (!IsInsideMM(y, 0, this->vscroll.GetCapacity())) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-02 08:40:31 +00:00
|
|
|
y += this->vscroll.GetPosition();
|
2009-07-01 17:43:26 +00:00
|
|
|
|
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) {
|
|
|
|
case ST_INDUSTRY: xy = Industry::Get(s->src)->xy; break;
|
|
|
|
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) {
|
|
|
|
case ST_INDUSTRY: xy = Industry::Get(s->dst)->xy; break;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-05-18 08:12:29 +00:00
|
|
|
virtual void OnPaint()
|
|
|
|
{
|
|
|
|
this->DrawWidgets();
|
2009-09-01 19:29:16 +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
|
|
|
|
2009-09-02 08:40:31 +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 */
|
|
|
|
SetupSubsidyDecodeParam(s, 1);
|
|
|
|
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)) {
|
2009-09-01 20:42:12 +00:00
|
|
|
SetupSubsidyDecodeParam(s, 1);
|
|
|
|
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
|
|
|
{
|
2009-10-17 14:29:10 +00:00
|
|
|
this->vscroll.SetCapacity(this->GetWidget<NWidgetBase>(SLW_PANEL)->current_y / this->resize.step_height);
|
2009-09-01 20:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnInvalidateData(int data)
|
|
|
|
{
|
2009-09-02 08:40:31 +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),
|
|
|
|
NWidget(WWT_CLOSEBOX, COLOUR_BROWN, SLW_CLOSEBOX),
|
2009-04-21 23:40:56 +00:00
|
|
|
NWidget(WWT_CAPTION, COLOUR_BROWN, SLW_CAPTION), SetDataTip(STR_SUBSIDIES_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
|
2009-04-17 20:03:44 +00:00
|
|
|
NWidget(WWT_STICKYBOX, COLOUR_BROWN, SLW_STICKYBOX),
|
|
|
|
EndContainer(),
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
2009-09-01 20:06:10 +00:00
|
|
|
NWidget(WWT_PANEL, COLOUR_BROWN, SLW_PANEL), SetDataTip(0x0, STR_SUBSIDIES_TOOLTIP_CLICK_ON_SERVICE_TO_CENTER), SetResize(1, 1), EndContainer(),
|
2009-04-17 20:03:44 +00:00
|
|
|
NWidget(NWID_VERTICAL),
|
|
|
|
NWidget(WWT_SCROLLBAR, COLOUR_BROWN, SLW_SCROLLBAR),
|
|
|
|
NWidget(WWT_RESIZEBOX, COLOUR_BROWN, SLW_RESIZEBOX),
|
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
|
|
|
};
|
|
|
|
|
2009-03-15 15:12:06 +00:00
|
|
|
static const WindowDesc _subsidies_list_desc(
|
2009-11-17 19:16:48 +00:00
|
|
|
WDP_AUTO, WDP_AUTO, 500, 127,
|
2007-02-01 15:49:12 +00:00
|
|
|
WC_SUBSIDIES_LIST, WC_NONE,
|
2008-04-04 02:24:44 +00:00
|
|
|
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON | WDF_RESIZABLE,
|
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
|
|
|
}
|