OpenTTD Source 20250328-master-gc3457cd4c0
newgrf_debug_gui.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#include "stdafx.h"
11#include "core/backup_type.hpp"
13#include "window_gui.h"
14#include "window_func.h"
16#include "spritecache.h"
17#include "string_func.h"
18#include "strings_func.h"
19#include "textbuf_gui.h"
20#include "vehicle_gui.h"
21#include "zoom_func.h"
22
23#include "engine_base.h"
24#include "industry.h"
25#include "object_base.h"
26#include "station_base.h"
27#include "town.h"
28#include "vehicle_base.h"
29#include "train.h"
30#include "roadveh.h"
31
32#include "newgrf_act5.h"
33#include "newgrf_airport.h"
34#include "newgrf_airporttiles.h"
35#include "newgrf_badge.h"
36#include "newgrf_debug.h"
37#include "newgrf_object.h"
38#include "newgrf_spritegroup.h"
39#include "newgrf_station.h"
40#include "newgrf_town.h"
41#include "newgrf_railtype.h"
42#include "newgrf_industries.h"
44
46
47#include "table/strings.h"
48
49#include "safeguards.h"
50
52NewGrfDebugSpritePicker _newgrf_debug_sprite_picker = { SPM_NONE, nullptr, std::vector<SpriteID>() };
53
59static inline uint GetFeatureIndex(uint window_number)
60{
61 return GB(window_number, 0, 24);
62}
63
71static inline uint GetInspectWindowNumber(GrfSpecFeature feature, uint index)
72{
73 assert((index >> 24) == 0);
74 return (feature << 24) | index;
75}
76
77static inline uint GetInspectWindowNumber(GrfSpecFeature feature, ConvertibleThroughBase auto index) { return GetInspectWindowNumber(feature, index.base()); }
78
83enum NIType : uint8_t {
86};
87
88typedef const void *NIOffsetProc(const void *b);
89
91struct NIProperty {
92 std::string_view name;
93 NIOffsetProc *offset_proc;
94 uint8_t read_size;
95 uint8_t prop;
96 uint8_t type;
97};
98
99
122
125 std::string_view name;
126 uint8_t var;
127};
128
130class NIHelper {
131public:
133 virtual ~NIHelper() = default;
134
140 virtual bool IsInspectable(uint index) const = 0;
141
147 virtual uint GetParent(uint index) const = 0;
148
154 virtual const void *GetInstance(uint index) const = 0;
155
161 virtual const void *GetSpec(uint index) const = 0;
162
167 virtual std::string GetName(uint index) const = 0;
168
174 virtual uint32_t GetGRFID(uint index) const = 0;
175
181 virtual std::span<const BadgeID> GetBadges(uint index) const = 0;
182
191 virtual uint Resolve(uint index, uint var, uint param, bool &avail) const = 0;
192
197 virtual bool PSAWithParameter() const
198 {
199 return false;
200 }
201
208 virtual const std::span<int32_t> GetPSA([[maybe_unused]] uint index, [[maybe_unused]] uint32_t grfid) const
209 {
210 return {};
211 }
212};
213
214
216struct NIFeature {
217 std::span<const NIProperty> properties;
218 std::span<const NICallback> callbacks;
219 std::span<const NIVariable> variables;
220 std::unique_ptr<const NIHelper> helper;
221};
222
223/* Load all the NewGRF debug data; externalised as it is just a huge bunch of tables. */
225
231static inline GrfSpecFeature GetFeatureNum(uint window_number)
232{
233 return (GrfSpecFeature)GB(window_number, 24, 8);
234}
235
241static inline const NIFeature *GetFeature(uint window_number)
242{
243 GrfSpecFeature idx = GetFeatureNum(window_number);
244 return idx < GSF_FAKE_END ? _nifeatures[idx] : nullptr;
245}
246
253static inline const NIHelper &GetFeatureHelper(uint window_number)
254{
255 return *GetFeature(window_number)->helper;
256}
257
261 static uint32_t var60params[GSF_FAKE_END][0x20];
262
265
267 uint chain_index = 0;
268
271
272 Scrollbar *vscroll = nullptr;
273
279 static bool HasVariableParameter(uint variable)
280 {
281 return IsInsideBS(variable, 0x60, 0x20);
282 }
283
289 {
290 this->caller_grfid = grfid;
291 this->SetDirty();
292 }
293
297 bool HasChainIndex() const
298 {
300 return f == GSF_TRAINS || f == GSF_ROADVEHICLES;
301 }
302
307 uint GetFeatureIndex() const
308 {
309 uint index = ::GetFeatureIndex(this->window_number);
310 if (this->chain_index > 0) {
311 assert(this->HasChainIndex());
312 const Vehicle *v = Vehicle::Get(index);
313 v = v->Move(this->chain_index);
314 if (v != nullptr) index = v->index.base();
315 }
316 return index;
317 }
318
323 {
324 if (this->chain_index == 0) return;
325
326 assert(this->HasChainIndex());
327
329 v = v->Move(this->chain_index);
330 if (v == nullptr) this->chain_index = 0;
331 }
332
334 {
335 this->CreateNestedTree();
336 this->vscroll = this->GetScrollbar(WID_NGRFI_SCROLLBAR);
337 this->FinishInitNested(wno);
338
339 this->vscroll->SetCount(0);
341
342 this->OnInvalidateData(0, true);
343 }
344
345 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
346 {
347 if (widget != WID_NGRFI_CAPTION) return this->Window::GetWidgetString(widget, stringid);
348
350 }
351
353 {
354 switch (widget) {
355 case WID_NGRFI_VEH_CHAIN: {
356 assert(this->HasChainIndex());
358 size.height = std::max(size.height, GetVehicleImageCellSize((VehicleType)(VEH_TRAIN + (f - GSF_TRAINS)), EIT_IN_DEPOT).height + 2 + WidgetDimensions::scaled.bevel.Vertical());
359 break;
360 }
361
363 resize.height = std::max(11, GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal);
364 resize.width = 1;
365
366 size.height = 5 * resize.height + WidgetDimensions::scaled.frametext.Vertical();
367 break;
368 }
369 }
370
377 void DrawString(const Rect &r, int offset, const std::string &string) const
378 {
379 offset -= this->vscroll->GetPosition();
380 if (offset < 0 || offset >= this->vscroll->GetCapacity()) return;
381
382 ::DrawString(r.Shrink(WidgetDimensions::scaled.frametext).Shrink(0, offset * this->resize.step_height, 0, 0), string, TC_BLACK);
383 }
384
389 void DrawVehicleChainWidget(const Rect &r) const
390 {
391 const Vehicle *v = Vehicle::Get(this->GetFeatureIndex());
392 int total_width = 0;
393 int sel_start = 0;
394 int sel_end = 0;
395 for (const Vehicle *u = v->First(); u != nullptr; u = u->Next()) {
396 if (u == v) sel_start = total_width;
397 switch (u->type) {
398 case VEH_TRAIN: total_width += Train::From(u)->GetDisplayImageWidth(); break;
399 case VEH_ROAD: total_width += RoadVehicle::From(u)->GetDisplayImageWidth(); break;
400 default: NOT_REACHED();
401 }
402 if (u == v) sel_end = total_width;
403 }
404
406 int width = br.Width();
407 int skip = 0;
408 if (total_width > width) {
409 int sel_center = (sel_start + sel_end) / 2;
410 if (sel_center > width / 2) skip = std::min(total_width - width, sel_center - width / 2);
411 }
412
414 int h = GetVehicleImageCellSize((VehicleType)(VEH_TRAIN + (f - GSF_TRAINS)), EIT_IN_DEPOT).height;
415 int y = CenterBounds(br.top, br.bottom, h);
416 DrawVehicleImage(v->First(), br, VehicleID::Invalid(), EIT_IN_DETAILS, skip);
417
418 /* Highlight the articulated part (this is different to the whole-vehicle highlighting of DrawVehicleImage */
419 if (_current_text_dir == TD_RTL) {
420 DrawFrameRect(r.right - sel_end + skip, y, r.right - sel_start + skip, y + h, COLOUR_WHITE, FrameFlag::BorderOnly);
421 } else {
422 DrawFrameRect(r.left + sel_start - skip, y, r.left + sel_end - skip, y + h, COLOUR_WHITE, FrameFlag::BorderOnly);
423 }
424 }
425
426 std::string GetPropertyString(const NIProperty &nip, uint value) const
427 {
428 switch (nip.type) {
429 case NIT_INT: return GetString(STR_JUST_INT, value);
430 case NIT_CARGO: return GetString(IsValidCargoType(value) ? CargoSpec::Get(value)->name : STR_QUANTITY_N_A);
431 default: NOT_REACHED();
432 }
433 }
434
439 void DrawMainPanelWidget(const Rect &r) const
440 {
441 uint index = this->GetFeatureIndex();
442 const NIFeature *nif = GetFeature(this->window_number);
443 const NIHelper &nih = *nif->helper;
444 const void *base = nih.GetInstance(index);
445 const void *base_spec = nih.GetSpec(index);
446
447 uint i = 0;
448 if (!nif->variables.empty()) {
449 this->DrawString(r, i++, "Variables:");
450 for (const NIVariable &niv : nif->variables) {
451 bool avail = true;
453 uint value = nih.Resolve(index, niv.var, param, avail);
454
455 if (!avail) continue;
456
457 if (HasVariableParameter(niv.var)) {
458 this->DrawString(r, i++, fmt::format(" {:02x}[{:02x}]: {:08x} ({})", niv.var, param, value, niv.name));
459 } else {
460 this->DrawString(r, i++, fmt::format(" {:02x}: {:08x} ({})", niv.var, value, niv.name));
461 }
462 }
463 }
464
465 auto psa = nih.GetPSA(index, this->caller_grfid);
466 if (!psa.empty()) {
467 if (nih.PSAWithParameter()) {
468 this->DrawString(r, i++, fmt::format("Persistent storage [{:08X}]:", std::byteswap(this->caller_grfid)));
469 } else {
470 this->DrawString(r, i++, "Persistent storage:");
471 }
472 assert(psa.size() % 4 == 0);
473 for (size_t j = 0; j < psa.size(); j += 4) {
474 this->DrawString(r, i++, fmt::format(" {}: {} {} {} {}", j, psa[j], psa[j + 1], psa[j + 2], psa[j + 3]));
475 }
476 }
477
478 auto badges = nih.GetBadges(index);
479 if (!badges.empty()) {
480 this->DrawString(r, i++, "Badges:");
481 for (const BadgeID &badge_index : badges) {
482 const Badge *badge = GetBadge(badge_index);
483 this->DrawString(r, i++, fmt::format(" {}: {}", StrMakeValid(badge->label), GetString(badge->name)));
484 }
485 }
486
487 if (!nif->properties.empty()) {
488 this->DrawString(r, i++, "Properties:");
489 for (const NIProperty &nip : nif->properties) {
490 const void *ptr = nip.offset_proc(base);
491 uint value;
492 switch (nip.read_size) {
493 case 1: value = *(const uint8_t *)ptr; break;
494 case 2: value = *(const uint16_t *)ptr; break;
495 case 4: value = *(const uint32_t *)ptr; break;
496 default: NOT_REACHED();
497 }
498
499 this->DrawString(r, i++, fmt::format(" {:02x}: {} ({})", nip.prop, this->GetPropertyString(nip, value), nip.name));
500 }
501 }
502
503 if (!nif->callbacks.empty()) {
504 this->DrawString(r, i++, "Callbacks:");
505 for (const NICallback &nic : nif->callbacks) {
506 if (!std::holds_alternative<std::monostate>(nic.cb_bit)) {
507 const void *ptr = nic.offset_proc(base_spec);
508 uint value;
509 switch (nic.read_size) {
510 case 1: value = *(const uint8_t *)ptr; break;
511 case 2: value = *(const uint16_t *)ptr; break;
512 case 4: value = *(const uint32_t *)ptr; break;
513 default: NOT_REACHED();
514 }
515
516 struct visitor {
517 uint value;
518
519 bool operator()(const std::monostate &) { return false; }
520 bool operator()(const VehicleCallbackMask &bit) { return static_cast<VehicleCallbackMasks>(this->value).Test(bit); }
521 bool operator()(const StationCallbackMask &bit) { return static_cast<StationCallbackMasks>(this->value).Test(bit); }
522 bool operator()(const RoadStopCallbackMask &bit) { return static_cast<RoadStopCallbackMasks>(this->value).Test(bit); }
523 bool operator()(const HouseCallbackMask &bit) { return static_cast<HouseCallbackMasks>(this->value).Test(bit); }
524 bool operator()(const CanalCallbackMask &bit) { return static_cast<CanalCallbackMasks>(this->value).Test(bit); }
525 bool operator()(const CargoCallbackMask &bit) { return static_cast<CargoCallbackMasks>(this->value).Test(bit); }
526 bool operator()(const IndustryCallbackMask &bit) { return static_cast<IndustryCallbackMasks>(this->value).Test(bit); }
527 bool operator()(const IndustryTileCallbackMask &bit) { return static_cast<IndustryTileCallbackMasks>(this->value).Test(bit); }
528 bool operator()(const ObjectCallbackMask &bit) { return static_cast<ObjectCallbackMasks>(this->value).Test(bit); }
529 bool operator()(const AirportTileCallbackMask &bit) { return static_cast<AirportTileCallbackMasks>(this->value).Test(bit); }
530 };
531
532 if (!std::visit(visitor{value}, nic.cb_bit)) continue;
533 this->DrawString(r, i++, fmt::format(" {:03x}: {}", nic.cb_id, nic.name));
534 } else {
535 this->DrawString(r, i++, fmt::format(" {:03x}: {} (unmasked)", nic.cb_id, nic.name));
536 }
537 }
538 }
539
540 /* Not nice and certainly a hack, but it beats duplicating
541 * this whole function just to count the actual number of
542 * elements. Especially because they need to be redrawn. */
543 const_cast<NewGRFInspectWindow*>(this)->vscroll->SetCount(i);
544 }
545
546 void DrawWidget(const Rect &r, WidgetID widget) const override
547 {
548 switch (widget) {
550 this->DrawVehicleChainWidget(r);
551 break;
552
554 this->DrawMainPanelWidget(r);
555 break;
556 }
557 }
558
559 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
560 {
561 switch (widget) {
562 case WID_NGRFI_PARENT: {
564 uint index = nih.GetParent(this->GetFeatureIndex());
565 ::ShowNewGRFInspectWindow(GetFeatureNum(index), ::GetFeatureIndex(index), nih.GetGRFID(this->GetFeatureIndex()));
566 break;
567 }
568
570 if (this->chain_index > 0) {
571 this->chain_index--;
572 this->InvalidateData();
573 }
574 break;
575
577 if (this->HasChainIndex()) {
578 uint index = this->GetFeatureIndex();
579 Vehicle *v = Vehicle::Get(index);
580 if (v != nullptr && v->Next() != nullptr) {
581 this->chain_index++;
582 this->InvalidateData();
583 }
584 }
585 break;
586
587 case WID_NGRFI_MAINPANEL: {
588 /* Does this feature have variables? */
589 const NIFeature *nif = GetFeature(this->window_number);
590 if (nif->variables.empty()) return;
591
592 /* Get the line, make sure it's within the boundaries. */
594 if (line == INT32_MAX) return;
595
596 /* Find the variable related to the line */
597 for (const NIVariable &niv : nif->variables) {
598 if (--line != 0) continue; // 0 because of the "Variables:" line
599
600 if (!HasVariableParameter(niv.var)) break;
601
602 this->current_edit_param = niv.var;
604 }
605 }
606 }
607 }
608
609 void OnQueryTextFinished(std::optional<std::string> str) override
610 {
611 if (!str.has_value() || str->empty()) return;
612
613 NewGRFInspectWindow::var60params[GetFeatureNum(this->window_number)][this->current_edit_param - 0x60] = std::strtol(str->c_str(), nullptr, 16);
614 this->SetDirty();
615 }
616
617 void OnResize() override
618 {
619 this->vscroll->SetCapacityFromWidget(this, WID_NGRFI_MAINPANEL, WidgetDimensions::scaled.frametext.Vertical());
620 }
621
627 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
628 {
629 if (!gui_scope) return;
630 if (this->HasChainIndex()) {
631 this->ValidateChainIndex();
632 this->SetWidgetDisabledState(WID_NGRFI_VEH_PREV, this->chain_index == 0);
634 this->SetWidgetDisabledState(WID_NGRFI_VEH_NEXT, v == nullptr || v->Next() == nullptr);
635 }
636 }
637};
638
639/* static */ uint32_t NewGRFInspectWindow::var60params[GSF_FAKE_END][0x20] = { {0} }; // Use spec to have 0s in whole array
640
641static constexpr NWidgetPart _nested_newgrf_inspect_chain_widgets[] = {
643 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
644 NWidget(WWT_CAPTION, COLOUR_GREY, WID_NGRFI_CAPTION), SetStringTip(STR_NEWGRF_INSPECT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
645 NWidget(WWT_SHADEBOX, COLOUR_GREY),
646 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
647 NWidget(WWT_STICKYBOX, COLOUR_GREY),
648 EndContainer(),
649 NWidget(WWT_PANEL, COLOUR_GREY),
653 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_NGRFI_VEH_CHAIN), SetFill(1, 0), SetResize(1, 0),
654 EndContainer(),
655 EndContainer(),
660 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
661 EndContainer(),
662 EndContainer(),
663};
664
665static constexpr NWidgetPart _nested_newgrf_inspect_widgets[] = {
667 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
668 NWidget(WWT_CAPTION, COLOUR_GREY, WID_NGRFI_CAPTION), SetStringTip(STR_NEWGRF_INSPECT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
669 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_NGRFI_PARENT), SetStringTip(STR_NEWGRF_INSPECT_PARENT_BUTTON, STR_NEWGRF_INSPECT_PARENT_TOOLTIP),
670 NWidget(WWT_SHADEBOX, COLOUR_GREY),
671 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
672 NWidget(WWT_STICKYBOX, COLOUR_GREY),
673 EndContainer(),
678 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
679 EndContainer(),
680 EndContainer(),
681};
682
683static WindowDesc _newgrf_inspect_chain_desc(
684 WDP_AUTO, "newgrf_inspect_chain", 400, 300,
686 {},
687 _nested_newgrf_inspect_chain_widgets
688);
689
690static WindowDesc _newgrf_inspect_desc(
691 WDP_AUTO, "newgrf_inspect", 400, 300,
693 {},
694 _nested_newgrf_inspect_widgets
695);
696
706void ShowNewGRFInspectWindow(GrfSpecFeature feature, uint index, const uint32_t grfid)
707{
708 if (!IsNewGRFInspectable(feature, index)) return;
709
710 WindowNumber wno = GetInspectWindowNumber(feature, index);
711 WindowDesc &desc = (feature == GSF_TRAINS || feature == GSF_ROADVEHICLES) ? _newgrf_inspect_chain_desc : _newgrf_inspect_desc;
712 NewGRFInspectWindow *w = AllocateWindowDescFront<NewGRFInspectWindow, true>(desc, wno);
713 w->SetCallerGRFID(grfid);
714}
715
725{
726 if (feature == GSF_INVALID) return;
727
728 WindowNumber wno = GetInspectWindowNumber(feature, index);
730}
731
741{
742 if (feature == GSF_INVALID) return;
743
744 WindowNumber wno = GetInspectWindowNumber(feature, index);
746
747 /* Reinitialise the land information window to remove the "debug" sprite if needed.
748 * Note: Since we might be called from a command here, it is important to not execute
749 * the invalidation immediately. The landinfo window tests commands itself. */
751}
752
762bool IsNewGRFInspectable(GrfSpecFeature feature, uint index)
763{
764 const NIFeature *nif = GetFeature(GetInspectWindowNumber(feature, index));
765 if (nif == nullptr) return false;
766 return nif->helper->IsInspectable(index);
767}
768
775{
776 switch (GetTileType(tile)) {
777 default: return GSF_INVALID;
778 case MP_RAILWAY: return GSF_RAILTYPES;
779 case MP_ROAD: return IsLevelCrossing(tile) ? GSF_RAILTYPES : GSF_ROADTYPES;
780 case MP_HOUSE: return GSF_HOUSES;
781 case MP_INDUSTRY: return GSF_INDUSTRYTILES;
782 case MP_OBJECT: return GSF_OBJECTS;
783
784 case MP_STATION:
785 switch (GetStationType(tile)) {
786 case StationType::Rail: return GSF_STATIONS;
787 case StationType::Airport: return GSF_AIRPORTTILES;
788 case StationType::Bus: return GSF_ROADSTOPS;
789 case StationType::Truck: return GSF_ROADSTOPS;
790 default: return GSF_INVALID;
791 }
792 }
793}
794
801{
802 switch (type) {
803 case VEH_TRAIN: return GSF_TRAINS;
804 case VEH_ROAD: return GSF_ROADVEHICLES;
805 case VEH_SHIP: return GSF_SHIPS;
806 case VEH_AIRCRAFT: return GSF_AIRCRAFT;
807 default: return GSF_INVALID;
808 }
809}
810
811
812/**** Sprite Aligner ****/
813
816 typedef std::pair<int16_t, int16_t> XyOffs;
817
819 Scrollbar *vscroll = nullptr;
820 std::map<SpriteID, XyOffs> offs_start_map{};
821
822 static inline ZoomLevel zoom = ZOOM_LVL_END;
823 static bool centre;
824 static bool crosshair;
825 const Action5Type *act5_type = nullptr;
826
828 {
829 /* On first opening, set initial zoom to current zoom level. */
830 if (SpriteAlignerWindow::zoom == ZOOM_LVL_END) SpriteAlignerWindow::zoom = _gui_zoom;
831 SpriteAlignerWindow::zoom = Clamp(SpriteAlignerWindow::zoom, _settings_client.gui.zoom_min, _settings_client.gui.zoom_max);
832
833 /* Oh yes, we assume there is at least one normal sprite! */
835 this->SelectAction5Type();
836
837 this->CreateNestedTree();
838 this->vscroll = this->GetScrollbar(WID_SA_SCROLLBAR);
839 this->vscroll->SetCount(_newgrf_debug_sprite_picker.sprites.size());
840 this->FinishInitNested(wno);
841
842 this->SetWidgetLoweredState(WID_SA_CENTRE, SpriteAlignerWindow::centre);
843 this->SetWidgetLoweredState(WID_SA_CROSSHAIR, SpriteAlignerWindow::crosshair);
844
845 this->InvalidateData(0, true);
846 }
847
848 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
849 {
850 const Sprite *spr = GetSprite(this->current_sprite, SpriteType::Normal);
851 switch (widget) {
852 case WID_SA_CAPTION:
853 if (this->act5_type != nullptr) {
855 this->act5_type - GetAction5Types().data(),
856 this->current_sprite - this->act5_type->sprite_base,
857 GetOriginFile(this->current_sprite)->GetSimplifiedFilename(),
859 }
860 if (this->current_sprite < SPR_OPENTTD_BASE) {
862 this->current_sprite,
863 GetOriginFile(this->current_sprite)->GetSimplifiedFilename(),
865 }
867 GetOriginFile(this->current_sprite)->GetSimplifiedFilename(),
869
871 return GetString(STR_SPRITE_ALIGNER_OFFSETS_ABS, UnScaleByZoom(spr->x_offs, SpriteAlignerWindow::zoom), UnScaleByZoom(spr->y_offs, SpriteAlignerWindow::zoom));
872
873 case WID_SA_OFFSETS_REL: {
874 /* Relative offset is new absolute offset - starting absolute offset.
875 * Show 0, 0 as the relative offsets if entry is not in the map (meaning they have not been changed yet).
876 */
877 const auto key_offs_pair = this->offs_start_map.find(this->current_sprite);
878 if (key_offs_pair != this->offs_start_map.end()) {
880 UnScaleByZoom(spr->x_offs - key_offs_pair->second.first, SpriteAlignerWindow::zoom),
881 UnScaleByZoom(spr->y_offs - key_offs_pair->second.second, SpriteAlignerWindow::zoom));
882 }
883
885 }
886
887 default:
888 return this->Window::GetWidgetString(widget, stringid);
889 }
890 }
891
893 {
894 switch (widget) {
895 case WID_SA_SPRITE:
896 size.height = ScaleGUITrad(200);
897 break;
898
899 case WID_SA_LIST: {
900 Dimension d = {};
901 for (const auto &spritefile : GetCachedSpriteFiles()) {
903 }
904 size.width = d.width + padding.width;
905 resize.height = GetCharacterHeight(FS_NORMAL) + padding.height;
906 resize.width = 1;
907 fill.height = resize.height;
908 break;
909 }
910
911 default:
912 break;
913 }
914 }
915
916 void DrawWidget(const Rect &r, WidgetID widget) const override
917 {
918 switch (widget) {
919 case WID_SA_SPRITE: {
920 /* Center the sprite ourselves */
921 const Sprite *spr = GetSprite(this->current_sprite, SpriteType::Normal);
923 int x;
924 int y;
925 if (SpriteAlignerWindow::centre) {
926 x = -UnScaleByZoom(spr->x_offs, SpriteAlignerWindow::zoom) + (ir.Width() - UnScaleByZoom(spr->width, SpriteAlignerWindow::zoom)) / 2;
927 y = -UnScaleByZoom(spr->y_offs, SpriteAlignerWindow::zoom) + (ir.Height() - UnScaleByZoom(spr->height, SpriteAlignerWindow::zoom)) / 2;
928 } else {
929 x = ir.Width() / 2;
930 y = ir.Height() / 2;
931 }
932
934 if (!FillDrawPixelInfo(&new_dpi, ir)) break;
936
937 DrawSprite(this->current_sprite, PAL_NONE, x, y, nullptr, SpriteAlignerWindow::zoom);
938
939 Rect outline = {0, 0, UnScaleByZoom(spr->width, SpriteAlignerWindow::zoom) - 1, UnScaleByZoom(spr->height, SpriteAlignerWindow::zoom) - 1};
940 outline = outline.Translate(x + UnScaleByZoom(spr->x_offs, SpriteAlignerWindow::zoom), y + UnScaleByZoom(spr->y_offs, SpriteAlignerWindow::zoom));
941 DrawRectOutline(outline.Expand(1), PC_LIGHT_BLUE, 1, 1);
942
943 if (SpriteAlignerWindow::crosshair) {
944 GfxDrawLine(x, 0, x, ir.Height() - 1, PC_WHITE, 1, 1);
945 GfxDrawLine(0, y, ir.Width() - 1, y, PC_WHITE, 1, 1);
946 }
947 break;
948 }
949
950 case WID_SA_LIST: {
951 /* Don't redraw sprite list while it is still being filled by picker. */
952 if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW) break;
953
954 const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget);
955 int step_size = nwid->resize_y;
956
957 const std::vector<SpriteID> &list = _newgrf_debug_sprite_picker.sprites;
958
960 auto [first, last] = this->vscroll->GetVisibleRangeIterators(list);
961 for (auto it = first; it != last; ++it) {
962 const SpriteFile *file = GetOriginFile(*it);
963 if (file == nullptr) {
964 DrawString(ir, GetString(STR_JUST_COMMA, *it), *it == this->current_sprite ? TC_WHITE : (TC_GREY | TC_NO_SHADE), SA_RIGHT | SA_FORCE);
965 } else {
966 DrawString(ir, GetString(STR_SPRITE_ALIGNER_SPRITE, file->GetSimplifiedFilename(), GetSpriteLocalID(*it)), *it == this->current_sprite ? TC_WHITE : TC_BLACK);
967 }
968 ir.top += step_size;
969 }
970 break;
971 }
972 }
973 }
974
975 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
976 {
977 switch (widget) {
978 case WID_SA_PREVIOUS:
979 do {
980 this->current_sprite = (this->current_sprite == 0 ? GetMaxSpriteID() : this->current_sprite) - 1;
982 this->SelectAction5Type();
983 this->SetDirty();
984 break;
985
986 case WID_SA_GOTO:
988 break;
989
990 case WID_SA_NEXT:
991 do {
992 this->current_sprite = (this->current_sprite + 1) % GetMaxSpriteID();
994 this->SelectAction5Type();
995 this->SetDirty();
996 break;
997
998 case WID_SA_PICKER:
1000 _newgrf_debug_sprite_picker.mode = SPM_WAIT_CLICK;
1001 this->SetDirty();
1002 break;
1003
1004 case WID_SA_LIST: {
1005 auto it = this->vscroll->GetScrolledItemFromWidget(_newgrf_debug_sprite_picker.sprites, pt.y, this, widget);
1006 if (it != _newgrf_debug_sprite_picker.sprites.end()) {
1007 SpriteID spr = *it;
1009 }
1010 this->SelectAction5Type();
1011 this->SetDirty();
1012 break;
1013 }
1014
1015 case WID_SA_UP:
1016 case WID_SA_DOWN:
1017 case WID_SA_LEFT:
1018 case WID_SA_RIGHT: {
1019 /*
1020 * Yes... this is a hack.
1021 *
1022 * No... I don't think it is useful to make this less of a hack.
1023 *
1024 * If you want to align sprites, you just need the number. Generally
1025 * the sprite caches are big enough to not remove the sprite from the
1026 * cache. If that's not the case, just let the NewGRF developer
1027 * increase the cache size instead of storing thousands of offsets
1028 * for the incredibly small chance that it's actually going to be
1029 * used by someone and the sprite cache isn't big enough for that
1030 * particular NewGRF developer.
1031 */
1032 Sprite *spr = const_cast<Sprite *>(GetSprite(this->current_sprite, SpriteType::Normal));
1033
1034 /* Remember the original offsets of the current sprite, if not already in mapping. */
1035 if (this->offs_start_map.count(this->current_sprite) == 0) {
1036 this->offs_start_map[this->current_sprite] = XyOffs(spr->x_offs, spr->y_offs);
1037 }
1038 int amt = ScaleByZoom(_ctrl_pressed ? 8 : 1, SpriteAlignerWindow::zoom);
1039 switch (widget) {
1040 /* Move eight units at a time if ctrl is pressed. */
1041 case WID_SA_UP: spr->y_offs -= amt; break;
1042 case WID_SA_DOWN: spr->y_offs += amt; break;
1043 case WID_SA_LEFT: spr->x_offs -= amt; break;
1044 case WID_SA_RIGHT: spr->x_offs += amt; break;
1045 }
1046 /* Of course, we need to redraw the sprite, but where is it used?
1047 * Everywhere is a safe bet. */
1049 break;
1050 }
1051
1052 case WID_SA_RESET_REL:
1053 /* Reset the starting offsets for the current sprite. */
1054 this->offs_start_map.erase(this->current_sprite);
1055 this->SetDirty();
1056 break;
1057
1058 case WID_SA_CENTRE:
1059 SpriteAlignerWindow::centre = !SpriteAlignerWindow::centre;
1060 this->SetWidgetLoweredState(widget, SpriteAlignerWindow::centre);
1061 this->SetDirty();
1062 break;
1063
1064 case WID_SA_CROSSHAIR:
1065 SpriteAlignerWindow::crosshair = !SpriteAlignerWindow::crosshair;
1066 this->SetWidgetLoweredState(widget, SpriteAlignerWindow::crosshair);
1067 this->SetDirty();
1068 break;
1069
1070 default:
1071 if (IsInsideBS(widget, WID_SA_ZOOM, ZOOM_LVL_END)) {
1072 SpriteAlignerWindow::zoom = ZoomLevel(widget - WID_SA_ZOOM);
1073 this->InvalidateData(0, true);
1074 }
1075 break;
1076 }
1077 }
1078
1079 void OnQueryTextFinished(std::optional<std::string> str) override
1080 {
1081 if (!str.has_value() || str->empty()) return;
1082
1083 this->current_sprite = atoi(str->c_str());
1084 if (this->current_sprite >= GetMaxSpriteID()) this->current_sprite = 0;
1086 this->current_sprite = (this->current_sprite + 1) % GetMaxSpriteID();
1087 }
1088 this->SelectAction5Type();
1089 this->SetDirty();
1090 }
1091
1097 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
1098 {
1099 if (!gui_scope) return;
1100 if (data == 1) {
1101 /* Sprite picker finished */
1103 this->vscroll->SetCount(_newgrf_debug_sprite_picker.sprites.size());
1104 }
1105
1106 SpriteAlignerWindow::zoom = Clamp(SpriteAlignerWindow::zoom, _settings_client.gui.zoom_min, _settings_client.gui.zoom_max);
1107 for (ZoomLevel z = ZOOM_LVL_BEGIN; z < ZOOM_LVL_END; z++) {
1109 this->SetWidgetsLoweredState(SpriteAlignerWindow::zoom == z, WID_SA_ZOOM + z);
1110 }
1111 }
1112
1113 void OnResize() override
1114 {
1115 this->vscroll->SetCapacityFromWidget(this, WID_SA_LIST);
1116 }
1117
1118private:
1119 void SelectAction5Type()
1120 {
1121 const auto act5types = GetAction5Types();
1122 for (auto it = std::begin(act5types); it != std::end(act5types); ++it) {
1123 if (it->sprite_base <= this->current_sprite && this->current_sprite < it->sprite_base + it->max_sprites) {
1124 this->act5_type = &*it;
1125 return;
1126 }
1127 }
1128 this->act5_type = nullptr;
1129 }
1130};
1131
1132bool SpriteAlignerWindow::centre = true;
1133bool SpriteAlignerWindow::crosshair = true;
1134
1135static constexpr NWidgetPart _nested_sprite_aligner_widgets[] = {
1137 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1138 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SA_CAPTION),
1139 NWidget(WWT_SHADEBOX, COLOUR_GREY),
1140 NWidget(WWT_STICKYBOX, COLOUR_GREY),
1141 EndContainer(),
1142 NWidget(WWT_PANEL, COLOUR_GREY),
1146 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SA_PREVIOUS), SetStringTip(STR_SPRITE_ALIGNER_PREVIOUS_BUTTON, STR_SPRITE_ALIGNER_PREVIOUS_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
1147 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SA_GOTO), SetStringTip(STR_SPRITE_ALIGNER_GOTO_BUTTON, STR_SPRITE_ALIGNER_GOTO_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
1148 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SA_NEXT), SetStringTip(STR_SPRITE_ALIGNER_NEXT_BUTTON, STR_SPRITE_ALIGNER_NEXT_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
1149 EndContainer(),
1151 NWidget(NWID_SPACER), SetFill(1, 1), SetResize(1, 0),
1152 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SA_UP), SetSpriteTip(SPR_ARROW_UP, STR_SPRITE_ALIGNER_MOVE_TOOLTIP), SetResize(0, 0), SetMinimalSize(11, 11),
1153 NWidget(NWID_SPACER), SetFill(1, 1), SetResize(1, 0),
1154 EndContainer(),
1157 NWidget(NWID_SPACER), SetFill(1, 1), SetResize(0, 1),
1158 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SA_LEFT), SetSpriteTip(SPR_ARROW_LEFT, STR_SPRITE_ALIGNER_MOVE_TOOLTIP), SetResize(0, 0), SetMinimalSize(11, 11),
1159 NWidget(NWID_SPACER), SetFill(1, 1), SetResize(0, 1),
1160 EndContainer(),
1161 NWidget(WWT_PANEL, COLOUR_DARK_BLUE, WID_SA_SPRITE), SetToolTip(STR_SPRITE_ALIGNER_SPRITE_TOOLTIP), SetResize(1, 1), SetFill(1, 1),
1162 EndContainer(),
1164 NWidget(NWID_SPACER), SetFill(1, 1), SetResize(0, 1),
1165 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SA_RIGHT), SetSpriteTip(SPR_ARROW_RIGHT, STR_SPRITE_ALIGNER_MOVE_TOOLTIP), SetResize(0, 0), SetMinimalSize(11, 11),
1166 NWidget(NWID_SPACER), SetFill(1, 1), SetResize(0, 1),
1167 EndContainer(),
1168 EndContainer(),
1170 NWidget(NWID_SPACER), SetFill(1, 1), SetResize(1, 0),
1171 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SA_DOWN), SetSpriteTip(SPR_ARROW_DOWN, STR_SPRITE_ALIGNER_MOVE_TOOLTIP), SetResize(0, 0), SetMinimalSize(11, 11),
1172 NWidget(NWID_SPACER), SetFill(1, 1), SetResize(1, 0),
1173 EndContainer(),
1174 NWidget(WWT_LABEL, INVALID_COLOUR, WID_SA_OFFSETS_ABS), SetFill(1, 0), SetResize(1, 0),
1175 NWidget(WWT_LABEL, INVALID_COLOUR, WID_SA_OFFSETS_REL), SetFill(1, 0), SetResize(1, 0),
1177 NWidget(WWT_TEXTBTN_2, COLOUR_GREY, WID_SA_CENTRE), SetStringTip(STR_SPRITE_ALIGNER_CENTRE_OFFSET), SetFill(1, 0), SetResize(1, 0),
1178 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SA_RESET_REL), SetStringTip(STR_SPRITE_ALIGNER_RESET_BUTTON, STR_SPRITE_ALIGNER_RESET_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
1179 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SA_CROSSHAIR), SetStringTip(STR_SPRITE_ALIGNER_CROSSHAIR), SetFill(1, 0), SetResize(1, 0),
1180 EndContainer(),
1181 EndContainer(),
1183 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SA_PICKER), SetStringTip(STR_SPRITE_ALIGNER_PICKER_BUTTON, STR_SPRITE_ALIGNER_PICKER_TOOLTIP), SetFill(1, 0),
1187 EndContainer(),
1189 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SA_ZOOM + ZOOM_LVL_IN_4X), SetStringTip(STR_CONFIG_SETTING_ZOOM_LVL_MIN), SetFill(1, 0),
1190 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SA_ZOOM + ZOOM_LVL_IN_2X), SetStringTip(STR_CONFIG_SETTING_ZOOM_LVL_IN_2X), SetFill(1, 0),
1191 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SA_ZOOM + ZOOM_LVL_NORMAL), SetStringTip(STR_CONFIG_SETTING_ZOOM_LVL_NORMAL), SetFill(1, 0),
1192 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SA_ZOOM + ZOOM_LVL_OUT_2X), SetStringTip(STR_CONFIG_SETTING_ZOOM_LVL_OUT_2X), SetFill(1, 0),
1193 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SA_ZOOM + ZOOM_LVL_OUT_4X), SetStringTip(STR_CONFIG_SETTING_ZOOM_LVL_OUT_4X), SetFill(1, 0),
1194 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SA_ZOOM + ZOOM_LVL_OUT_8X), SetStringTip(STR_CONFIG_SETTING_ZOOM_LVL_OUT_8X), SetFill(1, 0),
1195 EndContainer(),
1196 EndContainer(),
1197 EndContainer(),
1199 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1200 NWidget(WWT_RESIZEBOX, COLOUR_GREY), SetResizeWidgetTypeTip(RWV_HIDE_BEVEL, STR_TOOLTIP_RESIZE),
1201 EndContainer(),
1202 EndContainer(),
1203};
1204
1205static WindowDesc _sprite_aligner_desc(
1206 WDP_AUTO, "sprite_aligner", 400, 300,
1208 {},
1209 _nested_sprite_aligner_widgets
1210);
1211
1216{
1217 AllocateWindowDescFront<SpriteAlignerWindow>(_sprite_aligner_desc, 0);
1218}
Class for backupping variables and making sure they are restored later.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
bool IsValidCargoType(CargoType cargo)
Test whether cargo type is not INVALID_CARGO.
Definition cargo_type.h:106
std::string label
Label of badge.
StringID name
Short name.
Helper class to wrap some functionality/queries in.
virtual std::span< const BadgeID > GetBadges(uint index) const =0
Get the list of badges of this item.
virtual uint Resolve(uint index, uint var, uint param, bool &avail) const =0
Resolve (action2) variable for a given index.
virtual const void * GetSpec(uint index) const =0
Get (NewGRF) specs given an index.
virtual bool IsInspectable(uint index) const =0
Is the item with the given index inspectable?
virtual uint32_t GetGRFID(uint index) const =0
Get the GRFID of the file that includes this item.
virtual const void * GetInstance(uint index) const =0
Get the instance given an index.
virtual const std::span< int32_t > GetPSA(uint index, uint32_t grfid) const
Gets the span containing the persistent storage.
virtual ~NIHelper()=default
Silence a warning.
virtual std::string GetName(uint index) const =0
Get the name of this item.
virtual bool PSAWithParameter() const
Used to decide if the PSA needs a parameter or not.
virtual uint GetParent(uint index) const =0
Get the parent "window_number" of a given instance.
Baseclass for nested widgets.
const std::string & GetSimplifiedFilename() const
Get the simplified filename of the opened file.
Scrollbar data structure.
size_type GetCapacity() const
Gets the number of visible elements of the scrollbar.
void SetCount(size_t num)
Sets the number of elements in the list.
auto GetScrolledItemFromWidget(Tcontainer &container, int clickpos, const Window *const w, WidgetID widget, int padding=0, int line_height=-1) const
Return an iterator pointing to the element of a scrolled widget that a user clicked in.
size_type GetScrolledRowFromWidget(int clickpos, const Window *const w, WidgetID widget, int padding=0, int line_height=-1) const
Compute the row of a scrolled widget that a user clicked in.
Definition widget.cpp:2447
void SetCapacityFromWidget(Window *w, WidgetID widget, int padding=0)
Set capacity of visible elements from the size and resize properties of a widget.
Definition widget.cpp:2521
auto GetVisibleRangeIterators(Tcontainer &container) const
Get a pair of iterators for the range of visible elements in a container.
size_type GetPosition() const
Gets the position of the first visible element in the list.
RandomAccessFile with some extra information specific for sprite files.
RectPadding frametext
Padding inside frame with text.
Definition window_gui.h:41
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:29
static const WidgetDimensions unscaled
Unscaled widget dimensions.
Definition window_gui.h:94
A type is considered 'convertible through base()' when it has a 'base()' function that returns someth...
VehicleCellSize GetVehicleImageCellSize(VehicleType type, EngineImageType image_type)
Get the GUI cell size for a vehicle image.
Base class for engines.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:77
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Geometry functions.
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition gfx.cpp:852
void DrawRectOutline(const Rect &r, int colour, int width, int dash)
Draw the outline of a Rect.
Definition gfx.cpp:457
int DrawString(int left, int right, int top, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition gfx.cpp:658
bool _ctrl_pressed
Is Ctrl pressed?
Definition gfx.cpp:38
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
Definition gfx.cpp:989
ZoomLevel _gui_zoom
GUI Zoom level.
Definition gfx.cpp:61
bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
Set up a clipping area for only drawing into a certain area.
Definition gfx.cpp:1519
int CenterBounds(int min, int max, int size)
Determine where to draw a centred object inside a widget.
Definition gfx_func.h:166
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
@ Normal
The most basic (normal) sprite.
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:245
@ SA_RIGHT
Right align the text (must be a single bit).
Definition gfx_type.h:379
@ SA_FORCE
Force the alignment, i.e. don't swap for RTL languages.
Definition gfx_type.h:389
@ TC_NO_SHADE
Do not add shading to this text colour.
Definition gfx_type.h:320
constexpr NWidgetPart SetMatrixDataTip(uint32_t cols, uint32_t rows, StringID tip={})
Widget part function for setting the data and tooltip of WWT_MATRIX widgets.
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
constexpr NWidgetPart SetSpriteTip(SpriteID sprite, StringID tip={})
Widget part function for setting the sprite and tooltip.
constexpr NWidgetPart SetPIP(uint8_t pre, uint8_t inter, uint8_t post)
Widget part function for setting a pre/inter/post spaces.
constexpr NWidgetPart SetScrollbar(WidgetID index)
Attach a scrollbar to a widget.
constexpr NWidgetPart SetPadding(uint8_t top, uint8_t right, uint8_t bottom, uint8_t left)
Widget part function for setting additional space around a widget.
constexpr NWidgetPart SetStringTip(StringID string, StringID tip={})
Widget part function for setting the string and tooltip.
constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
Widget part function for setting the minimal size.
constexpr NWidgetPart SetResizeWidgetTypeTip(ResizeWidgetValues widget_type, StringID tip)
Widget part function for setting the resize widget type and tooltip.
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=-1)
Widget part function for starting a new 'real' widget.
constexpr NWidgetPart SetToolTip(StringID tip)
Widget part function for setting tooltip and clearing the widget data.
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
constexpr NWidgetPart SetArrowWidgetTypeTip(ArrowWidgetValues widget_type, StringID tip={})
Widget part function for setting the arrow widget type and tooltip.
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition window.cpp:945
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition gfx.cpp:1500
Base of all industries.
constexpr bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
void ShowQueryString(std::string_view str, StringID caption, uint maxsize, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
Show a query popup window with a textbox in it.
GrfSpecFeature
Definition newgrf.h:70
@ GSF_INVALID
An invalid spec feature.
Definition newgrf.h:98
@ GSF_FAKE_END
End of the fake features.
Definition newgrf.h:96
std::span< const Action5Type > GetAction5Types()
Get list of all action 5 types.
Information about NewGRF Action 5.
NewGRF handling of airports.
NewGRF handling of airport tiles.
Badge * GetBadge(BadgeID index)
Get a badge if it exists.
Functions related to NewGRF badges.
AirportTileCallbackMask
Callback masks for airport tiles.
StationCallbackMask
Callback masks for stations.
VehicleCallbackMask
Callback masks for vehicles, indicates which callbacks are used by a vehicle.
CargoCallbackMask
Callback masks for cargoes.
IndustryCallbackMask
Callback masks for Industries.
CanalCallbackMask
Callback masks for canals.
ObjectCallbackMask
Callback masks for objects.
RoadStopCallbackMask
Callback masks for road stops.
IndustryTileCallbackMask
Callback masks for industry tiles.
HouseCallbackMask
Callback masks for houses.
Functions/types related to NewGRF debugging.
Data 'tables' for NewGRF debugging.
static const NIFeature *const _nifeatures[]
Table with all NIFeatures.
static uint GetInspectWindowNumber(GrfSpecFeature feature, uint index)
Get the window number for the inspect window given a feature and index.
NIType
The type of a property to show.
@ NIT_INT
The property is a simple integer.
@ NIT_CARGO
The property is a cargo.
void ShowSpriteAlignerWindow()
Show the window for aligning sprites.
GrfSpecFeature GetGrfSpecFeature(TileIndex tile)
Get the GrfSpecFeature associated with the tile.
void InvalidateNewGRFInspectWindow(GrfSpecFeature feature, uint index)
Invalidate the inspect window for a given feature and index.
static const NIFeature * GetFeature(uint window_number)
Get the NIFeature related to the window number.
void ShowNewGRFInspectWindow(GrfSpecFeature feature, uint index, const uint32_t grfid)
Show the inspect window for a given feature and index.
bool IsNewGRFInspectable(GrfSpecFeature feature, uint index)
Can we inspect the data given a certain feature and index.
static const NIHelper & GetFeatureHelper(uint window_number)
Get the NIHelper related to the window number.
static uint GetFeatureIndex(uint window_number)
Get the feature index related to the window number.
void DeleteNewGRFInspectWindow(GrfSpecFeature feature, uint index)
Delete inspect window for a given feature and index.
static GrfSpecFeature GetFeatureNum(uint window_number)
Get the feature number related to the window number.
NewGrfDebugSpritePicker _newgrf_debug_sprite_picker
The sprite picker.
Types related to the newgrf debug widgets.
@ WID_NGRFI_VEH_CHAIN
Display for vehicle chain.
@ WID_NGRFI_PARENT
Inspect the parent.
@ WID_NGRFI_MAINPANEL
Panel widget containing the actual data.
@ WID_NGRFI_VEH_NEXT
Go to next vehicle in chain.
@ WID_NGRFI_CAPTION
The caption bar of course.
@ WID_NGRFI_VEH_PREV
Go to previous vehicle in chain.
@ WID_NGRFI_SCROLLBAR
Scrollbar.
@ WID_SA_NEXT
Skip to the next sprite.
@ WID_SA_CAPTION
Caption of the window.
@ WID_SA_SPRITE
The actual sprite.
@ WID_SA_PICKER
Sprite picker.
@ WID_SA_SCROLLBAR
Scrollbar for sprite list.
@ WID_SA_OFFSETS_ABS
The sprite offsets (absolute).
@ WID_SA_PREVIOUS
Skip to the previous sprite.
@ WID_SA_OFFSETS_REL
The sprite offsets (relative).
@ WID_SA_CROSSHAIR
Toggle crosshair.
@ WID_SA_GOTO
Go to a given sprite.
@ WID_SA_DOWN
Move the sprite down.
@ WID_SA_ZOOM
Zoom level buttons (from ZOOM_LVL_BEGIN to ZOOM_LVL_END).
@ WID_SA_UP
Move the sprite up.
@ WID_SA_RESET_REL
Reset relative sprite offset.
@ WID_SA_LIST
Queried sprite list.
@ WID_SA_RIGHT
Move the sprite to the right.
@ WID_SA_LEFT
Move the sprite to the left.
@ WID_SA_CENTRE
Toggle centre sprite.
Functions for NewGRF industries.
NewGRF handling of industry tiles.
Functions related to NewGRF objects.
NewGRF handling of rail types.
Action 2 handling.
Header file for NewGRF stations.
Functions to handle the town part of NewGRF towns.
Base for all objects.
static const uint8_t PC_WHITE
White palette colour.
static const uint8_t PC_LIGHT_BLUE
Light blue palette colour.
Class related to random access to files.
bool IsLevelCrossing(Tile t)
Return whether a tile is a level crossing.
Definition road_map.h:85
Road vehicle states.
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:58
SpriteType GetSpriteType(SpriteID sprite)
Get the sprite type of a given sprite.
SpriteID GetMaxSpriteID()
Get a reasonable (upper bound) estimate of the maximum SpriteID used in OpenTTD; there will be no spr...
std::span< const std::unique_ptr< SpriteFile > > GetCachedSpriteFiles()
Get the list of cached SpriteFiles.
SpriteFile * GetOriginFile(SpriteID sprite)
Get the SpriteFile of a given sprite.
uint32_t GetSpriteLocalID(SpriteID sprite)
Get the GRF-local sprite id of a given sprite.
Functions to cache sprites in memory.
static const SpriteID SPR_OPENTTD_BASE
Extra graphic spritenumbers.
Definition sprites.h:56
Base classes/functions for stations.
StationType GetStationType(Tile t)
Get the station type of this tile.
Definition station_map.h:44
Definition of base types and functions in a cross-platform compatible way.
static void StrMakeValid(T &dst, const char *str, const char *last, StringValidationSettings settings)
Copies the valid (UTF-8) characters from str up to last to the dst.
Definition string.cpp:125
Functions related to low-level strings.
@ CS_HEXADECIMAL
Only hexadecimal characters.
Definition string_type.h:30
@ CS_NUMERAL
Only numeric ones.
Definition string_type.h:26
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:426
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:56
uint64_t GetParamMaxDigits(uint count, FontSize size)
Get some number that is suitable for string size computations.
Definition strings.cpp:230
Functions related to OTTD's strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
@ TD_RTL
Text is written right-to-left by default.
Information about a single action 5 type.
Definition newgrf_act5.h:21
SpriteID sprite_base
Load the sprites starting from this sprite.
Definition newgrf_act5.h:23
Class to backup a specific variable and restore it upon destruction of this object to prevent stack v...
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:137
GUISettings gui
settings related to the GUI
Dimensions (a width and height) of a rectangle in 2D.
Data about how and where to blit pixels.
Definition gfx_type.h:158
ZoomLevel zoom_min
minimum zoom out level
ZoomLevel zoom_max
maximum zoom out level
Representation of the available callbacks with information on when they actually apply.
NIOffsetProc * offset_proc
Callback proc to get the actual variable address in memory.
std::string_view name
The human readable name of the callback.
uint16_t cb_id
The number of the callback.
std::variant< std::monostate, VehicleCallbackMask, StationCallbackMask, RoadStopCallbackMask, HouseCallbackMask, CanalCallbackMask, CargoCallbackMask, IndustryCallbackMask, IndustryTileCallbackMask, ObjectCallbackMask, AirportTileCallbackMask > cb_bit
The bit that needs to be set for this callback to be enabled.
uint8_t read_size
The number of bytes (i.e. byte, word, dword etc) to read.
Container for all information for a given feature.
std::span< const NICallback > callbacks
The callbacks associated with this feature.
std::span< const NIVariable > variables
The variables associated with this feature.
std::span< const NIProperty > properties
The properties associated with this feature.
std::unique_ptr< const NIHelper > helper
The class container all helper functions.
Representation of the data from a NewGRF property.
uint8_t read_size
Number of bytes (i.e. byte, word, dword etc)
std::string_view name
A (human readable) name for the property.
NIOffsetProc * offset_proc
Callback proc to get the actual variable address in memory.
uint8_t prop
The number of the property.
Representation on the NewGRF variables.
Partial widget specification to allow NWidgets to be written nested.
Window used for inspecting NewGRFs.
static bool HasVariableParameter(uint variable)
Check whether the given variable has a parameter.
static uint32_t var60params[GSF_FAKE_END][0x20]
The value for the variable 60 parameters.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
void DrawMainPanelWidget(const Rect &r) const
Helper function to draw the main panel widget.
void ValidateChainIndex()
Ensure that this->chain_index is in range.
void OnQueryTextFinished(std::optional< std::string > str) override
The query window opened from this window has closed.
void DrawString(const Rect &r, int offset, const std::string &string) const
Helper function to draw a string (line) in the window.
bool HasChainIndex() const
Check whether this feature has chain index, i.e.
uint32_t caller_grfid
GRFID of the caller of this window, 0 if it has no caller.
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
void DrawVehicleChainWidget(const Rect &r) const
Helper function to draw the vehicle chain widget.
uint GetFeatureIndex() const
Get the feature index.
void SetCallerGRFID(uint32_t grfid)
Set the GRFID of the item opening this window.
uint8_t current_edit_param
The currently edited parameter, to update the right one.
void OnResize() override
Called after the window got resized.
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
uint chain_index
For ground vehicles: Index in vehicle chain.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
Spritepicker of SpriteAligner.
NewGrfDebugSpritePickerMode mode
Current state.
std::vector< SpriteID > sprites
Sprites found.
Coordinates of a point in 2D.
static Titem * Get(auto index)
Returns Titem with given index.
Tindex index
Index of this pool item.
constexpr uint Vertical() const
Get total vertical padding of RectPadding.
Specification of a rectangle with absolute coordinates of all edges.
int Width() const
Get width of Rect.
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
Rect Translate(int x, int y) const
Copy and translate Rect by x,y pixels.
static T * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Window used for aligning sprites.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
std::pair< int16_t, int16_t > XyOffs
Pair for x and y offsets of the sprite before alignment. First value contains the x offset,...
const Action5Type * act5_type
Sprite Area of current selected sprite.
void OnResize() override
Called after the window got resized.
SpriteID current_sprite
The currently shown sprite.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
std::map< SpriteID, XyOffs > offs_start_map
Mapping of starting offsets for the sprites which have been aligned in the sprite aligner window.
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
void OnQueryTextFinished(std::optional< std::string > str) override
The query window opened from this window has closed.
Data structure describing a sprite.
Definition spritecache.h:17
int16_t x_offs
Number of pixels to shift the sprite to the right.
Definition spritecache.h:20
uint height
Vehicle cell height.
Definition vehicle_gui.h:84
Vehicle data structure.
Vehicle * Move(int n)
Get the vehicle at offset n of this vehicle chain.
Vehicle * First() const
Get the first vehicle of this vehicle chain.
Vehicle * Next() const
Get the next vehicle of this vehicle.
High level window description.
Definition window_gui.h:168
Number to differentiate different windows of the same class.
Data structure for an opened window.
Definition window_gui.h:274
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition window.cpp:1738
void InvalidateData(int data=0, bool gui_scope=true)
Mark this window's data as invalid (in need of re-computing)
Definition window.cpp:3166
void RaiseWidget(WidgetID widget_index)
Marks a widget as raised.
Definition window_gui.h:470
virtual std::string GetWidgetString(WidgetID widget, StringID stringid) const
Get the raw string for a widget.
Definition window.cpp:504
virtual void ShowNewGRFInspectWindow() const
Show the NewGRF inspection window.
Definition window_gui.h:858
ResizeInfo resize
Resize information.
Definition window_gui.h:315
void SetWidgetsDisabledState(bool disab_stat, Args... widgets)
Sets the enabled/disabled status of a list of widgets.
Definition window_gui.h:516
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition window.cpp:1728
void SetWidgetsLoweredState(bool lowered_stat, Args... widgets)
Sets the lowered/raised status of a list of widgets.
Definition window_gui.h:527
void SetWidgetLoweredState(WidgetID widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition window_gui.h:442
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition window_gui.h:973
void LowerWidget(WidgetID widget_index)
Marks a widget as lowered.
Definition window_gui.h:461
const Scrollbar * GetScrollbar(WidgetID widnum) const
Return the Scrollbar to a widget index.
Definition window.cpp:313
void SetWidgetDisabledState(WidgetID widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition window_gui.h:382
int height
Height of the window (number of pixels down in y direction)
Definition window_gui.h:313
int width
width of the window (number of pixels to the right in x direction)
Definition window_gui.h:312
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:303
Stuff related to the text buffer GUI.
static debug_inline TileType GetTileType(Tile tile)
Get the tiletype of a given tile.
Definition tile_map.h:96
@ MP_ROAD
A tile with road (or tram tracks)
Definition tile_type.h:50
@ MP_STATION
A tile of a station.
Definition tile_type.h:53
@ MP_HOUSE
A house by a town.
Definition tile_type.h:51
@ MP_RAILWAY
A railway.
Definition tile_type.h:49
@ MP_INDUSTRY
Part of an industry.
Definition tile_type.h:56
@ MP_OBJECT
Contains objects such as transmitters and owned land.
Definition tile_type.h:58
Base of the town class.
Base for the train class.
Base class for all vehicles.
void DrawVehicleImage(const Vehicle *v, const Rect &r, VehicleID selection, EngineImageType image_type, int skip)
Draws an image of a vehicle chain.
Functions related to the vehicle's GUIs.
@ EIT_IN_DEPOT
Vehicle drawn in depot.
@ EIT_IN_DETAILS
Vehicle drawn in vehicle details, refit window, ...
VehicleType
Available vehicle types.
@ VEH_ROAD
Road vehicle type.
@ VEH_AIRCRAFT
Aircraft vehicle type.
@ VEH_SHIP
Ship vehicle type.
@ VEH_TRAIN
Train vehicle type.
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition widget.cpp:296
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition widget.cpp:48
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
@ WWT_PUSHIMGBTN
Normal push-button (no toggle button) with image caption.
@ WWT_PUSHARROWBTN
Normal push-button (no toggle button) with arrow caption.
@ WWT_LABEL
Centered label.
Definition widget_type.h:47
@ NWID_SPACER
Invisible widget that takes some space.
Definition widget_type.h:69
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:65
@ WWT_TEXTBTN
(Toggle) Button with text
Definition widget_type.h:45
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:40
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition widget_type.h:56
@ WWT_MATRIX
Grid of rows and columns.
Definition widget_type.h:49
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition widget_type.h:54
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition widget_type.h:51
@ NWID_VSCROLLBAR
Vertical scrollbar.
Definition widget_type.h:75
@ NWID_VERTICAL
Vertical container.
Definition widget_type.h:67
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition widget_type.h:59
@ WWT_TEXTBTN_2
(Toggle) Button with diff text when clicked
Definition widget_type.h:46
@ WWT_EMPTY
Empty widget, place holder to reserve space in widget tree.
Definition widget_type.h:38
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window)
Definition widget_type.h:58
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
Definition widget_type.h:55
@ NWID_HORIZONTAL_LTR
Horizontal container that doesn't change the order of the widgets for RTL languages.
Definition widget_type.h:66
@ EqualSize
Containers should keep all their (resizing) children equally large.
@ AWV_DECREASE
Arrow to the left or in case of RTL to the right.
Definition widget_type.h:21
@ AWV_INCREASE
Arrow to the right or in case of RTL to the left.
Definition widget_type.h:22
@ RWV_HIDE_BEVEL
Bevel of resize box is hidden.
Definition widget_type.h:30
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
Definition window.cpp:1145
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition window.cpp:3226
Window functions not directly related to making/drawing windows.
Functions, definitions and such used only by the GUI.
@ BorderOnly
Draw border only, no background.
@ WDP_AUTO
Find a place automatically.
Definition window_gui.h:145
int WidgetID
Widget ID.
Definition window_type.h:20
@ WC_LAND_INFO
Land info window; Window numbers:
@ WC_SPRITE_ALIGNER
Sprite aligner (debug); Window numbers:
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition window_type.h:47
@ WC_NEWGRF_INSPECT
NewGRF inspect (debug); Window numbers:
Functions related to zooming.
int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_MIN) When shifting right,...
Definition zoom_func.h:22
int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_MIN) When shifting right,...
Definition zoom_func.h:34
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:16
@ ZOOM_LVL_NORMAL
The normal zoom level.
Definition zoom_type.h:21
@ ZOOM_LVL_BEGIN
Begin for iteration.
Definition zoom_type.h:18
@ ZOOM_LVL_OUT_4X
Zoomed 4 times out.
Definition zoom_type.h:23
@ ZOOM_LVL_OUT_2X
Zoomed 2 times out.
Definition zoom_type.h:22
@ ZOOM_LVL_OUT_8X
Zoomed 8 times out.
Definition zoom_type.h:24
@ ZOOM_LVL_IN_2X
Zoomed 2 times in.
Definition zoom_type.h:20
@ ZOOM_LVL_END
End for iteration.
Definition zoom_type.h:25
@ ZOOM_LVL_IN_4X
Zoomed 4 times in.
Definition zoom_type.h:19