UI.cpp 42 KB

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