Frame 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2008 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. // Code by: Jeremy Moles (cubicool) 2007-2008
  14. #ifndef OSGWIDGET_FRAME
  15. #define OSGWIDGET_FRAME
  16. #include <osgWidget/Table>
  17. namespace osgWidget {
  18. /*
  19. Lets take a moment and explain how Frame texturing works. When you create a Frame, you use
  20. a specially designed texture that is "chopped" up horizontally by the Frame code into 8 equal
  21. regions. Each region is then textured to a corresponding portion of the Frame, in the
  22. following order:
  23. +---+---+---+---+---+---+---+---+
  24. | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
  25. +---+---+---+---+---+---+---+---+
  26. 1. Upper-Left corner.
  27. 2. Top border (rotated 90 degrees CCW).
  28. 3. Upper-Right corner.
  29. 4. Left border.
  30. 5. Right border.
  31. 6. Bottom-Left corner.
  32. 7. Bottom border (rotated 90 degrees CCW).
  33. 8. Bottom-Right corner.
  34. Now, these should be pretty self-explanatory if you visualize a frame as a 3x3 "table"
  35. (which is exactly what it is), but note how regions 2 and 7 are rotated counter-clockwise.
  36. We do this for a VERY important reason: we want to enable texture repeat on the border
  37. regions, so that when the frame is resized the borders cleanly paint the texture over
  38. the entire are (and don't stretch it). However, it is impossible in OpenGL to repeat a
  39. sub-region of a texture without including either the vertical or horizontal bounds, so the
  40. artist is required to rotate the region during their rendering so that our code can properly
  41. rotate it back internally and have it repeat in the desired way.
  42. This method of texturing a Frame object is inspired by World of Warcraft "edge files", and it
  43. is both efficient and easy-to-use--once you understand the basics. If you're still confused,
  44. take a look at this URL, or any of the example themes:
  45. http://www.wowwiki.com/EdgeFiles
  46. */
  47. class OSGWIDGET_EXPORT Frame: public Table
  48. {
  49. public:
  50. enum CornerType
  51. {
  52. CORNER_LOWER_LEFT,
  53. CORNER_LOWER_RIGHT,
  54. CORNER_UPPER_LEFT,
  55. CORNER_UPPER_RIGHT
  56. };
  57. enum BorderType
  58. {
  59. BORDER_LEFT,
  60. BORDER_RIGHT,
  61. BORDER_TOP,
  62. BORDER_BOTTOM
  63. };
  64. enum FrameOptions
  65. {
  66. FRAME_RESIZE = 1,
  67. FRAME_MOVE = 2,
  68. FRAME_TEXTURE = 4,
  69. FRAME_ALL = FRAME_RESIZE | FRAME_MOVE | FRAME_TEXTURE
  70. };
  71. static std::string cornerTypeToString (CornerType);
  72. static std::string borderTypeToString (BorderType);
  73. class OSGWIDGET_EXPORT Corner: public Widget
  74. {
  75. public:
  76. META_Object(osgWidget, Corner);
  77. Corner (CornerType = CORNER_LOWER_LEFT, point_type = 0.0f, point_type = 0.0f);
  78. Corner (const Corner&, const osg::CopyOp&);
  79. virtual void parented (Window*);
  80. virtual bool mouseDrag (double, double, const WindowManager*);
  81. CornerType getCornerType() const
  82. {
  83. return _corner;
  84. }
  85. void setCornerType(CornerType corner)
  86. {
  87. _corner = corner;
  88. }
  89. void setCornerTypeAndName(CornerType corner)
  90. {
  91. _corner = corner;
  92. _name = cornerTypeToString(corner);
  93. }
  94. protected:
  95. CornerType _corner;
  96. };
  97. class OSGWIDGET_EXPORT Border: public Widget
  98. {
  99. public:
  100. META_Object(osgWidget, Border);
  101. Border (BorderType = BORDER_LEFT, point_type = 0.0f, point_type = 0.0f);
  102. Border (const Border&, const osg::CopyOp&);
  103. virtual void parented (Window*);
  104. virtual void positioned ();
  105. virtual bool mouseDrag (double, double, const WindowManager*);
  106. BorderType getBorderType() const
  107. {
  108. return _border;
  109. }
  110. void setBorderType(BorderType border)
  111. {
  112. _border = border;
  113. }
  114. void setBorderTypeAndName(BorderType border)
  115. {
  116. _border = border;
  117. _name = borderTypeToString(border);
  118. }
  119. protected:
  120. BorderType _border;
  121. };
  122. META_Object(osgWidget, Frame);
  123. Frame (const std::string& = "", unsigned int = 0);
  124. Frame (const Frame&, const osg::CopyOp&);
  125. static Frame* createSimpleFrame(
  126. const std::string&,
  127. point_type,
  128. point_type,
  129. point_type,
  130. point_type,
  131. unsigned int = 0,
  132. Frame* = 0
  133. );
  134. static Frame* createSimpleFrameWithSingleTexture(
  135. const std::string&,
  136. osg::ref_ptr<osg::Image>,
  137. point_type,
  138. point_type,
  139. unsigned int = 0,
  140. Frame* = 0
  141. );
  142. static Frame* createSimpleFrameFromTheme(
  143. const std::string&,
  144. osg::ref_ptr<osg::Image>,
  145. point_type,
  146. point_type,
  147. unsigned int = 0,
  148. Frame* = 0
  149. );
  150. void createSimpleFrame(point_type cw, point_type ch, point_type w, point_type h)
  151. {
  152. createSimpleFrame(_name, cw, ch, w, h, 0, this);
  153. }
  154. void createSimpleFrameWithSingleTexture(
  155. osg::Image* image,
  156. point_type w,
  157. point_type h
  158. )
  159. {
  160. createSimpleFrameWithSingleTexture(_name, image, w, h, 0, this);
  161. }
  162. bool setWindow(Window*);
  163. EmbeddedWindow* getEmbeddedWindow() { return dynamic_cast<EmbeddedWindow*>(getByRowCol(1, 1)); }
  164. const EmbeddedWindow* getEmbeddedWindow() const { return dynamic_cast<const EmbeddedWindow*>(getByRowCol(1, 1)); }
  165. Corner* getCorner(CornerType c) { return dynamic_cast<Corner*>(_getCorner(c)); }
  166. const Corner* getCorner(CornerType c) const { return dynamic_cast<const Corner*>(_getCorner(c)); }
  167. Border* getBorder(BorderType b) { return dynamic_cast<Border*>(_getBorder(b)); }
  168. const Border* getBorder(BorderType b) const { return dynamic_cast<const Border*>(_getBorder(b)); }
  169. // This method resizes the internal EmbeddedWindow object and then properly resizes
  170. // the reset of the Frame based on the sizes of the Corners, Borders, etc.
  171. bool resizeFrame(point_type, point_type);
  172. unsigned int getFlags() const
  173. {
  174. return _flags;
  175. }
  176. void setFlags(unsigned int flags)
  177. {
  178. _flags = flags;
  179. }
  180. bool canResize() const
  181. {
  182. return (_flags & FRAME_RESIZE) != 0;
  183. }
  184. bool canMove() const
  185. {
  186. return (_flags & FRAME_MOVE) != 0;
  187. }
  188. bool canTexture() const
  189. {
  190. return (_flags & FRAME_TEXTURE) != 0;
  191. }
  192. protected:
  193. Widget* _getCorner (CornerType) const;
  194. Widget* _getBorder (BorderType) const;
  195. unsigned int _flags;
  196. };
  197. }
  198. #endif