30 #include "video/renderbackend.h"
31 #include "util/math/fife_math.h"
32 #include "util/log/logger.h"
33 #include "model/structures/instance.h"
34 #include "model/structures/layer.h"
35 #include "model/structures/location.h"
36 #include "view/camera.h"
38 #include "renderernode.h"
42 static Logger _log(LM_VIEWVIEW);
44 class NodeInstanceDeleteListener :
public InstanceDeleteListener {
46 NodeInstanceDeleteListener(RendererNode* node) {
49 virtual ~NodeInstanceDeleteListener() {}
51 virtual void onInstanceDeleted(Instance* instance) {
52 m_node->removeInstance(instance,
false);
59 RendererNode::RendererNode(Instance* attached_instance,
const Location &relative_location, Layer* relative_layer,
const Point &relative_point):
61 m_location(relative_location),
62 m_layer(relative_layer),
63 m_point(relative_point),
65 addInstance(attached_instance);
67 RendererNode::RendererNode(Instance* attached_instance,
const Location &relative_location,
const Point &relative_point):
69 m_location(relative_location),
71 m_point(relative_point),
73 addInstance(attached_instance);
75 RendererNode::RendererNode(Instance* attached_instance, Layer* relative_layer,
const Point &relative_point):
78 m_layer(relative_layer),
79 m_point(relative_point),
81 addInstance(attached_instance);
83 RendererNode::RendererNode(Instance* attached_instance,
const Point &relative_point):
87 m_point(relative_point),
89 addInstance(attached_instance);
91 RendererNode::RendererNode(
const Location &attached_location, Layer* relative_layer,
const Point &relative_point):
93 m_location(attached_location),
94 m_layer(relative_layer),
95 m_point(relative_point),
98 RendererNode::RendererNode(
const Location &attached_location,
const Point &relative_point):
100 m_location(attached_location),
102 m_point(relative_point),
105 RendererNode::RendererNode(Layer* attached_layer,
const Point &relative_point):
108 m_layer(attached_layer),
109 m_point(relative_point),
112 RendererNode::RendererNode(
const Point &attached_point):
116 m_point(attached_point),
119 RendererNode::RendererNode(
const RendererNode& old):
121 m_location(old.m_location),
122 m_layer(old.m_layer),
123 m_point(old.m_point),
125 addInstance(old.m_instance);
127 RendererNode& RendererNode::operator=(
const RendererNode &source) {
128 if (
this != &source) {
129 changeInstance(source.m_instance);
130 m_location = source.m_location;
131 m_layer = source.m_layer;
132 m_point = source.m_point;
136 RendererNode::~RendererNode() {
137 removeInstance(m_instance);
141 void RendererNode::setAttached(Instance* attached_instance,
const Location &relative_location,
const Point &relative_point) {
142 changeInstance(attached_instance);
143 m_location = relative_location;
144 m_point = relative_point;
146 void RendererNode::setAttached(Instance* attached_instance,
const Location &relative_location) {
147 changeInstance(attached_instance);
148 m_location = relative_location;
150 void RendererNode::setAttached(Instance* attached_instance,
const Point &relative_point) {
151 changeInstance(attached_instance);
152 m_point = relative_point;
154 void RendererNode::setAttached(Instance* attached_instance) {
155 changeInstance(attached_instance);
157 void RendererNode::setAttached(
const Location &attached_location,
const Point &relative_point) {
158 changeInstance(NULL);
159 m_location = attached_location;
160 m_point = relative_point;
162 void RendererNode::setAttached(
const Location &attached_location) {
163 changeInstance(NULL);
164 m_location = attached_location;
166 void RendererNode::setAttached(Layer* attached_layer) {
167 m_layer = attached_layer;
169 void RendererNode::setAttached(
const Point &attached_point) {
170 changeInstance(NULL);
172 m_point = attached_point;
175 void RendererNode::setRelative(
const Location &relative_location) {
176 if(m_instance == NULL) {
177 FL_WARN(_log, LMsg(
"RendererNode::setRelative(Location) - ") <<
"No instance attached.");
179 m_location = relative_location;
181 void RendererNode::setRelative(
const Location &relative_location, Point relative_point) {
182 if(m_instance == NULL) {
183 FL_WARN(_log, LMsg(
"RendererNode::setRelative(Location, Point) - ") <<
"No instance attached.");
185 m_location = relative_location;
186 m_point = relative_point;
188 void RendererNode::setRelative(
const Point &relative_point) {
189 if(m_instance == NULL || m_location == NULL) {
190 FL_WARN(_log, LMsg(
"RendererNode::setRelative(Point) - ") <<
"No instance or location attached.");
192 m_point = relative_point;
195 Instance* RendererNode::getAttachedInstance() {
196 if(m_instance == NULL) {
197 FL_WARN(_log, LMsg(
"RendererNode::getAttachedInstance() - ") <<
"No instance attached.");
201 Location RendererNode::getAttachedLocation() {
202 if(m_instance != NULL || m_location == NULL) {
203 FL_WARN(_log, LMsg(
"RendererNode::getAttachedLocation() - ") <<
"No location attached.");
207 Layer* RendererNode::getAttachedLayer() {
208 if(m_layer == NULL) {
209 FL_WARN(_log, LMsg(
"RendererNode::getAttachedLayer() - ") <<
"No layer attached.");
213 Point RendererNode::getAttachedPoint() {
214 if(m_instance != NULL || m_location != NULL) {
215 FL_WARN(_log, LMsg(
"RendererNode::getAttachedPoint() - ") <<
"No point attached.");
220 Location RendererNode::getOffsetLocation() {
221 if(m_instance == NULL || m_location == NULL) {
222 FL_WARN(_log, LMsg(
"RendererNode::getOffsetLocation() - ") <<
"No location as offset used.");
226 Point RendererNode::getOffsetPoint() {
227 if(m_instance == NULL && m_location == NULL) {
228 FL_WARN(_log, LMsg(
"RendererNode::getOffsetPoint() - ") <<
"No point as offset used.");
233 Instance* RendererNode::getInstance() {
236 Location RendererNode::getLocation() {
239 const Location& RendererNode::getLocationRef() {
242 Layer* RendererNode::getLayer() {
245 Point RendererNode::getPoint() {
248 const Point& RendererNode::getPointRef() {
252 void RendererNode::addInstance(Instance* instance) {
253 checkDeleteListener();
254 m_instance = instance;
256 m_instance->addDeleteListener(m_listener);
260 void RendererNode::changeInstance(Instance* instance) {
261 if (m_instance == instance) {
264 checkDeleteListener();
266 m_instance->removeDeleteListener(m_listener);
268 m_instance = instance;
270 m_instance->addDeleteListener(m_listener);
274 void RendererNode::removeInstance(Instance* instance,
bool listener) {
275 if (m_instance == instance && instance) {
277 m_instance->removeDeleteListener(m_listener);
283 void RendererNode::checkDeleteListener() {
287 m_listener =
new NodeInstanceDeleteListener(
this);
290 Point RendererNode::getCalculatedPoint(Camera* cam, Layer* layer,
const bool zoomed) {
292 if(m_instance != NULL) {
293 if(m_layer == NULL) {
294 m_layer = m_instance->getLocationRef().getLayer();
296 if(m_location != NULL) {
297 p = cam->toScreenCoordinates(m_instance->getLocationRef().getMapCoordinates() + m_location.getMapCoordinates());
299 p = cam->toScreenCoordinates(m_instance->getLocationRef().getMapCoordinates());
301 }
else if(m_location != NULL) {
302 if(m_layer == NULL) {
303 m_layer = m_location.getLayer();
305 p = cam->toScreenCoordinates(m_location.getMapCoordinates());
306 }
else if(m_layer == NULL) {
308 FL_WARN(_log, LMsg(
"RendererNode::getCalculatedPoint(Camera, Layer) - ") <<
"No layer attached. So we use the first active layer of the renderer.");
312 return Point(round(m_point.x * cam->getZoom()) + p.x, round(m_point.y * cam->getZoom()) + p.y);
314 return Point(m_point.x + p.x, m_point.y + p.y);