-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prototypes.cs
1907 lines (1758 loc) · 68.5 KB
/
Prototypes.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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Net.NetworkInformation;
using System.Text;
using System.Windows.Forms;
namespace Schematix
{
public abstract class xBase
{
// Load
virtual protected void BeforeReadParameters()//Ok
{
}
virtual protected void AfterReadParameters()//Ok
{
}
virtual public void ReadParameters(BinaryReader stream)//Ok
{
BeforeReadParameters();
while (stream.PeekChar() > -1)
{
String parameterName = stream.ReadString();
// Check for record's end
if (parameterName == "END")
break;
int valueLength = stream.ReadInt32();
// Load(/skip) parameters
ReadParameter(stream, parameterName, valueLength);
}
AfterReadParameters();
}
virtual protected bool ReadParameter(BinaryReader stream, String parameterName, int valueLength)//Ok
{
stream.ReadBytes(valueLength);
return false;
}
protected String ReadStreamString(BinaryReader stream, int length)//Ok
{
return Encoding.UTF8.GetString(stream.ReadBytes(length));
}
// Save
virtual public void WriteParameters(BinaryWriter stream)//Ok
{
// Mark the end
stream.Write("END");
}
protected void WriteStreamString(String parameterName, BinaryWriter stream, String text)//Ok
{
stream.Write(parameterName);
byte[] b = Encoding.UTF8.GetBytes(text);
stream.Write(b.Length);
stream.Write(b);
}
protected void WriteStream_CloseBlock(BinaryWriter stream, int start)//Ok
{
// Jump back and fill size value
int pos = (int)stream.Seek(0, SeekOrigin.Current);
stream.Seek(start, SeekOrigin.Begin);
stream.Write(pos - start);
stream.Seek(pos, SeekOrigin.Begin);
}
}
public abstract class xRecord : xBase
{
public UInt64 ID = (UInt64)DateTime.Now.ToBinary();
public String Name = "";
public String Description = "";
public String FileName = "";
// Load
public bool LoadFromFileCheck(ref String fileName)//Ok
{
if (fileName == "")
{
var dlg = new OpenFileDialog();
dlg.Filter = Options.RECORD_EXTFILLTER_MAP;
if (dlg.ShowDialog() == DialogResult.Cancel)
return false;
using (var file = File.OpenRead(dlg.FileName)) { }
fileName = dlg.FileName;
}
return true;
}
public bool LoadFromFile(String fileName)//Ok
{
if (!LoadFromFileCheck(ref fileName))
return false;
try
{
using (var file = File.OpenRead(fileName))
{
using (var stream = new BinaryReader(file))
{
FileName = fileName;
ReadParameters(stream);
}
}
}
catch (Exception e)
{
MessageBox.Show(Options.LangCur.mErrorsOccurred + "" + e.Message, Options.LangCur.dFileLoading);
return false;
}
return true;
}
override protected bool ReadParameter(BinaryReader stream, String parameterName, int valueLength)//Ok
{
if (parameterName == "Name")
Name = new String(stream.ReadChars(valueLength));
else if (parameterName == "Description")
Description = new String(stream.ReadChars(valueLength));
else if (valueLength == 8 && parameterName == "ID")
ID = stream.ReadUInt64();
// Skip unknown data
else
return base.ReadParameter(stream, parameterName, valueLength);
return true;
}
// Save
public bool SaveToFileCheck(ref String fileName)//Ok
{
if (fileName == "")
{
var dlg = new SaveFileDialog();
dlg.Filter = Options.RECORD_EXTFILLTER_MAP;
if (dlg.ShowDialog() == DialogResult.Cancel)
return false;
using (var file = File.OpenWrite(dlg.FileName)) { }
fileName = dlg.FileName;
}
return true;
}
public bool SaveToFile(String fileName)//Ok
{
if (!SaveToFileCheck(ref fileName))
return false;
try
{
// Make temporary file
using (var file = File.Create(fileName + ".tmp"))
{
using (var stream = new BinaryWriter(file))
{
WriteParameters(stream);
FileName = fileName;
}
}
// Delete old backup file
if (File.Exists(fileName + ".bak"))
File.Delete(fileName + ".bak");
// Make current file as backup
if (File.Exists(fileName))
{
File.Move(fileName, fileName + ".bak");
File.SetAttributes(fileName + ".bak", FileAttributes.Hidden);
}
// Make temporary file as current
File.Move(fileName + ".tmp", fileName);
}
catch (Exception e)
{
MessageBox.Show(Options.LangCur.mErrorsOccurred + "" + e.Message, Options.LangCur.dFileSaving);
return false;
}
return true;
}
override public void WriteParameters(BinaryWriter stream)//Ok
{
WriteStreamString("Name", stream, Name);
WriteStreamString("Description", stream, Description);
stream.Write("ID");
stream.Write(8);
stream.Write(ID);
base.WriteParameters(stream);
}
}
// Prototypes
public abstract class xPrototype : xRecord
{
public bool isUsed;
public bool isPrototype;
public String NodeName;
public Int64 Revision = DateTime.Now.ToBinary();
public ListViewItem lvItemUsed;
public TreeNode tvNode;
public UInt64 NodeParent = 0;
virtual public bool IsObject { get; }
virtual public bool IsLink { get; }
virtual public bool IsBox { get; }
override protected bool ReadParameter(BinaryReader stream, String parameterName, int valueLength)//Ok
{
if (parameterName == "NodeName")
NodeName = new String(stream.ReadChars(valueLength));
else if (valueLength == 8 && parameterName == "Revision")
Revision = stream.ReadInt64();
else if (valueLength == 8 && parameterName == "NodeParent")
NodeParent = stream.ReadUInt64();
else if (valueLength == 1 && parameterName == "IsPrototype")
isPrototype = stream.ReadBoolean();
// Pass unprocessed data up
else
return base.ReadParameter(stream, parameterName, valueLength);
return true;
}
override public void WriteParameters(BinaryWriter stream)//Ok
{
// Write local part of body
WriteStreamString("NodeName", stream, NodeName);
stream.Write("Revision");
stream.Write(8);
stream.Write(Revision);
stream.Write("NodeParent");
stream.Write(8);
stream.Write(NodeParent);
stream.Write("IsPrototype");
stream.Write(1);
stream.Write(isPrototype);
// Upper class body
base.WriteParameters(stream);
}
}
public class xDot : xRecord
{
protected xPObject PObject { get; }
public short X = 0;
public short Y = 0;
public xDot(xPObject owner)
{
PObject = owner;
}
override protected bool ReadParameter(BinaryReader stream, String parameterName, int valueLength)//Ok
{
if (valueLength == 2 && parameterName == "X")
X = stream.ReadInt16();
else if (valueLength == 2 && parameterName == "Y")
Y = stream.ReadInt16();
// Pass unprocessed data up
else
return base.ReadParameter(stream, parameterName, valueLength);
return true;
}
override public void WriteParameters(BinaryWriter stream)//Ok
{
// Write local part of body
stream.Write("X");
stream.Write(2);
stream.Write(X);
stream.Write("Y");
stream.Write(2);
stream.Write(Y);
// Upper class body
base.WriteParameters(stream);
}
}
public enum ImageTypes
{
None,
Image,
Link
}
public enum ImageBPPs
{
b32argb,
b24rgb,
b16a1r5g5b5,
b16r5g6b5,
b8,
b4,
}
public class xPObject : xPrototype
{
public ImageTypes ImageType = ImageTypes.Image;
public String ImagePath = "";
public bool UseAlphaColor = false;
public Color AlphaColor = Options.DEFAULT_OBJECT_APLHA_COLOR;
public ImageBPPs ImageBPP = ImageBPPs.b32argb;
public Color BackColor = Options.DEFAULT_OBJECT_IMAGE_COLOR;
public List<xDot> Dots = new List<xDot>();
public Bitmap Canvas = Share.GetNoImage();
bool MustClearDots = false;
override public bool IsObject { get; } = true;
public xPObject()
{
Dots.Add(new xDot(this) { Name = "", Description = "", X = 0, Y = 0 });
MustClearDots = true;
}
override protected void BeforeReadParameters()
{
UseAlphaColor = false;
MustClearDots = false;
Dots = new List<xDot>();
}
override protected void AfterReadParameters()
{
if (ImageType == ImageTypes.None)
Canvas = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
if (ImageType == ImageTypes.Link)
if (File.Exists(ImagePath))
{
var bmap = new Bitmap(ImagePath);
Canvas = new Bitmap(bmap.Width, bmap.Height, PixelFormat.Format32bppArgb);
Graphics.FromImage(Canvas).DrawImageUnscaled(bmap, 0, 0);
}
if (UseAlphaColor)
Canvas.MakeTransparent(AlphaColor);
}
override protected bool ReadParameter(BinaryReader stream, String parameterName, int valueLength)//Ok
{
if (parameterName == "ImageType")
Enum.TryParse(ReadStreamString(stream, valueLength), out ImageType);
else if (valueLength == 4 && parameterName == "AlphaColor")
AlphaColor = Color.FromArgb(stream.ReadInt32());
else if (parameterName == "ImagePath")
ImagePath = ReadStreamString(stream, valueLength);
else if (parameterName == "Image")
Share.StreamGetImage(stream, valueLength, ref ImageBPP, ref Canvas);
else if (valueLength == 0 && parameterName == "UseAlphaColor")
UseAlphaColor = true;
else if (valueLength == 4 && parameterName == "BackColor")
BackColor = Color.FromArgb(stream.ReadInt32());
else if (parameterName == "Dot")
{
var Dot = new xDot(this);
Dot.ReadParameters(stream);
if (MustClearDots)
{
Dots.Clear();
MustClearDots = false;
}
Dots.Add(Dot);
}
// Pass unprocessed data up
else
return base.ReadParameter(stream, parameterName, valueLength);
return true;
}
override public void WriteParameters(BinaryWriter stream)//Ok
{
// Write local part of body
WriteStreamString("ImageType", stream, ImageType.ToString());
stream.Write("AlphaColor");
stream.Write(4);
stream.Write(AlphaColor.ToArgb());
WriteStreamString("ImagePath", stream, ImagePath);
if (ImageType == ImageTypes.Image)
{
stream.Write("Image");
Share.StreamPutImage(stream, Canvas, ImageBPP);
}
if (UseAlphaColor)
{
stream.Write("UseAlphaColor");
stream.Write(0);
}
stream.Write("BackColor");
stream.Write(4);
stream.Write(BackColor.ToArgb());
// Dots
int pos = 0;
foreach (var Dot in Dots)
{
stream.Write("Dot");
pos = (int)stream.Seek(0, SeekOrigin.Current);
stream.Write(pos);
Dot.WriteParameters(stream);
WriteStream_CloseBlock(stream, pos);
}
// Upper class body
base.WriteParameters(stream);
}
public void DeleteDot(xDot Dot)
{
Dots.Remove(Dot);
if (Dots.Count < 1)
Dots.Add(new xDot(this));
}
public xDot GetNearestDot(int x, int y)
{
double dotR = 9999, R, dx, dy;
xDot dot = null;
foreach (var Dot in Dots)
{
dx = x - Dot.X;
dy = y - Dot.Y;
R = Math.Sqrt((dx * dx) + (dy * dy));
if (R < dotR)
{
dotR = R;
dot = Dot;
}
}
return dot;
}
}
public class xPLink : xPrototype
{
public Pen Pen = new Pen(Options.DEFAULT_LINK_LINE_COLOR, 1);
override public bool IsLink { get; } = true;
override protected bool ReadParameter(BinaryReader stream, String parameterName, int valueLength)//Ok
{
if (valueLength == 4 && parameterName == "Thick")
Pen.Width = stream.ReadSingle();
else if (valueLength == 4 && parameterName == "LineColor")
Pen.Color = Color.FromArgb(stream.ReadInt32());
else if (parameterName == "LineStyle")
{
DashStyle ds;
Enum.TryParse(ReadStreamString(stream, valueLength), out ds);
Pen.DashStyle = ds;
if (ds == DashStyle.Custom)
Pen.DashPattern = new float[] { 5, 2, 1, 3 };
}
// Pass unprocessed data up
else
return base.ReadParameter(stream, parameterName, valueLength);
return true;
}
override public void WriteParameters(BinaryWriter stream)//Ok
{
// Write local part of body
stream.Write("Thick");
stream.Write(4);
stream.Write(Pen.Width);
stream.Write("LineColor");
stream.Write(4);
stream.Write(Pen.Color.ToArgb());
WriteStreamString("LineStyle", stream, Pen.DashStyle.ToString());
// Upper class body
base.WriteParameters(stream);
}
}
public enum BoxTypes
{
Text,
Rectangle,
Ellipse
}
public enum AlignTypes
{
TopLeft,
Top,
TopRight,
Left,
Center,
Right,
BottomLeft,
Bottom,
BottomRight
}
public class xPBox : xPLink
{
public BoxTypes BoxType = BoxTypes.Text;
public String Text = "Text";
public AlignTypes TextAlign = AlignTypes.TopLeft;
public Font Font = Options.DEFAULT_BOX_FONT;
public Brush Brush = new SolidBrush(Options.DEFAULT_BOX_TEXT_COLOR);
Color _textColor = Options.DEFAULT_BOX_TEXT_COLOR;
public Color TextColor
{
set { Brush = new SolidBrush(_textColor = value); }
get { return _textColor; }
}
override public bool IsBox { get; } = true;
override protected bool ReadParameter(BinaryReader stream, String parameterName, int valueLength)//Ok
{
if (parameterName == "BoxType")
Enum.TryParse(ReadStreamString(stream, valueLength), out BoxType);
else if (parameterName == "Text")
Text = ReadStreamString(stream, valueLength);
else if (valueLength == 1 && parameterName == "TextAlign")
Enum.TryParse(stream.ReadByte().ToString(), out TextAlign);
else if (parameterName == "FontName")
Font = new Font(ReadStreamString(stream, valueLength), Font.Size, Font.Style);
else if (valueLength == 4 && parameterName == "FontSize")
Font = new Font(Font.Name, stream.ReadSingle(), Font.Style);
else if (valueLength == 1 && parameterName == "FontStyle")
Font = new Font(Font.Name, Font.Size, (FontStyle)stream.ReadByte());
else if (valueLength == 4 && parameterName == "TextColor")
TextColor = Color.FromArgb(stream.ReadInt32());
// Pass unprocessed data up
else
return base.ReadParameter(stream, parameterName, valueLength);
return true;
}
override public void WriteParameters(BinaryWriter stream)//Ok
{
// Write local part of body
WriteStreamString("BoxType", stream, BoxType.ToString());
WriteStreamString("Text", stream, Text);
stream.Write("TextAlign");
stream.Write(1);
stream.Write((byte)TextAlign);
WriteStreamString("FontName", stream, Font.Name);
stream.Write("FontSize");
stream.Write(4);
stream.Write(Font.Size);
stream.Write("FontStyle");
stream.Write(1);
stream.Write((byte)Font.Style);
stream.Write("TextColor");
stream.Write(4);
stream.Write(TextColor.ToArgb());
// Upper class body
base.WriteParameters(stream);
}
}
// Exemplars
public abstract class xExemplar : xRecord
{
protected xMap Map { get; }
public xPrototype Prototype;
public UInt64 PrototypeID;
public Int64 PrototypeRevision;
public String Reference = "";
public int Left = 0, Top = 0;
public int Right = 0, Bottom = 0;
public int Width = 0, Height = 0;
virtual public bool IsObject { get; }
virtual public bool IsLink { get; }
virtual public bool IsBox { get; }
public xExemplar(xMap owner)
{
Map = owner;
}
override protected bool ReadParameter(BinaryReader stream, String parameterName, int valueLength)//Ok
{
if (parameterName == "Reference")
Reference = ReadStreamString(stream, valueLength);
else if (valueLength == 8 && parameterName == "PrototypeID")
PrototypeID = stream.ReadUInt64();
else if (valueLength == 8 && parameterName == "PrototypeRevision")
PrototypeRevision = stream.ReadInt64();
// Pass unprocessed data up
else
return base.ReadParameter(stream, parameterName, valueLength);
return true;
}
override public void WriteParameters(BinaryWriter stream)//Ok
{
// Write local part of body
WriteStreamString("Reference", stream, Reference);
stream.Write("PrototypeID");
stream.Write(8);
stream.Write(PrototypeID);
stream.Write("PrototypeRevision");
stream.Write(8);
stream.Write(PrototypeRevision);
// Upper class body
base.WriteParameters(stream);
}
virtual public bool isOver(int x, int y, byte padding = 0)//Ok
{
return (Left <= x + padding) && (x <= Right + padding) && (Top <= y + padding) && (y <= Bottom + padding);
}
virtual public void Check()//Ok
{
Right = Left + Width;
Bottom = Top + Height;
}
virtual public void Delete()
{
}
}
public enum PingStates
{
NotSend,
Send,
Replayed,
Cancelled
}
public class xPing : xBase
{
public PingStates State;
public DateTime SendTime = DateTime.Now;
public IPStatus Status;
public String Error = "";
public int TripTime;
public String Replayer = "";
override protected bool ReadParameter(BinaryReader stream, String parameterName, int valueLength)//Ok
{
if (parameterName == "State")
Enum.TryParse(ReadStreamString(stream, valueLength), out State);
else if (valueLength == 2 && parameterName == "Status")
Status = (IPStatus)stream.ReadInt16();
else if (valueLength == 8 && parameterName == "SendTime")
SendTime = DateTime.FromBinary(stream.ReadInt64());
else if (parameterName == "Error")
Error = ReadStreamString(stream, valueLength);
else if (valueLength == 4 && parameterName == "TripTime")
TripTime = stream.ReadInt32();
else if (parameterName == "Replayer")
Replayer = ReadStreamString(stream, valueLength);
// Pass unprocessed data up
else
return base.ReadParameter(stream, parameterName, valueLength);
return true;
}
override public void WriteParameters(BinaryWriter stream)//Ok
{
// Write local part of body
WriteStreamString("State", stream, State.ToString());
if (State != PingStates.NotSend)
{
stream.Write("Status");
stream.Write(2);
stream.Write((Int16)Status);
stream.Write("SendTime");
stream.Write(8);
stream.Write(SendTime.ToBinary());
if (Error != "")
WriteStreamString("Error", stream, Error);
stream.Write("TripTime");
stream.Write(4);
stream.Write(TripTime);
if (Replayer != "")
WriteStreamString("Replayer", stream, Replayer);
}
// Upper class body
base.WriteParameters(stream);
}
}
public class xIP : xRecord
{
xObject Object { get; }
public ListViewItem Map_lvItem = null;
public ListViewItem Obj_lvItem = null;
public ListView Pings_lv = null;
public String Address = "";
public int Period = Options.DEFAULT_PING_PERIOD;
public int TimeOutGreen = Options.DEFAULT_PING_TIMEOUT_GREEN;
public int TimeOutYellow = Options.DEFAULT_PING_TIMEOUT_YELLOW;
public int TimeOutRed = Options.DEFAULT_PING_TIMEOUT_RED;
public bool Onn = Options.DEFAULT_PING_ONN;
public DateTime TimeLast = DateTime.Now;
public DateTime TimeNext = DateTime.Now;
public xPing[] Pings = new xPing[Options.DEFAULT_PING_ARRAY];
int pingsCount = 0;
public int PingsCount { get { return pingsCount; } }
public xIP(xObject owner)//Ok
{
Object = owner;
}
public void ClearPings()//Ok
{
Pings = new xPing[Options.DEFAULT_PING_ARRAY];
pingsCount = 0;
}
public void PushNewPing(xPing Ping)//Ok
{
if (Pings.Length < ++pingsCount)
pingsCount = Pings.Length;
for (int i = pingsCount - 1; 0 < i; i--)
Pings[i] = Pings[i - 1];
Pings[0] = Ping;
}
public bool AddPing(DateTime now)//Ok
{
if (now < TimeNext || !Onn)
return false;
TimeLast = now;
TimeNext = now.AddMilliseconds(Period);
PushNewPing(new xPing());
Pings[0].State = PingStates.Send;
Pings[0].SendTime = now;
if(Pings_lv != null)
Share.lvPings_Add(Pings_lv, this, Pings[0]);
return true;
}
protected override void BeforeReadParameters() => ClearPings();//Ok
override protected bool ReadParameter(BinaryReader stream, String parameterName, int valueLength)//Ok
{
if (parameterName == "Address")
Address = new String(stream.ReadChars(valueLength));
else if (valueLength == 4 && parameterName == "Period")
Period = stream.ReadInt32();
else if (valueLength == 4 && parameterName == "TimeOutGreen")
TimeOutGreen = stream.ReadInt32();
else if (valueLength == 4 && parameterName == "TimeOutYellow")
TimeOutYellow = stream.ReadInt32();
else if (valueLength == 4 && parameterName == "TimeOutRed")
TimeOutRed = stream.ReadInt32();
else if (valueLength == 1 && parameterName == "Onn")
Onn = stream.ReadBoolean();
else if (valueLength == 8 && parameterName == "TimeLast")
TimeLast = DateTime.FromBinary(stream.ReadInt64());
else if (valueLength == 8 && parameterName == "TimeNext")
TimeNext = DateTime.FromBinary(stream.ReadInt64());
else if (parameterName == "Ping")
{
var Ping = new xPing();
Ping.ReadParameters(stream);
PushNewPing(Ping);
}
// Pass unprocessed data up
else
return base.ReadParameter(stream, parameterName, valueLength);
return true;
}
override public void WriteParameters(BinaryWriter stream)//Ok
{
// Write local part of body
WriteStreamString("Address", stream, Address);
stream.Write("Period");
stream.Write(4);
stream.Write(Period);
stream.Write("TimeOutGreen");
stream.Write(4);
stream.Write(TimeOutGreen);
stream.Write("TimeOutYellow");
stream.Write(4);
stream.Write(TimeOutYellow);
stream.Write("TimeOutRed");
stream.Write(4);
stream.Write(TimeOutRed);
stream.Write("Onn");
stream.Write(1);
stream.Write(Onn);
stream.Write("TimeLast");
stream.Write(8);
stream.Write(TimeLast.ToBinary());
stream.Write("TimeNext");
stream.Write(8);
stream.Write(TimeNext.ToBinary());
// Ping records
int pos = 0;
for (int i = pingsCount - 1; 0 <= i; i--)
if (Pings[i] != null)
if (Pings[i].State != PingStates.NotSend)
{
stream.Write("Ping");
pos = (int)stream.Seek(0, SeekOrigin.Current);
stream.Write(pos);
Pings[i].WriteParameters(stream);
WriteStream_CloseBlock(stream, pos);
}
// Upper class body
base.WriteParameters(stream);
}
public void Delete() => Object.DeleteIP(this);//Ok
}
public class xObject : xExemplar
{
public int X;
public int Y;
public List<xIP> IPs = new List<xIP>();
override public bool IsObject { get; } = true;
public xObject(xMap owner) : base(owner) { }
override protected bool ReadParameter(BinaryReader stream, String parameterName, int valueLength)//Ok
{
if (valueLength == 4 && parameterName == "X")
X = stream.ReadInt32();
else if (valueLength == 4 && parameterName == "Y")
Y = stream.ReadInt32();
else if (parameterName == "IP")
{
var IP = new xIP(this);
IP.ReadParameters(stream);
IPs.Add(IP);
}
// Pass unprocessed data up
else
return base.ReadParameter(stream, parameterName, valueLength);
return true;
}
override public void WriteParameters(BinaryWriter stream)//Ok
{
// Write local part of body
stream.Write("X");
stream.Write(4);
stream.Write(X);
stream.Write("Y");
stream.Write(4);
stream.Write(Y);
// Dots
int pos = 0;
foreach (var IP in IPs)
{
stream.Write("IP");
pos = (int)stream.Seek(0, SeekOrigin.Current);
stream.Write(pos);
IP.WriteParameters(stream);
WriteStream_CloseBlock(stream, pos);
}
// Upper class body
base.WriteParameters(stream);
}
override public void Check()
{
Left = X - (Prototype as xPObject).Dots[0].X;
Top = Y - (Prototype as xPObject).Dots[0].Y;
Width = (Prototype as xPObject).Canvas.Width;
Height = (Prototype as xPObject).Canvas.Height;
base.Check();
}
public void AddIP(xIP IP)
{
if (!IPs.Contains(IP))
IPs.Add(IP);
Map.AddIP(IP);
}
public void ClearIPs()
{
for (int i = IPs.Count - 1; 0 <= i; i--)
DeleteIP(IPs[i]);
}
public void DeleteIP(xIP IP)
{
IP.Map_lvItem?.Remove();
IP.Obj_lvItem?.Remove();
Map.DeleteIP(IP);
IPs.Remove(IP);
}
override public void Delete()
{
Map.DeleteObject(this);
}
public xDot GetNearestDot(int x, int y) => (Prototype as xPObject).GetNearestDot(x - Left, y - Top);
}
public class xLink : xExemplar
{
public UInt64 ObjectAID, DotAID;
public UInt64 ObjectBID, DotBID;
public xObject ObjectA = null;
public xObject ObjectB = null;
public xDot DotA = null;
public xDot DotB = null;
public int XA, YA;
public int XB, YB;
override public bool IsLink { get; } = true;
public xLink(xMap owner) : base(owner) { }
override protected bool ReadParameter(BinaryReader stream, String parameterName, int valueLength)//Ok
{
if (valueLength == 8 && parameterName == "ObjectAID")
ObjectAID = stream.ReadUInt64();
else if (valueLength == 8 && parameterName == "DotAID")
DotAID = stream.ReadUInt64();
else if (valueLength == 8 && parameterName == "ObjectBID")
ObjectBID = stream.ReadUInt64();
else if (valueLength == 8 && parameterName == "DotBID")
DotBID = stream.ReadUInt64();
else if (valueLength == 4 && parameterName == "YB")
YB = stream.ReadInt32();
else if (valueLength == 4 && parameterName == "XA")
XA = stream.ReadInt32();
else if (valueLength == 4 && parameterName == "YA")
YA = stream.ReadInt32();
else if (valueLength == 4 && parameterName == "XB")
XB = stream.ReadInt32();
else if (valueLength == 4 && parameterName == "YB")
YB = stream.ReadInt32();
// Pass unprocessed data up
else
return base.ReadParameter(stream, parameterName, valueLength);
return true;
}
override public void WriteParameters(BinaryWriter stream)//Ok
{
// Write local part of body
if (ObjectAID != 0)
{
stream.Write("ObjectAID");
stream.Write(8);
stream.Write(ObjectA.ID);
stream.Write("DotAID");
stream.Write(8);
stream.Write(DotA.ID);
}
else
{
stream.Write("XA");
stream.Write(4);
stream.Write(XA);
stream.Write("YA");
stream.Write(4);
stream.Write(YA);
}
if (ObjectBID != 0)
{
stream.Write("ObjectBID");
stream.Write(8);
stream.Write(ObjectBID);
stream.Write("DotBID");
stream.Write(8);
stream.Write(DotBID);
}
else
{
stream.Write("XB");
stream.Write(4);
stream.Write(XB);
stream.Write("YB");
stream.Write(4);
stream.Write(YB);
}
// Upper class body
base.WriteParameters(stream);
}
override public bool isOver(int x, int y, byte padding = 0)//Ok
{
if (base.isOver(x, y, padding))
{
double rx = x;
double ry = y;
// Find point on line:
if (Width < Height)
// Vertical
rx = Math.Round(XA + (double)((XB - XA) * Math.Abs(y - YA)) / Height);