Window.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. //
  2. // Copyright (c) 2008-2020 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 "../Input/InputEvents.h"
  25. #include "../UI/Cursor.h"
  26. #include "../UI/UI.h"
  27. #include "../UI/UIEvents.h"
  28. #include "../UI/Window.h"
  29. #include "../DebugNew.h"
  30. namespace Urho3D
  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_BLACK),
  45. modalFrameColor_(Color::TRANSPARENT_BLACK),
  46. modalFrameSize_(IntVector2::ZERO)
  47. {
  48. bringToFront_ = true;
  49. clipChildren_ = true;
  50. SetEnabled(true);
  51. }
  52. Window::~Window() = default;
  53. void Window::RegisterObject(Context* context)
  54. {
  55. context->RegisterFactory<Window>(UI_CATEGORY);
  56. URHO3D_COPY_BASE_ATTRIBUTES(BorderImage);
  57. URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Bring To Front", true);
  58. URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Clip Children", true);
  59. URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Is Enabled", true);
  60. URHO3D_ACCESSOR_ATTRIBUTE("Resize Border", GetResizeBorder, SetResizeBorder, IntRect, IntRect(DEFAULT_RESIZE_BORDER, \
  61. DEFAULT_RESIZE_BORDER, DEFAULT_RESIZE_BORDER, DEFAULT_RESIZE_BORDER), AM_FILE);
  62. URHO3D_ACCESSOR_ATTRIBUTE("Is Movable", IsMovable, SetMovable, bool, false, AM_FILE);
  63. URHO3D_ACCESSOR_ATTRIBUTE("Is Resizable", IsResizable, SetResizable, bool, false, AM_FILE);
  64. URHO3D_ACCESSOR_ATTRIBUTE("Fixed Width Resizing", GetFixedWidthResizing, SetFixedWidthResizing, bool, false, AM_FILE);
  65. URHO3D_ACCESSOR_ATTRIBUTE("Fixed Height Resizing", GetFixedHeightResizing, SetFixedHeightResizing, bool, false, AM_FILE);
  66. URHO3D_ACCESSOR_ATTRIBUTE("Is Modal", IsModal, SetModal, bool, false, AM_FILE | AM_NOEDIT);
  67. URHO3D_ACCESSOR_ATTRIBUTE("Modal Shade Color", GetModalShadeColor, SetModalShadeColor, Color, Color::TRANSPARENT_BLACK, AM_FILE);
  68. URHO3D_ACCESSOR_ATTRIBUTE("Modal Frame Color", GetModalFrameColor, SetModalFrameColor, Color, Color::TRANSPARENT_BLACK, AM_FILE);
  69. URHO3D_ACCESSOR_ATTRIBUTE("Modal Frame Size", GetModalFrameSize, SetModalFrameSize, IntVector2, IntVector2::ZERO, AM_FILE);
  70. // Modal auto dismiss is purposefully not an attribute, as using it can make the editor lock up.
  71. // Instead it should be set false in code when needed
  72. }
  73. void Window::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  74. {
  75. if (modal_)
  76. {
  77. // Modal shade
  78. if (modalShadeColor_ != Color::TRANSPARENT_BLACK)
  79. {
  80. UIElement* rootElement = GetRoot();
  81. const IntVector2& rootSize = rootElement->GetSize();
  82. UIBatch batch(rootElement, BLEND_ALPHA, IntRect(0, 0, rootSize.x_, rootSize.y_), nullptr, &vertexData);
  83. batch.SetColor(modalShadeColor_);
  84. batch.AddQuad(0, 0, rootSize.x_, rootSize.y_, 0, 0);
  85. UIBatch::AddOrMerge(batch, batches);
  86. }
  87. // Modal frame
  88. if (modalFrameColor_ != Color::TRANSPARENT_BLACK && modalFrameSize_ != IntVector2::ZERO)
  89. {
  90. UIBatch batch(this, BLEND_ALPHA, currentScissor, nullptr, &vertexData);
  91. int x = GetIndentWidth();
  92. IntVector2 size = GetSize();
  93. size.x_ -= x;
  94. batch.SetColor(modalFrameColor_);
  95. batch.AddQuad(x - modalFrameSize_.x_, -modalFrameSize_.y_, size.x_ + 2 * modalFrameSize_.x_,
  96. 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*/,
  128. 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. // Use GetEffectiveMinSize() instead of GetMinSize() to prevent windows moving once the effective minimum size is reached
  138. const IntVector2 effectiveMinSize = GetEffectiveMinSize();
  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_ - effectiveMinSize.x_)),
  148. Clamp(dragBeginPosition_.y_ + delta.y_, position.y_ - (maxSize.y_ - size.y_),
  149. position.y_ + (size.y_ - effectiveMinSize.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_ - effectiveMinSize.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_ - effectiveMinSize.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_ - effectiveMinSize.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_ - effectiveMinSize.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 && dragMode_ != DRAG_NONE)
  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. auto* ui = GetSubsystem<UI>();
  241. // Can be null at exit time; no-op in that case
  242. if (!ui)
  243. return;
  244. if (ui->SetModalElement(this, modal))
  245. {
  246. modal_ = modal;
  247. using namespace ModalChanged;
  248. VariantMap& eventData = GetEventDataMap();
  249. eventData[P_ELEMENT] = this;
  250. eventData[P_MODAL] = modal;
  251. SendEvent(E_MODALCHANGED, eventData);
  252. }
  253. }
  254. void Window::SetModalShadeColor(const Color& color)
  255. {
  256. modalShadeColor_ = color;
  257. }
  258. void Window::SetModalFrameColor(const Color& color)
  259. {
  260. modalFrameColor_ = color;
  261. }
  262. void Window::SetModalFrameSize(const IntVector2& size)
  263. {
  264. modalFrameSize_ = size;
  265. }
  266. void Window::SetModalAutoDismiss(bool enable)
  267. {
  268. modalAutoDismiss_ = enable;
  269. }
  270. WindowDragMode Window::GetDragMode(const IntVector2& position) const
  271. {
  272. WindowDragMode mode = DRAG_NONE;
  273. // Top row
  274. if (position.y_ < resizeBorder_.top_)
  275. {
  276. if (movable_)
  277. mode = DRAG_MOVE;
  278. if (resizable_)
  279. {
  280. mode = DRAG_RESIZE_TOP;
  281. if (position.x_ < resizeBorder_.left_)
  282. mode = DRAG_RESIZE_TOPLEFT;
  283. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  284. mode = DRAG_RESIZE_TOPRIGHT;
  285. }
  286. }
  287. // Bottom row
  288. else if (position.y_ >= GetHeight() - resizeBorder_.bottom_)
  289. {
  290. if (movable_)
  291. mode = DRAG_MOVE;
  292. if (resizable_)
  293. {
  294. mode = DRAG_RESIZE_BOTTOM;
  295. if (position.x_ < resizeBorder_.left_)
  296. mode = DRAG_RESIZE_BOTTOMLEFT;
  297. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  298. mode = DRAG_RESIZE_BOTTOMRIGHT;
  299. }
  300. }
  301. // Middle
  302. else
  303. {
  304. if (movable_)
  305. mode = DRAG_MOVE;
  306. if (resizable_)
  307. {
  308. if (position.x_ < resizeBorder_.left_)
  309. mode = DRAG_RESIZE_LEFT;
  310. if (position.x_ >= GetWidth() - resizeBorder_.right_)
  311. mode = DRAG_RESIZE_RIGHT;
  312. }
  313. }
  314. return mode;
  315. }
  316. void Window::SetCursorShape(WindowDragMode mode, Cursor* cursor) const
  317. {
  318. CursorShape shape = CS_NORMAL;
  319. switch (mode)
  320. {
  321. case DRAG_RESIZE_TOP:
  322. case DRAG_RESIZE_BOTTOM:
  323. shape = CS_RESIZEVERTICAL;
  324. break;
  325. case DRAG_RESIZE_LEFT:
  326. case DRAG_RESIZE_RIGHT:
  327. shape = CS_RESIZEHORIZONTAL;
  328. break;
  329. case DRAG_RESIZE_TOPRIGHT:
  330. case DRAG_RESIZE_BOTTOMLEFT:
  331. shape = CS_RESIZEDIAGONAL_TOPRIGHT;
  332. break;
  333. case DRAG_RESIZE_TOPLEFT:
  334. case DRAG_RESIZE_BOTTOMRIGHT:
  335. shape = CS_RESIZEDIAGONAL_TOPLEFT;
  336. break;
  337. default:
  338. break;
  339. }
  340. if (cursor)
  341. cursor->SetShape(shape);
  342. }
  343. void Window::ValidatePosition()
  344. {
  345. // Check that window does not go more than halfway outside its parent in either dimension
  346. if (!parent_)
  347. return;
  348. const IntVector2& parentSize = parent_->GetSize();
  349. IntVector2 position = GetPosition();
  350. IntVector2 halfSize = GetSize() / 2;
  351. position.x_ = Clamp(position.x_, -halfSize.x_, parentSize.x_ - halfSize.x_);
  352. position.y_ = Clamp(position.y_, -halfSize.y_, parentSize.y_ - halfSize.y_);
  353. SetPosition(position);
  354. }
  355. bool Window::CheckAlignment() const
  356. {
  357. // Only top left-alignment is supported for move and resize
  358. if (GetHorizontalAlignment() == HA_LEFT && GetVerticalAlignment() == VA_TOP)
  359. return true;
  360. else
  361. return false;
  362. }
  363. }