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