[4744] | 1 | /* |
---|
[1853] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[4838] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3955] | 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
[1853] | 17 | |
---|
[4839] | 18 | #include "element_2d.h" |
---|
[4840] | 19 | #include "render_2d.h" |
---|
[1853] | 20 | |
---|
[6142] | 21 | #include <algorithm> |
---|
| 22 | |
---|
[5775] | 23 | #include "p_node.h" |
---|
| 24 | |
---|
[4843] | 25 | #include "graphics_engine.h" |
---|
[4858] | 26 | #include "load_param.h" |
---|
| 27 | #include "class_list.h" |
---|
[5775] | 28 | |
---|
[5285] | 29 | #include "color.h" |
---|
[4843] | 30 | |
---|
[1856] | 31 | using namespace std; |
---|
[1853] | 32 | |
---|
[5285] | 33 | /** |
---|
[6295] | 34 | * @brief standard constructor |
---|
[5285] | 35 | * @param parent the parent to set for this Element2D |
---|
| 36 | * |
---|
| 37 | * NullElement2D needs this constructor with parameter NULL to initialize |
---|
| 38 | * itself. Otherwise it would result in an endless Loop. |
---|
| 39 | */ |
---|
[6299] | 40 | Element2D::Element2D (Element2D* parent, E2D_LAYER layer, short nodeFlags) |
---|
[3365] | 41 | { |
---|
[6299] | 42 | this->setClassID(CL_ELEMENT_2D, "Element2D"); |
---|
| 43 | |
---|
| 44 | this->setVisibility(true); |
---|
| 45 | this->activate2D(); |
---|
| 46 | this->setAlignment(E2D_ALIGN_NONE); |
---|
| 47 | this->bindNode = NULL; |
---|
| 48 | |
---|
| 49 | this->parentMode = nodeFlags; |
---|
| 50 | this->parent = NULL; |
---|
| 51 | this->absDirection = 0.0; |
---|
| 52 | this->relDirection = 0.0; |
---|
| 53 | this->bRelCoorChanged = true; |
---|
| 54 | this->bRelDirChanged = true; |
---|
| 55 | this->toCoordinate = NULL; |
---|
| 56 | this->toDirection = NULL; |
---|
| 57 | this->setSize2D(1, 1); |
---|
| 58 | |
---|
| 59 | |
---|
[5402] | 60 | this->layer = layer; |
---|
[6299] | 61 | if (parent != NULL) |
---|
| 62 | parent->addChild2D(this); |
---|
[3365] | 63 | } |
---|
[1853] | 64 | |
---|
[6299] | 65 | |
---|
[3245] | 66 | /** |
---|
[6299] | 67 | * @brief the mighty NullElement |
---|
| 68 | * TopMost Node of them all. |
---|
| 69 | */ |
---|
| 70 | Element2D* Element2D::nullElement = NULL; |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | /** |
---|
[6295] | 74 | * @brief standard deconstructor |
---|
[5285] | 75 | * |
---|
| 76 | * There are two general ways to delete an Element2D |
---|
| 77 | * 1. delete instance; |
---|
| 78 | * -> result |
---|
| 79 | * delete this Node and all its children and children's children... |
---|
| 80 | * (danger if you still want the instance!!) |
---|
| 81 | * |
---|
| 82 | * 2. instance->remove2D(); delete instance; |
---|
| 83 | * -> result: |
---|
[5401] | 84 | * moves its children to the NullElement2D |
---|
[5285] | 85 | * then deletes the Element. |
---|
| 86 | */ |
---|
[4838] | 87 | Element2D::~Element2D () |
---|
[3543] | 88 | { |
---|
[6299] | 89 | // remove the Element2D, delete it's children (if required). |
---|
| 90 | std::list<Element2D*>::iterator tmp = this->children.begin(); |
---|
| 91 | std::list<Element2D*>::iterator deleteNode; |
---|
| 92 | while(!this->children.empty()) |
---|
| 93 | while (tmp != this->children.end()) |
---|
| 94 | { |
---|
| 95 | deleteNode = tmp; |
---|
| 96 | tmp++; |
---|
| 97 | // printf("TEST::%s(%s) %s\n", (*deleteNode)->getName(), (*deleteNode)->getClassName(), this->getName()); |
---|
| 98 | if ((this->parentMode & E2D_PROHIBIT_CHILD_DELETE) || |
---|
| 99 | ((*deleteNode)->parentMode & E2D_PROHIBIT_DELETE_WITH_PARENT)) |
---|
| 100 | { |
---|
| 101 | if (this == Element2D::nullElement && (*deleteNode)->parentMode & E2D_REPARENT_TO_NULL) |
---|
| 102 | delete (*deleteNode); |
---|
| 103 | else |
---|
| 104 | (*deleteNode)->reparent2D(); |
---|
| 105 | } |
---|
| 106 | else |
---|
| 107 | delete (*deleteNode); |
---|
| 108 | } |
---|
| 109 | |
---|
[5285] | 110 | if (this->parent != NULL) |
---|
| 111 | { |
---|
[6299] | 112 | this->parent->eraseChild2D(this); |
---|
[5285] | 113 | this->parent = NULL; |
---|
| 114 | } |
---|
[5089] | 115 | |
---|
[5285] | 116 | // remove all other allocated memory. |
---|
[5088] | 117 | if (this->toCoordinate != NULL) |
---|
| 118 | delete this->toCoordinate; |
---|
| 119 | if (this->toDirection != NULL) |
---|
| 120 | delete this->toDirection; |
---|
[6299] | 121 | |
---|
| 122 | if (this == Element2D::nullElement) |
---|
| 123 | Element2D::nullElement = NULL; |
---|
[3543] | 124 | } |
---|
[4843] | 125 | |
---|
| 126 | |
---|
[4858] | 127 | /** |
---|
[6295] | 128 | * @brief Loads the Parameters of an Element2D from... |
---|
[4858] | 129 | * @param root The XML-element to load from |
---|
| 130 | */ |
---|
| 131 | void Element2D::loadParams(const TiXmlElement* root) |
---|
| 132 | { |
---|
[6512] | 133 | BaseObject::loadParams(root); |
---|
| 134 | |
---|
[5402] | 135 | // ELEMENT2D-native settings. |
---|
[5671] | 136 | LoadParam(root, "alignment", this, Element2D, setAlignment) |
---|
[4858] | 137 | .describe("loads the alignment: (either: center, left, right or screen-center)"); |
---|
| 138 | |
---|
[5671] | 139 | LoadParam(root, "layer", this, Element2D, setLayer) |
---|
[4858] | 140 | .describe("loads the layer onto which to project: (either: top, medium, bottom, below-all)"); |
---|
| 141 | |
---|
[5671] | 142 | LoadParam(root, "bind-node", this, Element2D, setBindNode) |
---|
[4858] | 143 | .describe("sets a node, this 2D-Element should be shown upon (name of the node)"); |
---|
| 144 | |
---|
[5671] | 145 | LoadParam(root, "visibility", this, Element2D, setVisibility) |
---|
[4860] | 146 | .describe("if the Element is visible or not"); |
---|
[5089] | 147 | |
---|
| 148 | |
---|
[5402] | 149 | // PNode-style: |
---|
[6878] | 150 | LoadParam(root, "rel-coor-2d", this, Element2D, setRelCoor2D) |
---|
[5091] | 151 | .describe("Sets The relative position of the Node to its parent."); |
---|
| 152 | |
---|
[6878] | 153 | LoadParam(root, "abs-coor-2d", this, Element2D, setAbsCoor2D) |
---|
[5091] | 154 | .describe("Sets The absolute Position of the Node."); |
---|
| 155 | |
---|
[6878] | 156 | LoadParam(root, "rel-dir-2d", this, Element2D, setRelDir2D) |
---|
[5091] | 157 | .describe("Sets The relative rotation of the Node to its parent."); |
---|
| 158 | |
---|
[6878] | 159 | LoadParam(root, "abs-dir-2d", this, Element2D, setAbsDir2D) |
---|
[5091] | 160 | .describe("Sets The absolute rotation of the Node."); |
---|
| 161 | |
---|
[5671] | 162 | LoadParam(root, "parent", this, Element2D, setParent2D) |
---|
[5091] | 163 | .describe("the Name of the Parent of this Element2D"); |
---|
| 164 | |
---|
[5671] | 165 | LoadParam(root, "parent-mode", this, Element2D, setParentMode2D) |
---|
[5091] | 166 | .describe("the mode to connect this node to its parent ()"); |
---|
| 167 | |
---|
| 168 | // cycling properties |
---|
[6295] | 169 | LOAD_PARAM_START_CYCLE(root, element); |
---|
[5091] | 170 | { |
---|
[6299] | 171 | LoadParam_CYCLE(element, "child", this, Element2D, addChild2D) |
---|
[6295] | 172 | .describe("adds a new Child to the current Node."); |
---|
[5091] | 173 | } |
---|
[6295] | 174 | LOAD_PARAM_END_CYCLE(element); |
---|
[4858] | 175 | } |
---|
| 176 | |
---|
| 177 | /** |
---|
| 178 | * sets the alignment of the 2D-element in form of a String |
---|
| 179 | * @param alignment the alignment @see loadParams |
---|
| 180 | */ |
---|
| 181 | void Element2D::setAlignment(const char* alignment) |
---|
| 182 | { |
---|
| 183 | if (!strcmp(alignment, "center")) |
---|
| 184 | this->setAlignment(E2D_ALIGN_CENTER); |
---|
| 185 | else if (!strcmp(alignment, "left")) |
---|
| 186 | this->setAlignment(E2D_ALIGN_LEFT); |
---|
| 187 | else if (!strcmp(alignment, "right")) |
---|
| 188 | this->setAlignment(E2D_ALIGN_RIGHT); |
---|
| 189 | else if (!strcmp(alignment, "screen-center")) |
---|
| 190 | this->setAlignment(E2D_ALIGN_SCREEN_CENTER); |
---|
| 191 | } |
---|
| 192 | |
---|
[4862] | 193 | |
---|
[4858] | 194 | /** |
---|
[4862] | 195 | * moves a Element to another layer |
---|
| 196 | * @param layer the Layer this is drawn on |
---|
| 197 | */ |
---|
| 198 | void Element2D::setLayer(E2D_LAYER layer) |
---|
| 199 | { |
---|
[5402] | 200 | if (this->parent != NULL && this->parent->getLayer() > layer) |
---|
| 201 | { |
---|
[5403] | 202 | PRINTF(2)("Unable to set %s to layer %s, because it's parent(%s) is of higher layer %s\n", |
---|
| 203 | this->getName(), |
---|
| 204 | Element2D::layer2DToChar(layer), |
---|
| 205 | this->parent->getName(), |
---|
| 206 | Element2D::layer2DToChar(this->parent->getLayer())); |
---|
[5402] | 207 | layer = this->parent->getLayer(); |
---|
| 208 | } |
---|
[4862] | 209 | this->layer = layer; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /** |
---|
[4858] | 213 | * sets the layer onto which this 2D-element is projected to. |
---|
[5401] | 214 | * @param layer the layer @see loadParams @see Element2D::charToLayer2D(const char* layer) |
---|
[4858] | 215 | */ |
---|
| 216 | void Element2D::setLayer(const char* layer) |
---|
| 217 | { |
---|
[5401] | 218 | this->setLayer(Element2D::charToLayer2D(layer)); |
---|
[4858] | 219 | } |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | /** |
---|
[6299] | 223 | * @brief sets a node, this 2D-Element should be shown upon |
---|
[4858] | 224 | * @param bindNode the name of the Node (should be existing) |
---|
| 225 | */ |
---|
| 226 | void Element2D::setBindNode(const char* bindNode) |
---|
| 227 | { |
---|
| 228 | const PNode* tmpBindNode = dynamic_cast<const PNode*>(ClassList::getObject(bindNode, CL_PARENT_NODE)); |
---|
| 229 | if (tmpBindNode != NULL) |
---|
| 230 | this->bindNode = tmpBindNode; |
---|
| 231 | } |
---|
| 232 | |
---|
[5091] | 233 | /** |
---|
| 234 | * sets the relative coordinate of the Element2D to its parent |
---|
| 235 | * @param relCoord the relative coordinate to the parent |
---|
| 236 | */ |
---|
[5081] | 237 | void Element2D::setRelCoor2D (const Vector& relCoord) |
---|
| 238 | { |
---|
[5113] | 239 | if (this->toCoordinate!= NULL) |
---|
| 240 | { |
---|
| 241 | delete this->toCoordinate; |
---|
| 242 | this->toCoordinate = NULL; |
---|
| 243 | } |
---|
[5082] | 244 | this->relCoordinate = relCoord; |
---|
| 245 | this->bRelCoorChanged = true; |
---|
[5081] | 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
[5091] | 249 | /** |
---|
| 250 | * sets the relative coordinate of the Element2D to its Parent |
---|
| 251 | * @param x the x coordinate |
---|
| 252 | * @param y the y coordinate |
---|
| 253 | * @param z the z coordinate |
---|
| 254 | */ |
---|
[5081] | 255 | void Element2D::setRelCoor2D (float x, float y, float z) |
---|
| 256 | { |
---|
[5113] | 257 | this->setRelCoor2D(Vector(x,y,z)); |
---|
[5081] | 258 | } |
---|
| 259 | |
---|
[5091] | 260 | /** |
---|
| 261 | * sets the Relative coordinate to the parent in Pixels |
---|
| 262 | * @param x the relCoord X |
---|
| 263 | * @param y the relCoord Y |
---|
| 264 | */ |
---|
[5089] | 265 | void Element2D::setRelCoor2Dpx (int x, int y) |
---|
| 266 | { |
---|
[5090] | 267 | this->setRelCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(), |
---|
| 268 | (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(), |
---|
[5089] | 269 | 0 |
---|
| 270 | )); |
---|
| 271 | } |
---|
| 272 | |
---|
[5091] | 273 | /** |
---|
| 274 | * sets a new relative position smoothely |
---|
| 275 | * @param relCoordSoft the new Position to iterate to |
---|
| 276 | * @param bias how fast to iterate to this position |
---|
| 277 | */ |
---|
[5081] | 278 | void Element2D::setRelCoorSoft2D(const Vector& relCoordSoft, float bias) |
---|
| 279 | { |
---|
[5082] | 280 | if (likely(this->toCoordinate == NULL)) |
---|
| 281 | this->toCoordinate = new Vector(); |
---|
| 282 | |
---|
| 283 | *this->toCoordinate = relCoordSoft; |
---|
| 284 | this->bias = bias; |
---|
[5081] | 285 | } |
---|
| 286 | |
---|
[5091] | 287 | /** |
---|
| 288 | * sets a new relative position smoothely |
---|
| 289 | * @param x the new x-coordinate in Pixels of the Position to iterate to |
---|
| 290 | * @param y the new y-coordinate in Pixels of the Position to iterate to |
---|
| 291 | * @param bias how fast to iterate to this position |
---|
| 292 | */ |
---|
[5089] | 293 | void Element2D::setRelCoorSoft2Dpx (int x, int y, float bias) |
---|
| 294 | { |
---|
[5090] | 295 | this->setRelCoorSoft2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(), |
---|
| 296 | (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(), |
---|
[5089] | 297 | 0), |
---|
| 298 | bias); |
---|
| 299 | } |
---|
| 300 | |
---|
[5091] | 301 | /** |
---|
| 302 | * set relative coordinates smoothely |
---|
| 303 | * @param x x-relative coordinates to its parent |
---|
| 304 | * @param y y-relative coordinates to its parent |
---|
| 305 | * @param z z-relative coordinates to its parent |
---|
| 306 | * @see void PNode::setRelCoorSoft (const Vector&, float) |
---|
| 307 | */ |
---|
[5082] | 308 | void Element2D::setRelCoorSoft2D(float x, float y, float depth, float bias) |
---|
[5081] | 309 | { |
---|
[5082] | 310 | this->setRelCoorSoft2D(Vector(x, y, depth), bias); |
---|
[5081] | 311 | } |
---|
| 312 | |
---|
[5091] | 313 | /** |
---|
| 314 | * @param absCoord set absolute coordinate |
---|
| 315 | */ |
---|
[5081] | 316 | void Element2D::setAbsCoor2D (const Vector& absCoord) |
---|
| 317 | { |
---|
[5113] | 318 | if (this->toCoordinate!= NULL) |
---|
| 319 | { |
---|
| 320 | delete this->toCoordinate; |
---|
| 321 | this->toCoordinate = NULL; |
---|
| 322 | } |
---|
| 323 | |
---|
[5082] | 324 | if( likely(this->parentMode & E2D_PARENT_MOVEMENT)) |
---|
| 325 | { |
---|
| 326 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
| 327 | if (likely(this->parent != NULL)) |
---|
| 328 | this->relCoordinate = absCoord - parent->getAbsCoor2D (); |
---|
| 329 | else |
---|
| 330 | this->relCoordinate = absCoord; |
---|
| 331 | } |
---|
| 332 | if( this->parentMode & E2D_PARENT_ROTATE_MOVEMENT) |
---|
| 333 | { |
---|
| 334 | if (likely(this->parent != NULL)) |
---|
| 335 | this->relCoordinate = absCoord - parent->getAbsCoor2D (); |
---|
| 336 | else |
---|
| 337 | this->relCoordinate = absCoord; |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | this->bRelCoorChanged = true; |
---|
[5081] | 341 | } |
---|
| 342 | |
---|
[5091] | 343 | /** |
---|
| 344 | * @param x x-coordinate. |
---|
| 345 | * @param y y-coordinate. |
---|
| 346 | * @param z z-coordinate. |
---|
| 347 | * @see void PNode::setAbsCoor (const Vector& absCoord) |
---|
| 348 | */ |
---|
[5081] | 349 | void Element2D::setAbsCoor2D (float x, float y, float depth) |
---|
| 350 | { |
---|
[5082] | 351 | this->setAbsCoor2D(Vector(x, y, depth)); |
---|
[5081] | 352 | } |
---|
| 353 | |
---|
[5091] | 354 | /** |
---|
| 355 | * @param x x-coordinate in Pixels |
---|
| 356 | * @param y y-coordinate in Pixels |
---|
| 357 | * @see void PNode::setAbsCoor (const Vector& absCoord) |
---|
| 358 | */ |
---|
[5089] | 359 | void Element2D::setAbsCoor2Dpx (int x, int y) |
---|
| 360 | { |
---|
[5090] | 361 | this->setAbsCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(), |
---|
| 362 | (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(), |
---|
[5089] | 363 | 0 |
---|
| 364 | )); |
---|
| 365 | } |
---|
| 366 | |
---|
[5091] | 367 | /** |
---|
[5414] | 368 | * @param absCoordSoft set absolute coordinate |
---|
| 369 | * @param bias how fast to iterato to the new Coordinate |
---|
| 370 | */ |
---|
| 371 | void Element2D::setAbsCoorSoft2D (const Vector& absCoordSoft, float bias) |
---|
| 372 | { |
---|
| 373 | if (this->toCoordinate == NULL) |
---|
| 374 | this->toCoordinate = new Vector(); |
---|
| 375 | |
---|
| 376 | if( likely(this->parentMode & E2D_PARENT_MOVEMENT)) |
---|
| 377 | { |
---|
| 378 | /* if you have set the absolute coordinates this overrides all other changes */ |
---|
| 379 | if (likely(this->parent != NULL)) |
---|
| 380 | *this->toCoordinate = absCoordSoft - parent->getAbsCoor2D (); |
---|
| 381 | else |
---|
| 382 | *this->toCoordinate = absCoordSoft; |
---|
| 383 | } |
---|
| 384 | if( this->parentMode & E2D_PARENT_ROTATE_MOVEMENT) |
---|
| 385 | { |
---|
| 386 | if (likely(this->parent != NULL)) |
---|
| 387 | *this->toCoordinate = absCoordSoft - parent->getAbsCoor2D (); |
---|
| 388 | else |
---|
| 389 | *this->toCoordinate = absCoordSoft; |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | this->bias = bias; |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | /** |
---|
| 396 | * @param x x-coordinate. |
---|
| 397 | * @param y y-coordinate. |
---|
| 398 | * @param z z-coordinate. |
---|
| 399 | * @see void PNode::setAbsCoor (const Vector& absCoord) |
---|
| 400 | */ |
---|
| 401 | void Element2D::setAbsCoorSoft2D (float x, float y, float depth, float bias) |
---|
| 402 | { |
---|
| 403 | this->setAbsCoorSoft2D(Vector(x, y, depth), bias); |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | /** |
---|
[5091] | 407 | * shift coordinate ralative |
---|
| 408 | * @param shift shift vector |
---|
| 409 | * |
---|
| 410 | * This simply adds the shift-Vector to the relative Coordinate |
---|
| 411 | */ |
---|
[5083] | 412 | void Element2D::shiftCoor2D (const Vector& shift) |
---|
[5081] | 413 | { |
---|
[5082] | 414 | this->relCoordinate += shift; |
---|
| 415 | this->bRelCoorChanged = true; |
---|
| 416 | |
---|
[5081] | 417 | } |
---|
| 418 | |
---|
[5091] | 419 | /** |
---|
| 420 | * shifts in PixelSpace |
---|
| 421 | * @param x the pixels to shift in X |
---|
| 422 | * @param y the pixels to shift in Y |
---|
| 423 | */ |
---|
[5089] | 424 | void Element2D::shiftCoor2Dpx (int x, int y) |
---|
| 425 | { |
---|
[5090] | 426 | this->shiftCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(), |
---|
| 427 | (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(), |
---|
[5089] | 428 | 0)); |
---|
| 429 | } |
---|
| 430 | |
---|
[5091] | 431 | /** |
---|
| 432 | * set relative direction |
---|
| 433 | * @param relDir to its parent |
---|
| 434 | */ |
---|
[5081] | 435 | void Element2D::setRelDir2D (float relDir) |
---|
| 436 | { |
---|
[5113] | 437 | if (this->toDirection!= NULL) |
---|
| 438 | { |
---|
| 439 | delete this->toDirection; |
---|
| 440 | this->toDirection = NULL; |
---|
| 441 | } |
---|
| 442 | |
---|
[5082] | 443 | this->relDirection = relDir; |
---|
| 444 | this->bRelDirChanged = true; |
---|
[5081] | 445 | } |
---|
| 446 | |
---|
[5091] | 447 | /** |
---|
| 448 | * sets the Relative Direction of this node to its parent in a Smoothed way |
---|
| 449 | * @param relDirSoft the direction to iterate to smoothely. |
---|
| 450 | * @param bias how fast to iterate to the new Direction |
---|
| 451 | */ |
---|
[5081] | 452 | void Element2D::setRelDirSoft2D(float relDirSoft, float bias) |
---|
| 453 | { |
---|
[5082] | 454 | if (likely(this->toDirection == NULL)) |
---|
| 455 | this->toDirection = new float; |
---|
| 456 | |
---|
| 457 | *this->toDirection = relDirSoft; |
---|
| 458 | this->bias = bias; |
---|
[5081] | 459 | } |
---|
| 460 | |
---|
[5091] | 461 | /** |
---|
| 462 | * sets the absolute direction |
---|
| 463 | * @param absDir absolute coordinates |
---|
| 464 | */ |
---|
[5081] | 465 | void Element2D::setAbsDir2D (float absDir) |
---|
| 466 | { |
---|
[5113] | 467 | if (this->toDirection!= NULL) |
---|
| 468 | { |
---|
| 469 | delete this->toDirection; |
---|
| 470 | this->toDirection = NULL; |
---|
| 471 | } |
---|
| 472 | |
---|
[5082] | 473 | if (likely(this->parent != NULL)) |
---|
| 474 | this->relDirection = absDir - this->parent->getAbsDir2D(); |
---|
| 475 | else |
---|
| 476 | this->relDirection = absDir; |
---|
| 477 | |
---|
| 478 | this->bRelDirChanged = true; |
---|
[5081] | 479 | } |
---|
| 480 | |
---|
[5091] | 481 | /** |
---|
[5414] | 482 | * sets the absolute direction softly |
---|
| 483 | * @param absDir absolute coordinates |
---|
| 484 | */ |
---|
| 485 | void Element2D::setAbsDirSoft2D (float absDirSoft, float bias) |
---|
| 486 | { |
---|
| 487 | if (this->toDirection == NULL) |
---|
| 488 | this->toDirection = new float; |
---|
| 489 | |
---|
| 490 | if (likely(this->parent != NULL)) |
---|
| 491 | *this->toDirection = absDirSoft - this->parent->getAbsDir2D(); |
---|
| 492 | else |
---|
| 493 | *this->toDirection = absDirSoft; |
---|
| 494 | |
---|
| 495 | this->bias = bias; |
---|
| 496 | } |
---|
| 497 | |
---|
| 498 | /** |
---|
[5091] | 499 | * shift Direction |
---|
| 500 | * @param shift the direction around which to shift. |
---|
| 501 | */ |
---|
[5083] | 502 | void Element2D::shiftDir2D (float shiftDir) |
---|
[5081] | 503 | { |
---|
[5082] | 504 | this->relDirection = this->relDirection + shiftDir; |
---|
| 505 | this->bRelDirChanged = true; |
---|
[5081] | 506 | } |
---|
| 507 | |
---|
[5091] | 508 | /** |
---|
| 509 | * adds a child and makes this node to a parent |
---|
| 510 | * @param child child reference |
---|
| 511 | * @param parentMode on which changes the child should also change ist state |
---|
| 512 | * |
---|
| 513 | * use this to add a child to this node. |
---|
| 514 | */ |
---|
[5403] | 515 | void Element2D::addChild2D (Element2D* child) |
---|
[5081] | 516 | { |
---|
[6295] | 517 | assert(child != NULL); |
---|
[5082] | 518 | if( likely(child->parent != NULL)) |
---|
| 519 | { |
---|
[5254] | 520 | PRINTF(5)("Element2D::addChild() - reparenting node: removing it and adding it again\n"); |
---|
[6299] | 521 | child->parent->eraseChild2D(child); |
---|
[5082] | 522 | } |
---|
[6299] | 523 | if (this->checkIntegrity(child)) |
---|
[5402] | 524 | { |
---|
[6299] | 525 | child->parent = this; |
---|
| 526 | if (likely(this != NULL)) |
---|
[5403] | 527 | { |
---|
[6299] | 528 | // ELEMENT SORTING TO LAYERS // |
---|
| 529 | unsigned int childCount = this->children.size(); |
---|
[5775] | 530 | |
---|
[6299] | 531 | list<Element2D*>::iterator elem; |
---|
| 532 | for (elem = this->children.begin(); elem != this->children.end(); elem++) |
---|
| 533 | { |
---|
| 534 | if ((*elem)->layer < child->layer) |
---|
| 535 | { |
---|
| 536 | this->children.insert(elem, child); |
---|
| 537 | break; |
---|
| 538 | } |
---|
| 539 | } |
---|
| 540 | if (childCount == this->children.size()) |
---|
| 541 | this->children.push_back(child); |
---|
| 542 | //////////////////////////////// |
---|
| 543 | if (unlikely(this->layer > child->getLayer())) |
---|
| 544 | { |
---|
| 545 | PRINTF(2)("Layer '%s' of Child(%s) lower than parents(%s) layer '%s'. updating...\n", |
---|
| 546 | Element2D::layer2DToChar(child->getLayer()),child->getName(), this->getName(), Element2D::layer2DToChar(this->layer)); |
---|
| 547 | child->setLayer(this->layer); |
---|
| 548 | } |
---|
| 549 | } |
---|
| 550 | else |
---|
| 551 | { |
---|
| 552 | PRINTF(1)("Tried to reparent2D to own child '%s::%s' to '%s::%s'.\n", |
---|
| 553 | this->getClassName(), this->getName(), child->getClassName(), child->getName()); |
---|
| 554 | child->parent = NULL; |
---|
| 555 | } |
---|
[5402] | 556 | } |
---|
[6299] | 557 | child->parentCoorChanged2D(); |
---|
[5081] | 558 | } |
---|
| 559 | |
---|
[5091] | 560 | /** |
---|
| 561 | * @see Element2D::addChild(Element2D* child); |
---|
| 562 | * @param childName the name of the child to add to this PNode |
---|
| 563 | */ |
---|
[5081] | 564 | void Element2D::addChild2D (const char* childName) |
---|
| 565 | { |
---|
[5082] | 566 | Element2D* childNode = dynamic_cast<Element2D*>(ClassList::getObject(childName, CL_ELEMENT_2D)); |
---|
| 567 | if (childNode != NULL) |
---|
| 568 | this->addChild2D(childNode); |
---|
[5081] | 569 | } |
---|
| 570 | |
---|
[5091] | 571 | /** |
---|
| 572 | * removes a child from the node |
---|
| 573 | * @param child the child to remove from this Node.. |
---|
| 574 | * |
---|
| 575 | * Children from nodes will not be lost, they are referenced to NullPointer |
---|
| 576 | */ |
---|
[5081] | 577 | void Element2D::removeChild2D (Element2D* child) |
---|
| 578 | { |
---|
[5212] | 579 | if (child != NULL) |
---|
| 580 | child->remove2D(); |
---|
[5081] | 581 | } |
---|
| 582 | |
---|
[5091] | 583 | /** |
---|
[6299] | 584 | * !! PRIVATE FUNCTION |
---|
| 585 | * @brief reparents an Element2D (happens on Parents Node delete or remove and Flags are set.) |
---|
| 586 | */ |
---|
| 587 | void Element2D::reparent2D() |
---|
| 588 | { |
---|
| 589 | if (this->parentMode & E2D_REPARENT_TO_NULL) |
---|
| 590 | this->setParent2D((Element2D*)NULL); |
---|
| 591 | else if (this->parentMode & E2D_REPARENT_TO_PARENTS_PARENT && this->parent != NULL) |
---|
| 592 | this->setParent2D(this->parent->getParent2D()); |
---|
| 593 | else |
---|
| 594 | this->setParent2D(Element2D::getNullElement()); |
---|
| 595 | } |
---|
| 596 | |
---|
| 597 | |
---|
| 598 | /** |
---|
| 599 | * @param child the child to be erased from this Nodes List |
---|
| 600 | */ |
---|
| 601 | void Element2D::eraseChild2D(Element2D* child) |
---|
| 602 | { |
---|
| 603 | assert (this != NULL && child != NULL); |
---|
| 604 | std::list<Element2D*>::iterator childIT = std::find(this->children.begin(), this->children.end(), child); |
---|
| 605 | this->children.erase(childIT); |
---|
| 606 | } |
---|
| 607 | |
---|
| 608 | |
---|
| 609 | |
---|
| 610 | /** |
---|
[5285] | 611 | * remove this Element from the tree and adds all children to NullElement2D |
---|
[5091] | 612 | * |
---|
[5285] | 613 | * afterwards this Node is free, and can be reattached, or deleted freely. |
---|
[5091] | 614 | */ |
---|
[5081] | 615 | void Element2D::remove2D() |
---|
| 616 | { |
---|
[6299] | 617 | list<Element2D*>::iterator child = this->children.begin(); |
---|
| 618 | list<Element2D*>::iterator reparenter; |
---|
| 619 | while (child != this->children.end()) |
---|
| 620 | { |
---|
| 621 | reparenter = child; |
---|
| 622 | child++; |
---|
| 623 | if (this->parentMode & E2D_REPARENT_CHILDREN_ON_REMOVE || |
---|
| 624 | (*reparenter)->parentMode & E2D_REPARENT_ON_PARENTS_REMOVE) |
---|
| 625 | { |
---|
| 626 | printf("TEST----------------%s ---- %s\n", this->getClassName(), (*reparenter)->getClassName()); |
---|
| 627 | (*reparenter)->reparent2D(); |
---|
| 628 | printf("REPARENTED TO: %s::%s\n",(*reparenter)->getParent2D()->getClassName(),(*reparenter)->getParent2D()->getName()); |
---|
| 629 | } |
---|
| 630 | } |
---|
[5214] | 631 | if (this->parent != NULL) |
---|
[5285] | 632 | { |
---|
[6299] | 633 | this->parent->eraseChild2D(this); |
---|
[5285] | 634 | this->parent = NULL; |
---|
| 635 | } |
---|
[5081] | 636 | } |
---|
| 637 | |
---|
| 638 | |
---|
[5091] | 639 | /** |
---|
| 640 | * @see Element2D::setParent(Element2D* parent); |
---|
| 641 | * @param parentName the name of the Parent to set to this Element2D |
---|
| 642 | */ |
---|
[5081] | 643 | void Element2D::setParent2D (const char* parentName) |
---|
| 644 | { |
---|
[5082] | 645 | Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D)); |
---|
| 646 | if (parentNode != NULL) |
---|
| 647 | parentNode->addChild2D(this); |
---|
[6299] | 648 | else |
---|
| 649 | PRINTF(2)("Not Found Element2D's (%s::%s) new Parent by Name: %s\n", |
---|
| 650 | this->getClassName(), this->getName(), parentName); |
---|
[5081] | 651 | } |
---|
| 652 | |
---|
[5091] | 653 | /** |
---|
| 654 | * does the reparenting in a very smooth way |
---|
| 655 | * @param parentNode the new Node to connect this node to. |
---|
| 656 | * @param bias the speed to iterate to this new Positions |
---|
| 657 | */ |
---|
[5382] | 658 | void Element2D::setParentSoft2D(Element2D* parentNode, float bias) |
---|
[5081] | 659 | { |
---|
[5082] | 660 | if (this->parent == parentNode) |
---|
| 661 | return; |
---|
| 662 | |
---|
| 663 | if (likely(this->toCoordinate == NULL)) |
---|
| 664 | { |
---|
| 665 | this->toCoordinate = new Vector(); |
---|
| 666 | *this->toCoordinate = this->getRelCoor2D(); |
---|
| 667 | } |
---|
| 668 | if (likely(this->toDirection == NULL)) |
---|
| 669 | { |
---|
| 670 | this->toDirection = new float; |
---|
| 671 | *this->toDirection = this->getRelDir2D(); |
---|
| 672 | } |
---|
| 673 | this->bias = bias; |
---|
| 674 | |
---|
| 675 | |
---|
| 676 | Vector tmpV = this->getAbsCoor2D(); |
---|
| 677 | float tmpQ = this->getAbsDir2D(); |
---|
| 678 | |
---|
| 679 | parentNode->addChild2D(this); |
---|
| 680 | |
---|
[6299] | 681 | if (this->parentMode & E2D_PARENT_ROTATE_MOVEMENT) //! @todo implement this. |
---|
[5082] | 682 | ;//this->setRelCoor(this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor())); |
---|
| 683 | else |
---|
[5382] | 684 | this->relCoordinate = (tmpV - parentNode->getAbsCoor2D()); |
---|
| 685 | this->bRelCoorChanged = true; |
---|
[5082] | 686 | |
---|
[5382] | 687 | this->relDirection = (tmpQ - parentNode->getAbsDir2D()); |
---|
| 688 | this->bRelDirChanged = true; |
---|
[5081] | 689 | } |
---|
| 690 | |
---|
[5091] | 691 | /** |
---|
| 692 | * does the reparenting in a very smooth way |
---|
| 693 | * @param parentName the name of the Parent to reconnect to |
---|
| 694 | * @param bias the speed to iterate to this new Positions |
---|
| 695 | */ |
---|
[5382] | 696 | void Element2D::setParentSoft2D(const char* parentName, float bias) |
---|
[5081] | 697 | { |
---|
[5082] | 698 | Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D)); |
---|
| 699 | if (parentNode != NULL) |
---|
[5382] | 700 | this->setParentSoft2D(parentNode, bias); |
---|
[5081] | 701 | } |
---|
| 702 | |
---|
[6299] | 703 | /** |
---|
| 704 | * @param parentMode sets the parentingMode of this Node |
---|
| 705 | */ |
---|
| 706 | void Element2D::setParentMode2D(E2D_PARENT_MODE parentMode) |
---|
[6142] | 707 | { |
---|
[6307] | 708 | this->parentMode = ((this->parentMode & 0xfff0) | parentMode); |
---|
[6142] | 709 | } |
---|
| 710 | |
---|
[6299] | 711 | |
---|
[5091] | 712 | /** |
---|
[6295] | 713 | * @brief sets the mode of this parent manually |
---|
[5091] | 714 | * @param parentMode a String representing this parentingMode |
---|
| 715 | */ |
---|
[5081] | 716 | void Element2D::setParentMode2D (const char* parentingMode) |
---|
| 717 | { |
---|
[5082] | 718 | this->setParentMode2D(Element2D::charToParentingMode2D(parentingMode)); |
---|
[5081] | 719 | } |
---|
| 720 | |
---|
[6299] | 721 | |
---|
[5091] | 722 | /** |
---|
[6299] | 723 | * @returns the NullElement (and if needed (most probably) creates it) |
---|
| 724 | */ |
---|
| 725 | Element2D* Element2D::createNullElement() |
---|
| 726 | { |
---|
| 727 | if (likely(Element2D::nullElement == NULL)) |
---|
| 728 | { |
---|
| 729 | Element2D::nullElement = new Element2D(NULL, E2D_LAYER_BELOW_ALL, E2D_PARENT_MODE_DEFAULT | E2D_REPARENT_TO_NULL); |
---|
| 730 | Element2D::nullElement->setName("NullElement"); |
---|
| 731 | } |
---|
| 732 | return Element2D::nullElement; |
---|
| 733 | } |
---|
| 734 | |
---|
| 735 | |
---|
| 736 | /** |
---|
| 737 | * !! PRIVATE FUNCTION |
---|
| 738 | * @brief checks the upward integrity (e.g if Element2D is somewhere up the Node tree.) |
---|
| 739 | * @param checkParent the Parent to check. |
---|
| 740 | * @returns true if the integrity-check succeeds, false otherwise. |
---|
| 741 | * |
---|
| 742 | * If there is a second occurence of checkParent before NULL, then a loop could get |
---|
| 743 | * into the Tree, and we do not want this. |
---|
| 744 | */ |
---|
| 745 | bool Element2D::checkIntegrity(const Element2D* checkParent) const |
---|
| 746 | { |
---|
| 747 | const Element2D* parent = this; |
---|
| 748 | while ( (parent = parent->getParent2D()) != NULL) |
---|
| 749 | if (unlikely(parent == checkParent)) |
---|
| 750 | return false; |
---|
| 751 | return true; |
---|
| 752 | } |
---|
| 753 | |
---|
| 754 | |
---|
| 755 | /** |
---|
[5091] | 756 | * updates the absCoordinate/absDirection |
---|
| 757 | * @param dt The time passed since the last update |
---|
[5081] | 758 | |
---|
[5091] | 759 | this is used to go through the parent-tree to update all the absolute coordinates |
---|
| 760 | and directions. this update should be done by the engine, so you don't have to |
---|
| 761 | worry, normaly... |
---|
| 762 | */ |
---|
[5081] | 763 | void Element2D::update2D (float dt) |
---|
| 764 | { |
---|
[5089] | 765 | // setting the Position of this 2D-Element. |
---|
[5083] | 766 | if( likely(this->parent != NULL)) |
---|
| 767 | { |
---|
| 768 | // movement for nodes with smoothMove enabled |
---|
| 769 | if (unlikely(this->toCoordinate != NULL)) |
---|
| 770 | { |
---|
[5376] | 771 | Vector moveVect = (*this->toCoordinate - this->relCoordinate) *fabsf(dt)*bias; |
---|
[5082] | 772 | |
---|
[5083] | 773 | if (likely(moveVect.len() >= .001))//PNODE_ITERATION_DELTA)) |
---|
| 774 | { |
---|
| 775 | this->shiftCoor2D(moveVect); |
---|
| 776 | } |
---|
| 777 | else |
---|
| 778 | { |
---|
[5377] | 779 | Vector tmp = *this->toCoordinate; |
---|
| 780 | this->setRelCoor2D(tmp); |
---|
[5083] | 781 | PRINTF(5)("SmoothMove of %s finished\n", this->getName()); |
---|
| 782 | } |
---|
| 783 | } |
---|
| 784 | if (unlikely(this->toDirection != NULL)) |
---|
| 785 | { |
---|
[5376] | 786 | float rotFlot = (*this->toDirection - this->relDirection) *fabsf(dt)*bias; |
---|
| 787 | if (likely(fabsf(rotFlot) >= .001))//PNODE_ITERATION_DELTA)) |
---|
[5083] | 788 | { |
---|
| 789 | this->shiftDir2D(rotFlot); |
---|
| 790 | } |
---|
| 791 | else |
---|
| 792 | { |
---|
[5377] | 793 | float tmp = *this->toDirection; |
---|
| 794 | this->setRelDir2D(tmp); |
---|
[5083] | 795 | PRINTF(5)("SmoothRotate of %s finished\n", this->getName()); |
---|
| 796 | } |
---|
| 797 | } |
---|
| 798 | |
---|
| 799 | // MAIN UPDATE ///////////////////////////////////// |
---|
| 800 | this->lastAbsCoordinate = this->absCoordinate; |
---|
| 801 | |
---|
[5084] | 802 | PRINTF(5)("Element2D::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
[5083] | 803 | |
---|
| 804 | |
---|
[5118] | 805 | if( this->parentMode & E2D_PARENT_LOCAL_ROTATE && this->bRelDirChanged) |
---|
[5083] | 806 | { |
---|
| 807 | /* update the current absDirection - remember * means rotation around sth.*/ |
---|
[5090] | 808 | this->prevRelDirection = this->relDirection; |
---|
[5083] | 809 | this->absDirection = this->relDirection + parent->getAbsDir2D();; |
---|
| 810 | } |
---|
| 811 | |
---|
[5089] | 812 | |
---|
[5397] | 813 | if (unlikely(this->alignment & E2D_ALIGN_SCREEN_CENTER && this->bRelCoorChanged)) |
---|
[5083] | 814 | { |
---|
| 815 | this->prevRelCoordinate = this->relCoordinate; |
---|
[5089] | 816 | this->absCoordinate.x = .5 + this->relCoordinate.x; |
---|
| 817 | this->absCoordinate.y = .5 + this->relCoordinate.y; |
---|
| 818 | this->absCoordinate.z = 0.0; |
---|
[5083] | 819 | } |
---|
[5397] | 820 | else if (unlikely(this->bindNode != NULL)) |
---|
[5083] | 821 | { |
---|
[6778] | 822 | GLdouble projectPos[3] = {0.0, 0.0, 0.0}; |
---|
[5089] | 823 | gluProject(this->bindNode->getAbsCoor().x, |
---|
| 824 | this->bindNode->getAbsCoor().y, |
---|
| 825 | this->bindNode->getAbsCoor().z, |
---|
| 826 | GraphicsEngine::modMat, |
---|
| 827 | GraphicsEngine::projMat, |
---|
| 828 | GraphicsEngine::viewPort, |
---|
| 829 | projectPos, |
---|
| 830 | projectPos+1, |
---|
| 831 | projectPos+2); |
---|
[6778] | 832 | // printf("%s::%s == %f %f %f :: %f %f\n", this->getClassName(), this->getName(), |
---|
| 833 | // this->bindNode->getAbsCoor().x, |
---|
| 834 | // this->bindNode->getAbsCoor().y, |
---|
| 835 | // this->bindNode->getAbsCoor().z, |
---|
| 836 | // projectPos[0], |
---|
| 837 | // projectPos[1]); |
---|
| 838 | |
---|
[5396] | 839 | this->prevRelCoordinate.x = this->absCoordinate.x = projectPos[0] /* /(float)GraphicsEngine::getInstance()->getResolutionX() */ + this->relCoordinate.x; |
---|
| 840 | this->prevRelCoordinate.y = this->absCoordinate.y = (float)GraphicsEngine::getInstance()->getResolutionY() - projectPos[1] + this->relCoordinate.y; |
---|
| 841 | this->prevRelCoordinate.z = this->absCoordinate.z = projectPos[2] + this->relCoordinate.z; |
---|
| 842 | this->bRelCoorChanged = true; |
---|
[5089] | 843 | } |
---|
| 844 | else |
---|
| 845 | { |
---|
| 846 | if(likely(this->parentMode & PNODE_MOVEMENT && this->bRelCoorChanged)) |
---|
| 847 | { |
---|
| 848 | /* update the current absCoordinate */ |
---|
| 849 | this->prevRelCoordinate = this->relCoordinate; |
---|
| 850 | this->absCoordinate = this->parent->getAbsCoor2D() + this->relCoordinate; |
---|
| 851 | } |
---|
| 852 | else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged) |
---|
| 853 | { |
---|
| 854 | /* update the current absCoordinate */ |
---|
| 855 | this->prevRelCoordinate = this->relCoordinate; |
---|
[5090] | 856 | float sine = sin(this->parent->getAbsDir2D()); |
---|
| 857 | float cose = cos(this->parent->getAbsDir2D()); |
---|
| 858 | // this->absCoordinate.x = this->relCoordinate.x*cose - this->relCoordinate.y*sine + this->parent->getRelCoor2D().x*(1-cose) +this->parent->getRelCoor2D().y*sine; |
---|
| 859 | // this->absCoordinate.y = this->relCoordinate.x*sine + this->relCoordinate.y*cose + this->parent->getRelCoor2D().y*(1-cose) +this->parent->getRelCoor2D().x*sine; |
---|
| 860 | |
---|
[5089] | 861 | this->absCoordinate.x = this->parent->getAbsCoor2D().x + (this->relCoordinate.x*cos(this->parent->getAbsDir2D()) - this->relCoordinate.y * sin(this->parent->getAbsDir2D())); |
---|
| 862 | this->absCoordinate.y = this->parent->getAbsCoor2D().y + (this->relCoordinate.x*sin(this->parent->getAbsDir2D()) + this->relCoordinate.y * cos(this->parent->getAbsDir2D())); |
---|
[5083] | 863 | |
---|
[5089] | 864 | } |
---|
[5083] | 865 | } |
---|
| 866 | ///////////////////////////////////////////////// |
---|
| 867 | } |
---|
| 868 | else |
---|
| 869 | { |
---|
[5084] | 870 | PRINTF(5)("Element2D::update - (%f, %f, %f)\n", this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
---|
[5083] | 871 | if (this->bRelCoorChanged) |
---|
[5118] | 872 | { |
---|
| 873 | this->prevRelCoordinate = this->relCoordinate; |
---|
[5083] | 874 | this->absCoordinate = this->relCoordinate; |
---|
[5118] | 875 | } |
---|
[5083] | 876 | if (this->bRelDirChanged) |
---|
[5118] | 877 | { |
---|
| 878 | this->prevRelDirection = this->relDirection; |
---|
[5376] | 879 | this->absDirection = this->getAbsDir2D() + this->relDirection; |
---|
[5118] | 880 | } |
---|
[5083] | 881 | } |
---|
| 882 | |
---|
[5089] | 883 | |
---|
| 884 | // UPDATE CHILDREN |
---|
[6299] | 885 | if(!this->children.empty() || this->parentMode & E2D_UPDATE_CHILDREN_IF_INACTIVE) |
---|
[5083] | 886 | { |
---|
[5775] | 887 | list<Element2D*>::iterator child; |
---|
| 888 | for (child = this->children.begin(); child != this->children.end(); child++) |
---|
[5083] | 889 | { |
---|
| 890 | /* if this node has changed, make sure, that all children are updated also */ |
---|
| 891 | if( likely(this->bRelCoorChanged)) |
---|
[6299] | 892 | (*child)->parentCoorChanged2D(); |
---|
[5083] | 893 | if( likely(this->bRelDirChanged)) |
---|
[6299] | 894 | (*child)->parentDirChanged2D(); |
---|
[5083] | 895 | |
---|
[5775] | 896 | (*child)->update2D(dt); |
---|
[5083] | 897 | } |
---|
| 898 | } |
---|
[5089] | 899 | |
---|
| 900 | // FINISHING PROCESS |
---|
[5083] | 901 | this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt; |
---|
| 902 | this->bRelCoorChanged = false; |
---|
| 903 | this->bRelDirChanged = false; |
---|
[5081] | 904 | } |
---|
| 905 | |
---|
[6299] | 906 | |
---|
[5091] | 907 | /** |
---|
| 908 | * displays some information about this pNode |
---|
| 909 | * @param depth The deph into which to debug the children of this Element2D to. |
---|
| 910 | * (0: all children will be debugged, 1: only this Element2D, 2: this and direct children...) |
---|
| 911 | * @param level The n-th level of the Node we draw (this is internal and only for nice output) |
---|
| 912 | */ |
---|
[5081] | 913 | void Element2D::debug (unsigned int depth, unsigned int level) const |
---|
| 914 | { |
---|
[5082] | 915 | for (unsigned int i = 0; i < level; i++) |
---|
| 916 | PRINT(0)(" |"); |
---|
[5775] | 917 | if (this->children.size() > 0) |
---|
[5082] | 918 | PRINT(0)(" +"); |
---|
| 919 | else |
---|
| 920 | PRINT(0)(" -"); |
---|
[5401] | 921 | PRINT(0)("Element2D(%s::%s) - absCoord: (%0.2f, %0.2f), relCoord(%0.2f, %0.2f), direction(%0.2f) - %s, layer:%s\n", |
---|
| 922 | this->getClassName(), |
---|
| 923 | this->getName(), |
---|
| 924 | this->absCoordinate.x, |
---|
| 925 | this->absCoordinate.y, |
---|
| 926 | this->relCoordinate.x, |
---|
| 927 | this->relCoordinate.y, |
---|
| 928 | this->getAbsDir2D(), |
---|
| 929 | Element2D::parentingModeToChar2D(parentMode), |
---|
| 930 | Element2D::layer2DToChar(this->layer)); |
---|
[5402] | 931 | |
---|
[5082] | 932 | if (depth >= 2 || depth == 0) |
---|
| 933 | { |
---|
[5775] | 934 | list<Element2D*>::const_iterator child; |
---|
| 935 | for (child = this->children.begin(); child != this->children.end(); child++) |
---|
[5082] | 936 | { |
---|
| 937 | if (depth == 0) |
---|
[5775] | 938 | (*child)->debug(0, level + 1); |
---|
[5082] | 939 | else |
---|
[5775] | 940 | (*child)->debug(depth - 1, level +1); |
---|
[5082] | 941 | } |
---|
| 942 | } |
---|
[5081] | 943 | } |
---|
| 944 | |
---|
[5285] | 945 | /** |
---|
| 946 | * ticks the 2d-Element |
---|
| 947 | * @param dt the time elapsed since the last tick |
---|
[5401] | 948 | * |
---|
| 949 | * the element only gets tickt, if it is active. |
---|
| 950 | * Be aware, that this walks through the entire Element2D-tree, |
---|
| 951 | * searching for Elements to be ticked. |
---|
[5285] | 952 | */ |
---|
[5401] | 953 | void Element2D::tick2D(float dt) |
---|
[5285] | 954 | { |
---|
[6299] | 955 | if (this->bActive) |
---|
[5401] | 956 | this->tick(dt); |
---|
[5775] | 957 | if (this->children.size() > 0) |
---|
[5401] | 958 | { |
---|
[5775] | 959 | list<Element2D*>::iterator child; |
---|
| 960 | for (child = this->children.begin(); child != this->children.end(); child++) |
---|
| 961 | (*child)->tick2D(dt); |
---|
[5401] | 962 | } |
---|
| 963 | } |
---|
[5082] | 964 | |
---|
[5401] | 965 | /** |
---|
| 966 | * draws all the Elements from this element2D downwards |
---|
[5404] | 967 | * @param layer the maximal Layer to draw. @see E2D_LAYER |
---|
[5401] | 968 | */ |
---|
[5404] | 969 | void Element2D::draw2D(short layer) const |
---|
[5401] | 970 | { |
---|
[6299] | 971 | if (this->bVisible) |
---|
[5401] | 972 | this->draw(); |
---|
[5775] | 973 | if (this->children.size() > 0) |
---|
[5401] | 974 | { |
---|
[5775] | 975 | list<Element2D*>::const_iterator child; |
---|
| 976 | for (child = this->children.begin(); child != this->children.end(); child++) |
---|
[5401] | 977 | if (likely(layer >= this->layer)) |
---|
[5775] | 978 | (*child)->draw2D(layer); |
---|
[5401] | 979 | } |
---|
[5285] | 980 | } |
---|
| 981 | |
---|
[5091] | 982 | /** |
---|
| 983 | * displays the Element2D at its position with its rotation as a Plane. |
---|
| 984 | */ |
---|
[5417] | 985 | void Element2D::debugDraw2D(unsigned int depth, float size, Vector color, unsigned int level) const |
---|
[5081] | 986 | { |
---|
[5417] | 987 | if (level == 0) |
---|
| 988 | { |
---|
| 989 | glPushAttrib(GL_ENABLE_BIT); |
---|
| 990 | glMatrixMode(GL_MODELVIEW); |
---|
[5082] | 991 | |
---|
[5417] | 992 | glDisable(GL_LIGHTING); |
---|
| 993 | glDisable(GL_BLEND); |
---|
| 994 | glDisable(GL_TEXTURE_2D); |
---|
| 995 | } |
---|
| 996 | |
---|
| 997 | glPushMatrix(); |
---|
| 998 | /* translate */ |
---|
| 999 | /* rotate */ |
---|
| 1000 | glColor3f(color.x, color.y, color.z); |
---|
| 1001 | |
---|
| 1002 | glTranslatef (this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); |
---|
| 1003 | glRotatef(this->getAbsDir2D(), 0,0,1); |
---|
| 1004 | glBegin(GL_LINE_LOOP); |
---|
[5418] | 1005 | glVertex2f(0, 0); |
---|
| 1006 | glVertex2f(0, +this->getSizeY2D()); |
---|
[5417] | 1007 | glVertex2f(+this->getSizeX2D(), +this->getSizeY2D()); |
---|
[5418] | 1008 | glVertex2f(+this->getSizeX2D(), 0); |
---|
[5417] | 1009 | glEnd(); |
---|
| 1010 | |
---|
| 1011 | |
---|
| 1012 | glPopMatrix(); |
---|
| 1013 | if (depth >= 2 || depth == 0) |
---|
| 1014 | { |
---|
| 1015 | Vector childColor = Color::HSVtoRGB(Color::RGBtoHSV(color)+Vector(20,0,.0)); |
---|
[5775] | 1016 | list<Element2D*>::const_iterator child; |
---|
| 1017 | for (child = this->children.begin(); child != this->children.end(); child++) |
---|
[5417] | 1018 | { |
---|
| 1019 | // drawing the Dependency graph |
---|
[6299] | 1020 | if (this != Element2D::getNullElement()) |
---|
[5417] | 1021 | { |
---|
| 1022 | glBegin(GL_LINES); |
---|
| 1023 | glColor3f(color.x, color.y, color.z); |
---|
| 1024 | glVertex3f(this->getAbsCoor2D ().x, |
---|
| 1025 | this->getAbsCoor2D ().y, |
---|
| 1026 | 0); |
---|
| 1027 | glColor3f(childColor.x, childColor.y, childColor.z); |
---|
[5775] | 1028 | glVertex3f((*child)->getAbsCoor2D ().x, |
---|
| 1029 | (*child)->getAbsCoor2D ().y, |
---|
[5417] | 1030 | 0); |
---|
| 1031 | glEnd(); |
---|
| 1032 | } |
---|
| 1033 | if (depth == 0) |
---|
[5775] | 1034 | (*child)->debugDraw2D(0, size, childColor, level+1); |
---|
[5417] | 1035 | else |
---|
[5775] | 1036 | (*child)->debugDraw2D(depth - 1, size, childColor, level +1); |
---|
[5417] | 1037 | } |
---|
| 1038 | } |
---|
| 1039 | if (level == 0) |
---|
| 1040 | glPopAttrib(); |
---|
| 1041 | |
---|
[5081] | 1042 | } |
---|
| 1043 | |
---|
| 1044 | |
---|
| 1045 | // helper functions // |
---|
[5091] | 1046 | /** |
---|
| 1047 | * converts a parentingMode into a string that is the name of it |
---|
| 1048 | * @param parentingMode the ParentingMode to convert |
---|
| 1049 | * @return the converted string |
---|
| 1050 | */ |
---|
[5082] | 1051 | const char* Element2D::parentingModeToChar2D(int parentingMode) |
---|
[5081] | 1052 | { |
---|
[5082] | 1053 | if (parentingMode == E2D_PARENT_LOCAL_ROTATE) |
---|
| 1054 | return "local-rotate"; |
---|
| 1055 | else if (parentingMode == E2D_PARENT_ROTATE_MOVEMENT) |
---|
| 1056 | return "rotate-movement"; |
---|
| 1057 | else if (parentingMode == E2D_PARENT_MOVEMENT) |
---|
| 1058 | return "movement"; |
---|
| 1059 | else if (parentingMode == E2D_PARENT_ALL) |
---|
| 1060 | return "all"; |
---|
| 1061 | else if (parentingMode == E2D_PARENT_ROTATE_AND_MOVE) |
---|
| 1062 | return "rotate-and-move"; |
---|
[5081] | 1063 | } |
---|
| 1064 | |
---|
[5091] | 1065 | /** |
---|
| 1066 | * converts a parenting-mode-string into a int |
---|
| 1067 | * @param parentingMode the string naming the parentingMode |
---|
| 1068 | * @return the int corresponding to the named parentingMode |
---|
| 1069 | */ |
---|
[5082] | 1070 | E2D_PARENT_MODE Element2D::charToParentingMode2D(const char* parentingMode) |
---|
[5081] | 1071 | { |
---|
[5082] | 1072 | if (!strcmp(parentingMode, "local-rotate")) |
---|
| 1073 | return (E2D_PARENT_LOCAL_ROTATE); |
---|
| 1074 | else if (!strcmp(parentingMode, "rotate-movement")) |
---|
| 1075 | return (E2D_PARENT_ROTATE_MOVEMENT); |
---|
| 1076 | else if (!strcmp(parentingMode, "movement")) |
---|
| 1077 | return (E2D_PARENT_MOVEMENT); |
---|
| 1078 | else if (!strcmp(parentingMode, "all")) |
---|
| 1079 | return (E2D_PARENT_ALL); |
---|
| 1080 | else if (!strcmp(parentingMode, "rotate-and-move")) |
---|
| 1081 | return (E2D_PARENT_ROTATE_AND_MOVE); |
---|
[5081] | 1082 | } |
---|
| 1083 | |
---|
[5401] | 1084 | /** |
---|
| 1085 | * converts a layer into its corresponding string |
---|
| 1086 | * @param layer the layer to get the name-String of. |
---|
| 1087 | * @returns the Name of the Layer (on error the default-layer-string is returned) |
---|
| 1088 | */ |
---|
| 1089 | const char* Element2D::layer2DToChar(E2D_LAYER layer) |
---|
| 1090 | { |
---|
| 1091 | switch(layer) |
---|
| 1092 | { |
---|
| 1093 | case E2D_LAYER_TOP: |
---|
| 1094 | return "top"; |
---|
| 1095 | break; |
---|
| 1096 | case E2D_LAYER_MEDIUM: |
---|
| 1097 | return "medium"; |
---|
| 1098 | break; |
---|
| 1099 | case E2D_LAYER_BOTTOM: |
---|
| 1100 | return "bottom"; |
---|
| 1101 | break; |
---|
| 1102 | case E2D_LAYER_BELOW_ALL: |
---|
| 1103 | return "below-all"; |
---|
| 1104 | break; |
---|
| 1105 | default: |
---|
| 1106 | return layer2DToChar(E2D_DEFAULT_LAYER); |
---|
| 1107 | break; |
---|
| 1108 | } |
---|
| 1109 | } |
---|
[5081] | 1110 | |
---|
[5401] | 1111 | /** |
---|
| 1112 | * converts a String holding a actual Layer |
---|
| 1113 | * @param layer the String to convert into a Layer2D |
---|
| 1114 | * @returns the E2D_LAYER on success, E2D_DEFAULT_LAYER on error. |
---|
| 1115 | */ |
---|
| 1116 | E2D_LAYER Element2D::charToLayer2D(const char* layer) |
---|
| 1117 | { |
---|
| 1118 | if (!strcmp(layer, "top")) |
---|
| 1119 | return (E2D_LAYER_TOP); |
---|
| 1120 | else if (!strcmp(layer, "medium")) |
---|
| 1121 | return (E2D_LAYER_MEDIUM); |
---|
| 1122 | else if (!strcmp(layer, "bottom")) |
---|
| 1123 | return (E2D_LAYER_BOTTOM); |
---|
| 1124 | else if (!strcmp(layer, "below-all")) |
---|
| 1125 | return (E2D_LAYER_BELOW_ALL); |
---|
| 1126 | else |
---|
| 1127 | return (E2D_DEFAULT_LAYER); |
---|
| 1128 | } |
---|