Advertisement
PeterSvP

iTweenPath - better usability

Sep 29th, 2016
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.51 KB | None | 0 0
  1. // Copyright (c) 2010 Bob Berkebile
  2. //
  3. // Modified by ~PeterSvP
  4. //  Made initial points at the transform position and close to it,
  5. //  added a [+Add] button - adds a point nesr the last one,
  6. //  added Drop field where you can drop scene object to add its transform as a point,
  7. //  added a [M] button that adds point in between its row and the next
  8. //  added DELETE buttons for each point to be able to delete it
  9. //
  10. // Please direct any bugs/comments/suggestions to http://www.pixelplacement.com
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining a copy
  13. // of this software and associated documentation files (the "Software"), to deal
  14. // in the Software without restriction, including without limitation the rights
  15. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. // copies of the Software, and to permit persons to whom the Software is
  17. // furnished to do so, subject to the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be included in
  20. // all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. // THE SOFTWARE.
  29.  
  30. using UnityEngine;
  31. using UnityEditor;
  32. using System.Collections;
  33. using System.Linq;
  34.  
  35. [CustomEditor(typeof(iTweenPath))]
  36. public class iTweenPathEditor : Editor
  37. {
  38.     iTweenPath _target;
  39.     GUIStyle style = new GUIStyle();
  40.     public static int count = 0;
  41.     static GameObject drop;
  42.     static string dropName = "\n\nDrop object to add its pos\n\n";    // the (Transform) is out of view and it's centered :D
  43.  
  44.     void OnEnable()
  45.     {
  46.         //i like bold handle labels since I'm getting old:
  47.         style.fontStyle = FontStyle.Bold;
  48.         style.normal.textColor = Color.white;
  49.         _target = (iTweenPath)target;
  50.  
  51.         //lock in a default path name:
  52.         if (!_target.initialized)
  53.         {
  54.             _target.initialized = true;
  55.             _target.pathName = "New Path " + ++count;
  56.             _target.initialName = _target.pathName;
  57.         }
  58.     }
  59.  
  60.     public override void OnInspectorGUI()
  61.     {
  62.         //draw the path?
  63.         EditorGUILayout.BeginHorizontal();
  64.         EditorGUILayout.PrefixLabel("Path Visible");
  65.         _target.pathVisible = EditorGUILayout.Toggle(_target.pathVisible);
  66.         EditorGUILayout.EndHorizontal();
  67.  
  68.         //path name:
  69.         EditorGUILayout.BeginHorizontal();
  70.         EditorGUILayout.PrefixLabel("Path Name");
  71.         _target.pathName = EditorGUILayout.TextField(_target.pathName);
  72.         EditorGUILayout.EndHorizontal();
  73.  
  74.         if (_target.pathName == "")
  75.         {
  76.             _target.pathName = _target.initialName;
  77.         }
  78.  
  79.         //path color:
  80.         EditorGUILayout.BeginHorizontal();
  81.         EditorGUILayout.PrefixLabel("Path Color");
  82.         _target.pathColor = EditorGUILayout.ColorField(_target.pathColor);
  83.         EditorGUILayout.EndHorizontal();
  84.  
  85.         //exploration segment count control:
  86.         EditorGUILayout.BeginHorizontal();
  87.         //EditorGUILayout.PrefixLabel("Node Count");
  88.         _target.nodeCount = Mathf.Max(2, EditorGUILayout.IntField("Node Count", _target.nodeCount));
  89.         //_target.nodeCount =  Mathf.Clamp(EditorGUILayout.IntSlider(_target.nodeCount, 0, 10), 2,100);
  90.         EditorGUILayout.EndHorizontal();
  91.  
  92.         //add node?
  93.         if (_target.nodeCount > _target.nodes.Count)
  94.         {
  95.             // if noone, add small tween from current to +1 forward instead of (0,0,0)
  96.             if (_target.nodes.Count == 0)
  97.             {
  98.                 _target.nodes.Add(_target.transform.position);
  99.                 _target.nodes.Add(_target.transform.position + Vector3.forward);
  100.             }
  101.             // Else add close to last
  102.             else for (int i = 0; i < _target.nodeCount - _target.nodes.Count; i++)
  103.                 {
  104.                     int c = _target.nodes.Count;
  105.                     _target.nodes.Add(Vector3.LerpUnclamped(_target.nodes[c - 2], _target.nodes[c - 1], 1.5f));
  106.                 }
  107.         }
  108.  
  109.         //remove node?
  110.         if (_target.nodeCount < _target.nodes.Count)
  111.         {
  112.             if (EditorUtility.DisplayDialog("Remove path node?", "Shortening the node list will permantently destory parts of your path. This operation cannot be undone.", "OK", "Cancel"))
  113.             {
  114.                 int removeCount = _target.nodes.Count - _target.nodeCount;
  115.                 _target.nodes.RemoveRange(_target.nodes.Count - removeCount, removeCount);
  116.             }
  117.             else {
  118.                 _target.nodeCount = _target.nodes.Count;
  119.             }
  120.         }
  121.  
  122.         //node display:
  123.         bool disableDelete = _target.nodeCount <= 2;
  124.         for (int i = 0; i < _target.nodes.Count; i++)
  125.         {
  126.             EditorGUILayout.BeginHorizontal();
  127.             GUILayout.Label((i + 1) + ":", GUILayout.MaxWidth(20));
  128.             _target.nodes[i] = EditorGUILayout.Vector3Field("", _target.nodes[i]);
  129.  
  130.             GUI.enabled = i < _target.nodes.Count - 1;
  131.             if (GUILayout.Button("M", GUILayout.MaxWidth(20)))
  132.             {
  133.                 Vector3 mid = (_target.nodes[i] + _target.nodes[i + 1]) / 2;
  134.                 _target.nodes.Insert(i+1, mid); _target.nodeCount++; return;
  135.             }
  136.             GUI.enabled = !disableDelete;
  137.             if (GUILayout.Button("X", GUILayout.MaxWidth(20))) { _target.nodes.RemoveAt(i); _target.nodeCount--; return; }
  138.             GUI.enabled = true;
  139.             EditorGUILayout.EndHorizontal();
  140.         }
  141.  
  142.         // Utility buttons feature - add new close to last or add from scene object
  143.         EditorGUILayout.BeginHorizontal();
  144.         if (GUILayout.Button("+ Add")) _target.nodeCount++;
  145.  
  146.         // Hack to dislay custom text into the ObjectField
  147.         if (drop == null)
  148.         {
  149.             drop = new GameObject();
  150.             drop.hideFlags = HideFlags.HideAndDontSave;
  151.             drop.name = dropName;
  152.         }
  153.  
  154.         // If something was selected, add its position
  155.         Transform changed = EditorGUILayout.ObjectField(drop, typeof(Transform), true) as Transform;
  156.         if (changed != null)
  157.         {
  158.             _target.nodeCount++;
  159.             _target.nodes.Add(changed.position);
  160.         }
  161.         EditorGUILayout.EndHorizontal();
  162.  
  163.         // Update and redraw:
  164.         if (GUI.changed)
  165.         {
  166.             EditorUtility.SetDirty(_target);
  167.         }
  168.     }
  169.  
  170.     void OnSceneGUI()
  171.     {
  172.         if (_target.pathVisible)
  173.         {
  174.             if (_target.nodes.Count > 0)
  175.             {
  176.                 //allow path adjustment undo:
  177.                 Undo.SetSnapshotTarget(_target, "Adjust iTween Path");
  178.  
  179.                 //path begin and end labels:
  180.                 Handles.Label(_target.nodes[0], "'" + _target.pathName + "' Begin", style);
  181.                 Handles.Label(_target.nodes[_target.nodes.Count - 1], "'" + _target.pathName + "' End", style);
  182.  
  183.                 //node handle display:
  184.                 for (int i = 0; i < _target.nodes.Count; i++)
  185.                 {
  186.                     _target.nodes[i] = Handles.PositionHandle(_target.nodes[i], Quaternion.identity);
  187.                 }
  188.             }
  189.         }
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement