UI.cpp 59 KB

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