Window.cpp 8.1 KB

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