-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathVariableOffsetTimer.cs
More file actions
210 lines (173 loc) · 8.34 KB
/
VariableOffsetTimer.cs
File metadata and controls
210 lines (173 loc) · 8.34 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
using System;
using System.Windows.Forms;
using System.Threading;
namespace FlowTimer {
public struct VariableInfo {
public uint Frame;
public double FPS;
public int Offset;
public uint Interval;
public uint NumBeeps;
}
public class VariableOffsetTimer : BaseTimer {
public TextBox TextBoxFrame;
public ComboBox ComboBoxFPS;
public TextBox TextBoxOffset;
public TextBox TextBoxInterval;
public TextBox TextBoxBeeps;
public Button ButtonSubmit;
public VariableInfo Info;
public bool Submitted;
public double CurrentOffset;
public double Adjusted;
public VariableOffsetTimer(TabPage tab, params Control[] copyControls) : base(tab, null, copyControls) {
TextBoxFrame = FlowTimer.MainForm.TextBoxFrame;
ComboBoxFPS = FlowTimer.MainForm.ComboBoxFPS;
TextBoxOffset = FlowTimer.MainForm.TextBoxOffset;
TextBoxInterval = FlowTimer.MainForm.TextBoxInterval;
TextBoxBeeps = FlowTimer.MainForm.TextBoxBeeps;
ButtonSubmit = FlowTimer.MainForm.ButtonSubmit;
TimerCallback = TimerCallbackFn; // c# is silly!!
TextBoxFrame.KeyDown += (sender, e) => { if(e.KeyCode == Keys.Enter && FlowTimer.MainForm.ButtonSubmit.Enabled) { Submit(); e.SuppressKeyPress = true; } };
ComboBoxFPS.SelectedIndexChanged += (sender, e) => FlowTimer.Settings.VariableFPS = FlowTimer.MainForm.ComboBoxFPS.SelectedItem as string;
TextBoxOffset.TextChanged += (sender, e) => FlowTimer.Settings.VariableOffset = FlowTimer.MainForm.TextBoxOffset.Text;
TextBoxInterval.TextChanged += (sender, e) => FlowTimer.Settings.VariableInterval = FlowTimer.MainForm.TextBoxInterval.Text;
TextBoxBeeps.TextChanged += (sender, e) => FlowTimer.Settings.VariableNumBeeps = FlowTimer.MainForm.TextBoxBeeps.Text;
}
public override void OnInit() {
ComboBoxFPS.SelectedItem = FlowTimer.Settings.VariableFPS;
TextBoxOffset.Text = FlowTimer.Settings.VariableOffset;
TextBoxInterval.Text = FlowTimer.Settings.VariableInterval;
TextBoxBeeps.Text = FlowTimer.Settings.VariableNumBeeps;
}
public override void OnLoad() {
base.OnLoad();
FlowTimer.ResizeForm(FlowTimer.MainForm.Width, 211);
FlowTimer.MainForm.ButtonStart.Enabled = true;
FlowTimer.MainForm.ButtonStop.Enabled = true;
FlowTimer.MainForm.LabelTimer.Text = 0.0.ToFormattedString();
OnTimerStop();
}
public override void OnTimerStart() {
CurrentOffset = double.MaxValue;
Submitted = false;
TextBoxFrame.Enabled = true;
TextBoxFrame.Focus();
Adjusted = 0;
}
public override void OnVisualTimerStart() {
}
public override void OnTimerStop() {
Submitted = false;
TextBoxFrame.Enabled = false;
TextBoxFrame.Text = "";
EnableControls(true);
FlowTimer.MainForm.LabelTimer.Text = 0.0.ToFormattedString();
FlowTimer.MainForm.LabelTimer.Focus();
}
public override void OnKeyEvent(Keys key) {
if(FlowTimer.Settings.AddFrame.IsPressed(key) && FlowTimer.MainForm.ButtonPlus.Enabled) {
ChangeAudio(1);
} else if(FlowTimer.Settings.SubFrame.IsPressed(key) && FlowTimer.MainForm.ButtonMinus.Enabled) {
ChangeAudio(-1);
} else if(FlowTimer.Settings.Undo.IsPressed(key) && FlowTimer.MainForm.ButtonUndo.Enabled) {
Undo();
}
}
public override void OnBeepSoundChange() {
}
public override void OnBeepVolumeChange() {
}
public double TimerCallbackFn(double start) {
OnDataChange();
double ret = Math.Min(Math.Max((Win32.GetTime() - start) / 1000.0, 0.001), CurrentOffset);
if(ret == CurrentOffset) ret = 0.0;
return ret;
}
public void OnDataChange() {
TimerError error = GetVariableInfo(out Info);
double currentTime = error == TimerError.NoError ? double.Parse(FlowTimer.MainForm.LabelTimer.Text) : 0;
FlowTimer.MainForm.ButtonSubmit.Enabled = error == TimerError.NoError && !Submitted && FlowTimer.IsTimerRunning && Info.Frame / Info.FPS + Info.Offset / 1000.0f >= currentTime + (Info.Interval * (Info.NumBeeps - 1) / 1000.0f);
FlowTimer.MainForm.ButtonUndo.Enabled = Submitted && FlowTimer.IsTimerRunning;
bool canAdjust = Submitted && currentTime < CurrentOffset - Info.Interval * (Info.NumBeeps - 1) / 1000.0f - 0.05;
FlowTimer.MainForm.ButtonPlus.Enabled = canAdjust;
FlowTimer.MainForm.ButtonMinus.Enabled = canAdjust;
}
public void Submit() {
GetVariableInfo(out Info);
double now = Win32.GetTime();
double offset = (Info.Frame / Info.FPS * 1000.0f) - (now - FlowTimer.TimerStart) + Info.Offset + Adjusted;
FlowTimer.UpdatePCM(new double[] { offset }, Info.Interval, Info.NumBeeps, false);
FlowTimer.AudioContext.QueueAudio(FlowTimer.PCM);
ButtonSubmit.Enabled = false;
CurrentOffset = Info.Frame / Info.FPS + (Info.Offset + Adjusted) / 1000.0f;
Submitted = true;
EnableControls(false);
FlowTimer.MainForm.TextBoxFrame.Enabled = false;
}
public void Undo() {
Submitted = false;
EnableControls(true);
CurrentOffset = double.MaxValue;
Adjusted = 0;
FlowTimer.AudioContext.ClearQueuedAudio();
new Thread(() => {
Thread.Sleep(50);
MethodInvoker inv = delegate {
FlowTimer.MainForm.TextBoxFrame.Text = "";
FlowTimer.MainForm.TextBoxFrame.Enabled = true;
FlowTimer.MainForm.TextBoxFrame.Focus();
};
FlowTimer.MainForm.Invoke(inv);
}).Start();
}
public void ChangeAudio(int numFrames) {
FlowTimer.AudioContext.ClearQueuedAudio();
double amount = numFrames * 1000.0 / Info.FPS;
Adjusted += amount;
Submit();
int numFramesAdjusted = (int) Math.Round(Adjusted / 1000.0 * Info.FPS);
GetVariableInfo(out VariableInfo info);
TextBoxFrame.Text = info.Frame.ToString();
if(numFramesAdjusted != 0) {
TextBoxFrame.Text += numFramesAdjusted.ToString("+#;-#");
}
}
public void EnableControls(bool enabled) {
ComboBoxFPS.Enabled = enabled;
TextBoxOffset.Enabled = enabled;
TextBoxInterval.Enabled = enabled;
TextBoxBeeps.Enabled = enabled;
}
public TimerError GetVariableInfo(out VariableInfo info) {
info = new VariableInfo();
string frameText = FlowTimer.MainForm.TextBoxFrame.Text;
if(frameText.Contains("+") || frameText.Contains("-")) frameText = frameText.Substring(0, Math.Max(frameText.IndexOf("+"), frameText.IndexOf("-")));
if(!uint.TryParse(frameText, out info.Frame)) {
return TimerError.InvalidFrame;
}
if(!double.TryParse(FlowTimer.MainForm.ComboBoxFPS.SelectedItem as string, out info.FPS)) {
return TimerError.InvalidFPS;
}
if(!int.TryParse(FlowTimer.MainForm.TextBoxOffset.Text, out info.Offset)) {
return TimerError.InvalidOffset;
}
if(!uint.TryParse(FlowTimer.MainForm.TextBoxInterval.Text, out info.Interval)) {
return TimerError.InvalidInterval;
}
if(!uint.TryParse(FlowTimer.MainForm.TextBoxBeeps.Text, out info.NumBeeps)) {
return TimerError.InvalidNumBeeps;
}
if(info.Interval >= ushort.MaxValue << 9) {
return TimerError.InvalidInterval;
}
if(info.NumBeeps >= ushort.MaxValue << 9) {
return TimerError.InvalidNumBeeps;
}
if(info.Frame >= ushort.MaxValue << 8) {
return TimerError.InvalidFrame;
}
return TimerError.NoError;
}
}
}