guiDefaultControlRender.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "graphics/dgl.h"
  23. #include "gui/guiDefaultControlRender.h"
  24. #include "gui/guiTypes.h"
  25. #include "graphics/color.h"
  26. #include "math/mRect.h"
  27. void renderBorderedRect(RectI &bounds, GuiControlProfile *profile, GuiControlState state )
  28. {
  29. //Get the border profiles
  30. GuiBorderProfile *leftProfile = (profile->mBorderLeft) ? profile->mBorderLeft : profile->mBorderDefault;
  31. GuiBorderProfile *rightProfile = (profile->mBorderRight) ? profile->mBorderRight : profile->mBorderDefault;
  32. GuiBorderProfile *topProfile = (profile->mBorderTop) ? profile->mBorderTop : profile->mBorderDefault;
  33. GuiBorderProfile *bottomProfile = (profile->mBorderBottom) ? profile->mBorderBottom : profile->mBorderDefault;
  34. //Get the colors
  35. ColorI fillColor = profile->getFillColor(state);
  36. ColorI leftColor = (leftProfile) ? leftProfile->getBorderColor(state) : ColorI();
  37. ColorI rightColor = (rightProfile) ? rightProfile->getBorderColor(state) : ColorI();
  38. ColorI topColor = (topProfile) ? topProfile->getBorderColor(state) : ColorI();
  39. ColorI bottomColor = (bottomProfile) ? bottomProfile->getBorderColor(state) : ColorI();
  40. S32 leftSize = (leftProfile) ? leftProfile->getBorder(state) : 0;
  41. S32 rightSize = (rightProfile) ? rightProfile->getBorder(state) : 0;
  42. S32 topSize = (topProfile) ? topProfile->getBorder(state) : 0;
  43. S32 bottomSize = (bottomProfile) ? bottomProfile->getBorder(state) : 0;
  44. //Get the inner rect
  45. RectI innerRect = RectI(bounds.point.x + leftSize, bounds.point.y + topSize, (bounds.extent.x - leftSize) - rightSize, (bounds.extent.y - topSize) - bottomSize);
  46. //Draw the fill
  47. if(profile->mOpaque)
  48. {
  49. S32 fillWidth = innerRect.extent.x + ((leftProfile && leftProfile->mUnderfill) ? leftSize : 0) + ((rightProfile && rightProfile->mUnderfill) ? rightSize : 0);
  50. S32 fillHeight = innerRect.extent.y + ((topProfile && topProfile->mUnderfill) ? topSize : 0) + ((bottomProfile && bottomProfile->mUnderfill) ? bottomSize : 0);
  51. RectI fillRect = RectI((leftProfile && leftProfile->mUnderfill) ? bounds.point.x : innerRect.point.x,
  52. (topProfile && topProfile->mUnderfill) ? bounds.point.y : innerRect.point.y, fillWidth, fillHeight);
  53. dglDrawRectFill(fillRect, fillColor);
  54. }
  55. //Draw the borders
  56. //Points for outer bounds starting top left and traveling counter-clockwise
  57. Point2I p1 = Point2I(bounds.point);
  58. Point2I p2 = Point2I(bounds.point.x, bounds.point.y + bounds.extent.y);
  59. Point2I p3 = Point2I(bounds.point.x + bounds.extent.x, bounds.point.y + bounds.extent.y);
  60. Point2I p4 = Point2I(bounds.point.x + bounds.extent.x, bounds.point.y);
  61. //Points for inner bounds starting top left and traveling counter-clockwise
  62. Point2I p5 = Point2I(innerRect.point);
  63. Point2I p6 = Point2I(innerRect.point.x, innerRect.point.y + innerRect.extent.y);
  64. Point2I p7 = Point2I(innerRect.point.x + innerRect.extent.x, innerRect.point.y + innerRect.extent.y);
  65. Point2I p8 = Point2I(innerRect.point.x + innerRect.extent.x, innerRect.point.y);
  66. if (leftSize > 0)
  67. {
  68. dglDrawQuadFill(p1, p2, p6, p5, leftColor);
  69. }
  70. if (rightSize > 0)
  71. {
  72. dglDrawQuadFill(p8, p7, p3, p4, rightColor);
  73. }
  74. if (topSize > 0)
  75. {
  76. dglDrawQuadFill(p1, p5, p8, p4, topColor);
  77. }
  78. if (bottomSize > 0)
  79. {
  80. dglDrawQuadFill(p6, p2, p3, p7, bottomColor);
  81. }
  82. }
  83. void renderBorderedCircle(Point2I &center, S32 radius, GuiControlProfile *profile, GuiControlState state)
  84. {
  85. //Get the border profiles
  86. GuiBorderProfile *borderProfile = profile->mBorderDefault;
  87. //Get the colors
  88. ColorI fillColor = profile->getFillColor(state);
  89. ColorI borderColor = (profile->mBorderDefault) ? profile->mBorderDefault->getBorderColor(state) : ColorI();
  90. S32 borderSize = (profile->mBorderDefault) ? profile->mBorderDefault->getBorder(state) : 0;
  91. //Draw the fill
  92. if (profile->mOpaque)
  93. {
  94. S32 fillRadius = (profile->mBorderDefault && profile->mBorderDefault->mUnderfill) ? radius : radius - borderSize;
  95. dglDrawCircleFill(center, fillRadius, fillColor);
  96. }
  97. //Draw the border
  98. dglDrawCircle(center, radius, borderColor, borderSize);
  99. }
  100. // DAW: Render out the sizable bitmap borders based on a multiplier into the bitmap array
  101. // Based on the 'Skinnable GUI Controls in TGE' resource by Justin DuJardin
  102. void renderSizableBitmapBordersFilled(RectI &bounds, S32 baseMultiplier, GuiControlProfile *profile)
  103. {
  104. S32 NumBitmaps = 9;
  105. S32 startIndex = NumBitmaps * (baseMultiplier - 1);
  106. renderSizableBitmapBordersFilledIndex(bounds, startIndex, profile);
  107. }
  108. // DAW: Render out the sizable bitmap borders based on a multiplier into the bitmap array
  109. // Based on the 'Skinnable GUI Controls in TGE' resource by Justin DuJardin
  110. void renderSizableBitmapBordersFilledIndex(RectI &bounds, S32 startIndex, GuiControlProfile *profile)
  111. {
  112. // DAW: Indices into the bitmap array
  113. S32 NumBitmaps = 9;
  114. S32 BorderTopLeft = startIndex;
  115. S32 BorderTop = 1 + BorderTopLeft;
  116. S32 BorderTopRight = 2 + BorderTopLeft;
  117. S32 BorderLeft = 3 + BorderTopLeft;
  118. S32 Fill = 4 + BorderTopLeft;
  119. S32 BorderRight = 5 + BorderTopLeft;
  120. S32 BorderBottomLeft = 6 + BorderTopLeft;
  121. S32 BorderBottom = 7 + BorderTopLeft;
  122. S32 BorderBottomRight = 8 + BorderTopLeft;
  123. dglClearBitmapModulation();
  124. if (profile->mBitmapArrayRects.size() >= (NumBitmaps + startIndex))
  125. {
  126. RectI destRect;
  127. RectI stretchRect;
  128. RectI* mBitmapBounds = profile->mBitmapArrayRects.address();
  129. // Draw all corners first.
  130. //top left border
  131. dglDrawBitmapSR(profile->mTextureHandle, Point2I(bounds.point.x, bounds.point.y), mBitmapBounds[BorderTopLeft]);
  132. //top right border
  133. dglDrawBitmapSR(profile->mTextureHandle, Point2I(bounds.point.x + bounds.extent.x - mBitmapBounds[BorderTopRight].extent.x, bounds.point.y), mBitmapBounds[BorderTopRight]);
  134. //bottom left border
  135. dglDrawBitmapSR(profile->mTextureHandle, Point2I(bounds.point.x, bounds.point.y + bounds.extent.y - mBitmapBounds[BorderBottomLeft].extent.y), mBitmapBounds[BorderBottomLeft]);
  136. //bottom right border
  137. dglDrawBitmapSR(profile->mTextureHandle, Point2I(
  138. bounds.point.x + bounds.extent.x - mBitmapBounds[BorderBottomRight].extent.x,
  139. bounds.point.y + bounds.extent.y - mBitmapBounds[BorderBottomRight].extent.y),
  140. mBitmapBounds[BorderBottomRight]);
  141. // End drawing corners
  142. // Begin drawing sides and top stretched borders
  143. //start with top line stretch
  144. destRect.point.x = bounds.point.x + mBitmapBounds[BorderTopLeft].extent.x;
  145. destRect.extent.x = bounds.extent.x - mBitmapBounds[BorderTopRight].extent.x - mBitmapBounds[BorderTopLeft].extent.x;
  146. destRect.extent.y = mBitmapBounds[BorderTop].extent.y;
  147. destRect.point.y = bounds.point.y;
  148. //stretch it
  149. stretchRect = mBitmapBounds[BorderTop];
  150. stretchRect.inset(1, 0);
  151. //draw it
  152. dglDrawBitmapStretchSR(profile->mTextureHandle, destRect, stretchRect);
  153. //bottom line stretch
  154. destRect.point.x = bounds.point.x + mBitmapBounds[BorderBottomLeft].extent.x;
  155. destRect.extent.x = bounds.extent.x - mBitmapBounds[BorderBottomRight].extent.x - mBitmapBounds[BorderBottomLeft].extent.x;
  156. destRect.extent.y = mBitmapBounds[BorderBottom].extent.y;
  157. destRect.point.y = bounds.point.y + bounds.extent.y - mBitmapBounds[BorderBottom].extent.y;
  158. //stretch it
  159. stretchRect = mBitmapBounds[BorderBottom];
  160. stretchRect.inset(1, 0);
  161. //draw it
  162. dglDrawBitmapStretchSR(profile->mTextureHandle, destRect, stretchRect);
  163. //left line stretch
  164. destRect.point.x = bounds.point.x;
  165. destRect.extent.x = mBitmapBounds[BorderLeft].extent.x;
  166. destRect.extent.y = bounds.extent.y - mBitmapBounds[BorderTopLeft].extent.y - mBitmapBounds[BorderBottomLeft].extent.y;
  167. destRect.point.y = bounds.point.y + mBitmapBounds[BorderTopLeft].extent.y;
  168. //stretch it
  169. stretchRect = mBitmapBounds[BorderLeft];
  170. stretchRect.inset(0, 1);
  171. //draw it
  172. dglDrawBitmapStretchSR(profile->mTextureHandle, destRect, stretchRect);
  173. //right line stretch
  174. destRect.point.x = bounds.point.x + bounds.extent.x - mBitmapBounds[BorderRight].extent.x;
  175. destRect.extent.x = mBitmapBounds[BorderRight].extent.x;
  176. destRect.extent.y = bounds.extent.y - mBitmapBounds[BorderTopRight].extent.y - mBitmapBounds[BorderBottomRight].extent.y;
  177. destRect.point.y = bounds.point.y + mBitmapBounds[BorderTopRight].extent.y;
  178. //stretch it
  179. stretchRect = mBitmapBounds[BorderRight];
  180. stretchRect.inset(0, 1);
  181. //draw it
  182. dglDrawBitmapStretchSR(profile->mTextureHandle, destRect, stretchRect);
  183. //fill stretch
  184. destRect.point.x = bounds.point.x + mBitmapBounds[BorderLeft].extent.x;
  185. destRect.extent.x = (bounds.extent.x) - mBitmapBounds[BorderLeft].extent.x - mBitmapBounds[BorderRight].extent.x;
  186. destRect.extent.y = bounds.extent.y - mBitmapBounds[BorderTop].extent.y - mBitmapBounds[BorderBottom].extent.y;
  187. destRect.point.y = bounds.point.y + mBitmapBounds[BorderTop].extent.y;
  188. //stretch it
  189. stretchRect = mBitmapBounds[Fill];
  190. stretchRect.inset(1, 1);
  191. //draw it
  192. dglDrawBitmapStretchSR(profile->mTextureHandle, destRect, stretchRect);
  193. // End drawing sides and top stretched borders
  194. }
  195. }
  196. // DAW: Render out the fixed bitmap borders based on a multiplier into the bitmap array
  197. // It renders left and right caps, with a sizable fill area in the middle to reach
  198. // the x extent. It does not stretch in the y direction.
  199. void renderFixedBitmapBordersFilled(RectI &bounds, S32 baseMultiplier, GuiControlProfile *profile)
  200. {
  201. // DAW: Indices into the bitmap array
  202. S32 NumBitmaps = 3;
  203. S32 BorderLeft = NumBitmaps * baseMultiplier - NumBitmaps;
  204. S32 Fill = 1 + BorderLeft;
  205. S32 BorderRight = 2 + BorderLeft;
  206. dglClearBitmapModulation();
  207. if(profile->mBitmapArrayRects.size() >= (NumBitmaps * baseMultiplier))
  208. {
  209. RectI destRect;
  210. RectI stretchRect;
  211. RectI* mBitmapBounds = profile->mBitmapArrayRects.address();
  212. // Draw all corners first.
  213. //left border
  214. dglDrawBitmapSR(profile->mTextureHandle,Point2I(bounds.point.x,bounds.point.y),mBitmapBounds[BorderLeft]);
  215. //right border
  216. dglDrawBitmapSR(profile->mTextureHandle,Point2I(bounds.point.x + bounds.extent.x - mBitmapBounds[BorderRight].extent.x,bounds.point.y),mBitmapBounds[BorderRight]);
  217. // End drawing corners
  218. // Begin drawing fill
  219. //fill stretch
  220. destRect.point.x = bounds.point.x + mBitmapBounds[BorderLeft].extent.x;
  221. destRect.extent.x = (bounds.extent.x) - mBitmapBounds[BorderLeft].extent.x - mBitmapBounds[BorderRight].extent.x;
  222. destRect.extent.y = mBitmapBounds[Fill].extent.y;
  223. destRect.point.y = bounds.point.y;
  224. //stretch it
  225. stretchRect = mBitmapBounds[Fill];
  226. stretchRect.inset(1,0);
  227. //draw it
  228. dglDrawBitmapStretchSR(profile->mTextureHandle,destRect,stretchRect);
  229. // End drawing fill
  230. }
  231. }
  232. // DAW: Render out the fixed bitmap borders based on a multiplier into the bitmap array
  233. // It renders left and right caps, with a sizable fill area in the middle to reach
  234. // the x extent. It does not stretch in the y direction.
  235. void renderFixedBitmapBordersFilledIndex(RectI &bounds, S32 startIndex, GuiControlProfile *profile)
  236. {
  237. // DAW: Indices into the bitmap array
  238. S32 NumBitmaps = 3;
  239. S32 BorderLeft = startIndex;
  240. S32 Fill = 1 + startIndex;
  241. S32 BorderRight = 2 + startIndex;
  242. dglClearBitmapModulation();
  243. if(profile->mBitmapArrayRects.size() >= (startIndex + NumBitmaps))
  244. {
  245. RectI destRect;
  246. RectI stretchRect;
  247. RectI* mBitmapBounds = profile->mBitmapArrayRects.address();
  248. // Draw all corners first.
  249. //left border
  250. dglDrawBitmapSR(profile->mTextureHandle,Point2I(bounds.point.x,bounds.point.y),mBitmapBounds[BorderLeft]);
  251. //right border
  252. dglDrawBitmapSR(profile->mTextureHandle,Point2I(bounds.point.x + bounds.extent.x - mBitmapBounds[BorderRight].extent.x,bounds.point.y),mBitmapBounds[BorderRight]);
  253. // End drawing corners
  254. // Begin drawing fill
  255. //fill stretch
  256. destRect.point.x = bounds.point.x + mBitmapBounds[BorderLeft].extent.x;
  257. destRect.extent.x = (bounds.extent.x) - mBitmapBounds[BorderLeft].extent.x - mBitmapBounds[BorderRight].extent.x;
  258. destRect.extent.y = mBitmapBounds[Fill].extent.y;
  259. destRect.point.y = bounds.point.y;
  260. //stretch it
  261. stretchRect = mBitmapBounds[Fill];
  262. stretchRect.inset(1,0);
  263. //draw it
  264. dglDrawBitmapStretchSR(profile->mTextureHandle,destRect,stretchRect);
  265. // End drawing fill
  266. }
  267. }
  268. // DAW: Render out the fixed bitmap borders based on a multiplier into the bitmap array
  269. // It renders left and right caps, with a sizable fill area in the middle to reach
  270. // the x extent. It does not stretch in the y direction.
  271. void renderFixedBitmapBordersStretchYFilled(RectI &bounds, S32 baseMultiplier, GuiControlProfile *profile)
  272. {
  273. // DAW: Indices into the bitmap array
  274. S32 NumBitmaps = 3;
  275. S32 BorderLeft = NumBitmaps * baseMultiplier - NumBitmaps;
  276. S32 Fill = 1 + BorderLeft;
  277. S32 BorderRight = 2 + BorderLeft;
  278. dglClearBitmapModulation();
  279. if(profile->mBitmapArrayRects.size() >= (NumBitmaps * baseMultiplier))
  280. {
  281. RectI destRect;
  282. RectI stretchRect;
  283. RectI* mBitmapBounds = profile->mBitmapArrayRects.address();
  284. // Draw all corners first.
  285. //left border
  286. dglDrawBitmapStretchSR(profile->mTextureHandle, RectI( bounds.point.x, bounds.point.y, mBitmapBounds[BorderLeft].extent.x, bounds.extent.y ), mBitmapBounds[BorderLeft] );
  287. //right border
  288. dglDrawBitmapStretchSR(profile->mTextureHandle, RectI(bounds.point.x + bounds.extent.x - mBitmapBounds[BorderRight].extent.x, bounds.point.y, mBitmapBounds[BorderRight].extent.x, bounds.extent.y ), mBitmapBounds[BorderRight] );
  289. // End drawing corners
  290. // Begin drawing fill
  291. //fill stretch
  292. destRect.point.x = bounds.point.x + mBitmapBounds[BorderLeft].extent.x;
  293. destRect.extent.x = (bounds.extent.x) - mBitmapBounds[BorderLeft].extent.x - mBitmapBounds[BorderRight].extent.x;
  294. destRect.extent.y = bounds.extent.y;
  295. destRect.point.y = bounds.point.y;
  296. //stretch it
  297. stretchRect = mBitmapBounds[Fill];
  298. stretchRect.inset(1,0);
  299. //draw it
  300. dglDrawBitmapStretchSR(profile->mTextureHandle, destRect, stretchRect);
  301. // End drawing fill
  302. }
  303. }