PageView.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace GitMerge.Utilities
  7. {
  8. public class PageView
  9. {
  10. public int PageIndex { get; set; } = 0;
  11. public int NumElementsPerPage { get; set; } = 10;
  12. private Vector2 scrollPosition;
  13. /// <summary>
  14. /// Draw a scroll view only a limited number of elements displayed.
  15. /// Add tool to change the page to display previous/next range of elements.
  16. /// </summary>
  17. /// <param name="numMaxElements">The total number of elements to draw.</param>
  18. /// <param name="callbackElementDraw">Called for each element to draw. The element index to draw is passed as argument.</param>
  19. public void Draw(int numMaxElements, Action<int> callbackElementDraw)
  20. {
  21. GUILayout.BeginVertical(GUILayout.ExpandWidth(true));
  22. {
  23. DrawPageContent(callbackElementDraw, numMaxElements);
  24. DrawPageNavigation(numMaxElements);
  25. }
  26. GUILayout.EndVertical();
  27. }
  28. private void DrawPageContent(Action<int> callbackElementDraw, int numMaxElements)
  29. {
  30. scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, true);
  31. {
  32. GUILayout.BeginVertical(GUILayout.ExpandWidth(true));
  33. int lastElementIndex = Mathf.Min((PageIndex + 1) * NumElementsPerPage, numMaxElements);
  34. for (int index = PageIndex * NumElementsPerPage; index < lastElementIndex; ++index)
  35. {
  36. callbackElementDraw(index);
  37. }
  38. GUILayout.EndVertical();
  39. }
  40. GUILayout.EndScrollView();
  41. }
  42. private void DrawPageNavigation(int numMaxElements)
  43. {
  44. int numPages = CalculateNumberOfPages(numMaxElements);
  45. if (numPages == 0)
  46. {
  47. return;
  48. }
  49. GUILayout.BeginHorizontal();
  50. {
  51. EditorGUILayout.PrefixLabel("Count Per Page");
  52. int newNumElementsPerPage = EditorGUILayout.DelayedIntField(NumElementsPerPage, GUILayout.Width(100));
  53. if (newNumElementsPerPage != NumElementsPerPage)
  54. {
  55. NumElementsPerPage = Mathf.Max(newNumElementsPerPage, 1);
  56. numPages = CalculateNumberOfPages(numMaxElements);
  57. }
  58. GUILayout.FlexibleSpace();
  59. EditorGUI.BeginDisabledGroup(PageIndex == 0);
  60. {
  61. if (GUILayout.Button("<"))
  62. {
  63. --PageIndex;
  64. }
  65. }
  66. EditorGUI.EndDisabledGroup();
  67. int newPageIndex = EditorGUILayout.DelayedIntField(PageIndex + 1, GUILayout.Width(30)) - 1;
  68. PageIndex = Mathf.Clamp(newPageIndex, 0, numPages - 1);
  69. GUILayout.Label("/" + numPages);
  70. EditorGUI.BeginDisabledGroup(PageIndex == numPages - 1);
  71. {
  72. if (GUILayout.Button(">"))
  73. {
  74. ++PageIndex;
  75. }
  76. }
  77. EditorGUI.EndDisabledGroup();
  78. }
  79. GUILayout.EndHorizontal();
  80. }
  81. private int CalculateNumberOfPages(int numMaxElements)
  82. {
  83. int numPages = numMaxElements / NumElementsPerPage;
  84. if (numMaxElements % NumElementsPerPage != 0)
  85. {
  86. ++numPages;
  87. }
  88. return numPages;
  89. }
  90. }
  91. }