OpenTTD Source 20260731-master-g77ba2b244a
widget_type.h
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef WIDGET_TYPE_H
11#define WIDGET_TYPE_H
12
13#include "core/math_func.hpp"
14#include "strings_type.h"
15#include "gfx_type.h"
16#include "window_type.h"
17
19enum class ArrowWidgetType : uint8_t {
24};
25
27enum class ResizeWidgetType : uint8_t {
30};
31
111
113enum class SizingType : uint8_t {
116};
117
119enum class AspectFlag : uint8_t {
122};
123
126
127/* Forward declarations. */
128class NWidgetCore;
129class Scrollbar;
130
132using WidgetLookup = std::map<WidgetID, class NWidgetBase *>;
133
140class NWidgetBase {
141public:
142 NWidgetBase(WidgetType tp, WidgetID index = INVALID_WIDGET) : type(tp), index(index) {}
144 virtual ~NWidgetBase() = default;
145
146 void ApplyAspectRatio();
147
151 virtual void AdjustPaddingForZoom();
152 virtual void SetupSmallestSize(Window *w) = 0;
153 virtual void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) = 0;
154
161 virtual std::pair<uint, uint> GetPreferredSizeForSize(uint given_width, uint given_height)
162 {
163 return {given_width, given_height};
164 }
165
166 virtual void FillWidgetLookup(WidgetLookup &widget_lookup);
167
168 virtual NWidgetCore *GetWidgetFromPos(int x, int y) = 0;
169 virtual NWidgetBase *GetWidgetOfType(WidgetType tp);
170
176 template <class NWID>
178 {
179 for (NWidgetBase *nwid_parent = this->parent; nwid_parent != nullptr; nwid_parent = nwid_parent->parent) {
180 if (NWID *nwid = dynamic_cast<NWID *>(nwid_parent); nwid != nullptr) return nwid;
181 }
182 return nullptr;
183 }
184
190 template <class NWID>
191 const NWID *GetParentWidget() const
192 {
193 for (const NWidgetBase *nwid_parent = this->parent; nwid_parent != nullptr; nwid_parent = nwid_parent->parent) {
194 if (const NWID *nwid = dynamic_cast<const NWID *>(nwid_parent); nwid != nullptr) return nwid;
195 }
196 return nullptr;
197 }
198
199 inline WidgetID GetIndex() const { return this->index; }
200
205 virtual bool IsHighlighted() const { return false; }
206
212
217 virtual void SetHighlighted([[maybe_unused]] TextColour highlight_colour) {}
218
226 inline void SetPadding(uint8_t top, uint8_t right, uint8_t bottom, uint8_t left)
227 {
228 this->uz_padding.top = top;
229 this->uz_padding.right = right;
230 this->uz_padding.bottom = bottom;
231 this->uz_padding.left = left;
232 this->AdjustPaddingForZoom();
233 }
234
239 inline void SetPadding(const RectPadding &padding)
240 {
241 this->uz_padding = padding;
242 this->AdjustPaddingForZoom();
243 }
244
245 inline uint GetHorizontalStepSize(SizingType sizing) const;
246 inline uint GetVerticalStepSize(SizingType sizing) const;
247
248 virtual void Draw(const Window *w) = 0;
249 virtual void SetDirty(const Window *w) const;
250
251 Rect GetCurrentRect() const
252 {
253 Rect r;
254 r.left = this->pos_x;
255 r.top = this->pos_y;
256 r.right = this->pos_x + this->current_x - 1;
257 r.bottom = this->pos_y + this->current_y - 1;
258 return r;
259 }
260
262 uint fill_x = 0;
263 uint fill_y = 0;
264 uint resize_x = 0;
265 uint resize_y = 0;
266 /* Size of the widget in the smallest window possible.
267 * Computed by #SetupSmallestSize() followed by #AssignSizePosition().
268 */
269 uint smallest_x = 0;
270 uint smallest_y = 0;
271 /* Current widget size (that is, after resizing). */
272 uint current_x = 0;
273 uint current_y = 0;
274 float aspect_ratio = 0;
276
277 int pos_x = 0;
278 int pos_y = 0;
279
282
283 NWidgetBase *parent = nullptr;
284
285protected:
287
288 inline void StoreSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height);
289};
290
297{
298 return (sizing == SizingType::Resize) ? this->resize_x : this->fill_x;
299}
300
307{
308 return (sizing == SizingType::Resize) ? this->resize_y : this->fill_y;
309}
310
319inline void NWidgetBase::StoreSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height)
320{
321 this->pos_x = x;
322 this->pos_y = y;
323 if (sizing == SizingType::Smallest) {
324 this->smallest_x = given_width;
325 this->smallest_y = given_height;
326 }
327 this->current_x = given_width;
328 this->current_y = given_height;
329}
330
331
336class NWidgetResizeBase : public NWidgetBase {
337public:
339
340 void AdjustPaddingForZoom() override;
341 void SetMinimalSize(uint min_x, uint min_y);
342 void SetMinimalSizeAbsolute(uint min_x, uint min_y);
343 void SetMinimalTextLines(uint8_t min_lines, uint8_t spacing, FontSize size);
345 void SetFill(uint fill_x, uint fill_y);
346 void SetResize(uint resize_x, uint resize_y);
347 void SetAspect(float ratio, AspectFlags flags = AspectFlag::ResizeX);
348 void SetAspect(int x_ratio, int y_ratio, AspectFlags flags = AspectFlag::ResizeX);
349
350 bool UpdateMultilineWidgetSize(const std::string &str, int max_lines);
351 bool UpdateSize(uint min_x, uint min_y);
352 bool UpdateVerticalSize(uint min_y);
353
354 void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override;
355
356 uint min_x = 0;
357 uint min_y = 0;
358
359 bool absolute = false;
360 uint uz_min_x = 0;
361 uint uz_min_y = 0;
362
363 uint8_t uz_text_lines = 0;
364 uint8_t uz_text_spacing = 0;
366 uint8_t toolbar_size = 0;
367};
368
370enum class NWidgetDisplayFlag : uint8_t {
371 /* Generic. */
374
375 /* Viewport widget. */
379
380 /* Button dropdown widget. */
382
383 /* Scrollbar widget. */
386
387 /* Generic. */
390};
391
394
397 StringID string{};
398 SpriteID sprite{};
399 ArrowWidgetType arrow_widget_type{};
400 ResizeWidgetType resize_widget_type{};
401 Colours alternate_colour = Colours::Invalid;
402 Dimension matrix{};
403};
404
410public:
412
413 void SetString(StringID string);
415 void SetSprite(SpriteID sprite);
417 void SetMatrixDimension(uint32_t columns, uint32_t rows);
420 StringID GetToolTip() const;
423
424 StringID GetString() const;
426
427 inline void SetLowered(bool lowered);
428 inline bool IsLowered() const;
429 inline void SetDisabled(bool disabled);
430 inline bool IsDisabled() const;
431
432 inline TextColour GetTextColour() const { return this->text_colour; }
433 inline FontSize GetFontSize() const { return this->text_size; }
434
435 NWidgetCore *GetWidgetFromPos(int x, int y) override;
436 bool IsHighlighted() const override;
437 TextColour GetHighlightColour() const override;
439
442protected:
450
451 /* This function constructs the widgets, so it should be able to write the variables. */
452 friend void ApplyNWidgetPartAttribute(const struct NWidgetPart &nwid, NWidgetBase *dest);
453};
454
460
461inline bool NWidgetCore::IsHighlighted() const
462{
463 return this->disp_flags.Test(NWidgetDisplayFlag::Highlight);
464}
465
467{
468 return this->highlight_colour;
469}
470
475inline void NWidgetCore::SetLowered(bool lowered)
476{
478}
479
484inline bool NWidgetCore::IsLowered() const
485{
486 return this->disp_flags.Test(NWidgetDisplayFlag::Lowered);
487}
488
493inline void NWidgetCore::SetDisabled(bool disabled)
494{
496}
497
502inline bool NWidgetCore::IsDisabled() const
503{
504 return this->disp_flags.Test(NWidgetDisplayFlag::Disabled);
505}
506
507
512class NWidgetContainer : public NWidgetBase {
513public:
514 NWidgetContainer(WidgetType tp, WidgetID index = INVALID_WIDGET) : NWidgetBase(tp, index) {}
515
516 void AdjustPaddingForZoom() override;
517 void Add(std::unique_ptr<NWidgetBase> &&wid);
518 void FillWidgetLookup(WidgetLookup &widget_lookup) override;
519
520 void Draw(const Window *w) override;
521 NWidgetCore *GetWidgetFromPos(int x, int y) override;
522
527 inline bool IsEmpty() { return this->children.empty(); }
528
530 void UnfocusWidgets(Window *parent_window);
531
536 inline void Clear(Window *parent_window)
537 {
538 this->UnfocusWidgets(parent_window);
539 this->children.clear();
540 }
541
542protected:
543 std::vector<std::unique_ptr<NWidgetBase>> children{};
544};
545
554
565class NWidgetStacked : public NWidgetContainer {
566public:
567 NWidgetStacked(WidgetID index) : NWidgetContainer(NWID_SELECTION, index) {}
568
569 void SetupSmallestSize(Window *w) override;
570 void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override;
572
573 void Draw(const Window *w) override;
574 NWidgetCore *GetWidgetFromPos(int x, int y) override;
575
576 bool SetDisplayedPlane(int plane);
577
578 int shown_plane = 0;
579private:
581};
582
584enum class NWidContainerFlag : uint8_t {
587};
588
591
593class NWidgetPIPContainer : public NWidgetContainer {
594public:
595 NWidgetPIPContainer(WidgetType tp, NWidContainerFlags flags = {}, WidgetID index = INVALID_WIDGET) : NWidgetContainer(tp, index), flags(flags) {}
596
597 void AdjustPaddingForZoom() override;
598 void SetPIP(uint8_t pip_pre, uint8_t pip_inter, uint8_t pip_post);
599 void SetPIPRatio(uint8_t pip_ratio_pre, uint8_t pip_ratio_inter, uint8_t pip_ratio_post);
600
601protected:
603 uint8_t pip_pre = 0;
604 uint8_t pip_inter = 0;
605 uint8_t pip_post = 0;
606 uint8_t pip_ratio_pre = 0;
607 uint8_t pip_ratio_inter = 0;
608 uint8_t pip_ratio_post = 0;
609
610 uint8_t uz_pip_pre = 0;
611 uint8_t uz_pip_inter = 0;
612 uint8_t uz_pip_post = 0;
613
614 uint8_t gaps = 0;
615};
616
621class NWidgetHorizontal : public NWidgetPIPContainer {
622public:
623 NWidgetHorizontal(NWidContainerFlags flags = {}, WidgetID index = INVALID_WIDGET, WidgetType type = NWID_HORIZONTAL) : NWidgetPIPContainer(type, flags, index) {}
624
625 void SetupSmallestSize(Window *w) override;
626 void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override;
627};
628
633class NWidgetHorizontalLTR : public NWidgetHorizontal {
634public:
635 NWidgetHorizontalLTR(NWidContainerFlags flags = {}, WidgetID index = INVALID_WIDGET) : NWidgetHorizontal(flags, index, NWID_HORIZONTAL_LTR) {}
636
637 void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override;
638};
639
644class NWidgetVertical : public NWidgetPIPContainer {
645public:
646 NWidgetVertical(NWidContainerFlags flags = {}, WidgetID index = INVALID_WIDGET) : NWidgetPIPContainer(NWID_VERTICAL, flags, index) {}
647
648 void SetupSmallestSize(Window *w) override;
649 void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override;
650
651 bool bottom_up = false;
652};
653
662class NWidgetMatrix : public NWidgetPIPContainer {
663public:
664 NWidgetMatrix(Colours colour, WidgetID index) : NWidgetPIPContainer(NWID_MATRIX, NWidContainerFlag::EqualSize, index), colour(colour) {}
665
666 void SetClicked(int clicked);
667 void SetCount(int count);
669 int GetCurrentElement() const;
670
671 void SetupSmallestSize(Window *w) override;
672 void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override;
673
674 NWidgetCore *GetWidgetFromPos(int x, int y) override;
675 void Draw(const Window *w) override;
676protected:
678 int clicked = -1;
679 int count = 0;
681 Scrollbar *sb = nullptr;
682private:
683 int widget_w = 0;
684 int widget_h = 0;
685 int widgets_x = 0;
686 int widgets_y = 0;
687
688 void GetScrollOffsets(int &start_x, int &start_y, int &base_offs_x, int &base_offs_y);
689};
690
691
697public:
698 NWidgetSpacer(int width, int height);
699
700 void SetupSmallestSize(Window *w) override;
701
702 void Draw(const Window *w) override;
703 void SetDirty(const Window *w) const override;
704 NWidgetCore *GetWidgetFromPos(int x, int y) override;
705};
706
712public:
713 NWidgetBackground(WidgetType tp, Colours colour, WidgetID index, std::unique_ptr<NWidgetPIPContainer> &&child = nullptr);
714
715 void Add(std::unique_ptr<NWidgetBase> &&nwid);
716 void SetPIP(uint8_t pip_pre, uint8_t pip_inter, uint8_t pip_post);
717 void SetPIPRatio(uint8_t pip_ratio_pre, uint8_t pip_ratio_inter, uint8_t pip_ratio_post);
718
719 void AdjustPaddingForZoom() override;
720 void SetupSmallestSize(Window *w) override;
721 void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override;
722
723 void FillWidgetLookup(WidgetLookup &widget_lookup) override;
724
725 void Draw(const Window *w) override;
726 NWidgetCore *GetWidgetFromPos(int x, int y) override;
728
729private:
730 std::unique_ptr<NWidgetPIPContainer> child{};
731};
732
742class NWidgetViewport : public NWidgetCore {
743public:
744 NWidgetViewport(WidgetID index);
745
746 void SetupSmallestSize(Window *w) override;
747 void Draw(const Window *w) override;
748
749 void InitializeViewport(Window *w, std::variant<TileIndex, VehicleID> focus, ZoomLevel zoom);
751};
752
756class Scrollbar {
757public:
758 using size_type = int32_t;
759 static constexpr size_type max_size_type = std::numeric_limits<size_type>::max();
760 static constexpr size_type npos = max_size_type;
761private:
762 const bool is_vertical = false;
763 size_type count = 0;
764 size_type cap = 0;
765 size_type pos = 0;
766 size_type stepsize = 1;
767
768public:
770 enum class Stepping : uint8_t {
774 };
775
777
782 inline size_type GetCount() const
783 {
784 return this->count;
785 }
786
791 inline size_type GetCapacity() const
792 {
793 return this->cap;
794 }
795
800 inline size_type GetPosition() const
801 {
802 return this->pos;
803 }
804
810 inline bool IsVisible(size_type item) const
811 {
812 return IsInsideBS(item, this->GetPosition(), this->GetCapacity());
813 }
814
819 inline bool IsVertical() const
820 {
821 return this->is_vertical;
822 }
823
829 {
830 assert(stepsize > 0);
831
832 this->stepsize = ClampTo<size_type>(stepsize);
833 }
834
840 void SetCount(size_t num)
841 {
842 assert(num < Scrollbar::max_size_type);
843
844 this->count = ClampTo<size_type>(num);
845 /* Ensure position is within bounds */
846 this->SetPosition(this->pos);
847 }
848
854 void SetCapacity(size_t capacity)
855 {
856 assert(capacity < Scrollbar::max_size_type);
857
858 this->cap = ClampTo<size_type>(capacity);
859 /* Ensure position is within bounds */
860 this->SetPosition(this->pos);
861 }
862
863 void SetCapacityFromWidget(Window *w, WidgetID widget, int padding = 0);
864
870 bool SetPosition(size_type position)
871 {
872 size_type old_pos = this->pos;
873 this->pos = Clamp(position, 0, std::max(this->count - this->cap, 0));
874 return this->pos != old_pos;
875 }
876
885 {
886 if (difference == 0) return false;
887 switch (unit) {
888 case Stepping::Small: difference *= this->stepsize; break;
889 case Stepping::Big: difference *= this->cap; break;
890 default: break;
891 }
892 return this->SetPosition(this->pos + difference);
893 }
894
901 void ScrollTowards(size_type position)
902 {
903 if (position < this->GetPosition()) {
904 /* scroll up to the item */
905 this->SetPosition(position);
906 } else if (position >= this->GetPosition() + this->GetCapacity()) {
907 /* scroll down so that the item is at the bottom */
908 this->SetPosition(position - this->GetCapacity() + 1);
909 }
910 }
911
912 size_type GetScrolledRowFromWidget(int clickpos, const Window * const w, WidgetID widget, int padding = 0, int line_height = -1) const;
913
919 template <typename Tcontainer>
920 auto GetVisibleRangeIterators(Tcontainer &container) const
921 {
922 assert(static_cast<size_t>(this->GetCount()) == container.size()); // Scrollbar and container size must match.
923 auto first = std::next(std::begin(container), this->GetPosition());
924 auto last = std::next(first, std::min<size_t>(this->GetCapacity(), this->GetCount() - this->GetPosition()));
925 return std::make_pair(first, last);
926 }
927
938 template <typename Tcontainer>
939 auto GetScrolledItemFromWidget(Tcontainer &container, int clickpos, const Window * const w, WidgetID widget, int padding = 0, int line_height = -1) const
940 {
941 assert(static_cast<size_t>(this->GetCount()) == container.size()); // Scrollbar and container size must match.
942 size_type row = this->GetScrolledRowFromWidget(clickpos, w, widget, padding, line_height);
943 if (row == Scrollbar::npos) return std::end(container);
944
945 return std::next(std::begin(container), row);
946 }
947
948 EventState UpdateListPositionOnKeyPress(int &list_position, uint16_t keycode) const;
949};
950
956class NWidgetScrollbar : public NWidgetCore, public Scrollbar {
957public:
959
960 void SetupSmallestSize(Window *w) override;
961 void Draw(const Window *w) override;
962
963 static void InvalidateDimensionCache();
964 static Dimension GetVerticalDimension();
965 static Dimension GetHorizontalDimension();
966
967private:
970};
971
976class NWidgetLeaf : public NWidgetCore {
977public:
979
980 void SetupSmallestSize(Window *w) override;
981 void Draw(const Window *w) override;
982
983 bool ButtonHit(const Point &pt);
984
985 static void InvalidateDimensionCache();
986
990private:
995};
996
1004inline uint ComputeMaxSize(uint base, uint max_space, uint step)
1005{
1006 if (base >= max_space || step == 0) return base;
1007 if (step == 1) return max_space;
1008 uint increment = max_space - base;
1009 increment -= increment % step;
1010 return base + increment;
1011}
1012
1061
1070
1079
1081 NWidContainerFlags flags;
1082 WidgetID index;
1083};
1084
1091
1097 uint8_t pre, inter, post;
1098};
1099
1109
1118
1126
1128 float ratio;
1129 AspectFlags flags;
1130};
1131
1136using NWidgetFunctionType = std::unique_ptr<NWidgetBase>();
1137
1142struct NWidgetPart {
1144 union NWidgetPartUnion {
1156
1157 /* Constructors for each NWidgetPartUnion data type. */
1158 constexpr NWidgetPartUnion() : xy() {}
1159 constexpr NWidgetPartUnion(Point xy) : xy(xy) {}
1160 constexpr NWidgetPartUnion(NWidgetPartDataTip data_tip) : data_tip(data_tip) {}
1161 constexpr NWidgetPartUnion(NWidgetPartWidget widget) : widget(widget) {}
1162 constexpr NWidgetPartUnion(NWidgetPartPaddings padding) : padding(padding) {}
1163 constexpr NWidgetPartUnion(NWidgetPartPIP pip) : pip(pip) {}
1164 constexpr NWidgetPartUnion(NWidgetPartTextLines text_lines) : text_lines(text_lines) {}
1165 constexpr NWidgetPartUnion(NWidgetPartTextStyle text_style) : text_style(text_style) {}
1166 constexpr NWidgetPartUnion(NWidgetPartAlignment align) : align(align) {}
1167 constexpr NWidgetPartUnion(NWidgetFunctionType *func_ptr) : func_ptr(func_ptr) {}
1168 constexpr NWidgetPartUnion(NWidgetPartContainer container) : container(container) {}
1169 constexpr NWidgetPartUnion(NWidgetPartAspect aspect) : aspect(aspect) {}
1170 } u;
1171
1172 /* Constructors for each NWidgetPart data type. */
1173 explicit constexpr NWidgetPart(WidgetType type) : type(type), u() {}
1174 constexpr NWidgetPart(WidgetType type, Point xy) : type(type), u(xy) {}
1175 constexpr NWidgetPart(WidgetType type, NWidgetPartDataTip data_tip) : type(type), u(data_tip) {}
1176 constexpr NWidgetPart(WidgetType type, NWidgetPartWidget widget) : type(type), u(widget) {}
1177 constexpr NWidgetPart(WidgetType type, NWidgetPartPaddings padding) : type(type), u(padding) {}
1178 constexpr NWidgetPart(WidgetType type, NWidgetPartPIP pip) : type(type), u(pip) {}
1179 constexpr NWidgetPart(WidgetType type, NWidgetPartTextLines text_lines) : type(type), u(text_lines) {}
1180 constexpr NWidgetPart(WidgetType type, NWidgetPartTextStyle text_style) : type(type), u(text_style) {}
1181 constexpr NWidgetPart(WidgetType type, NWidgetPartAlignment align) : type(type), u(align) {}
1182 constexpr NWidgetPart(WidgetType type, NWidgetFunctionType *func_ptr) : type(type), u(func_ptr) {}
1183 constexpr NWidgetPart(WidgetType type, NWidgetPartContainer container) : type(type), u(container) {}
1184 constexpr NWidgetPart(WidgetType type, NWidgetPartAspect aspect) : type(type), u(aspect) {}
1185};
1186
1194constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
1195{
1196 return NWidgetPart{WPT_RESIZE, Point{dx, dy}};
1197}
1198
1206constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
1207{
1208 return NWidgetPart{WPT_MINSIZE, Point{x, y}};
1209}
1210
1217{
1218 return NWidgetPart{WPT_MINSIZE, Point{4, 0}};
1219}
1220
1228{
1229 return NWidgetPart{WPT_TOOLBARSIZE, Point{width, 1}};
1230}
1231
1240constexpr NWidgetPart SetMinimalTextLines(uint8_t lines, uint8_t spacing, FontSize size = FontSize::Normal)
1241{
1242 return NWidgetPart{WPT_MINTEXTLINES, NWidgetPartTextLines{lines, spacing, size}};
1243}
1244
1253{
1254 return NWidgetPart{WPT_TEXTSTYLE, NWidgetPartTextStyle{colour, size}};
1255}
1256
1264{
1266}
1267
1275constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
1276{
1277 return NWidgetPart{WPT_FILL, Point{fill_x, fill_y}};
1278}
1279
1287{
1289}
1290
1298constexpr NWidgetPart SetStringTip(StringID string, StringID tip = {})
1299{
1300 return NWidgetPart{WPT_DATATIP, NWidgetPartDataTip{{.string = string}, tip}};
1301}
1302
1310constexpr NWidgetPart SetSpriteTip(SpriteID sprite, StringID tip = {})
1311{
1312 return NWidgetPart{WPT_DATATIP, NWidgetPartDataTip{{.sprite = sprite}, tip}};
1313}
1314
1323constexpr NWidgetPart SetSpriteStringTip(SpriteID sprite, StringID string, StringID tip = {})
1324{
1325 return NWidgetPart{WPT_DATATIP, NWidgetPartDataTip{{.string = string, .sprite = sprite}, tip}};
1326}
1327
1336{
1337 return NWidgetPart{WPT_DATATIP, NWidgetPartDataTip{{.arrow_widget_type = widget_type}, tip}};
1338}
1339
1348{
1349 return NWidgetPart{WPT_DATATIP, NWidgetPartDataTip{{.resize_widget_type = widget_type}, tip}};
1350}
1351
1360{
1361 return NWidgetPart{WPT_DATATIP, NWidgetPartDataTip{{.alternate_colour = colour}, tip}};
1362}
1363
1372constexpr NWidgetPart SetMatrixDataTip(uint32_t cols, uint32_t rows, StringID tip = {})
1373{
1374 return NWidgetPart{WPT_DATATIP, NWidgetPartDataTip{{.matrix{ cols, rows }}, tip}};
1375}
1376
1384{
1385 return NWidgetPart{WPT_DATATIP, NWidgetPartDataTip{{}, tip}};
1386}
1387
1398constexpr NWidgetPart SetPadding(uint8_t top, uint8_t right, uint8_t bottom, uint8_t left)
1399{
1400 return NWidgetPart{WPT_PADDING, NWidgetPartPaddings{left, top, right, bottom}};
1401}
1402
1410constexpr NWidgetPart SetPadding(uint8_t horizontal, uint8_t vertical)
1411{
1412 return NWidgetPart{WPT_PADDING, NWidgetPartPaddings{horizontal, vertical, horizontal, vertical}};
1413}
1414
1421constexpr NWidgetPart SetPadding(const RectPadding &padding)
1422{
1424}
1425
1432constexpr NWidgetPart SetPadding(uint8_t padding)
1433{
1434 return SetPadding(padding, padding, padding, padding);
1435}
1436
1445constexpr NWidgetPart SetPIP(uint8_t pre, uint8_t inter, uint8_t post)
1446{
1447 return NWidgetPart{WPT_PIPSPACE, NWidgetPartPIP{pre, inter, post}};
1448}
1449
1458constexpr NWidgetPart SetPIPRatio(uint8_t ratio_pre, uint8_t ratio_inter, uint8_t ratio_post)
1459{
1460 return NWidgetPart{WPT_PIPRATIO, NWidgetPartPIP{ratio_pre, ratio_inter, ratio_post}};
1461}
1462
1475
1484{
1485 return NWidgetPart{WPT_ASPECT, NWidgetPartAspect{ratio, flags}};
1486}
1487
1499{
1500 return NWidgetPart{tp, NWidgetPartWidget{col, idx}};
1501}
1502
1512{
1513 return NWidgetPart{tp, NWidgetPartContainer{cont_flags, idx}};
1514}
1515
1523{
1524 return NWidgetPart{WPT_FUNCTION, func_ptr};
1525}
1526
1528std::unique_ptr<NWidgetBase> MakeNWidgets(std::span<const NWidgetPart> nwid_parts, std::unique_ptr<NWidgetBase> &&container);
1529std::unique_ptr<NWidgetBase> MakeWindowNWidgetTree(std::span<const NWidgetPart> nwid_parts, NWidgetStacked **shade_select);
1530
1531std::unique_ptr<NWidgetBase> MakeCompanyButtonRows(WidgetID widget_first, WidgetID widget_last, Colours button_colour, int max_length, StringID button_tooltip, bool resizable = true);
1532
1534
1535#endif /* WIDGET_TYPE_H */
Enum-as-bit-set wrapper.
NWidgetCore * GetWidgetFromPos(int x, int y) override
Retrieve a widget by its position.
Definition widget.cpp:2387
NWidgetBase * GetWidgetOfType(WidgetType tp) override
Retrieve a widget by its type.
Definition widget.cpp:2397
void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override
Assign size and position to the widget.
Definition widget.cpp:2333
void SetupSmallestSize(Window *w) override
Compute smallest size needed by the widget.
Definition widget.cpp:2264
void AdjustPaddingForZoom() override
Adjust the padding based on the user interface zoom.
Definition widget.cpp:2258
NWidgetBackground(WidgetType tp, Colours colour, WidgetID index, std::unique_ptr< NWidgetPIPContainer > &&child=nullptr)
Constructor parent nested widgets.
Definition widget.cpp:2196
void SetPIP(uint8_t pip_pre, uint8_t pip_inter, uint8_t pip_post)
Set additional pre/inter/post space for the background widget.
Definition widget.cpp:2230
std::unique_ptr< NWidgetPIPContainer > child
Child widget.
void Draw(const Window *w) override
Draw the widgets of the tree.
Definition widget.cpp:2351
void Add(std::unique_ptr< NWidgetBase > &&nwid)
Add a child to the parent.
Definition widget.cpp:2211
void FillWidgetLookup(WidgetLookup &widget_lookup) override
Fill the Window::widget_lookup with pointers to nested widgets in the tree.
Definition widget.cpp:2345
void SetPIPRatio(uint8_t pip_ratio_pre, uint8_t pip_ratio_inter, uint8_t pip_ratio_post)
Set additional pre/inter/post space ratios for the background widget.
Definition widget.cpp:2249
Baseclass for nested widgets.
void StoreSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height)
Store size and position.
virtual TextColour GetHighlightColour() const
Get the colour of the highlighted text.
virtual ~NWidgetBase()=default
Ensure the destructor of the sub classes are called as well.
float aspect_ratio
Desired aspect ratio of widget.
uint GetHorizontalStepSize(SizingType sizing) const
Get the horizontal sizing step.
virtual bool IsHighlighted() const
Whether the widget is currently highlighted or not.
virtual void AdjustPaddingForZoom()
Adjust the padding based on the user interface zoom.
Definition widget.cpp:994
virtual void SetDirty(const Window *w) const
Mark the widget as 'dirty' (in need of repaint).
Definition widget.cpp:955
WidgetType type
Type of the widget / nested widget.
uint resize_x
Horizontal resize step (0 means not resizable).
uint fill_x
Horizontal fill stepsize (from initial size, 0 means not resizable).
NWID * GetParentWidget()
Get parent widget of type NWID.
NWidgetBase * parent
Parent widget of this widget, automatically filled in when added to container.
RectPadding uz_padding
Unscaled padding, for resize calculation.
virtual void SetupSmallestSize(Window *w)=0
Compute smallest size needed by the widget.
uint smallest_x
Smallest horizontal size of the widget in a filled window.
AspectFlags aspect_flags
Which dimensions can be resized.
uint current_x
Current horizontal size (after resizing).
int pos_y
Vertical position of top-left corner of the widget in the window.
int pos_x
Horizontal position of top-left corner of the widget in the window.
virtual void Draw(const Window *w)=0
Draw the widgets of the tree.
void SetPadding(uint8_t top, uint8_t right, uint8_t bottom, uint8_t left)
Set additional space (padding) around the widget.
void SetPadding(const RectPadding &padding)
Set additional space (padding) around the widget.
virtual void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl)=0
Assign size and position to the widget.
uint smallest_y
Smallest vertical size of the widget in a filled window.
const WidgetID index
Index of the nested widget (INVALID_WIDGET means 'not used').
virtual NWidgetBase * GetWidgetOfType(WidgetType tp)
Retrieve a widget by its type.
Definition widget.cpp:975
uint fill_y
Vertical fill stepsize (from initial size, 0 means not resizable).
uint resize_y
Vertical resize step (0 means not resizable).
virtual void SetHighlighted(TextColour highlight_colour)
Highlight the widget or not.
RectPadding padding
Padding added to the widget. Managed by parent container widget. (parent container may swap left and ...
virtual std::pair< uint, uint > GetPreferredSizeForSize(uint given_width, uint given_height)
Get a widget's preferred size for a given size.
virtual NWidgetCore * GetWidgetFromPos(int x, int y)=0
Retrieve a widget by its position.
uint current_y
Current vertical size (after resizing).
virtual void FillWidgetLookup(WidgetLookup &widget_lookup)
Fill the Window::widget_lookup with pointers to nested widgets in the tree.
Definition widget.cpp:939
uint GetVerticalStepSize(SizingType sizing) const
Get the vertical sizing step.
const NWID * GetParentWidget() const
Get parent widget of type NWID.
void Add(std::unique_ptr< NWidgetBase > &&wid)
Append widget wid to container.
Definition widget.cpp:1328
void FillWidgetLookup(WidgetLookup &widget_lookup) override
Fill the Window::widget_lookup with pointers to nested widgets in the tree.
Definition widget.cpp:1335
void UnfocusWidgets(Window *parent_window)
Unfocuses the focused widget of the window, if the focused widget is contained inside the container.
Definition widget.cpp:3542
void Draw(const Window *w) override
Draw the widgets of the tree.
Definition widget.cpp:1343
bool IsEmpty()
Return whether the container is empty.
std::vector< std::unique_ptr< NWidgetBase > > children
Child widgets in container.
void Clear(Window *parent_window)
Clears the container, deleting all widgets that were contained.
NWidgetCore * GetWidgetFromPos(int x, int y) override
Retrieve a widget by its position.
Definition widget.cpp:1352
void AdjustPaddingForZoom() override
Adjust the padding based on the user interface zoom.
Definition widget.cpp:1316
NWidgetBase * GetWidgetOfType(WidgetType tp) override
Retrieve a widget by its type.
Definition widget.cpp:1306
Base class for a 'real' widget.
WidgetData widget_data
Data of the widget.
void SetToolTip(StringID tool_tip)
Set the tool tip of the nested widget.
Definition widget.cpp:1260
bool IsHighlighted() const override
Whether the widget is currently highlighted or not.
bool IsDisabled() const
Return whether the widget is disabled.
void SetSprite(SpriteID sprite)
Set sprite of the nested widget.
Definition widget.cpp:1210
NWidgetDisplayFlags disp_flags
Flags that affect display and interaction with the widget.
void SetTextStyle(TextColour colour, FontSize size)
Set the text style of the nested widget.
Definition widget.cpp:1250
void SetAlignment(Alignment align)
Set the text/image alignment of the nested widget.
Definition widget.cpp:1278
WidgetID GetScrollbarIndex() const
Get the WidgetID of this nested widget's scrollbar.
Definition widget.cpp:1296
TextColour highlight_colour
Colour of highlight.
void SetResizeWidgetType(ResizeWidgetType type)
Set the resize widget type of the nested widget.
Definition widget.cpp:1240
Alignment align
Alignment of text/image within widget.
NWidgetCore(WidgetType tp, Colours colour, WidgetID index, uint fill_x, uint fill_y, const WidgetData &widget_data, StringID tool_tip)
Initialization of a 'real' widget.
Definition widget.cpp:1178
void SetSpriteTip(SpriteID sprite, StringID tool_tip)
Set sprite and tool tip of the nested widget.
Definition widget.cpp:1220
FontSize text_size
Size of text within widget.
void SetHighlighted(TextColour highlight_colour) override
Highlight the widget or not.
StringID GetString() const
Get the string that has been set for this nested widget.
Definition widget.cpp:1287
WidgetID scrollbar_index
Index of an attached scrollbar.
StringID GetToolTip() const
Get the tool tip of the nested widget.
Definition widget.cpp:1269
NWidgetCore * GetWidgetFromPos(int x, int y) override
Retrieve a widget by its position.
Definition widget.cpp:1301
void SetString(StringID string)
Set string of the nested widget.
Definition widget.cpp:1190
TextColour text_colour
Colour of text within widget.
Colours colour
Colour of this widget.
void SetMatrixDimension(uint32_t columns, uint32_t rows)
Set the matrix dimension.
Definition widget.cpp:1231
void SetLowered(bool lowered)
Lower or raise the widget.
TextColour GetHighlightColour() const override
Get the colour of the highlighted text.
void SetStringTip(StringID string, StringID tool_tip)
Set string and tool tip of the nested widget.
Definition widget.cpp:1200
StringID tool_tip
Tooltip of the widget.
void SetDisabled(bool disabled)
Disable (grey-out) or enable the widget.
bool IsLowered() const
Return whether the widget is lowered.
void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override
Assign size and position to the widget.
Definition widget.cpp:1755
void SetupSmallestSize(Window *w) override
Compute smallest size needed by the widget.
Definition widget.cpp:1571
void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override
Assign size and position to the widget.
Definition widget.cpp:1640
void Draw(const Window *w) override
Draw the widgets of the tree.
Definition widget.cpp:3014
static void InvalidateDimensionCache()
Reset the cached dimensions.
Definition widget.cpp:2691
static Dimension resizebox_dimension
Cached size of a resizebox widget.
static Dimension shadebox_dimension
Cached size of a shadebox widget.
static Dimension closebox_dimension
Cached size of a closebox widget.
static Dimension stickybox_dimension
Cached size of a stickybox widget.
NWidgetLeaf(WidgetType tp, Colours colour, WidgetID index, const WidgetData &data, StringID tip)
Nested leaf widget.
Definition widget.cpp:2718
static Dimension defsizebox_dimension
Cached size of a defsizebox widget.
void SetupSmallestSize(Window *w) override
Compute smallest size needed by the widget.
Definition widget.cpp:2827
static Dimension dropdown_dimension
Cached size of a dropdown widget.
static Dimension debugbox_dimension
Cached size of a debugbox widget.
bool ButtonHit(const Point &pt)
For a NWID_BUTTON_DROPDOWN, test whether pt refers to the button or to the drop-down.
Definition widget.cpp:3158
void Draw(const Window *w) override
Draw the widgets of the tree.
Definition widget.cpp:2110
int count
Amount of valid elements.
void SetClicked(int clicked)
Sets the clicked element in the matrix.
Definition widget.cpp:1979
int GetCurrentElement() const
Get current element.
Definition widget.cpp:2029
Scrollbar * sb
The scrollbar we're associated with.
void SetupSmallestSize(Window *w) override
Compute smallest size needed by the widget.
Definition widget.cpp:2034
int widget_w
The width of the child widget including inter spacing.
void SetScrollbar(Scrollbar *sb)
Assign a scrollbar to this matrix.
Definition widget.cpp:2020
void GetScrollOffsets(int &start_x, int &start_y, int &base_offs_x, int &base_offs_y)
Get the different offsets that are influenced by scrolling.
Definition widget.cpp:2165
int current_element
The element currently being processed.
int widgets_x
The number of visible widgets in horizontal direction.
int widget_h
The height of the child widget including inter spacing.
void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override
Assign size and position to the widget.
Definition widget.cpp:2056
NWidgetCore * GetWidgetFromPos(int x, int y) override
Retrieve a widget by its position.
Definition widget.cpp:2080
int clicked
The currently clicked element.
void SetCount(int count)
Set the number of elements in this matrix.
Definition widget.cpp:1996
int widgets_y
The number of visible widgets in vertical direction.
Colours colour
Colour of this widget.
uint8_t gaps
Number of gaps between widgets.
NWidContainerFlags flags
Flags of the container.
uint8_t pip_pre
Amount of space before first widget.
uint8_t uz_pip_pre
Unscaled space before first widget.
void SetPIPRatio(uint8_t pip_ratio_pre, uint8_t pip_ratio_inter, uint8_t pip_ratio_post)
Set additional pre/inter/post space for the container.
Definition widget.cpp:1564
uint8_t uz_pip_inter
Unscaled space between widgets.
uint8_t pip_ratio_pre
Ratio of remaining space before first widget.
void AdjustPaddingForZoom() override
Adjust the padding based on the user interface zoom.
Definition widget.cpp:1527
void SetPIP(uint8_t pip_pre, uint8_t pip_inter, uint8_t pip_post)
Set additional pre/inter/post space for the container.
Definition widget.cpp:1544
uint8_t pip_post
Amount of space after last widget.
uint8_t pip_ratio_inter
Ratio of remaining space between widgets.
uint8_t pip_ratio_post
Ratio of remaining space after last widget.
uint8_t uz_pip_post
Unscaled space after last widget.
uint8_t pip_inter
Amount of space between widgets.
uint8_t uz_text_spacing
'Unscaled' text padding, stored for resize calculation.
bool absolute
Set if minimum size is fixed and should not be resized.
void SetMinimalSize(uint min_x, uint min_y)
Set minimal size of the widget.
Definition widget.cpp:1053
bool UpdateSize(uint min_x, uint min_y)
Set absolute (post-scaling) minimal size of the widget.
Definition widget.cpp:1142
void AdjustPaddingForZoom() override
Adjust the padding based on the user interface zoom.
Definition widget.cpp:1035
uint min_x
Minimal horizontal size of only this widget.
FontSize uz_text_size
'Unscaled' font size, stored for resize calculation.
NWidgetResizeBase(WidgetType tp, WidgetID index, uint fill_x, uint fill_y)
Constructor for resizable nested widgets.
Definition widget.cpp:1006
uint uz_min_x
Unscaled Minimal horizontal size of only this widget.
uint8_t uz_text_lines
'Unscaled' text lines, stored for resize calculation.
void SetFill(uint fill_x, uint fill_y)
Set the filling of the widget from initial size.
Definition widget.cpp:1098
void SetToolbarMinimalSize(uint8_t toolbar_size)
Set minimal size of the widget in toolbar-icon-relative width.
Definition widget.cpp:1063
void SetMinimalTextLines(uint8_t min_lines, uint8_t spacing, FontSize size)
Set minimal text lines for the widget.
Definition widget.cpp:1086
uint min_y
Minimal vertical size of only this widget.
uint uz_min_y
Unscaled Minimal vertical size of only this widget.
bool UpdateMultilineWidgetSize(const std::string &str, int max_lines)
Try to set optimum widget size for a multiline text widget.
Definition widget.cpp:1122
void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override
Assign size and position to the widget.
Definition widget.cpp:1163
bool UpdateVerticalSize(uint min_y)
Set absolute (post-scaling) minimal size of the widget.
Definition widget.cpp:1156
void SetResize(uint resize_x, uint resize_y)
Set resize step of the widget.
Definition widget.cpp:1109
uint8_t toolbar_size
Minimal size in terms of toolbar images.
void SetAspect(float ratio, AspectFlags flags=AspectFlag::ResizeX)
Set desired aspect ratio of this widget.
Definition widget.cpp:1017
void SetMinimalSizeAbsolute(uint min_x, uint min_y)
Set absolute (post-scaling) minimal size of the widget.
Definition widget.cpp:1073
void Draw(const Window *w) override
Draw the widgets of the tree.
Definition widget.cpp:2635
NWidgetScrollbar(WidgetType tp, Colours colour, WidgetID index)
Scrollbar widget.
Definition widget.cpp:2593
void SetupSmallestSize(Window *w) override
Compute smallest size needed by the widget.
Definition widget.cpp:2614
static Dimension horizontal_dimension
Cached size of horizontal scrollbar button.
static Dimension vertical_dimension
Cached size of vertical scrollbar button.
NWidgetSpacer(int width, int height)
Generic spacer widget.
Definition widget.cpp:1940
NWidgetCore * GetWidgetFromPos(int x, int y) override
Retrieve a widget by its position.
Definition widget.cpp:1970
void SetDirty(const Window *w) const override
Mark the widget as 'dirty' (in need of repaint).
Definition widget.cpp:1965
void Draw(const Window *w) override
Draw the widgets of the tree.
Definition widget.cpp:1953
void SetupSmallestSize(Window *w) override
Compute smallest size needed by the widget.
Definition widget.cpp:1946
Stacked widgets, widgets all occupying the same space in the window.
int shown_plane
Plane being displayed (for NWID_SELECTION only).
void SetupSmallestSize(Window *w) override
Compute smallest size needed by the widget.
Definition widget.cpp:1363
WidgetLookup * widget_lookup
Window's widget lookup, updated in SetDisplayedPlane().
void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override
Assign size and position to the widget.
Definition widget.cpp:1404
void Draw(const Window *w) override
Draw the widgets of the tree.
Definition widget.cpp:1434
bool SetDisplayedPlane(int plane)
Select which plane to show (for NWID_SELECTION only).
Definition widget.cpp:1458
void FillWidgetLookup(WidgetLookup &widget_lookup) override
Fill the Window::widget_lookup with pointers to nested widgets in the tree.
Definition widget.cpp:1424
NWidgetCore * GetWidgetFromPos(int x, int y) override
Retrieve a widget by its position.
Definition widget.cpp:1443
void AssignSizePosition(SizingType sizing, int x, int y, uint given_width, uint given_height, bool rtl) override
Assign size and position to the widget.
Definition widget.cpp:1829
void SetupSmallestSize(Window *w) override
Compute smallest size needed by the widget.
Definition widget.cpp:1760
bool bottom_up
Set to flow the widget from bottom-to-top instead of top-to-bottom.
void UpdateViewportCoordinates(Window *w)
Update the position and size of the viewport (after eg a resize).
Definition widget.cpp:2451
void Draw(const Window *w) override
Draw the widgets of the tree.
Definition widget.cpp:2416
void SetupSmallestSize(Window *w) override
Compute smallest size needed by the widget.
Definition widget.cpp:2409
void InitializeViewport(Window *w, std::variant< TileIndex, VehicleID > focus, ZoomLevel zoom)
Initialize the viewport of the window.
Definition widget.cpp:2442
Scrollbar data structure.
bool IsVisible(size_type item) const
Checks whether given current item is visible in the list.
const bool is_vertical
Scrollbar has vertical orientation.
size_type GetCapacity() const
Gets the number of visible elements of the scrollbar.
size_type stepsize
Distance to scroll, when pressing the buttons or using the wheel.
void SetCount(size_t num)
Sets the number of elements in the list.
bool IsVertical() const
Is the scrollbar vertical or not?
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.
bool UpdatePosition(int difference, Scrollbar::Stepping unit=Stepping::Small)
Updates the position of the first visible element by the given amount.
Stepping
Stepping sizes when scrolling.
@ Small
Step in stepsize units.
@ Single
Step in single units.
@ Big
Step in cap units.
void SetCapacity(size_t capacity)
Set the capacity of visible elements.
size_type cap
Number of visible elements of the scroll bar.
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:2474
size_type count
Number of elements in the list.
bool SetPosition(size_type position)
Sets the position of the first visible element.
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:2548
size_type GetCount() const
Gets the number of elements in the list.
EventState UpdateListPositionOnKeyPress(int &list_position, uint16_t keycode) const
Update the given list position as if it were on this scroll bar when the given keycode was pressed.
Definition widget.cpp:2495
void ScrollTowards(size_type position)
Scroll towards the given position; if the item is visible nothing happens, otherwise it will be shown...
auto GetVisibleRangeIterators(Tcontainer &container) const
Get a pair of iterators for the range of visible elements in a container.
void SetStepSize(size_t stepsize)
Set the distance to scroll when using the buttons or the wheel.
size_type pos
Index of first visible item of the list.
size_type GetPosition() const
Gets the position of the first visible element in the list.
@ Centre
Align to the centre.
@ Middle
Align to the middle.
Types related to the graphics and/or input devices.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
FontSize
Available font sizes.
Definition gfx_type.h:248
@ Normal
Index of the normal font in the font tables.
Definition gfx_type.h:249
Colours
One of 16 base colours used for companies and windows/widgets.
Definition gfx_type.h:283
@ Invalid
Invalid marker.
Definition gfx_type.h:302
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:315
@ Invalid
Invalid colour.
Definition gfx_type.h:336
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 NWidgetFunction(NWidgetFunctionType *func_ptr)
Obtain a nested widget (sub)tree from an external source.
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 SetResizeWidgetTypeTip(ResizeWidgetType widget_type, StringID tip)
Widget part function for setting the resize widget type and tooltip.
constexpr NWidgetPart SetToolbarMinimalSize(int width)
Widget part function to setting the minimal size for a toolbar button.
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 SetAlternateColourTip(Colours colour, StringID tip)
Widget part function for setting the alternate colour and tooltip.
constexpr NWidgetPart SetStringTip(StringID string, StringID tip={})
Widget part function for setting the string and tooltip.
constexpr NWidgetPart SetAspect(float ratio, AspectFlags flags=AspectFlag::ResizeX)
Widget part function for setting the aspect ratio.
std::unique_ptr< NWidgetBase > MakeWindowNWidgetTree(std::span< const NWidgetPart > nwid_parts, NWidgetStacked **shade_select)
Make a nested widget tree for a window from a parts array.
Definition widget.cpp:3450
constexpr NWidgetPart SetMinimalTextLines(uint8_t lines, uint8_t spacing, FontSize size=FontSize::Normal)
Widget part function for setting the minimal text lines.
constexpr NWidgetPart SetToolbarSpacerMinimalSize()
Widget part function to setting the minimal size for a toolbar spacer.
constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
Widget part function for setting the minimal size.
std::unique_ptr< NWidgetBase > MakeNWidgets(std::span< const NWidgetPart > nwid_parts, std::unique_ptr< NWidgetBase > &&container)
Construct a nested widget tree from an array of parts.
Definition widget.cpp:3431
constexpr NWidgetPart SetSpriteStringTip(SpriteID sprite, StringID string, StringID tip={})
Widget part function for setting the sprite, string and tooltip.
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 SetTextStyle(TextColour colour, FontSize size=FontSize::Normal)
Widget part function for setting the text style.
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=INVALID_WIDGET)
Widget part function for starting a new 'real' widget.
constexpr NWidgetPart SetArrowWidgetTypeTip(ArrowWidgetType widget_type, StringID tip={})
Widget part function for setting the arrow widget type and tooltip.
constexpr NWidgetPart SetAlignment(Alignment align)
Widget part function for setting the alignment of text/images.
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
constexpr NWidgetPart SetPIPRatio(uint8_t ratio_pre, uint8_t ratio_inter, uint8_t ratio_post)
Widget part function for setting a pre/inter/post ratio.
#define Rect
Macro that prevents name conflicts between included headers.
#define Point
Macro that prevents name conflicts between included headers.
Integer math functions.
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
constexpr To ClampTo(From value)
Clamp the given value down to lie within the requested type.
Types related to strings.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
Horizontal and vertical alignment.
Dimensions (a width and height) of a rectangle in 2D.
Widget part for setting text/image alignment within a widget.
Alignment align
Alignment of text/image.
Widget part for storing data and tooltip information.
WidgetData data
Data value of the widget.
StringID tooltip
Tooltip of the widget.
Widget part for storing pre/inter/post spaces.
uint8_t post
Amount of space before/between/after child widgets.
Widget part for storing padding.
Widget part for storing minimal text line data.
uint8_t lines
Number of text lines.
uint8_t spacing
Extra spacing around lines.
FontSize size
Font size of text lines.
Widget part for storing text colour.
TextColour colour
TextColour for DrawString.
FontSize size
Font size of text.
Widget part for storing basic widget information.
Colours colour
Widget colour.
WidgetID index
Index of the widget.
Partial widget specification to allow NWidgets to be written nested.
WidgetType type
Type of the part.
Padding dimensions to apply to each side of a Rect.
Container with the data associated to a single widget.
Data structure for an opened window.
Definition window_gui.h:273
NWidgetPartContainer container
Part with container flags.
NWidgetPartTextLines text_lines
Part with text line data.
NWidgetPartPIP pip
Part with pre/inter/post spaces.
NWidgetPartPaddings padding
Part with paddings.
NWidgetFunctionType * func_ptr
Part with a function call.
NWidgetPartDataTip data_tip
Part with a data/tooltip.
NWidgetPartAlignment align
Part with internal alignment.
NWidgetPartAspect aspect
Part to set aspect ratio.
NWidgetPartTextStyle text_style
Part with text style data.
Point xy
Part with an x/y size.
NWidgetPartWidget widget
Part with a start of a widget.
void ApplyNWidgetPartAttribute(const NWidgetPart &nwid, NWidgetBase *dest)
Apply an attribute NWidgetPart to an NWidget.
Definition widget.cpp:3187
EnumBitSet< AspectFlag, uint8_t > AspectFlags
Bitset of AspectFlag elements.
WidgetType
Window widget types, nested widget types, and nested widget part types.
Definition widget_type.h:35
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
@ WWT_INSET
Pressed (inset) panel, most commonly used as combo box text area.
Definition widget_type.h:40
@ WPT_FILL
Widget part for specifying fill.
Definition widget_type.h:84
@ WWT_PUSHIMGTEXTBTN
Normal push-button (no toggle button) with image and text caption.
@ WPT_ALIGNMENT
Widget part for specifying text/image alignment.
Definition widget_type.h:90
@ WWT_IMGBTN
(Toggle) Button with image
Definition widget_type.h:41
@ WWT_PUSHBTN
Normal push-button (no toggle button) with custom drawing.
@ WWT_IMGBTN_2
(Toggle) Button with diff image when clicked
Definition widget_type.h:42
@ NWID_CUSTOM
General Custom widget.
Definition widget_type.h:77
@ WWT_PUSHIMGBTN
Normal push-button (no toggle button) with image caption.
@ WPT_MINSIZE
Widget part for specifying minimal size.
Definition widget_type.h:82
@ WWT_PUSHARROWBTN
Normal push-button (no toggle button) with arrow caption.
@ WWT_LABEL
Centered label.
Definition widget_type.h:48
@ NWID_BUTTON_DROPDOWN
Button with a drop-down.
Definition widget_type.h:74
@ NWID_SPACER
Invisible widget that takes some space.
Definition widget_type.h:70
@ WWT_EDITBOX
a textbox for typing
Definition widget_type.h:62
@ WWT_ARROWBTN
(Toggle) Button with an arrow
Definition widget_type.h:43
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:66
@ WWT_TEXTBTN
(Toggle) Button with text
Definition widget_type.h:44
@ WPT_SCROLLBAR
Widget part for attaching a scrollbar.
Definition widget_type.h:91
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:39
@ WPT_ASPECT
Widget part for specifying aspect ratio.
Definition widget_type.h:92
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX).
Definition widget_type.h:57
@ WPT_RESIZE
Widget part for specifying resizing.
Definition widget_type.h:81
@ WWT_IMGTEXTBTN
(Toggle) Button with image and text
Definition widget_type.h:47
@ 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
@ WPT_TOOLBARSIZE
Widget part for specifying minimal size in terms of toolbar images.
Definition widget_type.h:93
@ WWT_CAPTION
Window caption (window title between closebox and stickybox).
Definition widget_type.h:52
@ NWID_VSCROLLBAR
Vertical scrollbar.
Definition widget_type.h:76
@ WPT_TEXTSTYLE
Widget part for specifying text colour.
Definition widget_type.h:89
@ WPT_PADDING
Widget part for specifying a padding.
Definition widget_type.h:86
@ WWT_BOOLBTN
Standard boolean toggle button.
Definition widget_type.h:46
@ 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:45
@ WWT_FRAME
Frame.
Definition widget_type.h:51
@ WPT_MINTEXTLINES
Widget part for specifying minimal number of lines of text.
Definition widget_type.h:83
@ WWT_EMPTY
Empty widget, place holder to reserve space in widget tree.
Definition widget_type.h:37
@ WWT_LAST
Last Item. use WIDGETS_END to fill up padding!!
Definition widget_type.h:63
@ WPT_ATTRIBUTE_END
End marker for attribute NWidgetPart types.
Definition widget_type.h:94
@ NWID_HSCROLLBAR
Horizontal scrollbar.
Definition widget_type.h:75
@ WPT_ENDCONTAINER
Widget part to denote end of a container.
Definition widget_type.h:97
@ WWT_RESIZEBOX
Resize box (normally at bottom-right of a window).
Definition widget_type.h:59
@ WPT_PIPRATIO
Widget part for specifying pre/inter/post ratio for containers.
Definition widget_type.h:88
@ WPT_DATATIP
Widget part for specifying data and tooltip.
Definition widget_type.h:85
@ WWT_DEFSIZEBOX
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX).
Definition widget_type.h:56
@ WPT_ATTRIBUTE_BEGIN
Begin marker for attribute NWidgetPart types.
Definition widget_type.h:80
@ WPT_PIPSPACE
Widget part for specifying pre/inter/post space for containers.
Definition widget_type.h:87
@ NWID_MATRIX
Matrix container.
Definition widget_type.h:69
@ NWID_VIEWPORT
Nested widget containing a viewport.
Definition widget_type.h:73
@ WWT_DROPDOWN
Drop down list.
Definition widget_type.h:61
@ WWT_TEXT
Pure simple text.
Definition widget_type.h:49
@ NWID_HORIZONTAL_LTR
Horizontal container that doesn't change the order of the widgets for RTL languages.
Definition widget_type.h:67
@ WPT_FUNCTION
Widget part for calling a user function.
Definition widget_type.h:96
@ WWT_DEBUGBOX
NewGRF debug box (at top-right of a window, between WWT_CAPTION and WWT_SHADEBOX).
Definition widget_type.h:54
@ NWID_LAYER
Layered widgets, all visible together.
Definition widget_type.h:72
@ NWID_SELECTION
Stacked widgets, only one visible at a time (eg in a panel with tabs).
Definition widget_type.h:71
NWidgetDisplayFlag
Nested widget flags that affect display and interaction with 'real' widgets.
@ Highlight
Highlight of widget is on.
@ ScrollbarDown
Down-button is lowered bit.
@ ShadeGrey
Shade viewport to grey-scale.
@ NoTransparency
Viewport is never transparent.
@ ShadeDimmed
Display dimmed colours in the viewport.
@ Disabled
Widget is disabled (greyed out) bit.
@ DropdownClosed
Dropdown menu of the dropdown widget has closed.
@ Lowered
Widget is lowered (pressed down) bit.
@ DropdownActive
Dropdown menu of the button dropdown widget is active.
@ ScrollbarUp
Up-button is lowered bit.
uint ComputeMaxSize(uint base, uint max_space, uint step)
Return the biggest possible size of a nested widget.
bool IsContainerWidgetType(WidgetType tp)
Test if WidgetType is a container widget.
Definition widget.cpp:3374
StackedZeroSizePlanes
Display planes with zero size for NWidgetStacked.
@ SZSP_HORIZONTAL
Display plane with zero size vertically, and filling and resizing horizontally.
@ SZSP_BEGIN
First zero-size plane.
@ SZSP_VERTICAL
Display plane with zero size horizontally, and filling and resizing vertically.
@ SZSP_NONE
Display plane with zero size in both directions (none filling and resizing).
NWidContainerFlag
Nested widget container flags,.
@ EqualSize
Containers should keep all their (resizing) children equally large.
@ BigFirst
Allocate space to biggest resize first.
std::unique_ptr< NWidgetBase >() NWidgetFunctionType
Pointer to function returning a nested widget.
std::unique_ptr< NWidgetBase > MakeCompanyButtonRows(WidgetID widget_first, WidgetID widget_last, Colours button_colour, int max_length, StringID button_tooltip, bool resizable=true)
Make a number of rows with button-like graphics, for enabling/disabling each company.
Definition widget.cpp:3492
EnumBitSet< NWidContainerFlag, uint8_t > NWidContainerFlags
Bitset of NWidContainerFlag elements.
void SetupWidgetDimensions()
Set up pre-scaled versions of Widget Dimensions.
Definition widget.cpp:98
SizingType
Different forms of sizing nested widgets, using NWidgetBase::AssignSizePosition().
@ Resize
Resize the nested widget tree.
@ Smallest
Initialize nested widget tree to smallest size. Also updates current_x and current_y.
ArrowWidgetType
Values for an arrow widget.
Definition widget_type.h:19
@ Right
Force the arrow to the right.
Definition widget_type.h:23
@ Left
Force the arrow to the left.
Definition widget_type.h:22
@ Decrease
Arrow to the left or in case of RTL to the right.
Definition widget_type.h:20
@ Increase
Arrow to the right or in case of RTL to the left.
Definition widget_type.h:21
std::map< WidgetID, class NWidgetBase * > WidgetLookup
Lookup between widget IDs and NWidget objects.
ResizeWidgetType
WidgetData values for a resize box widget.
Definition widget_type.h:27
@ HideBevel
Bevel of resize box is hidden.
Definition widget_type.h:29
@ ShowBevel
Bevel of resize box is shown.
Definition widget_type.h:28
EnumBitSet< NWidgetDisplayFlag, uint16_t > NWidgetDisplayFlags
Bitset of NWidgetDisplayFlag elements.
AspectFlag
Flags to control how a widgeet is resized to reach its aspect ratio.
@ ResizeY
Resize vertically to reach desired aspect ratio.
@ ResizeX
Resize horizontally to reach desired aspect ratio.
Types related to windows.
int WidgetID
Widget ID.
Definition window_type.h:21
EventState
State of handling an event.
static constexpr WidgetID INVALID_WIDGET
An invalid widget index.
Definition window_type.h:24
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:20