goal_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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 "command_func.h"
00024 
00025 #include "widgets/goal_widget.h"
00026 
00027 #include "table/strings.h"
00028 
00029 struct GoalListWindow : Window {
00030   Scrollbar *vscroll;
00031 
00032   GoalListWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
00033   {
00034     this->CreateNestedTree(desc);
00035     this->vscroll = this->GetScrollbar(WID_GL_SCROLLBAR);
00036     this->FinishInitNested(desc, window_number);
00037     this->OnInvalidateData(0);
00038   }
00039 
00040   virtual void OnClick(Point pt, int widget, int click_count)
00041   {
00042     if (widget != WID_GL_PANEL) return;
00043 
00044     int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GL_PANEL, WD_FRAMERECT_TOP);
00045     int num = 0;
00046     const Goal *s;
00047     FOR_ALL_GOALS(s) {
00048       if (s->company == INVALID_COMPANY) {
00049         y--;
00050         if (y == 0) {
00051           this->HandleClick(s);
00052           return;
00053         }
00054         num++;
00055       }
00056     }
00057 
00058     if (num == 0) {
00059       y--; // "None"
00060       if (y < 0) return;
00061     }
00062 
00063     y -= 2; // "Company specific goals:"
00064     if (y < 0) return;
00065 
00066     FOR_ALL_GOALS(s) {
00067       if (s->company == _local_company) {
00068         y--;
00069         if (y == 0) {
00070           this->HandleClick(s);
00071           return;
00072         }
00073       }
00074     }
00075   }
00076 
00077   void HandleClick(const Goal *s)
00078   {
00079     /* determine dst coordinate for goal and try to scroll to it */
00080     TileIndex xy;
00081     switch (s->type) {
00082       case GT_NONE: return;
00083       case GT_COMPANY: return;
00084 
00085       case GT_TILE:
00086         if (!IsValidTile(s->dst)) return;
00087         xy = s->dst;
00088         break;
00089 
00090       case GT_INDUSTRY:
00091         if (!Industry::IsValidID(s->dst)) return;
00092         xy = Industry::Get(s->dst)->location.tile;
00093         break;
00094 
00095       case GT_TOWN:
00096         if (!Town::IsValidID(s->dst)) return;
00097         xy = Town::Get(s->dst)->xy;
00098         break;
00099 
00100       default: NOT_REACHED();
00101     }
00102 
00103     if (_ctrl_pressed) {
00104       ShowExtraViewPortWindow(xy);
00105     } else {
00106       ScrollMainWindowToTile(xy);
00107     }
00108   }
00109 
00114   uint CountLines()
00115   {
00116     /* Count number of (non) awarded goals */
00117     uint num_global = 0;
00118     uint num_company = 0;
00119     const Goal *s;
00120     FOR_ALL_GOALS(s) {
00121       if (s->company == INVALID_COMPANY) {
00122         num_global++;
00123       } else if (s->company == _local_company) {
00124         num_company++;
00125       }
00126     }
00127 
00128     /* Count the 'none' lines */
00129     if (num_global  == 0) num_global = 1;
00130     if (num_company == 0) num_company = 1;
00131 
00132     /* Global, company and an empty line before the accepted ones. */
00133     return 3 + num_global + num_company;
00134   }
00135 
00136   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00137   {
00138     if (widget != WID_GL_PANEL) return;
00139     Dimension d = maxdim(GetStringBoundingBox(STR_GOALS_GLOBAL_TITLE), GetStringBoundingBox(STR_GOALS_COMPANY_TITLE));
00140 
00141     resize->height = d.height;
00142 
00143     d.height *= 5;
00144     d.width += padding.width + WD_FRAMERECT_RIGHT + WD_FRAMERECT_LEFT;
00145     d.height += padding.height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00146     *size = maxdim(*size, d);
00147   }
00148 
00153   void DrawPartialGoalList(int &pos, const int cap, int x, int y, int right, bool global_section) const
00154   {
00155     if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, global_section ? STR_GOALS_GLOBAL_TITLE : STR_GOALS_COMPANY_TITLE);
00156     pos++;
00157 
00158     uint num = 0;
00159     const Goal *s;
00160     FOR_ALL_GOALS(s) {
00161       if (global_section ? s->company == INVALID_COMPANY : s->company == _local_company && s->company != INVALID_COMPANY) {
00162         if (IsInsideMM(pos, 0, cap)) {
00163           /* Display the goal */
00164           SetDParamStr(0, s->text);
00165           DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_GOALS_TEXT);
00166         }
00167         pos++;
00168         num++;
00169       }
00170     }
00171 
00172     if (num == 0) {
00173       if (IsInsideMM(pos, 0, cap)) DrawString(x, right, y + pos * FONT_HEIGHT_NORMAL, STR_GOALS_NONE);
00174       pos++;
00175     }
00176   }
00177 
00178   virtual void DrawWidget(const Rect &r, int widget) const
00179   {
00180     if (widget != WID_GL_PANEL) return;
00181 
00182     YearMonthDay ymd;
00183     ConvertDateToYMD(_date, &ymd);
00184 
00185     int right = r.right - WD_FRAMERECT_RIGHT;
00186     int y = r.top + WD_FRAMERECT_TOP;
00187     int x = r.left + WD_FRAMERECT_LEFT;
00188 
00189     int pos = -this->vscroll->GetPosition();
00190     const int cap = this->vscroll->GetCapacity();
00191 
00192     /* Draw partial list with global goals */
00193     DrawPartialGoalList(pos, cap, x, y, right, true);
00194 
00195     /* Draw partial list with company goals */
00196     pos++;
00197     DrawPartialGoalList(pos, cap, x, y, right, false);
00198   }
00199 
00200   virtual void OnResize()
00201   {
00202     this->vscroll->SetCapacityFromWidget(this, WID_GL_PANEL);
00203   }
00204 
00210   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00211   {
00212     if (!gui_scope) return;
00213     this->vscroll->SetCount(this->CountLines());
00214   }
00215 };
00216 
00217 static const NWidgetPart _nested_goals_list_widgets[] = {
00218   NWidget(NWID_HORIZONTAL),
00219     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00220     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_GOALS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00221     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
00222     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
00223   EndContainer(),
00224   NWidget(NWID_HORIZONTAL),
00225     NWidget(WWT_PANEL, COLOUR_BROWN, WID_GL_PANEL), SetDataTip(0x0, STR_GOALS_TOOLTIP_CLICK_ON_SERVICE_TO_CENTER), SetResize(1, 1), SetScrollbar(WID_GL_SCROLLBAR), EndContainer(),
00226     NWidget(NWID_VERTICAL),
00227       NWidget(NWID_VSCROLLBAR, COLOUR_BROWN, WID_GL_SCROLLBAR),
00228       NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
00229     EndContainer(),
00230   EndContainer(),
00231 };
00232 
00233 static const WindowDesc _goals_list_desc(
00234   WDP_AUTO, 500, 127,
00235   WC_GOALS_LIST, WC_NONE,
00236   0,
00237   _nested_goals_list_widgets, lengthof(_nested_goals_list_widgets)
00238 );
00239 
00240 void ShowGoalsList()
00241 {
00242   AllocateWindowDescFront<GoalListWindow>(&_goals_list_desc, 0);
00243 }
00244 
00245 
00246 
00247 struct GoalQuestionWindow : Window {
00248   char *question;
00249   int buttons;
00250   int button[3];
00251   byte type;
00252 
00253   GoalQuestionWindow(const WindowDesc *desc, WindowNumber window_number, byte type, uint32 button_mask, const char *question) : Window(), type(type)
00254   {
00255     assert(type < GOAL_QUESTION_TYPE_COUNT);
00256     this->question = strdup(question);
00257 
00258     /* Figure out which buttons we have to enable */
00259     uint bit;
00260     int n = 0;
00261     FOR_EACH_SET_BIT(bit, button_mask) {
00262       if (bit >= GOAL_QUESTION_BUTTON_COUNT) break;
00263       this->button[n++] = bit;
00264       if (n == 3) break;
00265     }
00266     this->buttons = n;
00267     assert(this->buttons > 0 && this->buttons < 4);
00268 
00269     this->CreateNestedTree(desc);
00270     this->GetWidget<NWidgetStacked>(WID_GQ_BUTTONS)->SetDisplayedPlane(this->buttons - 1);
00271     this->FinishInitNested(desc, window_number);
00272   }
00273 
00274   ~GoalQuestionWindow()
00275   {
00276     free(this->question);
00277   }
00278 
00279   virtual void SetStringParameters(int widget) const
00280   {
00281     switch (widget) {
00282       case WID_GQ_CAPTION:
00283         SetDParam(0, STR_GOAL_QUESTION_CAPTION_QUESTION + this->type);
00284         break;
00285 
00286       case WID_GQ_BUTTON_1:
00287         SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[0]);
00288         break;
00289 
00290       case WID_GQ_BUTTON_2:
00291         SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[1]);
00292         break;
00293 
00294       case WID_GQ_BUTTON_3:
00295         SetDParam(0, STR_GOAL_QUESTION_BUTTON_CANCEL + this->button[2]);
00296         break;
00297     }
00298   }
00299 
00300   virtual void OnClick(Point pt, int widget, int click_count)
00301   {
00302     switch (widget) {
00303       case WID_GQ_BUTTON_1:
00304         DoCommandP(0, this->window_number, this->button[0], CMD_GOAL_QUESTION_ANSWER);
00305         delete this;
00306         break;
00307 
00308       case WID_GQ_BUTTON_2:
00309         DoCommandP(0, this->window_number, this->button[1], CMD_GOAL_QUESTION_ANSWER);
00310         delete this;
00311         break;
00312 
00313       case WID_GQ_BUTTON_3:
00314         DoCommandP(0, this->window_number, this->button[2], CMD_GOAL_QUESTION_ANSWER);
00315         delete this;
00316         break;
00317     }
00318   }
00319 
00320   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00321   {
00322     if (widget != WID_GQ_QUESTION) return;
00323 
00324     SetDParamStr(0, this->question);
00325     size->height = GetStringHeight(STR_JUST_RAW_STRING, size->width) + WD_PAR_VSEP_WIDE;
00326   }
00327 
00328   virtual void DrawWidget(const Rect &r, int widget) const
00329   {
00330     if (widget != WID_GQ_QUESTION) return;
00331 
00332     SetDParamStr(0, this->question);
00333     DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_JUST_RAW_STRING, TC_BLACK, SA_TOP | SA_HOR_CENTER);
00334   }
00335 };
00336 
00337 static const NWidgetPart _nested_goal_question_widgets[] = {
00338   NWidget(NWID_HORIZONTAL),
00339     NWidget(WWT_CLOSEBOX, COLOUR_LIGHT_BLUE),
00340     NWidget(WWT_CAPTION, COLOUR_LIGHT_BLUE, WID_GQ_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00341   EndContainer(),
00342   NWidget(WWT_PANEL, COLOUR_LIGHT_BLUE),
00343     NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GQ_QUESTION), SetMinimalSize(300, 0), SetPadding(8, 8, 8, 8), SetFill(1, 0),
00344     NWidget(NWID_SELECTION, INVALID_COLOUR, WID_GQ_BUTTONS),
00345       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(85, 10, 85),
00346         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_1), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
00347       EndContainer(),
00348       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(65, 10, 65),
00349         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_1), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
00350         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_2), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
00351       EndContainer(),
00352       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(25, 10, 25),
00353         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_1), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
00354         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_2), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
00355         NWidget(WWT_PUSHTXTBTN, COLOUR_LIGHT_BLUE, WID_GQ_BUTTON_3), SetDataTip(STR_BLACK_STRING, STR_NULL), SetFill(1, 0),
00356       EndContainer(),
00357     EndContainer(),
00358     NWidget(NWID_SPACER), SetMinimalSize(0, 8),
00359   EndContainer(),
00360 };
00361 
00362 static const WindowDesc _goal_question_list_desc(
00363   WDP_CENTER, 0, 0,
00364   WC_GOAL_QUESTION, WC_NONE,
00365   WDF_CONSTRUCTION,
00366   _nested_goal_question_widgets, lengthof(_nested_goal_question_widgets)
00367 );
00368 
00369 
00370 void ShowGoalQuestion(uint16 id, byte type, uint32 button_mask, const char *question)
00371 {
00372   new GoalQuestionWindow(&_goal_question_list_desc, id, type, button_mask, question);
00373 }