Let HandleWindowDragging return a boolean status
[openttd/fttd.git] / src / goal_gui.cpp
blobf873dfaeb168d886c5936893d62a2ea1cbd63a5f
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 (const WindowDesc *desc, WindowNumber window_number)
42 : Window (desc), vscroll (NULL)
44 this->CreateNestedTree();
45 this->vscroll = this->GetScrollbar(WID_GOAL_SCROLLBAR);
46 this->InitNested(window_number);
47 this->owner = (Owner)this->window_number;
48 this->OnInvalidateData(0);
51 /* virtual */ void SetStringParameters(int widget) const
53 if (widget != WID_GOAL_CAPTION) return;
55 if (this->window_number == INVALID_COMPANY) {
56 SetDParam(0, STR_GOALS_SPECTATOR_CAPTION);
57 } else {
58 SetDParam(0, STR_GOALS_CAPTION);
59 SetDParam(1, this->window_number);
63 /* virtual */ void OnClick(Point pt, int widget, int click_count)
65 if (widget != WID_GOAL_LIST) return;
67 int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GOAL_LIST, WD_FRAMERECT_TOP);
68 int num = 0;
69 const Goal *s;
70 FOR_ALL_GOALS(s) {
71 if (s->company == INVALID_COMPANY) {
72 y--;
73 if (y == 0) {
74 this->HandleClick(s);
75 return;
77 num++;
81 if (num == 0) {
82 y--; // "None" line.
83 if (y < 0) return;
86 y -= 2; // "Company specific goals:" line.
87 if (y < 0) return;
89 FOR_ALL_GOALS(s) {
90 if (s->company == this->window_number) {
91 y--;
92 if (y == 0) {
93 this->HandleClick(s);
94 return;
101 * Handle clicking at a goal.
102 * @param s @Goal clicked at.
104 void HandleClick(const Goal *s)
106 /* Determine dst coordinate for goal and try to scroll to it. */
107 TileIndex xy;
108 switch (s->type) {
109 case GT_NONE: return;
110 case GT_COMPANY: return;
112 case GT_TILE:
113 if (!IsValidTile(s->dst)) return;
114 xy = s->dst;
115 break;
117 case GT_INDUSTRY:
118 if (!Industry::IsValidID(s->dst)) return;
119 xy = Industry::Get(s->dst)->location.tile;
120 break;
122 case GT_TOWN:
123 if (!Town::IsValidID(s->dst)) return;
124 xy = Town::Get(s->dst)->xy;
125 break;
127 case GT_STORY_PAGE: {
128 if (!StoryPage::IsValidID(s->dst)) return;
130 /* Verify that:
131 * - if global goal: story page must be global.
132 * - if company goal: story page must be global or of the same company.
134 CompanyID goal_company = s->company;
135 CompanyID story_company = StoryPage::Get(s->dst)->company;
136 if (goal_company == INVALID_COMPANY ? story_company != INVALID_COMPANY : story_company != INVALID_COMPANY && story_company != goal_company) return;
138 ShowStoryBook((CompanyID)this->window_number, s->dst);
139 return;
142 default: NOT_REACHED();
145 if (_ctrl_pressed) {
146 ShowExtraViewPortWindow(xy);
147 } else {
148 ScrollMainWindowToTile(xy);
153 * Count the number of lines in this window.
154 * @return the number of lines.
156 uint CountLines()
158 /* Count number of (non) awarded goals. */
159 uint num_global = 0;
160 uint num_company = 0;
161 const Goal *s;
162 FOR_ALL_GOALS(s) {
163 if (s->company == INVALID_COMPANY) {
164 num_global++;
165 } else if (s->company == this->window_number) {
166 num_company++;
170 /* Count the 'none' lines. */
171 if (num_global == 0) num_global = 1;
172 if (num_company == 0) num_company = 1;
174 /* Global, company and an empty line before the accepted ones. */
175 return 3 + num_global + num_company;
178 /* virtual */ void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
180 if (widget != WID_GOAL_LIST) return;
181 Dimension d = maxdim(GetStringBoundingBox(STR_GOALS_GLOBAL_TITLE), GetStringBoundingBox(STR_GOALS_COMPANY_TITLE));
183 resize->height = d.height;
185 d.height *= 5;
186 d.width += padding.width + WD_FRAMERECT_RIGHT + WD_FRAMERECT_LEFT;
187 d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
188 *size = maxdim(*size, d);
192 * Draws either the global goals or the company goal section.
193 * This is a helper method for #DrawWidget.
194 * @param dpi Area to draw on.
195 * @param pos [inout] Vertical line number to draw.
196 * @param cap Number of lines to draw in the window.
197 * @param x Left edge of the text line to draw.
198 * @param y Vertical position of the top edge of the window.
199 * @param right Right edge of the text line to draw.
200 * @param global_section Whether the global goals are printed.
201 * @param column Which column to draw.
203 void DrawPartialGoalList (BlitArea *dpi, int &pos, const int cap, int x, int y, int right, uint progress_col_width, bool global_section, GoalColumn column) const
205 if (column == GC_GOAL && IsInsideMM(pos, 0, cap)) {
206 DrawString (dpi, x, right, y + pos * FONT_HEIGHT_NORMAL, global_section ? STR_GOALS_GLOBAL_TITLE : STR_GOALS_COMPANY_TITLE);
208 pos++;
210 bool rtl = _current_text_dir == TD_RTL;
212 uint num = 0;
213 const Goal *s;
214 FOR_ALL_GOALS(s) {
215 if (global_section ? s->company == INVALID_COMPANY : (s->company == this->window_number && s->company != INVALID_COMPANY)) {
216 if (IsInsideMM(pos, 0, cap)) {
217 switch (column) {
218 case GC_GOAL: {
219 /* Display the goal. */
220 SetDParamStr(0, s->text);
221 uint width_reduction = progress_col_width > 0 ? progress_col_width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT : 0;
222 DrawString (dpi, x + (rtl ? width_reduction : 0), right - (rtl ? 0 : width_reduction), y + pos * FONT_HEIGHT_NORMAL, STR_GOALS_TEXT);
223 break;
226 case GC_PROGRESS:
227 if (s->progress != NULL) {
228 SetDParamStr(0, s->progress);
229 StringID str = s->completed ? STR_GOALS_PROGRESS_COMPLETE : STR_GOALS_PROGRESS;
230 int progress_x = x;
231 int progress_right = rtl ? x + progress_col_width : right;
232 DrawString (dpi, progress_x, progress_right, y + pos * FONT_HEIGHT_NORMAL, str, TC_FROMSTRING, SA_RIGHT | SA_FORCE);
234 break;
237 pos++;
238 num++;
242 if (num == 0) {
243 if (column == GC_GOAL && IsInsideMM(pos, 0, cap)) {
244 StringID str = !global_section && this->window_number == INVALID_COMPANY ? STR_GOALS_SPECTATOR_NONE : STR_GOALS_NONE;
245 DrawString (dpi, x, right, y + pos * FONT_HEIGHT_NORMAL, str);
247 pos++;
252 * Draws a given column of the goal list.
253 * @param dpi Area to draw on.
254 * @param column Which column to draw.
255 * @wid Pointer to the goal list widget.
256 * @progress_col_width Width of the progress column.
257 * @return max width of drawn text
259 void DrawListColumn (BlitArea *dpi, GoalColumn column, NWidgetBase *wid, uint progress_col_width) const
261 /* Get column draw area. */
262 int y = wid->pos_y + WD_FRAMERECT_TOP;
263 int x = wid->pos_x + WD_FRAMERECT_LEFT;
264 int right = x + wid->current_x - WD_FRAMERECT_RIGHT;
266 int pos = -this->vscroll->GetPosition();
267 const int cap = this->vscroll->GetCapacity();
269 /* Draw partial list with global goals. */
270 DrawPartialGoalList (dpi, pos, cap, x, y, right, progress_col_width, true, column);
272 /* Draw partial list with company goals. */
273 pos++;
274 DrawPartialGoalList (dpi, pos, cap, x, y, right, progress_col_width, false, column);
277 void OnPaint (BlitArea *dpi) OVERRIDE
279 this->DrawWidgets (dpi);
281 if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
283 /* Calculate progress column width. */
284 uint max_width = 0;
285 Goal *s;
286 FOR_ALL_GOALS(s) {
287 if (s->progress != NULL) {
288 SetDParamStr(0, s->progress);
289 StringID str = s->completed ? STR_GOALS_PROGRESS_COMPLETE : STR_GOALS_PROGRESS;
290 uint str_width = GetStringBoundingBox(str).width;
291 if (str_width > max_width) max_width = str_width;
295 NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_GOAL_LIST);
296 uint progress_col_width = min(max_width, wid->current_x);
298 /* Draw goal list. */
299 this->DrawListColumn (dpi, GC_PROGRESS, wid, progress_col_width);
300 this->DrawListColumn (dpi, GC_GOAL, wid, progress_col_width);
304 /* virtual */ void OnResize()
306 this->vscroll->SetCapacityFromWidget(this, WID_GOAL_LIST);
310 * Some data on this window has become invalid.
311 * @param data Information about the changed data.
312 * @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.
314 /* virtual */ void OnInvalidateData(int data = 0, bool gui_scope = true)
316 if (!gui_scope) return;
317 this->vscroll->SetCount(this->CountLines());
318 this->SetWidgetDirty(WID_GOAL_LIST);
322 /** Widgets of the #GoalListWindow. */
323 static const NWidgetPart _nested_goals_list_widgets[] = {
324 NWidget(NWID_HORIZONTAL),
325 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
326 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_GOAL_CAPTION), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
327 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
328 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
329 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
330 EndContainer(),
331 NWidget(NWID_HORIZONTAL),
332 NWidget(WWT_PANEL, COLOUR_BROWN), SetDataTip(0x0, STR_GOALS_TOOLTIP_CLICK_ON_SERVICE_TO_CENTER), SetScrollbar(WID_GOAL_SCROLLBAR),
333 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),
334 EndContainer(),
335 NWidget(NWID_VERTICAL),
336 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_GOAL_SCROLLBAR),
337 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
338 EndContainer(),
339 EndContainer(),
342 static WindowDesc::Prefs _goals_list_prefs ("list_goals");
344 static const WindowDesc _goals_list_desc(
345 WDP_AUTO, 500, 127,
346 WC_GOALS_LIST, WC_NONE,
348 _nested_goals_list_widgets, lengthof(_nested_goals_list_widgets),
349 &_goals_list_prefs
353 * Open a goal list window.
354 * @param company %Company to display the goals for, use #INVALID_COMPANY to display global goals.
356 void ShowGoalsList(CompanyID company)
358 if (!Company::IsValidID(company)) company = (CompanyID)INVALID_COMPANY;
360 AllocateWindowDescFront<GoalListWindow>(&_goals_list_desc, company);
363 /** Ask a question about a goal. */
364 struct GoalQuestionWindow : public Window {
365 char *question; ///< Question to ask (private copy).
366 int buttons; ///< Number of valid buttons in #button.
367 int button[3]; ///< Buttons to display.
368 byte type; ///< Type of question.
370 GoalQuestionWindow (const WindowDesc *desc, WindowNumber window_number, byte type, uint32 button_mask, const char *question)
371 : Window (desc), question (xstrdup (question)), type (type)
373 assert(type < GOAL_QUESTION_TYPE_COUNT);
375 /* Figure out which buttons we have to enable. */
376 uint bit;
377 int n = 0;
378 FOR_EACH_SET_BIT(bit, button_mask) {
379 if (bit >= GOAL_QUESTION_BUTTON_COUNT) break;
380 this->button[n++] = bit;
381 if (n == 3) break;
383 this->buttons = n;
384 assert(this->buttons > 0 && this->buttons < 4);
386 this->CreateNestedTree();
387 this->GetWidget<NWidgetStacked>(WID_GQ_BUTTONS)->SetDisplayedPlane(this->buttons - 1);
388 this->InitNested(window_number);
391 ~GoalQuestionWindow()
393 free(this->question);
396 /* virtual */ void SetStringParameters(int widget) const
398 switch (widget) {
399 case WID_GQ_CAPTION:
400 SetDParam(0, STR_GOAL_QUESTION_CAPTION_QUESTION + this->type);
401 break;
403 case WID_GQ_BUTTON_1:
404 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[0]);
405 break;
407 case WID_GQ_BUTTON_2:
408 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[1]);
409 break;
411 case WID_GQ_BUTTON_3:
412 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[2]);
413 break;
417 /* virtual */ void OnClick(Point pt, int widget, int click_count)
419 switch (widget) {
420 case WID_GQ_BUTTON_1:
421 DoCommandP(0, this->window_number, this->button[0], CMD_GOAL_QUESTION_ANSWER);
422 this->Delete();
423 break;
425 case WID_GQ_BUTTON_2:
426 DoCommandP(0, this->window_number, this->button[1], CMD_GOAL_QUESTION_ANSWER);
427 this->Delete();
428 break;
430 case WID_GQ_BUTTON_3:
431 DoCommandP(0, this->window_number, this->button[2], CMD_GOAL_QUESTION_ANSWER);
432 this->Delete();
433 break;
437 /* virtual */ void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
439 if (widget != WID_GQ_QUESTION) return;
441 SetDParamStr(0, this->question);
442 size->height = GetStringHeight(STR_JUST_RAW_STRING, size->width) + WD_PAR_VSEP_WIDE;
445 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
447 if (widget != WID_GQ_QUESTION) return;
449 SetDParamStr(0, this->question);
450 DrawStringMultiLine (dpi, r.left, r.right, r.top, UINT16_MAX, STR_JUST_RAW_STRING, TC_BLACK, SA_TOP | SA_HOR_CENTER);
454 /** Widgets of the goal question window. */
455 static const NWidgetPart _nested_goal_question_widgets[] = {
456 NWidget(NWID_HORIZONTAL),
457 NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
458 NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE, WID_GQ_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
459 EndContainer(),
460 NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE),
461 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GQ_QUESTION), SetMinimalSize(300, 0), SetPadding(8, 8, 8, 8), SetFill(1, 0),
462 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_GQ_BUTTONS),
463 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(85, 10, 85),
464 NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_1), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
465 EndContainer(),
466 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(65, 10, 65),
467 NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_1), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
468 NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_2), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
469 EndContainer(),
470 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(25, 10, 25),
471 NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_1), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
472 NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_2), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
473 NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_3), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
474 EndContainer(),
475 EndContainer(),
476 NWidget(NWID_SPACER), SetMinimalSize(0, 8),
477 EndContainer(),
480 static const WindowDesc _goal_question_list_desc(
481 WDP_CENTER, 0, 0,
482 WC_GOAL_QUESTION, WC_NONE,
483 WDF_CONSTRUCTION,
484 _nested_goal_question_widgets, lengthof(_nested_goal_question_widgets)
488 * Display a goal question.
489 * @param id Window number to use.
490 * @param type Type of question.
491 * @param button_mask Buttons to display.
492 * @param question Question to ask.
494 void ShowGoalQuestion(uint16 id, byte type, uint32 button_mask, const char *question)
496 new GoalQuestionWindow(&_goal_question_list_desc, id, type, button_mask, question);