guiRectHandles.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "console/console.h"
  23. #include "console/consoleTypes.h"
  24. #include "console/engineAPI.h"
  25. #include "gfx/gfxDevice.h"
  26. #include "gfx/gfxDrawUtil.h"
  27. #include "gui/editor/guiRectHandles.h"
  28. IMPLEMENT_CONOBJECT(GuiRectHandles);
  29. ConsoleDocClass( GuiRectHandles,
  30. "@brief Draws a box with handles for the user to manipulate.\n\n"
  31. "Editor use only.\n\n"
  32. "@internal"
  33. );
  34. //--------------------------------------------------------------------------
  35. GuiRectHandles::GuiRectHandles() : GuiControl()
  36. {
  37. mHandleRect.set(0.0f, 0.0f, 1.0f, 1.0f);
  38. mHandleSize = 10;
  39. mUseCustomColor = false;
  40. mHandleColor.set(100,100,100);
  41. mHitHandle = 0;
  42. }
  43. GuiRectHandles::~GuiRectHandles()
  44. {
  45. }
  46. //--------------------------------------------------------------------------
  47. void GuiRectHandles::initPersistFields()
  48. {
  49. addField("handleRect", TypeRectF, Offset(mHandleRect, GuiRectHandles), "RectF of handle's box." );
  50. addField("handleSize", TypeS32, Offset(mHandleSize, GuiRectHandles), "Size of handles in pixels." );
  51. addField("useCustomColor", TypeBool, Offset(mUseCustomColor, GuiRectHandles), "Use given custom color for handles." );
  52. addField("handleColor", TypeColorI, Offset(mHandleColor, GuiRectHandles), "Use given custom color for handles." );
  53. Parent::initPersistFields();
  54. }
  55. //--------------------------------------------------------------------------
  56. void GuiRectHandles::onMouseUp(const GuiEvent &event)
  57. {
  58. mHitHandle = 0;
  59. }
  60. void GuiRectHandles::onMouseDown(const GuiEvent &event)
  61. {
  62. // The handles range from 0-1, so scale to fit within the
  63. // control's bounds.
  64. const Point2I& extent = getExtent();
  65. Point2I pos(extent.x*mHandleRect.point.x, extent.y*mHandleRect.point.y);
  66. Point2I size(extent.x*mHandleRect.extent.x, extent.y*mHandleRect.extent.y);
  67. RectI box(pos, size);
  68. Point2I localMousePoint = globalToLocalCoord(event.mousePoint);
  69. // Check if mouse is within handle rect
  70. if(!box.pointInRect(localMousePoint))
  71. {
  72. mHitHandle = 0;
  73. return;
  74. }
  75. Point2I normalizedMouse = localMousePoint - pos;
  76. Point2I halfSize = size / 2;
  77. S32 halfHandleSize = mHandleSize / 2;
  78. if(normalizedMouse.y < mHandleSize)
  79. {
  80. // Top handles
  81. if(normalizedMouse.x < mHandleSize)
  82. mHitHandle = 1;
  83. else if(normalizedMouse.x >= (size.x-mHandleSize))
  84. mHitHandle = 3;
  85. else if(normalizedMouse.x >= (halfSize.x-halfHandleSize) && normalizedMouse.x < (halfSize.x+halfHandleSize))
  86. mHitHandle = 2;
  87. }
  88. else if(normalizedMouse.y >= (size.y-mHandleSize))
  89. {
  90. // Bottom handles
  91. if(normalizedMouse.x < mHandleSize)
  92. mHitHandle = 7;
  93. else if(normalizedMouse.x >= (size.x-mHandleSize))
  94. mHitHandle = 5;
  95. else if(normalizedMouse.x >= (halfSize.x-halfHandleSize) && normalizedMouse.x < (halfSize.x+halfHandleSize))
  96. mHitHandle = 6;
  97. }
  98. else if(normalizedMouse.y >= (halfSize.y-halfHandleSize) && normalizedMouse.y < (halfSize.y+halfHandleSize))
  99. {
  100. // Middle handles
  101. if(normalizedMouse.x < mHandleSize)
  102. mHitHandle = 8;
  103. else if(normalizedMouse.x >= (size.x-mHandleSize))
  104. mHitHandle = 4;
  105. else if(normalizedMouse.x >= (halfSize.x-halfHandleSize) && normalizedMouse.x < (halfSize.x+halfHandleSize))
  106. mHitHandle = 9;
  107. }
  108. mHitPoint = localMousePoint;
  109. }
  110. void GuiRectHandles::onMouseDragged(const GuiEvent &event)
  111. {
  112. if(mHitHandle == 0)
  113. return;
  114. // The handles range from 0-1, so scale to fit within the
  115. // control's bounds.
  116. const Point2I& extent = getExtent();
  117. Point2I localMousePoint = globalToLocalCoord(event.mousePoint);
  118. Point2I diffI = localMousePoint - mHitPoint;
  119. Point2F diffF(diffI.x/F32(extent.x), diffI.y/F32(extent.y));
  120. RectF box(mHandleRect);
  121. bool postMoveExtentX = false;
  122. bool postMoveExtentY = false;
  123. bool keepExtent = false;
  124. switch(mHitHandle)
  125. {
  126. case 1:
  127. {
  128. // Top left
  129. box.point += diffF;
  130. postMoveExtentX = true;
  131. postMoveExtentY = true;
  132. break;
  133. }
  134. case 2:
  135. {
  136. // Top middle
  137. box.point.y += diffF.y;
  138. postMoveExtentY = true;
  139. break;
  140. }
  141. case 3:
  142. {
  143. // Top right
  144. box.point.y += diffF.y;
  145. box.extent.x += diffF.x;
  146. postMoveExtentY = true;
  147. break;
  148. }
  149. case 4:
  150. {
  151. // Middle right
  152. box.extent.x += diffF.x;
  153. break;
  154. }
  155. case 5:
  156. {
  157. // Bottom right
  158. box.extent += diffF;
  159. break;
  160. }
  161. case 6:
  162. {
  163. // Bottom middle
  164. box.extent.y += diffF.y;
  165. break;
  166. }
  167. case 7:
  168. {
  169. // Bottom left
  170. box.point.x += diffF.x;
  171. box.extent.y += diffF.y;
  172. postMoveExtentX = true;
  173. break;
  174. }
  175. case 8:
  176. {
  177. // Middle left
  178. box.point.x += diffF.x;
  179. postMoveExtentX = true;
  180. break;
  181. }
  182. case 9:
  183. {
  184. // Centre
  185. box.point += diffF;
  186. keepExtent = true;
  187. break;
  188. }
  189. default:
  190. break;
  191. }
  192. // Position limits
  193. if(box.point.x < 0.0f)
  194. box.point.x = 0.0f;
  195. else if(box.point.x > 1.0f)
  196. box.point.x = 1.0f;
  197. if(box.point.y < 0.0f)
  198. box.point.y = 0.0f;
  199. else if(box.point.y > 1.0f)
  200. box.point.y = 1.0f;
  201. // Move any extent to counter a change in handle position. Do this
  202. // after the limits above to make sure the extent doesn't accidentally
  203. // grow when the position is clamped.
  204. if(postMoveExtentX)
  205. box.extent.x += mHandleRect.point.x - box.point.x;
  206. if(postMoveExtentY)
  207. box.extent.y += mHandleRect.point.y - box.point.y;
  208. // Extent limits
  209. if(box.extent.x < 0.0f)
  210. box.extent.x = 0.0f;
  211. else if(box.extent.x > 1.0f)
  212. box.extent.x = 1.0f;
  213. if(box.point.x+box.extent.x > 1.0f)
  214. {
  215. if(keepExtent)
  216. box.point.x = 1.0f-box.extent.x;
  217. else
  218. box.extent.x = 1.0f-box.point.x;
  219. }
  220. if(box.extent.y < 0.0f)
  221. box.extent.y = 0.0f;
  222. else if(box.extent.y > 1.0f)
  223. box.extent.y = 1.0f;
  224. if(box.point.y+box.extent.y > 1.0f)
  225. {
  226. if(keepExtent)
  227. box.point.y = 1.0f-box.extent.y;
  228. else
  229. box.extent.y = 1.0f-box.point.y;
  230. }
  231. mHandleRect = box;
  232. mHitPoint = localMousePoint;
  233. if( isMethod( "onHandleRectChange" ) )
  234. Con::executef(this, "onHandleRectChange" );
  235. }
  236. //--------------------------------------------------------------------------
  237. void GuiRectHandles::onRender(Point2I offset, const RectI &updateRect)
  238. {
  239. Parent::onRender( offset, updateRect );
  240. ColorI handleColor = mProfile->mBorderColor;
  241. if(mUseCustomColor)
  242. handleColor = mHandleColor;
  243. // The handles range from 0-1, so scale to fit within the
  244. // control's bounds.
  245. const Point2I& extent = getExtent();
  246. Point2I pos(extent.x*mHandleRect.point.x, extent.y*mHandleRect.point.y);
  247. Point2I size(extent.x*mHandleRect.extent.x, extent.y*mHandleRect.extent.y);
  248. RectI box(offset+pos, size);
  249. GFXDrawUtil* drawUtil = GFX->getDrawUtil();
  250. // Draw border
  251. drawUtil->drawRect(box, handleColor);
  252. // Draw each handle
  253. Point2I handleSize(mHandleSize, mHandleSize);
  254. RectI handleRect(box.point, handleSize);
  255. drawUtil->drawRectFill(handleRect, handleColor); // Upper left
  256. handleRect.point = Point2I(box.point.x+size.x-handleSize.x, box.point.y);
  257. drawUtil->drawRectFill(handleRect, handleColor); // Upper right
  258. handleRect.point = Point2I(box.point.x, box.point.y+size.y-handleSize.y);
  259. drawUtil->drawRectFill(handleRect, handleColor); // Lower left
  260. handleRect.point = Point2I(box.point.x+size.x-handleSize.x, box.point.y+size.y-handleSize.y);
  261. drawUtil->drawRectFill(handleRect, handleColor); // Lower right
  262. Point2I halfSize = size / 2;
  263. Point2I halfHandleSize = handleSize / 2;
  264. handleRect.point = Point2I(box.point.x+halfSize.x-halfHandleSize.x, box.point.y);
  265. drawUtil->drawRectFill(handleRect, handleColor); // Upper middle
  266. handleRect.point = Point2I(box.point.x+halfSize.x-halfHandleSize.x, box.point.y+size.y-handleSize.y);
  267. drawUtil->drawRectFill(handleRect, handleColor); // Lower middle
  268. handleRect.point = Point2I(box.point.x, box.point.y+halfSize.y-halfHandleSize.y);
  269. drawUtil->drawRectFill(handleRect, handleColor); // Left middle
  270. handleRect.point = Point2I(box.point.x+size.x-handleSize.x, box.point.y+halfSize.y-halfHandleSize.y);
  271. drawUtil->drawRectFill(handleRect, handleColor); // Right middle
  272. handleRect.point = Point2I(box.point.x+halfSize.x-halfHandleSize.x, box.point.y+halfSize.y-halfHandleSize.y);
  273. drawUtil->drawRectFill(handleRect, handleColor); // Middle
  274. renderChildControls(offset, updateRect);
  275. }