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/>.
10 /** @file goal_gui.cpp GUI for goals. */
15 #include "window_gui.h"
16 #include "strings_func.h"
17 #include "date_func.h"
18 #include "viewport_func.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. */
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
);
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
);
71 if (s
->company
== INVALID_COMPANY
) {
86 y
-= 2; // "Company specific goals:" line.
90 if (s
->company
== this->window_number
) {
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;
110 case GT_COMPANY
: return;
113 if (!IsValidTile(s
->dst
)) return;
118 if (!Industry::IsValidID(s
->dst
)) return;
119 xy
= Industry::Get(s
->dst
)->location
.tile
;
123 if (!Town::IsValidID(s
->dst
)) return;
124 xy
= Town::Get(s
->dst
)->xy
;
127 case GT_STORY_PAGE
: {
128 if (!StoryPage::IsValidID(s
->dst
)) return;
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
);
142 default: NOT_REACHED();
146 ShowExtraViewPortWindow(xy
);
148 ScrollMainWindowToTile(xy
);
153 * Count the number of lines in this window.
154 * @return the number of lines.
158 /* Count number of (non) awarded goals. */
160 uint num_company
= 0;
163 if (s
->company
== INVALID_COMPANY
) {
165 } else if (s
->company
== this->window_number
) {
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
;
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
);
210 bool rtl
= _current_text_dir
== TD_RTL
;
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 (dpi
, x
+ (rtl
? width_reduction
: 0), right
- (rtl
? 0 : width_reduction
), y
+ pos
* FONT_HEIGHT_NORMAL
, STR_GOALS_TEXT
);
227 if (s
->progress
!= NULL
) {
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 (dpi
, 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 (dpi
, x
, right
, y
+ pos
* FONT_HEIGHT_NORMAL
, str
);
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. */
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. */
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
),
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),
335 NWidget(NWID_VERTICAL
),
336 NWidget(NWID_VSCROLLBAR
, COLOUR_BROWN
, WID_GOAL_SCROLLBAR
),
337 NWidget(WWT_RESIZEBOX
, COLOUR_BROWN
),
342 static WindowDesc::Prefs
_goals_list_prefs ("list_goals");
344 static const WindowDesc
_goals_list_desc(
346 WC_GOALS_LIST
, WC_NONE
,
348 _nested_goals_list_widgets
, lengthof(_nested_goals_list_widgets
),
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. */
378 FOR_EACH_SET_BIT(bit
, button_mask
) {
379 if (bit
>= GOAL_QUESTION_BUTTON_COUNT
) break;
380 this->button
[n
++] = bit
;
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
400 SetDParam(0, STR_GOAL_QUESTION_CAPTION_QUESTION
+ this->type
);
403 case WID_GQ_BUTTON_1
:
404 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL
+ this->button
[0]);
407 case WID_GQ_BUTTON_2
:
408 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL
+ this->button
[1]);
411 case WID_GQ_BUTTON_3
:
412 SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL
+ this->button
[2]);
417 /* virtual */ void OnClick(Point pt
, int widget
, int click_count
)
420 case WID_GQ_BUTTON_1
:
421 DoCommandP(0, this->window_number
, this->button
[0], CMD_GOAL_QUESTION_ANSWER
);
425 case WID_GQ_BUTTON_2
:
426 DoCommandP(0, this->window_number
, this->button
[1], CMD_GOAL_QUESTION_ANSWER
);
430 case WID_GQ_BUTTON_3
:
431 DoCommandP(0, this->window_number
, this->button
[2], CMD_GOAL_QUESTION_ANSWER
);
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
),
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),
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),
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),
476 NWidget(NWID_SPACER
), SetMinimalSize(0, 8),
480 static const WindowDesc
_goal_question_list_desc(
482 WC_GOAL_QUESTION
, WC_NONE
,
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
);