Window.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "Cursor.h"
  26. #include "InputEvents.h"
  27. #include "Window.h"
  28. #include "DebugNew.h"
  29. static const int DEFAULT_RESIZE_BORDER = 4;
  30. OBJECTTYPESTATIC(Window);
  31. Window::Window(Context* context) :
  32. BorderImage(context),
  33. movable_(false),
  34. resizable_(false),
  35. resizeBorder_(DEFAULT_RESIZE_BORDER, DEFAULT_RESIZE_BORDER, DEFAULT_RESIZE_BORDER, DEFAULT_RESIZE_BORDER),
  36. dragMode_(DRAG_NONE)
  37. {
  38. bringToFront_ = true;
  39. clipChildren_ = true;
  40. active_ = true;
  41. }
  42. Window::~Window()
  43. {
  44. }
  45. void Window::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<Window>();
  48. }
  49. void Window::SetStyle(const XMLElement& element)
  50. {
  51. BorderImage::SetStyle(element);
  52. if (element.HasChildElement("resizeborder"))
  53. SetResizeBorder(element.GetChildElement("resizeborder").GetIntRect("value"));
  54. if (element.HasChildElement("movable"))
  55. SetMovable(element.GetChildElement("movable").GetBool("enable"));
  56. if (element.HasChildElement("resizable"))
  57. SetResizable(element.GetChildElement("resizable").GetBool("enable"));
  58. }
  59. void Window::OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  60. {
  61. if (dragMode_ == DRAG_NONE)
  62. {
  63. WindowDragMode mode = GetDragMode(position);
  64. SetCursorShape(mode, cursor);
  65. }
  66. else
  67. SetCursorShape(dragMode_, cursor);
  68. }
  69. void Window::OnDragStart(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  70. {
  71. if ((buttons != MOUSEB_LEFT) || (!CheckAlignment()))
  72. {
  73. dragMode_ = DRAG_NONE;
  74. return;
  75. }
  76. dragStartCursor_ = screenPosition;
  77. dragStartPosition_ = GetPosition();
  78. dragStartSize_ = GetSize();
  79. dragMode_ = GetDragMode(position);
  80. SetCursorShape(dragMode_, cursor);
  81. }
  82. void Window::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  83. {
  84. if (dragMode_ == DRAG_NONE)
  85. return;
  86. IntVector2 delta = screenPosition - dragStartCursor_;
  87. switch (dragMode_)
  88. {
  89. case DRAG_MOVE:
  90. SetPosition(dragStartPosition_ + delta);
  91. break;
  92. case DRAG_RESIZE_TOPLEFT:
  93. SetPosition(dragStartPosition_ + delta);
  94. SetSize(dragStartSize_ - delta);
  95. break;
  96. case DRAG_RESIZE_TOP:
  97. SetPosition(dragStartPosition_.x_, dragStartPosition_.y_ + delta.y_);
  98. SetSize(dragStartSize_.x_, dragStartSize_.y_ - delta.y_);
  99. break;
  100. case DRAG_RESIZE_TOPRIGHT:
  101. SetPosition(dragStartPosition_.x_, dragStartPosition_.y_ + delta.y_);
  102. SetSize(dragStartSize_.x_ + delta.x_, dragStartSize_.y_ - delta.y_);
  103. break;
  104. case DRAG_RESIZE_RIGHT:
  105. SetSize(dragStartSize_.x_ + delta.x_, dragStartSize_.y_);
  106. break;
  107. case DRAG_RESIZE_BOTTOMRIGHT:
  108. SetSize(dragStartSize_ + delta);
  109. break;
  110. case DRAG_RESIZE_BOTTOM:
  111. SetSize(dragStartSize_.x_, dragStartSize_.y_ + delta.y_);
  112. break;
  113. case DRAG_RESIZE_BOTTOMLEFT:
  114. SetPosition(dragStartPosition_.x_ + delta.x_, dragStartPosition_.y_);
  115. SetSize(dragStartSize_.x_ - delta.x_, dragStartSize_.y_ + delta.y_);
  116. break;
  117. case DRAG_RESIZE_LEFT:
  118. SetPosition(dragStartPosition_.x_ + delta.x_, dragStartPosition_.y_);
  119. SetSize(dragStartSize_.x_ - delta.x_, dragStartSize_.y_);
  120. break;
  121. }
  122. ValidatePosition();
  123. SetCursorShape(dragMode_, cursor);
  124. }
  125. void Window::OnDragEnd(const IntVector2& position, const IntVector2& screenPosition, Cursor* cursor)
  126. {
  127. dragMode_ = DRAG_NONE;
  128. }
  129. void Window::SetMovable(bool enable)
  130. {
  131. movable_ = enable;
  132. }
  133. void Window::SetResizable(bool enable)
  134. {
  135. resizable_ = enable;
  136. }
  137. void Window::SetResizeBorder(const IntRect& rect)
  138. {
  139. resizeBorder_.left_ = Max(rect.left_, 0);
  140. resizeBorder_.top_ = Max(rect.top_, 0);
  141. resizeBorder_.right_ = Max(rect.right_, 0);
  142. resizeBorder_.bottom_ = Max(rect.bottom_, 0);
  143. }
  144. WindowDragMode Window::GetDragMode(const IntVector2& position) const
  145. {
  146. WindowDragMode mode = DRAG_NONE;
  147. // Top row
  148. if (position.y_ < resizeBorder_.top_)
  149. {
  150. if (movable_)
  151. mode = DRAG_MOVE;
  152. if (resizable_)
  153. {
  154. mode = DRAG_RESIZE_TOP;
  155. if (position.x_ < resizeBorder_.left_)
  156. mode = DRAG_RESIZE_TOPLEFT;
  157. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  158. mode = DRAG_RESIZE_TOPRIGHT;
  159. }
  160. }
  161. // Bottom row
  162. else if (position.y_ >= GetHeight() - resizeBorder_.bottom_)
  163. {
  164. if (movable_)
  165. mode = DRAG_MOVE;
  166. if (resizable_)
  167. {
  168. mode = DRAG_RESIZE_BOTTOM;
  169. if (position.x_ < resizeBorder_.left_)
  170. mode = DRAG_RESIZE_BOTTOMLEFT;
  171. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  172. mode = DRAG_RESIZE_BOTTOMRIGHT;
  173. }
  174. }
  175. // Middle
  176. else
  177. {
  178. if (movable_)
  179. mode = DRAG_MOVE;
  180. if (resizable_)
  181. {
  182. if (position.x_ < resizeBorder_.left_)
  183. mode = DRAG_RESIZE_LEFT;
  184. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  185. mode = DRAG_RESIZE_RIGHT;
  186. }
  187. }
  188. return mode;
  189. }
  190. void Window::SetCursorShape(WindowDragMode mode, Cursor* cursor) const
  191. {
  192. switch (mode)
  193. {
  194. case DRAG_RESIZE_TOP:
  195. case DRAG_RESIZE_BOTTOM:
  196. cursor->SetShape(CS_RESIZEVERTICAL);
  197. break;
  198. case DRAG_RESIZE_LEFT:
  199. case DRAG_RESIZE_RIGHT:
  200. cursor->SetShape(CS_RESIZEHORIZONTAL);
  201. break;
  202. case DRAG_RESIZE_TOPRIGHT:
  203. case DRAG_RESIZE_BOTTOMLEFT:
  204. cursor->SetShape(CS_RESIZEDIAGONAL_TOPRIGHT);
  205. break;
  206. case DRAG_RESIZE_TOPLEFT:
  207. case DRAG_RESIZE_BOTTOMRIGHT:
  208. cursor->SetShape(CS_RESIZEDIAGONAL_TOPLEFT);
  209. break;
  210. }
  211. }
  212. void Window::ValidatePosition()
  213. {
  214. // Check that window does not go more than halfway outside its parent in either dimension
  215. if (!parent_)
  216. return;
  217. const IntVector2& parentSize = parent_->GetSize();
  218. IntVector2 position = GetPosition();
  219. IntVector2 halfSize = GetSize() / 2;
  220. position.x_ = Clamp(position.x_, -halfSize.x_, parentSize.x_ - halfSize.x_);
  221. position.y_ = Clamp(position.y_, -halfSize.y_, parentSize.y_ - halfSize.y_);
  222. SetPosition(position);
  223. }
  224. bool Window::CheckAlignment() const
  225. {
  226. // Only top left-alignment is supported for move and resize
  227. if ((GetHorizontalAlignment() == HA_LEFT) && (GetVerticalAlignment() == VA_TOP))
  228. return true;
  229. else
  230. return false;
  231. }