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