<commit_before>﻿using System;
using FairyGUI.Utils;
using UnityEngine;
using DG.Tweening;

namespace FairyGUI
{
	/// <summary>
	/// GProgressBar class.
	/// </summary>
	public class GProgressBar : GComponent
	{
		double _max;
		double _value;
		ProgressTitleType _titleType;
		bool _reverse;

		GTextField _titleObject;
		GMovieClip _aniObject;
		GObject _barObjectH;
		GObject _barObjectV;
		float _barMaxWidth;
		float _barMaxHeight;
		float _barMaxWidthDelta;
		float _barMaxHeightDelta;
		float _barStartX;
		float _barStartY;

		Tweener _tweener;

		public GProgressBar()
		{
			_value = 50;
			_max = 100;
		}

		/// <summary>
		/// 
		/// </summary>
		public ProgressTitleType titleType
		{

			get
			{
				return _titleType;
			}
			set
			{
				if (_titleType != value)
				{
					_titleType = value;
					Update(_value);
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public double max
		{
			get
			{
				return _max;
			}
			set
			{
				if (_max != value)
				{
					_max = value;
					Update(_value);
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public double value
		{
			get
			{
				return _value;
			}
			set
			{
				if (_tweener != null)
				{
					_tweener.Kill(true);
					_tweener = null;
				}

				if (_value != value)
				{
					_value = value;
					Update(_value);
				}
			}
		}

		public bool reverse
		{
			get { return _reverse; }
			set { _reverse = value; }
		}

		/// <summary>
		/// 动态改变进度值。
		/// </summary>
		/// <param name="value"></param>
		/// <param name="duration"></param>
		public Tweener TweenValue(double value, float duration)
		{
			if (_value != value)
			{
				if (_tweener != null)
					_tweener.Kill(false);

				double oldValue = _value;
				_value = value;
				_tweener = DOTween.To(() => oldValue, v => { Update(v); }, value, duration)
					.SetEase(Ease.Linear).OnComplete(() => { _tweener = null; });

				return _tweener;
			}
			else
				return null;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="newValue"></param>
		public void Update(double newValue)
		{
			float percent = (float)Math.Min(newValue / _max, 1);
			if (_titleObject != null)
			{
				switch (_titleType)
				{
					case ProgressTitleType.Percent:
						_titleObject.text = Mathf.RoundToInt(percent * 100) + "%";
						break;

					case ProgressTitleType.ValueAndMax:
						_titleObject.text = Math.Round(newValue) + "/" + Math.Round(max);
						break;

					case ProgressTitleType.Value:
						_titleObject.text = "" + Math.Round(newValue);
						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)
				{<commit_msg>Handle properly when GProgressBar.max is zero.<commit_after>﻿using System;
using FairyGUI.Utils;
using UnityEngine;
using DG.Tweening;

namespace FairyGUI
{
	/// <summary>
	/// GProgressBar class.
	/// </summary>
	public class GProgressBar : GComponent
	{
		double _max;
		double _value;
		ProgressTitleType _titleType;
		bool _reverse;

		GTextField _titleObject;
		GMovieClip _aniObject;
		GObject _barObjectH;
		GObject _barObjectV;
		float _barMaxWidth;
		float _barMaxHeight;
		float _barMaxWidthDelta;
		float _barMaxHeightDelta;
		float _barStartX;
		float _barStartY;

		Tweener _tweener;

		public GProgressBar()
		{
			_value = 50;
			_max = 100;
		}

		/// <summary>
		/// 
		/// </summary>
		public ProgressTitleType titleType
		{

			get
			{
				return _titleType;
			}
			set
			{
				if (_titleType != value)
				{
					_titleType = value;
					Update(_value);
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public double max
		{
			get
			{
				return _max;
			}
			set
			{
				if (_max != value)
				{
					_max = value;
					Update(_value);
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public double value
		{
			get
			{
				return _value;
			}
			set
			{
				if (_tweener != null)
				{
					_tweener.Kill(true);
					_tweener = null;
				}

				if (_value != value)
				{
					_value = value;
					Update(_value);
				}
			}
		}

		public bool reverse
		{
			get { return _reverse; }
			set { _reverse = value; }
		}

		/// <summary>
		/// 动态改变进度值。
		/// </summary>
		/// <param name="value"></param>
		/// <param name="duration"></param>
		public Tweener TweenValue(double value, float duration)
		{
			if (_value != value)
			{
				if (_tweener != null)
					_tweener.Kill(false);

				double oldValue = _value;
				_value = value;
				_tweener = DOTween.To(() => oldValue, v => { Update(v); }, value, duration)
					.SetEase(Ease.Linear).OnComplete(() => { _tweener = null; });

				return _tweener;
			}
			else
				return null;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="newValue"></param>
		public void Update(double newValue)
		{
			float percent = _max != 0 ? (float)Math.Min(newValue / _max, 1) : 0;
			if (_titleObject != null)
			{
				switch (_titleType)
				{
					case ProgressTitleType.Percent:
						_titleObject.text = Mathf.RoundToInt(percent * 100) + "%";
						break;

					case ProgressTitleType.ValueAndMax:
						_titleObject.text = Math.Round(newValue) + "/" + Math.Round(max);
						break;

					case ProgressTitleType.Value:
						_titleObject.text = "" + Math.Round(newValue);
						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)
				{
{'fraction_non_alphanumeric': 0.13613119379298183, 'fraction_numerical': 0.0035267148651031564, 'mean_word_length': 2.200902934537246, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2342441', 'n_tokens_mistral': 2658, 'n_tokens_neox': 2003, 'n_words': 422}