UIManager.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <UI/UIManager.h>
  6. #include <UI/UIAnimationSlide.h>
  7. #include <Jolt/Core/Profiler.h>
  8. #include <Renderer/Renderer.h>
  9. #include <Renderer/Font.h>
  10. struct QuadVertex
  11. {
  12. Float3 mPosition;
  13. Float2 mTexCoord;
  14. Color mColor;
  15. };
  16. UIManager::UIManager(Renderer *inRenderer) :
  17. mRenderer(inRenderer),
  18. mListener(nullptr),
  19. mState(STATE_INVALID)
  20. {
  21. mManager = this;
  22. // Set dimensions of the screen
  23. ApplicationWindow *window = mRenderer->GetWindow();
  24. SetWidth(window->GetWindowWidth());
  25. SetHeight(window->GetWindowHeight());
  26. // Create input layout
  27. const PipelineState::EInputDescription vertex_desc[] =
  28. {
  29. PipelineState::EInputDescription::Position,
  30. PipelineState::EInputDescription::TexCoord,
  31. PipelineState::EInputDescription::Color
  32. };
  33. // Load vertex shader
  34. Ref<VertexShader> vtx = mRenderer->CreateVertexShader("Assets/Shaders/UIVertexShader");
  35. // Load pixel shader
  36. Ref<PixelShader> pix_textured = mRenderer->CreatePixelShader("Assets/Shaders/UIPixelShader");
  37. Ref<PixelShader> pix_untextured = mRenderer->CreatePixelShader("Assets/Shaders/UIPixelShaderUntextured");
  38. mTextured = mRenderer->CreatePipelineState(vtx, vertex_desc, std::size(vertex_desc), pix_textured, PipelineState::EDrawPass::Normal, PipelineState::EFillMode::Solid, PipelineState::ETopology::Triangle, PipelineState::EDepthTest::Off, PipelineState::EBlendMode::AlphaBlend, PipelineState::ECullMode::Backface);
  39. mUntextured = mRenderer->CreatePipelineState(vtx, vertex_desc, std::size(vertex_desc), pix_untextured, PipelineState::EDrawPass::Normal, PipelineState::EFillMode::Solid, PipelineState::ETopology::Triangle, PipelineState::EDepthTest::Off, PipelineState::EBlendMode::AlphaBlend, PipelineState::ECullMode::Backface);
  40. }
  41. UIManager::~UIManager()
  42. {
  43. while (!mInactiveElements.empty())
  44. PopLayer();
  45. }
  46. // Update elements
  47. void UIManager::Update(float inDeltaTime)
  48. {
  49. JPH_PROFILE_FUNCTION();
  50. // Update inactive elements (array can resize at any time, so no iterators and extra checking here)
  51. for (int i = (int)mInactiveElements.size() - 1; i >= 0; --i)
  52. for (int j = 0; i < (int)mInactiveElements.size() && j < (int)mInactiveElements[i].size(); ++j)
  53. mInactiveElements[i][j]->Update(inDeltaTime);
  54. // Update elements
  55. UIElement::Update(inDeltaTime);
  56. // Update state
  57. mStateTime += inDeltaTime;
  58. switch (mState)
  59. {
  60. case STATE_ACTIVATING:
  61. if (mStateTime > cActivateScreenTime)
  62. SwitchToState(STATE_ACTIVE);
  63. break;
  64. case STATE_DEACTIVATING:
  65. if (mStateTime > cActivateScreenTime)
  66. SwitchToState(STATE_DEACTIVE);
  67. break;
  68. case STATE_ACTIVE:
  69. case STATE_DEACTIVE:
  70. case STATE_INVALID:
  71. default:
  72. break;
  73. }
  74. }
  75. void UIManager::Draw() const
  76. {
  77. JPH_PROFILE_FUNCTION();
  78. // Switch tho ortho mode
  79. mRenderer->SetOrthoMode();
  80. // Draw inactive elements first
  81. if (mDrawInactiveElements)
  82. for (int i = (int)mInactiveElements.size() - 1; i >= 0; --i)
  83. for (const UIElement *j : mInactiveElements[i])
  84. if (j->IsVisible())
  85. j->Draw();
  86. // Then draw active elements
  87. UIElement::Draw();
  88. // Restore state
  89. mRenderer->SetProjectionMode();
  90. }
  91. void UIManager::PushLayer()
  92. {
  93. mInactiveElements.push_back(mChildren);
  94. mChildren.clear();
  95. }
  96. void UIManager::PopLayer()
  97. {
  98. Clear();
  99. mChildren = mInactiveElements.back();
  100. mInactiveElements.pop_back();
  101. }
  102. UIElement *UIManager::FindByID(int inID)
  103. {
  104. UIElement *element = UIElement::FindByID(inID);
  105. if (element != nullptr)
  106. return element;
  107. for (int i = (int)mInactiveElements.size() - 1; i >= 0; --i)
  108. for (int j = 0; j < (int)mInactiveElements[i].size(); ++j)
  109. {
  110. element = mInactiveElements[i][j]->FindByID(inID);
  111. if (element != nullptr)
  112. return element;
  113. }
  114. return nullptr;
  115. }
  116. bool UIManager::HandleUIEvent(EUIEvent inEvent, UIElement *inSender)
  117. {
  118. if (UIElement::HandleUIEvent(inEvent, inSender))
  119. return true;
  120. return mListener != nullptr && mListener->HandleUIEvent(inEvent, inSender);
  121. }
  122. void UIManager::GetMaxElementDistanceToScreenEdge(int &outMaxH, int &outMaxV)
  123. {
  124. outMaxH = 0;
  125. outMaxV = 0;
  126. ApplicationWindow *window = mRenderer->GetWindow();
  127. for (const UIElement *e : mChildren)
  128. if (e->HasDeactivateAnimation())
  129. {
  130. int dl = e->GetX() + e->GetWidth();
  131. int dr = window->GetWindowWidth() - e->GetX();
  132. outMaxH = max(outMaxH, min(dl, dr));
  133. int dt = e->GetY() + e->GetHeight();
  134. int db = window->GetWindowHeight() - e->GetY();
  135. outMaxV = max(outMaxV, min(dt, db));
  136. }
  137. }
  138. void UIManager::SwitchToState(EState inState)
  139. {
  140. // Clean up old state
  141. switch (mState)
  142. {
  143. case STATE_ACTIVATING:
  144. case STATE_DEACTIVATING:
  145. for (UIElement *e : mChildren)
  146. e->StopAnimation(JPH_RTTI(UIAnimationSlide));
  147. break;
  148. case STATE_ACTIVE:
  149. case STATE_DEACTIVE:
  150. case STATE_INVALID:
  151. default:
  152. break;
  153. }
  154. // Store new state
  155. mState = inState;
  156. mStateTime = 0.0f;
  157. // Calculate max horizontal and vertical distance of elements to edge of screen
  158. int max_h, max_v;
  159. GetMaxElementDistanceToScreenEdge(max_h, max_v);
  160. switch (inState)
  161. {
  162. case STATE_ACTIVATING:
  163. for (UIElement *e : mChildren)
  164. if (e->HasActivateAnimation())
  165. e->StartAnimation(new UIAnimationSlide(UIAnimationSlide::SLIDE_ON_SCREEN, max_h, max_v, 0.0f, cActivateScreenTime));
  166. break;
  167. case STATE_DEACTIVATING:
  168. for (UIElement *e : mChildren)
  169. if (e->HasDeactivateAnimation())
  170. e->StartAnimation(new UIAnimationSlide(UIAnimationSlide::SLIDE_OFF_SCREEN, max_h, max_v, 0.0f, cActivateScreenTime));
  171. break;
  172. case STATE_DEACTIVE:
  173. HandleUIEvent(EVENT_MENU_DEACTIVATED, this);
  174. if (mDeactivatedAction)
  175. mDeactivatedAction();
  176. break;
  177. case STATE_ACTIVE:
  178. case STATE_INVALID:
  179. default:
  180. break;
  181. }
  182. }
  183. inline static void sDrawQuad(QuadVertex *&v ,float x1, float y1, float x2, float y2, float tx1, float ty1, float tx2, float ty2, ColorArg inColor)
  184. {
  185. v->mPosition = Float3(x1, y1, 0);
  186. v->mTexCoord = Float2(tx1, ty1);
  187. v->mColor = inColor;
  188. ++v;
  189. v->mPosition = Float3(x1, y2, 0);
  190. v->mTexCoord = Float2(tx1, ty2);
  191. v->mColor = inColor;
  192. ++v;
  193. v->mPosition = Float3(x2, y2, 0);
  194. v->mTexCoord = Float2(tx2, ty2);
  195. v->mColor = inColor;
  196. ++v;
  197. v->mPosition = Float3(x1, y1, 0);
  198. v->mTexCoord = Float2(tx1, ty1);
  199. v->mColor = inColor;
  200. ++v;
  201. v->mPosition = Float3(x2, y2, 0);
  202. v->mTexCoord = Float2(tx2, ty2);
  203. v->mColor = inColor;
  204. ++v;
  205. v->mPosition = Float3(x2, y1, 0);
  206. v->mTexCoord = Float2(tx2, ty1);
  207. v->mColor = inColor;
  208. ++v;
  209. }
  210. void UIManager::DrawQuad(int inX, int inY, int inWidth, int inHeight, const UITexturedQuad &inQuad, ColorArg inColor)
  211. {
  212. // Outer area - screen coordinates
  213. float x1 = float(inX);
  214. float y1 = float(inY);
  215. float x2 = float(inX + inWidth);
  216. float y2 = float(inY + inHeight);
  217. if (inQuad.mTexture != nullptr)
  218. {
  219. bool has_inner = inQuad.HasInnerPart();
  220. Ref<RenderPrimitive> primitive = mRenderer->CreateRenderPrimitive(PipelineState::ETopology::Triangle);
  221. primitive->CreateVertexBuffer(has_inner? 9 * 6 : 6, sizeof(QuadVertex));
  222. float w = float(inQuad.mTexture->GetWidth()), h = float(inQuad.mTexture->GetHeight());
  223. // Outer area - texture coordinates
  224. float tx1 = float(inQuad.mX);
  225. float ty1 = float(inQuad.mY);
  226. float tx2 = float(inQuad.mX + inQuad.mWidth);
  227. float ty2 = float(inQuad.mY + inQuad.mHeight);
  228. tx1 /= w; ty1 /= h;
  229. tx2 /= w; ty2 /= h;
  230. QuadVertex *v = (QuadVertex *)primitive->LockVertexBuffer();
  231. if (has_inner)
  232. {
  233. // Inner area - screen coordinates
  234. float ix1 = float(inX + inQuad.mInnerX - inQuad.mX);
  235. float iy1 = float(inY + inQuad.mInnerY - inQuad.mY);
  236. float ix2 = float(inX + inWidth - (inQuad.mWidth - inQuad.mInnerWidth - (inQuad.mInnerX - inQuad.mX)));
  237. float iy2 = float(inY + inHeight - (inQuad.mHeight - inQuad.mInnerHeight - (inQuad.mInnerY - inQuad.mY)));
  238. // Inner area - texture coordinates
  239. float itx1 = float(inQuad.mInnerX);
  240. float ity1 = float(inQuad.mInnerY);
  241. float itx2 = float(inQuad.mInnerX + inQuad.mInnerWidth);
  242. float ity2 = float(inQuad.mInnerY + inQuad.mInnerHeight);
  243. itx1 /= w; ity1 /= h;
  244. itx2 /= w; ity2 /= h;
  245. sDrawQuad(v, x1, y1, ix1, iy1, tx1, ty1, itx1, ity1, inColor);
  246. sDrawQuad(v, ix1, y1, ix2, iy1, itx1, ty1, itx2, ity1, inColor);
  247. sDrawQuad(v, ix2, y1, x2, iy1, itx2, ty1, tx2, ity1, inColor);
  248. sDrawQuad(v, x1, iy1, ix1, iy2, tx1, ity1, itx1, ity2, inColor);
  249. sDrawQuad(v, ix1, iy1, ix2, iy2, itx1, ity1, itx2, ity2, inColor);
  250. sDrawQuad(v, ix2, iy1, x2, iy2, itx2, ity1, tx2, ity2, inColor);
  251. sDrawQuad(v, x1, iy2, ix1, y2, tx1, ity2, itx1, ty2, inColor);
  252. sDrawQuad(v, ix1, iy2, ix2, y2, itx1, ity2, itx2, ty2, inColor);
  253. sDrawQuad(v, ix2, iy2, x2, y2, itx2, ity2, tx2, ty2, inColor);
  254. }
  255. else
  256. {
  257. sDrawQuad(v, x1, y1, x2, y2, tx1, ty1, tx2, ty2, inColor);
  258. }
  259. primitive->UnlockVertexBuffer();
  260. inQuad.mTexture->Bind();
  261. mTextured->Activate();
  262. primitive->Draw();
  263. }
  264. else
  265. {
  266. Ref<RenderPrimitive> primitive = mRenderer->CreateRenderPrimitive(PipelineState::ETopology::Triangle);
  267. primitive->CreateVertexBuffer(6, sizeof(QuadVertex));
  268. QuadVertex *v = (QuadVertex *)primitive->LockVertexBuffer();
  269. sDrawQuad(v, x1, y1, x2, y2, 0, 0, 0, 0, inColor);
  270. primitive->UnlockVertexBuffer();
  271. mUntextured->Activate();
  272. primitive->Draw();
  273. }
  274. }
  275. void UIManager::DrawText(int inX, int inY, const string_view &inText, const Font *inFont, ColorArg inColor)
  276. {
  277. Vec4 pos(float(inX), float(inY), 0.0f, 1.0f);
  278. Vec4 right(float(inFont->GetCharHeight()), 0.0f, 0.0f, 0.0f);
  279. Vec4 up(0.0f, float(-inFont->GetCharHeight()), 0.0f, 0.0f);
  280. Vec4 forward(0.0f, 0.0f, 1.0f, 0.0f);
  281. Mat44 transform(right, up, forward, pos);
  282. inFont->DrawText3D(transform, inText, inColor);
  283. }