| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 | using System;using UnityEngine;using FairyGUI.Utils;namespace FairyGUI{    /// <summary>    ///     /// </summary>    public class GSlider : GComponent    {        double _min;        double _max;        double _value;        ProgressTitleType _titleType;        bool _reverse;        bool _wholeNumbers;        GObject _titleObject;        GObject _barObjectH;        GObject _barObjectV;        float _barMaxWidth;        float _barMaxHeight;        float _barMaxWidthDelta;        float _barMaxHeightDelta;        GObject _gripObject;        Vector2 _clickPos;        float _clickPercent;        float _barStartX;        float _barStartY;        EventListener _onChanged;        EventListener _onGripTouchEnd;        public bool changeOnClick;        public bool canDrag;        public GSlider()        {            _value = 50;            _max = 100;            changeOnClick = true;            canDrag = true;        }        /// <summary>        ///         /// </summary>        public EventListener onChanged        {            get { return _onChanged ?? (_onChanged = new EventListener(this, "onChanged")); }        }        /// <summary>        ///         /// </summary>        public EventListener onGripTouchEnd        {            get { return _onGripTouchEnd ?? (_onGripTouchEnd = new EventListener(this, "onGripTouchEnd")); }        }        /// <summary>        ///         /// </summary>        public ProgressTitleType titleType        {            get            {                return _titleType;            }            set            {                if (_titleType != value)                {                    _titleType = value;                    Update();                }            }        }        /// <summary>        ///         /// </summary>        public double min        {            get            {                return _min;            }            set            {                if (_min != value)                {                    _min = value;                    Update();                }            }        }        /// <summary>        ///         /// </summary>        public double max        {            get            {                return _max;            }            set            {                if (_max != value)                {                    _max = value;                    Update();                }            }        }        /// <summary>        ///         /// </summary>        public double value        {            get            {                return _value;            }            set            {                if (_value != value)                {                    _value = value;                    Update();                }            }        }        /// <summary>        ///         /// </summary>        public bool wholeNumbers        {            get            {                return _wholeNumbers;            }            set            {                if (_wholeNumbers != value)                {                    _wholeNumbers = value;                    Update();                }            }        }        private void Update()        {            UpdateWithPercent((float)((_value - _min) / (_max - _min)), false);        }        private void UpdateWithPercent(float percent, bool manual)        {            percent = Mathf.Clamp01(percent);            if (manual)            {                double newValue = _min + (_max - _min) * percent;                if (newValue < _min)                    newValue = _min;                if (newValue > _max)                    newValue = _max;                if (_wholeNumbers)                {                    newValue = Math.Round(newValue);                    percent = Mathf.Clamp01((float)((newValue - _min) / (_max - _min)));                }                if (newValue != _value)                {                    _value = newValue;                    if (DispatchEvent("onChanged", null))                        return;                }            }            if (_titleObject != null)            {                switch (_titleType)                {                    case ProgressTitleType.Percent:                        _titleObject.text = Mathf.FloorToInt(percent * 100) + "%";                        break;                    case ProgressTitleType.ValueAndMax:                        _titleObject.text = Math.Round(_value) + "/" + Math.Round(max);                        break;                    case ProgressTitleType.Value:                        _titleObject.text = "" + Math.Round(_value);                        break;                    case ProgressTitleType.Max:                        _titleObject.text = "" + Math.Round(_max);                        break;                }            }            float fullWidth = this.width - _barMaxWidthDelta;            float fullHeight = this.height - _barMaxHeightDelta;            if (!_reverse)            {                if (_barObjectH != null)                {                    if (!SetFillAmount(_barObjectH, percent))                        _barObjectH.width = Mathf.RoundToInt(fullWidth * percent);                }                if (_barObjectV != null)                {                    if (!SetFillAmount(_barObjectV, percent))                        _barObjectV.height = Mathf.RoundToInt(fullHeight * percent);                }            }            else            {                if (_barObjectH != null)                {                    if (!SetFillAmount(_barObjectH, 1 - percent))                    {                        _barObjectH.width = Mathf.RoundToInt(fullWidth * percent);                        _barObjectH.x = _barStartX + (fullWidth - _barObjectH.width);                    }                }                if (_barObjectV != null)                {                    if (!SetFillAmount(_barObjectV, 1 - percent))                    {                        _barObjectV.height = Mathf.RoundToInt(fullHeight * percent);                        _barObjectV.y = _barStartY + (fullHeight - _barObjectV.height);                    }                }            }            InvalidateBatchingState(true);        }        bool SetFillAmount(GObject bar, float amount)        {            if ((bar is GImage) && ((GImage)bar).fillMethod != FillMethod.None)                ((GImage)bar).fillAmount = amount;            else if ((bar is GLoader) && ((GLoader)bar).fillMethod != FillMethod.None)                ((GLoader)bar).fillAmount = amount;            else                return false;            return true;        }        override protected void ConstructExtension(ByteBuffer buffer)        {            buffer.Seek(0, 6);            _titleType = (ProgressTitleType)buffer.ReadByte();            _reverse = buffer.ReadBool();            if (buffer.version >= 2)            {                _wholeNumbers = buffer.ReadBool();                this.changeOnClick = buffer.ReadBool();            }            _titleObject = GetChild("title");            _barObjectH = GetChild("bar");            _barObjectV = GetChild("bar_v");            _gripObject = GetChild("grip");            if (_barObjectH != null)            {                _barMaxWidth = _barObjectH.width;                _barMaxWidthDelta = this.width - _barMaxWidth;                _barStartX = _barObjectH.x;            }            if (_barObjectV != null)            {                _barMaxHeight = _barObjectV.height;                _barMaxHeightDelta = this.height - _barMaxHeight;                _barStartY = _barObjectV.y;            }            if (_gripObject != null)            {                _gripObject.onTouchBegin.Add(__gripTouchBegin);                _gripObject.onTouchMove.Add(__gripTouchMove);                _gripObject.onTouchEnd.Add(__gripTouchEnd);            }            onTouchBegin.Add(__barTouchBegin);        }        override public void Setup_AfterAdd(ByteBuffer buffer, int beginPos)        {            base.Setup_AfterAdd(buffer, beginPos);            if (!buffer.Seek(beginPos, 6))            {                Update();                return;            }            if ((ObjectType)buffer.ReadByte() != packageItem.objectType)            {                Update();                return;            }            _value = buffer.ReadInt();            _max = buffer.ReadInt();            if (buffer.version >= 2)                _min = buffer.ReadInt();            Update();        }        override protected void HandleSizeChanged()        {            base.HandleSizeChanged();            if (_barObjectH != null)                _barMaxWidth = this.width - _barMaxWidthDelta;            if (_barObjectV != null)                _barMaxHeight = this.height - _barMaxHeightDelta;            if (!this.underConstruct)                Update();        }        private void __gripTouchBegin(EventContext context)        {            this.canDrag = true;            context.StopPropagation();            InputEvent evt = context.inputEvent;            if (evt.button != 0)                return;            context.CaptureTouch();            _clickPos = this.GlobalToLocal(new Vector2(evt.x, evt.y));            _clickPercent = Mathf.Clamp01((float)((_value - _min) / (_max - _min)));        }        private void __gripTouchMove(EventContext context)        {            if (!this.canDrag)                return;            InputEvent evt = context.inputEvent;            Vector2 pt = this.GlobalToLocal(new Vector2(evt.x, evt.y));            if (float.IsNaN(pt.x))                return;            float deltaX = pt.x - _clickPos.x;            float deltaY = pt.y - _clickPos.y;            if (_reverse)            {                deltaX = -deltaX;                deltaY = -deltaY;            }            float percent;            if (_barObjectH != null)                percent = _clickPercent + deltaX / _barMaxWidth;            else                percent = _clickPercent + deltaY / _barMaxHeight;            UpdateWithPercent(percent, true);        }        private void __gripTouchEnd(EventContext context)        {            DispatchEvent("onGripTouchEnd", null);        }        private void __barTouchBegin(EventContext context)        {            if (!changeOnClick)                return;            InputEvent evt = context.inputEvent;            Vector2 pt = _gripObject.GlobalToLocal(new Vector2(evt.x, evt.y));            float percent = Mathf.Clamp01((float)((_value - _min) / (_max - _min)));            float delta = 0;            if (_barObjectH != null)                delta = (pt.x - _gripObject.width / 2) / _barMaxWidth;            if (_barObjectV != null)                delta = (pt.y - _gripObject.height / 2) / _barMaxHeight;            if (_reverse)                percent -= delta;            else                percent += delta;            UpdateWithPercent(percent, true);        }    }}
 |