summaryrefslogtreecommitdiff
path: root/src/cargodest_gui.cpp
blob: bf5fdd1d4c50727b9dd90a96785552eaa27c5841 (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
/* $Id$ */

/*
 * 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/>.
 */

/** @file cargodest_gui.cpp GUI for cargo destinations. */

#include "stdafx.h"
#include "window_gui.h"
#include "gfx_func.h"
#include "strings_func.h"
#include "cargodest_base.h"
#include "cargodest_gui.h"
#include "town.h"
#include "industry.h"
#include "string_func.h"
#include "gui.h"
#include "viewport_func.h"

#include "table/strings.h"

static const CargoSourceSink *_cur_cargo_source;

int CDECL CargoLinkSorter(const GUICargoLink *a, const GUICargoLink *b)
{
	/* Sort by cargo type. */
	if (a->cid != b->cid) return a->cid < b->cid ? -1 : +1;

	/* Sort unspecified destination links always last. */
	if (a->link->dest == NULL) return +1;
	if (b->link->dest == NULL) return -1;

	/* Sort link with the current source as destination first. */
	if (a->link->dest == _cur_cargo_source) return -1;
	if (b->link->dest == _cur_cargo_source) return +1;

	/* Sort towns before industries. */
	if (a->link->dest->GetType() != b->link->dest->GetType()) {
		return a->link->dest->GetType() < b->link->dest->GetType() ? +1 : -1;
	}

	/* Sort by name. */
	static const CargoLink *last_b = NULL;
	static char last_name[128];

	char name[128];
	SetDParam(0, a->link->dest->GetID());
	GetString(name, a->link->dest->GetType() == ST_TOWN ? STR_TOWN_NAME : STR_INDUSTRY_NAME, lastof(name));

	/* Cache name lookup of 'b', as the sorter is often called
	 * multiple times with the same 'b'. */
	if (b->link != last_b) {
		last_b = b->link;

		SetDParam(0, b->link->dest->GetID());
		GetString(last_name, b->link->dest->GetType() == ST_TOWN ? STR_TOWN_NAME : STR_INDUSTRY_NAME, lastof(last_name));
	}

	return strcmp(name, last_name);
}

CargoDestinationList::CargoDestinationList(const CargoSourceSink *o) : obj(o)
{
	this->InvalidateData();
}

/** Rebuild the link list from the source object. */
void CargoDestinationList::RebuildList()
{
	if (!this->link_list.NeedRebuild()) return;

	this->link_list.Clear();
	for (CargoID i = 0; i < lengthof(this->obj->cargo_links); i++) {
		for (const CargoLink *l = this->obj->cargo_links[i].Begin(); l != this->obj->cargo_links[i].End(); l++) {
			*this->link_list.Append() = GUICargoLink(i, l);
		}
	}

	this->link_list.Compact();
	this->link_list.RebuildDone();
}

/** Sort the link list. */
void CargoDestinationList::SortList()
{
	_cur_cargo_source = this->obj;
	this->link_list.Sort(&CargoLinkSorter);
}

/** Rebuild the list, e.g. when a new cargo link was added. */
void CargoDestinationList::InvalidateData()
{
	this->link_list.ForceRebuild();
	this->RebuildList();
	this->SortList();
}

/** Resort the list, e.g. when a town is renamed. */
void CargoDestinationList::Resort()
{
	this->link_list.ForceResort();
	this->SortList();
}

/**
 * Get the height needed to display the destination list.
 * @param obj Object to display the destinations of.
 * @return Height needed for display.
 */
uint CargoDestinationList::GetListHeight() const
{
	uint lines = 2 + this->link_list.Length();
	return lines > 2 ? WD_PAR_VSEP_WIDE + lines * FONT_HEIGHT_NORMAL : 0;
}

/**
 * Draw the destination list.
 * @param left The left most position to draw on.
 * @param right The right most position to draw on.
 * @param y The top position to start drawing.
 * @return New \c y value below the drawn text.
 */
uint CargoDestinationList::DrawList(uint left, uint right, uint y) const
{
	if (this->link_list.Length() == 0) return y;

	DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y += WD_PAR_VSEP_WIDE + FONT_HEIGHT_NORMAL, STR_VIEW_CARGO_LAST_MONTH_OUT);

	for (const GUICargoLink *l = this->link_list.Begin(); l != this->link_list.End(); l++) {
		SetDParam(0, l->cid);
		SetDParam(1, l->link->amount.old_act);
		SetDParam(2, l->cid);
		SetDParam(3, l->link->amount.old_max);

		/* Select string according to the destination type. */
		if (l->link->dest == NULL) {
			DrawString(left + 2 * WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y += FONT_HEIGHT_NORMAL, STR_VIEW_CARGO_LAST_MONTH_OTHER);
		} else if (l->link->dest == obj) {
			DrawString(left + 2 * WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y += FONT_HEIGHT_NORMAL, STR_VIEW_CARGO_LAST_MONTH_LOCAL);
		} else {
			SetDParam(4, l->link->dest->GetID());
			DrawString(left + 2 * WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, y += FONT_HEIGHT_NORMAL, l->link->dest->GetType() == ST_TOWN ? STR_VIEW_CARGO_LAST_MONTH_TOWN : STR_VIEW_CARGO_LAST_MONTH_INDUSTRY);
		}
	}

	return y + FONT_HEIGHT_NORMAL;
}

/**
 * Handle click event onto the destination list.
 * @param y Position of the click in relative to the top of the destination list.
 */
void CargoDestinationList::OnClick(uint y) const
{
	/* Subtract caption height. */
	y -= WD_PAR_VSEP_WIDE + 2 * FONT_HEIGHT_NORMAL;

	/* Calculate line from click pos. */
	y /= FONT_HEIGHT_NORMAL;
	if (y >= this->link_list.Length()) return;

	/* Move viewpoint to the position of the destination. */
	const CargoLink *l = this->link_list[y].link;
	if (l->dest == NULL) return;

	TileIndex xy = l->dest->GetType() == ST_TOWN ? static_cast<const Town *>(l->dest)->xy : static_cast<const Industry *>(l->dest)->location.tile;
	if (_ctrl_pressed) {
		ShowExtraViewPortWindow(xy);
	} else {
		ScrollMainWindowToTile(xy);
	}
}