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 
78 void EmoteShortcutContainer::setSkin(const Widget2 *const widget,
79  Skin *const skin)
80 {
81  ShortcutContainer::setSkin(widget, skin);
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,
115  (i / mGridWidth) * mBoxHeight + mImageOffsetY);
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 + mTextOffsetX,
133  emoteY + mTextOffsetY);
134  }
135 
136  BLOCK_END("EmoteShortcutContainer::draw")
137 }
138 
140 {
141  if (emoteShortcut == nullptr)
142  return;
143 
144  BLOCK_START("EmoteShortcutContainer::draw")
145  if (settings.guiAlpha != mAlpha)
146  {
147  if (mBackgroundImg != nullptr)
150  }
151 
152  Font *const font = getFont();
153  safeDrawBackground(graphics);
154 
155  unsigned sz = CAST_U32(mEmoteImg.size());
156  if (sz > mMaxItems)
157  sz = mMaxItems;
158  for (unsigned i = 0; i < sz; i++)
159  {
160  const EmoteSprite *restrict const emoteImg = mEmoteImg[i];
161  if (emoteImg != nullptr)
162  {
163  const AnimatedSprite *restrict const sprite = emoteImg->sprite;
164  if (sprite != nullptr)
165  {
166  sprite->draw(graphics,
168  (i / mGridWidth) * mBoxHeight + mImageOffsetY);
169  }
170  }
171  }
172  for (unsigned i = 0; i < mMaxItems; i++)
173  {
174  const int emoteX = (i % mGridWidth) * mBoxWidth;
175  const int emoteY = (i / mGridWidth) * mBoxHeight;
176 
177  // Draw emote keyboard shortcut.
178  const std::string key = inputManager.getKeyValueString(
180 
181  font->drawString(graphics,
184  key,
185  emoteX + mTextOffsetX,
186  emoteY + mTextOffsetY);
187  }
188 
189  BLOCK_END("EmoteShortcutContainer::draw")
190 }
191 
193  restrict2
194 {
195 }
196 
198 {
199  if (event.isConsumed())
200  return;
201 
202  if (event.getButton() == MouseButton::LEFT)
203  {
204  if (emoteShortcut == nullptr)
205  return;
206 
207  const int index = getIndexFromGrid(event.getX(), event.getY());
208 
209  if (index == -1)
210  return;
211 
212  event.consume();
213  // Stores the selected emote if there is one.
215  {
216  emoteShortcut->setEmote(index);
218  }
219  else if (emoteShortcut->getEmote(index) != 0u)
220  {
221  mEmoteClicked = true;
222  }
223  }
224  else if (event.getButton() == MouseButton::RIGHT)
225  {
226  if (popupMenu != nullptr)
227  {
228  event.consume();
230  }
231  }
232 }
233 
235  restrict2
236 {
237  if (emoteShortcut == nullptr)
238  return;
239 
240  if (event.getButton() == MouseButton::LEFT)
241  {
242  const int index = getIndexFromGrid(event.getX(), event.getY());
243 
246 
247  if (index == -1)
248  {
249  mEmoteMoved = 0;
250  return;
251  }
252 
253  if (mEmoteMoved != 0u)
254  {
256  mEmoteMoved = 0;
257  }
258  else if ((emoteShortcut->getEmote(index) != 0u) && mEmoteClicked)
259  {
260  emoteShortcut->useEmote(index + 1);
261  }
262 
263  mEmoteClicked = false;
264  }
265 }
266 
268 {
269  if ((emoteShortcut == nullptr) || (textPopup == nullptr))
270  return;
271 
272  const int index = getIndexFromGrid(event.getX(), event.getY());
273 
274  if (index == -1)
275  return;
276 
278 
279  if (CAST_SIZE(index) < mEmoteImg.size() && (mEmoteImg[index] != nullptr))
280  {
281  const EmoteSprite *restrict const sprite = mEmoteImg[index];
283  strprintf("%s, %d", sprite->name.c_str(), sprite->id));
284  }
285 }
286 
288  restrict2
289 {
290  if (textPopup != nullptr)
292 }
293 
295  restrict2
296 {
297  if (textPopup != nullptr)
299 }
#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: skin.h:35
Definition: font.h:88
void setSkin(const Widget2 *const widget, Skin *const skin)
void draw(Graphics *graphics)
void setVisible(Visible visible)
Definition: widget.cpp:224
#define BLOCK_START(name)
Definition: perfomance.h:78
void mouseMoved(MouseEvent &event)
#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:286
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)
virtual void setSkin(const Widget2 *const widget, Skin *const skin)
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:151
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:156
#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:157
TextPopup * textPopup
Definition: textpopup.cpp:32
Viewport * viewport
Definition: viewport.cpp:35