00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "debug.h"
00014 #include "landscape.h"
00015 #include "error.h"
00016 #include "gui.h"
00017 #include "command_func.h"
00018 #include "company_func.h"
00019 #include "town.h"
00020 #include "string_func.h"
00021 #include "company_base.h"
00022 #include "texteff.hpp"
00023 #include "strings_func.h"
00024 #include "window_func.h"
00025 #include "querystring_gui.h"
00026 #include "core/geometry_func.hpp"
00027 #include "newgrf_debug.h"
00028
00029 #include "widgets/misc_widget.h"
00030
00031 #include "table/strings.h"
00032
00033 static const NWidgetPart _nested_land_info_widgets[] = {
00034 NWidget(NWID_HORIZONTAL),
00035 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00036 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_LAND_AREA_INFORMATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00037 NWidget(WWT_DEBUGBOX, COLOUR_GREY),
00038 EndContainer(),
00039 NWidget(WWT_PANEL, COLOUR_GREY, WID_LI_BACKGROUND), EndContainer(),
00040 };
00041
00042 static const WindowDesc _land_info_desc(
00043 WDP_AUTO, 0, 0,
00044 WC_LAND_INFO, WC_NONE,
00045 0,
00046 _nested_land_info_widgets, lengthof(_nested_land_info_widgets)
00047 );
00048
00049 class LandInfoWindow : public Window {
00050 enum LandInfoLines {
00051 LAND_INFO_CENTERED_LINES = 12,
00052 LAND_INFO_MULTICENTER_LINE = LAND_INFO_CENTERED_LINES,
00053 LAND_INFO_LINE_END,
00054 };
00055
00056 static const uint LAND_INFO_LINE_BUFF_SIZE = 512;
00057
00058 public:
00059 char landinfo_data[LAND_INFO_LINE_END][LAND_INFO_LINE_BUFF_SIZE];
00060 TileIndex tile;
00061
00062 virtual void DrawWidget(const Rect &r, int widget) const
00063 {
00064 if (widget != WID_LI_BACKGROUND) return;
00065
00066 uint y = r.top + WD_TEXTPANEL_TOP;
00067 for (uint i = 0; i < LAND_INFO_CENTERED_LINES; i++) {
00068 if (StrEmpty(this->landinfo_data[i])) break;
00069
00070 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, this->landinfo_data[i], i == 0 ? TC_LIGHT_BLUE : TC_FROMSTRING, SA_HOR_CENTER);
00071 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00072 if (i == 0) y += 4;
00073 }
00074
00075 if (!StrEmpty(this->landinfo_data[LAND_INFO_MULTICENTER_LINE])) {
00076 SetDParamStr(0, this->landinfo_data[LAND_INFO_MULTICENTER_LINE]);
00077 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, r.bottom - WD_TEXTPANEL_BOTTOM, STR_JUST_RAW_STRING, TC_FROMSTRING, SA_CENTER);
00078 }
00079 }
00080
00081 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00082 {
00083 if (widget != WID_LI_BACKGROUND) return;
00084
00085 size->height = WD_TEXTPANEL_TOP + WD_TEXTPANEL_BOTTOM;
00086 for (uint i = 0; i < LAND_INFO_CENTERED_LINES; i++) {
00087 if (StrEmpty(this->landinfo_data[i])) break;
00088
00089 uint width = GetStringBoundingBox(this->landinfo_data[i]).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
00090 size->width = max(size->width, width);
00091
00092 size->height += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00093 if (i == 0) size->height += 4;
00094 }
00095
00096 if (!StrEmpty(this->landinfo_data[LAND_INFO_MULTICENTER_LINE])) {
00097 uint width = GetStringBoundingBox(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
00098 size->width = max(size->width, min(300u, width));
00099 SetDParamStr(0, this->landinfo_data[LAND_INFO_MULTICENTER_LINE]);
00100 size->height += GetStringHeight(STR_JUST_RAW_STRING, size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT);
00101 }
00102 }
00103
00104 LandInfoWindow(TileIndex tile) : Window(), tile(tile)
00105 {
00106 this->InitNested(&_land_info_desc);
00107
00108 #if defined(_DEBUG)
00109 # define LANDINFOD_LEVEL 0
00110 #else
00111 # define LANDINFOD_LEVEL 1
00112 #endif
00113 DEBUG(misc, LANDINFOD_LEVEL, "TILE: %#x (%i,%i)", tile, TileX(tile), TileY(tile));
00114 DEBUG(misc, LANDINFOD_LEVEL, "type_height = %#x", _m[tile].type_height);
00115 DEBUG(misc, LANDINFOD_LEVEL, "m1 = %#x", _m[tile].m1);
00116 DEBUG(misc, LANDINFOD_LEVEL, "m2 = %#x", _m[tile].m2);
00117 DEBUG(misc, LANDINFOD_LEVEL, "m3 = %#x", _m[tile].m3);
00118 DEBUG(misc, LANDINFOD_LEVEL, "m4 = %#x", _m[tile].m4);
00119 DEBUG(misc, LANDINFOD_LEVEL, "m5 = %#x", _m[tile].m5);
00120 DEBUG(misc, LANDINFOD_LEVEL, "m6 = %#x", _m[tile].m6);
00121 DEBUG(misc, LANDINFOD_LEVEL, "m7 = %#x", _me[tile].m7);
00122 #undef LANDINFOD_LEVEL
00123 }
00124
00125 virtual void OnInit()
00126 {
00127 Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
00128
00129
00130 TileDesc td;
00131
00132 td.build_date = INVALID_DATE;
00133
00134
00135
00136
00137
00138 td.owner_type[0] = STR_LAND_AREA_INFORMATION_OWNER;
00139 td.owner_type[1] = STR_NULL;
00140 td.owner_type[2] = STR_NULL;
00141 td.owner_type[3] = STR_NULL;
00142 td.owner[0] = OWNER_NONE;
00143 td.owner[1] = OWNER_NONE;
00144 td.owner[2] = OWNER_NONE;
00145 td.owner[3] = OWNER_NONE;
00146
00147 td.station_class = STR_NULL;
00148 td.station_name = STR_NULL;
00149 td.airport_class = STR_NULL;
00150 td.airport_name = STR_NULL;
00151 td.airport_tile_name = STR_NULL;
00152 td.rail_speed = 0;
00153
00154 td.grf = NULL;
00155
00156 CargoArray acceptance;
00157 AddAcceptedCargo(tile, acceptance, NULL);
00158 GetTileDesc(tile, &td);
00159
00160 uint line_nr = 0;
00161
00162
00163 SetDParam(0, td.dparam[0]);
00164 GetString(this->landinfo_data[line_nr], td.str, lastof(this->landinfo_data[line_nr]));
00165 line_nr++;
00166
00167
00168 for (uint i = 0; i < 4; i++) {
00169 if (td.owner_type[i] == STR_NULL) continue;
00170
00171 SetDParam(0, STR_LAND_AREA_INFORMATION_OWNER_N_A);
00172 if (td.owner[i] != OWNER_NONE && td.owner[i] != OWNER_WATER) GetNameOfOwner(td.owner[i], tile);
00173 GetString(this->landinfo_data[line_nr], td.owner_type[i], lastof(this->landinfo_data[line_nr]));
00174 line_nr++;
00175 }
00176
00177
00178 StringID str = STR_LAND_AREA_INFORMATION_COST_TO_CLEAR_N_A;
00179 Company *c = Company::GetIfValid(_local_company);
00180 if (c != NULL) {
00181 Money old_money = c->money;
00182 c->money = INT64_MAX;
00183 assert(_current_company == _local_company);
00184 CommandCost costclear = DoCommand(tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR);
00185 c->money = old_money;
00186 if (costclear.Succeeded()) {
00187 Money cost = costclear.GetCost();
00188 if (cost < 0) {
00189 cost = -cost;
00190 str = STR_LAND_AREA_INFORMATION_REVENUE_WHEN_CLEARED;
00191 } else {
00192 str = STR_LAND_AREA_INFORMATION_COST_TO_CLEAR;
00193 }
00194 SetDParam(0, cost);
00195 }
00196 }
00197 GetString(this->landinfo_data[line_nr], str, lastof(this->landinfo_data[line_nr]));
00198 line_nr++;
00199
00200
00201 char tmp[16];
00202 snprintf(tmp, lengthof(tmp), "0x%.4X", tile);
00203 SetDParam(0, TileX(tile));
00204 SetDParam(1, TileY(tile));
00205 SetDParam(2, GetTileZ(tile));
00206 SetDParamStr(3, tmp);
00207 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_LANDINFO_COORDS, lastof(this->landinfo_data[line_nr]));
00208 line_nr++;
00209
00210
00211 SetDParam(0, STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE);
00212 if (t != NULL) {
00213 SetDParam(0, STR_TOWN_NAME);
00214 SetDParam(1, t->index);
00215 }
00216 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY, lastof(this->landinfo_data[line_nr]));
00217 line_nr++;
00218
00219
00220 if (td.build_date != INVALID_DATE) {
00221 SetDParam(0, td.build_date);
00222 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_BUILD_DATE, lastof(this->landinfo_data[line_nr]));
00223 line_nr++;
00224 }
00225
00226
00227 if (td.station_class != STR_NULL) {
00228 SetDParam(0, td.station_class);
00229 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_STATION_CLASS, lastof(this->landinfo_data[line_nr]));
00230 line_nr++;
00231 }
00232
00233
00234 if (td.station_name != STR_NULL) {
00235 SetDParam(0, td.station_name);
00236 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_STATION_TYPE, lastof(this->landinfo_data[line_nr]));
00237 line_nr++;
00238 }
00239
00240
00241 if (td.airport_class != STR_NULL) {
00242 SetDParam(0, td.airport_class);
00243 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORT_CLASS, lastof(this->landinfo_data[line_nr]));
00244 line_nr++;
00245 }
00246
00247
00248 if (td.airport_name != STR_NULL) {
00249 SetDParam(0, td.airport_name);
00250 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORT_NAME, lastof(this->landinfo_data[line_nr]));
00251 line_nr++;
00252 }
00253
00254
00255 if (td.airport_tile_name != STR_NULL) {
00256 SetDParam(0, td.airport_tile_name);
00257 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORTTILE_NAME, lastof(this->landinfo_data[line_nr]));
00258 line_nr++;
00259 }
00260
00261
00262 if (td.rail_speed != 0) {
00263 SetDParam(0, td.rail_speed);
00264 GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_RAIL_SPEED_LIMIT, lastof(this->landinfo_data[line_nr]));
00265 line_nr++;
00266 }
00267
00268
00269 if (td.grf != NULL) {
00270 SetDParamStr(0, td.grf);
00271 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_NEWGRF_NAME, lastof(this->landinfo_data[line_nr]));
00272 line_nr++;
00273 }
00274
00275 assert(line_nr < LAND_INFO_CENTERED_LINES);
00276
00277
00278 this->landinfo_data[line_nr][0] = '\0';
00279
00280
00281 char *strp = GetString(this->landinfo_data[LAND_INFO_MULTICENTER_LINE], STR_LAND_AREA_INFORMATION_CARGO_ACCEPTED, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
00282 bool found = false;
00283
00284 for (CargoID i = 0; i < NUM_CARGO; ++i) {
00285 if (acceptance[i] > 0) {
00286
00287 if (found) {
00288 *strp++ = ',';
00289 *strp++ = ' ';
00290 }
00291 found = true;
00292
00293
00294 if (acceptance[i] < 8) {
00295 SetDParam(0, acceptance[i]);
00296 SetDParam(1, CargoSpec::Get(i)->name);
00297 strp = GetString(strp, STR_LAND_AREA_INFORMATION_CARGO_EIGHTS, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
00298 } else {
00299 strp = GetString(strp, CargoSpec::Get(i)->name, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
00300 }
00301 }
00302 }
00303 if (!found) this->landinfo_data[LAND_INFO_MULTICENTER_LINE][0] = '\0';
00304 }
00305
00306 virtual bool IsNewGRFInspectable() const
00307 {
00308 return ::IsNewGRFInspectable(GetGrfSpecFeature(this->tile), this->tile);
00309 }
00310
00311 virtual void ShowNewGRFInspectWindow() const
00312 {
00313 ::ShowNewGRFInspectWindow(GetGrfSpecFeature(this->tile), this->tile);
00314 }
00315
00321 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00322 {
00323 if (!gui_scope) return;
00324 switch (data) {
00325 case 1:
00326
00327 this->ReInit();
00328 break;
00329 }
00330 }
00331 };
00332
00337 void ShowLandInfo(TileIndex tile)
00338 {
00339 DeleteWindowById(WC_LAND_INFO, 0);
00340 new LandInfoWindow(tile);
00341 }
00342
00343 static const NWidgetPart _nested_about_widgets[] = {
00344 NWidget(NWID_HORIZONTAL),
00345 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00346 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_ABOUT_OPENTTD, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00347 EndContainer(),
00348 NWidget(WWT_PANEL, COLOUR_GREY), SetPIP(4, 2, 4),
00349 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_ORIGINAL_COPYRIGHT, STR_NULL),
00350 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_VERSION, STR_NULL),
00351 NWidget(WWT_FRAME, COLOUR_GREY), SetPadding(0, 5, 1, 5),
00352 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_A_SCROLLING_TEXT),
00353 EndContainer(),
00354 NWidget(WWT_LABEL, COLOUR_GREY, WID_A_WEBSITE), SetDataTip(STR_BLACK_RAW_STRING, STR_NULL),
00355 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_COPYRIGHT_OPENTTD, STR_NULL),
00356 EndContainer(),
00357 };
00358
00359 static const WindowDesc _about_desc(
00360 WDP_CENTER, 0, 0,
00361 WC_GAME_OPTIONS, WC_NONE,
00362 0,
00363 _nested_about_widgets, lengthof(_nested_about_widgets)
00364 );
00365
00366 static const char * const _credits[] = {
00367 "Original design by Chris Sawyer",
00368 "Original graphics by Simon Foster",
00369 "",
00370 "The OpenTTD team (in alphabetical order):",
00371 " Albert Hofkamp (Alberth) - GUI expert",
00372 " Jean-Fran\xC3\xA7ois Claeys (Belugas) - GUI, newindustries and more",
00373 " Matthijs Kooijman (blathijs) - Pathfinder-guru, pool rework",
00374 " Christoph Elsenhans (frosch) - General coding",
00375 " Lo\xC3\xAF""c Guilloux (glx) - Windows Expert",
00376 " Michael Lutz (michi_cc) - Path based signals",
00377 " Owen Rudge (orudge) - Forum host, OS/2 port",
00378 " Peter Nelson (peter1138) - Spiritual descendant from NewGRF gods",
00379 " Ingo von Borstel (planetmaker) - Support",
00380 " Remko Bijker (Rubidium) - Lead coder and way more",
00381 " Zden\xC4\x9Bk Sojka (SmatZ) - Bug finder and fixer",
00382 " Jos\xC3\xA9 Soler (Terkhen) - General coding",
00383 " Thijs Marinussen (Yexo) - AI Framework",
00384 "",
00385 "Inactive Developers:",
00386 " Bjarni Corfitzen (Bjarni) - MacOSX port, coder and vehicles",
00387 " Victor Fischer (Celestar) - Programming everywhere you need him to",
00388 " Tam\xC3\xA1s Farag\xC3\xB3 (Darkvater) - Ex-Lead coder",
00389 " Jaroslav Mazanec (KUDr) - YAPG (Yet Another Pathfinder God) ;)",
00390 " Jonathan Coome (Maedhros) - High priest of the NewGRF Temple",
00391 " Attila B\xC3\xA1n (MiHaMiX) - Developer WebTranslator 1 and 2",
00392 " Christoph Mallon (Tron) - Programmer, code correctness police",
00393 "",
00394 "Retired Developers:",
00395 " Ludvig Strigeus (ludde) - OpenTTD author, main coder (0.1 - 0.3.3)",
00396 " Serge Paquet (vurlix) - Assistant project manager, coder (0.1 - 0.3.3)",
00397 " Dominik Scherer (dominik81) - Lead programmer, GUI expert (0.3.0 - 0.3.6)",
00398 " Benedikt Br\xC3\xBCggemeier (skidd13) - Bug fixer and code reworker",
00399 " Patric Stout (TrueBrain) - NoProgrammer (0.3 - 1.2), sys op (active)",
00400 "",
00401 "Special thanks go out to:",
00402 " Josef Drexler - For his great work on TTDPatch",
00403 " Marcin Grzegorczyk - For describing Transport Tycoon Deluxe internals",
00404 " Petr Baudi\xC5\xA1 (pasky) - Many patches, newGRF support",
00405 " Simon Sasburg (HackyKid) - Many bugfixes he has blessed us with",
00406 " Stefan Mei\xC3\x9Fner (sign_de) - For his work on the console",
00407 " Mike Ragsdale - OpenTTD installer",
00408 " Cian Duffy (MYOB) - BeOS port / manual writing",
00409 " Christian Rosentreter (tokai) - MorphOS / AmigaOS port",
00410 " Richard Kempton (richK) - additional airports, initial TGP implementation",
00411 "",
00412 " Alberto Demichelis - Squirrel scripting language \xC2\xA9 2003-2008",
00413 " L. Peter Deutsch - MD5 implementation \xC2\xA9 1999, 2000, 2002",
00414 " Michael Blunck - Pre-signals and semaphores \xC2\xA9 2003",
00415 " George - Canal/Lock graphics \xC2\xA9 2003-2004",
00416 " Andrew Parkhouse (andythenorth) - River graphics",
00417 " David Dallaston (Pikka) - Tram tracks",
00418 " Marcin Grzegorczyk - Foundations for tracks on slopes",
00419 " All Translators - Who made OpenTTD a truly international game",
00420 " Bug Reporters - Without whom OpenTTD would still be full of bugs!",
00421 "",
00422 "",
00423 "And last but not least:",
00424 " Chris Sawyer - For an amazing game!"
00425 };
00426
00427 struct AboutWindow : public Window {
00428 int text_position;
00429 byte counter;
00430 int line_height;
00431 static const int num_visible_lines = 19;
00432
00433 AboutWindow() : Window()
00434 {
00435 this->InitNested(&_about_desc, WN_GAME_OPTIONS_ABOUT);
00436
00437 this->counter = 5;
00438 this->text_position = this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->current_y;
00439 }
00440
00441 virtual void SetStringParameters(int widget) const
00442 {
00443 if (widget == WID_A_WEBSITE) SetDParamStr(0, "Website: http://www.openttd.org");
00444 }
00445
00446 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00447 {
00448 if (widget != WID_A_SCROLLING_TEXT) return;
00449
00450 this->line_height = FONT_HEIGHT_NORMAL;
00451
00452 Dimension d;
00453 d.height = this->line_height * num_visible_lines;
00454
00455 d.width = 0;
00456 for (uint i = 0; i < lengthof(_credits); i++) {
00457 d.width = max(d.width, GetStringBoundingBox(_credits[i]).width);
00458 }
00459 *size = maxdim(*size, d);
00460 }
00461
00462 virtual void DrawWidget(const Rect &r, int widget) const
00463 {
00464 if (widget != WID_A_SCROLLING_TEXT) return;
00465
00466 int y = this->text_position;
00467
00468
00469 for (uint i = 0; i < lengthof(_credits); i++) {
00470 if (y >= r.top + 7 && y < r.bottom - this->line_height) {
00471 DrawString(r.left, r.right, y, _credits[i], TC_BLACK, SA_LEFT | SA_FORCE);
00472 }
00473 y += this->line_height;
00474 }
00475 }
00476
00477 virtual void OnTick()
00478 {
00479 if (--this->counter == 0) {
00480 this->counter = 5;
00481 this->text_position--;
00482
00483 if (this->text_position < (int)(this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y - lengthof(_credits) * this->line_height)) {
00484 this->text_position = this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->current_y;
00485 }
00486 this->SetDirty();
00487 }
00488 }
00489 };
00490
00491 void ShowAboutWindow()
00492 {
00493 DeleteWindowByClass(WC_GAME_OPTIONS);
00494 new AboutWindow();
00495 }
00496
00503 void ShowEstimatedCostOrIncome(Money cost, int x, int y)
00504 {
00505 StringID msg = STR_MESSAGE_ESTIMATED_COST;
00506
00507 if (cost < 0) {
00508 cost = -cost;
00509 msg = STR_MESSAGE_ESTIMATED_INCOME;
00510 }
00511 SetDParam(0, cost);
00512 ShowErrorMessage(msg, INVALID_STRING_ID, WL_INFO, x, y);
00513 }
00514
00522 void ShowCostOrIncomeAnimation(int x, int y, int z, Money cost)
00523 {
00524 Point pt = RemapCoords(x, y, z);
00525 StringID msg = STR_INCOME_FLOAT_COST;
00526
00527 if (cost < 0) {
00528 cost = -cost;
00529 msg = STR_INCOME_FLOAT_INCOME;
00530 }
00531 SetDParam(0, cost);
00532 AddTextEffect(msg, pt.x, pt.y, DAY_TICKS, TE_RISING);
00533 }
00534
00543 void ShowFeederIncomeAnimation(int x, int y, int z, Money transfer, Money income)
00544 {
00545 Point pt = RemapCoords(x, y, z);
00546
00547 SetDParam(0, transfer);
00548 if (income == 0) {
00549 AddTextEffect(STR_FEEDER, pt.x, pt.y, DAY_TICKS, TE_RISING);
00550 } else {
00551 StringID msg = STR_FEEDER_COST;
00552 if (income < 0) {
00553 income = -income;
00554 msg = STR_FEEDER_INCOME;
00555 }
00556 SetDParam(1, income);
00557 AddTextEffect(msg, pt.x, pt.y, DAY_TICKS, TE_RISING);
00558 }
00559 }
00560
00570 TextEffectID ShowFillingPercent(int x, int y, int z, uint8 percent, StringID string)
00571 {
00572 Point pt = RemapCoords(x, y, z);
00573
00574 assert(string != STR_NULL);
00575
00576 SetDParam(0, percent);
00577 return AddTextEffect(string, pt.x, pt.y, 0, TE_STATIC);
00578 }
00579
00585 void UpdateFillingPercent(TextEffectID te_id, uint8 percent, StringID string)
00586 {
00587 assert(string != STR_NULL);
00588
00589 SetDParam(0, percent);
00590 UpdateTextEffect(te_id, string);
00591 }
00592
00597 void HideFillingPercent(TextEffectID *te_id)
00598 {
00599 if (*te_id == INVALID_TE_ID) return;
00600
00601 RemoveTextEffect(*te_id);
00602 *te_id = INVALID_TE_ID;
00603 }
00604
00605 static const NWidgetPart _nested_tooltips_widgets[] = {
00606 NWidget(WWT_PANEL, COLOUR_GREY, WID_TT_BACKGROUND), SetMinimalSize(200, 32), EndContainer(),
00607 };
00608
00609 static const WindowDesc _tool_tips_desc(
00610 WDP_MANUAL, 0, 0,
00611 WC_TOOLTIPS, WC_NONE,
00612 0,
00613 _nested_tooltips_widgets, lengthof(_nested_tooltips_widgets)
00614 );
00615
00617 struct TooltipsWindow : public Window
00618 {
00619 StringID string_id;
00620 byte paramcount;
00621 uint64 params[5];
00622 TooltipCloseCondition close_cond;
00623
00624 TooltipsWindow(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip) : Window()
00625 {
00626 this->parent = parent;
00627 this->string_id = str;
00628 assert_compile(sizeof(this->params[0]) == sizeof(params[0]));
00629 assert(paramcount <= lengthof(this->params));
00630 memcpy(this->params, params, sizeof(this->params[0]) * paramcount);
00631 this->paramcount = paramcount;
00632 this->close_cond = close_tooltip;
00633
00634 this->InitNested(&_tool_tips_desc);
00635
00636 CLRBITS(this->flags, WF_WHITE_BORDER);
00637 }
00638
00639 virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
00640 {
00641
00642
00643
00644 int scr_top = GetMainViewTop() + 2;
00645 int scr_bot = GetMainViewBottom() - 2;
00646
00647 Point pt;
00648
00649
00650
00651
00652 pt.y = Clamp(_cursor.pos.y + _cursor.size.y + _cursor.offs.y + 5, scr_top, scr_bot);
00653 if (pt.y + sm_height > scr_bot) pt.y = min(_cursor.pos.y + _cursor.offs.y - 5, scr_bot) - sm_height;
00654 pt.x = sm_width >= _screen.width ? 0 : Clamp(_cursor.pos.x - (sm_width >> 1), 0, _screen.width - sm_width);
00655
00656 return pt;
00657 }
00658
00659 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00660 {
00661
00662 for (uint i = 0; i != this->paramcount; i++) SetDParam(i, this->params[i]);
00663
00664 size->width = min(GetStringBoundingBox(this->string_id).width, 194);
00665 size->height = GetStringHeight(this->string_id, size->width);
00666
00667
00668 size->width += 2 + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00669 size->height += 2 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00670 }
00671
00672 virtual void DrawWidget(const Rect &r, int widget) const
00673 {
00674
00675 GfxFillRect(r.left, r.top, r.right, r.bottom, PC_BLACK);
00676 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_LIGHT_YELLOW);
00677
00678 for (uint arg = 0; arg < this->paramcount; arg++) {
00679 SetDParam(arg, this->params[arg]);
00680 }
00681 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM, this->string_id, TC_FROMSTRING, SA_CENTER);
00682 }
00683
00684 virtual void OnMouseLoop()
00685 {
00686
00687 if (!_cursor.in_window) {
00688 delete this;
00689 return;
00690 }
00691
00692
00693
00694 switch (this->close_cond) {
00695 case TCC_RIGHT_CLICK: if (!_right_button_down) delete this; break;
00696 case TCC_LEFT_CLICK: if (!_left_button_down) delete this; break;
00697 case TCC_HOVER: if (!_mouse_hovering) delete this; break;
00698 }
00699 }
00700 };
00701
00710 void GuiShowTooltips(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip)
00711 {
00712 DeleteWindowById(WC_TOOLTIPS, 0);
00713
00714 if (str == STR_NULL) return;
00715
00716 new TooltipsWindow(parent, str, paramcount, params, close_tooltip);
00717 }
00718
00719 bool QueryString::HasEditBoxFocus(const Window *w, int wid) const
00720 {
00721 if (w->IsWidgetGloballyFocused(wid)) return true;
00722 if (w->window_class != WC_OSK || _focused_window != w->parent) return false;
00723 return w->parent->nested_focus != NULL && w->parent->nested_focus->type == WWT_EDITBOX;
00724 }
00725
00726 HandleEditBoxResult QueryString::HandleEditBoxKey(Window *w, int wid, uint16 key, uint16 keycode, EventState &state)
00727 {
00728 if (!QueryString::HasEditBoxFocus(w, wid)) return HEBR_NOT_FOCUSED;
00729
00730 state = ES_HANDLED;
00731
00732 switch (keycode) {
00733 case WKC_ESC: return HEBR_CANCEL;
00734
00735 case WKC_RETURN: case WKC_NUM_ENTER: return HEBR_CONFIRM;
00736
00737 #ifdef WITH_COCOA
00738 case (WKC_META | 'V'):
00739 #endif
00740 case (WKC_CTRL | 'V'):
00741 if (this->text.InsertClipboard()) w->SetWidgetDirty(wid);
00742 break;
00743
00744 #ifdef WITH_COCOA
00745 case (WKC_META | 'U'):
00746 #endif
00747 case (WKC_CTRL | 'U'):
00748 this->text.DeleteAll();
00749 w->SetWidgetDirty(wid);
00750 break;
00751
00752 case WKC_BACKSPACE: case WKC_DELETE:
00753 if (this->text.DeleteChar(keycode)) w->SetWidgetDirty(wid);
00754 break;
00755
00756 case WKC_LEFT: case WKC_RIGHT: case WKC_END: case WKC_HOME:
00757 if (this->text.MovePos(keycode)) w->SetWidgetDirty(wid);
00758 break;
00759
00760 default:
00761 if (IsValidChar(key, this->afilter)) {
00762 if (this->text.InsertChar(key)) w->SetWidgetDirty(wid);
00763 } else {
00764 state = ES_NOT_HANDLED;
00765 }
00766 }
00767
00768 return HEBR_EDITING;
00769 }
00770
00771 void QueryString::HandleEditBox(Window *w, int wid)
00772 {
00773 if (HasEditBoxFocus(w, wid) && this->text.HandleCaret()) {
00774 w->SetWidgetDirty(wid);
00775
00776
00777 if (w->window_class != WC_OSK) {
00778 Window *w_osk = FindWindowById(WC_OSK, 0);
00779 if (w_osk != NULL && w_osk->parent == w) w_osk->InvalidateData();
00780 }
00781 }
00782 }
00783
00784 void QueryString::DrawEditBox(Window *w, int wid)
00785 {
00786 const NWidgetBase *wi = w->GetWidget<NWidgetBase>(wid);
00787
00788 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
00789 int left = wi->pos_x;
00790 int right = wi->pos_x + wi->current_x - 1;
00791 int top = wi->pos_y;
00792 int bottom = wi->pos_y + wi->current_y - 1;
00793
00794 GfxFillRect(left + 1, top + 1, right - 1, bottom - 1, PC_BLACK);
00795
00796
00797 DrawPixelInfo dpi;
00798 if (!FillDrawPixelInfo(&dpi, left + WD_FRAMERECT_LEFT, top + WD_FRAMERECT_TOP, right - left - WD_FRAMERECT_RIGHT, bottom - top - WD_FRAMERECT_BOTTOM)) return;
00799
00800 DrawPixelInfo *old_dpi = _cur_dpi;
00801 _cur_dpi = &dpi;
00802
00803
00804
00805 const Textbuf *tb = &this->text;
00806 int delta = min(0, (right - left) - tb->pixels - 10);
00807
00808 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
00809
00810 DrawString(delta, tb->pixels, 0, tb->buf, TC_YELLOW);
00811 if (HasEditBoxFocus(w, wid) && tb->caret) {
00812 int caret_width = GetStringBoundingBox("_").width;
00813 DrawString(tb->caretxoffs + delta, tb->caretxoffs + delta + caret_width, 0, "_", TC_WHITE);
00814 }
00815
00816 _cur_dpi = old_dpi;
00817 }
00818
00819 HandleEditBoxResult QueryStringBaseWindow::HandleEditBoxKey(int wid, uint16 key, uint16 keycode, EventState &state)
00820 {
00821 return this->QueryString::HandleEditBoxKey(this, wid, key, keycode, state);
00822 }
00823
00824 void QueryStringBaseWindow::HandleEditBox(int wid)
00825 {
00826 this->QueryString::HandleEditBox(this, wid);
00827 }
00828
00829 void QueryStringBaseWindow::DrawEditBox(int wid)
00830 {
00831 this->QueryString::DrawEditBox(this, wid);
00832 }
00833
00834 void QueryStringBaseWindow::OnOpenOSKWindow(int wid)
00835 {
00836 ShowOnScreenKeyboard(this, wid, 0, 0);
00837 }
00838
00840 struct QueryStringWindow : public QueryStringBaseWindow
00841 {
00842 QueryStringFlags flags;
00843
00844 QueryStringWindow(StringID str, StringID caption, uint max_bytes, uint max_chars, const WindowDesc *desc, Window *parent, CharSetFilter afilter, QueryStringFlags flags) :
00845 QueryStringBaseWindow(max_bytes, max_chars)
00846 {
00847 GetString(this->edit_str_buf, str, &this->edit_str_buf[max_bytes - 1]);
00848 str_validate(this->edit_str_buf, &this->edit_str_buf[max_bytes - 1], SVS_NONE);
00849
00850
00851
00852 while (Utf8StringLength(this->edit_str_buf) + 1 > max_chars) {
00853 *Utf8PrevChar(this->edit_str_buf + strlen(this->edit_str_buf)) = '\0';
00854 }
00855
00856 if ((flags & QSF_ACCEPT_UNCHANGED) == 0) this->orig = strdup(this->edit_str_buf);
00857
00858 this->caption = caption;
00859 this->afilter = afilter;
00860 this->flags = flags;
00861 this->text.Initialize(this->edit_str_buf, max_bytes, max_chars);
00862
00863 this->InitNested(desc, WN_QUERY_STRING);
00864
00865 this->parent = parent;
00866
00867 this->SetFocusedWidget(WID_QS_TEXT);
00868 this->LowerWidget(WID_QS_TEXT);
00869 }
00870
00871 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00872 {
00873 if (widget == WID_QS_DEFAULT && (this->flags & QSF_ENABLE_DEFAULT) == 0) {
00874
00875 fill->width = 0;
00876 resize->width = 0;
00877 size->width = 0;
00878 }
00879 }
00880
00881 virtual void OnPaint()
00882 {
00883 this->DrawWidgets();
00884
00885 this->DrawEditBox(WID_QS_TEXT);
00886 }
00887
00888 virtual void SetStringParameters(int widget) const
00889 {
00890 if (widget == WID_QS_CAPTION) SetDParam(0, this->caption);
00891 }
00892
00893 void OnOk()
00894 {
00895 if (this->orig == NULL || strcmp(this->text.buf, this->orig) != 0) {
00896
00897
00898 if (this->parent != NULL) {
00899 this->parent->OnQueryTextFinished(this->text.buf);
00900 } else {
00901 HandleOnEditText(this->text.buf);
00902 }
00903 this->handled = true;
00904 }
00905 }
00906
00907 virtual void OnClick(Point pt, int widget, int click_count)
00908 {
00909 switch (widget) {
00910 case WID_QS_DEFAULT:
00911 this->text.buf[0] = '\0';
00912
00913 case WID_QS_OK:
00914 this->OnOk();
00915
00916 case WID_QS_CANCEL:
00917 delete this;
00918 break;
00919 }
00920 }
00921
00922 virtual void OnMouseLoop()
00923 {
00924 this->HandleEditBox(WID_QS_TEXT);
00925 }
00926
00927 virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00928 {
00929 EventState state = ES_NOT_HANDLED;
00930 switch (this->HandleEditBoxKey(WID_QS_TEXT, key, keycode, state)) {
00931 default: NOT_REACHED();
00932 case HEBR_EDITING: {
00933 Window *osk = FindWindowById(WC_OSK, 0);
00934 if (osk != NULL && osk->parent == this) osk->InvalidateData();
00935 break;
00936 }
00937 case HEBR_CONFIRM: this->OnOk();
00938
00939 case HEBR_CANCEL: delete this; break;
00940 case HEBR_NOT_FOCUSED: break;
00941 }
00942 return state;
00943 }
00944
00945 virtual void OnOpenOSKWindow(int wid)
00946 {
00947 ShowOnScreenKeyboard(this, wid, WID_QS_CANCEL, WID_QS_OK);
00948 }
00949
00950 ~QueryStringWindow()
00951 {
00952 if (!this->handled && this->parent != NULL) {
00953 Window *parent = this->parent;
00954 this->parent = NULL;
00955 parent->OnQueryTextFinished(NULL);
00956 }
00957 }
00958 };
00959
00960 static const NWidgetPart _nested_query_string_widgets[] = {
00961 NWidget(NWID_HORIZONTAL),
00962 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00963 NWidget(WWT_CAPTION, COLOUR_GREY, WID_QS_CAPTION), SetDataTip(STR_WHITE_STRING, STR_NULL),
00964 EndContainer(),
00965 NWidget(WWT_PANEL, COLOUR_GREY),
00966 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_QS_TEXT), SetMinimalSize(256, 12), SetFill(1, 1), SetPadding(2, 2, 2, 2),
00967 EndContainer(),
00968 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00969 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_DEFAULT), SetMinimalSize(87, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_DEFAULT, STR_NULL),
00970 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_CANCEL), SetMinimalSize(86, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
00971 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_OK), SetMinimalSize(87, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_OK, STR_NULL),
00972 EndContainer(),
00973 };
00974
00975 static const WindowDesc _query_string_desc(
00976 WDP_CENTER, 0, 0,
00977 WC_QUERY_STRING, WC_NONE,
00978 0,
00979 _nested_query_string_widgets, lengthof(_nested_query_string_widgets)
00980 );
00981
00992 void ShowQueryString(StringID str, StringID caption, uint maxsize, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
00993 {
00994 DeleteWindowByClass(WC_QUERY_STRING);
00995 new QueryStringWindow(str, caption, ((flags & QSF_LEN_IN_CHARS) ? MAX_CHAR_LENGTH : 1) * maxsize, maxsize, &_query_string_desc, parent, afilter, flags);
00996 }
00997
01001 struct QueryWindow : public Window {
01002 QueryCallbackProc *proc;
01003 uint64 params[10];
01004 StringID message;
01005 StringID caption;
01006
01007 QueryWindow(const WindowDesc *desc, StringID caption, StringID message, Window *parent, QueryCallbackProc *callback) : Window()
01008 {
01009
01010
01011 CopyOutDParam(this->params, 0, lengthof(this->params));
01012 this->caption = caption;
01013 this->message = message;
01014 this->proc = callback;
01015
01016 this->InitNested(desc, WN_CONFIRM_POPUP_QUERY);
01017
01018 this->parent = parent;
01019 this->left = parent->left + (parent->width / 2) - (this->width / 2);
01020 this->top = parent->top + (parent->height / 2) - (this->height / 2);
01021 }
01022
01023 ~QueryWindow()
01024 {
01025 if (this->proc != NULL) this->proc(this->parent, false);
01026 }
01027
01028 virtual void SetStringParameters(int widget) const
01029 {
01030 switch (widget) {
01031 case WID_Q_CAPTION:
01032 CopyInDParam(1, this->params, lengthof(this->params));
01033 SetDParam(0, this->caption);
01034 break;
01035
01036 case WID_Q_TEXT:
01037 CopyInDParam(0, this->params, lengthof(this->params));
01038 break;
01039 }
01040 }
01041
01042 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01043 {
01044 if (widget != WID_Q_TEXT) return;
01045
01046 Dimension d = GetStringMultiLineBoundingBox(this->message, *size);
01047 d.width += WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
01048 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
01049 *size = d;
01050 }
01051
01052 virtual void DrawWidget(const Rect &r, int widget) const
01053 {
01054 if (widget != WID_Q_TEXT) return;
01055
01056 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
01057 this->message, TC_FROMSTRING, SA_CENTER);
01058 }
01059
01060 virtual void OnClick(Point pt, int widget, int click_count)
01061 {
01062 switch (widget) {
01063 case WID_Q_YES: {
01064
01065
01066 QueryCallbackProc *proc = this->proc;
01067 Window *parent = this->parent;
01068
01069 this->proc = NULL;
01070 delete this;
01071 if (proc != NULL) {
01072 proc(parent, true);
01073 proc = NULL;
01074 }
01075 break;
01076 }
01077 case WID_Q_NO:
01078 delete this;
01079 break;
01080 }
01081 }
01082
01083 virtual EventState OnKeyPress(uint16 key, uint16 keycode)
01084 {
01085
01086 switch (keycode) {
01087 case WKC_RETURN:
01088 case WKC_NUM_ENTER:
01089 if (this->proc != NULL) {
01090 this->proc(this->parent, true);
01091 this->proc = NULL;
01092 }
01093
01094 case WKC_ESC:
01095 delete this;
01096 return ES_HANDLED;
01097 }
01098 return ES_NOT_HANDLED;
01099 }
01100 };
01101
01102 static const NWidgetPart _nested_query_widgets[] = {
01103 NWidget(NWID_HORIZONTAL),
01104 NWidget(WWT_CLOSEBOX, COLOUR_RED),
01105 NWidget(WWT_CAPTION, COLOUR_RED, WID_Q_CAPTION), SetDataTip(STR_JUST_STRING, STR_NULL),
01106 EndContainer(),
01107 NWidget(WWT_PANEL, COLOUR_RED), SetPIP(8, 15, 8),
01108 NWidget(WWT_TEXT, COLOUR_RED, WID_Q_TEXT), SetMinimalSize(200, 12),
01109 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(20, 29, 20),
01110 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_Q_NO), SetMinimalSize(71, 12), SetDataTip(STR_QUIT_NO, STR_NULL),
01111 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_Q_YES), SetMinimalSize(71, 12), SetDataTip(STR_QUIT_YES, STR_NULL),
01112 EndContainer(),
01113 EndContainer(),
01114 };
01115
01116 static const WindowDesc _query_desc(
01117 WDP_CENTER, 0, 0,
01118 WC_CONFIRM_POPUP_QUERY, WC_NONE,
01119 WDF_UNCLICK_BUTTONS | WDF_MODAL,
01120 _nested_query_widgets, lengthof(_nested_query_widgets)
01121 );
01122
01132 void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc *callback)
01133 {
01134 if (parent == NULL) parent = FindWindowById(WC_MAIN_WINDOW, 0);
01135
01136 const Window *w;
01137 FOR_ALL_WINDOWS_FROM_BACK(w) {
01138 if (w->window_class != WC_CONFIRM_POPUP_QUERY) continue;
01139
01140 const QueryWindow *qw = (const QueryWindow *)w;
01141 if (qw->parent != parent || qw->proc != callback) continue;
01142
01143 delete qw;
01144 break;
01145 }
01146
01147 new QueryWindow(&_query_desc, caption, message, parent, callback);
01148 }