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 "viewport_func.h"
17 #include "goal_base.h"
18 #include "core/geometry_func.hpp"
19 #include "company_func.h"
20 #include "company_base.h"
21 #include "company_gui.h"
22 #include "story_base.h"
23 #include "command_func.h"
24 #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 NWidgetStacked
*wi
= this->GetWidget
<NWidgetStacked
>(WID_GOAL_SELECT_BUTTONS
);
50 wi
->SetDisplayedPlane(window_number
== INVALID_COMPANY
? 1 : 0);
51 this->OnInvalidateData(0);
54 void SetStringParameters(WidgetID widget
) const override
56 if (widget
!= WID_GOAL_CAPTION
) return;
58 if (this->window_number
== INVALID_COMPANY
) {
59 SetDParam(0, STR_GOALS_SPECTATOR_CAPTION
);
61 SetDParam(0, STR_GOALS_CAPTION
);
62 SetDParam(1, this->window_number
);
66 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
69 case WID_GOAL_GLOBAL_BUTTON
:
70 ShowGoalsList(INVALID_COMPANY
);
73 case WID_GOAL_COMPANY_BUTTON
:
74 ShowGoalsList(_local_company
);
78 int y
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_GOAL_LIST
, WidgetDimensions::scaled
.framerect
.top
);
79 for (const Goal
*s
: Goal::Iterate()) {
80 if (s
->company
== this->window_number
) {
97 * Handle clicking at a goal.
98 * @param s #Goal clicked at.
100 void HandleClick(const Goal
*s
)
102 /* Determine dst coordinate for goal and try to scroll to it. */
105 case GT_NONE
: return;
108 /* s->dst here is not a tile, but a CompanyID.
109 * Show the window with the overview of the company instead. */
110 ShowCompany((CompanyID
)s
->dst
);
114 if (!IsValidTile(s
->dst
)) return;
119 if (!Industry::IsValidID(s
->dst
)) return;
120 xy
= Industry::Get(s
->dst
)->location
.tile
;
124 if (!Town::IsValidID(s
->dst
)) return;
125 xy
= Town::Get(s
->dst
)->xy
;
128 case GT_STORY_PAGE
: {
129 if (!StoryPage::IsValidID(s
->dst
)) return;
132 * - if global goal: story page must be global.
133 * - if company goal: story page must be global or of the same company.
135 CompanyID goal_company
= s
->company
;
136 CompanyID story_company
= StoryPage::Get(s
->dst
)->company
;
137 if (goal_company
== INVALID_COMPANY
? story_company
!= INVALID_COMPANY
: story_company
!= INVALID_COMPANY
&& story_company
!= goal_company
) return;
139 ShowStoryBook((CompanyID
)this->window_number
, s
->dst
);
143 default: NOT_REACHED();
147 ShowExtraViewportWindow(xy
);
149 ScrollMainWindowToTile(xy
);
154 * Count the number of lines in this window.
155 * @return the number of lines.
159 /* Count number of (non) awarded goals. */
161 for (const Goal
*s
: Goal::Iterate()) {
162 if (s
->company
== this->window_number
) num
++;
165 /* Count the 'none' lines. */
166 if (num
== 0) num
= 1;
171 void UpdateWidgetSize(WidgetID widget
, Dimension
&size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
&fill
, [[maybe_unused
]] Dimension
&resize
) override
173 if (widget
!= WID_GOAL_LIST
) return;
174 Dimension d
= GetStringBoundingBox(STR_GOALS_NONE
);
177 resize
.height
= d
.height
;
180 d
.width
+= WidgetDimensions::scaled
.framerect
.Horizontal();
181 d
.height
+= WidgetDimensions::scaled
.framerect
.Vertical();
182 size
= maxdim(size
, d
);
186 * Draws a given column of the goal list.
187 * @param column Which column to draw.
188 * @param wid Pointer to the goal list widget.
189 * @param progress_col_width Width of the progress column.
190 * @return max width of drawn text
192 void DrawListColumn(GoalColumn column
, NWidgetBase
*wid
, uint progress_col_width
) const
194 /* Get column draw area. */
195 Rect r
= wid
->GetCurrentRect().Shrink(WidgetDimensions::scaled
.framerect
);
196 bool rtl
= _current_text_dir
== TD_RTL
;
198 int pos
= -this->vscroll
->GetPosition();
199 const int cap
= this->vscroll
->GetCapacity();
202 for (const Goal
*s
: Goal::Iterate()) {
203 if (s
->company
== this->window_number
) {
204 if (IsInsideMM(pos
, 0, cap
)) {
207 /* Display the goal. */
208 SetDParamStr(0, s
->text
);
209 uint width_reduction
= progress_col_width
> 0 ? progress_col_width
+ WidgetDimensions::scaled
.framerect
.Horizontal() : 0;
210 DrawString(r
.Indent(width_reduction
, !rtl
), STR_GOALS_TEXT
);
215 if (!s
->progress
.empty()) {
216 SetDParamStr(0, s
->progress
);
217 StringID str
= s
->completed
? STR_GOALS_PROGRESS_COMPLETE
: STR_GOALS_PROGRESS
;
218 DrawString(r
.WithWidth(progress_col_width
, !rtl
), str
, TC_FROMSTRING
, SA_RIGHT
| SA_FORCE
);
222 r
.top
+= GetCharacterHeight(FS_NORMAL
);
230 if (column
== GC_GOAL
&& IsInsideMM(pos
, 0, cap
)) {
231 DrawString(r
, STR_GOALS_NONE
);
236 void OnPaint() override
240 if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
242 /* Calculate progress column width. */
244 for (const Goal
*s
: Goal::Iterate()) {
245 if (!s
->progress
.empty()) {
246 SetDParamStr(0, s
->progress
);
247 StringID str
= s
->completed
? STR_GOALS_PROGRESS_COMPLETE
: STR_GOALS_PROGRESS
;
248 uint str_width
= GetStringBoundingBox(str
).width
;
249 if (str_width
> max_width
) max_width
= str_width
;
253 NWidgetBase
*wid
= this->GetWidget
<NWidgetBase
>(WID_GOAL_LIST
);
254 uint progress_col_width
= std::min(max_width
, wid
->current_x
);
256 /* Draw goal list. */
257 this->DrawListColumn(GC_PROGRESS
, wid
, progress_col_width
);
258 this->DrawListColumn(GC_GOAL
, wid
, progress_col_width
);
262 void OnResize() override
264 this->vscroll
->SetCapacityFromWidget(this, WID_GOAL_LIST
, WidgetDimensions::scaled
.framerect
.Vertical());
268 * Some data on this window has become invalid.
269 * @param data Information about the changed data.
270 * @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.
272 void OnInvalidateData([[maybe_unused
]] int data
= 0, [[maybe_unused
]] bool gui_scope
= true) override
274 if (!gui_scope
) return;
275 this->vscroll
->SetCount(this->CountLines());
276 this->SetWidgetDisabledState(WID_GOAL_COMPANY_BUTTON
, _local_company
== COMPANY_SPECTATOR
);
277 this->SetWidgetDirty(WID_GOAL_COMPANY_BUTTON
);
278 this->SetWidgetDirty(WID_GOAL_LIST
);
282 /** Widgets of the #GoalListWindow. */
283 static constexpr NWidgetPart _nested_goals_list_widgets
[] = {
284 NWidget(NWID_HORIZONTAL
),
285 NWidget(WWT_CLOSEBOX
, COLOUR_BROWN
),
286 NWidget(WWT_CAPTION
, COLOUR_BROWN
, WID_GOAL_CAPTION
), SetDataTip(STR_JUST_STRING1
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
287 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_GOAL_SELECT_BUTTONS
),
288 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_GOAL_GLOBAL_BUTTON
), SetMinimalSize(50, 0), SetDataTip(STR_GOALS_GLOBAL_BUTTON
, STR_GOALS_GLOBAL_BUTTON_HELPTEXT
),
289 NWidget(WWT_PUSHTXTBTN
, COLOUR_BROWN
, WID_GOAL_COMPANY_BUTTON
), SetMinimalSize(50, 0), SetDataTip(STR_GOALS_COMPANY_BUTTON
, STR_GOALS_COMPANY_BUTTON_HELPTEXT
),
291 NWidget(WWT_SHADEBOX
, COLOUR_BROWN
),
292 NWidget(WWT_DEFSIZEBOX
, COLOUR_BROWN
),
293 NWidget(WWT_STICKYBOX
, COLOUR_BROWN
),
295 NWidget(NWID_HORIZONTAL
),
296 NWidget(WWT_PANEL
, COLOUR_BROWN
, WID_GOAL_LIST
), SetDataTip(0x0, STR_GOALS_TOOLTIP_CLICK_ON_SERVICE_TO_CENTER
), SetScrollbar(WID_GOAL_SCROLLBAR
), SetResize(1, 1), SetMinimalTextLines(2, 0),
298 NWidget(NWID_VERTICAL
),
299 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_GOAL_SCROLLBAR
),
300 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
305 static WindowDesc
_goals_list_desc(
306 WDP_AUTO
, "list_goals", 500, 127,
307 WC_GOALS_LIST
, WC_NONE
,
309 _nested_goals_list_widgets
313 * Open a goal list window.
314 * @param company %Company to display the goals for, use #INVALID_COMPANY to display global goals.
316 void ShowGoalsList(CompanyID company
)
318 if (!Company::IsValidID(company
)) company
= (CompanyID
)INVALID_COMPANY
;
320 AllocateWindowDescFront
<GoalListWindow
>(_goals_list_desc
, company
);
323 /** Ask a question about a goal. */
324 struct GoalQuestionWindow
: public Window
{
325 std::string question
; ///< Question to ask (private copy).
326 int buttons
; ///< Number of valid buttons in #button.
327 int button
[3]; ///< Buttons to display.
328 TextColour colour
; ///< Colour of the question text.
330 GoalQuestionWindow(WindowDesc
&desc
, WindowNumber window_number
, TextColour colour
, uint32_t button_mask
, const std::string
&question
) : Window(desc
), colour(colour
)
332 this->question
= question
;
334 /* Figure out which buttons we have to enable. */
336 for (uint bit
: SetBitIterator(button_mask
)) {
337 if (bit
>= GOAL_QUESTION_BUTTON_COUNT
) break;
338 this->button
[n
++] = bit
;
342 assert(this->buttons
< 4);
344 this->CreateNestedTree();
345 if (this->buttons
== 0) {
346 this->GetWidget
<NWidgetStacked
>(WID_GQ_BUTTONS
)->SetDisplayedPlane(SZSP_HORIZONTAL
);
348 this->GetWidget
<NWidgetStacked
>(WID_GQ_BUTTONS
)->SetDisplayedPlane(this->buttons
- 1);
350 this->FinishInitNested(window_number
);
354 void SetStringParameters(WidgetID widget
) const override
357 case WID_GQ_BUTTON_1
:
358 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL
+ this->button
[0]);
361 case WID_GQ_BUTTON_2
:
362 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL
+ this->button
[1]);
365 case WID_GQ_BUTTON_3
:
366 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL
+ this->button
[2]);
371 void OnClick([[maybe_unused
]] Point pt
, WidgetID widget
, [[maybe_unused
]] int click_count
) override
374 case WID_GQ_BUTTON_1
:
375 Command
<CMD_GOAL_QUESTION_ANSWER
>::Post(this->window_number
, this->button
[0]);
379 case WID_GQ_BUTTON_2
:
380 Command
<CMD_GOAL_QUESTION_ANSWER
>::Post(this->window_number
, this->button
[1]);
384 case WID_GQ_BUTTON_3
:
385 Command
<CMD_GOAL_QUESTION_ANSWER
>::Post(this->window_number
, this->button
[2]);
391 void UpdateWidgetSize(WidgetID widget
, Dimension
&size
, [[maybe_unused
]] const Dimension
&padding
, [[maybe_unused
]] Dimension
&fill
, [[maybe_unused
]] Dimension
&resize
) override
393 if (widget
!= WID_GQ_QUESTION
) return;
395 SetDParamStr(0, this->question
);
396 size
.height
= GetStringHeight(STR_JUST_RAW_STRING
, size
.width
);
399 void DrawWidget(const Rect
&r
, WidgetID widget
) const override
401 if (widget
!= WID_GQ_QUESTION
) return;
403 SetDParamStr(0, this->question
);
404 DrawStringMultiLine(r
, STR_JUST_RAW_STRING
, this->colour
, SA_TOP
| SA_HOR_CENTER
);
409 * Widgets of the goal question window.
410 * @tparam bg_colour Background colour.
411 * @tparam btn_colour Button colour.
412 * @tparam caption Window caption string.
414 template <Colours bg_colour
, Colours btn_colour
, StringID caption
>
415 struct NestedGoalWidgets
{
416 static constexpr auto widgetparts
= {
417 NWidget(NWID_HORIZONTAL
),
418 NWidget(WWT_CLOSEBOX
, bg_colour
),
419 NWidget(WWT_CAPTION
, bg_colour
, WID_GQ_CAPTION
), SetDataTip(STR_GOAL_QUESTION_CAPTION_QUESTION
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
421 NWidget(WWT_PANEL
, bg_colour
),
422 NWidget(NWID_VERTICAL
), SetPadding(WidgetDimensions::unscaled
.modalpopup
), SetPIP(0, WidgetDimensions::unscaled
.vsep_wide
, 0),
423 NWidget(WWT_EMPTY
, INVALID_COLOUR
, WID_GQ_QUESTION
), SetMinimalSize(300, 0), SetFill(1, 0),
424 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_GQ_BUTTONS
),
425 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(85, WidgetDimensions::unscaled
.hsep_wide
, 85),
426 NWidget(WWT_PUSHTXTBTN
, btn_colour
, WID_GQ_BUTTON_1
), SetDataTip(STR_JUST_STRING
, STR_NULL
), SetFill(1, 0),
428 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(65, WidgetDimensions::unscaled
.hsep_wide
, 65),
429 NWidget(WWT_PUSHTXTBTN
, btn_colour
, WID_GQ_BUTTON_1
), SetDataTip(STR_JUST_STRING
, STR_NULL
), SetFill(1, 0),
430 NWidget(WWT_PUSHTXTBTN
, btn_colour
, WID_GQ_BUTTON_2
), SetDataTip(STR_JUST_STRING
, STR_NULL
), SetFill(1, 0),
432 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
), SetPIP(25, WidgetDimensions::unscaled
.hsep_wide
, 25),
433 NWidget(WWT_PUSHTXTBTN
, btn_colour
, WID_GQ_BUTTON_1
), SetDataTip(STR_JUST_STRING
, STR_NULL
), SetFill(1, 0),
434 NWidget(WWT_PUSHTXTBTN
, btn_colour
, WID_GQ_BUTTON_2
), SetDataTip(STR_JUST_STRING
, STR_NULL
), SetFill(1, 0),
435 NWidget(WWT_PUSHTXTBTN
, btn_colour
, WID_GQ_BUTTON_3
), SetDataTip(STR_JUST_STRING
, STR_NULL
), SetFill(1, 0),
443 static constexpr auto _nested_goal_question_widgets_question
= NestedGoalWidgets
<COLOUR_LIGHT_BLUE
, COLOUR_LIGHT_BLUE
, STR_GOAL_QUESTION_CAPTION_QUESTION
>::widgetparts
;
444 static constexpr auto _nested_goal_question_widgets_info
= NestedGoalWidgets
<COLOUR_LIGHT_BLUE
, COLOUR_LIGHT_BLUE
, STR_GOAL_QUESTION_CAPTION_INFORMATION
>::widgetparts
;
445 static constexpr auto _nested_goal_question_widgets_warning
= NestedGoalWidgets
<COLOUR_YELLOW
, COLOUR_YELLOW
, STR_GOAL_QUESTION_CAPTION_WARNING
>::widgetparts
;
446 static constexpr auto _nested_goal_question_widgets_error
= NestedGoalWidgets
<COLOUR_RED
, COLOUR_YELLOW
, STR_GOAL_QUESTION_CAPTION_ERROR
>::widgetparts
;
448 static WindowDesc _goal_question_list_desc
[] = {
450 WDP_CENTER
, nullptr, 0, 0,
451 WC_GOAL_QUESTION
, WC_NONE
,
453 _nested_goal_question_widgets_question
,
456 WDP_CENTER
, nullptr, 0, 0,
457 WC_GOAL_QUESTION
, WC_NONE
,
459 _nested_goal_question_widgets_info
,
462 WDP_CENTER
, nullptr, 0, 0,
463 WC_GOAL_QUESTION
, WC_NONE
,
465 _nested_goal_question_widgets_warning
,
468 WDP_CENTER
, nullptr, 0, 0,
469 WC_GOAL_QUESTION
, WC_NONE
,
471 _nested_goal_question_widgets_error
,
476 * Display a goal question.
477 * @param id Window number to use.
478 * @param type Type of question.
479 * @param button_mask Buttons to display.
480 * @param question Question to ask.
482 void ShowGoalQuestion(uint16_t id
, uint8_t type
, uint32_t button_mask
, const std::string
&question
)
484 assert(type
< GQT_END
);
485 new GoalQuestionWindow(_goal_question_list_desc
[type
], id
, type
== 3 ? TC_WHITE
: TC_BLACK
, button_mask
, question
);