ManaPlus
textpopup.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2008 The Legend of Mazzeroth Development Team
4  * Copyright (C) 2008-2009 The Mana World Development Team
5  * Copyright (C) 2009-2010 The Mana Developers
6  * Copyright (C) 2011-2018 The ManaPlus Developers
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/popups/textpopup.h"
25 
26 #include "gui/widgets/label.h"
27 
28 #include "gui/fonts/font.h"
29 
30 #include "debug.h"
31 
32 TextPopup *textPopup = nullptr;
33 
35  Popup("TextPopup", "textpopup.xml"),
36  mText()
37 {
38 }
39 
41 {
43  const int fontHeight = getFont()->getHeight();
44  int y = 0;
45  for (int f = 0; f < TEXTPOPUPCOUNT; f ++)
46  {
47  Label *const label = new Label(this);
48  mText[f] = label;
49  label->setPosition(0, y);
50  label->setForegroundColorAll(
53  add(label);
54  y += fontHeight;
55  }
56  addMouseListener(this);
57 }
58 
60 {
61 }
62 
63 void TextPopup::show(const int x, const int y, const std::string &str1,
64  const std::string &str2, const std::string &str3)
65 {
66  mText[0]->setCaption(str1);
67  mText[1]->setCaption(str2);
68  mText[2]->setCaption(str3);
69 
70  int minWidth = 0;
71  for (int f = 0; f < TEXTPOPUPCOUNT; f ++)
72  {
73  Label *const label = mText[f];
74  label->adjustSize();
75  const int width = label->getWidth();
76  if (width > minWidth)
77  minWidth = width;
78  }
79 
80  const int pad2 = 2 * mPadding;
81  minWidth += pad2;
82  setWidth(minWidth);
83 
84  int cnt = 1;
85  if (!str2.empty())
86  cnt ++;
87  if (!str3.empty())
88  cnt ++;
89 
90  setHeight(pad2 + mText[0]->getFont()->getHeight() * cnt);
91  const int distance = 20;
92 
93  const Rect &rect = mDimension;
94  int posX = std::max(0, x - rect.width / 2);
95  int posY = y + distance;
96 
97  if (posX + rect.width > mainGraphics->mWidth)
98  posX = mainGraphics->mWidth - rect.width;
99  if (posY + rect.height > mainGraphics->mHeight)
100  posY = y - rect.height - distance;
101 
102  setPosition(posX, posY);
105 }
106 
108 {
109  Popup::mouseMoved(event);
110 
111  // When the mouse moved on top of the popup, hide it
113 }
Font * getFont() const
Definition: widget.cpp:330
void show(const int x, const int y, const std::string &str1)
Definition: textpopup.h:56
int width
Definition: rect.h:218
int getWidth() const
Definition: widget.h:220
void setWidth(const int width)
Definition: widget.cpp:132
const bool Visible_true
Definition: visible.h:29
void setVisible(Visible visible)
Definition: widget.cpp:224
Definition: rect.h:72
virtual void requestMoveToTop()
Definition: widget.cpp:212
void postInit()
Definition: popup.h:176
int mWidth
Definition: graphics.h:483
int mPadding
Definition: popup.h:180
Label * mText[TEXTPOPUPCOUNT]
Definition: textpopup.h:78
void setCaption(const std::string &caption)
Definition: label.cpp:261
const bool Visible_false
Definition: visible.h:29
Definition: label.h:87
void mouseMoved(MouseEvent &event)
Definition: popup.cpp:254
virtual void add(Widget *const widget)
Graphics * mainGraphics
Definition: graphics.cpp:108
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:291
void setPosition(const int x, const int y)
Definition: widget.cpp:160
int height
Definition: rect.h:223
int mHeight
Definition: graphics.h:484
void setHeight(const int height)
Definition: widget.cpp:139
const int TEXTPOPUPCOUNT
Definition: textpopup.h:31
int getHeight() const
Definition: widget.h:239
Definition: popup.h:48
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44
void setForegroundColorAll(const Color &color1, const Color &color2)
Definition: label.cpp:214
int getHeight() const
Definition: font.cpp:361
Rect mDimension
Definition: widget.h:1100
void adjustSize()
Definition: label.cpp:197
void mouseMoved(MouseEvent &event)
Definition: textpopup.cpp:107
void postInit()
Definition: textpopup.cpp:40
TextPopup * textPopup
Definition: textpopup.cpp:32