UI.cpp 60 KB

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