Window.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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.HasChild("resizeborder"))
  53. SetResizeBorder(element.GetChild("resizeborder").GetIntRect("value"));
  54. if (element.HasChild("movable"))
  55. SetMovable(element.GetChild("movable").GetBool("enable"));
  56. if (element.HasChild("resizable"))
  57. SetResizable(element.GetChild("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. default:
  122. break;
  123. }
  124. ValidatePosition();
  125. SetCursorShape(dragMode_, cursor);
  126. }
  127. void Window::OnDragEnd(const IntVector2& position, const IntVector2& screenPosition, Cursor* cursor)
  128. {
  129. dragMode_ = DRAG_NONE;
  130. }
  131. void Window::SetMovable(bool enable)
  132. {
  133. movable_ = enable;
  134. }
  135. void Window::SetResizable(bool enable)
  136. {
  137. resizable_ = enable;
  138. }
  139. void Window::SetResizeBorder(const IntRect& rect)
  140. {
  141. resizeBorder_.left_ = Max(rect.left_, 0);
  142. resizeBorder_.top_ = Max(rect.top_, 0);
  143. resizeBorder_.right_ = Max(rect.right_, 0);
  144. resizeBorder_.bottom_ = Max(rect.bottom_, 0);
  145. }
  146. WindowDragMode Window::GetDragMode(const IntVector2& position) const
  147. {
  148. WindowDragMode mode = DRAG_NONE;
  149. // Top row
  150. if (position.y_ < resizeBorder_.top_)
  151. {
  152. if (movable_)
  153. mode = DRAG_MOVE;
  154. if (resizable_)
  155. {
  156. mode = DRAG_RESIZE_TOP;
  157. if (position.x_ < resizeBorder_.left_)
  158. mode = DRAG_RESIZE_TOPLEFT;
  159. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  160. mode = DRAG_RESIZE_TOPRIGHT;
  161. }
  162. }
  163. // Bottom row
  164. else if (position.y_ >= GetHeight() - resizeBorder_.bottom_)
  165. {
  166. if (movable_)
  167. mode = DRAG_MOVE;
  168. if (resizable_)
  169. {
  170. mode = DRAG_RESIZE_BOTTOM;
  171. if (position.x_ < resizeBorder_.left_)
  172. mode = DRAG_RESIZE_BOTTOMLEFT;
  173. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  174. mode = DRAG_RESIZE_BOTTOMRIGHT;
  175. }
  176. }
  177. // Middle
  178. else
  179. {
  180. if (movable_)
  181. mode = DRAG_MOVE;
  182. if (resizable_)
  183. {
  184. if (position.x_ < resizeBorder_.left_)
  185. mode = DRAG_RESIZE_LEFT;
  186. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  187. mode = DRAG_RESIZE_RIGHT;
  188. }
  189. }
  190. return mode;
  191. }
  192. void Window::SetCursorShape(WindowDragMode mode, Cursor* cursor) const
  193. {
  194. switch (mode)
  195. {
  196. case DRAG_RESIZE_TOP:
  197. case DRAG_RESIZE_BOTTOM:
  198. cursor->SetShape(CS_RESIZEVERTICAL);
  199. break;
  200. case DRAG_RESIZE_LEFT:
  201. case DRAG_RESIZE_RIGHT:
  202. cursor->SetShape(CS_RESIZEHORIZONTAL);
  203. break;
  204. case DRAG_RESIZE_TOPRIGHT:
  205. case DRAG_RESIZE_BOTTOMLEFT:
  206. cursor->SetShape(CS_RESIZEDIAGONAL_TOPRIGHT);
  207. break;
  208. case DRAG_RESIZE_TOPLEFT:
  209. case DRAG_RESIZE_BOTTOMRIGHT:
  210. cursor->SetShape(CS_RESIZEDIAGONAL_TOPLEFT);
  211. break;
  212. }
  213. }
  214. void Window::ValidatePosition()
  215. {
  216. // Check that window does not go more than halfway outside its parent in either dimension
  217. if (!parent_)
  218. return;
  219. const IntVector2& parentSize = parent_->GetSize();
  220. IntVector2 position = GetPosition();
  221. IntVector2 halfSize = GetSize() / 2;
  222. position.x_ = Clamp(position.x_, -halfSize.x_, parentSize.x_ - halfSize.x_);
  223. position.y_ = Clamp(position.y_, -halfSize.y_, parentSize.y_ - halfSize.y_);
  224. SetPosition(position);
  225. }
  226. bool Window::CheckAlignment() const
  227. {
  228. // Only top left-alignment is supported for move and resize
  229. if (GetHorizontalAlignment() == HA_LEFT && GetVerticalAlignment() == VA_TOP)
  230. return true;
  231. else
  232. return false;
  233. }