object_gui.cpp

Go to the documentation of this file.
00001 /* $Id: object_gui.cpp 26653 2014-06-17 19:14:59Z 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 "command_func.h"
00014 #include "newgrf.h"
00015 #include "newgrf_object.h"
00016 #include "newgrf_text.h"
00017 #include "strings_func.h"
00018 #include "viewport_func.h"
00019 #include "window_gui.h"
00020 #include "window_func.h"
00021 
00022 #include "widgets/object_widget.h"
00023 
00024 #include "table/strings.h"
00025 
00026 static ObjectClassID _selected_object_class; 
00027 static int _selected_object_index;           
00028 static uint8 _selected_object_view;          
00029 
00031 class BuildObjectWindow : public PickerWindowBase {
00032   static const int OBJECT_MARGIN = 4; 
00033   int line_height;                    
00034   int info_height;                    
00035   Scrollbar *vscroll;                 
00036 
00038   void EnsureSelectedObjectClassIsVisible()
00039   {
00040     uint pos = 0;
00041     for (int i = 0; i < _selected_object_class; i++) {
00042       if (ObjectClass::Get((ObjectClassID) i)->GetUISpecCount() == 0) continue;
00043       pos++;
00044     }
00045     this->vscroll->ScrollTowards(pos);
00046   }
00047 
00052   bool CanRestoreSelectedObject()
00053   {
00054     if (_selected_object_index == -1) return false;
00055 
00056     ObjectClass *sel_objclass = ObjectClass::Get(_selected_object_class);
00057     if ((int)sel_objclass->GetSpecCount() <= _selected_object_index) return false;
00058 
00059     return sel_objclass->GetSpec(_selected_object_index)->IsAvailable();
00060   }
00061 
00066   uint GetMatrixColumnCount()
00067   {
00068     const NWidgetBase *matrix = this->GetWidget<NWidgetBase>(WID_BO_SELECT_MATRIX);
00069     return 1 + (matrix->current_x - matrix->smallest_x) / matrix->resize_x;
00070   }
00071 
00072 public:
00073   BuildObjectWindow(WindowDesc *desc, Window *w) : PickerWindowBase(desc, w), info_height(1)
00074   {
00075     this->CreateNestedTree();
00076     this->vscroll = this->GetScrollbar(WID_BO_SCROLLBAR);
00077     this->FinishInitNested(0);
00078 
00079     this->vscroll->SetPosition(0);
00080     this->vscroll->SetCount(ObjectClass::GetUIClassCount());
00081 
00082     NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX);
00083     matrix->SetScrollbar(this->GetScrollbar(WID_BO_SELECT_SCROLL));
00084 
00085     this->SelectOtherClass(_selected_object_class);
00086     if (this->CanRestoreSelectedObject()) {
00087       this->SelectOtherObject(_selected_object_index);
00088     } else {
00089       this->SelectFirstAvailableObject(true);
00090     }
00091     assert(ObjectClass::Get(_selected_object_class)->GetUISpecCount() > 0); // object GUI should be disables elsewise
00092     this->EnsureSelectedObjectClassIsVisible();
00093     this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetCount(4);
00094   }
00095 
00096   virtual void SetStringParameters(int widget) const
00097   {
00098     switch (widget) {
00099       case WID_BO_OBJECT_NAME: {
00100         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00101         SetDParam(0, spec != NULL ? spec->name : STR_EMPTY);
00102         break;
00103       }
00104 
00105       case WID_BO_OBJECT_SIZE: {
00106         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00107         int size = spec == NULL ? 0 : spec->size;
00108         SetDParam(0, GB(size, HasBit(_selected_object_view, 0) ? 4 : 0, 4));
00109         SetDParam(1, GB(size, HasBit(_selected_object_view, 0) ? 0 : 4, 4));
00110         break;
00111       }
00112 
00113       default: break;
00114     }
00115   }
00116 
00117   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00118   {
00119     switch (widget) {
00120       case WID_BO_CLASS_LIST: {
00121         for (uint i = 0; i < ObjectClass::GetClassCount(); i++) {
00122           ObjectClass *objclass = ObjectClass::Get((ObjectClassID)i);
00123           if (objclass->GetUISpecCount() == 0) continue;
00124           size->width = max(size->width, GetStringBoundingBox(objclass->name).width);
00125         }
00126         size->width += padding.width;
00127         this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00128         resize->height = this->line_height;
00129         size->height = 5 * this->line_height;
00130         break;
00131       }
00132 
00133       case WID_BO_OBJECT_NAME:
00134       case WID_BO_OBJECT_SIZE:
00135         /* We do not want the window to resize when selecting objects; better clip texts */
00136         size->width = 0;
00137         break;
00138 
00139       case WID_BO_OBJECT_MATRIX: {
00140         /* Get the right amount of buttons based on the current spec. */
00141         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00142         if (spec != NULL) {
00143           if (spec->views >= 2) size->width  += resize->width;
00144           if (spec->views >= 4) size->height += resize->height;
00145         }
00146         resize->width = 0;
00147         resize->height = 0;
00148         break;
00149       }
00150 
00151       case WID_BO_OBJECT_SPRITE: {
00152         bool two_wide = false;  // Whether there will be two widgets next to each other in the matrix or not.
00153         int height[2] = {0, 0}; // The height for the different views; in this case views 1/2 and 4.
00154 
00155         /* Get the height and view information. */
00156         for (int i = 0; i < NUM_OBJECTS; i++) {
00157           const ObjectSpec *spec = ObjectSpec::Get(i);
00158           if (!spec->IsEverAvailable()) continue;
00159           two_wide |= spec->views >= 2;
00160           height[spec->views / 4] = max<int>(ObjectSpec::Get(i)->height, height[spec->views / 4]);
00161         }
00162 
00163         /* Determine the pixel heights. */
00164         for (size_t i = 0; i < lengthof(height); i++) {
00165           height[i] *= TILE_HEIGHT;
00166           height[i] += TILE_PIXELS + 2 * OBJECT_MARGIN;
00167         }
00168 
00169         /* Now determine the size of the minimum widgets. When there are two columns, then
00170          * we want these columns to be slightly less wide. When there are two rows, then
00171          * determine the size of the widgets based on the maximum size for a single row
00172          * of widgets, or just the twice the widget height of the two row ones. */
00173         size->height = max(height[0], height[1] * 2 + 2);
00174         if (two_wide) {
00175           size->width  = (3 * TILE_PIXELS + 2 * OBJECT_MARGIN) * 2 + 2;
00176         } else {
00177           size->width  = 4 * TILE_PIXELS + 2 * OBJECT_MARGIN;
00178         }
00179 
00180         /* Get the right size for the single widget based on the current spec. */
00181         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00182         if (spec != NULL) {
00183           if (spec->views >= 2) size->width  = size->width  / 2 - 1;
00184           if (spec->views >= 4) size->height = size->height / 2 - 1;
00185         }
00186         break;
00187       }
00188 
00189       case WID_BO_INFO:
00190         size->height = this->info_height;
00191         break;
00192 
00193       case WID_BO_SELECT_MATRIX:
00194         fill->height = 1;
00195         resize->height = 1;
00196         break;
00197 
00198       default: break;
00199     }
00200   }
00201 
00202   virtual void DrawWidget(const Rect &r, int widget) const
00203   {
00204     switch (GB(widget, 0, 16)) {
00205       case WID_BO_CLASS_LIST: {
00206         int y = r.top;
00207         uint pos = 0;
00208         for (uint i = 0; i < ObjectClass::GetClassCount(); i++) {
00209           ObjectClass *objclass = ObjectClass::Get((ObjectClassID)i);
00210           if (objclass->GetUISpecCount() == 0) continue;
00211           if (!this->vscroll->IsVisible(pos++)) continue;
00212           DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, y + WD_MATRIX_TOP, objclass->name,
00213               ((int)i == _selected_object_class) ? TC_WHITE : TC_BLACK);
00214           y += this->line_height;
00215         }
00216         break;
00217       }
00218 
00219       case WID_BO_OBJECT_SPRITE: {
00220         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00221         if (spec == NULL) break;
00222 
00223         /* Height of the selection matrix.
00224          * Depending on the number of views, the matrix has a 1x1, 1x2, 2x1 or 2x2 layout. To make the previews
00225          * look nice in all layouts, we use the 4x4 layout (smallest previews) as starting point. For the bigger
00226          * previews in the layouts with less views we add space homogeneously on all sides, so the 4x4 preview-rectangle
00227          * is centered in the 2x1, 1x2 resp. 1x1 buttons. */
00228         uint matrix_height = this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->current_y;
00229 
00230         DrawPixelInfo tmp_dpi;
00231         /* Set up a clipping area for the preview. */
00232         if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
00233           DrawPixelInfo *old_dpi = _cur_dpi;
00234           _cur_dpi = &tmp_dpi;
00235           if (spec->grf_prop.grffile == NULL) {
00236             extern const DrawTileSprites _objects[];
00237             const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
00238             DrawOrigTileSeqInGUI((r.right - r.left) / 2 - 1, (r.bottom - r.top + matrix_height / 2) / 2 - OBJECT_MARGIN - TILE_PIXELS, dts, PAL_NONE);
00239           } else {
00240             DrawNewObjectTileInGUI((r.right - r.left) / 2 - 1, (r.bottom - r.top + matrix_height / 2) / 2 - OBJECT_MARGIN - TILE_PIXELS, spec, GB(widget, 16, 16));
00241           }
00242           _cur_dpi = old_dpi;
00243         }
00244         break;
00245       }
00246 
00247       case WID_BO_SELECT_IMAGE: {
00248         ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
00249         int obj_index = objclass->GetIndexFromUI(GB(widget, 16, 16));
00250         if (obj_index < 0) break;
00251         const ObjectSpec *spec = objclass->GetSpec(obj_index);
00252         if (spec == NULL) break;
00253 
00254         if (!spec->IsAvailable()) {
00255           GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
00256         }
00257         DrawPixelInfo tmp_dpi;
00258         /* Set up a clipping area for the preview. */
00259         if (FillDrawPixelInfo(&tmp_dpi, r.left + 1, r.top, (r.right - 1) - (r.left + 1) + 1, r.bottom - r.top + 1)) {
00260           DrawPixelInfo *old_dpi = _cur_dpi;
00261           _cur_dpi = &tmp_dpi;
00262           if (spec->grf_prop.grffile == NULL) {
00263             extern const DrawTileSprites _objects[];
00264             const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
00265             DrawOrigTileSeqInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, dts, PAL_NONE);
00266           } else {
00267             DrawNewObjectTileInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, spec,
00268                 min(_selected_object_view, spec->views - 1));
00269           }
00270           _cur_dpi = old_dpi;
00271         }
00272         break;
00273       }
00274 
00275       case WID_BO_INFO: {
00276         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00277         if (spec == NULL) break;
00278 
00279         /* Get the extra message for the GUI */
00280         if (HasBit(spec->callback_mask, CBM_OBJ_FUND_MORE_TEXT)) {
00281           uint16 callback_res = GetObjectCallback(CBID_OBJECT_FUND_MORE_TEXT, 0, 0, spec, NULL, INVALID_TILE, _selected_object_view);
00282           if (callback_res != CALLBACK_FAILED && callback_res != 0x400) {
00283             if (callback_res > 0x400) {
00284               ErrorUnknownCallbackResult(spec->grf_prop.grffile->grfid, CBID_OBJECT_FUND_MORE_TEXT, callback_res);
00285             } else {
00286               StringID message = GetGRFStringID(spec->grf_prop.grffile->grfid, 0xD000 + callback_res);
00287               if (message != STR_NULL && message != STR_UNDEFINED) {
00288                 StartTextRefStackUsage(spec->grf_prop.grffile, 6);
00289                 /* Use all the available space left from where we stand up to the
00290                  * end of the window. We ALSO enlarge the window if needed, so we
00291                  * can 'go' wild with the bottom of the window. */
00292                 int y = DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, message, TC_ORANGE) - r.top;
00293                 StopTextRefStackUsage();
00294                 if (y > this->info_height) {
00295                   BuildObjectWindow *bow = const_cast<BuildObjectWindow *>(this);
00296                   bow->info_height = y + 2;
00297                   bow->ReInit();
00298                 }
00299               }
00300             }
00301           }
00302         }
00303       }
00304     }
00305   }
00306 
00311   void SelectOtherClass(ObjectClassID object_class_index)
00312   {
00313     _selected_object_class = object_class_index;
00314     this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetCount(ObjectClass::Get(_selected_object_class)->GetUISpecCount());
00315   }
00316 
00321   void SelectOtherObject(int object_index)
00322   {
00323     _selected_object_index = object_index;
00324     if (_selected_object_index != -1) {
00325       const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00326       _selected_object_view = min(_selected_object_view, spec->views - 1);
00327       this->ReInit();
00328     } else {
00329       _selected_object_view = 0;
00330     }
00331 
00332     this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetClicked(_selected_object_view);
00333     this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetClicked(_selected_object_index != -1 ? ObjectClass::Get(_selected_object_class)->GetUIFromIndex(_selected_object_index) : -1);
00334     this->UpdateSelectSize();
00335     this->SetDirty();
00336   }
00337 
00338   void UpdateSelectSize()
00339   {
00340     if (_selected_object_index == -1) {
00341       SetTileSelectSize(1, 1);
00342     } else {
00343       const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00344       int w = GB(spec->size, HasBit(_selected_object_view, 0) ? 4 : 0, 4);
00345       int h = GB(spec->size, HasBit(_selected_object_view, 0) ? 0 : 4, 4);
00346       SetTileSelectSize(w, h);
00347     }
00348   }
00349 
00350   virtual void OnResize()
00351   {
00352     this->vscroll->SetCapacityFromWidget(this, WID_BO_CLASS_LIST);
00353   }
00354 
00355   virtual void OnClick(Point pt, int widget, int click_count)
00356   {
00357     switch (GB(widget, 0, 16)) {
00358       case WID_BO_CLASS_LIST: {
00359         int num_clicked = this->vscroll->GetPosition() + (pt.y - this->nested_array[widget]->pos_y) / this->line_height;
00360         if (num_clicked >= (int)ObjectClass::GetUIClassCount()) break;
00361 
00362         this->SelectOtherClass(ObjectClass::GetUIClass(num_clicked));
00363         this->SelectFirstAvailableObject(false);
00364         break;
00365       }
00366 
00367       case WID_BO_SELECT_IMAGE: {
00368         ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
00369         int num_clicked = objclass->GetIndexFromUI(GB(widget, 16, 16));
00370         if (num_clicked >= 0 && objclass->GetSpec(num_clicked)->IsAvailable()) this->SelectOtherObject(num_clicked);
00371         break;
00372       }
00373 
00374       case WID_BO_OBJECT_SPRITE:
00375         if (_selected_object_index != -1) {
00376           _selected_object_view = GB(widget, 16, 16);
00377           this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetClicked(_selected_object_view);
00378           this->UpdateSelectSize();
00379           this->SetDirty();
00380         }
00381         break;
00382     }
00383   }
00384 
00390   void SelectFirstAvailableObject(bool change_class)
00391   {
00392     /* First try to select an object in the selected class. */
00393     ObjectClass *sel_objclass = ObjectClass::Get(_selected_object_class);
00394     for (uint i = 0; i < sel_objclass->GetSpecCount(); i++) {
00395       const ObjectSpec *spec = sel_objclass->GetSpec(i);
00396       if (spec->IsAvailable()) {
00397         this->SelectOtherObject(i);
00398         return;
00399       }
00400     }
00401     if (change_class) {
00402       /* If that fails, select the first available object
00403        * from a random class. */
00404       for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
00405         ObjectClass *objclass = ObjectClass::Get(j);
00406         for (uint i = 0; i < objclass->GetSpecCount(); i++) {
00407           const ObjectSpec *spec = objclass->GetSpec(i);
00408           if (spec->IsAvailable()) {
00409             this->SelectOtherClass(j);
00410             this->SelectOtherObject(i);
00411             return;
00412           }
00413         }
00414       }
00415     }
00416     /* If all objects are unavailable, select nothing... */
00417     if (ObjectClass::Get(_selected_object_class)->GetUISpecCount() == 0) {
00418       /* ... but make sure that the class is not empty. */
00419       for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
00420         if (ObjectClass::Get(j)->GetUISpecCount() > 0) {
00421           this->SelectOtherClass(j);
00422           break;
00423         }
00424       }
00425     }
00426     this->SelectOtherObject(-1);
00427   }
00428 };
00429 
00430 static const NWidgetPart _nested_build_object_widgets[] = {
00431   NWidget(NWID_HORIZONTAL),
00432     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00433     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_OBJECT_BUILD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00434     NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
00435   EndContainer(),
00436   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00437     NWidget(NWID_HORIZONTAL), SetPadding(2, 0, 0, 0),
00438       NWidget(NWID_VERTICAL),
00439         NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 2, 5),
00440           NWidget(WWT_MATRIX, COLOUR_GREY, WID_BO_CLASS_LIST), SetFill(1, 0), SetMatrixDataTip(1, 0, STR_OBJECT_BUILD_CLASS_TOOLTIP), SetScrollbar(WID_BO_SCROLLBAR),
00441           NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_BO_SCROLLBAR),
00442         EndContainer(),
00443         NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 0, 5),
00444           NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BO_OBJECT_MATRIX), SetPIP(0, 2, 0),
00445             NWidget(WWT_PANEL, COLOUR_GREY, WID_BO_OBJECT_SPRITE), SetDataTip(0x0, STR_OBJECT_BUILD_PREVIEW_TOOLTIP), EndContainer(),
00446           EndContainer(),
00447         EndContainer(),
00448         NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BO_OBJECT_NAME), SetDataTip(STR_ORANGE_STRING, STR_NULL), SetPadding(2, 5, 2, 5),
00449         NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BO_OBJECT_SIZE), SetDataTip(STR_OBJECT_BUILD_SIZE, STR_NULL), SetPadding(2, 5, 2, 5),
00450       EndContainer(),
00451       NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(WID_BO_SELECT_SCROLL),
00452         NWidget(NWID_HORIZONTAL),
00453           NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BO_SELECT_MATRIX), SetFill(0, 1), SetPIP(0, 2, 0),
00454             NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BO_SELECT_IMAGE), SetMinimalSize(66, 60), SetDataTip(0x0, STR_OBJECT_BUILD_TOOLTIP),
00455                 SetFill(0, 0), SetResize(0, 0), SetScrollbar(WID_BO_SELECT_SCROLL),
00456             EndContainer(),
00457           EndContainer(),
00458           NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BO_SELECT_SCROLL),
00459         EndContainer(),
00460       EndContainer(),
00461     EndContainer(),
00462     NWidget(NWID_HORIZONTAL),
00463       NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BO_INFO), SetPadding(2, 5, 0, 5), SetFill(1, 0), SetResize(1, 0),
00464       NWidget(NWID_VERTICAL),
00465         NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
00466         NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
00467       EndContainer(),
00468     EndContainer(),
00469   EndContainer(),
00470 };
00471 
00472 static WindowDesc _build_object_desc(
00473   WDP_AUTO, "build_object", 0, 0,
00474   WC_BUILD_OBJECT, WC_BUILD_TOOLBAR,
00475   WDF_CONSTRUCTION,
00476   _nested_build_object_widgets, lengthof(_nested_build_object_widgets)
00477 );
00478 
00483 void ShowBuildObjectPicker(Window *w)
00484 {
00485   new BuildObjectWindow(&_build_object_desc, w);
00486 }
00487 
00489 void InitializeObjectGui()
00490 {
00491   _selected_object_class = (ObjectClassID)0;
00492 }
00493 
00499 void PlaceProc_Object(TileIndex tile)
00500 {
00501   DoCommandP(tile, ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index)->Index(), _selected_object_view, CMD_BUILD_OBJECT | CMD_MSG(STR_ERROR_CAN_T_BUILD_OBJECT), CcTerraform);
00502 }