object_gui.cpp

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

Generated on Thu Jan 20 22:57:37 2011 for OpenTTD by  doxygen 1.6.1