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