using System; namespace Thousandto.Launcher.Form { /// /// 下载进度的处理 /// public class UIDownloading { private UILoadingBasePanel _panel; //每秒倒计时 private float _perSecondTimer; //速度 private int _speed = 0; //旧进度,用来计算下载速度的 private int _oldProgressValue; //已经下载 private int _downSize; //总的数量 private int _totalSize; private System.Func _onRunningHandler; private bool _enabled; private string _stepName; //展示下载的样式 private bool _showDownloadStyle = true; public bool ShowDownloadStyle { get { return _showDownloadStyle; } set { _showDownloadStyle = value; } } public int DownSize { get { return _downSize; } set { _downSize = value; } } public int TotalSize { get { return _totalSize; } set { _totalSize = value; } } public Func OnRunningHandler { get { return _onRunningHandler; } set { _onRunningHandler = value; } } public bool Enabled { get { return _enabled; } set { _enabled = value; } } public string StepName { get { return _stepName; } set { _stepName = value; } } public UIDownloading(UILoadingBasePanel panel) { _panel = panel; } public void Update(float deltaTime) { if (_enabled) { _perSecondTimer += deltaTime; if (_perSecondTimer >= 1.0f) { _perSecondTimer = 0; } if (_onRunningHandler != null && _onRunningHandler()) { Refresh(); } } } /// /// 刷新信息 /// public void Refresh() { if (_oldProgressValue == 0) _oldProgressValue = _downSize; if (_perSecondTimer == 0) { _speed = _perSecondTimer == 0 ? (_downSize - _oldProgressValue) / 1024 : 0; _oldProgressValue = _downSize; if (_speed < 0) _speed = 0; } float percetage = _totalSize <= 0 ? 1 : _downSize / (float)_totalSize; _panel.SetSpeed(_speed); _panel.SetProgress(percetage); _panel.SetUseDownload(_showDownloadStyle); _panel.SetProgressMinToMax(_downSize, _totalSize); _panel.SetStepName(_stepName); _panel.Refresh(); } } }