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         DrawPixelInfo tmp_dpi;
00185         /* Set up a clipping area for the preview. */
00186         if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
00187           DrawPixelInfo *old_dpi = _cur_dpi;
00188           _cur_dpi = &tmp_dpi;
00189           if (spec->grf_prop.grffile == NULL) {
00190             extern const DrawTileSprites _objects[];
00191             const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
00192             DrawOrigTileSeqInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, dts, PAL_NONE);
00193           } else {
00194             DrawNewObjectTileInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, spec, GB(widget, 16, 16));
00195           }
00196           _cur_dpi = old_dpi;
00197         }
00198         break;
00199       }
00200 
00201       case WID_BO_SELECT_IMAGE: {
00202         ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
00203         int obj_index = objclass->GetIndexFromUI(GB(widget, 16, 16));
00204         if (obj_index < 0) break;
00205         const ObjectSpec *spec = objclass->GetSpec(obj_index);
00206         if (spec == NULL) break;
00207 
00208         if (!spec->IsAvailable()) {
00209           GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
00210         }
00211         DrawPixelInfo tmp_dpi;
00212         /* Set up a clipping area for the preview. */
00213         if (FillDrawPixelInfo(&tmp_dpi, r.left + 1, r.top, (r.right - 1) - (r.left + 1) + 1, r.bottom - r.top + 1)) {
00214           DrawPixelInfo *old_dpi = _cur_dpi;
00215           _cur_dpi = &tmp_dpi;
00216           if (spec->grf_prop.grffile == NULL) {
00217             extern const DrawTileSprites _objects[];
00218             const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
00219             DrawOrigTileSeqInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, dts, PAL_NONE);
00220           } else {
00221             DrawNewObjectTileInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, spec,
00222                 min(_selected_object_view, spec->views - 1));
00223           }
00224           _cur_dpi = old_dpi;
00225         }
00226         break;
00227       }
00228 
00229       case WID_BO_INFO: {
00230         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00231         if (spec == NULL) break;
00232 
00233         /* Get the extra message for the GUI */
00234         if (HasBit(spec->callback_mask, CBM_OBJ_FUND_MORE_TEXT)) {
00235           uint16 callback_res = GetObjectCallback(CBID_OBJECT_FUND_MORE_TEXT, 0, 0, spec, NULL, INVALID_TILE, _selected_object_view);
00236           if (callback_res != CALLBACK_FAILED && callback_res != 0x400) {
00237             if (callback_res > 0x400) {
00238               ErrorUnknownCallbackResult(spec->grf_prop.grffile->grfid, CBID_OBJECT_FUND_MORE_TEXT, callback_res);
00239             } else {
00240               StringID message = GetGRFStringID(spec->grf_prop.grffile->grfid, 0xD000 + callback_res);
00241               if (message != STR_NULL && message != STR_UNDEFINED) {
00242                 StartTextRefStackUsage(6);
00243                 /* Use all the available space left from where we stand up to the
00244                  * end of the window. We ALSO enlarge the window if needed, so we
00245                  * can 'go' wild with the bottom of the window. */
00246                 int y = DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, message, TC_ORANGE) - r.top;
00247                 StopTextRefStackUsage();
00248                 if (y > this->info_height) {
00249                   BuildObjectWindow *bow = const_cast<BuildObjectWindow *>(this);
00250                   bow->info_height = y + 2;
00251                   bow->ReInit();
00252                 }
00253               }
00254             }
00255           }
00256         }
00257       }
00258     }
00259   }
00260 
00265   void SelectOtherObject(int object_index)
00266   {
00267     _selected_object_index = object_index;
00268     if (_selected_object_index != -1) {
00269       const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00270       _selected_object_view = min(_selected_object_view, spec->views - 1);
00271       this->ReInit();
00272     } else {
00273       _selected_object_view = 0;
00274     }
00275 
00276     this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetClicked(_selected_object_view);
00277     this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetClicked(_selected_object_index != -1 ? ObjectClass::Get(_selected_object_class)->GetUIFromIndex(_selected_object_index) : -1);
00278     this->UpdateSelectSize();
00279     this->SetDirty();
00280   }
00281 
00282   void UpdateSelectSize()
00283   {
00284     if (_selected_object_index == -1) {
00285       SetTileSelectSize(1, 1);
00286     } else {
00287       const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00288       int w = GB(spec->size, HasBit(_selected_object_view, 0) ? 4 : 0, 4);
00289       int h = GB(spec->size, HasBit(_selected_object_view, 0) ? 0 : 4, 4);
00290       SetTileSelectSize(w, h);
00291     }
00292   }
00293 
00294   virtual void OnResize()
00295   {
00296     this->vscroll->SetCapacityFromWidget(this, WID_BO_CLASS_LIST);
00297     this->GetWidget<NWidgetCore>(WID_BO_CLASS_LIST)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00298   }
00299 
00300   virtual void OnClick(Point pt, int widget, int click_count)
00301   {
00302     switch (GB(widget, 0, 16)) {
00303       case WID_BO_CLASS_LIST: {
00304         int num_clicked = this->vscroll->GetPosition() + (pt.y - this->nested_array[widget]->pos_y) / this->line_height;
00305         if (num_clicked >= (int)ObjectClass::GetUIClassCount()) break;
00306 
00307         _selected_object_class = ObjectClass::GetUIClass(num_clicked);
00308         this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetCount(ObjectClass::Get(_selected_object_class)->GetUISpecCount());
00309         this->SelectFirstAvailableObject(false);
00310         break;
00311       }
00312 
00313       case WID_BO_SELECT_IMAGE: {
00314         ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
00315         int num_clicked = objclass->GetIndexFromUI(GB(widget, 16, 16));
00316         if (num_clicked >= 0 && objclass->GetSpec(num_clicked)->IsAvailable()) this->SelectOtherObject(num_clicked);
00317         break;
00318       }
00319 
00320       case WID_BO_OBJECT_SPRITE:
00321         if (_selected_object_index != -1) {
00322           _selected_object_view = GB(widget, 16, 16);
00323           this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetClicked(_selected_object_view);
00324           this->UpdateSelectSize();
00325           this->SetDirty();
00326         }
00327         break;
00328     }
00329   }
00330 
00336   void SelectFirstAvailableObject(bool change_class)
00337   {
00338     /* First try to select an object in the selected class. */
00339     ObjectClass *sel_objclass = ObjectClass::Get(_selected_object_class);
00340     for (uint i = 0; i < sel_objclass->GetSpecCount(); i++) {
00341       const ObjectSpec *spec = sel_objclass->GetSpec(i);
00342       if (spec->IsAvailable()) {
00343         this->SelectOtherObject(i);
00344         return;
00345       }
00346     }
00347     if (change_class) {
00348       /* If that fails, select the first available object
00349        * from a random class. */
00350       for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
00351         ObjectClass *objclass = ObjectClass::Get(j);
00352         for (uint i = 0; i < objclass->GetSpecCount(); i++) {
00353           const ObjectSpec *spec = objclass->GetSpec(i);
00354           if (spec->IsAvailable()) {
00355             _selected_object_class = j;
00356             this->SelectOtherObject(i);
00357             return;
00358           }
00359         }
00360       }
00361     }
00362     /* If all objects are unavailable, select nothing... */
00363     if (ObjectClass::Get(_selected_object_class)->GetUISpecCount() == 0) {
00364       /* ... but make sure that the class is not empty. */
00365       for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
00366         if (ObjectClass::Get(j)->GetUISpecCount() > 0) {
00367           _selected_object_class = j;
00368           break;
00369         }
00370       }
00371     }
00372     this->SelectOtherObject(-1);
00373   }
00374 };
00375 
00376 static const NWidgetPart _nested_build_object_widgets[] = {
00377   NWidget(NWID_HORIZONTAL),
00378     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00379     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_OBJECT_BUILD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00380   EndContainer(),
00381   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00382     NWidget(NWID_HORIZONTAL), SetPadding(2, 0, 0, 0),
00383       NWidget(NWID_VERTICAL),
00384         NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 2, 5),
00385           NWidget(WWT_MATRIX, COLOUR_GREY, WID_BO_CLASS_LIST), SetFill(1, 0), SetDataTip(0x501, STR_OBJECT_BUILD_CLASS_TOOLTIP), SetScrollbar(WID_BO_SCROLLBAR),
00386           NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_BO_SCROLLBAR),
00387         EndContainer(),
00388         NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 0, 5),
00389           NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BO_OBJECT_MATRIX), SetPIP(0, 2, 0),
00390             NWidget(WWT_PANEL, COLOUR_GREY, WID_BO_OBJECT_SPRITE), SetDataTip(0x0, STR_OBJECT_BUILD_PREVIEW_TOOLTIP), EndContainer(),
00391           EndContainer(),
00392         EndContainer(),
00393         NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BO_OBJECT_NAME), SetDataTip(STR_ORANGE_STRING, STR_NULL), SetPadding(2, 5, 2, 5),
00394         NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BO_OBJECT_SIZE), SetDataTip(STR_OBJECT_BUILD_SIZE, STR_NULL), SetPadding(2, 5, 2, 5),
00395       EndContainer(),
00396       NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(WID_BO_SELECT_SCROLL),
00397         NWidget(NWID_HORIZONTAL),
00398           NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BO_SELECT_MATRIX), SetFill(0, 1), SetPIP(0, 2, 0),
00399             NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BO_SELECT_IMAGE), SetMinimalSize(66, 60), SetDataTip(0x0, STR_OBJECT_BUILD_TOOLTIP),
00400                 SetFill(0, 0), SetResize(0, 0), SetScrollbar(WID_BO_SELECT_SCROLL),
00401             EndContainer(),
00402           EndContainer(),
00403           NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BO_SELECT_SCROLL),
00404         EndContainer(),
00405       EndContainer(),
00406     EndContainer(),
00407     NWidget(NWID_HORIZONTAL),
00408       NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BO_INFO), SetPadding(2, 5, 0, 5), SetFill(1, 0), SetResize(1, 0),
00409       NWidget(NWID_VERTICAL),
00410         NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
00411         NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
00412       EndContainer(),
00413     EndContainer(),
00414   EndContainer(),
00415 };
00416 
00417 static const WindowDesc _build_object_desc(
00418   WDP_AUTO, 0, 0,
00419   WC_BUILD_OBJECT, WC_BUILD_TOOLBAR,
00420   WDF_CONSTRUCTION,
00421   _nested_build_object_widgets, lengthof(_nested_build_object_widgets)
00422 );
00423 
00428 void ShowBuildObjectPicker(Window *w)
00429 {
00430   new BuildObjectWindow(&_build_object_desc, w);
00431 }
00432 
00434 void InitializeObjectGui()
00435 {
00436   _selected_object_class = (ObjectClassID)0;
00437 }
00438 
00444 void PlaceProc_Object(TileIndex tile)
00445 {
00446   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);
00447 }