-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathMainPage.xaml.cs
More file actions
258 lines (220 loc) · 7.49 KB
/
MainPage.xaml.cs
File metadata and controls
258 lines (220 loc) · 7.49 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
#region Header
//
// Project: WriteableBitmapEx - Silverlight WriteableBitmap extensions
// Description: Main Page of WinPhone Curve demo.
//
// Changed by: $Author: unknown $
// Changed on: $Date: 2015-02-24 20:36:41 +0100 (Di, 24 Feb 2015) $
// Changed in: $Revision: 112951 $
// Project: $URL: https://writeablebitmapex.svn.codeplex.com/svn/trunk/Source/WriteableBitmapExWinPhoneCurveSample/MainPage.xaml.cs $
// Id: $Id: MainPage.xaml.cs 112951 2015-02-24 19:36:41Z unknown $
//
//
// Copyright © 2009-2015 Rene Schulte and WriteableBitmapEx Contributors
//
// This code is open source. Please read the License.txt for details. No worries, we won't sue you! ;)
//
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using WriteableBitmapExCurveSample;
namespace WriteableBitmapExWinPhoneCurveSample
{
public partial class MainPage : PhoneApplicationPage
{
#region Consts
// Minimum size according to the WP7 UI Design Guideline
// http://go.microsoft.com/?linkid=9713252
private const int PointHitZoneSize = 34;
private const int PointHitZoneSizeHalf = PointHitZoneSize >> 1;
private const int PointVisualSize = 20;
private const int PointVisualSizeHalf = PointVisualSize >> 1;
#endregion
#region Fields
private WriteableBitmap writeableBmp;
private List<ControlPoint> points;
private ControlPoint PickedPoint;
#endregion
#region Properties
public float Tension { get; set; }
#endregion
#region Contructors
public MainPage()
{
InitializeComponent();
}
#endregion
#region Methods
private void Init()
{
// Show fps counter
Application.Current.Host.Settings.EnableFrameRateCounter = true;
// Init vars
points = new List<ControlPoint>();
Tension = 0.5f;
this.DataContext = this;
// Init WriteableBitmap
writeableBmp = new WriteableBitmap((int)ViewPortContainer.Width, (int)ViewPortContainer.Height);
Viewport.Source = writeableBmp;
}
private void Draw()
{
if (this.points != null && this.writeableBmp != null)
{
writeableBmp.Clear();
if (ChkShowPoints.IsChecked.Value)
{
DrawPoints();
}
if (RBBezier.IsChecked.Value)
{
DrawBeziers();
}
else if (RBCardinal.IsChecked.Value)
{
DrawCardinal();
}
writeableBmp.Invalidate();
}
}
private void DrawPoints()
{
foreach (var p in points)
{
DrawPoint(p, Colors.Green, PointVisualSizeHalf);
}
if (PickedPoint != null)
{
DrawPoint(PickedPoint, Colors.White, PointHitZoneSizeHalf);
}
}
private void DrawPoint(ControlPoint p, Color color, int halfSizeOfPoint)
{
var x1 = p.X - halfSizeOfPoint;
var y1 = p.Y - halfSizeOfPoint;
var x2 = p.X + halfSizeOfPoint;
var y2 = p.Y + halfSizeOfPoint;
writeableBmp.DrawRectangle(x1, y1, x2, y2, color);
}
private void DrawBeziers()
{
if (points.Count > 3)
{
writeableBmp.DrawBeziers(GetPointArray(), Colors.Yellow);
}
}
private void DrawCardinal()
{
if (points.Count > 2)
{
writeableBmp.DrawCurve(GetPointArray(), Tension, Colors.Yellow);
}
}
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(Viewport));
}
#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 (PickedPoint == null)
{
points.Add(GetMousePoint(e));
}
PickedPoint = null;
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 - PointHitZoneSizeHalf && p.X < mp.X + PointHitZoneSizeHalf
&& p.Y > mp.Y - PointHitZoneSizeHalf && p.Y < mp.Y + PointHitZoneSizeHalf
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();
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// Remove all comtrol points
if (this.points != null)
{
this.points.Clear();
Draw();
}
}
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
// Refresh
Draw();
}
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
// 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();
}
}
private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
if (this.points != null && points.Count > 0)
{
points.RemoveAt(points.Count - 1);
}
Draw();
e.Cancel = true;
}
#endregion
}
}