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 "sprite.h"
00018 #include "strings_func.h"
00019 #include "viewport_func.h"
00020 #include "window_gui.h"
00021 
00022 #include "table/strings.h"
00023 
00024 static ObjectClassID _selected_object_class; 
00025 static int _selected_object_index;           
00026 static uint8 _selected_object_view;          
00027 
00029 enum BuildObjectWidgets {
00030   BOW_CLASS_LIST,     
00031   BOW_SCROLLBAR,      
00032   BOW_OBJECT_MATRIX,  
00033   BOW_OBJECT_SPRITE,  
00034   BOW_OBJECT_SIZE,    
00035   BOW_INFO,           
00036 
00037   BOW_SELECT_MATRIX,  
00038   BOW_SELECT_IMAGE,   
00039   BOW_SELECT_SCROLL,  
00040 };
00041 
00043 class BuildObjectWindow : public PickerWindowBase {
00044   static const int OBJECT_MARGIN = 4; 
00045   int line_height;                    
00046   int object_height;                  
00047   int info_height;                    
00048   Scrollbar *vscroll;                 
00049 
00050 public:
00051   BuildObjectWindow(const WindowDesc *desc, Window *w) : PickerWindowBase(w), info_height(1)
00052   {
00053     this->CreateNestedTree(desc);
00054 
00055     this->vscroll = this->GetScrollbar(BOW_SCROLLBAR);
00056     this->vscroll->SetCapacity(5);
00057     this->vscroll->SetPosition(0);
00058     this->vscroll->SetCount(ObjectClass::GetCount());
00059 
00060     this->FinishInitNested(desc, 0);
00061 
00062     this->SelectFirstAvailableObject(true);
00063     this->GetWidget<NWidgetMatrix>(BOW_OBJECT_MATRIX)->SetCount(4);
00064 
00065     NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(BOW_SELECT_MATRIX);
00066     matrix->SetScrollbar(this->GetScrollbar(BOW_SELECT_SCROLL));
00067     matrix->SetCount(ObjectClass::GetCount(_selected_object_class));
00068   }
00069 
00070   virtual ~BuildObjectWindow()
00071   {
00072   }
00073 
00074   virtual void SetStringParameters(int widget) const
00075   {
00076     switch (widget) {
00077       case BOW_OBJECT_SIZE: {
00078         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00079         int size = spec == NULL ? 0 : spec->size;
00080         SetDParam(0, GB(size, HasBit(_selected_object_view, 0) ? 4 : 0, 4));
00081         SetDParam(1, GB(size, HasBit(_selected_object_view, 0) ? 0 : 4, 4));
00082         break;
00083       }
00084 
00085       default: break;
00086     }
00087   }
00088 
00089   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00090   {
00091     switch (widget) {
00092       case BOW_CLASS_LIST: {
00093         for (uint i = 0; i < ObjectClass::GetCount(); i++) {
00094           size->width = max(size->width, GetStringBoundingBox(ObjectClass::GetName((ObjectClassID)i)).width);
00095         }
00096         size->width += padding.width;
00097         this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00098         resize->height = this->line_height;
00099         size->height = this->vscroll->GetCapacity() * this->line_height;
00100         break;
00101       }
00102 
00103       case BOW_OBJECT_MATRIX: {
00104         /* Get the right amount of buttons based on the current spec. */
00105         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00106         if (spec != NULL) {
00107           if (spec->views >= 2) size->width  += resize->width;
00108           if (spec->views >= 4) size->height += resize->height;
00109         }
00110         resize->width = 0;
00111         resize->height = 0;
00112         break;
00113       }
00114 
00115       case BOW_OBJECT_SPRITE: {
00116         bool two_wide = false;  // Whether there will be two widgets next to eachother in the matrix or not.
00117         int height[2] = {0, 0}; // The height for the different views; in this case views 1/2 and 4.
00118 
00119         /* Get the height and view information. */
00120         for (int i = 0; i < NUM_OBJECTS; i++) {
00121           const ObjectSpec *spec = ObjectSpec::Get(i);
00122           if (!spec->enabled) continue;
00123           two_wide |= spec->views >= 2;
00124           height[spec->views / 4] = max<int>(ObjectSpec::Get(i)->height, height[spec->views / 4]);
00125         }
00126 
00127         /* Determine the pixel heights. */
00128         for (size_t i = 0; i < lengthof(height); i++) {
00129           height[i] *= TILE_HEIGHT;
00130           height[i] += TILE_PIXELS + 2 * OBJECT_MARGIN;
00131         }
00132 
00133         /* Now determine the size of the minimum widgets. When there are two columns, then
00134          * we want these columns to be slightly less wide. When there are two rows, then
00135          * determine the size of the widgets based on the maximum size for a single row
00136          * of widgets, or just the twice the widget height of the two row ones. */
00137         size->height = max(height[0], height[1] * 2 + 2);
00138         if (two_wide) {
00139           size->width  = (3 * TILE_PIXELS + 2 * OBJECT_MARGIN) * 2 + 2;
00140         } else {
00141           size->width  = 4 * TILE_PIXELS + 2 * OBJECT_MARGIN;
00142         }
00143 
00144         /* Get the right size for the single widget based on the current spec. */
00145         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00146         if (spec != NULL) {
00147           if (spec->views >= 2) size->width  = size->width  / 2 - 1;
00148           if (spec->views >= 4) size->height = size->height / 2 - 1;
00149         }
00150         break;
00151       }
00152 
00153       case BOW_INFO:
00154         size->height = this->info_height;
00155         break;
00156 
00157       case BOW_SELECT_MATRIX:
00158         fill->height = 1;
00159         resize->height = 1;
00160         break;
00161 
00162       default: break;
00163     }
00164   }
00165 
00166   virtual void DrawWidget(const Rect &r, int widget) const
00167   {
00168     switch (GB(widget, 0, 16)) {
00169       case BOW_CLASS_LIST: {
00170         int y = r.top;
00171         for (uint i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < ObjectClass::GetCount(); i++) {
00172           SetDParam(0, ObjectClass::GetName((ObjectClassID)i));
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 BOW_OBJECT_SPRITE: {
00181         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _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 BOW_SELECT_IMAGE: {
00202         if (_selected_object_index < 0) break;
00203 
00204         int obj_index = GB(widget, 16, 16);
00205         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, 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 BOW_INFO: {
00230         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _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, _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>(BOW_OBJECT_MATRIX)->SetClicked(_selected_object_view);
00277     this->GetWidget<NWidgetMatrix>(BOW_SELECT_MATRIX)->SetClicked(_selected_object_index);
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, _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, BOW_CLASS_LIST);
00297     this->GetWidget<NWidgetCore>(BOW_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 BOW_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::GetCount()) break;
00306 
00307         _selected_object_class = (ObjectClassID)num_clicked;
00308         this->GetWidget<NWidgetMatrix>(BOW_SELECT_MATRIX)->SetCount(ObjectClass::GetCount(_selected_object_class));
00309         this->SelectFirstAvailableObject(false);
00310         break;
00311       }
00312 
00313       case BOW_SELECT_IMAGE: {
00314         int num_clicked = GB(widget, 16, 16);
00315         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, num_clicked);
00316         if (spec->IsAvailable()) this->SelectOtherObject(num_clicked);
00317         break;
00318       }
00319 
00320       case BOW_OBJECT_SPRITE:
00321         if (_selected_object_index != -1) {
00322           _selected_object_view = GB(widget, 16, 16);
00323           this->GetWidget<NWidgetMatrix>(BOW_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     for (uint i = 0; i < ObjectClass::GetCount(_selected_object_class); i++) {
00340       const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, i);
00341       if (spec->IsAvailable()) {
00342         this->SelectOtherObject(0);
00343         return;
00344       }
00345     }
00346     if (change_class) {
00347       /* If that fails, select the first available object
00348        * from a random class. */
00349       for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
00350         for (uint i = 0; i < ObjectClass::GetCount(j); i++) {
00351           const ObjectSpec *spec = ObjectClass::Get(j, i);
00352           if (spec->IsAvailable()) {
00353             _selected_object_class = j;
00354             this->SelectOtherObject(i);
00355             return;
00356           }
00357         }
00358       }
00359     }
00360     /* If all objects are unavailable, select nothing. */
00361     this->SelectOtherObject(-1);
00362   }
00363 };
00364 
00365 static const NWidgetPart _nested_build_object_widgets[] = {
00366   NWidget(NWID_HORIZONTAL),
00367     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00368     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_OBJECT_BUILD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00369   EndContainer(),
00370   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00371     NWidget(NWID_HORIZONTAL), SetPadding(2, 0, 0, 0),
00372       NWidget(NWID_VERTICAL),
00373         NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 2, 5),
00374           NWidget(WWT_MATRIX, COLOUR_GREY, BOW_CLASS_LIST), SetFill(1, 0), SetDataTip(0x501, STR_OBJECT_BUILD_CLASS_TOOLTIP), SetScrollbar(BOW_SCROLLBAR),
00375           NWidget(NWID_VSCROLLBAR, COLOUR_GREY, BOW_SCROLLBAR),
00376         EndContainer(),
00377         NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 0, 5),
00378           NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, BOW_OBJECT_MATRIX), SetPIP(0, 2, 0),
00379             NWidget(WWT_PANEL, COLOUR_GREY, BOW_OBJECT_SPRITE), SetDataTip(0x0, STR_OBJECT_BUILD_PREVIEW_TOOLTIP), EndContainer(),
00380           EndContainer(),
00381         EndContainer(),
00382         NWidget(WWT_TEXT, COLOUR_DARK_GREEN, BOW_OBJECT_SIZE), SetDataTip(STR_OBJECT_BUILD_SIZE, STR_NULL), SetPadding(2, 5, 2, 5),
00383       EndContainer(),
00384       NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(BOW_SELECT_SCROLL),
00385         NWidget(NWID_HORIZONTAL),
00386           NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, BOW_SELECT_MATRIX), SetFill(0, 1), SetPIP(0, 2, 0),
00387             NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BOW_SELECT_IMAGE), SetMinimalSize(66, 60), SetDataTip(0x0, STR_OBJECT_BUILD_TOOLTIP),
00388                 SetFill(0, 0), SetResize(0, 0), SetScrollbar(BOW_SELECT_SCROLL),
00389             EndContainer(),
00390           EndContainer(),
00391           NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, BOW_SELECT_SCROLL),
00392         EndContainer(),
00393       EndContainer(),
00394     EndContainer(),
00395     NWidget(NWID_HORIZONTAL),
00396       NWidget(WWT_EMPTY, INVALID_COLOUR, BOW_INFO), SetPadding(2, 5, 0, 5), SetFill(1, 0), SetResize(1, 0),
00397       NWidget(NWID_VERTICAL),
00398         NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
00399         NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
00400       EndContainer(),
00401     EndContainer(),
00402   EndContainer(),
00403 };
00404 
00405 static const WindowDesc _build_object_desc(
00406   WDP_AUTO, 0, 0,
00407   WC_BUILD_OBJECT, WC_BUILD_TOOLBAR,
00408   WDF_CONSTRUCTION,
00409   _nested_build_object_widgets, lengthof(_nested_build_object_widgets)
00410 );
00411 
00416 void ShowBuildObjectPicker(Window *w)
00417 {
00418   new BuildObjectWindow(&_build_object_desc, w);
00419 }
00420 
00422 void InitializeObjectGui()
00423 {
00424   _selected_object_class = (ObjectClassID)0;
00425 }
00426 
00432 void PlaceProc_Object(TileIndex tile)
00433 {
00434   DoCommandP(tile, ObjectClass::Get(_selected_object_class, _selected_object_index)->Index(), _selected_object_view, CMD_BUILD_OBJECT | CMD_MSG(STR_ERROR_CAN_T_BUILD_OBJECT), CcTerraform);
00435 }