-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScoreboardLoader.xaml.cs
207 lines (180 loc) · 7.83 KB
/
ScoreboardLoader.xaml.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml;
namespace vMixWpfScoreboardLoader
{
/// <summary>
/// Логика взаимодействия для UserControl1.xaml
/// </summary>
public partial class ScoreboardLoader : vMixInterop.vMixWPFUserControl
{
SettingsWindow _settings = new SettingsWindow();
Dictionary<string, int> _teamSet = new Dictionary<string, int>();
string _titlePath;
public ScoreboardLoader()
{
InitializeComponent();
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.Filter = "XAML Title|*.xaml";
var result = ofd.ShowDialog();
if (result.HasValue && result.Value)
{
using (var fs = File.OpenRead(ofd.FileName))
{
_titlePath = Path.GetDirectoryName(ofd.FileName);
XmlDocument document = new XmlDocument();
document.Load(fs);
for (int i = document.DocumentElement.Attributes.Count - 1; i >= 0; i += -1)
{
XmlAttribute attribute = document.DocumentElement.Attributes[i];
if (attribute.Name == "x:Class")
{
document.DocumentElement.Attributes.RemoveAt(i);
}
}
using (var ms = new MemoryStream())
{
document.Save(ms);
ms.Position = 0;
var control = (FrameworkElement)XamlReader.Load(ms);
foreach (var item in control.Resources.Keys)
this.Resources.Add(item, control.FindResource(item));
control.Resources.Clear();
foreach (var item in control.Triggers)
this.Triggers.Add(item);
control.Triggers.Clear();
container.Children.Add(control);
if (control is UserControl)
{
var _c = control as UserControl;
ProcessPanel(_c.Content as Panel);
}
}
}
}
}
private void ProcessPanel(Panel p)
{
var _tag = (string)p.Tag;
var props = ParseTag(_tag);
if (props.Count > 0)
{
if (props.ContainsKey("Type") && props["Type"] == "Graphics" && props.ContainsKey("Image"))
p.Background = new ImageBrush(new BitmapImage(new Uri(Path.Combine(_titlePath, props["Image"]))));
}
foreach (FrameworkElement item in p.Children)
{
ProcessFrameworkElement(item);
}
}
private void ProcessFrameworkElement(FrameworkElement item)
{
if (item is Decorator)
ProcessFrameworkElement((item as Decorator).Child as FrameworkElement);
if (item is vMixTitleLibrary.TextBlockDesign || item is TextBlock)
ProcessTextBlock(item);
if (!string.IsNullOrEmpty(item.Name))
RegisterName(item.Name, item);
if (item is Panel)
ProcessPanel(item as Panel);
}
private void ProcessTextBlock(FrameworkElement item)
{
var _tag = (string)item.Tag;
var props = ParseTag(_tag);
if (props.Count > 0)
{
if (props.ContainsKey("Type"))
{
switch (props["Type"])
{
case "Team":
case "Score":
var key = props["Number"];
if (!_teamSet.ContainsKey(key))
{
var _data = new Data.TeamData();
if (props.ContainsKey("Type") && props["Type"] == "Team" && props.ContainsKey("Description"))
_data.Description = props["Description"];
BindingOperations.SetBinding(item, item is TextBlock ? TextBlock.TextProperty : vMixTitleLibrary.TextBlockDesign.TextProperty, new Binding(props["Type"] == "Team" ? "Name" : "Score") { Source = _data, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
_settings.Teams.Add(_data);
_teamSet.Add(key, _settings.Teams.Count - 1);
}
else
BindingOperations.SetBinding(item, item is TextBlock ? TextBlock.TextProperty : vMixTitleLibrary.TextBlockDesign.TextProperty, new Binding(props["Type"] == "Team" ? "Name" : "Score") { Source = _settings.Teams[_teamSet[key]], UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
if (props.ContainsKey("Type") && props["Type"] == "Team" && props.ContainsKey("Description"))
_settings.Teams[_teamSet[key]].Description = props["Description"];
if (props.ContainsKey("Type") && props["Type"] == "Team" && props.ContainsKey("Default"))
_settings.Teams[_teamSet[key]].Name = props["Default"];
break;
case "Stopwatch":
var _sdata = new Data.StopwatchData();
BindingOperations.SetBinding(item, item is TextBlock ? TextBlock.TextProperty : vMixTitleLibrary.TextBlockDesign.TextProperty, new Binding("Time") { Source = _sdata, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
_settings.Teams.Add(_sdata);
break;
}
}
}
}
Dictionary<string, string> ParseTag(string tag)
{
var result = new Dictionary<string, string>();
if (!string.IsNullOrEmpty(tag))
{
var tags = tag.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();
foreach (var item in tags)
{
var prop = item.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();
result.Add(prop[0], prop[1]);
}
}
return result;
}
public void Close()
{
//throw new NotImplementedException();
}
public TimeSpan GetDuration()
{
return TimeSpan.FromSeconds(1);
//throw new NotImplementedException();
}
public TimeSpan GetPosition()
{
return TimeSpan.FromSeconds(0);
//throw new NotImplementedException();
}
public void Load(int width, int height)
{
//throw new NotImplementedException();
}
public void Pause()
{
//throw new NotImplementedException();
}
public void Play()
{
//throw new NotImplementedException();
}
public void SetPosition(TimeSpan position)
{
//throw new NotImplementedException();
}
public void ShowProperties()
{
Dispatcher.Invoke(new Action(delegate
{
_settings.Show();
}));
//throw new NotImplementedException();
}
}
}