# goal_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: goal_gui.cpp 26012 2013-11-16 17:41:57Z zuu $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "industry.h"
    00014 #include "town.h"
    00015 #include "window_gui.h"
    00016 #include "strings_func.h"
    00017 #include "date_func.h"
    00018 #include "viewport_func.h"
    00019 #include "gui.h"
    00020 #include "goal_base.h"
    00021 #include "core/geometry_func.hpp"
    00022 #include "company_func.h"
    00023 #include "company_base.h"
    00024 #include "story_base.h"
    00025 #include "command_func.h"
    00026
    00027 #include "widgets/goal_widget.h"
    00028
    00029 #include "table/strings.h"
    00030
    00032 enum GoalColumn {
    00033   GC_GOAL = 0,
    00034   GC_PROGRESS,
    00035 };
    00036
    00038 struct GoalListWindow : public Window {
    00039   Scrollbar *vscroll;
    00040
    00041   GoalListWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
    00042   {
    00043     this->CreateNestedTree();
    00044     this->vscroll = this->GetScrollbar(WID_GOAL_SCROLLBAR);
    00045     this->FinishInitNested(window_number);
    00046     this->owner = (Owner)this->window_number;
    00047     this->OnInvalidateData(0);
    00048   }
    00049
    00050   /* virtual */ void SetStringParameters(int widget) const
    00051   {
    00052     if (widget != WID_GOAL_CAPTION) return;
    00053
    00054     if (this->window_number == INVALID_COMPANY) {
    00055       SetDParam(0, STR_GOALS_SPECTATOR_CAPTION);
    00056     } else {
    00057       SetDParam(0, STR_GOALS_CAPTION);
    00058       SetDParam(1, this->window_number);
    00059     }
    00060   }
    00061
    00062   /* virtual */ void OnClick(Point pt, int widget, int click_count)
    00063   {
    00064     if (widget != WID_GOAL_LIST) return;
    00065
    00066     int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GOAL_LIST, WD_FRAMERECT_TOP);
    00067     int num = 0;
    00068     const Goal *s;
    00069     FOR_ALL_GOALS(s) {
    00070       if (s->company == INVALID_COMPANY) {
    00071         y--;
    00072         if (y == 0) {
    00073           this->HandleClick(s);
    00074           return;
    00075         }
    00076         num++;
    00077       }
    00078     }
    00079
    00080     if (num == 0) {
    00081       y--; // "None" line.
    00082       if (y < 0) return;
    00083     }
    00084
    00085     y -= 2; // "Company specific goals:" line.
    00086     if (y < 0) return;
    00087
    00088     FOR_ALL_GOALS(s) {
    00089       if (s->company == this->window_number) {
    00090         y--;
    00091         if (y == 0) {
    00092           this->HandleClick(s);
    00093           return;
    00094         }
    00095       }
    00096     }
    00097   }
    00098
    00103   void HandleClick(const Goal *s)
    00104   {
    00105     /* Determine dst coordinate for goal and try to scroll to it. */
    00106     TileIndex xy;
    00107     switch (s->type) {
    00108       case GT_NONE: return;
    00109       case GT_COMPANY: return;
    00110
    00111       case GT_TILE:
    00112         if (!IsValidTile(s->dst)) return;
    00113         xy = s->dst;
    00114         break;
    00115
    00116       case GT_INDUSTRY:
    00117         if (!Industry::IsValidID(s->dst)) return;
    00118         xy = Industry::Get(s->dst)->location.tile;
    00119         break;
    00120
    00121       case GT_TOWN:
    00122         if (!Town::IsValidID(s->dst)) return;
    00123         xy = Town::Get(s->dst)->xy;
    00124         break;
    00125
    00126       case GT_STORY_PAGE: {
    00127         if (!StoryPage::IsValidID(s->dst)) return;
    00128
    00129         /* Verify that:
    00130          * - if global goal: story page must be global.
    00131          * - if company goal: story page must be global or of the same company.
    00132          */
    00133         CompanyID goal_company = s->company;
    00134         CompanyID story_company = StoryPage::Get(s->dst)->company;
    00135         if (goal_company == INVALID_COMPANY ? story_company != INVALID_COMPANY : story_company != INVALID_COMPANY && story_company != goal_company) return;
    00136
    00137         ShowStoryBook((CompanyID)this->window_number, s->dst);
    00138         return;
    00139       }
    00140
    00141       default: NOT_REACHED();
    00142     }
    00143
    00144     if (_ctrl_pressed) {
    00145       ShowExtraViewPortWindow(xy);
    00146     } else {
    00147       ScrollMainWindowToTile(xy);
    00148     }
    00149   }
    00150
    00155   uint CountLines()
    00156   {
    00157     /* Count number of (non) awarded goals. */
    00158     uint num_global = 0;
    00159     uint num_company = 0;
    00160     const Goal *s;
    00161     FOR_ALL_GOALS(s) {
    00162       if (s->company == INVALID_COMPANY) {
    00163         num_global++;
    00164       } else if (s->company == this->window_number) {
    00165         num_company++;
    00166       }
    00167     }
    00168
    00169     /* Count the 'none' lines. */
    00170     if (num_global  == 0) num_global = 1;
    00171     if (num_company == 0) num_company = 1;
    00172
    00173     /* Global, company and an empty line before the accepted ones. */
    00174     return 3 + num_global + num_company;
    00175   }
    00176
    00177   /* virtual */ void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00178   {
    00179     if (widget != WID_GOAL_LIST) return;
    00180     Dimension d = maxdim(GetStringBoundingBox(STR_GOALS_GLOBAL_TITLE), GetStringBoundingBox(STR_GOALS_COMPANY_TITLE));
    00181
    00182     resize->height = d.height;
    00183
    00184     d.height *= 5;
    00185     d.width += padding.width + WD_FRAMERECT_RIGHT + WD_FRAMERECT_LEFT;
    00186     d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00187     *size = maxdim(*size, d);
    00188   }
    00189
    00201   void DrawPartialGoalList(int &pos, const int cap, int x, int y, int right, uint progress_col_width, bool global_section, GoalColumn column) const
    00202   {
    00203     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);
    00204     pos++;
    00205
    00206     bool rtl = _current_text_dir == TD_RTL;
    00207
    00208     uint num = 0;
    00209     const Goal *s;
    00210     FOR_ALL_GOALS(s) {
    00211       if (global_section ? s->company == INVALID_COMPANY : (s->company == this->window_number && s->company != INVALID_COMPANY)) {
    00212         if (IsInsideMM(pos, 0, cap)) {
    00213           switch (column) {
    00214             case GC_GOAL: {
    00215               /* Display the goal. */
    00216               SetDParamStr(0, s->text);
    00217               uint width_reduction = progress_col_width > 0 ? progress_col_width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT : 0;
    00218               DrawString(x + (rtl ? width_reduction : 0), right - (rtl ? 0 : width_reduction), y + pos * FONT_HEIGHT_NORMAL, STR_GOALS_TEXT);
    00219               break;
    00220             }
    00221
    00222             case GC_PROGRESS:
    00223               if (s->progress != NULL) {
    00224                 SetDParamStr(0, s->progress);
    00225                 StringID str = s->completed ? STR_GOALS_PROGRESS_COMPLETE : STR_GOALS_PROGRESS;
    00226                 int progress_x = x;
    00227                 int progress_right = rtl ? x + progress_col_width : right;
    00228                 DrawString(progress_x, progress_right, y + pos * FONT_HEIGHT_NORMAL, str, TC_FROMSTRING, SA_RIGHT | SA_FORCE);
    00229               }
    00230               break;
    00231           }
    00232         }
    00233         pos++;
    00234         num++;
    00235       }
    00236     }
    00237
    00238     if (num == 0) {
    00239       if (column == GC_GOAL && IsInsideMM(pos, 0, cap)) {
    00240         StringID str = !global_section && this->window_number == INVALID_COMPANY ? STR_GOALS_SPECTATOR_NONE : STR_GOALS_NONE;
    00241         DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, str);
    00242       }
    00243       pos++;
    00244     }
    00245   }
    00246
    00254   void DrawListColumn(GoalColumn column, NWidgetBase *wid, uint progress_col_width) const
    00255   {
    00256     /* Get column draw area. */
    00257     int y = wid->pos_y + WD_FRAMERECT_TOP;
    00258     int x = wid->pos_x + WD_FRAMERECT_LEFT;
    00259     int right = x + wid->current_x - WD_FRAMERECT_RIGHT;
    00260
    00261     int pos = -this->vscroll->GetPosition();
    00262     const int cap = this->vscroll->GetCapacity();
    00263
    00264     /* Draw partial list with global goals. */
    00265     DrawPartialGoalList(pos, cap, x, y, right, progress_col_width, true, column);
    00266
    00267     /* Draw partial list with company goals. */
    00268     pos++;
    00269     DrawPartialGoalList(pos, cap, x, y, right, progress_col_width, false, column);
    00270   }
    00271
    00272   /* virtual */ void OnPaint()
    00273   {
    00274     this->DrawWidgets();
    00275
    00276     /* Calculate progress column width. */
    00277     uint max_width = 0;
    00278     Goal *s;
    00279     FOR_ALL_GOALS(s) {
    00280       if (s->progress != NULL) {
    00281         SetDParamStr(0, s->progress);
    00282         StringID str = s->completed ? STR_GOALS_PROGRESS_COMPLETE : STR_GOALS_PROGRESS;
    00283         uint str_width = GetStringBoundingBox(str).width;
    00284         if (str_width > max_width) max_width = str_width;
    00285       }
    00286     }
    00287
    00288     NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_GOAL_LIST);
    00289     uint progress_col_width = min(max_width, wid->current_x);
    00290
    00291     /* Draw goal list. */
    00292     this->DrawListColumn(GC_PROGRESS, wid, progress_col_width);
    00293     this->DrawListColumn(GC_GOAL, wid, progress_col_width);
    00294
    00295   }
    00296
    00297   /* virtual */ void OnResize()
    00298   {
    00299     this->vscroll->SetCapacityFromWidget(this, WID_GOAL_LIST);
    00300   }
    00301
    00307   /* virtual */ void OnInvalidateData(int data = 0, bool gui_scope = true)
    00308   {
    00309     if (!gui_scope) return;
    00310     this->vscroll->SetCount(this->CountLines());
    00311     this->SetWidgetDirty(WID_GOAL_LIST);
    00312   }
    00313 };
    00314
    00316 static const NWidgetPart _nested_goals_list_widgets[] = {
    00317   NWidget(NWID_HORIZONTAL),
    00318     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
    00319     NWidget(WWT_CAPTION, COLOUR_BROWN, WID_GOAL_CAPTION), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00320     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
    00321     NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
    00322     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
    00323   EndContainer(),
    00324   NWidget(NWID_HORIZONTAL),
    00325     NWidget(WWT_PANEL, COLOUR_BROWN), SetDataTip(0x0, STR_GOALS_TOOLTIP_CLICK_ON_SERVICE_TO_CENTER), SetScrollbar(WID_GOAL_SCROLLBAR),
    00326       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),
    00327     EndContainer(),
    00328     NWidget(NWID_VERTICAL),
    00329       NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_GOAL_SCROLLBAR),
    00330       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
    00331     EndContainer(),
    00332   EndContainer(),
    00333 };
    00334
    00335 static WindowDesc _goals_list_desc(
    00336   WDP_AUTO, "list_goals", 500, 127,
    00337   WC_GOALS_LIST, WC_NONE,
    00338   0,
    00339   _nested_goals_list_widgets, lengthof(_nested_goals_list_widgets)
    00340 );
    00341
    00346 void ShowGoalsList(CompanyID company)
    00347 {
    00348   if (!Company::IsValidID(company)) company = (CompanyID)INVALID_COMPANY;
    00349
    00350   AllocateWindowDescFront<GoalListWindow>(&_goals_list_desc, company);
    00351 }
    00352
    00354 struct GoalQuestionWindow : public Window {
    00355   char *question;
    00356   int buttons;
    00357   int button[3];
    00358   byte type;
    00359
    00360   GoalQuestionWindow(WindowDesc *desc, WindowNumber window_number, byte type, uint32 button_mask, const char *question) : Window(desc), type(type)
    00361   {
    00362     assert(type < GOAL_QUESTION_TYPE_COUNT);
    00363     this->question = strdup(question);
    00364
    00365     /* Figure out which buttons we have to enable. */
    00366     uint bit;
    00367     int n = 0;
    00368     FOR_EACH_SET_BIT(bit, button_mask) {
    00369       if (bit >= GOAL_QUESTION_BUTTON_COUNT) break;
    00370       this->button[n++] = bit;
    00371       if (n == 3) break;
    00372     }
    00373     this->buttons = n;
    00374     assert(this->buttons > 0 && this->buttons < 4);
    00375
    00376     this->CreateNestedTree();
    00377     this->GetWidget<NWidgetStacked>(WID_GQ_BUTTONS)->SetDisplayedPlane(this->buttons - 1);
    00378     this->FinishInitNested(window_number);
    00379   }
    00380
    00381   ~GoalQuestionWindow()
    00382   {
    00383     free(this->question);
    00384   }
    00385
    00386   /* virtual */ void SetStringParameters(int widget) const
    00387   {
    00388     switch (widget) {
    00389       case WID_GQ_CAPTION:
    00390         SetDParam(0, STR_GOAL_QUESTION_CAPTION_QUESTION + this->type);
    00391         break;
    00392
    00393       case WID_GQ_BUTTON_1:
    00394         SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[0]);
    00395         break;
    00396
    00397       case WID_GQ_BUTTON_2:
    00398         SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[1]);
    00399         break;
    00400
    00401       case WID_GQ_BUTTON_3:
    00402         SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[2]);
    00403         break;
    00404     }
    00405   }
    00406
    00407   /* virtual */ void OnClick(Point pt, int widget, int click_count)
    00408   {
    00409     switch (widget) {
    00410       case WID_GQ_BUTTON_1:
    00411         DoCommandP(0, this->window_number, this->button[0], CMD_GOAL_QUESTION_ANSWER);
    00412         delete this;
    00413         break;
    00414
    00415       case WID_GQ_BUTTON_2:
    00416         DoCommandP(0, this->window_number, this->button[1], CMD_GOAL_QUESTION_ANSWER);
    00417         delete this;
    00418         break;
    00419
    00420       case WID_GQ_BUTTON_3:
    00421         DoCommandP(0, this->window_number, this->button[2], CMD_GOAL_QUESTION_ANSWER);
    00422         delete this;
    00423         break;
    00424     }
    00425   }
    00426
    00427   /* virtual */ void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00428   {
    00429     if (widget != WID_GQ_QUESTION) return;
    00430
    00431     SetDParamStr(0, this->question);
    00432     size->height = GetStringHeight(STR_JUST_RAW_STRING, size->width) + WD_PAR_VSEP_WIDE;
    00433   }
    00434
    00435   /* virtual */ void DrawWidget(const Rect &r, int widget) const
    00436   {
    00437     if (widget != WID_GQ_QUESTION) return;
    00438
    00439     SetDParamStr(0, this->question);
    00440     DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_JUST_RAW_STRING, TC_BLACK, SA_TOP | SA_HOR_CENTER);
    00441   }
    00442 };
    00443
    00445 static const NWidgetPart _nested_goal_question_widgets[] = {
    00446   NWidget(NWID_HORIZONTAL),
    00447     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
    00448     NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE, WID_GQ_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00449   EndContainer(),
    00450   NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE),
    00451     NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GQ_QUESTION), SetMinimalSize(300, 0), SetPadding(8, 8, 8, 8), SetFill(1, 0),
    00452     NWidget(NWID_SELECTION, INVALID_COLOUR, WID_GQ_BUTTONS),
    00453       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(85, 10, 85),
    00454         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_1), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
    00455       EndContainer(),
    00456       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(65, 10, 65),
    00457         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_1), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
    00458         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_2), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
    00459       EndContainer(),
    00460       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(25, 10, 25),
    00461         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_1), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
    00462         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_2), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
    00463         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_3), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
    00464       EndContainer(),
    00465     EndContainer(),
    00466     NWidget(NWID_SPACER), SetMinimalSize(0, 8),
    00467   EndContainer(),
    00468 };
    00469
    00470 static WindowDesc _goal_question_list_desc(
    00471   WDP_CENTER, NULL, 0, 0,
    00472   WC_GOAL_QUESTION, WC_NONE,
    00473   WDF_CONSTRUCTION,
    00474   _nested_goal_question_widgets, lengthof(_nested_goal_question_widgets)
    00475 );
    00476
    00484 void ShowGoalQuestion(uint16 id, byte type, uint32 button_mask, const char *question)
    00485 {
    00486   new GoalQuestionWindow(&_goal_question_list_desc, id, type, button_mask, question);
    00487 }