2
0

UIScreen.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #pragma once
  9. #include "Math.h"
  10. #include <cstdint>
  11. #include <string>
  12. #include <functional>
  13. #include <vector>
  14. class Button
  15. {
  16. public:
  17. Button(const std::string& name, class Font* font,
  18. std::function<void()> onClick,
  19. const Vector2& pos, const Vector2& dims);
  20. ~Button();
  21. // Set the name of the button
  22. void SetName(const std::string& name);
  23. // Getters/setters
  24. class Texture* GetNameTex() { return mNameTex; }
  25. const Vector2& GetPosition() const { return mPosition; }
  26. void SetHighlighted(bool sel) { mHighlighted = sel; }
  27. bool GetHighlighted() const { return mHighlighted; }
  28. // Returns true if the point is within the button's bounds
  29. bool ContainsPoint(const Vector2& pt) const;
  30. // Called when button is clicked
  31. void OnClick();
  32. private:
  33. std::function<void()> mOnClick;
  34. std::string mName;
  35. class Texture* mNameTex;
  36. class Font* mFont;
  37. Vector2 mPosition;
  38. Vector2 mDimensions;
  39. bool mHighlighted;
  40. };
  41. class UIScreen
  42. {
  43. public:
  44. UIScreen(class Game* game);
  45. virtual ~UIScreen();
  46. // UIScreen subclasses can override these
  47. virtual void Update(float deltaTime);
  48. virtual void Draw(class Shader* shader);
  49. virtual void ProcessInput(const uint8_t* keys);
  50. virtual void HandleKeyPress(int key);
  51. // Tracks if the UI is active or closing
  52. enum UIState
  53. {
  54. EActive,
  55. EClosing
  56. };
  57. // Set state to closing
  58. void Close();
  59. // Get state of UI screen
  60. UIState GetState() const { return mState; }
  61. // Change the title text
  62. void SetTitle(const std::string& text,
  63. const Vector3& color = Color::White,
  64. int pointSize = 40);
  65. // Add a button to this screen
  66. void AddButton(const std::string& name, std::function<void()> onClick);
  67. protected:
  68. // Helper to draw a texture
  69. void DrawTexture(class Shader* shader, class Texture* texture,
  70. const Vector2& offset = Vector2::Zero,
  71. float scale = 1.0f);
  72. // Sets the mouse mode to relative or not
  73. void SetRelativeMouseMode(bool relative);
  74. class Game* mGame;
  75. class Font* mFont;
  76. class Texture* mTitle;
  77. class Texture* mBackground;
  78. class Texture* mButtonOn;
  79. class Texture* mButtonOff;
  80. // Configure positions
  81. Vector2 mTitlePos;
  82. Vector2 mNextButtonPos;
  83. Vector2 mBGPos;
  84. // State
  85. UIState mState;
  86. // List of buttons
  87. std::vector<Button*> mButtons;
  88. };