UIManager.h 2.5 KB

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