Skip to content

Commit 6f5aedf

Browse files
committed
fix freezes
1 parent 427fa8a commit 6f5aedf

File tree

4 files changed

+140
-43
lines changed

4 files changed

+140
-43
lines changed

WpfCellLifeSimulationApp/LinegraphicWindow.xaml.cs

+38-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ namespace WpfCellLifeSimulationApp
2121
/// </summary>
2222
public partial class LinegraphicWindow : Window
2323
{
24+
const int maxpoints = 100;
25+
const int even = 2;
26+
27+
int[] counterVal = new int[3] { 1, 1, 1 };
28+
int[] counterEven = new int[3] { 1, 1, 1 };
29+
2430
public LinegraphicWindow()
2531
{
2632
InitializeComponent();
@@ -29,11 +35,39 @@ public LinegraphicWindow()
2935
graph_Hunter.Values = new ChartValues<int>();
3036
}
3137

32-
public void draw(int[] values)
38+
public void addValues(int[] vals)
3339
{
34-
graph_Plant.Values.Add(values[0]);
35-
graph_Planter.Values.Add(values[1]);
36-
graph_Hunter.Values.Add(values[2]);
40+
addChartValue(ref graph_Plant, vals, 0);
41+
addChartValue(ref graph_Planter, vals, 1);
42+
addChartValue(ref graph_Hunter, vals, 2);
43+
}
44+
45+
void addChartValue(ref LiveCharts.Wpf.LineSeries line, int[] vals, int index)
46+
{
47+
if (line.Values.Count > maxpoints)
48+
{
49+
int ind = 0;
50+
List<object> newvals = new();
51+
foreach (var item in line.Values)
52+
{
53+
if (ind % even == 0) newvals.Add(item);
54+
ind++;
55+
};
56+
line.Values.Clear();
57+
line.Values.AddRange(newvals);
58+
counterEven[index] *= even;
59+
};
60+
61+
if (counterVal[index] >= counterEven[index])
62+
{
63+
line.Values.Add(vals[index]);
64+
counterVal[index] = 1;
65+
}
66+
else
67+
{
68+
counterVal[index]++;
69+
}
70+
3771
}
3872

3973
private void onHide(object sender, RoutedEventArgs e)

WpfCellLifeSimulationApp/MainWindow.xaml

+10-1
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,16 @@
6868
Style="{StaticResource MaterialDesignFlatAccentButton}"
6969
VerticalAlignment="Center" HorizontalAlignment="Left" Height="30" Width="33"
7070
Content="-" Click="onClear" Cursor="Hand" FontSize="16" FontWeight="Bold" />
71-
</Grid>
7271

72+
</Grid>
73+
<Button x:Name="ShowGraph"
74+
Grid.Column="2"
75+
mat:ButtonAssist.CornerRadius="6"
76+
Padding="0"
77+
Style="{StaticResource MaterialDesignFlatAccentButton}"
78+
VerticalAlignment="Center" HorizontalAlignment="Left" Height="30" Width="50"
79+
Content="graph" Click="onShowGraph" Cursor="Hand" FontSize="12"
80+
/>
7381
<Grid Grid.Column="3" Margin="16,0,0,0">
7482
<Grid.ColumnDefinitions>
7583
<ColumnDefinition Width="10*"/>
@@ -87,6 +95,7 @@
8795
Style="{StaticResource MaterialDesignToolButton}"
8896
VerticalAlignment="Center" HorizontalAlignment="Right" Height="30" Width="30"
8997
Content="x" Click="onClose" />
98+
9099
</Grid>
91100
</Grid>
92101

WpfCellLifeSimulationApp/MainWindow.xaml.cs

+13-7
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,29 @@ namespace WpfCellLifeSimulationApp
2323
public partial class MainWindow : Window
2424
{
2525
SimDrawController simulation;
26-
LinegraphicWindow graph;
26+
LinegraphicWindow graph = null;
2727
public MainWindow()
2828
{
2929
InitializeComponent();
30-
graph = new();
31-
32-
simulation = new(this.SimView, graph);
33-
simulation.setDispatchTimerInterval(100);
34-
simulation.setTimeSettings(30, 50);
30+
simulation = new();
31+
simulation.setImage(SimView);
32+
simulation.setDispatchTimerInterval(30);
33+
simulation.setTimeSettings(30, 30);
3534
simulation.setCellsReproductionLimit(300);
36-
graph.Show();
3735
}
3836

3937
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
4038
{
4139
this.DragMove();
4240
}
4341

42+
private void onShowGraph(object sender, RoutedEventArgs e)
43+
{
44+
graph = new();
45+
simulation.setGraph(graph);
46+
graph.Show();
47+
}
48+
4449
private void onHide(object sender, RoutedEventArgs e)
4550
{
4651
this.WindowState = WindowState.Minimized;
@@ -49,6 +54,7 @@ private void onHide(object sender, RoutedEventArgs e)
4954
private void onClose(object sender, RoutedEventArgs e)
5055
{
5156
this.Close();
57+
if (graph != null) graph.Close();
5258
}
5359

5460
private void onClear(object sender, RoutedEventArgs e)

WpfCellLifeSimulationApp/SimDrawController.cs

+79-31
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace WpfCellLifeSimulationApp
1515
{
1616
class DTimer : DispatcherTimer
1717
{
18-
public DTimer() : base(System.Windows.Threading.DispatcherPriority.Normal)
18+
public DTimer(DispatcherPriority priority) : base(priority)
1919
{
2020
setInterval(50);
2121
Stop();
@@ -39,45 +39,74 @@ public partial class SimDrawController
3939
{
4040
private SimulationCLR simulation;
4141
private DrawEntity[] frame;
42-
private Canvas view;
43-
private LinegraphicWindow graphic;
42+
private Canvas view = null;
43+
private LinegraphicWindow graphic = null;
4444

45-
private DTimer dispatch_timer;
45+
private DTimer dispatch_timer_image;
46+
private DTimer dispatch_timer_chart;
4647
private Timer simulation_timer;
4748

48-
public SimDrawController(Canvas image, LinegraphicWindow graph)
49+
Brush[] brushes;
50+
51+
public SimDrawController()
4952
{
5053
frame = Array.Empty<DrawEntity>();
5154
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);
5760
simulation_timer = new Timer();
5861
simulation_timer.Elapsed += nextFrameTimerHandler;
5962
setDispatchTimerInterval(30);
6063
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+
};
6170
}
71+
72+
public SimDrawController(Canvas canvas, LinegraphicWindow graphic) : this()
73+
{
74+
setImage(canvas);
75+
setGraph(graphic);
76+
}
77+
6278
~SimDrawController()
6379
{
6480
stop();
6581
}
6682

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+
6794
public bool isrunning()
6895
{
6996
return simulation_timer.Enabled;
7097
}
7198

7299
public void start()
73100
{
74-
dispatch_timer.Start();
101+
dispatch_timer_image.Start();
102+
dispatch_timer_chart.Start();
75103
simulation_timer.Start();
76104
}
77105
public void stop()
78106
{
79107
simulation_timer.Stop();
80-
dispatch_timer.Stop();
108+
dispatch_timer_chart.Stop();
109+
dispatch_timer_image.Stop();
81110
}
82111

83112
public void setTimeSettings(float simulation_timelapse, int timer_interval)
@@ -88,18 +117,51 @@ public void setTimeSettings(float simulation_timelapse, int timer_interval)
88117

89118
public void setDispatchTimerInterval(int timer_interval)
90119
{
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);
92122
}
93123

94124
private void nextFrameTimerHandler(Object source, ElapsedEventArgs e)
95125
{
96126
frame = simulation.getNextFrame();
97127
}
98128

99-
private void onDispatchTimerTick()
129+
private void onDispatchTimerImageTick()
100130
{
101-
var countsarr = new int[3] { 0, 0, 0 };
131+
if(view!=null)redrawImage();
132+
}
102133

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;
103165
view.Children.Clear();
104166
foreach (var item in this.frame)
105167
{
@@ -109,24 +171,10 @@ private void onDispatchTimerTick()
109171
double x = item.x - el.Width / 2;
110172
double y = item.y - el.Height / 2;
111173
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)];
127175
view.Children.Add(el);
128176
}
129-
graphic.draw(countsarr);
177+
view.Visibility = Visibility.Visible;
130178
}
131179
};
132180

0 commit comments

Comments
 (0)