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