123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- using System;
- using UnityEngine;
- [ExecuteInEditMode]
- [AddComponentMenu("NGUI/Interaction/Button Color")]
- public class UIButtonColor : UIWidgetContainer
- {
- public UIButtonColor.State state
- {
- get
- {
- return this.mState;
- }
- set
- {
- this.SetState(value, false);
- }
- }
- public Color defaultColor
- {
- get
- {
- if (!this.mInitDone)
- {
- this.OnInit();
- }
- return this.mDefaultColor;
- }
- set
- {
- if (!this.mInitDone)
- {
- this.OnInit();
- }
- this.mDefaultColor = value;
- UIButtonColor.State state = this.mState;
- this.mState = UIButtonColor.State.Disabled;
- this.SetState(state, false);
- }
- }
- public virtual bool isEnabled
- {
- get
- {
- return base.enabled;
- }
- set
- {
- base.enabled = value;
- }
- }
- public void ResetDefaultColor()
- {
- this.defaultColor = this.mStartingColor;
- }
- private void Start()
- {
- if (!this.mInitDone)
- {
- this.OnInit();
- }
- if (!this.isEnabled)
- {
- this.SetState(UIButtonColor.State.Disabled, true);
- }
- }
- protected virtual void OnInit()
- {
- this.mInitDone = true;
- if (this.tweenTarget == null)
- {
- this.tweenTarget = base.gameObject;
- }
- if (this.tweenTarget != null)
- {
- this.mWidget = this.tweenTarget.GetComponent<UIWidget>();
- }
- if (this.mWidget != null)
- {
- this.mDefaultColor = this.mWidget.color;
- this.mStartingColor = this.mDefaultColor;
- }
- else if (this.tweenTarget != null)
- {
- Renderer component = this.tweenTarget.GetComponent<Renderer>();
- if (component != null)
- {
- this.mDefaultColor = ((!Application.isPlaying) ? component.sharedMaterial.color : component.material.color);
- this.mStartingColor = this.mDefaultColor;
- }
- else
- {
- Light component2 = this.tweenTarget.GetComponent<Light>();
- if (component2 != null)
- {
- this.mDefaultColor = component2.color;
- this.mStartingColor = this.mDefaultColor;
- }
- else
- {
- this.tweenTarget = null;
- this.mInitDone = false;
- }
- }
- }
- }
- protected virtual void OnEnable()
- {
- if (this.mInitDone)
- {
- this.OnHover(UICamera.IsHighlighted(base.gameObject));
- }
- if (UICamera.currentTouch != null)
- {
- if (UICamera.currentTouch.pressed == base.gameObject)
- {
- this.OnPress(true);
- }
- else if (UICamera.currentTouch.current == base.gameObject)
- {
- this.OnHover(true);
- }
- }
- }
- protected virtual void OnDisable()
- {
- if (this.mInitDone && this.tweenTarget != null)
- {
- this.SetState(UIButtonColor.State.Normal, true);
- TweenColor component = this.tweenTarget.GetComponent<TweenColor>();
- if (component != null)
- {
- component.value = this.mDefaultColor;
- component.enabled = false;
- }
- }
- }
- protected virtual void OnHover(bool isOver)
- {
- if (this.isEnabled)
- {
- if (!this.mInitDone)
- {
- this.OnInit();
- }
- if (this.tweenTarget != null)
- {
- this.SetState((!isOver) ? UIButtonColor.State.Normal : UIButtonColor.State.Hover, false);
- }
- }
- }
- protected virtual void OnPress(bool isPressed)
- {
- if (this.isEnabled && UICamera.currentTouch != null)
- {
- if (!this.mInitDone)
- {
- this.OnInit();
- }
- if (this.tweenTarget != null)
- {
- if (isPressed)
- {
- this.SetState(UIButtonColor.State.Pressed, false);
- }
- else if (UICamera.currentTouch.current == base.gameObject)
- {
- if (UICamera.currentScheme == UICamera.ControlScheme.Controller)
- {
- this.SetState(UIButtonColor.State.Hover, false);
- }
- else if (UICamera.currentScheme == UICamera.ControlScheme.Mouse && UICamera.hoveredObject == base.gameObject)
- {
- this.SetState(UIButtonColor.State.Hover, false);
- }
- else
- {
- this.SetState(UIButtonColor.State.Normal, false);
- }
- }
- else
- {
- this.SetState(UIButtonColor.State.Normal, false);
- }
- }
- }
- }
- protected virtual void OnDragOver()
- {
- if (this.isEnabled)
- {
- if (!this.mInitDone)
- {
- this.OnInit();
- }
- if (this.tweenTarget != null)
- {
- this.SetState(UIButtonColor.State.Pressed, false);
- }
- }
- }
- protected virtual void OnDragOut()
- {
- if (this.isEnabled)
- {
- if (!this.mInitDone)
- {
- this.OnInit();
- }
- if (this.tweenTarget != null)
- {
- this.SetState(UIButtonColor.State.Normal, false);
- }
- }
- }
- protected virtual void OnSelect(bool isSelected)
- {
- if (this.isEnabled && this.tweenTarget != null)
- {
- if (UICamera.currentScheme == UICamera.ControlScheme.Controller)
- {
- this.OnHover(isSelected);
- }
- else if (!isSelected && UICamera.touchCount < 2)
- {
- this.OnHover(isSelected);
- }
- }
- }
- public virtual void SetState(UIButtonColor.State state, bool instant)
- {
- if (!this.mInitDone)
- {
- this.mInitDone = true;
- this.OnInit();
- }
- if (this.mState != state)
- {
- this.mState = state;
- this.UpdateColor(instant);
- }
- }
- public void UpdateColor(bool instant)
- {
- if (this.tweenTarget != null)
- {
- TweenColor tweenColor;
- switch (this.mState)
- {
- case UIButtonColor.State.Hover:
- tweenColor = TweenColor.Begin(this.tweenTarget, this.duration, this.hover);
- break;
- case UIButtonColor.State.Pressed:
- tweenColor = TweenColor.Begin(this.tweenTarget, this.duration, this.pressed);
- break;
- case UIButtonColor.State.Disabled:
- tweenColor = TweenColor.Begin(this.tweenTarget, this.duration, this.disabledColor);
- break;
- default:
- tweenColor = TweenColor.Begin(this.tweenTarget, this.duration, this.mDefaultColor);
- break;
- }
- if (instant && tweenColor != null)
- {
- tweenColor.value = tweenColor.to;
- tweenColor.enabled = false;
- }
- }
- }
- public GameObject tweenTarget;
- public Color hover = new Color(0.882352948f, 0.784313738f, 0.5882353f, 1f);
- public Color pressed = new Color(0.7176471f, 0.6392157f, 0.482352942f, 1f);
- public Color disabledColor = Color.grey;
- public float duration = 0.2f;
- [NonSerialized]
- protected Color mStartingColor;
- [NonSerialized]
- protected Color mDefaultColor;
- [NonSerialized]
- protected bool mInitDone;
- [NonSerialized]
- protected UIWidget mWidget;
- [NonSerialized]
- protected UIButtonColor.State mState;
- public enum State
- {
- Normal,
- Hover,
- Pressed,
- Disabled
- }
- }
|