goal_gui.cpp

Go to the documentation of this file.
00001 /* $Id: goal_gui.cpp 26460 2014-04-13 10:47:39Z frosch $ */
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     if (this->IsShaded()) return; // Don't draw anything when the window is shaded.
00277 
00278     /* Calculate progress column width. */
00279     uint max_width = 0;
00280     Goal *s;
00281     FOR_ALL_GOALS(s) {
00282       if (s->progress != NULL) {
00283         SetDParamStr(0, s->progress);
00284         StringID str = s->completed ? STR_GOALS_PROGRESS_COMPLETE : STR_GOALS_PROGRESS;
00285         uint str_width = GetStringBoundingBox(str).width;
00286         if (str_width > max_width) max_width = str_width;
00287       }
00288     }
00289 
00290     NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_GOAL_LIST);
00291     uint progress_col_width = min(max_width, wid->current_x);
00292 
00293     /* Draw goal list. */
00294     this->DrawListColumn(GC_PROGRESS, wid, progress_col_width);
00295     this->DrawListColumn(GC_GOAL, wid, progress_col_width);
00296 
00297   }
00298 
00299   /* virtual */ void OnResize()
00300   {
00301     this->vscroll->SetCapacityFromWidget(this, WID_GOAL_LIST);
00302   }
00303 
00309   /* virtual */ void OnInvalidateData(int data = 0, bool gui_scope = true)
00310   {
00311     if (!gui_scope) return;
00312     this->vscroll->SetCount(this->CountLines());
00313     this->SetWidgetDirty(WID_GOAL_LIST);
00314   }
00315 };
00316 
00318 static const NWidgetPart _nested_goals_list_widgets[] = {
00319   NWidget(NWID_HORIZONTAL),
00320     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00321     NWidget(WWT_CAPTION, COLOUR_BROWN, WID_GOAL_CAPTION), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00322     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00323     NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
00324     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00325   EndContainer(),
00326   NWidget(NWID_HORIZONTAL),
00327     NWidget(WWT_PANEL, COLOUR_BROWN), SetDataTip(0x0, STR_GOALS_TOOLTIP_CLICK_ON_SERVICE_TO_CENTER), SetScrollbar(WID_GOAL_SCROLLBAR),
00328       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),
00329     EndContainer(),
00330     NWidget(NWID_VERTICAL),
00331       NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_GOAL_SCROLLBAR),
00332       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00333     EndContainer(),
00334   EndContainer(),
00335 };
00336 
00337 static WindowDesc _goals_list_desc(
00338   WDP_AUTO, "list_goals", 500, 127,
00339   WC_GOALS_LIST, WC_NONE,
00340   0,
00341   _nested_goals_list_widgets, lengthof(_nested_goals_list_widgets)
00342 );
00343 
00348 void ShowGoalsList(CompanyID company)
00349 {
00350   if (!Company::IsValidID(company)) company = (CompanyID)INVALID_COMPANY;
00351 
00352   AllocateWindowDescFront<GoalListWindow>(&_goals_list_desc, company);
00353 }
00354 
00356 struct GoalQuestionWindow : public Window {
00357   char *question; 
00358   int buttons;    
00359   int button[3];  
00360   byte type;      
00361 
00362   GoalQuestionWindow(WindowDesc *desc, WindowNumber window_number, byte type, uint32 button_mask, const char *question) : Window(desc), type(type)
00363   {
00364     assert(type < GOAL_QUESTION_TYPE_COUNT);
00365     this->question = strdup(question);
00366 
00367     /* Figure out which buttons we have to enable. */
00368     uint bit;
00369     int n = 0;
00370     FOR_EACH_SET_BIT(bit, button_mask) {
00371       if (bit >= GOAL_QUESTION_BUTTON_COUNT) break;
00372       this->button[n++] = bit;
00373       if (n == 3) break;
00374     }
00375     this->buttons = n;
00376     assert(this->buttons > 0 && this->buttons < 4);
00377 
00378     this->CreateNestedTree();
00379     this->GetWidget<NWidgetStacked>(WID_GQ_BUTTONS)->SetDisplayedPlane(this->buttons - 1);
00380     this->FinishInitNested(window_number);
00381   }
00382 
00383   ~GoalQuestionWindow()
00384   {
00385     free(this->question);
00386   }
00387 
00388   /* virtual */ void SetStringParameters(int widget) const
00389   {
00390     switch (widget) {
00391       case WID_GQ_CAPTION:
00392         SetDParam(0, STR_GOAL_QUESTION_CAPTION_QUESTION + this->type);
00393         break;
00394 
00395       case WID_GQ_BUTTON_1:
00396         SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[0]);
00397         break;
00398 
00399       case WID_GQ_BUTTON_2:
00400         SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[1]);
00401         break;
00402 
00403       case WID_GQ_BUTTON_3:
00404         SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[2]);
00405         break;
00406     }
00407   }
00408 
00409   /* virtual */ void OnClick(Point pt, int widget, int click_count)
00410   {
00411     switch (widget) {
00412       case WID_GQ_BUTTON_1:
00413         DoCommandP(0, this->window_number, this->button[0], CMD_GOAL_QUESTION_ANSWER);
00414         delete this;
00415         break;
00416 
00417       case WID_GQ_BUTTON_2:
00418         DoCommandP(0, this->window_number, this->button[1], CMD_GOAL_QUESTION_ANSWER);
00419         delete this;
00420         break;
00421 
00422       case WID_GQ_BUTTON_3:
00423         DoCommandP(0, this->window_number, this->button[2], CMD_GOAL_QUESTION_ANSWER);
00424         delete this;
00425         break;
00426     }
00427   }
00428 
00429   /* virtual */ void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00430   {
00431     if (widget != WID_GQ_QUESTION) return;
00432 
00433     SetDParamStr(0, this->question);
00434     size->height = GetStringHeight(STR_JUST_RAW_STRING, size->width) + WD_PAR_VSEP_WIDE;
00435   }
00436 
00437   /* virtual */ void DrawWidget(const Rect &r, int widget) const
00438   {
00439     if (widget != WID_GQ_QUESTION) return;
00440 
00441     SetDParamStr(0, this->question);
00442     DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_JUST_RAW_STRING, TC_BLACK, SA_TOP | SA_HOR_CENTER);
00443   }
00444 };
00445 
00447 static const NWidgetPart _nested_goal_question_widgets[] = {
00448   NWidget(NWID_HORIZONTAL),
00449     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
00450     NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE, WID_GQ_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00451   EndContainer(),
00452   NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE),
00453     NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GQ_QUESTION), SetMinimalSize(300, 0), SetPadding(8, 8, 8, 8), SetFill(1, 0),
00454     NWidget(NWID_SELECTION, INVALID_COLOUR, WID_GQ_BUTTONS),
00455       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(85, 10, 85),
00456         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_1), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
00457       EndContainer(),
00458       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(65, 10, 65),
00459         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_1), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
00460         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_2), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
00461       EndContainer(),
00462       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(25, 10, 25),
00463         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_1), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
00464         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_2), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
00465         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_3), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
00466       EndContainer(),
00467     EndContainer(),
00468     NWidget(NWID_SPACER), SetMinimalSize(0, 8),
00469   EndContainer(),
00470 };
00471 
00472 static WindowDesc _goal_question_list_desc(
00473   WDP_CENTER, NULL, 0, 0,
00474   WC_GOAL_QUESTION, WC_NONE,
00475   WDF_CONSTRUCTION,
00476   _nested_goal_question_widgets, lengthof(_nested_goal_question_widgets)
00477 );
00478 
00486 void ShowGoalQuestion(uint16 id, byte type, uint32 button_mask, const char *question)
00487 {
00488   new GoalQuestionWindow(&_goal_question_list_desc, id, type, button_mask, question);
00489 }