Window.cpp 14 KB

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