FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
visual.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 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  }
51 
53  }
54 
56  if (object->getVisual<ObjectVisual>()) {
57  throw Duplicate("Object already contains visualization");
58  }
59  ObjectVisual* v = new ObjectVisual();
60  object->adoptVisual(v);
61  return v;
62  }
63 
65  }
66 
67  void ObjectVisual::addStaticImage(uint32_t angle, int32_t image_index) {
68  m_angle2img[angle % 360] = image_index;
69  }
70 
72  int32_t closestMatch = 0;
73  return getIndexByAngle(angle, m_angle2img, closestMatch);
74  }
75 
76  int32_t ObjectVisual::getClosestMatchingAngle(int32_t angle) {
77  int32_t closestMatch = 0;
78  getIndexByAngle(angle, m_angle2img, closestMatch);
79  return closestMatch;
80  }
81 
82  void ObjectVisual::getStaticImageAngles(std::vector<int32_t>& angles) {
83  angles.clear();
84  type_angle2id::const_iterator i(m_angle2img.begin());
85  while (i != m_angle2img.end()) {
86  angles.push_back(i->first);
87  ++i;
88  }
89  }
90 
92  m_transparency(0),
93  m_visible(true),
94  m_stackposition(0),
95  m_instance(NULL) {
96  }
97 
99  if (instance->getVisual<InstanceVisual>()) {
100  throw Duplicate("Instance already contains visualization");
101  }
102  InstanceVisual* v = new InstanceVisual();
103  instance->setVisual(v);
104  v->m_instance = instance;
105  return v;
106  }
107 
109  }
110 
112  if (m_transparency != transparency) {
113  m_transparency = transparency;
115  }
116  }
117 
119  return m_transparency;
120  }
121 
122  void InstanceVisual::setVisible(bool visible) {
123  if (m_visible != visible) {
124  m_visible = visible;
126  }
127  }
128 
130  return m_visible;
131  }
132 
133  void InstanceVisual::setStackPosition(int32_t stackposition) {
134  if (m_stackposition != stackposition) {
135  m_stackposition = stackposition;
137  }
138  }
139 
141  return m_stackposition;
142  }
143 
144  ActionVisual::ActionVisual(): m_animation_map(), m_map() {
145  }
146 
148  if (action->getVisual<ActionVisual>()) {
149  throw Duplicate("Action already contains visualization");
150  }
151  ActionVisual* v = new ActionVisual();
152  action->adoptVisual(v);
153  return v;
154  }
155 
157  }
158 
160  m_animation_map[angle % 360] = animationptr;
161  m_map[angle % 360] = angle % 360;
162  }
163 
165  int32_t closestMatch = 0;
166  return m_animation_map[getIndexByAngle(angle, m_map, closestMatch)];
167  }
168 
169  void ActionVisual::getActionImageAngles(std::vector<int32_t>& angles) {
170  angles.clear();
171  AngleAnimationMap::const_iterator i(m_animation_map.begin());
172  while (i != m_animation_map.end()) {
173  angles.push_back(i->first);
174  ++i;
175  }
176  }
177 }
static InstanceVisual * create(Instance *instance)
Constructs and assigns it to the passed item.
Definition: visual.cpp:98
void callOnVisibleChange()
Definition: instance.cpp:907
ObjectVisual()
Constructor.
Definition: visual.cpp:52
AngleAnimationMap m_animation_map
Definition: visual.h:195
int32_t getClosestMatchingAngle(int32_t angle)
Returns closest matching image angle for given angle.
Definition: visual.cpp:76
int32_t getStackPosition()
Gets current stack position of instance.
Definition: visual.cpp:140
T * getVisual() const
Gets used visualization.
Definition: action.h:73
Visual2DGfx()
Constructor.
Definition: visual.cpp:46
Object class.
Definition: object.h:51
Instance visual contains data that is needed to visualize the instance on screen. ...
Definition: visual.h:111
ActionVisual()
Constructor.
Definition: visual.cpp:144
Action visual contains data that is needed to visualize different actions on screen.
Definition: visual.h:165
virtual ~Visual2DGfx()
Destructor.
Definition: visual.cpp:49
static ActionVisual * create(Action *action)
Constructs and assigns it to the passed item.
Definition: visual.cpp:147
T * getVisual() const
Gets used visualization.
Definition: instance.h:331
type_angle2id m_angle2img
Definition: visual.h:105
static Logger _log(LM_AUDIO)
void setVisual(IVisual *visual)
Sets visualization to be used.
Definition: instance.h:327
void setTransparency(uint8_t transparency)
Sets transparency value for object to be visualized.
Definition: visual.cpp:111
void callOnTransparencyChange()
Definition: instance.cpp:898
int32_t m_stackposition
Definition: visual.h:159
int32_t getStaticImageIndexByAngle(int32_t angle)
Returns closest matching static image for given angle.
Definition: visual.cpp:71
unsigned char uint8_t
Definition: core.h:38
bool isVisible()
Is instance visible or not.
Definition: visual.cpp:129
void setVisible(bool visible)
Sets visibility value for object to be visualized.
Definition: visual.cpp:122
void getActionImageAngles(std::vector< int32_t > &angles)
Returns list of available angles for this Action.
Definition: visual.cpp:169
T * getVisual() const
Gets used visualization.
Definition: object.h:122
void callOnStackPositionChange()
Definition: instance.cpp:916
void getStaticImageAngles(std::vector< int32_t > &angles)
Returns list of available static image angles for this object.
Definition: visual.cpp:82
type_angle2id m_map
Definition: visual.h:199
void addAnimation(uint32_t angle, AnimationPtr animationptr)
Adds new animation with given angle (degrees)
Definition: visual.cpp:159
Instance * m_instance
Definition: visual.h:160
uint8_t m_transparency
Definition: visual.h:157
virtual ~ActionVisual()
Destructor.
Definition: visual.cpp:156
InstanceVisual()
Constructor.
Definition: visual.cpp:91
Object visual contains data that is needed for visualizing objects.
Definition: visual.h:65
static ObjectVisual * create(Object *object)
Constructs and assigns it to the passed item.
Definition: visual.cpp:55
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:67
AnimationPtr getAnimationByAngle(int32_t angle)
Gets index to animation closest to given angle.
Definition: visual.cpp:164
uint8_t getTransparency()
Gets current transparency value (0-255)
Definition: visual.cpp:118
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:133
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:64
virtual ~InstanceVisual()
Destructor.
Definition: visual.cpp:108
unsigned int uint32_t
Definition: core.h:40
An Instance is an &quot;instantiation&quot; of an Object at a Location.
Definition: instance.h:97
void adoptVisual(IVisual *visual)
Sets visualization to be used.
Definition: action.h:69