UIManager.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <UI/UIElement.h>
  6. #include <UI/UITexturedQuad.h>
  7. #include <Renderer/PipelineState.h>
  8. #include <memory>
  9. class Font;
  10. const float cActivateScreenTime = 0.2f;
  11. /// Manager class that manages UIElements
  12. class UIManager : public UIElement
  13. {
  14. public:
  15. /// Constructor
  16. UIManager(Renderer *inRenderer);
  17. virtual ~UIManager() override;
  18. /// Update elements
  19. virtual void Update(float inDeltaTime) override;
  20. /// Draw elements
  21. virtual void Draw() const override;
  22. /// Only one layer can be active, if you push a layer it will exit in the background and no longer be updated
  23. void PushLayer();
  24. void PopLayer();
  25. int GetNumLayers() const { return (int)mInactiveElements.size() + 1; }
  26. void SetDrawInactiveLayers(bool inDraw) { mDrawInactiveElements = inDraw; }
  27. /// Find element by ID
  28. virtual UIElement * FindByID(int inID) override;
  29. /// Listeners
  30. void SetListener(UIEventListener *inListener) { mListener = inListener; }
  31. UIEventListener * GetListener() const { return mListener; }
  32. /// Actions
  33. void SetDeactivatedAction(function<void()> inAction) { mDeactivatedAction = inAction; }
  34. /// Event handling (returns true if the event has been handled)
  35. virtual bool HandleUIEvent(EUIEvent inEvent, UIElement *inSender) override;
  36. /// Current state
  37. enum EState
  38. {
  39. STATE_INVALID,
  40. STATE_ACTIVATING,
  41. STATE_ACTIVE,
  42. STATE_DEACTIVATING,
  43. STATE_DEACTIVE
  44. };
  45. void SwitchToState(EState inState);
  46. EState GetState() const { return mState; }
  47. /// Calculate max horizontal and vertical distance of elements to edge of screen
  48. void GetMaxElementDistanceToScreenEdge(int &outMaxH, int &outMaxV);
  49. /// Access to the renderer
  50. Renderer * GetRenderer() { return mRenderer; }
  51. /// Drawing
  52. void DrawQuad(int inX, int inY, int inWidth, int inHeight, const UITexturedQuad &inQuad, ColorArg inColor);
  53. /// Draw a string in screen coordinates (assumes that the projection matrix has been set up correctly)
  54. void DrawText(int inX, int inY, const string_view &inText, const Font *inFont, ColorArg inColor = Color::sWhite);
  55. private:
  56. Renderer * mRenderer;
  57. UIEventListener * mListener;
  58. Array<UIElementVector> mInactiveElements;
  59. bool mDrawInactiveElements = true;
  60. unique_ptr<PipelineState> mTextured;
  61. unique_ptr<PipelineState> mUntextured;
  62. function<void()> mDeactivatedAction;
  63. EState mState;
  64. float mStateTime;
  65. };