UI.cpp 43 KB

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