UI.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  1. //
  2. // Copyright (c) 2008-2015 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 "../../Core/Context.h"
  23. #include "../../Core/CoreEvents.h"
  24. #include "../../Core/Profiler.h"
  25. #include "../../Container/Sort.h"
  26. #include "../../Graphics/Graphics.h"
  27. #include "../../Graphics/GraphicsEvents.h"
  28. #include "../../Graphics/Shader.h"
  29. #include "../../Graphics/ShaderVariation.h"
  30. #include "../../Graphics/Texture2D.h"
  31. #include "../../Graphics/VertexBuffer.h"
  32. #include "../../Input/Input.h"
  33. #include "../../Input/InputEvents.h"
  34. #include "../../IO/Log.h"
  35. #include "../../Math/Matrix3x4.h"
  36. #include "../../Resource/ResourceCache.h"
  37. #include "CheckBox.h"
  38. #include "Cursor.h"
  39. #include "DropDownList.h"
  40. #include "Font.h"
  41. #include "LineEdit.h"
  42. #include "ListView.h"
  43. #include "MessageBox.h"
  44. #include "ScrollBar.h"
  45. #include "Slider.h"
  46. #include "Sprite.h"
  47. #include "Text.h"
  48. #include "ToolTip.h"
  49. #include "UI.h"
  50. #include "UIEvents.h"
  51. #include "Window.h"
  52. #include <SDL/include/SDL.h>
  53. #include "../../DebugNew.h"
  54. #define TOUCHID_MASK(id) (1 << id)
  55. namespace Atomic
  56. {
  57. StringHash VAR_ORIGIN("Origin");
  58. const StringHash VAR_ORIGINAL_PARENT("OriginalParent");
  59. const StringHash VAR_ORIGINAL_CHILD_INDEX("OriginalChildIndex");
  60. const StringHash VAR_PARENT_CHANGED("ParentChanged");
  61. const float DEFAULT_DOUBLECLICK_INTERVAL = 0.5f;
  62. const float DEFAULT_DRAGBEGIN_INTERVAL = 0.5f;
  63. const float DEFAULT_TOOLTIP_DELAY = 0.5f;
  64. const int DEFAULT_DRAGBEGIN_DISTANCE = 5;
  65. const int DEFAULT_FONT_TEXTURE_MAX_SIZE = 2048;
  66. const char* UI_CATEGORY = "UI";
  67. UI::UI(Context* context) :
  68. Object(context),
  69. rootElement_(new UIElement(context)),
  70. rootModalElement_(new UIElement(context)),
  71. doubleClickInterval_(DEFAULT_DOUBLECLICK_INTERVAL),
  72. dragBeginInterval_(DEFAULT_DRAGBEGIN_INTERVAL),
  73. defaultToolTipDelay_(DEFAULT_TOOLTIP_DELAY),
  74. dragBeginDistance_(DEFAULT_DRAGBEGIN_DISTANCE),
  75. mouseButtons_(0),
  76. lastMouseButtons_(0),
  77. qualifiers_(0),
  78. maxFontTextureSize_(DEFAULT_FONT_TEXTURE_MAX_SIZE),
  79. initialized_(false),
  80. usingTouchInput_(false),
  81. #ifdef WIN32
  82. nonFocusedMouseWheel_(false), // Default MS Windows behaviour
  83. #else
  84. nonFocusedMouseWheel_(true), // Default Mac OS X and Linux behaviour
  85. #endif
  86. useSystemClipboard_(false),
  87. #if defined(ANDROID) || defined(IOS)
  88. useScreenKeyboard_(true),
  89. #else
  90. useScreenKeyboard_(false),
  91. #endif
  92. useMutableGlyphs_(false),
  93. forceAutoHint_(false),
  94. uiRendered_(false),
  95. nonModalBatchSize_(0),
  96. dragElementsCount_(0),
  97. dragConfirmedCount_(0)
  98. {
  99. rootElement_->SetTraversalMode(TM_DEPTH_FIRST);
  100. rootModalElement_->SetTraversalMode(TM_DEPTH_FIRST);
  101. // Register UI library object factories
  102. RegisterUILibrary(context_);
  103. SubscribeToEvent(E_SCREENMODE, HANDLER(UI, HandleScreenMode));
  104. SubscribeToEvent(E_MOUSEBUTTONDOWN, HANDLER(UI, HandleMouseButtonDown));
  105. SubscribeToEvent(E_MOUSEBUTTONUP, HANDLER(UI, HandleMouseButtonUp));
  106. SubscribeToEvent(E_MOUSEMOVE, HANDLER(UI, HandleMouseMove));
  107. SubscribeToEvent(E_MOUSEWHEEL, HANDLER(UI, HandleMouseWheel));
  108. SubscribeToEvent(E_TOUCHBEGIN, HANDLER(UI, HandleTouchBegin));
  109. SubscribeToEvent(E_TOUCHEND, HANDLER(UI, HandleTouchEnd));
  110. SubscribeToEvent(E_TOUCHMOVE, HANDLER(UI, HandleTouchMove));
  111. SubscribeToEvent(E_KEYDOWN, HANDLER(UI, HandleKeyDown));
  112. SubscribeToEvent(E_TEXTINPUT, HANDLER(UI, HandleTextInput));
  113. SubscribeToEvent(E_DROPFILE, HANDLER(UI, HandleDropFile));
  114. // Try to initialize right now, but skip if screen mode is not yet set
  115. Initialize();
  116. }
  117. UI::~UI()
  118. {
  119. }
  120. void UI::SetCursor(Cursor* cursor)
  121. {
  122. // Remove old cursor (if any) and set new
  123. if (cursor_)
  124. {
  125. rootElement_->RemoveChild(cursor_);
  126. cursor_.Reset();
  127. }
  128. if (cursor)
  129. {
  130. rootElement_->AddChild(cursor);
  131. cursor_ = cursor;
  132. IntVector2 pos = cursor_->GetPosition();
  133. const IntVector2& rootSize = rootElement_->GetSize();
  134. pos.x_ = Clamp(pos.x_, 0, rootSize.x_ - 1);
  135. pos.y_ = Clamp(pos.y_, 0, rootSize.y_ - 1);
  136. cursor_->SetPosition(pos);
  137. }
  138. }
  139. void UI::SetFocusElement(UIElement* element, bool byKey)
  140. {
  141. using namespace FocusChanged;
  142. UIElement* originalElement = element;
  143. if (element)
  144. {
  145. // Return if already has focus
  146. if (focusElement_ == element)
  147. return;
  148. // Only allow child elements of the modal element to receive focus
  149. if (HasModalElement())
  150. {
  151. UIElement* topLevel = element->GetParent();
  152. while (topLevel && topLevel->GetParent() != rootElement_)
  153. topLevel = topLevel->GetParent();
  154. if (topLevel) // If parented to non-modal root then ignore
  155. return;
  156. }
  157. // Search for an element in the hierarchy that can alter focus. If none found, exit
  158. element = GetFocusableElement(element);
  159. if (!element)
  160. return;
  161. }
  162. // Remove focus from the old element
  163. if (focusElement_)
  164. {
  165. UIElement* oldFocusElement = focusElement_;
  166. focusElement_.Reset();
  167. VariantMap& focusEventData = GetEventDataMap();
  168. focusEventData[Defocused::P_ELEMENT] = oldFocusElement;
  169. oldFocusElement->SendEvent(E_DEFOCUSED, focusEventData);
  170. }
  171. // Then set focus to the new
  172. if (element && element->GetFocusMode() >= FM_FOCUSABLE)
  173. {
  174. focusElement_ = element;
  175. VariantMap& focusEventData = GetEventDataMap();
  176. focusEventData[Focused::P_ELEMENT] = element;
  177. focusEventData[Focused::P_BYKEY] = byKey;
  178. element->SendEvent(E_FOCUSED, focusEventData);
  179. }
  180. VariantMap& eventData = GetEventDataMap();
  181. eventData[P_CLICKEDELEMENT] = originalElement;
  182. eventData[P_ELEMENT] = element;
  183. SendEvent(E_FOCUSCHANGED, eventData);
  184. }
  185. bool UI::SetModalElement(UIElement* modalElement, bool enable)
  186. {
  187. if (!modalElement)
  188. return false;
  189. // Currently only allow modal window
  190. if (modalElement->GetType() != Window::GetTypeStatic())
  191. return false;
  192. assert(rootModalElement_);
  193. UIElement* currParent = modalElement->GetParent();
  194. if (enable)
  195. {
  196. // Make sure it is not already the child of the root modal element
  197. if (currParent == rootModalElement_)
  198. return false;
  199. // Adopt modal root as parent
  200. modalElement->SetVar(VAR_ORIGINAL_PARENT, currParent);
  201. modalElement->SetVar(VAR_ORIGINAL_CHILD_INDEX, currParent ? currParent->FindChild(modalElement) : M_MAX_UNSIGNED);
  202. modalElement->SetParent(rootModalElement_);
  203. // If it is a popup element, bring along its top-level parent
  204. UIElement* originElement = static_cast<UIElement*>(modalElement->GetVar(VAR_ORIGIN).GetPtr());
  205. if (originElement)
  206. {
  207. UIElement* element = originElement;
  208. while (element && element->GetParent() != rootElement_)
  209. element = element->GetParent();
  210. if (element)
  211. {
  212. originElement->SetVar(VAR_PARENT_CHANGED, element);
  213. UIElement* oriParent = element->GetParent();
  214. element->SetVar(VAR_ORIGINAL_PARENT, oriParent);
  215. element->SetVar(VAR_ORIGINAL_CHILD_INDEX, oriParent ? oriParent->FindChild(element) : M_MAX_UNSIGNED);
  216. element->SetParent(rootModalElement_);
  217. }
  218. }
  219. return true;
  220. }
  221. else
  222. {
  223. // Only the modal element can disable itself
  224. if (currParent != rootModalElement_)
  225. return false;
  226. // Revert back to original parent
  227. modalElement->SetParent(static_cast<UIElement*>(modalElement->GetVar(VAR_ORIGINAL_PARENT).GetPtr()),
  228. modalElement->GetVar(VAR_ORIGINAL_CHILD_INDEX).GetUInt());
  229. VariantMap& vars = const_cast<VariantMap&>(modalElement->GetVars());
  230. vars.Erase(VAR_ORIGINAL_PARENT);
  231. vars.Erase(VAR_ORIGINAL_CHILD_INDEX);
  232. // If it is a popup element, revert back its top-level parent
  233. UIElement* originElement = static_cast<UIElement*>(modalElement->GetVar(VAR_ORIGIN).GetPtr());
  234. if (originElement)
  235. {
  236. UIElement* element = static_cast<UIElement*>(originElement->GetVar(VAR_PARENT_CHANGED).GetPtr());
  237. if (element)
  238. {
  239. const_cast<VariantMap&>(originElement->GetVars()).Erase(VAR_PARENT_CHANGED);
  240. element->SetParent(static_cast<UIElement*>(element->GetVar(VAR_ORIGINAL_PARENT).GetPtr()),
  241. element->GetVar(VAR_ORIGINAL_CHILD_INDEX).GetUInt());
  242. vars = const_cast<VariantMap&>(element->GetVars());
  243. vars.Erase(VAR_ORIGINAL_PARENT);
  244. vars.Erase(VAR_ORIGINAL_CHILD_INDEX);
  245. }
  246. }
  247. return true;
  248. }
  249. }
  250. void UI::Clear()
  251. {
  252. rootElement_->RemoveAllChildren();
  253. rootModalElement_->RemoveAllChildren();
  254. if (cursor_)
  255. rootElement_->AddChild(cursor_);
  256. }
  257. void UI::Update(float timeStep)
  258. {
  259. assert(rootElement_ && rootModalElement_);
  260. PROFILE(UpdateUI);
  261. // Expire hovers
  262. for (HashMap<WeakPtr<UIElement>, bool>::Iterator i = hoveredElements_.Begin(); i != hoveredElements_.End(); ++i)
  263. i->second_ = false;
  264. Input* input = GetSubsystem<Input>();
  265. bool mouseGrabbed = input->IsMouseGrabbed();
  266. IntVector2 cursorPos;
  267. bool cursorVisible;
  268. GetCursorPositionAndVisible(cursorPos, cursorVisible);
  269. // Drag begin based on time
  270. if (dragElementsCount_ > 0 && !mouseGrabbed)
  271. {
  272. for (HashMap<WeakPtr<UIElement>, UI::DragData*>::Iterator i = dragElements_.Begin(); i != dragElements_.End();)
  273. {
  274. WeakPtr<UIElement> dragElement = i->first_;
  275. UI::DragData* dragData = i->second_;
  276. if (!dragElement)
  277. {
  278. i = DragElementErase(i);
  279. continue;
  280. }
  281. if (!dragData->dragBeginPending)
  282. {
  283. ++i;
  284. continue;
  285. }
  286. if (dragData->dragBeginTimer.GetMSec(false) >= (unsigned)(dragBeginInterval_ * 1000))
  287. {
  288. dragData->dragBeginPending = false;
  289. IntVector2 beginSendPos = dragData->dragBeginSumPos / dragData->numDragButtons;
  290. dragConfirmedCount_++;
  291. if (!usingTouchInput_)
  292. dragElement->OnDragBegin(dragElement->ScreenToElement(beginSendPos), beginSendPos, dragData->dragButtons,
  293. qualifiers_, cursor_);
  294. else
  295. dragElement->OnDragBegin(dragElement->ScreenToElement(beginSendPos), beginSendPos, dragData->dragButtons, 0, 0);
  296. SendDragOrHoverEvent(E_DRAGBEGIN, dragElement, beginSendPos, IntVector2::ZERO, dragData);
  297. }
  298. ++i;
  299. }
  300. }
  301. // Mouse hover
  302. if (!mouseGrabbed && !input->GetTouchEmulation())
  303. {
  304. if (!usingTouchInput_ && cursorVisible)
  305. ProcessHover(cursorPos, mouseButtons_, qualifiers_, cursor_);
  306. }
  307. // Touch hover
  308. unsigned numTouches = input->GetNumTouches();
  309. for (unsigned i = 0; i < numTouches; ++i)
  310. {
  311. TouchState* touch = input->GetTouch(i);
  312. ProcessHover(touch->position_, TOUCHID_MASK(touch->touchID_), 0, 0);
  313. }
  314. // End hovers that expired without refreshing
  315. for (HashMap<WeakPtr<UIElement>, bool>::Iterator i = hoveredElements_.Begin(); i != hoveredElements_.End();)
  316. {
  317. if (i->first_.Expired() || !i->second_)
  318. {
  319. UIElement* element = i->first_;
  320. if (element)
  321. {
  322. using namespace HoverEnd;
  323. VariantMap& eventData = GetEventDataMap();
  324. eventData[P_ELEMENT] = element;
  325. element->SendEvent(E_HOVEREND, eventData);
  326. }
  327. i = hoveredElements_.Erase(i);
  328. }
  329. else
  330. ++i;
  331. }
  332. Update(timeStep, rootElement_);
  333. Update(timeStep, rootModalElement_);
  334. }
  335. void UI::RenderUpdate()
  336. {
  337. assert(rootElement_ && rootModalElement_ && graphics_);
  338. PROFILE(GetUIBatches);
  339. uiRendered_ = false;
  340. // If the OS cursor is visible, do not render the UI's own cursor
  341. bool osCursorVisible = GetSubsystem<Input>()->IsMouseVisible();
  342. // Get rendering batches from the non-modal UI elements
  343. batches_.Clear();
  344. vertexData_.Clear();
  345. const IntVector2& rootSize = rootElement_->GetSize();
  346. IntRect currentScissor = IntRect(0, 0, rootSize.x_, rootSize.y_);
  347. if (rootElement_->IsVisible())
  348. GetBatches(rootElement_, currentScissor);
  349. // Save the batch size of the non-modal batches for later use
  350. nonModalBatchSize_ = batches_.Size();
  351. // Get rendering batches from the modal UI elements
  352. GetBatches(rootModalElement_, currentScissor);
  353. // Get batches from the cursor (and its possible children) last to draw it on top of everything
  354. if (cursor_ && cursor_->IsVisible() && !osCursorVisible)
  355. {
  356. currentScissor = IntRect(0, 0, rootSize.x_, rootSize.y_);
  357. cursor_->GetBatches(batches_, vertexData_, currentScissor);
  358. GetBatches(cursor_, currentScissor);
  359. }
  360. }
  361. void UI::Render(bool resetRenderTargets)
  362. {
  363. // Perform the default render only if not rendered yet
  364. if (resetRenderTargets && uiRendered_)
  365. return;
  366. PROFILE(RenderUI);
  367. // If the OS cursor is visible, apply its shape now if changed
  368. bool osCursorVisible = GetSubsystem<Input>()->IsMouseVisible();
  369. if (cursor_ && osCursorVisible)
  370. cursor_->ApplyOSCursorShape();
  371. SetVertexData(vertexBuffer_, vertexData_);
  372. SetVertexData(debugVertexBuffer_, debugVertexData_);
  373. // Render non-modal batches
  374. Render(resetRenderTargets, vertexBuffer_, batches_, 0, nonModalBatchSize_);
  375. // Render debug draw
  376. Render(resetRenderTargets, debugVertexBuffer_, debugDrawBatches_, 0, debugDrawBatches_.Size());
  377. // Render modal batches
  378. Render(resetRenderTargets, vertexBuffer_, batches_, nonModalBatchSize_, batches_.Size());
  379. // Clear the debug draw batches and data
  380. debugDrawBatches_.Clear();
  381. debugVertexData_.Clear();
  382. uiRendered_ = true;
  383. }
  384. void UI::DebugDraw(UIElement* element)
  385. {
  386. if (element)
  387. {
  388. const IntVector2& rootSize = rootElement_->GetSize();
  389. element->GetDebugDrawBatches(debugDrawBatches_, debugVertexData_, IntRect(0, 0, rootSize.x_, rootSize.y_));
  390. }
  391. }
  392. SharedPtr<UIElement> UI::LoadLayout(Deserializer& source, XMLFile* styleFile)
  393. {
  394. SharedPtr<XMLFile> xml(new XMLFile(context_));
  395. if (!xml->Load(source))
  396. return SharedPtr<UIElement>();
  397. else
  398. return LoadLayout(xml, styleFile);
  399. }
  400. SharedPtr<UIElement> UI::LoadLayout(XMLFile* file, XMLFile* styleFile)
  401. {
  402. PROFILE(LoadUILayout);
  403. SharedPtr<UIElement> root;
  404. if (!file)
  405. {
  406. LOGERROR("Null UI layout XML file");
  407. return root;
  408. }
  409. LOGDEBUG("Loading UI layout " + file->GetName());
  410. XMLElement rootElem = file->GetRoot("element");
  411. if (!rootElem)
  412. {
  413. LOGERROR("No root UI element in " + file->GetName());
  414. return root;
  415. }
  416. String typeName = rootElem.GetAttribute("type");
  417. if (typeName.Empty())
  418. typeName = "UIElement";
  419. root = DynamicCast<UIElement>(context_->CreateObject(typeName));
  420. if (!root)
  421. {
  422. LOGERROR("Could not create unknown UI element " + typeName);
  423. return root;
  424. }
  425. // Use default style file of the root element if it has one
  426. if (!styleFile)
  427. styleFile = rootElement_->GetDefaultStyle(false);
  428. // Set it as default for later use by children elements
  429. if (styleFile)
  430. root->SetDefaultStyle(styleFile);
  431. root->LoadXML(rootElem, styleFile);
  432. return root;
  433. }
  434. bool UI::SaveLayout(Serializer& dest, UIElement* element)
  435. {
  436. PROFILE(SaveUILayout);
  437. return element && element->SaveXML(dest);
  438. }
  439. void UI::SetClipboardText(const String& text)
  440. {
  441. clipBoard_ = text;
  442. if (useSystemClipboard_)
  443. SDL_SetClipboardText(text.CString());
  444. }
  445. void UI::SetDoubleClickInterval(float interval)
  446. {
  447. doubleClickInterval_ = Max(interval, 0.0f);
  448. }
  449. void UI::SetDragBeginInterval(float interval)
  450. {
  451. dragBeginInterval_ = Max(interval, 0.0f);
  452. }
  453. void UI::SetDragBeginDistance(int pixels)
  454. {
  455. dragBeginDistance_ = Max(pixels, 0);
  456. }
  457. void UI::SetDefaultToolTipDelay(float delay)
  458. {
  459. defaultToolTipDelay_ = Max(delay, 0.0f);
  460. }
  461. void UI::SetMaxFontTextureSize(int size)
  462. {
  463. if (IsPowerOfTwo((unsigned)size) && size >= FONT_TEXTURE_MIN_SIZE)
  464. {
  465. if (size != maxFontTextureSize_)
  466. {
  467. maxFontTextureSize_ = size;
  468. ReleaseFontFaces();
  469. }
  470. }
  471. }
  472. void UI::SetNonFocusedMouseWheel(bool nonFocusedMouseWheel)
  473. {
  474. nonFocusedMouseWheel_ = nonFocusedMouseWheel;
  475. }
  476. void UI::SetUseSystemClipboard(bool enable)
  477. {
  478. useSystemClipboard_ = enable;
  479. }
  480. void UI::SetUseScreenKeyboard(bool enable)
  481. {
  482. useScreenKeyboard_ = enable;
  483. }
  484. void UI::SetUseMutableGlyphs(bool enable)
  485. {
  486. if (enable != useMutableGlyphs_)
  487. {
  488. useMutableGlyphs_ = enable;
  489. ReleaseFontFaces();
  490. }
  491. }
  492. void UI::SetForceAutoHint(bool enable)
  493. {
  494. if (enable != forceAutoHint_)
  495. {
  496. forceAutoHint_ = enable;
  497. ReleaseFontFaces();
  498. }
  499. }
  500. IntVector2 UI::GetCursorPosition() const
  501. {
  502. return cursor_ ? cursor_->GetPosition() : GetSubsystem<Input>()->GetMousePosition();
  503. }
  504. UIElement* UI::GetElementAt(const IntVector2& position, bool enabledOnly)
  505. {
  506. UIElement* result = 0;
  507. GetElementAt(result, HasModalElement() ? rootModalElement_ : rootElement_, position, enabledOnly);
  508. return result;
  509. }
  510. UIElement* UI::GetElementAt(int x, int y, bool enabledOnly)
  511. {
  512. return GetElementAt(IntVector2(x, y), enabledOnly);
  513. }
  514. UIElement* UI::GetFrontElement() const
  515. {
  516. const Vector<SharedPtr<UIElement> >& rootChildren = rootElement_->GetChildren();
  517. int maxPriority = M_MIN_INT;
  518. UIElement* front = 0;
  519. for (unsigned i = 0; i < rootChildren.Size(); ++i)
  520. {
  521. // Do not take into account input-disabled elements, hidden elements or those that are always in the front
  522. if (!rootChildren[i]->IsEnabled() || !rootChildren[i]->IsVisible() || !rootChildren[i]->GetBringToBack())
  523. continue;
  524. int priority = rootChildren[i]->GetPriority();
  525. if (priority > maxPriority)
  526. {
  527. maxPriority = priority;
  528. front = rootChildren[i];
  529. }
  530. }
  531. return front;
  532. }
  533. const Vector<UIElement*> UI::GetDragElements()
  534. {
  535. // Do not return the element until drag begin event has actually been posted
  536. if (!dragElementsConfirmed_.Empty())
  537. return dragElementsConfirmed_;
  538. for (HashMap<WeakPtr<UIElement>, UI::DragData*>::Iterator i = dragElements_.Begin(); i != dragElements_.End();)
  539. {
  540. WeakPtr<UIElement> dragElement = i->first_;
  541. UI::DragData* dragData = i->second_;
  542. if (!dragElement)
  543. {
  544. i = DragElementErase(i);
  545. continue;
  546. }
  547. if (!dragData->dragBeginPending)
  548. dragElementsConfirmed_.Push(dragElement);
  549. ++i;
  550. }
  551. return dragElementsConfirmed_;
  552. }
  553. UIElement* UI::GetDragElement(unsigned index)
  554. {
  555. GetDragElements();
  556. if (index >= dragElementsConfirmed_.Size())
  557. return (UIElement*)0;
  558. return dragElementsConfirmed_[index];
  559. }
  560. const String& UI::GetClipboardText() const
  561. {
  562. if (useSystemClipboard_)
  563. {
  564. char* text = SDL_GetClipboardText();
  565. clipBoard_ = String(text);
  566. if (text)
  567. SDL_free(text);
  568. }
  569. return clipBoard_;
  570. }
  571. bool UI::HasModalElement() const
  572. {
  573. return rootModalElement_->GetNumChildren() > 0;
  574. }
  575. void UI::Initialize()
  576. {
  577. Graphics* graphics = GetSubsystem<Graphics>();
  578. if (!graphics || !graphics->IsInitialized())
  579. return;
  580. PROFILE(InitUI);
  581. graphics_ = graphics;
  582. UIBatch::posAdjust = Vector3(Graphics::GetPixelUVOffset(), 0.0f);
  583. rootElement_->SetSize(graphics->GetWidth(), graphics->GetHeight());
  584. rootModalElement_->SetSize(rootElement_->GetSize());
  585. vertexBuffer_ = new VertexBuffer(context_);
  586. debugVertexBuffer_ = new VertexBuffer(context_);
  587. initialized_ = true;
  588. SubscribeToEvent(E_BEGINFRAME, HANDLER(UI, HandleBeginFrame));
  589. SubscribeToEvent(E_POSTUPDATE, HANDLER(UI, HandlePostUpdate));
  590. SubscribeToEvent(E_RENDERUPDATE, HANDLER(UI, HandleRenderUpdate));
  591. LOGINFO("Initialized user interface");
  592. }
  593. void UI::Update(float timeStep, UIElement* element)
  594. {
  595. // Keep a weak pointer to the element in case it destroys itself on update
  596. WeakPtr<UIElement> elementWeak(element);
  597. element->Update(timeStep);
  598. if (elementWeak.Expired())
  599. return;
  600. const Vector<SharedPtr<UIElement> >& children = element->GetChildren();
  601. // Update of an element may modify its child vector. Use just index-based iteration to be safe
  602. for (unsigned i = 0; i < children.Size(); ++i)
  603. Update(timeStep, children[i]);
  604. }
  605. void UI::SetVertexData(VertexBuffer* dest, const PODVector<float>& vertexData)
  606. {
  607. if (vertexData.Empty())
  608. return;
  609. // Update quad geometry into the vertex buffer
  610. // Resize the vertex buffer first if too small or much too large
  611. unsigned numVertices = vertexData.Size() / UI_VERTEX_SIZE;
  612. if (dest->GetVertexCount() < numVertices || dest->GetVertexCount() > numVertices * 2)
  613. dest->SetSize(numVertices, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1, true);
  614. dest->SetData(&vertexData[0]);
  615. }
  616. void UI::Render(bool resetRenderTargets, VertexBuffer* buffer, const PODVector<UIBatch>& batches, unsigned batchStart,
  617. unsigned batchEnd)
  618. {
  619. // Engine does not render when window is closed or device is lost
  620. assert(graphics_ && graphics_->IsInitialized() && !graphics_->IsDeviceLost());
  621. if (batches.Empty())
  622. return;
  623. Vector2 invScreenSize(1.0f / (float)graphics_->GetWidth(), 1.0f / (float)graphics_->GetHeight());
  624. Vector2 scale(2.0f * invScreenSize.x_, -2.0f * invScreenSize.y_);
  625. Vector2 offset(-1.0f, 1.0f);
  626. Matrix4 projection(Matrix4::IDENTITY);
  627. projection.m00_ = scale.x_;
  628. projection.m03_ = offset.x_;
  629. projection.m11_ = scale.y_;
  630. projection.m13_ = offset.y_;
  631. projection.m22_ = 1.0f;
  632. projection.m23_ = 0.0f;
  633. projection.m33_ = 1.0f;
  634. graphics_->ClearParameterSources();
  635. graphics_->SetColorWrite(true);
  636. graphics_->SetCullMode(CULL_CCW);
  637. graphics_->SetDepthTest(CMP_ALWAYS);
  638. graphics_->SetDepthWrite(false);
  639. graphics_->SetFillMode(FILL_SOLID);
  640. graphics_->SetStencilTest(false);
  641. if (resetRenderTargets)
  642. graphics_->ResetRenderTargets();
  643. graphics_->SetVertexBuffer(buffer);
  644. ShaderVariation* noTextureVS = graphics_->GetShader(VS, "Basic", "VERTEXCOLOR");
  645. ShaderVariation* diffTextureVS = graphics_->GetShader(VS, "Basic", "DIFFMAP VERTEXCOLOR");
  646. ShaderVariation* noTexturePS = graphics_->GetShader(PS, "Basic", "VERTEXCOLOR");
  647. ShaderVariation* diffTexturePS = graphics_->GetShader(PS, "Basic", "DIFFMAP VERTEXCOLOR");
  648. ShaderVariation* diffMaskTexturePS = graphics_->GetShader(PS, "Basic", "DIFFMAP ALPHAMASK VERTEXCOLOR");
  649. ShaderVariation* alphaTexturePS = graphics_->GetShader(PS, "Basic", "ALPHAMAP VERTEXCOLOR");
  650. unsigned alphaFormat = Graphics::GetAlphaFormat();
  651. for (unsigned i = batchStart; i < batchEnd; ++i)
  652. {
  653. const UIBatch& batch = batches[i];
  654. if (batch.vertexStart_ == batch.vertexEnd_)
  655. continue;
  656. ShaderVariation* ps;
  657. ShaderVariation* vs;
  658. if (!batch.texture_)
  659. {
  660. ps = noTexturePS;
  661. vs = noTextureVS;
  662. }
  663. else
  664. {
  665. // If texture contains only an alpha channel, use alpha shader (for fonts)
  666. vs = diffTextureVS;
  667. if (batch.texture_->GetFormat() == alphaFormat)
  668. ps = alphaTexturePS;
  669. else if (batch.blendMode_ != BLEND_ALPHA && batch.blendMode_ != BLEND_ADDALPHA && batch.blendMode_ != BLEND_PREMULALPHA)
  670. ps = diffMaskTexturePS;
  671. else
  672. ps = diffTexturePS;
  673. }
  674. graphics_->SetShaders(vs, ps);
  675. if (graphics_->NeedParameterUpdate(SP_OBJECT, this))
  676. graphics_->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
  677. if (graphics_->NeedParameterUpdate(SP_CAMERA, this))
  678. graphics_->SetShaderParameter(VSP_VIEWPROJ, projection);
  679. if (graphics_->NeedParameterUpdate(SP_MATERIAL, this))
  680. graphics_->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
  681. graphics_->SetBlendMode(batch.blendMode_);
  682. graphics_->SetScissorTest(true, batch.scissor_);
  683. graphics_->SetTexture(0, batch.texture_);
  684. graphics_->Draw(TRIANGLE_LIST, batch.vertexStart_ / UI_VERTEX_SIZE,
  685. (batch.vertexEnd_ - batch.vertexStart_) / UI_VERTEX_SIZE);
  686. }
  687. }
  688. void UI::GetBatches(UIElement* element, IntRect currentScissor)
  689. {
  690. // Set clipping scissor for child elements. No need to draw if zero size
  691. element->AdjustScissor(currentScissor);
  692. if (currentScissor.left_ == currentScissor.right_ || currentScissor.top_ == currentScissor.bottom_)
  693. return;
  694. element->SortChildren();
  695. const Vector<SharedPtr<UIElement> >& children = element->GetChildren();
  696. if (children.Empty())
  697. return;
  698. // For non-root elements draw all children of same priority before recursing into their children: assumption is that they have
  699. // same renderstate
  700. Vector<SharedPtr<UIElement> >::ConstIterator i = children.Begin();
  701. if (element->GetTraversalMode() == TM_BREADTH_FIRST)
  702. {
  703. Vector<SharedPtr<UIElement> >::ConstIterator j = i;
  704. while (i != children.End())
  705. {
  706. int currentPriority = (*i)->GetPriority();
  707. while (j != children.End() && (*j)->GetPriority() == currentPriority)
  708. {
  709. if ((*j)->IsWithinScissor(currentScissor) && (*j) != cursor_)
  710. (*j)->GetBatches(batches_, vertexData_, currentScissor);
  711. ++j;
  712. }
  713. // Now recurse into the children
  714. while (i != j)
  715. {
  716. if ((*i)->IsVisible() && (*i) != cursor_)
  717. GetBatches(*i, currentScissor);
  718. ++i;
  719. }
  720. }
  721. }
  722. // On the root level draw each element and its children immediately after to avoid artifacts
  723. else
  724. {
  725. while (i != children.End())
  726. {
  727. if ((*i) != cursor_)
  728. {
  729. if ((*i)->IsWithinScissor(currentScissor))
  730. (*i)->GetBatches(batches_, vertexData_, currentScissor);
  731. if ((*i)->IsVisible())
  732. GetBatches(*i, currentScissor);
  733. }
  734. ++i;
  735. }
  736. }
  737. }
  738. void UI::GetElementAt(UIElement*& result, UIElement* current, const IntVector2& position, bool enabledOnly)
  739. {
  740. if (!current)
  741. return;
  742. current->SortChildren();
  743. const Vector<SharedPtr<UIElement> >& children = current->GetChildren();
  744. LayoutMode parentLayoutMode = current->GetLayoutMode();
  745. for (unsigned i = 0; i < children.Size(); ++i)
  746. {
  747. UIElement* element = children[i];
  748. bool hasChildren = element->GetNumChildren() > 0;
  749. if (element != cursor_.Get() && element->IsVisible())
  750. {
  751. if (element->IsInside(position, true))
  752. {
  753. // Store the current result, then recurse into its children. Because children
  754. // are sorted from lowest to highest priority, the topmost match should remain
  755. if (element->IsEnabled() || !enabledOnly)
  756. result = element;
  757. if (hasChildren)
  758. GetElementAt(result, element, position, enabledOnly);
  759. // Layout optimization: if the element has no children, can break out after the first match
  760. else if (parentLayoutMode != LM_FREE)
  761. break;
  762. }
  763. else
  764. {
  765. if (hasChildren)
  766. {
  767. if (element->IsInsideCombined(position, true))
  768. GetElementAt(result, element, position, enabledOnly);
  769. }
  770. // Layout optimization: if position is much beyond the visible screen, check how many elements we can skip,
  771. // or if we already passed all visible elements
  772. else if (parentLayoutMode != LM_FREE)
  773. {
  774. if (!i)
  775. {
  776. int screenPos = (parentLayoutMode == LM_HORIZONTAL) ? element->GetScreenPosition().x_ :
  777. element->GetScreenPosition().y_;
  778. int layoutMaxSize = current->GetLayoutMaxSize();
  779. if (screenPos < 0 && layoutMaxSize > 0)
  780. {
  781. unsigned toSkip = (unsigned)(-screenPos / layoutMaxSize);
  782. if (toSkip > 0)
  783. i += (toSkip - 1);
  784. }
  785. }
  786. else if (parentLayoutMode == LM_HORIZONTAL)
  787. {
  788. if (element->GetScreenPosition().x_ >= rootElement_->GetSize().x_)
  789. break;
  790. }
  791. else if (parentLayoutMode == LM_VERTICAL)
  792. {
  793. if (element->GetScreenPosition().y_ >= rootElement_->GetSize().y_)
  794. break;
  795. }
  796. }
  797. }
  798. }
  799. }
  800. }
  801. UIElement* UI::GetFocusableElement(UIElement* element)
  802. {
  803. while (element)
  804. {
  805. if (element->GetFocusMode() != FM_NOTFOCUSABLE)
  806. break;
  807. element = element->GetParent();
  808. }
  809. return element;
  810. }
  811. void UI::GetCursorPositionAndVisible(IntVector2& pos, bool& visible)
  812. {
  813. // Prefer software cursor then OS-specific cursor
  814. if (cursor_ && cursor_->IsVisible())
  815. {
  816. pos = cursor_->GetPosition();
  817. visible = true;
  818. }
  819. else if (GetSubsystem<Input>()->GetMouseMode() == MM_RELATIVE)
  820. visible = true;
  821. else
  822. {
  823. Input* input = GetSubsystem<Input>();
  824. pos = input->GetMousePosition();
  825. visible = input->IsMouseVisible();
  826. if (!visible && cursor_)
  827. pos = cursor_->GetPosition();
  828. }
  829. }
  830. void UI::SetCursorShape(CursorShape shape)
  831. {
  832. if (cursor_)
  833. cursor_->SetShape(shape);
  834. }
  835. void UI::ReleaseFontFaces()
  836. {
  837. LOGDEBUG("Reloading font faces");
  838. PODVector<Font*> fonts;
  839. GetSubsystem<ResourceCache>()->GetResources<Font>(fonts);
  840. for (unsigned i = 0; i < fonts.Size(); ++i)
  841. fonts[i]->ReleaseFaces();
  842. }
  843. void UI::ProcessHover(const IntVector2& cursorPos, int buttons, int qualifiers, Cursor* cursor)
  844. {
  845. WeakPtr<UIElement> element(GetElementAt(cursorPos));
  846. for (HashMap<WeakPtr<UIElement>, UI::DragData*>::Iterator i = dragElements_.Begin(); i != dragElements_.End();)
  847. {
  848. WeakPtr<UIElement> dragElement = i->first_;
  849. UI::DragData* dragData = i->second_;
  850. if (!dragElement)
  851. {
  852. i = DragElementErase(i);
  853. continue;
  854. }
  855. bool dragSource = dragElement && (dragElement->GetDragDropMode() & DD_SOURCE) != 0;
  856. bool dragTarget = element && (element->GetDragDropMode() & DD_TARGET) != 0;
  857. bool dragDropTest = dragSource && dragTarget && element != dragElement;
  858. // If drag start event has not been posted yet, do not do drag handling here
  859. if (dragData->dragBeginPending)
  860. dragSource = dragTarget = dragDropTest = false;
  861. // Hover effect
  862. // If a drag is going on, transmit hover only to the element being dragged, unless it's a drop target
  863. if (element && element->IsEnabled())
  864. {
  865. if (dragElement == element || dragDropTest)
  866. {
  867. element->OnHover(element->ScreenToElement(cursorPos), cursorPos, buttons, qualifiers, cursor);
  868. // Begin hover event
  869. if (!hoveredElements_.Contains(element))
  870. {
  871. SendDragOrHoverEvent(E_HOVERBEGIN, element, cursorPos, IntVector2::ZERO, 0);
  872. // Exit if element is destroyed by the event handling
  873. if (!element)
  874. return;
  875. }
  876. hoveredElements_[element] = true;
  877. }
  878. }
  879. // Drag and drop test
  880. if (dragDropTest)
  881. {
  882. bool accept = element->OnDragDropTest(dragElement);
  883. if (accept)
  884. {
  885. using namespace DragDropTest;
  886. VariantMap& eventData = GetEventDataMap();
  887. eventData[P_SOURCE] = dragElement.Get();
  888. eventData[P_TARGET] = element.Get();
  889. eventData[P_ACCEPT] = accept;
  890. SendEvent(E_DRAGDROPTEST, eventData);
  891. accept = eventData[P_ACCEPT].GetBool();
  892. }
  893. if (cursor)
  894. cursor->SetShape(accept ? CS_ACCEPTDROP : CS_REJECTDROP);
  895. }
  896. else if (dragSource && cursor)
  897. cursor->SetShape(dragElement == element ? CS_ACCEPTDROP : CS_REJECTDROP);
  898. ++i;
  899. }
  900. // Hover effect
  901. // If no drag is going on, transmit hover event.
  902. if (element && element->IsEnabled())
  903. {
  904. if (dragElementsCount_ == 0)
  905. {
  906. element->OnHover(element->ScreenToElement(cursorPos), cursorPos, buttons, qualifiers, cursor);
  907. // Begin hover event
  908. if (!hoveredElements_.Contains(element))
  909. {
  910. SendDragOrHoverEvent(E_HOVERBEGIN, element, cursorPos, IntVector2::ZERO, 0);
  911. // Exit if element is destroyed by the event handling
  912. if (!element)
  913. return;
  914. }
  915. hoveredElements_[element] = true;
  916. }
  917. }
  918. }
  919. void UI::ProcessClickBegin(const IntVector2& cursorPos, int button, int buttons, int qualifiers, Cursor* cursor, bool cursorVisible)
  920. {
  921. if (cursorVisible)
  922. {
  923. WeakPtr<UIElement> element(GetElementAt(cursorPos));
  924. bool newButton;
  925. if (usingTouchInput_)
  926. newButton = (button & buttons) == 0;
  927. else
  928. newButton = true;
  929. buttons |= button;
  930. if (element)
  931. SetFocusElement(element);
  932. // Focus change events may destroy the element, check again.
  933. if (element)
  934. {
  935. // Handle focusing & bringing to front
  936. element->BringToFront();
  937. // Handle click
  938. element->OnClickBegin(element->ScreenToElement(cursorPos), cursorPos, button, buttons, qualifiers, cursor);
  939. SendClickEvent(E_UIMOUSECLICK, NULL, element, cursorPos, button, buttons, qualifiers);
  940. // Fire double click event if element matches and is in time
  941. if (doubleClickElement_ && element == doubleClickElement_ &&
  942. clickTimer_.GetMSec(true) < (unsigned)(doubleClickInterval_ * 1000) && lastMouseButtons_ == buttons)
  943. {
  944. element->OnDoubleClick(element->ScreenToElement(cursorPos), cursorPos, button, buttons, qualifiers, cursor);
  945. doubleClickElement_.Reset();
  946. SendClickEvent(E_UIMOUSEDOUBLECLICK, NULL, element, cursorPos, button, buttons, qualifiers);
  947. }
  948. else
  949. {
  950. doubleClickElement_ = element;
  951. clickTimer_.Reset();
  952. }
  953. // Handle start of drag. Click handling may have caused destruction of the element, so check the pointer again
  954. bool dragElementsContain = dragElements_.Contains(element);
  955. if (element && !dragElementsContain)
  956. {
  957. DragData* dragData = new DragData();
  958. dragElements_[element] = dragData;
  959. dragData->dragBeginPending = true;
  960. dragData->sumPos = cursorPos;
  961. dragData->dragBeginSumPos = cursorPos;
  962. dragData->dragBeginTimer.Reset();
  963. dragData->dragButtons = button;
  964. dragData->numDragButtons = CountSetBits((unsigned)dragData->dragButtons);
  965. dragElementsCount_++;
  966. dragElementsContain = dragElements_.Contains(element);
  967. }
  968. else if (element && dragElementsContain && newButton)
  969. {
  970. DragData* dragData = dragElements_[element];
  971. dragData->sumPos += cursorPos;
  972. dragData->dragBeginSumPos += cursorPos;
  973. dragData->dragButtons |= button;
  974. dragData->numDragButtons = CountSetBits((unsigned)dragData->dragButtons);
  975. }
  976. }
  977. else
  978. {
  979. // If clicked over no element, or a disabled element, lose focus (but not if there is a modal element)
  980. if (!HasModalElement())
  981. SetFocusElement(0);
  982. SendClickEvent(E_UIMOUSECLICK, NULL, element, cursorPos, button, buttons, qualifiers);
  983. }
  984. lastMouseButtons_ = buttons;
  985. }
  986. }
  987. void UI::ProcessClickEnd(const IntVector2& cursorPos, int button, int buttons, int qualifiers, Cursor* cursor, bool cursorVisible)
  988. {
  989. if (cursorVisible)
  990. {
  991. WeakPtr<UIElement> element(GetElementAt(cursorPos));
  992. // Handle end of drag
  993. for (HashMap<WeakPtr<UIElement>, UI::DragData*>::Iterator i = dragElements_.Begin(); i != dragElements_.End();)
  994. {
  995. WeakPtr<UIElement> dragElement = i->first_;
  996. UI::DragData* dragData = i->second_;
  997. if (!dragElement)
  998. {
  999. i = DragElementErase(i);
  1000. continue;
  1001. }
  1002. if (dragData->dragButtons & button)
  1003. {
  1004. // Handle end of click
  1005. if (element)
  1006. element->OnClickEnd(element->ScreenToElement(cursorPos), cursorPos, button, buttons, qualifiers, cursor,
  1007. dragElement);
  1008. SendClickEvent(E_UIMOUSECLICKEND, dragElement, element, cursorPos, button, buttons, qualifiers);
  1009. if (dragElement && dragElement->IsEnabled() && dragElement->IsVisible() && !dragData->dragBeginPending)
  1010. {
  1011. dragElement->OnDragEnd(dragElement->ScreenToElement(cursorPos), cursorPos, dragData->dragButtons, buttons,
  1012. cursor);
  1013. SendDragOrHoverEvent(E_DRAGEND, dragElement, cursorPos, IntVector2::ZERO, dragData);
  1014. bool dragSource = dragElement && (dragElement->GetDragDropMode() & DD_SOURCE) != 0;
  1015. if (dragSource)
  1016. {
  1017. bool dragTarget = element && (element->GetDragDropMode() & DD_TARGET) != 0;
  1018. bool dragDropFinish = dragSource && dragTarget && element != dragElement;
  1019. if (dragDropFinish)
  1020. {
  1021. bool accept = element->OnDragDropFinish(dragElement);
  1022. // OnDragDropFinish() may have caused destruction of the elements, so check the pointers again
  1023. if (accept && dragElement && element)
  1024. {
  1025. using namespace DragDropFinish;
  1026. VariantMap& eventData = GetEventDataMap();
  1027. eventData[P_SOURCE] = dragElement.Get();
  1028. eventData[P_TARGET] = element.Get();
  1029. eventData[P_ACCEPT] = accept;
  1030. SendEvent(E_DRAGDROPFINISH, eventData);
  1031. }
  1032. }
  1033. }
  1034. }
  1035. i = DragElementErase(i);
  1036. }
  1037. else
  1038. ++i;
  1039. }
  1040. }
  1041. }
  1042. void UI::ProcessMove(const IntVector2& cursorPos, const IntVector2& cursorDeltaPos, int buttons, int qualifiers, Cursor* cursor,
  1043. bool cursorVisible)
  1044. {
  1045. if (cursorVisible && dragElementsCount_ > 0 && buttons)
  1046. {
  1047. Input* input = GetSubsystem<Input>();
  1048. bool mouseGrabbed = input->IsMouseGrabbed();
  1049. for (HashMap<WeakPtr<UIElement>, UI::DragData*>::Iterator i = dragElements_.Begin(); i != dragElements_.End();)
  1050. {
  1051. WeakPtr<UIElement> dragElement = i->first_;
  1052. UI::DragData* dragData = i->second_;
  1053. if (!dragElement)
  1054. {
  1055. i = DragElementErase(i);
  1056. continue;
  1057. }
  1058. if (!(dragData->dragButtons & buttons))
  1059. {
  1060. ++i;
  1061. continue;
  1062. }
  1063. // Calculate the position that we should send for this drag event.
  1064. IntVector2 sendPos;
  1065. if (usingTouchInput_)
  1066. {
  1067. dragData->sumPos += cursorDeltaPos;
  1068. sendPos.x_ = dragData->sumPos.x_ / dragData->numDragButtons;
  1069. sendPos.y_ = dragData->sumPos.y_ / dragData->numDragButtons;
  1070. }
  1071. else
  1072. {
  1073. dragData->sumPos = cursorPos;
  1074. sendPos = cursorPos;
  1075. }
  1076. if (dragElement->IsEnabled() && dragElement->IsVisible())
  1077. {
  1078. // Signal drag begin if distance threshold was exceeded
  1079. if (dragData->dragBeginPending && !mouseGrabbed)
  1080. {
  1081. IntVector2 beginSendPos;
  1082. beginSendPos.x_ = dragData->dragBeginSumPos.x_ / dragData->numDragButtons;
  1083. beginSendPos.y_ = dragData->dragBeginSumPos.y_ / dragData->numDragButtons;
  1084. IntVector2 offset = cursorPos - beginSendPos;
  1085. if (Abs(offset.x_) >= dragBeginDistance_ || Abs(offset.y_) >= dragBeginDistance_)
  1086. {
  1087. dragData->dragBeginPending = false;
  1088. dragConfirmedCount_++;
  1089. dragElement->OnDragBegin(dragElement->ScreenToElement(beginSendPos), beginSendPos, buttons, qualifiers,
  1090. cursor);
  1091. SendDragOrHoverEvent(E_DRAGBEGIN, dragElement, beginSendPos, IntVector2::ZERO, dragData);
  1092. }
  1093. }
  1094. if (!dragData->dragBeginPending)
  1095. {
  1096. dragElement->OnDragMove(dragElement->ScreenToElement(sendPos), sendPos, cursorDeltaPos, buttons, qualifiers,
  1097. cursor);
  1098. SendDragOrHoverEvent(E_DRAGMOVE, dragElement, sendPos, cursorDeltaPos, dragData);
  1099. }
  1100. }
  1101. else
  1102. {
  1103. dragElement->OnDragEnd(dragElement->ScreenToElement(sendPos), sendPos, dragData->dragButtons, buttons, cursor);
  1104. SendDragOrHoverEvent(E_DRAGEND, dragElement, sendPos, IntVector2::ZERO, dragData);
  1105. dragElement.Reset();
  1106. }
  1107. ++i;
  1108. }
  1109. }
  1110. }
  1111. void UI::SendDragOrHoverEvent(StringHash eventType, UIElement* element, const IntVector2& screenPos, const IntVector2& deltaPos,
  1112. UI::DragData* dragData)
  1113. {
  1114. if (!element)
  1115. return;
  1116. IntVector2 relativePos = element->ScreenToElement(screenPos);
  1117. using namespace DragMove;
  1118. VariantMap& eventData = GetEventDataMap();
  1119. eventData[P_ELEMENT] = element;
  1120. eventData[P_X] = screenPos.x_;
  1121. eventData[P_Y] = screenPos.y_;
  1122. eventData[P_ELEMENTX] = relativePos.x_;
  1123. eventData[P_ELEMENTY] = relativePos.y_;
  1124. if (eventType == E_DRAGMOVE)
  1125. {
  1126. eventData[P_DX] = deltaPos.x_;
  1127. eventData[P_DY] = deltaPos.y_;
  1128. }
  1129. if (dragData)
  1130. {
  1131. eventData[P_BUTTONS] = dragData->dragButtons;
  1132. eventData[P_NUMBUTTONS] = dragData->numDragButtons;
  1133. }
  1134. element->SendEvent(eventType, eventData);
  1135. }
  1136. void UI::SendClickEvent(StringHash eventType, UIElement* beginElement, UIElement* endElement, const IntVector2& pos, int button,
  1137. int buttons, int qualifiers)
  1138. {
  1139. VariantMap& eventData = GetEventDataMap();
  1140. eventData[UIMouseClick::P_ELEMENT] = endElement;
  1141. eventData[UIMouseClick::P_X] = pos.x_;
  1142. eventData[UIMouseClick::P_Y] = pos.y_;
  1143. eventData[UIMouseClick::P_BUTTON] = button;
  1144. eventData[UIMouseClick::P_BUTTONS] = buttons;
  1145. eventData[UIMouseClick::P_QUALIFIERS] = qualifiers;
  1146. // For click end events, send also the element the click began on
  1147. if (eventType == E_UIMOUSECLICKEND)
  1148. eventData[UIMouseClickEnd::P_BEGINELEMENT] = beginElement;
  1149. SendEvent(eventType, eventData);
  1150. }
  1151. void UI::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  1152. {
  1153. using namespace ScreenMode;
  1154. if (!initialized_)
  1155. Initialize();
  1156. else
  1157. {
  1158. rootElement_->SetSize(eventData[P_WIDTH].GetInt(), eventData[P_HEIGHT].GetInt());
  1159. rootModalElement_->SetSize(rootElement_->GetSize());
  1160. }
  1161. }
  1162. void UI::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  1163. {
  1164. using namespace MouseButtonDown;
  1165. mouseButtons_ = eventData[P_BUTTONS].GetInt();
  1166. qualifiers_ = eventData[P_QUALIFIERS].GetInt();
  1167. usingTouchInput_ = false;
  1168. IntVector2 cursorPos;
  1169. bool cursorVisible;
  1170. GetCursorPositionAndVisible(cursorPos, cursorVisible);
  1171. // Handle drag cancelling
  1172. ProcessDragCancel();
  1173. Input* input = GetSubsystem<Input>();
  1174. if (!input->IsMouseGrabbed())
  1175. ProcessClickBegin(cursorPos, eventData[P_BUTTON].GetInt(), mouseButtons_, qualifiers_, cursor_, cursorVisible);
  1176. }
  1177. void UI::HandleMouseButtonUp(StringHash eventType, VariantMap& eventData)
  1178. {
  1179. using namespace MouseButtonUp;
  1180. mouseButtons_ = eventData[P_BUTTONS].GetInt();
  1181. qualifiers_ = eventData[P_QUALIFIERS].GetInt();
  1182. IntVector2 cursorPos;
  1183. bool cursorVisible;
  1184. GetCursorPositionAndVisible(cursorPos, cursorVisible);
  1185. ProcessClickEnd(cursorPos, eventData[P_BUTTON].GetInt(), mouseButtons_, qualifiers_, cursor_, cursorVisible);
  1186. }
  1187. void UI::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  1188. {
  1189. using namespace MouseMove;
  1190. mouseButtons_ = eventData[P_BUTTONS].GetInt();
  1191. qualifiers_ = eventData[P_QUALIFIERS].GetInt();
  1192. usingTouchInput_ = false;
  1193. Input* input = GetSubsystem<Input>();
  1194. const IntVector2& rootSize = rootElement_->GetSize();
  1195. IntVector2 DeltaP = IntVector2(eventData[P_DX].GetInt(), eventData[P_DY].GetInt());
  1196. if (cursor_)
  1197. {
  1198. if (!input->IsMouseVisible())
  1199. {
  1200. // Relative mouse motion: move cursor only when visible
  1201. if (cursor_->IsVisible())
  1202. {
  1203. IntVector2 pos = cursor_->GetPosition();
  1204. pos.x_ += eventData[P_DX].GetInt();
  1205. pos.y_ += eventData[P_DY].GetInt();
  1206. pos.x_ = Clamp(pos.x_, 0, rootSize.x_ - 1);
  1207. pos.y_ = Clamp(pos.y_, 0, rootSize.y_ - 1);
  1208. cursor_->SetPosition(pos);
  1209. }
  1210. }
  1211. else
  1212. {
  1213. // Absolute mouse motion: move always
  1214. cursor_->SetPosition(IntVector2(eventData[P_X].GetInt(), eventData[P_Y].GetInt()));
  1215. }
  1216. }
  1217. IntVector2 cursorPos;
  1218. bool cursorVisible;
  1219. GetCursorPositionAndVisible(cursorPos, cursorVisible);
  1220. ProcessMove(cursorPos, DeltaP, mouseButtons_, qualifiers_, cursor_, cursorVisible);
  1221. }
  1222. void UI::HandleMouseWheel(StringHash eventType, VariantMap& eventData)
  1223. {
  1224. Input* input = GetSubsystem<Input>();
  1225. if (input->IsMouseGrabbed())
  1226. return;
  1227. using namespace MouseWheel;
  1228. mouseButtons_ = eventData[P_BUTTONS].GetInt();
  1229. qualifiers_ = eventData[P_QUALIFIERS].GetInt();
  1230. int delta = eventData[P_WHEEL].GetInt();
  1231. usingTouchInput_ = false;
  1232. IntVector2 cursorPos;
  1233. bool cursorVisible;
  1234. GetCursorPositionAndVisible(cursorPos, cursorVisible);
  1235. UIElement* element;
  1236. if (!nonFocusedMouseWheel_ && (element = focusElement_))
  1237. element->OnWheel(delta, mouseButtons_, qualifiers_);
  1238. else
  1239. {
  1240. // If no element has actual focus or in non-focused mode, get the element at cursor
  1241. if (cursorVisible)
  1242. {
  1243. element = GetElementAt(cursorPos);
  1244. if (nonFocusedMouseWheel_)
  1245. {
  1246. // Going up the hierarchy chain to find element that could handle mouse wheel
  1247. while (element)
  1248. {
  1249. if (element->GetType() == ListView::GetTypeStatic() ||
  1250. element->GetType() == ScrollView::GetTypeStatic())
  1251. break;
  1252. element = element->GetParent();
  1253. }
  1254. }
  1255. else
  1256. // If the element itself is not focusable, search for a focusable parent,
  1257. // although the focusable element may not actually handle mouse wheel
  1258. element = GetFocusableElement(element);
  1259. if (element && (nonFocusedMouseWheel_ || element->GetFocusMode() >= FM_FOCUSABLE))
  1260. element->OnWheel(delta, mouseButtons_, qualifiers_);
  1261. }
  1262. }
  1263. }
  1264. void UI::HandleTouchBegin(StringHash eventType, VariantMap& eventData)
  1265. {
  1266. Input* input = GetSubsystem<Input>();
  1267. if (input->IsMouseGrabbed())
  1268. return;
  1269. using namespace TouchBegin;
  1270. IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
  1271. usingTouchInput_ = true;
  1272. int touchId = TOUCHID_MASK(eventData[P_TOUCHID].GetInt());
  1273. WeakPtr<UIElement> element(GetElementAt(pos));
  1274. if (element)
  1275. {
  1276. ProcessClickBegin(pos, touchId, touchDragElements_[element], 0, 0, true);
  1277. touchDragElements_[element] |= touchId;
  1278. }
  1279. else
  1280. ProcessClickBegin(pos, touchId, touchId, 0, 0, true);
  1281. }
  1282. void UI::HandleTouchEnd(StringHash eventType, VariantMap& eventData)
  1283. {
  1284. using namespace TouchEnd;
  1285. IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
  1286. // Get the touch index
  1287. int touchId = TOUCHID_MASK(eventData[P_TOUCHID].GetInt());
  1288. // Transmit hover end to the position where the finger was lifted
  1289. WeakPtr<UIElement> element(GetElementAt(pos));
  1290. // Clear any drag events that were using the touch id
  1291. for (HashMap<WeakPtr<UIElement>, int>::Iterator i = touchDragElements_.Begin(); i != touchDragElements_.End();)
  1292. {
  1293. int touches = i->second_;
  1294. if (touches & touchId)
  1295. i = touchDragElements_.Erase(i);
  1296. else
  1297. ++i;
  1298. }
  1299. if (element && element->IsEnabled())
  1300. element->OnHover(element->ScreenToElement(pos), pos, 0, 0, 0);
  1301. ProcessClickEnd(pos, touchId, 0, 0, 0, true);
  1302. }
  1303. void UI::HandleTouchMove(StringHash eventType, VariantMap& eventData)
  1304. {
  1305. using namespace TouchMove;
  1306. IntVector2 pos(eventData[P_X].GetInt(), eventData[P_Y].GetInt());
  1307. IntVector2 deltaPos(eventData[P_DX].GetInt(), eventData[P_DY].GetInt());
  1308. usingTouchInput_ = true;
  1309. int touchId = TOUCHID_MASK(eventData[P_TOUCHID].GetInt());
  1310. ProcessMove(pos, deltaPos, touchId, 0, 0, true);
  1311. }
  1312. void UI::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  1313. {
  1314. using namespace KeyDown;
  1315. mouseButtons_ = eventData[P_BUTTONS].GetInt();
  1316. qualifiers_ = eventData[P_QUALIFIERS].GetInt();
  1317. int key = eventData[P_KEY].GetInt();
  1318. // Cancel UI dragging
  1319. if (key == KEY_ESC && dragElementsCount_ > 0)
  1320. {
  1321. ProcessDragCancel();
  1322. return;
  1323. }
  1324. // Dismiss modal element if any when ESC key is pressed
  1325. if (key == KEY_ESC && HasModalElement())
  1326. {
  1327. UIElement* element = rootModalElement_->GetChild(rootModalElement_->GetNumChildren() - 1);
  1328. if (element->GetVars().Contains(VAR_ORIGIN))
  1329. // If it is a popup, dismiss by defocusing it
  1330. SetFocusElement(0);
  1331. else
  1332. {
  1333. // If it is a modal window, by resetting its modal flag
  1334. Window* window = dynamic_cast<Window*>(element);
  1335. if (window && window->GetModalAutoDismiss())
  1336. window->SetModal(false);
  1337. }
  1338. return;
  1339. }
  1340. UIElement* element = focusElement_;
  1341. if (element)
  1342. {
  1343. // Switch focus between focusable elements in the same top level window
  1344. if (key == KEY_TAB)
  1345. {
  1346. UIElement* topLevel = element->GetParent();
  1347. while (topLevel && topLevel->GetParent() != rootElement_ && topLevel->GetParent() != rootModalElement_)
  1348. topLevel = topLevel->GetParent();
  1349. if (topLevel)
  1350. {
  1351. topLevel->GetChildren(tempElements_, true);
  1352. for (PODVector<UIElement*>::Iterator i = tempElements_.Begin(); i != tempElements_.End();)
  1353. {
  1354. if ((*i)->GetFocusMode() < FM_FOCUSABLE)
  1355. i = tempElements_.Erase(i);
  1356. else
  1357. ++i;
  1358. }
  1359. for (unsigned i = 0; i < tempElements_.Size(); ++i)
  1360. {
  1361. if (tempElements_[i] == element)
  1362. {
  1363. int dir = (qualifiers_ & QUAL_SHIFT) ? -1 : 1;
  1364. unsigned nextIndex = (tempElements_.Size() + i + dir) % tempElements_.Size();
  1365. UIElement* next = tempElements_[nextIndex];
  1366. SetFocusElement(next, true);
  1367. return;
  1368. }
  1369. }
  1370. }
  1371. }
  1372. // Defocus the element
  1373. else if (key == KEY_ESC && element->GetFocusMode() == FM_FOCUSABLE_DEFOCUSABLE)
  1374. element->SetFocus(false);
  1375. // If none of the special keys, pass the key to the focused element
  1376. else
  1377. element->OnKey(key, mouseButtons_, qualifiers_);
  1378. }
  1379. }
  1380. void UI::HandleTextInput(StringHash eventType, VariantMap& eventData)
  1381. {
  1382. using namespace TextInput;
  1383. mouseButtons_ = eventData[P_BUTTONS].GetInt();
  1384. qualifiers_ = eventData[P_QUALIFIERS].GetInt();
  1385. UIElement* element = focusElement_;
  1386. if (element)
  1387. element->OnTextInput(eventData[P_TEXT].GetString(), mouseButtons_, qualifiers_);
  1388. }
  1389. void UI::HandleBeginFrame(StringHash eventType, VariantMap& eventData)
  1390. {
  1391. // If have a cursor, and a drag is not going on, reset the cursor shape. Application logic that wants to apply
  1392. // custom shapes can do it after this, but needs to do it each frame
  1393. if (cursor_ && dragElementsCount_ == 0)
  1394. cursor_->SetShape(CS_NORMAL);
  1395. }
  1396. void UI::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  1397. {
  1398. using namespace PostUpdate;
  1399. Update(eventData[P_TIMESTEP].GetFloat());
  1400. }
  1401. void UI::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
  1402. {
  1403. RenderUpdate();
  1404. }
  1405. void UI::HandleDropFile(StringHash eventType, VariantMap& eventData)
  1406. {
  1407. Input* input = GetSubsystem<Input>();
  1408. // Sending the UI variant of the event only makes sense if the OS cursor is visible (not locked to window center)
  1409. if (input->IsMouseVisible())
  1410. {
  1411. IntVector2 screenPos = input->GetMousePosition();
  1412. UIElement* element = GetElementAt(screenPos);
  1413. using namespace UIDropFile;
  1414. VariantMap uiEventData;
  1415. uiEventData[P_FILENAME] = eventData[P_FILENAME];
  1416. uiEventData[P_X] = screenPos.x_;
  1417. uiEventData[P_Y] = screenPos.y_;
  1418. uiEventData[P_ELEMENT] = element;
  1419. if (element)
  1420. {
  1421. IntVector2 relativePos = element->ScreenToElement(screenPos);
  1422. uiEventData[P_ELEMENTX] = relativePos.x_;
  1423. uiEventData[P_ELEMENTY] = relativePos.y_;
  1424. }
  1425. SendEvent(E_UIDROPFILE, uiEventData);
  1426. }
  1427. }
  1428. HashMap<WeakPtr<UIElement>, UI::DragData*>::Iterator UI::DragElementErase(HashMap<WeakPtr<UIElement>, UI::DragData*>::Iterator i)
  1429. {
  1430. // If running the engine frame in response to an event (re-entering UI frame logic) the dragElements_ may already be empty
  1431. if (dragElements_.Empty())
  1432. return dragElements_.End();
  1433. dragElementsConfirmed_.Clear();
  1434. DragData* dragData = i->second_;
  1435. if (!dragData->dragBeginPending)
  1436. --dragConfirmedCount_;
  1437. i = dragElements_.Erase(i);
  1438. --dragElementsCount_;
  1439. delete dragData;
  1440. return i;
  1441. }
  1442. void UI::ProcessDragCancel()
  1443. {
  1444. // How to tell difference between drag cancel and new selection on multi-touch?
  1445. if (usingTouchInput_)
  1446. return;
  1447. IntVector2 cursorPos;
  1448. bool cursorVisible;
  1449. GetCursorPositionAndVisible(cursorPos, cursorVisible);
  1450. for (HashMap<WeakPtr<UIElement>, UI::DragData*>::Iterator i = dragElements_.Begin(); i != dragElements_.End();)
  1451. {
  1452. WeakPtr<UIElement> dragElement = i->first_;
  1453. UI::DragData* dragData = i->second_;
  1454. if (dragElement && dragElement->IsEnabled() && dragElement->IsVisible() && !dragData->dragBeginPending)
  1455. {
  1456. dragElement->OnDragCancel(dragElement->ScreenToElement(cursorPos), cursorPos, dragData->dragButtons, mouseButtons_,
  1457. cursor_);
  1458. SendDragOrHoverEvent(E_DRAGCANCEL, dragElement, cursorPos, IntVector2::ZERO, dragData);
  1459. i = DragElementErase(i);
  1460. }
  1461. else
  1462. ++i;
  1463. }
  1464. }
  1465. IntVector2 UI::SumTouchPositions(UI::DragData* dragData, const IntVector2& oldSendPos)
  1466. {
  1467. IntVector2 sendPos = oldSendPos;
  1468. if (usingTouchInput_)
  1469. {
  1470. int buttons = dragData->dragButtons;
  1471. dragData->sumPos = IntVector2::ZERO;
  1472. Input* input = GetSubsystem<Input>();
  1473. for (int i = 0; (1 << i) <= buttons; i++)
  1474. {
  1475. if ((1 << i) & buttons)
  1476. {
  1477. TouchState* ts = input->GetTouch((unsigned)i);
  1478. if (!ts)
  1479. break;
  1480. IntVector2 pos = ts->position_;
  1481. dragData->sumPos.x_ += pos.x_;
  1482. dragData->sumPos.y_ += pos.y_;
  1483. }
  1484. }
  1485. sendPos.x_ = dragData->sumPos.x_ / dragData->numDragButtons;
  1486. sendPos.y_ = dragData->sumPos.y_ / dragData->numDragButtons;
  1487. }
  1488. return sendPos;
  1489. }
  1490. void RegisterUILibrary(Context* context)
  1491. {
  1492. Font::RegisterObject(context);
  1493. UIElement::RegisterObject(context);
  1494. BorderImage::RegisterObject(context);
  1495. Sprite::RegisterObject(context);
  1496. Button::RegisterObject(context);
  1497. CheckBox::RegisterObject(context);
  1498. Cursor::RegisterObject(context);
  1499. Text::RegisterObject(context);
  1500. Window::RegisterObject(context);
  1501. LineEdit::RegisterObject(context);
  1502. Slider::RegisterObject(context);
  1503. ScrollBar::RegisterObject(context);
  1504. ScrollView::RegisterObject(context);
  1505. ListView::RegisterObject(context);
  1506. Menu::RegisterObject(context);
  1507. DropDownList::RegisterObject(context);
  1508. MessageBox::RegisterObject(context);
  1509. ToolTip::RegisterObject(context);
  1510. }
  1511. }