ManaPlus
emotepage.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2013-2018 The ManaPlus Developers
4  *
5  * This file is part of The ManaPlus Client.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "gui/widgets/emotepage.h"
22 
23 #include "render/graphics.h"
24 
26 
27 #include "resources/imageset.h"
28 
30 
31 #include "utils/delete2.h"
32 #include "utils/foreach.h"
33 
34 #include "debug.h"
35 
36 namespace
37 {
38  const unsigned int emoteWidth = 17;
39  const unsigned int emoteHeight = 18;
40 } // namespace
41 
42 EmotePage::EmotePage(const Widget2 *const widget) :
43  Widget(widget),
44  MouseListener(),
46  mEmotes(Loader::getImageSet(
47  "graphics/sprites/chatemotes.png", emoteWidth, emoteHeight)),
48  mVertexes(new ImageCollection),
49  mSelectedIndex(-1)
50 {
51  addMouseListener(this);
52  addWidgetListener(this);
53  mAllowLogic = false;
54 }
55 
57 {
58  if (mEmotes != nullptr)
59  {
60  mEmotes->decRef();
61  mEmotes = nullptr;
62  }
64 }
65 
66 void EmotePage::draw(Graphics *const graphics)
67 {
68  BLOCK_START("EmotePage::draw")
69 
70  if (mRedraw)
71  {
72  if (mEmotes == nullptr)
73  return;
74 
75  const STD_VECTOR<Image*> &images = mEmotes->getImages();
76 
77  const unsigned int width = mDimension.width;
78  unsigned int x = 0;
79  unsigned int y = 0;
80 
81  mRedraw = false;
82  mVertexes->clear();
83  FOR_EACH (STD_VECTOR<Image*>::const_iterator, it, images)
84  {
85  const Image *const image = *it;
86  if (image != nullptr)
87  {
88  graphics->calcTileCollection(mVertexes, image, x, y);
89  x += emoteWidth;
90  if (x + emoteWidth > width)
91  {
92  x = 0;
93  y += emoteHeight;
94  }
95  }
96  }
97  graphics->finalize(mVertexes);
98  }
99  graphics->drawTileCollection(mVertexes);
100 
101  BLOCK_END("EmotePage::draw")
102 }
103 
104 void EmotePage::safeDraw(Graphics *const graphics)
105 {
106  BLOCK_START("EmotePage::safeDraw")
107 
108  if (mEmotes == nullptr)
109  return;
110 
111  const STD_VECTOR<Image*> &images = mEmotes->getImages();
112 
113  const unsigned int width = mDimension.width;
114  unsigned int x = 0;
115  unsigned int y = 0;
116 
117  FOR_EACH (STD_VECTOR<Image*>::const_iterator, it, images)
118  {
119  const Image *const image = *it;
120  if (image != nullptr)
121  {
122  graphics->drawImage(image, x, y);
123  x += emoteWidth;
124  if (x + emoteWidth > width)
125  {
126  x = 0;
127  y += emoteHeight;
128  }
129  }
130  }
131 
132  BLOCK_END("EmotePage::safeDraw")
133 }
134 
136 {
137  mSelectedIndex = getIndexFromGrid(event.getX(), event.getY());
138  event.consume();
140 }
141 
142 int EmotePage::getIndexFromGrid(const int x, const int y) const
143 {
144  const int width = mDimension.width;
145  if (x < 0 || x > width || y < 0 || y > mDimension.height)
146  return -1;
147  const int cols = width / emoteWidth;
148  const int index = (y / emoteHeight) * cols + (x / emoteWidth);
149  if (index >= CAST_S32(mEmotes->size()))
150  return -1;
151  return index;
152 }
153 
155 {
156  mSelectedIndex = -1;
157 }
158 
160 {
161  mRedraw = true;
162 }
163 
165 {
166  mRedraw = true;
167 }
int getIndexFromGrid(const int x, const int y) const
Definition: emotepage.cpp:142
const std::vector< Image * > & getImages() const
Definition: imageset.h:87
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
int width
Definition: rect.h:218
void safeDraw(Graphics *const graphics)
Definition: emotepage.cpp:104
void mousePressed(MouseEvent &event)
Definition: emotepage.cpp:135
virtual void drawTileCollection(const ImageCollection *const vertCol)=0
virtual void decRef()
Definition: resource.cpp:49
#define BLOCK_START(name)
Definition: perfomance.h:78
ImageSet * getImageSet(const std::string &imagePath, const int w, const int h)
void widgetMoved(const Event &event)
Definition: emotepage.cpp:164
#define BLOCK_END(name)
Definition: perfomance.h:79
#define delete2(var)
Definition: delete2.h:24
EmotePage(const Widget2 *const widget)
Definition: emotepage.cpp:42
virtual void finalize(ImageCollection *const col)
Definition: graphics.h:464
void distributeActionEvent()
Definition: widget.cpp:492
#define new
Definition: debug_new.h:147
#define CAST_S32
Definition: cast.h:29
void resetAction()
Definition: emotepage.cpp:154
ImageSet * mEmotes
Definition: emotepage.h:62
int mSelectedIndex
Definition: emotepage.h:64
size_type size() const
Definition: imageset.h:72
bool mAllowLogic
Definition: widget.h:1159
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:291
int height
Definition: rect.h:223
void widgetResized(const Event &event)
Definition: emotepage.cpp:159
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:301
#define A_UNUSED
Definition: localconsts.h:151
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
Definition: widget.h:97
Definition: event.h:77
bool mRedraw
Definition: widget.h:1163
ImageCollection * mVertexes
Definition: emotepage.h:63
Definition: image.h:61
virtual void calcTileCollection(ImageCollection *const vertCol, const Image *const image, int x, int y)=0
void draw(Graphics *const graphics)
Definition: emotepage.cpp:66
int getX() const
Definition: mouseevent.h:126
const unsigned int emoteHeight
Definition: emotepage.cpp:39
Rect mDimension
Definition: widget.h:1100