[4838] | 1 | /*! |
---|
[4839] | 2 | * @file element_2d.h |
---|
[5081] | 3 | * Definition of the 2D elements rendered on top through the GraphicsEngine |
---|
| 4 | * @todo reimplement it, so it looks just like PNode. |
---|
[3245] | 5 | */ |
---|
[1853] | 6 | |
---|
[4838] | 7 | #ifndef _ELEMENT_2D_H |
---|
| 8 | #define _ELEMENT_2D_H |
---|
[1853] | 9 | |
---|
[3543] | 10 | #include "base_object.h" |
---|
[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 | { |
---|
[4862] | 21 | E2D_BELOW_ALL = 1, //!< Will be rendered below the 3D-scene. @todo make this work. |
---|
| 22 | E2D_BOTTOM = 2, //!< Will be rendered on the bottom Layer |
---|
| 23 | E2D_MEDIUM = 4, //!< Will be rendered on the medium Layer. |
---|
| 24 | E2D_TOP = 8, //!< Will be rendered on top of everything else |
---|
[4848] | 25 | |
---|
[4862] | 26 | E2D_LAYER_COUNT = 4 //!< The count of Layers. |
---|
| 27 | } E2D_LAYER; |
---|
| 28 | #define E2D_DEFAULT_LAYER E2D_TOP |
---|
| 29 | #define E2D_ALL_LAYERS E2D_TOP | E2D_MEDIUM | E2D_BOTTOM | E2D_BELOW_ALL |
---|
[4839] | 30 | |
---|
[4843] | 31 | typedef enum |
---|
| 32 | { |
---|
[4856] | 33 | E2D_ALIGN_NONE = 0, |
---|
| 34 | E2D_ALIGN_LEFT = 1, |
---|
| 35 | E2D_ALIGN_RIGHT = 2, |
---|
| 36 | E2D_ALIGN_CENTER = 4, |
---|
| 37 | E2D_ALIGN_SCREEN_CENTER = 8 |
---|
[4848] | 38 | } E2D_ALIGNMENT; |
---|
[4843] | 39 | |
---|
[5081] | 40 | typedef enum |
---|
| 41 | { |
---|
| 42 | E2D_LOCAL_ROTATE = 1, //!< Rotates all the children around their centers. |
---|
| 43 | E2D_ROTATE_MOVEMENT = 2, //!< Moves all the children around the center of their parent, without the rotation around their own centers. |
---|
| 44 | |
---|
| 45 | E2D_MOVEMENT = 4, //!< Moves all children along with the parent. |
---|
| 46 | // special linkage modes |
---|
| 47 | E2D_ALL = 3, //!< Moves all children around the center of their parent, and also rotates their centers |
---|
| 48 | E2D_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. |
---|
| 49 | } E2D_PARENT_MODE; |
---|
| 50 | #define E2D_DEFAULT_PARENTING_MODE E2D_ALL |
---|
| 51 | |
---|
[4847] | 52 | //! A Struct defining the Position of an Element in 2D-space |
---|
| 53 | struct Position2D |
---|
| 54 | { |
---|
| 55 | float x; //!< The x-coordinate. |
---|
| 56 | float y; //!< The y-coordinate. |
---|
| 57 | float depth; //!< The distance from the viewing plane. |
---|
| 58 | }; |
---|
| 59 | |
---|
[3955] | 60 | //! A class for ... |
---|
[4849] | 61 | class Element2D : virtual public BaseObject { |
---|
[1853] | 62 | |
---|
[4850] | 63 | public: |
---|
| 64 | Element2D(); |
---|
| 65 | virtual ~Element2D(); |
---|
[1853] | 66 | |
---|
[4858] | 67 | void init(); |
---|
| 68 | void loadParams(const TiXmlElement* root); |
---|
| 69 | |
---|
[4856] | 70 | /** @param xCoord the xCoordinate @param yCoord the y-Coordinate. These coordinates are Relative */ |
---|
| 71 | inline void setPosition2D(int xCoord, int yCoord) { this->relPos2D[0] = xCoord; this->relPos2D[1] = yCoord; }; |
---|
| 72 | /** this returns the Absolute Position on the screen set by positioning in the tick-phase */ |
---|
| 73 | inline const Position2D& getPosition2D() { return this->absPos2D; }; |
---|
[4862] | 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); |
---|
[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); |
---|
[4840] | 93 | |
---|
[4850] | 94 | /** @returns the visibility state */ |
---|
| 95 | inline bool isVisible() { return this->visible; }; |
---|
[5068] | 96 | /** @returns the active-State */ |
---|
| 97 | inline bool isActive() { return this->active; }; |
---|
[4840] | 98 | |
---|
[5081] | 99 | |
---|
| 100 | // LIKE PNODE |
---|
| 101 | public: |
---|
| 102 | void setRelCoor2D (const Vector& relCoord); |
---|
| 103 | void setRelCoor2D (float x, float y, float dontCare); |
---|
| 104 | void setRelCoorSoft2D(const Vector& relCoordSoft, float bias = 1.0); |
---|
| 105 | void setRelCoorSoft2D(float x, float y, float dontCare, float bias = 1.0); |
---|
| 106 | /** @returns the relative position */ |
---|
| 107 | inline const Vector& getRelCoor2D () const { return this->prevRelCoordinate; }; |
---|
| 108 | void setAbsCoor2D (const Vector& absCoord); |
---|
| 109 | void setAbsCoor2D (float x, float y, float depth); |
---|
| 110 | /** @returns the absolute position */ |
---|
| 111 | inline const Vector& getAbsCoor2D () const { return this->absCoordinate; }; |
---|
| 112 | void shiftCoor (const Vector& shift); |
---|
| 113 | |
---|
| 114 | void setRelDir2D (float relDir); |
---|
| 115 | void setRelDirSoft2D(float relDirSoft, float bias = 1.0); |
---|
| 116 | /** @returns the relative Direction */ |
---|
| 117 | inline float getRelDir2D () const { return this->prevRelDirection; }; |
---|
| 118 | void setAbsDir2D (float absDir); |
---|
| 119 | /** @returns the absolute Direction */ |
---|
| 120 | inline float getAbsDir2D () const { return this->absDirection; }; |
---|
| 121 | void shiftDir (float shiftDir); |
---|
| 122 | |
---|
| 123 | /** @returns the Speed of the Node */ |
---|
| 124 | inline float getSpeed() const { return 0; }; |
---|
| 125 | /** @returns the Velocity of the Node */ |
---|
| 126 | inline const Vector& getVelocity() const { return this->velocity; }; |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | void addChild2D (Element2D* child, int parentingMode = E2D_DEFAULT_PARENTING_MODE); |
---|
| 130 | void addChild2D (const char* childName); |
---|
| 131 | void removeChild2D (Element2D* child); |
---|
| 132 | void remove2D(); |
---|
| 133 | |
---|
| 134 | void setParent2D (Element2D* parent); |
---|
| 135 | void setParent2D (const char* parentName); |
---|
| 136 | /** @returns the parent of this Element2D */ |
---|
| 137 | Element2D* getParent () const { return this->parent; }; |
---|
| 138 | |
---|
| 139 | void softReparent(PNode* parentNode, float bias = 1.0); |
---|
| 140 | void softReparent(const char* parentName, float bias = 1.0); |
---|
| 141 | |
---|
| 142 | /** @param parentMode sets the parentingMode of this Node */ |
---|
| 143 | void setParentMode2D (E2D_PARENT_MODE parentMode) { this->parentMode = parentMode; }; |
---|
| 144 | void setParentMode2D (const char* parentingMode); |
---|
| 145 | /** @returns the Parenting mode of this node */ |
---|
| 146 | int getParentMode2D() const { return this->parentMode; }; |
---|
| 147 | |
---|
| 148 | void update2D (float dt); |
---|
| 149 | |
---|
| 150 | void debug (unsigned int depth = 1, unsigned int level = 0) const; |
---|
| 151 | void debugDraw2D(unsigned int depth = 1, float size = 1.0, Vector color = Vector(1,1,1)) const; |
---|
| 152 | |
---|
| 153 | // helper functions // |
---|
| 154 | static const char* parentingModeToChar(int parentingMode); |
---|
| 155 | static E2D_PARENT_MODE charToParentingMode(const char* parentingMode); |
---|
| 156 | |
---|
| 157 | private: |
---|
| 158 | void init(Element2D* parent); |
---|
| 159 | /** tells the child that the parent's Coordinate has changed */ |
---|
| 160 | inline void parentCoorChanged () { this->bRelCoorChanged = true; } |
---|
| 161 | /** tells the child that the parent's Direction has changed */ |
---|
| 162 | inline void parentDirChanged () { this->bRelDirChanged = true; } |
---|
| 163 | /** @returns the last calculated coordinate */ |
---|
| 164 | inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; } |
---|
| 165 | |
---|
| 166 | public: |
---|
[4850] | 167 | virtual void tick(float dt); |
---|
| 168 | virtual void draw() const = NULL; |
---|
[4840] | 169 | |
---|
[4858] | 170 | protected: |
---|
| 171 | void positioning(); |
---|
[4847] | 172 | |
---|
[4850] | 173 | protected: |
---|
| 174 | const PNode* bindNode; //!< a node over which to display this 2D-element |
---|
[4856] | 175 | int relPos2D[2]; //!< X-coord, Y-Coord (relative to the Coordinates of the alignment if given.) |
---|
| 176 | Position2D absPos2D; //!< The absolute position of the 2D-Element. |
---|
| 177 | |
---|
| 178 | E2D_ALIGNMENT alignment; //!< How the Element is aligned around its Position |
---|
| 179 | |
---|
[4850] | 180 | private: |
---|
[5068] | 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. |
---|
[5081] | 184 | |
---|
| 185 | |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | private: |
---|
| 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 |
---|
| 192 | |
---|
| 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 |
---|
| 196 | float absDirection; //!< absolute direvtion in the world ( from (0,0,1) ) |
---|
| 197 | |
---|
| 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 | // Quaternion lastAbsDirection; |
---|
| 202 | |
---|
| 203 | Vector velocity; //!< Saves the velocity. |
---|
| 204 | |
---|
| 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) |
---|
| 208 | |
---|
| 209 | Element2D* parent; //!< a pointer to the parent node |
---|
| 210 | tList<Element2D>* children; //!< list of the children of this Element2D |
---|
| 211 | |
---|
| 212 | unsigned int parentMode; //!< the mode of the binding |
---|
[1853] | 213 | }; |
---|
| 214 | |
---|
[4839] | 215 | |
---|
[4838] | 216 | #endif /* _ELEMENT_2D_H */ |
---|