UIManager.h 2.6 KB

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