UI.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. //
  2. // Copyright (c) 2008-2013 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 "CheckBox.h"
  24. #include "Context.h"
  25. #include "CoreEvents.h"
  26. #include "Cursor.h"
  27. #include "DropDownList.h"
  28. #include "FileSelector.h"
  29. #include "Font.h"
  30. #include "Graphics.h"
  31. #include "GraphicsEvents.h"
  32. #include "Input.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 "Renderer.h"
  40. #include "ScrollBar.h"
  41. #include "Shader.h"
  42. #include "ShaderVariation.h"
  43. #include "Slider.h"
  44. #include "Sort.h"
  45. #include "Sprite.h"
  46. #include "Text.h"
  47. #include "Text3D.h"
  48. #include "Texture2D.h"
  49. #include "UI.h"
  50. #include "UIEvents.h"
  51. #include "VertexBuffer.h"
  52. #include "Window.h"
  53. #include "DebugNew.h"
  54. namespace Urho3D
  55. {
  56. ShortStringHash VAR_ORIGIN("Origin");
  57. const ShortStringHash VAR_ORIGINAL_PARENT("OriginalParent");
  58. const ShortStringHash VAR_ORIGINAL_CHILD_INDEX("OriginalChildIndex");
  59. const ShortStringHash VAR_PARENT_CHANGED("ParentChanged");
  60. const float DEFAULT_DOUBLECLICK_INTERVAL = 0.5f;
  61. const char* UI_CATEGORY = "UI";
  62. UI::UI(Context* context) :
  63. Object(context),
  64. rootElement_(new UIElement(context)),
  65. rootModalElement_(new UIElement(context)),
  66. mouseButtons_(0),
  67. qualifiers_(0),
  68. doubleClickInterval_(DEFAULT_DOUBLECLICK_INTERVAL),
  69. initialized_(false),
  70. usingTouchInput_(false),
  71. #ifdef WIN32
  72. nonFocusedMouseWheel_(false), // Default MS Windows behaviour
  73. #else
  74. nonFocusedMouseWheel_(true), // Default Mac OS X and Linux behaviour
  75. #endif
  76. nonModalBatchSize_(0)
  77. {
  78. rootElement_->SetTraversalMode(TM_DEPTH_FIRST);
  79. rootModalElement_->SetTraversalMode(TM_DEPTH_FIRST);
  80. clickTimer_ = new Timer();
  81. // Register UI library object factories
  82. RegisterUILibrary(context_);
  83. SubscribeToEvent(E_SCREENMODE, HANDLER(UI, HandleScreenMode));
  84. SubscribeToEvent(E_MOUSEBUTTONDOWN, HANDLER(UI, HandleMouseButtonDown));
  85. SubscribeToEvent(E_MOUSEBUTTONUP, HANDLER(UI, HandleMouseButtonUp));
  86. SubscribeToEvent(E_MOUSEMOVE, HANDLER(UI, HandleMouseMove));
  87. SubscribeToEvent(E_MOUSEWHEEL, HANDLER(UI, HandleMouseWheel));
  88. SubscribeToEvent(E_TOUCHBEGIN, HANDLER(UI, HandleTouchBegin));
  89. SubscribeToEvent(E_TOUCHEND, HANDLER(UI, HandleTouchEnd));
  90. SubscribeToEvent(E_TOUCHMOVE, HANDLER(UI, HandleTouchMove));
  91. SubscribeToEvent(E_KEYDOWN, HANDLER(UI, HandleKeyDown));
  92. SubscribeToEvent(E_CHAR, HANDLER(UI, HandleChar));
  93. // Try to initialize right now, but skip if screen mode is not yet set
  94. Initialize();
  95. }
  96. UI::~UI()
  97. {
  98. delete clickTimer_;
  99. }
  100. void UI::SetCursor(Cursor* cursor)
  101. {
  102. // Remove old cursor (if any) and set new
  103. if (cursor_)
  104. {
  105. rootElement_->RemoveChild(cursor_);
  106. cursor_.Reset();
  107. }
  108. if (cursor)
  109. {
  110. rootElement_->AddChild(cursor);
  111. cursor_ = cursor;
  112. IntVector2 pos = cursor_->GetPosition();
  113. const IntVector2& rootSize = rootElement_->GetSize();
  114. pos.x_ = Clamp(pos.x_, 0, rootSize.x_ - 1);
  115. pos.y_ = Clamp(pos.y_, 0, rootSize.y_ - 1);
  116. cursor_->SetPosition(pos);
  117. }
  118. }
  119. void UI::SetFocusElement(UIElement* element)
  120. {
  121. using namespace FocusChanged;
  122. VariantMap eventData;
  123. eventData[P_CLICKEDELEMENT] = (void*)element;
  124. if (element)
  125. {
  126. // Return if already has focus
  127. if (focusElement_ == element)
  128. return;
  129. // Only allow child elements of the modal element to receive focus
  130. if (HasModalElement())
  131. {
  132. UIElement* topLevel = element->GetParent();
  133. while (topLevel && topLevel->GetParent() != rootElement_)
  134. topLevel = topLevel->GetParent();
  135. if (topLevel) // If parented to non-modal root then ignore
  136. return;
  137. }
  138. // Search for an element in the hierarchy that can alter focus. If none found, exit
  139. element = GetFocusableElement(element);
  140. if (!element)
  141. return;
  142. }
  143. // Remove focus from the old element
  144. if (focusElement_)
  145. {
  146. UIElement* oldFocusElement = focusElement_;
  147. focusElement_.Reset();
  148. VariantMap focusEventData;
  149. focusEventData[Defocused::P_ELEMENT] = oldFocusElement;
  150. oldFocusElement->SendEvent(E_DEFOCUSED, focusEventData);
  151. }
  152. // Then set focus to the new
  153. if (element && element->GetFocusMode() >= FM_FOCUSABLE)
  154. {
  155. focusElement_ = element;
  156. VariantMap focusEventData;
  157. focusEventData[Focused::P_ELEMENT] = element;
  158. element->SendEvent(E_FOCUSED, focusEventData);
  159. }
  160. eventData[P_ELEMENT] = (void*)element;
  161. SendEvent(E_FOCUSCHANGED, eventData);
  162. }
  163. bool UI::SetModalElement(UIElement* modalElement, bool enable)
  164. {
  165. if (!modalElement)
  166. return false;
  167. // Currently only allow modal window
  168. if (modalElement->GetType() != Window::GetTypeStatic())
  169. return false;
  170. assert(rootModalElement_);
  171. UIElement* currParent = modalElement->GetParent();
  172. if (enable)
  173. {
  174. // Make sure it is not already the child of the root modal element
  175. if (currParent == rootModalElement_)
  176. return false;
  177. // Adopt modal root as parent
  178. modalElement->SetVar(VAR_ORIGINAL_PARENT, currParent);
  179. modalElement->SetVar(VAR_ORIGINAL_CHILD_INDEX, currParent ? currParent->FindChild(modalElement) : M_MAX_UNSIGNED);
  180. modalElement->SetParent(rootModalElement_);
  181. // If it is a popup element, bring along its top-level parent
  182. UIElement* originElement = static_cast<UIElement*>(modalElement->GetVar(VAR_ORIGIN).GetPtr());
  183. if (originElement)
  184. {
  185. UIElement* element = originElement;
  186. while (element && element->GetParent() != rootElement_)
  187. element = element->GetParent();
  188. if (element)
  189. {
  190. originElement->SetVar(VAR_PARENT_CHANGED, element);
  191. UIElement* oriParent = element->GetParent();
  192. element->SetVar(VAR_ORIGINAL_PARENT, oriParent);
  193. element->SetVar(VAR_ORIGINAL_CHILD_INDEX, oriParent ? oriParent->FindChild(element) : M_MAX_UNSIGNED);
  194. element->SetParent(rootModalElement_);
  195. }
  196. }
  197. return true;
  198. }
  199. else
  200. {
  201. // Only the modal element can disable itself
  202. if (currParent != rootModalElement_)
  203. return false;
  204. // Revert back to original parent
  205. modalElement->SetParent(static_cast<UIElement*>(modalElement->GetVar(VAR_ORIGINAL_PARENT).GetPtr()), modalElement->GetVar(VAR_ORIGINAL_CHILD_INDEX).GetUInt());
  206. VariantMap& vars = const_cast<VariantMap&>(modalElement->GetVars());
  207. vars.Erase(VAR_ORIGINAL_PARENT);
  208. vars.Erase(VAR_ORIGINAL_CHILD_INDEX);
  209. // If it is a popup element, revert back its top-level parent
  210. UIElement* originElement = static_cast<UIElement*>(modalElement->GetVar(VAR_ORIGIN).GetPtr());
  211. if (originElement)
  212. {
  213. UIElement* element = static_cast<UIElement*>(originElement->GetVar(VAR_PARENT_CHANGED).GetPtr());
  214. if (element)
  215. {
  216. const_cast<VariantMap&>(originElement->GetVars()).Erase(VAR_PARENT_CHANGED);
  217. element->SetParent(static_cast<UIElement*>(element->GetVar(VAR_ORIGINAL_PARENT).GetPtr()), element->GetVar(VAR_ORIGINAL_CHILD_INDEX).GetUInt());
  218. vars = const_cast<VariantMap&>(element->GetVars());
  219. vars.Erase(VAR_ORIGINAL_PARENT);
  220. vars.Erase(VAR_ORIGINAL_CHILD_INDEX);
  221. }
  222. }
  223. return true;
  224. }
  225. }
  226. void UI::Clear()
  227. {
  228. rootElement_->RemoveAllChildren();
  229. rootModalElement_->RemoveAllChildren();
  230. if (cursor_)
  231. rootElement_->AddChild(cursor_);
  232. }
  233. void UI::Update(float timeStep)
  234. {
  235. assert(rootElement_ && rootModalElement_);
  236. PROFILE(UpdateUI);
  237. IntVector2 cursorPos;
  238. bool cursorVisible;
  239. GetCursorPositionAndVisible(cursorPos, cursorVisible);
  240. // Mouse hover
  241. if (!usingTouchInput_ && cursorVisible)
  242. {
  243. WeakPtr<UIElement> element(GetElementAt(cursorPos));
  244. bool dragSource = dragElement_ && (dragElement_->GetDragDropMode() & DD_SOURCE) != 0;
  245. bool dragTarget = element && (element->GetDragDropMode() & DD_TARGET) != 0;
  246. bool dragDropTest = dragSource && dragTarget && element != dragElement_;
  247. // Hover effect
  248. // If a drag is going on, transmit hover only to the element being dragged, unless it's a drop target
  249. if (element && element->IsEnabled())
  250. {
  251. if (!dragElement_ || dragElement_ == element || dragDropTest)
  252. element->OnHover(element->ScreenToElement(cursorPos), cursorPos, mouseButtons_, qualifiers_, cursor_);
  253. }
  254. else
  255. SetCursorShape(CS_NORMAL);
  256. // Drag and drop test
  257. if (dragDropTest)
  258. {
  259. bool accept = element->OnDragDropTest(dragElement_);
  260. if (accept)
  261. {
  262. using namespace DragDropTest;
  263. VariantMap eventData;
  264. eventData[P_SOURCE] = (void*)dragElement_.Get();
  265. eventData[P_TARGET] = (void*)element.Get();
  266. eventData[P_ACCEPT] = accept;
  267. SendEvent(E_DRAGDROPTEST, eventData);
  268. accept = eventData[P_ACCEPT].GetBool();
  269. }
  270. SetCursorShape(accept ? CS_ACCEPTDROP : CS_REJECTDROP);
  271. }
  272. else if (dragSource)
  273. SetCursorShape(dragElement_ == element ? CS_ACCEPTDROP : CS_REJECTDROP);
  274. }
  275. // Touch hover
  276. Input* input = GetSubsystem<Input>();
  277. unsigned numTouches = input->GetNumTouches();
  278. for (unsigned i = 0; i < numTouches; ++i)
  279. {
  280. TouchState* touch = input->GetTouch(i);
  281. UIElement* element = GetElementAt(touch->position_);
  282. if (element && element->IsEnabled())
  283. element->OnHover(element->ScreenToElement(touch->position_), touch->position_, MOUSEB_LEFT, 0, 0);
  284. }
  285. Update(timeStep, rootElement_);
  286. Update(timeStep, rootModalElement_);
  287. }
  288. void UI::RenderUpdate()
  289. {
  290. assert(rootElement_ && rootModalElement_ && graphics_);
  291. PROFILE(GetUIBatches);
  292. // If the OS cursor is visible, do not render the UI's own cursor
  293. bool osCursorVisible = GetSubsystem<Input>()->IsMouseVisible();
  294. bool uiCursorVisible = false;
  295. if (osCursorVisible && cursor_)
  296. {
  297. uiCursorVisible = cursor_->IsVisible();
  298. cursor_->SetTempVisible(false);
  299. }
  300. // Get rendering batches from the non-modal UI elements
  301. batches_.Clear();
  302. vertexData_.Clear();
  303. const IntVector2& rootSize = rootElement_->GetSize();
  304. IntRect currentScissor = IntRect(0, 0, rootSize.x_, rootSize.y_);
  305. GetBatches(rootElement_, currentScissor);
  306. // Save the batch size of the non-modal batches for later use
  307. nonModalBatchSize_ = batches_.Size();
  308. // Get rendering batches from the modal UI elements
  309. GetBatches(rootModalElement_, currentScissor);
  310. // Restore UI cursor visibility state
  311. if (osCursorVisible && cursor_)
  312. cursor_->SetTempVisible(uiCursorVisible);
  313. }
  314. void UI::Render()
  315. {
  316. PROFILE(RenderUI);
  317. SetVertexData(vertexBuffer_, vertexData_);
  318. SetVertexData(debugVertexBuffer_, debugVertexData_);
  319. // Render non-modal batches
  320. Render(vertexBuffer_, batches_, 0, nonModalBatchSize_);
  321. // Render debug draw
  322. Render(debugVertexBuffer_, debugDrawBatches_, 0, debugDrawBatches_.Size());
  323. // Render modal batches
  324. Render(vertexBuffer_, batches_, nonModalBatchSize_, batches_.Size());
  325. // Clear the debug draw batches and data
  326. debugDrawBatches_.Clear();
  327. debugVertexData_.Clear();
  328. }
  329. void UI::DebugDraw(UIElement* element)
  330. {
  331. if (element)
  332. {
  333. const IntVector2& rootSize = rootElement_->GetSize();
  334. element->GetDebugDrawBatches(debugDrawBatches_, debugVertexData_, IntRect(0, 0, rootSize.x_, rootSize.y_));
  335. }
  336. }
  337. SharedPtr<UIElement> UI::LoadLayout(Deserializer& source, XMLFile* styleFile)
  338. {
  339. SharedPtr<XMLFile> xml(new XMLFile(context_));
  340. if (!xml->Load(source))
  341. return SharedPtr<UIElement>();
  342. else
  343. return LoadLayout(xml, styleFile);
  344. }
  345. SharedPtr<UIElement> UI::LoadLayout(XMLFile* file, XMLFile* styleFile)
  346. {
  347. PROFILE(LoadUILayout);
  348. SharedPtr<UIElement> root;
  349. if (!file)
  350. {
  351. LOGERROR("Null UI layout XML file");
  352. return root;
  353. }
  354. LOGDEBUG("Loading UI layout " + file->GetName());
  355. XMLElement rootElem = file->GetRoot("element");
  356. if (!rootElem)
  357. {
  358. LOGERROR("No root UI element in " + file->GetName());
  359. return root;
  360. }
  361. String typeName = rootElem.GetAttribute("type");
  362. if (typeName.Empty())
  363. typeName = "UIElement";
  364. root = DynamicCast<UIElement>(context_->CreateObject(typeName));
  365. if (!root)
  366. {
  367. LOGERROR("Could not create unknown UI element " + typeName);
  368. return root;
  369. }
  370. // Use default style file of the root element if it has one
  371. if (!styleFile)
  372. styleFile = rootElement_->GetDefaultStyle(false);
  373. // Set it as default for later use by children elements
  374. if (styleFile)
  375. root->SetDefaultStyle(styleFile);
  376. root->LoadXML(rootElem, styleFile);
  377. return root;
  378. }
  379. bool UI::SaveLayout(Serializer& dest, UIElement* element)
  380. {
  381. PROFILE(SaveUILayout);
  382. return element && element->SaveXML(dest);
  383. }
  384. void UI::SetClipBoardText(const String& text)
  385. {
  386. clipBoard_ = text;
  387. }
  388. void UI::SetDoubleClickInterval(float interval)
  389. {
  390. doubleClickInterval_ = Max(interval, 0.0f);
  391. }
  392. void UI::SetNonFocusedMouseWheel(bool nonFocusedMouseWheel)
  393. {
  394. nonFocusedMouseWheel_ = nonFocusedMouseWheel;
  395. }
  396. UIElement* UI::GetElementAt(const IntVector2& position, bool enabledOnly)
  397. {
  398. UIElement* result = 0;
  399. GetElementAt(result, HasModalElement() ? rootModalElement_ : rootElement_, position, enabledOnly);
  400. return result;
  401. }
  402. UIElement* UI::GetElementAt(int x, int y, bool enabledOnly)
  403. {
  404. return GetElementAt(IntVector2(x, y), enabledOnly);
  405. }
  406. UIElement* UI::GetFrontElement() const
  407. {
  408. const Vector<SharedPtr<UIElement> >& rootChildren = rootElement_->GetChildren();
  409. int maxPriority = M_MIN_INT;
  410. UIElement* front = 0;
  411. for (unsigned i = 0; i < rootChildren.Size(); ++i)
  412. {
  413. // Do not take into account input-disabled elements, hidden elements or those that are always in the front
  414. if (!rootChildren[i]->IsEnabled() || !rootChildren[i]->IsVisible() || !rootChildren[i]->GetBringToBack())
  415. continue;
  416. int priority = rootChildren[i]->GetPriority();
  417. if (priority > maxPriority)
  418. {
  419. maxPriority = priority;
  420. front = rootChildren[i];
  421. }
  422. }
  423. return front;
  424. }
  425. IntVector2 UI::GetCursorPosition() const
  426. {
  427. return cursor_ ? cursor_->GetPosition() : GetSubsystem<Input>()->GetMousePosition();
  428. }
  429. bool UI::HasModalElement() const
  430. {
  431. return rootModalElement_->GetNumChildren() > 0;
  432. }
  433. void UI::Initialize()
  434. {
  435. Graphics* graphics = GetSubsystem<Graphics>();
  436. Renderer* renderer = GetSubsystem<Renderer>();
  437. if (!graphics || !graphics->IsInitialized() || !renderer)
  438. return;
  439. PROFILE(InitUI);
  440. graphics_ = graphics;
  441. rootElement_->SetSize(graphics->GetWidth(), graphics->GetHeight());
  442. rootModalElement_->SetSize(rootElement_->GetSize());
  443. noTextureVS_ = renderer->GetVertexShader("Basic_VCol");
  444. diffTextureVS_ = renderer->GetVertexShader("Basic_DiffVCol");
  445. noTexturePS_ = renderer->GetPixelShader("Basic_VCol");
  446. diffTexturePS_ = renderer->GetPixelShader("Basic_DiffVCol");
  447. diffMaskTexturePS_ = renderer->GetPixelShader("Basic_DiffAlphaMaskVCol");
  448. alphaTexturePS_ = renderer->GetPixelShader("Basic_AlphaVCol");
  449. vertexBuffer_ = new VertexBuffer(context_);
  450. debugVertexBuffer_ = new VertexBuffer(context_);
  451. initialized_ = true;
  452. SubscribeToEvent(E_POSTUPDATE, HANDLER(UI, HandlePostUpdate));
  453. SubscribeToEvent(E_RENDERUPDATE, HANDLER(UI, HandleRenderUpdate));
  454. LOGINFO("Initialized user interface");
  455. }
  456. void UI::Update(float timeStep, UIElement* element)
  457. {
  458. element->Update(timeStep);
  459. const Vector<SharedPtr<UIElement> >& children = element->GetChildren();
  460. for (Vector<SharedPtr<UIElement> >::ConstIterator i = children.Begin(); i != children.End(); ++i)
  461. Update(timeStep, *i);
  462. }
  463. void UI::SetVertexData(VertexBuffer* dest, const PODVector<float>& vertexData)
  464. {
  465. if (vertexData.Empty())
  466. return;
  467. // Update quad geometry into the vertex buffer
  468. // Resize the vertex buffer first if too small or much too large
  469. unsigned numVertices = vertexData.Size() / UI_VERTEX_SIZE;
  470. if (dest->GetVertexCount() < numVertices || dest->GetVertexCount() > numVertices * 2)
  471. dest->SetSize(numVertices, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1, true);
  472. dest->SetData(&vertexData[0]);
  473. }
  474. void UI::Render(VertexBuffer* buffer, const PODVector<UIBatch>& batches, unsigned batchStart, unsigned batchEnd)
  475. {
  476. // Engine does not render when window is closed or device is lost
  477. assert(graphics_ && graphics_->IsInitialized() && !graphics_->IsDeviceLost());
  478. if (batches.Empty())
  479. return;
  480. Vector2 invScreenSize(1.0f / (float)graphics_->GetWidth(), 1.0f / (float)graphics_->GetHeight());
  481. Vector2 scale(2.0f * invScreenSize.x_, -2.0f * invScreenSize.y_);
  482. Vector2 offset(-1.0f, 1.0f);
  483. Matrix4 projection(Matrix4::IDENTITY);
  484. projection.m00_ = scale.x_;
  485. projection.m03_ = offset.x_;
  486. projection.m11_ = scale.y_;
  487. projection.m13_ = offset.y_;
  488. projection.m22_ = 1.0f;
  489. projection.m23_ = 0.0f;
  490. projection.m33_ = 1.0f;
  491. graphics_->ClearParameterSources();
  492. graphics_->SetCullMode(CULL_CCW);
  493. graphics_->SetDepthTest(CMP_ALWAYS);
  494. graphics_->SetDepthWrite(false);
  495. graphics_->SetStencilTest(false);
  496. graphics_->ResetRenderTargets();
  497. graphics_->SetVertexBuffer(buffer);
  498. ShaderVariation* ps = 0;
  499. ShaderVariation* vs = 0;
  500. unsigned alphaFormat = Graphics::GetAlphaFormat();
  501. for (unsigned i = batchStart; i < batchEnd; ++i)
  502. {
  503. const UIBatch& batch = batches[i];
  504. if (batch.vertexStart_ == batch.vertexEnd_)
  505. continue;
  506. if (!batch.texture_)
  507. {
  508. ps = noTexturePS_;
  509. vs = noTextureVS_;
  510. }
  511. else
  512. {
  513. // If texture contains only an alpha channel, use alpha shader (for fonts)
  514. vs = diffTextureVS_;
  515. if (batch.texture_->GetFormat() == alphaFormat)
  516. ps = alphaTexturePS_;
  517. else if (batch.blendMode_ != BLEND_ALPHA && batch.blendMode_ != BLEND_ADDALPHA && batch.blendMode_ != BLEND_PREMULALPHA)
  518. ps = diffMaskTexturePS_;
  519. else
  520. ps = diffTexturePS_;
  521. }
  522. graphics_->SetShaders(vs, ps);
  523. if (graphics_->NeedParameterUpdate(SP_OBJECTTRANSFORM, this))
  524. graphics_->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
  525. if (graphics_->NeedParameterUpdate(SP_CAMERA, this))
  526. graphics_->SetShaderParameter(VSP_VIEWPROJ, projection);
  527. if (graphics_->NeedParameterUpdate(SP_MATERIAL, this))
  528. graphics_->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
  529. graphics_->SetBlendMode(batch.blendMode_);
  530. graphics_->SetScissorTest(true, batch.scissor_);
  531. graphics_->SetTexture(0, batch.texture_);
  532. graphics_->Draw(TRIANGLE_LIST, batch.vertexStart_ / UI_VERTEX_SIZE, (batch.vertexEnd_ - batch.vertexStart_) /
  533. UI_VERTEX_SIZE);
  534. }
  535. }
  536. void UI::GetBatches(UIElement* element, IntRect currentScissor)
  537. {
  538. // Set clipping scissor for child elements. No need to draw if zero size
  539. element->AdjustScissor(currentScissor);
  540. if (currentScissor.left_ == currentScissor.right_ || currentScissor.top_ == currentScissor.bottom_)
  541. return;
  542. element->SortChildren();
  543. const Vector<SharedPtr<UIElement> >& children = element->GetChildren();
  544. if (children.Empty())
  545. return;
  546. // For non-root elements draw all children of same priority before recursing into their children: assumption is that they have
  547. // same renderstate
  548. Vector<SharedPtr<UIElement> >::ConstIterator i = children.Begin();
  549. if (element->GetTraversalMode() == TM_BREADTH_FIRST)
  550. {
  551. Vector<SharedPtr<UIElement> >::ConstIterator j = i;
  552. while (i != children.End())
  553. {
  554. int currentPriority = (*i)->GetPriority();
  555. while (j != children.End() && (*j)->GetPriority() == currentPriority)
  556. {
  557. if ((*j)->IsWithinScissor(currentScissor))
  558. (*j)->GetBatches(batches_, vertexData_, currentScissor);
  559. ++j;
  560. }
  561. // Now recurse into the children
  562. while (i != j)
  563. {
  564. if ((*i)->IsVisible())
  565. GetBatches(*i, currentScissor);
  566. ++i;
  567. }
  568. }
  569. }
  570. // On the root level draw each element and its children immediately after to avoid artifacts
  571. else
  572. {
  573. while (i != children.End())
  574. {
  575. if ((*i)->IsWithinScissor(currentScissor))
  576. (*i)->GetBatches(batches_, vertexData_, currentScissor);
  577. if ((*i)->IsVisible())
  578. GetBatches(*i, currentScissor);
  579. ++i;
  580. }
  581. }
  582. }
  583. void UI::GetElementAt(UIElement*& result, UIElement* current, const IntVector2& position, bool enabledOnly)
  584. {
  585. if (!current)
  586. return;
  587. current->SortChildren();
  588. const Vector<SharedPtr<UIElement> >& children = current->GetChildren();
  589. LayoutMode parentLayoutMode = current->GetLayoutMode();
  590. for (unsigned i = 0; i < children.Size(); ++i)
  591. {
  592. UIElement* element = children[i];
  593. bool hasChildren = element->GetNumChildren() > 0;
  594. if (element != cursor_.Get() && element->IsVisible())
  595. {
  596. if (element->IsInside(position, true))
  597. {
  598. // Store the current result, then recurse into its children. Because children
  599. // are sorted from lowest to highest priority, the topmost match should remain
  600. if (element->IsEnabled() || !enabledOnly)
  601. result = element;
  602. if (hasChildren)
  603. GetElementAt(result, element, position, enabledOnly);
  604. // Layout optimization: if the element has no children, can break out after the first match
  605. else if (parentLayoutMode != LM_FREE)
  606. break;
  607. }
  608. else
  609. {
  610. if (hasChildren)
  611. {
  612. if (element->IsInsideCombined(position, true))
  613. GetElementAt(result, element, position, enabledOnly);
  614. }
  615. // Layout optimization: if position is much beyond the visible screen, check how many elements we can skip,
  616. // or if we already passed all visible elements
  617. else if (parentLayoutMode != LM_FREE)
  618. {
  619. if (!i)
  620. {
  621. int screenPos = (parentLayoutMode == LM_HORIZONTAL) ? element->GetScreenPosition().x_ :
  622. element->GetScreenPosition().y_;
  623. int layoutMinSize = current->GetLayoutMinSize();
  624. if (screenPos < 0 && layoutMinSize > 0)
  625. {
  626. unsigned toSkip = -screenPos / layoutMinSize;
  627. if (toSkip > 0)
  628. i += (toSkip - 1);
  629. }
  630. }
  631. else if (parentLayoutMode == LM_HORIZONTAL)
  632. {
  633. if (element->GetScreenPosition().x_ >= rootElement_->GetSize().x_)
  634. break;
  635. }
  636. else if (parentLayoutMode == LM_VERTICAL)
  637. {
  638. if (element->GetScreenPosition().y_ >= rootElement_->GetSize().y_)
  639. break;
  640. }
  641. }
  642. }
  643. }
  644. }
  645. }
  646. UIElement* UI::GetFocusableElement(UIElement* element)
  647. {
  648. while (element)
  649. {
  650. if (element->GetFocusMode() != FM_NOTFOCUSABLE)
  651. break;
  652. element = element->GetParent();
  653. }
  654. return element;
  655. }
  656. void UI::GetCursorPositionAndVisible(IntVector2& pos, bool& visible)
  657. {
  658. if (cursor_)
  659. {
  660. pos = cursor_->GetPosition();
  661. visible = cursor_->IsVisible();
  662. }
  663. else
  664. {
  665. Input* input = GetSubsystem<Input>();
  666. pos = input->GetMousePosition();
  667. visible = input->IsMouseVisible();
  668. }
  669. }
  670. void UI::SetCursorShape(CursorShape shape)
  671. {
  672. if (cursor_)
  673. cursor_->SetShape(shape);
  674. }
  675. void UI::SendDragEvent(StringHash eventType, UIElement* element, const IntVector2& screenPos)
  676. {
  677. if (!element)
  678. return;
  679. IntVector2 relativePos = element->ScreenToElement(screenPos);
  680. using namespace DragBegin;
  681. VariantMap eventData;
  682. eventData[P_ELEMENT] = (void*)element;
  683. eventData[P_X] = screenPos.x_;
  684. eventData[P_Y] = screenPos.y_;
  685. eventData[P_ELEMENTX] = relativePos.x_;
  686. eventData[P_ELEMENTY] = relativePos.y_;
  687. element->SendEvent(eventType, eventData);
  688. }
  689. void UI::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  690. {
  691. using namespace ScreenMode;
  692. if (!initialized_)
  693. Initialize();
  694. else
  695. {
  696. rootElement_->SetSize(eventData[P_WIDTH].GetInt(), eventData[P_HEIGHT].GetInt());
  697. rootModalElement_->SetSize(rootElement_->GetSize());
  698. }
  699. }
  700. void UI::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  701. {
  702. mouseButtons_ = eventData[MouseButtonDown::P_BUTTONS].GetInt();
  703. qualifiers_ = eventData[MouseButtonDown::P_QUALIFIERS].GetInt();
  704. usingTouchInput_ = false;
  705. IntVector2 cursorPos;
  706. bool cursorVisible;
  707. GetCursorPositionAndVisible(cursorPos, cursorVisible);
  708. if (cursorVisible)
  709. {
  710. int button = eventData[MouseButtonDown::P_BUTTON].GetInt();
  711. WeakPtr<UIElement> element(GetElementAt(cursorPos));
  712. if (element)
  713. {
  714. // Handle focusing & bringing to front
  715. if (button == MOUSEB_LEFT)
  716. {
  717. SetFocusElement(element);
  718. element->BringToFront();
  719. }
  720. // Handle click
  721. element->OnClick(element->ScreenToElement(cursorPos), cursorPos, mouseButtons_, qualifiers_, cursor_);
  722. // Handle start of drag. OnClick() may have caused destruction of the element, so check the pointer again
  723. if (element && !dragElement_ && mouseButtons_ == MOUSEB_LEFT)
  724. {
  725. dragElement_ = element;
  726. element->OnDragBegin(element->ScreenToElement(cursorPos), cursorPos, mouseButtons_, qualifiers_, cursor_);
  727. SendDragEvent(E_DRAGBEGIN, element, cursorPos);
  728. }
  729. // Fire double click event if element matches and is in time
  730. if (doubleClickElement_ && element == doubleClickElement_ && clickTimer_->GetMSec(true) <
  731. (unsigned)(doubleClickInterval_ * 1000) && lastMouseButtons_ == mouseButtons_)
  732. {
  733. element->OnDoubleClick(element->ScreenToElement(cursorPos), cursorPos, mouseButtons_, qualifiers_, cursor_);
  734. doubleClickElement_.Reset();
  735. using namespace UIMouseDoubleClick;
  736. VariantMap eventData;
  737. eventData[P_ELEMENT] = (void*)element.Get();
  738. eventData[P_X] = cursorPos.x_;
  739. eventData[P_Y] = cursorPos.y_;
  740. eventData[P_BUTTON] = button;
  741. eventData[P_BUTTONS] = mouseButtons_;
  742. eventData[P_QUALIFIERS] = qualifiers_;
  743. element->SendEvent(E_UIMOUSEDOUBLECLICK, eventData);
  744. }
  745. else
  746. {
  747. doubleClickElement_ = element;
  748. clickTimer_->Reset();
  749. }
  750. }
  751. else
  752. {
  753. // If clicked over no element, or a disabled element, lose focus
  754. SetFocusElement(0);
  755. }
  756. VariantMap eventData;
  757. eventData[UIMouseClick::P_ELEMENT] = (void*)element.Get();
  758. eventData[UIMouseClick::P_X] = cursorPos.x_;
  759. eventData[UIMouseClick::P_Y] = cursorPos.y_;
  760. eventData[UIMouseClick::P_BUTTON] = button;
  761. eventData[UIMouseClick::P_BUTTONS] = mouseButtons_;
  762. eventData[UIMouseClick::P_QUALIFIERS] = qualifiers_;
  763. SendEvent(E_UIMOUSECLICK, eventData);
  764. lastMouseButtons_ = mouseButtons_;
  765. }
  766. }
  767. void UI::HandleMouseButtonUp(StringHash eventType, VariantMap& eventData)
  768. {
  769. using namespace MouseButtonUp;
  770. mouseButtons_ = eventData[P_BUTTONS].GetInt();
  771. qualifiers_ = eventData[P_QUALIFIERS].GetInt();
  772. IntVector2 cursorPos;
  773. bool cursorVisible;
  774. GetCursorPositionAndVisible(cursorPos, cursorVisible);
  775. if (cursorVisible || dragElement_)
  776. {
  777. if (dragElement_ && !mouseButtons_)
  778. {
  779. if (dragElement_->IsEnabled() && dragElement_->IsVisible())
  780. {
  781. dragElement_->OnDragEnd(dragElement_->ScreenToElement(cursorPos), cursorPos, cursor_);
  782. SendDragEvent(E_DRAGEND, dragElement_, cursorPos);
  783. // Drag and drop finish
  784. bool dragSource = dragElement_ && (dragElement_->GetDragDropMode() & DD_SOURCE) != 0;
  785. if (dragSource)
  786. {
  787. WeakPtr<UIElement> target(GetElementAt(cursorPos));
  788. bool dragTarget = target && (target->GetDragDropMode() & DD_TARGET) != 0;
  789. bool dragDropFinish = dragSource && dragTarget && target != dragElement_;
  790. if (dragDropFinish)
  791. {
  792. bool accept = target->OnDragDropFinish(dragElement_);
  793. // OnDragDropFinish() may have caused destruction of the elements, so check the pointers again
  794. if (accept && dragElement_ && target)
  795. {
  796. using namespace DragDropFinish;
  797. VariantMap eventData;
  798. eventData[P_SOURCE] = (void*)dragElement_.Get();
  799. eventData[P_TARGET] = (void*)target.Get();
  800. eventData[P_ACCEPT] = accept;
  801. SendEvent(E_DRAGDROPFINISH, eventData);
  802. }
  803. }
  804. }
  805. }
  806. dragElement_.Reset();
  807. }
  808. }
  809. }
  810. void UI::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  811. {
  812. using namespace MouseMove;
  813. mouseButtons_ = eventData[P_BUTTONS].GetInt();
  814. qualifiers_ = eventData[P_QUALIFIERS].GetInt();
  815. usingTouchInput_ = false;
  816. Input* input = GetSubsystem<Input>();
  817. const IntVector2& rootSize = rootElement_->GetSize();
  818. if (cursor_)
  819. {
  820. if (!input->IsMouseVisible())
  821. {
  822. // Relative mouse motion: move cursor only when visible
  823. if (cursor_->IsVisible())
  824. {
  825. IntVector2 pos = cursor_->GetPosition();
  826. pos.x_ += eventData[P_DX].GetInt();
  827. pos.y_ += eventData[P_DY].GetInt();
  828. pos.x_ = Clamp(pos.x_, 0, rootSize.x_ - 1);
  829. pos.y_ = Clamp(pos.y_, 0, rootSize.y_ - 1);
  830. cursor_->SetPosition(pos);
  831. }
  832. }
  833. else
  834. {
  835. // Absolute mouse motion: move always
  836. cursor_->SetPosition(IntVector2(eventData[P_X].GetInt(), eventData[P_Y].GetInt()));
  837. }
  838. }
  839. IntVector2 cursorPos;
  840. bool cursorVisible;
  841. GetCursorPositionAndVisible(cursorPos, cursorVisible);
  842. if (cursorVisible && dragElement_ && mouseButtons_)
  843. {
  844. if (dragElement_->IsEnabled() && dragElement_->IsVisible())
  845. {
  846. dragElement_->OnDragMove(dragElement_->ScreenToElement(cursorPos), cursorPos, mouseButtons_, qualifiers_, cursor_);
  847. SendDragEvent(E_DRAGMOVE, dragElement_, cursorPos);
  848. }
  849. else
  850. {
  851. dragElement_->OnDragEnd(dragElement_->ScreenToElement(cursorPos), cursorPos, cursor_);
  852. SendDragEvent(E_DRAGEND, dragElement_, cursorPos);
  853. dragElement_.Reset();
  854. }
  855. }
  856. }
  857. void UI::HandleMouseWheel(StringHash eventType, VariantMap& eventData)
  858. {
  859. using namespace MouseWheel;
  860. mouseButtons_ = eventData[P_BUTTONS].GetInt();
  861. qualifiers_ = eventData[P_QUALIFIERS].GetInt();
  862. int delta = eventData[P_WHEEL].GetInt();
  863. usingTouchInput_ = false;
  864. IntVector2 cursorPos;
  865. bool cursorVisible;
  866. GetCursorPositionAndVisible(cursorPos, cursorVisible);
  867. UIElement* element;
  868. if (!nonFocusedMouseWheel_&& (element = focusElement_))
  869. element->OnWheel(delta, mouseButtons_, qualifiers_);
  870. else
  871. {
  872. // If no element has actual focus or in non-focused mode, get the element at cursor
  873. if (cursorVisible)
  874. {
  875. element = GetElementAt(cursorPos);
  876. if (nonFocusedMouseWheel_)
  877. {
  878. // Going up the hierarchy chain to find element that could handle mouse wheel
  879. while (element)
  880. {
  881. if (element->GetType() == ListView::GetTypeStatic() ||
  882. element->GetType() == ScrollView::GetTypeStatic())
  883. break;
  884. element = element->GetParent();
  885. }
  886. }
  887. else
  888. // If the element itself is not focusable, search for a focusable parent,
  889. // although the focusable element may not actually handle mouse wheel
  890. element = GetFocusableElement(element);
  891. if (element && (nonFocusedMouseWheel_ || element->GetFocusMode() >= FM_FOCUSABLE))
  892. element->OnWheel(delta, mouseButtons_, qualifiers_);
  893. }
  894. }
  895. }
  896. void UI::HandleTouchBegin(StringHash eventType, VariantMap& eventData)
  897. {
  898. using namespace TouchBegin;
  899. IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
  900. WeakPtr<UIElement> element(GetElementAt(pos));
  901. usingTouchInput_ = true;
  902. if (element)
  903. {
  904. // Handle focusing & bringing to front
  905. SetFocusElement(element);
  906. element->BringToFront();
  907. // Handle click
  908. element->OnClick(element->ScreenToElement(pos), pos, MOUSEB_LEFT, 0, 0);
  909. // Handle start of drag. OnClick() may have caused destruction of the element, so check the pointer again
  910. if (element && !dragElement_ )
  911. {
  912. dragElement_ = element;
  913. element->OnDragBegin(element->ScreenToElement(pos), pos, MOUSEB_LEFT, 0, 0);
  914. SendDragEvent(E_DRAGBEGIN, element, pos);
  915. }
  916. }
  917. else
  918. {
  919. // If clicked over no element, or a disabled element, lose focus
  920. SetFocusElement(0);
  921. }
  922. VariantMap clickEventData;
  923. clickEventData[UIMouseClick::P_ELEMENT] = (void*)element.Get();
  924. clickEventData[UIMouseClick::P_X] = pos.x_;
  925. clickEventData[UIMouseClick::P_Y] = pos.y_;
  926. clickEventData[UIMouseClick::P_BUTTON] = MOUSEB_LEFT;
  927. clickEventData[UIMouseClick::P_BUTTONS] = MOUSEB_LEFT;
  928. clickEventData[UIMouseClick::P_QUALIFIERS] = 0;
  929. SendEvent(E_UIMOUSECLICK, clickEventData);
  930. }
  931. void UI::HandleTouchEnd(StringHash eventType, VariantMap& eventData)
  932. {
  933. using namespace TouchEnd;
  934. IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
  935. // Transmit hover end to the position where the finger was lifted
  936. UIElement* element = GetElementAt(pos);
  937. if (element && element->IsEnabled())
  938. element->OnHover(element->ScreenToElement(pos), pos, 0, 0, 0);
  939. if (dragElement_)
  940. {
  941. if (dragElement_->IsEnabled() && dragElement_->IsVisible())
  942. {
  943. dragElement_->OnDragEnd(dragElement_->ScreenToElement(pos), pos, cursor_);
  944. SendDragEvent(E_DRAGEND, dragElement_, pos);
  945. // Drag and drop finish
  946. bool dragSource = dragElement_ && (dragElement_->GetDragDropMode() & DD_SOURCE) != 0;
  947. if (dragSource)
  948. {
  949. WeakPtr<UIElement> target(GetElementAt(pos));
  950. bool dragTarget = target && (target->GetDragDropMode() & DD_TARGET) != 0;
  951. bool dragDropFinish = dragSource && dragTarget && target != dragElement_;
  952. if (dragDropFinish)
  953. {
  954. bool accept = target->OnDragDropFinish(dragElement_);
  955. // OnDragDropFinish() may have caused destruction of the elements, so check the pointers again
  956. if (accept && dragElement_ && target)
  957. {
  958. using namespace DragDropFinish;
  959. VariantMap eventData;
  960. eventData[P_SOURCE] = (void*)dragElement_.Get();
  961. eventData[P_TARGET] = (void*)target.Get();
  962. eventData[P_ACCEPT] = accept;
  963. SendEvent(E_DRAGDROPFINISH, eventData);
  964. }
  965. }
  966. }
  967. }
  968. dragElement_.Reset();
  969. }
  970. }
  971. void UI::HandleTouchMove(StringHash eventType, VariantMap& eventData)
  972. {
  973. using namespace TouchMove;
  974. IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
  975. usingTouchInput_ = true;
  976. if (dragElement_)
  977. {
  978. if (dragElement_->IsEnabled() && dragElement_->IsVisible())
  979. {
  980. dragElement_->OnDragMove(dragElement_->ScreenToElement(pos), pos, MOUSEB_LEFT, 0, 0);
  981. SendDragEvent(E_DRAGMOVE, dragElement_, pos);
  982. }
  983. else
  984. {
  985. dragElement_->OnDragEnd(dragElement_->ScreenToElement(pos), pos, 0);
  986. SendDragEvent(E_DRAGEND, dragElement_, pos);
  987. dragElement_.Reset();
  988. }
  989. }
  990. }
  991. void UI::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  992. {
  993. using namespace KeyDown;
  994. mouseButtons_ = eventData[P_BUTTONS].GetInt();
  995. qualifiers_ = eventData[P_QUALIFIERS].GetInt();
  996. int key = eventData[P_KEY].GetInt();
  997. // Dismiss modal element if any when ESC key is pressed
  998. if (key == KEY_ESC && HasModalElement())
  999. {
  1000. UIElement* element = rootModalElement_->GetChild(rootModalElement_->GetNumChildren() - 1);
  1001. if (element->GetVars().Contains(VAR_ORIGIN))
  1002. // If it is a popup, dismiss by defocusing it
  1003. SetFocusElement(0);
  1004. else
  1005. {
  1006. // If it is a modal window, by resetting its modal flag
  1007. Window* window = dynamic_cast<Window*>(element);
  1008. if (window)
  1009. window->SetModal(false);
  1010. }
  1011. return;
  1012. }
  1013. UIElement* element = focusElement_;
  1014. if (element)
  1015. {
  1016. // Switch focus between focusable elements in the same top level window
  1017. if (key == KEY_TAB)
  1018. {
  1019. UIElement* topLevel = element->GetParent();
  1020. while (topLevel && topLevel->GetParent() != rootElement_ && topLevel->GetParent() != rootModalElement_)
  1021. topLevel = topLevel->GetParent();
  1022. if (topLevel)
  1023. {
  1024. topLevel->GetChildren(tempElements_, true);
  1025. for (PODVector<UIElement*>::Iterator i = tempElements_.Begin(); i != tempElements_.End();)
  1026. {
  1027. if ((*i)->GetFocusMode() < FM_FOCUSABLE)
  1028. i = tempElements_.Erase(i);
  1029. else
  1030. ++i;
  1031. }
  1032. for (unsigned i = 0; i < tempElements_.Size(); ++i)
  1033. {
  1034. if (tempElements_[i] == element)
  1035. {
  1036. UIElement* next = tempElements_[(i + 1) % tempElements_.Size()];
  1037. SetFocusElement(next);
  1038. return;
  1039. }
  1040. }
  1041. }
  1042. }
  1043. // Defocus the element
  1044. else if (key == KEY_ESC && element->GetFocusMode() == FM_FOCUSABLE_DEFOCUSABLE)
  1045. element->SetFocus(false);
  1046. // If none of the special keys, pass the key to the focused element
  1047. else
  1048. element->OnKey(key, mouseButtons_, qualifiers_);
  1049. }
  1050. }
  1051. void UI::HandleChar(StringHash eventType, VariantMap& eventData)
  1052. {
  1053. using namespace Char;
  1054. mouseButtons_ = eventData[P_BUTTONS].GetInt();
  1055. qualifiers_ = eventData[P_QUALIFIERS].GetInt();
  1056. UIElement* element = focusElement_;
  1057. if (element)
  1058. element->OnChar(eventData[P_CHAR].GetInt(), mouseButtons_, qualifiers_);
  1059. }
  1060. void UI::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  1061. {
  1062. using namespace PostUpdate;
  1063. Update(eventData[P_TIMESTEP].GetFloat());
  1064. }
  1065. void UI::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
  1066. {
  1067. RenderUpdate();
  1068. }
  1069. void RegisterUILibrary(Context* context)
  1070. {
  1071. Font::RegisterObject(context);
  1072. UIElement::RegisterObject(context);
  1073. BorderImage::RegisterObject(context);
  1074. Sprite::RegisterObject(context);
  1075. Button::RegisterObject(context);
  1076. CheckBox::RegisterObject(context);
  1077. Cursor::RegisterObject(context);
  1078. Text::RegisterObject(context);
  1079. Text3D::RegisterObject(context);
  1080. Window::RegisterObject(context);
  1081. LineEdit::RegisterObject(context);
  1082. Slider::RegisterObject(context);
  1083. ScrollBar::RegisterObject(context);
  1084. ScrollView::RegisterObject(context);
  1085. ListView::RegisterObject(context);
  1086. Menu::RegisterObject(context);
  1087. DropDownList::RegisterObject(context);
  1088. FileSelector::RegisterObject(context);
  1089. }
  1090. }