Add bindings to ProgressBarWidget.

This commit is contained in:
Paul Chote
2014-03-16 18:02:15 +13:00
parent e2e0728e20
commit 6b199d3376
2 changed files with 28 additions and 17 deletions

View File

@@ -8,6 +8,7 @@
*/ */
#endregion #endregion
using System;
using System.Drawing; using System.Drawing;
namespace OpenRA.Widgets namespace OpenRA.Widgets
@@ -17,45 +18,57 @@ namespace OpenRA.Widgets
public int Percentage = 0; public int Percentage = 0;
public bool Indeterminate = false; public bool Indeterminate = false;
public Func<int> GetPercentage;
public Func<bool> IsIndeterminate;
// Indeterminant bar properties // Indeterminant bar properties
float offset = 0f; float offset = 0f;
float tickStep = 0.04f; float tickStep = 0.04f;
public ProgressBarWidget() {} public ProgressBarWidget()
protected ProgressBarWidget(ProgressBarWidget widget)
: base(widget)
{ {
Percentage = widget.Percentage; GetPercentage = () => Percentage;
IsIndeterminate = () => Indeterminate;
}
protected ProgressBarWidget(ProgressBarWidget other)
: base(other)
{
Percentage = other.Percentage;
GetPercentage = other.GetPercentage;
IsIndeterminate = other.IsIndeterminate;
} }
public override void Draw() public override void Draw()
{ {
var rb = RenderBounds; var rb = RenderBounds;
var percentage = GetPercentage();
WidgetUtils.DrawPanel("progressbar-bg", rb); WidgetUtils.DrawPanel("progressbar-bg", rb);
Rectangle barRect = Indeterminate ? Rectangle barRect = wasIndeterminate ?
new Rectangle(rb.X + 2 + (int)(0.75*offset*(rb.Width - 4)), rb.Y + 2, (rb.Width - 4) / 4, rb.Height - 4) : new Rectangle(rb.X + 2 + (int)(0.75*offset*(rb.Width - 4)), rb.Y + 2, (rb.Width - 4) / 4, rb.Height - 4) :
new Rectangle(rb.X + 2, rb.Y + 2, Percentage * (rb.Width - 4) / 100, rb.Height - 4); new Rectangle(rb.X + 2, rb.Y + 2, percentage * (rb.Width - 4) / 100, rb.Height - 4);
if (barRect.Width > 0) if (barRect.Width > 0)
WidgetUtils.DrawPanel("progressbar-thumb", barRect); WidgetUtils.DrawPanel("progressbar-thumb", barRect);
} }
bool wasIndeterminate;
public override void Tick() public override void Tick()
{ {
if (Indeterminate) var indeterminate = IsIndeterminate();
if (indeterminate != wasIndeterminate)
offset = 0f;
if (indeterminate)
{ {
offset += tickStep; offset += tickStep;
offset = offset.Clamp(0f, 1f); offset = offset.Clamp(0f, 1f);
if (offset == 0f || offset == 1f) if (offset == 0f || offset == 1f)
tickStep *= -1; tickStep *= -1;
} }
}
public void SetIndeterminate(bool value) wasIndeterminate = indeterminate;
{
Indeterminate = value;
offset = 0f;
} }
public override Widget Clone() { return new ProgressBarWidget(this); } public override Widget Clone() { return new ProgressBarWidget(this); }

View File

@@ -43,7 +43,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
void ShowDownloadDialog() void ShowDownloadDialog()
{ {
statusLabel.GetText = () => "Initializing..."; statusLabel.GetText = () => "Initializing...";
progressBar.SetIndeterminate(true); progressBar.Indeterminate = true;
var retryButton = panel.Get<ButtonWidget>("RETRY_BUTTON"); var retryButton = panel.Get<ButtonWidget>("RETRY_BUTTON");
retryButton.IsVisible = () => false; retryButton.IsVisible = () => false;
@@ -55,9 +55,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
Action<DownloadProgressChangedEventArgs> onDownloadProgress = i => Action<DownloadProgressChangedEventArgs> onDownloadProgress = i =>
{ {
if (progressBar.Indeterminate) progressBar.Indeterminate = false;
progressBar.SetIndeterminate(false);
progressBar.Percentage = i.ProgressPercentage; progressBar.Percentage = i.ProgressPercentage;
statusLabel.GetText = () => "Downloading {1}/{2} kB ({0}%)".F(i.ProgressPercentage, i.BytesReceived / 1024, i.TotalBytesToReceive / 1024); statusLabel.GetText = () => "Downloading {1}/{2} kB ({0}%)".F(i.ProgressPercentage, i.BytesReceived / 1024, i.TotalBytesToReceive / 1024);
}; };
@@ -91,7 +89,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
// Automatically extract // Automatically extract
statusLabel.GetText = () => "Extracting..."; statusLabel.GetText = () => "Extracting...";
progressBar.SetIndeterminate(true); progressBar.Indeterminate = true;
if (InstallUtils.ExtractZip(file, dest, onExtractProgress, onError)) if (InstallUtils.ExtractZip(file, dest, onExtractProgress, onError))
{ {
Game.RunAfterTick(() => Game.RunAfterTick(() =>