Window.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 "UI.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. modal_(false),
  40. modalShadeColor_(Color::TRANSPARENT),
  41. modalFrameColor_(Color::TRANSPARENT),
  42. modalFrameSize_(IntVector2::ZERO)
  43. {
  44. bringToFront_ = true;
  45. clipChildren_ = true;
  46. enabled_ = true;
  47. }
  48. Window::~Window()
  49. {
  50. }
  51. void Window::RegisterObject(Context* context)
  52. {
  53. context->RegisterFactory<Window>();
  54. COPY_BASE_ATTRIBUTES(Window, BorderImage);
  55. UPDATE_ATTRIBUTE_DEFAULT_VALUE(Window, "Bring To Front", true);
  56. UPDATE_ATTRIBUTE_DEFAULT_VALUE(Window, "Clip Children", true);
  57. UPDATE_ATTRIBUTE_DEFAULT_VALUE(Window, "Is Enabled", true);
  58. REF_ACCESSOR_ATTRIBUTE(Window, VAR_INTRECT, "Resize Border", GetResizeBorder, SetResizeBorder, IntRect, IntRect(DEFAULT_RESIZE_BORDER, \
  59. DEFAULT_RESIZE_BORDER, DEFAULT_RESIZE_BORDER, DEFAULT_RESIZE_BORDER), AM_FILE);
  60. ACCESSOR_ATTRIBUTE(Window, VAR_BOOL, "Is Movable", IsMovable, SetMovable, bool, false, AM_FILE);
  61. ACCESSOR_ATTRIBUTE(Window, VAR_BOOL, "Is Resizable", IsResizable, SetResizable, bool, false, AM_FILE);
  62. ACCESSOR_ATTRIBUTE(Window, VAR_BOOL, "Is Modal", IsModal, SetModal, bool, false, AM_FILE);
  63. REF_ACCESSOR_ATTRIBUTE(Window, VAR_COLOR, "Modal Shade Color", GetModalShadeColor, SetModalShadeColor, Color, Color::TRANSPARENT, AM_FILE);
  64. REF_ACCESSOR_ATTRIBUTE(Window, VAR_COLOR, "Modal Frame Color", GetModalFrameColor, SetModalFrameColor, Color, Color::TRANSPARENT, AM_FILE);
  65. REF_ACCESSOR_ATTRIBUTE(Window, VAR_INTVECTOR2, "Modal Frame Size", GetModalFrameSize, SetModalFrameSize, IntVector2, IntVector2::ZERO, AM_FILE);
  66. }
  67. void Window::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  68. {
  69. if (modal_)
  70. {
  71. // Modal shade
  72. if (modalShadeColor_ != Color::TRANSPARENT)
  73. {
  74. UIElement* rootElement = GetRoot();
  75. const IntVector2& rootSize = rootElement->GetSize();
  76. UIBatch batch(rootElement, BLEND_ALPHA, IntRect(0, 0, rootSize.x_, rootSize.y_), 0, &vertexData);
  77. batch.AddQuad(0, 0, rootSize.x_, rootSize.y_, 0, 0, 0, 0, modalShadeColor_);
  78. UIBatch::AddOrMerge(batch, batches);
  79. }
  80. // Modal frame
  81. if (modalFrameColor_ != Color::TRANSPARENT && modalFrameSize_ != IntVector2::ZERO)
  82. {
  83. UIBatch batch(this, BLEND_ALPHA, currentScissor, 0, &vertexData);
  84. int x = GetIndentWidth();
  85. IntVector2 size = GetSize();
  86. size.x_ -= x;
  87. batch.AddQuad(x - modalFrameSize_.x_, -modalFrameSize_.y_, size.x_ + 2 * modalFrameSize_.x_, size.y_ + 2 * modalFrameSize_.y_, 0, 0, 0, 0, modalFrameColor_);
  88. UIBatch::AddOrMerge(batch, batches);
  89. }
  90. }
  91. BorderImage::GetBatches(batches, vertexData, currentScissor);
  92. }
  93. void Window::OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  94. {
  95. if (dragMode_ == DRAG_NONE)
  96. {
  97. WindowDragMode mode = GetDragMode(position);
  98. SetCursorShape(mode, cursor);
  99. }
  100. else
  101. SetCursorShape(dragMode_, cursor);
  102. }
  103. void Window::OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  104. {
  105. if (buttons != MOUSEB_LEFT || !CheckAlignment())
  106. {
  107. dragMode_ = DRAG_NONE;
  108. return;
  109. }
  110. dragBeginCursor_ = screenPosition;
  111. dragBeginPosition_ = GetPosition();
  112. dragBeginSize_ = GetSize();
  113. dragMode_ = GetDragMode(position);
  114. SetCursorShape(dragMode_, cursor);
  115. }
  116. void Window::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  117. {
  118. if (dragMode_ == DRAG_NONE)
  119. return;
  120. IntVector2 delta = screenPosition - dragBeginCursor_;
  121. const IntVector2& position_ = GetPosition();
  122. const IntVector2& size_ = GetSize();
  123. const IntVector2& minSize_ = GetMinSize();
  124. const IntVector2& maxSize_ = GetMaxSize();
  125. switch (dragMode_)
  126. {
  127. case DRAG_MOVE:
  128. SetPosition(dragBeginPosition_ + delta);
  129. break;
  130. case DRAG_RESIZE_TOPLEFT:
  131. SetPosition(Clamp(dragBeginPosition_.x_ + delta.x_, position_.x_ - (maxSize_.x_ - size_.x_), position_.x_ + (size_.x_ - minSize_.x_)),
  132. Clamp(dragBeginPosition_.y_ + delta.y_, position_.y_ - (maxSize_.y_ - size_.y_), position_.y_ + (size_.y_ - minSize_.y_)));
  133. SetSize(dragBeginSize_ - delta);
  134. break;
  135. case DRAG_RESIZE_TOP:
  136. SetPosition(dragBeginPosition_.x_, Clamp(dragBeginPosition_.y_ + delta.y_, position_.y_ - (maxSize_.y_ - size_.y_), position_.y_ + (size_.y_ - minSize_.y_)));
  137. SetSize(dragBeginSize_.x_, dragBeginSize_.y_ - delta.y_);
  138. break;
  139. case DRAG_RESIZE_TOPRIGHT:
  140. SetPosition(dragBeginPosition_.x_, dragBeginPosition_.y_ + delta.y_);
  141. SetSize(dragBeginSize_.x_ + delta.x_, dragBeginSize_.y_ - delta.y_);
  142. break;
  143. case DRAG_RESIZE_RIGHT:
  144. SetSize(dragBeginSize_.x_ + delta.x_, dragBeginSize_.y_);
  145. break;
  146. case DRAG_RESIZE_BOTTOMRIGHT:
  147. SetSize(dragBeginSize_ + delta);
  148. break;
  149. case DRAG_RESIZE_BOTTOM:
  150. SetSize(dragBeginSize_.x_, dragBeginSize_.y_ + delta.y_);
  151. break;
  152. case DRAG_RESIZE_BOTTOMLEFT:
  153. SetPosition(Clamp(dragBeginPosition_.x_ + delta.x_, position_.x_ - (maxSize_.x_ - size_.x_), position_.x_ + (size_.x_ - minSize_.x_)), dragBeginPosition_.y_);
  154. SetSize(dragBeginSize_.x_ - delta.x_, dragBeginSize_.y_ + delta.y_);
  155. break;
  156. case DRAG_RESIZE_LEFT:
  157. SetPosition(Clamp(dragBeginPosition_.x_ + delta.x_, position_.x_ - (maxSize_.x_ - size_.x_), position_.x_ + (size_.x_ - minSize_.x_)), dragBeginPosition_.y_);
  158. SetSize(dragBeginSize_.x_ - delta.x_, dragBeginSize_.y_);
  159. break;
  160. default:
  161. break;
  162. }
  163. ValidatePosition();
  164. SetCursorShape(dragMode_, cursor);
  165. }
  166. void Window::OnDragEnd(const IntVector2& position, const IntVector2& screenPosition, Cursor* cursor)
  167. {
  168. dragMode_ = DRAG_NONE;
  169. }
  170. void Window::SetMovable(bool enable)
  171. {
  172. movable_ = enable;
  173. }
  174. void Window::SetResizable(bool enable)
  175. {
  176. resizable_ = enable;
  177. }
  178. void Window::SetResizeBorder(const IntRect& rect)
  179. {
  180. resizeBorder_.left_ = Max(rect.left_, 0);
  181. resizeBorder_.top_ = Max(rect.top_, 0);
  182. resizeBorder_.right_ = Max(rect.right_, 0);
  183. resizeBorder_.bottom_ = Max(rect.bottom_, 0);
  184. }
  185. void Window::SetModal(bool modal)
  186. {
  187. UI* ui = GetSubsystem<UI>();
  188. // UI may be null at shutdown if for example a script was holding a reference to this window
  189. if (ui && ui->SetModalElement(this, modal))
  190. modal_ = modal;
  191. }
  192. void Window::SetModalShadeColor(const Color& color)
  193. {
  194. modalShadeColor_ = color;
  195. }
  196. void Window::SetModalFrameColor(const Color& color)
  197. {
  198. modalFrameColor_ = color;
  199. }
  200. void Window::SetModalFrameSize(const IntVector2& size)
  201. {
  202. modalFrameSize_ = size;
  203. }
  204. WindowDragMode Window::GetDragMode(const IntVector2& position) const
  205. {
  206. WindowDragMode mode = DRAG_NONE;
  207. // Top row
  208. if (position.y_ < resizeBorder_.top_)
  209. {
  210. if (movable_)
  211. mode = DRAG_MOVE;
  212. if (resizable_)
  213. {
  214. mode = DRAG_RESIZE_TOP;
  215. if (position.x_ < resizeBorder_.left_)
  216. mode = DRAG_RESIZE_TOPLEFT;
  217. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  218. mode = DRAG_RESIZE_TOPRIGHT;
  219. }
  220. }
  221. // Bottom row
  222. else if (position.y_ >= GetHeight() - resizeBorder_.bottom_)
  223. {
  224. if (movable_)
  225. mode = DRAG_MOVE;
  226. if (resizable_)
  227. {
  228. mode = DRAG_RESIZE_BOTTOM;
  229. if (position.x_ < resizeBorder_.left_)
  230. mode = DRAG_RESIZE_BOTTOMLEFT;
  231. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  232. mode = DRAG_RESIZE_BOTTOMRIGHT;
  233. }
  234. }
  235. // Middle
  236. else
  237. {
  238. if (movable_)
  239. mode = DRAG_MOVE;
  240. if (resizable_)
  241. {
  242. if (position.x_ < resizeBorder_.left_)
  243. mode = DRAG_RESIZE_LEFT;
  244. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  245. mode = DRAG_RESIZE_RIGHT;
  246. }
  247. }
  248. return mode;
  249. }
  250. void Window::SetCursorShape(WindowDragMode mode, Cursor* cursor) const
  251. {
  252. if (!cursor)
  253. return;
  254. switch (mode)
  255. {
  256. case DRAG_NONE:
  257. case DRAG_MOVE:
  258. cursor->SetShape(CS_NORMAL);
  259. break;
  260. case DRAG_RESIZE_TOP:
  261. case DRAG_RESIZE_BOTTOM:
  262. cursor->SetShape(CS_RESIZEVERTICAL);
  263. break;
  264. case DRAG_RESIZE_LEFT:
  265. case DRAG_RESIZE_RIGHT:
  266. cursor->SetShape(CS_RESIZEHORIZONTAL);
  267. break;
  268. case DRAG_RESIZE_TOPRIGHT:
  269. case DRAG_RESIZE_BOTTOMLEFT:
  270. cursor->SetShape(CS_RESIZEDIAGONAL_TOPRIGHT);
  271. break;
  272. case DRAG_RESIZE_TOPLEFT:
  273. case DRAG_RESIZE_BOTTOMRIGHT:
  274. cursor->SetShape(CS_RESIZEDIAGONAL_TOPLEFT);
  275. break;
  276. default:
  277. break;
  278. }
  279. }
  280. void Window::ValidatePosition()
  281. {
  282. // Check that window does not go more than halfway outside its parent in either dimension
  283. if (!parent_)
  284. return;
  285. const IntVector2& parentSize = parent_->GetSize();
  286. IntVector2 position = GetPosition();
  287. IntVector2 halfSize = GetSize() / 2;
  288. position.x_ = Clamp(position.x_, -halfSize.x_, parentSize.x_ - halfSize.x_);
  289. position.y_ = Clamp(position.y_, -halfSize.y_, parentSize.y_ - halfSize.y_);
  290. SetPosition(position);
  291. }
  292. bool Window::CheckAlignment() const
  293. {
  294. // Only top left-alignment is supported for move and resize
  295. if (GetHorizontalAlignment() == HA_LEFT && GetVerticalAlignment() == VA_TOP)
  296. return true;
  297. else
  298. return false;
  299. }
  300. }