OpenTTD Source 20250205-master-gfd85ab1e2c
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_debug.h"
36#include "newgrf_object.h"
37#include "newgrf_spritegroup.h"
38#include "newgrf_station.h"
39#include "newgrf_town.h"
40#include "newgrf_railtype.h"
41#include "newgrf_industries.h"
43
45
46#include "table/strings.h"
47
48#include "safeguards.h"
49
51NewGrfDebugSpritePicker _newgrf_debug_sprite_picker = { SPM_NONE, nullptr, std::vector<SpriteID>() };
52
58static inline uint GetFeatureIndex(uint window_number)
59{
60 return GB(window_number, 0, 24);
61}
62
70static inline uint GetInspectWindowNumber(GrfSpecFeature feature, uint index)
71{
72 assert((index >> 24) == 0);
73 return (feature << 24) | index;
74}
75
80enum NIType : uint8_t {
83};
84
85typedef const void *NIOffsetProc(const void *b);
86
88struct NIProperty {
89 std::string_view name;
90 NIOffsetProc *offset_proc;
91 uint8_t read_size;
92 uint8_t prop;
93 uint8_t type;
94};
95
96
119
122 std::string_view name;
123 uint8_t var;
124};
125
127class NIHelper {
128public:
130 virtual ~NIHelper() = default;
131
137 virtual bool IsInspectable(uint index) const = 0;
138
144 virtual uint GetParent(uint index) const = 0;
145
151 virtual const void *GetInstance(uint index) const = 0;
152
158 virtual const void *GetSpec(uint index) const = 0;
159
164 virtual void SetStringParameters(uint index) const = 0;
165
171 virtual uint32_t GetGRFID(uint index) const = 0;
172
181 virtual uint Resolve(uint index, uint var, uint param, bool &avail) const = 0;
182
187 virtual bool PSAWithParameter() const
188 {
189 return false;
190 }
191
198 virtual const std::span<int32_t> GetPSA([[maybe_unused]] uint index, [[maybe_unused]] uint32_t grfid) const
199 {
200 return {};
201 }
202
203protected:
209 void SetSimpleStringParameters(StringID string, uint32_t index) const
210 {
211 SetDParam(0, string);
212 SetDParam(1, index);
213 }
214
215
222 void SetObjectAtStringParameters(StringID string, uint32_t index, TileIndex tile) const
223 {
224 SetDParam(0, STR_NEWGRF_INSPECT_CAPTION_OBJECT_AT);
225 SetDParam(1, string);
226 SetDParam(2, index);
227 SetDParam(3, tile);
228 }
229};
230
231
233struct NIFeature {
234 std::span<const NIProperty> properties;
235 std::span<const NICallback> callbacks;
236 std::span<const NIVariable> variables;
237 std::unique_ptr<const NIHelper> helper;
238};
239
240/* Load all the NewGRF debug data; externalised as it is just a huge bunch of tables. */
242
248static inline GrfSpecFeature GetFeatureNum(uint window_number)
249{
250 return (GrfSpecFeature)GB(window_number, 24, 8);
251}
252
258static inline const NIFeature *GetFeature(uint window_number)
259{
260 GrfSpecFeature idx = GetFeatureNum(window_number);
261 return idx < GSF_FAKE_END ? _nifeatures[idx] : nullptr;
262}
263
270static inline const NIHelper &GetFeatureHelper(uint window_number)
271{
272 return *GetFeature(window_number)->helper;
273}
274
278 static uint32_t var60params[GSF_FAKE_END][0x20];
279
282
285
288
289 Scrollbar *vscroll;
290
296 static bool HasVariableParameter(uint variable)
297 {
298 return IsInsideBS(variable, 0x60, 0x20);
299 }
300
306 {
307 this->caller_grfid = grfid;
308 this->SetDirty();
309 }
310
314 bool HasChainIndex() const
315 {
317 return f == GSF_TRAINS || f == GSF_ROADVEHICLES;
318 }
319
324 uint GetFeatureIndex() const
325 {
326 uint index = ::GetFeatureIndex(this->window_number);
327 if (this->chain_index > 0) {
328 assert(this->HasChainIndex());
329 const Vehicle *v = Vehicle::Get(index);
330 v = v->Move(this->chain_index);
331 if (v != nullptr) index = v->index;
332 }
333 return index;
334 }
335
340 {
341 if (this->chain_index == 0) return;
342
343 assert(this->HasChainIndex());
344
346 v = v->Move(this->chain_index);
347 if (v == nullptr) this->chain_index = 0;
348 }
349
351 {
352 this->CreateNestedTree();
353 this->vscroll = this->GetScrollbar(WID_NGRFI_SCROLLBAR);
354 this->FinishInitNested(wno);
355
356 this->vscroll->SetCount(0);
358
359 this->OnInvalidateData(0, true);
360 }
361
362 void SetStringParameters(WidgetID widget) const override
363 {
364 if (widget != WID_NGRFI_CAPTION) return;
365
367 }
368
370 {
371 switch (widget) {
372 case WID_NGRFI_VEH_CHAIN: {
373 assert(this->HasChainIndex());
375 size.height = std::max(size.height, GetVehicleImageCellSize((VehicleType)(VEH_TRAIN + (f - GSF_TRAINS)), EIT_IN_DEPOT).height + 2 + WidgetDimensions::scaled.bevel.Vertical());
376 break;
377 }
378
380 resize.height = std::max(11, GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal);
381 resize.width = 1;
382
383 size.height = 5 * resize.height + WidgetDimensions::scaled.frametext.Vertical();
384 break;
385 }
386 }
387
394 void DrawString(const Rect &r, int offset, const std::string &string) const
395 {
396 offset -= this->vscroll->GetPosition();
397 if (offset < 0 || offset >= this->vscroll->GetCapacity()) return;
398
399 ::DrawString(r.Shrink(WidgetDimensions::scaled.frametext).Shrink(0, offset * this->resize.step_height, 0, 0), string, TC_BLACK);
400 }
401
406 void DrawVehicleChainWidget(const Rect &r) const
407 {
408 const Vehicle *v = Vehicle::Get(this->GetFeatureIndex());
409 int total_width = 0;
410 int sel_start = 0;
411 int sel_end = 0;
412 for (const Vehicle *u = v->First(); u != nullptr; u = u->Next()) {
413 if (u == v) sel_start = total_width;
414 switch (u->type) {
415 case VEH_TRAIN: total_width += Train::From(u)->GetDisplayImageWidth(); break;
416 case VEH_ROAD: total_width += RoadVehicle::From(u)->GetDisplayImageWidth(); break;
417 default: NOT_REACHED();
418 }
419 if (u == v) sel_end = total_width;
420 }
421
423 int width = br.Width();
424 int skip = 0;
425 if (total_width > width) {
426 int sel_center = (sel_start + sel_end) / 2;
427 if (sel_center > width / 2) skip = std::min(total_width - width, sel_center - width / 2);
428 }
429
431 int h = GetVehicleImageCellSize((VehicleType)(VEH_TRAIN + (f - GSF_TRAINS)), EIT_IN_DEPOT).height;
432 int y = CenterBounds(br.top, br.bottom, h);
434
435 /* Highlight the articulated part (this is different to the whole-vehicle highlighting of DrawVehicleImage */
436 if (_current_text_dir == TD_RTL) {
437 DrawFrameRect(r.right - sel_end + skip, y, r.right - sel_start + skip, y + h, COLOUR_WHITE, FrameFlag::BorderOnly);
438 } else {
439 DrawFrameRect(r.left + sel_start - skip, y, r.left + sel_end - skip, y + h, COLOUR_WHITE, FrameFlag::BorderOnly);
440 }
441 }
442
447 void DrawMainPanelWidget(const Rect &r) const
448 {
449 uint index = this->GetFeatureIndex();
450 const NIFeature *nif = GetFeature(this->window_number);
451 const NIHelper &nih = *nif->helper;
452 const void *base = nih.GetInstance(index);
453 const void *base_spec = nih.GetSpec(index);
454
455 uint i = 0;
456 if (!nif->variables.empty()) {
457 this->DrawString(r, i++, "Variables:");
458 for (const NIVariable &niv : nif->variables) {
459 bool avail = true;
461 uint value = nih.Resolve(index, niv.var, param, avail);
462
463 if (!avail) continue;
464
465 if (HasVariableParameter(niv.var)) {
466 this->DrawString(r, i++, fmt::format(" {:02x}[{:02x}]: {:08x} ({})", niv.var, param, value, niv.name));
467 } else {
468 this->DrawString(r, i++, fmt::format(" {:02x}: {:08x} ({})", niv.var, value, niv.name));
469 }
470 }
471 }
472
473 auto psa = nih.GetPSA(index, this->caller_grfid);
474 if (!psa.empty()) {
475 if (nih.PSAWithParameter()) {
476 this->DrawString(r, i++, fmt::format("Persistent storage [{:08X}]:", std::byteswap(this->caller_grfid)));
477 } else {
478 this->DrawString(r, i++, "Persistent storage:");
479 }
480 assert(psa.size() % 4 == 0);
481 for (size_t j = 0; j < psa.size(); j += 4) {
482 this->DrawString(r, i++, fmt::format(" {}: {} {} {} {}", j, psa[j], psa[j + 1], psa[j + 2], psa[j + 3]));
483 }
484 }
485
486 if (!nif->properties.empty()) {
487 this->DrawString(r, i++, "Properties:");
488 for (const NIProperty &nip : nif->properties) {
489 const void *ptr = nip.offset_proc(base);
490 uint value;
491 switch (nip.read_size) {
492 case 1: value = *(const uint8_t *)ptr; break;
493 case 2: value = *(const uint16_t *)ptr; break;
494 case 4: value = *(const uint32_t *)ptr; break;
495 default: NOT_REACHED();
496 }
497
498 StringID string;
499 SetDParam(0, value);
500 switch (nip.type) {
501 case NIT_INT:
502 string = STR_JUST_INT;
503 break;
504
505 case NIT_CARGO:
506 string = IsValidCargoType(value) ? CargoSpec::Get(value)->name : STR_QUANTITY_N_A;
507 break;
508
509 default:
510 NOT_REACHED();
511 }
512
513 this->DrawString(r, i++, fmt::format(" {:02x}: {} ({})", nip.prop, GetString(string), nip.name));
514 }
515 }
516
517 if (!nif->callbacks.empty()) {
518 this->DrawString(r, i++, "Callbacks:");
519 for (const NICallback &nic : nif->callbacks) {
520 if (!std::holds_alternative<std::monostate>(nic.cb_bit)) {
521 const void *ptr = nic.offset_proc(base_spec);
522 uint value;
523 switch (nic.read_size) {
524 case 1: value = *(const uint8_t *)ptr; break;
525 case 2: value = *(const uint16_t *)ptr; break;
526 case 4: value = *(const uint32_t *)ptr; break;
527 default: NOT_REACHED();
528 }
529
530 struct visitor {
531 uint value;
532
533 bool operator()(const std::monostate &) { return false; }
534 bool operator()(const VehicleCallbackMask &bit) { return static_cast<VehicleCallbackMasks>(this->value).Test(bit); }
535 bool operator()(const StationCallbackMask &bit) { return static_cast<StationCallbackMasks>(this->value).Test(bit); }
536 bool operator()(const RoadStopCallbackMask &bit) { return static_cast<RoadStopCallbackMasks>(this->value).Test(bit); }
537 bool operator()(const HouseCallbackMask &bit) { return static_cast<HouseCallbackMasks>(this->value).Test(bit); }
538 bool operator()(const CanalCallbackMask &bit) { return static_cast<CanalCallbackMasks>(this->value).Test(bit); }
539 bool operator()(const CargoCallbackMask &bit) { return static_cast<CargoCallbackMasks>(this->value).Test(bit); }
540 bool operator()(const IndustryCallbackMask &bit) { return static_cast<IndustryCallbackMasks>(this->value).Test(bit); }
541 bool operator()(const IndustryTileCallbackMask &bit) { return static_cast<IndustryTileCallbackMasks>(this->value).Test(bit); }
542 bool operator()(const ObjectCallbackMask &bit) { return static_cast<ObjectCallbackMasks>(this->value).Test(bit); }
543 bool operator()(const AirportTileCallbackMask &bit) { return static_cast<AirportTileCallbackMasks>(this->value).Test(bit); }
544 };
545
546 if (!std::visit(visitor{value}, nic.cb_bit)) continue;
547 this->DrawString(r, i++, fmt::format(" {:03x}: {}", nic.cb_id, nic.name));
548 } else {
549 this->DrawString(r, i++, fmt::format(" {:03x}: {} (unmasked)", nic.cb_id, nic.name));
550 }
551 }
552 }
553
554 /* Not nice and certainly a hack, but it beats duplicating
555 * this whole function just to count the actual number of
556 * elements. Especially because they need to be redrawn. */
557 const_cast<NewGRFInspectWindow*>(this)->vscroll->SetCount(i);
558 }
559
560 void DrawWidget(const Rect &r, WidgetID widget) const override
561 {
562 switch (widget) {
564 this->DrawVehicleChainWidget(r);
565 break;
566
568 this->DrawMainPanelWidget(r);
569 break;
570 }
571 }
572
573 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
574 {
575 switch (widget) {
576 case WID_NGRFI_PARENT: {
578 uint index = nih.GetParent(this->GetFeatureIndex());
579 ::ShowNewGRFInspectWindow(GetFeatureNum(index), ::GetFeatureIndex(index), nih.GetGRFID(this->GetFeatureIndex()));
580 break;
581 }
582
584 if (this->chain_index > 0) {
585 this->chain_index--;
586 this->InvalidateData();
587 }
588 break;
589
591 if (this->HasChainIndex()) {
592 uint index = this->GetFeatureIndex();
593 Vehicle *v = Vehicle::Get(index);
594 if (v != nullptr && v->Next() != nullptr) {
595 this->chain_index++;
596 this->InvalidateData();
597 }
598 }
599 break;
600
601 case WID_NGRFI_MAINPANEL: {
602 /* Does this feature have variables? */
603 const NIFeature *nif = GetFeature(this->window_number);
604 if (nif->variables.empty()) return;
605
606 /* Get the line, make sure it's within the boundaries. */
608 if (line == INT32_MAX) return;
609
610 /* Find the variable related to the line */
611 for (const NIVariable &niv : nif->variables) {
612 if (--line != 0) continue; // 0 because of the "Variables:" line
613
614 if (!HasVariableParameter(niv.var)) break;
615
616 this->current_edit_param = niv.var;
618 }
619 }
620 }
621 }
622
623 void OnQueryTextFinished(std::optional<std::string> str) override
624 {
625 if (!str.has_value() || str->empty()) return;
626
627 NewGRFInspectWindow::var60params[GetFeatureNum(this->window_number)][this->current_edit_param - 0x60] = std::strtol(str->c_str(), nullptr, 16);
628 this->SetDirty();
629 }
630
631 void OnResize() override
632 {
633 this->vscroll->SetCapacityFromWidget(this, WID_NGRFI_MAINPANEL, WidgetDimensions::scaled.frametext.Vertical());
634 }
635
641 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
642 {
643 if (!gui_scope) return;
644 if (this->HasChainIndex()) {
645 this->ValidateChainIndex();
646 this->SetWidgetDisabledState(WID_NGRFI_VEH_PREV, this->chain_index == 0);
648 this->SetWidgetDisabledState(WID_NGRFI_VEH_NEXT, v == nullptr || v->Next() == nullptr);
649 }
650 }
651};
652
653/* static */ uint32_t NewGRFInspectWindow::var60params[GSF_FAKE_END][0x20] = { {0} }; // Use spec to have 0s in whole array
654
655static constexpr NWidgetPart _nested_newgrf_inspect_chain_widgets[] = {
657 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
658 NWidget(WWT_CAPTION, COLOUR_GREY, WID_NGRFI_CAPTION), SetStringTip(STR_NEWGRF_INSPECT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
659 NWidget(WWT_SHADEBOX, COLOUR_GREY),
660 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
661 NWidget(WWT_STICKYBOX, COLOUR_GREY),
662 EndContainer(),
663 NWidget(WWT_PANEL, COLOUR_GREY),
667 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_NGRFI_VEH_CHAIN), SetFill(1, 0), SetResize(1, 0),
668 EndContainer(),
669 EndContainer(),
674 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
675 EndContainer(),
676 EndContainer(),
677};
678
679static constexpr NWidgetPart _nested_newgrf_inspect_widgets[] = {
681 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
682 NWidget(WWT_CAPTION, COLOUR_GREY, WID_NGRFI_CAPTION), SetStringTip(STR_NEWGRF_INSPECT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
683 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_NGRFI_PARENT), SetStringTip(STR_NEWGRF_INSPECT_PARENT_BUTTON, STR_NEWGRF_INSPECT_PARENT_TOOLTIP),
684 NWidget(WWT_SHADEBOX, COLOUR_GREY),
685 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
686 NWidget(WWT_STICKYBOX, COLOUR_GREY),
687 EndContainer(),
692 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
693 EndContainer(),
694 EndContainer(),
695};
696
697static WindowDesc _newgrf_inspect_chain_desc(
698 WDP_AUTO, "newgrf_inspect_chain", 400, 300,
700 {},
701 _nested_newgrf_inspect_chain_widgets
702);
703
704static WindowDesc _newgrf_inspect_desc(
705 WDP_AUTO, "newgrf_inspect", 400, 300,
707 {},
708 _nested_newgrf_inspect_widgets
709);
710
720void ShowNewGRFInspectWindow(GrfSpecFeature feature, uint index, const uint32_t grfid)
721{
722 if (!IsNewGRFInspectable(feature, index)) return;
723
724 WindowNumber wno = GetInspectWindowNumber(feature, index);
725 WindowDesc &desc = (feature == GSF_TRAINS || feature == GSF_ROADVEHICLES) ? _newgrf_inspect_chain_desc : _newgrf_inspect_desc;
726 NewGRFInspectWindow *w = AllocateWindowDescFront<NewGRFInspectWindow, true>(desc, wno);
727 w->SetCallerGRFID(grfid);
728}
729
739{
740 if (feature == GSF_INVALID) return;
741
742 WindowNumber wno = GetInspectWindowNumber(feature, index);
744}
745
755{
756 if (feature == GSF_INVALID) return;
757
758 WindowNumber wno = GetInspectWindowNumber(feature, index);
760
761 /* Reinitialise the land information window to remove the "debug" sprite if needed.
762 * Note: Since we might be called from a command here, it is important to not execute
763 * the invalidation immediately. The landinfo window tests commands itself. */
765}
766
776bool IsNewGRFInspectable(GrfSpecFeature feature, uint index)
777{
778 const NIFeature *nif = GetFeature(GetInspectWindowNumber(feature, index));
779 if (nif == nullptr) return false;
780 return nif->helper->IsInspectable(index);
781}
782
789{
790 switch (GetTileType(tile)) {
791 default: return GSF_INVALID;
792 case MP_RAILWAY: return GSF_RAILTYPES;
793 case MP_ROAD: return IsLevelCrossing(tile) ? GSF_RAILTYPES : GSF_ROADTYPES;
794 case MP_HOUSE: return GSF_HOUSES;
795 case MP_INDUSTRY: return GSF_INDUSTRYTILES;
796 case MP_OBJECT: return GSF_OBJECTS;
797
798 case MP_STATION:
799 switch (GetStationType(tile)) {
800 case StationType::Rail: return GSF_STATIONS;
801 case StationType::Airport: return GSF_AIRPORTTILES;
802 case StationType::Bus: return GSF_ROADSTOPS;
803 case StationType::Truck: return GSF_ROADSTOPS;
804 default: return GSF_INVALID;
805 }
806 }
807}
808
815{
816 switch (type) {
817 case VEH_TRAIN: return GSF_TRAINS;
818 case VEH_ROAD: return GSF_ROADVEHICLES;
819 case VEH_SHIP: return GSF_SHIPS;
820 case VEH_AIRCRAFT: return GSF_AIRCRAFT;
821 default: return GSF_INVALID;
822 }
823}
824
825
826/**** Sprite Aligner ****/
827
830 typedef std::pair<int16_t, int16_t> XyOffs;
831
833 Scrollbar *vscroll;
834 std::map<SpriteID, XyOffs> offs_start_map;
835
836 static inline ZoomLevel zoom = ZOOM_LVL_END;
837 static bool centre;
838 static bool crosshair;
839 const Action5Type *act5_type = nullptr;
840
842 {
843 /* On first opening, set initial zoom to current zoom level. */
844 if (SpriteAlignerWindow::zoom == ZOOM_LVL_END) SpriteAlignerWindow::zoom = _gui_zoom;
845 SpriteAlignerWindow::zoom = Clamp(SpriteAlignerWindow::zoom, _settings_client.gui.zoom_min, _settings_client.gui.zoom_max);
846
847 /* Oh yes, we assume there is at least one normal sprite! */
848 while (GetSpriteType(this->current_sprite) != SpriteType::Normal) this->current_sprite++;
849 this->SelectAction5Type();
850
851 this->CreateNestedTree();
852 this->vscroll = this->GetScrollbar(WID_SA_SCROLLBAR);
853 this->vscroll->SetCount(_newgrf_debug_sprite_picker.sprites.size());
854 this->FinishInitNested(wno);
855
856 this->SetWidgetLoweredState(WID_SA_CENTRE, SpriteAlignerWindow::centre);
857 this->SetWidgetLoweredState(WID_SA_CROSSHAIR, SpriteAlignerWindow::crosshair);
858
859 this->InvalidateData(0, true);
860 }
861
862 void SetStringParameters(WidgetID widget) const override
863 {
864 const Sprite *spr = GetSprite(this->current_sprite, SpriteType::Normal);
865 switch (widget) {
866 case WID_SA_CAPTION:
867 if (this->act5_type != nullptr) {
869 SetDParam(1, this->act5_type - GetAction5Types().data());
870 SetDParam(2, this->current_sprite - this->act5_type->sprite_base);
871 SetDParamStr(3, GetOriginFile(this->current_sprite)->GetSimplifiedFilename());
872 SetDParam(4, GetSpriteLocalID(this->current_sprite));
873 } else if (this->current_sprite < SPR_OPENTTD_BASE) {
875 SetDParam(1, this->current_sprite);
876 SetDParamStr(2, GetOriginFile(this->current_sprite)->GetSimplifiedFilename());
877 SetDParam(3, GetSpriteLocalID(this->current_sprite));
878 } else {
880 SetDParamStr(1, GetOriginFile(this->current_sprite)->GetSimplifiedFilename());
881 SetDParam(2, GetSpriteLocalID(this->current_sprite));
882 }
883 break;
884
886 SetDParam(0, UnScaleByZoom(spr->x_offs, SpriteAlignerWindow::zoom));
887 SetDParam(1, UnScaleByZoom(spr->y_offs, SpriteAlignerWindow::zoom));
888 break;
889
890 case WID_SA_OFFSETS_REL: {
891 /* Relative offset is new absolute offset - starting absolute offset.
892 * Show 0, 0 as the relative offsets if entry is not in the map (meaning they have not been changed yet).
893 */
894 const auto key_offs_pair = this->offs_start_map.find(this->current_sprite);
895 if (key_offs_pair != this->offs_start_map.end()) {
896 SetDParam(0, UnScaleByZoom(spr->x_offs - key_offs_pair->second.first, SpriteAlignerWindow::zoom));
897 SetDParam(1, UnScaleByZoom(spr->y_offs - key_offs_pair->second.second, SpriteAlignerWindow::zoom));
898 } else {
899 SetDParam(0, 0);
900 SetDParam(1, 0);
901 }
902 break;
903 }
904
905 default:
906 break;
907 }
908 }
909
911 {
912 switch (widget) {
913 case WID_SA_SPRITE:
914 size.height = ScaleGUITrad(200);
915 break;
916
917 case WID_SA_LIST: {
918 Dimension d = {};
919 for (const auto &spritefile : GetCachedSpriteFiles()) {
920 SetDParamStr(0, spritefile->GetSimplifiedFilename());
921 SetDParamMaxDigits(1, 6);
923 }
924 size.width = d.width + padding.width;
925 resize.height = GetCharacterHeight(FS_NORMAL) + padding.height;
926 resize.width = 1;
927 fill.height = resize.height;
928 break;
929 }
930
931 default:
932 break;
933 }
934 }
935
936 void DrawWidget(const Rect &r, WidgetID widget) const override
937 {
938 switch (widget) {
939 case WID_SA_SPRITE: {
940 /* Center the sprite ourselves */
941 const Sprite *spr = GetSprite(this->current_sprite, SpriteType::Normal);
943 int x;
944 int y;
945 if (SpriteAlignerWindow::centre) {
946 x = -UnScaleByZoom(spr->x_offs, SpriteAlignerWindow::zoom) + (ir.Width() - UnScaleByZoom(spr->width, SpriteAlignerWindow::zoom)) / 2;
947 y = -UnScaleByZoom(spr->y_offs, SpriteAlignerWindow::zoom) + (ir.Height() - UnScaleByZoom(spr->height, SpriteAlignerWindow::zoom)) / 2;
948 } else {
949 x = ir.Width() / 2;
950 y = ir.Height() / 2;
951 }
952
954 if (!FillDrawPixelInfo(&new_dpi, ir)) break;
956
957 DrawSprite(this->current_sprite, PAL_NONE, x, y, nullptr, SpriteAlignerWindow::zoom);
958
959 Rect outline = {0, 0, UnScaleByZoom(spr->width, SpriteAlignerWindow::zoom) - 1, UnScaleByZoom(spr->height, SpriteAlignerWindow::zoom) - 1};
960 outline = outline.Translate(x + UnScaleByZoom(spr->x_offs, SpriteAlignerWindow::zoom), y + UnScaleByZoom(spr->y_offs, SpriteAlignerWindow::zoom));
961 DrawRectOutline(outline.Expand(1), PC_LIGHT_BLUE, 1, 1);
962
963 if (SpriteAlignerWindow::crosshair) {
964 GfxDrawLine(x, 0, x, ir.Height() - 1, PC_WHITE, 1, 1);
965 GfxDrawLine(0, y, ir.Width() - 1, y, PC_WHITE, 1, 1);
966 }
967 break;
968 }
969
970 case WID_SA_LIST: {
971 /* Don't redraw sprite list while it is still being filled by picker. */
972 if (_newgrf_debug_sprite_picker.mode == SPM_REDRAW) break;
973
974 const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(widget);
975 int step_size = nwid->resize_y;
976
977 const std::vector<SpriteID> &list = _newgrf_debug_sprite_picker.sprites;
978
980 auto [first, last] = this->vscroll->GetVisibleRangeIterators(list);
981 for (auto it = first; it != last; ++it) {
982 const SpriteFile *file = GetOriginFile(*it);
983 if (file == nullptr) {
984 SetDParam(0, *it);
985 DrawString(ir, STR_JUST_COMMA, *it == this->current_sprite ? TC_WHITE : (TC_GREY | TC_NO_SHADE), SA_RIGHT | SA_FORCE);
986 } else {
989 DrawString(ir, STR_SPRITE_ALIGNER_SPRITE, *it == this->current_sprite ? TC_WHITE : TC_BLACK);
990 }
991 ir.top += step_size;
992 }
993 break;
994 }
995 }
996 }
997
998 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
999 {
1000 switch (widget) {
1001 case WID_SA_PREVIOUS:
1002 do {
1003 this->current_sprite = (this->current_sprite == 0 ? GetMaxSpriteID() : this->current_sprite) - 1;
1004 } while (GetSpriteType(this->current_sprite) != SpriteType::Normal);
1005 this->SelectAction5Type();
1006 this->SetDirty();
1007 break;
1008
1009 case WID_SA_GOTO:
1011 break;
1012
1013 case WID_SA_NEXT:
1014 do {
1015 this->current_sprite = (this->current_sprite + 1) % GetMaxSpriteID();
1016 } while (GetSpriteType(this->current_sprite) != SpriteType::Normal);
1017 this->SelectAction5Type();
1018 this->SetDirty();
1019 break;
1020
1021 case WID_SA_PICKER:
1023 _newgrf_debug_sprite_picker.mode = SPM_WAIT_CLICK;
1024 this->SetDirty();
1025 break;
1026
1027 case WID_SA_LIST: {
1028 auto it = this->vscroll->GetScrolledItemFromWidget(_newgrf_debug_sprite_picker.sprites, pt.y, this, widget);
1029 if (it != _newgrf_debug_sprite_picker.sprites.end()) {
1030 SpriteID spr = *it;
1031 if (GetSpriteType(spr) == SpriteType::Normal) this->current_sprite = spr;
1032 }
1033 this->SelectAction5Type();
1034 this->SetDirty();
1035 break;
1036 }
1037
1038 case WID_SA_UP:
1039 case WID_SA_DOWN:
1040 case WID_SA_LEFT:
1041 case WID_SA_RIGHT: {
1042 /*
1043 * Yes... this is a hack.
1044 *
1045 * No... I don't think it is useful to make this less of a hack.
1046 *
1047 * If you want to align sprites, you just need the number. Generally
1048 * the sprite caches are big enough to not remove the sprite from the
1049 * cache. If that's not the case, just let the NewGRF developer
1050 * increase the cache size instead of storing thousands of offsets
1051 * for the incredibly small chance that it's actually going to be
1052 * used by someone and the sprite cache isn't big enough for that
1053 * particular NewGRF developer.
1054 */
1055 Sprite *spr = const_cast<Sprite *>(GetSprite(this->current_sprite, SpriteType::Normal));
1056
1057 /* Remember the original offsets of the current sprite, if not already in mapping. */
1058 if (this->offs_start_map.count(this->current_sprite) == 0) {
1059 this->offs_start_map[this->current_sprite] = XyOffs(spr->x_offs, spr->y_offs);
1060 }
1061 int amt = ScaleByZoom(_ctrl_pressed ? 8 : 1, SpriteAlignerWindow::zoom);
1062 switch (widget) {
1063 /* Move eight units at a time if ctrl is pressed. */
1064 case WID_SA_UP: spr->y_offs -= amt; break;
1065 case WID_SA_DOWN: spr->y_offs += amt; break;
1066 case WID_SA_LEFT: spr->x_offs -= amt; break;
1067 case WID_SA_RIGHT: spr->x_offs += amt; break;
1068 }
1069 /* Of course, we need to redraw the sprite, but where is it used?
1070 * Everywhere is a safe bet. */
1072 break;
1073 }
1074
1075 case WID_SA_RESET_REL:
1076 /* Reset the starting offsets for the current sprite. */
1077 this->offs_start_map.erase(this->current_sprite);
1078 this->SetDirty();
1079 break;
1080
1081 case WID_SA_CENTRE:
1082 SpriteAlignerWindow::centre = !SpriteAlignerWindow::centre;
1083 this->SetWidgetLoweredState(widget, SpriteAlignerWindow::centre);
1084 this->SetDirty();
1085 break;
1086
1087 case WID_SA_CROSSHAIR:
1088 SpriteAlignerWindow::crosshair = !SpriteAlignerWindow::crosshair;
1089 this->SetWidgetLoweredState(widget, SpriteAlignerWindow::crosshair);
1090 this->SetDirty();
1091 break;
1092
1093 default:
1094 if (IsInsideBS(widget, WID_SA_ZOOM, ZOOM_LVL_END)) {
1095 SpriteAlignerWindow::zoom = ZoomLevel(widget - WID_SA_ZOOM);
1096 this->InvalidateData(0, true);
1097 }
1098 break;
1099 }
1100 }
1101
1102 void OnQueryTextFinished(std::optional<std::string> str) override
1103 {
1104 if (!str.has_value() || str->empty()) return;
1105
1106 this->current_sprite = atoi(str->c_str());
1107 if (this->current_sprite >= GetMaxSpriteID()) this->current_sprite = 0;
1108 while (GetSpriteType(this->current_sprite) != SpriteType::Normal) {
1109 this->current_sprite = (this->current_sprite + 1) % GetMaxSpriteID();
1110 }
1111 this->SelectAction5Type();
1112 this->SetDirty();
1113 }
1114
1120 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
1121 {
1122 if (!gui_scope) return;
1123 if (data == 1) {
1124 /* Sprite picker finished */
1126 this->vscroll->SetCount(_newgrf_debug_sprite_picker.sprites.size());
1127 }
1128
1129 SpriteAlignerWindow::zoom = Clamp(SpriteAlignerWindow::zoom, _settings_client.gui.zoom_min, _settings_client.gui.zoom_max);
1130 for (ZoomLevel z = ZOOM_LVL_BEGIN; z < ZOOM_LVL_END; z++) {
1132 this->SetWidgetsLoweredState(SpriteAlignerWindow::zoom == z, WID_SA_ZOOM + z);
1133 }
1134 }
1135
1136 void OnResize() override
1137 {
1138 this->vscroll->SetCapacityFromWidget(this, WID_SA_LIST);
1139 }
1140
1141private:
1142 void SelectAction5Type()
1143 {
1144 const auto act5types = GetAction5Types();
1145 for (auto it = std::begin(act5types); it != std::end(act5types); ++it) {
1146 if (it->sprite_base <= this->current_sprite && this->current_sprite < it->sprite_base + it->max_sprites) {
1147 this->act5_type = &*it;
1148 return;
1149 }
1150 }
1151 this->act5_type = nullptr;
1152 }
1153};
1154
1155bool SpriteAlignerWindow::centre = true;
1156bool SpriteAlignerWindow::crosshair = true;
1157
1158static constexpr NWidgetPart _nested_sprite_aligner_widgets[] = {
1160 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1161 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SA_CAPTION), SetStringTip(STR_JUST_STRING4, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1162 NWidget(WWT_SHADEBOX, COLOUR_GREY),
1163 NWidget(WWT_STICKYBOX, COLOUR_GREY),
1164 EndContainer(),
1165 NWidget(WWT_PANEL, COLOUR_GREY),
1169 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),
1170 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),
1171 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),
1172 EndContainer(),
1174 NWidget(NWID_SPACER), SetFill(1, 1), SetResize(1, 0),
1175 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SA_UP), SetSpriteTip(SPR_ARROW_UP, STR_SPRITE_ALIGNER_MOVE_TOOLTIP), SetResize(0, 0), SetMinimalSize(11, 11),
1176 NWidget(NWID_SPACER), SetFill(1, 1), SetResize(1, 0),
1177 EndContainer(),
1180 NWidget(NWID_SPACER), SetFill(1, 1), SetResize(0, 1),
1181 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SA_LEFT), SetSpriteTip(SPR_ARROW_LEFT, STR_SPRITE_ALIGNER_MOVE_TOOLTIP), SetResize(0, 0), SetMinimalSize(11, 11),
1182 NWidget(NWID_SPACER), SetFill(1, 1), SetResize(0, 1),
1183 EndContainer(),
1184 NWidget(WWT_PANEL, COLOUR_DARK_BLUE, WID_SA_SPRITE), SetToolTip(STR_SPRITE_ALIGNER_SPRITE_TOOLTIP), SetResize(1, 1), SetFill(1, 1),
1185 EndContainer(),
1187 NWidget(NWID_SPACER), SetFill(1, 1), SetResize(0, 1),
1188 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SA_RIGHT), SetSpriteTip(SPR_ARROW_RIGHT, STR_SPRITE_ALIGNER_MOVE_TOOLTIP), SetResize(0, 0), SetMinimalSize(11, 11),
1189 NWidget(NWID_SPACER), SetFill(1, 1), SetResize(0, 1),
1190 EndContainer(),
1191 EndContainer(),
1193 NWidget(NWID_SPACER), SetFill(1, 1), SetResize(1, 0),
1194 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SA_DOWN), SetSpriteTip(SPR_ARROW_DOWN, STR_SPRITE_ALIGNER_MOVE_TOOLTIP), SetResize(0, 0), SetMinimalSize(11, 11),
1195 NWidget(NWID_SPACER), SetFill(1, 1), SetResize(1, 0),
1196 EndContainer(),
1197 NWidget(WWT_LABEL, INVALID_COLOUR, WID_SA_OFFSETS_ABS), SetStringTip(STR_SPRITE_ALIGNER_OFFSETS_ABS), SetFill(1, 0), SetResize(1, 0),
1198 NWidget(WWT_LABEL, INVALID_COLOUR, WID_SA_OFFSETS_REL), SetStringTip(STR_SPRITE_ALIGNER_OFFSETS_REL), SetFill(1, 0), SetResize(1, 0),
1200 NWidget(WWT_TEXTBTN_2, COLOUR_GREY, WID_SA_CENTRE), SetStringTip(STR_SPRITE_ALIGNER_CENTRE_OFFSET), SetFill(1, 0), SetResize(1, 0),
1201 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),
1202 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SA_CROSSHAIR), SetStringTip(STR_SPRITE_ALIGNER_CROSSHAIR), SetFill(1, 0), SetResize(1, 0),
1203 EndContainer(),
1204 EndContainer(),
1206 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SA_PICKER), SetStringTip(STR_SPRITE_ALIGNER_PICKER_BUTTON, STR_SPRITE_ALIGNER_PICKER_TOOLTIP), SetFill(1, 0),
1210 EndContainer(),
1212 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SA_ZOOM + ZOOM_LVL_IN_4X), SetStringTip(STR_CONFIG_SETTING_ZOOM_LVL_MIN), SetFill(1, 0),
1213 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SA_ZOOM + ZOOM_LVL_IN_2X), SetStringTip(STR_CONFIG_SETTING_ZOOM_LVL_IN_2X), SetFill(1, 0),
1214 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SA_ZOOM + ZOOM_LVL_NORMAL), SetStringTip(STR_CONFIG_SETTING_ZOOM_LVL_NORMAL), SetFill(1, 0),
1215 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SA_ZOOM + ZOOM_LVL_OUT_2X), SetStringTip(STR_CONFIG_SETTING_ZOOM_LVL_OUT_2X), SetFill(1, 0),
1216 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SA_ZOOM + ZOOM_LVL_OUT_4X), SetStringTip(STR_CONFIG_SETTING_ZOOM_LVL_OUT_4X), SetFill(1, 0),
1217 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SA_ZOOM + ZOOM_LVL_OUT_8X), SetStringTip(STR_CONFIG_SETTING_ZOOM_LVL_OUT_8X), SetFill(1, 0),
1218 EndContainer(),
1219 EndContainer(),
1220 EndContainer(),
1222 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1223 NWidget(WWT_RESIZEBOX, COLOUR_GREY), SetResizeWidgetTypeTip(RWV_HIDE_BEVEL, STR_TOOLTIP_RESIZE),
1224 EndContainer(),
1225 EndContainer(),
1226};
1227
1228static WindowDesc _sprite_aligner_desc(
1229 WDP_AUTO, "sprite_aligner", 400, 300,
1231 {},
1232 _nested_sprite_aligner_widgets
1233);
1234
1239{
1240 AllocateWindowDescFront<SpriteAlignerWindow>(_sprite_aligner_desc, 0);
1241}
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 t)
Test whether cargo type is not INVALID_CARGO.
Definition cargo_type.h:105
Helper class to wrap some functionality/queries in.
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.
void SetSimpleStringParameters(StringID string, uint32_t index) const
Helper to make setting the strings easier.
virtual bool PSAWithParameter() const
Used to decide if the PSA needs a parameter or not.
void SetObjectAtStringParameters(StringID string, uint32_t index, TileIndex tile) const
Helper to make setting the strings easier for objects at a specific tile.
virtual void SetStringParameters(uint index) const =0
Set the string parameters to write the right data for a STRINGn.
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:2459
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:2533
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:28
static const WidgetDimensions unscaled
Unscaled widget dimensions.
Definition window_gui.h:94
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:851
void DrawRectOutline(const Rect &r, int colour, int width, int dash)
Draw the outline of a Rect.
Definition gfx.cpp:456
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:657
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:988
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:1548
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:243
@ SA_RIGHT
Right align the text (must be a single bit).
Definition gfx_type.h:377
@ SA_FORCE
Force the alignment, i.e. don't swap for RTL languages.
Definition gfx_type.h:387
@ TC_NO_SHADE
Do not add shading to this text colour.
Definition gfx_type.h:318
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:937
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition gfx.cpp:1529
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(StringID str, StringID caption, uint maxsize, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
Show a query popup window with a textbox in it.
std::span< const Action5Type > GetAction5Types()
Get list of all action 5 types.
Definition newgrf.cpp:6501
GrfSpecFeature
Definition newgrf.h:68
@ GSF_INVALID
An invalid spec feature.
Definition newgrf.h:95
@ GSF_FAKE_END
End of the fake features.
Definition newgrf.h:93
Information about NewGRF Action 5.
NewGRF handling of airports.
NewGRF handling of airport tiles.
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:56
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.
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
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
Definition strings.cpp:104
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
Definition strings.cpp:332
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:56
void SetDParamStr(size_t n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition strings.cpp:370
void SetDParamMaxDigits(size_t n, uint count, FontSize size)
Set DParam n to some number that is suitable for string size computations.
Definition strings.cpp:143
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:140
StringID name
Name of this type of cargo.
Definition cargotype.h:94
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:156
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.
void DrawVehicleChainWidget(const Rect &r) const
Helper function to draw the vehicle chain widget.
void SetStringParameters(WidgetID widget) const override
Initialize string parameters for a 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.
Tindex index
Index of this pool item.
static Titem * Get(size_t index)
Returns Titem with given index.
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 SetStringParameters(WidgetID widget) const override
Initialize string parameters for a widget.
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.
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:272
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition window.cpp:1730
void InvalidateData(int data=0, bool gui_scope=true)
Mark this window's data as invalid (in need of re-computing)
Definition window.cpp:3157
void RaiseWidget(WidgetID widget_index)
Marks a widget as raised.
Definition window_gui.h:468
virtual void ShowNewGRFInspectWindow() const
Show the NewGRF inspection window.
Definition window_gui.h:854
ResizeInfo resize
Resize information.
Definition window_gui.h:313
void SetWidgetsDisabledState(bool disab_stat, Args... widgets)
Sets the enabled/disabled status of a list of widgets.
Definition window_gui.h:514
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
Definition window.cpp:1720
void SetWidgetsLoweredState(bool lowered_stat, Args... widgets)
Sets the lowered/raised status of a list of widgets.
Definition window_gui.h:525
void SetWidgetLoweredState(WidgetID widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition window_gui.h:440
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition window_gui.h:970
void LowerWidget(WidgetID widget_index)
Marks a widget as lowered.
Definition window_gui.h:459
const Scrollbar * GetScrollbar(WidgetID widnum) const
Return the Scrollbar to a widget index.
Definition window.cpp:311
void SetWidgetDisabledState(WidgetID widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition window_gui.h:380
int height
Height of the window (number of pixels down in y direction)
Definition window_gui.h:311
int width
width of the window (number of pixels to the right in x direction)
Definition window_gui.h:310
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:301
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.
static const VehicleID INVALID_VEHICLE
Constant representing a non-existing vehicle.
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition widget.cpp:283
static RectPadding ScaleGUITrad(const RectPadding &r)
Scale a RectPadding to GUI zoom level.
Definition widget.cpp:35
@ 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:48
@ NWID_SPACER
Invisible widget that takes some space.
Definition widget_type.h:70
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:66
@ WWT_TEXTBTN
(Toggle) Button with text
Definition widget_type.h:46
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:41
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition widget_type.h:57
@ WWT_MATRIX
Grid of rows and columns.
Definition widget_type.h:50
@ WWT_SHADEBOX
Shade box (at top-right of a window, between WWT_DEBUGBOX and WWT_DEFSIZEBOX)
Definition widget_type.h:55
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition widget_type.h:52
@ NWID_VSCROLLBAR
Vertical scrollbar.
Definition widget_type.h:76
@ NWID_VERTICAL
Vertical container.
Definition widget_type.h:68
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition widget_type.h:60
@ WWT_TEXTBTN_2
(Toggle) Button with diff text when clicked
Definition widget_type.h:47
@ WWT_EMPTY
Empty widget, place holder to reserve space in widget tree.
Definition widget_type.h:39
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window)
Definition widget_type.h:59
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX)
Definition widget_type.h:56
@ NWID_HORIZONTAL_LTR
Horizontal container that doesn't change the order of the widgets for RTL languages.
Definition widget_type.h:67
@ AWV_DECREASE
Arrow to the left or in case of RTL to the right.
Definition widget_type.h:22
@ AWV_INCREASE
Arrow to the right or in case of RTL to the left.
Definition widget_type.h:23
@ NC_EQUALSIZE
Value of the NCB_EQUALSIZE flag.
@ RWV_HIDE_BEVEL
Bevel of resize box is hidden.
Definition widget_type.h:31
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:1137
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:3217
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