Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / goal_gui.cpp
blobd8be74d35835f340d353ba5cedd00825f2870c04
1 /*
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/>.
6 */
8 /** @file goal_gui.cpp GUI for goals. */
10 #include "stdafx.h"
11 #include "industry.h"
12 #include "town.h"
13 #include "window_gui.h"
14 #include "strings_func.h"
15 #include "viewport_func.h"
16 #include "gui.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"
25 #include "goal_cmd.h"
27 #include "widgets/goal_widget.h"
29 #include "table/strings.h"
31 #include "safeguards.h"
33 /** Goal list columns. */
34 enum GoalColumn {
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);
60 } else {
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
68 switch (widget) {
69 case WID_GOAL_GLOBAL_BUTTON:
70 ShowGoalsList(INVALID_COMPANY);
71 break;
73 case WID_GOAL_COMPANY_BUTTON:
74 ShowGoalsList(_local_company);
75 break;
77 case WID_GOAL_LIST: {
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) {
81 if (y == 0) {
82 this->HandleClick(s);
83 return;
85 y--;
88 break;
91 default:
92 break;
96 /**
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. */
103 TileIndex xy;
104 switch (s->type) {
105 case GT_NONE: return;
107 case GT_COMPANY:
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);
111 return;
113 case GT_TILE:
114 if (!IsValidTile(s->dst)) return;
115 xy = s->dst;
116 break;
118 case GT_INDUSTRY:
119 if (!Industry::IsValidID(s->dst)) return;
120 xy = Industry::Get(s->dst)->location.tile;
121 break;
123 case GT_TOWN:
124 if (!Town::IsValidID(s->dst)) return;
125 xy = Town::Get(s->dst)->xy;
126 break;
128 case GT_STORY_PAGE: {
129 if (!StoryPage::IsValidID(s->dst)) return;
131 /* Verify that:
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);
140 return;
143 default: NOT_REACHED();
146 if (_ctrl_pressed) {
147 ShowExtraViewportWindow(xy);
148 } else {
149 ScrollMainWindowToTile(xy);
154 * Count the number of lines in this window.
155 * @return the number of lines.
157 uint CountLines()
159 /* Count number of (non) awarded goals. */
160 uint num = 0;
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;
168 return num;
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);
176 resize->width = 1;
177 resize->height = d.height;
179 d.height *= 5;
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();
201 uint num = 0;
202 for (const Goal *s : Goal::Iterate()) {
203 if (s->company == this->window_number) {
204 if (IsInsideMM(pos, 0, cap)) {
205 switch (column) {
206 case GC_GOAL: {
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);
211 break;
214 case GC_PROGRESS:
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);
220 break;
222 r.top += GetCharacterHeight(FS_NORMAL);
224 pos++;
225 num++;
229 if (num == 0) {
230 if (column == GC_GOAL && IsInsideMM(pos, 0, cap)) {
231 DrawString(r, STR_GOALS_NONE);
236 void OnPaint() override
238 this->DrawWidgets();
240 if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
242 /* Calculate progress column width. */
243 uint max_width = 0;
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),
290 EndContainer(),
291 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
292 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
293 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
294 EndContainer(),
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),
297 EndContainer(),
298 NWidget(NWID_VERTICAL),
299 NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_GOAL_SCROLLBAR),
300 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
301 EndContainer(),
302 EndContainer(),
305 static WindowDesc _goals_list_desc(__FILE__, __LINE__,
306 WDP_AUTO, "list_goals", 500, 127,
307 WC_GOALS_LIST, WC_NONE,
309 std::begin(_nested_goals_list_widgets), std::end(_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. */
335 int n = 0;
336 for (uint bit : SetBitIterator(button_mask)) {
337 if (bit >= GOAL_QUESTION_BUTTON_COUNT) break;
338 this->button[n++] = bit;
339 if (n == 3) break;
341 this->buttons = n;
342 assert(this->buttons < 4);
344 this->CreateNestedTree();
345 if (this->buttons == 0) {
346 this->GetWidget<NWidgetStacked>(WID_GQ_BUTTONS)->SetDisplayedPlane(SZSP_HORIZONTAL);
347 } else {
348 this->GetWidget<NWidgetStacked>(WID_GQ_BUTTONS)->SetDisplayedPlane(this->buttons - 1);
350 this->FinishInitNested(window_number);
354 void SetStringParameters(WidgetID widget) const override
356 switch (widget) {
357 case WID_GQ_BUTTON_1:
358 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[0]);
359 break;
361 case WID_GQ_BUTTON_2:
362 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[1]);
363 break;
365 case WID_GQ_BUTTON_3:
366 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[2]);
367 break;
371 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
373 switch (widget) {
374 case WID_GQ_BUTTON_1:
375 Command<CMD_GOAL_QUESTION_ANSWER>::Post(this->window_number, this->button[0]);
376 this->Close();
377 break;
379 case WID_GQ_BUTTON_2:
380 Command<CMD_GOAL_QUESTION_ANSWER>::Post(this->window_number, this->button[1]);
381 this->Close();
382 break;
384 case WID_GQ_BUTTON_3:
385 Command<CMD_GOAL_QUESTION_ANSWER>::Post(this->window_number, this->button[2]);
386 this->Close();
387 break;
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),
420 EndContainer(),
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),
427 EndContainer(),
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),
431 EndContainer(),
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),
436 EndContainer(),
437 EndContainer(),
438 EndContainer(),
439 EndContainer(),
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 __FILE__, __LINE__,
451 WDP_CENTER, nullptr, 0, 0,
452 WC_GOAL_QUESTION, WC_NONE,
453 WDF_CONSTRUCTION,
454 std::begin(_nested_goal_question_widgets_question), std::end(_nested_goal_question_widgets_question),
457 __FILE__, __LINE__,
458 WDP_CENTER, nullptr, 0, 0,
459 WC_GOAL_QUESTION, WC_NONE,
460 WDF_CONSTRUCTION,
461 std::begin(_nested_goal_question_widgets_info), std::end(_nested_goal_question_widgets_info),
464 __FILE__, __LINE__,
465 WDP_CENTER, nullptr, 0, 0,
466 WC_GOAL_QUESTION, WC_NONE,
467 WDF_CONSTRUCTION,
468 std::begin(_nested_goal_question_widgets_warning), std::end(_nested_goal_question_widgets_warning),
471 __FILE__, __LINE__,
472 WDP_CENTER, nullptr, 0, 0,
473 WC_GOAL_QUESTION, WC_NONE,
474 WDF_CONSTRUCTION,
475 std::begin(_nested_goal_question_widgets_error), std::end(_nested_goal_question_widgets_error),
480 * Display a goal question.
481 * @param id Window number to use.
482 * @param type Type of question.
483 * @param button_mask Buttons to display.
484 * @param question Question to ask.
486 void ShowGoalQuestion(uint16_t id, byte type, uint32_t button_mask, const std::string &question)
488 assert(type < GQT_END);
489 new GoalQuestionWindow(&_goal_question_list_desc[type], id, type == 3 ? TC_WHITE : TC_BLACK, button_mask, question);