-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
382 lines (333 loc) · 11.7 KB
/
MainWindow.xaml.cs
File metadata and controls
382 lines (333 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using Schulte.Silverlight;
using Vector = Schulte.Silverlight.Vector;
namespace WriteableBitmapExCurveSample.Wpf
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Stopwatch _stopwatch = Stopwatch.StartNew();
private double _lastTime = 0.0;
private double _lowestFrameTime = double.MaxValue;
public MainWindow()
{
InitializeComponent();
CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
}
private void CompositionTarget_Rendering(object sender, EventArgs e)
{
double timeNow = _stopwatch.ElapsedMilliseconds;
double elapsed = timeNow - _lastTime;
_lowestFrameTime = Math.Min(_lowestFrameTime, elapsed);
FpsCounter.Text = string.Format("FPS: {0:0.0} / Max: {1:0.0}", 1000.0 / elapsed, 1000.0 / _lowestFrameTime);
_lastTime = timeNow;
}
#region Consts
private const int PointSize = 10;
private const int PointSizeHalf = PointSize >> 1;
private const int PointCount = 3000;
#endregion
#region Fields
private WriteableBitmap writeableBmp;
private List<ControlPoint> points;
private ControlPoint PickedPoint;
private Random rand;
private bool isInDelete;
private Plant plant;
#endregion
#region Properties
public float Tension { get; set; }
#endregion
#region Methods
private void Init()
{
// Init vars
rand = new Random();
points = new List<ControlPoint>();
isInDelete = false;
Tension = 0.5f;
// Init plant
int vw = (int)ViewPortContainer.Width;
int vh = (int)ViewPortContainer.Height;
plant = new Plant(new Schulte.Silverlight.Vector(vw >> 1, vh), new Schulte.Silverlight.Vector(1, -1), vw, vh);
plant.BranchLenMin = (int)(vw * 0.17f);
plant.BranchLenMax = plant.BranchLenMin + (plant.BranchLenMin >> 1);
plant.MaxGenerations = 6;
plant.MaxBranchesPerGeneration = 80;
plant.BranchPoints.AddRange(new List<BranchPoint>
{
new BranchPoint(1f, 40), // 40° Right at 100% of branch
new BranchPoint(1f, 5), // 5° Right at 100% of branch
new BranchPoint(1f, -5), // 5° Left at 100% of branch
new BranchPoint(1f, -40), // 40° Left at 100% of branch
});
ChkDemoPerf.Content = String.Format("Perf. Demo {0} points", PointCount);
CheckDemoPlant_Checked(this, null);
this.DataContext = this;
// Init WriteableBitmap
writeableBmp = BitmapFactory.New((int)ViewPortContainer.Width, (int)ViewPortContainer.Height);
ImageViewport.Source = writeableBmp;
// Start render loop
CompositionTarget.Rendering += (s, e) =>
{
if (ChkDemoPlant.IsChecked.Value)
{
plant.Grow();
plant.Draw(this.writeableBmp);
}
else if (ChkDemoPerf.IsChecked.Value)
{
AddRandomPoints();
Draw();
}
};
}
private void AddRandomPoints()
{
int w = (int)ViewPortContainer.Width;
int h = (int)ViewPortContainer.Height;
points.Clear();
for (int i = 0; i < PointCount; i++)
{
points.Add(new ControlPoint(rand.Next(0, w), rand.Next(0, h)));
}
}
private void Draw()
{
if (this.points != null && this.writeableBmp != null)
{
// Wrap updates in a GetContext call, to prevent invalidation and nested locking/unlocking during this block
using (writeableBmp.GetBitmapContext())
{
writeableBmp.Clear();
if (ChkShowPoints.IsChecked.Value)
{
DrawPoints();
}
if (RBBezier.IsChecked.Value)
{
DrawBeziers();
}
else if (RBCardinal.IsChecked.Value)
{
DrawCardinal();
}
}
}
}
private void DrawPoints()
{
foreach (var p in points)
{
DrawPoint(p, Colors.Blue);
}
if (PickedPoint != null)
{
DrawPoint(PickedPoint, Colors.Red);
}
}
private void DrawPoint(ControlPoint p, Color color)
{
var x1 = p.X - PointSizeHalf;
var y1 = p.Y - PointSizeHalf;
var x2 = p.X + PointSizeHalf;
var y2 = p.Y + PointSizeHalf;
writeableBmp.DrawRectangle(x1, y1, x2, y2, color);
}
private void DrawBeziers()
{
if (points.Count > 3)
{
writeableBmp.DrawBeziers(GetPointArray(), Colors.Purple);
}
}
private void DrawCardinal()
{
if (points.Count > 2)
{
writeableBmp.DrawCurve(GetPointArray(), Tension, Colors.Purple);
}
}
private int[] GetPointArray()
{
int[] pts = new int[points.Count * 2];
for (int i = 0; i < points.Count; i++)
{
pts[i * 2] = points[i].X;
pts[i * 2 + 1] = points[i].Y;
}
return pts;
}
private ControlPoint GetMousePoint(MouseEventArgs e)
{
return new ControlPoint(e.GetPosition(ImageViewport));
}
private void RemovePickedPointPoint()
{
if (PickedPoint != null)
{
points.Remove(PickedPoint);
PickedPoint = null;
isInDelete = true;
Draw();
}
}
#endregion
#region Eventhandler
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Init();
}
private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
// Only add new control point is [DEL] wasn't pressed
if (!isInDelete && PickedPoint == null)
{
points.Add(GetMousePoint(e));
}
PickedPoint = null;
isInDelete = false;
Draw();
}
private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Pick control point
var mp = GetMousePoint(e);
PickedPoint = (from p in points
where p.X > mp.X - PointSizeHalf && p.X < mp.X + PointSizeHalf
&& p.Y > mp.Y - PointSizeHalf && p.Y < mp.Y + PointSizeHalf
select p).FirstOrDefault();
Draw();
}
private void Image_MouseMove(object sender, MouseEventArgs e)
{
// Move control point
if (PickedPoint != null)
{
var mp = GetMousePoint(e);
PickedPoint.X = mp.X;
PickedPoint.Y = mp.Y;
Draw();
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
// Delete selected control point
base.OnKeyDown(e);
if (e.Key == Key.Delete)
{
RemovePickedPointPoint();
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
_lowestFrameTime = double.MaxValue;
// Restart plant
if (plant != null && ChkDemoPlant.IsChecked.Value)
{
plant.Clear();
}
// Remove all comtrol points
else if (this.points != null)
{
this.points.Clear();
Draw();
}
}
private void BtnSave_Click(object sender, RoutedEventArgs e)
{
// Take snapshot
var clone = this.writeableBmp.Clone();
// Save as TGA
SaveFileDialog dialog = new SaveFileDialog { Filter = "TGA Image (*.tga)|*.tga" };
if (dialog.ShowDialog().Value)
{
using (var fileStream = dialog.OpenFile())
{
clone.WriteTga(fileStream);
}
}
}
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
_lowestFrameTime = double.MaxValue;
// Refresh
Draw();
}
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
if (SldTension == null)
return;
// Tension only makes sense for cardinal splines
if (RBCardinal != null)
{
if (RBCardinal.IsChecked.Value)
{
SldTension.Opacity = 1;
}
else
{
SldTension.Opacity = 0;
}
}
Draw();
}
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
// Set tension text
if (this.TxtTension != null)
{
this.TxtTension.Text = String.Format("Tension: {0:f2}", Tension);
Draw();
}
// Update plant
if (plant != null && ChkDemoPlant.IsChecked.Value)
{
plant.Tension = Tension;
plant.Draw(this.writeableBmp);
}
}
private void CheckDemoPlant_UnChecked(object sender, RoutedEventArgs e)
{
// Show irrelevant controls for plant growth demo
if (SPCurveMode != null && ChkDemoPerf != null && ChkShowPoints != null)
{
SPCurveMode.Opacity = 1;
ChkDemoPerf.Opacity = 1;
ChkShowPoints.Opacity = 1;
TxtUsage.Opacity = 1;
BtnClear.Content = "Clear";
Draw();
}
}
private void CheckDemoPlant_Checked(object sender, RoutedEventArgs e)
{
// Hide irrelevant controls for plant growth demo
if (SPCurveMode != null && ChkDemoPerf != null && ChkShowPoints != null)
{
SPCurveMode.Opacity = 0;
ChkDemoPerf.Opacity = 0;
ChkShowPoints.Opacity = 0;
TxtUsage.Opacity = 0;
BtnClear.Content = "Restart";
}
}
#endregion
}
}