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