UI.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 "Cursor.h"
  25. #include "Font.h"
  26. #include "InputEvents.h"
  27. #include "Log.h"
  28. #include "Matrix4.h"
  29. #include "PixelShader.h"
  30. #include "Profiler.h"
  31. #include "Renderer.h"
  32. #include "RendererEvents.h"
  33. #include "RendererImpl.h"
  34. #include "ResourceCache.h"
  35. #include "Texture2D.h"
  36. #include "UI.h"
  37. #include "VertexShader.h"
  38. #include <algorithm>
  39. #include "DebugNew.h"
  40. static bool compareUIElements(const UIElement* lhs, const UIElement* rhs)
  41. {
  42. return lhs->getPriority() < rhs->getPriority();
  43. }
  44. UI::UI(Renderer* renderer, ResourceCache* cache) :
  45. mRenderer(renderer),
  46. mCache(cache),
  47. mMouseDrag(false),
  48. mMouseDragElement(0),
  49. mMouseButtons(0)
  50. {
  51. if (!mRenderer)
  52. EXCEPTION("Null renderer for UI");
  53. if (!mCache)
  54. EXCEPTION("Null resource cache for UI");
  55. LOGINFO("UI created");
  56. mRootElement = new UIElement();
  57. mRootElement->setSize(mRenderer->getWidth(), mRenderer->getHeight());
  58. subscribeToEvent(EVENT_WINDOWRESIZED, EVENT_HANDLER(UI, handleWindowResized));
  59. subscribeToEvent(EVENT_MOUSEMOVE, EVENT_HANDLER(UI, handleMouseMove));
  60. subscribeToEvent(EVENT_MOUSEBUTTONDOWN, EVENT_HANDLER(UI, handleMouseButtonDown));
  61. subscribeToEvent(EVENT_MOUSEBUTTONUP, EVENT_HANDLER(UI, handleMouseButtonUp));
  62. subscribeToEvent(EVENT_CHAR, EVENT_HANDLER(UI, handleChar));
  63. mNoTextureVS = mCache->getResource<VertexShader>("Shaders/SM2/Basic_VCol.vs2");
  64. mDiffTextureVS = mCache->getResource<VertexShader>("Shaders/SM2/Basic_DiffVCol.vs2");
  65. mNoTexturePS = mCache->getResource<PixelShader>("Shaders/SM2/Basic_VCol.ps2");
  66. mDiffTexturePS = mCache->getResource<PixelShader>("Shaders/SM2/Basic_DiffVCol.ps2");
  67. mAlphaTexturePS = mCache->getResource<PixelShader>("Shaders/SM2/Basic_AlphaVCol.ps2");
  68. }
  69. UI::~UI()
  70. {
  71. LOGINFO("UI shut down");
  72. }
  73. void UI::setCursor(Cursor* cursor)
  74. {
  75. // Remove old cursor (if any) and set new
  76. if (mCursor)
  77. {
  78. mRootElement->removeChild(mCursor);
  79. mCursor.reset();
  80. }
  81. if (cursor)
  82. {
  83. mRootElement->addChild(cursor);
  84. mCursor = cursor;
  85. IntVector2 pos = mCursor->getPosition();
  86. IntVector2 rootSize = mRootElement->getSize();
  87. pos.mX = clamp(pos.mX, 0, rootSize.mX - 1);
  88. pos.mY = clamp(pos.mY, 0, rootSize.mY - 1);
  89. mCursor->setPosition(pos);
  90. }
  91. }
  92. void UI::setFocusElement(UIElement* element)
  93. {
  94. if (element)
  95. {
  96. if ((element->hasFocus()) || (!element->isFocusable()))
  97. return;
  98. }
  99. std::vector<UIElement*> allChildren = mRootElement->getChildren(true);
  100. // Go through all elements to clear the old focus
  101. for (std::vector<UIElement*>::iterator i = allChildren.begin(); i != allChildren.end(); ++i)
  102. {
  103. UIElement* other = *i;
  104. if ((other != element) && (other->hasFocus()))
  105. other->setFocus(false);
  106. }
  107. if (element)
  108. element->setFocus(true);
  109. }
  110. void UI::bringToFront(UIElement* element)
  111. {
  112. if (!element)
  113. return;
  114. // Follow the parent chain to the top level window. If it has BringToFront mode, bring it to front now
  115. UIElement* ptr = element;
  116. while ((ptr) && (ptr->getParent() != mRootElement))
  117. ptr = ptr->getParent();
  118. if ((!ptr) || (!ptr->getBringToFront()))
  119. return;
  120. // Get the highest priority used by all other top level elements, decrease their priority by one,
  121. // and assign that to new front element. However, take into account only active (enabled) elements
  122. // so that any noninteractive overlays are left alone
  123. int maxPriority = M_MIN_INT;
  124. std::vector<UIElement*> topLevelElements = mRootElement->getChildren();
  125. for (std::vector<UIElement*>::iterator i = topLevelElements.begin(); i != topLevelElements.end(); ++i)
  126. {
  127. UIElement* other = *i;
  128. if ((other->isEnabled()) && (other != ptr))
  129. {
  130. int priority = other->getPriority();
  131. maxPriority = max(priority, maxPriority);
  132. other->setPriority(priority - 1);
  133. }
  134. }
  135. ptr->setPriority(maxPriority);
  136. }
  137. void UI::update(float timeStep)
  138. {
  139. PROFILE(UI_Update);
  140. // If device lost, do not perform update
  141. if (mRenderer->isDeviceLost())
  142. return;
  143. if ((mCursor) && (mCursor->isVisible()))
  144. {
  145. IntVector2 pos = mCursor->getPosition();
  146. UIElement* element = getElementAt(pos);
  147. if (element)
  148. element->onHover(element->screenToElement(pos), pos);
  149. }
  150. {
  151. PROFILE(UI_UpdateElements);
  152. update(timeStep, mRootElement);
  153. }
  154. {
  155. PROFILE(UI_GetBatches);
  156. mBatches.clear();
  157. mQuads.clear();
  158. IntVector2 rootSize = mRootElement->getSize();
  159. getBatches(mRootElement, IntRect(0, 0, rootSize.mX, rootSize.mY));
  160. }
  161. }
  162. void UI::render()
  163. {
  164. PROFILE(UI_Render);
  165. static const Vector2 scale(2.0f, -2.0f);
  166. static const Vector2 offset(-1.0f, 1.0f);
  167. Matrix4 projection;
  168. memset(&projection, 0, sizeof(projection));
  169. projection.m00 = scale.mX;
  170. projection.m03 = offset.mX;
  171. projection.m11 = scale.mY;
  172. projection.m13 = offset.mY;
  173. projection.m22 = 1.0f;
  174. projection.m23 = 0.0f;
  175. projection.m33 = 1.0f;
  176. mRenderer->resetRenderTargets();
  177. mRenderer->setCullMode(CULL_CCW);
  178. mRenderer->setDepthTest(CMP_ALWAYS);
  179. mRenderer->setDepthWrite(false);
  180. mRenderer->setFillMode(FILL_SOLID);
  181. mRenderer->setStencilTest(false);
  182. mRenderer->setVertexShaderConstant(getVSRegister(VSP_MODELVIEWPROJ), projection);
  183. mRenderer->setPixelShaderConstant(getPSRegister(PSP_MATDIFFCOLOR), Color(1.0f, 1.0f, 1.0f, 1.0f));
  184. PixelShader* ps = 0;
  185. VertexShader* vs = 0;
  186. for (unsigned i = 0; i < mBatches.size(); ++i)
  187. {
  188. // Choose shaders here so that UIBatch does not need to look up shaders each time
  189. if (!mBatches[i].mTexture)
  190. {
  191. ps = mNoTexturePS;
  192. vs = mNoTextureVS;
  193. }
  194. else
  195. {
  196. // If texture contains only an alpha channel, use the alpha pixel shader
  197. vs = mDiffTextureVS;
  198. if (mBatches[i].mTexture->getFormat() == D3DFMT_A8)
  199. ps = mAlphaTexturePS;
  200. else
  201. ps = mDiffTexturePS;
  202. }
  203. mBatches[i].draw(mRenderer, vs, ps);
  204. }
  205. }
  206. UIElement* UI::getElementAt(const IntVector2& position, bool enabledOnly)
  207. {
  208. UIElement* result = 0;
  209. getElementAt(result, mRootElement, position, enabledOnly);
  210. return result;
  211. }
  212. UIElement* UI::getElementAt(int x, int y, bool enabledOnly)
  213. {
  214. UIElement* result = 0;
  215. getElementAt(result, mRootElement, IntVector2(x, y), enabledOnly);
  216. return result;
  217. }
  218. UIElement* UI::getFocusElement()
  219. {
  220. std::vector<UIElement*> allChildren = mRootElement->getChildren(true);
  221. for (std::vector<UIElement*>::iterator i = allChildren.begin(); i != allChildren.end(); ++i)
  222. {
  223. if ((*i)->hasFocus())
  224. return *i;
  225. }
  226. return 0;
  227. }
  228. IntVector2 UI::getCursorPosition()
  229. {
  230. if (!mCursor)
  231. return IntVector2::sZero;
  232. else
  233. return mCursor->getPosition();
  234. }
  235. void UI::update(float timeStep, UIElement* element)
  236. {
  237. element->update(timeStep);
  238. const std::vector<UIElement*> children = element->getChildren();
  239. for (std::vector<UIElement*>::const_iterator i = children.begin(); i != children.end(); ++i)
  240. update(timeStep, *i);
  241. }
  242. void UI::getBatches(UIElement* element, IntRect currentScissor)
  243. {
  244. // If hidden, do not draw this element or its children
  245. if (!element->isVisible())
  246. return;
  247. element->getBatches(mBatches, mQuads, currentScissor);
  248. // Set clipping scissor for child elements, then get child element batches in low-to-high priority order
  249. element->adjustScissor(currentScissor);
  250. std::vector<UIElement*> children = element->getChildren();
  251. std::sort(children.begin(), children.end(), compareUIElements);
  252. for (std::vector<UIElement*>::const_iterator i = children.begin(); i != children.end(); ++i)
  253. getBatches(*i, currentScissor);
  254. }
  255. void UI::getElementAt(UIElement*& result, UIElement* current, const IntVector2& position, bool enabledOnly)
  256. {
  257. if (!current)
  258. return;
  259. // Get children from lowest priority to highest
  260. std::vector<UIElement*> children = current->getChildren(false);
  261. std::sort(children.begin(), children.end(), compareUIElements);
  262. for (std::vector<UIElement*>::const_iterator i = children.begin(); i != children.end(); ++i)
  263. {
  264. UIElement* element = *i;
  265. if ((element != mCursor.getPtr()) && (element->isVisible()))
  266. {
  267. const IntVector2& screenPosition = element->getScreenPosition();
  268. const IntVector2& size = element->getSize();
  269. if ((position.mX >= screenPosition.mX) && (position.mX < screenPosition.mX + size.mX) &&
  270. (position.mY >= screenPosition.mY) && (position.mY < screenPosition.mY + size.mY))
  271. {
  272. // Store the current result, then recurse into its children. Because children
  273. // are sorted from lowest to highest priority, we should be left with the topmost match
  274. if ((element->isEnabled()) || (!enabledOnly))
  275. result = element;
  276. getElementAt(result, element, position, enabledOnly);
  277. }
  278. }
  279. }
  280. }
  281. UIElement* UI::verifyElement(UIElement* element)
  282. {
  283. std::vector<UIElement*> allChildren = mRootElement->getChildren(true);
  284. for (std::vector<UIElement*>::iterator i = allChildren.begin(); i != allChildren.end(); ++i)
  285. {
  286. if ((*i) == element)
  287. return *i;
  288. }
  289. return 0;
  290. }
  291. void UI::handleWindowResized(StringHash eventType, VariantMap& eventData)
  292. {
  293. using namespace WindowResized;
  294. mRootElement->setSize(eventData[P_WIDTH].getInt(), eventData[P_HEIGHT].getInt());
  295. }
  296. void UI::handleMouseMove(StringHash eventType, VariantMap& eventData)
  297. {
  298. using namespace MouseMove;
  299. mMouseButtons = eventData[P_BUTTONS].getInt();
  300. if ((mCursor) && (mCursor->isVisible()))
  301. {
  302. IntVector2 pos = mCursor->getPosition();
  303. pos.mX += eventData[P_X].getInt();
  304. pos.mY += eventData[P_Y].getInt();
  305. IntVector2 rootSize = mRootElement->getSize();
  306. pos.mX = clamp(pos.mX, 0, rootSize.mX - 1);
  307. pos.mY = clamp(pos.mY, 0, rootSize.mY - 1);
  308. mCursor->setPosition(pos);
  309. if ((mMouseDrag) && (mMouseButtons))
  310. {
  311. UIElement* element = verifyElement(mMouseDragElement);
  312. if ((element) && (element->isEnabled()) && (element->isVisible()))
  313. element->onDragMove(element->screenToElement(pos), pos, mMouseButtons);
  314. else
  315. {
  316. mMouseDrag = false;
  317. mMouseDragElement = 0;
  318. }
  319. }
  320. }
  321. }
  322. void UI::handleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  323. {
  324. using namespace MouseButtonDown;
  325. mMouseButtons = eventData[P_BUTTONS].getInt();
  326. int button = eventData[P_BUTTON].getInt();
  327. if ((mCursor) && (mCursor->isVisible()))
  328. {
  329. IntVector2 pos = mCursor->getPosition();
  330. UIElement* element = getElementAt(pos);
  331. if (element)
  332. {
  333. // Handle focusing & bringing to front
  334. if (button == MOUSEB_LEFT)
  335. {
  336. setFocusElement(element);
  337. bringToFront(element);
  338. }
  339. // Handle click
  340. element->onClick(element->screenToElement(pos), pos, mMouseButtons);
  341. // Handle start of drag
  342. if (!mMouseDrag)
  343. {
  344. mMouseDrag = true;
  345. mMouseDragElement = element;
  346. element->onDragStart(element->screenToElement(pos), pos, mMouseButtons);
  347. }
  348. }
  349. }
  350. }
  351. void UI::handleMouseButtonUp(StringHash eventType, VariantMap& eventData)
  352. {
  353. using namespace MouseButtonUp;
  354. mMouseButtons = eventData[P_BUTTONS].getInt();
  355. if ((mCursor) && (mCursor->isVisible()))
  356. {
  357. IntVector2 pos = mCursor->getPosition();
  358. if ((mMouseDrag) && (!mMouseButtons))
  359. {
  360. UIElement* element = verifyElement(mMouseDragElement);
  361. if ((element) && (element->isEnabled()) && (element->isVisible()))
  362. element->onDragEnd(element->screenToElement(pos), pos);
  363. mMouseDrag = false;
  364. mMouseDragElement = 0;
  365. }
  366. }
  367. }
  368. void UI::handleChar(StringHash eventType, VariantMap& eventData)
  369. {
  370. using namespace Char;
  371. UIElement* element = getFocusElement();
  372. if (element)
  373. element->onChar(eventData[P_CHAR].getInt());
  374. }