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