ManaPlus
spellpopup.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) 2009 The Mana World Development Team
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2009-2021 Andrei Karas
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/spellpopup.h"
25 
26 #include "textcommand.h"
27 
28 #include "gui/gui.h"
29 
30 #include "gui/widgets/label.h"
31 
32 #include "debug.h"
33 
35 
37  Popup("SpellPopup", "spellpopup.xml"),
38  mItemName(new Label(this)),
39  mItemComment(new Label(this))
40 {
43  getThemeColor(ThemeColorId::POPUP, 255U),
44  getThemeColor(ThemeColorId::POPUP_OUTLINE, 255U));
46  getThemeColor(ThemeColorId::POPUP, 255U),
47  getThemeColor(ThemeColorId::POPUP_OUTLINE, 255U));
48 }
49 
51 {
53  add(mItemName);
55 
56  addMouseListener(this);
57 }
58 
60 {
61 }
62 
63 void SpellPopup::setItem(const TextCommand *const spell)
64 {
65  if (spell != nullptr)
66  {
67  mItemName->setCaption(spell->getName());
69  }
70  else
71  {
72  mItemName->setCaption("?");
74  }
75 
78  int minWidth = mItemName->getWidth();
79  if (mItemComment->getWidth() > minWidth)
80  minWidth = mItemComment->getWidth();
81 
82  mItemName->setPosition(0, 0);
83  const int fontHeight = mItemName->getHeight();
84  mItemComment->setPosition(0, fontHeight);
85  if (!mItemComment->getCaption().empty())
86  setContentSize(minWidth, 2 * fontHeight);
87  else
88  setContentSize(minWidth, fontHeight);
89 }
90 
91 void SpellPopup::view(const int x, const int y)
92 {
93  const int distance = 20;
94 
95  int posX = std::max(0, x - getWidth() / 2);
96  int posY = y + distance;
97 
98  const Rect &rect = mDimension;
99  const int w = rect.width;
100  const int h = rect.height;
101  if (posX + w > mainGraphics->mWidth)
102  {
103  if (mainGraphics->mWidth > w)
104  posX = mainGraphics->mWidth - w;
105  else
106  posX = 0;
107  }
108  if (posY + h > mainGraphics->mHeight)
109  {
110  if (y > h + distance)
111  posY = y - h - distance;
112  }
113 
114  setPosition(posX, posY);
117 }
118 
120 {
121  Popup::mouseMoved(event);
122 
123  // When the mouse moved on top of the popup, hide it
125 }
virtual void add(Widget *const widget)
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
const std::string & getCaption() const
Definition: label.h:133
void setCaption(const std::string &caption)
Definition: label.cpp:264
Definition: popup.h:52
void setContentSize(int width, int height)
Definition: popup.cpp:155
void mouseMoved(MouseEvent &event)
Definition: popup.cpp:255
void postInit()
Definition: popup.h:177
Definition: rect.h:74
int width
Definition: rect.h:219
int height
Definition: rect.h:224
void view(const int x, const int y)
Definition: spellpopup.cpp:91
void mouseMoved(MouseEvent &event)
Definition: spellpopup.cpp:119
Label * mItemName
Definition: spellpopup.h:65
void postInit()
Definition: spellpopup.cpp:50
Label * mItemComment
Definition: spellpopup.h:66
void setItem(const TextCommand *const spell)
Definition: spellpopup.cpp:63
std::string getName() const
Definition: textcommand.h:82
std::string getComment() const
Definition: textcommand.h:88
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
virtual void requestMoveToTop()
Definition: widget.cpp:213
Rect mDimension
Definition: widget.h:1101
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:292
void setFont(Font *const font)
Definition: widget.cpp:349
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
#define new
Definition: debug_new.h:147
Graphics * mainGraphics
Definition: graphics.cpp:109
Font * boldFont
Definition: gui.cpp:112
SpellPopup * spellPopup
Definition: spellpopup.cpp:34
const bool Visible_false
Definition: visible.h:30
const bool Visible_true
Definition: visible.h:30