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) {
00237             StringID message = GetGRFStringID(spec->grf_prop.grffile->grfid, 0xD000 + callback_res);
00238             if (message != STR_NULL && message != STR_UNDEFINED) {
00239               PrepareTextRefStackUsage(6);
00240               /* Use all the available space left from where we stand up to the
00241                * end of the window. We ALSO enlarge the window if needed, so we
00242                * can 'go' wild with the bottom of the window. */
00243               int y = DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, message) - r.top;
00244               StopTextRefStackUsage();
00245               if (y > this->info_height) {
00246                 BuildObjectWindow *bow = const_cast<BuildObjectWindow *>(this);
00247                 bow->info_height = y + 2;
00248                 bow->ReInit();
00249               }
00250             }
00251           }
00252         }
00253       }
00254     }
00255   }
00256 
00261   void SelectOtherObject(int object_index)
00262   {
00263     _selected_object_index = object_index;
00264     if (_selected_object_index != -1) {
00265       const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00266       _selected_object_view = min(_selected_object_view, spec->views - 1);
00267       this->ReInit();
00268     } else {
00269       _selected_object_view = 0;
00270     }
00271 
00272     this->GetWidget<NWidgetMatrix>(BOW_OBJECT_MATRIX)->SetClicked(_selected_object_view);
00273     this->GetWidget<NWidgetMatrix>(BOW_SELECT_MATRIX)->SetClicked(_selected_object_index);
00274     this->UpdateSelectSize();
00275     this->SetDirty();
00276   }
00277 
00278   void UpdateSelectSize()
00279   {
00280     if (_selected_object_index == -1) {
00281       SetTileSelectSize(1, 1);
00282     } else {
00283       const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00284       int w = GB(spec->size, HasBit(_selected_object_view, 0) ? 4 : 0, 4);
00285       int h = GB(spec->size, HasBit(_selected_object_view, 0) ? 0 : 4, 4);
00286       SetTileSelectSize(w, h);
00287     }
00288   }
00289 
00290   virtual void OnResize()
00291   {
00292     this->vscroll->SetCapacityFromWidget(this, BOW_CLASS_LIST);
00293     this->GetWidget<NWidgetCore>(BOW_CLASS_LIST)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00294   }
00295 
00296   virtual void OnClick(Point pt, int widget, int click_count)
00297   {
00298     switch (GB(widget, 0, 16)) {
00299       case BOW_CLASS_LIST: {
00300         int num_clicked = this->vscroll->GetPosition() + (pt.y - this->nested_array[widget]->pos_y) / this->line_height;
00301         if (num_clicked >= (int)ObjectClass::GetCount()) break;
00302 
00303         _selected_object_class = (ObjectClassID)num_clicked;
00304         this->GetWidget<NWidgetMatrix>(BOW_SELECT_MATRIX)->SetCount(ObjectClass::GetCount(_selected_object_class));
00305         this->SelectFirstAvailableObject(false);
00306         break;
00307       }
00308 
00309       case BOW_SELECT_IMAGE: {
00310         int num_clicked = GB(widget, 16, 16);
00311         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, num_clicked);
00312         if (spec->IsAvailable()) this->SelectOtherObject(num_clicked);
00313         break;
00314       }
00315 
00316       case BOW_OBJECT_SPRITE:
00317         if (_selected_object_index != -1) {
00318           _selected_object_view = GB(widget, 16, 16);
00319           this->GetWidget<NWidgetMatrix>(BOW_OBJECT_MATRIX)->SetClicked(_selected_object_view);
00320           this->UpdateSelectSize();
00321           this->SetDirty();
00322         }
00323         break;
00324     }
00325   }
00326 
00332   void SelectFirstAvailableObject(bool change_class)
00333   {
00334     /* First try to select an object in the selected class. */
00335     for (uint i = 0; i < ObjectClass::GetCount(_selected_object_class); i++) {
00336       const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, i);
00337       if (spec->IsAvailable()) {
00338         this->SelectOtherObject(0);
00339         return;
00340       }
00341     }
00342     if (change_class) {
00343       /* If that fails, select the first available object
00344        * from a random class. */
00345       for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
00346         for (uint i = 0; i < ObjectClass::GetCount(j); i++) {
00347           const ObjectSpec *spec = ObjectClass::Get(j, i);
00348           if (spec->IsAvailable()) {
00349             _selected_object_class = j;
00350             this->SelectOtherObject(i);
00351             return;
00352           }
00353         }
00354       }
00355     }
00356     /* If all objects are unavailable, select nothing. */
00357     this->SelectOtherObject(-1);
00358   }
00359 };
00360 
00361 static const NWidgetPart _nested_build_object_widgets[] = {
00362   NWidget(NWID_HORIZONTAL),
00363     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00364     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_OBJECT_BUILD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00365   EndContainer(),
00366   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00367     NWidget(NWID_HORIZONTAL), SetPadding(2, 0, 0, 0),
00368       NWidget(NWID_VERTICAL),
00369         NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 2, 5),
00370           NWidget(WWT_MATRIX, COLOUR_GREY, BOW_CLASS_LIST), SetFill(1, 0), SetDataTip(0x501, STR_OBJECT_BUILD_CLASS_TOOLTIP), SetScrollbar(BOW_SCROLLBAR),
00371           NWidget(NWID_VSCROLLBAR, COLOUR_GREY, BOW_SCROLLBAR),
00372         EndContainer(),
00373         NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 0, 5),
00374           NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, BOW_OBJECT_MATRIX), SetPIP(0, 2, 0),
00375             NWidget(WWT_PANEL, COLOUR_GREY, BOW_OBJECT_SPRITE), SetDataTip(0x0, STR_OBJECT_BUILD_PREVIEW_TOOLTIP), EndContainer(),
00376           EndContainer(),
00377         EndContainer(),
00378         NWidget(WWT_TEXT, COLOUR_DARK_GREEN, BOW_OBJECT_SIZE), SetDataTip(STR_OBJECT_BUILD_SIZE, STR_NULL), SetPadding(2, 5, 2, 5),
00379       EndContainer(),
00380       NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(BOW_SELECT_SCROLL),
00381         NWidget(NWID_HORIZONTAL),
00382           NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, BOW_SELECT_MATRIX), SetFill(0, 1), SetPIP(0, 2, 0),
00383             NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BOW_SELECT_IMAGE), SetMinimalSize(66, 60), SetDataTip(0x0, STR_OBJECT_BUILD_TOOLTIP),
00384                 SetFill(0, 0), SetResize(0, 0), SetScrollbar(BOW_SELECT_SCROLL),
00385             EndContainer(),
00386           EndContainer(),
00387           NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, BOW_SELECT_SCROLL),
00388         EndContainer(),
00389       EndContainer(),
00390     EndContainer(),
00391     NWidget(NWID_HORIZONTAL),
00392       NWidget(WWT_EMPTY, INVALID_COLOUR, BOW_INFO), SetPadding(2, 5, 0, 5), SetFill(1, 0), SetResize(1, 0),
00393       NWidget(NWID_VERTICAL),
00394         NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
00395         NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
00396       EndContainer(),
00397     EndContainer(),
00398   EndContainer(),
00399 };
00400 
00401 static const WindowDesc _build_object_desc(
00402   WDP_AUTO, 0, 0,
00403   WC_BUILD_OBJECT, WC_BUILD_TOOLBAR,
00404   WDF_CONSTRUCTION,
00405   _nested_build_object_widgets, lengthof(_nested_build_object_widgets)
00406 );
00407 
00412 void ShowBuildObjectPicker(Window *w)
00413 {
00414   new BuildObjectWindow(&_build_object_desc, w);
00415 }
00416 
00418 void InitializeObjectGui()
00419 {
00420   _selected_object_class = (ObjectClassID)0;
00421 }
00422 
00428 void PlaceProc_Object(TileIndex tile)
00429 {
00430   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);
00431 }

Generated on Mon May 9 05:18:57 2011 for OpenTTD by  doxygen 1.6.1