summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpeter1138 <peter1138@openttd.org>2019-03-10 02:10:40 +0000
committerPeterN <peter@fuzzle.org>2019-03-10 10:31:37 +0000
commitb56ea5ca27440fd8023bfecf8299422c93eb05c3 (patch)
tree38d7f340afd0377ec6dc5b561cf0106e3814c168 /src
parentfb35cb5ed2735ad22d8a2bee9df2a174994ed319 (diff)
downloadopenttd-b56ea5ca27440fd8023bfecf8299422c93eb05c3.tar.xz
Add: Generic drop down list string item with icon.
Diffstat (limited to 'src')
-rw-r--r--src/widgets/dropdown.cpp34
-rw-r--r--src/widgets/dropdown_type.h18
2 files changed, 52 insertions, 0 deletions
diff --git a/src/widgets/dropdown.cpp b/src/widgets/dropdown.cpp
index fb235a6eb..f789b47b7 100644
--- a/src/widgets/dropdown.cpp
+++ b/src/widgets/dropdown.cpp
@@ -71,6 +71,40 @@ StringID DropDownListCharStringItem::String() const
return this->string;
}
+DropDownListIconItem::DropDownListIconItem(SpriteID sprite, PaletteID pal, StringID string, int result, bool masked) : DropDownListParamStringItem(string, result, masked), sprite(sprite), pal(pal)
+{
+ this->dim = GetSpriteSize(sprite);
+ if (this->dim.height < (uint)FONT_HEIGHT_NORMAL) {
+ this->sprite_y = (FONT_HEIGHT_NORMAL - dim.height) / 2;
+ this->text_y = 0;
+ } else {
+ this->sprite_y = 0;
+ this->text_y = (dim.height - FONT_HEIGHT_NORMAL) / 2;
+ }
+}
+
+uint DropDownListIconItem::Height(uint width) const
+{
+ return max(this->dim.height, (uint)FONT_HEIGHT_NORMAL);
+}
+
+uint DropDownListIconItem::Width() const
+{
+ return DropDownListStringItem::Width() + this->dim.width + WD_FRAMERECT_LEFT;
+}
+
+void DropDownListIconItem::Draw(int left, int right, int top, int bottom, bool sel, Colours bg_colour) const
+{
+ bool rtl = _current_text_dir == TD_RTL;
+ DrawSprite(this->sprite, this->pal, rtl ? right - this->dim.width - WD_FRAMERECT_RIGHT : left + WD_FRAMERECT_LEFT, top + this->sprite_y);
+ DrawString(left + WD_FRAMERECT_LEFT + (rtl ? 0 : (this->dim.width + WD_FRAMERECT_LEFT)), right - WD_FRAMERECT_RIGHT - (rtl ? (this->dim.width + WD_FRAMERECT_RIGHT) : 0), top + this->text_y, this->String(), sel ? TC_WHITE : TC_BLACK);
+}
+
+void DropDownListIconItem::SetDimension(Dimension d)
+{
+ this->dim = d;
+}
+
static const NWidgetPart _nested_dropdown_menu_widgets[] = {
NWidget(NWID_HORIZONTAL),
NWidget(WWT_PANEL, COLOUR_END, WID_DM_ITEMS), SetMinimalSize(1, 1), SetScrollbar(WID_DM_SCROLL), EndContainer(),
diff --git a/src/widgets/dropdown_type.h b/src/widgets/dropdown_type.h
index a5fe27f64..8300dd26d 100644
--- a/src/widgets/dropdown_type.h
+++ b/src/widgets/dropdown_type.h
@@ -78,6 +78,24 @@ public:
};
/**
+ * List item with icon and string.
+ */
+class DropDownListIconItem : public DropDownListParamStringItem {
+ SpriteID sprite;
+ PaletteID pal;
+ Dimension dim;
+ uint sprite_y;
+ uint text_y;
+public:
+ DropDownListIconItem(SpriteID sprite, PaletteID pal, StringID string, int result, bool masked);
+
+ /* virtual */ uint Height(uint width) const;
+ /* virtual */ uint Width() const;
+ /* virtual */ void Draw(int left, int right, int top, int bottom, bool sel, Colours bg_colour) const;
+ void SetDimension(Dimension d);
+};
+
+/**
* A drop down list is a collection of drop down list items.
*/
typedef AutoDeleteSmallVector<const DropDownListItem *, 4> DropDownList;