Posted By


xterminhate on 03/05/13

Tagged


Statistics


Viewed 109 times
Favorited by 0 user(s)

CGUIDynamicGrid.h


/ Published in: C++
Save to your folder(s)

http://irrlicht.sourceforge.net/forum/viewtopic.php?f=9&t=48291&p=278332#p278332


Copy this code and paste it in your HTML
  1. // Xterm-in'Hate
  2.  
  3. #ifndef __C_GUI_DYNAMIC_GRID_H_INCLUDED__
  4. #define __C_GUI_DYNAMIC_GRID_H_INCLUDED__
  5.  
  6. #include "IrrCompileConfig.h"
  7. //#ifdef _IRR_COMPILE_WITH_GUI_
  8.  
  9. #include "IGUIDynamicGrid.h"
  10. #include "SColor.h"
  11. #include "vector2d.h"
  12.  
  13. namespace irr
  14. {
  15. namespace core{
  16.  
  17. template<class InputIt, class T>
  18. InputIt find(InputIt first, InputIt last, const T& value)
  19. {
  20. for (; first != last; ++first)
  21. {
  22. if (*first == value)
  23. {
  24. return first;
  25. }
  26. }
  27. return last;
  28. }
  29. }
  30. namespace gui
  31. {
  32.  
  33. class CGUIDynamicGrid : public IGUIDynamicGrid
  34. {
  35. public:
  36. //! constructor
  37. CGUIDynamicGrid(IGUIEnvironment* environment, IGUIElement* parent);
  38.  
  39. //! destructor
  40. virtual ~CGUIDynamicGrid();
  41.  
  42. //! show joints (DYNAMIC GRID DEBUG:To Be Deleted)
  43. void setJointVisible(bool visible);
  44.  
  45. //! set the behavior of the Dynamic Grid
  46. void setBehavior(EGUI_DYNAMIC_GRID_BEHAVIOR behavior);
  47.  
  48. //! get the behavior of the Dynamic Grid
  49. EGUI_DYNAMIC_GRID_BEHAVIOR getBehavior() const;
  50.  
  51. //! draws the element and its children
  52. virtual void draw();
  53.  
  54. private:
  55. //! Child GUI elements, which are sticked together, form a 'body'.
  56.  
  57. //! Each child GUI element of a body is a 'bone'.
  58. struct bone; // forward decl.
  59.  
  60. //! Orphan GUI elements are considered as bodies and bones too.
  61.  
  62. //! Each 'bone' has got 8 joints (1 per axis (=2) x 1 per corner (=4)).
  63. struct joint
  64. {
  65. ///! enumaration of all types of joint
  66. enum EJOINT_TYPE
  67. {
  68. EJT_UPPER_RIGHT_TO_TOP=0,
  69. EJT_UPPER_RIGHT_TO_RIGHT,
  70. EJT_LOWER_RIGHT_TO_RIGHT,
  71. EJT_LOWER_RIGHT_TO_BOTTOM,
  72. EJT_LOWER_LEFT_TO_BOTTOM,
  73. EJT_LOWER_LEFT_TO_LEFT,
  74. EJT_UPPER_LEFT_TO_LEFT,
  75. EJT_UPPER_LEFT_TO_TOP,
  76. EJT_MAX
  77. };
  78. joint(EJOINT_TYPE type, bone* parent_bone, const core::rect<s32>& rectangle);
  79. ~joint();
  80. const core::position2di& getPosition() const;
  81. EJOINT_TYPE getType() const;
  82. bool isAttached() const;
  83. bool canAttachToJoint(const joint* j) const;
  84. void attachToJoint(joint* j);
  85. void dettach();
  86. bool isAttachedToBone(const bone* b) const;
  87. bone* getAttachedBone() const;
  88. void dragTo(u32 counter, const core::position2di& position);
  89. void update(u32 counter, const core::rect<s32>& rectangle);
  90. private:
  91. EJOINT_TYPE Type;
  92. bone* ParentBone;
  93. joint* AttachedJoint;
  94. core::position2di Position;
  95. u32 Counter;
  96. };
  97.  
  98. struct bone
  99. {
  100. bone(IGUIElement* element);
  101. ~bone();
  102. joint* getJoint(size_t index) const;
  103. bool own(const IGUIElement* element) const;
  104. void update(u32 counter, const core::position2di& dist = core::position2di(0,0));
  105. bool canAttachToBone(const bone* b, size_t& j1, size_t& j2 ) const;
  106. void attachToBone(bone* b, size_t j1, size_t j2 );
  107. void dettach();
  108. bool isAttachedToBone(const bone* b) const;
  109. bool isRectCollidedBody(const bone* b) const;
  110. ///! Attach together every bones of the body identified by THIS bone, by as many joints as possible
  111. void consolidate();
  112. ///! Attach bones THIS and B with as many joint as possible
  113. void attachToBoneByAllPossibleJoints(bone* b);
  114. const IGUIElement* getElement() const;
  115. private:
  116. //! Recursive algorithm that detects overlapping bones => dont attached a bone to a body if a bone overlapping is detected
  117. static bool isRectCollidedBodyRecursive(const CGUIDynamicGrid::bone* b, const core::rect<s32>& rect, core::list<const bone*>& body_bones_processed);
  118. //! Recursive algorithm that lists the bones of a body, from one bone of this body
  119. static void listBonesRecursive(const bone* b,core::list<const bone*>& body_bones);
  120. static void listBonesRecursive(bone* b,core::list<bone*>& body_bones);
  121. joint* Joints[joint::EJT_MAX];
  122. IGUIElement* Element;
  123. u32 Counter;
  124. };
  125.  
  126. //! bones database
  127. core::list<bone*> Bones;
  128.  
  129. //! return the bone of a child element
  130. bone* getBone( IGUIElement* element );
  131.  
  132. //! return true if this element is one of my managed child element (!= irr::isMyChild)
  133. bool hasBone(const IGUIElement* element, bone*& b) const;
  134.  
  135. //! init dynamic grid algorithm
  136. void init();
  137.  
  138. //! reset dynamic grid algorithm
  139. void reset();
  140.  
  141. //! apply dynamic grid algorithm
  142. void snap();
  143.  
  144. //! try to attach this bone to a body
  145. void tryToAttach(bone* b);
  146.  
  147. //! try to form body based on bone proximity
  148. void tryToAttachAll();
  149.  
  150. //! turn bodies into free bones
  151. void dettachAll();
  152.  
  153. ///! appearance of the dynamic grid (DEBUG)
  154. bool isJointVisible;
  155.  
  156. //! behavior of the dynamic grid
  157. EGUI_DYNAMIC_GRID_BEHAVIOR Behavior;
  158. };
  159.  
  160. } // end namespace gui
  161. } // end namespace irr
  162.  
  163. //#endif // _IRR_COMPILE_WITH_GUI_
  164.  
  165. #endif // __C_GUI_DYNAMIC_GRID_H_INCLUDED__

URL: http://irrlicht.sourceforge.net/forum/viewtopic.php?f=9&t=48291&p=278332#p278332

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.