123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- //### Curve Drawing Helper
- //Provides
- //
- //License information: [ASSET STORE TERMS OF SERVICE AND EULA](https://unity3d.com/legal/as_terms)
- //
- //Developed by [MathArtCode](https://www.assetstore.unity3d.com/en/#!/search/page=1/sortby=popularity/query=publisher:8738) team, 2015
- using Assets.Scripts.BezierCurvedParticlesFlow.Interfaces;
- using UnityEditor;
- using UnityEngine;
- namespace Assets.Editor.BezierCurvedParticlesFlow.Utilities {
- public class CurveDrawingHelper {
- //### Provides events for handling interactions
- public delegate void ChangeCommit();
- public delegate void ChangeChangeStart( string label );
- public event ChangeChangeStart OnChangeInitiated;
- public event ChangeCommit OnChangeCommit;
- //### UI style settings
- private const float HandleSize = 0.04f;
- private const float PickSize = 0.06f;
- //### Main actors
- private readonly IBezierLogic _bezierLogic;
- private readonly UnityEditor.Editor _client;
- private readonly GameObject _splineControllerHost;
- //### Currently selected handle
- private Quaternion _handleRotation;
- private Transform _handleTransform;
- public int SelectedIndex = -1;
- //### Functions
- //Draws a dinamically movable UnityEditor point
- private Vector3 ShowPoint( int index ) {
- var point = _handleTransform.TransformPoint( _bezierLogic.GetControlPoint( index ) );
- var size = HandleUtility.GetHandleSize( point );
- if ( index == 0 ) {
- size *= 2f;
- }
- Handles.color = Color.cyan;
- if ( Handles.Button( point, _handleRotation, size * HandleSize, size * PickSize, Handles.DotHandleCap ) ) {
- SelectedIndex = index;
- _client.Repaint();
- }
- if ( SelectedIndex == index ) {
- EditorGUI.BeginChangeCheck();
- point = Handles.DoPositionHandle( point, _handleRotation );
- if ( EditorGUI.EndChangeCheck() ) {
- if ( OnChangeInitiated != null ) {
- OnChangeInitiated( "Move Point" );
- }
- _bezierLogic.SetControlPoint( index, _handleTransform.InverseTransformPoint( point ) );
- if ( OnChangeCommit != null ) {
- OnChangeCommit();
- }
- }
- }
- return point;
- }
- // Draws a bezier curve in Scene space
- // ReSharper disable once InconsistentNaming
- public void OnSceneGUI() {
- if (_splineControllerHost == null) {
- Debug.LogError("_spline != null") ;
- return;
- }
- _handleTransform = _splineControllerHost.transform;
- _handleRotation = Tools.pivotRotation == PivotRotation.Local
- ? _handleTransform.rotation
- : Quaternion.identity;
- var p0 = ShowPoint( 0 );
- for ( var i = 1; i < _bezierLogic.ControlPointCount; i += 3 ) {
- var p1 = ShowPoint( i );
- var p2 = ShowPoint( i + 1 );
- var p3 = ShowPoint( i + 2 );
- Handles.color = Color.gray;
- Handles.DrawLine( p0, p1 );
- Handles.DrawLine( p2, p3 );
- Handles.color = Color.yellow;
- Handles.ArrowHandleCap( 0, p0, Quaternion.LookRotation( p3 - p0 ), 0.25f ,EventType.MouseDown);
- #if !UNITY_3_5
- Handles.DrawBezier( p0, p3, p1, p2, Color.white, null, 1f );
- #endif
- p0 = p3;
- }
- #if UNITY_3_5
- var rasterize = 250;
- Handles.color = Color.gray;
- for (var i = 0; i < rasterize-1; i++) {
- var pn = _handleTransform.TransformPoint( _bezierLogic.GetPoint( i / (float)rasterize ));
- var pk = _handleTransform.TransformPoint( _bezierLogic.GetPoint( ( i + 1 ) / (float)rasterize ));
- Handles.DrawLine( pn, pk );
- }
- #endif
- var e = Event.current;
- switch ( e.type ) {
- case EventType.KeyDown: {
- if ( Event.current.keyCode == ( KeyCode.Delete ) || Event.current.keyCode == ( KeyCode.Backspace ) ) {
- if ( SelectedIndex % 3 == 0 && SelectedIndex > 3 && SelectedIndex < _bezierLogic.ControlPointCount ) {
- e.Use();
- if ( OnChangeInitiated != null ) {
- OnChangeInitiated( "Remove Point" );
- }
- _bezierLogic.RemovePoint( SelectedIndex );
- if ( OnChangeCommit != null ) {
- OnChangeCommit();
- }
- }
- }
- break;
- }
- }
- }
- public CurveDrawingHelper( UnityEditor.Editor uiClient, GameObject splineControllerHost, IBezierLogic splineSource ) {
- _client = uiClient;
- _splineControllerHost = splineControllerHost;
- _bezierLogic = splineSource;
- }
- }
- }
|