00001
00002
00003
00004
00005
00006
00007
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::GetCount());
00046
00047 this->FinishInitNested(desc, 0);
00048
00049 this->SelectFirstAvailableObject(true);
00050 this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetCount(4);
00051
00052 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX);
00053 matrix->SetScrollbar(this->GetScrollbar(WID_BO_SELECT_SCROLL));
00054 matrix->SetCount(ObjectClass::GetCount(_selected_object_class));
00055 }
00056
00057 virtual ~BuildObjectWindow()
00058 {
00059 }
00060
00061 virtual void SetStringParameters(int widget) const
00062 {
00063 switch (widget) {
00064 case WID_BO_OBJECT_SIZE: {
00065 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00066 int size = spec == NULL ? 0 : spec->size;
00067 SetDParam(0, GB(size, HasBit(_selected_object_view, 0) ? 4 : 0, 4));
00068 SetDParam(1, GB(size, HasBit(_selected_object_view, 0) ? 0 : 4, 4));
00069 break;
00070 }
00071
00072 default: break;
00073 }
00074 }
00075
00076 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00077 {
00078 switch (widget) {
00079 case WID_BO_CLASS_LIST: {
00080 for (uint i = 0; i < ObjectClass::GetCount(); i++) {
00081 size->width = max(size->width, GetStringBoundingBox(ObjectClass::GetName((ObjectClassID)i)).width);
00082 }
00083 size->width += padding.width;
00084 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00085 resize->height = this->line_height;
00086 size->height = this->vscroll->GetCapacity() * this->line_height;
00087 break;
00088 }
00089
00090 case WID_BO_OBJECT_MATRIX: {
00091
00092 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00093 if (spec != NULL) {
00094 if (spec->views >= 2) size->width += resize->width;
00095 if (spec->views >= 4) size->height += resize->height;
00096 }
00097 resize->width = 0;
00098 resize->height = 0;
00099 break;
00100 }
00101
00102 case WID_BO_OBJECT_SPRITE: {
00103 bool two_wide = false;
00104 int height[2] = {0, 0};
00105
00106
00107 for (int i = 0; i < NUM_OBJECTS; i++) {
00108 const ObjectSpec *spec = ObjectSpec::Get(i);
00109 if (!spec->enabled) continue;
00110 two_wide |= spec->views >= 2;
00111 height[spec->views / 4] = max<int>(ObjectSpec::Get(i)->height, height[spec->views / 4]);
00112 }
00113
00114
00115 for (size_t i = 0; i < lengthof(height); i++) {
00116 height[i] *= TILE_HEIGHT;
00117 height[i] += TILE_PIXELS + 2 * OBJECT_MARGIN;
00118 }
00119
00120
00121
00122
00123
00124 size->height = max(height[0], height[1] * 2 + 2);
00125 if (two_wide) {
00126 size->width = (3 * TILE_PIXELS + 2 * OBJECT_MARGIN) * 2 + 2;
00127 } else {
00128 size->width = 4 * TILE_PIXELS + 2 * OBJECT_MARGIN;
00129 }
00130
00131
00132 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00133 if (spec != NULL) {
00134 if (spec->views >= 2) size->width = size->width / 2 - 1;
00135 if (spec->views >= 4) size->height = size->height / 2 - 1;
00136 }
00137 break;
00138 }
00139
00140 case WID_BO_INFO:
00141 size->height = this->info_height;
00142 break;
00143
00144 case WID_BO_SELECT_MATRIX:
00145 fill->height = 1;
00146 resize->height = 1;
00147 break;
00148
00149 default: break;
00150 }
00151 }
00152
00153 virtual void DrawWidget(const Rect &r, int widget) const
00154 {
00155 switch (GB(widget, 0, 16)) {
00156 case WID_BO_CLASS_LIST: {
00157 int y = r.top;
00158 for (uint i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < ObjectClass::GetCount(); i++) {
00159 SetDParam(0, ObjectClass::GetName((ObjectClassID)i));
00160 DrawString(r.left + WD_MATRIX_LEFT, r.right + WD_MATRIX_RIGHT, y + WD_MATRIX_TOP, STR_JUST_STRING,
00161 ((int)i == _selected_object_class) ? TC_WHITE : TC_BLACK);
00162 y += this->line_height;
00163 }
00164 break;
00165 }
00166
00167 case WID_BO_OBJECT_SPRITE: {
00168 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00169 if (spec == NULL) break;
00170
00171 DrawPixelInfo tmp_dpi;
00172
00173 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
00174 DrawPixelInfo *old_dpi = _cur_dpi;
00175 _cur_dpi = &tmp_dpi;
00176 if (spec->grf_prop.grffile == NULL) {
00177 extern const DrawTileSprites _objects[];
00178 const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
00179 DrawOrigTileSeqInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, dts, PAL_NONE);
00180 } else {
00181 DrawNewObjectTileInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, spec, GB(widget, 16, 16));
00182 }
00183 _cur_dpi = old_dpi;
00184 }
00185 break;
00186 }
00187
00188 case WID_BO_SELECT_IMAGE: {
00189 if (_selected_object_index < 0) break;
00190
00191 int obj_index = GB(widget, 16, 16);
00192 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, obj_index);
00193 if (spec == NULL) break;
00194
00195 if (!spec->IsAvailable()) {
00196 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
00197 }
00198 DrawPixelInfo tmp_dpi;
00199
00200 if (FillDrawPixelInfo(&tmp_dpi, r.left + 1, r.top, (r.right - 1) - (r.left + 1) + 1, r.bottom - r.top + 1)) {
00201 DrawPixelInfo *old_dpi = _cur_dpi;
00202 _cur_dpi = &tmp_dpi;
00203 if (spec->grf_prop.grffile == NULL) {
00204 extern const DrawTileSprites _objects[];
00205 const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
00206 DrawOrigTileSeqInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, dts, PAL_NONE);
00207 } else {
00208 DrawNewObjectTileInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, spec,
00209 min(_selected_object_view, spec->views - 1));
00210 }
00211 _cur_dpi = old_dpi;
00212 }
00213 break;
00214 }
00215
00216 case WID_BO_INFO: {
00217 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00218 if (spec == NULL) break;
00219
00220
00221 if (HasBit(spec->callback_mask, CBM_OBJ_FUND_MORE_TEXT)) {
00222 uint16 callback_res = GetObjectCallback(CBID_OBJECT_FUND_MORE_TEXT, 0, 0, spec, NULL, INVALID_TILE, _selected_object_view);
00223 if (callback_res != CALLBACK_FAILED && callback_res != 0x400) {
00224 if (callback_res > 0x400) {
00225 ErrorUnknownCallbackResult(spec->grf_prop.grffile->grfid, CBID_OBJECT_FUND_MORE_TEXT, callback_res);
00226 } else {
00227 StringID message = GetGRFStringID(spec->grf_prop.grffile->grfid, 0xD000 + callback_res);
00228 if (message != STR_NULL && message != STR_UNDEFINED) {
00229 StartTextRefStackUsage(6);
00230
00231
00232
00233 int y = DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, message, TC_ORANGE) - r.top;
00234 StopTextRefStackUsage();
00235 if (y > this->info_height) {
00236 BuildObjectWindow *bow = const_cast<BuildObjectWindow *>(this);
00237 bow->info_height = y + 2;
00238 bow->ReInit();
00239 }
00240 }
00241 }
00242 }
00243 }
00244 }
00245 }
00246 }
00247
00252 void SelectOtherObject(int object_index)
00253 {
00254 _selected_object_index = object_index;
00255 if (_selected_object_index != -1) {
00256 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00257 _selected_object_view = min(_selected_object_view, spec->views - 1);
00258 this->ReInit();
00259 } else {
00260 _selected_object_view = 0;
00261 }
00262
00263 this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetClicked(_selected_object_view);
00264 this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetClicked(_selected_object_index);
00265 this->UpdateSelectSize();
00266 this->SetDirty();
00267 }
00268
00269 void UpdateSelectSize()
00270 {
00271 if (_selected_object_index == -1) {
00272 SetTileSelectSize(1, 1);
00273 } else {
00274 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, _selected_object_index);
00275 int w = GB(spec->size, HasBit(_selected_object_view, 0) ? 4 : 0, 4);
00276 int h = GB(spec->size, HasBit(_selected_object_view, 0) ? 0 : 4, 4);
00277 SetTileSelectSize(w, h);
00278 }
00279 }
00280
00281 virtual void OnResize()
00282 {
00283 this->vscroll->SetCapacityFromWidget(this, WID_BO_CLASS_LIST);
00284 this->GetWidget<NWidgetCore>(WID_BO_CLASS_LIST)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00285 }
00286
00287 virtual void OnClick(Point pt, int widget, int click_count)
00288 {
00289 switch (GB(widget, 0, 16)) {
00290 case WID_BO_CLASS_LIST: {
00291 int num_clicked = this->vscroll->GetPosition() + (pt.y - this->nested_array[widget]->pos_y) / this->line_height;
00292 if (num_clicked >= (int)ObjectClass::GetCount()) break;
00293
00294 _selected_object_class = (ObjectClassID)num_clicked;
00295 this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetCount(ObjectClass::GetCount(_selected_object_class));
00296 this->SelectFirstAvailableObject(false);
00297 break;
00298 }
00299
00300 case WID_BO_SELECT_IMAGE: {
00301 int num_clicked = GB(widget, 16, 16);
00302 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, num_clicked);
00303 if (spec->IsAvailable()) this->SelectOtherObject(num_clicked);
00304 break;
00305 }
00306
00307 case WID_BO_OBJECT_SPRITE:
00308 if (_selected_object_index != -1) {
00309 _selected_object_view = GB(widget, 16, 16);
00310 this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetClicked(_selected_object_view);
00311 this->UpdateSelectSize();
00312 this->SetDirty();
00313 }
00314 break;
00315 }
00316 }
00317
00323 void SelectFirstAvailableObject(bool change_class)
00324 {
00325
00326 for (uint i = 0; i < ObjectClass::GetCount(_selected_object_class); i++) {
00327 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class, i);
00328 if (spec->IsAvailable()) {
00329 this->SelectOtherObject(0);
00330 return;
00331 }
00332 }
00333 if (change_class) {
00334
00335
00336 for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
00337 for (uint i = 0; i < ObjectClass::GetCount(j); i++) {
00338 const ObjectSpec *spec = ObjectClass::Get(j, i);
00339 if (spec->IsAvailable()) {
00340 _selected_object_class = j;
00341 this->SelectOtherObject(i);
00342 return;
00343 }
00344 }
00345 }
00346 }
00347
00348 this->SelectOtherObject(-1);
00349 }
00350 };
00351
00352 static const NWidgetPart _nested_build_object_widgets[] = {
00353 NWidget(NWID_HORIZONTAL),
00354 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00355 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_OBJECT_BUILD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00356 EndContainer(),
00357 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00358 NWidget(NWID_HORIZONTAL), SetPadding(2, 0, 0, 0),
00359 NWidget(NWID_VERTICAL),
00360 NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 2, 5),
00361 NWidget(WWT_MATRIX, COLOUR_GREY, WID_BO_CLASS_LIST), SetFill(1, 0), SetDataTip(0x501, STR_OBJECT_BUILD_CLASS_TOOLTIP), SetScrollbar(WID_BO_SCROLLBAR),
00362 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_BO_SCROLLBAR),
00363 EndContainer(),
00364 NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 0, 5),
00365 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BO_OBJECT_MATRIX), SetPIP(0, 2, 0),
00366 NWidget(WWT_PANEL, COLOUR_GREY, WID_BO_OBJECT_SPRITE), SetDataTip(0x0, STR_OBJECT_BUILD_PREVIEW_TOOLTIP), EndContainer(),
00367 EndContainer(),
00368 EndContainer(),
00369 NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BO_OBJECT_SIZE), SetDataTip(STR_OBJECT_BUILD_SIZE, STR_NULL), SetPadding(2, 5, 2, 5),
00370 EndContainer(),
00371 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(WID_BO_SELECT_SCROLL),
00372 NWidget(NWID_HORIZONTAL),
00373 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BO_SELECT_MATRIX), SetFill(0, 1), SetPIP(0, 2, 0),
00374 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BO_SELECT_IMAGE), SetMinimalSize(66, 60), SetDataTip(0x0, STR_OBJECT_BUILD_TOOLTIP),
00375 SetFill(0, 0), SetResize(0, 0), SetScrollbar(WID_BO_SELECT_SCROLL),
00376 EndContainer(),
00377 EndContainer(),
00378 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BO_SELECT_SCROLL),
00379 EndContainer(),
00380 EndContainer(),
00381 EndContainer(),
00382 NWidget(NWID_HORIZONTAL),
00383 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BO_INFO), SetPadding(2, 5, 0, 5), SetFill(1, 0), SetResize(1, 0),
00384 NWidget(NWID_VERTICAL),
00385 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
00386 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
00387 EndContainer(),
00388 EndContainer(),
00389 EndContainer(),
00390 };
00391
00392 static const WindowDesc _build_object_desc(
00393 WDP_AUTO, 0, 0,
00394 WC_BUILD_OBJECT, WC_BUILD_TOOLBAR,
00395 WDF_CONSTRUCTION,
00396 _nested_build_object_widgets, lengthof(_nested_build_object_widgets)
00397 );
00398
00403 void ShowBuildObjectPicker(Window *w)
00404 {
00405 new BuildObjectWindow(&_build_object_desc, w);
00406 }
00407
00409 void InitializeObjectGui()
00410 {
00411 _selected_object_class = (ObjectClassID)0;
00412 }
00413
00419 void PlaceProc_Object(TileIndex tile)
00420 {
00421 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);
00422 }