UI.cpp 42 KB

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