ManaPlus
popup.cpp
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  * Copyright (C) 2009 Aethyra Development Team
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 #include "gui/widgets/popup.h"
25 
26 #include "gui/popupmanager.h"
27 #include "gui/skin.h"
28 
30 
31 #include "utils/delete2.h"
32 
33 #include "render/graphics.h"
34 
36 
37 #include "debug.h"
38 
39 Popup::Popup(const std::string &name,
40  std::string skin) :
42  MouseListener(),
44  mPadding(3),
45  mSkin(nullptr),
46  mPopupName(name),
47  mVertexes(new ImageCollection),
48  mMinWidth(100),
49  mMinHeight(40),
50  mMaxWidth(mainGraphics->mWidth),
51  mMaxHeight(mainGraphics->mHeight),
52  mInit(false)
53 {
54  logger->log("Popup::Popup(\"%s\")", name.c_str());
55 
56  mWindow = this;
57 
58  addWidgetListener(this);
59 
60  if (skin.empty())
61  skin = "popup.xml";
62 
63  if (theme != nullptr)
64  {
65  mSkin = theme->load(skin,
66  "popup.xml",
67  true,
69  if (mSkin != nullptr)
70  {
72  setPalette(mSkin->getOption("palette"));
73  }
74  }
75 
76  if (windowContainer != nullptr)
77  windowContainer->add(this);
78 
79  // Popups are invisible by default
81 }
82 
84 {
85  logger->log("Popup::~Popup(\"%s\")", mPopupName.c_str());
86 
88 
89  if (mSkin != nullptr)
90  {
91  if (theme != nullptr)
92  theme->unload(mSkin);
93  mSkin = nullptr;
94  }
95 
96  if (!mInit)
97  {
98  logger->log("error: Popup created without calling postInit(): "
99  + mPopupName);
100  }
101 }
102 
104 {
105  windowContainer = wc;
106 }
107 
108 void Popup::draw(Graphics *const graphics)
109 {
110  BLOCK_START("Popup::draw")
111 
112  if (mSkin != nullptr)
113  {
114  if (mRedraw)
115  {
116  mRedraw = false;
117  mVertexes->clear();
118  graphics->calcWindow(mVertexes,
119  0, 0,
121  mSkin->getBorder());
122  }
123  // need cache or remove calc call.
124  graphics->finalize(mVertexes);
125  graphics->drawTileCollection(mVertexes);
126  }
127 
128  drawChildren(graphics);
129  BLOCK_END("Popup::draw")
130 }
131 
132 void Popup::safeDraw(Graphics *const graphics)
133 {
134  BLOCK_START("Popup::safeDraw")
135 
136  if (mSkin != nullptr)
137  {
138  graphics->drawImageRect(0, 0,
140  mSkin->getBorder());
141  }
142 
143  safeDrawChildren(graphics);
144  BLOCK_END("Popup::safeDraw")
145 }
146 
148 {
149  const int pad2 = mPadding * 2;
150  return Rect(mPadding, mPadding,
151  mDimension.width - pad2, mDimension.height - pad2);
152 }
153 
154 void Popup::setContentSize(int width, int height)
155 {
156  const int pad2 = mPadding * 2;
157  width += pad2;
158  height += pad2;
159 
160  if (mMinWidth > width)
161  width = mMinWidth;
162  else if (mMaxWidth < width)
163  width = mMaxWidth;
164  if (mMinHeight > height)
165  height = mMinHeight;
166  else if (mMaxHeight < height)
167  height = mMaxHeight;
168 
169  setSize(width, height);
170  mRedraw = true;
171 }
172 
173 void Popup::setLocationRelativeTo(const Widget *const widget)
174 {
175  if (widget == nullptr)
176  return;
177 
178  int wx, wy;
179  int x, y;
180 
181  widget->getAbsolutePosition(wx, wy);
182  getAbsolutePosition(x, y);
183 
184  setPosition(mDimension.x + (wx + (widget->getWidth()
185  - mDimension.width) / 2 - x),
186  mDimension.y + (wy + (widget->getHeight()
187  - mDimension.height) / 2 - y));
188  mRedraw = true;
189 }
190 
191 void Popup::setMinWidth(const int width)
192 {
193  if (mSkin != nullptr)
194  {
195  mMinWidth = width > mSkin->getMinWidth()
196  ? width : mSkin->getMinWidth();
197  }
198  else
199  {
200  mMinWidth = width;
201  }
202 }
203 
204 void Popup::setMinHeight(const int height)
205 {
206  if (mSkin != nullptr)
207  {
208  mMinHeight = height > mSkin->getMinHeight() ?
209  height : mSkin->getMinHeight();
210  }
211  else
212  {
213  mMinHeight = height;
214  }
215 }
216 
217 void Popup::setMaxWidth(const int width)
218 {
219  mMaxWidth = width;
220 }
221 
222 void Popup::setMaxHeight(const int height)
223 {
224  mMaxHeight = height;
225 }
226 
228 {
230 }
231 
232 void Popup::position(const int x, const int y)
233 {
234  const int distance = 20;
235 
236  const int width = mDimension.width;
237  const int height = mDimension.height;
238  int posX = std::max(0, x - width / 2);
239  int posY = y + distance;
240 
241  if (posX + width > mainGraphics->mWidth)
242  posX = mainGraphics->mWidth - width;
243  if (posY + height > mainGraphics->mHeight)
244  posY = y - height - distance;
245 
246  setPosition(posX, posY);
249  mRedraw = true;
250 }
251 
253 {
254  if (popupManager != nullptr)
255  {
258  }
259  mRedraw = true;
260 }
261 
263 {
265  mRedraw = true;
266 }
267 
269 {
270  mRedraw = true;
271 }
272 
274 {
275  mRedraw = true;
276 }
void setSize(const int width, const int height)
Definition: widget.cpp:366
void setMaxHeight(const int height)
Definition: popup.cpp:222
int getMinHeight() const
Definition: skin.cpp:141
int width
Definition: rect.h:218
void setContentSize(int width, int height)
Definition: popup.cpp:154
int getWidth() const
Definition: widget.h:220
virtual void drawTileCollection(const ImageCollection *const vertCol)=0
void unload(Skin *const skin)
Definition: theme.cpp:249
void setMinHeight(const int height)
Definition: popup.cpp:204
Popup(const std::string &name, std::string skin)
Definition: popup.cpp:39
void scheduleDelete(Widget *const widget)
const bool Visible_true
Definition: visible.h:29
WindowContainer * windowContainer
virtual void drawImageRect(const int x, const int y, const int w, const int h, const ImageRect &imgRect)=0
void setVisible(Visible visible)
Definition: widget.cpp:224
Definition: rect.h:72
virtual void requestMoveToTop()
Definition: widget.cpp:212
int mWidth
Definition: graphics.h:483
#define BLOCK_START(name)
Definition: perfomance.h:78
static std::string getThemePath()
Definition: theme.h:66
void safeDraw(Graphics *const graphics)
Definition: popup.cpp:132
PopupManager * popupManager
Skin * load(const std::string &filename, const std::string &filename2, const bool full, const std::string &defaultPath)
Definition: theme.cpp:178
#define BLOCK_END(name)
Definition: perfomance.h:79
void setMinWidth(const int width)
Definition: popup.cpp:191
#define delete2(var)
Definition: delete2.h:24
int mPadding
Definition: popup.h:180
int getOption(const std::string &name) const
Definition: skin.h:105
Logger * logger
Definition: logger.cpp:88
virtual void finalize(ImageCollection *const col)
Definition: graphics.h:464
#define new
Definition: debug_new.h:147
int x
Definition: rect.h:208
int getPadding() const
Definition: skin.h:99
int mMaxHeight
Definition: popup.h:189
void hide()
Definition: popup.cpp:262
int mMaxWidth
Definition: popup.h:188
static void hideTextPopup()
const ImageRect & getBorder() const
Definition: skin.h:67
void scheduleDelete()
Definition: popup.cpp:227
const bool Visible_false
Definition: visible.h:29
Widget * mWindow
Definition: widget2.h:111
void mouseMoved(MouseEvent &event)
Definition: popup.cpp:252
void setLocationRelativeTo(const Widget *const widget)
Definition: popup.cpp:173
int y
Definition: rect.h:213
void position(const int x, const int y)
Definition: popup.cpp:232
virtual void add(Widget *const widget)
#define nullptr
Definition: localconsts.h:44
Graphics * mainGraphics
Definition: graphics.cpp:108
int mMinWidth
Definition: popup.h:186
bool mInit
Definition: popup.h:190
void setPosition(const int x, const int y)
Definition: widget.cpp:160
int height
Definition: rect.h:223
int mMinHeight
Definition: popup.h:187
int mHeight
Definition: graphics.h:484
void draw(Graphics *const graphics)
Definition: popup.cpp:108
std::string mPopupName
Definition: popup.h:184
virtual void getAbsolutePosition(int &x, int &y) const
Definition: widget.cpp:311
static void setWindowContainer(WindowContainer *const windowContainer)
Definition: popup.cpp:103
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:301
void widgetMoved(const Event &event)
Definition: popup.cpp:273
Theme * theme
Definition: theme.cpp:61
#define A_UNUSED
Definition: localconsts.h:151
static void hideBeingPopup()
Definition: widget.h:97
virtual void drawChildren(Graphics *const graphics)
Definition: event.h:77
bool mRedraw
Definition: widget.h:1163
Rect getChildrenArea()
Definition: popup.cpp:147
ImageCollection * mVertexes
Definition: popup.h:185
int getHeight() const
Definition: widget.h:239
void log(const char *const log_text,...)
Definition: logger.cpp:264
void widgetResized(const Event &event)
Definition: popup.cpp:268
void setMaxWidth(const int width)
Definition: popup.cpp:217
Rect mDimension
Definition: widget.h:1100
void setPadding(int padding)
Definition: popup.h:137
virtual void safeDrawChildren(Graphics *const graphics)
Skin * mSkin
Definition: popup.h:181
virtual void calcWindow(ImageCollection *const vertCol, const int x, const int y, const int w, const int h, const ImageRect &imgRect)=0
int getMinWidth() const
Definition: skin.cpp:128
void setPalette(int palette)
Definition: widget2.h:68
~Popup()
Definition: popup.cpp:83