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
|
using Crow.Cairo;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
namespace Crow
{
public class Painter : ScrollingObject
{
#region CTOR
protected Painter () { }
public Painter (Interface iface, string style = null) : base (iface, style) { }
#endregion
public class Point
{
public double X;
public double Y;
}
public enum DrawMode
{
Select,
Lines,
Rect,
Arc,
}
DrawMode currentDrawMode = DrawMode.Select;
public DrawMode CurrentDrawMode {
get => currentDrawMode;
set {
if (value == currentDrawMode)
return;
currentDrawMode = value;
NotifyValueChanged ("CurrentDrawMode", currentDrawMode);
updateMouseCursor ();
}
}
void updateMouseCursor () {
if (currentDrawMode == DrawMode.Select)
MouseCursor = MouseCursor.arrow;
else
MouseCursor = MouseCursor.crosshair;
}
public double[] Zooms => new double[] { 0.5, 1.0, 2.0, 4.0, 8.0, 16.0 };
string path;
double strokeWidth;
Size size;
double zoom;
Fill shapeBackground, shapeForeground;
[DefaultValue(1.0)]
public double Zoom {
get => zoom;
set {
if (zoom == value)
return;
zoom = value;
NotifyValueChangedAuto (zoom);
RegisterForGraphicUpdate ();
updateMaxScrolls ();
}
}
/// <summary>
/// Path expression, for syntax see 'PathParser'.
/// </summary>
public string Path {
get { return path; }
set {
if (path == value)
return;
path = value;
contentSize = default (Size);
NotifyValueChangedAuto (path);
RegisterForGraphicUpdate ();
}
}
/// <summary>
/// Default stroke width, may be overriden by a 'S' command in the path string.
/// </summary>
/// <value>The width of the stoke.</value>
[DefaultValue (1.0)]
public double StrokeWidth {
get { return strokeWidth; }
set {
if (strokeWidth == value)
return;
strokeWidth = value;
contentSize = default (Size);
NotifyValueChangedAuto (strokeWidth);
RegisterForGraphicUpdate ();
}
}
/// <summary>
/// View box size in pixel
/// </summary>
[DefaultValue ("32,32")]
public Size Size {
get { return size; }
set {
if (size == value)
return;
size = value;
NotifyValueChangedAuto (size);
//RegisterForLayouting (LayoutingType.Sizing);
RegisterForGraphicUpdate ();
updateMaxScrolls ();
}
}
[DefaultValue ("White")]
public virtual Fill ShapeBackground {
get => shapeBackground;
set {
if (shapeBackground == value)
return;
shapeBackground = value;
NotifyValueChangedAuto (shapeBackground);
RegisterForRedraw ();
}
}
[DefaultValue ("Black")]
public virtual Fill ShapeForeground {
get => shapeForeground;
set {
if (shapeForeground == value)
return;
shapeForeground = value;
NotifyValueChangedAuto (shapeForeground);
RegisterForRedraw ();
}
}
public override void OnLayoutChanges (LayoutingType layoutType) {
base.OnLayoutChanges (layoutType);
if (layoutType == LayoutingType.Height)
NotifyValueChanged ("PageHeight", Slot.Height);
else if (layoutType == LayoutingType.Width)
NotifyValueChanged ("PageWidth", Slot.Width);
else
return;
updateMaxScrolls ();
}
public override void onMouseEnter (object sender, MouseMoveEventArgs e) {
base.onMouseEnter (sender, e);
updateMouseCursor ();
}
protected override void onDraw (Context gr) {
base.onDraw (gr);
if (string.IsNullOrEmpty (path))
return;
Rectangle cr = ClientRectangle;
gr.Save ();
gr.Translate (-ScrollX, -ScrollY);
gr.Scale (Zoom, Zoom);
Rectangle r = new Rectangle (cr.Position, size);
shapeBackground.SetAsSource (IFace, gr);
gr.Rectangle (r);
gr.Fill ();
gr.LineWidth = strokeWidth;
ShapeForeground.SetAsSource (IFace, gr, cr);
using (PathParser parser = new PathParser (path))
parser.Draw (gr);
gr.Restore ();
}
public override void onMouseWheel (object sender, MouseWheelEventArgs e) {
if (e.Delta > 0)
Zoom *= 2.0;
else
Zoom /= 2.0;
e.Handled = true;
base.onMouseWheel (sender, e);
}
void updateMaxScrolls () {
Rectangle cb = ClientRectangle;
Size scalledSize = size * Zoom;
MaxScrollX = scalledSize.Width - cb.Width;
if (scalledSize.Width > 0)
NotifyValueChanged ("ChildWidthRatio", (double)cb.Width / scalledSize.Width);
MaxScrollY = scalledSize.Height - cb.Height;
if (scalledSize.Height > 0)
NotifyValueChanged ("ChildHeightRatio", (double)cb.Height / scalledSize.Height);
}
}
}
|