2 using System
.Collections
;
9 class Schematic
: DrawingArea
11 private static bool Antialias
= Config
.Get("schematic/antialias", true);
12 private static bool ChangeBackground
= Config
.Get("schematic/changebackground", false);
13 private static int BackgroundRed
= Config
.Get("schematic/red", 128);
14 private static int BackgroundGreen
= Config
.Get("schematic/green", 128);
15 private static int BackgroundBlue
= Config
.Get("schematic/blue", 128);
16 private static int BackgroundAlpha
= Config
.Get("schematic/alpha", 255);
18 internal new class Action
27 public static Mode m
= Mode
.Normal
;
28 public static object data
;
31 internal enum Connection
38 private Statusbar status
;
39 private ArrayList blocks
= new ArrayList();
40 private static int aa
= 10;
41 private object selected
= null;
42 private int tmpx
, tmpy
;
44 public ArrayList Blocks
46 get { return blocks; }
49 public static void CheckGConf()
51 bool NewAntialias
= Config
.Get("schematic/antialias", true);
52 bool NewChangeBackground
= Config
.Get("schematic/changebackground", false);
53 int NewRed
= Config
.Get("schematic/red", 128);
54 int NewGreen
= Config
.Get("schematic/green", 128);
55 int NewBlue
= Config
.Get("schematic/blue", 128);
56 int NewAlpha
= Config
.Get("schematic/alpha", 255);
59 Antialias
!= NewAntialias
||
60 ChangeBackground
!= NewChangeBackground
||
61 BackgroundRed
!= NewRed
||
62 BackgroundGreen
!= NewGreen
||
63 BackgroundBlue
!= NewBlue
||
64 BackgroundAlpha
!= NewAlpha
;
66 Antialias
= NewAntialias
;
67 ChangeBackground
= NewChangeBackground
;
68 BackgroundRed
= NewRed
;
69 BackgroundGreen
= NewGreen
;
70 BackgroundBlue
= NewBlue
;
71 BackgroundAlpha
= NewAlpha
;
74 MainWindow
.RedrawSchematic();
77 public Schematic(Statusbar status
) : base()
81 // FIXME wpisany na sztywno rozmiar obszaru roboczego
82 SetSizeRequest(2048, 2048);
84 Events
|= Gdk
.EventMask
.PointerMotionMask
| Gdk
.EventMask
.ButtonPressMask
| Gdk
.EventMask
.ButtonReleaseMask
85 | Gdk
.EventMask
.LeaveNotifyMask
;
88 protected override bool OnExposeEvent(Gdk
.EventExpose args
)
90 Context c
= Gdk
.Context
.CreateDrawable(this.GdkWindow
);
93 c
.Antialias
= Cairo
.Antialias
.Gray
;
95 c
.Antialias
= Cairo
.Antialias
.None
;
99 ((IDisposable
) c
.Target
).Dispose();
100 ((IDisposable
) c
).Dispose();
105 private object CheckSelection(int x
, int y
)
107 foreach(Block b
in new ReverseIterator(blocks
))
109 object o
= b
.Overlap(x
, y
);
118 private Connection
CheckConnection(Socket
from, Socket to
)
120 if(to
.Type
== Socket
.T
.Out
)
121 return Connection
.None
;
123 if(IsGoodConnection(from, to
))
124 return Connection
.Good
;
126 return Connection
.Bad
;
129 private bool IsGoodConnection(Socket
from, Socket to
)
132 if(from.Parent
== to
.Parent
)
134 // do gniazda już jest coś podłączone
138 IPlugin fp
= from.Parent
.Plugin
;
139 IPlugin tp
= to
.Parent
.Plugin
;
141 foreach(string mOut
in fp
.MatchOut
)
144 ArrayList matches
= new ArrayList();
147 while(tmp
.LastIndexOf('/') != -1)
149 int lastIndex
= tmp
.LastIndexOf('/');
150 tmp
= tmp
.Substring(0, lastIndex
);
155 foreach(string mIn
in tp
.MatchIn
)
157 if(matches
.Contains(mIn
))
165 protected override bool OnMotionNotifyEvent(Gdk
.EventMotion args
)
167 if(Action
.m
== Action
.Mode
.Normal
)
169 object oldselected
= selected
;
171 selected
= CheckSelection((int)args
.X
, (int)args
.Y
);
173 if(oldselected
!= selected
)
177 if(selected
is Block
)
178 status
.Push(1, String
.Format(Catalog
.GetString("{0} block selected"), (selected
as Block
).Plugin
.Info
.Name
));
179 else if(selected
is Socket
)
181 Socket s
= selected
as Socket
;
182 IPlugin p
= s
.Parent
.Plugin
;
184 if(s
.Type
== Socket
.T
.In
)
185 status
.Push(1, String
.Format(Catalog
.GetString("{0} block, input socket {1}. {2}"),
186 p
.Info
.Name
, s
.Num
, p
.DescIn(s
.Num
)));
188 status
.Push(1, String
.Format(Catalog
.GetString("{0} block, output socket {1}. {2}"),
189 p
.Info
.Name
, s
.Num
, p
.DescOut(s
.Num
)));
195 else if(Action
.m
== Action
.Mode
.Move
)
197 int dx
= (int)args
.X
- tmpx
;
198 int dy
= (int)args
.Y
- tmpy
;
200 (selected
as Block
).Move(dx
, dy
);
206 else if(Action
.m
== Action
.Mode
.Connect
)
211 object tmp
= CheckSelection((int)args
.X
, (int)args
.Y
);
214 Action
.data
= CheckConnection(selected
as Socket
, tmp
as Socket
);
216 Action
.data
= Connection
.None
;
224 protected override bool OnButtonPressEvent(Gdk
.EventButton args
)
229 object selected
= _t
.selected
;
231 object tmp
= CheckSelection((int)args
.X
, (int)args
.Y
);
233 if(selected
!= null && selected
== tmp
)
235 if(selected
is Block
)
237 Block b
= selected
as Block
;
241 if(args
.Type
== Gdk
.EventType
.ButtonPress
)
243 status
.Push(1, Catalog
.GetString("Move block to desired location"));
245 // przeniesienie klikniętego bloku na wierzch
246 if(blocks
[blocks
.Count
-1] != selected
)
248 blocks
.Remove(selected
);
249 blocks
.Add(selected
);
253 Action
.m
= Action
.Mode
.Move
;
257 else if(args
.Type
== Gdk
.EventType
.TwoButtonPress
)
259 Action
.m
= Action
.Mode
.Normal
;
261 if(b
.Plugin
is IOutPlugin
&& b
.Plugin
.WorkDone
)
265 (b
.Plugin
as IOutPlugin
).DisplayResults();
271 new PluginError(e
, b
, true);
274 else if(b
.Plugin
.HasSetup
)
284 new PluginError(e
, b
, true);
289 else if(args
.Button
== 3)
293 status
.Push(1, String
.Format(Catalog
.GetString("{0} menu"), b
.Plugin
.Info
.Name
));
295 Action
.m
= Action
.Mode
.Normal
;
299 if(b
.Plugin
is IOutPlugin
)
301 mi
= new ImageMenuItem(Catalog
.GetString("Display _results"));
302 mi
.Image
= new Image(null, "system-search.png");
303 mi
.Activated
+= PluginResults
;
304 if(!b
.Plugin
.WorkDone
)
305 mi
.Sensitive
= false;
309 if(b
.Plugin
.HasSetup
)
311 mi
= new ImageMenuItem(Catalog
.GetString("_Setup"));
312 mi
.Image
= new Image(null, "preferences-desktop.png");
313 mi
.Activated
+= PluginSetup
;
317 if(b
.Plugin
.HasSetup
|| b
.Plugin
is IOutPlugin
)
318 m
.Append(new SeparatorMenuItem());
320 mi
= new ImageMenuItem(Catalog
.GetString("D_isconnect all"));
321 mi
.Image
= new Image(null, "edit-cut.png");
322 mi
.Activated
+= delegate(object sender
, EventArgs eargs
)
327 status
.Push(1, Catalog
.GetString("Removed all block's connections"));
331 mi
= new ImageMenuItem(Catalog
.GetString("In_validate"));
332 mi
.Image
= new Image(null, "user-trash-full.png");
333 if(b
.CheckState() != Block
.State
.Good
)
334 mi
.Sensitive
= false;
335 mi
.Activated
+= delegate(object sender
, EventArgs eargs
)
340 status
.Push(1, Catalog
.GetString("Invalidated results"));
344 mi
= new ImageMenuItem(Catalog
.GetString("_Delete"));
345 mi
.Image
= new Image(null, "edit-delete.png");
346 mi
.Activated
+= delegate(object sender
, EventArgs eargs
)
349 blocks
.Remove(selected
);
353 status
.Push(1, String
.Format(Catalog
.GetString("Deleted {0} block"), b
.Plugin
.Info
.Name
));
357 m
.Append(new SeparatorMenuItem());
359 mi
= new ImageMenuItem(Catalog
.GetString("_About"));
360 mi
.Image
= new Image(null, "help-browser.png");
361 mi
.Activated
+= delegate(object sender
, EventArgs eargs
) { new PluginAbout(b.Plugin); }
;
368 else if(selected
is Socket
)
370 Socket s
= selected
as Socket
;
374 if(s
.Type
== Socket
.T
.Out
)
377 status
.Push(1, Catalog
.GetString("Connect block with another"));
378 Action
.m
= Action
.Mode
.Connect
;
379 Action
.data
= Connection
.None
;
387 if(args
.Button
== 1 && args
.Type
== Gdk
.EventType
.TwoButtonPress
)
389 status
.Push(1, Catalog
.GetString("Removed connection"));
393 else if(args
.Button
== 3)
396 ImageMenuItem mi
= new ImageMenuItem(Catalog
.GetString("_Disconnect"));
397 mi
.Image
= new Image(null, "edit-cut.png");
398 mi
.Activated
+= delegate(object sender
, EventArgs eargs
)
404 _t
.status
.Push(1, Catalog
.GetString("Removed connection"));
416 Action
.m
= Action
.Mode
.Normal
;
424 private void PluginSetup(object sender
, EventArgs args
)
428 (selected
as Block
).Plugin
.Setup();
432 (selected
as Block
).ShowError
= true;
434 new PluginError(e
, selected
as Block
, true);
438 private void PluginResults(object sender
, EventArgs args
)
442 ((selected
as Block
).Plugin
as IOutPlugin
).DisplayResults();
446 (selected
as Block
).ShowError
= true;
448 new PluginError(e
, selected
as Block
, true);
452 protected override bool OnButtonReleaseEvent(Gdk
.EventButton args
)
457 if(Action
.m
!= Action
.Mode
.Normal
)
460 Action
.m
= Action
.Mode
.Normal
;
462 if(selected
is Socket
)
464 Socket
from = selected
as Socket
;
466 selected
= CheckSelection((int)args
.X
, (int)args
.Y
);
468 if(selected
is Socket
)
470 Socket to
= selected
as Socket
;
472 if(CheckConnection(from, to
) == Connection
.Good
)
474 status
.Push(1, Catalog
.GetString("Connected blocks"));
485 public void Add(IPlugin plugin
)
487 Context c
= Gdk
.Context
.CreateDrawable(this.GdkWindow
);
489 blocks
.Add(new Block(this, c
, plugin
, 10, aa
));
491 ((IDisposable
) c
.Target
).Dispose();
492 ((IDisposable
) c
).Dispose();
500 public void Load(ArrayList blocks
)
502 this.blocks
= blocks
;
510 blocks
= new ArrayList();
516 private void Draw(Context c
)
520 if(MainWindow
.HaveAlpha
)
521 c
.Color
= new Color(BackgroundRed
/255.0, BackgroundGreen
/255.0, BackgroundBlue
/255.0, BackgroundAlpha
/255.0);
523 c
.Color
= new Color(BackgroundRed
/255.0, BackgroundGreen
/255.0, BackgroundBlue
/255.0, 1.0);
525 c
.Operator
= Operator
.Source
;
527 c
.Operator
= Operator
.Over
;
530 foreach(Block b
in blocks
)
533 foreach(Block b
in blocks
)
534 b
.DrawConnections(c
);
536 if(Action
.m
== Action
.Mode
.Connect
)
538 c
.Color
= new Color(0, 0, 0, 0.5);
539 // c.SetDash(new double[1] {5}, 0);
541 c
.MoveTo((selected
as Socket
).PX
+ 6.5, (selected
as Socket
).PY
+ 5);
542 c
.LineTo(tmpx
, tmpy
);
545 // c.SetDash(new double[0] {}, 0);
546 switch((Connection
)Action
.data
)
548 case Connection
.None
:
549 c
.Color
= new Color(1, 1, 1, 0.8);
550 c
.Arc(tmpx
, tmpy
, 4, 0, 2*Math
.PI
);
553 case Connection
.Good
:
554 c
.Color
= new Color(0.3, 1, 0.3, 0.8);
555 c
.Arc(tmpx
, tmpy
, 7, 0, 2*Math
.PI
);
559 c
.Color
= new Color(1, 0.3, 0.3, 0.8);
560 c
.Arc(tmpx
, tmpy
, 7, 0, 2*Math
.PI
);
564 switch((Connection
)Action
.data
)
566 case Connection
.None
:
568 c
.Color
= new Color(0, 0, 0);
571 case Connection
.Good
:
573 c
.Color
= new Color(0, 0.5, 0);
578 c
.Color
= new Color(0.5, 0, 0);