imgui_tex_inspect_internal.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // ImGuiTexInspect, a texture inspector widget for dear imgui
  2. #pragma once
  3. #include "imgui.h"
  4. #include "imgui_internal.h"
  5. #include "imgui_tex_inspect.h"
  6. namespace ImGuiTexInspect
  7. {
  8. //-------------------------------------------------------------------------
  9. // [SECTION] UTILITIES
  10. //-------------------------------------------------------------------------
  11. // Returns true if a flag is set
  12. template <typename TSet, typename TFlag>
  13. static inline bool HasFlag(TSet set, TFlag flag)
  14. {
  15. return (set & flag) == flag;
  16. }
  17. // Set flag or flags in set
  18. template <typename TSet, typename TFlag>
  19. static inline void SetFlag(TSet &set, TFlag flags)
  20. {
  21. set = static_cast<TSet>(set | flags);
  22. }
  23. // Clear flag or flags in set
  24. template <typename TSet, typename TFlag>
  25. static inline void ClearFlag(TSet &set, TFlag flag)
  26. {
  27. set = static_cast<TSet>(set & ~flag);
  28. }
  29. // Proper modulus operator, as opposed to remainder as calculated by %
  30. template <typename T>
  31. static inline T Modulus(T a, T b)
  32. {
  33. return a - b * ImFloorSigned(a / b);
  34. }
  35. // Defined in recent versions of imgui_internal.h. Included here in case user is on older
  36. // imgui version.
  37. static inline float ImFloorSigned(float f)
  38. {
  39. return (float)((f >= 0 || (int)f == f) ? (int)f : (int)f - 1);
  40. }
  41. static inline float Round(float f)
  42. {
  43. return ImFloorSigned(f + 0.5f);
  44. }
  45. static inline ImVec2 Abs(ImVec2 v)
  46. {
  47. return ImVec2(ImAbs(v.x), ImAbs(v.y));
  48. }
  49. //-------------------------------------------------------------------------
  50. // [SECTION] STRUCTURES
  51. //-------------------------------------------------------------------------
  52. struct ShaderOptions
  53. {
  54. float ColorTransform[16] = {}; // See CurrentInspector_SetColorMatrix for details
  55. float ColorOffset[4] = {};
  56. ImVec4 BackgroundColor = {0,0,0,0}; // Color used for alpha blending
  57. float PremultiplyAlpha = 0; // If 1 then color will be multiplied by alpha in shader, before blend stage
  58. float DisableFinalAlpha = 0; // If 1 then fragment shader will always output alpha = 1
  59. bool ForceNearestSampling = false; // If true fragment shader will always sample from texel centers
  60. ImVec2 GridWidth = {0,0}; // Width in UV coords of grid line
  61. ImVec4 GridColor = {0,0,0,0};
  62. void ResetColorTransform();
  63. ShaderOptions();
  64. };
  65. struct Inspector
  66. {
  67. ImGuiID ID;
  68. bool Initialized = false;
  69. // Texture
  70. ImTextureID Texture = 0;
  71. ImVec2 TextureSize = {0, 0}; // Size in texels of texture
  72. float PixelAspectRatio = 1; // Values other than 1 not supported yet
  73. // View State
  74. bool IsDragging = false; // Is user currently dragging to pan view
  75. ImVec2 PanPos = {0.5f, 0.5f}; // The UV value at the center of the current view
  76. ImVec2 Scale = {1, 1}; // 1 pixel is 1 texel
  77. ImVec2 PanelTopLeftPixel = {0, 0}; // Top left of view in ImGui pixel coordinates
  78. ImVec2 PanelSize = {0, 0}; // Size of area allocated to drawing the image in pixels.
  79. ImVec2 ViewTopLeftPixel = {0, 0}; // Position in ImGui pixel coordinates
  80. ImVec2 ViewSize = {0, 0}; // Rendered size of current image. This could be smaller than panel size if user has zoomed out.
  81. ImVec2 ViewSizeUV = {0, 0}; // Visible region of the texture in UV coordinates
  82. /* Conversion transforms to go back and forth between screen pixels (what ImGui considers screen pixels) and texels*/
  83. Transform2D TexelsToPixels;
  84. Transform2D PixelsToTexels;
  85. // Cached pixel data
  86. bool HaveCurrentTexelData = false;
  87. BufferDesc Buffer;
  88. /* We don't actually access texel data through this pointer. We just
  89. * manage its lifetime. The backend might have asked us to allocated a
  90. * buffer, or it might not. The pointer we actually use to access texel
  91. * data is in the Buffer object above (which depending on what the backend
  92. * did might point to the same memory as this pointer)
  93. */
  94. ImU8 *DataBuffer = nullptr;
  95. size_t DataBufferSize = 0;
  96. // Configuration
  97. InspectorFlags Flags = 0;
  98. // Background mode
  99. InspectorAlphaMode AlphaMode = InspectorAlphaMode_ImGui;
  100. ImVec4 CustomBackgroundColor = {0, 0, 0, 1};
  101. // Scaling limits
  102. ImVec2 ScaleMin = {0.02f, 0.02f};
  103. ImVec2 ScaleMax = {500, 500};
  104. // Grid
  105. float MinimumGridSize = 4; // Don't draw the grid if lines would be closer than MinimumGridSize pixels
  106. // Annotations
  107. ImU32 MaxAnnotatedTexels = 0;
  108. // Color transformation
  109. ShaderOptions ActiveShaderOptions;
  110. ShaderOptions CachedShaderOptions;
  111. ~Inspector();
  112. };
  113. //-------------------------------------------------------------------------
  114. // [SECTION] INTERNAL FUNCTIONS
  115. //-------------------------------------------------------------------------
  116. Inspector *GetByKey(const Context *ctx, ImGuiID key);
  117. Inspector *GetOrAddByKey(Context *ctx, ImGuiID key);
  118. void SetPanPos(Inspector *inspector, ImVec2 pos);
  119. void SetScale(Inspector *inspector, ImVec2 scale);
  120. void SetScale(Inspector *inspector, float scaleY);
  121. void RoundPanPos(Inspector *inspector);
  122. ImU8 *GetBuffer(Inspector *inspector, size_t bytes);
  123. /* GetTexelsToPixels
  124. * Calculate a transform to convert from texel coordinates to screen pixel coordinates
  125. * */
  126. Transform2D GetTexelsToPixels(ImVec2 screenTopLeft, ImVec2 screenViewSize, ImVec2 uvTopLeft, ImVec2 uvViewSize, ImVec2 textureSize);
  127. //-------------------------------------------------------------------------
  128. // [SECTION] IMGUI UTILS
  129. //-------------------------------------------------------------------------
  130. /* TextVector
  131. * Draws a single-column ImGui table with one row for each provided string
  132. */
  133. void TextVector(const char *title, const char *const *strings, int n);
  134. /* PushDisabled & PopDisabled
  135. * Push and Pop and ImGui styles that disable and "grey out" ImGui elements
  136. * by making them non interactive and transparent*/
  137. void PushDisabled();
  138. void PopDisabled();
  139. //-------------------------------------------------------------------------
  140. // [SECTION] BACKEND FUNCTIONS
  141. //-------------------------------------------------------------------------
  142. void BackEnd_SetShader(const ImDrawList *drawList, const ImDrawCmd *cmd, const Inspector *inspector);
  143. bool BackEnd_GetData(Inspector *inspector, ImTextureID texture, int x, int y, int width, int height, BufferDesc *buffer);
  144. } // namespace ImGuiTexInspect