UI.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "CheckBox.h"
  25. #include "Context.h"
  26. #include "CoreEvents.h"
  27. #include "Cursor.h"
  28. #include "DropDownList.h"
  29. #include "FileSelector.h"
  30. #include "Font.h"
  31. #include "Graphics.h"
  32. #include "GraphicsEvents.h"
  33. #include "InputEvents.h"
  34. #include "LineEdit.h"
  35. #include "ListView.h"
  36. #include "Log.h"
  37. #include "Matrix3x4.h"
  38. #include "Profiler.h"
  39. #include "ResourceCache.h"
  40. #include "ScrollBar.h"
  41. #include "Shader.h"
  42. #include "ShaderVariation.h"
  43. #include "Slider.h"
  44. #include "Text.h"
  45. #include "Texture2D.h"
  46. #include "UI.h"
  47. #include "UIEvents.h"
  48. #include "Window.h"
  49. #include "Sort.h"
  50. #include "DebugNew.h"
  51. static bool CompareUIElements(const UIElement* lhs, const UIElement* rhs)
  52. {
  53. return lhs->GetPriority() < rhs->GetPriority();
  54. }
  55. OBJECTTYPESTATIC(UI);
  56. UI::UI(Context* context) :
  57. Object(context),
  58. mouseButtons_(0),
  59. qualifiers_(0),
  60. initialized_(false)
  61. {
  62. SubscribeToEvent(E_SCREENMODE, HANDLER(UI, HandleScreenMode));
  63. SubscribeToEvent(E_MOUSEMOVE, HANDLER(UI, HandleMouseMove));
  64. SubscribeToEvent(E_MOUSEBUTTONDOWN, HANDLER(UI, HandleMouseButtonDown));
  65. SubscribeToEvent(E_MOUSEBUTTONUP, HANDLER(UI, HandleMouseButtonUp));
  66. SubscribeToEvent(E_MOUSEWHEEL, HANDLER(UI, HandleMouseWheel));
  67. SubscribeToEvent(E_KEYDOWN, HANDLER(UI, HandleKeyDown));
  68. SubscribeToEvent(E_CHAR, HANDLER(UI, HandleChar));
  69. SubscribeToEvent(E_POSTUPDATE, HANDLER(UI, HandlePostUpdate));
  70. SubscribeToEvent(E_RENDERUPDATE, HANDLER(UI, HandleRenderUpdate));
  71. // Try to initialize right now, but skip if screen mode is not yet set
  72. Initialize();
  73. }
  74. UI::~UI()
  75. {
  76. }
  77. void UI::SetCursor(Cursor* cursor)
  78. {
  79. if (!rootElement_)
  80. return;
  81. // Remove old cursor (if any) and set new
  82. if (cursor_)
  83. {
  84. rootElement_->RemoveChild(cursor_);
  85. cursor_.Reset();
  86. }
  87. if (cursor)
  88. {
  89. rootElement_->AddChild(cursor);
  90. cursor_ = cursor;
  91. IntVector2 pos = cursor_->GetPosition();
  92. const IntVector2& rootSize = rootElement_->GetSize();
  93. pos.x_ = Clamp(pos.x_, 0, rootSize.x_ - 1);
  94. pos.y_ = Clamp(pos.y_, 0, rootSize.y_ - 1);
  95. cursor_->SetPosition(pos);
  96. }
  97. }
  98. void UI::SetFocusElement(UIElement* element)
  99. {
  100. if (!rootElement_)
  101. return;
  102. using namespace FocusChanged;
  103. VariantMap eventData;
  104. eventData[P_ORIGINALELEMENT] = (void*)element;
  105. if (element)
  106. {
  107. // Return if already has focus
  108. if (element->HasFocus())
  109. return;
  110. // If element can not be focused, and does not reset the focus either, search toward the parent
  111. for (;;)
  112. {
  113. if (element->GetFocusMode() != FM_NOTFOCUSABLE)
  114. break;
  115. element = element->GetParent();
  116. // Return if did not find any parent element that changes the focus
  117. if (!element)
  118. return;
  119. }
  120. }
  121. PODVector<UIElement*> allChildren = rootElement_->GetChildren(true);
  122. // Go through all elements to clear the old focus
  123. for (PODVector<UIElement*>::Iterator i = allChildren.Begin(); i != allChildren.End(); ++i)
  124. {
  125. UIElement* other = *i;
  126. if (other != element && other->HasFocus())
  127. other->SetFocus(false);
  128. }
  129. if (element)
  130. element->SetFocus(true);
  131. eventData[P_ELEMENT] = (void*)element;
  132. SendEvent(E_FOCUSCHANGED, eventData);
  133. }
  134. void UI::Clear()
  135. {
  136. if (!rootElement_)
  137. return;
  138. rootElement_->RemoveAllChildren();
  139. if (cursor_)
  140. rootElement_->AddChild(cursor_);
  141. }
  142. void UI::Update(float timeStep)
  143. {
  144. if (!rootElement_)
  145. return;
  146. PROFILE(UpdateUI);
  147. if (cursor_ && cursor_->IsVisible())
  148. {
  149. IntVector2 pos = cursor_->GetPosition();
  150. WeakPtr<UIElement> element(GetElementAt(pos));
  151. bool dragSource = dragElement_ && (dragElement_->GetDragDropMode() & DD_SOURCE) != 0;
  152. bool dragTarget = element && (element->GetDragDropMode() & DD_TARGET) != 0;
  153. bool dragDropTest = dragSource && dragTarget && element != dragElement_;
  154. // Hover effect
  155. // If a drag is going on, transmit hover only to the element being dragged, unless it's a drop target
  156. if (element)
  157. {
  158. if (!dragElement_ || dragElement_ == element || dragDropTest)
  159. element->OnHover(element->ScreenToElement(pos), pos, mouseButtons_, qualifiers_, cursor_);
  160. }
  161. // Drag and drop test
  162. if (dragDropTest)
  163. {
  164. bool accept = element->OnDragDropTest(dragElement_);
  165. if (accept)
  166. {
  167. using namespace DragDropTest;
  168. VariantMap eventData;
  169. eventData[P_SOURCE] = (void*)dragElement_.RawPtr();
  170. eventData[P_TARGET] = (void*)element.RawPtr();
  171. eventData[P_ACCEPT] = accept;
  172. SendEvent(E_DRAGDROPTEST, eventData);
  173. accept = eventData[P_ACCEPT].GetBool();
  174. }
  175. cursor_->SetShape(accept ? CS_ACCEPTDROP : CS_REJECTDROP);
  176. }
  177. else if (dragSource)
  178. cursor_->SetShape(dragElement_ == element ? CS_ACCEPTDROP : CS_REJECTDROP);
  179. }
  180. // Defocus element now if should
  181. if (defocusElement_)
  182. {
  183. // Do nothing if the focus element changed in the meanwhile
  184. if (defocusElement_ == GetFocusElement())
  185. SetFocusElement(0);
  186. defocusElement_.Reset();
  187. }
  188. Update(timeStep, rootElement_);
  189. }
  190. void UI::RenderUpdate()
  191. {
  192. if (!rootElement_ || !graphics_ || graphics_->IsDeviceLost())
  193. return;
  194. PROFILE(GetUIBatches);
  195. batches_.Clear();
  196. quads_.Clear();
  197. const IntVector2& rootSize = rootElement_->GetSize();
  198. GetBatches(rootElement_, IntRect(0, 0, rootSize.x_, rootSize.y_));
  199. // If no drag, reset cursor shape for next frame
  200. if (cursor_ && !dragElement_)
  201. cursor_->SetShape(CS_NORMAL);
  202. }
  203. void UI::Render()
  204. {
  205. PROFILE(RenderUI);
  206. if (!graphics_)
  207. return;
  208. Vector2 scale(2.0f, -2.0f);
  209. Vector2 offset(-1.0f, 1.0f);
  210. Matrix4 projection(Matrix4::IDENTITY);
  211. projection.m00_ = scale.x_;
  212. projection.m03_ = offset.x_;
  213. projection.m11_ = scale.y_;
  214. projection.m13_ = offset.y_;
  215. projection.m22_ = 1.0f;
  216. projection.m23_ = 0.0f;
  217. projection.m33_ = 1.0f;
  218. graphics_->ClearParameterSources();
  219. graphics_->ResetRenderTargets();
  220. graphics_->SetAlphaTest(false);
  221. graphics_->SetCullMode(CULL_CCW);
  222. graphics_->SetDepthTest(CMP_ALWAYS);
  223. graphics_->SetDepthWrite(false);
  224. graphics_->SetFillMode(FILL_SOLID);
  225. graphics_->SetStencilTest(false);
  226. ShaderVariation* ps = 0;
  227. ShaderVariation* vs = 0;
  228. unsigned alphaFormat = Graphics::GetAlphaFormat();
  229. for (unsigned i = 0; i < batches_.Size(); ++i)
  230. {
  231. // Choose shaders here so that UIBatch does not need to look up shaders each time
  232. if (!batches_[i].texture_)
  233. {
  234. ps = noTexturePS_;
  235. vs = noTextureVS_;
  236. }
  237. else
  238. {
  239. // If texture contains only an alpha channel, interpret it as alpha (for fonts)
  240. vs = diffTextureVS_;
  241. if (batches_[i].texture_->GetFormat() == alphaFormat)
  242. ps = alphaTexturePS_;
  243. else
  244. ps = diffTexturePS_;
  245. }
  246. graphics_->SetShaders(vs, ps);
  247. if (graphics_->NeedParameterUpdate(VSP_MODEL, this))
  248. graphics_->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
  249. if (graphics_->NeedParameterUpdate(VSP_VIEWPROJ, this))
  250. graphics_->SetShaderParameter(VSP_VIEWPROJ, projection);
  251. if (graphics_->NeedParameterUpdate(PSP_MATDIFFCOLOR, this))
  252. graphics_->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
  253. batches_[i].Draw(graphics_);
  254. }
  255. }
  256. SharedPtr<UIElement> UI::LoadLayout(XMLFile* file, XMLFile* styleFile)
  257. {
  258. PROFILE(LoadUILayout);
  259. SharedPtr<UIElement> root;
  260. if (!file)
  261. {
  262. LOGERROR("Null UI layout XML file");
  263. return root;
  264. }
  265. LOGDEBUG("Loading UI layout " + file->GetName());
  266. XMLElement rootElem = file->GetRoot("element");
  267. if (!rootElem)
  268. {
  269. LOGERROR("No root UI element in " + file->GetName());
  270. return root;
  271. }
  272. String type = rootElem.GetString("type");
  273. root = DynamicCast<UIElement>(context_->CreateObject(ShortStringHash(type)));
  274. if (!root)
  275. {
  276. LOGERROR("Could not create UI element " + type);
  277. return root;
  278. }
  279. root->SetName(rootElem.GetString("name"));
  280. String styleName = rootElem.HasAttribute("style") ? rootElem.GetString("style") : rootElem.GetString("type");
  281. // First set the base style from the style file if exists, then apply UI layout overrides
  282. if (styleFile)
  283. root->SetStyle(styleFile, styleName);
  284. root->SetStyle(rootElem);
  285. // Load rest of the elements recursively
  286. LoadLayout(root, rootElem, styleFile);
  287. return root;
  288. }
  289. void UI::SetClipBoardText(const String& text)
  290. {
  291. clipBoard_ = text;
  292. }
  293. UIElement* UI::GetElementAt(const IntVector2& position, bool activeOnly)
  294. {
  295. if (!rootElement_)
  296. return 0;
  297. UIElement* result = 0;
  298. GetElementAt(result, rootElement_, position, activeOnly);
  299. return result;
  300. }
  301. UIElement* UI::GetElementAt(int x, int y, bool activeOnly)
  302. {
  303. return GetElementAt(IntVector2(x, y), activeOnly);
  304. }
  305. UIElement* UI::GetFocusElement() const
  306. {
  307. if (!rootElement_)
  308. return 0;
  309. PODVector<UIElement*> allChildren = rootElement_->GetChildren(true);
  310. for (PODVector<UIElement*>::Iterator i = allChildren.Begin(); i != allChildren.End(); ++i)
  311. {
  312. if ((*i)->HasFocus())
  313. return *i;
  314. }
  315. return 0;
  316. }
  317. UIElement* UI::GetFrontElement() const
  318. {
  319. if (!rootElement_)
  320. return 0;
  321. PODVector<UIElement*> rootChildren = rootElement_->GetChildren(false);
  322. int maxPriority = M_MIN_INT;
  323. UIElement* front = 0;
  324. for (unsigned i = 0; i < rootChildren.Size(); ++i)
  325. {
  326. // Do not take into account input-disabled elements, hidden elements or those that are always in the front
  327. if (!rootChildren[i]->IsActive() || !rootChildren[i]->IsVisible() || !rootChildren[i]->GetBringToBack())
  328. continue;
  329. int priority = rootChildren[i]->GetPriority();
  330. if (priority > maxPriority)
  331. {
  332. maxPriority = priority;
  333. front = rootChildren[i];
  334. }
  335. }
  336. return front;
  337. }
  338. IntVector2 UI::GetCursorPosition()
  339. {
  340. if (!cursor_)
  341. return IntVector2::ZERO;
  342. else
  343. return cursor_->GetPosition();
  344. }
  345. void UI::Initialize()
  346. {
  347. Graphics* graphics = GetSubsystem<Graphics>();
  348. ResourceCache* cache = GetSubsystem<ResourceCache>();
  349. if (!graphics || !graphics->IsInitialized() || !cache)
  350. return;
  351. PROFILE(InitUI);
  352. graphics_ = graphics;
  353. cache_ = cache;
  354. rootElement_ = new UIElement(context_);
  355. rootElement_->SetSize(graphics->GetWidth(), graphics->GetHeight());
  356. #ifdef USE_OPENGL
  357. Shader* basicVS = cache->GetResource<Shader>("Shaders/GLSL/Basic.vert");
  358. Shader* basicPS = cache->GetResource<Shader>("Shaders/GLSL/Basic.frag");
  359. #else
  360. Shader* basicVS = cache->GetResource<Shader>("Shaders/SM2/Basic.vs2");
  361. Shader* basicPS = cache->GetResource<Shader>("Shaders/SM2/Basic.ps2");
  362. #endif
  363. if (basicVS && basicPS)
  364. {
  365. noTextureVS_ = basicVS->GetVariation("VCol");
  366. diffTextureVS_ = basicVS->GetVariation("DiffVCol");
  367. noTexturePS_ = basicPS->GetVariation("VCol");
  368. diffTexturePS_ = basicPS->GetVariation("DiffVCol");
  369. alphaTexturePS_ = basicPS->GetVariation("AlphaVCol");
  370. }
  371. LOGINFO("Initialized user interface");
  372. initialized_ = true;
  373. }
  374. void UI::Update(float timeStep, UIElement* element)
  375. {
  376. element->Update(timeStep);
  377. const PODVector<UIElement*> children = element->GetChildren();
  378. for (PODVector<UIElement*>::ConstIterator i = children.Begin(); i != children.End(); ++i)
  379. Update(timeStep, *i);
  380. }
  381. void UI::GetBatches(UIElement* element, IntRect currentScissor)
  382. {
  383. // Set clipping scissor for child elements. No need to draw if zero size
  384. element->AdjustScissor(currentScissor);
  385. if (currentScissor.left_ == currentScissor.right_ || currentScissor.top_ == currentScissor.bottom_)
  386. return;
  387. PODVector<UIElement*> children = element->GetChildren();
  388. if (children.Empty())
  389. return;
  390. Sort(children.Begin(), children.End(), CompareUIElements);
  391. // For non-root elements draw all children of same priority before recursing into their children: assumption is that they have
  392. // same renderstate
  393. PODVector<UIElement*>::ConstIterator i = children.Begin();
  394. if (element != rootElement_)
  395. {
  396. PODVector<UIElement*>::ConstIterator j = i;
  397. int currentPriority = children.Front()->GetPriority();
  398. while (i != children.End())
  399. {
  400. while (j != children.End() && (*j)->GetPriority() == currentPriority)
  401. {
  402. if ((*j)->IsVisible())
  403. (*j)->GetBatches(batches_, quads_, currentScissor);
  404. ++j;
  405. }
  406. // Now recurse into the children
  407. while (i != j)
  408. {
  409. if ((*i)->IsVisible())
  410. GetBatches(*i, currentScissor);
  411. ++i;
  412. }
  413. if (i != children.End())
  414. currentPriority = (*i)->GetPriority();
  415. }
  416. }
  417. // On the root level draw each element and its children immediately after to avoid artifacts
  418. else
  419. {
  420. while (i != children.End())
  421. {
  422. if ((*i)->IsVisible())
  423. {
  424. (*i)->GetBatches(batches_, quads_, currentScissor);
  425. GetBatches(*i, currentScissor);
  426. }
  427. ++i;
  428. }
  429. }
  430. }
  431. void UI::GetElementAt(UIElement*& result, UIElement* current, const IntVector2& position, bool activeOnly)
  432. {
  433. if (!current)
  434. return;
  435. // Get children from lowest priority to highest
  436. PODVector<UIElement*> children = current->GetChildren(false);
  437. Sort(children.Begin(), children.End(), CompareUIElements);
  438. for (PODVector<UIElement*>::ConstIterator i = children.Begin(); i != children.End(); ++i)
  439. {
  440. UIElement* element = *i;
  441. if (element != cursor_.RawPtr() && element->IsVisible())
  442. {
  443. if (element->IsInside(position, true))
  444. {
  445. // Store the current result, then recurse into its children. Because children
  446. // are sorted from lowest to highest priority, the topmost match should remain
  447. if (element->IsActive() || !activeOnly)
  448. result = element;
  449. }
  450. if (element->IsInsideCombined(position, true))
  451. GetElementAt(result, element, position, activeOnly);
  452. }
  453. }
  454. }
  455. void UI::LoadLayout(UIElement* current, const XMLElement& elem, XMLFile* styleFile)
  456. {
  457. XMLElement childElem = elem.GetChild("element");
  458. while (childElem)
  459. {
  460. // Create element
  461. String type = childElem.GetString("type");
  462. SharedPtr<UIElement> child = DynamicCast<UIElement>(context_->CreateObject(ShortStringHash(type)));
  463. if (!child)
  464. {
  465. LOGERROR("Could not create UI element " + type);
  466. childElem = childElem.GetNext("element");
  467. continue;
  468. }
  469. child->SetName(childElem.GetString("name"));
  470. // Add to the hierarchy
  471. current->AddChild(child);
  472. // First set the base style from the style file if exists, then apply UI layout overrides
  473. String styleName = childElem.HasAttribute("style") ? childElem.GetString("style") : childElem.GetString("type");
  474. if (styleFile)
  475. child->SetStyle(styleFile, styleName);
  476. child->SetStyle(childElem);
  477. // Load the children recursively
  478. LoadLayout(child, childElem, styleFile);
  479. childElem = childElem.GetNext("element");
  480. }
  481. }
  482. void UI::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  483. {
  484. using namespace ScreenMode;
  485. if (!initialized_)
  486. Initialize();
  487. else
  488. rootElement_->SetSize(eventData[P_WIDTH].GetInt(), eventData[P_HEIGHT].GetInt());
  489. }
  490. void UI::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  491. {
  492. using namespace MouseMove;
  493. mouseButtons_ = eventData[P_BUTTONS].GetInt();
  494. qualifiers_ = eventData[P_QUALIFIERS].GetInt();
  495. if (cursor_)
  496. {
  497. const IntVector2& rootSize = rootElement_->GetSize();
  498. // Move cursor only when visible
  499. if (cursor_->IsVisible())
  500. {
  501. IntVector2 pos = cursor_->GetPosition();
  502. pos.x_ += eventData[P_DX].GetInt();
  503. pos.y_ += eventData[P_DY].GetInt();
  504. pos.x_ = Clamp(pos.x_, 0, rootSize.x_ - 1);
  505. pos.y_ = Clamp(pos.y_, 0, rootSize.y_ - 1);
  506. cursor_->SetPosition(pos);
  507. }
  508. if (dragElement_ && mouseButtons_)
  509. {
  510. IntVector2 pos = cursor_->GetPosition();
  511. if (dragElement_->IsActive() && dragElement_->IsVisible())
  512. dragElement_->OnDragMove(dragElement_->ScreenToElement(pos), pos, mouseButtons_, qualifiers_, cursor_);
  513. else
  514. {
  515. dragElement_->OnDragEnd(dragElement_->ScreenToElement(pos), pos, cursor_);
  516. dragElement_.Reset();
  517. }
  518. }
  519. }
  520. }
  521. void UI::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  522. {
  523. mouseButtons_ = eventData[MouseButtonDown::P_BUTTONS].GetInt();
  524. qualifiers_ = eventData[MouseButtonDown::P_QUALIFIERS].GetInt();
  525. int button = eventData[MouseButtonDown::P_BUTTON].GetInt();
  526. if (cursor_ && cursor_->IsVisible())
  527. {
  528. IntVector2 pos = cursor_->GetPosition();
  529. WeakPtr<UIElement> element(GetElementAt(pos));
  530. if (element)
  531. {
  532. // Handle focusing & bringing to front
  533. if (button == MOUSEB_LEFT)
  534. {
  535. SetFocusElement(element);
  536. element->BringToFront();
  537. }
  538. // Handle click
  539. element->OnClick(element->ScreenToElement(pos), pos, mouseButtons_, qualifiers_, cursor_);
  540. // Handle start of drag. OnClick() may have caused destruction of the element, so check the pointer again
  541. if (element && !dragElement_ && mouseButtons_ == MOUSEB_LEFT)
  542. {
  543. dragElement_ = element;
  544. element->OnDragStart(element->ScreenToElement(pos), pos, mouseButtons_, qualifiers_, cursor_);
  545. }
  546. }
  547. else
  548. {
  549. // If clicked over no element, or a disabled element, try to lose focus
  550. defocusElement_ = GetFocusElement();
  551. }
  552. using namespace UIMouseClick;
  553. VariantMap eventData;
  554. eventData[UIMouseClick::P_ELEMENT] = (void*)element.RawPtr();
  555. eventData[UIMouseClick::P_X] = pos.x_;
  556. eventData[UIMouseClick::P_Y] = pos.y_;
  557. eventData[UIMouseClick::P_BUTTON] = button;
  558. eventData[UIMouseClick::P_BUTTONS] = mouseButtons_;
  559. eventData[UIMouseClick::P_QUALIFIERS] = qualifiers_;
  560. SendEvent(E_UIMOUSECLICK, eventData);
  561. }
  562. }
  563. void UI::HandleMouseButtonUp(StringHash eventType, VariantMap& eventData)
  564. {
  565. using namespace MouseButtonUp;
  566. mouseButtons_ = eventData[P_BUTTONS].GetInt();
  567. qualifiers_ = eventData[P_QUALIFIERS].GetInt();
  568. if (cursor_ && (cursor_->IsVisible())|| (dragElement_))
  569. {
  570. IntVector2 pos = cursor_->GetPosition();
  571. if (dragElement_ && !mouseButtons_)
  572. {
  573. if (dragElement_->IsActive() && dragElement_->IsVisible())
  574. {
  575. dragElement_->OnDragEnd(dragElement_->ScreenToElement(pos), pos, cursor_);
  576. // Drag and drop finish
  577. bool dragSource = dragElement_ && (dragElement_->GetDragDropMode() & DD_SOURCE) != 0;
  578. if (dragSource)
  579. {
  580. WeakPtr<UIElement> target(GetElementAt(pos));
  581. bool dragTarget = target && (target->GetDragDropMode() & DD_TARGET) != 0;
  582. bool dragDropFinish = dragSource && dragTarget && target != dragElement_;
  583. if (dragDropFinish)
  584. {
  585. bool accept = target->OnDragDropFinish(dragElement_);
  586. // OnDragDropFinish() may have caused destruction of the elements, so check the pointers again
  587. if (accept && dragElement_ && target)
  588. {
  589. using namespace DragDropFinish;
  590. VariantMap eventData;
  591. eventData[P_SOURCE] = (void*)dragElement_.RawPtr();
  592. eventData[P_TARGET] = (void*)target.RawPtr();
  593. eventData[P_ACCEPT] = accept;
  594. SendEvent(E_DRAGDROPFINISH, eventData);
  595. }
  596. }
  597. }
  598. }
  599. dragElement_.Reset();
  600. }
  601. }
  602. }
  603. void UI::HandleMouseWheel(StringHash eventType, VariantMap& eventData)
  604. {
  605. using namespace MouseWheel;
  606. mouseButtons_ = eventData[P_BUTTONS].GetInt();
  607. qualifiers_ = eventData[P_QUALIFIERS].GetInt();
  608. int delta = eventData[P_WHEEL].GetInt();
  609. UIElement* element = GetFocusElement();
  610. if (element)
  611. element->OnWheel(delta, mouseButtons_, qualifiers_);
  612. }
  613. void UI::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  614. {
  615. using namespace KeyDown;
  616. mouseButtons_ = eventData[P_BUTTONS].GetInt();
  617. qualifiers_ = eventData[P_QUALIFIERS].GetInt();
  618. int key = eventData[P_KEY].GetInt();
  619. UIElement* element = GetFocusElement();
  620. if (element)
  621. {
  622. // Switch focus between focusable elements in the same top level window
  623. if (key == KEY_TAB)
  624. {
  625. UIElement* topLevel = element->GetParent();
  626. while (topLevel && topLevel->GetParent() != rootElement_)
  627. topLevel = topLevel->GetParent();
  628. if (topLevel)
  629. {
  630. PODVector<UIElement*> children = topLevel->GetChildren(true);
  631. for (PODVector<UIElement*>::Iterator i = children.Begin(); i != children.End();)
  632. {
  633. if ((*i)->GetFocusMode() < FM_FOCUSABLE)
  634. i = children.Erase(i);
  635. else
  636. ++i;
  637. }
  638. for (unsigned i = 0; i < children.Size(); ++i)
  639. {
  640. if (children[i] == element)
  641. {
  642. UIElement* next = children[(i + 1) % children.Size()];
  643. SetFocusElement(next);
  644. return;
  645. }
  646. }
  647. }
  648. }
  649. // Defocus the element
  650. else if (key == KEY_ESC && element->GetFocusMode() == FM_FOCUSABLE_DEFOCUSABLE)
  651. defocusElement_ = element;
  652. // If none of the special keys, pass the key to the focused element
  653. else
  654. element->OnKey(key, mouseButtons_, qualifiers_);
  655. }
  656. }
  657. void UI::HandleChar(StringHash eventType, VariantMap& eventData)
  658. {
  659. using namespace Char;
  660. mouseButtons_ = eventData[P_BUTTONS].GetInt();
  661. qualifiers_ = eventData[P_QUALIFIERS].GetInt();
  662. UIElement* element = GetFocusElement();
  663. if (element)
  664. element->OnChar(eventData[P_CHAR].GetInt(), mouseButtons_, qualifiers_);
  665. }
  666. void UI::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  667. {
  668. if (initialized_)
  669. {
  670. using namespace PostUpdate;
  671. Update(eventData[P_TIMESTEP].GetFloat());
  672. }
  673. }
  674. void UI::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
  675. {
  676. if (initialized_)
  677. RenderUpdate();
  678. }
  679. void RegisterUILibrary(Context* context)
  680. {
  681. Font::RegisterObject(context);
  682. UIElement::RegisterObject(context);
  683. BorderImage::RegisterObject(context);
  684. Button::RegisterObject(context);
  685. CheckBox::RegisterObject(context);
  686. Cursor::RegisterObject(context);
  687. Text::RegisterObject(context);
  688. Window::RegisterObject(context);
  689. LineEdit::RegisterObject(context);
  690. Slider::RegisterObject(context);
  691. ScrollBar::RegisterObject(context);
  692. ScrollView::RegisterObject(context);
  693. ListView::RegisterObject(context);
  694. Menu::RegisterObject(context);
  695. DropDownList::RegisterObject(context);
  696. FileSelector::RegisterObject(context);
  697. }