3 using System
.Reflection
;
10 private readonly int num
;
12 private readonly Block parent
;
13 private readonly T type
;
35 get { return parent.X + x; }
40 get { return parent.Y + y; }
45 get { return parent; }
63 public Socket(int x
, int y
, Block parent
, T type
, int num
)
74 public void Draw(Context c
)
76 int xc
= parent
.X
+ x
;
77 int yc
= parent
.Y
+ y
+ 5;
83 c
.Arc(xc
+6.5, yc
, 4, 0, 2*Math
.PI
);
84 c
.Color
= new Color(0.2, 0.2, 1, 0.6);
97 c
.Arc(xc
, yc
, r
, 0, 2*Math
.PI
);
98 c
.Color
= new Color(1, 0, 0);
101 c
.Color
= new Color(0.3, 0, 0);
103 c
.MoveTo(xc
-0.707*r
, yc
-0.707*r
);
104 c
.LineTo(xc
+0.707*r
, yc
+0.707*r
);
106 c
.MoveTo(xc
-0.707*r
, yc
+0.707*r
);
107 c
.LineTo(xc
+0.707*6, yc
-0.707*6);
112 public void Disconnect()
119 other
.parent
.Invalidate();
126 public void Connect(Socket to
)
138 private readonly IPlugin plugin
;
139 private readonly Schematic schematic
;
141 private int h
= 0, w
= 0, th
= 0;
142 private int tx
= 0, ty
= 0;
143 private int sx
= 0, sy
= 0;
144 private int cx
= 0, cy
= 0;
145 private Socket
[] socketin
= null;
146 private Socket
[] socketout
= null;
147 private bool working
= false;
148 private bool showerror
= false;
150 private static ImageSurface StateBad
= new ImageSurface(System
.IO
.Path
.Combine(AppDomain
.CurrentDomain
.BaseDirectory
, "data/state-bad.png"));
151 private static ImageSurface StateGood
= new ImageSurface(System
.IO
.Path
.Combine(AppDomain
.CurrentDomain
.BaseDirectory
, "data/state-good.png"));
152 private static ImageSurface StateNotReady
= new ImageSurface(System
.IO
.Path
.Combine(AppDomain
.CurrentDomain
.BaseDirectory
, "data/state-not-ready.png"));
153 private static ImageSurface StateReady
= new ImageSurface(System
.IO
.Path
.Combine(AppDomain
.CurrentDomain
.BaseDirectory
, "data/state-ready.png"));
154 private static ImageSurface Clock
= new ImageSurface(System
.IO
.Path
.Combine(AppDomain
.CurrentDomain
.BaseDirectory
, "data/clock.png"));
156 private static bool ConfigGradient
= Config
.Get("block/gradient", true);
157 private static bool ConfigInner
= Config
.Get("block/innerpath", true);
158 private static bool ConfigRound
= Config
.Get("block/round", true);
159 private static bool ConfigSmoothConnections
= Config
.Get("block/smoothconnections", true);
169 public bool ShowError
171 set { showerror = value; }
174 public IPlugin Plugin
176 get { return plugin; }
189 public Socket
[] SocketIn
191 get { return socketin; }
194 public Socket
[] SocketOut
196 get { return socketout; }
201 get { return working; }
202 set { working = value; }
205 public Block(Schematic s
, Context c
, IPlugin plugin
, int x
, int y
)
208 this.plugin
= plugin
;
214 UpdateCoordinates(c
);
216 socketin
= new Socket
[plugin
.NumIn
];
217 socketout
= new Socket
[plugin
.NumOut
];
219 int curpos
= 10+(h
-20-CalcHeight(plugin
.NumIn
))/2;
220 for(int i
=0; i
<plugin
.NumIn
; i
++)
222 socketin
[i
] = new Socket(0, curpos
, this, Socket
.T
.In
, i
);
226 curpos
= 10+(h
-20-CalcHeight(plugin
.NumOut
))/2;
227 for(int i
=0; i
<plugin
.NumOut
; i
++)
229 socketout
[i
] = new Socket(w
-10, curpos
, this, Socket
.T
.Out
, i
);
234 public static void CheckGConf()
236 bool NewConfigGradient
= Config
.Get("block/gradient", true);
237 bool NewConfigInner
= Config
.Get("block/innerpath", true);
238 bool NewConfigRound
= Config
.Get("block/round", true);
239 bool NewConfigSmoothConnections
= Config
.Get("block/smoothconnections", true);
242 ConfigGradient
!= NewConfigGradient
||
243 ConfigInner
!= NewConfigInner
||
244 ConfigRound
!= NewConfigRound
||
245 ConfigSmoothConnections
!= NewConfigSmoothConnections
;
247 ConfigGradient
= NewConfigGradient
;
248 ConfigInner
= NewConfigInner
;
249 ConfigRound
= NewConfigRound
;
250 ConfigSmoothConnections
= NewConfigSmoothConnections
;
253 MainWindow
.RedrawSchematic();
256 public object Overlap(int x
, int y
)
258 if(x
< this.x
|| x
> this.x
+ w
|| y
< this.y
|| y
> this.y
+ h
)
264 int curpos
= this.y
+10+(h
-20-CalcHeight(socketin
.Length
))/2;
266 for(int i
=0; i
<socketin
.Length
; i
++)
268 if(y
> curpos
&& y
< curpos
+ 10)
274 else if(x
> this.x
+ w
- 10)
277 int curpos
= this.y
+10+(h
-20-CalcHeight(socketout
.Length
))/2;
279 for(int i
=0; i
<socketout
.Length
; i
++)
281 if(y
> curpos
&& y
< curpos
+ 10)
291 // FIXME wpisany na sztywno rozmiar obszaru roboczego
292 public void Move(int dx
, int dy
)
308 // usuwa wszystkie połączenia
309 public void Disconnect()
311 for(int i
=0; i
<socketin
.Length
; i
++)
312 socketin
[i
].Disconnect();
314 for(int i
=0; i
<socketout
.Length
; i
++)
315 socketout
[i
].Disconnect();
318 // wykonywane po zmianie liczby slotów we wtyczce
319 private void UpdateCoordinates()
321 int h1
= CalcHeight(plugin
.NumIn
);
322 int h2
= CalcHeight(plugin
.NumOut
);
325 h
= Math
.Max(Math
.Max(th
, h1
), h2
) + 20;
334 if(plugin
is ConnectorPlugin
)
343 // wykonywane tylko raz, przez konstruktor
344 private void UpdateCoordinates(Context c
)
346 TextExtents t
= c
.TextExtents(plugin
.Info
.ShortName
);
351 w
= (int)t
.Width
+ 30;
357 if(plugin
is ConnectorPlugin
)
368 public void Draw(Context c
, object selected
)
372 if(this == selected
|| (selected
is Socket
&& (selected
as Socket
).Parent
== this))
381 if(selected
is Socket
)
383 for(int i
=0; i
<socketin
.Length
; i
++)
384 if(socketin
[i
] == selected
)
390 for(int i
=0; i
<socketout
.Length
; i
++)
391 if(socketout
[i
] == selected
)
393 socketout
[i
].Draw(c
);
400 c
.Color
= new Color(1, 0, 0, 0.5);
406 public State
CheckState()
408 for(int i
=0; i
<socketin
.Length
; i
++)
409 if(SocketIn
[i
].Other
== null)
410 return State
.NotReady
;
421 public bool WorkPossible()
423 for(int i
=0; i
<socketin
.Length
; i
++)
424 if(socketin
[i
].Other
.Parent
.CheckState() != State
.Good
)
430 private void DrawClock(Context c
)
433 c
.Translate(x
+ cx
, y
+ cy
);
439 private void DrawState(Context c
)
441 State s
= CheckState();
443 c
.Color
= new Color(1, 1, 1);
445 c
.Translate(x
+ sx
, y
+ sy
);
447 if(s
== State
.NotReady
)
448 StateNotReady
.Show(c
, 0, 0);
449 else if(s
== State
.Bad
)
450 StateBad
.Show(c
, 0, 0);
451 else if(s
== State
.Good
)
452 StateGood
.Show(c
, 0, 0);
454 StateReady
.Show(c
, 0, 0);
460 private void DrawBlockGradient(Context c
)
462 LinearGradient g
= new LinearGradient(0, y
, 0, y
+h
);
463 if(plugin
is IInPlugin
)
465 g
.AddColorStop(0, new Color(0.65, 0.85, 1.00, 0.85));
466 g
.AddColorStop(0.33, new Color(0.45, 0.65, 1.00, 0.85));
467 g
.AddColorStop(1, new Color(0.20, 0.50, 0.80, 0.85));
469 else if(plugin
is IOutPlugin
)
471 g
.AddColorStop(0, new Color(1.00, 0.85, 1.00, 0.85));
472 g
.AddColorStop(0.33, new Color(1.00, 0.65, 1.00, 0.85));
473 g
.AddColorStop(1, new Color(0.80, 0.40, 0.80, 0.85));
475 else if(plugin
is IImgProcPlugin
)
477 g
.AddColorStop(0, new Color(0.75, 1.00, 0.75, 0.85));
478 g
.AddColorStop(0.33, new Color(0.55, 1.00, 0.55, 0.85));
479 g
.AddColorStop(1, new Color(0.30, 0.80, 0.30, 0.85));
481 else if(plugin
is IResProcPlugin
)
483 g
.AddColorStop(0, new Color(1.00, 0.75, 0.75, 0.85));
484 g
.AddColorStop(0.33, new Color(1.00, 0.55, 0.55, 0.85));
485 g
.AddColorStop(1, new Color(0.80, 0.30, 0.30, 0.85));
487 else if(plugin
is IComparatorPlugin
)
489 g
.AddColorStop(0, new Color(1.00, 1.00, 0.75, 0.85));
490 g
.AddColorStop(0.33, new Color(1.00, 1.00, 0.55, 0.85));
491 g
.AddColorStop(1, new Color(0.80, 0.80, 0.30, 0.85));
493 else if(plugin
is IOtherPlugin
)
495 g
.AddColorStop(0, new Color(0.7, 0.7, 0.7, 0.85));
496 g
.AddColorStop(0.33, new Color(0.5, 0.5, 0.5, 0.85));
497 g
.AddColorStop(1, new Color(0.35, 0.35, 0.35, 0.85));
505 private void DrawBlockNoGradient(Context c
)
507 if(plugin
is IInPlugin
)
508 c
.Color
= new Color(0.35, 0.55, 0.95, 0.85);
509 else if(plugin
is IOutPlugin
)
510 c
.Color
= new Color(0.95, 0.55, 0.95, 0.85);
511 else if(plugin
is IImgProcPlugin
)
512 c
.Color
= new Color(0.45, 0.95, 0.45, 0.85);
513 else if(plugin
is IResProcPlugin
)
514 c
.Color
= new Color(0.95, 0.45, 0.45, 0.85);
515 else if(plugin
is IComparatorPlugin
)
516 c
.Color
= new Color(0.95, 0.95, 0.45, 0.85);
517 else if(plugin
is IOtherPlugin
)
518 c
.Color
= new Color(0.5, 0.5, 0.5, 0.85);
524 private void DrawBlock(Context c
, bool IsSelected
)
527 DrawBlockGradient(c
);
529 DrawBlockNoGradient(c
);
532 c
.Color
= new Color(1, 0, 0);
534 c
.Color
= new Color(0.2, 0.2, 1);
536 c
.Color
= new Color(0, 0, 0);
544 c
.Color
= new Color(1, 1, 1, 0.5);
550 c
.Color
= new Color(0, 0, 0);
553 c
.Translate(x
+ tx
, y
+ ty
);
554 c
.ShowText(plugin
.Info
.ShortName
);
559 private void DrawSockets(Context c
)
561 int curpos
= y
+10+(h
-20-CalcHeight(socketout
.Length
))/2;
563 for(int i
=0; i
<socketout
.Length
; i
++)
565 if(socketout
[i
].Other
== null)
568 c
.Arc(x
+w
-3, curpos
+5, 4, 0, 2*Math
.PI
);
569 c
.Color
= new Color(1, 1, 1, 0.8);
571 c
.Color
= new Color(0, 0, 0);
577 c
.MoveTo(x
+w
, curpos
);
580 c
.CurveTo(x
+w
-10, curpos
, x
+w
-10, curpos
+10, x
+w
, curpos
+10);
583 c
.LineTo(x
+w
-8, curpos
);
584 c
.LineTo(x
+w
-8, curpos
+10);
585 c
.LineTo(x
+w
, curpos
+10);
588 c
.LineTo(x
+w
, curpos
);
589 c
.Color
= new Color(1, 1, 1, 0.8);
591 c
.MoveTo(x
+w
, curpos
);
592 c
.LineTo(x
+w
, curpos
+10);
593 c
.Color
= new Color(0, 0, 0);
600 curpos
= y
+10+(h
-20-CalcHeight(socketin
.Length
))/2;
602 for(int i
=0; i
<socketin
.Length
; i
++)
604 if(socketin
[i
].Other
!= null)
610 c
.CurveTo(x
+10, curpos
, x
+10, curpos
+10, x
, curpos
+10);
613 c
.LineTo(x
+8, curpos
);
614 c
.LineTo(x
+8, curpos
+10);
615 c
.LineTo(x
, curpos
+10);
619 c
.Color
= new Color(1, 1, 1, 0.8);
622 c
.LineTo(x
, curpos
+10);
623 c
.Color
= new Color(0, 0, 0);
631 public void DrawConnections(Context c
)
633 int curpos
= y
+15+(h
-20-CalcHeight(socketout
.Length
))/2;
636 c
.Color
= new Color(0, 0, 0);
637 for(int i
=0; i
<socketout
.Length
; i
++)
639 if(socketout
[i
].Other
!= null)
641 c
.MoveTo(x
+w
, curpos
);
643 if(ConfigSmoothConnections
)
644 c
.CurveTo(x
+w
+10, curpos
, socketout
[i
].Other
.PX
-10, socketout
[i
].Other
.PY
+5, socketout
[i
].Other
.PX
, socketout
[i
].Other
.PY
+5);
646 c
.LineTo(socketout
[i
].Other
.PX
, socketout
[i
].Other
.PY
+5);
655 private int CalcHeight(int sockets
)
657 return Math
.Max(sockets
*10 + (sockets
-1)*5, 0);
660 private void DrawPath(Context c
)
668 private void DrawPathRound(Context c
)
679 c
.CurveTo(x
+w
, y
, x
+w
, y
, x
+w
, y
+10);
682 if(socketout
.Length
== 0)
683 c
.LineTo(x
+w
, y
+h
-10);
686 int curpos
= y
+10+(h
-20-CalcHeight(socketout
.Length
))/2;
688 c
.LineTo(x
+w
, curpos
);
690 for(int i
=0; i
<socketout
.Length
; i
++)
695 c
.LineTo(x
+w
, curpos
);
698 c
.CurveTo(x
+w
-10, curpos
, x
+w
-10, curpos
+10, x
+w
, curpos
+10);
702 c
.LineTo(x
+w
,y
+h
-10);
706 c
.CurveTo(x
+w
, y
+h
, x
+w
, y
+h
, x
+w
-10, y
+h
);
709 c
.CurveTo(x
, y
+h
, x
, y
+h
, x
, y
+h
-10);
712 if(socketin
.Length
== 0)
716 int curpos
= y
+h
-10-(h
-20-CalcHeight(socketin
.Length
))/2;
720 for(int i
=0; i
<socketin
.Length
; i
++)
728 c
.CurveTo(x
+10, curpos
, x
+10, curpos
-10, x
, curpos
-10);
736 c
.CurveTo(x
, y
, x
, y
, x
+10, y
);
739 private void DrawPathSquare(Context c
)
752 if(socketout
.Length
== 0)
756 int curpos
= y
+10+(h
-20-CalcHeight(socketout
.Length
))/2;
758 c
.LineTo(x
+w
, curpos
);
760 for(int i
=0; i
<socketout
.Length
; i
++)
765 c
.LineTo(x
+w
, curpos
);
768 c
.LineTo(x
+w
-8, curpos
);
769 c
.LineTo(x
+w
-8, curpos
+10);
770 c
.LineTo(x
+w
, curpos
+10);
783 if(socketin
.Length
== 0)
787 int curpos
= y
+h
-10-(h
-20-CalcHeight(socketin
.Length
))/2;
791 for(int i
=0; i
<socketin
.Length
; i
++)
799 c
.LineTo(x
+8, curpos
);
800 c
.LineTo(x
+8, curpos
-10);
801 c
.LineTo(x
, curpos
-10);
814 private void DrawPathInner(Context c
)
817 DrawPathInnerRound(c
);
820 // DrawPathInnerSquare(c);
824 private void DrawPathInnerRound(Context c
)
832 c
.MoveTo(x
+11.5, y
+1.5);
833 c
.LineTo(x
+w
-11.5, y
+1.5);
835 c
.CurveTo(x
+w
-1.5, y
+1.5, x
+w
-1.5, y
+1.5, x
+w
-1.5, y
+8.5);
838 if(socketout
.Length
== 0)
839 c
.LineTo(x
+w
-1.5, y
+h
-11.5);
842 int curpos
= y
+10+(h
-20-CalcHeight(socketout
.Length
))/2;
844 c
.LineTo(x
+w
-1.5, curpos
-1.5);
846 for(int i
=0; i
<socketout
.Length
; i
++)
851 c
.LineTo(x
+w
-1.5, curpos
-1.5);
854 c
.CurveTo(x
+w
-11.5, curpos
-1.5, x
+w
-11.5, curpos
+11.5, x
+w
-1.5, curpos
+11.5);
858 c
.LineTo(x
+w
-1.5, y
+h
-8.5);
862 c
.CurveTo(x
+w
-1.5, y
+h
-1.5, x
+w
-1.5, y
+h
-1.5, x
+w
-11.5, y
+h
-1.5);
863 c
.LineTo(x
+11.5, y
+h
-1.5);
865 c
.CurveTo(x
+1.5, y
+h
-1.5, x
+1.5, y
+h
-1.5, x
+1.5, y
+h
-8.5);
868 if(socketin
.Length
== 0)
869 c
.LineTo(x
+1.5, y
+11.5);
872 int curpos
= y
+h
-10-(h
-20-CalcHeight(socketin
.Length
))/2;
874 c
.LineTo(x
+1.5, curpos
+1.5);
876 for(int i
=0; i
<socketin
.Length
; i
++)
881 c
.LineTo(x
+1.5, curpos
+1.5);
884 c
.CurveTo(x
+11.5, curpos
+1.5, x
+11.5, curpos
-11.5, x
+1.5, curpos
-11.5);
888 c
.LineTo(x
+1.5, y
+8.5);
892 c
.CurveTo(x
+1.5, y
+1.5, x
+1.5, y
+1.5, x
+11.5, y
+1.5);
895 private void Invalidate(bool x
)
897 Plugin
.WorkDone
= false;
900 for(int i
=0; i
<socketout
.Length
; i
++)
902 Socket o
= socketout
[i
].Other
;
904 if(o
!= null && o
.Parent
.CheckState() == State
.Good
)
905 o
.Parent
.Invalidate(true);
909 public void Invalidate()
912 schematic
.QueueDraw();
915 public void SlotsChanged()
919 if(socketin
.Length
!= plugin
.NumIn
)
921 Socket
[] newsocketin
= new Socket
[plugin
.NumIn
];
922 int curpos
= 10+(h
-20-CalcHeight(plugin
.NumIn
))/2;
924 for(int i
=0; i
<plugin
.NumIn
; i
++)
926 if(i
< socketin
.Length
)
927 newsocketin
[i
] = socketin
[i
];
929 newsocketin
[i
] = new Socket(0, curpos
, this, Socket
.T
.In
, i
);
932 for(int i
=plugin
.NumIn
; i
<socketout
.Length
; i
++)
933 socketout
[i
].Disconnect();
935 socketin
= newsocketin
;
939 int curpos
= 10+(h
-20-CalcHeight(socketin
.Length
))/2;
941 for(int i
=0; i
<socketin
.Length
; i
++)
943 socketin
[i
].Y
= curpos
;
948 if(socketout
.Length
!= plugin
.NumOut
)
950 Socket
[] newsocketout
= new Socket
[plugin
.NumOut
];
951 int curpos
= 10+(h
-20-CalcHeight(plugin
.NumOut
))/2;
953 for(int i
=0; i
<plugin
.NumOut
; i
++)
955 if(i
< socketout
.Length
)
956 newsocketout
[i
] = socketout
[i
];
958 newsocketout
[i
] = new Socket(w
-10, curpos
, this, Socket
.T
.Out
, i
);
961 for(int i
=plugin
.NumOut
; i
<socketout
.Length
; i
++)
962 socketout
[i
].Disconnect();
964 socketout
= newsocketout
;
968 int curpos
= 10+(h
-20-CalcHeight(socketout
.Length
))/2;
970 for(int i
=0; i
<socketout
.Length
; i
++)
972 socketout
[i
].Y
= curpos
;
977 schematic
.QueueDraw();