Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5089 in orxonox.OLD for trunk/src/lib/graphics/render2D


Ignore:
Timestamp:
Aug 21, 2005, 8:17:31 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: rebuild Element2D. Now it is more like PNode

Location:
trunk/src/lib/graphics/render2D
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/graphics/render2D/element_2d.cc

    r5088 r5089  
    2828using namespace std;
    2929
     30Element2D::Element2D()
     31{
     32  this->init();
     33  this->setParent2D(NullElement2D::getInstance());
     34}
    3035
    3136/**
     
    3540Element2D::Element2D (Element2D* parent)
    3641{
    37   this->init(parent);
     42  this->init();
     43
     44  if (this->parent != NULL)
     45    this->setParent2D(parent);
    3846}
    3947
     
    4553  // delete what has to be deleted here
    4654  Render2D::getInstance()->unregisterElement2D(this);
     55
     56  tIterator<Element2D>* iterator = this->children->getIterator();
     57  Element2D* pn = iterator->nextElement();
     58  while( pn != NULL)
     59  {
     60    delete pn;
     61    pn = iterator->nextElement();
     62  }
     63  delete iterator;
     64  /* this deletes all children in the list */
     65  delete this->children;
     66  if (this->parent)
     67    this->parent->removeChild2D(this);
    4768
    4869  if (this->toCoordinate != NULL)
     
    5677 * initializes a Element2D
    5778 */
    58 void Element2D::init(Element2D* parent)
     79void Element2D::init()
    5980{
    6081  this->setClassID(CL_ELEMENT_2D, "Element2D");
     
    6283  this->setVisibility(true);
    6384  this->setActiveness(true);
    64   this->setPosition2D(0,0);
    6585  this->setAlignment(E2D_ALIGN_NONE);
    6686  this->layer = E2D_TOP;
     87  this->bindNode = NULL;
    6788
    6889  this->setParentMode2D(E2D_PARENT_ALL);
     90  this->parent = NULL;
    6991  this->children = new tList<Element2D>;
     92  this->relDirection = 0.0;
    7093  this->bRelCoorChanged = true;
    7194  this->bRelDirChanged = true;
    7295  this->toCoordinate = NULL;
    7396  this->toDirection = NULL;
    74   if (parent == NULL)
    75     this->parent = NULL;
    7697//  else
    7798  //  this->setParent2D(parent);
     
    95116      .describe("sets a node, this 2D-Element should be shown upon (name of the node)");
    96117
    97   LoadParam<Element2D>(root, "2d-position", this, &Element2D::setPosition2D)
    98       .describe("the _relative_ position (away from alignment) this 2d-element shows");
    99 
    100118  LoadParam<Element2D>(root, "visibility", this, &Element2D::setVisibility)
    101119      .describe("if the Element is visible or not");
     120
     121//   LoadParam<Element2D>(root, "2d-position", this, &Element2D::setPosition2D)
     122//       .describe("the _relative_ position (away from alignment) this 2d-element shows");
     123
    102124}
    103125
     
    157179}
    158180
    159 /**
    160  * this sets the position of the Element on the screen.
    161  * Use this in the your tick function, if you want the element to be automatically positioned.
    162  *
    163  * @todo must be in update
    164  */
    165 void Element2D::positioning()
    166 {
    167   // setting the Position of this 2D-Element.
    168   if (this->alignment == E2D_ALIGN_SCREEN_CENTER)
    169   {
    170     absPos2D.x = GraphicsEngine::getInstance()->getResolutionX()/2 + this->relPos2D[0];
    171     absPos2D.y = GraphicsEngine::getInstance()->getResolutionY()/2 + this->relPos2D[1];
    172     absPos2D.depth = 0;
    173   }
    174   else if (this->bindNode)
    175   {
    176     GLdouble projectPos[3];
    177     gluProject(this->bindNode->getAbsCoor().x,
    178                this->bindNode->getAbsCoor().y,
    179                this->bindNode->getAbsCoor().z,
    180                GraphicsEngine::modMat,
    181                GraphicsEngine::projMat,
    182                GraphicsEngine::viewPort,
    183                projectPos,
    184                projectPos+1,
    185                projectPos+2);
    186     absPos2D.x = projectPos[0] + this->relPos2D[0];
    187     absPos2D.y = GraphicsEngine::getInstance()->getResolutionY() - projectPos[1] + this->relPos2D[1];
    188     absPos2D.depth = projectPos[2];
    189   }
    190   else
    191   {
    192     absPos2D.x = this->relPos2D[0];
    193     absPos2D.y = this->relPos2D[1];
    194     absPos2D.depth = 0;
    195   }
    196 }
    197 
    198181
    199182void Element2D::setRelCoor2D (const Vector& relCoord)
     
    209192}
    210193
     194void Element2D::setRelCoor2Dpx (int x, int y)
     195{
     196  this->setRelCoor2D(Vector((float)GraphicsEngine::getInstance()->getResolutionX()/(float)x,
     197                     (float)GraphicsEngine::getInstance()->getResolutionY()/(float)y,
     198                     0
     199                           ));
     200}
     201
     202
    211203void Element2D::setRelCoorSoft2D(const Vector& relCoordSoft, float bias)
    212204{
     
    217209  this->bias = bias;
    218210}
     211
     212void Element2D::setRelCoorSoft2Dpx (int x, int y, float bias)
     213{
     214  this->setRelCoorSoft2D(Vector((float)GraphicsEngine::getInstance()->getResolutionX()/(float)x,
     215                         (float)GraphicsEngine::getInstance()->getResolutionY()/(float)y,
     216                         0),
     217                         bias);
     218}
     219
    219220
    220221void Element2D::setRelCoorSoft2D(float x, float y, float depth, float bias)
     
    249250}
    250251
     252
     253void Element2D::setAbsCoor2Dpx (int x, int y)
     254{
     255  this->setAbsCoor2D(Vector((float)GraphicsEngine::getInstance()->getResolutionX()/(float)x,
     256                     (float)GraphicsEngine::getInstance()->getResolutionY()/(float)y,
     257                     0
     258                           ));
     259}
     260
    251261void Element2D::shiftCoor2D (const Vector& shift)
    252262{
     
    254264  this->bRelCoorChanged = true;
    255265
     266}
     267
     268
     269void Element2D::shiftCoor2Dpx (int x, int y)
     270{
     271  this->shiftCoor2D(Vector((float)GraphicsEngine::getInstance()->getResolutionX()/(float)x,
     272                     (float)GraphicsEngine::getInstance()->getResolutionY()/(float)y,
     273                     0));
    256274}
    257275
     
    392410void Element2D::update2D (float dt)
    393411{
     412  // setting the Position of this 2D-Element.
    394413  if( likely(this->parent != NULL))
    395414  {
     
    438457    }
    439458
    440     if(likely(this->parentMode & PNODE_MOVEMENT))
    441     {
    442       /* update the current absCoordinate */
     459
     460    if (this->alignment == E2D_ALIGN_SCREEN_CENTER && this->bRelCoorChanged)
     461    {
    443462      this->prevRelCoordinate = this->relCoordinate;
    444       this->absCoordinate = this->parent->getAbsCoor2D() + this->relCoordinate;
    445     }
    446     else if( this->parentMode & PNODE_ROTATE_MOVEMENT)
    447     {
    448       /* update the current absCoordinate */
    449       this->prevRelCoordinate = this->relCoordinate;
    450       this->absCoordinate.x = this->parent->getAbsCoor2D().x + (this->relCoordinate.x*cos(this->parent->getAbsDir2D()) - this->relCoordinate.y * sin(this->parent->getAbsDir2D()));
    451       this->absCoordinate.y = this->parent->getAbsCoor2D().y + (this->relCoordinate.x*sin(this->parent->getAbsDir2D()) + this->relCoordinate.y * cos(this->parent->getAbsDir2D()));
    452 
     463      this->absCoordinate.x = .5 + this->relCoordinate.x;
     464      this->absCoordinate.y = .5 + this->relCoordinate.y;
     465      this->absCoordinate.z = 0.0;
     466    }
     467    else if (this->bindNode)
     468    {
     469      GLdouble projectPos[3];
     470      gluProject(this->bindNode->getAbsCoor().x,
     471                 this->bindNode->getAbsCoor().y,
     472                 this->bindNode->getAbsCoor().z,
     473                 GraphicsEngine::modMat,
     474                 GraphicsEngine::projMat,
     475                 GraphicsEngine::viewPort,
     476                 projectPos,
     477                 projectPos+1,
     478                 projectPos+2);
     479      this->absCoordinate.x = projectPos[0]/(float)GraphicsEngine::getInstance()->getResolutionX() + this->relCoordinate.x;
     480      this->absCoordinate.y = projectPos[1]/(float)GraphicsEngine::getInstance()->getResolutionY() + this->relCoordinate.y;
     481      this->absCoordinate.z = projectPos[2] + this->relCoordinate.z;
     482    }
     483    else
     484    {
     485      if(likely(this->parentMode & PNODE_MOVEMENT && this->bRelCoorChanged))
     486      {
     487        /* update the current absCoordinate */
     488        this->prevRelCoordinate = this->relCoordinate;
     489        this->absCoordinate = this->parent->getAbsCoor2D() + this->relCoordinate;
     490      }
     491      else if( this->parentMode & PNODE_ROTATE_MOVEMENT && this->bRelCoorChanged)
     492      {
     493        /* update the current absCoordinate */
     494        this->prevRelCoordinate = this->relCoordinate;
     495        this->absCoordinate.x = this->parent->getAbsCoor2D().x + (this->relCoordinate.x*cos(this->parent->getAbsDir2D()) - this->relCoordinate.y * sin(this->parent->getAbsDir2D()));
     496        this->absCoordinate.y = this->parent->getAbsCoor2D().y + (this->relCoordinate.x*sin(this->parent->getAbsDir2D()) + this->relCoordinate.y * cos(this->parent->getAbsDir2D()));
     497
     498      }
    453499    }
    454500    /////////////////////////////////////////////////
     
    463509  }
    464510
     511
     512  // UPDATE CHILDREN
    465513  if(this->children->getSize() > 0)
    466514  {
     
    481529    delete iterator;
    482530  }
     531
     532  // FINISHING PROCESS
    483533  this->velocity = (this->absCoordinate - this->lastAbsCoordinate) / dt;
    484534  this->bRelCoorChanged = false;
     
    569619void Element2D::tick(float dt)
    570620{
    571   this->positioning();
     621
    572622}
    573623
     
    584634 * @param absCoordinate the cordinate of the Parent (normally Vector(0,0,0))
    585635 */
    586 NullElement2D::NullElement2D ()
     636NullElement2D::NullElement2D () : Element2D(NULL)
    587637{
    588638  this->setClassID(CL_NULL_PARENT, "NullElement2D");
  • trunk/src/lib/graphics/render2D/element_2d.h

    r5084 r5089  
    1010
    1111#include "base_object.h"
     12
    1213#include "vector.h"
    1314
     
    6364
    6465  public:
    65     Element2D(Element2D* parent = NULL);
     66    Element2D();
     67    Element2D(Element2D* parent);
    6668    virtual ~Element2D();
    6769
    68     void init(Element2D* parent = NULL);
     70    void init();
    6971    void loadParams(const TiXmlElement* root);
    70 
    71     /** @param xCoord the xCoordinate @param yCoord the y-Coordinate. These coordinates are Relative */
    72     inline void setPosition2D(int xCoord, int yCoord) { this->relPos2D[0] = xCoord; this->relPos2D[1] = yCoord; };
    73     /** this returns the Absolute Position on the screen set by positioning in the tick-phase */
    74     inline const Position2D& getPosition2D() { return this->absPos2D; };
    7572
    7673    /** @param alignment the new Alignment of the 2D-Element */
    7774    inline void setAlignment(E2D_ALIGNMENT alignment) { this->alignment = alignment; };
    7875    void setAlignment(const char* alignment);
     76    inline E2D_ALIGNMENT getAlignment() const { return this->alignment; };
    7977
    8078    void setLayer(E2D_LAYER layer);
     
    9290    inline void setBindNode(const PNode* bindNode) { this->bindNode = bindNode; };
    9391    void setBindNode(const char* bindNode);
     92    inline const PNode* getBindNode() const { return this->bindNode; };
    9493
    9594    /** @returns the visibility state */
     
    102101  public:
    103102    void setRelCoor2D (const Vector& relCoord);
    104     void setRelCoor2D (float x, float y, float dontCare);
    105     void setRelCoorSoft2D(const Vector& relCoordSoft, float bias = 1.0);
    106     void setRelCoorSoft2D(float x, float y, float dontCare, float bias = 1.0);
     103    void setRelCoor2D (float x, float y, float dontCare = 1.0);
     104    void setRelCoor2Dpx (int x, int y);
     105    void setRelCoorSoft2D (const Vector& relCoordSoft, float bias = 1.0);
     106    void setRelCoorSoft2D (float x, float y, float dontCare = 1.0, float bias = 1.0);
     107    void setRelCoorSoft2Dpx (int x, int y, float bias = 1.0);
    107108    /** @returns the relative position */
    108109    inline const Vector& getRelCoor2D () const { return this->prevRelCoordinate; };
     110    const Vector& getRelCoor2Dpx() const;
    109111    void setAbsCoor2D (const Vector& absCoord);
    110     void setAbsCoor2D (float x, float y, float depth);
     112    void setAbsCoor2D (float x, float y, float depth = 1.0);
     113    void setAbsCoor2Dpx (int x, int y);
    111114    /** @returns the absolute position */
    112115    inline const Vector& getAbsCoor2D () const { return this->absCoordinate; };
     116    const Vector& getAbsCoor2Dpx () const;
     117
    113118    void shiftCoor2D (const Vector& shift);
     119    void shiftCoor2Dpx (int x, int y);
    114120
    115121    void setRelDir2D (float relDir);
     
    168174    virtual void draw() const = NULL;
    169175
    170   protected:
    171     void positioning();
    172     //void Element2D(NullElement2D* nullElem);
    173 
    174   protected:
    175     const PNode*            bindNode;         //!< a node over which to display this 2D-element
    176     int                     relPos2D[2];      //!< X-coord, Y-Coord (relative to the Coordinates of the alignment if given.)
    177     Position2D              absPos2D;         //!< The absolute position of the 2D-Element.
    178 
    179     E2D_ALIGNMENT           alignment;        //!< How the Element is aligned around its Position
    180 
    181   private:
    182     bool                    visible;          //!< If the given Element2D is visible.
    183     bool                    active;           //!< If the given Element2D is active.
    184     E2D_LAYER               layer;            //!< What layer this Element2D is on.
    185 
    186 
    187   private:
    188     bool              bRelCoorChanged;    //!< If Relative Coordinate has changed since last time we checked
    189     bool              bRelDirChanged;     //!< If Relative Direction has changed since last time we checked
    190 
    191     Vector            relCoordinate;      //!< coordinates relative to the parent
    192     Vector            absCoordinate;      //!< absolute coordinates in the world ( from (0,0,0) )
    193     float             relDirection;       //!< direction relative to the parent
    194     float             absDirection;       //!< absolute direvtion in the world ( from (0,0,1) )
    195 
    196     Vector            prevRelCoordinate;  //!< The last Relative Coordinate from the last update-Cycle.
    197     Vector            lastAbsCoordinate;  //!< this is used for speedcalculation, it stores the last coordinate
    198     float             prevRelDirection;   //!< The last Relative Direciton from the last update-Cycle.
    199 //  Quaternion        lastAbsDirection;
    200 
    201     Vector            velocity;           //!< Saves the velocity.
    202 
    203     Vector*           toCoordinate;       //!< a position to which to iterate. (This is used in conjunction with softReparent.and set*CoorSoft)
    204     float*            toDirection;        //!< a direction to which to iterate. (This is used in conjunction with softReparent and set*DirSoft)
    205     float             bias;               //!< how fast to iterate to the given position (default is 1)
    206 
    207     Element2D*        parent;             //!< a pointer to the parent node
    208     tList<Element2D>* children;           //!< list of the children of this Element2D
    209 
    210     unsigned int      parentMode;         //!< the mode of the binding
     176  private:
     177    const PNode*            bindNode;           //!< a node over which to display this 2D-element
     178
     179    E2D_ALIGNMENT           alignment;          //!< How the Element is aligned around its Position
     180
     181    bool                    visible;            //!< If the given Element2D is visible.
     182    bool                    active;             //!< If the given Element2D is active.
     183    E2D_LAYER               layer;              //!< What layer this Element2D is on.
     184
     185    bool                    bRelCoorChanged;    //!< If Relative Coordinate has changed since last time we checked
     186    bool                    bRelDirChanged;     //!< If Relative Direction has changed since last time we checked
     187
     188    Vector                  relCoordinate;      //!< coordinates relative to the parent
     189    Vector                  absCoordinate;      //!< absolute coordinates in the world ( from (0,0,0) )
     190    float                   relDirection;       //!< direction relative to the parent
     191    float                   absDirection;       //!< absolute direvtion in the world ( from (0,0,1) )
     192
     193    Vector                  prevRelCoordinate;  //!< The last Relative Coordinate from the last update-Cycle.
     194    Vector                  lastAbsCoordinate;  //!< this is used for speedcalculation, it stores the last coordinate
     195    float                   prevRelDirection;   //!< The last Relative Direciton from the last update-Cycle.
     196//  float                   lastAbsDirection;
     197
     198    Vector                  velocity;           //!< Saves the velocity.
     199
     200    Vector*                 toCoordinate;       //!< a position to which to iterate. (This is used in conjunction with softReparent.and set*CoorSoft)
     201    float*                  toDirection;        //!< a direction to which to iterate. (This is used in conjunction with softReparent and set*DirSoft)
     202    float                   bias;               //!< how fast to iterate to the given position (default is 1)
     203
     204    Element2D*              parent;             //!< a pointer to the parent node
     205    tList<Element2D>*       children;           //!< list of the children of this Element2D
     206
     207    unsigned int            parentMode;         //!< the mode of the binding
    211208};
    212209
Note: See TracChangeset for help on using the changeset viewer.