55
66using System ;
77using System . Collections . Generic ;
8- using System . Diagnostics ;
98using System . Windows ;
109using System . Windows . Controls ;
11- using System . Windows . Input ;
1210using System . Windows . Media ;
11+ using System . Windows . Media . Animation ;
1312using System . Windows . Shapes ;
14- using System . Windows . Threading ;
1513
1614namespace BrainSimulator
1715{
@@ -84,13 +82,19 @@ public NeuronArrayView()
8482 public DisplayParams Dp { get => dp ; set => dp = value ; }
8583
8684 private Canvas targetNeuronCanvas = null ;
85+ Canvas animationCanvas = null ; //used to animate synapses
8786
8887 //refresh the display of the neuron network
8988 public void Update ( )
9089 {
9190 var watch = System . Diagnostics . Stopwatch . StartNew ( ) ;
9291 NeuronArray theNeuronArray = MainWindow . theNeuronArray ;
9392
93+ if ( animationCanvas != null )
94+ {
95+ theCanvas . Children . Remove ( animationCanvas ) ;
96+ animationCanvas = null ;
97+ }
9498 Canvas labelCanvas = new Canvas ( ) ;
9599 Canvas . SetLeft ( labelCanvas , 0 ) ;
96100 Canvas . SetTop ( labelCanvas , 0 ) ;
@@ -201,7 +205,7 @@ public void Update()
201205 moduleLabel . Background = new SolidColorBrush ( Colors . LightGray ) ;
202206 moduleLabel . Measure ( new Size ( double . PositiveInfinity , double . PositiveInfinity ) ) ;
203207 Canvas . SetLeft ( moduleLabel , Canvas . GetLeft ( r ) ) ;
204- Canvas . SetTop ( moduleLabel , Canvas . GetTop ( r ) - moduleLabel . DesiredSize . Height ) ;
208+ Canvas . SetTop ( moduleLabel , Canvas . GetTop ( r ) - moduleLabel . DesiredSize . Height ) ;
205209 moduleLabel . SetValue ( ShapeType , shapeType . Module ) ;
206210 moduleLabel . SetValue ( ModuleView . AreaNumberProperty , i ) ;
207211 labelCanvas . Children . Add ( moduleLabel ) ;
@@ -311,12 +315,15 @@ public void Update()
311315 {
312316 if ( l is Shape s && s . Fill is SolidColorBrush b && b . Color == Colors . White )
313317 lbl . Foreground = new SolidColorBrush ( Colors . Black ) ;
318+ if ( l is NeuronView . FillableDisc && n . currentCharge == 1 )
319+ lbl . Foreground = new SolidColorBrush ( Colors . Black ) ;
314320 lbl . SetValue ( ShapeType , shapeType . Neuron ) ;
315321 labelCanvas . Children . Add ( lbl ) ;
316322 }
317323
318324 NeuronOnScreen neuronScreenCache = null ;
319- if ( ( n . inUse || n . Label != "" || n . currentCharge != 0 || n . lastCharge != 0 ) && ( l is Ellipse || l is Rectangle ) )
325+ if ( ( n . inUse || n . Label != "" || n . currentCharge != 0 || n . lastCharge != 0 ) &&
326+ ( l is Ellipse || l is Rectangle || l is NeuronView . FillableDisc ) )
320327 {
321328 neuronScreenCache = new NeuronOnScreen ( neuronID , l , - 10 , lbl ) ;
322329 }
@@ -392,13 +399,13 @@ public void Update()
392399 watch . Stop ( ) ;
393400 var elapsedMs = watch . ElapsedMilliseconds ;
394401 if ( synapseCount >= dp . maxSynapsesToDisplay )
395- MainWindow . thisWindow . SetStatus ( 0 , "Too many synapses to display" , 1 ) ;
402+ MainWindow . thisWindow . SetStatus ( 0 , "Too many synapses to display" , 1 ) ;
396403 else
397404 {
398405 if ( ! dp . ShowNeurons ( ) )
399- MainWindow . thisWindow . SetStatus ( 0 , "Grid Size: " + ( boxSize * boxSize ) . ToString ( "#,##" ) , 1 ) ;
406+ MainWindow . thisWindow . SetStatus ( 0 , "Grid Size: " + ( boxSize * boxSize ) . ToString ( "#,##" ) , 1 ) ;
400407 else
401- MainWindow . thisWindow . SetStatus ( 0 , "OK" , 0 ) ;
408+ MainWindow . thisWindow . SetStatus ( 0 , "OK" , 0 ) ;
402409 }
403410 MainWindow . thisWindow . UpdateFreeMem ( ) ;
404411 //Debug.WriteLine("Update Done " + elapsedMs + "ms");
@@ -412,7 +419,7 @@ public void AddNeuronToUpdateList(int neuronID)
412419 Neuron n = MainWindow . theNeuronArray . GetCompleteNeuron ( neuronID ) ;
413420 UIElement l = NeuronView . GetNeuronView ( n , this , out TextBlock lbl ) ;
414421 theCanvas . Children . Add ( l ) ;
415- if ( lbl != null )
422+ if ( lbl != null )
416423 theCanvas . Children . Add ( lbl ) ;
417424 NeuronOnScreen neuronScreenCache = new NeuronOnScreen ( neuronID , l , - 10 , lbl ) ;
418425 neuronsOnScreen . Add ( neuronScreenCache ) ;
@@ -464,11 +471,71 @@ public void UpdateNeuronColors()
464471 //}
465472
466473 SetTargetNeuronSymbol ( ) ;
474+ if ( animationCanvas == null )
475+ {
476+ animationCanvas = new Canvas ( ) ;
477+ Canvas . SetLeft ( animationCanvas , 0 ) ;
478+ Canvas . SetTop ( animationCanvas , 0 ) ;
479+ theCanvas . Children . Add ( animationCanvas ) ;
480+ }
481+ animationCanvas . Children . Clear ( ) ;
467482
468483 for ( int i = 0 ; i < neuronsOnScreen . Count ; i ++ )
469484 {
470485 NeuronOnScreen a = neuronsOnScreen [ i ] ;
471486 Neuron n = MainWindow . theNeuronArray . GetNeuronForDrawing ( a . neuronIndex ) ;
487+
488+ if ( MainWindow . thisWindow . checkBoxAnimate . IsChecked == true && n . lastCharge >= 1 )
489+ {
490+ //synapse animation trial
491+ float electronSize = dp . NeuronDisplaySize * .2f ;
492+ n . synapses = MainWindow . theNeuronArray . GetSynapsesList ( n . id ) ;
493+ Point pStart = dp . pointFromNeuron ( n . id ) ;
494+ pStart . X += - electronSize / 2 + dp . NeuronDisplaySize / 2 ;
495+ pStart . Y += - electronSize / 2 + dp . NeuronDisplaySize / 2 ;
496+ foreach ( Synapse synapse in n . synapses )
497+ {
498+ Point pTarget = dp . pointFromNeuron ( synapse . targetNeuron ) ;
499+ pTarget . X += - electronSize / 2 + dp . NeuronDisplaySize / 2 ;
500+ pTarget . Y += - electronSize / 2 + dp . NeuronDisplaySize / 2 ;
501+ var fill = Brushes . Yellow ;
502+ if ( synapse . weight < 0 )
503+ fill = Brushes . DeepPink ;
504+ // Create the disk (Ellipse)
505+ Ellipse disk = new Ellipse
506+ {
507+ Width = electronSize ,
508+ Height = electronSize ,
509+ Fill = fill
510+ } ;
511+ // Initial position
512+ Canvas . SetLeft ( disk , pStart . X ) ;
513+ Canvas . SetTop ( disk , pStart . Y ) ;
514+ animationCanvas . Children . Add ( disk ) ;
515+ var animX = new DoubleAnimation
516+ {
517+ From = pStart . X ,
518+ To = pTarget . X ,
519+ Duration = TimeSpan . FromMilliseconds ( 500 )
520+
521+ } ;
522+ Storyboard . SetTarget ( animX , disk ) ;
523+ Storyboard . SetTargetProperty ( animX , new PropertyPath ( "(Canvas.Left)" ) ) ;
524+ var animY = new DoubleAnimation
525+ {
526+ From = pStart . Y ,
527+ To = pTarget . Y ,
528+ Duration = TimeSpan . FromMilliseconds ( 500 )
529+ } ;
530+ Storyboard . SetTarget ( animY , disk ) ;
531+ Storyboard . SetTargetProperty ( animY , new PropertyPath ( "(Canvas.Top)" ) ) ;
532+ Storyboard storyboard = new ( ) ;
533+ storyboard . Children . Add ( animX ) ;
534+ storyboard . Children . Add ( animY ) ;
535+ storyboard . Begin ( ) ;
536+ }
537+ }
538+
472539 if ( neuronsOnScreen [ i ] . synapsesOnScreen != null )
473540 {
474541 n . synapses = MainWindow . theNeuronArray . GetSynapsesList ( n . id ) ;
@@ -489,6 +556,15 @@ public void UpdateNeuronColors()
489556 }
490557 }
491558 }
559+ if ( a . graphic is NeuronView . FillableDisc f )
560+ {
561+ float x = n . lastCharge ;
562+ if ( a . label != null && x >= 1 )
563+ a . label . Foreground = new SolidColorBrush ( Colors . Black ) ;
564+ else if ( a . label != null )
565+ a . label . Foreground = new SolidColorBrush ( Colors . White ) ;
566+ f . SetValue ( x ) ;
567+ }
492568 if ( a . graphic is Shape e )
493569 {
494570 float x = n . lastCharge ;
0 commit comments