Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions src/LiveSplit.TotalPlaytime/TimeFormatters/DaysTimeFormatter.cs

This file was deleted.

82 changes: 82 additions & 0 deletions src/LiveSplit.TotalPlaytime/TimeFormatters/TotalTimeFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Globalization;
using System.Text;

namespace LiveSplit.TimeFormatters;

public class TotalTimeFormatter : ITimeFormatter
{
public bool ShowTotalHours { get; set; }
public bool ShowMinutes { get; set; }
public bool ShowSeconds { get; set; }

public TotalTimeFormatter()
{
ShowTotalHours = false;
ShowMinutes = true;
ShowSeconds = true;
}

public string Format(TimeSpan? time)
{
if (!time.HasValue)
{
return "0";
}

var timeValue = time.Value;

if (!ShowMinutes && timeValue.TotalHours <= 1)
{
return "0h";
}

var builder = new StringBuilder();

// days, hours
if (timeValue.TotalHours >= 1)
{
if (timeValue.TotalDays >= 1)
{
if (ShowTotalHours)
{
builder.Append((int)timeValue.TotalHours);
}
else
{
builder.Append($"{(int)timeValue.TotalDays}d {timeValue.Hours}");
}
}
else
{
builder.Append(timeValue.Hours);
}
}

// minutes, seconds
if (ShowMinutes)
{
var format = new StringBuilder();

if (timeValue.TotalHours >= 1)
{
builder.Append(timeValue.ToString(@"\:mm", CultureInfo.InvariantCulture));
}
else
{
builder.Append($"{timeValue.Minutes}");
}

if (ShowSeconds)
{
builder.Append(timeValue.ToString(@"\:ss", CultureInfo.InvariantCulture));
}
}
else
{
builder.Append("h");
}

return builder.ToString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace LiveSplit.UI.Components;

public class TotalPlaytimeComponent : IComponent
{
protected ITimeFormatter HoursTimeFormatter { get; set; }
protected ITimeFormatter DaysTimeFormatter { get; set; }
protected TotalTimeFormatter Formatter { get; set; }
protected InfoTimeComponent InternalComponent { get; set; }
protected TotalPlaytimeSettings Settings { get; set; }

protected TimerPhase LastPhase { get; set; }
protected int LastAttemptCount { get; set; }
protected IRun LastRun { get; set; }
protected GraphicsCache Cache { get; set; }

public string ComponentName => "Total Playtime";

Expand All @@ -43,13 +43,14 @@ public class TotalPlaytimeComponent : IComponent

public TotalPlaytimeComponent(LiveSplitState state)
{
HoursTimeFormatter = new RegularTimeFormatter();
DaysTimeFormatter = new DaysTimeFormatter();
InternalComponent = new InfoTimeComponent("Total Playtime", TimeSpan.Zero, DaysTimeFormatter);
Formatter = new TotalTimeFormatter();
InternalComponent = new InfoTimeComponent("Total Playtime", TimeSpan.Zero, Formatter);
Settings = new TotalPlaytimeSettings()
{
CurrentState = state
};

Cache = new GraphicsCache();
}

private void DrawBackground(Graphics g, LiveSplitState state, float width, float height)
Expand Down Expand Up @@ -152,8 +153,6 @@ public TimeSpan CalculateTotalPlaytime(LiveSplitState state)

public void Update(IInvalidator invalidator, LiveSplitState state, float width, float height, LayoutMode mode)
{
InternalComponent.Formatter = Settings.ShowTotalHours ? HoursTimeFormatter : DaysTimeFormatter;

if (LastAttemptCount != state.Run.AttemptHistory.Count
|| LastPhase != state.CurrentPhase
|| LastRun != state.Run
Expand All @@ -167,6 +166,17 @@ public void Update(IInvalidator invalidator, LiveSplitState state, float width,
LastRun = state.Run;
}

Cache.Restart();
Cache["ShowTotalHours"] = Formatter.ShowTotalHours = Settings.ShowTotalHours;
Cache["ShowMinutes"] = Formatter.ShowMinutes = Settings.ShowMinutes;
Cache["ShowSeconds"] = Formatter.ShowSeconds = Settings.ShowSeconds;

if (Cache.HasChanged)
{
InternalComponent.Formatter = null;
InternalComponent.Formatter = Formatter;
}

InternalComponent.Update(invalidator, state, width, height, mode);
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public string GradientString
public LiveSplitState CurrentState { get; set; }
public bool Display2Rows { get; set; }
public bool ShowTotalHours { get; set; }
public bool ShowMinutes { get; set; }
public bool ShowSeconds { get; set; }

public LayoutMode Mode { get; set; }

Expand All @@ -42,6 +44,8 @@ public TotalPlaytimeSettings()
BackgroundGradient = GradientType.Plain;
Display2Rows = false;
ShowTotalHours = false;
ShowMinutes = true;
ShowSeconds = true;

chkOverrideTextColor.DataBindings.Add("Checked", this, "OverrideTextColor", false, DataSourceUpdateMode.OnPropertyChanged);
btnTextColor.DataBindings.Add("BackColor", this, "TextColor", false, DataSourceUpdateMode.OnPropertyChanged);
Expand All @@ -51,6 +55,13 @@ public TotalPlaytimeSettings()
btnColor1.DataBindings.Add("BackColor", this, "BackgroundColor", false, DataSourceUpdateMode.OnPropertyChanged);
btnColor2.DataBindings.Add("BackColor", this, "BackgroundColor2", false, DataSourceUpdateMode.OnPropertyChanged);
chkShowTotalHours.DataBindings.Add("Checked", this, "ShowTotalHours", false, DataSourceUpdateMode.OnPropertyChanged);
chkShowMinutes.DataBindings.Add("Checked", this, "ShowMinutes", false, DataSourceUpdateMode.OnPropertyChanged);
chkShowSeconds.DataBindings.Add("Checked", this, "ShowSeconds", false, DataSourceUpdateMode.OnPropertyChanged);
}

private void chkShowMinutes_CheckedChanged(object sender, EventArgs e)
{
chkShowSeconds.Enabled = chkShowMinutes.Checked;
}

private void chkOverrideTimeColor_CheckedChanged(object sender, EventArgs e)
Expand Down Expand Up @@ -101,6 +112,8 @@ public void SetSettings(XmlNode node)
GradientString = SettingsHelper.ParseString(element["BackgroundGradient"]);
Display2Rows = SettingsHelper.ParseBool(element["Display2Rows"]);
ShowTotalHours = SettingsHelper.ParseBool(element["ShowTotalHours"], false);
ShowMinutes= SettingsHelper.ParseBool(element["ShowMinutes"], true);
ShowSeconds = SettingsHelper.ParseBool(element["ShowSeconds"], true);
}

public XmlNode GetSettings(XmlDocument document)
Expand All @@ -126,7 +139,9 @@ private int CreateSettingsNode(XmlDocument document, XmlElement parent)
SettingsHelper.CreateSetting(document, parent, "BackgroundColor2", BackgroundColor2) ^
SettingsHelper.CreateSetting(document, parent, "BackgroundGradient", BackgroundGradient) ^
SettingsHelper.CreateSetting(document, parent, "Display2Rows", Display2Rows) ^
SettingsHelper.CreateSetting(document, parent, "ShowTotalHours", ShowTotalHours);
SettingsHelper.CreateSetting(document, parent, "ShowTotalHours", ShowTotalHours) ^
SettingsHelper.CreateSetting(document, parent, "ShowMinutes", ShowMinutes) ^
SettingsHelper.CreateSetting(document, parent, "ShowSeconds", ShowSeconds);
}

private void ColorButtonClick(object sender, EventArgs e)
Expand Down