guiDragAndDropCtrl.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "gui/containers/guiDragAndDropCtrl.h"
  23. #include "gui/guiCanvas.h"
  24. #include "guiDragAndDropCtrl_ScriptBinding.h"
  25. IMPLEMENT_CONOBJECT(GuiDragAndDropCtrl);
  26. GuiDragAndDropCtrl::GuiDragAndDropCtrl()
  27. {
  28. mDeleteOnMouseUp = false;
  29. setField("profile", "GuiDefaultProfile");
  30. }
  31. void GuiDragAndDropCtrl::initPersistFields()
  32. {
  33. Parent::initPersistFields();
  34. addField("deleteOnMouseUp", TypeBool, Offset(mDeleteOnMouseUp, GuiDragAndDropCtrl));
  35. }
  36. void GuiDragAndDropCtrl::startDragging(Point2I offset)
  37. {
  38. GuiCanvas* canvas = getRoot();
  39. AssertFatal(canvas, "DragAndDropControl wasn't added to the gui before the drag started.");
  40. if (canvas->getMouseLockedControl())
  41. {
  42. GuiEvent event;
  43. canvas->getMouseLockedControl()->onTouchLeave(event);
  44. canvas->mouseUnlock(canvas->getMouseLockedControl());
  45. }
  46. canvas->mouseLock(this);
  47. canvas->setFirstResponder(this);
  48. mOffset = offset;
  49. mLastTarget=NULL;
  50. }
  51. void GuiDragAndDropCtrl::onTouchDown(const GuiEvent& event)
  52. {
  53. startDragging(event.mousePoint - mBounds.point);
  54. }
  55. void GuiDragAndDropCtrl::onTouchDragged(const GuiEvent& event)
  56. {
  57. mBounds.point = event.mousePoint - mOffset;
  58. // Allow the control under the drag to react to a potential drop
  59. GuiControl* enterTarget = findDragTarget(event.mousePoint, "onControlDragEnter");
  60. if(mLastTarget != enterTarget)
  61. {
  62. sendDragEvent(mLastTarget, "onControlDragExit");
  63. sendDragEvent(enterTarget, "onControlDragEnter");
  64. mLastTarget = enterTarget;
  65. }
  66. GuiControl* dragTarget = findDragTarget(event.mousePoint, "onControlDragged");
  67. sendDragEvent(dragTarget, "onControlDragged");
  68. }
  69. void GuiDragAndDropCtrl::onTouchUp(const GuiEvent& event)
  70. {
  71. mouseUnlock();
  72. GuiControl* target = findDragTarget(event.mousePoint, "onControlDropped");
  73. sendDragEvent(target, "onControlDropped");
  74. if (mDeleteOnMouseUp)
  75. deleteObject();
  76. }
  77. void GuiDragAndDropCtrl::sendDragEvent(GuiControl* target, const char* event)
  78. {
  79. if(!target || !target->isMethod(event))
  80. return;
  81. char position[32];
  82. Point2I dropPoint = mBounds.point + (mBounds.extent / 2);
  83. dSprintf(position, 32, "%d %d", dropPoint.x, dropPoint.y);
  84. Con::executef(target, 3, event, Con::getIntArg(at(0)->getId()), position);
  85. }
  86. GuiControl* GuiDragAndDropCtrl::findDragTarget(Point2I mousePoint, const char* method)
  87. {
  88. // If there are any children and we have a parent.
  89. GuiControl* parent = getParent();
  90. if (size() && parent)
  91. {
  92. mVisible = false;
  93. GuiControl* dropControl = parent->findHitControl(mousePoint);
  94. mVisible = true;
  95. while( dropControl )
  96. {
  97. if (dropControl->isMethod(method))
  98. return dropControl;
  99. else
  100. dropControl = dropControl->getParent();
  101. }
  102. }
  103. return NULL;
  104. }