guiDragAndDropCtrl.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. IMPLEMENT_CONOBJECT(GuiDragAndDropControl);
  25. void GuiDragAndDropControl::initPersistFields()
  26. {
  27. Parent::initPersistFields();
  28. addField("deleteOnMouseUp", TypeBool, Offset(mDeleteOnMouseUp, GuiDragAndDropControl));
  29. }
  30. ConsoleMethod(GuiDragAndDropControl, startDragging, void, 2, 4, "( int x, int y ) "
  31. "@param x X coordinate relative to the point on the object on which you are clicking\n"
  32. "@param y Y coordinate relative to the point on the object on which you are clicking\n"
  33. "@note If no point is passed, or only one coordinate is passed, the method defaults to (0,0)")
  34. {
  35. Point2I offset = Point2I(0, 0);
  36. if (argc > 3)
  37. {
  38. offset.x = dAtoi(argv[2]);
  39. offset.y = dAtoi(argv[3]);
  40. }
  41. object->startDragging(offset);
  42. }
  43. void GuiDragAndDropControl::startDragging(Point2I offset)
  44. {
  45. GuiCanvas* canvas = getRoot();
  46. AssertFatal(canvas, "DragAndDropControl wasn't added to the gui before the drag started.");
  47. if (canvas->getMouseLockedControl())
  48. {
  49. GuiEvent event;
  50. canvas->getMouseLockedControl()->onMouseLeave(event);
  51. canvas->mouseUnlock(canvas->getMouseLockedControl());
  52. }
  53. canvas->mouseLock(this);
  54. canvas->setFirstResponder(this);
  55. mOffset = offset;
  56. mLastTarget=NULL;
  57. }
  58. void GuiDragAndDropControl::onMouseDown(const GuiEvent& event)
  59. {
  60. startDragging(event.mousePoint - mBounds.point);
  61. }
  62. void GuiDragAndDropControl::onMouseDragged(const GuiEvent& event)
  63. {
  64. mBounds.point = event.mousePoint - mOffset;
  65. // Allow the control under the drag to react to a potential drop
  66. GuiControl* enterTarget = findDragTarget(event.mousePoint, "onControlDragEnter");
  67. if(mLastTarget != enterTarget)
  68. {
  69. sendDragEvent(mLastTarget, "onControlDragExit");
  70. sendDragEvent(enterTarget, "onControlDragEnter");
  71. mLastTarget = enterTarget;
  72. }
  73. GuiControl* dragTarget = findDragTarget(event.mousePoint, "onControlDragged");
  74. sendDragEvent(dragTarget, "onControlDragged");
  75. }
  76. void GuiDragAndDropControl::onMouseUp(const GuiEvent& event)
  77. {
  78. mouseUnlock();
  79. GuiControl* target = findDragTarget(event.mousePoint, "onControlDropped");
  80. sendDragEvent(target, "onControlDropped");
  81. if (mDeleteOnMouseUp)
  82. deleteObject();
  83. }
  84. void GuiDragAndDropControl::sendDragEvent(GuiControl* target, const char* event)
  85. {
  86. if(!target || !target->isMethod(event))
  87. return;
  88. char position[32];
  89. Point2I dropPoint = mBounds.point + (mBounds.extent / 2);
  90. dSprintf(position, 32, "%d %d", dropPoint.x, dropPoint.y);
  91. Con::executef(target, 3, event, Con::getIntArg(at(0)->getId()), position);
  92. }
  93. GuiControl* GuiDragAndDropControl::findDragTarget(Point2I mousePoint, const char* method)
  94. {
  95. // If there are any children and we have a parent.
  96. GuiControl* parent = getParent();
  97. if (size() && parent)
  98. {
  99. mVisible = false;
  100. GuiControl* dropControl = parent->findHitControl(mousePoint);
  101. mVisible = true;
  102. while( dropControl )
  103. {
  104. if (dropControl->isMethod(method))
  105. return dropControl;
  106. else
  107. dropControl = dropControl->getParent();
  108. }
  109. }
  110. return NULL;
  111. }