diff --git a/src/LiveSplit.TotalPlaytime/TimeFormatters/DaysTimeFormatter.cs b/src/LiveSplit.TotalPlaytime/TimeFormatters/DaysTimeFormatter.cs deleted file mode 100644 index aca7c05..0000000 --- a/src/LiveSplit.TotalPlaytime/TimeFormatters/DaysTimeFormatter.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Globalization; -using System.Text; - -namespace LiveSplit.TimeFormatters; - -public class DaysTimeFormatter : ITimeFormatter -{ - public string Format(TimeSpan? time) - { - if (time.HasValue) - { - var builder = new StringBuilder(); - - if (time.Value.TotalDays >= 1) - { - builder.Append((int)time.Value.TotalDays).Append("d "); - } - - if (time.Value.TotalHours >= 1) - { - builder.Append(time.Value.ToString(@"h\:mm\:ss", CultureInfo.InvariantCulture)); - } - else - { - builder.Append(time.Value.ToString(@"m\:ss", CultureInfo.InvariantCulture)); - } - - return builder.ToString(); - } - - return "0"; - } -} diff --git a/src/LiveSplit.TotalPlaytime/TimeFormatters/TotalTimeFormatter.cs b/src/LiveSplit.TotalPlaytime/TimeFormatters/TotalTimeFormatter.cs new file mode 100644 index 0000000..87bdba9 --- /dev/null +++ b/src/LiveSplit.TotalPlaytime/TimeFormatters/TotalTimeFormatter.cs @@ -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(); + } +} diff --git a/src/LiveSplit.TotalPlaytime/UI/Components/TotalPlaytimeComponent.cs b/src/LiveSplit.TotalPlaytime/UI/Components/TotalPlaytimeComponent.cs index f58dba7..9116b1f 100644 --- a/src/LiveSplit.TotalPlaytime/UI/Components/TotalPlaytimeComponent.cs +++ b/src/LiveSplit.TotalPlaytime/UI/Components/TotalPlaytimeComponent.cs @@ -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"; @@ -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) @@ -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 @@ -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); } diff --git a/src/LiveSplit.TotalPlaytime/UI/Components/TotalPlaytimeSettings.Designer.cs b/src/LiveSplit.TotalPlaytime/UI/Components/TotalPlaytimeSettings.Designer.cs index fd02297..e841da5 100644 --- a/src/LiveSplit.TotalPlaytime/UI/Components/TotalPlaytimeSettings.Designer.cs +++ b/src/LiveSplit.TotalPlaytime/UI/Components/TotalPlaytimeSettings.Designer.cs @@ -45,6 +45,8 @@ private void InitializeComponent() this.btnColor2 = new System.Windows.Forms.Button(); this.chkTwoRows = new System.Windows.Forms.CheckBox(); this.chkShowTotalHours = new System.Windows.Forms.CheckBox(); + this.chkShowMinutes = new System.Windows.Forms.CheckBox(); + this.chkShowSeconds = new System.Windows.Forms.CheckBox(); this.tableLayoutPanel1.SuspendLayout(); this.groupBox1.SuspendLayout(); this.tableLayoutPanel2.SuspendLayout(); @@ -59,23 +61,26 @@ private void InitializeComponent() this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 29F)); this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 29F)); this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); - this.tableLayoutPanel1.Controls.Add(this.groupBox1, 0, 2); - this.tableLayoutPanel1.Controls.Add(this.groupBox2, 0, 3); + this.tableLayoutPanel1.Controls.Add(this.groupBox1, 0, 3); + this.tableLayoutPanel1.Controls.Add(this.groupBox2, 0, 4); this.tableLayoutPanel1.Controls.Add(this.cmbGradientType, 3, 0); this.tableLayoutPanel1.Controls.Add(this.label11, 0, 0); this.tableLayoutPanel1.Controls.Add(this.btnColor1, 1, 0); this.tableLayoutPanel1.Controls.Add(this.btnColor2, 2, 0); this.tableLayoutPanel1.Controls.Add(this.chkTwoRows, 0, 1); this.tableLayoutPanel1.Controls.Add(this.chkShowTotalHours, 3, 1); + this.tableLayoutPanel1.Controls.Add(this.chkShowMinutes, 0, 2); + this.tableLayoutPanel1.Controls.Add(this.chkShowSeconds, 3, 2); this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; this.tableLayoutPanel1.Location = new System.Drawing.Point(7, 7); this.tableLayoutPanel1.Name = "tableLayoutPanel1"; this.tableLayoutPanel1.RowCount = 4; this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 29F)); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 29F)); + this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 29F)); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 83F)); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 60F)); - this.tableLayoutPanel1.Size = new System.Drawing.Size(462, 225); + this.tableLayoutPanel1.Size = new System.Drawing.Size(462, 254); this.tableLayoutPanel1.TabIndex = 0; // // groupBox1 @@ -86,7 +91,7 @@ private void InitializeComponent() this.groupBox1.Location = new System.Drawing.Point(3, 61); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(456, 77); - this.groupBox1.TabIndex = 4; + this.groupBox1.TabIndex = 7; this.groupBox1.TabStop = false; this.groupBox1.Text = "Text Color"; // @@ -153,7 +158,7 @@ private void InitializeComponent() this.groupBox2.Location = new System.Drawing.Point(3, 144); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(456, 78); - this.groupBox2.TabIndex = 5; + this.groupBox2.TabIndex = 8; this.groupBox2.TabStop = false; this.groupBox2.Text = "Time Color"; // @@ -267,7 +272,7 @@ private void InitializeComponent() this.chkTwoRows.Margin = new System.Windows.Forms.Padding(7, 3, 3, 3); this.chkTwoRows.Name = "chkTwoRows"; this.chkTwoRows.Size = new System.Drawing.Size(149, 17); - this.chkTwoRows.TabIndex = 36; + this.chkTwoRows.TabIndex = 3; this.chkTwoRows.Text = "Display 2 Rows"; this.chkTwoRows.UseVisualStyleBackColor = true; // @@ -279,10 +284,34 @@ private void InitializeComponent() this.chkShowTotalHours.Margin = new System.Windows.Forms.Padding(7, 3, 3, 3); this.chkShowTotalHours.Name = "chkShowTotalHours"; this.chkShowTotalHours.Size = new System.Drawing.Size(235, 17); - this.chkShowTotalHours.TabIndex = 37; + this.chkShowTotalHours.TabIndex = 4; this.chkShowTotalHours.Text = "Show Total Hours"; this.chkShowTotalHours.UseVisualStyleBackColor = true; // + // chkShowMinutes + // + this.chkShowMinutes.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); + this.chkShowMinutes.AutoSize = true; + this.chkShowMinutes.Location = new System.Drawing.Point(7, 59); + this.chkShowMinutes.Margin = new System.Windows.Forms.Padding(7, 3, 3, 3); + this.chkShowMinutes.Name = "chkShowMinutes"; + this.chkShowMinutes.Size = new System.Drawing.Size(149, 17); + this.chkShowMinutes.TabIndex = 5; + this.chkShowMinutes.Text = "Show Minutes"; + this.chkShowMinutes.UseVisualStyleBackColor = true; + // + // chkShowSeconds + // + this.chkShowSeconds.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); + this.chkShowSeconds.AutoSize = true; + this.chkShowSeconds.Location = new System.Drawing.Point(224, 59); + this.chkShowSeconds.Margin = new System.Windows.Forms.Padding(7, 3, 3, 3); + this.chkShowSeconds.Name = "chkShowSeconds"; + this.chkShowSeconds.Size = new System.Drawing.Size(235, 17); + this.chkShowSeconds.TabIndex = 6; + this.chkShowSeconds.Text = "Show Seconds"; + this.chkShowSeconds.UseVisualStyleBackColor = true; + // // TotalPlaytimeSettings // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -290,7 +319,7 @@ private void InitializeComponent() this.Controls.Add(this.tableLayoutPanel1); this.Name = "TotalPlaytimeSettings"; this.Padding = new System.Windows.Forms.Padding(7); - this.Size = new System.Drawing.Size(476, 239); + this.Size = new System.Drawing.Size(476, 268); this.Load += new System.EventHandler(this.TotalPlaytimeSettings_Load); this.tableLayoutPanel1.ResumeLayout(false); this.tableLayoutPanel1.PerformLayout(); @@ -323,5 +352,7 @@ private void InitializeComponent() private System.Windows.Forms.Button btnColor2; private System.Windows.Forms.CheckBox chkTwoRows; private System.Windows.Forms.CheckBox chkShowTotalHours; + private System.Windows.Forms.CheckBox chkShowMinutes; + private System.Windows.Forms.CheckBox chkShowSeconds; } } diff --git a/src/LiveSplit.TotalPlaytime/UI/Components/TotalPlaytimeSettings.cs b/src/LiveSplit.TotalPlaytime/UI/Components/TotalPlaytimeSettings.cs index 8270787..2e2d8a3 100644 --- a/src/LiveSplit.TotalPlaytime/UI/Components/TotalPlaytimeSettings.cs +++ b/src/LiveSplit.TotalPlaytime/UI/Components/TotalPlaytimeSettings.cs @@ -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; } @@ -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); @@ -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) @@ -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) @@ -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)