-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwc.fsx
186 lines (141 loc) · 5.11 KB
/
lwc.fsx
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
open System.Windows.Forms
open System.Drawing
type W2V() =
let mutable w2v = new Drawing2D.Matrix()
let mutable v2w = new Drawing2D.Matrix()
member this.Translate(tx, ty) =
w2v.Translate(tx, ty)
v2w.Translate(-tx, -ty, Drawing2D.MatrixOrder.Append)
member this.Rotate(a) =
w2v.Rotate(a)
v2w.Rotate(-a, Drawing2D.MatrixOrder.Append)
member this.Scale(sx, sy) =
w2v.Scale(sx, sy)
v2w.Scale(1.f/sx, 1.f/sy, Drawing2D.MatrixOrder.Append)
member this.W2V with get() = w2v and set(m) = w2v <- m
member this.V2W with get() = v2w and set(m) = v2w <- m
let Norm (p:PointF) =
sqrt(p.X*p.X+p.Y*p.Y)
let SubPointF (p:PointF,p2:PointF) =
PointF(p.X-p2.X,p.Y-p2.Y)
let SumPointF (p:PointF,p2:PointF) =
PointF(p.X+p2.X,p.Y+p2.Y)
let MulPointF (p:PointF,d:float32) =
PointF(p.X*d,p.Y*d)
let Rect2RectF (r:Rectangle) =
RectangleF(single r.X, single r.Y, single r.Width, single r.Height)
let RectF2Rect (r:RectangleF) =
Rectangle(int r.X, int r.Y, int r.Width, int r.Height)
let PointsQuiteEqual (p1: PointF, p2: PointF, d: float32) =
let x = p1.X - p2.X
let y = p1.Y - p2.Y
x < d && x > -d && y < d && y > -d
type CoordinateType = View | World
type LWControl() =
let mutable coordinates = View
let mutable position = PointF()
let mutable size = SizeF()
let mutable parent : Control = null
let mousedownevt = new Event<MouseEventArgs>()
let mousemoveevt = new Event<MouseEventArgs>()
let mouseupevt = new Event<MouseEventArgs>()
member this.CoordinateType
with get() = coordinates
and set(v) = coordinates <- v
member this.Position
with get() = position
and set(v) = position <- v
member this.Size
with get() = size
and set(v) = size <- v
member this.Parent
with get() = parent
and set(v) = parent <- v
member this.MouseDown = mousedownevt.Publish
abstract OnMouseDown : MouseEventArgs -> unit
default this.OnMouseDown e = mousedownevt.Trigger(e)
abstract OnMouseMove : MouseEventArgs -> unit
default this.OnMouseMove e = mousemoveevt.Trigger(e)
abstract OnMouseUp : MouseEventArgs -> unit
default this.OnMouseUp e = mouseupevt.Trigger(e)
abstract OnPaint : PaintEventArgs -> unit
default this.OnPaint e = ()
abstract HitTest : PointF -> bool
default this.HitTest p =
(new RectangleF(position, size)).Contains(p)
type LWContainer() as this =
inherit UserControl()
let mutable transform = W2V()
let controls = ResizeArray<LWControl>()
do
this.SetStyle(ControlStyles.AllPaintingInWmPaint ||| ControlStyles.OptimizedDoubleBuffer, true)
member this.Transform with get() = transform
and set(t) = transform <- t
member this.TransformPoint (m:Drawing2D.Matrix) (p:PointF) =
let pts = [| p |]
m.TransformPoints(pts)
pts.[0]
member this.LWControls with get() = controls
override this.OnMouseDown e =
let p = PointF(single e.X, single e.Y)
let controlsView = controls |> Seq.filter (fun c -> c.CoordinateType = View)
match (controlsView |> Seq.tryFind (fun c -> c.HitTest p)) with
| Some c -> c.OnMouseDown(e)
| None ->
let pw = this.TransformPoint transform.V2W p
let controlsWorld = controls |> Seq.filter (fun c -> c.CoordinateType = World)
match (controlsWorld |> Seq.tryFind(fun c -> c.HitTest pw)) with
| Some c -> c.OnMouseDown(e)
| None -> ()
override this.OnMouseMove e =
let p = PointF(single e.X, single e.Y)
let controlsView = controls |> Seq.filter (fun c -> c.CoordinateType = View)
match (controlsView |> Seq.tryFind (fun c -> c.HitTest p)) with
| Some c -> c.OnMouseMove(e)
| None -> ()
override this.OnMouseUp e =
let p = PointF(single e.X, single e.Y)
let controlsView = controls |> Seq.filter (fun c -> c.CoordinateType = View)
match (controlsView |> Seq.tryFind (fun c -> c.HitTest p)) with
| Some c -> c.OnMouseUp(e)
| None -> ()
override this.OnPaint e =
let g = e.Graphics
let t = g.Transform
g.Transform <- transform.W2V
for idx in (controls.Count - 1) .. -1 .. 0 do
let c = controls.[idx]
if c.CoordinateType = World then
c.OnPaint e
g.Transform <- t
for idx in (controls.Count - 1) .. -1 .. 0 do
let c = controls.[idx]
if c.CoordinateType = View then
c.OnPaint e
override this.OnKeyDown e =
match e.KeyCode with
| Keys.W ->
transform.Translate(0.f, 10.f)
this.Invalidate()
| Keys.A ->
transform.Translate(10.f, 0.f)
this.Invalidate()
| Keys.S ->
transform.Translate(0.f, -10.f)
this.Invalidate()
| Keys.D ->
transform.Translate(-10.f, 0.f)
this.Invalidate()
| Keys.Q ->
transform.Rotate(10.f)
this.Invalidate()
| Keys.E ->
transform.Rotate(-10.f)
this.Invalidate()
| Keys.Z ->
transform.Scale(1.1f, 1.1f)
this.Invalidate()
| Keys.X ->
transform.Scale(1.f/1.1f, 1.f/1.1f)
this.Invalidate()
| _ -> ()