UIManager.h 2.5 KB

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