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-2019 The ManaPlus Developers
7  * Copyright (C) 2019-2021 Andrei Karas
8  *
9  * This file is part of The ManaPlus Client.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 #include "gui/popups/textpopup.h"
26 
27 #include "gui/widgets/label.h"
28 
29 #include "gui/fonts/font.h"
30 
31 #include "debug.h"
32 
33 TextPopup *textPopup = nullptr;
34 
36  Popup("TextPopup", "textpopup.xml"),
37  mText()
38 {
39 }
40 
42 {
44  const int fontHeight = getFont()->getHeight();
45  int y = 0;
46  for (int f = 0; f < TEXTPOPUPCOUNT; f ++)
47  {
48  Label *const label = new Label(this);
49  mText[f] = label;
50  label->setPosition(0, y);
51  label->setForegroundColorAll(
52  getThemeColor(ThemeColorId::POPUP, 255U),
53  getThemeColor(ThemeColorId::POPUP_OUTLINE, 255U));
54  add(label);
55  y += fontHeight;
56  }
57  addMouseListener(this);
58 }
59 
61 {
62 }
63 
64 void TextPopup::show(const int x, const int y, const std::string &str1,
65  const std::string &str2, const std::string &str3)
66 {
67  mText[0]->setCaption(str1);
68  mText[1]->setCaption(str2);
69  mText[2]->setCaption(str3);
70 
71  int minWidth = 0;
72  for (int f = 0; f < TEXTPOPUPCOUNT; f ++)
73  {
74  Label *const label = mText[f];
75  label->adjustSize();
76  const int width = label->getWidth();
77  if (width > minWidth)
78  minWidth = width;
79  }
80 
81  const int pad2 = 2 * mPadding;
82  minWidth += pad2;
83  setWidth(minWidth);
84 
85  int cnt = 1;
86  if (!str2.empty())
87  cnt ++;
88  if (!str3.empty())
89  cnt ++;
90 
91  setHeight(pad2 + mText[0]->getFont()->getHeight() * cnt);
92  const int distance = 20;
93 
94  const Rect &rect = mDimension;
95  int posX = std::max(0, x - rect.width / 2);
96  int posY = y + distance;
97 
98  if (posX + rect.width > mainGraphics->mWidth)
99  posX = mainGraphics->mWidth - rect.width;
100  if (posY + rect.height > mainGraphics->mHeight)
101  posY = y - rect.height - distance;
102 
103  setPosition(posX, posY);
106 }
107 
109 {
110  Popup::mouseMoved(event);
111 
112  // When the mouse moved on top of the popup, hide it
114 }
virtual void add(Widget *const widget)
int getHeight() const
Definition: font.cpp:362
int mWidth
Definition: graphics.h:484
int mHeight
Definition: graphics.h:485
Definition: label.h:91
void adjustSize()
Definition: label.cpp:200
void setForegroundColorAll(const Color &color1, const Color &color2)
Definition: label.cpp:217
void setCaption(const std::string &caption)
Definition: label.cpp:264
Definition: popup.h:52
void mouseMoved(MouseEvent &event)
Definition: popup.cpp:255
int mPadding
Definition: popup.h:181
void postInit()
Definition: popup.h:177
Definition: rect.h:74
int width
Definition: rect.h:219
int height
Definition: rect.h:224
Label * mText[TEXTPOPUPCOUNT]
Definition: textpopup.h:79
void postInit()
Definition: textpopup.cpp:41
void mouseMoved(MouseEvent &event)
Definition: textpopup.cpp:108
void show(const int x, const int y, const std::string &str1)
Definition: textpopup.h:57
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:45
void setVisible(Visible visible)
Definition: widget.cpp:225
void setWidth(const int width)
Definition: widget.cpp:133
virtual void requestMoveToTop()
Definition: widget.cpp:213
Rect mDimension
Definition: widget.h:1101
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:292
void setHeight(const int height)
Definition: widget.cpp:140
Font * getFont() const
Definition: widget.cpp:331
void setPosition(const int x, const int y)
Definition: widget.cpp:161
int getHeight() const
Definition: widget.h:240
int getWidth() const
Definition: widget.h:221
Graphics * mainGraphics
Definition: graphics.cpp:109
TextPopup * textPopup
Definition: textpopup.cpp:33
const int TEXTPOPUPCOUNT
Definition: textpopup.h:32
const bool Visible_false
Definition: visible.h:30
const bool Visible_true
Definition: visible.h:30