Updated Stepper and SegmentedControl scripts to UI Extensions standards.

Adding documentation next
pull/413/head
Simon Jackson 2017-07-25 19:54:07 +01:00
parent a2bb233141
commit 8e443b98c7
2 changed files with 99 additions and 76 deletions

View File

@ -1,9 +1,9 @@
/// Credit David Gileadi
/// Sourced from - https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/pull-requests/12
using System; using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Events; using UnityEngine.Events;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
using UnityEngine.Serialization;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {
@ -12,49 +12,61 @@ namespace UnityEngine.UI.Extensions
[RequireComponent(typeof(RectTransform))] [RequireComponent(typeof(RectTransform))]
public class SegmentedControl : UIBehaviour public class SegmentedControl : UIBehaviour
{ {
private Button[] m_segments;
[SerializeField]
[Tooltip("A GameObject with an Image to use as a separator between segments. Size of the RectTransform will determine the size of the separator used.\nNote, make sure to disable the separator GO so that it does not affect the scene")]
private Graphic m_separator;
private float m_separatorWidth = 0;
[SerializeField]
[Tooltip("When True, it allows each button to be toggled on/off")]
private bool m_allowSwitchingOff = false;
[SerializeField]
[Tooltip("The selected default for the control (zero indexed array)")]
private int m_selectedSegmentIndex = -1;
// Event delegates triggered on click.
[SerializeField]
[Tooltip("Event to fire once the selection has been changed")]
private SegmentSelectedEvent m_onValueChanged = new SegmentSelectedEvent();
protected internal Button selectedSegment;
protected float SeparatorWidth
{
get
{
if (m_separatorWidth == 0 && separator)
{
m_separatorWidth = separator.rectTransform.rect.width;
var image = separator.GetComponent<Image>();
if (image)
m_separatorWidth /= image.pixelsPerUnit;
}
return m_separatorWidth;
}
}
[Serializable]
public class SegmentSelectedEvent : UnityEvent<int> { }
public Button[] segments public Button[] segments
{ {
get get
{ {
if (_segments == null || _segments.Length == 0) if (m_segments == null || m_segments.Length == 0)
{ {
_segments = GetChildSegments(); m_segments = GetChildSegments();
} }
return _segments; return m_segments;
} }
} }
private Button[] _segments;
[SerializeField] [SerializeField]
public Color selectedColor; public Color selectedColor;
[SerializeField] public Graphic separator { get { return m_separator; } set { m_separator = value; m_separatorWidth = 0; LayoutSegments(); } }
private Graphic _separator;
public Graphic separator { get { return _separator; } set { _separator = value; _separatorWidth = 0; LayoutSegments(); } }
private float _separatorWidth = 0; public bool allowSwitchingOff { get { return m_allowSwitchingOff; } set { m_allowSwitchingOff = value; } }
private float separatorWidth
{
get
{
if (_separatorWidth == 0 && separator)
{
_separatorWidth = separator.rectTransform.rect.width;
var image = separator.GetComponent<Image>();
if (image)
_separatorWidth /= image.pixelsPerUnit;
}
return _separatorWidth;
}
}
[SerializeField] private bool _allowSwitchingOff = false;
public bool allowSwitchingOff { get { return _allowSwitchingOff; } set { _allowSwitchingOff = value; } }
protected internal Button selectedSegment;
[SerializeField]
private int _selectedSegmentIndex = -1;
public int selectedSegmentIndex public int selectedSegmentIndex
{ {
get { return Array.IndexOf(segments, selectedSegment); } get { return Array.IndexOf(segments, selectedSegment); }
@ -62,7 +74,7 @@ namespace UnityEngine.UI.Extensions
{ {
value = Math.Max(value, -1); value = Math.Max(value, -1);
value = Math.Min(value, segments.Length - 1); value = Math.Min(value, segments.Length - 1);
_selectedSegmentIndex = value; m_selectedSegmentIndex = value;
if (value == -1) if (value == -1)
{ {
if (selectedSegment) if (selectedSegment)
@ -81,17 +93,10 @@ namespace UnityEngine.UI.Extensions
} }
} }
[Serializable]
public class SegmentSelectedEvent : UnityEvent<int> { }
// Event delegates triggered on click.
[SerializeField]
private SegmentSelectedEvent _onValueChanged = new SegmentSelectedEvent();
public SegmentSelectedEvent onValueChanged public SegmentSelectedEvent onValueChanged
{ {
get { return _onValueChanged; } get { return m_onValueChanged; }
set { _onValueChanged = value; } set { m_onValueChanged = value; }
} }
protected SegmentedControl() protected SegmentedControl()
@ -103,8 +108,8 @@ namespace UnityEngine.UI.Extensions
LayoutSegments(); LayoutSegments();
if (_selectedSegmentIndex != -1) if (m_selectedSegmentIndex != -1)
selectedSegmentIndex = _selectedSegmentIndex; selectedSegmentIndex = m_selectedSegmentIndex;
} }
#if UNITY_EDITOR #if UNITY_EDITOR
@ -115,8 +120,18 @@ namespace UnityEngine.UI.Extensions
if (separator) if (separator)
LayoutSegments(); LayoutSegments();
if (_selectedSegmentIndex != -1) if (m_selectedSegmentIndex != -1)
selectedSegmentIndex = _selectedSegmentIndex; selectedSegmentIndex = m_selectedSegmentIndex;
if (m_selectedSegmentIndex > transform.childCount)
{
selectedSegmentIndex = transform.childCount - 1;
}
if (selectedColor == new Color(0, 0, 0, 0))
{
selectedColor = new Color(0f, 0.455f, 0.894f);
}
} }
#endif #endif
@ -180,11 +195,11 @@ namespace UnityEngine.UI.Extensions
RecreateSprites(); RecreateSprites();
RectTransform transform = this.transform as RectTransform; RectTransform transform = this.transform as RectTransform;
float width = (transform.rect.width / segments.Length) - (separatorWidth * (segments.Length - 1)); float width = (transform.rect.width / segments.Length) - (SeparatorWidth * (segments.Length - 1));
for (int i = 0; i < segments.Length; i++) for (int i = 0; i < segments.Length; i++)
{ {
float insetX = ((width + separatorWidth) * i); float insetX = ((width + SeparatorWidth) * i);
var rectTransform = segments[i].GetComponent<RectTransform>(); var rectTransform = segments[i].GetComponent<RectTransform>();
rectTransform.anchorMin = Vector2.zero; rectTransform.anchorMin = Vector2.zero;
@ -201,7 +216,7 @@ namespace UnityEngine.UI.Extensions
sep.rectTransform.SetParent(this.transform, false); sep.rectTransform.SetParent(this.transform, false);
sep.rectTransform.anchorMin = Vector2.zero; sep.rectTransform.anchorMin = Vector2.zero;
sep.rectTransform.anchorMax = Vector2.zero; sep.rectTransform.anchorMax = Vector2.zero;
sep.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, insetX - separatorWidth, separatorWidth); sep.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, insetX - SeparatorWidth, SeparatorWidth);
sep.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, transform.rect.height); sep.rectTransform.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, transform.rect.height);
} }
// TODO: maybe adjust text position // TODO: maybe adjust text position

View File

@ -12,44 +12,27 @@ namespace UnityEngine.UI.Extensions
[RequireComponent(typeof(RectTransform))] [RequireComponent(typeof(RectTransform))]
public class Stepper : UIBehaviour public class Stepper : UIBehaviour
{ {
public Button[] sides
{
get
{
if (_sides == null || _sides.Length == 0)
{
_sides = GetSides();
}
return _sides;
}
}
private Button[] _sides; private Button[] _sides;
[SerializeField] [SerializeField]
[Tooltip("The current step value of the control")]
private int _value = 0; private int _value = 0;
public int value { get { return _value; } set { _value = value; } }
[SerializeField] [SerializeField]
[Tooltip("The minimum step value allowed by the control. When reached it will disable the '-' button")]
private int _minimum = 0; private int _minimum = 0;
public int minimum { get { return _minimum; } set { _minimum = value; } }
[SerializeField] [SerializeField]
[Tooltip("The maximum step value allowed by the control. When reached it will disable the '+' button")]
private int _maximum = 100; private int _maximum = 100;
public int maximum { get { return _maximum; } set { _maximum = value; } }
[SerializeField] [SerializeField]
[Tooltip("The step increment used to increment / decrement the step value")]
private int _step = 1; private int _step = 1;
public int step { get { return _step; } set { _step = value; } }
[SerializeField] [SerializeField]
[Tooltip("Does the step value loop around from end to end")]
private bool _wrap = false; private bool _wrap = false;
public bool wrap { get { return _wrap; } set { _wrap = value; } }
[SerializeField] [SerializeField]
[Tooltip("A GameObject with an Image to use as a separator between segments. Size of the RectTransform will determine the size of the separator used.\nNote, make sure to disable the separator GO so that it does not affect the scene")]
private Graphic _separator; private Graphic _separator;
public Graphic separator { get { return _separator; } set { _separator = value; _separatorWidth = 0; LayoutSides(sides); } }
private float _separatorWidth = 0; private float _separatorWidth = 0;
private float separatorWidth private float separatorWidth
{ {
get get
@ -65,12 +48,37 @@ namespace UnityEngine.UI.Extensions
} }
} }
[Serializable]
public class StepperValueChangedEvent : UnityEvent<int> { }
// Event delegates triggered on click. // Event delegates triggered on click.
[SerializeField] [SerializeField]
private StepperValueChangedEvent _onValueChanged = new StepperValueChangedEvent(); private StepperValueChangedEvent _onValueChanged = new StepperValueChangedEvent();
[Serializable]
public class StepperValueChangedEvent : UnityEvent<int> { }
public Button[] sides
{
get
{
if (_sides == null || _sides.Length == 0)
{
_sides = GetSides();
}
return _sides;
}
}
public int value { get { return _value; } set { _value = value; } }
public int minimum { get { return _minimum; } set { _minimum = value; } }
public int maximum { get { return _maximum; } set { _maximum = value; } }
public int step { get { return _step; } set { _step = value; } }
public bool wrap { get { return _wrap; } set { _wrap = value; } }
public Graphic separator { get { return _separator; } set { _separator = value; _separatorWidth = 0; LayoutSides(sides); } }
public StepperValueChangedEvent onValueChanged public StepperValueChangedEvent onValueChanged
{ {
get { return _onValueChanged; } get { return _onValueChanged; }