SystemUI.cpp 61 KB

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