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