@@ -15,7 +15,7 @@ namespace WpfCellLifeSimulationApp
15
15
{
16
16
class DTimer : DispatcherTimer
17
17
{
18
- public DTimer ( ) : base ( System . Windows . Threading . DispatcherPriority . Normal )
18
+ public DTimer ( DispatcherPriority priority ) : base ( priority )
19
19
{
20
20
setInterval ( 50 ) ;
21
21
Stop ( ) ;
@@ -39,45 +39,74 @@ public partial class SimDrawController
39
39
{
40
40
private SimulationCLR simulation ;
41
41
private DrawEntity [ ] frame ;
42
- private Canvas view ;
43
- private LinegraphicWindow graphic ;
42
+ private Canvas view = null ;
43
+ private LinegraphicWindow graphic = null ;
44
44
45
- private DTimer dispatch_timer ;
45
+ private DTimer dispatch_timer_image ;
46
+ private DTimer dispatch_timer_chart ;
46
47
private Timer simulation_timer ;
47
48
48
- public SimDrawController ( Canvas image , LinegraphicWindow graph )
49
+ Brush [ ] brushes ;
50
+
51
+ public SimDrawController ( )
49
52
{
50
53
frame = Array . Empty < DrawEntity > ( ) ;
51
54
simulation = new SimulationCLR ( ) ;
52
- view = image ;
53
- graphic = graph ;
54
- simulation . setSize ( ( int ) view . Width , ( int ) view . Height ) ;
55
- dispatch_timer = new DTimer ( ) ;
56
- dispatch_timer . addHandler ( onDispatchTimerTick ) ;
55
+
56
+ dispatch_timer_image = new DTimer ( System . Windows . Threading . DispatcherPriority . Normal ) ;
57
+ dispatch_timer_image . addHandler ( onDispatchTimerImageTick ) ;
58
+ dispatch_timer_chart = new DTimer ( System . Windows . Threading . DispatcherPriority . SystemIdle ) ;
59
+ dispatch_timer_chart . addHandler ( onDispatchTimerChartTick ) ;
57
60
simulation_timer = new Timer ( ) ;
58
61
simulation_timer . Elapsed += nextFrameTimerHandler ;
59
62
setDispatchTimerInterval ( 30 ) ;
60
63
setTimeSettings ( 30 , 30 ) ;
64
+
65
+ brushes = new Brush [ 3 ] {
66
+ new SolidColorBrush ( ( Color ) ColorConverter . ConvertFromString ( "SeaGreen" ) ) ,
67
+ new SolidColorBrush ( ( Color ) ColorConverter . ConvertFromString ( "#FF1295D3" ) ) ,
68
+ new SolidColorBrush ( ( Color ) ColorConverter . ConvertFromString ( "Red" ) )
69
+ } ;
61
70
}
71
+
72
+ public SimDrawController ( Canvas canvas , LinegraphicWindow graphic ) : this ( )
73
+ {
74
+ setImage ( canvas ) ;
75
+ setGraph ( graphic ) ;
76
+ }
77
+
62
78
~ SimDrawController ( )
63
79
{
64
80
stop ( ) ;
65
81
}
66
82
83
+ public void setImage ( Canvas image )
84
+ {
85
+ view = image ;
86
+ simulation . setSize ( ( int ) view . Width , ( int ) view . Height ) ;
87
+ }
88
+
89
+ public void setGraph ( LinegraphicWindow graphic )
90
+ {
91
+ this . graphic = graphic ;
92
+ }
93
+
67
94
public bool isrunning ( )
68
95
{
69
96
return simulation_timer . Enabled ;
70
97
}
71
98
72
99
public void start ( )
73
100
{
74
- dispatch_timer . Start ( ) ;
101
+ dispatch_timer_image . Start ( ) ;
102
+ dispatch_timer_chart . Start ( ) ;
75
103
simulation_timer . Start ( ) ;
76
104
}
77
105
public void stop ( )
78
106
{
79
107
simulation_timer . Stop ( ) ;
80
- dispatch_timer . Stop ( ) ;
108
+ dispatch_timer_chart . Stop ( ) ;
109
+ dispatch_timer_image . Stop ( ) ;
81
110
}
82
111
83
112
public void setTimeSettings ( float simulation_timelapse , int timer_interval )
@@ -88,18 +117,51 @@ public void setTimeSettings(float simulation_timelapse, int timer_interval)
88
117
89
118
public void setDispatchTimerInterval ( int timer_interval )
90
119
{
91
- dispatch_timer . setInterval ( timer_interval > 0 ? timer_interval : 100 ) ;
120
+ dispatch_timer_chart . setInterval ( timer_interval > 0 ? timer_interval : 100 ) ;
121
+ dispatch_timer_image . setInterval ( timer_interval > 0 ? timer_interval : 100 ) ;
92
122
}
93
123
94
124
private void nextFrameTimerHandler ( Object source , ElapsedEventArgs e )
95
125
{
96
126
frame = simulation . getNextFrame ( ) ;
97
127
}
98
128
99
- private void onDispatchTimerTick ( )
129
+ private void onDispatchTimerImageTick ( )
100
130
{
101
- var countsarr = new int [ 3 ] { 0 , 0 , 0 } ;
131
+ if ( view != null ) redrawImage ( ) ;
132
+ }
102
133
134
+ private void onDispatchTimerChartTick ( )
135
+ {
136
+ if ( graphic != null ) updateChart ( ) ;
137
+ }
138
+
139
+ private int getColorId ( String colorname )
140
+ {
141
+ return colorname switch
142
+ {
143
+ "Green" => 0 ,
144
+ "Blue" => 1 ,
145
+ "Red" => 2 ,
146
+ _ => throw new ArgumentException ( "Недопустимый код операции" )
147
+ } ;
148
+ }
149
+
150
+ private void updateChart ( )
151
+ {
152
+ graphic . chart . Visibility = Visibility . Hidden ;
153
+ var values = new int [ 3 ] { 0 , 0 , 0 } ;
154
+ foreach ( var item in this . frame )
155
+ {
156
+ values [ getColorId ( item . color . Name ) ] ++ ;
157
+ }
158
+ graphic . addValues ( values ) ;
159
+ graphic . chart . Visibility = Visibility . Visible ;
160
+ }
161
+
162
+ private void redrawImage ( )
163
+ {
164
+ view . Visibility = Visibility . Hidden ;
103
165
view . Children . Clear ( ) ;
104
166
foreach ( var item in this . frame )
105
167
{
@@ -109,24 +171,10 @@ private void onDispatchTimerTick()
109
171
double x = item . x - el . Width / 2 ;
110
172
double y = item . y - el . Height / 2 ;
111
173
el . Margin = new Thickness ( x , y , 0 , 0 ) ;
112
- try
113
- {
114
- Color color = ( Color ) ColorConverter . ConvertFromString ( item . color . Name ) ;
115
-
116
- if ( color == Colors . Green ) { countsarr [ 0 ] ++ ; color = ( Color ) ColorConverter . ConvertFromString ( "SeaGreen" ) ; }
117
- else if ( color == Colors . Blue ) { countsarr [ 1 ] ++ ; color = ( Color ) ColorConverter . ConvertFromString ( "#FF1295D3" ) ; }
118
- else if ( color == Colors . Red ) { countsarr [ 2 ] ++ ; }
119
-
120
- el . Fill = new SolidColorBrush ( color ) ;
121
- }
122
- catch ( Exception err )
123
- {
124
- el . Fill = Brushes . Black ;
125
- }
126
-
174
+ el . Fill = brushes [ getColorId ( item . color . Name ) ] ;
127
175
view . Children . Add ( el ) ;
128
176
}
129
- graphic . draw ( countsarr ) ;
177
+ view . Visibility = Visibility . Visible ;
130
178
}
131
179
} ;
132
180
0 commit comments