-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPreviousSegment.cs
More file actions
231 lines (198 loc) · 9.77 KB
/
PreviousSegment.cs
File metadata and controls
231 lines (198 loc) · 9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using LiveSplit.Model;
using LiveSplit.Model.Comparisons;
using LiveSplit.TimeFormatters;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;
namespace LiveSplit.UI.Components
{
public class PreviousSegment : IComponent
{
protected InfoTimeComponent InternalComponent { get; set; }
public PreviousSegmentSettings Settings { get; set; }
protected DeltaTimeFormatter DeltaFormatter { get; set; }
protected PossibleTimeSaveFormatter TimeSaveFormatter { get; set; }
public float PaddingTop => InternalComponent.PaddingTop;
public float PaddingLeft => InternalComponent.PaddingLeft;
public float PaddingBottom => InternalComponent.PaddingBottom;
public float PaddingRight => InternalComponent.PaddingRight;
private string previousNameText { get; set; }
public IDictionary<string, Action> ContextMenuControls => null;
public PreviousSegment(LiveSplitState state)
{
DeltaFormatter = new DeltaTimeFormatter();
TimeSaveFormatter = new PossibleTimeSaveFormatter();
Settings = new PreviousSegmentSettings()
{
CurrentState = state
};
InternalComponent = new InfoTimeComponent(null, null, DeltaFormatter);
state.ComparisonRenamed += state_ComparisonRenamed;
}
void state_ComparisonRenamed(object sender, EventArgs e)
{
var args = (RenameEventArgs)e;
if (Settings.Comparison == args.OldName)
{
Settings.Comparison = args.NewName;
((LiveSplitState)sender).Layout.HasChanged = true;
}
}
private void PrepareDraw(LiveSplitState state)
{
InternalComponent.DisplayTwoRows = Settings.Display2Rows;
InternalComponent.NameLabel.HasShadow
= InternalComponent.ValueLabel.HasShadow
= state.LayoutSettings.DropShadows;
InternalComponent.NameLabel.ForeColor = Settings.OverrideTextColor ? Settings.TextColor : state.LayoutSettings.TextColor;
}
private void DrawBackground(Graphics g, LiveSplitState state, float width, float height)
{
if (Settings.BackgroundColor.A > 0
|| Settings.BackgroundGradient != GradientType.Plain
&& Settings.BackgroundColor2.A > 0)
{
var gradientBrush = new LinearGradientBrush(
new PointF(0, 0),
Settings.BackgroundGradient == GradientType.Horizontal
? new PointF(width, 0)
: new PointF(0, height),
Settings.BackgroundColor,
Settings.BackgroundGradient == GradientType.Plain
? Settings.BackgroundColor
: Settings.BackgroundColor2);
g.FillRectangle(gradientBrush, 0, 0, width, height);
}
}
public void DrawVertical(Graphics g, LiveSplitState state, float width, Region clipRegion)
{
DrawBackground(g, state, width, VerticalHeight);
PrepareDraw(state);
InternalComponent.DrawVertical(g, state, width, clipRegion);
}
public void DrawHorizontal(Graphics g, LiveSplitState state, float height, Region clipRegion)
{
DrawBackground(g, state, HorizontalWidth, height);
PrepareDraw(state);
InternalComponent.DrawHorizontal(g, state, height, clipRegion);
}
public float VerticalHeight => InternalComponent.VerticalHeight;
public float MinimumWidth => InternalComponent.MinimumWidth;
public float HorizontalWidth => InternalComponent.HorizontalWidth;
public float MinimumHeight => InternalComponent.MinimumHeight;
public string ComponentName
=> "Previous Segment" + (Settings.Comparison == "Current Comparison"
? ""
: " (" + CompositeComparisons.GetShortComparisonName(Settings.Comparison) + ")");
public Control GetSettingsControl(LayoutMode mode)
{
Settings.Mode = mode;
return Settings;
}
public void SetSettings(System.Xml.XmlNode settings)
{
Settings.SetSettings(settings);
}
public System.Xml.XmlNode GetSettings(System.Xml.XmlDocument document)
{
return Settings.GetSettings(document);
}
public TimeSpan? GetPossibleTimeSave(LiveSplitState state, int splitIndex, string comparison)
{
var prevTime = TimeSpan.Zero;
TimeSpan? bestSegments = state.Run[splitIndex].BestSegmentTime[state.CurrentTimingMethod];
while (splitIndex > 0 && bestSegments != null)
{
var splitTime = state.Run[splitIndex - 1].Comparisons[comparison][state.CurrentTimingMethod];
if (splitTime != null)
{
prevTime = splitTime.Value;
break;
}
else
{
splitIndex--;
bestSegments += state.Run[splitIndex].BestSegmentTime[state.CurrentTimingMethod];
}
}
var time = state.Run[splitIndex].Comparisons[comparison][state.CurrentTimingMethod] - prevTime - bestSegments;
if (time < TimeSpan.Zero)
time = TimeSpan.Zero;
return time;
}
public void Update(IInvalidator invalidator, LiveSplitState state, float width, float height, LayoutMode mode)
{
var comparison = Settings.Comparison == "Current Comparison" ? state.CurrentComparison : Settings.Comparison;
if (!state.Run.Comparisons.Contains(comparison))
comparison = state.CurrentComparison;
var comparisonName = CompositeComparisons.GetShortComparisonName(comparison);
var componentName = "Previous Segment" + (Settings.Comparison == "Current Comparison" ? "" : " (" + comparisonName + ")");
InternalComponent.LongestString = componentName;
InternalComponent.InformationName = componentName;
DeltaFormatter.Accuracy = Settings.DeltaAccuracy;
DeltaFormatter.DropDecimals = Settings.DropDecimals;
TimeSaveFormatter.Accuracy = Settings.TimeSaveAccuracy;
TimeSpan? timeChange = null;
TimeSpan? timeSave = null;
var liveSegment = LiveSplitStateHelper.CheckLiveDelta(state, false, comparison, state.CurrentTimingMethod, false);
if (state.CurrentPhase != TimerPhase.NotRunning)
{
if (liveSegment != null)
{
timeChange = liveSegment;
timeSave = GetPossibleTimeSave(state, state.CurrentSplitIndex, comparison);
InternalComponent.InformationName = "Live Segment" + (Settings.Comparison == "Current Comparison" ? "" : " (" + comparisonName + ")");
}
else if (state.CurrentSplitIndex > 0)
{
timeChange = LiveSplitStateHelper.GetPreviousSegmentDelta(state, state.CurrentSplitIndex - 1, comparison, state.CurrentTimingMethod);
timeSave = GetPossibleTimeSave(state, state.CurrentSplitIndex - 1, comparison);
}
if (timeChange != null)
{
if (liveSegment != null)
InternalComponent.ValueLabel.ForeColor = LiveSplitStateHelper.GetSplitColor(state, timeChange, state.CurrentSplitIndex, false, false, comparison, state.CurrentTimingMethod).Value;
else
InternalComponent.ValueLabel.ForeColor = LiveSplitStateHelper.GetSplitColor(state, timeChange.Value, state.CurrentSplitIndex - 1, false, true, comparison, state.CurrentTimingMethod).Value;
}
else
{
var color = LiveSplitStateHelper.GetSplitColor(state, null, state.CurrentSplitIndex - 1, true, true, comparison, state.CurrentTimingMethod);
if (color == null)
color = Settings.OverrideTextColor ? Settings.TextColor : state.LayoutSettings.TextColor;
InternalComponent.ValueLabel.ForeColor = color.Value;
}
}
else
{
InternalComponent.ValueLabel.ForeColor = Settings.OverrideTextColor ? Settings.TextColor : state.LayoutSettings.TextColor;
}
if (InternalComponent.InformationName != previousNameText)
{
InternalComponent.AlternateNameText.Clear();
if (liveSegment != null)
{
InternalComponent.AlternateNameText.Add("Live Segment");
InternalComponent.AlternateNameText.Add("Live Seg.");
}
else
{
InternalComponent.AlternateNameText.Add("Previous Segment");
InternalComponent.AlternateNameText.Add("Prev. Segment");
InternalComponent.AlternateNameText.Add("Prev. Seg.");
}
previousNameText = InternalComponent.InformationName;
}
InternalComponent.InformationValue = DeltaFormatter.Format(timeChange)
+ (Settings.ShowPossibleTimeSave ? " / " + TimeSaveFormatter.Format(timeSave) : "");
InternalComponent.Update(invalidator, state, width, height, mode);
}
public void Dispose()
{
}
public int GetSettingsHashCode() => Settings.GetSettingsHashCode();
}
}