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-2017 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, "popup.xml");
66  if (mSkin != nullptr)
67  {
69  setPalette(mSkin->getOption("palette"));
70  }
71  }
72 
73  if (windowContainer != nullptr)
74  windowContainer->add(this);
75 
76  // Popups are invisible by default
78 }
79 
81 {
82  logger->log("Popup::~Popup(\"%s\")", mPopupName.c_str());
83 
85 
86  if (mSkin != nullptr)
87  {
88  if (theme != nullptr)
89  theme->unload(mSkin);
90  mSkin = nullptr;
91  }
92 
93  if (!mInit)
94  {
95  logger->log("error: Popup created without calling postInit(): "
96  + mPopupName);
97  }
98 }
99 
101 {
102  windowContainer = wc;
103 }
104 
105 void Popup::draw(Graphics *const graphics)
106 {
107  BLOCK_START("Popup::draw")
108 
109  if (mSkin != nullptr)
110  {
111  if (mRedraw)
112  {
113  mRedraw = false;
114  mVertexes->clear();
115  graphics->calcWindow(mVertexes,
116  0, 0,
118  mSkin->getBorder());
119  }
120  // need cache or remove calc call.
121  graphics->finalize(mVertexes);
122  graphics->drawTileCollection(mVertexes);
123  }
124 
125  drawChildren(graphics);
126  BLOCK_END("Popup::draw")
127 }
128 
129 void Popup::safeDraw(Graphics *const graphics)
130 {
131  BLOCK_START("Popup::safeDraw")
132 
133  if (mSkin != nullptr)
134  {
135  graphics->drawImageRect(0, 0,
137  mSkin->getBorder());
138  }
139 
140  safeDrawChildren(graphics);
141  BLOCK_END("Popup::safeDraw")
142 }
143 
145 {
146  const int pad2 = mPadding * 2;
147  return Rect(mPadding, mPadding,
148  mDimension.width - pad2, mDimension.height - pad2);
149 }
150 
151 void Popup::setContentSize(int width, int height)
152 {
153  const int pad2 = mPadding * 2;
154  width += pad2;
155  height += pad2;
156 
157  if (mMinWidth > width)
158  width = mMinWidth;
159  else if (mMaxWidth < width)
160  width = mMaxWidth;
161  if (mMinHeight > height)
162  height = mMinHeight;
163  else if (mMaxHeight < height)
164  height = mMaxHeight;
165 
166  setSize(width, height);
167  mRedraw = true;
168 }
169 
170 void Popup::setLocationRelativeTo(const Widget *const widget)
171 {
172  if (widget == nullptr)
173  return;
174 
175  int wx, wy;
176  int x, y;
177 
178  widget->getAbsolutePosition(wx, wy);
179  getAbsolutePosition(x, y);
180 
181  setPosition(mDimension.x + (wx + (widget->getWidth()
182  - mDimension.width) / 2 - x),
183  mDimension.y + (wy + (widget->getHeight()
184  - mDimension.height) / 2 - y));
185  mRedraw = true;
186 }
187 
188 void Popup::setMinWidth(const int width)
189 {
190  if (mSkin != nullptr)
191  {
192  mMinWidth = width > mSkin->getMinWidth()
193  ? width : mSkin->getMinWidth();
194  }
195  else
196  {
197  mMinWidth = width;
198  }
199 }
200 
201 void Popup::setMinHeight(const int height)
202 {
203  if (mSkin != nullptr)
204  {
205  mMinHeight = height > mSkin->getMinHeight() ?
206  height : mSkin->getMinHeight();
207  }
208  else
209  {
210  mMinHeight = height;
211  }
212 }
213 
214 void Popup::setMaxWidth(const int width)
215 {
216  mMaxWidth = width;
217 }
218 
219 void Popup::setMaxHeight(const int height)
220 {
221  mMaxHeight = height;
222 }
223 
225 {
227 }
228 
229 void Popup::position(const int x, const int y)
230 {
231  const int distance = 20;
232 
233  const int width = mDimension.width;
234  const int height = mDimension.height;
235  int posX = std::max(0, x - width / 2);
236  int posY = y + distance;
237 
238  if (posX + width > mainGraphics->mWidth)
239  posX = mainGraphics->mWidth - width;
240  if (posY + height > mainGraphics->mHeight)
241  posY = y - height - distance;
242 
243  setPosition(posX, posY);
246  mRedraw = true;
247 }
248 
250 {
251  if (popupManager != nullptr)
252  {
255  }
256  mRedraw = true;
257 }
258 
260 {
262  mRedraw = true;
263 }
264 
266 {
267  mRedraw = true;
268 }
269 
271 {
272  mRedraw = true;
273 }
void setSize(const int width, const int height)
Definition: widget.cpp:366
void setMaxHeight(const int height)
Definition: popup.cpp:219
int getMinHeight() const
Definition: skin.cpp:141
int width
Definition: rect.h:218
void setContentSize(int width, int height)
Definition: popup.cpp:151
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:201
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
void safeDraw(Graphics *const graphics)
Definition: popup.cpp:129
PopupManager * popupManager
#define BLOCK_END(name)
Definition: perfomance.h:79
void setMinWidth(const int width)
Definition: popup.cpp:188
#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:95
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:259
int mMaxWidth
Definition: popup.h:188
static void hideTextPopup()
const ImageRect & getBorder() const
Definition: skin.h:67
void scheduleDelete()
Definition: popup.cpp:224
const bool Visible_false
Definition: visible.h:29
Widget * mWindow
Definition: widget2.h:111
void mouseMoved(MouseEvent &event)
Definition: popup.cpp:249
void setLocationRelativeTo(const Widget *const widget)
Definition: popup.cpp:170
int y
Definition: rect.h:213
void position(const int x, const int y)
Definition: popup.cpp:229
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:105
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:100
Popup(const std::string &name="", std::string skin="")
Definition: popup.cpp:39
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:301
Skin * load(const std::string &filename, const std::string &filename2, const bool full=true, const std::string &defaultPath=getThemePath())
Definition: theme.cpp:178
void widgetMoved(const Event &event)
Definition: popup.cpp:270
Theme * theme
Definition: theme.cpp:61
#define A_UNUSED
Definition: localconsts.h:171
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:144
ImageCollection * mVertexes
Definition: popup.h:185
int getHeight() const
Definition: widget.h:239
void log(const char *const log_text,...)
Definition: logger.cpp:243
void widgetResized(const Event &event)
Definition: popup.cpp:265
void setMaxWidth(const int width)
Definition: popup.cpp:214
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
virtual ~Popup()
Definition: popup.cpp:80