From dc809858b9436e6f11e2920587ca2f84d43802c0 Mon Sep 17 00:00:00 2001 From: Simon Jackson Date: Fri, 5 May 2017 14:06:52 +0100 Subject: [PATCH] 1st Pass update, Updated controls: * DropDownList/ComboBox * Reorderable List (Screen Space Camera Updates * Curved text fixes --- Examples/TextEffects/TextEffects.unity | 14 +- .../Controls/ComboBox/AutoCompleteComboBox.cs | 17 +-- Scripts/Controls/ComboBox/ComboBox.cs | 18 ++- Scripts/Controls/ComboBox/DropDownList.cs | 2 - .../ReorderableList/ReorderableList.cs | 14 +- .../ReorderableList/ReorderableList.cs.meta | 24 +-- .../ReorderableList/ReorderableListContent.cs | 8 +- .../ReorderableList/ReorderableListElement.cs | 138 ++++++++++++++---- .../ReorderableListElement.cs.meta | 24 +-- Scripts/Effects/CurvedText.cs | 9 +- 10 files changed, 187 insertions(+), 81 deletions(-) diff --git a/Examples/TextEffects/TextEffects.unity b/Examples/TextEffects/TextEffects.unity index 191bae6..59237c2 100644 --- a/Examples/TextEffects/TextEffects.unity +++ b/Examples/TextEffects/TextEffects.unity @@ -37,7 +37,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 0} - m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1} + m_IndirectSpecularColor: {r: 0.44657868, g: 0.49641263, b: 0.57481706, a: 1} --- !u!157 &3 LightmapSettings: m_ObjectHideFlags: 0 @@ -756,9 +756,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 97a58789aa01843488ef44dc747ee8e8, type: 3} m_Name: m_EditorClassIdentifier: - useHalfCharWidth: 0 - halfCharWidth: 0 m_spacing: 94.2 + HalfCharWidth: 1 + UseHalfCharWidth: 0 --- !u!114 &755348327 MonoBehaviour: m_ObjectHideFlags: 0 @@ -985,7 +985,7 @@ RectTransform: m_PrefabParentObject: {fileID: 0} m_PrefabInternal: {fileID: 0} m_GameObject: {fileID: 1101119004} - m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: @@ -1009,7 +1009,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 0ec526a95e7733b4396be80d3e1df80e, type: 3} m_Name: m_EditorClassIdentifier: - radius: 121.96 + radius: 98.31 --- !u!114 &1101119007 MonoBehaviour: m_ObjectHideFlags: 0 @@ -1339,9 +1339,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 97a58789aa01843488ef44dc747ee8e8, type: 3} m_Name: m_EditorClassIdentifier: - useHalfCharWidth: 1 - halfCharWidth: 4.55 m_spacing: 71.12 + HalfCharWidth: 1 + UseHalfCharWidth: 0 --- !u!114 &1814753282 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Scripts/Controls/ComboBox/AutoCompleteComboBox.cs b/Scripts/Controls/ComboBox/AutoCompleteComboBox.cs index c24dc92..323e731 100644 --- a/Scripts/Controls/ComboBox/AutoCompleteComboBox.cs +++ b/Scripts/Controls/ComboBox/AutoCompleteComboBox.cs @@ -76,8 +76,9 @@ namespace UnityEngine.UI.Extensions public bool SelectFirstItemOnStart = false; [SerializeField] - private bool _ChangeInputTextColorBasedOnMatchingItems = false; - public bool ChangeInputTextColorBasedOnMatchingItems{ + [Tooltip("Change input text color based on matching items")] + private bool _ChangeInputTextColorBasedOnMatchingItems = false; + public bool InputColorMatching{ get { return _ChangeInputTextColorBasedOnMatchingItems; } set { @@ -99,8 +100,8 @@ namespace UnityEngine.UI.Extensions public class SelectionChangedEvent : UnityEngine.Events.UnityEvent { } - [System.Serializable] - public class SelectinTextChangedEvent : UnityEngine.Events.UnityEvent { + [System.Serializable] + public class SelectionTextChangedEvent : UnityEngine.Events.UnityEvent { } [System.Serializable] @@ -108,14 +109,12 @@ namespace UnityEngine.UI.Extensions } // fires when input text is changed; - public SelectinTextChangedEvent OnSelectinTextChanged; + public SelectionTextChangedEvent OnSelectionTextChanged; // fires when when an Item gets selected / deselected (including when items are added/removed once this is possible) public SelectionValidityChangedEvent OnSelectionValidityChanged; // fires in both cases public SelectionChangedEvent OnSelectionChanged; - - public void Awake() { Initialize(); @@ -354,7 +353,7 @@ namespace UnityEngine.UI.Extensions bool validity_changed = (_panelItems.Contains (Text) == _selectionIsValid); _selectionIsValid = _panelItems.Contains (Text); OnSelectionChanged.Invoke (Text, _selectionIsValid); - OnSelectinTextChanged.Invoke (Text); + OnSelectionTextChanged.Invoke (Text); if(validity_changed){ OnSelectionValidityChanged.Invoke (_selectionIsValid); } @@ -363,7 +362,7 @@ namespace UnityEngine.UI.Extensions } private void SetInputTextColor(){ - if (ChangeInputTextColorBasedOnMatchingItems) { + if (InputColorMatching) { if (_selectionIsValid) { _mainInput.textComponent.color = ValidSelectionTextColor; } else if (_panelItems.Count > 0) { diff --git a/Scripts/Controls/ComboBox/ComboBox.cs b/Scripts/Controls/ComboBox/ComboBox.cs index 0c80757..a597a25 100644 --- a/Scripts/Controls/ComboBox/ComboBox.cs +++ b/Scripts/Controls/ComboBox/ComboBox.cs @@ -15,7 +15,18 @@ namespace UnityEngine.UI.Extensions public List AvailableOptions; - public System.Action OnSelectionChanged; // fires when selection is changed; + [SerializeField] + private float _scrollBarWidth = 20.0f; + + [SerializeField] + private int _itemsToDisplay; + + [System.Serializable] + public class SelectionChangedEvent : UnityEngine.Events.UnityEvent + { + } + // fires when item is changed; + public SelectionChangedEvent OnSelectionChanged; //private bool isInitialized = false; private bool _isPanelActive = false; @@ -46,8 +57,6 @@ namespace UnityEngine.UI.Extensions public string Text { get; private set; } - [SerializeField] - private float _scrollBarWidth = 20.0f; public float ScrollBarWidth { get { return _scrollBarWidth; } @@ -61,8 +70,6 @@ namespace UnityEngine.UI.Extensions // private int scrollOffset; //offset of the selected item // private int _selectedIndex = 0; - [SerializeField] - private int _itemsToDisplay; public int ItemsToDisplay { get { return _itemsToDisplay; } @@ -294,6 +301,7 @@ namespace UnityEngine.UI.Extensions { ToggleDropdownPanel(false); } + OnSelectionChanged.Invoke(Text); } /// diff --git a/Scripts/Controls/ComboBox/DropDownList.cs b/Scripts/Controls/ComboBox/DropDownList.cs index 5c879a3..7f04146 100644 --- a/Scripts/Controls/ComboBox/DropDownList.cs +++ b/Scripts/Controls/ComboBox/DropDownList.cs @@ -79,8 +79,6 @@ namespace UnityEngine.UI.Extensions public SelectionChangedEvent OnSelectionChanged; - - public void Start() { Initialize(); diff --git a/Scripts/Controls/ReorderableList/ReorderableList.cs b/Scripts/Controls/ReorderableList/ReorderableList.cs index cb2ee35..1876bed 100644 --- a/Scripts/Controls/ReorderableList/ReorderableList.cs +++ b/Scripts/Controls/ReorderableList/ReorderableList.cs @@ -2,6 +2,8 @@ /// Sourced from - http://forum.unity3d.com/threads/free-reorderable-list.364600/ using System; +using System.Collections; +using System.Linq; using UnityEngine.Events; namespace UnityEngine.UI.Extensions @@ -22,12 +24,13 @@ namespace UnityEngine.UI.Extensions [Tooltip("Can new draggable items be dropped in to the container?")] public bool IsDropable = true; - + [Header("UI Re-orderable Events")] public ReorderableListHandler OnElementDropped = new ReorderableListHandler(); public ReorderableListHandler OnElementGrabbed = new ReorderableListHandler(); public ReorderableListHandler OnElementRemoved = new ReorderableListHandler(); + public ReorderableListHandler OnElementAdded = new ReorderableListHandler(); private RectTransform _content; private ReorderableListContent _listContent; @@ -83,9 +86,9 @@ namespace UnityEngine.UI.Extensions Debug.LogError("You need to have a Graphic control (such as an Image) for the list [" + name + "] to be droppable", gameObject); return; } - if (GetCanvas().renderMode != RenderMode.ScreenSpaceOverlay) + if (GetCanvas().renderMode > RenderMode.ScreenSpaceCamera) { - Debug.LogError("The ReOrderable List is only supported on a Screenspace-Overlay Canvas at the moment"); + Debug.LogError("The ReOrderable List is only supported on a Screen-Space Canvas at the moment"); } _listContent = ContentLayout.gameObject.AddComponent(); @@ -104,6 +107,11 @@ namespace UnityEngine.UI.Extensions public GameObject SourceObject; public int ToIndex; public ReorderableList ToList; + + public void Cancel() + { + SourceObject.GetComponent().isValid = false; + } } #endregion diff --git a/Scripts/Controls/ReorderableList/ReorderableList.cs.meta b/Scripts/Controls/ReorderableList/ReorderableList.cs.meta index 0c82a31..9b14d3c 100644 --- a/Scripts/Controls/ReorderableList/ReorderableList.cs.meta +++ b/Scripts/Controls/ReorderableList/ReorderableList.cs.meta @@ -1,12 +1,12 @@ -fileFormatVersion: 2 -guid: 6b333d67eb08d464d823874f6a1666c2 -timeCreated: 1446072130 -licenseType: Pro -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: +fileFormatVersion: 2 +guid: 6b333d67eb08d464d823874f6a1666c2 +timeCreated: 1492560112 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/Controls/ReorderableList/ReorderableListContent.cs b/Scripts/Controls/ReorderableList/ReorderableListContent.cs index 9d5b47c..37287f6 100644 --- a/Scripts/Controls/ReorderableList/ReorderableListContent.cs +++ b/Scripts/Controls/ReorderableList/ReorderableListContent.cs @@ -14,9 +14,15 @@ namespace UnityEngine.UI.Extensions private ReorderableList _extList; private RectTransform _rect; + private void OnEnable() + { + if(_rect)StartCoroutine(RefreshChildren()); + } + + public void OnTransformChildrenChanged() { - StartCoroutine(RefreshChildren()); + if(this.isActiveAndEnabled)StartCoroutine(RefreshChildren()); } public void Init(ReorderableList extList) diff --git a/Scripts/Controls/ReorderableList/ReorderableListElement.cs b/Scripts/Controls/ReorderableList/ReorderableListElement.cs index a0f7a21..3b34815 100644 --- a/Scripts/Controls/ReorderableList/ReorderableListElement.cs +++ b/Scripts/Controls/ReorderableList/ReorderableListElement.cs @@ -2,7 +2,9 @@ /// Sourced from - http://forum.unity3d.com/threads/free-reorderable-list.364600/ /// Last Child Fix - https://bitbucket.org/ddreaper/unity-ui-extensions/issues/70/all-re-orderable-lists-cause-a-transform +using System; using System.Collections.Generic; +using System.Linq; using UnityEngine.EventSystems; namespace UnityEngine.UI.Extensions @@ -11,6 +13,14 @@ namespace UnityEngine.UI.Extensions [RequireComponent(typeof(RectTransform))] public class ReorderableListElement : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler { + [Tooltip("Can this element be dragged?")] + public bool IsGrabbable = true; + [Tooltip("Can this element be transfered to another list")] + public bool IsTransferable = true; + [Tooltip("Can this element be dropped in space?")] + public bool isDroppableInSpace = false; + + private readonly List _raycastResults = new List(); private ReorderableList _currentReorderableListRaycasted; private RectTransform _draggingObject; @@ -22,16 +32,18 @@ namespace UnityEngine.UI.Extensions private bool _isDragging; private RectTransform _rect; private ReorderableList _reorderableList; + internal bool isValid; #region IBeginDragHandler Members public void OnBeginDrag(PointerEventData eventData) { + isValid = true; if (_reorderableList == null) return; //Can't drag, return... - if (!_reorderableList.IsDraggable) + if (!_reorderableList.IsDraggable || !this.IsGrabbable) { _draggingObject = null; return; @@ -54,6 +66,11 @@ namespace UnityEngine.UI.Extensions FromIndex = _fromIndex, }); } + if (isValid == false) + { + _draggingObject = null; + return; + } } //Else Duplicate else @@ -86,6 +103,12 @@ namespace UnityEngine.UI.Extensions FromList = _reorderableList, FromIndex = _fromIndex, }); + + if (!isValid) + { + CancelDrag(); + return; + } } _isDragging = true; @@ -99,10 +122,18 @@ namespace UnityEngine.UI.Extensions { if (!_isDragging) return; - + if (!isValid) + { + CancelDrag(); + return; + } //Set dragging object on cursor - _draggingObject.position = eventData.position; - + var canvas = _draggingObject.GetComponentInParent(); + Vector3 worldPoint; + RectTransformUtility.ScreenPointToWorldPointInRectangle(canvas.GetComponent(), eventData.position, + canvas.worldCamera, out worldPoint); + _draggingObject.position = worldPoint; + //Check everything under the cursor to find a ReorderableList EventSystem.current.RaycastAll(eventData, _raycastResults); for (int i = 0; i < _raycastResults.Count; i++) @@ -135,11 +166,11 @@ namespace UnityEngine.UI.Extensions var c = _currentReorderableListRaycasted.Content.GetChild(j).GetComponent(); if (_currentReorderableListRaycasted.ContentLayout is VerticalLayoutGroup) - dist = Mathf.Abs(c.position.y - eventData.position.y); + dist = Mathf.Abs(c.position.y - worldPoint.y); else if (_currentReorderableListRaycasted.ContentLayout is HorizontalLayoutGroup) - dist = Mathf.Abs(c.position.x - eventData.position.x); + dist = Mathf.Abs(c.position.x - worldPoint.x); else if (_currentReorderableListRaycasted.ContentLayout is GridLayoutGroup) - dist = (Mathf.Abs(c.position.x - eventData.position.x) + Mathf.Abs(c.position.y - eventData.position.y)); + dist = (Mathf.Abs(c.position.x - worldPoint.x) + Mathf.Abs(c.position.y - worldPoint.y)); if (dist < minDistance) { @@ -167,42 +198,56 @@ namespace UnityEngine.UI.Extensions { //If we have a, ReorderableList that is dropable //Put the dragged object into the content and at the right index - if (_currentReorderableListRaycasted != null && _currentReorderableListRaycasted.IsDropable) + if (_currentReorderableListRaycasted != null && _currentReorderableListRaycasted.IsDropable + && (IsTransferable || _currentReorderableListRaycasted == _reorderableList )) { + var args = new ReorderableList.ReorderableListEventStruct + { + DroppedObject = _draggingObject.gameObject, + IsAClone = _reorderableList.CloneDraggedObject, + SourceObject = _reorderableList.CloneDraggedObject ? gameObject : _draggingObject.gameObject, + FromList = _reorderableList, + FromIndex = _fromIndex, + ToList = _currentReorderableListRaycasted, + ToIndex = _fakeElement.GetSiblingIndex() + }; + //Send OnelementDropped Event + if (_reorderableList && _reorderableList.OnElementDropped != null) + { + _reorderableList.OnElementDropped.Invoke(args); + } + if (!isValid) + { + CancelDrag(); + return; + } RefreshSizes(); _draggingObject.SetParent(_currentReorderableListRaycasted.Content, false); _draggingObject.SetSiblingIndex(_fakeElement.GetSiblingIndex()); + _reorderableList.OnElementAdded.Invoke(args); - //Send OnelementDropped Event - if (_reorderableList.OnElementDropped != null) + if(!isValid) throw new Exception("It's too late to cancel the Transfer! Do so in OnElementDropped!"); + + } + //We don't have an ReorderableList + else + { + if (this.isDroppableInSpace) { _reorderableList.OnElementDropped.Invoke(new ReorderableList.ReorderableListEventStruct { DroppedObject = _draggingObject.gameObject, IsAClone = _reorderableList.CloneDraggedObject, - SourceObject = _reorderableList.CloneDraggedObject ? gameObject : _draggingObject.gameObject, + SourceObject = + _reorderableList.CloneDraggedObject ? gameObject : _draggingObject.gameObject, FromList = _reorderableList, - FromIndex = _fromIndex, - ToList = _currentReorderableListRaycasted, - ToIndex = _fakeElement.GetSiblingIndex() - 1 + FromIndex = _fromIndex }); } - } - //We don't have an ReorderableList - else - { - //If it's a clone, delete it - if (_reorderableList.CloneDraggedObject) - { - Destroy(_draggingObject.gameObject); - } - //Else replace the draggedObject to his first place else { - RefreshSizes(); - _draggingObject.SetParent(_reorderableList.Content, false); - _draggingObject.SetSiblingIndex(_fromIndex); + CancelDrag(); } } } @@ -214,6 +259,45 @@ namespace UnityEngine.UI.Extensions #endregion + void CancelDrag() + { + _isDragging = false; + //If it's a clone, delete it + if (_reorderableList.CloneDraggedObject) + { + Destroy(_draggingObject.gameObject); + } + //Else replace the draggedObject to his first place + else + { + RefreshSizes(); + _draggingObject.SetParent(_reorderableList.Content, false); + _draggingObject.SetSiblingIndex(_fromIndex); + + + var args = new ReorderableList.ReorderableListEventStruct + { + DroppedObject = _draggingObject.gameObject, + IsAClone = _reorderableList.CloneDraggedObject, + SourceObject = _reorderableList.CloneDraggedObject ? gameObject : _draggingObject.gameObject, + FromList = _reorderableList, + FromIndex = _fromIndex, + ToList = _reorderableList, + ToIndex = _fromIndex + }; + + + _reorderableList.OnElementAdded.Invoke(args); + + if (!isValid) throw new Exception("Transfer is already Cancelled."); + + } + + //Delete fake element + if (_fakeElement != null) + Destroy(_fakeElement.gameObject); + } + private void RefreshSizes() { Vector2 size = _draggingObjectOriginalSize; diff --git a/Scripts/Controls/ReorderableList/ReorderableListElement.cs.meta b/Scripts/Controls/ReorderableList/ReorderableListElement.cs.meta index cd587fb..e14a747 100644 --- a/Scripts/Controls/ReorderableList/ReorderableListElement.cs.meta +++ b/Scripts/Controls/ReorderableList/ReorderableListElement.cs.meta @@ -1,12 +1,12 @@ -fileFormatVersion: 2 -guid: 916e98f1b982a9a4082fcc45c87b66c5 -timeCreated: 1446072130 -licenseType: Pro -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: +fileFormatVersion: 2 +guid: 916e98f1b982a9a4082fcc45c87b66c5 +timeCreated: 1492560112 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/Effects/CurvedText.cs b/Scripts/Effects/CurvedText.cs index 244e99b..07a1bb1 100644 --- a/Scripts/Effects/CurvedText.cs +++ b/Scripts/Effects/CurvedText.cs @@ -57,9 +57,12 @@ namespace UnityEngine.UI.Extensions } protected override void OnRectTransformDimensionsChange() { - var tmpRect = curveForText[curveForText.length - 1]; - tmpRect.time = rectTrans.rect.width; - curveForText.MoveKey(curveForText.length - 1, tmpRect); + if (rectTrans) + { + Keyframe tmpRect = curveForText[curveForText.length - 1]; + tmpRect.time = rectTrans.rect.width; + curveForText.MoveKey(curveForText.length - 1, tmpRect); + } } } }