ElementHandle.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "ElementHandle.h"
  29. #include "../../Include/Rocket/Core/ElementDocument.h"
  30. #include "../../Include/Rocket/Core/ElementUtilities.h"
  31. #include "../../Include/Rocket/Core/Property.h"
  32. #include "../../Include/Rocket/Core/Event.h"
  33. namespace Rocket {
  34. namespace Core {
  35. ElementHandle::ElementHandle(const String& tag) : Element(tag), drag_start(0, 0)
  36. {
  37. // Make sure we can be dragged!
  38. SetProperty(DRAG, DRAG);
  39. move_target = NULL;
  40. size_target = NULL;
  41. initialised = false;
  42. }
  43. ElementHandle::~ElementHandle()
  44. {
  45. }
  46. void ElementHandle::OnAttributeChange(const PropertyNameList& changed_attributes)
  47. {
  48. Element::OnAttributeChange(changed_attributes);
  49. // Reset initialised state if the move or size targets have changed.
  50. if (changed_attributes.find("move_target") != changed_attributes.end() ||
  51. changed_attributes.find("size_target") != changed_attributes.end())
  52. {
  53. initialised = false;
  54. move_target = NULL;
  55. size_target = NULL;
  56. }
  57. }
  58. void ElementHandle::ProcessEvent(Event& event)
  59. {
  60. Element::ProcessEvent(event);
  61. if (event.GetTargetElement() == this)
  62. {
  63. // Lazy initialisation.
  64. if (!initialised && GetOwnerDocument())
  65. {
  66. String move_target_name = GetAttribute<String>("move_target", "");
  67. if (!move_target_name.empty())
  68. move_target = GetElementById(move_target_name);
  69. String size_target_name = GetAttribute<String>("size_target", "");
  70. if (!size_target_name.empty())
  71. size_target = GetElementById(size_target_name);
  72. initialised = true;
  73. }
  74. if (event == DRAGSTART)
  75. {
  76. // Store the drag starting position
  77. drag_start.x = event.GetParameter< int >("mouse_x", 0);
  78. drag_start.y = event.GetParameter< int >("mouse_y", 0);
  79. // Store current element position and size
  80. if (move_target)
  81. {
  82. move_original_position.x = move_target->GetOffsetLeft();
  83. move_original_position.y = move_target->GetOffsetTop();
  84. }
  85. if (size_target)
  86. size_original_size = size_target->GetBox().GetSize(Box::CONTENT);
  87. }
  88. else if (event == DRAG)
  89. {
  90. // Work out the delta
  91. int x = event.GetParameter< int >("mouse_x", 0) - drag_start.x;
  92. int y = event.GetParameter< int >("mouse_y", 0) - drag_start.y;
  93. // Update the move and size objects
  94. if (move_target)
  95. {
  96. move_target->SetProperty(LEFT, Property(Math::RealToInteger(move_original_position.x + x), Property::PX));
  97. move_target->SetProperty(TOP, Property(Math::RealToInteger(move_original_position.y + y), Property::PX));
  98. }
  99. if (size_target)
  100. {
  101. const Property *margin_top, *margin_bottom, *margin_left, *margin_right;
  102. size_target->GetMarginProperties(&margin_top, &margin_bottom, &margin_left, &margin_right);
  103. // Check if we have auto-margins; if so, they have to be set to the current margins.
  104. if (margin_top->unit == Property::KEYWORD)
  105. size_target->SetProperty(MARGIN_TOP, Property((float) Math::RealToInteger(size_target->GetBox().GetEdge(Box::MARGIN, Box::TOP)), Property::PX));
  106. if (margin_right->unit == Property::KEYWORD)
  107. size_target->SetProperty(MARGIN_RIGHT, Property((float) Math::RealToInteger(size_target->GetBox().GetEdge(Box::MARGIN, Box::RIGHT)), Property::PX));
  108. if (margin_bottom->unit == Property::KEYWORD)
  109. size_target->SetProperty(MARGIN_BOTTOM, Property((float) Math::RealToInteger(size_target->GetBox().GetEdge(Box::MARGIN, Box::BOTTOM)), Property::PX));
  110. if (margin_left->unit == Property::KEYWORD)
  111. size_target->SetProperty(MARGIN_LEFT, Property((float) Math::RealToInteger(size_target->GetBox().GetEdge(Box::MARGIN, Box::LEFT)), Property::PX));
  112. int new_x = Math::RealToInteger(size_original_size.x + x);
  113. int new_y = Math::RealToInteger(size_original_size.y + y);
  114. size_target->SetProperty(WIDTH, Property(Math::Max< float >((float) new_x, 0), Property::PX));
  115. size_target->SetProperty(HEIGHT, Property(Math::Max< float >((float) new_y, 0), Property::PX));
  116. }
  117. Dictionary parameters;
  118. parameters.emplace("handle_x", x);
  119. parameters.emplace("handle_y", y);
  120. DispatchEvent("handledrag", parameters);
  121. }
  122. }
  123. }
  124. }
  125. }