OpenTTD Source 15.0-beta2
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 "window_gui.h"
19
25template <class TBase, FontSize TFs = FS_NORMAL>
26class DropDownDivider : public TBase {
27public:
28 template <typename... Args>
29 explicit DropDownDivider(Args&&... args) : TBase(std::forward<Args>(args)...) {}
30
31 bool Selectable() const override { return false; }
32 uint Height() const override { return std::max<uint>(GetCharacterHeight(TFs), this->TBase::Height()); }
33
34 void Draw(const Rect &full, const Rect &, bool, Colours bg_colour) const override
35 {
36 uint8_t c1 = GetColourGradient(bg_colour, SHADE_DARK);
37 uint8_t c2 = GetColourGradient(bg_colour, SHADE_LIGHTEST);
38
39 int mid = CenterBounds(full.top, full.bottom, 0);
40 GfxFillRect(full.left, mid - WidgetDimensions::scaled.bevel.bottom, full.right, mid - 1, c1);
41 GfxFillRect(full.left, mid, full.right, mid + WidgetDimensions::scaled.bevel.top - 1, c2);
42 }
43};
44
51template <class TBase, FontSize TFs = FS_NORMAL, bool TEnd = false>
52class DropDownString : public TBase {
53 std::string string;
55public:
56 template <typename... Args>
57 explicit DropDownString(std::string &&string, Args&&... args) : TBase(std::forward<Args>(args)...)
58 {
59 this->SetString(std::move(string));
60 }
61
62 void SetString(std::string &&string)
63 {
64 this->string = std::move(string);
65 this->dim = GetStringBoundingBox(this->string, TFs);
66 }
67
68 uint Height() const override
69 {
70 return std::max<uint>(this->dim.height, this->TBase::Height());
71 }
72
73 uint Width() const override { return this->dim.width + this->TBase::Width(); }
74
75 void Draw(const Rect &full, const Rect &r, bool sel, Colours bg_colour) const override
76 {
77 bool rtl = TEnd ^ (_current_text_dir == TD_RTL);
78 DrawStringMultiLine(r.WithWidth(this->dim.width, rtl), this->string, this->GetColour(sel), SA_CENTER, false, TFs);
79 this->TBase::Draw(full, r.Indent(this->dim.width, rtl), sel, bg_colour);
80 }
81
89 static bool NatSortFunc(std::unique_ptr<const DropDownListItem> const &first, std::unique_ptr<const DropDownListItem> const &second)
90 {
91 const std::string &str1 = static_cast<const DropDownString*>(first.get())->string;
92 const std::string &str2 = static_cast<const DropDownString*>(second.get())->string;
93 return StrNaturalCompare(str1, str2) < 0;
94 }
95};
96
102template <class TBase, bool TEnd = false>
103class DropDownIcon : public TBase {
108public:
109 template <typename... Args>
110 explicit DropDownIcon(SpriteID sprite, PaletteID palette, Args&&... args) : TBase(std::forward<Args>(args)...), sprite(sprite), palette(palette)
111 {
112 this->dsprite = GetSpriteSize(this->sprite);
113 this->dbounds = this->dsprite;
114 }
115
116 template <typename... Args>
117 explicit DropDownIcon(const Dimension &dim, SpriteID sprite, PaletteID palette, Args&&... args) : TBase(std::forward<Args>(args)...), sprite(sprite), palette(palette), dbounds(dim)
118 {
119 this->dsprite = GetSpriteSize(this->sprite);
120 }
121
122 uint Height() const override { return std::max(this->dbounds.height, this->TBase::Height()); }
123 uint Width() const override { return this->dbounds.width + WidgetDimensions::scaled.hsep_normal + this->TBase::Width(); }
124
125 void Draw(const Rect &full, const Rect &r, bool sel, Colours bg_colour) const override
126 {
127 bool rtl = TEnd ^ (_current_text_dir == TD_RTL);
128 Rect ir = r.WithWidth(this->dbounds.width, rtl);
129 DrawSprite(this->sprite, this->palette, CenterBounds(ir.left, ir.right, this->dsprite.width), CenterBounds(r.top, r.bottom, this->dsprite.height));
130 this->TBase::Draw(full, r.Indent(this->dbounds.width + WidgetDimensions::scaled.hsep_normal, rtl), sel, bg_colour);
131 }
132};
133
140template <class TBase, bool TEnd = false, FontSize TFs = FS_NORMAL>
141class DropDownCheck : public TBase {
142 bool checked;
144public:
145 template <typename... Args>
146 explicit DropDownCheck(bool checked, Args&&... args) : TBase(std::forward<Args>(args)...), checked(checked)
147 {
148 this->dim = GetStringBoundingBox(STR_JUST_CHECKMARK, TFs);
149 }
150
151 uint Height() const override { return std::max<uint>(this->dim.height, this->TBase::Height()); }
152 uint Width() const override { return this->dim.width + WidgetDimensions::scaled.hsep_wide + this->TBase::Width(); }
153
154 void Draw(const Rect &full, const Rect &r, bool sel, Colours bg_colour) const override
155 {
156 bool rtl = TEnd ^ (_current_text_dir == TD_RTL);
157 if (this->checked) {
158 DrawStringMultiLine(r.WithWidth(this->dim.width, rtl), STR_JUST_CHECKMARK, this->GetColour(sel), SA_CENTER, false, TFs);
159 }
160 this->TBase::Draw(full, r.Indent(this->dim.width + WidgetDimensions::scaled.hsep_wide, rtl), sel, bg_colour);
161 }
162};
163
169template <class TBase, bool TEnd = false>
170class DropDownIndent : public TBase {
171 uint indent;
172public:
173 template <typename... Args>
174 explicit DropDownIndent(uint indent, Args&&... args) : TBase(std::forward<Args>(args)...), indent(indent) {}
175
176 uint Width() const override { return this->indent * WidgetDimensions::scaled.hsep_indent + this->TBase::Width(); }
177
178 void Draw(const Rect &full, const Rect &r, bool sel, Colours bg_colour) const override
179 {
180 bool rtl = TEnd ^ (_current_text_dir == TD_RTL);
181 this->TBase::Draw(full, r.Indent(this->indent * WidgetDimensions::scaled.hsep_indent, rtl), sel, bg_colour);
182 }
183};
184
185/* Commonly used drop down list items. */
190
191#endif /* DROPDOWN_COMMON_TYPE_H */
Drop down checkmark component.
Dimension dim
Dimension of checkmark.
bool checked
Is item checked.
Drop down divider component.
Drop down icon component.
PaletteID palette
Palette ID to use.
Dimension dbounds
Bounding box dimensions of bounds.
Dimension dsprite
Bounding box dimensions of sprite.
SpriteID sprite
Sprite ID to be drawn.
Drop down indent component.
Drop down string component.
std::string string
String to be drawn.
static bool NatSortFunc(std::unique_ptr< const DropDownListItem > const &first, std::unique_ptr< const DropDownListItem > const &second)
Natural sorting comparator function for DropDownList::sort().
Dimension dim
Dimensions of string.
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:29
int hsep_wide
Wide horizontal spacing.
Definition window_gui.h:62
int hsep_normal
Normal horizontal spacing.
Definition window_gui.h:61
RectPadding bevel
Bevel thickness, affected by "scaled bevels" game option.
Definition window_gui.h:38
int hsep_indent
Width of indentation for tree layouts.
Definition window_gui.h:63
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:77
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition gfx.cpp:923
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition gfx.cpp:852
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:115
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
Definition gfx.cpp:989
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:775
Functions related to the gfx engine.
int CenterBounds(int min, int max, int size)
Determine where to draw a centred object inside a widget.
Definition gfx_func.h:166
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
@ SA_CENTER
Center both horizontally and vertically.
Definition gfx_type.h:387
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
uint8_t GetColourGradient(Colours colour, ColourShade shade)
Get colour gradient palette index.
Definition palette.cpp:387
Functions related to palettes.
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:514
Functions related to low-level strings.
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition strings.cpp:57
Functions related to OTTD's strings.
@ TD_RTL
Text is written right-to-left by default.
Dimensions (a width and height) of a rectangle in 2D.
Specification of a rectangle with absolute coordinates of all edges.
Rect WithWidth(int width, bool end) const
Copy Rect and set its width.
Rect Indent(int indent, bool end) const
Copy Rect and indent it from its position.
int16_t Height
Fixed point type for heights.
Definition tgp.cpp:153
Functions, definitions and such used only by the GUI.