| 1 | /* |
|---|
| 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. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Benjamin Grauer |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
|---|
| 17 | |
|---|
| 18 | #include "element_2d.h" |
|---|
| 19 | #include "render_2d.h" |
|---|
| 20 | |
|---|
| 21 | #include "graphics_engine.h" |
|---|
| 22 | #include "p_node.h" |
|---|
| 23 | #include "load_param.h" |
|---|
| 24 | #include "tinyxml.h" |
|---|
| 25 | #include "class_list.h" |
|---|
| 26 | #include "list.h" |
|---|
| 27 | |
|---|
| 28 | using namespace std; |
|---|
| 29 | |
|---|
| 30 | Element2D::Element2D() |
|---|
| 31 | { |
|---|
| 32 | this->init(); |
|---|
| 33 | this->setParent2D(NullElement2D::getInstance()); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * standard constructor |
|---|
| 38 | * @todo this constructor is not jet implemented - do it |
|---|
| 39 | */ |
|---|
| 40 | Element2D::Element2D (Element2D* parent) |
|---|
| 41 | { |
|---|
| 42 | this->init(); |
|---|
| 43 | |
|---|
| 44 | if (this->parent != NULL) |
|---|
| 45 | this->setParent2D(parent); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * standard deconstructor |
|---|
| 50 | */ |
|---|
| 51 | Element2D::~Element2D () |
|---|
| 52 | { |
|---|
| 53 | // delete what has to be deleted here |
|---|
| 54 | Render2D::getInstance()->unregisterElement2D(this); |
|---|
| 55 | |
|---|
| 56 | if (this->parent) |
|---|
| 57 | this->parent->removeChild2D(this); |
|---|
| 58 | else |
|---|
| 59 | { |
|---|
| 60 | tIterator<Element2D>* iterator = this->children->getIterator(); |
|---|
| 61 | Element2D* pn = iterator->firstElement(); |
|---|
| 62 | while( pn != NULL) |
|---|
| 63 | { |
|---|
| 64 | delete pn; |
|---|
| 65 | pn = iterator->nextElement(); |
|---|
| 66 | } |
|---|
| 67 | delete iterator; |
|---|
| 68 | /* this deletes all children in the list */ |
|---|
| 69 | } |
|---|
| 70 | delete this->children; |
|---|
| 71 | |
|---|
| 72 | if (this->toCoordinate != NULL) |
|---|
| 73 | delete this->toCoordinate; |
|---|
| 74 | if (this->toDirection != NULL) |
|---|
| 75 | delete this->toDirection; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | /** |
|---|
| 80 | * initializes a Element2D |
|---|
| 81 | */ |
|---|
| 82 | void Element2D::init() |
|---|
| 83 | { |
|---|
| 84 | this->setClassID(CL_ELEMENT_2D, "Element2D"); |
|---|
| 85 | |
|---|
| 86 | this->setVisibility(true); |
|---|
| 87 | this->setActiveness(true); |
|---|
| 88 | this->setAlignment(E2D_ALIGN_NONE); |
|---|
| 89 | this->layer = E2D_TOP; |
|---|
| 90 | this->bindNode = NULL; |
|---|
| 91 | |
|---|
| 92 | this->setParentMode2D(E2D_PARENT_ALL); |
|---|
| 93 | this->parent = NULL; |
|---|
| 94 | this->children = new tList<Element2D>; |
|---|
| 95 | this->absDirection = 0.0; |
|---|
| 96 | this->relDirection = 0.0; |
|---|
| 97 | this->bRelCoorChanged = true; |
|---|
| 98 | this->bRelDirChanged = true; |
|---|
| 99 | this->toCoordinate = NULL; |
|---|
| 100 | this->toDirection = NULL; |
|---|
| 101 | // else |
|---|
| 102 | // this->setParent2D(parent); |
|---|
| 103 | |
|---|
| 104 | Render2D::getInstance()->registerElement2D(this); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | /** |
|---|
| 108 | * Loads the Parameters of an Element2D from... |
|---|
| 109 | * @param root The XML-element to load from |
|---|
| 110 | */ |
|---|
| 111 | void Element2D::loadParams(const TiXmlElement* root) |
|---|
| 112 | { |
|---|
| 113 | LoadParam<Element2D>(root, "alignment", this, &Element2D::setAlignment) |
|---|
| 114 | .describe("loads the alignment: (either: center, left, right or screen-center)"); |
|---|
| 115 | |
|---|
| 116 | LoadParam<Element2D>(root, "layer", this, &Element2D::setLayer) |
|---|
| 117 | .describe("loads the layer onto which to project: (either: top, medium, bottom, below-all)"); |
|---|
| 118 | |
|---|
| 119 | LoadParam<Element2D>(root, "bind-node", this, &Element2D::setBindNode) |
|---|
| 120 | .describe("sets a node, this 2D-Element should be shown upon (name of the node)"); |
|---|
| 121 | |
|---|
| 122 | LoadParam<Element2D>(root, "visibility", this, &Element2D::setVisibility) |
|---|
| 123 | .describe("if the Element is visible or not"); |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | // PNode-style: |
|---|
| 127 | LoadParam<Element2D>(root, "rel-coor", this, &Element2D::setRelCoor2D) |
|---|
| 128 | .describe("Sets The relative position of the Node to its parent."); |
|---|
| 129 | |
|---|
| 130 | LoadParam<Element2D>(root, "abs-coor", this, &Element2D::setAbsCoor2D) |
|---|
| 131 | .describe("Sets The absolute Position of the Node."); |
|---|
| 132 | |
|---|
| 133 | LoadParam<Element2D>(root, "rel-dir", this, &Element2D::setRelDir2D) |
|---|
| 134 | .describe("Sets The relative rotation of the Node to its parent."); |
|---|
| 135 | |
|---|
| 136 | LoadParam<Element2D>(root, "abs-dir", this, &Element2D::setAbsDir2D) |
|---|
| 137 | .describe("Sets The absolute rotation of the Node."); |
|---|
| 138 | |
|---|
| 139 | LoadParam<Element2D>(root, "parent", this, &Element2D::setParent2D) |
|---|
| 140 | .describe("the Name of the Parent of this Element2D"); |
|---|
| 141 | |
|---|
| 142 | LoadParam<Element2D>(root, "parent-mode", this, &Element2D::setParentMode2D) |
|---|
| 143 | .describe("the mode to connect this node to its parent ()"); |
|---|
| 144 | |
|---|
| 145 | // cycling properties |
|---|
| 146 | if (root != NULL) |
|---|
| 147 | { |
|---|
| 148 | const TiXmlElement* element = root->FirstChildElement(); |
|---|
| 149 | while (element != NULL) |
|---|
| 150 | { |
|---|
| 151 | LoadParam<Element2D>(root, "parent", this, &Element2D::addChild2D, true) |
|---|
| 152 | .describe("adds a new Child to the current Node."); |
|---|
| 153 | |
|---|
| 154 | element = element->NextSiblingElement(); |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | /** |
|---|
| 160 | * sets the alignment of the 2D-element in form of a String |
|---|
| 161 | * @param alignment the alignment @see loadParams |
|---|
| 162 | */ |
|---|
| 163 | void Element2D::setAlignment(const char* alignment) |
|---|
| 164 | { |
|---|
| 165 | if (!strcmp(alignment, "center")) |
|---|
| 166 | this->setAlignment(E2D_ALIGN_CENTER); |
|---|
| 167 | else if (!strcmp(alignment, "left")) |
|---|
| 168 | this->setAlignment(E2D_ALIGN_LEFT); |
|---|
| 169 | else if (!strcmp(alignment, "right")) |
|---|
| 170 | this->setAlignment(E2D_ALIGN_RIGHT); |
|---|
| 171 | else if (!strcmp(alignment, "screen-center")) |
|---|
| 172 | this->setAlignment(E2D_ALIGN_SCREEN_CENTER); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | /** |
|---|
| 177 | * moves a Element to another layer |
|---|
| 178 | * @param layer the Layer this is drawn on |
|---|
| 179 | */ |
|---|
| 180 | void Element2D::setLayer(E2D_LAYER layer) |
|---|
| 181 | { |
|---|
| 182 | Render2D::getInstance()->moveToLayer(this, layer); |
|---|
| 183 | this->layer = layer; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | /** |
|---|
| 187 | * sets the layer onto which this 2D-element is projected to. |
|---|
| 188 | * @param layer the layer @see loadParams |
|---|
| 189 | */ |
|---|
| 190 | void Element2D::setLayer(const char* layer) |
|---|
| 191 | { |
|---|
| 192 | if (!strcmp(layer, "top")) |
|---|
| 193 | this->setLayer(E2D_TOP); |
|---|
| 194 | else if (!strcmp(layer, "medium")) |
|---|
| 195 | this->setLayer(E2D_MEDIUM); |
|---|
| 196 | else if (!strcmp(layer, "bottom")) |
|---|
| 197 | this->setLayer(E2D_BOTTOM); |
|---|
| 198 | else if (!strcmp(layer, "below-all")) |
|---|
| 199 | this->setLayer(E2D_BELOW_ALL); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | /** |
|---|
| 204 | * sets a node, this 2D-Element should be shown upon |
|---|
| 205 | * @param bindNode the name of the Node (should be existing) |
|---|
| 206 | */ |
|---|
| 207 | void Element2D::setBindNode(const char* bindNode) |
|---|
| 208 | { |
|---|
| 209 | const PNode* tmpBindNode = dynamic_cast<const PNode*>(ClassList::getObject(bindNode, CL_PARENT_NODE)); |
|---|
| 210 | if (tmpBindNode != NULL) |
|---|
| 211 | this->bindNode = tmpBindNode; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | /** |
|---|
| 215 | * sets the relative coordinate of the Element2D to its parent |
|---|
| 216 | * @param relCoord the relative coordinate to the parent |
|---|
| 217 | */ |
|---|
| 218 | void Element2D::setRelCoor2D (const Vector& relCoord) |
|---|
| 219 | { |
|---|
| 220 | if (this->toCoordinate!= NULL) |
|---|
| 221 | { |
|---|
| 222 | delete this->toCoordinate; |
|---|
| 223 | this->toCoordinate = NULL; |
|---|
| 224 | } |
|---|
| 225 | this->relCoordinate = relCoord; |
|---|
| 226 | this->bRelCoorChanged = true; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | /** |
|---|
| 231 | * sets the relative coordinate of the Element2D to its Parent |
|---|
| 232 | * @param x the x coordinate |
|---|
| 233 | * @param y the y coordinate |
|---|
| 234 | * @param z the z coordinate |
|---|
| 235 | */ |
|---|
| 236 | void Element2D::setRelCoor2D (float x, float y, float z) |
|---|
| 237 | { |
|---|
| 238 | this->setRelCoor2D(Vector(x,y,z)); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | /** |
|---|
| 242 | * sets the Relative coordinate to the parent in Pixels |
|---|
| 243 | * @param x the relCoord X |
|---|
| 244 | * @param y the relCoord Y |
|---|
| 245 | */ |
|---|
| 246 | void Element2D::setRelCoor2Dpx (int x, int y) |
|---|
| 247 | { |
|---|
| 248 | this->setRelCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(), |
|---|
| 249 | (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(), |
|---|
| 250 | 0 |
|---|
| 251 | )); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | /** |
|---|
| 255 | * sets a new relative position smoothely |
|---|
| 256 | * @param relCoordSoft the new Position to iterate to |
|---|
| 257 | * @param bias how fast to iterate to this position |
|---|
| 258 | */ |
|---|
| 259 | void Element2D::setRelCoorSoft2D(const Vector& relCoordSoft, float bias) |
|---|
| 260 | { |
|---|
| 261 | if (likely(this->toCoordinate == NULL)) |
|---|
| 262 | this->toCoordinate = new Vector(); |
|---|
| 263 | |
|---|
| 264 | *this->toCoordinate = relCoordSoft; |
|---|
| 265 | this->bias = bias; |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | /** |
|---|
| 269 | * sets a new relative position smoothely |
|---|
| 270 | * @param x the new x-coordinate in Pixels of the Position to iterate to |
|---|
| 271 | * @param y the new y-coordinate in Pixels of the Position to iterate to |
|---|
| 272 | * @param bias how fast to iterate to this position |
|---|
| 273 | */ |
|---|
| 274 | void Element2D::setRelCoorSoft2Dpx (int x, int y, float bias) |
|---|
| 275 | { |
|---|
| 276 | this->setRelCoorSoft2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(), |
|---|
| 277 | (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(), |
|---|
| 278 | 0), |
|---|
| 279 | bias); |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | /** |
|---|
| 283 | * set relative coordinates smoothely |
|---|
| 284 | * @param x x-relative coordinates to its parent |
|---|
| 285 | * @param y y-relative coordinates to its parent |
|---|
| 286 | * @param z z-relative coordinates to its parent |
|---|
| 287 | * @see void PNode::setRelCoorSoft (const Vector&, float) |
|---|
| 288 | */ |
|---|
| 289 | void Element2D::setRelCoorSoft2D(float x, float y, float depth, float bias) |
|---|
| 290 | { |
|---|
| 291 | this->setRelCoorSoft2D(Vector(x, y, depth), bias); |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | /** |
|---|
| 295 | * @param absCoord set absolute coordinate |
|---|
| 296 | */ |
|---|
| 297 | void Element2D::setAbsCoor2D (const Vector& absCoord) |
|---|
| 298 | { |
|---|
| 299 | if (this->toCoordinate!= NULL) |
|---|
| 300 | { |
|---|
| 301 | delete this->toCoordinate; |
|---|
| 302 | this->toCoordinate = NULL; |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | if( likely(this->parentMode & E2D_PARENT_MOVEMENT)) |
|---|
| 306 | { |
|---|
| 307 | /* if you have set the absolute coordinates this overrides all other changes */ |
|---|
| 308 | if (likely(this->parent != NULL)) |
|---|
| 309 | this->relCoordinate = absCoord - parent->getAbsCoor2D (); |
|---|
| 310 | else |
|---|
| 311 | this->relCoordinate = absCoord; |
|---|
| 312 | } |
|---|
| 313 | if( this->parentMode & E2D_PARENT_ROTATE_MOVEMENT) |
|---|
| 314 | { |
|---|
| 315 | if (likely(this->parent != NULL)) |
|---|
| 316 | this->relCoordinate = absCoord - parent->getAbsCoor2D (); |
|---|
| 317 | else |
|---|
| 318 | this->relCoordinate = absCoord; |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | this->bRelCoorChanged = true; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | /** |
|---|
| 325 | * @param x x-coordinate. |
|---|
| 326 | * @param y y-coordinate. |
|---|
| 327 | * @param z z-coordinate. |
|---|
| 328 | * @see void PNode::setAbsCoor (const Vector& absCoord) |
|---|
| 329 | */ |
|---|
| 330 | void Element2D::setAbsCoor2D (float x, float y, float depth) |
|---|
| 331 | { |
|---|
| 332 | this->setAbsCoor2D(Vector(x, y, depth)); |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | /** |
|---|
| 336 | * @param x x-coordinate in Pixels |
|---|
| 337 | * @param y y-coordinate in Pixels |
|---|
| 338 | * @see void PNode::setAbsCoor (const Vector& absCoord) |
|---|
| 339 | */ |
|---|
| 340 | void Element2D::setAbsCoor2Dpx (int x, int y) |
|---|
| 341 | { |
|---|
| 342 | this->setAbsCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(), |
|---|
| 343 | (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(), |
|---|
| 344 | 0 |
|---|
| 345 | )); |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | /** |
|---|
| 349 | * shift coordinate ralative |
|---|
| 350 | * @param shift shift vector |
|---|
| 351 | * |
|---|
| 352 | * This simply adds the shift-Vector to the relative Coordinate |
|---|
| 353 | */ |
|---|
| 354 | void Element2D::shiftCoor2D (const Vector& shift) |
|---|
| 355 | { |
|---|
| 356 | this->relCoordinate += shift; |
|---|
| 357 | this->bRelCoorChanged = true; |
|---|
| 358 | |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | /** |
|---|
| 362 | * shifts in PixelSpace |
|---|
| 363 | * @param x the pixels to shift in X |
|---|
| 364 | * @param y the pixels to shift in Y |
|---|
| 365 | */ |
|---|
| 366 | void Element2D::shiftCoor2Dpx (int x, int y) |
|---|
| 367 | { |
|---|
| 368 | this->shiftCoor2D(Vector((float)x/(float)GraphicsEngine::getInstance()->getResolutionX(), |
|---|
| 369 | (float)y/(float)GraphicsEngine::getInstance()->getResolutionY(), |
|---|
| 370 | 0)); |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | /** |
|---|
| 374 | * set relative direction |
|---|
| 375 | * @param relDir to its parent |
|---|
| 376 | */ |
|---|
| 377 | void Element2D::setRelDir2D (float relDir) |
|---|
| 378 | { |
|---|
| 379 | if (this->toDirection!= NULL) |
|---|
| 380 | { |
|---|
| 381 | delete this->toDirection; |
|---|
| 382 | this->toDirection = NULL; |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | this->relDirection = relDir; |
|---|
| 386 | this->bRelDirChanged = true; |
|---|
| 387 | } |
|---|
| 388 | |
|---|
| 389 | /** |
|---|
| 390 | * sets the Relative Direction of this node to its parent in a Smoothed way |
|---|
| 391 | * @param relDirSoft the direction to iterate to smoothely. |
|---|
| 392 | * @param bias how fast to iterate to the new Direction |
|---|
| 393 | */ |
|---|
| 394 | void Element2D::setRelDirSoft2D(float relDirSoft, float bias) |
|---|
| 395 | { |
|---|
| 396 | if (likely(this->toDirection == NULL)) |
|---|
| 397 | this->toDirection = new float; |
|---|
| 398 | |
|---|
| 399 | *this->toDirection = relDirSoft; |
|---|
| 400 | this->bias = bias; |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | /** |
|---|
| 404 | * sets the absolute direction |
|---|
| 405 | * @param absDir absolute coordinates |
|---|
| 406 | */ |
|---|
| 407 | void Element2D::setAbsDir2D (float absDir) |
|---|
| 408 | { |
|---|
| 409 | if (this->toDirection!= NULL) |
|---|
| 410 | { |
|---|
| 411 | delete this->toDirection; |
|---|
| 412 | this->toDirection = NULL; |
|---|
| 413 | } |
|---|
| 414 | |
|---|
| 415 | if (likely(this->parent != NULL)) |
|---|
| 416 | this->relDirection = absDir - this->parent->getAbsDir2D(); |
|---|
| 417 | else |
|---|
| 418 | this->relDirection = absDir; |
|---|
| 419 | |
|---|
| 420 | this->bRelDirChanged = true; |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | /** |
|---|
| 424 | * shift Direction |
|---|
| 425 | * @param shift the direction around which to shift. |
|---|
| 426 | */ |
|---|
| 427 | void Element2D::shiftDir2D (float shiftDir) |
|---|
| 428 | { |
|---|
| 429 | this->relDirection = this->relDirection + shiftDir; |
|---|
| 430 | this->bRelDirChanged = true; |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | /** |
|---|
| 434 | * adds a child and makes this node to a parent |
|---|
| 435 | * @param child child reference |
|---|
| 436 | * @param parentMode on which changes the child should also change ist state |
|---|
| 437 | * |
|---|
| 438 | * use this to add a child to this node. |
|---|
| 439 | */ |
|---|
| 440 | void Element2D::addChild2D (Element2D* child, int parentingMode) |
|---|
| 441 | { |
|---|
| 442 | if( likely(child->parent != NULL)) |
|---|
| 443 | { |
|---|
| 444 | PRINTF(4)("Element2D::addChild() - reparenting node: removing it and adding it again\n"); |
|---|
| 445 | child->parent->children->remove(child); |
|---|
| 446 | } |
|---|
| 447 | if (parentingMode != E2D_PARENT_NONE) |
|---|
| 448 | child->parentMode = parentingMode; |
|---|
| 449 | child->parent = this; |
|---|
| 450 | this->children->add(child); |
|---|
| 451 | child->parentCoorChanged(); |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | /** |
|---|
| 455 | * @see Element2D::addChild(Element2D* child); |
|---|
| 456 | * @param childName the name of the child to add to this PNode |
|---|
| 457 | */ |
|---|
| 458 | void Element2D::addChild2D (const char* childName) |
|---|
| 459 | { |
|---|
| 460 | Element2D* childNode = dynamic_cast<Element2D*>(ClassList::getObject(childName, CL_ELEMENT_2D)); |
|---|
| 461 | if (childNode != NULL) |
|---|
| 462 | this->addChild2D(childNode); |
|---|
| 463 | } |
|---|
| 464 | |
|---|
| 465 | /** |
|---|
| 466 | * removes a child from the node |
|---|
| 467 | * @param child the child to remove from this Node.. |
|---|
| 468 | * |
|---|
| 469 | * Children from nodes will not be lost, they are referenced to NullPointer |
|---|
| 470 | */ |
|---|
| 471 | void Element2D::removeChild2D (Element2D* child) |
|---|
| 472 | { |
|---|
| 473 | if (child != NULL) |
|---|
| 474 | { |
|---|
| 475 | child->remove2D(); |
|---|
| 476 | // this->children->remove(child); |
|---|
| 477 | // child->parent = NULL; |
|---|
| 478 | } |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | /** |
|---|
| 482 | * remove this pnode from the tree and adds all following to NullParent |
|---|
| 483 | * |
|---|
| 484 | * this can be the case, if an entity in the world is being destroyed. |
|---|
| 485 | */ |
|---|
| 486 | void Element2D::remove2D() |
|---|
| 487 | { |
|---|
| 488 | tIterator<Element2D>* iterator = this->children->getIterator(); |
|---|
| 489 | Element2D* pn = iterator->firstElement(); |
|---|
| 490 | |
|---|
| 491 | while( pn != NULL) |
|---|
| 492 | { |
|---|
| 493 | NullElement2D::getInstance()->addChild2D(pn, pn->getParentMode2D()); |
|---|
| 494 | pn = iterator->nextElement(); |
|---|
| 495 | } |
|---|
| 496 | delete iterator; |
|---|
| 497 | if (this->parent != NULL) |
|---|
| 498 | this->parent->children->remove(this); |
|---|
| 499 | } |
|---|
| 500 | |
|---|
| 501 | /** |
|---|
| 502 | * sets the parent of this Element2D |
|---|
| 503 | * @param parent the Parent to set |
|---|
| 504 | */ |
|---|
| 505 | void Element2D::setParent2D (Element2D* parent) |
|---|
| 506 | { |
|---|
| 507 | parent->addChild2D(this); |
|---|
| 508 | } |
|---|
| 509 | |
|---|
| 510 | /** |
|---|
| 511 | * @see Element2D::setParent(Element2D* parent); |
|---|
| 512 | * @param parentName the name of the Parent to set to this Element2D |
|---|
| 513 | */ |
|---|
| 514 | void Element2D::setParent2D (const char* parentName) |
|---|
| 515 | { |
|---|
| 516 | Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D)); |
|---|
| 517 | if (parentNode != NULL) |
|---|
| 518 | parentNode->addChild2D(this); |
|---|
| 519 | |
|---|
| 520 | } |
|---|
| 521 | |
|---|
| 522 | /** |
|---|
| 523 | * does the reparenting in a very smooth way |
|---|
| 524 | * @param parentNode the new Node to connect this node to. |
|---|
| 525 | * @param bias the speed to iterate to this new Positions |
|---|
| 526 | */ |
|---|
| 527 | void Element2D::softReparent2D(Element2D* parentNode, float bias) |
|---|
| 528 | { |
|---|
| 529 | if (this->parent == parentNode) |
|---|
| 530 | return; |
|---|
| 531 | |
|---|
| 532 | if (likely(this->toCoordinate == NULL)) |
|---|
| 533 | { |
|---|
| 534 | this->toCoordinate = new Vector(); |
|---|
| 535 | *this->toCoordinate = this->getRelCoor2D(); |
|---|
| 536 | } |
|---|
| 537 | if (likely(this->toDirection == NULL)) |
|---|
| 538 | { |
|---|
| 539 | this->toDirection = new float; |
|---|
| 540 | *this->toDirection = this->getRelDir2D(); |
|---|
| 541 | } |
|---|
| 542 | this->bias = bias; |
|---|
| 543 | |
|---|
| 544 | |
|---|
| 545 | Vector tmpV = this->getAbsCoor2D(); |
|---|
| 546 | float tmpQ = this->getAbsDir2D(); |
|---|
| 547 | |
|---|
| 548 | parentNode->addChild2D(this); |
|---|
| 549 | |
|---|
| 550 | if (this->parentMode & PNODE_ROTATE_MOVEMENT) |
|---|
| 551 | ;//this->setRelCoor(this->parent->getAbsDir().inverse().apply(tmpV - this->parent->getAbsCoor())); |
|---|
| 552 | else |
|---|
| 553 | this->setRelCoor2D(tmpV - parentNode->getAbsCoor2D()); |
|---|
| 554 | |
|---|
| 555 | this->setRelDir2D(tmpQ - parentNode->getAbsDir2D()); |
|---|
| 556 | } |
|---|
| 557 | |
|---|
| 558 | /** |
|---|
| 559 | * does the reparenting in a very smooth way |
|---|
| 560 | * @param parentName the name of the Parent to reconnect to |
|---|
| 561 | * @param bias the speed to iterate to this new Positions |
|---|
| 562 | */ |
|---|
| 563 | void Element2D::softReparent2D(const char* parentName, float bias) |
|---|
| 564 | { |
|---|
| 565 | Element2D* parentNode = dynamic_cast<Element2D*>(ClassList::getObject(parentName, CL_ELEMENT_2D)); |
|---|
| 566 | if (parentNode != NULL) |
|---|
| 567 | this->softReparent2D(parentNode, bias); |
|---|
| 568 | } |
|---|
| 569 | |
|---|
| 570 | /** |
|---|
| 571 | * sets the mode of this parent manually |
|---|
| 572 | * @param parentMode a String representing this parentingMode |
|---|
| 573 | */ |
|---|
| 574 | void Element2D::setParentMode2D (const char* parentingMode) |
|---|
| 575 | { |
|---|
| 576 | this->setParentMode2D(Element2D::charToParentingMode2D(parentingMode)); |
|---|
| 577 | } |
|---|
| 578 | |
|---|
| 579 | /** |
|---|
| 580 | * updates the absCoordinate/absDirection |
|---|
| 581 | * @param dt The time passed since the last update |
|---|
| 582 | |
|---|
| 583 | this is used to go through the parent-tree to update all the absolute coordinates |
|---|
| 584 | and directions. this update should be done by the engine, so you don't have to |
|---|
| 585 | worry, normaly... |
|---|
| 586 | */ |
|---|
| 587 | void Element2D::update2D (float dt) |
|---|
| 588 | { |
|---|
| 589 | // setting the Position of this 2D-Element. |
|---|
| 590 | if( likely(this->parent != NULL)) |
|---|
| 591 | { |
|---|
| 592 | // movement for nodes with smoothMove enabled |
|---|
| 593 | if (unlikely(this->toCoordinate != NULL)) |
|---|
| 594 | { |
|---|
| 595 | Vector moveVect = (*this->toCoordinate - this->getRelCoor2D()) *dt*bias; |
|---|
| 596 | |
|---|
| 597 | if (likely(moveVect.len() >= .001))//PNODE_ITERATION_DELTA)) |
|---|
| 598 | { |
|---|
| 599 | this->shiftCoor2D(moveVect); |
|---|
| 600 | } |
|---|
| 601 | else |
|---|
| 602 | { |
|---|
| 603 | delete this->toCoordinate; |
|---|
| 604 | this->toCoordinate = NULL; |
|---|
| 605 | PRINTF(5)("SmoothMove of %s finished\n", this->getName()); |
|---|
| 606 | } |
|---|
| 607 | } |
|---|
| 608 | if (unlikely(this->toDirection != NULL)) |
|---|
| 609 | { |
|---|
| 610 | float rotFlot = (*this->toDirection - this->getRelDir2D()) *dt*bias; |
|---|
| 611 | if (likely(rotFlot >= .001))//PNODE_ITERATION_DELTA)) |
|---|
| 612 | { |
|---|
| 613 | this->shiftDir2D(rotFlot); |
|---|
| 614 | } |
|---|
| 615 | else |
|---|
| 616 | { |
|---|
| 617 | delete this->toDirection; |
|---|
| 618 | this->toDirection = NULL; |
|---|
| 619 | PRINTF(5)("SmoothRotate of %s finished\n", this->getName()); |
|---|
| 620 | } |
|---|
| 621 | } |
|---|
| 622 | |
|---|
| 623 | // MAIN UPDATE ///////////////////////////////////// |
|---|
| 624 | this->lastAbsCoordinate = this->absCoordinate; |
|---|
| 625 | |
|---|
| 626 | PRINTF(5)("Element2D::update - %s - (%f, %f, %f)\n", this->getName(), this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
|---|
| 627 | |
|---|
| 628 | |
|---|
| 629 | if( this->parentMode & E2D_PARENT_LOCAL_ROTATE && this->bRelDirChanged) |
|---|
| 630 | { |
|---|
| 631 | /* update the current absDirection - remember * means rotation around sth.*/ |
|---|
| 632 | this->prevRelDirection = this->relDirection; |
|---|
| 633 | this->absDirection = this->relDirection + parent->getAbsDir2D();; |
|---|
| 634 | } |
|---|
| 635 | |
|---|
| 636 | |
|---|
| 637 | if (this->alignment == E2D_ALIGN_SCREEN_CENTER && this->bRelCoorChanged) |
|---|
| 638 | { |
|---|
| 639 | this->prevRelCoordinate = this->relCoordinate; |
|---|
| 640 | this->absCoordinate.x = .5 + this->relCoordinate.x; |
|---|
| 641 | this->absCoordinate.y = .5 + this->relCoordinate.y; |
|---|
| 642 | this->absCoordinate.z = 0.0; |
|---|
| 643 | } |
|---|
| 644 | else if (this->bindNode) |
|---|
| 645 | { |
|---|
| 646 | GLdouble projectPos[3]; |
|---|
| 647 | gluProject(this->bindNode->getAbsCoor().x, |
|---|
| 648 | this->bindNode->getAbsCoor().y, |
|---|
| 649 | this->bindNode->getAbsCoor().z, |
|---|
| 650 | GraphicsEngine::modMat, |
|---|
| 651 | GraphicsEngine::projMat, |
|---|
| 652 | GraphicsEngine::viewPort, |
|---|
| 653 | projectPos, |
|---|
| 654 | projectPos+1, |
|---|
| 655 | projectPos+2); |
|---|
| 656 | this->absCoordinate.x = projectPos[0]/(float)GraphicsEngine::getInstance()->getResolutionX() + this->relCoordinate.x; |
|---|
| 657 | this->absCoordinate.y = projectPos[1]/(float)GraphicsEngine::getInstance()->getResolutionY() + this->relCoordinate.y; |
|---|
| 658 | this->absCoordinate.z = projectPos[2] + this->relCoordinate.z; |
|---|
| 659 | } |
|---|
| 660 | else |
|---|
| 661 | { |
|---|
| 662 | if(likely(this->parentMode & PNODE_MOVEMENT && this->bRelCoorChanged)) |
|---|
| 663 | { |
|---|
| 664 | /* update the current absCoordinate */ |
|---|
| 665 | this->prevRelCoordinate = this->relCoordinate; |
|---|
| 666 | this->absCoordinate = this->parent->getAbsCoor2D() + this->relCoordinate; |
|---|
| 667 | } |
|---|
| 668 | else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged) |
|---|
| 669 | { |
|---|
| 670 | /* update the current absCoordinate */ |
|---|
| 671 | this->prevRelCoordinate = this->relCoordinate; |
|---|
| 672 | float sine = sin(this->parent->getAbsDir2D()); |
|---|
| 673 | float cose = cos(this->parent->getAbsDir2D()); |
|---|
| 674 | // this->absCoordinate.x = this->relCoordinate.x*cose - this->relCoordinate.y*sine + this->parent->getRelCoor2D().x*(1-cose) +this->parent->getRelCoor2D().y*sine; |
|---|
| 675 | // this->absCoordinate.y = this->relCoordinate.x*sine + this->relCoordinate.y*cose + this->parent->getRelCoor2D().y*(1-cose) +this->parent->getRelCoor2D().x*sine; |
|---|
| 676 | |
|---|
| 677 | this->absCoordinate.x = this->parent->getAbsCoor2D().x + (this->relCoordinate.x*cos(this->parent->getAbsDir2D()) - this->relCoordinate.y * sin(this->parent->getAbsDir2D())); |
|---|
| 678 | this->absCoordinate.y = this->parent->getAbsCoor2D().y + (this->relCoordinate.x*sin(this->parent->getAbsDir2D()) + this->relCoordinate.y * cos(this->parent->getAbsDir2D())); |
|---|
| 679 | |
|---|
| 680 | } |
|---|
| 681 | } |
|---|
| 682 | ///////////////////////////////////////////////// |
|---|
| 683 | } |
|---|
| 684 | else |
|---|
| 685 | { |
|---|
| 686 | PRINTF(5)("Element2D::update - (%f, %f, %f)\n", this->absCoordinate.x, this->absCoordinate.y, this->absCoordinate.z); |
|---|
| 687 | if (this->bRelCoorChanged) |
|---|
| 688 | { |
|---|
| 689 | this->prevRelCoordinate = this->relCoordinate; |
|---|
| 690 | this->absCoordinate = this->relCoordinate; |
|---|
| 691 | } |
|---|
| 692 | if (this->bRelDirChanged) |
|---|
| 693 | { |
|---|
| 694 | this->prevRelDirection = this->relDirection; |
|---|
| 695 | this->absDirection = this->getAbsDir2D() * this->relDirection; |
|---|
| 696 | } |
|---|
| 697 | } |
|---|
| 698 | |
|---|
| 699 | |
|---|
| 700 | // UPDATE CHILDREN |
|---|
| 701 | if(this->children->getSize() > 0) |
|---|
| 702 | { |
|---|
| 703 | tIterator<Element2D>* iterator = this->children->getIterator(); |
|---|
| 704 | Element2D* pn = iterator->firstElement(); |
|---|
| 705 | while( pn != NULL) |
|---|
| 706 | { |
|---|
| 707 | /* if this node has changed, make sure, that all children are updated also */ |
|---|
| 708 | if( likely(this->bRelCoorChanged)) |
|---|
| 709 | pn->parentCoorChanged (); |
|---|
| 710 | if( likely(this->bRelDirChanged)) |
|---|
| 711 | pn->parentDirChanged (); |
|---|
| 712 | |
|---|
| 713 | pn->update2D(dt); |
|---|
| 714 | pn = iterator->nextElement(); |
|---|
| 715 | } |
|---|
| 716 | delete iterator; |
|---|
| 717 | } |
|---|
| 718 | |
|---|
| 719 | // FINISHING PROCESS |
|---|
| 720 | this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt; |
|---|
| 721 | this->bRelCoorChanged = false; |
|---|
| 722 | this->bRelDirChanged = false; |
|---|
| 723 | } |
|---|
| 724 | |
|---|
| 725 | /** |
|---|
| 726 | * displays some information about this pNode |
|---|
| 727 | * @param depth The deph into which to debug the children of this Element2D to. |
|---|
| 728 | * (0: all children will be debugged, 1: only this Element2D, 2: this and direct children...) |
|---|
| 729 | * @param level The n-th level of the Node we draw (this is internal and only for nice output) |
|---|
| 730 | */ |
|---|
| 731 | void Element2D::debug (unsigned int depth, unsigned int level) const |
|---|
| 732 | { |
|---|
| 733 | for (unsigned int i = 0; i < level; i++) |
|---|
| 734 | PRINT(0)(" |"); |
|---|
| 735 | if (this->children->getSize() > 0) |
|---|
| 736 | PRINT(0)(" +"); |
|---|
| 737 | else |
|---|
| 738 | PRINT(0)(" -"); |
|---|
| 739 | PRINT(0)("Element2D(%s::%s) - absCoord: (%0.2f, %0.2f), relCoord(%0.2f, %0.2f), direction(%0.2f) - %s\n", |
|---|
| 740 | this->getClassName(), |
|---|
| 741 | this->getName(), |
|---|
| 742 | this->absCoordinate.x, |
|---|
| 743 | this->absCoordinate.y, |
|---|
| 744 | this->relCoordinate.x, |
|---|
| 745 | this->relCoordinate.y, |
|---|
| 746 | this->getAbsDir2D(), |
|---|
| 747 | Element2D::parentingModeToChar2D(parentMode)); |
|---|
| 748 | if (depth >= 2 || depth == 0) |
|---|
| 749 | { |
|---|
| 750 | tIterator<Element2D>* iterator = this->children->getIterator(); |
|---|
| 751 | Element2D* pn = iterator->firstElement(); |
|---|
| 752 | while( pn != NULL) |
|---|
| 753 | { |
|---|
| 754 | if (depth == 0) |
|---|
| 755 | pn->debug(0, level + 1); |
|---|
| 756 | else |
|---|
| 757 | pn->debug(depth - 1, level +1); |
|---|
| 758 | pn = iterator->nextElement(); |
|---|
| 759 | } |
|---|
| 760 | delete iterator; |
|---|
| 761 | } |
|---|
| 762 | } |
|---|
| 763 | |
|---|
| 764 | #include "color.h" |
|---|
| 765 | |
|---|
| 766 | /** |
|---|
| 767 | * displays the Element2D at its position with its rotation as a Plane. |
|---|
| 768 | */ |
|---|
| 769 | void Element2D::debugDraw2D(unsigned int depth, float size, Vector color) const |
|---|
| 770 | { |
|---|
| 771 | |
|---|
| 772 | } |
|---|
| 773 | |
|---|
| 774 | |
|---|
| 775 | // helper functions // |
|---|
| 776 | /** |
|---|
| 777 | * converts a parentingMode into a string that is the name of it |
|---|
| 778 | * @param parentingMode the ParentingMode to convert |
|---|
| 779 | * @return the converted string |
|---|
| 780 | */ |
|---|
| 781 | const char* Element2D::parentingModeToChar2D(int parentingMode) |
|---|
| 782 | { |
|---|
| 783 | if (parentingMode == E2D_PARENT_LOCAL_ROTATE) |
|---|
| 784 | return "local-rotate"; |
|---|
| 785 | else if (parentingMode == E2D_PARENT_ROTATE_MOVEMENT) |
|---|
| 786 | return "rotate-movement"; |
|---|
| 787 | else if (parentingMode == E2D_PARENT_MOVEMENT) |
|---|
| 788 | return "movement"; |
|---|
| 789 | else if (parentingMode == E2D_PARENT_ALL) |
|---|
| 790 | return "all"; |
|---|
| 791 | else if (parentingMode == E2D_PARENT_ROTATE_AND_MOVE) |
|---|
| 792 | return "rotate-and-move"; |
|---|
| 793 | } |
|---|
| 794 | |
|---|
| 795 | /** |
|---|
| 796 | * converts a parenting-mode-string into a int |
|---|
| 797 | * @param parentingMode the string naming the parentingMode |
|---|
| 798 | * @return the int corresponding to the named parentingMode |
|---|
| 799 | */ |
|---|
| 800 | E2D_PARENT_MODE Element2D::charToParentingMode2D(const char* parentingMode) |
|---|
| 801 | { |
|---|
| 802 | if (!strcmp(parentingMode, "local-rotate")) |
|---|
| 803 | return (E2D_PARENT_LOCAL_ROTATE); |
|---|
| 804 | else if (!strcmp(parentingMode, "rotate-movement")) |
|---|
| 805 | return (E2D_PARENT_ROTATE_MOVEMENT); |
|---|
| 806 | else if (!strcmp(parentingMode, "movement")) |
|---|
| 807 | return (E2D_PARENT_MOVEMENT); |
|---|
| 808 | else if (!strcmp(parentingMode, "all")) |
|---|
| 809 | return (E2D_PARENT_ALL); |
|---|
| 810 | else if (!strcmp(parentingMode, "rotate-and-move")) |
|---|
| 811 | return (E2D_PARENT_ROTATE_AND_MOVE); |
|---|
| 812 | } |
|---|
| 813 | |
|---|
| 814 | |
|---|
| 815 | |
|---|
| 816 | |
|---|
| 817 | |
|---|
| 818 | |
|---|
| 819 | /** |
|---|
| 820 | * ticks the 2d-Element |
|---|
| 821 | * @param dt the time elapsed since the last tick |
|---|
| 822 | */ |
|---|
| 823 | void Element2D::tick(float dt) |
|---|
| 824 | { |
|---|
| 825 | |
|---|
| 826 | } |
|---|
| 827 | |
|---|
| 828 | |
|---|
| 829 | |
|---|
| 830 | |
|---|
| 831 | |
|---|
| 832 | |
|---|
| 833 | |
|---|
| 834 | NullElement2D* NullElement2D::singletonRef = 0; |
|---|
| 835 | |
|---|
| 836 | /** |
|---|
| 837 | * creates the one and only NullElement2D |
|---|
| 838 | * @param absCoordinate the cordinate of the Parent (normally Vector(0,0,0)) |
|---|
| 839 | */ |
|---|
| 840 | NullElement2D::NullElement2D () : Element2D(NULL) |
|---|
| 841 | { |
|---|
| 842 | this->setClassID(CL_NULL_ELEMENT_2D, "NullElement2D"); |
|---|
| 843 | this->setName("NullElement2D"); |
|---|
| 844 | |
|---|
| 845 | this->setParentMode2D(E2D_PARENT_ALL); |
|---|
| 846 | NullElement2D::singletonRef = this; |
|---|
| 847 | } |
|---|
| 848 | |
|---|
| 849 | |
|---|
| 850 | /** |
|---|
| 851 | * standard deconstructor |
|---|
| 852 | */ |
|---|
| 853 | NullElement2D::~NullElement2D () |
|---|
| 854 | { |
|---|
| 855 | NullElement2D::singletonRef = NULL; |
|---|
| 856 | } |
|---|