[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 | |
---|
| 101 | // LIKE PNODE |
---|
| 102 | public: |
---|
| 103 | void setRelCoor2D (const Vector& relCoord); |
---|
[5089] | 104 | void setRelCoor2D (float x, float y, float dontCare = 1.0); |
---|
| 105 | void setRelCoor2Dpx (int x, int y); |
---|
| 106 | void setRelCoorSoft2D (const Vector& relCoordSoft, float bias = 1.0); |
---|
| 107 | void setRelCoorSoft2D (float x, float y, float dontCare = 1.0, float bias = 1.0); |
---|
| 108 | void setRelCoorSoft2Dpx (int x, int y, float bias = 1.0); |
---|
[5081] | 109 | /** @returns the relative position */ |
---|
| 110 | inline const Vector& getRelCoor2D () const { return this->prevRelCoordinate; }; |
---|
[5113] | 111 | /** @returns the Relative Coordinate Destination */ |
---|
| 112 | inline const Vector& getRelCoorSoft2D() const { return (this->toCoordinate)?*this->toCoordinate:this->relCoordinate; }; |
---|
[5089] | 113 | const Vector& getRelCoor2Dpx() const; |
---|
[5081] | 114 | void setAbsCoor2D (const Vector& absCoord); |
---|
[5089] | 115 | void setAbsCoor2D (float x, float y, float depth = 1.0); |
---|
| 116 | void setAbsCoor2Dpx (int x, int y); |
---|
[5081] | 117 | /** @returns the absolute position */ |
---|
| 118 | inline const Vector& getAbsCoor2D () const { return this->absCoordinate; }; |
---|
[5089] | 119 | const Vector& getAbsCoor2Dpx () const; |
---|
| 120 | |
---|
[5083] | 121 | void shiftCoor2D (const Vector& shift); |
---|
[5089] | 122 | void shiftCoor2Dpx (int x, int y); |
---|
[5081] | 123 | |
---|
| 124 | void setRelDir2D (float relDir); |
---|
| 125 | void setRelDirSoft2D(float relDirSoft, float bias = 1.0); |
---|
| 126 | /** @returns the relative Direction */ |
---|
| 127 | inline float getRelDir2D () const { return this->prevRelDirection; }; |
---|
[5113] | 128 | /** @returns the Relative Directional Destination */ |
---|
| 129 | inline float getRelDirSoft2D() const { return (this->toDirection)?*this->toDirection:this->relDirection; }; |
---|
[5081] | 130 | void setAbsDir2D (float absDir); |
---|
| 131 | /** @returns the absolute Direction */ |
---|
| 132 | inline float getAbsDir2D () const { return this->absDirection; }; |
---|
[5083] | 133 | void shiftDir2D (float shiftDir); |
---|
[5081] | 134 | |
---|
| 135 | /** @returns the Speed of the Node */ |
---|
| 136 | inline float getSpeed() const { return 0; }; |
---|
| 137 | /** @returns the Velocity of the Node */ |
---|
| 138 | inline const Vector& getVelocity() const { return this->velocity; }; |
---|
| 139 | |
---|
| 140 | |
---|
[5215] | 141 | void addChild2D (Element2D* child, int parentingMode = E2D_PARENT_NONE); |
---|
[5081] | 142 | void addChild2D (const char* childName); |
---|
| 143 | void removeChild2D (Element2D* child); |
---|
| 144 | void remove2D(); |
---|
| 145 | |
---|
| 146 | void setParent2D (Element2D* parent); |
---|
| 147 | void setParent2D (const char* parentName); |
---|
| 148 | /** @returns the parent of this Element2D */ |
---|
| 149 | Element2D* getParent () const { return this->parent; }; |
---|
| 150 | |
---|
[5082] | 151 | void softReparent2D(Element2D* parentNode, float bias = 1.0); |
---|
| 152 | void softReparent2D(const char* parentName, float bias = 1.0); |
---|
[5081] | 153 | |
---|
| 154 | /** @param parentMode sets the parentingMode of this Node */ |
---|
| 155 | void setParentMode2D (E2D_PARENT_MODE parentMode) { this->parentMode = parentMode; }; |
---|
| 156 | void setParentMode2D (const char* parentingMode); |
---|
| 157 | /** @returns the Parenting mode of this node */ |
---|
| 158 | int getParentMode2D() const { return this->parentMode; }; |
---|
| 159 | |
---|
| 160 | void update2D (float dt); |
---|
| 161 | |
---|
| 162 | void debug (unsigned int depth = 1, unsigned int level = 0) const; |
---|
| 163 | void debugDraw2D(unsigned int depth = 1, float size = 1.0, Vector color = Vector(1,1,1)) const; |
---|
| 164 | |
---|
| 165 | // helper functions // |
---|
[5082] | 166 | static const char* parentingModeToChar2D(int parentingMode); |
---|
| 167 | static E2D_PARENT_MODE charToParentingMode2D(const char* parentingMode); |
---|
[5081] | 168 | |
---|
| 169 | private: |
---|
| 170 | /** tells the child that the parent's Coordinate has changed */ |
---|
| 171 | inline void parentCoorChanged () { this->bRelCoorChanged = true; } |
---|
| 172 | /** tells the child that the parent's Direction has changed */ |
---|
| 173 | inline void parentDirChanged () { this->bRelDirChanged = true; } |
---|
| 174 | /** @returns the last calculated coordinate */ |
---|
| 175 | inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; } |
---|
| 176 | |
---|
| 177 | public: |
---|
[4850] | 178 | virtual void tick(float dt); |
---|
| 179 | virtual void draw() const = NULL; |
---|
[4840] | 180 | |
---|
[5089] | 181 | private: |
---|
| 182 | const PNode* bindNode; //!< a node over which to display this 2D-element |
---|
[4847] | 183 | |
---|
[5089] | 184 | E2D_ALIGNMENT alignment; //!< How the Element is aligned around its Position |
---|
[4856] | 185 | |
---|
[5089] | 186 | bool visible; //!< If the given Element2D is visible. |
---|
| 187 | bool active; //!< If the given Element2D is active. |
---|
| 188 | E2D_LAYER layer; //!< What layer this Element2D is on. |
---|
[4856] | 189 | |
---|
[5089] | 190 | bool bRelCoorChanged; //!< If Relative Coordinate has changed since last time we checked |
---|
| 191 | bool bRelDirChanged; //!< If Relative Direction has changed since last time we checked |
---|
[5081] | 192 | |
---|
[5089] | 193 | Vector relCoordinate; //!< coordinates relative to the parent |
---|
| 194 | Vector absCoordinate; //!< absolute coordinates in the world ( from (0,0,0) ) |
---|
| 195 | float relDirection; //!< direction relative to the parent |
---|
[5211] | 196 | float absDirection; //!< absolute diretion in the world ( from (0,0,1) ) |
---|
[5081] | 197 | |
---|
[5089] | 198 | Vector prevRelCoordinate; //!< The last Relative Coordinate from the last update-Cycle. |
---|
| 199 | Vector lastAbsCoordinate; //!< this is used for speedcalculation, it stores the last coordinate |
---|
| 200 | float prevRelDirection; //!< The last Relative Direciton from the last update-Cycle. |
---|
| 201 | // float lastAbsDirection; |
---|
[5081] | 202 | |
---|
[5089] | 203 | Vector velocity; //!< Saves the velocity. |
---|
[5081] | 204 | |
---|
[5089] | 205 | Vector* toCoordinate; //!< a position to which to iterate. (This is used in conjunction with softReparent.and set*CoorSoft) |
---|
| 206 | float* toDirection; //!< a direction to which to iterate. (This is used in conjunction with softReparent and set*DirSoft) |
---|
| 207 | float bias; //!< how fast to iterate to the given position (default is 1) |
---|
[5081] | 208 | |
---|
[5089] | 209 | Element2D* parent; //!< a pointer to the parent node |
---|
| 210 | tList<Element2D>* children; //!< list of the children of this Element2D |
---|
[5081] | 211 | |
---|
[5089] | 212 | unsigned int parentMode; //!< the mode of the binding |
---|
[1853] | 213 | }; |
---|
| 214 | |
---|
[5082] | 215 | //! The top joint of all Element2D's every Element2D is somehow connected to this one. |
---|
| 216 | class NullElement2D : public Element2D { |
---|
[4839] | 217 | |
---|
[5082] | 218 | public: |
---|
| 219 | /** @returns a Pointer to the only object of this Class */ |
---|
| 220 | inline static NullElement2D* getInstance() { if (!singletonRef) singletonRef = new NullElement2D(); return singletonRef; }; |
---|
| 221 | virtual ~NullElement2D (); |
---|
| 222 | |
---|
| 223 | private: |
---|
| 224 | NullElement2D (); |
---|
| 225 | virtual void draw() const {}; |
---|
| 226 | |
---|
| 227 | private: |
---|
| 228 | static NullElement2D* singletonRef; //!< A reference to the NullElement2D |
---|
| 229 | |
---|
| 230 | }; |
---|
| 231 | |
---|
[4838] | 232 | #endif /* _ELEMENT_2D_H */ |
---|