-
Notifications
You must be signed in to change notification settings - Fork 3
/
CallbacksHandlers.cs
302 lines (243 loc) · 10.1 KB
/
CallbacksHandlers.cs
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Media;
using System.Runtime.InteropServices;
using BMDSwitcherAPI;
namespace AtemSharp
{
public partial class MainWindow : Window
{
private void OnSwitcherConnected()
{
ButtonConnect.Content = "Connected";
ButtonConnect.IsEnabled = false;
TextBoxSwitcherIPAddress.IsEnabled = false;
string productName;
bSwitcher.GetProductName(out productName);
TextBoxProductName.Text = productName;
GridProgram.IsEnabled = true;
GridPreview.IsEnabled = true;
SliderTransition.IsEnabled = true;
StackPanelTransition.IsEnabled = true;
bSwitcher.AddCallback(bSwitcherCallback);
GetInputs();
GetMixEffectBlock();
}
private void GetInputs()
{
IBMDSwitcherInputIterator inputIterator = null;
IntPtr inputIteratorPointer;
Guid inputIteratorInterfaceID = typeof(IBMDSwitcherInputIterator).GUID;
bSwitcher.CreateIterator(ref inputIteratorInterfaceID, out inputIteratorPointer);
if (inputIteratorPointer == null)
return;
inputIterator = (IBMDSwitcherInputIterator)Marshal.GetObjectForIUnknown(inputIteratorPointer);
if (inputIterator == null)
return;
IBMDSwitcherInput input;
inputIterator.Next(out input);
while (input != null)
{
long inputId;
string inputName;
input.GetInputId(out inputId);
input.GetShortName(out inputName);
bInputNamesById.Add(inputId, inputName);
bInputIdsByName.Add(inputName, inputId);
InputCallback inputCallback = new InputCallback(input);
input.AddCallback(inputCallback);
inputCallback.LongNameChanged +=
() => Dispatcher.Invoke((Action)(() => OnLongNameChanged()));
bInputCallbacks.Add(inputCallback);
inputIterator.Next(out input);
}
}
private void GetMixEffectBlock()
{
bSwitcherMixEffectBlock = null;
IBMDSwitcherMixEffectBlockIterator mixEffectBlockIterator = null;
IntPtr mixEffectBlockIteratorPointer;
Guid mixEffectBlockIteratorInterfaceID = typeof(IBMDSwitcherMixEffectBlockIterator).GUID;
bSwitcher.CreateIterator(ref mixEffectBlockIteratorInterfaceID, out mixEffectBlockIteratorPointer);
if (mixEffectBlockIteratorPointer == null)
return;
mixEffectBlockIterator =
(IBMDSwitcherMixEffectBlockIterator)Marshal.GetObjectForIUnknown(mixEffectBlockIteratorPointer);
if (mixEffectBlockIterator == null)
return;
mixEffectBlockIterator.Next(out bSwitcherMixEffectBlock);
if (bSwitcherMixEffectBlock == null)
return;
bSwitcherMixEffectBlock.AddCallback(bMixEffectBlockCallback);
}
private void OnSwitcherDisconnected()
{
ButtonConnect.Content = "Connect";
ButtonConnect.IsEnabled = true;
TextBoxSwitcherIPAddress.IsEnabled = true;
TextBoxProductName.Text = "";
GridProgram.IsEnabled = false;
GridPreview.IsEnabled = false;
SliderTransition.IsEnabled = false;
StackPanelTransition.IsEnabled = false;
foreach (InputCallback inputCallback in bInputCallbacks)
{
inputCallback.Input.RemoveCallback(inputCallback);
inputCallback.LongNameChanged -=
() => Dispatcher.Invoke((Action)(() => OnLongNameChanged()));
}
bInputNamesById.Clear();
bInputIdsByName.Clear();
bInputCallbacks.Clear();
if (bSwitcherMixEffectBlock != null)
{
bSwitcherMixEffectBlock.RemoveCallback(bMixEffectBlockCallback);
bSwitcherMixEffectBlock = null;
}
if (bSwitcher != null)
{
bSwitcher.RemoveCallback(bSwitcherCallback);
bSwitcher = null;
}
}
private void OnProgramInputChanged()
{
long programId;
bSwitcherMixEffectBlock.GetInt
(_BMDSwitcherMixEffectBlockPropertyId.bmdSwitcherMixEffectBlockPropertyIdProgramInput, out programId);
foreach (Button button in GridProgram.Children)
{
if (button.Background == Brushes.Coral)
button.ClearValue(Button.BackgroundProperty);
if (button.Tag.ToString() == bInputNamesById[programId])
button.Background = Brushes.Coral;
}
}
private void OnPreviewInputChanged()
{
long previewId;
bSwitcherMixEffectBlock.GetInt
(_BMDSwitcherMixEffectBlockPropertyId.bmdSwitcherMixEffectBlockPropertyIdPreviewInput, out previewId);
SolidColorBrush brush = bInTransition ? Brushes.Coral : Brushes.LightGreen;
foreach (Button button in GridPreview.Children)
{
if (button.Background == brush)
button.ClearValue(Button.BackgroundProperty);
if (button.Tag.ToString() == bInputNamesById[previewId])
button.Background = brush;
}
}
private void OnTransitionFramesRemainingChanged()
{
}
private void OnTransitionPositionChanged()
{
if (!bMouseDown)
{
double position;
bSwitcherMixEffectBlock.GetFloat
(_BMDSwitcherMixEffectBlockPropertyId.bmdSwitcherMixEffectBlockPropertyIdTransitionPosition, out position);
SliderTransition.Value = position;
if (position == 1)
SliderTransition.IsDirectionReversed = !SliderTransition.IsDirectionReversed;
}
}
private void OnInTransitionChanged()
{
int inTransition;
bSwitcherMixEffectBlock.GetFlag
(_BMDSwitcherMixEffectBlockPropertyId.bmdSwitcherMixEffectBlockPropertyIdInTransition, out inTransition);
if (inTransition == 1)
{
bInTransition = true;
ButtonAuto.Background = Brushes.Coral;
}
else
{
bInTransition = false;
ButtonAuto.ClearValue(Button.BackgroundProperty);
}
OnPreviewInputChanged();
}
private void OnLongNameChanged()
{
}
private void ButtonConnect_Click(object sender, RoutedEventArgs e)
{
bSwitcherDiscovery = new CBMDSwitcherDiscovery();
if (bSwitcherDiscovery == null)
{
MessageBox.Show("Could not create Switcher Discovery Instance.\n"
+ "ATEM Switcher Software may not be installed.", "Error");
return;
}
string deviceAddress = TextBoxSwitcherIPAddress.Text;
_BMDSwitcherConnectToFailure failReason = 0;
try
{
bSwitcherDiscovery.ConnectTo(deviceAddress, out bSwitcher, out failReason);
}
catch (COMException)
{
switch (failReason)
{
case _BMDSwitcherConnectToFailure.bmdSwitcherConnectToFailureNoResponse:
MessageBox.Show("No response from Switcher", "Error");
break;
case _BMDSwitcherConnectToFailure.bmdSwitcherConnectToFailureIncompatibleFirmware:
MessageBox.Show("Switcher has incompatible firmware", "Error");
break;
default:
MessageBox.Show("Connection failed for unknown reason", "Error");
break;
}
return;
}
OnSwitcherConnected();
}
private void ButtonProgram_Click(object sender, RoutedEventArgs e)
{
string inputName = ((Button)sender).Tag.ToString();
long inputId = bInputIdsByName[inputName];
bSwitcherMixEffectBlock.SetInt
(_BMDSwitcherMixEffectBlockPropertyId.bmdSwitcherMixEffectBlockPropertyIdProgramInput, inputId);
}
private void ButtonPreview_Click(object sender, RoutedEventArgs e)
{
string inputName = ((Button)sender).Tag.ToString();
long inputId = bInputIdsByName[inputName];
bSwitcherMixEffectBlock.SetInt
(_BMDSwitcherMixEffectBlockPropertyId.bmdSwitcherMixEffectBlockPropertyIdPreviewInput, inputId);
}
private void SliderTransition_MouseDown(object sender, MouseButtonEventArgs e)
{
bMouseDown = true;
}
private void SliderTransition_MouseUp(object sender, MouseButtonEventArgs e)
{
bMouseDown = false;
}
private void SliderTransition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (bMouseDown)
{
double position = ((Slider)sender).Value;
if (position == 1)
((Slider)sender).IsDirectionReversed = !((Slider)sender).IsDirectionReversed;
bSwitcherMixEffectBlock.SetFloat
(_BMDSwitcherMixEffectBlockPropertyId.bmdSwitcherMixEffectBlockPropertyIdTransitionPosition, position);
}
}
private void ButtonAuto_Click(object sender, RoutedEventArgs e)
{
bSwitcherMixEffectBlock.PerformAutoTransition();
}
private void ButtonCut_Click(object sender, RoutedEventArgs e)
{
bSwitcherMixEffectBlock.PerformCut();
}
}
}