ManaPlus
emotepage.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2013-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
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 
22 #include "gui/widgets/emotepage.h"
23 
24 #include "render/graphics.h"
25 
27 
28 #include "resources/imageset.h"
29 
31 
32 #include "utils/delete2.h"
33 #include "utils/foreach.h"
34 
35 #include "debug.h"
36 
37 namespace
38 {
39  const unsigned int emoteWidth = 17;
40  const unsigned int emoteHeight = 18;
41 } // namespace
42 
43 EmotePage::EmotePage(const Widget2 *const widget) :
44  Widget(widget),
45  MouseListener(),
47  mEmotes(Loader::getImageSet(
48  "graphics/sprites/chatemotes.png", emoteWidth, emoteHeight)),
49  mVertexes(new ImageCollection),
50  mSelectedIndex(-1)
51 {
52  addMouseListener(this);
53  addWidgetListener(this);
54  mAllowLogic = false;
55 }
56 
58 {
59  if (mEmotes != nullptr)
60  {
61  mEmotes->decRef();
62  mEmotes = nullptr;
63  }
65 }
66 
67 void EmotePage::draw(Graphics *const graphics)
68 {
69  BLOCK_START("EmotePage::draw")
70 
71  if (mRedraw)
72  {
73  if (mEmotes == nullptr)
74  return;
75 
76  const STD_VECTOR<Image*> &images = mEmotes->getImages();
77 
78  const unsigned int width = mDimension.width;
79  unsigned int x = 0;
80  unsigned int y = 0;
81 
82  mRedraw = false;
83  mVertexes->clear();
84  FOR_EACH (STD_VECTOR<Image*>::const_iterator, it, images)
85  {
86  const Image *const image = *it;
87  if (image != nullptr)
88  {
89  graphics->calcTileCollection(mVertexes, image, x, y);
90  x += emoteWidth;
91  if (x + emoteWidth > width)
92  {
93  x = 0;
94  y += emoteHeight;
95  }
96  }
97  }
98  graphics->finalize(mVertexes);
99  }
100  graphics->drawTileCollection(mVertexes);
101 
102  BLOCK_END("EmotePage::draw")
103 }
104 
105 void EmotePage::safeDraw(Graphics *const graphics)
106 {
107  BLOCK_START("EmotePage::safeDraw")
108 
109  if (mEmotes == nullptr)
110  return;
111 
112  const STD_VECTOR<Image*> &images = mEmotes->getImages();
113 
114  const unsigned int width = mDimension.width;
115  unsigned int x = 0;
116  unsigned int y = 0;
117 
118  FOR_EACH (STD_VECTOR<Image*>::const_iterator, it, images)
119  {
120  const Image *const image = *it;
121  if (image != nullptr)
122  {
123  graphics->drawImage(image, x, y);
124  x += emoteWidth;
125  if (x + emoteWidth > width)
126  {
127  x = 0;
128  y += emoteHeight;
129  }
130  }
131  }
132 
133  BLOCK_END("EmotePage::safeDraw")
134 }
135 
137 {
138  mSelectedIndex = getIndexFromGrid(event.getX(), event.getY());
139  event.consume();
141 }
142 
143 int EmotePage::getIndexFromGrid(const int x, const int y) const
144 {
145  const int width = mDimension.width;
146  if (x < 0 || x > width || y < 0 || y > mDimension.height)
147  return -1;
148  const int cols = width / emoteWidth;
149  const int index = (y / emoteHeight) * cols + (x / emoteWidth);
150  if (index >= CAST_S32(mEmotes->size()))
151  return -1;
152  return index;
153 }
154 
156 {
157  mSelectedIndex = -1;
158 }
159 
161 {
162  mRedraw = true;
163 }
164 
166 {
167  mRedraw = true;
168 }
#define CAST_S32
Definition: cast.h:30
int mSelectedIndex
Definition: emotepage.h:65
void safeDraw(Graphics *const graphics)
Definition: emotepage.cpp:105
void mousePressed(MouseEvent &event)
Definition: emotepage.cpp:136
void widgetMoved(const Event &event)
Definition: emotepage.cpp:165
int getIndexFromGrid(const int x, const int y) const
Definition: emotepage.cpp:143
ImageCollection * mVertexes
Definition: emotepage.h:64
void resetAction()
Definition: emotepage.cpp:155
EmotePage(const Widget2 *const widget)
Definition: emotepage.cpp:43
void widgetResized(const Event &event)
Definition: emotepage.cpp:160
ImageSet * mEmotes
Definition: emotepage.h:63
void draw(Graphics *const graphics)
Definition: emotepage.cpp:67
Definition: event.h:79
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
virtual void drawTileCollection(const ImageCollection *const vertCol)=0
virtual void calcTileCollection(ImageCollection *const vertCol, const Image *const image, int x, int y)=0
virtual void finalize(ImageCollection *const col)
Definition: graphics.h:465
size_type size() const
Definition: imageset.h:73
const std::vector< Image * > & getImages() const
Definition: imageset.h:88
int getX() const
Definition: mouseevent.h:127
int getY() const
Definition: mouseevent.h:138
int width
Definition: rect.h:219
int height
Definition: rect.h:224
virtual void decRef()
Definition: resource.cpp:50
Definition: widget.h:99
void distributeActionEvent()
Definition: widget.cpp:493
Rect mDimension
Definition: widget.h:1101
bool mAllowLogic
Definition: widget.h:1160
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:292
void addWidgetListener(WidgetListener *const widgetListener)
Definition: widget.cpp:302
bool mRedraw
Definition: widget.h:1164
#define new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
#define A_UNUSED
Definition: localconsts.h:160
ImageSet * getImageSet(const std::string &imagePath, const int w, const int h)
const unsigned int emoteHeight
Definition: emotepage.cpp:40
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79