SystemUI.cpp 61 KB

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