Window.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 "../UI/Cursor.h"
  24. #include "../Input/InputEvents.h"
  25. #include "../UI/UI.h"
  26. #include "../UI/UIEvents.h"
  27. #include "../UI/Window.h"
  28. #include "../DebugNew.h"
  29. namespace Urho3D
  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_, size.y_ + 2 * modalFrameSize_.y_, 0, 0);
  97. UIBatch::AddOrMerge(batch, batches);
  98. }
  99. }
  100. BorderImage::GetBatches(batches, vertexData, currentScissor);
  101. }
  102. void Window::OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  103. {
  104. UIElement::OnHover(position, screenPosition, buttons, qualifiers, cursor);
  105. if (dragMode_ == DRAG_NONE)
  106. {
  107. WindowDragMode mode = GetDragMode(position);
  108. SetCursorShape(mode, cursor);
  109. }
  110. else
  111. SetCursorShape(dragMode_, cursor);
  112. }
  113. void Window::OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  114. {
  115. UIElement::OnDragBegin(position, screenPosition, buttons, qualifiers, cursor);
  116. if (buttons != MOUSEB_LEFT || !CheckAlignment())
  117. {
  118. dragMode_ = DRAG_NONE;
  119. return;
  120. }
  121. dragBeginCursor_ = screenPosition;
  122. dragBeginPosition_ = GetPosition();
  123. dragBeginSize_ = GetSize();
  124. dragMode_ = GetDragMode(position);
  125. SetCursorShape(dragMode_, cursor);
  126. }
  127. void Window::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, const IntVector2& deltaPos, int buttons, int qualifiers, Cursor* cursor)
  128. {
  129. if (dragMode_ == DRAG_NONE)
  130. return;
  131. IntVector2 delta = screenPosition - dragBeginCursor_;
  132. IntVector2 dragSize;
  133. IntVector2 resizeBorderSize(resizeBorder_.left_ + resizeBorder_.right_, resizeBorder_.top_ + resizeBorder_.bottom_);
  134. const IntVector2& position_ = GetPosition();
  135. const IntVector2& size_ = GetSize();
  136. const IntVector2& minSize_ = GetMinSize();
  137. const IntVector2& maxSize_ = GetMaxSize();
  138. switch (dragMode_)
  139. {
  140. case DRAG_MOVE:
  141. SetPosition(dragBeginPosition_ + delta);
  142. break;
  143. case DRAG_RESIZE_TOPLEFT:
  144. SetPosition(Clamp(dragBeginPosition_.x_ + delta.x_, position_.x_ - (maxSize_.x_ - size_.x_), position_.x_ + (size_.x_ - minSize_.x_)),
  145. Clamp(dragBeginPosition_.y_ + delta.y_, position_.y_ - (maxSize_.y_ - size_.y_), position_.y_ + (size_.y_ - minSize_.y_)));
  146. dragSize = dragBeginSize_ - delta;
  147. fixedWidthResizing_ ? SetFixedWidth(Max(dragSize.x_, resizeBorderSize.x_)) : SetWidth(dragSize.x_);
  148. fixedHeightResizing_ ? SetFixedHeight(Max(dragSize.y_, resizeBorderSize.y_)) : SetHeight(dragSize.y_);
  149. break;
  150. case DRAG_RESIZE_TOP:
  151. SetPosition(dragBeginPosition_.x_, Clamp(dragBeginPosition_.y_ + delta.y_, position_.y_ - (maxSize_.y_ - size_.y_), position_.y_ + (size_.y_ - minSize_.y_)));
  152. dragSize = IntVector2(dragBeginSize_.x_, dragBeginSize_.y_ - delta.y_);
  153. fixedHeightResizing_ ? SetFixedHeight(Max(dragSize.y_, resizeBorderSize.y_)) : SetHeight(dragSize.y_);
  154. break;
  155. case DRAG_RESIZE_TOPRIGHT:
  156. SetPosition(dragBeginPosition_.x_, Clamp(dragBeginPosition_.y_ + delta.y_, position_.y_ - (maxSize_.y_ - size_.y_), position_.y_ + (size_.y_ - minSize_.y_)));
  157. dragSize = IntVector2(dragBeginSize_.x_ + delta.x_, dragBeginSize_.y_ - delta.y_);
  158. fixedWidthResizing_ ? SetFixedWidth(Max(dragSize.x_, resizeBorderSize.x_)) : SetWidth(dragSize.x_);
  159. fixedHeightResizing_ ? SetFixedHeight(Max(dragSize.y_, resizeBorderSize.y_)) : SetHeight(dragSize.y_);
  160. break;
  161. case DRAG_RESIZE_RIGHT:
  162. dragSize = IntVector2(dragBeginSize_.x_ + delta.x_, dragBeginSize_.y_);
  163. fixedWidthResizing_ ? SetFixedWidth(Max(dragSize.x_, resizeBorderSize.x_)) : SetWidth(dragSize.x_);
  164. break;
  165. case DRAG_RESIZE_BOTTOMRIGHT:
  166. dragSize = dragBeginSize_ + delta;
  167. fixedWidthResizing_ ? SetFixedWidth(Max(dragSize.x_, resizeBorderSize.x_)) : SetWidth(dragSize.x_);
  168. fixedHeightResizing_ ? SetFixedHeight(Max(dragSize.y_, resizeBorderSize.y_)) : SetHeight(dragSize.y_);
  169. break;
  170. case DRAG_RESIZE_BOTTOM:
  171. dragSize = IntVector2(dragBeginSize_.x_, dragBeginSize_.y_ + delta.y_);
  172. fixedHeightResizing_ ? SetFixedHeight(Max(dragSize.y_, resizeBorderSize.y_)) : SetHeight(dragSize.y_);
  173. break;
  174. case DRAG_RESIZE_BOTTOMLEFT:
  175. SetPosition(Clamp(dragBeginPosition_.x_ + delta.x_, position_.x_ - (maxSize_.x_ - size_.x_), position_.x_ + (size_.x_ - minSize_.x_)), dragBeginPosition_.y_);
  176. dragSize = IntVector2(dragBeginSize_.x_ - delta.x_, dragBeginSize_.y_ + delta.y_);
  177. fixedWidthResizing_ ? SetFixedWidth(Max(dragSize.x_, resizeBorderSize.x_)) : SetWidth(dragSize.x_);
  178. fixedHeightResizing_ ? SetFixedHeight(Max(dragSize.y_, resizeBorderSize.y_)) : SetHeight(dragSize.y_);
  179. break;
  180. case DRAG_RESIZE_LEFT:
  181. SetPosition(Clamp(dragBeginPosition_.x_ + delta.x_, position_.x_ - (maxSize_.x_ - size_.x_), position_.x_ + (size_.x_ - minSize_.x_)), dragBeginPosition_.y_);
  182. dragSize = IntVector2(dragBeginSize_.x_ - delta.x_, dragBeginSize_.y_);
  183. fixedWidthResizing_ ? SetFixedWidth(Max(dragSize.x_, resizeBorderSize.x_)) : SetWidth(dragSize.x_);
  184. break;
  185. default:
  186. break;
  187. }
  188. ValidatePosition();
  189. SetCursorShape(dragMode_, cursor);
  190. }
  191. void Window::OnDragEnd(const IntVector2& position, const IntVector2& screenPosition, int dragButtons, int buttons, Cursor* cursor)
  192. {
  193. UIElement::OnDragEnd(position, screenPosition, dragButtons, buttons, cursor);
  194. dragMode_ = DRAG_NONE;
  195. }
  196. void Window::OnDragCancel(const IntVector2& position, const IntVector2& screenPosition, int dragButtons, int buttons, Cursor* cursor)
  197. {
  198. UIElement::OnDragCancel(position, screenPosition, dragButtons, buttons, cursor);
  199. if (dragButtons == MOUSEB_LEFT)
  200. {
  201. dragMode_ = DRAG_NONE;
  202. SetPosition(dragBeginPosition_);
  203. SetSize(dragBeginSize_);
  204. }
  205. }
  206. void Window::SetMovable(bool enable)
  207. {
  208. movable_ = enable;
  209. }
  210. void Window::SetResizable(bool enable)
  211. {
  212. resizable_ = enable;
  213. }
  214. void Window::SetFixedWidthResizing(bool enable)
  215. {
  216. fixedWidthResizing_ = enable;
  217. }
  218. void Window::SetFixedHeightResizing(bool enable)
  219. {
  220. fixedHeightResizing_ = enable;
  221. }
  222. void Window::SetResizeBorder(const IntRect& rect)
  223. {
  224. resizeBorder_.left_ = Max(rect.left_, 0);
  225. resizeBorder_.top_ = Max(rect.top_, 0);
  226. resizeBorder_.right_ = Max(rect.right_, 0);
  227. resizeBorder_.bottom_ = Max(rect.bottom_, 0);
  228. }
  229. void Window::SetModal(bool modal)
  230. {
  231. if (GetSubsystem<UI>()->SetModalElement(this, modal))
  232. {
  233. modal_ = modal;
  234. using namespace ModalChanged;
  235. VariantMap& eventData = GetEventDataMap();
  236. eventData[P_ELEMENT] = this;
  237. eventData[P_MODAL] = modal;
  238. SendEvent(E_MODALCHANGED, eventData);
  239. }
  240. }
  241. void Window::SetModalShadeColor(const Color& color)
  242. {
  243. modalShadeColor_ = color;
  244. }
  245. void Window::SetModalFrameColor(const Color& color)
  246. {
  247. modalFrameColor_ = color;
  248. }
  249. void Window::SetModalFrameSize(const IntVector2& size)
  250. {
  251. modalFrameSize_ = size;
  252. }
  253. void Window::SetModalAutoDismiss(bool enable)
  254. {
  255. modalAutoDismiss_ = enable;
  256. }
  257. WindowDragMode Window::GetDragMode(const IntVector2& position) const
  258. {
  259. WindowDragMode mode = DRAG_NONE;
  260. // Top row
  261. if (position.y_ < resizeBorder_.top_)
  262. {
  263. if (movable_)
  264. mode = DRAG_MOVE;
  265. if (resizable_)
  266. {
  267. mode = DRAG_RESIZE_TOP;
  268. if (position.x_ < resizeBorder_.left_)
  269. mode = DRAG_RESIZE_TOPLEFT;
  270. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  271. mode = DRAG_RESIZE_TOPRIGHT;
  272. }
  273. }
  274. // Bottom row
  275. else if (position.y_ >= GetHeight() - resizeBorder_.bottom_)
  276. {
  277. if (movable_)
  278. mode = DRAG_MOVE;
  279. if (resizable_)
  280. {
  281. mode = DRAG_RESIZE_BOTTOM;
  282. if (position.x_ < resizeBorder_.left_)
  283. mode = DRAG_RESIZE_BOTTOMLEFT;
  284. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  285. mode = DRAG_RESIZE_BOTTOMRIGHT;
  286. }
  287. }
  288. // Middle
  289. else
  290. {
  291. if (movable_)
  292. mode = DRAG_MOVE;
  293. if (resizable_)
  294. {
  295. if (position.x_ < resizeBorder_.left_)
  296. mode = DRAG_RESIZE_LEFT;
  297. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  298. mode = DRAG_RESIZE_RIGHT;
  299. }
  300. }
  301. return mode;
  302. }
  303. void Window::SetCursorShape(WindowDragMode mode, Cursor* cursor) const
  304. {
  305. CursorShape shape = CS_NORMAL;
  306. switch (mode)
  307. {
  308. case DRAG_RESIZE_TOP:
  309. case DRAG_RESIZE_BOTTOM:
  310. shape = CS_RESIZEVERTICAL;
  311. break;
  312. case DRAG_RESIZE_LEFT:
  313. case DRAG_RESIZE_RIGHT:
  314. shape = CS_RESIZEHORIZONTAL;
  315. break;
  316. case DRAG_RESIZE_TOPRIGHT:
  317. case DRAG_RESIZE_BOTTOMLEFT:
  318. shape = CS_RESIZEDIAGONAL_TOPRIGHT;
  319. break;
  320. case DRAG_RESIZE_TOPLEFT:
  321. case DRAG_RESIZE_BOTTOMRIGHT:
  322. shape = CS_RESIZEDIAGONAL_TOPLEFT;
  323. break;
  324. default:
  325. break;
  326. }
  327. if (cursor)
  328. cursor->SetShape(shape);
  329. }
  330. void Window::ValidatePosition()
  331. {
  332. // Check that window does not go more than halfway outside its parent in either dimension
  333. if (!parent_)
  334. return;
  335. const IntVector2& parentSize = parent_->GetSize();
  336. IntVector2 position = GetPosition();
  337. IntVector2 halfSize = GetSize() / 2;
  338. position.x_ = Clamp(position.x_, -halfSize.x_, parentSize.x_ - halfSize.x_);
  339. position.y_ = Clamp(position.y_, -halfSize.y_, parentSize.y_ - halfSize.y_);
  340. SetPosition(position);
  341. }
  342. bool Window::CheckAlignment() const
  343. {
  344. // Only top left-alignment is supported for move and resize
  345. if (GetHorizontalAlignment() == HA_LEFT && GetVerticalAlignment() == VA_TOP)
  346. return true;
  347. else
  348. return false;
  349. }
  350. }