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 #include "window_func.h"
00021
00022 #include "widgets/object_widget.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 static uint _matrix_col_count;
00030 static uint _list_row_count;
00031
00033 class BuildObjectWindow : public PickerWindowBase {
00034 static const int OBJECT_MARGIN = 4;
00035 int line_height;
00036 int info_height;
00037 Scrollbar *vscroll;
00038
00040 void EnsureSelectedObjectClassIsVisible()
00041 {
00042 uint pos = 0;
00043 for (int i = 0; i < _selected_object_class; i++) {
00044 if (ObjectClass::Get((ObjectClassID) i)->GetUISpecCount() == 0) continue;
00045 pos++;
00046 }
00047 this->vscroll->ScrollTowards(pos);
00048 }
00049
00054 bool CanRestoreSelectedObject()
00055 {
00056 if (_selected_object_index == -1) return false;
00057
00058 ObjectClass *sel_objclass = ObjectClass::Get(_selected_object_class);
00059 if ((int)sel_objclass->GetSpecCount() <= _selected_object_index) return false;
00060
00061 return sel_objclass->GetSpec(_selected_object_index)->IsAvailable();
00062 }
00063
00068 uint GetMatrixColumnCount()
00069 {
00070 const NWidgetBase *matrix = this->GetWidget<NWidgetBase>(WID_BO_SELECT_MATRIX);
00071 return 1 + (matrix->current_x - matrix->smallest_x) / matrix->resize_x;
00072 }
00073
00074 public:
00075 BuildObjectWindow(const WindowDesc *desc, Window *w) : PickerWindowBase(w), info_height(1)
00076 {
00077 this->CreateNestedTree(desc);
00078
00079 this->vscroll = this->GetScrollbar(WID_BO_SCROLLBAR);
00080 this->vscroll->SetCapacity(5);
00081 this->vscroll->SetPosition(0);
00082 this->vscroll->SetCount(ObjectClass::GetUIClassCount());
00083
00084 this->FinishInitNested(desc, 0);
00085
00086 if (this->CanRestoreSelectedObject()) {
00087 this->SelectOtherObject(_selected_object_index);
00088 } else {
00089 this->SelectFirstAvailableObject(true);
00090 }
00091 assert(ObjectClass::Get(_selected_object_class)->GetUISpecCount() > 0);
00092 this->EnsureSelectedObjectClassIsVisible();
00093 this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetCount(4);
00094
00095 NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX);
00096 matrix->SetScrollbar(this->GetScrollbar(WID_BO_SELECT_SCROLL));
00097 matrix->SetCount(ObjectClass::Get(_selected_object_class)->GetUISpecCount());
00098
00099
00100 uint default_num_cols = this->GetMatrixColumnCount();
00101 uint default_num_rows = this->vscroll->GetCapacity();
00102 int delta_x = (_matrix_col_count > default_num_cols) ? (_matrix_col_count - default_num_cols) * this->resize.step_width : 0;
00103 int delta_y = (_list_row_count > default_num_rows) ? (_list_row_count - default_num_rows) * this->resize.step_height : 0;
00104 if (delta_x > 0 || delta_y > 0) {
00105 ResizeWindow(this, delta_x, delta_y, false);
00106
00107
00108
00109 this->FindWindowPlacementAndResize(this->width, this->height);
00110 }
00111
00112 if (_selected_object_index != -1) matrix->SetClicked(_selected_object_index);
00113 }
00114
00115 virtual ~BuildObjectWindow()
00116 {
00117
00118
00119 _matrix_col_count = this->GetMatrixColumnCount();
00120 _list_row_count = this->vscroll->GetCapacity();
00121 }
00122
00123 virtual void SetStringParameters(int widget) const
00124 {
00125 switch (widget) {
00126 case WID_BO_OBJECT_NAME: {
00127 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00128 SetDParam(0, spec != NULL ? spec->name : STR_EMPTY);
00129 break;
00130 }
00131
00132 case WID_BO_OBJECT_SIZE: {
00133 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00134 int size = spec == NULL ? 0 : spec->size;
00135 SetDParam(0, GB(size, HasBit(_selected_object_view, 0) ? 4 : 0, 4));
00136 SetDParam(1, GB(size, HasBit(_selected_object_view, 0) ? 0 : 4, 4));
00137 break;
00138 }
00139
00140 default: break;
00141 }
00142 }
00143
00144 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00145 {
00146 switch (widget) {
00147 case WID_BO_CLASS_LIST: {
00148 for (uint i = 0; i < ObjectClass::GetClassCount(); i++) {
00149 ObjectClass *objclass = ObjectClass::Get((ObjectClassID)i);
00150 if (objclass->GetUISpecCount() == 0) continue;
00151 size->width = max(size->width, GetStringBoundingBox(objclass->name).width);
00152 }
00153 size->width += padding.width;
00154 this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00155 resize->height = this->line_height;
00156 size->height = this->vscroll->GetCapacity() * this->line_height;
00157 break;
00158 }
00159
00160 case WID_BO_OBJECT_NAME:
00161 case WID_BO_OBJECT_SIZE:
00162
00163 size->width = 0;
00164 break;
00165
00166 case WID_BO_OBJECT_MATRIX: {
00167
00168 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00169 if (spec != NULL) {
00170 if (spec->views >= 2) size->width += resize->width;
00171 if (spec->views >= 4) size->height += resize->height;
00172 }
00173 resize->width = 0;
00174 resize->height = 0;
00175 break;
00176 }
00177
00178 case WID_BO_OBJECT_SPRITE: {
00179 bool two_wide = false;
00180 int height[2] = {0, 0};
00181
00182
00183 for (int i = 0; i < NUM_OBJECTS; i++) {
00184 const ObjectSpec *spec = ObjectSpec::Get(i);
00185 if (!spec->IsEverAvailable()) continue;
00186 two_wide |= spec->views >= 2;
00187 height[spec->views / 4] = max<int>(ObjectSpec::Get(i)->height, height[spec->views / 4]);
00188 }
00189
00190
00191 for (size_t i = 0; i < lengthof(height); i++) {
00192 height[i] *= TILE_HEIGHT;
00193 height[i] += TILE_PIXELS + 2 * OBJECT_MARGIN;
00194 }
00195
00196
00197
00198
00199
00200 size->height = max(height[0], height[1] * 2 + 2);
00201 if (two_wide) {
00202 size->width = (3 * TILE_PIXELS + 2 * OBJECT_MARGIN) * 2 + 2;
00203 } else {
00204 size->width = 4 * TILE_PIXELS + 2 * OBJECT_MARGIN;
00205 }
00206
00207
00208 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00209 if (spec != NULL) {
00210 if (spec->views >= 2) size->width = size->width / 2 - 1;
00211 if (spec->views >= 4) size->height = size->height / 2 - 1;
00212 }
00213 break;
00214 }
00215
00216 case WID_BO_INFO:
00217 size->height = this->info_height;
00218 break;
00219
00220 case WID_BO_SELECT_MATRIX:
00221 fill->height = 1;
00222 resize->height = 1;
00223 break;
00224
00225 default: break;
00226 }
00227 }
00228
00229 virtual void DrawWidget(const Rect &r, int widget) const
00230 {
00231 switch (GB(widget, 0, 16)) {
00232 case WID_BO_CLASS_LIST: {
00233 int y = r.top;
00234 uint pos = 0;
00235 for (uint i = 0; i < ObjectClass::GetClassCount(); i++) {
00236 ObjectClass *objclass = ObjectClass::Get((ObjectClassID)i);
00237 if (objclass->GetUISpecCount() == 0) continue;
00238 if (!this->vscroll->IsVisible(pos++)) continue;
00239 SetDParam(0, objclass->name);
00240 DrawString(r.left + WD_MATRIX_LEFT, r.right + WD_MATRIX_RIGHT, y + WD_MATRIX_TOP, STR_JUST_STRING,
00241 ((int)i == _selected_object_class) ? TC_WHITE : TC_BLACK);
00242 y += this->line_height;
00243 }
00244 break;
00245 }
00246
00247 case WID_BO_OBJECT_SPRITE: {
00248 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00249 if (spec == NULL) break;
00250
00251
00252
00253
00254
00255
00256 uint matrix_height = this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->current_y;
00257
00258 DrawPixelInfo tmp_dpi;
00259
00260 if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
00261 DrawPixelInfo *old_dpi = _cur_dpi;
00262 _cur_dpi = &tmp_dpi;
00263 if (spec->grf_prop.grffile == NULL) {
00264 extern const DrawTileSprites _objects[];
00265 const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
00266 DrawOrigTileSeqInGUI((r.right - r.left) / 2 - 1, (r.bottom - r.top + matrix_height / 2) / 2 - OBJECT_MARGIN - TILE_PIXELS, dts, PAL_NONE);
00267 } else {
00268 DrawNewObjectTileInGUI((r.right - r.left) / 2 - 1, (r.bottom - r.top + matrix_height / 2) / 2 - OBJECT_MARGIN - TILE_PIXELS, spec, GB(widget, 16, 16));
00269 }
00270 _cur_dpi = old_dpi;
00271 }
00272 break;
00273 }
00274
00275 case WID_BO_SELECT_IMAGE: {
00276 ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
00277 int obj_index = objclass->GetIndexFromUI(GB(widget, 16, 16));
00278 if (obj_index < 0) break;
00279 const ObjectSpec *spec = objclass->GetSpec(obj_index);
00280 if (spec == NULL) break;
00281
00282 if (!spec->IsAvailable()) {
00283 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK, FILLRECT_CHECKER);
00284 }
00285 DrawPixelInfo tmp_dpi;
00286
00287 if (FillDrawPixelInfo(&tmp_dpi, r.left + 1, r.top, (r.right - 1) - (r.left + 1) + 1, r.bottom - r.top + 1)) {
00288 DrawPixelInfo *old_dpi = _cur_dpi;
00289 _cur_dpi = &tmp_dpi;
00290 if (spec->grf_prop.grffile == NULL) {
00291 extern const DrawTileSprites _objects[];
00292 const DrawTileSprites *dts = &_objects[spec->grf_prop.local_id];
00293 DrawOrigTileSeqInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, dts, PAL_NONE);
00294 } else {
00295 DrawNewObjectTileInGUI((r.right - r.left) / 2 - 1, r.bottom - r.top - OBJECT_MARGIN - TILE_PIXELS, spec,
00296 min(_selected_object_view, spec->views - 1));
00297 }
00298 _cur_dpi = old_dpi;
00299 }
00300 break;
00301 }
00302
00303 case WID_BO_INFO: {
00304 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00305 if (spec == NULL) break;
00306
00307
00308 if (HasBit(spec->callback_mask, CBM_OBJ_FUND_MORE_TEXT)) {
00309 uint16 callback_res = GetObjectCallback(CBID_OBJECT_FUND_MORE_TEXT, 0, 0, spec, NULL, INVALID_TILE, _selected_object_view);
00310 if (callback_res != CALLBACK_FAILED && callback_res != 0x400) {
00311 if (callback_res > 0x400) {
00312 ErrorUnknownCallbackResult(spec->grf_prop.grffile->grfid, CBID_OBJECT_FUND_MORE_TEXT, callback_res);
00313 } else {
00314 StringID message = GetGRFStringID(spec->grf_prop.grffile->grfid, 0xD000 + callback_res);
00315 if (message != STR_NULL && message != STR_UNDEFINED) {
00316 StartTextRefStackUsage(6);
00317
00318
00319
00320 int y = DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, message, TC_ORANGE) - r.top;
00321 StopTextRefStackUsage();
00322 if (y > this->info_height) {
00323 BuildObjectWindow *bow = const_cast<BuildObjectWindow *>(this);
00324 bow->info_height = y + 2;
00325 bow->ReInit();
00326 }
00327 }
00328 }
00329 }
00330 }
00331 }
00332 }
00333 }
00334
00339 void SelectOtherObject(int object_index)
00340 {
00341 _selected_object_index = object_index;
00342 if (_selected_object_index != -1) {
00343 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00344 _selected_object_view = min(_selected_object_view, spec->views - 1);
00345 this->ReInit();
00346 } else {
00347 _selected_object_view = 0;
00348 }
00349
00350 this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetClicked(_selected_object_view);
00351 this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetClicked(_selected_object_index != -1 ? ObjectClass::Get(_selected_object_class)->GetUIFromIndex(_selected_object_index) : -1);
00352 this->UpdateSelectSize();
00353 this->SetDirty();
00354 }
00355
00356 void UpdateSelectSize()
00357 {
00358 if (_selected_object_index == -1) {
00359 SetTileSelectSize(1, 1);
00360 } else {
00361 const ObjectSpec *spec = ObjectClass::Get(_selected_object_class)->GetSpec(_selected_object_index);
00362 int w = GB(spec->size, HasBit(_selected_object_view, 0) ? 4 : 0, 4);
00363 int h = GB(spec->size, HasBit(_selected_object_view, 0) ? 0 : 4, 4);
00364 SetTileSelectSize(w, h);
00365 }
00366 }
00367
00368 virtual void OnResize()
00369 {
00370 this->vscroll->SetCapacityFromWidget(this, WID_BO_CLASS_LIST);
00371 this->GetWidget<NWidgetCore>(WID_BO_CLASS_LIST)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00372 }
00373
00374 virtual void OnClick(Point pt, int widget, int click_count)
00375 {
00376 switch (GB(widget, 0, 16)) {
00377 case WID_BO_CLASS_LIST: {
00378 int num_clicked = this->vscroll->GetPosition() + (pt.y - this->nested_array[widget]->pos_y) / this->line_height;
00379 if (num_clicked >= (int)ObjectClass::GetUIClassCount()) break;
00380
00381 _selected_object_class = ObjectClass::GetUIClass(num_clicked);
00382 this->GetWidget<NWidgetMatrix>(WID_BO_SELECT_MATRIX)->SetCount(ObjectClass::Get(_selected_object_class)->GetUISpecCount());
00383 this->SelectFirstAvailableObject(false);
00384 break;
00385 }
00386
00387 case WID_BO_SELECT_IMAGE: {
00388 ObjectClass *objclass = ObjectClass::Get(_selected_object_class);
00389 int num_clicked = objclass->GetIndexFromUI(GB(widget, 16, 16));
00390 if (num_clicked >= 0 && objclass->GetSpec(num_clicked)->IsAvailable()) this->SelectOtherObject(num_clicked);
00391 break;
00392 }
00393
00394 case WID_BO_OBJECT_SPRITE:
00395 if (_selected_object_index != -1) {
00396 _selected_object_view = GB(widget, 16, 16);
00397 this->GetWidget<NWidgetMatrix>(WID_BO_OBJECT_MATRIX)->SetClicked(_selected_object_view);
00398 this->UpdateSelectSize();
00399 this->SetDirty();
00400 }
00401 break;
00402 }
00403 }
00404
00410 void SelectFirstAvailableObject(bool change_class)
00411 {
00412
00413 ObjectClass *sel_objclass = ObjectClass::Get(_selected_object_class);
00414 for (uint i = 0; i < sel_objclass->GetSpecCount(); i++) {
00415 const ObjectSpec *spec = sel_objclass->GetSpec(i);
00416 if (spec->IsAvailable()) {
00417 this->SelectOtherObject(i);
00418 return;
00419 }
00420 }
00421 if (change_class) {
00422
00423
00424 for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
00425 ObjectClass *objclass = ObjectClass::Get(j);
00426 for (uint i = 0; i < objclass->GetSpecCount(); i++) {
00427 const ObjectSpec *spec = objclass->GetSpec(i);
00428 if (spec->IsAvailable()) {
00429 _selected_object_class = j;
00430 this->SelectOtherObject(i);
00431 return;
00432 }
00433 }
00434 }
00435 }
00436
00437 if (ObjectClass::Get(_selected_object_class)->GetUISpecCount() == 0) {
00438
00439 for (ObjectClassID j = OBJECT_CLASS_BEGIN; j < OBJECT_CLASS_MAX; j++) {
00440 if (ObjectClass::Get(j)->GetUISpecCount() > 0) {
00441 _selected_object_class = j;
00442 break;
00443 }
00444 }
00445 }
00446 this->SelectOtherObject(-1);
00447 }
00448 };
00449
00450 static const NWidgetPart _nested_build_object_widgets[] = {
00451 NWidget(NWID_HORIZONTAL),
00452 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
00453 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_OBJECT_BUILD_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00454 EndContainer(),
00455 NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
00456 NWidget(NWID_HORIZONTAL), SetPadding(2, 0, 0, 0),
00457 NWidget(NWID_VERTICAL),
00458 NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 2, 5),
00459 NWidget(WWT_MATRIX, COLOUR_GREY, WID_BO_CLASS_LIST), SetFill(1, 0), SetDataTip(0x501, STR_OBJECT_BUILD_CLASS_TOOLTIP), SetScrollbar(WID_BO_SCROLLBAR),
00460 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_BO_SCROLLBAR),
00461 EndContainer(),
00462 NWidget(NWID_HORIZONTAL), SetPadding(0, 5, 0, 5),
00463 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BO_OBJECT_MATRIX), SetPIP(0, 2, 0),
00464 NWidget(WWT_PANEL, COLOUR_GREY, WID_BO_OBJECT_SPRITE), SetDataTip(0x0, STR_OBJECT_BUILD_PREVIEW_TOOLTIP), EndContainer(),
00465 EndContainer(),
00466 EndContainer(),
00467 NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BO_OBJECT_NAME), SetDataTip(STR_ORANGE_STRING, STR_NULL), SetPadding(2, 5, 2, 5),
00468 NWidget(WWT_TEXT, COLOUR_DARK_GREEN, WID_BO_OBJECT_SIZE), SetDataTip(STR_OBJECT_BUILD_SIZE, STR_NULL), SetPadding(2, 5, 2, 5),
00469 EndContainer(),
00470 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(WID_BO_SELECT_SCROLL),
00471 NWidget(NWID_HORIZONTAL),
00472 NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, WID_BO_SELECT_MATRIX), SetFill(0, 1), SetPIP(0, 2, 0),
00473 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_BO_SELECT_IMAGE), SetMinimalSize(66, 60), SetDataTip(0x0, STR_OBJECT_BUILD_TOOLTIP),
00474 SetFill(0, 0), SetResize(0, 0), SetScrollbar(WID_BO_SELECT_SCROLL),
00475 EndContainer(),
00476 EndContainer(),
00477 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BO_SELECT_SCROLL),
00478 EndContainer(),
00479 EndContainer(),
00480 EndContainer(),
00481 NWidget(NWID_HORIZONTAL),
00482 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_BO_INFO), SetPadding(2, 5, 0, 5), SetFill(1, 0), SetResize(1, 0),
00483 NWidget(NWID_VERTICAL),
00484 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
00485 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
00486 EndContainer(),
00487 EndContainer(),
00488 EndContainer(),
00489 };
00490
00491 static const WindowDesc _build_object_desc(
00492 WDP_AUTO, 0, 0,
00493 WC_BUILD_OBJECT, WC_BUILD_TOOLBAR,
00494 WDF_CONSTRUCTION,
00495 _nested_build_object_widgets, lengthof(_nested_build_object_widgets)
00496 );
00497
00502 void ShowBuildObjectPicker(Window *w)
00503 {
00504 new BuildObjectWindow(&_build_object_desc, w);
00505 }
00506
00508 void InitializeObjectGui()
00509 {
00510 _selected_object_class = (ObjectClassID)0;
00511 }
00512
00518 void PlaceProc_Object(TileIndex tile)
00519 {
00520 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);
00521 }