forked from opensourceautomation/Open-Source-Automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
824 lines (717 loc) · 35 KB
/
MainWindow.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
namespace GUI2
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using MySql.Data.MySqlClient;
using OSAE;
using OSAE.UI.Controls;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Logging logging = Logging.GetLogger("GUI");
String gAppName = "";
String gCurrentScreen = "";
List<StateImage> stateImages = new List<StateImage>();
List<NavigationImage> navImages = new List<NavigationImage>();
List<MethodImage> methodImages = new List<MethodImage>();
List<VideoStreamViewer> cameraViewers = new List<VideoStreamViewer>();
List<PropertyLabel> propLabels = new List<PropertyLabel>();
List<StaticLabel> staticLabels = new List<StaticLabel>();
List<TimerLabel> timerLabels = new List<TimerLabel>();
List<dynamic> userControls = new List<dynamic>();
bool loadingScreen = true;
bool editMode = false;
bool closing = false;
#region drag and drop properties
private Point _startPoint;
DragAdorner _adorner = null;
AdornerLayer _layer;
private bool _dragHasLeftScope = false;
private bool _isDragging;
public bool IsDragging
{
get { return _isDragging; }
set { _isDragging = value; }
}
FrameworkElement _dragScope;
public FrameworkElement DragScope
{
get { return _dragScope; }
set { _dragScope = value; }
}
private dynamic beingDragged;
private List<Type> controlTypes = new List<Type>();
#endregion
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("pack://siteoforigin:,,,/OSA.png");
var bitmapImage = new BitmapImage(uri);
canGUI.Background = new ImageBrush(bitmapImage);
canGUI.Height = bitmapImage.Height;
canGUI.Width = bitmapImage.Width;
canGUI.Background = new ImageBrush(bitmapImage);
canGUI.Height = bitmapImage.Height;
canGUI.Width = bitmapImage.Width;
Load_App_Name();
gCurrentScreen = OSAEObjectPropertyManager.GetObjectPropertyValue(gAppName, "Default Screen").Value;
if (gCurrentScreen == "")
{
Set_Default_Screen();
}
Load_Screen(gCurrentScreen);
Thread thread = new Thread(() => Update_Objects());
thread.Start();
this.canGUI.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(DragSource_PreviewMouseLeftButtonDown);
this.canGUI.Drop += new DragEventHandler(DragSource_Drop);
}
public void Load_Screen(string sScreen)
{
try
{
stateImages.Clear();
propLabels.Clear();
navImages.Clear();
cameraViewers.Clear();
canGUI.Children.Clear();
loadingScreen = true;
logging.AddToLog("Loading screen: " + sScreen, false);
gCurrentScreen = sScreen;
OSAEObjectPropertyManager.ObjectPropertySet(gAppName, "Current Screen", sScreen, "GUI");
OSAE.OSAEImageManager imgMgr = new OSAE.OSAEImageManager();
string imgID = OSAEObjectPropertyManager.GetObjectPropertyValue(sScreen, "Background Image").Value;
OSAE.OSAEImage img = imgMgr.GetImage(imgID);
//sPath = OSAEApi.APIpath + OSAEApi.GetObjectPropertyValue(sScreen, "Background Image").Value;
//byte[] byteArray = File.ReadAllBytes(sPath);
if (img.Data != null)
{
var imageStream = new MemoryStream(img.Data);
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = imageStream;
bitmapImage.EndInit();
canGUI.Background = new ImageBrush(bitmapImage);
canGUI.Height = bitmapImage.Height;
canGUI.Width = bitmapImage.Width;
}
Thread thread = new Thread(() => Load_Objects(sScreen));
thread.Start();
logging.AddToLog("Loading screen complete: " + sScreen, false);
}
catch (Exception ex)
{
logging.AddToLog("Failed to load screen: " + sScreen, true);
}
}
private void Load_Objects(String sScreen)
{
OSAEObjectCollection screenObjects = OSAEObjectManager.GetObjectsByContainer(sScreen);
foreach (OSAE.OSAEObject obj in screenObjects)
{
LoadControl(obj);
}
loadingScreen = false;
}
private void Update_Objects()
{
while (!closing)
{
if (!loadingScreen)
{
bool oldCtrl = false;
logging.AddToLog("Entering Update_Objects", false);
List<OSAE.OSAEScreenControl> controls = OSAEScreenControlManager.GetScreenControls(gCurrentScreen);
foreach (OSAE.OSAEScreenControl newCtrl in controls)
{
oldCtrl = false;
#region CONTROL STATE IMAGE
if (newCtrl.ControlType == "CONTROL STATE IMAGE")
{
foreach (StateImage sImage in stateImages)
{
if (newCtrl.ControlName == sImage.screenObject.Name)
{
if (newCtrl.LastUpdated != sImage.LastUpdated)
{
logging.AddToLog("Updating: " + newCtrl.ControlName, false);
sImage.LastUpdated = newCtrl.LastUpdated;
sImage.Update();
this.Dispatcher.Invoke((Action)(() =>
{
Canvas.SetLeft(sImage, sImage.Location.X);
Canvas.SetTop(sImage, sImage.Location.Y);
}));
logging.AddToLog("Complete: " + newCtrl.ControlName, false);
}
oldCtrl = true;
}
}
}
#endregion
#region CONTROL PROPERTY LABEL
else if (newCtrl.ControlType == "CONTROL PROPERTY LABEL")
{
foreach (PropertyLabel pl in propLabels)
{
if (newCtrl.ControlName == pl.screenObject.Name)
{
if (newCtrl.LastUpdated != pl.LastUpdated)
{
logging.AddToLog("Updating: " + newCtrl.ControlName, false);
pl.LastUpdated = newCtrl.LastUpdated;
pl.Update();
logging.AddToLog("Complete: " + newCtrl.ControlName, false);
}
oldCtrl = true;
}
}
}
#endregion
#region CONTROL TIMER LABEL
else if (newCtrl.ControlType == "CONTROL TIMER LABEL")
{
foreach (OSAE.UI.Controls.TimerLabel tl in timerLabels)
{
if (newCtrl.ControlName == tl.screenObject.Name)
{
if (newCtrl.LastUpdated != tl.LastUpdated)
{
logging.AddToLog("Updating: " + newCtrl.ControlName, false);
tl.LastUpdated = newCtrl.LastUpdated;
tl.Update();
logging.AddToLog("Complete: " + newCtrl.ControlName, false);
}
oldCtrl = true;
}
}
}
#endregion
#region CONTROL STATIC LABEL
else if (newCtrl.ControlType == "CONTROL STATIC LABEL")
{
foreach (OSAE.UI.Controls.StaticLabel sl in staticLabels)
{
if (newCtrl.ControlName == sl.screenObject.Name)
{
oldCtrl = true;
}
}
}
#endregion
#region CONTROL NAVIGATION IMAGE
else if (newCtrl.ControlType == "CONTROL NAVIGATION IMAGE")
{
foreach (OSAE.UI.Controls.NavigationImage nav in navImages)
{
if (newCtrl.ControlName == nav.screenObject.Name)
{
oldCtrl = true;
}
}
}
#endregion
#region CONTROL CAMERA VIEWER
else if (newCtrl.ControlType == "CONTROL CAMERA VIEWER")
{
foreach (OSAE.UI.Controls.VideoStreamViewer vsv in cameraViewers)
{
if (newCtrl.ControlName == vsv.screenObject.Name)
{
oldCtrl = true;
}
}
}
#endregion
#region CONTROL USAER CONTROL
else if (newCtrl.ControlType == "USER CONTROL")
{
foreach (dynamic obj in userControls)
{
if (newCtrl.ControlName == obj.screenObject.Name)
{
oldCtrl = true;
}
}
}
#endregion
if (!oldCtrl)
{
OSAE.OSAEObject obj = OSAEObjectManager.GetObjectByName(newCtrl.ControlName);
logging.AddToLog("Load new control: " + newCtrl.ControlName, false);
LoadControl(obj);
}
}
logging.AddToLog("Leaving Update_Objects", false);
}
System.Threading.Thread.Sleep(1000);
}
}
private void LoadControl(OSAE.OSAEObject obj)
{
this.Dispatcher.Invoke((Action)(() =>
{
String sStateMatch = "";
#region CONTROL STATE IMAGE
if (obj.Type == "CONTROL STATE IMAGE")
{
StateImage stateImageControl = new StateImage(obj);
foreach (OSAE.OSAEObjectProperty p in obj.Properties)
{
if (p.Value.ToLower() == stateImageControl.CurState.ToLower())
{
sStateMatch = p.Name.Substring(0, p.Name.LastIndexOf(' '));
}
}
int dZ = Int32.Parse(obj.Property("ZOrder").Value);
stateImageControl.Location.X = Double.Parse(obj.Property(sStateMatch + " X").Value);
stateImageControl.Location.Y = Double.Parse(obj.Property(sStateMatch + " Y").Value);
canGUI.Children.Add(stateImageControl);
Canvas.SetLeft(stateImageControl, stateImageControl.Location.X);
Canvas.SetTop(stateImageControl, stateImageControl.Location.Y);
Canvas.SetZIndex(stateImageControl, dZ);
stateImages.Add(stateImageControl);
controlTypes.Add(typeof(StateImage));
stateImageControl.PreviewMouseMove += new MouseEventHandler(DragSource_PreviewMouseMove);
}
#endregion
#region CONTROL PROPERTY LABEL
else if (obj.Type == "CONTROL PROPERTY LABEL")
{
logging.AddToLog("Loading PropertyLabelControl: " + obj.Name, false);
PropertyLabel pl = new PropertyLabel(obj);
canGUI.Children.Add(pl);
int dZ = Int32.Parse(obj.Property("ZOrder").Value);
pl.Location.X = Double.Parse(obj.Property("X").Value);
pl.Location.Y = Double.Parse(obj.Property("Y").Value);
Canvas.SetLeft(pl, pl.Location.X);
Canvas.SetTop(pl, pl.Location.Y);
Canvas.SetZIndex(pl, dZ);
propLabels.Add(pl);
controlTypes.Add(typeof(PropertyLabel));
pl.PreviewMouseMove += new MouseEventHandler(DragSource_PreviewMouseMove);
}
#endregion
#region CONTROL STATIC LABEL
else if (obj.Type == "CONTROL STATIC LABEL")
{
logging.AddToLog("Loading PropertyLabelControl: " + obj.Name, false);
OSAE.UI.Controls.StaticLabel sl = new OSAE.UI.Controls.StaticLabel(obj);
canGUI.Children.Add(sl);
int dZ = Int32.Parse(obj.Property("ZOrder").Value);
sl.Location.X = Double.Parse(obj.Property("X").Value);
sl.Location.Y = Double.Parse(obj.Property("Y").Value);
Canvas.SetLeft(sl, sl.Location.X);
Canvas.SetTop(sl, sl.Location.Y);
Canvas.SetZIndex(sl, dZ);
staticLabels.Add(sl);
controlTypes.Add(typeof(OSAE.UI.Controls.StaticLabel));
sl.PreviewMouseMove += new MouseEventHandler(DragSource_PreviewMouseMove);
}
#endregion
#region CONTROL TIMER LABEL
else if (obj.Type == "CONTROL TIMER LABEL")
{
logging.AddToLog("Loading PropertyLabelControl: " + obj.Name, false);
OSAE.UI.Controls.TimerLabel tl = new OSAE.UI.Controls.TimerLabel(obj);
canGUI.Children.Add(tl);
int dZ = Int32.Parse(obj.Property("ZOrder").Value);
tl.Location.X = Double.Parse(obj.Property("X").Value);
tl.Location.Y = Double.Parse(obj.Property("Y").Value);
Canvas.SetLeft(tl, tl.Location.X);
Canvas.SetTop(tl, tl.Location.Y);
Canvas.SetZIndex(tl, dZ);
timerLabels.Add(tl);
controlTypes.Add(typeof(OSAE.UI.Controls.TimerLabel));
tl.PreviewMouseMove += new MouseEventHandler(DragSource_PreviewMouseMove);
}
#endregion
#region CONTROL METHOD IMAGE
else if (obj.Type == "CONTROL METHOD IMAGE")
{
try
{
MethodImage methodImageControl = new MethodImage(obj);
canGUI.Children.Add(methodImageControl);
OSAE.OSAEObjectProperty pZOrder = obj.Property("ZOrder");
OSAE.OSAEObjectProperty pX = obj.Property("X");
OSAE.OSAEObjectProperty pY = obj.Property("Y");
Double dX = Convert.ToDouble(pX.Value);
Canvas.SetLeft(methodImageControl, dX);
Double dY = Convert.ToDouble(pY.Value);
Canvas.SetTop(methodImageControl, dY);
int dZ = Convert.ToInt32(pZOrder.Value);
Canvas.SetZIndex(methodImageControl, dZ);
methodImageControl.Location.X = dX;
methodImageControl.Location.Y = dY;
methodImages.Add(methodImageControl);
controlTypes.Add(typeof(MethodImage));
methodImageControl.PreviewMouseMove += new MouseEventHandler(DragSource_PreviewMouseMove);
}
catch (MySqlException myerror)
{
MessageBox.Show("GUI Error Load Navigation Image: " + myerror.Message);
}
}
#endregion
#region CONTROL NAVIGATION IMAGE
else if (obj.Type == "CONTROL NAVIGATION IMAGE")
{
try
{
NavigationImage navImageControl = new NavigationImage(obj.Property("Screen").Value, obj);
navImageControl.MouseLeftButtonUp += new MouseButtonEventHandler(Navigaton_Image_MouseLeftButtonUp);
canGUI.Children.Add(navImageControl);
OSAE.OSAEObjectProperty pZOrder = obj.Property("ZOrder");
OSAE.OSAEObjectProperty pX = obj.Property("X");
OSAE.OSAEObjectProperty pY = obj.Property("Y");
Double dX = Convert.ToDouble(pX.Value);
Canvas.SetLeft(navImageControl, dX);
Double dY = Convert.ToDouble(pY.Value);
Canvas.SetTop(navImageControl, dY);
int dZ = Convert.ToInt32(pZOrder.Value);
Canvas.SetZIndex(navImageControl, dZ);
navImageControl.Location.X = dX;
navImageControl.Location.Y = dY;
navImages.Add(navImageControl);
controlTypes.Add(typeof(NavigationImage));
navImageControl.PreviewMouseMove += new MouseEventHandler(DragSource_PreviewMouseMove);
}
catch (MySqlException myerror)
{
MessageBox.Show("GUI Error Load Navigation Image: " + myerror.Message);
}
}
#endregion
#region CONTROL CAMERA VIEWER
else if (obj.Type == "CONTROL CAMERA VIEWER")
{
try
{
string stream = OSAEObjectPropertyManager.GetObjectPropertyValue(obj.Property("Object Name").Value, "Stream Address").Value;
VideoStreamViewer vsv = new VideoStreamViewer(stream, obj);
canGUI.Children.Add(vsv);
OSAE.OSAEObjectProperty pZOrder = obj.Property("ZOrder");
OSAE.OSAEObjectProperty pX = obj.Property("X");
OSAE.OSAEObjectProperty pY = obj.Property("Y");
Double dX = Convert.ToDouble(pX.Value);
Canvas.SetLeft(vsv, dX);
Double dY = Convert.ToDouble(pY.Value);
Canvas.SetTop(vsv, dY);
int dZ = Convert.ToInt32(pZOrder.Value);
Canvas.SetZIndex(vsv, dZ);
vsv.Location.X = dX;
vsv.Location.Y = dY;
cameraViewers.Add(vsv);
controlTypes.Add(typeof(VideoStreamViewer));
vsv.PreviewMouseMove += new MouseEventHandler(DragSource_PreviewMouseMove);
}
catch (MySqlException myerror)
{
MessageBox.Show("GUI Error Load Camera Viewer: " + myerror.Message);
}
}
#endregion
#region USER CONTROL
else if (obj.Type == "USER CONTROL")
{
string sUCType = obj.Property("Control Type").Value;
if (sUCType == "USER CONTROL WEATHER")
{
Weather wc = new Weather(obj);
canGUI.Children.Add(wc);
OSAE.OSAEObjectProperty pZOrder = obj.Property("ZOrder");
OSAE.OSAEObjectProperty pX = obj.Property("X");
OSAE.OSAEObjectProperty pY = obj.Property("Y");
Double dX = Convert.ToDouble(pX.Value);
Canvas.SetLeft(wc, dX);
Double dY = Convert.ToDouble(pY.Value);
Canvas.SetTop(wc, dY);
int dZ = Convert.ToInt32(pZOrder.Value);
Canvas.SetZIndex(wc, dZ);
wc.Location.X = dX;
wc.Location.Y = dY;
userControls.Add(wc);
controlTypes.Add(typeof(Weather));
wc.PreviewMouseMove += new MouseEventHandler(DragSource_PreviewMouseMove);
}
}
#endregion
}));
}
private void Load_App_Name()
{
OSAEObjectCollection screens = OSAEObjectManager.GetObjectsByType("GUI CLIENT");
foreach (OSAE.OSAEObject obj in screens)
{
if (obj.Property("Computer Name").Value == Common.ComputerName)
gAppName = obj.Name;
}
if (gAppName == "")
{
gAppName = "GUI CLIENT-" + Common.ComputerName;
OSAEObjectManager.ObjectAdd(gAppName, gAppName, "GUI CLIENT", "", "SYSTEM", true);
OSAEObjectPropertyManager.ObjectPropertySet(gAppName, "Computer Name", Common.ComputerName, "GUI");
}
}
private void Set_Default_Screen()
{
OSAEObjectCollection screens = OSAEObjectManager.GetObjectsByType("SCREEN");
if (screens.Count > 0)
{
gCurrentScreen = screens[0].Name;
OSAEObjectPropertyManager.ObjectPropertySet(gAppName, "Default Screen", gCurrentScreen, "GUI");
}
}
private void canvas1_RightButtonDown(object sender, RoutedEventArgs e)
{
//mnuMain. = true;
}
private void Navigaton_Image_MouseLeftButtonUp(object sender, MouseEventArgs e)
{
NavigationImage navCtrl = (NavigationImage)sender;
gCurrentScreen = navCtrl.screenName;
Load_Screen(gCurrentScreen);
}
#region Drag Events
void DragSource_Drop(dynamic sender, DragEventArgs e)
{
IDataObject data = e.Data;
if (data.GetDataPresent(DataFormats.Text))
{
int newX = Convert.ToInt32(e.GetPosition(DragScope).X - (beingDragged.ActualWidth / 2));
int newY = Convert.ToInt32(e.GetPosition(DragScope).Y - (beingDragged.ActualHeight / 2));
Canvas.SetLeft(beingDragged, newX);
Canvas.SetTop(beingDragged, newY);
beingDragged.Location = new Point(newX, newY);
if (beingDragged.GetType() == typeof(StateImage))
{
Thread thread = new Thread(() => updateObjectCoordsStateImg(beingDragged.screenObject, beingDragged.StateMatch, newX.ToString(), newY.ToString()));
thread.Start();
}
else
{
Thread thread = new Thread(() => updateObjectCoords(beingDragged.screenObject, newX.ToString(), newY.ToString()));
thread.Start();
}
}
}
void DragSource_PreviewMouseMove(dynamic sender, MouseEventArgs e)
{
if (editMode)
{
if (e.LeftButton == MouseButtonState.Pressed && !IsDragging)
{
foreach (Type t in controlTypes)
{
if (t == sender.GetType())
{
Point position = e.GetPosition(null);
double width = sender.ActualWidth;
double height = sender.ActualHeight;
double x = sender.Location.X;
double y = sender.Location.Y;
if (
(Math.Abs(position.X - _startPoint.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(position.Y - _startPoint.Y) > SystemParameters.MinimumVerticalDragDistance)
&&
(sender.Location.X < _startPoint.X && _startPoint.X < (sender.Location.X + sender.ActualWidth))
&&
(sender.Location.Y < _startPoint.Y && _startPoint.Y < (sender.Location.Y + sender.ActualHeight))
)
{
beingDragged = (UIElement)sender;
StartDragInProcAdorner(e, sender);
}
}
}
}
}
}
void DragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(DragScope);
}
private void StartDragInProcAdorner(MouseEventArgs e, dynamic sender)
{
// Let's define our DragScope .. In this case it is every thing inside our main window ..
DragScope = this.canGUI as FrameworkElement;
System.Diagnostics.Debug.Assert(DragScope != null);
// We enable Drag & Drop in our scope ... We are not implementing Drop, so it is OK, but this allows us to get DragOver
bool previousDrop = DragScope.AllowDrop;
DragScope.AllowDrop = true;
// Let's wire our usual events..
// GiveFeedback just tells it to use no standard cursors..
//GiveFeedbackEventHandler feedbackhandler = new GiveFeedbackEventHandler(DragSource_GiveFeedback);
//this.DragSource.GiveFeedback += feedbackhandler;
// The DragOver event ...
DragEventHandler draghandler = new DragEventHandler(Window1_DragOver);
DragScope.DragOver += draghandler;
// Drag Leave is optional, but write up explains why I like it ..
DragEventHandler dragleavehandler = new DragEventHandler(DragScope_DragLeave);
DragScope.DragLeave += dragleavehandler;
// QueryContinue Drag goes with drag leave...
QueryContinueDragEventHandler queryhandler = new QueryContinueDragEventHandler(DragScope_QueryContinueDrag);
DragScope.QueryContinueDrag += queryhandler;
//Here we create our adorner..
_adorner = new DragAdorner(DragScope, (UIElement)sender, true, 0.5);
_layer = AdornerLayer.GetAdornerLayer(DragScope as Visual);
_layer.Add(_adorner);
IsDragging = true;
_dragHasLeftScope = false;
//Finally lets drag drop
DataObject data = new DataObject(System.Windows.DataFormats.Text.ToString(), "abcd");
DragDropEffects de = DragDrop.DoDragDrop(this.canGUI, data, DragDropEffects.Move);
// Clean up our mess :)
DragScope.AllowDrop = previousDrop;
AdornerLayer.GetAdornerLayer(DragScope).Remove(_adorner);
_adorner = null;
//DragSource.GiveFeedback -= feedbackhandler;
DragScope.DragLeave -= dragleavehandler;
DragScope.QueryContinueDrag -= queryhandler;
IsDragging = false;
}
void Window1_DragOver(object sender, DragEventArgs args)
{
if (_adorner != null)
{
_adorner.LeftOffset = args.GetPosition(DragScope).X /* - _startPoint.X */ ;
_adorner.TopOffset = args.GetPosition(DragScope).Y /* - _startPoint.Y */ ;
}
}
void DragScope_DragLeave(object sender, DragEventArgs e)
{
if (e.OriginalSource == DragScope)
{
Point p = e.GetPosition(DragScope);
Rect r = VisualTreeHelper.GetContentBounds(DragScope);
if (!r.Contains(p))
{
this._dragHasLeftScope = true;
e.Handled = true;
}
}
}
void DragScope_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
if (this._dragHasLeftScope)
{
e.Action = DragAction.Cancel;
e.Handled = true;
}
}
private void updateObjectCoords(OSAE.OSAEObject obj, string X, string Y)
{
OSAEObjectPropertyManager.ObjectPropertySet(obj.Name, "X", X, "GUI");
OSAEObjectPropertyManager.ObjectPropertySet(obj.Name, "Y", Y, "GUI");
obj.Property("X").Value = X;
obj.Property("Y").Value = Y;
}
private void updateObjectCoordsStateImg(OSAE.OSAEObject obj, string state, string X, string Y)
{
OSAEObjectPropertyManager.ObjectPropertySet(obj.Name, state + " X", X, "GUI");
OSAEObjectPropertyManager.ObjectPropertySet(obj.Name, state + " Y", Y, "GUI");
obj.Property(state + " X").Value = X;
obj.Property(state + " Y").Value = Y;
}
#endregion
#region menu events
private void menuEditMode_Checked(object sender, RoutedEventArgs e)
{
editMode = true;
}
private void menuEditMode_Unchecked(object sender, RoutedEventArgs e)
{
editMode = false;
}
private void menuAddStateImage_Click(object sender, RoutedEventArgs e)
{
AddControl addControl = new AddControl();
AddControlStateImage csi = new AddControlStateImage(gCurrentScreen);
addControl.Width = csi.Width + 80;
addControl.Height = csi.Height + 80;
addControl.Content = csi;
addControl.Show();
}
private void menuAddPropertyLabel_Click(object sender, RoutedEventArgs e)
{
AddControl addControl = new AddControl();
AddControlPropertyLabel csi = new AddControlPropertyLabel(gCurrentScreen);
addControl.Width = csi.Width + 80;
addControl.Height = csi.Height + 80;
addControl.Content = csi;
addControl.Show();
}
private void menuAddNavImage_Click(object sender, RoutedEventArgs e)
{
AddControl addControl = new AddControl();
AddControlNavigationImage cni = new AddControlNavigationImage(gCurrentScreen);
addControl.Content = cni;
addControl.Width = cni.Width + 80;
addControl.Height = cni.Height + 80;
addControl.Show();
}
private void menuAddMethodImage_Click(object sender, RoutedEventArgs e)
{
AddControl addControl = new AddControl();
AddControlMethodImage cmi = new AddControlMethodImage(gCurrentScreen);
addControl.Content = cmi;
addControl.Width = cmi.Width + 80;
addControl.Height = cmi.Height + 80;
addControl.Show();
}
private void menuAddTimerLabel_Click(object sender, RoutedEventArgs e)
{
AddControl addControl = new AddControl();
AddControlTimerLabel csi = new AddControlTimerLabel(gCurrentScreen);
addControl.Width = csi.Width + 80;
addControl.Height = csi.Height + 80;
addControl.Content = csi;
addControl.Show();
}
private void menuAddCameraViewer_Click(object sender, RoutedEventArgs e)
{
AddControl addControl = new AddControl();
AddNewCameraViewer csi = new AddNewCameraViewer(gCurrentScreen);
addControl.Width = csi.Width + 40;
addControl.Height = csi.Height + 40;
addControl.Content = csi;
addControl.Show();
}
private void menuAddUserControl_Click(object sender, RoutedEventArgs e)
{
AddControl addControl = new AddControl();
AddControlUserControl csi = new AddControlUserControl(gCurrentScreen);
addControl.Width = csi.Width + 80;
addControl.Height = csi.Height + 80;
addControl.Content = csi;
addControl.Show();
}
private void menuCreateScreen_Click(object sender, RoutedEventArgs e)
{
CreateScreen addControl = new CreateScreen(this);
addControl.Show();
}
#endregion
private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
{
closing = true;
}
private void menuChangeScreen_Click(object sender, RoutedEventArgs e)
{
ChangeScreen chgScrn = new ChangeScreen(this);
chgScrn.Show();
}
}
}