FoamTool.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // River Modeler
  2. // Staggart Creations (http://staggart.xyz)
  3. // Copyright protected under Unity Asset Store EULA
  4. // Copying or referencing source code for the production of new asset store content is strictly prohibited.
  5. using UnityEngine;
  6. using UnityEditor;
  7. using sc.modeling.river.runtime;
  8. using UnityEditor.EditorTools;
  9. #if SPLINES
  10. using UnityEditor.Splines;
  11. using UnityEngine.Splines;
  12. #endif
  13. #if MATHEMATICS
  14. using Unity.Mathematics;
  15. #endif
  16. namespace sc.modeling.river.editor
  17. {
  18. #if SPLINES && MATHEMATICS
  19. [EditorTool(NAME, typeof(RiverModeler))]
  20. public class FoamTool : RiverAttributeTool<float>, IDrawSelectedHandles
  21. {
  22. private const string NAME = "River Foam Tool";
  23. public const string IconName = "river-foam-tool-64px";
  24. void OnEnable()
  25. {
  26. name = NAME;
  27. m_IconContent = new GUIContent()
  28. {
  29. image = Resources.Load<Texture2D>(IconName),
  30. text = "Foam Tool",
  31. tooltip = "River: +/- foam amount"
  32. };
  33. changeFlag = RiverModeler.ChangeFlags.Foam | RiverModeler.ChangeFlags.Data;
  34. }
  35. public override void OnToolGUI(EditorWindow window)
  36. {
  37. RiverModeler modeler = target as RiverModeler;
  38. if (modeler == null || modeler.splineContainer == null)
  39. return;
  40. base.OnToolGUI(window);
  41. Handles.color = Color.gray;
  42. m_DisableHandles = false;
  43. var splines = modeler.splineContainer.Splines;
  44. for (var i = 0; i < splines.Count; i++)
  45. {
  46. if (i < modeler.FoamData.Count)
  47. {
  48. var nativeSpline = new NativeSpline(splines[i], modeler.splineContainer.transform.localToWorldMatrix);
  49. Undo.RecordObject(modeler, "Modifying River Foam");
  50. // User defined handles to manipulate width
  51. DrawDataPoints(nativeSpline, modeler.FoamData[i]);
  52. nativeSpline.DataPointHandles(modeler.FoamData[i], true, i);
  53. if (GUI.changed)
  54. {
  55. modeler.Rebuild(changeFlag);
  56. }
  57. }
  58. }
  59. }
  60. public void OnDrawHandles()
  61. {
  62. if (Asset.Preferences.ShowToolPoints == false) return;
  63. RiverModeler modeler = target as RiverModeler;
  64. if (ToolManager.IsActiveTool(this) || modeler.splineContainer == null)
  65. return;
  66. Color color = Color.gray;
  67. color.a = 0.5f;
  68. Handles.color = color;
  69. m_DisableHandles = true;
  70. var splines = modeler.splineContainer.Splines;
  71. for (var i = 0; i < splines.Count; i++)
  72. {
  73. if (i < modeler.FoamData.Count)
  74. {
  75. var nativeSpline = new NativeSpline(splines[i], modeler.splineContainer.transform.localToWorldMatrix);
  76. if (DrawDataPoints(nativeSpline, modeler.FoamData[i]))
  77. {
  78. }
  79. }
  80. }
  81. }
  82. protected override bool DrawDataPoint(Vector3 position, Vector3 tangent, Vector3 up, float inValue, out float outValue)
  83. {
  84. int id = m_DisableHandles ? -1 : GUIUtility.GetControlID(FocusType.Passive);
  85. outValue = -1f;
  86. if (tangent == Vector3.zero) return false;
  87. if (Event.current.type == EventType.MouseUp && Event.current.button != 0 && (GUIUtility.hotControl == id))
  88. {
  89. Event.current.Use();
  90. return false;
  91. }
  92. var handleColor = Handles.color;
  93. if (GUIUtility.hotControl == id)
  94. handleColor = Handles.selectedColor;
  95. else if (GUIUtility.hotControl == 0 && (HandleUtility.nearestControl == id))
  96. handleColor = Handles.preselectionColor;
  97. var right = math.normalize(math.cross(tangent, up));
  98. EditorGUI.BeginChangeCheck();
  99. //if (GUIUtility.hotControl == id)
  100. {
  101. using (new Handles.DrawingScope(handleColor))
  102. {
  103. //Handles.ScaleValueHandle(inValue, position, Quaternion.LookRotation(tangent, up), HandleUtility.GetHandleSize(position) * 10f, Handles.ArrowHandleCap, 0.01f);
  104. Handles.BeginGUI();
  105. Vector2 screenPos = HandleUtility.WorldToGUIPoint(position);
  106. Rect sliderRect = new Rect(screenPos.x - (SLIDER_WIDTH * 0.5f), screenPos.y - 50f, SLIDER_WIDTH, 22f);
  107. EditorGUI.DrawRect(sliderRect, headerBackground);
  108. outValue = GUI.HorizontalSlider(sliderRect, inValue, 0f, 1f);
  109. outValue = math.clamp(outValue, 0f, 1f);
  110. Handles.EndGUI();
  111. }
  112. }
  113. if (inValue != outValue)
  114. {
  115. //return true;
  116. }
  117. if (EditorGUI.EndChangeCheck()) return true;
  118. return false;
  119. }
  120. }
  121. #endif
  122. }