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