Mana
Loading...
Searching...
No Matches
popup.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 * Copyright (C) 2009 Aethyra Development Team
6 *
7 * This file is part of The Mana 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#include "gui/widgets/popup.h"
24
25#include "browserbox.h"
26#include "graphics.h"
27#include "log.h"
28#include "textbox.h"
29
30#include "gui/gui.h"
31#include "gui/viewport.h"
32#include "gui/widgets/label.h"
34
35#include <guichan/exception.hpp>
36
37Popup::Popup(const std::string &name, SkinType skinType)
38 : mPopupName(name)
39 , mMaxWidth(graphics->getWidth())
40 , mMaxHeight(graphics->getHeight())
41 , mSkinType(skinType)
42{
43 Log::debug("Popup::Popup(\"%s\")", name.c_str());
44
45 if (!windowContainer)
46 throw GCN_EXCEPTION("Popup::Popup(): no windowContainer set");
47
48 auto &skin = getSkin();
49 setFrameSize(skin.frameSize);
50 setPadding(skin.padding);
51
52 // Add this window to the window container
53 windowContainer->add(this);
54
55 // Popups are invisible by default
56 setVisible(false);
57}
58
60{
61 Log::debug("Popup::~Popup(\"%s\")", mPopupName.c_str());
62}
63
68
69void Popup::add(gcn::Widget *widget)
70{
71 Container::add(widget);
72 widgetAdded(widget);
73}
74
75void Popup::add(gcn::Widget *widget, int x, int y)
76{
77 Container::add(widget, x, y);
78 widgetAdded(widget);
79}
80
81void Popup::widgetAdded(gcn::Widget *widget) const
82{
83 if (const int paletteId = getSkin().palette)
84 {
85 if (auto browserBox = dynamic_cast<BrowserBox*>(widget))
86 {
87 browserBox->setPalette(paletteId);
88 }
89 else if (auto label = dynamic_cast<Label*>(widget))
90 {
91 auto &palette = gui->getTheme()->getPalette(paletteId);
92 label->setForegroundColor(palette.getColor(Theme::TEXT));
93 label->setOutlineColor(palette.getOutlineColor(Theme::TEXT));
94 }
95 else if (auto textBox = dynamic_cast<TextBox*>(widget))
96 {
97 auto &palette = gui->getTheme()->getPalette(paletteId);
98 textBox->setTextColor(&palette.getColor(Theme::TEXT));
99 textBox->setOutlineColor(palette.getOutlineColor(Theme::TEXT));
100 }
101 }
102}
103
104void Popup::draw(gcn::Graphics *graphics)
105{
106 if (getFrameSize() == 0)
108
109 drawChildren(graphics);
110}
111
112void Popup::drawFrame(gcn::Graphics *graphics)
113{
114 WidgetState state(this);
115 state.width += getFrameSize() * 2;
116 state.height += getFrameSize() * 2;
117 getSkin().draw(static_cast<Graphics *>(graphics), state);
118}
119
121{
122 return gcn::Rectangle(getPadding(), getPadding(),
123 getWidth() - getPadding() * 2,
124 getHeight() - getPadding() * 2);
125}
126
127void Popup::setContentSize(int width, int height)
128{
129 width += 2 * getPadding();
130 height += 2 * getPadding();
131
132 if (getMinWidth() > width)
133 width = getMinWidth();
134 else if (getMaxWidth() < width)
135 width = getMaxWidth();
136 if (getMinHeight() > height)
137 height = getMinHeight();
138 else if (getMaxHeight() < height)
139 height = getMaxHeight();
140
141 setSize(width, height);
142}
143
144void Popup::setLocationRelativeTo(gcn::Widget *widget)
145{
146 int wx;
147 int wy;
148 widget->getAbsolutePosition(wx, wy);
149
150 int x;
151 int y;
152 getAbsolutePosition(x, y);
153
154 setPosition(getX() + (wx + (widget->getWidth() - getWidth()) / 2 - x),
155 getY() + (wy + (widget->getHeight() - getHeight()) / 2 - y));
156}
157
158void Popup::setMinWidth(int width)
159{
160 mMinWidth = std::max(getSkin().getMinWidth(), width);
161}
162
163void Popup::setMinHeight(int height)
164{
165 mMinHeight = std::max(getSkin().getMinHeight(), height);
166}
167
168void Popup::setMaxWidth(int width)
169{
170 mMaxWidth = width;
171}
172
173void Popup::setMaxHeight(int height)
174{
175 mMaxHeight = height;
176}
177
182
183void Popup::position(int x, int y)
184{
185 const int distance = 20;
186
187 int posX = std::max(0, x - getWidth() / 2);
188 int posY = y + distance;
189
190 if (posX > graphics->getWidth() - getWidth())
191 posX = graphics->getWidth() - getWidth();
192 if (posY > graphics->getHeight() - getHeight())
193 posY = y - getHeight() - distance;
194
195 setPosition(posX, posY);
196 setVisible(true);
197 requestMoveToTop();
198}
199
200const Skin &Popup::getSkin() const
201{
202 return gui->getTheme()->getSkin(mSkinType);
203}
204
205void Popup::mouseMoved(gcn::MouseEvent &event)
206{
207 if (viewport)
209}
A simple browser box able to handle links and forward events to the parent conteiner.
Definition browserbox.h:74
A central point of control for graphics.
Definition graphics.h:78
int getHeight() const
Returns the logical height of the screen.
Definition graphics.h:226
int getWidth() const
Returns the logical width of the screen.
Definition graphics.h:221
Theme * getTheme() const
The global GUI theme.
Definition gui.h:138
Label widget.
Definition label.h:34
SkinType mSkinType
The skin type used when drawing the popup widget.
Definition popup.h:178
const Skin & getSkin() const
Returns the Skin used by this popup.
Definition popup.cpp:200
static void setWindowContainer(WindowContainer *windowContainer)
Sets the window container to be used by new popups.
Definition popup.cpp:64
void setPadding(int padding)
Definition popup.h:134
void scheduleDelete()
Schedule this popup for deletion.
Definition popup.cpp:178
int getMinWidth() const
Definition popup.h:102
void setMinHeight(int height)
Sets the minimum height of the popup.
Definition popup.cpp:163
void add(gcn::Widget *widget) override
Definition popup.cpp:69
void mouseMoved(gcn::MouseEvent &event) override
Definition popup.cpp:205
void setMinWidth(int width)
Sets the minimum width of the popup.
Definition popup.cpp:158
int getMaxWidth() const
Definition popup.h:116
int mMinHeight
Minimum popup height.
Definition popup.h:173
void drawFrame(gcn::Graphics *graphics) override
Draws the popup frame.
Definition popup.cpp:112
int getPadding() const
Gets the padding of the popup.
Definition popup.h:132
void draw(gcn::Graphics *graphics) override
Draws the popup.
Definition popup.cpp:104
std::string mPopupName
Name of the popup.
Definition popup.h:171
void setContentSize(int width, int height)
Sets the size of this popup.
Definition popup.cpp:127
~Popup() override
Destructor.
Definition popup.cpp:59
int getMinHeight() const
Definition popup.h:109
int mMinWidth
Minimum popup width.
Definition popup.h:172
void setMaxWidth(int width)
Sets the maximum width of the popup.
Definition popup.cpp:168
int mMaxWidth
Maximum popup width.
Definition popup.h:174
Popup(const std::string &name=std::string(), SkinType skinType=SkinType::Popup)
Constructor.
Definition popup.cpp:37
void setMaxHeight(int height)
Sets the minimum height of the popup.
Definition popup.cpp:173
void widgetAdded(gcn::Widget *widget) const
Definition popup.cpp:81
void position(int x, int y)
Sets the location to display the popup.
Definition popup.cpp:183
gcn::Rectangle getChildrenArea() override
Definition popup.cpp:120
int mMaxHeight
Maximum popup height.
Definition popup.h:175
int getMaxHeight() const
Definition popup.h:123
void setLocationRelativeTo(gcn::Widget *widget)
Sets the location relative to the given widget.
Definition popup.cpp:144
Definition theme.h:154
void draw(Graphics *graphics, const WidgetState &state) const
Definition theme.cpp:122
A text box, meant to be used inside a scroll area.
Definition textbox.h:36
const Skin & getSkin(SkinType skinType) const
Definition theme.cpp:434
const Palette & getPalette(size_t index) const
Definition theme.cpp:328
@ TEXT
Definition theme.h:214
void hideBeingPopup()
Hides the BeingPopup.
Definition viewport.cpp:600
A window container.
void scheduleDelete(gcn::Widget *widget)
Schedule a widget for deletion.
Graphics * graphics
Definition client.cpp:104
Viewport * viewport
Viewport on the map.
Definition game.cpp:115
Gui * gui
The GUI system.
Definition gui.cpp:50
void debug(const char *log_text,...) LOG_PRINTF_ATTR
int width
Definition theme.h:148
int height
Definition theme.h:149
SkinType
Definition theme.h:69
WindowContainer * windowContainer