imguiRowsBackground.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #pragma once
  2. #include <imgui.h>
  3. #include <cstdint>
  4. #include <cctype>
  5. #include <utility>
  6. #include <vector>
  7. #include <string>
  8. #include <algorithm>
  9. //https://github.com/ocornut/imgui/issues/2668
  10. namespace ImGui
  11. {
  12. void ItemRowsBackground(float lineHeight = -1.0f, const ImColor &color = ImColor(20, 20, 20, 64))
  13. {
  14. auto *drawList = ImGui::GetWindowDrawList();
  15. const auto &style = ImGui::GetStyle();
  16. if (lineHeight < 0)
  17. {
  18. lineHeight = ImGui::GetTextLineHeight();
  19. }
  20. lineHeight += style.ItemSpacing.y;
  21. float scrollOffsetH = ImGui::GetScrollX();
  22. float scrollOffsetV = ImGui::GetScrollY();
  23. float scrolledOutLines = floorf(scrollOffsetV / lineHeight);
  24. scrollOffsetV -= lineHeight * scrolledOutLines;
  25. ImVec2 clipRectMin(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y);
  26. ImVec2 clipRectMax(clipRectMin.x + ImGui::GetWindowWidth(), clipRectMin.y + ImGui::GetWindowHeight());
  27. if (ImGui::GetScrollMaxX() > 0)
  28. {
  29. clipRectMax.y -= style.ScrollbarSize;
  30. }
  31. drawList->PushClipRect(clipRectMin, clipRectMax);
  32. bool isOdd = (static_cast<int>(scrolledOutLines) % 2) == 0;
  33. float yMin = clipRectMin.y - scrollOffsetV + ImGui::GetCursorPosY();
  34. float yMax = clipRectMax.y - scrollOffsetV + lineHeight;
  35. float xMin = clipRectMin.x + scrollOffsetH + ImGui::GetWindowContentRegionMin().x;
  36. float xMax = clipRectMin.x + scrollOffsetH + ImGui::GetWindowContentRegionMax().x;
  37. for (float y = yMin; y < yMax; y += lineHeight, isOdd = !isOdd)
  38. {
  39. if (isOdd)
  40. {
  41. drawList->AddRectFilled({xMin, y - style.ItemSpacing.y}, {xMax, y + lineHeight}, color);
  42. }
  43. }
  44. drawList->PopClipRect();
  45. }
  46. }
  47. // Please do not forget this!
  48. //ImGui::Begin("Example Bug");
  49. //ItemRowsBackground();
  50. // whatever code that draws list or tree items
  51. //ImGui::End();