2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 /** @file goal_gui.cpp GUI for goals. */
13 #include "window_gui.h"
14 #include "strings_func.h"
15 #include "date_func.h"
16 #include "viewport_func.h"
18 #include "goal_base.h"
19 #include "core/geometry_func.hpp"
20 #include "company_func.h"
21 #include "company_base.h"
22 #include "company_gui.h"
23 #include "story_base.h"
24 #include "command_func.h"
25 #include "string_func.h"
27 #include "widgets/goal_widget.h"
29 #include "table/strings.h"
31 #include "safeguards.h"
33 /** Goal list columns. */
35 GC_GOAL
= 0, ///< Goal text column.
36 GC_PROGRESS
, ///< Goal progress column.
39 /** Window for displaying goals. */
40 struct GoalListWindow
: public Window
{
41 Scrollbar
*vscroll
; ///< Reference to the scrollbar widget.
43 GoalListWindow(WindowDesc
*desc
, WindowNumber window_number
) : Window(desc
)
45 this->CreateNestedTree();
46 this->vscroll
= this->GetScrollbar(WID_GOAL_SCROLLBAR
);
47 this->FinishInitNested(window_number
);
48 this->owner
= (Owner
)this->window_number
;
49 this->OnInvalidateData(0);
52 void SetStringParameters(int widget
) const override
54 if (widget
!= WID_GOAL_CAPTION
) return;
56 if (this->window_number
== INVALID_COMPANY
) {
57 SetDParam(0, STR_GOALS_SPECTATOR_CAPTION
);
59 SetDParam(0, STR_GOALS_CAPTION
);
60 SetDParam(1, this->window_number
);
64 void OnClick(Point pt
, int widget
, int click_count
) override
66 if (widget
!= WID_GOAL_LIST
) return;
68 int y
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_GOAL_LIST
, WD_FRAMERECT_TOP
);
70 for (const Goal
*s
: Goal::Iterate()) {
71 if (s
->company
== INVALID_COMPANY
) {
86 y
-= 2; // "Company specific goals:" line.
89 for (const Goal
*s
: Goal::Iterate()) {
90 if (s
->company
== this->window_number
&& s
->company
!= INVALID_COMPANY
) {
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. */
109 case GT_NONE
: return;
112 /* s->dst here is not a tile, but a CompanyID.
113 * Show the window with the overview of the company instead. */
114 ShowCompany((CompanyID
)s
->dst
);
118 if (!IsValidTile(s
->dst
)) return;
123 if (!Industry::IsValidID(s
->dst
)) return;
124 xy
= Industry::Get(s
->dst
)->location
.tile
;
128 if (!Town::IsValidID(s
->dst
)) return;
129 xy
= Town::Get(s
->dst
)->xy
;
132 case GT_STORY_PAGE
: {
133 if (!StoryPage::IsValidID(s
->dst
)) return;
136 * - if global goal: story page must be global.
137 * - if company goal: story page must be global or of the same company.
139 CompanyID goal_company
= s
->company
;
140 CompanyID story_company
= StoryPage::Get(s
->dst
)->company
;
141 if (goal_company
== INVALID_COMPANY
? story_company
!= INVALID_COMPANY
: story_company
!= INVALID_COMPANY
&& story_company
!= goal_company
) return;
143 ShowStoryBook((CompanyID
)this->window_number
, s
->dst
);
147 default: NOT_REACHED();
151 ShowExtraViewPortWindow(xy
);
153 ScrollMainWindowToTile(xy
);
158 * Count the number of lines in this window.
159 * @return the number of lines.
163 /* Count number of (non) awarded goals. */
165 uint num_company
= 0;
166 for (const Goal
*s
: Goal::Iterate()) {
167 if (s
->company
== INVALID_COMPANY
) {
169 } else if (s
->company
== this->window_number
) {
174 /* Count the 'none' lines. */
175 if (num_global
== 0) num_global
= 1;
176 if (num_company
== 0) num_company
= 1;
178 /* Global, company and an empty line before the accepted ones. */
179 return 3 + num_global
+ num_company
;
182 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
184 if (widget
!= WID_GOAL_LIST
) return;
185 Dimension d
= maxdim(GetStringBoundingBox(STR_GOALS_GLOBAL_TITLE
), GetStringBoundingBox(STR_GOALS_COMPANY_TITLE
));
187 resize
->height
= d
.height
;
190 d
.width
+= padding
.width
+ WD_FRAMERECT_RIGHT
+ WD_FRAMERECT_LEFT
;
191 d
.height
+= padding
.height
+ WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
;
192 *size
= maxdim(*size
, d
);
196 * Draws either the global goals or the company goal section.
197 * This is a helper method for #DrawWidget.
198 * @param[in,out] pos Vertical line number to draw.
199 * @param cap Number of lines to draw in the window.
200 * @param x Left edge of the text line to draw.
201 * @param y Vertical position of the top edge of the window.
202 * @param right Right edge of the text line to draw.
203 * @param global_section Whether the global goals are printed.
204 * @param column Which column to draw.
206 void DrawPartialGoalList(int &pos
, const int cap
, int x
, int y
, int right
, uint progress_col_width
, bool global_section
, GoalColumn column
) const
208 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
);
211 bool rtl
= _current_text_dir
== TD_RTL
;
214 for (const Goal
*s
: Goal::Iterate()) {
215 if (global_section
? s
->company
== INVALID_COMPANY
: (s
->company
== this->window_number
&& s
->company
!= INVALID_COMPANY
)) {
216 if (IsInsideMM(pos
, 0, cap
)) {
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(x
+ (rtl
? width_reduction
: 0), right
- (rtl
? 0 : width_reduction
), y
+ pos
* FONT_HEIGHT_NORMAL
, STR_GOALS_TEXT
);
227 if (s
->progress
!= nullptr) {
228 SetDParamStr(0, s
->progress
);
229 StringID str
= s
->completed
? STR_GOALS_PROGRESS_COMPLETE
: STR_GOALS_PROGRESS
;
231 int progress_right
= rtl
? x
+ progress_col_width
: right
;
232 DrawString(progress_x
, progress_right
, y
+ pos
* FONT_HEIGHT_NORMAL
, str
, TC_FROMSTRING
, SA_RIGHT
| SA_FORCE
);
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(x
, right
, y
+ pos
* FONT_HEIGHT_NORMAL
, str
);
252 * Draws a given column of the goal list.
253 * @param column Which column to draw.
254 * @param wid Pointer to the goal list widget.
255 * @param progress_col_width Width of the progress column.
256 * @return max width of drawn text
258 void DrawListColumn(GoalColumn column
, NWidgetBase
*wid
, uint progress_col_width
) const
260 /* Get column draw area. */
261 int y
= wid
->pos_y
+ WD_FRAMERECT_TOP
;
262 int x
= wid
->pos_x
+ WD_FRAMERECT_LEFT
;
263 int right
= x
+ wid
->current_x
- WD_FRAMERECT_RIGHT
;
265 int pos
= -this->vscroll
->GetPosition();
266 const int cap
= this->vscroll
->GetCapacity();
268 /* Draw partial list with global goals. */
269 DrawPartialGoalList(pos
, cap
, x
, y
, right
, progress_col_width
, true, column
);
271 /* Draw partial list with company goals. */
273 DrawPartialGoalList(pos
, cap
, x
, y
, right
, progress_col_width
, false, column
);
276 void OnPaint() override
280 if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
282 /* Calculate progress column width. */
284 for (const Goal
*s
: Goal::Iterate()) {
285 if (s
->progress
!= nullptr) {
286 SetDParamStr(0, s
->progress
);
287 StringID str
= s
->completed
? STR_GOALS_PROGRESS_COMPLETE
: STR_GOALS_PROGRESS
;
288 uint str_width
= GetStringBoundingBox(str
).width
;
289 if (str_width
> max_width
) max_width
= str_width
;
293 NWidgetBase
*wid
= this->GetWidget
<NWidgetBase
>(WID_GOAL_LIST
);
294 uint progress_col_width
= min(max_width
, wid
->current_x
);
296 /* Draw goal list. */
297 this->DrawListColumn(GC_PROGRESS
, wid
, progress_col_width
);
298 this->DrawListColumn(GC_GOAL
, wid
, progress_col_width
);
302 void OnResize() override
304 this->vscroll
->SetCapacityFromWidget(this, WID_GOAL_LIST
);
308 * Some data on this window has become invalid.
309 * @param data Information about the changed data.
310 * @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.
312 void OnInvalidateData(int data
= 0, bool gui_scope
= true) override
314 if (!gui_scope
) return;
315 this->vscroll
->SetCount(this->CountLines());
316 this->SetWidgetDirty(WID_GOAL_LIST
);
320 /** Widgets of the #GoalListWindow. */
321 static const NWidgetPart _nested_goals_list_widgets
[] = {
322 NWidget(NWID_HORIZONTAL
),
323 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
324 NWidget(WWT_CAPTION
, COLOUR_BROWN
, WID_GOAL_CAPTION
), SetDataTip(STR_JUST_STRING
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
325 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
326 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
327 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
329 NWidget(NWID_HORIZONTAL
),
330 NWidget(WWT_PANEL
, COLOUR_BROWN
), SetDataTip(0x0, STR_GOALS_TOOLTIP_CLICK_ON_SERVICE_TO_CENTER
), SetScrollbar(WID_GOAL_SCROLLBAR
),
331 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),
333 NWidget(NWID_VERTICAL
),
334 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_GOAL_SCROLLBAR
),
335 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
340 static WindowDesc
_goals_list_desc(
341 WDP_AUTO
, "list_goals", 500, 127,
342 WC_GOALS_LIST
, WC_NONE
,
344 _nested_goals_list_widgets
, lengthof(_nested_goals_list_widgets
)
348 * Open a goal list window.
349 * @param company %Company to display the goals for, use #INVALID_COMPANY to display global goals.
351 void ShowGoalsList(CompanyID company
)
353 if (!Company::IsValidID(company
)) company
= (CompanyID
)INVALID_COMPANY
;
355 AllocateWindowDescFront
<GoalListWindow
>(&_goals_list_desc
, company
);
358 /** Ask a question about a goal. */
359 struct GoalQuestionWindow
: public Window
{
360 char *question
; ///< Question to ask (private copy).
361 int buttons
; ///< Number of valid buttons in #button.
362 int button
[3]; ///< Buttons to display.
363 byte type
; ///< Type of question.
365 GoalQuestionWindow(WindowDesc
*desc
, WindowNumber window_number
, byte type
, uint32 button_mask
, const char *question
) : Window(desc
), type(type
)
367 assert(type
< GOAL_QUESTION_TYPE_COUNT
);
368 this->question
= stredup(question
);
370 /* Figure out which buttons we have to enable. */
373 FOR_EACH_SET_BIT(bit
, button_mask
) {
374 if (bit
>= GOAL_QUESTION_BUTTON_COUNT
) break;
375 this->button
[n
++] = bit
;
379 assert(this->buttons
> 0 && this->buttons
< 4);
381 this->CreateNestedTree();
382 this->GetWidget
<NWidgetStacked
>(WID_GQ_BUTTONS
)->SetDisplayedPlane(this->buttons
- 1);
383 this->FinishInitNested(window_number
);
386 ~GoalQuestionWindow()
388 free(this->question
);
391 void SetStringParameters(int widget
) const override
395 SetDParam(0, STR_GOAL_QUESTION_CAPTION_QUESTION
+ this->type
);
398 case WID_GQ_BUTTON_1
:
399 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL
+ this->button
[0]);
402 case WID_GQ_BUTTON_2
:
403 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL
+ this->button
[1]);
406 case WID_GQ_BUTTON_3
:
407 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL
+ this->button
[2]);
412 void OnClick(Point pt
, int widget
, int click_count
) override
415 case WID_GQ_BUTTON_1
:
416 DoCommandP(0, this->window_number
, this->button
[0], CMD_GOAL_QUESTION_ANSWER
);
420 case WID_GQ_BUTTON_2
:
421 DoCommandP(0, this->window_number
, this->button
[1], CMD_GOAL_QUESTION_ANSWER
);
425 case WID_GQ_BUTTON_3
:
426 DoCommandP(0, this->window_number
, this->button
[2], CMD_GOAL_QUESTION_ANSWER
);
432 void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
) override
434 if (widget
!= WID_GQ_QUESTION
) return;
436 SetDParamStr(0, this->question
);
437 size
->height
= GetStringHeight(STR_JUST_RAW_STRING
, size
->width
) + WD_PAR_VSEP_WIDE
;
440 void DrawWidget(const Rect
&r
, int widget
) const override
442 if (widget
!= WID_GQ_QUESTION
) return;
444 SetDParamStr(0, this->question
);
445 DrawStringMultiLine(r
.left
, r
.right
, r
.top
, UINT16_MAX
, STR_JUST_RAW_STRING
, TC_BLACK
, SA_TOP
| SA_HOR_CENTER
);
449 /** Widgets of the goal question window. */
450 static const NWidgetPart _nested_goal_question_widgets
[] = {
451 NWidget(NWID_HORIZONTAL
),
452 NWidget(WWT_CLOSEBOX
, COLOUR_LIGHT_BLUE
),
453 NWidget(WWT_CAPTION
, COLOUR_LIGHT_BLUE
, WID_GQ_CAPTION
), SetDataTip(STR_WHITE_STRING
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
455 NWidget(WWT_PANEL
, COLOUR_LIGHT_BLUE
),
456 NWidget(WWT_EMPTY
, INVALID_COLOUR
, WID_GQ_QUESTION
), SetMinimalSize(300, 0), SetPadding(8, 8, 8, 8), SetFill(1, 0),
457 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_GQ_BUTTONS
),
458 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(85, 10, 85),
459 NWidget(WWT_PUSHTXTBTN
, COLOUR_LIGHT_BLUE
, WID_GQ_BUTTON_1
), SetDataTip(STR_BLACK_STRING
, STR_NULL
), SetFill(1, 0),
461 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(65, 10, 65),
462 NWidget(WWT_PUSHTXTBTN
, COLOUR_LIGHT_BLUE
, WID_GQ_BUTTON_1
), SetDataTip(STR_BLACK_STRING
, STR_NULL
), SetFill(1, 0),
463 NWidget(WWT_PUSHTXTBTN
, COLOUR_LIGHT_BLUE
, WID_GQ_BUTTON_2
), SetDataTip(STR_BLACK_STRING
, STR_NULL
), SetFill(1, 0),
465 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(25, 10, 25),
466 NWidget(WWT_PUSHTXTBTN
, COLOUR_LIGHT_BLUE
, WID_GQ_BUTTON_1
), SetDataTip(STR_BLACK_STRING
, STR_NULL
), SetFill(1, 0),
467 NWidget(WWT_PUSHTXTBTN
, COLOUR_LIGHT_BLUE
, WID_GQ_BUTTON_2
), SetDataTip(STR_BLACK_STRING
, STR_NULL
), SetFill(1, 0),
468 NWidget(WWT_PUSHTXTBTN
, COLOUR_LIGHT_BLUE
, WID_GQ_BUTTON_3
), SetDataTip(STR_BLACK_STRING
, STR_NULL
), SetFill(1, 0),
471 NWidget(NWID_SPACER
), SetMinimalSize(0, 8),
475 static WindowDesc
_goal_question_list_desc(
476 WDP_CENTER
, nullptr, 0, 0,
477 WC_GOAL_QUESTION
, WC_NONE
,
479 _nested_goal_question_widgets
, lengthof(_nested_goal_question_widgets
)
483 * Display a goal question.
484 * @param id Window number to use.
485 * @param type Type of question.
486 * @param button_mask Buttons to display.
487 * @param question Question to ask.
489 void ShowGoalQuestion(uint16 id
, byte type
, uint32 button_mask
, const char *question
)
491 new GoalQuestionWindow(&_goal_question_list_desc
, id
, type
, button_mask
, question
);