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