UiObject.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (C) 2009-2021, 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. namespace anki {
  8. /// @addtogroup ui
  9. /// @{
  10. /// The base of all UI objects.
  11. class UiObject
  12. {
  13. public:
  14. UiObject(UiManager* manager)
  15. : m_manager(manager)
  16. {
  17. ANKI_ASSERT(manager);
  18. }
  19. virtual ~UiObject() = default;
  20. UiAllocator getAllocator() const;
  21. Atomic<I32>& getRefcount()
  22. {
  23. return m_refcount;
  24. }
  25. /// Set the global IMGUI allocator.
  26. void setImAllocator(BaseMemoryPool* pool = nullptr)
  27. {
  28. pool = (pool) ? pool : &getAllocator().getMemoryPool();
  29. auto allocCallback = [](size_t size, void* userData) -> void* {
  30. BaseMemoryPool* pool = static_cast<BaseMemoryPool*>(userData);
  31. return pool->allocate(size, 16);
  32. };
  33. auto freeCallback = [](void* ptr, void* userData) -> void {
  34. if(ptr)
  35. {
  36. BaseMemoryPool* pool = static_cast<BaseMemoryPool*>(userData);
  37. pool->free(ptr);
  38. }
  39. };
  40. ImGui::SetAllocatorFunctions(allocCallback, freeCallback, pool);
  41. }
  42. /// Unset the global IMGUI allocator.
  43. static void unsetImAllocator()
  44. {
  45. ImGui::SetAllocatorFunctions(nullptr, nullptr, nullptr);
  46. }
  47. protected:
  48. UiManager* m_manager;
  49. Atomic<I32> m_refcount = {0};
  50. };
  51. /// @}
  52. } // end namespace anki