Window.cpp 14 KB

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