guiDefaultControlRender.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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/gColor.h"
  26. #include "math/mRect.h"
  27. void renderBorderedRect(RectI &bounds, GuiControlProfile *profile, GuiControlState state)
  28. {
  29. if(profile)
  30. {
  31. ColorI fillColor = profile->getFillColor(state);
  32. renderBorderedRect(bounds, profile, state, fillColor);
  33. }
  34. }
  35. void renderBorderedRect(RectI &bounds, GuiControlProfile *profile, GuiControlState state, const ColorI &fillColor)
  36. {
  37. if (!profile)
  38. {
  39. return;
  40. }
  41. //Get the border profiles
  42. GuiBorderProfile *leftProfile = profile->getLeftBorder();
  43. GuiBorderProfile *rightProfile = profile->getRightBorder();
  44. GuiBorderProfile *topProfile = profile->getTopBorder();
  45. GuiBorderProfile *bottomProfile = profile->getBottomBorder();
  46. //Get the colors
  47. ColorI leftColor = (leftProfile) ? leftProfile->getBorderColor(state) : ColorI();
  48. ColorI rightColor = (rightProfile) ? rightProfile->getBorderColor(state) : ColorI();
  49. ColorI topColor = (topProfile) ? topProfile->getBorderColor(state) : ColorI();
  50. ColorI bottomColor = (bottomProfile) ? bottomProfile->getBorderColor(state) : ColorI();
  51. S32 leftSize = (leftProfile) ? leftProfile->getBorder(state) : 0;
  52. S32 rightSize = (rightProfile) ? rightProfile->getBorder(state) : 0;
  53. S32 topSize = (topProfile) ? topProfile->getBorder(state) : 0;
  54. S32 bottomSize = (bottomProfile) ? bottomProfile->getBorder(state) : 0;
  55. //Get the inner rect
  56. RectI innerRect = RectI(bounds.point.x + leftSize, bounds.point.y + topSize, (bounds.extent.x - leftSize) - rightSize, (bounds.extent.y - topSize) - bottomSize);
  57. //Draw the fill
  58. if(fillColor.alpha > 0)
  59. {
  60. S32 fillWidth = innerRect.extent.x + ((leftProfile && leftProfile->mUnderfill) ? leftSize : 0) + ((rightProfile && rightProfile->mUnderfill) ? rightSize : 0);
  61. S32 fillHeight = innerRect.extent.y + ((topProfile && topProfile->mUnderfill) ? topSize : 0) + ((bottomProfile && bottomProfile->mUnderfill) ? bottomSize : 0);
  62. RectI fillRect = RectI((leftProfile && leftProfile->mUnderfill) ? bounds.point.x : innerRect.point.x,
  63. (topProfile && topProfile->mUnderfill) ? bounds.point.y : innerRect.point.y, fillWidth, fillHeight);
  64. dglDrawRectFill(fillRect, fillColor);
  65. }
  66. //Draw the borders
  67. //Points for outer bounds starting top left and traveling counter-clockwise
  68. Point2I p1 = Point2I(bounds.point);
  69. Point2I p2 = Point2I(bounds.point.x, bounds.point.y + bounds.extent.y);
  70. Point2I p3 = Point2I(bounds.point.x + bounds.extent.x, bounds.point.y + bounds.extent.y);
  71. Point2I p4 = Point2I(bounds.point.x + bounds.extent.x, bounds.point.y);
  72. //Points for inner bounds starting top left and traveling counter-clockwise
  73. Point2I p5 = Point2I(innerRect.point);
  74. Point2I p6 = Point2I(innerRect.point.x, innerRect.point.y + innerRect.extent.y);
  75. Point2I p7 = Point2I(innerRect.point.x + innerRect.extent.x, innerRect.point.y + innerRect.extent.y);
  76. Point2I p8 = Point2I(innerRect.point.x + innerRect.extent.x, innerRect.point.y);
  77. if (leftSize > 0)
  78. {
  79. dglDrawQuadFill(p1, p2, p6, p5, leftColor);
  80. }
  81. if (rightSize > 0)
  82. {
  83. dglDrawQuadFill(p8, p7, p3, p4, rightColor);
  84. }
  85. if (topSize > 0)
  86. {
  87. dglDrawQuadFill(p1, p5, p8, p4, topColor);
  88. }
  89. if (bottomSize > 0)
  90. {
  91. dglDrawQuadFill(p6, p2, p3, p7, bottomColor);
  92. }
  93. }
  94. void renderBorderedCircle(Point2I &center, S32 radius, GuiControlProfile *profile, GuiControlState state)
  95. {
  96. //Get the border profiles
  97. GuiBorderProfile *borderProfile = profile->mBorderDefault;
  98. //Get the colors
  99. ColorI fillColor = profile->getFillColor(state);
  100. ColorI borderColor = (profile->mBorderDefault) ? profile->mBorderDefault->getBorderColor(state) : ColorI();
  101. S32 borderSize = (profile->mBorderDefault) ? profile->mBorderDefault->getBorder(state) : 0;
  102. //Draw the fill
  103. S32 fillRadius = (profile->mBorderDefault && profile->mBorderDefault->mUnderfill) ? radius : radius - borderSize;
  104. dglDrawCircleFill(center, (F32)fillRadius, fillColor);
  105. //Draw the border
  106. dglDrawCircle(center, (F32)radius, borderColor, (F32)borderSize);
  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 renderSizableBitmapBordersFilled(RectI &bounds, S32 baseMultiplier, GuiControlProfile *profile)
  111. {
  112. S32 NumBitmaps = 9;
  113. S32 startIndex = NumBitmaps * (baseMultiplier - 1);
  114. renderSizableBitmapBordersFilledIndex(bounds, startIndex, profile);
  115. }
  116. // DAW: Render out the sizable bitmap borders based on a multiplier into the bitmap array
  117. // Based on the 'Skinnable GUI Controls in TGE' resource by Justin DuJardin
  118. void renderSizableBitmapBordersFilledIndex(RectI &bounds, S32 startIndex, GuiControlProfile *profile)
  119. {
  120. // DAW: Indices into the bitmap array
  121. S32 NumBitmaps = 9;
  122. S32 BorderTopLeft = startIndex;
  123. S32 BorderTop = 1 + BorderTopLeft;
  124. S32 BorderTopRight = 2 + BorderTopLeft;
  125. S32 BorderLeft = 3 + BorderTopLeft;
  126. S32 Fill = 4 + BorderTopLeft;
  127. S32 BorderRight = 5 + BorderTopLeft;
  128. S32 BorderBottomLeft = 6 + BorderTopLeft;
  129. S32 BorderBottom = 7 + BorderTopLeft;
  130. S32 BorderBottomRight = 8 + BorderTopLeft;
  131. dglClearBitmapModulation();
  132. if (profile->mBitmapArrayRects.size() >= (NumBitmaps + startIndex))
  133. {
  134. RectI destRect;
  135. RectI stretchRect;
  136. RectI* mBitmapBounds = profile->mBitmapArrayRects.address();
  137. // Draw all corners first.
  138. //top left border
  139. dglDrawBitmapSR(profile->mTextureHandle, Point2I(bounds.point.x, bounds.point.y), mBitmapBounds[BorderTopLeft]);
  140. //top right border
  141. dglDrawBitmapSR(profile->mTextureHandle, Point2I(bounds.point.x + bounds.extent.x - mBitmapBounds[BorderTopRight].extent.x, bounds.point.y), mBitmapBounds[BorderTopRight]);
  142. //bottom left border
  143. dglDrawBitmapSR(profile->mTextureHandle, Point2I(bounds.point.x, bounds.point.y + bounds.extent.y - mBitmapBounds[BorderBottomLeft].extent.y), mBitmapBounds[BorderBottomLeft]);
  144. //bottom right border
  145. dglDrawBitmapSR(profile->mTextureHandle, Point2I(
  146. bounds.point.x + bounds.extent.x - mBitmapBounds[BorderBottomRight].extent.x,
  147. bounds.point.y + bounds.extent.y - mBitmapBounds[BorderBottomRight].extent.y),
  148. mBitmapBounds[BorderBottomRight]);
  149. // End drawing corners
  150. // Begin drawing sides and top stretched borders
  151. //start with top line stretch
  152. destRect.point.x = bounds.point.x + mBitmapBounds[BorderTopLeft].extent.x;
  153. destRect.extent.x = bounds.extent.x - mBitmapBounds[BorderTopRight].extent.x - mBitmapBounds[BorderTopLeft].extent.x;
  154. destRect.extent.y = mBitmapBounds[BorderTop].extent.y;
  155. destRect.point.y = bounds.point.y;
  156. //stretch it
  157. stretchRect = mBitmapBounds[BorderTop];
  158. stretchRect.inset(1, 0);
  159. //draw it
  160. dglDrawBitmapStretchSR(profile->mTextureHandle, destRect, stretchRect);
  161. //bottom line stretch
  162. destRect.point.x = bounds.point.x + mBitmapBounds[BorderBottomLeft].extent.x;
  163. destRect.extent.x = bounds.extent.x - mBitmapBounds[BorderBottomRight].extent.x - mBitmapBounds[BorderBottomLeft].extent.x;
  164. destRect.extent.y = mBitmapBounds[BorderBottom].extent.y;
  165. destRect.point.y = bounds.point.y + bounds.extent.y - mBitmapBounds[BorderBottom].extent.y;
  166. //stretch it
  167. stretchRect = mBitmapBounds[BorderBottom];
  168. stretchRect.inset(1, 0);
  169. //draw it
  170. dglDrawBitmapStretchSR(profile->mTextureHandle, destRect, stretchRect);
  171. //left line stretch
  172. destRect.point.x = bounds.point.x;
  173. destRect.extent.x = mBitmapBounds[BorderLeft].extent.x;
  174. destRect.extent.y = bounds.extent.y - mBitmapBounds[BorderTopLeft].extent.y - mBitmapBounds[BorderBottomLeft].extent.y;
  175. destRect.point.y = bounds.point.y + mBitmapBounds[BorderTopLeft].extent.y;
  176. //stretch it
  177. stretchRect = mBitmapBounds[BorderLeft];
  178. stretchRect.inset(0, 1);
  179. //draw it
  180. dglDrawBitmapStretchSR(profile->mTextureHandle, destRect, stretchRect);
  181. //right line stretch
  182. destRect.point.x = bounds.point.x + bounds.extent.x - mBitmapBounds[BorderRight].extent.x;
  183. destRect.extent.x = mBitmapBounds[BorderRight].extent.x;
  184. destRect.extent.y = bounds.extent.y - mBitmapBounds[BorderTopRight].extent.y - mBitmapBounds[BorderBottomRight].extent.y;
  185. destRect.point.y = bounds.point.y + mBitmapBounds[BorderTopRight].extent.y;
  186. //stretch it
  187. stretchRect = mBitmapBounds[BorderRight];
  188. stretchRect.inset(0, 1);
  189. //draw it
  190. dglDrawBitmapStretchSR(profile->mTextureHandle, destRect, stretchRect);
  191. //fill stretch
  192. destRect.point.x = bounds.point.x + mBitmapBounds[BorderLeft].extent.x;
  193. destRect.extent.x = (bounds.extent.x) - mBitmapBounds[BorderLeft].extent.x - mBitmapBounds[BorderRight].extent.x;
  194. destRect.extent.y = bounds.extent.y - mBitmapBounds[BorderTop].extent.y - mBitmapBounds[BorderBottom].extent.y;
  195. destRect.point.y = bounds.point.y + mBitmapBounds[BorderTop].extent.y;
  196. //stretch it
  197. stretchRect = mBitmapBounds[Fill];
  198. stretchRect.inset(1, 1);
  199. //draw it
  200. dglDrawBitmapStretchSR(profile->mTextureHandle, destRect, stretchRect);
  201. // End drawing sides and top stretched borders
  202. }
  203. }
  204. // DAW: Render out the fixed bitmap borders based on a multiplier into the bitmap array
  205. // It renders left and right caps, with a sizable fill area in the middle to reach
  206. // the x extent. It does not stretch in the y direction.
  207. void renderFixedBitmapBordersFilled(RectI &bounds, S32 baseMultiplier, GuiControlProfile *profile)
  208. {
  209. // DAW: Indices into the bitmap array
  210. S32 NumBitmaps = 3;
  211. S32 BorderLeft = NumBitmaps * baseMultiplier - NumBitmaps;
  212. S32 Fill = 1 + BorderLeft;
  213. S32 BorderRight = 2 + BorderLeft;
  214. dglClearBitmapModulation();
  215. if(profile->mBitmapArrayRects.size() >= (NumBitmaps * baseMultiplier))
  216. {
  217. RectI destRect;
  218. RectI stretchRect;
  219. RectI* mBitmapBounds = profile->mBitmapArrayRects.address();
  220. // Draw all corners first.
  221. //left border
  222. dglDrawBitmapSR(profile->mTextureHandle,Point2I(bounds.point.x,bounds.point.y),mBitmapBounds[BorderLeft]);
  223. //right border
  224. dglDrawBitmapSR(profile->mTextureHandle,Point2I(bounds.point.x + bounds.extent.x - mBitmapBounds[BorderRight].extent.x,bounds.point.y),mBitmapBounds[BorderRight]);
  225. // End drawing corners
  226. // Begin drawing fill
  227. //fill stretch
  228. destRect.point.x = bounds.point.x + mBitmapBounds[BorderLeft].extent.x;
  229. destRect.extent.x = (bounds.extent.x) - mBitmapBounds[BorderLeft].extent.x - mBitmapBounds[BorderRight].extent.x;
  230. destRect.extent.y = mBitmapBounds[Fill].extent.y;
  231. destRect.point.y = bounds.point.y;
  232. //stretch it
  233. stretchRect = mBitmapBounds[Fill];
  234. stretchRect.inset(1,0);
  235. //draw it
  236. dglDrawBitmapStretchSR(profile->mTextureHandle,destRect,stretchRect);
  237. // End drawing fill
  238. }
  239. }
  240. // DAW: Render out the fixed bitmap borders based on a multiplier into the bitmap array
  241. // It renders left and right caps, with a sizable fill area in the middle to reach
  242. // the x extent. It does not stretch in the y direction.
  243. void renderFixedBitmapBordersFilledIndex(RectI &bounds, S32 startIndex, GuiControlProfile *profile)
  244. {
  245. // DAW: Indices into the bitmap array
  246. S32 NumBitmaps = 3;
  247. S32 BorderLeft = startIndex;
  248. S32 Fill = 1 + startIndex;
  249. S32 BorderRight = 2 + startIndex;
  250. dglClearBitmapModulation();
  251. if(profile->mBitmapArrayRects.size() >= (startIndex + NumBitmaps))
  252. {
  253. RectI destRect;
  254. RectI stretchRect;
  255. RectI* mBitmapBounds = profile->mBitmapArrayRects.address();
  256. // Draw all corners first.
  257. //left border
  258. dglDrawBitmapSR(profile->mTextureHandle,Point2I(bounds.point.x,bounds.point.y),mBitmapBounds[BorderLeft]);
  259. //right border
  260. dglDrawBitmapSR(profile->mTextureHandle,Point2I(bounds.point.x + bounds.extent.x - mBitmapBounds[BorderRight].extent.x,bounds.point.y),mBitmapBounds[BorderRight]);
  261. // End drawing corners
  262. // Begin drawing fill
  263. //fill stretch
  264. destRect.point.x = bounds.point.x + mBitmapBounds[BorderLeft].extent.x;
  265. destRect.extent.x = (bounds.extent.x) - mBitmapBounds[BorderLeft].extent.x - mBitmapBounds[BorderRight].extent.x;
  266. destRect.extent.y = mBitmapBounds[Fill].extent.y;
  267. destRect.point.y = bounds.point.y;
  268. //stretch it
  269. stretchRect = mBitmapBounds[Fill];
  270. stretchRect.inset(1,0);
  271. //draw it
  272. dglDrawBitmapStretchSR(profile->mTextureHandle,destRect,stretchRect);
  273. // End drawing fill
  274. }
  275. }
  276. // DAW: Render out the fixed bitmap borders based on a multiplier into the bitmap array
  277. // It renders left and right caps, with a sizable fill area in the middle to reach
  278. // the x extent. It does not stretch in the y direction.
  279. void renderFixedBitmapBordersStretchYFilled(RectI &bounds, S32 baseMultiplier, GuiControlProfile *profile)
  280. {
  281. // DAW: Indices into the bitmap array
  282. S32 NumBitmaps = 3;
  283. S32 BorderLeft = NumBitmaps * baseMultiplier - NumBitmaps;
  284. S32 Fill = 1 + BorderLeft;
  285. S32 BorderRight = 2 + BorderLeft;
  286. dglClearBitmapModulation();
  287. if(profile->mBitmapArrayRects.size() >= (NumBitmaps * baseMultiplier))
  288. {
  289. RectI destRect;
  290. RectI stretchRect;
  291. RectI* mBitmapBounds = profile->mBitmapArrayRects.address();
  292. // Draw all corners first.
  293. //left border
  294. dglDrawBitmapStretchSR(profile->mTextureHandle, RectI( bounds.point.x, bounds.point.y, mBitmapBounds[BorderLeft].extent.x, bounds.extent.y ), mBitmapBounds[BorderLeft] );
  295. //right border
  296. 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] );
  297. // End drawing corners
  298. // Begin drawing fill
  299. //fill stretch
  300. destRect.point.x = bounds.point.x + mBitmapBounds[BorderLeft].extent.x;
  301. destRect.extent.x = (bounds.extent.x) - mBitmapBounds[BorderLeft].extent.x - mBitmapBounds[BorderRight].extent.x;
  302. destRect.extent.y = bounds.extent.y;
  303. destRect.point.y = bounds.point.y;
  304. //stretch it
  305. stretchRect = mBitmapBounds[Fill];
  306. stretchRect.inset(1,0);
  307. //draw it
  308. dglDrawBitmapStretchSR(profile->mTextureHandle, destRect, stretchRect);
  309. // End drawing fill
  310. }
  311. }