Window.cpp 14 KB

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