Window.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "Cursor.h"
  25. #include "InputEvents.h"
  26. #include "Window.h"
  27. #include "DebugNew.h"
  28. namespace Urho3D
  29. {
  30. static const int DEFAULT_RESIZE_BORDER = 4;
  31. OBJECTTYPESTATIC(Window);
  32. Window::Window(Context* context) :
  33. BorderImage(context),
  34. movable_(false),
  35. resizable_(false),
  36. resizeBorder_(DEFAULT_RESIZE_BORDER, DEFAULT_RESIZE_BORDER, DEFAULT_RESIZE_BORDER, DEFAULT_RESIZE_BORDER),
  37. dragMode_(DRAG_NONE)
  38. {
  39. bringToFront_ = true;
  40. clipChildren_ = true;
  41. active_ = true;
  42. }
  43. Window::~Window()
  44. {
  45. }
  46. void Window::RegisterObject(Context* context)
  47. {
  48. context->RegisterFactory<Window>();
  49. REF_ACCESSOR_ATTRIBUTE(Window, VAR_INTRECT, "Resize Border", GetResizeBorder, SetResizeBorder, IntRect, IntRect(DEFAULT_RESIZE_BORDER, \
  50. DEFAULT_RESIZE_BORDER, DEFAULT_RESIZE_BORDER, DEFAULT_RESIZE_BORDER), AM_FILE);
  51. ACCESSOR_ATTRIBUTE(Window, VAR_BOOL, "Is Movable", IsMovable, SetMovable, bool, false, AM_FILE);
  52. ACCESSOR_ATTRIBUTE(Window, VAR_BOOL, "Is Resizable", IsResizable, SetResizable, bool, false, AM_FILE);
  53. COPY_BASE_ATTRIBUTES(Window, BorderImage);
  54. }
  55. void Window::OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  56. {
  57. if (dragMode_ == DRAG_NONE)
  58. {
  59. WindowDragMode mode = GetDragMode(position);
  60. SetCursorShape(mode, cursor);
  61. }
  62. else
  63. SetCursorShape(dragMode_, cursor);
  64. }
  65. void Window::OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  66. {
  67. if (buttons != MOUSEB_LEFT || !CheckAlignment())
  68. {
  69. dragMode_ = DRAG_NONE;
  70. return;
  71. }
  72. dragBeginCursor_ = screenPosition;
  73. dragBeginPosition_ = GetPosition();
  74. dragBeginSize_ = GetSize();
  75. dragMode_ = GetDragMode(position);
  76. SetCursorShape(dragMode_, cursor);
  77. }
  78. void Window::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  79. {
  80. if (dragMode_ == DRAG_NONE)
  81. return;
  82. IntVector2 delta = screenPosition - dragBeginCursor_;
  83. const IntVector2& position_ = GetPosition();
  84. const IntVector2& size_ = GetSize();
  85. const IntVector2& minSize_ = GetMinSize();
  86. const IntVector2& maxSize_ = GetMaxSize();
  87. switch (dragMode_)
  88. {
  89. case DRAG_MOVE:
  90. SetPosition(dragBeginPosition_ + delta);
  91. break;
  92. case DRAG_RESIZE_TOPLEFT:
  93. SetPosition(Clamp(dragBeginPosition_.x_ + delta.x_, position_.x_ - (maxSize_.x_ - size_.x_), position_.x_ + (size_.x_ - minSize_.x_)),
  94. Clamp(dragBeginPosition_.y_ + delta.y_, position_.y_ - (maxSize_.y_ - size_.y_), position_.y_ + (size_.y_ - minSize_.y_)));
  95. SetSize(dragBeginSize_ - delta);
  96. break;
  97. case DRAG_RESIZE_TOP:
  98. SetPosition(dragBeginPosition_.x_, Clamp(dragBeginPosition_.y_ + delta.y_, position_.y_ - (maxSize_.y_ - size_.y_), position_.y_ + (size_.y_ - minSize_.y_)));
  99. SetSize(dragBeginSize_.x_, dragBeginSize_.y_ - delta.y_);
  100. break;
  101. case DRAG_RESIZE_TOPRIGHT:
  102. SetPosition(dragBeginPosition_.x_, dragBeginPosition_.y_ + delta.y_);
  103. SetSize(dragBeginSize_.x_ + delta.x_, dragBeginSize_.y_ - delta.y_);
  104. break;
  105. case DRAG_RESIZE_RIGHT:
  106. SetSize(dragBeginSize_.x_ + delta.x_, dragBeginSize_.y_);
  107. break;
  108. case DRAG_RESIZE_BOTTOMRIGHT:
  109. SetSize(dragBeginSize_ + delta);
  110. break;
  111. case DRAG_RESIZE_BOTTOM:
  112. SetSize(dragBeginSize_.x_, dragBeginSize_.y_ + delta.y_);
  113. break;
  114. case DRAG_RESIZE_BOTTOMLEFT:
  115. SetPosition(Clamp(dragBeginPosition_.x_ + delta.x_, position_.x_ - (maxSize_.x_ - size_.x_), position_.x_ + (size_.x_ - minSize_.x_)), dragBeginPosition_.y_);
  116. SetSize(dragBeginSize_.x_ - delta.x_, dragBeginSize_.y_ + delta.y_);
  117. break;
  118. case DRAG_RESIZE_LEFT:
  119. SetPosition(Clamp(dragBeginPosition_.x_ + delta.x_, position_.x_ - (maxSize_.x_ - size_.x_), position_.x_ + (size_.x_ - minSize_.x_)), dragBeginPosition_.y_);
  120. SetSize(dragBeginSize_.x_ - delta.x_, dragBeginSize_.y_);
  121. break;
  122. default:
  123. break;
  124. }
  125. ValidatePosition();
  126. SetCursorShape(dragMode_, cursor);
  127. }
  128. void Window::OnDragEnd(const IntVector2& position, const IntVector2& screenPosition, Cursor* cursor)
  129. {
  130. dragMode_ = DRAG_NONE;
  131. }
  132. void Window::SetMovable(bool enable)
  133. {
  134. movable_ = enable;
  135. }
  136. void Window::SetResizable(bool enable)
  137. {
  138. resizable_ = enable;
  139. }
  140. void Window::SetResizeBorder(const IntRect& rect)
  141. {
  142. resizeBorder_.left_ = Max(rect.left_, 0);
  143. resizeBorder_.top_ = Max(rect.top_, 0);
  144. resizeBorder_.right_ = Max(rect.right_, 0);
  145. resizeBorder_.bottom_ = Max(rect.bottom_, 0);
  146. }
  147. WindowDragMode Window::GetDragMode(const IntVector2& position) const
  148. {
  149. WindowDragMode mode = DRAG_NONE;
  150. // Top row
  151. if (position.y_ < resizeBorder_.top_)
  152. {
  153. if (movable_)
  154. mode = DRAG_MOVE;
  155. if (resizable_)
  156. {
  157. mode = DRAG_RESIZE_TOP;
  158. if (position.x_ < resizeBorder_.left_)
  159. mode = DRAG_RESIZE_TOPLEFT;
  160. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  161. mode = DRAG_RESIZE_TOPRIGHT;
  162. }
  163. }
  164. // Bottom row
  165. else if (position.y_ >= GetHeight() - resizeBorder_.bottom_)
  166. {
  167. if (movable_)
  168. mode = DRAG_MOVE;
  169. if (resizable_)
  170. {
  171. mode = DRAG_RESIZE_BOTTOM;
  172. if (position.x_ < resizeBorder_.left_)
  173. mode = DRAG_RESIZE_BOTTOMLEFT;
  174. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  175. mode = DRAG_RESIZE_BOTTOMRIGHT;
  176. }
  177. }
  178. // Middle
  179. else
  180. {
  181. if (movable_)
  182. mode = DRAG_MOVE;
  183. if (resizable_)
  184. {
  185. if (position.x_ < resizeBorder_.left_)
  186. mode = DRAG_RESIZE_LEFT;
  187. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  188. mode = DRAG_RESIZE_RIGHT;
  189. }
  190. }
  191. return mode;
  192. }
  193. void Window::SetCursorShape(WindowDragMode mode, Cursor* cursor) const
  194. {
  195. if (!cursor)
  196. return;
  197. switch (mode)
  198. {
  199. case DRAG_RESIZE_TOP:
  200. case DRAG_RESIZE_BOTTOM:
  201. cursor->SetShape(CS_RESIZEVERTICAL);
  202. break;
  203. case DRAG_RESIZE_LEFT:
  204. case DRAG_RESIZE_RIGHT:
  205. cursor->SetShape(CS_RESIZEHORIZONTAL);
  206. break;
  207. case DRAG_RESIZE_TOPRIGHT:
  208. case DRAG_RESIZE_BOTTOMLEFT:
  209. cursor->SetShape(CS_RESIZEDIAGONAL_TOPRIGHT);
  210. break;
  211. case DRAG_RESIZE_TOPLEFT:
  212. case DRAG_RESIZE_BOTTOMRIGHT:
  213. cursor->SetShape(CS_RESIZEDIAGONAL_TOPLEFT);
  214. break;
  215. default:
  216. break;
  217. }
  218. }
  219. void Window::ValidatePosition()
  220. {
  221. // Check that window does not go more than halfway outside its parent in either dimension
  222. if (!parent_)
  223. return;
  224. const IntVector2& parentSize = parent_->GetSize();
  225. IntVector2 position = GetPosition();
  226. IntVector2 halfSize = GetSize() / 2;
  227. position.x_ = Clamp(position.x_, -halfSize.x_, parentSize.x_ - halfSize.x_);
  228. position.y_ = Clamp(position.y_, -halfSize.y_, parentSize.y_ - halfSize.y_);
  229. SetPosition(position);
  230. }
  231. bool Window::CheckAlignment() const
  232. {
  233. // Only top left-alignment is supported for move and resize
  234. if (GetHorizontalAlignment() == HA_LEFT && GetVerticalAlignment() == VA_TOP)
  235. return true;
  236. else
  237. return false;
  238. }
  239. }