guiRectHandles.cpp 9.9 KB

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