ManaPlus
emoteshortcutcontainer.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2009 Aethyra Development Team
4  * Copyright (C) 2011-2018 The ManaPlus Developers
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
23 
24 #include "settings.h"
25 
26 #include "input/inputmanager.h"
27 
28 #include "gui/viewport.h"
29 
30 #include "gui/fonts/font.h"
31 
33 
34 #include "gui/popups/popupmenu.h"
35 #include "gui/popups/textpopup.h"
36 
38 
39 #include "resources/emotesprite.h"
40 
41 #include "resources/db/emotedb.h"
42 
43 #include "resources/image/image.h"
44 
46 
47 #include "utils/stringutils.h"
48 
49 #include "debug.h"
50 
51 static const int MAX_ITEMS = 48;
52 
54  widget) :
55  ShortcutContainer(widget),
56  mEmoteImg(),
57  mEmoteClicked(false),
58  mEmoteMoved(0)
59 {
60  if (mBackgroundImg != nullptr)
62 
63  // Setup emote sprites
64  for (int i = 0; i <= EmoteDB::getLast(); i++)
65  {
66  const EmoteSprite *const sprite = EmoteDB::getSprite(i, true);
67  if ((sprite != nullptr) && (sprite->sprite != nullptr))
68  mEmoteImg.push_back(sprite);
69  }
70 
72 }
73 
75 {
76 }
77 
79  restrict2
80 {
81  Widget2::setWidget2(widget);
84 }
85 
87 {
88  if (emoteShortcut == nullptr)
89  return;
90 
91  BLOCK_START("EmoteShortcutContainer::draw")
92  if (settings.guiAlpha != mAlpha)
93  {
94  if (mBackgroundImg != nullptr)
97  }
98 
99  Font *const font = getFont();
100  drawBackground(graphics);
101 
102  unsigned sz = CAST_U32(mEmoteImg.size());
103  if (sz > mMaxItems)
104  sz = mMaxItems;
105  for (unsigned i = 0; i < sz; i++)
106  {
107  const EmoteSprite *restrict const emoteImg = mEmoteImg[i];
108  if (emoteImg != nullptr)
109  {
110  const AnimatedSprite *restrict const sprite = emoteImg->sprite;
111  if (sprite != nullptr)
112  {
113  sprite->draw(graphics,
114  (i % mGridWidth) * mBoxWidth + 2,
115  (i / mGridWidth) * mBoxHeight + 10);
116  }
117  }
118  }
119  for (unsigned i = 0; i < mMaxItems; i++)
120  {
121  const int emoteX = (i % mGridWidth) * mBoxWidth;
122  const int emoteY = (i / mGridWidth) * mBoxHeight;
123 
124  // Draw emote keyboard shortcut.
125  const std::string key = inputManager.getKeyValueString(
127 
128  font->drawString(graphics,
131  key,
132  emoteX + 2, emoteY + 2);
133  }
134 
135  BLOCK_END("EmoteShortcutContainer::draw")
136 }
137 
139 {
140  if (emoteShortcut == nullptr)
141  return;
142 
143  BLOCK_START("EmoteShortcutContainer::draw")
144  if (settings.guiAlpha != mAlpha)
145  {
146  if (mBackgroundImg != nullptr)
149  }
150 
151  Font *const font = getFont();
152  safeDrawBackground(graphics);
153 
154  unsigned sz = CAST_U32(mEmoteImg.size());
155  if (sz > mMaxItems)
156  sz = mMaxItems;
157  for (unsigned i = 0; i < sz; i++)
158  {
159  const EmoteSprite *restrict const emoteImg = mEmoteImg[i];
160  if (emoteImg != nullptr)
161  {
162  const AnimatedSprite *restrict const sprite = emoteImg->sprite;
163  if (sprite != nullptr)
164  {
165  sprite->draw(graphics,
166  (i % mGridWidth) * mBoxWidth + 2,
167  (i / mGridWidth) * mBoxHeight + 10);
168  }
169  }
170  }
171  for (unsigned i = 0; i < mMaxItems; i++)
172  {
173  const int emoteX = (i % mGridWidth) * mBoxWidth;
174  const int emoteY = (i / mGridWidth) * mBoxHeight;
175 
176  // Draw emote keyboard shortcut.
177  const std::string key = inputManager.getKeyValueString(
179 
180  font->drawString(graphics,
183  key,
184  emoteX + 2, emoteY + 2);
185  }
186 
187  BLOCK_END("EmoteShortcutContainer::draw")
188 }
189 
191  restrict2
192 {
193 }
194 
196 {
197  if (event.isConsumed())
198  return;
199 
200  if (event.getButton() == MouseButton::LEFT)
201  {
202  if (emoteShortcut == nullptr)
203  return;
204 
205  const int index = getIndexFromGrid(event.getX(), event.getY());
206 
207  if (index == -1)
208  return;
209 
210  event.consume();
211  // Stores the selected emote if there is one.
213  {
214  emoteShortcut->setEmote(index);
216  }
217  else if (emoteShortcut->getEmote(index) != 0u)
218  {
219  mEmoteClicked = true;
220  }
221  }
222  else if (event.getButton() == MouseButton::RIGHT)
223  {
224  if (popupMenu != nullptr)
225  {
226  event.consume();
228  }
229  }
230 }
231 
233  restrict2
234 {
235  if (emoteShortcut == nullptr)
236  return;
237 
238  if (event.getButton() == MouseButton::LEFT)
239  {
240  const int index = getIndexFromGrid(event.getX(), event.getY());
241 
244 
245  if (index == -1)
246  {
247  mEmoteMoved = 0;
248  return;
249  }
250 
251  if (mEmoteMoved != 0u)
252  {
254  mEmoteMoved = 0;
255  }
256  else if ((emoteShortcut->getEmote(index) != 0u) && mEmoteClicked)
257  {
258  emoteShortcut->useEmote(index + 1);
259  }
260 
261  mEmoteClicked = false;
262  }
263 }
264 
266 {
267  if ((emoteShortcut == nullptr) || (textPopup == nullptr))
268  return;
269 
270  const int index = getIndexFromGrid(event.getX(), event.getY());
271 
272  if (index == -1)
273  return;
274 
276 
277  if (CAST_SIZE(index) < mEmoteImg.size() && (mEmoteImg[index] != nullptr))
278  {
279  const EmoteSprite *restrict const sprite = mEmoteImg[index];
281  strprintf("%s, %d", sprite->name.c_str(), sprite->id));
282  }
283 }
284 
286  restrict2
287 {
288  if (textPopup != nullptr)
290 }
291 
293  restrict2
294 {
295  if (textPopup != nullptr)
297 }
#define CAST_U32
Definition: cast.h:30
Font * getFont() const
Definition: widget.cpp:330
void show(const int x, const int y, const std::string &str1)
Definition: textpopup.h:56
Definition: font.h:88
void draw(Graphics *graphics)
void setVisible(Visible visible)
Definition: widget.cpp:224
virtual void setWidget2(const Widget2 *const widget)
Definition: widget2.h:63
#define BLOCK_START(name)
Definition: perfomance.h:78
void mouseMoved(MouseEvent &event)
void setWidget2(const Widget2 *const widget)
#define BLOCK_END(name)
Definition: perfomance.h:79
int mMouseX
Definition: viewport.h:153
const int & getLast() A_CONST
Definition: emotedb.cpp:300
Color mForegroundColor
Definition: widget.h:1085
EmoteShortcutContainer(Widget2 *const widget)
void safeDraw(Graphics *graphics)
int getIndexFromGrid(const int pointX, const int pointY) const
Settings settings
Definition: settings.cpp:31
virtual void setAlpha(const float alpha)
Definition: image.cpp:285
void mouseReleased(MouseEvent &event)
std::string strprintf(const char *const format,...)
Definition: stringutils.cpp:99
const bool Visible_false
Definition: visible.h:29
void setEmoteSelected(const unsigned char emoteId)
Definition: emoteshortcut.h:97
void mouseExited(MouseEvent &event)
std::string getKeyValueString(const InputActionT index) const
PopupMenu * popupMenu
Definition: popupmenu.cpp:102
void drawString(Graphics *const graphics, Color col, const Color &col2, const std::string &text, const int x, const int y)
Definition: font.cpp:253
unsigned char getEmote(const size_t index) const
Definition: emoteshortcut.h:59
void showEmoteType()
Definition: popupmenu.cpp:1214
bool isEmoteSelected() const
void mousePressed(MouseEvent &event)
int mMouseY
Definition: viewport.h:154
void drawBackground(Graphics *const g)
void setEmote(const size_t index)
Definition: emoteshortcut.h:79
EmoteShortcut * emoteShortcut
void mouseDragged(MouseEvent &event)
#define A_UNUSED
Definition: localconsts.h:171
Color mForegroundColor2
Definition: widget2.h:112
const EmoteSprite * getSprite(const int id, const bool allowNull)
Definition: emotedb.cpp:291
Definition: event.h:77
void useEmote(const size_t index) const
void widgetHidden(const Event &event)
static const int MAX_ITEMS
void safeDrawBackground(Graphics *const g)
#define restrict
Definition: localconsts.h:176
#define CAST_SIZE
Definition: cast.h:33
const Color & getThemeColor(const ThemeColorIdT type, const unsigned int alpha) const A_INLINE
Definition: widget2.h:44
InputManager inputManager
std::vector< const EmoteSprite * > mEmoteImg
float guiAlpha
Definition: settings.h:129
const AnimatedSprite * sprite
Definition: emotesprite.h:41
void setEmotes(const size_t index, const unsigned char emoteId)
Definition: emoteshortcut.h:88
#define restrict2
Definition: localconsts.h:177
TextPopup * textPopup
Definition: textpopup.cpp:32
Viewport * viewport
Definition: viewport.cpp:35