Mana
Loading...
Searching...
No Matches
avatarlistbox.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2010-2012 The Mana Developers
4 *
5 * This file is part of The Mana Client.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
22
23#include "graphics.h"
24
25#include "gui/gui.h"
26
27#include "resources/image.h"
29#include "resources/theme.h"
30
31#include "utils/stringutils.h"
32
33#include <guichan/font.hpp>
34
36 ListBox(model)
37{
38 setWidth(200);
39}
40
41unsigned int AvatarListBox::getRowHeight() const
42{
43 auto rowHeight = ListBox::getRowHeight();
44
45 auto theme = gui->getTheme();
46 if (auto onlineIcon = theme->getIcon("online"))
47 rowHeight = std::max<unsigned>(rowHeight, onlineIcon->getHeight() + 2);
48
49 return rowHeight;
50}
51
52void AvatarListBox::draw(gcn::Graphics *gcnGraphics)
53{
54 if (!mListModel)
55 return;
56
57 auto *model = static_cast<AvatarListModel *>(mListModel);
58 auto *graphics = static_cast<Graphics *>(gcnGraphics);
59
60 graphics->setFont(getFont());
61
62 const int rowHeight = getRowHeight();
63
64 // Draw filled rectangle around the selected list element
65 if (mSelected >= 0)
66 {
67 auto highlightColor = Theme::getThemeColor(Theme::HIGHLIGHT);
68 highlightColor.a = gui->getTheme()->getGuiAlpha();
69 graphics->setColor(highlightColor);
70 graphics->fillRectangle(gcn::Rectangle(0, rowHeight * mSelected,
71 getWidth(), rowHeight));
72 }
73
74 auto theme = gui->getTheme();
75 auto onlineIcon = theme->getIcon("online");
76 auto offlineIcon = theme->getIcon("offline");
77
78 // Draw the list elements
79 for (int i = 0, y = 0;
80 i < model->getNumberOfElements();
81 ++i, y += rowHeight)
82 {
83 if (mSelected == i)
85 else
87
88 Avatar *a = model->getAvatarAt(i);
89 int x = 1;
90
91 // Draw online status
92 if (const Image *icon = a->getOnline() ? onlineIcon : offlineIcon)
93 {
94 graphics->drawImage(icon, x, y + (rowHeight - icon->getHeight()) / 2);
95 x += icon->getWidth() + 4;
96 }
97
98 if (a->getDisplayBold())
99 graphics->setFont(boldFont);
100
101 std::string text;
102
103 if (a->getMaxHp() > 0)
104 {
105 text = strprintf("%s %d/%d", a->getName().c_str(),
106 a->getHp(), a->getMaxHp());
107 }
108 else
109 {
110 text = a->getName();
111 }
112
113 // Draw Name
114 graphics->drawText(text, x, y);
115
116 if (a->getDisplayBold())
117 graphics->setFont(getFont());
118 }
119}
120
121void AvatarListBox::mousePressed(gcn::MouseEvent &event)
122{
123 if (event.getButton() == gcn::MouseEvent::LEFT)
124 {
126 }
127 // TODO: Add support for context menu
128 else if (event.getButton() == gcn::MouseEvent::RIGHT)
129 {
130 // Show context menu
131 }
132}
void mousePressed(gcn::MouseEvent &event) override
AvatarListBox(AvatarListModel *model)
unsigned int getRowHeight() const override
void draw(gcn::Graphics *gcnGraphics) override
Draws the list box.
bool getOnline() const
Returns the avatar's online status.
Definition avatar.h:44
const std::string & getName() const
Returns the avatar's name.
Definition avatar.h:34
int getMaxHp() const
Definition avatar.h:55
bool getDisplayBold() const
Definition avatar.h:59
int getHp() const
Definition avatar.h:51
A central point of control for graphics.
Definition graphics.h:78
void drawText(const std::string &text, int x, int y, gcn::Graphics::Alignment alignment, const gcn::Color &color, gcn::Font *font, bool outline=false, bool shadow=false, const std::optional< gcn::Color > &outlineColor={}, const std::optional< gcn::Color > &shadowColor={})
Definition graphics.cpp:176
void setColor(const gcn::Color &color) override
Definition graphics.h:255
bool drawImage(const Image *image, int x, int y)
Blits an image onto the screen.
Definition graphics.cpp:36
Theme * getTheme() const
The global GUI theme.
Definition gui.h:138
Defines a class for loading and storing images.
Definition image.h:45
A list box, meant to be used inside a scroll area.
Definition listbox.h:36
void mousePressed(gcn::MouseEvent &mouseEvent) override
Definition listbox.cpp:109
int getGuiAlpha() const
Get the current GUI alpha value.
Definition theme.h:321
static const gcn::Color & getThemeColor(int type)
Gets the color associated with the type in the default palette (0).
Definition theme.cpp:313
@ HIGHLIGHT_TEXT
Definition theme.h:232
@ HIGHLIGHT
Definition theme.h:231
@ TEXT
Definition theme.h:214
const Image * getIcon(const std::string &name) const
Definition theme.cpp:441
Graphics * graphics
Definition client.cpp:104
Gui * gui
The GUI system.
Definition gui.cpp:50
gcn::Font * boldFont
Bolded text font.
Definition gui.cpp:54
std::string strprintf(char const *format,...)
A safe version of sprintf that returns a std::string of the result.