ElementHandle.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // Modified by uniquejack
  28. #include "precompiled.h"
  29. #include "ElementHandle.h"
  30. #include "../../Include/Rocket/Core/ElementDocument.h"
  31. #include "../../Include/Rocket/Core/ElementUtilities.h"
  32. #include "../../Include/Rocket/Core/Property.h"
  33. #include "../../Include/Rocket/Core/Event.h"
  34. namespace Rocket {
  35. namespace Core {
  36. ElementHandle::ElementHandle(const String& tag) : Element(tag), drag_start(0, 0)
  37. {
  38. // Make sure we can be dragged!
  39. SetProperty(DRAG, DRAG);
  40. move_clip = NULL;
  41. move_target = NULL;
  42. size_target = NULL;
  43. initialised = false;
  44. }
  45. ElementHandle::~ElementHandle()
  46. {
  47. }
  48. void ElementHandle::OnAttributeChange(const PropertyNameList& changed_attributes)
  49. {
  50. Element::OnAttributeChange(changed_attributes);
  51. // Reset initialised state if the move or size targets have changed.
  52. if (changed_attributes.find("move_target") != changed_attributes.end() ||
  53. changed_attributes.find("size_target") != changed_attributes.end() ||
  54. changed_attributes.find("move_clip") != changed_attributes.end())
  55. {
  56. initialised = false;
  57. move_target = NULL;
  58. size_target = NULL;
  59. }
  60. }
  61. void ElementHandle::ProcessEvent(Event& event)
  62. {
  63. Element::ProcessEvent(event);
  64. if (event.GetTargetElement() == this)
  65. {
  66. // Lazy initialisation.
  67. if (!initialised && GetOwnerDocument())
  68. {
  69. String move_target_name = GetAttribute<String>("move_target", "");
  70. if (!move_target_name.Empty())
  71. move_target = GetElementById(move_target_name);
  72. String size_target_name = GetAttribute<String>("size_target", "");
  73. if (!size_target_name.Empty())
  74. size_target = GetElementById(size_target_name);
  75. String move_clip_name = GetAttribute<String>("move_clip", "");
  76. if (!move_clip_name.Empty())
  77. move_clip = GetElementById(move_clip_name);
  78. initialised = true;
  79. }
  80. if (event == DRAGSTART)
  81. {
  82. // Store the drag starting position
  83. drag_start.x = event.GetParameter< int >("mouse_x", 0);
  84. drag_start.y = event.GetParameter< int >("mouse_y", 0);
  85. // Store current element position and size
  86. if (move_target)
  87. {
  88. move_original_position.x = move_target->GetOffsetLeft();
  89. move_original_position.y = move_target->GetOffsetTop();
  90. }
  91. if (size_target)
  92. size_original_size = size_target->GetBox().GetSize(Box::CONTENT);
  93. }
  94. else if (event == DRAG)
  95. {
  96. // Work out the delta
  97. int x = event.GetParameter< int >("mouse_x", 0) - drag_start.x;
  98. int y = event.GetParameter< int >("mouse_y", 0) - drag_start.y;
  99. // Update the move and size objects
  100. if (move_target)
  101. {
  102. Vector2f pos, size;
  103. if (move_clip)
  104. {
  105. pos = move_clip->GetBox().GetPosition();
  106. size = move_clip->GetBox().GetSize();
  107. }
  108. Vector2f move;
  109. move.x = (move_original_position.x + x);
  110. move.y = (move_original_position.y + y);
  111. Vector2f object_size = move_target->GetBox().GetSize();
  112. if (object_size.x <= size.x && object_size.y <= size.y)
  113. {
  114. if (move.x < pos.x)
  115. move.x = pos.x;
  116. else if (move.x + object_size.x > pos.x + size.x)
  117. move.x = pos.x + size.x - object_size.x;
  118. if (move.y < pos.y)
  119. move.y = pos.y;
  120. else if (move.y + object_size.y > pos.y + size.y)
  121. move.y = pos.y + size.y - object_size.y;
  122. }
  123. move_target->SetProperty(LEFT, Property(Math::RealToInteger(move.x), Property::PX));
  124. move_target->SetProperty(TOP, Property(Math::RealToInteger(move.y), Property::PX));
  125. }
  126. if (size_target)
  127. {
  128. const Property *margin_top, *margin_bottom, *margin_left, *margin_right;
  129. size_target->GetMarginProperties(&margin_top, &margin_bottom, &margin_left, &margin_right);
  130. // Check if we have auto-margins; if so, they have to be set to the current margins.
  131. if (margin_top->unit == Property::KEYWORD)
  132. size_target->SetProperty(MARGIN_TOP, Property((float) Math::RealToInteger(size_target->GetBox().GetEdge(Box::MARGIN, Box::TOP)), Property::PX));
  133. if (margin_right->unit == Property::KEYWORD)
  134. size_target->SetProperty(MARGIN_RIGHT, Property((float) Math::RealToInteger(size_target->GetBox().GetEdge(Box::MARGIN, Box::RIGHT)), Property::PX));
  135. if (margin_bottom->unit == Property::KEYWORD)
  136. size_target->SetProperty(MARGIN_BOTTOM, Property((float) Math::RealToInteger(size_target->GetBox().GetEdge(Box::MARGIN, Box::BOTTOM)), Property::PX));
  137. if (margin_left->unit == Property::KEYWORD)
  138. size_target->SetProperty(MARGIN_LEFT, Property((float) Math::RealToInteger(size_target->GetBox().GetEdge(Box::MARGIN, Box::LEFT)), Property::PX));
  139. int new_x = Math::RealToInteger(size_original_size.x + x);
  140. int new_y = Math::RealToInteger(size_original_size.y + y);
  141. size_target->SetProperty(WIDTH, Property(Math::Max< float >((float) new_x, 0), Property::PX));
  142. size_target->SetProperty(HEIGHT, Property(Math::Max< float >((float) new_y, 0), Property::PX));
  143. }
  144. Dictionary parameters;
  145. parameters.Set("handle_x", x);
  146. parameters.Set("handle_y", y);
  147. DispatchEvent("handledrag", parameters);
  148. }
  149. }
  150. }
  151. }
  152. }