UI.cpp 42 KB

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