GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/serverslistbox.h Lines: 49 55 89.1 %
Date: 2021-03-17 Branches: 11 18 61.1 %

Line Branch Exec Source
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
29
#include "gui/models/serverslistmodel.h"
30
31
#include "localconsts.h"
32
33
2
class ServersListBox final : public ListBox
34
{
35
    public:
36
2
        ServersListBox(const Widget2 *const widget,
37
2
                       ServersListModel *const model) :
38
            ListBox(widget, model, "serverslistbox.xml"),
39
            mNotSupportedColor(getThemeColor(
40
4
                ThemeColorId::SERVER_VERSION_NOT_SUPPORTED, 255U)),
41
            mNotSupportedColor2(getThemeColor(
42
12
                ThemeColorId::SERVER_VERSION_NOT_SUPPORTED_OUTLINE, 255U))
43
        {
44
4
            mHighlightColor = getThemeColor(ThemeColorId::HIGHLIGHT, 255U);
45
2
        }
46
47
        A_DELETE_COPY(ServersListBox)
48
49
2
        void draw(Graphics *const graphics) override final A_NONNULL(2)
50
        {
51
2
            if (mListModel == nullptr)
52
                return;
53
54
            ServersListModel *const model
55
2
                = static_cast<ServersListModel *>(mListModel);
56
57
2
            updateAlpha();
58
59
2
            mHighlightColor.a = CAST_S32(mAlpha * 255.0F);
60
2
            graphics->setColor(mHighlightColor);
61
62
2
            const int height = getRowHeight();
63
2
            mNotSupportedColor.a = CAST_S32(mAlpha * 255.0F);
64
65
            // Draw filled rectangle around the selected list element
66
2
            if (mSelected >= 0)
67
            {
68
8
                graphics->fillRectangle(Rect(mPadding,
69
2
                    height * mSelected + mPadding,
70
4
                    getWidth() - 2 * mPadding,
71
4
                    height));
72
            }
73
74
2
            Font *const font1 = boldFont;
75
2
            Font *const font2 = getFont();
76
2
            const int fontHeight = font1->getHeight();
77
2
            const int pad1 = fontHeight + mPadding;
78
2
            const int pad2 = height / 4 + mPadding;
79
4
            const int width = getWidth();
80
            // Draw the list elements
81
30
            for (int i = 0, y = 0; i < model->getNumberOfElements();
82
14
                 ++i, y += height)
83
            {
84
14
                const ServerInfo &info = model->getServer(i);
85
86
                const Color *color1;
87
                const Color *color2;
88
14
                if (mSelected == i)
89
                {
90
2
                    color1 = &mForegroundSelectedColor;
91
2
                    color2 = &mForegroundSelectedColor2;
92
                }
93
                else
94
                {
95
12
                    color1 = &mForegroundColor;
96
12
                    color2 = &mForegroundColor2;
97
                }
98
99
                int top;
100
14
                int x = mPadding;
101
102
28
                if (!info.name.empty())
103
                {
104
14
                    x += font1->getWidth(info.name) + 15;
105
14
                    font1->drawString(graphics,
106
                        *color1,
107
                        *color2,
108
                        info.name,
109
                        mPadding,
110
28
                        y + mPadding);
111
14
                    top = y + pad1;
112
                }
113
                else
114
                {
115
                    top = y + pad2;
116
                }
117
118
28
                if (!info.description.empty())
119
                {
120
14
                    font2->drawString(graphics,
121
                        *color1,
122
                        *color2,
123
                        info.description,
124
                        x,
125
28
                        y + mPadding);
126
                }
127
14
                font2->drawString(graphics,
128
                    *color1,
129
                    *color2,
130
28
                    model->getElementAt(i),
131
                    mPadding,
132
14
                    top);
133
134
14
                if (info.version.first > 0)
135
                {
136
                    font2->drawString(graphics,
137
                        mNotSupportedColor,
138
                        mNotSupportedColor2,
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
12
        unsigned int getRowHeight() const override final
152
        {
153
12
            return 2 * getFont()->getHeight() + 5;
154
        }
155
    private:
156
        Color mNotSupportedColor;
157
        Color mNotSupportedColor2;
158
};
159
160
#endif  // GUI_WIDGETS_SERVERSLISTBOX_H