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