Projekt

Obecné

Profil

Stáhnout (15.4 KB) Statistiky
| Větev: | Tag: | Revize:
1
using System;
2
using System.Diagnostics;
3
using System.Drawing;
4
using System.Globalization;
5
using System.Windows.Forms;
6

    
7
namespace DeltaRobotVr.Launcher
8
{
9
    public class LauncherForm : Form
10
    {
11
        private readonly Label _serverLabel = new Label();
12
        private readonly Label _serverHostLabel = new Label();
13
        private readonly TextBox _serverHost = new TextBox();
14
        private readonly Label _serverPortLabel = new Label();
15
        private readonly TextBox _serverPort = new TextBox();
16

    
17
        private readonly Label _curveTranslationLabel = new Label();
18
        private readonly Label _curveTranslationXLabel = new Label();
19
        private readonly TextBox _curveTranslationX = new TextBox();
20
        private readonly Label _curveTranslationYLabel = new Label();
21
        private readonly TextBox _curveTranslationY = new TextBox();
22
        private readonly Label _curveTranslationZLabel = new Label();
23
        private readonly TextBox _curveTranslationZ = new TextBox();
24

    
25
        private readonly Label _curveScaleLabel = new Label();
26
        private readonly Label _curveScaleXLabel = new Label();
27
        private readonly TextBox _curveScaleX = new TextBox();
28
        private readonly Label _curveScaleYLabel = new Label();
29
        private readonly TextBox _curveScaleY = new TextBox();
30
        private readonly Label _curveScaleZLabel = new Label();
31
        private readonly TextBox _curveScaleZ = new TextBox();
32
        private readonly Label _curveScaleOverallLabel = new Label();
33
        private readonly TextBox _curveScaleOverall = new TextBox();
34

    
35
        private readonly Label _curveColorLabel = new Label();
36
        private readonly TextBox _curveColorHex = new TextBox();
37
        private readonly Panel _curveColorPreviewPanel = new Panel();
38
        private readonly Label _curveColorAlpha = new Label();
39

    
40
        private readonly Label _sceneLabel = new Label();
41
        private readonly ComboBox _scene = new ComboBox();
42

    
43
        private readonly Button _launchButton = new Button();
44

    
45
        private static readonly string[] Scenes =
46
        {
47
            "BasicRoomScene",
48
            "NeonRoom",
49
            "Desert1",
50
            "Desert2",
51
            "Forest",
52
            "SciFiRoom",
53
            "PaintBooth",
54
            "Village",
55
            "CityStreetDay",
56
            "CityStreetNight"
57
        };
58

    
59
        private const int Spacing = 6;
60
        private const int SectionSpacing = 4 * Spacing;
61
        private const int ControlHeight = 24;
62
        private const int LeftLabelWidth = 120;
63

    
64
        private const string VrAppExe = "deltarobot-vr.exe";
65

    
66
        protected override void OnLoad(EventArgs e)
67
        {
68
            base.OnLoad(e);
69

    
70
            Width = 640;
71
            Height = 520;
72

    
73
            Text = "Deltarobot VR Launcher";
74
            
75
            // Scene
76
            _sceneLabel.Text = "Scene";
77
            _sceneLabel.TextAlign = ContentAlignment.MiddleRight;
78
            Controls.Add(_sceneLabel);
79
            
80
            // Curve color
81
            _curveColorLabel.Text = "Curve color";
82
            _curveColorLabel.TextAlign = ContentAlignment.MiddleRight;
83
            Controls.Add(_curveColorLabel);
84

    
85
            _curveColorHex.Text = ConfigurationProvider.Instance.CurveColorHex;
86
            _curveColorHex.TextChanged += CurveColorHex_Changed;
87
            Controls.Add(_curveColorHex);
88

    
89
            Controls.Add(_curveColorPreviewPanel);
90

    
91
            _curveColorAlpha.TextAlign = ContentAlignment.MiddleLeft;
92
            Controls.Add(_curveColorAlpha);
93

    
94
            UpdateCurveColor();
95

    
96
            // Curve translation
97
            _curveTranslationLabel.Text = "Curve translation";
98
            _curveTranslationLabel.TextAlign = ContentAlignment.MiddleCenter;
99
            Controls.Add(_curveTranslationLabel);
100

    
101
            _curveTranslationXLabel.Text = "X (+ right; - left)";
102
            _curveTranslationXLabel.TextAlign = ContentAlignment.MiddleCenter;
103
            Controls.Add(_curveTranslationXLabel);
104

    
105
            _curveTranslationX.Text = ConfigurationProvider.Instance.VisualizationTranslateX.ToString("F");
106
            Controls.Add(_curveTranslationX);
107

    
108
            _curveTranslationYLabel.Text = "Y (+ up; - down)";
109
            _curveTranslationYLabel.TextAlign = ContentAlignment.MiddleCenter;
110
            Controls.Add(_curveTranslationYLabel);
111

    
112
            _curveTranslationY.Text = ConfigurationProvider.Instance.VisualizationTranslateY.ToString("F");
113
            Controls.Add(_curveTranslationY);
114

    
115
            _curveTranslationZLabel.Text = "Z (+ further; - closer)";
116
            _curveTranslationZLabel.TextAlign = ContentAlignment.MiddleCenter;
117
            Controls.Add(_curveTranslationZLabel);
118

    
119
            _curveTranslationZ.Text = ConfigurationProvider.Instance.VisualizationTranslateZ.ToString("F");
120
            Controls.Add(_curveTranslationZ);
121

    
122
            // Curve scale
123
            _curveScaleLabel.Text = "Curve scale";
124
            _curveScaleLabel.TextAlign = ContentAlignment.MiddleCenter;
125
            Controls.Add(_curveScaleLabel);
126

    
127
            _curveScaleXLabel.Text = "X";
128
            _curveScaleXLabel.TextAlign = ContentAlignment.MiddleCenter;
129
            Controls.Add(_curveScaleXLabel);
130

    
131
            _curveScaleX.Text = ConfigurationProvider.Instance.VisualizationScaleX.ToString("F");
132
            Controls.Add(_curveScaleX);
133

    
134
            _curveScaleYLabel.Text = "Y";
135
            _curveScaleYLabel.TextAlign = ContentAlignment.MiddleCenter;
136
            Controls.Add(_curveScaleYLabel);
137

    
138
            _curveScaleY.Text = ConfigurationProvider.Instance.VisualizationScaleY.ToString("F");
139
            Controls.Add(_curveScaleY);
140

    
141
            _curveScaleZLabel.Text = "Z";
142
            _curveScaleZLabel.TextAlign = ContentAlignment.MiddleCenter;
143
            Controls.Add(_curveScaleZLabel);
144

    
145
            _curveScaleZ.Text = ConfigurationProvider.Instance.VisualizationScaleZ.ToString("F");
146
            Controls.Add(_curveScaleZ);
147

    
148
            _curveScaleOverallLabel.Text = "Overall curve scale";
149
            _curveScaleOverallLabel.TextAlign = ContentAlignment.MiddleCenter;
150
            Controls.Add(_curveScaleOverallLabel);
151

    
152
            _curveScaleOverall.Text = ConfigurationProvider.Instance.VisualizationScaleOverall.ToString("F");
153
            Controls.Add(_curveScaleOverall);
154

    
155
            // Server
156
            _serverLabel.Text = "Exercise data server";
157
            _serverLabel.TextAlign = ContentAlignment.MiddleCenter;
158
            Controls.Add(_serverLabel);
159
            
160
            _serverHostLabel.Text = "Host";
161
            _serverHostLabel.TextAlign = ContentAlignment.MiddleRight;
162
            Controls.Add(_serverHostLabel);
163

    
164
            _serverHost.Text = ConfigurationProvider.Instance.Host;
165
            Controls.Add(_serverHost);
166
            
167
            _serverPortLabel.Text = "Port";
168
            _serverPortLabel.TextAlign = ContentAlignment.MiddleRight;
169
            Controls.Add(_serverPortLabel);
170

    
171
            _serverPort.Text = ConfigurationProvider.Instance.Port.ToString();
172
            Controls.Add(_serverPort);
173

    
174
            foreach (var scene in Scenes)
175
            {
176
                _scene.Items.Add(scene);
177
            }
178

    
179
            _scene.Text = ConfigurationProvider.Instance.SceneName;
180
            Controls.Add(_scene);
181

    
182
            // Launch button
183
            _launchButton.Text = "Save and Launch";
184
            _launchButton.Click += LaunchButton_Click;
185
            Controls.Add(_launchButton);
186

    
187
            UpdateLayout();
188
            CenterToScreen();
189
        }
190

    
191
        protected override void OnResize(EventArgs e)
192
        {
193
            base.OnResize(e);
194

    
195
            UpdateLayout();
196
        }
197

    
198
        private void UpdateLayout()
199
        {
200
            var lr = new LayoutRectangle(ClientRectangle);
201
            lr.Shrink(10);
202
            
203
            {
204
                var rect = lr.RemoveFromTop(ControlHeight);
205
                _sceneLabel.Bounds = rect.RemoveFromLeft(LeftLabelWidth).ToRectangle();
206
                rect.RemoveFromLeft(Spacing);
207
                _scene.Bounds = rect.ToRectangle();
208
            }
209

    
210
            lr.RemoveFromTop(Spacing);
211
            
212
            {
213
                var rect = lr.RemoveFromTop(ControlHeight);
214
                _curveColorLabel.Bounds = rect.RemoveFromLeft(LeftLabelWidth).ToRectangle();
215
                rect.RemoveFromLeft(Spacing);
216
                _curveColorHex.Bounds = rect.RemoveFromLeft(96).ToRectangle();
217
                rect.RemoveFromLeft(Spacing);
218
                _curveColorPreviewPanel.Bounds = rect.RemoveFromLeft(32).ToRectangle();
219
                rect.RemoveFromLeft(Spacing);
220
                _curveColorAlpha.Bounds = rect.ToRectangle();
221
            }
222

    
223
            lr.RemoveFromTop(SectionSpacing);
224

    
225
            {
226
                _curveTranslationLabel.Bounds = lr.RemoveFromTop(ControlHeight).ToRectangle();
227

    
228
                var rect = lr.RemoveFromTop(2 * ControlHeight);
229

    
230
                var third = rect.Width / 3;
231
                var xRect = rect.RemoveFromLeft(third);
232
                _curveTranslationXLabel.Bounds = xRect.RemoveFromTop(ControlHeight).ToRectangle();
233
                _curveTranslationX.Bounds = xRect.ToRectangle();
234

    
235
                var yRect = rect.RemoveFromLeft(third);
236
                _curveTranslationYLabel.Bounds = yRect.RemoveFromTop(ControlHeight).ToRectangle();
237
                _curveTranslationY.Bounds = yRect.ToRectangle();
238

    
239
                var zRect = rect;
240
                _curveTranslationZLabel.Bounds = zRect.RemoveFromTop(ControlHeight).ToRectangle();
241
                _curveTranslationZ.Bounds = zRect.ToRectangle();
242
            }
243

    
244
            lr.RemoveFromTop(SectionSpacing);
245

    
246
            {
247
                _curveScaleLabel.Bounds = lr.RemoveFromTop(ControlHeight).ToRectangle();
248

    
249
                var rect = lr.RemoveFromTop(4 * ControlHeight);
250

    
251
                var third = rect.Width / 3;
252
                var xRect = rect.RemoveFromLeft(third);
253
                _curveScaleXLabel.Bounds = xRect.RemoveFromTop(ControlHeight).ToRectangle();
254
                _curveScaleX.Bounds = xRect.ToRectangle();
255

    
256
                var yRect = rect.RemoveFromLeft(third);
257
                _curveScaleYLabel.Bounds = yRect.RemoveFromTop(ControlHeight).ToRectangle();
258
                _curveScaleY.Bounds = yRect.RemoveFromTop(ControlHeight).ToRectangle();
259
                _curveScaleOverallLabel.Bounds = yRect.RemoveFromTop(ControlHeight).ToRectangle();
260
                _curveScaleOverall.Bounds = yRect.ToRectangle();
261

    
262
                var zRect = rect;
263
                _curveScaleZLabel.Bounds = zRect.RemoveFromTop(ControlHeight).ToRectangle();
264
                _curveScaleZ.Bounds = zRect.ToRectangle();
265
            }
266

    
267
            lr.RemoveFromTop(SectionSpacing);
268

    
269
            _serverLabel.Bounds = lr.RemoveFromTop(ControlHeight).ToRectangle();
270
            
271
            lr.RemoveFromTop(Spacing);
272
            
273
            {
274
                var rect = lr.RemoveFromTop(ControlHeight);
275
                _serverHostLabel.Bounds = rect.RemoveFromLeft(LeftLabelWidth).ToRectangle();
276
                rect.RemoveFromLeft(Spacing);
277
                _serverHost.Bounds = rect.RemoveFromLeft(250).ToRectangle();
278
            }
279
            
280
            lr.RemoveFromTop(Spacing);
281
            
282
            {
283
                var rect = lr.RemoveFromTop(ControlHeight);
284
                _serverPortLabel.Bounds = rect.RemoveFromLeft(LeftLabelWidth).ToRectangle();
285
                rect.RemoveFromLeft(Spacing);
286
                _serverPort.Bounds = rect.RemoveFromLeft(64).ToRectangle();
287
            }
288

    
289
            _launchButton.Bounds = lr.RemoveFromBottom(ControlHeight * 2).Shrink(10).ToRectangle();
290
        }
291

    
292
        private void CurveColorHex_Changed(object sender, EventArgs args)
293
        {
294
            UpdateCurveColor();
295
        }
296

    
297
        private void LaunchButton_Click(object sender, EventArgs args)
298
        {
299
            SaveAndLaunch();
300
        }
301

    
302
        private void UpdateCurveColor()
303
        {
304
            bool valid = true;
305
            int alpha = 255;
306
            _curveColorAlpha.ForeColor = Color.Black;
307

    
308
            var colorToParse = _curveColorHex.Text;
309
            if (colorToParse.StartsWith("#"))
310
            {
311
                if (colorToParse.Length == 9)
312
                {
313
                    if (!int.TryParse(colorToParse.Substring(7, 2), NumberStyles.HexNumber, null, out alpha))
314
                    {
315
                        valid = false;
316
                    }
317

    
318
                    colorToParse = colorToParse.Substring(0, 7);
319
                }
320
                else if (colorToParse.Length != 7)
321
                {
322
                    valid = false;
323
                }
324
            }
325

    
326
            if (valid)
327
            {
328
                try
329
                {
330
                    _curveColorPreviewPanel.BackColor = ColorTranslator.FromHtml(colorToParse);
331
                }
332
                catch (Exception)
333
                {
334
                    valid = false;
335
                }
336
            }
337

    
338
            if (valid)
339
            {
340
                _curveColorAlpha.Text = $"(alpha: {alpha})";
341
            }
342
            else
343
            {
344
                _curveColorPreviewPanel.BackColor = Color.Fuchsia;
345
                _curveColorAlpha.ForeColor = Color.Red;
346
                _curveColorAlpha.Text = "(invalid)";
347
            }
348
        }
349

    
350
        private void SaveAndLaunch()
351
        {
352
            ConfigurationProvider conf = ConfigurationProvider.Instance;
353
            bool valid = true;
354

    
355
            conf.Host = _serverHost.Text;
356
            if (!int.TryParse(_serverPort.Text, out conf.Port))
357
            {
358
                MessageBox.Show(this, "Invalid port: must be a valid integer.");
359
                valid = false;
360
            }
361

    
362
            if (!float.TryParse(_curveTranslationX.Text, out conf.VisualizationTranslateX))
363
            {
364
                MessageBox.Show(this, "Invalid curve translation X: must be valid number.");
365
                valid = false;
366
            }
367

    
368
            if (!float.TryParse(_curveTranslationY.Text, out conf.VisualizationTranslateY))
369
            {
370
                MessageBox.Show(this, "Invalid curve translation Y: must be valid number.");
371
                valid = false;
372
            }
373

    
374
            if (!float.TryParse(_curveTranslationZ.Text, out conf.VisualizationTranslateZ))
375
            {
376
                MessageBox.Show(this, "Invalid curve translation Z: must be valid number.");
377
                valid = false;
378
            }
379

    
380
            if (!float.TryParse(_curveScaleX.Text, out conf.VisualizationScaleX))
381
            {
382
                MessageBox.Show(this, "Invalid curve scale X: must be valid number.");
383
                valid = false;
384
            }
385

    
386
            if (!float.TryParse(_curveScaleY.Text, out conf.VisualizationScaleY))
387
            {
388
                MessageBox.Show(this, "Invalid curve scale Y: must be valid number.");
389
                valid = false;
390
            }
391

    
392
            if (!float.TryParse(_curveScaleZ.Text, out conf.VisualizationScaleZ))
393
            {
394
                MessageBox.Show(this, "Invalid curve scale Z: must be valid number.");
395
                valid = false;
396
            }
397

    
398
            if (!float.TryParse(_curveScaleOverall.Text, out conf.VisualizationScaleOverall))
399
            {
400
                MessageBox.Show(this, "Invalid overall curve scale: must be valid number.");
401
                valid = false;
402
            }
403

    
404
            conf.SceneName = _scene.Text;
405

    
406
            conf.CurveColorHex = _curveColorHex.Text;
407

    
408
            if (valid)
409
            {
410
                conf.Save();
411

    
412
                try
413
                {
414
                    Process.Start(VrAppExe);
415
                }
416
                catch (Exception e)
417
                {
418
                    MessageBox.Show(this, $"Could not launch: {e.Message}");
419
                }
420
            }
421
        }
422
    }
423
}
(3-3/5)