object_gui.cpp

Go to the documentation of this file.
00001 /* $Id: object_gui.cpp 21455 2010-12-10 21:34:36Z rubidium $ */
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 "core/geometry_func.hpp"
00015 #include "newgrf.h"
00016 #include "newgrf_object.h"
00017 #include "newgrf_text.h"
00018 #include "sprite.h"
00019 #include "strings_func.h"
00020 #include "viewport_func.h"
00021 #include "widgets/dropdown_type.h"
00022 #include "window_gui.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 enum BuildObjectWidgets {
00032   BOW_CLASS_DROPDOWN, 
00033   BOW_OBJECT_LIST,    
00034   BOW_SCROLLBAR,      
00035   BOW_OBJECT_MATRIX,  
00036   BOW_OBJECT_SPRITE,  
00037   BOW_OBJECT_SIZE,    
00038   BOW_INFO,           
00039 };
00040 
00042 class BuildObjectWindow : public PickerWindowBase {
00043   static const int OBJECT_MARGIN = 4; 
00044   int line_height;                    
00045   int object_height;                  
00046   int info_height;                    
00047   Scrollbar *vscroll;                 
00048 
00050   static DropDownList *BuildObjectClassDropDown()
00051   {
00052     DropDownList *list = new DropDownList();
00053 
00054     for (uint i = 0; i < ObjectClass::GetCount(); i++) {
00055       list->push_back(new DropDownListStringItem(ObjectClass::GetName((ObjectClassID)i), i, false));
00056     }
00057 
00058     return list;
00059   }
00060 
00061 public:
00062   BuildObjectWindow(const WindowDesc *desc, Window *w) : PickerWindowBase(w), info_height(1)
00063   {
00064     this->CreateNestedTree(desc);
00065 
00066     this->vscroll = this->GetScrollbar(BOW_SCROLLBAR);
00067     this->vscroll->SetCapacity(5);
00068     this->vscroll->SetPosition(0);
00069 
00070     this->FinishInitNested(desc, 0);
00071 
00072     this->vscroll->SetCount(ObjectClass::GetCount(_selected_object_class));
00073     this->SelectFirstAvailableObject(true);
00074     this->GetWidget<NWidgetMatrix>(BOW_OBJECT_MATRIX)->SetCount(4);
00075   }
00076 
00077   virtual ~BuildObjectWindow()
00078   {
00079   }
00080 
00081   virtual void SetStringParameters(int widget) const
00082   {
00083     switch (widget) {
00084       case BOW_CLASS_DROPDOWN:
00085         SetDParam(0, ObjectClass::GetName(_selected_object_class));
00086         break;
00087 
00088       case BOW_OBJECT_SIZE: {
00089         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00090         int size = spec == NULL ? 0 : spec->size;
00091         SetDParam(0, GB(size, HasBit(_selected_object_view, 0) ? 4 : 0, 4));
00092         SetDParam(1, GB(size, HasBit(_selected_object_view, 0) ? 0 : 4, 4));
00093         break;
00094       }
00095 
00096       default: break;
00097     }
00098   }
00099 
00100   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00101   {
00102     switch (widget) {
00103       case BOW_CLASS_DROPDOWN: {
00104         Dimension d = {0, 0};
00105         for (uint i = 0; i < ObjectClass::GetCount(); i++) {
00106           SetDParam(0, ObjectClass::GetName((ObjectClassID)i));
00107           d = maxdim(d, GetStringBoundingBox(STR_BLACK_STRING));
00108         }
00109         d.width += padding.width;
00110         d.height += padding.height;
00111         *size = maxdim(*size, d);
00112         break;
00113       }
00114 
00115       case BOW_OBJECT_LIST: {
00116         for (int i = 0; i < NUM_OBJECTS; i++) {
00117           const ObjectSpec *spec = ObjectSpec::Get(i);
00118           if (!spec->enabled) continue;
00119 
00120           size->width = max(size->width, GetStringBoundingBox(spec->name).width);
00121         }
00122 
00123         this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00124         size->height = this->vscroll->GetCapacity() * this->line_height;
00125         break;
00126       }
00127 
00128       case BOW_OBJECT_MATRIX: {
00129         /* Get the right amount of buttons based on the current spec. */
00130         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00131         if (spec != NULL) {
00132           if (spec->views >= 2) size->width  += resize->width;
00133           if (spec->views >= 4) size->height += resize->height;
00134         }
00135         break;
00136       }
00137 
00138       case BOW_OBJECT_SPRITE: {
00139         bool two_wide = false;  // Whether there will be two widgets next to eachother in the matrix or not.
00140         int height[2] = {0, 0}; // The height for the different views; in this case views 1/2 and 4.
00141 
00142         /* Get the height and view information. */
00143         for (int i = 0; i < NUM_OBJECTS; i++) {
00144           const ObjectSpec *spec = ObjectSpec::Get(i);
00145           if (!spec->enabled) continue;
00146           two_wide |= spec->views >= 2;
00147           height[spec->views / 4] = max<int>(ObjectSpec::Get(i)->height, height[spec->views / 4]);
00148         }
00149 
00150         /* Determine the pixel heights. */
00151         for (size_t i = 0; i < lengthof(height); i++) {
00152           height[i] *= TILE_HEIGHT;
00153           height[i] += TILE_PIXELS + 2 * OBJECT_MARGIN;
00154         }
00155 
00156         /* Now determine the size of the minimum widgets. When there are two columns, then
00157          * we want these columns to be slightly less wide. When there are two rows, then
00158          * determine the size of the widgets based on the maximum size for a single row
00159          * of widgets, or just the twice the widget height of the two row ones. */
00160         size->height = max(height[0], height[1] * 2 + 2);
00161         if (two_wide) {
00162           size->width  = (3 * TILE_PIXELS + 2 * OBJECT_MARGIN) * 2 + 2;
00163         } else {
00164           size->width  = 4 * TILE_PIXELS + 2 * OBJECT_MARGIN;
00165         }
00166 
00167         /* Get the right size for the single widget based on the current spec. */
00168         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00169         if (spec != NULL) {
00170           if (spec->views >= 2) size->width  = size->width  / 2 - 1;
00171           if (spec->views >= 4) size->height = size->height / 2 - 1;
00172         }
00173         break;
00174       }
00175 
00176       case BOW_INFO:
00177         size->height = this->info_height;
00178         break;
00179 
00180       default: break;
00181     }
00182   }
00183 
00184   virtual void DrawWidget(const Rect &r, int widget) const
00185   {
00186     switch (GB(widget, 0, 16)) {
00187       case BOW_OBJECT_LIST: {
00188         int y = r.top;
00189         for (uint i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < ObjectClass::GetCount(_selected_object_class); i++) {
00190           const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, i);
00191           if (!spec->IsAvailable()) {
00192             GfxFillRect(r.left + 1, y + 1, r.right - 1, y + this->line_height - 2, 0, FILLRECT_CHECKER);
00193           }
00194           DrawString(r.left + WD_MATRIX_LEFT, r.right + WD_MATRIX_RIGHT, y + WD_MATRIX_TOP, spec->name, ((int)i == _selected_object_index) ? TC_WHITE : TC_BLACK);
00195           y += this->line_height;
00196         }
00197         break;
00198       }
00199 
00200       case BOW_OBJECT_SPRITE: {
00201         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00202         if (spec == NULL) break;
00203 
00204         DrawPixelInfo tmp_dpi;
00205         /* Set up a clipping area for the preview. */
00206         if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
00207           DrawPixelInfo *old_dpi = _cur_dpi;
00208           _cur_dpi = &tmp_dpi;
00209           if (spec->grf_prop.grffile == NULL) {
00210             extern const DrawTileSprites _objects[];
00211             const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
00212             DrawOrigTileSeqInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, dts, PAL_NONE);
00213           } else {
00214             DrawNewObjectTileInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, spec, GB(widget, 16, 16));
00215           }
00216           _cur_dpi = old_dpi;
00217         }
00218         break;
00219       }
00220 
00221       case BOW_INFO: {
00222         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00223         if (spec == NULL) break;
00224 
00225         /* Get the extra message for the GUI */
00226         if (HasBit(spec->callback_mask, CBM_OBJ_FUND_MORE_TEXT)) {
00227           uint16 callback_res = GetObjectCallback(CBID_OBJECT_FUND_MORE_TEXT, 0, 0, spec, NULL, INVALID_TILE);
00228           if (callback_res != CALLBACK_FAILED) {
00229             StringID message = GetGRFStringID(spec->grf_prop.grffile->grfid, 0xD000 + callback_res);
00230             if (message != STR_NULL && message != STR_UNDEFINED) {
00231               PrepareTextRefStackUsage(6);
00232               /* Use all the available space left from where we stand up to the
00233                * end of the window. We ALSO enlarge the window if needed, so we
00234                * can 'go' wild with the bottom of the window. */
00235               int y = DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, message) - r.top;
00236               StopTextRefStackUsage();
00237               if (y > this->info_height) {
00238                 BuildObjectWindow *bow = const_cast<BuildObjectWindow *>(this);
00239                 bow->info_height = y + 2;
00240                 bow->ReInit();
00241               }
00242             }
00243           }
00244         }
00245       }
00246     }
00247   }
00248 
00249   void SelectOtherObject(int object_index)
00250   {
00251     _selected_object_index = object_index;
00252     if (_selected_object_index != -1) {
00253       const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00254       _selected_object_view = min(_selected_object_view, spec->views - 1);
00255       this->ReInit();
00256     } else {
00257       _selected_object_view = 0;
00258     }
00259 
00260     this->GetWidget<NWidgetMatrix>(BOW_OBJECT_MATRIX)->SetClicked(_selected_object_view);
00261     this->UpdateSelectSize();
00262     this->SetDirty();
00263   }
00264 
00265   void UpdateSelectSize()
00266   {
00267     if (_selected_object_index == -1) {
00268       SetTileSelectSize(1, 1);
00269     } else {
00270       const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00271       int w = GB(spec->size, HasBit(_selected_object_view, 0) ? 4 : 0, 4);
00272       int h = GB(spec->size, HasBit(_selected_object_view, 0) ? 0 : 4, 4);
00273       SetTileSelectSize(w, h);
00274     }
00275   }
00276 
00277   virtual void OnClick(Point pt, int widget, int click_count)
00278   {
00279     switch (GB(widget, 0, 16)) {
00280       case BOW_CLASS_DROPDOWN:
00281         ShowDropDownList(this, BuildObjectClassDropDown(), _selected_object_class, BOW_CLASS_DROPDOWN);
00282         break;
00283 
00284       case BOW_OBJECT_LIST: {
00285         int num_clicked = this->vscroll->GetPosition() + (pt.y - this->nested_array[widget]->pos_y) / this->line_height;
00286         if (num_clicked >= this->vscroll->GetCount()) break;
00287         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, num_clicked);
00288         if (spec->IsAvailable()) this->SelectOtherObject(num_clicked);
00289         break;
00290       }
00291 
00292       case BOW_OBJECT_SPRITE:
00293         if (_selected_object_index != -1) {
00294           _selected_object_view = GB(widget, 16, 16);
00295           this->GetWidget<NWidgetMatrix>(BOW_OBJECT_MATRIX)->SetClicked(_selected_object_view);
00296           this->UpdateSelectSize();
00297           this->SetDirty();
00298         }
00299         break;
00300     }
00301   }
00302 
00308   void SelectFirstAvailableObject(bool change_class)
00309   {
00310     /* First try to select an object in the selected class. */
00311     for (uint i = 0; i < ObjectClass::GetCount(_selected_object_class); i++) {
00312       const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, i);
00313       if (spec->IsAvailable()) {
00314         this->SelectOtherObject(0);
00315         return;
00316       }
00317     }
00318     if (change_class) {
00319       /* If that fails, select the first available object
00320        * from a random class. */
00321       for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
00322         for (uint i = 0; i < ObjectClass::GetCount(j); i++) {
00323           const ObjectSpec *spec = ObjectClass::Get(j, i);
00324           if (spec->IsAvailable()) {
00325             _selected_object_class = j;
00326             this->SelectOtherObject(i);
00327             return;
00328           }
00329         }
00330       }
00331     }
00332     /* If all objects are unavailable, select nothing. */
00333     this->SelectOtherObject(-1);
00334   }
00335 
00336   virtual void OnDropdownSelect(int widget, int index)
00337   {
00338     assert(widget == BOW_CLASS_DROPDOWN);
00339     _selected_object_class = (ObjectClassID)index;
00340     this->vscroll->SetCount(ObjectClass::GetCount(_selected_object_class));
00341     this->SelectFirstAvailableObject(false);
00342   }
00343 };
00344 
00345 static const NWidgetPart _nested_build_object_widgets[] = {
00346   NWidget(NWID_HORIZONTAL),
00347     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00348     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_OBJECT_BUILD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00349   EndContainer(),
00350   NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(1, 0),
00351     NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetPadding(2, 5, 2, 5), SetDataTip(STR_OBJECT_BUILD_CLASS_LABEL, STR_NULL), SetFill(1, 0),
00352     NWidget(WWT_DROPDOWN, COLOUR_GREY, BOW_CLASS_DROPDOWN), SetPadding(0, 5, 2, 5), SetFill(1, 0), SetDataTip(STR_BLACK_STRING, STR_OBJECT_BUILD_TOOLTIP),
00353     NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 2, 5),
00354       NWidget(WWT_MATRIX, COLOUR_GREY, BOW_OBJECT_LIST), SetFill(1, 0), SetDataTip(0x501, STR_OBJECT_BUILD_TOOLTIP), SetScrollbar(BOW_SCROLLBAR),
00355       NWidget(NWID_VSCROLLBAR, COLOUR_GREY, BOW_SCROLLBAR),
00356     EndContainer(),
00357     NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 0, 5),
00358       NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, BOW_OBJECT_MATRIX), SetPIP(0, 2, 0),
00359         NWidget(WWT_PANEL, COLOUR_GREY, BOW_OBJECT_SPRITE), SetDataTip(0x0, STR_OBJECT_BUILD_PREVIEW_TOOLTIP), EndContainer(),
00360       EndContainer(),
00361     EndContainer(),
00362     NWidget(WWT_TEXT, COLOUR_DARK_GREEN, BOW_OBJECT_SIZE), SetDataTip(STR_OBJECT_BUILD_SIZE, STR_NULL), SetPadding(2, 5, 2, 5),
00363     NWidget(WWT_EMPTY, COLOUR_DARK_GREEN, BOW_INFO), SetPadding(2, 5, 0, 5),
00364   EndContainer(),
00365 };
00366 
00367 static const WindowDesc _build_object_desc(
00368   WDP_AUTO, 0, 0,
00369   WC_BUILD_OBJECT, WC_BUILD_TOOLBAR,
00370   WDF_CONSTRUCTION,
00371   _nested_build_object_widgets, lengthof(_nested_build_object_widgets)
00372 );
00373 
00378 void ShowBuildObjectPicker(Window *w)
00379 {
00380   new BuildObjectWindow(&_build_object_desc, w);
00381 }
00382 
00384 void InitializeObjectGui()
00385 {
00386   _selected_object_class = (ObjectClassID)0;
00387 }
00388 
00394 void PlaceProc_Object(TileIndex tile)
00395 {
00396   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);
00397 }

Generated on Fri Dec 31 17:15:35 2010 for OpenTTD by  doxygen 1.6.1