Window.cpp 8.1 KB

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