Change Stepper and Segmented Control to use overrideImage, standard size, other fixes

release
David Gileadi 2017-08-12 16:19:19 -07:00
parent 1f16a65f0f
commit c5edc0cdf1
3 changed files with 129 additions and 70 deletions

View File

@ -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);
@ -1682,7 +1682,7 @@ namespace UnityEditor.UI
[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[] { "", "+" };

View File

@ -117,7 +117,6 @@ namespace UnityEngine.UI.Extensions
{ {
base.OnValidate(); base.OnValidate();
if (separator)
LayoutSegments(); LayoutSegments();
if (m_selectedSegmentIndex != -1) if (m_selectedSegmentIndex != -1)
@ -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,26 +163,32 @@ 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);
segments[i].GetComponent<Segment>().cutSprite = sprite;
segments[i].image.overrideSprite = sprite;
}
}
static internal Sprite CutSprite(Sprite sprite, bool leftmost, bool rightmost)
{
if (sprite.border.x == 0 || sprite.border.z == 0) if (sprite.border.x == 0 || sprite.border.z == 0)
continue; return sprite;
var rect = sprite.rect; var rect = sprite.rect;
var border = sprite.border; var border = sprite.border;
if (i > 0) if (!leftmost)
{ {
rect.xMin = border.x; rect.xMin = border.x;
border.x = 0; border.x = 0;
} }
if (i < segments.Length - 1) if (!rightmost)
{ {
rect.xMax = border.z; rect.xMax = border.z;
border.z = 0; border.z = 0;
} }
segments[i].image.sprite = Sprite.Create(sprite.texture, rect, sprite.pivot, sprite.pixelsPerUnit, 0, SpriteMeshType.FullRect, border); return Sprite.Create(sprite.texture, rect, sprite.pivot, sprite.pixelsPerUnit, 0, SpriteMeshType.FullRect, border);
}
} }
public void LayoutSegments() public void LayoutSegments()
@ -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:

View File

@ -93,6 +93,7 @@ namespace UnityEngine.UI.Extensions
{ {
base.OnValidate(); base.OnValidate();
RecreateSprites(sides);
if (separator) if (separator)
LayoutSides(); LayoutSides();
@ -172,14 +173,21 @@ 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);
sides[i].GetComponent<StepperSide>().cutSprite = sprite;
sides[i].image.overrideSprite = sprite;
}
}
static internal Sprite CutSprite(Sprite sprite, bool leftmost)
{
if (sprite.border.x == 0 || sprite.border.z == 0) if (sprite.border.x == 0 || sprite.border.z == 0)
continue; return sprite;
var rect = sprite.rect; var rect = sprite.rect;
var border = sprite.border; var border = sprite.border;
if (i == 0) if (leftmost)
{ {
rect.xMax = border.z; rect.xMax = border.z;
border.z = 0; border.z = 0;
@ -190,8 +198,7 @@ namespace UnityEngine.UI.Extensions
border.x = 0; border.x = 0;
} }
sides[i].image.sprite = Sprite.Create(sprite.texture, rect, sprite.pivot, sprite.pixelsPerUnit, 0, SpriteMeshType.FullRect, border); 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)
@ -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);
}
} }
} }