Window.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. //
  2. // Copyright (c) 2008-2015 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 "../../Core/Context.h"
  23. #include "../../Input/InputEvents.h"
  24. #include "Cursor.h"
  25. #include "UI.h"
  26. #include "UIEvents.h"
  27. #include "Window.h"
  28. #include "../../DebugNew.h"
  29. namespace Atomic
  30. {
  31. static const int DEFAULT_RESIZE_BORDER = 4;
  32. extern const char* UI_CATEGORY;
  33. Window::Window(Context* context) :
  34. BorderImage(context),
  35. movable_(false),
  36. resizable_(false),
  37. fixedWidthResizing_(false),
  38. fixedHeightResizing_(false),
  39. resizeBorder_(DEFAULT_RESIZE_BORDER, DEFAULT_RESIZE_BORDER, DEFAULT_RESIZE_BORDER, DEFAULT_RESIZE_BORDER),
  40. dragMode_(DRAG_NONE),
  41. modal_(false),
  42. modalAutoDismiss_(true),
  43. modalShadeColor_(Color::TRANSPARENT),
  44. modalFrameColor_(Color::TRANSPARENT),
  45. modalFrameSize_(IntVector2::ZERO)
  46. {
  47. bringToFront_ = true;
  48. clipChildren_ = true;
  49. SetEnabled(true);
  50. }
  51. Window::~Window()
  52. {
  53. }
  54. void Window::RegisterObject(Context* context)
  55. {
  56. context->RegisterFactory<Window>(UI_CATEGORY);
  57. COPY_BASE_ATTRIBUTES(BorderImage);
  58. UPDATE_ATTRIBUTE_DEFAULT_VALUE("Bring To Front", true);
  59. UPDATE_ATTRIBUTE_DEFAULT_VALUE("Clip Children", true);
  60. UPDATE_ATTRIBUTE_DEFAULT_VALUE("Is Enabled", true);
  61. ACCESSOR_ATTRIBUTE("Resize Border", GetResizeBorder, SetResizeBorder, IntRect, IntRect(DEFAULT_RESIZE_BORDER, \
  62. DEFAULT_RESIZE_BORDER, DEFAULT_RESIZE_BORDER, DEFAULT_RESIZE_BORDER), AM_FILE);
  63. ACCESSOR_ATTRIBUTE("Is Movable", IsMovable, SetMovable, bool, false, AM_FILE);
  64. ACCESSOR_ATTRIBUTE("Is Resizable", IsResizable, SetResizable, bool, false, AM_FILE);
  65. ACCESSOR_ATTRIBUTE("Fixed Width Resizing", GetFixedWidthResizing, SetFixedWidthResizing, bool, false, AM_FILE);
  66. ACCESSOR_ATTRIBUTE("Fixed Height Resizing", GetFixedHeightResizing, SetFixedHeightResizing, bool, false, AM_FILE);
  67. ACCESSOR_ATTRIBUTE("Is Modal", IsModal, SetModal, bool, false, AM_FILE | AM_NOEDIT);
  68. ACCESSOR_ATTRIBUTE("Modal Shade Color", GetModalShadeColor, SetModalShadeColor, Color, Color::TRANSPARENT, AM_FILE);
  69. ACCESSOR_ATTRIBUTE("Modal Frame Color", GetModalFrameColor, SetModalFrameColor, Color, Color::TRANSPARENT, AM_FILE);
  70. ACCESSOR_ATTRIBUTE("Modal Frame Size", GetModalFrameSize, SetModalFrameSize, IntVector2, IntVector2::ZERO, AM_FILE);
  71. // Modal auto dismiss is purposefully not an attribute, as using it can make the editor lock up.
  72. // Instead it should be set false in code when needed
  73. }
  74. void Window::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  75. {
  76. if (modal_)
  77. {
  78. // Modal shade
  79. if (modalShadeColor_ != Color::TRANSPARENT)
  80. {
  81. UIElement* rootElement = GetRoot();
  82. const IntVector2& rootSize = rootElement->GetSize();
  83. UIBatch batch(rootElement, BLEND_ALPHA, IntRect(0, 0, rootSize.x_, rootSize.y_), 0, &vertexData);
  84. batch.SetColor(modalShadeColor_);
  85. batch.AddQuad(0, 0, rootSize.x_, rootSize.y_, 0, 0);
  86. UIBatch::AddOrMerge(batch, batches);
  87. }
  88. // Modal frame
  89. if (modalFrameColor_ != Color::TRANSPARENT && modalFrameSize_ != IntVector2::ZERO)
  90. {
  91. UIBatch batch(this, BLEND_ALPHA, currentScissor, 0, &vertexData);
  92. int x = GetIndentWidth();
  93. IntVector2 size = GetSize();
  94. size.x_ -= x;
  95. batch.SetColor(modalFrameColor_);
  96. batch.AddQuad(x - modalFrameSize_.x_, -modalFrameSize_.y_, size.x_ + 2 * modalFrameSize_.x_,
  97. size.y_ + 2 * modalFrameSize_.y_, 0, 0);
  98. UIBatch::AddOrMerge(batch, batches);
  99. }
  100. }
  101. BorderImage::GetBatches(batches, vertexData, currentScissor);
  102. }
  103. void Window::OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  104. {
  105. UIElement::OnHover(position, screenPosition, buttons, qualifiers, cursor);
  106. if (dragMode_ == DRAG_NONE)
  107. {
  108. WindowDragMode mode = GetDragMode(position);
  109. SetCursorShape(mode, cursor);
  110. }
  111. else
  112. SetCursorShape(dragMode_, cursor);
  113. }
  114. void Window::OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  115. {
  116. UIElement::OnDragBegin(position, screenPosition, buttons, qualifiers, cursor);
  117. if (buttons != MOUSEB_LEFT || !CheckAlignment())
  118. {
  119. dragMode_ = DRAG_NONE;
  120. return;
  121. }
  122. dragBeginCursor_ = screenPosition;
  123. dragBeginPosition_ = GetPosition();
  124. dragBeginSize_ = GetSize();
  125. dragMode_ = GetDragMode(position);
  126. SetCursorShape(dragMode_, cursor);
  127. }
  128. void Window::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, const IntVector2& deltaPos, int buttons,
  129. int qualifiers, Cursor* cursor)
  130. {
  131. if (dragMode_ == DRAG_NONE)
  132. return;
  133. IntVector2 delta = screenPosition - dragBeginCursor_;
  134. IntVector2 dragSize;
  135. IntVector2 resizeBorderSize(resizeBorder_.left_ + resizeBorder_.right_, resizeBorder_.top_ + resizeBorder_.bottom_);
  136. const IntVector2& position_ = GetPosition();
  137. const IntVector2& size_ = GetSize();
  138. const IntVector2& minSize_ = GetMinSize();
  139. const IntVector2& maxSize_ = GetMaxSize();
  140. switch (dragMode_)
  141. {
  142. case DRAG_MOVE:
  143. SetPosition(dragBeginPosition_ + delta);
  144. break;
  145. case DRAG_RESIZE_TOPLEFT:
  146. SetPosition(Clamp(dragBeginPosition_.x_ + delta.x_, position_.x_ - (maxSize_.x_ - size_.x_),
  147. position_.x_ + (size_.x_ - minSize_.x_)),
  148. Clamp(dragBeginPosition_.y_ + delta.y_, position_.y_ - (maxSize_.y_ - size_.y_),
  149. position_.y_ + (size_.y_ - minSize_.y_)));
  150. dragSize = dragBeginSize_ - delta;
  151. fixedWidthResizing_ ? SetFixedWidth(Max(dragSize.x_, resizeBorderSize.x_)) : SetWidth(dragSize.x_);
  152. fixedHeightResizing_ ? SetFixedHeight(Max(dragSize.y_, resizeBorderSize.y_)) : SetHeight(dragSize.y_);
  153. break;
  154. case DRAG_RESIZE_TOP:
  155. SetPosition(dragBeginPosition_.x_, Clamp(dragBeginPosition_.y_ + delta.y_, position_.y_ - (maxSize_.y_ - size_.y_),
  156. position_.y_ + (size_.y_ - minSize_.y_)));
  157. dragSize = IntVector2(dragBeginSize_.x_, dragBeginSize_.y_ - delta.y_);
  158. fixedHeightResizing_ ? SetFixedHeight(Max(dragSize.y_, resizeBorderSize.y_)) : SetHeight(dragSize.y_);
  159. break;
  160. case DRAG_RESIZE_TOPRIGHT:
  161. SetPosition(dragBeginPosition_.x_, Clamp(dragBeginPosition_.y_ + delta.y_, position_.y_ - (maxSize_.y_ - size_.y_),
  162. position_.y_ + (size_.y_ - minSize_.y_)));
  163. dragSize = IntVector2(dragBeginSize_.x_ + delta.x_, dragBeginSize_.y_ - delta.y_);
  164. fixedWidthResizing_ ? SetFixedWidth(Max(dragSize.x_, resizeBorderSize.x_)) : SetWidth(dragSize.x_);
  165. fixedHeightResizing_ ? SetFixedHeight(Max(dragSize.y_, resizeBorderSize.y_)) : SetHeight(dragSize.y_);
  166. break;
  167. case DRAG_RESIZE_RIGHT:
  168. dragSize = IntVector2(dragBeginSize_.x_ + delta.x_, dragBeginSize_.y_);
  169. fixedWidthResizing_ ? SetFixedWidth(Max(dragSize.x_, resizeBorderSize.x_)) : SetWidth(dragSize.x_);
  170. break;
  171. case DRAG_RESIZE_BOTTOMRIGHT:
  172. dragSize = dragBeginSize_ + delta;
  173. fixedWidthResizing_ ? SetFixedWidth(Max(dragSize.x_, resizeBorderSize.x_)) : SetWidth(dragSize.x_);
  174. fixedHeightResizing_ ? SetFixedHeight(Max(dragSize.y_, resizeBorderSize.y_)) : SetHeight(dragSize.y_);
  175. break;
  176. case DRAG_RESIZE_BOTTOM:
  177. dragSize = IntVector2(dragBeginSize_.x_, dragBeginSize_.y_ + delta.y_);
  178. fixedHeightResizing_ ? SetFixedHeight(Max(dragSize.y_, resizeBorderSize.y_)) : SetHeight(dragSize.y_);
  179. break;
  180. case DRAG_RESIZE_BOTTOMLEFT:
  181. SetPosition(Clamp(dragBeginPosition_.x_ + delta.x_, position_.x_ - (maxSize_.x_ - size_.x_),
  182. position_.x_ + (size_.x_ - minSize_.x_)), dragBeginPosition_.y_);
  183. dragSize = IntVector2(dragBeginSize_.x_ - delta.x_, dragBeginSize_.y_ + delta.y_);
  184. fixedWidthResizing_ ? SetFixedWidth(Max(dragSize.x_, resizeBorderSize.x_)) : SetWidth(dragSize.x_);
  185. fixedHeightResizing_ ? SetFixedHeight(Max(dragSize.y_, resizeBorderSize.y_)) : SetHeight(dragSize.y_);
  186. break;
  187. case DRAG_RESIZE_LEFT:
  188. SetPosition(Clamp(dragBeginPosition_.x_ + delta.x_, position_.x_ - (maxSize_.x_ - size_.x_),
  189. position_.x_ + (size_.x_ - minSize_.x_)), dragBeginPosition_.y_);
  190. dragSize = IntVector2(dragBeginSize_.x_ - delta.x_, dragBeginSize_.y_);
  191. fixedWidthResizing_ ? SetFixedWidth(Max(dragSize.x_, resizeBorderSize.x_)) : SetWidth(dragSize.x_);
  192. break;
  193. default:
  194. break;
  195. }
  196. ValidatePosition();
  197. SetCursorShape(dragMode_, cursor);
  198. }
  199. void Window::OnDragEnd(const IntVector2& position, const IntVector2& screenPosition, int dragButtons, int buttons, Cursor* cursor)
  200. {
  201. UIElement::OnDragEnd(position, screenPosition, dragButtons, buttons, cursor);
  202. dragMode_ = DRAG_NONE;
  203. }
  204. void Window::OnDragCancel(const IntVector2& position, const IntVector2& screenPosition, int dragButtons, int buttons,
  205. Cursor* cursor)
  206. {
  207. UIElement::OnDragCancel(position, screenPosition, dragButtons, buttons, cursor);
  208. if (dragButtons == MOUSEB_LEFT)
  209. {
  210. dragMode_ = DRAG_NONE;
  211. SetPosition(dragBeginPosition_);
  212. SetSize(dragBeginSize_);
  213. }
  214. }
  215. void Window::SetMovable(bool enable)
  216. {
  217. movable_ = enable;
  218. }
  219. void Window::SetResizable(bool enable)
  220. {
  221. resizable_ = enable;
  222. }
  223. void Window::SetFixedWidthResizing(bool enable)
  224. {
  225. fixedWidthResizing_ = enable;
  226. }
  227. void Window::SetFixedHeightResizing(bool enable)
  228. {
  229. fixedHeightResizing_ = enable;
  230. }
  231. void Window::SetResizeBorder(const IntRect& rect)
  232. {
  233. resizeBorder_.left_ = Max(rect.left_, 0);
  234. resizeBorder_.top_ = Max(rect.top_, 0);
  235. resizeBorder_.right_ = Max(rect.right_, 0);
  236. resizeBorder_.bottom_ = Max(rect.bottom_, 0);
  237. }
  238. void Window::SetModal(bool modal)
  239. {
  240. if (GetSubsystem<UI>()->SetModalElement(this, modal))
  241. {
  242. modal_ = modal;
  243. using namespace ModalChanged;
  244. VariantMap& eventData = GetEventDataMap();
  245. eventData[P_ELEMENT] = this;
  246. eventData[P_MODAL] = modal;
  247. SendEvent(E_MODALCHANGED, eventData);
  248. }
  249. }
  250. void Window::SetModalShadeColor(const Color& color)
  251. {
  252. modalShadeColor_ = color;
  253. }
  254. void Window::SetModalFrameColor(const Color& color)
  255. {
  256. modalFrameColor_ = color;
  257. }
  258. void Window::SetModalFrameSize(const IntVector2& size)
  259. {
  260. modalFrameSize_ = size;
  261. }
  262. void Window::SetModalAutoDismiss(bool enable)
  263. {
  264. modalAutoDismiss_ = enable;
  265. }
  266. WindowDragMode Window::GetDragMode(const IntVector2& position) const
  267. {
  268. WindowDragMode mode = DRAG_NONE;
  269. // Top row
  270. if (position.y_ < resizeBorder_.top_)
  271. {
  272. if (movable_)
  273. mode = DRAG_MOVE;
  274. if (resizable_)
  275. {
  276. mode = DRAG_RESIZE_TOP;
  277. if (position.x_ < resizeBorder_.left_)
  278. mode = DRAG_RESIZE_TOPLEFT;
  279. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  280. mode = DRAG_RESIZE_TOPRIGHT;
  281. }
  282. }
  283. // Bottom row
  284. else if (position.y_ >= GetHeight() - resizeBorder_.bottom_)
  285. {
  286. if (movable_)
  287. mode = DRAG_MOVE;
  288. if (resizable_)
  289. {
  290. mode = DRAG_RESIZE_BOTTOM;
  291. if (position.x_ < resizeBorder_.left_)
  292. mode = DRAG_RESIZE_BOTTOMLEFT;
  293. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  294. mode = DRAG_RESIZE_BOTTOMRIGHT;
  295. }
  296. }
  297. // Middle
  298. else
  299. {
  300. if (movable_)
  301. mode = DRAG_MOVE;
  302. if (resizable_)
  303. {
  304. if (position.x_ < resizeBorder_.left_)
  305. mode = DRAG_RESIZE_LEFT;
  306. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  307. mode = DRAG_RESIZE_RIGHT;
  308. }
  309. }
  310. return mode;
  311. }
  312. void Window::SetCursorShape(WindowDragMode mode, Cursor* cursor) const
  313. {
  314. CursorShape shape = CS_NORMAL;
  315. switch (mode)
  316. {
  317. case DRAG_RESIZE_TOP:
  318. case DRAG_RESIZE_BOTTOM:
  319. shape = CS_RESIZEVERTICAL;
  320. break;
  321. case DRAG_RESIZE_LEFT:
  322. case DRAG_RESIZE_RIGHT:
  323. shape = CS_RESIZEHORIZONTAL;
  324. break;
  325. case DRAG_RESIZE_TOPRIGHT:
  326. case DRAG_RESIZE_BOTTOMLEFT:
  327. shape = CS_RESIZEDIAGONAL_TOPRIGHT;
  328. break;
  329. case DRAG_RESIZE_TOPLEFT:
  330. case DRAG_RESIZE_BOTTOMRIGHT:
  331. shape = CS_RESIZEDIAGONAL_TOPLEFT;
  332. break;
  333. default:
  334. break;
  335. }
  336. if (cursor)
  337. cursor->SetShape(shape);
  338. }
  339. void Window::ValidatePosition()
  340. {
  341. // Check that window does not go more than halfway outside its parent in either dimension
  342. if (!parent_)
  343. return;
  344. const IntVector2& parentSize = parent_->GetSize();
  345. IntVector2 position = GetPosition();
  346. IntVector2 halfSize = GetSize() / 2;
  347. position.x_ = Clamp(position.x_, -halfSize.x_, parentSize.x_ - halfSize.x_);
  348. position.y_ = Clamp(position.y_, -halfSize.y_, parentSize.y_ - halfSize.y_);
  349. SetPosition(position);
  350. }
  351. bool Window::CheckAlignment() const
  352. {
  353. // Only top left-alignment is supported for move and resize
  354. if (GetHorizontalAlignment() == HA_LEFT && GetVerticalAlignment() == VA_TOP)
  355. return true;
  356. else
  357. return false;
  358. }
  359. }