UiCanvas.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Ui/Common.h>
  7. #include <AnKi/Resource/ShaderProgramResource.h>
  8. #include <AnKi/Resource/GenericResource.h>
  9. #include <AnKi/Gr/Sampler.h>
  10. namespace anki {
  11. /// @addtogroup ui
  12. /// @{
  13. /// UI canvas. It's more of a context.
  14. class UiCanvas : public UiObject
  15. {
  16. friend class UiManager;
  17. public:
  18. UiCanvas() = default;
  19. ~UiCanvas();
  20. /// Resize canvas.
  21. void resize(UVec2 size)
  22. {
  23. ANKI_ASSERT(size > 0);
  24. m_size = size;
  25. }
  26. UVec2 getSize() const
  27. {
  28. return m_size;
  29. }
  30. Vec2 getSizef() const
  31. {
  32. return Vec2(m_size);
  33. }
  34. /// @name building commands. The order matters.
  35. /// @{
  36. virtual void handleInput();
  37. void beginBuilding();
  38. ImFont* addFont(CString fname);
  39. /// Merge fonts into one
  40. ImFont* addFonts(ConstWeakArray<CString> fnames);
  41. void endBuilding();
  42. template<typename TFunc>
  43. void visitTexturesForUpdate(TFunc func)
  44. {
  45. for(const UploadRequest& req : m_texturesPendingUpload)
  46. {
  47. func(*req.m_texture, req.m_isNew);
  48. }
  49. }
  50. void appendNonGraphicsCommands(CommandBuffer& cmdb) const;
  51. void appendGraphicsCommands(CommandBuffer& cmdb) const;
  52. /// @}
  53. private:
  54. ImGuiContext* m_imCtx = nullptr;
  55. ImFont* m_defaultFont = nullptr;
  56. UVec2 m_size;
  57. enum ShaderType
  58. {
  59. kNoTex,
  60. kRgbaTex,
  61. kShaderCount
  62. };
  63. ShaderProgramResourcePtr m_prog;
  64. Array<ShaderProgramPtr, kShaderCount> m_grProgs;
  65. SamplerPtr m_linearLinearRepeatSampler;
  66. SamplerPtr m_nearestNearestRepeatSampler;
  67. class FontCacheEntry
  68. {
  69. public:
  70. ImFont* m_font = nullptr;
  71. UiDynamicArray<GenericResourcePtr> m_resources;
  72. };
  73. UiHashMap<U64, FontCacheEntry> m_fontCache;
  74. class UploadRequest
  75. {
  76. public:
  77. Texture* m_texture = nullptr;
  78. TextureRect m_rect;
  79. UiDynamicArray<U8> m_data;
  80. Bool m_isNew = true;
  81. };
  82. UiDynamicArray<UploadRequest> m_texturesPendingUpload;
  83. Error init(UVec2 size);
  84. };
  85. /// @}
  86. } // end namespace anki