ScrollableToolbar.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System.Collections.Generic;
  2. using UnityEditor.U2D.Animation;
  3. using UnityEngine;
  4. using UnityEngine.UIElements;
  5. namespace UnityEditor.U2D.Layout
  6. {
  7. internal class ScrollableToolbar : VisualElement
  8. {
  9. public class ScrollableToolbarFactory : UxmlFactory<ScrollableToolbar, ScrollableToolbarUxmlTraits> {}
  10. public class ScrollableToolbarUxmlTraits : UxmlTraits
  11. {
  12. UxmlBoolAttributeDescription m_IsHorizontal;
  13. public ScrollableToolbarUxmlTraits()
  14. {
  15. m_IsHorizontal = new UxmlBoolAttributeDescription { name = "isHorizontal" };
  16. }
  17. public override IEnumerable<UxmlChildElementDescription> uxmlChildElementsDescription
  18. {
  19. get { yield break; }
  20. }
  21. public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
  22. {
  23. base.Init(ve, bag, cc);
  24. ScrollableToolbar toolbar = ((ScrollableToolbar)ve);
  25. toolbar.isHorizontal = m_IsHorizontal.GetValueFromBag(bag, cc);
  26. }
  27. }
  28. private ScrollView m_ScrollView;
  29. private bool m_IsHorizontal;
  30. public bool isHorizontal
  31. {
  32. get { return m_IsHorizontal; }
  33. set
  34. {
  35. if (m_IsHorizontal != value)
  36. {
  37. m_IsHorizontal = value;
  38. SetupScrolling();
  39. }
  40. }
  41. }
  42. public ScrollableToolbar() : this(false)
  43. {
  44. }
  45. public ScrollableToolbar(bool isHorizontal)
  46. {
  47. m_ScrollView = new ScrollView() {name = "ScrollView"};;
  48. m_ScrollView.StretchToParentSize();
  49. hierarchy.Add(m_ScrollView);
  50. m_IsHorizontal = isHorizontal;
  51. SetupScrolling();
  52. styleSheets.Add(ResourceLoader.Load<StyleSheet>("LayoutOverlay/ScrollableToolbar.uss"));
  53. // TODO: Add onto current ScrollView internal WheelEvent
  54. m_ScrollView.RegisterCallback<WheelEvent>(OnScrollWheel);
  55. pickingMode = PickingMode.Ignore;
  56. m_ScrollView.pickingMode = PickingMode.Ignore;
  57. m_ScrollView.contentViewport.pickingMode = PickingMode.Ignore;
  58. m_ScrollView.contentContainer.pickingMode = PickingMode.Ignore;
  59. }
  60. public void AddToContainer(VisualElement element)
  61. {
  62. m_ScrollView.contentContainer.Add(element);
  63. }
  64. public void Collapse(bool collapse)
  65. {
  66. if (collapse)
  67. AddToClassList("Collapse");
  68. else
  69. RemoveFromClassList("Collapse");
  70. }
  71. private void SetupScrolling()
  72. {
  73. if (isHorizontal)
  74. {
  75. m_ScrollView.style.flexDirection = FlexDirection.Row;
  76. m_ScrollView.contentViewport.style.marginLeft = 10;
  77. m_ScrollView.contentViewport.style.marginRight = 10;
  78. m_ScrollView.contentViewport.style.marginTop = 0;
  79. m_ScrollView.contentViewport.style.marginBottom = 0;
  80. m_ScrollView.contentContainer.style.flexDirection = FlexDirection.Row;
  81. m_ScrollView.contentContainer.style.flexGrow = 1f;
  82. }
  83. else
  84. {
  85. m_ScrollView.style.flexDirection = FlexDirection.Column;
  86. m_ScrollView.contentViewport.style.marginLeft = 0;
  87. m_ScrollView.contentViewport.style.marginRight = 0;
  88. m_ScrollView.contentViewport.style.marginTop = 10;
  89. m_ScrollView.contentViewport.style.marginBottom = 10;
  90. m_ScrollView.contentContainer.style.flexDirection = FlexDirection.Column;
  91. m_ScrollView.contentContainer.style.flexGrow = 1f;
  92. }
  93. }
  94. void OnScrollWheel(WheelEvent evt)
  95. {
  96. /*
  97. // Handled by ScrollView
  98. if (!isHorizontal && m_ScrollView.contentContainer.layout.height - layout.height > 0)
  99. {
  100. if (evt.delta.y < 0)
  101. m_ScrollView.verticalScroller.ScrollPageUp();
  102. else if (evt.delta.y > 0)
  103. m_ScrollView.verticalScroller.ScrollPageDown();
  104. }
  105. */
  106. if (isHorizontal && m_ScrollView.contentContainer.layout.width - layout.width > 0)
  107. {
  108. // TODO: Does not provide delta.x for sidescrolling mouse wheel. Use delta.y for now.
  109. if (evt.delta.y < 0)
  110. m_ScrollView.horizontalScroller.ScrollPageUp();
  111. else if (evt.delta.y > 0)
  112. m_ScrollView.horizontalScroller.ScrollPageDown();
  113. }
  114. evt.StopPropagation();
  115. }
  116. }
  117. }