UI.cpp 61 KB

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