OpenTTD Source: src/dropdown_common_type.h Source File

OpenTTD Source 15.0-beta1

dropdown_common_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 <http://www.gnu.org/licenses/>.
6 */
7
10#ifndef DROPDOWN_COMMON_TYPE_H
11#define DROPDOWN_COMMON_TYPE_H
12
13#include "gfx_func.h"
14#include "gfx_type.h"
15#include "palette_func.h"
16#include "string_func.h"
17#include "strings_func.h"
18#include "table/strings.h"
19#include "window_gui.h"
20
26template <class TBase, FontSize TFs = FS_NORMAL>
27 class DropDownDivider : public TBase {
28public:
29 template <typename... Args>
30 explicit DropDownDivider(Args&&... args) : TBase(std::forward<Args>(args)...) {}
31
32 bool Selectable() const override { return false; }
33 uint Height() const override { return std::max<uint>(GetCharacterHeight(TFs), this->TBase::Height()); }
34
35 void Draw(const Rect &full, const Rect &, bool, Colours bg_colour) const override
36 {
37 uint8_t c1 = GetColourGradient(bg_colour, SHADE_DARK);
38 uint8_t c2 = GetColourGradient(bg_colour, SHADE_LIGHTEST);
39
40 int mid = CenterBounds(full.top, full.bottom, 0);
41 GfxFillRect(full.left, mid - WidgetDimensions::scaled.bevel.bottom, full.right, mid - 1, c1);
42 GfxFillRect(full.left, mid, full.right, mid + WidgetDimensions::scaled.bevel.top - 1, c2);
43 }
44};
45
52template <class TBase, FontSize TFs = FS_NORMAL, bool TEnd = false>
53 class DropDownString : public TBase {
54 std::string string;
55 Dimension dim;
56public:
57 template <typename... Args>
58 explicit DropDownString(StringID string, Args&&... args) : TBase(std::forward<Args>(args)...)
59 {
60 this->SetString(GetString(string));
61 }
62
63 template <typename... Args>
64 explicit DropDownString(const std::string &string, Args&&... args) : TBase(std::forward<Args>(args)...)
65 {
66 SetDParamStr(0, string);
67 this->SetString(GetString(STR_JUST_RAW_STRING));
68 }
69
70 void SetString(std::string &&string)
71 {
72 this->string = std::move(string);
73 this->dim = GetStringBoundingBox(this->string, TFs);
74 }
75
76 uint Height() const override
77 {
78 return std::max<uint>(this->dim.height, this->TBase::Height());
79 }
80
81 uint Width() const override { return this->dim.width + this->TBase::Width(); }
82
83 void Draw(const Rect &full, const Rect &r, bool sel, Colours bg_colour) const override
84 {
85 bool rtl = TEnd ^ (_current_text_dir == TD_RTL);
86 DrawStringMultiLine(r.WithWidth(this->dim.width, rtl), this->string, this->GetColour(sel), SA_CENTER, false, TFs);
87 this->TBase::Draw(full, r.Indent(this->dim.width, rtl), sel, bg_colour);
88 }
89
97 static bool NatSortFunc(std::unique_ptr<const DropDownListItem> const &first, std::unique_ptr<const DropDownListItem> const &second)
98 {
99 const std::string &str1 = static_cast<const DropDownString*>(first.get())->string;
100 const std::string &str2 = static_cast<const DropDownString*>(second.get())->string;
101 return StrNaturalCompare(str1, str2) < 0;
102 }
103};
104
110template <class TBase, bool TEnd = false>
111 class DropDownIcon : public TBase {
112 SpriteID sprite;
113 PaletteID palette;
114 Dimension dsprite;
115 Dimension dbounds;
116public:
117 template <typename... Args>
118 explicit DropDownIcon(SpriteID sprite, PaletteID palette, Args&&... args) : TBase(std::forward<Args>(args)...), sprite(sprite), palette(palette)
119 {
120 this->dsprite = GetSpriteSize(this->sprite);
121 this->dbounds = this->dsprite;
122 }
123
124 template <typename... Args>
125 explicit DropDownIcon(const Dimension &dim, SpriteID sprite, PaletteID palette, Args&&... args) : TBase(std::forward<Args>(args)...), sprite(sprite), palette(palette), dbounds(dim)
126 {
127 this->dsprite = GetSpriteSize(this->sprite);
128 }
129
130 uint Height() const override { return std::max(this->dbounds.height, this->TBase::Height()); }
131 uint Width() const override { return this->dbounds.width + WidgetDimensions::scaled.hsep_normal + this->TBase::Width(); }
132
133 void Draw(const Rect &full, const Rect &r, bool sel, Colours bg_colour) const override
134 {
135 bool rtl = TEnd ^ (_current_text_dir == TD_RTL);
136 Rect ir = r.WithWidth(this->dbounds.width, rtl);
137 DrawSprite(this->sprite, this->palette, CenterBounds(ir.left, ir.right, this->dsprite.width), CenterBounds(r.top, r.bottom, this->dsprite.height));
138 this->TBase::Draw(full, r.Indent(this->dbounds.width + WidgetDimensions::scaled.hsep_normal, rtl), sel, bg_colour);
139 }
140};
141
148template <class TBase, bool TEnd = false, FontSize TFs = FS_NORMAL>
149 class DropDownCheck : public TBase {
150 bool checked;
151 Dimension dim;
152public:
153 template <typename... Args>
154 explicit DropDownCheck(bool checked, Args&&... args) : TBase(std::forward<Args>(args)...), checked(checked)
155 {
156 this->dim = GetStringBoundingBox(STR_JUST_CHECKMARK, TFs);
157 }
158
159 uint Height() const override { return std::max<uint>(this->dim.height, this->TBase::Height()); }
160 uint Width() const override { return this->dim.width + WidgetDimensions::scaled.hsep_wide + this->TBase::Width(); }
161
162 void Draw(const Rect &full, const Rect &r, bool sel, Colours bg_colour) const override
163 {
164 bool rtl = TEnd ^ (_current_text_dir == TD_RTL);
165 if (this->checked) {
166 DrawStringMultiLine(r.WithWidth(this->dim.width, rtl), STR_JUST_CHECKMARK, this->GetColour(sel), SA_CENTER, false, TFs);
167 }
168 this->TBase::Draw(full, r.Indent(this->dim.width + WidgetDimensions::scaled.hsep_wide, rtl), sel, bg_colour);
169 }
170};
171
172/* Commonly used drop down list items. */
173using DropDownListDividerItem = DropDownDivider<DropDownListItem>;
174using DropDownListStringItem = DropDownString<DropDownListItem>;
175using DropDownListIconItem = DropDownIcon<DropDownString<DropDownListItem>>;
176using DropDownListCheckedItem = DropDownCheck<DropDownString<DropDownListItem>>;
177
178#endif /* DROPDOWN_COMMON_TYPE_H */
DropDownCheck
Drop down checkmark component.
Definition dropdown_common_type.h:149
DropDownCheck::dim
Dimension dim
Dimension of checkmark.
Definition dropdown_common_type.h:151
DropDownCheck::checked
bool checked
Is item checked.
Definition dropdown_common_type.h:150
DropDownDivider
Drop down divider component.
Definition dropdown_common_type.h:27
DropDownIcon
Drop down icon component.
Definition dropdown_common_type.h:111
DropDownIcon::palette
PaletteID palette
Palette ID to use.
Definition dropdown_common_type.h:113
DropDownIcon::dbounds
Dimension dbounds
Bounding box dimensions of bounds.
Definition dropdown_common_type.h:115
DropDownIcon::dsprite
Dimension dsprite
Bounding box dimensions of sprite.
Definition dropdown_common_type.h:114
DropDownIcon::sprite
SpriteID sprite
Sprite ID to be drawn.
Definition dropdown_common_type.h:112
DropDownString
Drop down string component.
Definition dropdown_common_type.h:53
DropDownString::string
std::string string
String to be drawn.
Definition dropdown_common_type.h:54
DropDownString::NatSortFunc
static bool NatSortFunc(std::unique_ptr< const DropDownListItem > const &first, std::unique_ptr< const DropDownListItem > const &second)
Natural sorting comparator function for DropDownList::sort().
Definition dropdown_common_type.h:97
DropDownString::dim
Dimension dim
Dimensions of string.
Definition dropdown_common_type.h:55
WidgetDimensions::scaled
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:28
WidgetDimensions::hsep_wide
int hsep_wide
Wide horizontal spacing.
Definition window_gui.h:64
WidgetDimensions::hsep_normal
int hsep_normal
Normal horizontal spacing.
Definition window_gui.h:63
WidgetDimensions::bevel
RectPadding bevel
Bevel thickness, affected by "scaled bevels" game option.
Definition window_gui.h:40
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:77
GetSpriteSize
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition gfx.cpp:922
GetStringBoundingBox
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition gfx.cpp:851
GfxFillRect
void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
Definition gfx.cpp:114
DrawSprite
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
DrawStringMultiLine
int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition gfx.cpp:774
gfx_func.h
Functions related to the gfx engine.
CenterBounds
int CenterBounds(int min, int max, int size)
Determine where to draw a centred object inside a widget.
Definition gfx_func.h:166
gfx_type.h
Types related to the graphics and/or input devices.
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:18
SA_CENTER
@ SA_CENTER
Center both horizontally and vertically.
Definition gfx_type.h:353
PaletteID
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:19
GetColourGradient
uint8_t GetColourGradient(Colours colour, ColourShade shade)
Get colour gradient palette index.
Definition palette.cpp:314
palette_func.h
Functions related to palettes.
StrNaturalCompare
int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Definition string.cpp:589
string_func.h
Functions related to low-level strings.
GetString
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
Definition strings.cpp:333
_current_text_dir
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:56
SetDParamStr
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:371
strings_func.h
Functions related to OTTD's strings.
TD_RTL
@ TD_RTL
Text is written right-to-left by default.
Definition strings_type.h:24
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:16
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition geometry_type.hpp:30
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:75
Rect::WithWidth
Rect WithWidth(int width, bool end) const
Copy Rect and set its width.
Definition geometry_type.hpp:185
Rect::Indent
Rect Indent(int indent, bool end) const
Copy Rect and indent it from its position.
Definition geometry_type.hpp:198
Height
int16_t Height
Fixed point type for heights.
Definition tgp.cpp:153
window_gui.h
Functions, definitions and such used only by the GUI.