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