Add support for deleted functions
[openttd/fttd.git] / src / goal_gui.cpp
blobcaa04e544cb0c278ce3672754e3beed3c81e7723
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file goal_gui.cpp GUI for goals. */
12 #include "stdafx.h"
13 #include "industry.h"
14 #include "town.h"
15 #include "window_gui.h"
16 #include "strings_func.h"
17 #include "date_func.h"
18 #include "viewport_func.h"
19 #include "gui.h"
20 #include "goal_base.h"
21 #include "core/geometry_func.hpp"
22 #include "company_func.h"
23 #include "company_base.h"
24 #include "story_base.h"
25 #include "command_func.h"
27 #include "widgets/goal_widget.h"
29 #include "table/strings.h"
31 /** Goal list columns. */
32 enum GoalColumn {
33 GC_GOAL = 0, ///< Goal text column.
34 GC_PROGRESS, ///< Goal progress column.
37 /** Window for displaying goals. */
38 struct GoalListWindow : public Window {
39 Scrollbar *vscroll; ///< Reference to the scrollbar widget.
41 GoalListWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
43 this->CreateNestedTree();
44 this->vscroll = this->GetScrollbar(WID_GOAL_SCROLLBAR);
45 this->FinishInitNested(window_number);
46 this->owner = (Owner)this->window_number;
47 this->OnInvalidateData(0);
50 /* virtual */ void SetStringParameters(int widget) const
52 if (widget != WID_GOAL_CAPTION) return;
54 if (this->window_number == INVALID_COMPANY) {
55 SetDParam(0, STR_GOALS_SPECTATOR_CAPTION);
56 } else {
57 SetDParam(0, STR_GOALS_CAPTION);
58 SetDParam(1, this->window_number);
62 /* virtual */ void OnClick(Point pt, int widget, int click_count)
64 if (widget != WID_GOAL_LIST) return;
66 int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GOAL_LIST, WD_FRAMERECT_TOP);
67 int num = 0;
68 const Goal *s;
69 FOR_ALL_GOALS(s) {
70 if (s->company == INVALID_COMPANY) {
71 y--;
72 if (y == 0) {
73 this->HandleClick(s);
74 return;
76 num++;
80 if (num == 0) {
81 y--; // "None" line.
82 if (y < 0) return;
85 y -= 2; // "Company specific goals:" line.
86 if (y < 0) return;
88 FOR_ALL_GOALS(s) {
89 if (s->company == this->window_number) {
90 y--;
91 if (y == 0) {
92 this->HandleClick(s);
93 return;
99 /**
100 * Handle clicking at a goal.
101 * @param s @Goal clicked at.
103 void HandleClick(const Goal *s)
105 /* Determine dst coordinate for goal and try to scroll to it. */
106 TileIndex xy;
107 switch (s->type) {
108 case GT_NONE: return;
109 case GT_COMPANY: return;
111 case GT_TILE:
112 if (!IsValidTile(s->dst)) return;
113 xy = s->dst;
114 break;
116 case GT_INDUSTRY:
117 if (!Industry::IsValidID(s->dst)) return;
118 xy = Industry::Get(s->dst)->location.tile;
119 break;
121 case GT_TOWN:
122 if (!Town::IsValidID(s->dst)) return;
123 xy = Town::Get(s->dst)->xy;
124 break;
126 case GT_STORY_PAGE: {
127 if (!StoryPage::IsValidID(s->dst)) return;
129 /* Verify that:
130 * - if global goal: story page must be global.
131 * - if company goal: story page must be global or of the same company.
133 CompanyID goal_company = s->company;
134 CompanyID story_company = StoryPage::Get(s->dst)->company;
135 if (goal_company == INVALID_COMPANY ? story_company != INVALID_COMPANY : story_company != INVALID_COMPANY && story_company != goal_company) return;
137 ShowStoryBook((CompanyID)this->window_number, s->dst);
138 return;
141 default: NOT_REACHED();
144 if (_ctrl_pressed) {
145 ShowExtraViewPortWindow(xy);
146 } else {
147 ScrollMainWindowToTile(xy);
152 * Count the number of lines in this window.
153 * @return the number of lines.
155 uint CountLines()
157 /* Count number of (non) awarded goals. */
158 uint num_global = 0;
159 uint num_company = 0;
160 const Goal *s;
161 FOR_ALL_GOALS(s) {
162 if (s->company == INVALID_COMPANY) {
163 num_global++;
164 } else if (s->company == this->window_number) {
165 num_company++;
169 /* Count the 'none' lines. */
170 if (num_global == 0) num_global = 1;
171 if (num_company == 0) num_company = 1;
173 /* Global, company and an empty line before the accepted ones. */
174 return 3 + num_global + num_company;
177 /* virtual */ void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
179 if (widget != WID_GOAL_LIST) return;
180 Dimension d = maxdim(GetStringBoundingBox(STR_GOALS_GLOBAL_TITLE), GetStringBoundingBox(STR_GOALS_COMPANY_TITLE));
182 resize->height = d.height;
184 d.height *= 5;
185 d.width += padding.width + WD_FRAMERECT_RIGHT + WD_FRAMERECT_LEFT;
186 d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
187 *size = maxdim(*size, d);
191 * Draws either the global goals or the company goal section.
192 * This is a helper method for #DrawWidget.
193 * @param pos [inout] Vertical line number to draw.
194 * @param cap Number of lines to draw in the window.
195 * @param x Left edge of the text line to draw.
196 * @param y Vertical position of the top edge of the window.
197 * @param right Right edge of the text line to draw.
198 * @param global_section Whether the global goals are printed.
199 * @param column Which column to draw.
201 void DrawPartialGoalList(int &pos, const int cap, int x, int y, int right, uint progress_col_width, bool global_section, GoalColumn column) const
203 if (column == GC_GOAL && IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, global_section ? STR_GOALS_GLOBAL_TITLE : STR_GOALS_COMPANY_TITLE);
204 pos++;
206 bool rtl = _current_text_dir == TD_RTL;
208 uint num = 0;
209 const Goal *s;
210 FOR_ALL_GOALS(s) {
211 if (global_section ? s->company == INVALID_COMPANY : (s->company == this->window_number && s->company != INVALID_COMPANY)) {
212 if (IsInsideMM(pos, 0, cap)) {
213 switch (column) {
214 case GC_GOAL: {
215 /* Display the goal. */
216 SetDParamStr(0, s->text);
217 uint width_reduction = progress_col_width > 0 ? progress_col_width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT : 0;
218 DrawString(x + (rtl ? width_reduction : 0), right - (rtl ? 0 : width_reduction), y + pos * FONT_HEIGHT_NORMAL, STR_GOALS_TEXT);
219 break;
222 case GC_PROGRESS:
223 if (s->progress != NULL) {
224 SetDParamStr(0, s->progress);
225 StringID str = s->completed ? STR_GOALS_PROGRESS_COMPLETE : STR_GOALS_PROGRESS;
226 int progress_x = x;
227 int progress_right = rtl ? x + progress_col_width : right;
228 DrawString(progress_x, progress_right, y + pos * FONT_HEIGHT_NORMAL, str, TC_FROMSTRING, SA_RIGHT | SA_FORCE);
230 break;
233 pos++;
234 num++;
238 if (num == 0) {
239 if (column == GC_GOAL && IsInsideMM(pos, 0, cap)) {
240 StringID str = !global_section && this->window_number == INVALID_COMPANY ? STR_GOALS_SPECTATOR_NONE : STR_GOALS_NONE;
241 DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, str);
243 pos++;
248 * Draws a given column of the goal list.
249 * @param column Which column to draw.
250 * @wid Pointer to the goal list widget.
251 * @progress_col_width Width of the progress column.
252 * @return max width of drawn text
254 void DrawListColumn(GoalColumn column, NWidgetBase *wid, uint progress_col_width) const
256 /* Get column draw area. */
257 int y = wid->pos_y + WD_FRAMERECT_TOP;
258 int x = wid->pos_x + WD_FRAMERECT_LEFT;
259 int right = x + wid->current_x - WD_FRAMERECT_RIGHT;
261 int pos = -this->vscroll->GetPosition();
262 const int cap = this->vscroll->GetCapacity();
264 /* Draw partial list with global goals. */
265 DrawPartialGoalList(pos, cap, x, y, right, progress_col_width, true, column);
267 /* Draw partial list with company goals. */
268 pos++;
269 DrawPartialGoalList(pos, cap, x, y, right, progress_col_width, false, column);
272 /* virtual */ void OnPaint()
274 this->DrawWidgets();
276 /* Calculate progress column width. */
277 uint max_width = 0;
278 Goal *s;
279 FOR_ALL_GOALS(s) {
280 if (s->progress != NULL) {
281 SetDParamStr(0, s->progress);
282 StringID str = s->completed ? STR_GOALS_PROGRESS_COMPLETE : STR_GOALS_PROGRESS;
283 uint str_width = GetStringBoundingBox(str).width;
284 if (str_width > max_width) max_width = str_width;
288 NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_GOAL_LIST);
289 uint progress_col_width = min(max_width, wid->current_x);
291 /* Draw goal list. */
292 this->DrawListColumn(GC_PROGRESS, wid, progress_col_width);
293 this->DrawListColumn(GC_GOAL, wid, progress_col_width);
297 /* virtual */ void OnResize()
299 this->vscroll->SetCapacityFromWidget(this, WID_GOAL_LIST);
303 * Some data on this window has become invalid.
304 * @param data Information about the changed data.
305 * @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.
307 /* virtual */ void OnInvalidateData(int data = 0, bool gui_scope = true)
309 if (!gui_scope) return;
310 this->vscroll->SetCount(this->CountLines());
311 this->SetWidgetDirty(WID_GOAL_LIST);
315 /** Widgets of the #GoalListWindow. */
316 static const NWidgetPart _nested_goals_list_widgets[] = {
317 NWidget(NWID_HORIZONTAL),
318 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
319 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_GOAL_CAPTION), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
320 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
321 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
322 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
323 EndContainer(),
324 NWidget(NWID_HORIZONTAL),
325 NWidget(WWT_PANEL, COLOUR_BROWN), SetDataTip(0x0, STR_GOALS_TOOLTIP_CLICK_ON_SERVICE_TO_CENTER), SetScrollbar(WID_GOAL_SCROLLBAR),
326 NWidget(WWT_EMPTY, COLOUR_GREY, WID_GOAL_LIST), SetResize(1, 1), SetMinimalTextLines(2, 0), SetFill(1, 1), SetPadding(WD_FRAMERECT_TOP, 2, WD_FRAMETEXT_BOTTOM, 2),
327 EndContainer(),
328 NWidget(NWID_VERTICAL),
329 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_GOAL_SCROLLBAR),
330 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
331 EndContainer(),
332 EndContainer(),
335 static WindowDesc _goals_list_desc(
336 WDP_AUTO, "list_goals", 500, 127,
337 WC_GOALS_LIST, WC_NONE,
339 _nested_goals_list_widgets, lengthof(_nested_goals_list_widgets)
343 * Open a goal list window.
344 * @param company %Company to display the goals for, use #INVALID_COMPANY to display global goals.
346 void ShowGoalsList(CompanyID company)
348 if (!Company::IsValidID(company)) company = (CompanyID)INVALID_COMPANY;
350 AllocateWindowDescFront<GoalListWindow>(&_goals_list_desc, company);
353 /** Ask a question about a goal. */
354 struct GoalQuestionWindow : public Window {
355 char *question; ///< Question to ask (private copy).
356 int buttons; ///< Number of valid buttons in #button.
357 int button[3]; ///< Buttons to display.
358 byte type; ///< Type of question.
360 GoalQuestionWindow(WindowDesc *desc, WindowNumber window_number, byte type, uint32 button_mask, const char *question) : Window(desc), type(type)
362 assert(type < GOAL_QUESTION_TYPE_COUNT);
363 this->question = strdup(question);
365 /* Figure out which buttons we have to enable. */
366 uint bit;
367 int n = 0;
368 FOR_EACH_SET_BIT(bit, button_mask) {
369 if (bit >= GOAL_QUESTION_BUTTON_COUNT) break;
370 this->button[n++] = bit;
371 if (n == 3) break;
373 this->buttons = n;
374 assert(this->buttons > 0 && this->buttons < 4);
376 this->CreateNestedTree();
377 this->GetWidget<NWidgetStacked>(WID_GQ_BUTTONS)->SetDisplayedPlane(this->buttons - 1);
378 this->FinishInitNested(window_number);
381 ~GoalQuestionWindow()
383 free(this->question);
386 /* virtual */ void SetStringParameters(int widget) const
388 switch (widget) {
389 case WID_GQ_CAPTION:
390 SetDParam(0, STR_GOAL_QUESTION_CAPTION_QUESTION + this->type);
391 break;
393 case WID_GQ_BUTTON_1:
394 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[0]);
395 break;
397 case WID_GQ_BUTTON_2:
398 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[1]);
399 break;
401 case WID_GQ_BUTTON_3:
402 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[2]);
403 break;
407 /* virtual */ void OnClick(Point pt, int widget, int click_count)
409 switch (widget) {
410 case WID_GQ_BUTTON_1:
411 DoCommandP(0, this->window_number, this->button[0], CMD_GOAL_QUESTION_ANSWER);
412 delete this;
413 break;
415 case WID_GQ_BUTTON_2:
416 DoCommandP(0, this->window_number, this->button[1], CMD_GOAL_QUESTION_ANSWER);
417 delete this;
418 break;
420 case WID_GQ_BUTTON_3:
421 DoCommandP(0, this->window_number, this->button[2], CMD_GOAL_QUESTION_ANSWER);
422 delete this;
423 break;
427 /* virtual */ void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
429 if (widget != WID_GQ_QUESTION) return;
431 SetDParamStr(0, this->question);
432 size->height = GetStringHeight(STR_JUST_RAW_STRING, size->width) + WD_PAR_VSEP_WIDE;
435 /* virtual */ void DrawWidget(const Rect &r, int widget) const
437 if (widget != WID_GQ_QUESTION) return;
439 SetDParamStr(0, this->question);
440 DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_JUST_RAW_STRING, TC_BLACK, SA_TOP | SA_HOR_CENTER);
444 /** Widgets of the goal question window. */
445 static const NWidgetPart _nested_goal_question_widgets[] = {
446 NWidget(NWID_HORIZONTAL),
447 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
448 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE, WID_GQ_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
449 EndContainer(),
450 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE),
451 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GQ_QUESTION), SetMinimalSize(300, 0), SetPadding(8, 8, 8, 8), SetFill(1, 0),
452 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_GQ_BUTTONS),
453 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(85, 10, 85),
454 NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_1), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
455 EndContainer(),
456 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(65, 10, 65),
457 NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_1), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
458 NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_2), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
459 EndContainer(),
460 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(25, 10, 25),
461 NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_1), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
462 NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_2), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
463 NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_3), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
464 EndContainer(),
465 EndContainer(),
466 NWidget(NWID_SPACER), SetMinimalSize(0, 8),
467 EndContainer(),
470 static WindowDesc _goal_question_list_desc(
471 WDP_CENTER, NULL, 0, 0,
472 WC_GOAL_QUESTION, WC_NONE,
473 WDF_CONSTRUCTION,
474 _nested_goal_question_widgets, lengthof(_nested_goal_question_widgets)
478 * Display a goal question.
479 * @param id Window number to use.
480 * @param type Type of question.
481 * @param button_mask Buttons to display.
482 * @param question Question to ask.
484 void ShowGoalQuestion(uint16 id, byte type, uint32 button_mask, const char *question)
486 new GoalQuestionWindow(&_goal_question_list_desc, id, type, button_mask, question);