Files

157 lines
3.3 KiB
C#
Raw Permalink Normal View History

2025-01-25 04:38:09 +08:00
using System;
namespace Thousandto.Launcher.Form
{
/// <summary>
/// 下载进度的处理
/// </summary>
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<bool> _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<bool> 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();
}
}
}
/// <summary>
/// 刷新信息
/// </summary>
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();
}
}
}