ManaPlus
serverslistbox.h
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef GUI_WIDGETS_SERVERSLISTBOX_H
25 #define GUI_WIDGETS_SERVERSLISTBOX_H
26 
27 #include "gui/widgets/listbox.h"
28 
30 
31 #include "localconsts.h"
32 
34 {
35  public:
36  ServersListBox(const Widget2 *const widget,
37  ServersListModel *const model) :
38  ListBox(widget, model, "serverslistbox.xml"),
40  ThemeColorId::SERVER_VERSION_NOT_SUPPORTED, 255U)),
42  ThemeColorId::SERVER_VERSION_NOT_SUPPORTED_OUTLINE, 255U))
43  {
44  mHighlightColor = getThemeColor(ThemeColorId::HIGHLIGHT, 255U);
45  }
46 
48 
49  void draw(Graphics *const graphics) override final A_NONNULL(2)
50  {
51  if (mListModel == nullptr)
52  return;
53 
54  ServersListModel *const model
55  = static_cast<ServersListModel *>(mListModel);
56 
57  updateAlpha();
58 
59  mHighlightColor.a = CAST_S32(mAlpha * 255.0F);
60  graphics->setColor(mHighlightColor);
61 
62  const int height = getRowHeight();
63  mNotSupportedColor.a = CAST_S32(mAlpha * 255.0F);
64 
65  // Draw filled rectangle around the selected list element
66  if (mSelected >= 0)
67  {
68  graphics->fillRectangle(Rect(mPadding,
69  height * mSelected + mPadding,
70  getWidth() - 2 * mPadding,
71  height));
72  }
73 
74  Font *const font1 = boldFont;
75  Font *const font2 = getFont();
76  const int fontHeight = font1->getHeight();
77  const int pad1 = fontHeight + mPadding;
78  const int pad2 = height / 4 + mPadding;
79  const int width = getWidth();
80  // Draw the list elements
81  for (int i = 0, y = 0; i < model->getNumberOfElements();
82  ++i, y += height)
83  {
84  const ServerInfo &info = model->getServer(i);
85 
86  const Color *color1;
87  const Color *color2;
88  if (mSelected == i)
89  {
90  color1 = &mForegroundSelectedColor;
91  color2 = &mForegroundSelectedColor2;
92  }
93  else
94  {
95  color1 = &mForegroundColor;
96  color2 = &mForegroundColor2;
97  }
98 
99  int top;
100  int x = mPadding;
101 
102  if (!info.name.empty())
103  {
104  x += font1->getWidth(info.name) + 15;
105  font1->drawString(graphics,
106  *color1,
107  *color2,
108  info.name,
109  mPadding,
110  y + mPadding);
111  top = y + pad1;
112  }
113  else
114  {
115  top = y + pad2;
116  }
117 
118  if (!info.description.empty())
119  {
120  font2->drawString(graphics,
121  *color1,
122  *color2,
123  info.description,
124  x,
125  y + mPadding);
126  }
127  font2->drawString(graphics,
128  *color1,
129  *color2,
130  model->getElementAt(i),
131  mPadding,
132  top);
133 
134  if (info.version.first > 0)
135  {
136  font2->drawString(graphics,
139  info.version.second,
140  width - info.version.first - mPadding,
141  top);
142  }
143  }
144  }
145 
146  void safeDraw(Graphics *const graphics) override final A_NONNULL(2)
147  {
148  ServersListBox::draw(graphics);
149  }
150 
151  unsigned int getRowHeight() const override final
152  {
153  return 2 * getFont()->getHeight() + 5;
154  }
155  private:
158 };
159 
160 #endif // GUI_WIDGETS_SERVERSLISTBOX_H
#define CAST_S32
Definition: cast.h:30
Definition: color.h:76
unsigned int a
Definition: color.h:251
Definition: font.h:90
int getHeight() const
Definition: font.cpp:362
int getWidth(const std::string &text) const
Definition: font.cpp:334
void drawString(Graphics *const graphics, Color col, const Color &col2, const std::string &text, const int x, const int y)
Definition: font.cpp:254
int mSelected
Definition: listbox.h:256
Color mHighlightColor
Definition: listbox.h:283
void updateAlpha()
Definition: listbox.cpp:154
Color mForegroundSelectedColor2
Definition: listbox.h:285
ListModel * mListModel
Definition: listbox.h:261
int mPadding
Definition: listbox.h:287
static float mAlpha
Definition: listbox.h:292
Color mForegroundSelectedColor
Definition: listbox.h:284
Definition: rect.h:74
Color mNotSupportedColor
void safeDraw(Graphics *const graphics)
unsigned int getRowHeight() const
Color mNotSupportedColor2
void draw(Graphics *const graphics)
ServersListBox(const Widget2 *const widget, ServersListModel *const model)
const ServerInfo & getServer(const int elementIndex) const
std::string getElementAt(int elementIndex)
Color mForegroundColor2
Definition: widget2.h:113
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:45
Color mForegroundColor
Definition: widget.h:1086
Font * getFont() const
Definition: widget.cpp:331
int getWidth() const
Definition: widget.h:221
const Image *restrict const top
Font * boldFont
Definition: gui.cpp:112
#define override
Definition: localconsts.h:47
#define A_NONNULL(...)
Definition: localconsts.h:168
#define final
Definition: localconsts.h:46
#define A_DELETE_COPY(func)
Definition: localconsts.h:53
bool info(InputEvent &event)
Definition: commands.cpp:57