[4838] | 1 | /*! |
---|
[4839] | 2 | * @file element_2d.h |
---|
[5081] | 3 | * Definition of the 2D elements rendered on top through the GraphicsEngine |
---|
[5084] | 4 | * |
---|
[5081] | 5 | * @todo reimplement it, so it looks just like PNode. |
---|
[3245] | 6 | */ |
---|
[1853] | 7 | |
---|
[4838] | 8 | #ifndef _ELEMENT_2D_H |
---|
| 9 | #define _ELEMENT_2D_H |
---|
[1853] | 10 | |
---|
[3543] | 11 | #include "base_object.h" |
---|
[5089] | 12 | |
---|
[5081] | 13 | #include "vector.h" |
---|
[1853] | 14 | |
---|
[4838] | 15 | // FORWARD DECLARATION |
---|
[4843] | 16 | class PNode; |
---|
[4858] | 17 | class TiXmlElement; |
---|
[5081] | 18 | template<class T> class tList; |
---|
[3543] | 19 | |
---|
[4839] | 20 | //!< An enumerator defining the Depth of a 2D-element. |
---|
| 21 | typedef enum |
---|
| 22 | { |
---|
[4862] | 23 | E2D_BELOW_ALL = 1, //!< Will be rendered below the 3D-scene. @todo make this work. |
---|
| 24 | E2D_BOTTOM = 2, //!< Will be rendered on the bottom Layer |
---|
| 25 | E2D_MEDIUM = 4, //!< Will be rendered on the medium Layer. |
---|
| 26 | E2D_TOP = 8, //!< Will be rendered on top of everything else |
---|
[4848] | 27 | |
---|
[4862] | 28 | E2D_LAYER_COUNT = 4 //!< The count of Layers. |
---|
| 29 | } E2D_LAYER; |
---|
| 30 | #define E2D_DEFAULT_LAYER E2D_TOP |
---|
| 31 | #define E2D_ALL_LAYERS E2D_TOP | E2D_MEDIUM | E2D_BOTTOM | E2D_BELOW_ALL |
---|
[4839] | 32 | |
---|
[4843] | 33 | typedef enum |
---|
| 34 | { |
---|
[4856] | 35 | E2D_ALIGN_NONE = 0, |
---|
| 36 | E2D_ALIGN_LEFT = 1, |
---|
| 37 | E2D_ALIGN_RIGHT = 2, |
---|
| 38 | E2D_ALIGN_CENTER = 4, |
---|
| 39 | E2D_ALIGN_SCREEN_CENTER = 8 |
---|
[4848] | 40 | } E2D_ALIGNMENT; |
---|
[4843] | 41 | |
---|
[5081] | 42 | typedef enum |
---|
| 43 | { |
---|
[5215] | 44 | E2D_PARENT_NONE = 0, |
---|
[5082] | 45 | E2D_PARENT_LOCAL_ROTATE = 1, //!< Rotates all the children around their centers. |
---|
| 46 | E2D_PARENT_ROTATE_MOVEMENT = 2, //!< Moves all the children around the center of their parent, without the rotation around their own centers. |
---|
[5081] | 47 | |
---|
[5082] | 48 | E2D_PARENT_MOVEMENT = 4, //!< Moves all children along with the parent. |
---|
[5081] | 49 | // special linkage modes |
---|
[5082] | 50 | E2D_PARENT_ALL = 3, //!< Moves all children around the center of their parent, and also rotates their centers |
---|
| 51 | E2D_PARENT_ROTATE_AND_MOVE = 5 //!< Rotates all children around their axis, and moves them as the Parent Moves, but does not rotate around the center of their parent. |
---|
[5081] | 52 | } E2D_PARENT_MODE; |
---|
[5082] | 53 | #define E2D_DEFAULT_PARENTING_MODE E2D_PARENT_ALL |
---|
[5081] | 54 | |
---|
[4847] | 55 | //! A Struct defining the Position of an Element in 2D-space |
---|
| 56 | struct Position2D |
---|
| 57 | { |
---|
| 58 | float x; //!< The x-coordinate. |
---|
| 59 | float y; //!< The y-coordinate. |
---|
| 60 | float depth; //!< The distance from the viewing plane. |
---|
| 61 | }; |
---|
| 62 | |
---|
[3955] | 63 | //! A class for ... |
---|
[4849] | 64 | class Element2D : virtual public BaseObject { |
---|
[1853] | 65 | |
---|
[4850] | 66 | public: |
---|
[5089] | 67 | Element2D(); |
---|
| 68 | Element2D(Element2D* parent); |
---|
[4850] | 69 | virtual ~Element2D(); |
---|
[1853] | 70 | |
---|
[5089] | 71 | void init(); |
---|
[4858] | 72 | void loadParams(const TiXmlElement* root); |
---|
| 73 | |
---|
[4856] | 74 | /** @param alignment the new Alignment of the 2D-Element */ |
---|
| 75 | inline void setAlignment(E2D_ALIGNMENT alignment) { this->alignment = alignment; }; |
---|
[4858] | 76 | void setAlignment(const char* alignment); |
---|
[5089] | 77 | inline E2D_ALIGNMENT getAlignment() const { return this->alignment; }; |
---|
[4862] | 78 | |
---|
| 79 | void setLayer(E2D_LAYER layer); |
---|
[4858] | 80 | void setLayer(const char* layer); |
---|
[4862] | 81 | /** @returns the Layer this Element is drawn to */ |
---|
| 82 | inline E2D_LAYER getLayer() { return this->layer; }; |
---|
| 83 | |
---|
[4850] | 84 | /** @param visible true if the Element should be visible false otherwise (will not be rendered) */ |
---|
| 85 | inline void setVisibility(bool visible) { this->visible = visible; }; |
---|
[5068] | 86 | /** @param active true if the Element should be active, false otherwise (will not be ticked) */ |
---|
| 87 | inline void setActiveness(bool active) { this->active = active; }; |
---|
[4862] | 88 | |
---|
[5068] | 89 | |
---|
[4850] | 90 | /** @param bindNode the Node this 2D-element should follow. if NULL the Element will not follow anything */ |
---|
[4856] | 91 | inline void setBindNode(const PNode* bindNode) { this->bindNode = bindNode; }; |
---|
[4858] | 92 | void setBindNode(const char* bindNode); |
---|
[5089] | 93 | inline const PNode* getBindNode() const { return this->bindNode; }; |
---|
[4840] | 94 | |
---|
[4850] | 95 | /** @returns the visibility state */ |
---|
| 96 | inline bool isVisible() { return this->visible; }; |
---|
[5068] | 97 | /** @returns the active-State */ |
---|
| 98 | inline bool isActive() { return this->active; }; |
---|
[4840] | 99 | |
---|
[5081] | 100 | |
---|
[5378] | 101 | inline void setSize2D(float x, float y) { this->sizeX = x, this->sizeY = y; }; |
---|
| 102 | inline void setSizeX2D(float x) { this->sizeX = x; }; |
---|
| 103 | inline void setSizeY2D(float y) { this->sizeY = y; }; |
---|
| 104 | inline float getSizeX2D() const { return this->sizeX; }; |
---|
| 105 | inline float getSizeY2D() const { return this->sizeY; }; |
---|
| 106 | |
---|
[5081] | 107 | // LIKE PNODE |
---|
| 108 | public: |
---|
| 109 | void setRelCoor2D (const Vector& relCoord); |
---|
[5089] | 110 | void setRelCoor2D (float x, float y, float dontCare = 1.0); |
---|
| 111 | void setRelCoor2Dpx (int x, int y); |
---|
| 112 | void setRelCoorSoft2D (const Vector& relCoordSoft, float bias = 1.0); |
---|
| 113 | void setRelCoorSoft2D (float x, float y, float dontCare = 1.0, float bias = 1.0); |
---|
| 114 | void setRelCoorSoft2Dpx (int x, int y, float bias = 1.0); |
---|
[5081] | 115 | /** @returns the relative position */ |
---|
| 116 | inline const Vector& getRelCoor2D () const { return this->prevRelCoordinate; }; |
---|
[5113] | 117 | /** @returns the Relative Coordinate Destination */ |
---|
| 118 | inline const Vector& getRelCoorSoft2D() const { return (this->toCoordinate)?*this->toCoordinate:this->relCoordinate; }; |
---|
[5089] | 119 | const Vector& getRelCoor2Dpx() const; |
---|
[5081] | 120 | void setAbsCoor2D (const Vector& absCoord); |
---|
[5089] | 121 | void setAbsCoor2D (float x, float y, float depth = 1.0); |
---|
| 122 | void setAbsCoor2Dpx (int x, int y); |
---|
[5081] | 123 | /** @returns the absolute position */ |
---|
| 124 | inline const Vector& getAbsCoor2D () const { return this->absCoordinate; }; |
---|
[5089] | 125 | const Vector& getAbsCoor2Dpx () const; |
---|
| 126 | |
---|
[5083] | 127 | void shiftCoor2D (const Vector& shift); |
---|
[5089] | 128 | void shiftCoor2Dpx (int x, int y); |
---|
[5081] | 129 | |
---|
| 130 | void setRelDir2D (float relDir); |
---|
| 131 | void setRelDirSoft2D(float relDirSoft, float bias = 1.0); |
---|
| 132 | /** @returns the relative Direction */ |
---|
| 133 | inline float getRelDir2D () const { return this->prevRelDirection; }; |
---|
[5113] | 134 | /** @returns the Relative Directional Destination */ |
---|
| 135 | inline float getRelDirSoft2D() const { return (this->toDirection)?*this->toDirection:this->relDirection; }; |
---|
[5081] | 136 | void setAbsDir2D (float absDir); |
---|
| 137 | /** @returns the absolute Direction */ |
---|
| 138 | inline float getAbsDir2D () const { return this->absDirection; }; |
---|
[5083] | 139 | void shiftDir2D (float shiftDir); |
---|
[5081] | 140 | |
---|
| 141 | /** @returns the Speed of the Node */ |
---|
| 142 | inline float getSpeed() const { return 0; }; |
---|
| 143 | /** @returns the Velocity of the Node */ |
---|
| 144 | inline const Vector& getVelocity() const { return this->velocity; }; |
---|
| 145 | |
---|
| 146 | |
---|
[5215] | 147 | void addChild2D (Element2D* child, int parentingMode = E2D_PARENT_NONE); |
---|
[5081] | 148 | void addChild2D (const char* childName); |
---|
| 149 | void removeChild2D (Element2D* child); |
---|
| 150 | void remove2D(); |
---|
| 151 | |
---|
| 152 | void setParent2D (Element2D* parent); |
---|
| 153 | void setParent2D (const char* parentName); |
---|
| 154 | /** @returns the parent of this Element2D */ |
---|
[5387] | 155 | inline Element2D* getParent () const { return this->parent; }; |
---|
| 156 | /** @returns the List of Children of this Element2D */ |
---|
| 157 | inline const tList<Element2D>* getChildren2D() const { return this->children; }; |
---|
[5081] | 158 | |
---|
[5382] | 159 | void setParentSoft2D(Element2D* parentNode, float bias = 1.0); |
---|
| 160 | void setParentSoft2D(const char* parentName, float bias = 1.0); |
---|
[5081] | 161 | |
---|
| 162 | /** @param parentMode sets the parentingMode of this Node */ |
---|
| 163 | void setParentMode2D (E2D_PARENT_MODE parentMode) { this->parentMode = parentMode; }; |
---|
| 164 | void setParentMode2D (const char* parentingMode); |
---|
| 165 | /** @returns the Parenting mode of this node */ |
---|
| 166 | int getParentMode2D() const { return this->parentMode; }; |
---|
| 167 | |
---|
| 168 | void update2D (float dt); |
---|
| 169 | |
---|
| 170 | void debug (unsigned int depth = 1, unsigned int level = 0) const; |
---|
| 171 | void debugDraw2D(unsigned int depth = 1, float size = 1.0, Vector color = Vector(1,1,1)) const; |
---|
| 172 | |
---|
| 173 | // helper functions // |
---|
[5082] | 174 | static const char* parentingModeToChar2D(int parentingMode); |
---|
| 175 | static E2D_PARENT_MODE charToParentingMode2D(const char* parentingMode); |
---|
[5081] | 176 | |
---|
| 177 | private: |
---|
| 178 | /** tells the child that the parent's Coordinate has changed */ |
---|
| 179 | inline void parentCoorChanged () { this->bRelCoorChanged = true; } |
---|
| 180 | /** tells the child that the parent's Direction has changed */ |
---|
| 181 | inline void parentDirChanged () { this->bRelDirChanged = true; } |
---|
| 182 | /** @returns the last calculated coordinate */ |
---|
| 183 | inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; } |
---|
| 184 | |
---|
| 185 | public: |
---|
[4850] | 186 | virtual void tick(float dt); |
---|
[5279] | 187 | virtual void draw() const = 0; |
---|
[4840] | 188 | |
---|
[5378] | 189 | private: |
---|
| 190 | const PNode* bindNode; //!< a node over which to display this 2D-element |
---|
[5371] | 191 | float sizeX; //!< The size of the rendered item in x-direction |
---|
| 192 | float sizeY; //!< The size of the rendered Item in y-direction |
---|
| 193 | |
---|
[5089] | 194 | E2D_ALIGNMENT alignment; //!< How the Element is aligned around its Position |
---|
[4856] | 195 | |
---|
[5089] | 196 | bool visible; //!< If the given Element2D is visible. |
---|
| 197 | bool active; //!< If the given Element2D is active. |
---|
| 198 | E2D_LAYER layer; //!< What layer this Element2D is on. |
---|
[4856] | 199 | |
---|
[5089] | 200 | bool bRelCoorChanged; //!< If Relative Coordinate has changed since last time we checked |
---|
| 201 | bool bRelDirChanged; //!< If Relative Direction has changed since last time we checked |
---|
[5081] | 202 | |
---|
[5089] | 203 | Vector relCoordinate; //!< coordinates relative to the parent |
---|
| 204 | Vector absCoordinate; //!< absolute coordinates in the world ( from (0,0,0) ) |
---|
| 205 | float relDirection; //!< direction relative to the parent |
---|
[5211] | 206 | float absDirection; //!< absolute diretion in the world ( from (0,0,1) ) |
---|
[5081] | 207 | |
---|
[5089] | 208 | Vector prevRelCoordinate; //!< The last Relative Coordinate from the last update-Cycle. |
---|
| 209 | Vector lastAbsCoordinate; //!< this is used for speedcalculation, it stores the last coordinate |
---|
| 210 | float prevRelDirection; //!< The last Relative Direciton from the last update-Cycle. |
---|
[5081] | 211 | |
---|
[5089] | 212 | Vector velocity; //!< Saves the velocity. |
---|
[5081] | 213 | |
---|
[5382] | 214 | Vector* toCoordinate; //!< a position to which to iterate. (This is used in conjunction with setParentSoft.and set*CoorSoft) |
---|
| 215 | float* toDirection; //!< a direction to which to iterate. (This is used in conjunction with setParentSoft and set*DirSoft) |
---|
[5089] | 216 | float bias; //!< how fast to iterate to the given position (default is 1) |
---|
[5081] | 217 | |
---|
[5089] | 218 | Element2D* parent; //!< a pointer to the parent node |
---|
| 219 | tList<Element2D>* children; //!< list of the children of this Element2D |
---|
[5081] | 220 | |
---|
[5089] | 221 | unsigned int parentMode; //!< the mode of the binding |
---|
[1853] | 222 | }; |
---|
| 223 | |
---|
[5082] | 224 | //! The top joint of all Element2D's every Element2D is somehow connected to this one. |
---|
| 225 | class NullElement2D : public Element2D { |
---|
[4839] | 226 | |
---|
[5082] | 227 | public: |
---|
| 228 | /** @returns a Pointer to the only object of this Class */ |
---|
[5285] | 229 | inline static NullElement2D* getInstance() { if (!NullElement2D::singletonRef) NullElement2D::singletonRef = new NullElement2D(); return NullElement2D::singletonRef; }; |
---|
| 230 | inline static bool isInstanciated() { return (NullElement2D::singletonRef != NULL)?true:false; }; |
---|
[5082] | 231 | virtual ~NullElement2D (); |
---|
| 232 | |
---|
| 233 | private: |
---|
| 234 | NullElement2D (); |
---|
| 235 | virtual void draw() const {}; |
---|
| 236 | |
---|
| 237 | private: |
---|
| 238 | static NullElement2D* singletonRef; //!< A reference to the NullElement2D |
---|
| 239 | |
---|
| 240 | }; |
---|
| 241 | |
---|
[4838] | 242 | #endif /* _ELEMENT_2D_H */ |
---|