Change Stepper and Segmented Control to use overrideImage, standard size, other fixes
parent
1f16a65f0f
commit
c5edc0cdf1
|
@ -1656,7 +1656,7 @@ namespace UnityEditor.UI
|
||||||
[MenuItem("GameObject/UI/Extensions/Segmented Control", false)]
|
[MenuItem("GameObject/UI/Extensions/Segmented Control", false)]
|
||||||
static public void AddSegmentedControl(MenuCommand menuCommand)
|
static public void AddSegmentedControl(MenuCommand menuCommand)
|
||||||
{
|
{
|
||||||
GameObject go = CreateUIElementRoot("Segmented Control", menuCommand, s_ThinGUIElementSize);
|
GameObject go = CreateUIElementRoot("Segmented Control", menuCommand, s_ThickGUIElementSize);
|
||||||
SegmentedControl control = go.AddComponent<SegmentedControl>();
|
SegmentedControl control = go.AddComponent<SegmentedControl>();
|
||||||
|
|
||||||
Color selectedColor = new Color(0f, 0.455f, 0.894f);
|
Color selectedColor = new Color(0f, 0.455f, 0.894f);
|
||||||
|
@ -1677,12 +1677,12 @@ namespace UnityEditor.UI
|
||||||
Selection.activeGameObject = go;
|
Selection.activeGameObject = go;
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Stepper
|
#region Stepper
|
||||||
[MenuItem("GameObject/UI/Extensions/Stepper", false)]
|
[MenuItem("GameObject/UI/Extensions/Stepper", false)]
|
||||||
static public void AddStepper(MenuCommand menuCommand)
|
static public void AddStepper(MenuCommand menuCommand)
|
||||||
{
|
{
|
||||||
GameObject go = CreateUIElementRoot("Stepper", menuCommand, new Vector2(42, kThinHeight));
|
GameObject go = CreateUIElementRoot("Stepper", menuCommand, new Vector2(kWidth / 2, kThickHeight));
|
||||||
Stepper control = go.AddComponent<Stepper>();
|
Stepper control = go.AddComponent<Stepper>();
|
||||||
|
|
||||||
var labels = new string[] { "−", "+" };
|
var labels = new string[] { "−", "+" };
|
||||||
|
@ -1741,7 +1741,7 @@ namespace UnityEditor.UI
|
||||||
backgroundImage.fillCenter = false;
|
backgroundImage.fillCenter = false;
|
||||||
backgroundImage.color = new Color(1f, 1f, 1f, 0.392f);
|
backgroundImage.color = new Color(1f, 1f, 1f, 0.392f);
|
||||||
|
|
||||||
RectTransform handleRect = SetAnchorsAndStretch(handle);
|
RectTransform handleRect = SetAnchorsAndStretch(handle);
|
||||||
handleRect.sizeDelta = new Vector2(25, 25);
|
handleRect.sizeDelta = new Vector2(25, 25);
|
||||||
Image handleImage = handle.AddComponent<Image>();
|
Image handleImage = handle.AddComponent<Image>();
|
||||||
handleImage.sprite = AssetDatabase.GetBuiltinExtraResource<Sprite>(kKnobPath);
|
handleImage.sprite = AssetDatabase.GetBuiltinExtraResource<Sprite>(kKnobPath);
|
||||||
|
|
|
@ -117,8 +117,7 @@ namespace UnityEngine.UI.Extensions
|
||||||
{
|
{
|
||||||
base.OnValidate();
|
base.OnValidate();
|
||||||
|
|
||||||
if (separator)
|
LayoutSegments();
|
||||||
LayoutSegments();
|
|
||||||
|
|
||||||
if (m_selectedSegmentIndex != -1)
|
if (m_selectedSegmentIndex != -1)
|
||||||
selectedSegmentIndex = m_selectedSegmentIndex;
|
selectedSegmentIndex = m_selectedSegmentIndex;
|
||||||
|
@ -151,16 +150,12 @@ namespace UnityEngine.UI.Extensions
|
||||||
segment = buttons[i].gameObject.AddComponent<Segment>();
|
segment = buttons[i].gameObject.AddComponent<Segment>();
|
||||||
}
|
}
|
||||||
segment.index = i;
|
segment.index = i;
|
||||||
|
segment.segmentedControl = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
return buttons;
|
return buttons;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetAllSegmentsOff()
|
|
||||||
{
|
|
||||||
selectedSegment = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void RecreateSprites()
|
private void RecreateSprites()
|
||||||
{
|
{
|
||||||
for (int i = 0; i < segments.Length; i++)
|
for (int i = 0; i < segments.Length; i++)
|
||||||
|
@ -168,28 +163,34 @@ namespace UnityEngine.UI.Extensions
|
||||||
if (segments[i].image == null)
|
if (segments[i].image == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
var sprite = segments[i].image.sprite;
|
var sprite = CutSprite(segments[i].image.sprite, i == 0, i == segments.Length - 1);
|
||||||
if (sprite.border.x == 0 || sprite.border.z == 0)
|
segments[i].GetComponent<Segment>().cutSprite = sprite;
|
||||||
continue;
|
segments[i].image.overrideSprite = sprite;
|
||||||
|
|
||||||
var rect = sprite.rect;
|
|
||||||
var border = sprite.border;
|
|
||||||
|
|
||||||
if (i > 0)
|
|
||||||
{
|
|
||||||
rect.xMin = border.x;
|
|
||||||
border.x = 0;
|
|
||||||
}
|
|
||||||
if (i < segments.Length - 1)
|
|
||||||
{
|
|
||||||
rect.xMax = border.z;
|
|
||||||
border.z = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
segments[i].image.sprite = Sprite.Create(sprite.texture, rect, sprite.pivot, sprite.pixelsPerUnit, 0, SpriteMeshType.FullRect, border);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static internal Sprite CutSprite(Sprite sprite, bool leftmost, bool rightmost)
|
||||||
|
{
|
||||||
|
if (sprite.border.x == 0 || sprite.border.z == 0)
|
||||||
|
return sprite;
|
||||||
|
|
||||||
|
var rect = sprite.rect;
|
||||||
|
var border = sprite.border;
|
||||||
|
|
||||||
|
if (!leftmost)
|
||||||
|
{
|
||||||
|
rect.xMin = border.x;
|
||||||
|
border.x = 0;
|
||||||
|
}
|
||||||
|
if (!rightmost)
|
||||||
|
{
|
||||||
|
rect.xMax = border.z;
|
||||||
|
border.z = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Sprite.Create(sprite.texture, rect, sprite.pivot, sprite.pixelsPerUnit, 0, SpriteMeshType.FullRect, border);
|
||||||
|
}
|
||||||
|
|
||||||
public void LayoutSegments()
|
public void LayoutSegments()
|
||||||
{
|
{
|
||||||
RecreateSprites();
|
RecreateSprites();
|
||||||
|
@ -234,6 +235,7 @@ namespace UnityEngine.UI.Extensions
|
||||||
ISelectHandler, IDeselectHandler
|
ISelectHandler, IDeselectHandler
|
||||||
{
|
{
|
||||||
internal int index;
|
internal int index;
|
||||||
|
internal SegmentedControl segmentedControl;
|
||||||
|
|
||||||
internal bool leftmost
|
internal bool leftmost
|
||||||
{
|
{
|
||||||
|
@ -241,20 +243,15 @@ namespace UnityEngine.UI.Extensions
|
||||||
}
|
}
|
||||||
internal bool rightmost
|
internal bool rightmost
|
||||||
{
|
{
|
||||||
get { return index == segmentControl.segments.Length - 1; }
|
get { return index == segmentedControl.segments.Length - 1; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool selected
|
public bool selected
|
||||||
{
|
{
|
||||||
get { return segmentControl.selectedSegment == this.button; }
|
get { return segmentedControl.selectedSegment == this.button; }
|
||||||
set { SetSelected(value); }
|
set { SetSelected(value); }
|
||||||
}
|
}
|
||||||
|
|
||||||
internal SegmentedControl segmentControl
|
|
||||||
{
|
|
||||||
get { return GetComponentInParent<SegmentedControl>(); }
|
|
||||||
}
|
|
||||||
|
|
||||||
internal Selectable button
|
internal Selectable button
|
||||||
{
|
{
|
||||||
get { return GetComponent<Selectable>(); }
|
get { return GetComponent<Selectable>(); }
|
||||||
|
@ -262,6 +259,7 @@ namespace UnityEngine.UI.Extensions
|
||||||
|
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
Color textColor;
|
Color textColor;
|
||||||
|
internal Sprite cutSprite;
|
||||||
|
|
||||||
protected Segment()
|
protected Segment()
|
||||||
{ }
|
{ }
|
||||||
|
@ -313,9 +311,9 @@ namespace UnityEngine.UI.Extensions
|
||||||
{
|
{
|
||||||
if (value && button.IsActive() && button.IsInteractable())
|
if (value && button.IsActive() && button.IsInteractable())
|
||||||
{
|
{
|
||||||
if (segmentControl.selectedSegment == this.button)
|
if (segmentedControl.selectedSegment == this.button)
|
||||||
{
|
{
|
||||||
if (segmentControl.allowSwitchingOff)
|
if (segmentedControl.allowSwitchingOff)
|
||||||
{
|
{
|
||||||
Deselect();
|
Deselect();
|
||||||
}
|
}
|
||||||
|
@ -326,20 +324,20 @@ namespace UnityEngine.UI.Extensions
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (segmentControl.selectedSegment)
|
if (segmentedControl.selectedSegment)
|
||||||
{
|
{
|
||||||
var segment = segmentControl.selectedSegment.GetComponent<Segment>();
|
var segment = segmentedControl.selectedSegment.GetComponent<Segment>();
|
||||||
segmentControl.selectedSegment = null;
|
segmentedControl.selectedSegment = null;
|
||||||
segment.TransitionButton();
|
segment.TransitionButton();
|
||||||
}
|
}
|
||||||
|
|
||||||
segmentControl.selectedSegment = this.button;
|
segmentedControl.selectedSegment = this.button;
|
||||||
StoreTextColor();
|
StoreTextColor();
|
||||||
TransitionButton();
|
TransitionButton();
|
||||||
segmentControl.onValueChanged.Invoke(index);
|
segmentedControl.onValueChanged.Invoke(index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (segmentControl.selectedSegment == this.button)
|
else if (segmentedControl.selectedSegment == this.button)
|
||||||
{
|
{
|
||||||
Deselect();
|
Deselect();
|
||||||
}
|
}
|
||||||
|
@ -347,14 +345,14 @@ namespace UnityEngine.UI.Extensions
|
||||||
|
|
||||||
private void Deselect()
|
private void Deselect()
|
||||||
{
|
{
|
||||||
segmentControl.selectedSegment = null;
|
segmentedControl.selectedSegment = null;
|
||||||
TransitionButton();
|
TransitionButton();
|
||||||
segmentControl.onValueChanged.Invoke(-1);
|
segmentedControl.onValueChanged.Invoke(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MaintainSelection()
|
void MaintainSelection()
|
||||||
{
|
{
|
||||||
if (button != segmentControl.selectedSegment)
|
if (button != segmentedControl.selectedSegment)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
TransitionButton(true);
|
TransitionButton(true);
|
||||||
|
@ -367,9 +365,9 @@ namespace UnityEngine.UI.Extensions
|
||||||
|
|
||||||
internal void TransitionButton(bool instant)
|
internal void TransitionButton(bool instant)
|
||||||
{
|
{
|
||||||
Color tintColor = selected ? segmentControl.selectedColor : button.colors.normalColor;
|
Color tintColor = selected ? segmentedControl.selectedColor : button.colors.normalColor;
|
||||||
Color textColor = selected ? button.colors.normalColor : this.textColor;
|
Color textColor = selected ? button.colors.normalColor : this.textColor;
|
||||||
Sprite transitionSprite = selected ? button.spriteState.pressedSprite : null;
|
Sprite transitionSprite = selected ? button.spriteState.pressedSprite : cutSprite;
|
||||||
string triggerName = selected ? button.animationTriggers.pressedTrigger : button.animationTriggers.normalTrigger;
|
string triggerName = selected ? button.animationTriggers.pressedTrigger : button.animationTriggers.normalTrigger;
|
||||||
|
|
||||||
switch (button.transition)
|
switch (button.transition)
|
||||||
|
@ -379,6 +377,8 @@ namespace UnityEngine.UI.Extensions
|
||||||
ChangeTextColor(textColor * button.colors.colorMultiplier);
|
ChangeTextColor(textColor * button.colors.colorMultiplier);
|
||||||
break;
|
break;
|
||||||
case Selectable.Transition.SpriteSwap:
|
case Selectable.Transition.SpriteSwap:
|
||||||
|
if (transitionSprite != cutSprite)
|
||||||
|
transitionSprite = SegmentedControl.CutSprite(transitionSprite, leftmost, rightmost);
|
||||||
DoSpriteSwap(transitionSprite);
|
DoSpriteSwap(transitionSprite);
|
||||||
break;
|
break;
|
||||||
case Selectable.Transition.Animation:
|
case Selectable.Transition.Animation:
|
||||||
|
|
|
@ -93,6 +93,7 @@ namespace UnityEngine.UI.Extensions
|
||||||
{
|
{
|
||||||
base.OnValidate();
|
base.OnValidate();
|
||||||
|
|
||||||
|
RecreateSprites(sides);
|
||||||
if (separator)
|
if (separator)
|
||||||
LayoutSides();
|
LayoutSides();
|
||||||
|
|
||||||
|
@ -172,28 +173,34 @@ namespace UnityEngine.UI.Extensions
|
||||||
if (sides[i].image == null)
|
if (sides[i].image == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
var sprite = sides[i].image.sprite;
|
var sprite = CutSprite(sides[i].image.sprite, i == 0);
|
||||||
if (sprite.border.x == 0 || sprite.border.z == 0)
|
sides[i].GetComponent<StepperSide>().cutSprite = sprite;
|
||||||
continue;
|
sides[i].image.overrideSprite = sprite;
|
||||||
|
|
||||||
var rect = sprite.rect;
|
|
||||||
var border = sprite.border;
|
|
||||||
|
|
||||||
if (i == 0)
|
|
||||||
{
|
|
||||||
rect.xMax = border.z;
|
|
||||||
border.z = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rect.xMin = border.x;
|
|
||||||
border.x = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
sides[i].image.sprite = Sprite.Create(sprite.texture, rect, sprite.pivot, sprite.pixelsPerUnit, 0, SpriteMeshType.FullRect, border);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static internal Sprite CutSprite(Sprite sprite, bool leftmost)
|
||||||
|
{
|
||||||
|
if (sprite.border.x == 0 || sprite.border.z == 0)
|
||||||
|
return sprite;
|
||||||
|
|
||||||
|
var rect = sprite.rect;
|
||||||
|
var border = sprite.border;
|
||||||
|
|
||||||
|
if (leftmost)
|
||||||
|
{
|
||||||
|
rect.xMax = border.z;
|
||||||
|
border.z = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rect.xMin = border.x;
|
||||||
|
border.x = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Sprite.Create(sprite.texture, rect, sprite.pivot, sprite.pixelsPerUnit, 0, SpriteMeshType.FullRect, border);
|
||||||
|
}
|
||||||
|
|
||||||
public void LayoutSides(Selectable[] sides = null)
|
public void LayoutSides(Selectable[] sides = null)
|
||||||
{
|
{
|
||||||
sides = sides ?? this.sides;
|
sides = sides ?? this.sides;
|
||||||
|
@ -232,7 +239,13 @@ namespace UnityEngine.UI.Extensions
|
||||||
}
|
}
|
||||||
|
|
||||||
[RequireComponent(typeof(Selectable))]
|
[RequireComponent(typeof(Selectable))]
|
||||||
public class StepperSide : UIBehaviour, IPointerClickHandler, ISubmitHandler
|
public class StepperSide :
|
||||||
|
UIBehaviour,
|
||||||
|
IPointerClickHandler,
|
||||||
|
ISubmitHandler,
|
||||||
|
IPointerEnterHandler, IPointerExitHandler,
|
||||||
|
IPointerDownHandler, IPointerUpHandler,
|
||||||
|
ISelectHandler, IDeselectHandler
|
||||||
{
|
{
|
||||||
Selectable button { get { return GetComponent<Selectable>(); } }
|
Selectable button { get { return GetComponent<Selectable>(); } }
|
||||||
|
|
||||||
|
@ -240,6 +253,8 @@ namespace UnityEngine.UI.Extensions
|
||||||
|
|
||||||
bool leftmost { get { return button == stepper.sides[0]; } }
|
bool leftmost { get { return button == stepper.sides[0]; } }
|
||||||
|
|
||||||
|
internal Sprite cutSprite;
|
||||||
|
|
||||||
protected StepperSide()
|
protected StepperSide()
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
|
@ -249,11 +264,43 @@ namespace UnityEngine.UI.Extensions
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Press();
|
Press();
|
||||||
|
AdjustSprite(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void OnSubmit(BaseEventData eventData)
|
public virtual void OnSubmit(BaseEventData eventData)
|
||||||
{
|
{
|
||||||
Press();
|
Press();
|
||||||
|
AdjustSprite(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void OnPointerEnter(PointerEventData eventData)
|
||||||
|
{
|
||||||
|
AdjustSprite(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void OnPointerExit(PointerEventData eventData)
|
||||||
|
{
|
||||||
|
AdjustSprite(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void OnPointerDown(PointerEventData eventData)
|
||||||
|
{
|
||||||
|
AdjustSprite(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void OnPointerUp(PointerEventData eventData)
|
||||||
|
{
|
||||||
|
AdjustSprite(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void OnSelect(BaseEventData eventData)
|
||||||
|
{
|
||||||
|
AdjustSprite(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void OnDeselect(BaseEventData eventData)
|
||||||
|
{
|
||||||
|
AdjustSprite(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Press()
|
private void Press()
|
||||||
|
@ -270,5 +317,17 @@ namespace UnityEngine.UI.Extensions
|
||||||
stepper.StepUp();
|
stepper.StepUp();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void AdjustSprite(bool restore)
|
||||||
|
{
|
||||||
|
var image = button.image;
|
||||||
|
if (!image || image.overrideSprite == cutSprite)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (restore)
|
||||||
|
image.overrideSprite = cutSprite;
|
||||||
|
else
|
||||||
|
image.overrideSprite = Stepper.CutSprite(image.overrideSprite, leftmost);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue