RiverAttributeTool.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 sc.modeling.river.runtime;
  6. using UnityEditor;
  7. using UnityEditor.EditorTools;
  8. using UnityEngine;
  9. #if SPLINES
  10. using UnityEngine.Splines;
  11. #endif
  12. namespace sc.modeling.river.editor
  13. {
  14. #if SPLINES && MATHEMATICS
  15. public abstract class RiverAttributeTool<DataType> : EditorTool
  16. {
  17. protected GUIContent m_IconContent;
  18. public override GUIContent toolbarIcon => m_IconContent;
  19. protected bool m_DisableHandles = false;
  20. protected const float k_MinSliderSize = 1.35f;
  21. protected const float k_HandleSize = 0.1f;
  22. protected const float SLIDER_WIDTH = 150f;
  23. private GUIStyle windowLabelStyle;
  24. static readonly Color headerBackgroundDark = new Color(0.1f, 0.1f, 0.1f, 0.9f);
  25. static readonly Color headerBackgroundLight = new Color(1f, 1f, 1f, 0.9f);
  26. public static Color headerBackground { get { return EditorGUIUtility.isProSkin ? headerBackgroundDark : headerBackgroundLight; } }
  27. public RiverModeler.ChangeFlags changeFlag = RiverModeler.ChangeFlags.All;
  28. public override void OnToolGUI(EditorWindow window)
  29. {
  30. Rect toolRect = new Rect(15, 15, 250, 300);
  31. if (windowLabelStyle == null)
  32. {
  33. windowLabelStyle = new GUIStyle(EditorStyles.label);
  34. windowLabelStyle.richText = true;
  35. }
  36. Handles.BeginGUI();
  37. GUILayout.BeginArea(toolRect);
  38. {
  39. GUILayout.BeginVertical(EditorStyles.textArea);
  40. GUILayout.Label("<b>Click+Drag</b> a node to move it", windowLabelStyle);
  41. GUILayout.Label("<b>Left-click</b> to add a node", windowLabelStyle);
  42. GUILayout.Label("<b>Right-click</b> to delete a node", windowLabelStyle);
  43. GUILayout.EndHorizontal();
  44. }
  45. GUILayout.EndArea();
  46. Handles.EndGUI();
  47. }
  48. public override void OnActivated()
  49. {
  50. RiverModeler modeler = target as RiverModeler;
  51. if (modeler == null || modeler.splineContainer == null)
  52. return;
  53. modeler.ValidateData(modeler.splineContainer);
  54. SceneView.lastActiveSceneView.ShowNotification(new GUIContent(name), .2f);
  55. }
  56. protected bool DrawDataPoints(ISpline spline, SplineData<DataType> splineData)
  57. {
  58. RiverModeler modeler = target as RiverModeler;
  59. var inUse = false;
  60. for (int dataFrameIndex = 0; dataFrameIndex < splineData.Count; dataFrameIndex++)
  61. {
  62. var dataPoint = splineData[dataFrameIndex];
  63. var normalizedT = SplineUtility.GetNormalizedInterpolation(spline, dataPoint.Index, splineData.PathIndexUnit);
  64. spline.Evaluate(normalizedT, out var position, out var tangent, out var up);
  65. if (DrawDataPoint(position, tangent, up, dataPoint.Value, out var result))
  66. {
  67. dataPoint.Value = result;
  68. splineData[dataFrameIndex] = dataPoint;
  69. inUse = true;
  70. modeler.Rebuild(changeFlag);
  71. }
  72. }
  73. return inUse;
  74. }
  75. protected abstract bool DrawDataPoint(
  76. Vector3 position,
  77. Vector3 tangent,
  78. Vector3 up,
  79. DataType inValue,
  80. out DataType outValue);
  81. }
  82. #endif
  83. }