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