VFXMatrix4x4Field.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. using UnityEditor.UIElements;
  4. using Action = System.Action;
  5. using FloatField = UnityEditor.VFX.UI.VFXLabeledField<UnityEditor.UIElements.FloatField, float>;
  6. namespace UnityEditor.VFX.UI
  7. {
  8. class VFXMatrix4x4Field : VFXControl<Matrix4x4>
  9. {
  10. FloatField[,] m_FloatFields;
  11. void CreateTextField()
  12. {
  13. m_FloatFields = new FloatField[4, 4];
  14. for (int i = 0; i < m_FloatFields.GetLength(0); ++i)
  15. {
  16. for (int j = 0; j < m_FloatFields.GetLength(1); ++j)
  17. {
  18. var newField = new FloatField(string.Format("{0}{1}", i, j));
  19. m_FloatFields[i, j] = newField;
  20. newField.AddToClassList("fieldContainer");
  21. newField.control.AddToClassList("fieldContainer");
  22. newField.RegisterCallback<ChangeEvent<float>>(OnFloatValueChanged);
  23. newField.onValueDragFinished = t => ValueDragFinished();
  24. newField.onValueDragStarted = t => ValueDragStarted();
  25. }
  26. }
  27. }
  28. public override bool indeterminate
  29. {
  30. get
  31. {
  32. return m_FloatFields[0, 0].indeterminate;
  33. }
  34. set
  35. {
  36. for (int i = 0; i < m_FloatFields.GetLength(0); ++i)
  37. {
  38. for (int j = 0; j < m_FloatFields.GetLength(1); ++j)
  39. {
  40. m_FloatFields[i, j].indeterminate = value;
  41. }
  42. }
  43. }
  44. }
  45. void ValueDragFinished()
  46. {
  47. if (onValueDragFinished != null)
  48. onValueDragFinished();
  49. }
  50. void ValueDragStarted()
  51. {
  52. if (onValueDragStarted != null)
  53. onValueDragStarted();
  54. }
  55. public Action onValueDragFinished;
  56. public Action onValueDragStarted;
  57. void OnFloatValueChanged(ChangeEvent<float> e)
  58. {
  59. Matrix4x4 newValue = value;
  60. int i = 0;
  61. int j = 0;
  62. bool found = false;
  63. for (; i < m_FloatFields.GetLength(0); ++i)
  64. {
  65. j = 0;
  66. for (; j < m_FloatFields.GetLength(1); ++j)
  67. {
  68. if (m_FloatFields[i, j] == e.target)
  69. {
  70. found = true;
  71. break;
  72. }
  73. }
  74. if (found)
  75. break;
  76. }
  77. if (i < m_FloatFields.GetLength(0) && j < m_FloatFields.GetLength(1))
  78. {
  79. newValue[i, j] = e.newValue;
  80. SetValueAndNotify(newValue);
  81. }
  82. }
  83. public override void SetEnabled(bool value)
  84. {
  85. for (int i = 0; i < m_FloatFields.GetLength(0); ++i)
  86. {
  87. for (int j = 0; j < m_FloatFields.GetLength(1); ++j)
  88. {
  89. m_FloatFields[i, j].SetEnabled(value);
  90. }
  91. }
  92. }
  93. public VFXMatrix4x4Field()
  94. {
  95. CreateTextField();
  96. style.flexDirection = FlexDirection.Column;
  97. for (int i = 0; i < m_FloatFields.GetLength(0); ++i)
  98. {
  99. var line = new VisualElement() { name = "matrixLine" };
  100. line.style.flexDirection = FlexDirection.Row;
  101. for (int j = 0; j < m_FloatFields.GetLength(1); ++j)
  102. {
  103. line.Add(m_FloatFields[i, j]);
  104. }
  105. Add(line);
  106. }
  107. }
  108. protected override void ValueToGUI(bool force)
  109. {
  110. Matrix4x4 value = this.value;
  111. for (int i = 0; i < m_FloatFields.GetLength(0); ++i)
  112. {
  113. for (int j = 0; j < m_FloatFields.GetLength(1); ++j)
  114. {
  115. if (!m_FloatFields[i, j].control.HasFocus() || force)
  116. {
  117. m_FloatFields[i, j].value = value[i, j];
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }