# 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_NAME:
    00100       case WID_BO_OBJECT_SIZE:
    00101         /* We do not want the window to resize when selecting objects; better clip texts */
    00102         size->width = 0;
    00103         break;
    00104
    00105       case WID_BO_OBJECT_MATRIX: {
    00106         /* Get the right amount of buttons based on the current spec. */
    00107         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_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 WID_BO_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->IsEverAvailable()) 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)->GetSpec(_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 WID_BO_INFO:
    00156         size->height = this->info_height;
    00157         break;
    00158
    00159       case WID_BO_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 WID_BO_CLASS_LIST: {
    00172         int y = r.top;
    00173         uint pos = 0;
    00174         for (uint i = 0; i < ObjectClass::GetClassCount(); i++) {
    00175           ObjectClass *objclass = ObjectClass::Get((ObjectClassID)i);
    00176           if (objclass->GetUISpecCount() == 0) continue;
    00177           if (!this->vscroll->IsVisible(pos++)) continue;
    00178           SetDParam(0, objclass->name);
    00179           DrawString(r.left + WD_MATRIX_LEFT, r.right + WD_MATRIX_RIGHT, y + WD_MATRIX_TOP, STR_JUST_STRING,
    00180               ((int)i == _selected_object_class) ? TC_WHITE : TC_BLACK);
    00181           y += this->line_height;
    00182         }
    00183         break;
    00184       }
    00185
    00186       case WID_BO_OBJECT_SPRITE: {
    00187         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
    00188         if (spec == NULL) break;
    00189
    00190         /* Height of the selection matrix.
    00191          * Depending on the number of views, the matrix has a 1x1, 1x2, 2x1 or 2x2 layout. To make the previews
    00192          * look nice in all layouts, we use the 4x4 layout (smallest previews) as starting point. For the bigger
    00193          * previews in the layouts with less views we add space homogenously on all sides, so the 4x4 preview-rectangle
    00194          * is centered in the 2x1, 1x2 resp. 1x1 buttons. */
    00195         uint matrix_height = this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->current_y;
    00196
    00197         DrawPixelInfo tmp_dpi;
    00198         /* Set up a clipping area for the preview. */
    00199         if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
    00200           DrawPixelInfo *old_dpi = _cur_dpi;
    00201           _cur_dpi = &tmp_dpi;
    00202           if (spec->grf_prop.grffile == NULL) {
    00203             extern const DrawTileSprites _objects[];
    00204             const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
    00205             DrawOrigTileSeqInGUI((r.right - r.left) / 2 - 1, (r.bottom - r.top + matrix_height / 2) / 2 - OBJECT_MARGIN - TILE_PIXELS, dts, PAL_NONE);
    00206           } else {
    00207             DrawNewObjectTileInGUI((r.right - r.left) / 2 - 1, (r.bottom - r.top + matrix_height / 2) / 2 - OBJECT_MARGIN - TILE_PIXELS, spec, GB(widget, 16, 16));
    00208           }
    00209           _cur_dpi = old_dpi;
    00210         }
    00211         break;
    00212       }
    00213
    00214       case WID_BO_SELECT_IMAGE: {
    00215         ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
    00216         int obj_index = objclass->GetIndexFromUI(GB(widget, 16, 16));
    00217         if (obj_index < 0) break;
    00218         const ObjectSpec *spec = objclass->GetSpec(obj_index);
    00219         if (spec == NULL) break;
    00220
    00221         if (!spec->IsAvailable()) {
    00222           GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
    00223         }
    00224         DrawPixelInfo tmp_dpi;
    00225         /* Set up a clipping area for the preview. */
    00226         if (FillDrawPixelInfo(&tmp_dpi, r.left + 1, r.top, (r.right - 1) - (r.left + 1) + 1, r.bottom - r.top + 1)) {
    00227           DrawPixelInfo *old_dpi = _cur_dpi;
    00228           _cur_dpi = &tmp_dpi;
    00229           if (spec->grf_prop.grffile == NULL) {
    00230             extern const DrawTileSprites _objects[];
    00231             const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
    00232             DrawOrigTileSeqInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, dts, PAL_NONE);
    00233           } else {
    00234             DrawNewObjectTileInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, spec,
    00235                 min(_selected_object_view, spec->views - 1));
    00236           }
    00237           _cur_dpi = old_dpi;
    00238         }
    00239         break;
    00240       }
    00241
    00242       case WID_BO_INFO: {
    00243         const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
    00244         if (spec == NULL) break;
    00245
    00246         /* Get the extra message for the GUI */
    00247         if (HasBit(spec->callback_mask, CBM_OBJ_FUND_MORE_TEXT)) {
    00248           uint16 callback_res = GetObjectCallback(CBID_OBJECT_FUND_MORE_TEXT, 0, 0, spec, NULL, INVALID_TILE, _selected_object_view);
    00249           if (callback_res != CALLBACK_FAILED && callback_res != 0x400) {
    00250             if (callback_res > 0x400) {
    00251               ErrorUnknownCallbackResult(spec->grf_prop.grffile->grfid, CBID_OBJECT_FUND_MORE_TEXT, callback_res);
    00252             } else {
    00253               StringID message = GetGRFStringID(spec->grf_prop.grffile->grfid, 0xD000 + callback_res);
    00254               if (message != STR_NULL && message != STR_UNDEFINED) {
    00255                 StartTextRefStackUsage(6);
    00256                 /* Use all the available space left from where we stand up to the
    00257                  * end of the window. We ALSO enlarge the window if needed, so we
    00258                  * can 'go' wild with the bottom of the window. */
    00259                 int y = DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, message, TC_ORANGE) - r.top;
    00260                 StopTextRefStackUsage();
    00261                 if (y > this->info_height) {
    00262                   BuildObjectWindow *bow = const_cast<BuildObjectWindow *>(this);
    00263                   bow->info_height = y + 2;
    00264                   bow->ReInit();
    00265                 }
    00266               }
    00267             }
    00268           }
    00269         }
    00270       }
    00271     }
    00272   }
    00273
    00278   void SelectOtherObject(int object_index)
    00279   {
    00280     _selected_object_index = object_index;
    00281     if (_selected_object_index != -1) {
    00282       const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
    00283       _selected_object_view = min(_selected_object_view, spec->views - 1);
    00284       this->ReInit();
    00285     } else {
    00286       _selected_object_view = 0;
    00287     }
    00288
    00289     this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetClicked(_selected_object_view);
    00290     this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetClicked(_selected_object_index != -1 ? ObjectClass::Get(_selected_object_class)->GetUIFromIndex(_selected_object_index) : -1);
    00291     this->UpdateSelectSize();
    00292     this->SetDirty();
    00293   }
    00294
    00295   void UpdateSelectSize()
    00296   {
    00297     if (_selected_object_index == -1) {
    00298       SetTileSelectSize(1, 1);
    00299     } else {
    00300       const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
    00301       int w = GB(spec->size, HasBit(_selected_object_view, 0) ? 4 : 0, 4);
    00302       int h = GB(spec->size, HasBit(_selected_object_view, 0) ? 0 : 4, 4);
    00303       SetTileSelectSize(w, h);
    00304     }
    00305   }
    00306
    00307   virtual void OnResize()
    00308   {
    00309     this->vscroll->SetCapacityFromWidget(this, WID_BO_CLASS_LIST);
    00310     this->GetWidget<NWidgetCore>(WID_BO_CLASS_LIST)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00311   }
    00312
    00313   virtual void OnClick(Point pt, int widget, int click_count)
    00314   {
    00315     switch (GB(widget, 0, 16)) {
    00316       case WID_BO_CLASS_LIST: {
    00317         int num_clicked = this->vscroll->GetPosition() + (pt.y - this->nested_array[widget]->pos_y) / this->line_height;
    00318         if (num_clicked >= (int)ObjectClass::GetUIClassCount()) break;
    00319
    00320         _selected_object_class = ObjectClass::GetUIClass(num_clicked);
    00321         this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetCount(ObjectClass::Get(_selected_object_class)->GetUISpecCount());
    00322         this->SelectFirstAvailableObject(false);
    00323         break;
    00324       }
    00325
    00326       case WID_BO_SELECT_IMAGE: {
    00327         ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
    00328         int num_clicked = objclass->GetIndexFromUI(GB(widget, 16, 16));
    00329         if (num_clicked >= 0 && objclass->GetSpec(num_clicked)->IsAvailable()) this->SelectOtherObject(num_clicked);
    00330         break;
    00331       }
    00332
    00333       case WID_BO_OBJECT_SPRITE:
    00334         if (_selected_object_index != -1) {
    00335           _selected_object_view = GB(widget, 16, 16);
    00336           this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetClicked(_selected_object_view);
    00337           this->UpdateSelectSize();
    00338           this->SetDirty();
    00339         }
    00340         break;
    00341     }
    00342   }
    00343
    00349   void SelectFirstAvailableObject(bool change_class)
    00350   {
    00351     /* First try to select an object in the selected class. */
    00352     ObjectClass *sel_objclass = ObjectClass::Get(_selected_object_class);
    00353     for (uint i = 0; i < sel_objclass->GetSpecCount(); i++) {
    00354       const ObjectSpec *spec = sel_objclass->GetSpec(i);
    00355       if (spec->IsAvailable()) {
    00356         this->SelectOtherObject(i);
    00357         return;
    00358       }
    00359     }
    00360     if (change_class) {
    00361       /* If that fails, select the first available object
    00362        * from a random class. */
    00363       for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
    00364         ObjectClass *objclass = ObjectClass::Get(j);
    00365         for (uint i = 0; i < objclass->GetSpecCount(); i++) {
    00366           const ObjectSpec *spec = objclass->GetSpec(i);
    00367           if (spec->IsAvailable()) {
    00368             _selected_object_class = j;
    00369             this->SelectOtherObject(i);
    00370             return;
    00371           }
    00372         }
    00373       }
    00374     }
    00375     /* If all objects are unavailable, select nothing... */
    00376     if (ObjectClass::Get(_selected_object_class)->GetUISpecCount() == 0) {
    00377       /* ... but make sure that the class is not empty. */
    00378       for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
    00379         if (ObjectClass::Get(j)->GetUISpecCount() > 0) {
    00380           _selected_object_class = j;
    00381           break;
    00382         }
    00383       }
    00384     }
    00385     this->SelectOtherObject(-1);
    00386   }
    00387 };
    00388
    00389 static const NWidgetPart _nested_build_object_widgets[] = {
    00390   NWidget(NWID_HORIZONTAL),
    00391     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00392     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_OBJECT_BUILD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00393   EndContainer(),
    00394   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
    00395     NWidget(NWID_HORIZONTAL), SetPadding(2, 0, 0, 0),
    00396       NWidget(NWID_VERTICAL),
    00397         NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 2, 5),
    00398           NWidget(WWT_MATRIX, COLOUR_GREY, WID_BO_CLASS_LIST), SetFill(1, 0), SetDataTip(0x501, STR_OBJECT_BUILD_CLASS_TOOLTIP), SetScrollbar(WID_BO_SCROLLBAR),
    00399           NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_BO_SCROLLBAR),
    00400         EndContainer(),
    00401         NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 0, 5),
    00402           NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BO_OBJECT_MATRIX), SetPIP(0, 2, 0),
    00403             NWidget(WWT_PANEL, COLOUR_GREY, WID_BO_OBJECT_SPRITE), SetDataTip(0x0, STR_OBJECT_BUILD_PREVIEW_TOOLTIP), EndContainer(),
    00404           EndContainer(),
    00405         EndContainer(),
    00406         NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BO_OBJECT_NAME), SetDataTip(STR_ORANGE_STRING, STR_NULL), SetPadding(2, 5, 2, 5),
    00407         NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BO_OBJECT_SIZE), SetDataTip(STR_OBJECT_BUILD_SIZE, STR_NULL), SetPadding(2, 5, 2, 5),
    00408       EndContainer(),
    00409       NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(WID_BO_SELECT_SCROLL),
    00410         NWidget(NWID_HORIZONTAL),
    00411           NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BO_SELECT_MATRIX), SetFill(0, 1), SetPIP(0, 2, 0),
    00412             NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BO_SELECT_IMAGE), SetMinimalSize(66, 60), SetDataTip(0x0, STR_OBJECT_BUILD_TOOLTIP),
    00413                 SetFill(0, 0), SetResize(0, 0), SetScrollbar(WID_BO_SELECT_SCROLL),
    00414             EndContainer(),
    00415           EndContainer(),
    00416           NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BO_SELECT_SCROLL),
    00417         EndContainer(),
    00418       EndContainer(),
    00419     EndContainer(),
    00420     NWidget(NWID_HORIZONTAL),
    00421       NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BO_INFO), SetPadding(2, 5, 0, 5), SetFill(1, 0), SetResize(1, 0),
    00422       NWidget(NWID_VERTICAL),
    00423         NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
    00424         NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
    00425       EndContainer(),
    00426     EndContainer(),
    00427   EndContainer(),
    00428 };
    00429
    00430 static const WindowDesc _build_object_desc(
    00431   WDP_AUTO, 0, 0,
    00432   WC_BUILD_OBJECT, WC_BUILD_TOOLBAR,
    00433   WDF_CONSTRUCTION,
    00434   _nested_build_object_widgets, lengthof(_nested_build_object_widgets)
    00435 );
    00436
    00441 void ShowBuildObjectPicker(Window *w)
    00442 {
    00443   new BuildObjectWindow(&_build_object_desc, w);
    00444 }
    00445
    00447 void InitializeObjectGui()
    00448 {
    00449   _selected_object_class = (ObjectClassID)0;
    00450 }
    00451
    00457 void PlaceProc_Object(TileIndex tile)
    00458 {
    00459   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);
    00460 }