UI.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "BaseUIElementFactory.h"
  25. #include "Cursor.h"
  26. #include "Font.h"
  27. #include "InputEvents.h"
  28. #include "Log.h"
  29. #include "Matrix4.h"
  30. #include "PixelShader.h"
  31. #include "Profiler.h"
  32. #include "Renderer.h"
  33. #include "RendererEvents.h"
  34. #include "RendererImpl.h"
  35. #include "ResourceCache.h"
  36. #include "StringUtils.h"
  37. #include "Texture2D.h"
  38. #include "UI.h"
  39. #include "UIEvents.h"
  40. #include "VertexShader.h"
  41. #include <algorithm>
  42. #include "DebugNew.h"
  43. static bool compareUIElements(const UIElement* lhs, const UIElement* rhs)
  44. {
  45. return lhs->getPriority() < rhs->getPriority();
  46. }
  47. UI::UI(Renderer* renderer, ResourceCache* cache) :
  48. mRenderer(renderer),
  49. mCache(cache),
  50. mMouseButtons(0),
  51. mQualifiers(0)
  52. {
  53. if (!mRenderer)
  54. EXCEPTION("Null renderer for UI");
  55. if (!mCache)
  56. EXCEPTION("Null resource cache for UI");
  57. LOGINFO("UI created");
  58. mRootElement = new UIElement();
  59. mRootElement->setSize(mRenderer->getWidth(), mRenderer->getHeight());
  60. subscribeToEvent(mRenderer, EVENT_SCREENMODE, EVENT_HANDLER(UI, handleScreenMode));
  61. subscribeToEvent(EVENT_MOUSEMOVE, EVENT_HANDLER(UI, handleMouseMove));
  62. subscribeToEvent(EVENT_MOUSEBUTTONDOWN, EVENT_HANDLER(UI, handleMouseButtonDown));
  63. subscribeToEvent(EVENT_MOUSEBUTTONUP, EVENT_HANDLER(UI, handleMouseButtonUp));
  64. subscribeToEvent(EVENT_MOUSEWHEEL, EVENT_HANDLER(UI, handleMouseWheel));
  65. subscribeToEvent(EVENT_KEYDOWN, EVENT_HANDLER(UI, handleKeyDown));
  66. subscribeToEvent(EVENT_CHAR, EVENT_HANDLER(UI, handleChar));
  67. mNoTextureVS = mCache->getResource<VertexShader>("Shaders/SM2/Basic_VCol.vs2");
  68. mDiffTextureVS = mCache->getResource<VertexShader>("Shaders/SM2/Basic_DiffVCol.vs2");
  69. mNoTexturePS = mCache->getResource<PixelShader>("Shaders/SM2/Basic_VCol.ps2");
  70. mDiffTexturePS = mCache->getResource<PixelShader>("Shaders/SM2/Basic_DiffVCol.ps2");
  71. mAlphaTexturePS = mCache->getResource<PixelShader>("Shaders/SM2/Basic_AlphaVCol.ps2");
  72. // Add the base element factory
  73. addElementFactory(new BaseUIElementFactory());
  74. }
  75. UI::~UI()
  76. {
  77. LOGINFO("UI shut down");
  78. }
  79. void UI::setCursor(Cursor* cursor)
  80. {
  81. // Remove old cursor (if any) and set new
  82. if (mCursor)
  83. {
  84. mRootElement->removeChild(mCursor);
  85. mCursor.reset();
  86. }
  87. if (cursor)
  88. {
  89. mRootElement->addChild(cursor);
  90. mCursor = cursor;
  91. IntVector2 pos = mCursor->getPosition();
  92. const IntVector2& rootSize = mRootElement->getSize();
  93. pos.mX = clamp(pos.mX, 0, rootSize.mX - 1);
  94. pos.mY = clamp(pos.mY, 0, rootSize.mY - 1);
  95. mCursor->setPosition(pos);
  96. }
  97. }
  98. void UI::setFocusElement(UIElement* element)
  99. {
  100. if (element)
  101. {
  102. // Return if already has focus
  103. if (element->hasFocus())
  104. return;
  105. // If element can not be focused, and does not reset the focus either, search toward the parent
  106. for (;;)
  107. {
  108. if (element->getFocusMode() != FM_NOTFOCUSABLE)
  109. break;
  110. element = element->getParent();
  111. // Return if did not find any parent element that changes the focus
  112. if (!element)
  113. return;
  114. }
  115. }
  116. std::vector<UIElement*> allChildren = mRootElement->getChildren(true);
  117. // Go through all elements to clear the old focus
  118. for (std::vector<UIElement*>::iterator i = allChildren.begin(); i != allChildren.end(); ++i)
  119. {
  120. UIElement* other = *i;
  121. if ((other != element) && (other->hasFocus()))
  122. other->setFocus(false);
  123. }
  124. if (element)
  125. element->setFocus(true);
  126. }
  127. void UI::clear()
  128. {
  129. mRootElement->removeAllChildren();
  130. if (mCursor)
  131. mRootElement->addChild(mCursor);
  132. }
  133. void UI::update(float timeStep)
  134. {
  135. PROFILE(UI_Update);
  136. if (mRenderer->isDeviceLost())
  137. return;
  138. if ((mCursor) && (mCursor->isVisible()))
  139. {
  140. IntVector2 pos = mCursor->getPosition();
  141. WeakPtr<UIElement> element(getElementAt(pos));
  142. bool dragSource = (mMouseDragElement) && ((mMouseDragElement->getDragDropMode() & DD_SOURCE) != 0);
  143. bool dragTarget = (element) && ((element->getDragDropMode() & DD_TARGET) != 0);
  144. bool dragDropTest = (dragSource) && (dragTarget) && (element != mMouseDragElement);
  145. // Hover effect
  146. // If a drag is going on, transmit hover only to the element being dragged, unless it's a drop target
  147. if (element)
  148. {
  149. if ((!mMouseDragElement) || (mMouseDragElement == element) || (dragDropTest))
  150. element->onHover(element->screenToElement(pos), pos, mMouseButtons, mQualifiers, mCursor);
  151. }
  152. // Drag and drop test
  153. if (dragDropTest)
  154. {
  155. bool accept = element->onDragDropTest(mMouseDragElement);
  156. if (accept)
  157. {
  158. using namespace UIDragDropTest;
  159. VariantMap eventData;
  160. eventData[P_SOURCE] = (void*)mMouseDragElement.getPtr();
  161. eventData[P_TARGET] = (void*)element.getPtr();
  162. eventData[P_ACCEPT] = accept;
  163. sendEvent(EVENT_UIDRAGDROPTEST, eventData);
  164. accept = eventData[P_ACCEPT].getBool();
  165. }
  166. mCursor->setShape(accept ? CS_ACCEPTDROP : CS_REJECTDROP);
  167. }
  168. else if (dragSource)
  169. mCursor->setShape(mMouseDragElement == element ? CS_ACCEPTDROP : CS_REJECTDROP);
  170. }
  171. // Defocus element now if should
  172. if (mDefocusElement)
  173. {
  174. // Do nothing if the focus element changed in the meanwhile
  175. if (mDefocusElement == getFocusElement())
  176. setFocusElement(0);
  177. mDefocusElement.reset();
  178. }
  179. {
  180. PROFILE(UI_UpdateElements);
  181. update(timeStep, mRootElement);
  182. }
  183. {
  184. PROFILE(UI_GetBatches);
  185. mBatches.clear();
  186. mQuads.clear();
  187. const IntVector2& rootSize = mRootElement->getSize();
  188. getBatches(mRootElement, IntRect(0, 0, rootSize.mX, rootSize.mY));
  189. }
  190. // If no drag, reset cursor shape for next frame
  191. if ((mCursor) && (!mMouseDragElement))
  192. mCursor->setShape(CS_NORMAL);
  193. }
  194. void UI::render()
  195. {
  196. PROFILE(UI_Render);
  197. static const Vector2 scale(2.0f, -2.0f);
  198. static const Vector2 offset(-1.0f, 1.0f);
  199. Matrix4 projection;
  200. memset(&projection, 0, sizeof(projection));
  201. projection.m00 = scale.mX;
  202. projection.m03 = offset.mX;
  203. projection.m11 = scale.mY;
  204. projection.m13 = offset.mY;
  205. projection.m22 = 1.0f;
  206. projection.m23 = 0.0f;
  207. projection.m33 = 1.0f;
  208. mRenderer->resetRenderTargets();
  209. mRenderer->setCullMode(CULL_CCW);
  210. mRenderer->setDepthTest(CMP_ALWAYS);
  211. mRenderer->setDepthWrite(false);
  212. mRenderer->setFillMode(FILL_SOLID);
  213. mRenderer->setStencilTest(false);
  214. mRenderer->setVertexShaderConstant(getVSRegister(VSP_MODELVIEWPROJ), projection);
  215. mRenderer->setPixelShaderConstant(getPSRegister(PSP_MATDIFFCOLOR), Color(1.0f, 1.0f, 1.0f, 1.0f));
  216. PixelShader* ps = 0;
  217. VertexShader* vs = 0;
  218. for (unsigned i = 0; i < mBatches.size(); ++i)
  219. {
  220. // Choose shaders here so that UIBatch does not need to look up shaders each time
  221. if (!mBatches[i].mTexture)
  222. {
  223. ps = mNoTexturePS;
  224. vs = mNoTextureVS;
  225. }
  226. else
  227. {
  228. // If texture contains only an alpha channel, use the alpha pixel shader
  229. vs = mDiffTextureVS;
  230. if (mBatches[i].mTexture->getFormat() == D3DFMT_A8)
  231. ps = mAlphaTexturePS;
  232. else
  233. ps = mDiffTexturePS;
  234. }
  235. mBatches[i].draw(mRenderer, vs, ps);
  236. }
  237. }
  238. void UI::addElementFactory(UIElementFactory* factory)
  239. {
  240. if (!factory)
  241. return;
  242. mFactories.push_back(SharedPtr<UIElementFactory>(factory));
  243. }
  244. SharedPtr<UIElement> UI::createElement(ShortStringHash type, const std::string& name)
  245. {
  246. SharedPtr<UIElement> element;
  247. for (unsigned i = 0; i < mFactories.size(); ++i)
  248. {
  249. element = mFactories[i]->createElement(type, name);
  250. if (element)
  251. return element;
  252. }
  253. EXCEPTION("Could not create unknown UI element type " + toString(type));
  254. }
  255. SharedPtr<UIElement> UI::loadLayout(XMLFile* file, XMLFile* styleFile)
  256. {
  257. PROFILE(UI_LoadLayout);
  258. SharedPtr<UIElement> root;
  259. if (!file)
  260. {
  261. LOGERROR("Null UI layout XML file");
  262. return root;
  263. }
  264. LOGDEBUG("Loading UI layout " + file->getName());
  265. XMLElement rootElem = file->getRootElement("element");
  266. if (!rootElem)
  267. {
  268. LOGERROR("No root UI element in " + file->getName());
  269. return root;
  270. }
  271. root = createElement(ShortStringHash(rootElem.getString("type")), rootElem.getString("name"));
  272. // First set the base style from the style file if exists, then apply UI layout overrides
  273. if (styleFile)
  274. root->setStyleAuto(styleFile, mCache);
  275. root->setStyle(rootElem, mCache);
  276. // Load rest of the elements recursively
  277. loadLayout(root, rootElem, styleFile);
  278. return root;
  279. }
  280. UIElement* UI::getElementAt(const IntVector2& position, bool enabledOnly)
  281. {
  282. UIElement* result = 0;
  283. getElementAt(result, mRootElement, position, enabledOnly);
  284. return result;
  285. }
  286. UIElement* UI::getElementAt(int x, int y, bool enabledOnly)
  287. {
  288. return getElementAt(IntVector2(x, y), enabledOnly);
  289. }
  290. UIElement* UI::getFocusElement()
  291. {
  292. std::vector<UIElement*> allChildren = mRootElement->getChildren(true);
  293. for (std::vector<UIElement*>::iterator i = allChildren.begin(); i != allChildren.end(); ++i)
  294. {
  295. if ((*i)->hasFocus())
  296. return *i;
  297. }
  298. return 0;
  299. }
  300. IntVector2 UI::getCursorPosition()
  301. {
  302. if (!mCursor)
  303. return IntVector2::sZero;
  304. else
  305. return mCursor->getPosition();
  306. }
  307. void UI::update(float timeStep, UIElement* element)
  308. {
  309. element->update(timeStep);
  310. const std::vector<UIElement*> children = element->getChildren();
  311. for (std::vector<UIElement*>::const_iterator i = children.begin(); i != children.end(); ++i)
  312. update(timeStep, *i);
  313. }
  314. void UI::getBatches(UIElement* element, IntRect currentScissor)
  315. {
  316. // Set clipping scissor for child elements. No need to draw if zero size
  317. element->adjustScissor(currentScissor);
  318. if ((currentScissor.mLeft == currentScissor.mRight) || (currentScissor.mTop == currentScissor.mBottom))
  319. return;
  320. std::vector<UIElement*> children = element->getChildren();
  321. if (children.empty())
  322. return;
  323. std::sort(children.begin(), children.end(), compareUIElements);
  324. // For non-root elements draw all children of same priority before recursing into their children: assumption is that they have
  325. // same renderstate
  326. std::vector<UIElement*>::const_iterator i = children.begin();
  327. if (element != mRootElement)
  328. {
  329. std::vector<UIElement*>::const_iterator j = i;
  330. int currentPriority = children.front()->getPriority();
  331. while (i != children.end())
  332. {
  333. while ((j != children.end()) && ((*j)->getPriority() == currentPriority))
  334. {
  335. if ((*j)->isVisible())
  336. (*j)->getBatches(mBatches, mQuads, currentScissor);
  337. ++j;
  338. }
  339. // Now recurse into the children
  340. while (i != j)
  341. {
  342. if ((*i)->isVisible())
  343. getBatches(*i, currentScissor);
  344. ++i;
  345. }
  346. if (i != children.end())
  347. currentPriority = (*i)->getPriority();
  348. }
  349. }
  350. // On the root level draw each element and its children immediately after to avoid artifacts
  351. else
  352. {
  353. while (i != children.end())
  354. {
  355. if ((*i)->isVisible())
  356. {
  357. (*i)->getBatches(mBatches, mQuads, currentScissor);
  358. getBatches(*i, currentScissor);
  359. }
  360. ++i;
  361. }
  362. }
  363. }
  364. void UI::getElementAt(UIElement*& result, UIElement* current, const IntVector2& position, bool enabledOnly)
  365. {
  366. if (!current)
  367. return;
  368. // Get children from lowest priority to highest
  369. std::vector<UIElement*> children = current->getChildren(false);
  370. std::sort(children.begin(), children.end(), compareUIElements);
  371. for (std::vector<UIElement*>::const_iterator i = children.begin(); i != children.end(); ++i)
  372. {
  373. UIElement* element = *i;
  374. if ((element != mCursor.getPtr()) && (element->isVisible()))
  375. {
  376. if (element->isInside(position, true))
  377. {
  378. // Store the current result, then recurse into its children. Because children
  379. // are sorted from lowest to highest priority, we should be left with the topmost match
  380. if ((element->isEnabled()) || (!enabledOnly))
  381. result = element;
  382. }
  383. if (element->isInsideCombined(position, true))
  384. getElementAt(result, element, position, enabledOnly);
  385. }
  386. }
  387. }
  388. void UI::handleScreenMode(StringHash eventType, VariantMap& eventData)
  389. {
  390. using namespace ScreenMode;
  391. mRootElement->setSize(mRenderer->getWidth(), mRenderer->getHeight());
  392. }
  393. void UI::handleMouseMove(StringHash eventType, VariantMap& eventData)
  394. {
  395. using namespace MouseMove;
  396. mMouseButtons = eventData[P_BUTTONS].getInt();
  397. mQualifiers = eventData[P_QUALIFIERS].getInt();
  398. if (mCursor)
  399. {
  400. if (eventData[P_CLIPCURSOR].getBool())
  401. {
  402. // When in confined cursor mode, move cursor only when visible
  403. if (mCursor->isVisible())
  404. {
  405. IntVector2 pos = mCursor->getPosition();
  406. pos.mX += eventData[P_DX].getInt();
  407. pos.mY += eventData[P_DY].getInt();
  408. const IntVector2& rootSize = mRootElement->getSize();
  409. pos.mX = clamp(pos.mX, 0, rootSize.mX - 1);
  410. pos.mY = clamp(pos.mY, 0, rootSize.mY - 1);
  411. mCursor->setPosition(pos);
  412. }
  413. }
  414. else
  415. {
  416. // When in non-confined mode, move cursor always to ensure accurate position
  417. // Do not clamp, but hide the cursor when not in the window's client area
  418. int x = eventData[P_X].getInt();
  419. int y = eventData[P_Y].getInt();
  420. mCursor->setPosition(x, y);
  421. mCursor->setVisible((x >= 0) && (y >= 0) && (x < mRenderer->getWidth()) && (y < mRenderer->getHeight()));
  422. }
  423. if ((mMouseDragElement) && (mMouseButtons))
  424. {
  425. IntVector2 pos = mCursor->getPosition();
  426. if ((mMouseDragElement->isEnabled()) && (mMouseDragElement->isVisible()))
  427. mMouseDragElement->onDragMove(mMouseDragElement->screenToElement(pos), pos, mMouseButtons, mQualifiers, mCursor);
  428. else
  429. mMouseDragElement.reset();
  430. }
  431. }
  432. }
  433. void UI::handleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  434. {
  435. mMouseButtons = eventData[MouseButtonDown::P_BUTTONS].getInt();
  436. mQualifiers = eventData[MouseButtonDown::P_QUALIFIERS].getInt();
  437. int button = eventData[MouseButtonDown::P_BUTTON].getInt();
  438. if ((mCursor) && (mCursor->isVisible()))
  439. {
  440. IntVector2 pos = mCursor->getPosition();
  441. WeakPtr<UIElement> element(getElementAt(pos));
  442. if (element)
  443. {
  444. // Handle focusing & bringing to front
  445. if (button == MOUSEB_LEFT)
  446. {
  447. setFocusElement(element);
  448. element->bringToFront();
  449. }
  450. // Handle click
  451. element->onClick(element->screenToElement(pos), pos, mMouseButtons, mQualifiers, mCursor);
  452. // Handle start of drag. onClick() may have caused destruction of the element, so check the pointer again
  453. if ((element) && (!mMouseDragElement))
  454. {
  455. mMouseDragElement = element;
  456. element->onDragStart(element->screenToElement(pos), pos, mMouseButtons, mQualifiers, mCursor);
  457. }
  458. }
  459. else
  460. {
  461. // If clicked over no element, or a disabled element, try to lose focus
  462. mDefocusElement = getFocusElement();
  463. }
  464. using namespace UIMouseClick;
  465. VariantMap eventData;
  466. eventData[UIMouseClick::P_ELEMENT] = (void*)element.getPtr();
  467. eventData[UIMouseClick::P_X] = pos.mX;
  468. eventData[UIMouseClick::P_Y] = pos.mY;
  469. eventData[UIMouseClick::P_BUTTON] = button;
  470. eventData[UIMouseClick::P_BUTTONS] = mMouseButtons;
  471. eventData[UIMouseClick::P_QUALIFIERS] = mQualifiers;
  472. sendEvent(EVENT_UIMOUSECLICK, eventData);
  473. }
  474. }
  475. void UI::handleMouseButtonUp(StringHash eventType, VariantMap& eventData)
  476. {
  477. using namespace MouseButtonUp;
  478. mMouseButtons = eventData[P_BUTTONS].getInt();
  479. mQualifiers = eventData[P_QUALIFIERS].getInt();
  480. if ((mCursor) && (mCursor->isVisible()))
  481. {
  482. IntVector2 pos = mCursor->getPosition();
  483. if ((mMouseDragElement) && (!mMouseButtons))
  484. {
  485. if ((mMouseDragElement->isEnabled()) && (mMouseDragElement->isVisible()))
  486. {
  487. mMouseDragElement->onDragEnd(mMouseDragElement->screenToElement(pos), pos, mCursor);
  488. // Drag and drop finish
  489. bool dragSource = (mMouseDragElement) && ((mMouseDragElement->getDragDropMode() & DD_SOURCE) != 0);
  490. if (dragSource)
  491. {
  492. WeakPtr<UIElement> target(getElementAt(pos));
  493. bool dragTarget = (target) && ((target->getDragDropMode() & DD_TARGET) != 0);
  494. bool dragDropFinish = (dragSource) && (dragTarget) && (target != mMouseDragElement);
  495. if (dragDropFinish)
  496. {
  497. bool accept = target->onDragDropFinish(mMouseDragElement);
  498. // onDragDropFinish() may have caused destruction of the elements, so check the pointers again
  499. if ((accept) && (mMouseDragElement) && (target))
  500. {
  501. using namespace UIDragDropFinish;
  502. VariantMap eventData;
  503. eventData[P_SOURCE] = (void*)mMouseDragElement.getPtr();
  504. eventData[P_TARGET] = (void*)target.getPtr();
  505. eventData[P_ACCEPT] = accept;
  506. sendEvent(EVENT_UIDRAGDROPFINISH, eventData);
  507. }
  508. }
  509. }
  510. }
  511. mMouseDragElement.reset();
  512. }
  513. }
  514. }
  515. void UI::handleMouseWheel(StringHash eventType, VariantMap& eventData)
  516. {
  517. using namespace MouseWheel;
  518. mMouseButtons = eventData[P_BUTTONS].getInt();
  519. mQualifiers = eventData[P_QUALIFIERS].getInt();
  520. int delta = eventData[P_WHEEL].getInt();
  521. UIElement* element = getFocusElement();
  522. if (element)
  523. element->onWheel(delta, mMouseButtons, mQualifiers);
  524. }
  525. void UI::handleKeyDown(StringHash eventType, VariantMap& eventData)
  526. {
  527. using namespace KeyDown;
  528. mMouseButtons = eventData[P_BUTTONS].getInt();
  529. mQualifiers = eventData[P_QUALIFIERS].getInt();
  530. int key = eventData[P_KEY].getInt();
  531. UIElement* element = getFocusElement();
  532. if (element)
  533. {
  534. // Switch focus between focusable elements in the same top level window
  535. if (key == KEY_TAB)
  536. {
  537. UIElement* topLevel = element->getParent();
  538. while ((topLevel) && (topLevel->getParent() != mRootElement))
  539. topLevel = topLevel->getParent();
  540. if (topLevel)
  541. {
  542. std::vector<UIElement*> children = topLevel->getChildren(true);
  543. for (std::vector<UIElement*>::iterator i = children.begin(); i != children.end();)
  544. {
  545. if ((*i)->getFocusMode() < FM_FOCUSABLE)
  546. i = children.erase(i);
  547. else
  548. ++i;
  549. }
  550. for (unsigned i = 0; i < children.size(); ++i)
  551. {
  552. if (children[i] == element)
  553. {
  554. UIElement* next = children[(i + 1) % children.size()];
  555. setFocusElement(next);
  556. return;
  557. }
  558. }
  559. }
  560. }
  561. // Defocus the element
  562. else if ((key == KEY_ESC) && (element->getFocusMode() == FM_FOCUSABLE_DEFOCUSABLE))
  563. mDefocusElement = element;
  564. // If none of the special keys, pass the key to the focused element
  565. else
  566. element->onKey(key, mMouseButtons, mQualifiers);
  567. }
  568. }
  569. void UI::handleChar(StringHash eventType, VariantMap& eventData)
  570. {
  571. using namespace Char;
  572. mMouseButtons = eventData[P_BUTTONS].getInt();
  573. mQualifiers = eventData[P_QUALIFIERS].getInt();
  574. UIElement* element = getFocusElement();
  575. if (element)
  576. element->onChar(eventData[P_CHAR].getInt(), mMouseButtons, mQualifiers);
  577. }
  578. void UI::loadLayout(UIElement* current, const XMLElement& elem, XMLFile* styleFile)
  579. {
  580. XMLElement childElem = elem.getChildElement("element");
  581. while (childElem)
  582. {
  583. // Create and add to the hierarchy
  584. SharedPtr<UIElement> child = createElement(ShortStringHash(childElem.getString("type")), childElem.getString("name"));
  585. current->addChild(child);
  586. // First set the base style from the style file if exists, then apply UI layout overrides
  587. if (styleFile)
  588. child->setStyleAuto(styleFile, mCache);
  589. child->setStyle(childElem, mCache);
  590. // Load the children recursively
  591. loadLayout(child, childElem, styleFile);
  592. childElem = childElem.getNextElement("element");
  593. }
  594. }