FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
visual.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2019 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 
24 // 3rd party library includes
25 
26 // FIFE includes
27 // These includes are split up in two parts, separated by one empty line
28 // First block: files included from the FIFE root src directory
29 // Second block: files included from the same folder
30 #include "util/log/logger.h"
31 #include "util/base/exception.h"
32 
34 #include "model/metamodel/object.h"
35 #include "model/metamodel/action.h"
36 
37 #include "visual.h"
38 
39 
40 namespace FIFE {
44  static Logger _log(LM_VIEW);
45 
47  }
48 
50  m_image(image) {
51  }
52 
54  m_animation(animation) {
55  }
56 
58  }
59 
61  m_image = image;
62  }
63 
65  return m_image;
66  }
67 
69  m_animation = animation;
70  }
71 
73  return m_animation;
74  }
75 
76  void OverlayColors::changeColor(const Color& source, const Color& target) {
77  std::pair<std::map<Color, Color>::iterator, bool> inserter = m_colorMap.insert(std::make_pair(source, target));
78  if (!inserter.second) {
79  Color& c = inserter.first->second;
80  c.set(target.getR(), target.getG(), target.getB(), target.getAlpha());
81  }
82  }
83 
84  const std::map<Color, Color>& OverlayColors::getColors() {
85  return m_colorMap;
86  }
87 
89  m_colorMap.clear();
90  }
91 
93  }
94 
96  }
97 
99  }
100 
102  if (object->getVisual<ObjectVisual>()) {
103  throw Duplicate("Object already contains visualization");
104  }
105  ObjectVisual* v = new ObjectVisual();
106  object->adoptVisual(v);
107  return v;
108  }
109 
111  }
112 
113  void ObjectVisual::addStaticImage(uint32_t angle, int32_t image_index) {
114  m_angle2img[angle % 360] = image_index;
115  }
116 
118  int32_t closestMatch = 0;
119  return getIndexByAngle(angle, m_angle2img, closestMatch);
120  }
121 
123  OverlayColors t = colors;
124  m_map[angle % 360] = angle % 360;
125  std::pair<AngleColorOverlayMap::iterator, bool> inserter = m_colorOverlayMap.insert(std::make_pair(angle % 360, colors));
126  if (!inserter.second) {
127  OverlayColors tmp = colors;
128  OverlayColors& c = inserter.first->second;
130 
131  const std::map<Color, Color>& colorMap = tmp.getColors();
132  std::map<Color, Color>::const_iterator it = colorMap.begin();
133  for (; it != colorMap.end(); ++it) {
134  c.changeColor(it->first, it->second);
135  }
136  }
137  }
138 
140  if (m_colorOverlayMap.empty()) {
141  return 0;
142  }
143  int32_t closestMatch = 0;
144  return &m_colorOverlayMap[getIndexByAngle(angle, m_map, closestMatch)];
145  }
146 
148  if (m_colorOverlayMap.empty()) {
149  return;
150  }
151  int32_t closestMatch = 0;
152  int32_t index = getIndexByAngle(angle, m_map, closestMatch);
153  m_colorOverlayMap.erase(index);
154  m_map.erase(index);
155  }
156 
157  int32_t ObjectVisual::getClosestMatchingAngle(int32_t angle) {
158  int32_t closestMatch = 0;
159  getIndexByAngle(angle, m_angle2img, closestMatch);
160  return closestMatch;
161  }
162 
163  void ObjectVisual::getStaticImageAngles(std::vector<int32_t>& angles) {
164  angles.clear();
165  type_angle2id::const_iterator i(m_angle2img.begin());
166  while (i != m_angle2img.end()) {
167  angles.push_back(i->first);
168  ++i;
169  }
170  }
171 
173  m_transparency(0),
174  m_visible(true),
175  m_stackposition(0),
176  m_instance(NULL) {
177  }
178 
180  if (instance->getVisual<InstanceVisual>()) {
181  throw Duplicate("Instance already contains visualization");
182  }
183  InstanceVisual* v = new InstanceVisual();
184  instance->setVisual(v);
185  v->m_instance = instance;
186  return v;
187  }
188 
190  }
191 
193  if (m_transparency != transparency) {
194  m_transparency = transparency;
196  }
197  }
198 
200  return m_transparency;
201  }
202 
203  void InstanceVisual::setVisible(bool visible) {
204  if (m_visible != visible) {
205  m_visible = visible;
207  }
208  }
209 
211  return m_visible;
212  }
213 
214  void InstanceVisual::setStackPosition(int32_t stackposition) {
215  if (m_stackposition != stackposition) {
216  m_stackposition = stackposition;
218  }
219  }
220 
222  return m_stackposition;
223  }
224 
225  ActionVisual::ActionVisual(): m_animation_map(), m_map() {
226  }
227 
229  if (action->getVisual<ActionVisual>()) {
230  throw Duplicate("Action already contains visualization");
231  }
232  ActionVisual* v = new ActionVisual();
233  action->adoptVisual(v);
234  return v;
235  }
236 
238  }
239 
241  m_animation_map[angle % 360] = animationptr;
242  m_map[angle % 360] = angle % 360;
243  }
244 
246  int32_t closestMatch = 0;
247  return m_animation_map[getIndexByAngle(angle, m_map, closestMatch)];
248  }
249 
250  void ActionVisual::addAnimationOverlay(uint32_t angle, int32_t order, AnimationPtr animationptr) {
251  std::map<int32_t, AnimationPtr>& orderMap = m_animationOverlayMap[angle % 360];
252  m_map[angle % 360] = angle % 360;
253  orderMap.insert(std::pair<int32_t, AnimationPtr>(order, animationptr));
254  }
255 
256  std::map<int32_t, AnimationPtr> ActionVisual::getAnimationOverlay(int32_t angle) {
257  int32_t closestMatch = 0;
258  return m_animationOverlayMap[getIndexByAngle(angle, m_map, closestMatch)];
259  }
260 
261  void ActionVisual::removeAnimationOverlay(uint32_t angle, int32_t order) {
262  if (m_animationOverlayMap.empty()) {
263  return;
264  }
265  int32_t closestMatch = 0;
266  AngleAnimationOverlayMap::iterator it = m_animationOverlayMap.find(getIndexByAngle(angle, m_map, closestMatch));
267  if (it != m_animationOverlayMap.end()) {
268  it->second.erase(order);
269  if (it->second.empty()) {
270  m_animationOverlayMap.erase(it);
271  }
272  }
273  }
274 
276  m_map[angle % 360] = angle % 360;
277  std::pair<AngleColorOverlayMap::iterator, bool> inserter = m_colorOverlayMap.insert(std::make_pair(angle % 360, colors));
278  if (!inserter.second) {
279  OverlayColors tmp = colors;
280  OverlayColors& c = inserter.first->second;
282 
283  const std::map<Color, Color>& colorMap = tmp.getColors();
284  std::map<Color, Color>::const_iterator it = colorMap.begin();
285  for (; it != colorMap.end(); ++it) {
286  c.changeColor(it->first, it->second);
287  }
288  }
289  }
290 
292  if (m_colorOverlayMap.empty()) {
293  return 0;
294  }
295  int32_t closestMatch = 0;
296  int32_t index = getIndexByAngle(angle, m_map, closestMatch);
297  if (m_colorOverlayMap.find(index) == m_colorOverlayMap.end()) {
298  return 0;
299  }
300  return &m_colorOverlayMap[getIndexByAngle(angle, m_map, closestMatch)];
301  }
302 
303  void ActionVisual::removeColorOverlay(int32_t angle) {
304  if (m_colorOverlayMap.empty()) {
305  return;
306  }
307  int32_t closestMatch = 0;
308  int32_t index = getIndexByAngle(angle, m_map, closestMatch);
309  m_colorOverlayMap.erase(index);
310  }
311 
312  void ActionVisual::addColorOverlay(uint32_t angle, int32_t order, const OverlayColors& colors) {
313  std::map<int32_t, OverlayColors>& orderMap = m_colorAnimationOverlayMap[angle % 360];
314  m_map[angle % 360] = angle % 360;
315  std::pair<std::map<int32_t, OverlayColors>::iterator, bool> inserter = orderMap.insert(std::make_pair(order, colors));
316  if (!inserter.second) {
317  OverlayColors tmp = colors;
318  OverlayColors& c = inserter.first->second;
320 
321  const std::map<Color, Color>& colorMap = tmp.getColors();
322  std::map<Color, Color>::const_iterator it = colorMap.begin();
323  for (; it != colorMap.end(); ++it) {
324  c.changeColor(it->first, it->second);
325  }
326  }
327  }
328 
329  OverlayColors* ActionVisual::getColorOverlay(int32_t angle, int32_t order) {
330  if (m_colorAnimationOverlayMap.empty()) {
331  return 0;
332  }
333 
334  int32_t closestMatch = 0;
335  AngleColorAnimationOverlayMap::iterator it = m_colorAnimationOverlayMap.find(getIndexByAngle(angle, m_map, closestMatch));
336  if (it != m_colorAnimationOverlayMap.end()) {
337  std::map<int32_t, OverlayColors>::iterator sit = it->second.find(order);
338  if (sit != it->second.end()) {
339  return &it->second[order];
340  }
341  }
342  return 0;
343  }
344 
345  void ActionVisual::removeColorOverlay(int32_t angle, int32_t order) {
346  if (m_colorAnimationOverlayMap.empty()) {
347  return;
348  }
349 
350  int32_t closestMatch = 0;
351  AngleColorAnimationOverlayMap::iterator it = m_colorAnimationOverlayMap.find(getIndexByAngle(angle, m_map, closestMatch));
352  if (it != m_colorAnimationOverlayMap.end()) {
353  it->second.erase(order);
354  if (it->second.empty()) {
355  m_colorAnimationOverlayMap.erase(it);
356  }
357  }
358  }
359 
360  void ActionVisual::getActionImageAngles(std::vector<int32_t>& angles) {
361  angles.clear();
362  type_angle2id::const_iterator i(m_map.begin());
363  while (i != m_map.end()) {
364  angles.push_back(i->first);
365  ++i;
366  }
367  }
368 
370  bool colorOverlay = color && !m_colorOverlayMap.empty();
371  type_angle2id::const_iterator it = m_map.begin();
372  for (; it != m_map.end(); ++it) {
373  addAnimationOverlay(it->first, 0, getAnimationByAngle(it->first));
374  if (colorOverlay) {
375  OverlayColors* oldC = getColorOverlay(it->first);
376  if (oldC) {
377  OverlayColors c = OverlayColors(*oldC);
378  addColorOverlay(it->first, 0, c);
379  }
380  }
381  }
382  }
383 }
static InstanceVisual * create(Instance *instance)
Constructs and assigns it to the passed item.
Definition: visual.cpp:179
void callOnVisibleChange()
Definition: instance.cpp:954
void setColorOverlayAnimation(AnimationPtr animation)
Definition: visual.cpp:68
ImagePtr m_image
Definition: visual.h:68
ObjectVisual()
Constructor.
Definition: visual.cpp:98
AngleAnimationMap m_animation_map
Definition: visual.h:297
int32_t getClosestMatchingAngle(int32_t angle)
Returns closest matching image angle for given angle.
Definition: visual.cpp:157
AngleColorAnimationOverlayMap m_colorAnimationOverlayMap
Definition: visual.h:306
int32_t getStackPosition()
Gets current stack position of instance.
Definition: visual.cpp:221
std::map< Color, Color > m_colorMap
Definition: visual.h:67
Visual2DGfx()
Constructor.
Definition: visual.cpp:92
Object class.
Definition: object.h:51
Instance visual contains data that is needed to visualize the instance on screen. ...
Definition: visual.h:158
void convertToOverlays(bool color)
Convertes animations and optional color overlay to default animation overlay.
Definition: visual.cpp:369
ActionVisual()
Constructor.
Definition: visual.cpp:225
Action visual contains data that is needed to visualize different actions on screen.
Definition: visual.h:212
virtual ~Visual2DGfx()
Destructor.
Definition: visual.cpp:95
static ActionVisual * create(Action *action)
Constructs and assigns it to the passed item.
Definition: visual.cpp:228
uint8_t getB() const
Definition: color.cpp:80
void removeAnimationOverlay(uint32_t angle, int32_t order)
Removes animation overlay with given angle (degrees) and order.
Definition: visual.cpp:261
static Logger _log(LM_AUDIO)
void setVisual(IVisual *visual)
Sets visualization to be used.
Definition: instance.h:338
void removeStaticColorOverlay(int32_t angle)
Removes a static color overlay with given angle (degrees).
Definition: visual.cpp:147
void setTransparency(uint8_t transparency)
Sets transparency value for object to be visualized.
Definition: visual.cpp:192
void callOnTransparencyChange()
Definition: instance.cpp:949
OverlayColors * getStaticColorOverlay(int32_t angle)
Returns closest matching static color overlay for given angle.
Definition: visual.cpp:139
uint8_t getAlpha() const
Definition: color.cpp:84
void set(uint8_t r, uint8_t g, uint8_t b, uint8_t alpha)
Set all color channel values.
Definition: color.cpp:49
int32_t m_stackposition
Definition: visual.h:206
OverlayColors * getColorOverlay(int32_t angle)
Gets OverlayColors for given angle (degrees).
Definition: visual.cpp:291
int32_t getStaticImageIndexByAngle(int32_t angle)
Returns closest matching static image for given angle.
Definition: visual.cpp:117
unsigned char uint8_t
Definition: core.h:38
AngleColorOverlayMap m_colorOverlayMap
Definition: visual.h:303
bool isVisible()
Is instance visible or not.
Definition: visual.cpp:210
void changeColor(const Color &source, const Color &target)
Definition: visual.cpp:76
void removeColorOverlay(int32_t angle)
Removes color overlay with given angle (degrees).
Definition: visual.cpp:303
AngleAnimationOverlayMap m_animationOverlayMap
Definition: visual.h:300
void setColorOverlayImage(ImagePtr image)
Definition: visual.cpp:60
void setVisible(bool visible)
Sets visibility value for object to be visualized.
Definition: visual.cpp:203
void getActionImageAngles(std::vector< int32_t > &angles)
Returns list of available angles for this Action.
Definition: visual.cpp:360
void addColorOverlay(uint32_t angle, const OverlayColors &colors)
Adds new color overlay with given angle (degrees) and colors.
Definition: visual.cpp:275
void callOnStackPositionChange()
Definition: instance.cpp:959
void addAnimationOverlay(uint32_t angle, int32_t order, AnimationPtr animationptr)
Adds new animation overlay with given angle (degrees) and order.
Definition: visual.cpp:250
void getStaticImageAngles(std::vector< int32_t > &angles)
Returns list of available static image angles for this object.
Definition: visual.cpp:163
type_angle2id m_map
Definition: visual.h:308
const std::map< Color, Color > & getColors()
Definition: visual.cpp:84
void addAnimation(uint32_t angle, AnimationPtr animationptr)
Adds new animation with given angle (degrees)
Definition: visual.cpp:240
void addStaticColorOverlay(uint32_t angle, const OverlayColors &colors)
Adds new static color overlay with given angle (degrees).
Definition: visual.cpp:122
Instance * m_instance
Definition: visual.h:207
ImagePtr getColorOverlayImage()
Definition: visual.cpp:64
uint8_t m_transparency
Definition: visual.h:204
virtual ~ActionVisual()
Destructor.
Definition: visual.cpp:237
InstanceVisual()
Constructor.
Definition: visual.cpp:172
Object visual contains data that is needed for visualizing objects.
Definition: visual.h:91
static ObjectVisual * create(Object *object)
Constructs and assigns it to the passed item.
Definition: visual.cpp:101
void addStaticImage(uint32_t angle, int32_t image_index)
Adds new static image with given angle (degrees) Static images are used in case there are no actions ...
Definition: visual.cpp:113
AnimationPtr m_animation
Definition: visual.h:69
AnimationPtr getAnimationByAngle(int32_t angle)
Gets index to animation closest to given angle.
Definition: visual.cpp:245
uint8_t getTransparency()
Gets current transparency value (0-255)
Definition: visual.cpp:199
uint8_t getG() const
Definition: color.cpp:76
void setStackPosition(int32_t stackposition)
Sets stack position of the instance Stack position is used to define the order in which instances res...
Definition: visual.cpp:214
int32_t getIndexByAngle(int32_t angle, const type_angle2id &angle2id, int32_t &closestMatchingAngle)
Returns id for given angle from angle2id map in case there are no elements in the map...
Definition: angles.cpp:37
virtual ~ObjectVisual()
Destructor.
Definition: visual.cpp:110
uint8_t getR() const
Definition: color.cpp:72
T * getVisual() const
Gets used visualization.
Definition: instance.h:342
virtual ~InstanceVisual()
Destructor.
Definition: visual.cpp:189
AnimationPtr getColorOverlayAnimation()
Definition: visual.cpp:72
OverlayColors()
Constructors.
Definition: visual.cpp:46
T * getVisual() const
Gets used visualization.
Definition: action.h:74
unsigned int uint32_t
Definition: core.h:40
T * getVisual() const
Gets used visualization.
Definition: object.h:122
void resetColors()
Definition: visual.cpp:88
~OverlayColors()
Destructor.
Definition: visual.cpp:57
An Instance is an "instantiation" of an Object at a Location.
Definition: instance.h:94
std::map< int32_t, AnimationPtr > getAnimationOverlay(int32_t angle)
Gets map with animations closest to given angle.
Definition: visual.cpp:256
void adoptVisual(IVisual *visual)
Sets visualization to be used.
Definition: action.h:70