/
gilliamoid.bmx
216 lines (190 loc) · 4.26 KB
/
gilliamoid.bmx
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
Global pieces:tlist=New tlist
Type piece
Field img:timage
Field w:Float, h:Float
Field x# , y#
Field hx#,hy#
Field an#
Field pins:tlist
Field anchored
Field mypin:pin
Method New()
pieces.addlast Self
pins=New tlist
End Method
Function create:piece(img:timage , x# , y#)
p:piece = New piece
p.img = img
p.w = ImageWidth(img)
p.h = ImageHeight(img)
p.hx = p.w / 2
p.hy = p.h / 2
p.mypin=pin.create(0,0)
p.mypin.x=x
p.mypin.y=y
Return p
End Function
Method update()
DrawText pins.count(),0,0
For pp:pin = EachIn pins
pp.rot=mypin.rot+an
pp.x = mypin.x + pp.d * Cos(pp.an + an)
pp.y = mypin.y + pp.d * Sin(pp.an + an)
'DrawText String(pp.x) + "," + String(pp.y) , 0 , 15
'DrawText pp.d,0,30
'DrawLine mypin.x,mypin.y,pp.x,pp.y
Next
End Method
Method attach(p:piece , x# , y#)
p.anchored=1
x1# = x - mypin.x
y1# = y - mypin.y
Print x1
Print y1
d1# = Sqr(x1 * x1 + y1 * y1)
an1#=ATan2(y1,x1)-an
pp:pin = pin.create(an1 , d1)
pins.addlast pp
p.hx# = x - p.mypin.x + p.hx
p.hy# = y - p.mypin.y + p.hy
p.mypin = pp
End Method
Method draw()
SetImageHandle img , hx , hy
SetRotation an+mypin.rot
DrawImage img , mypin.x , mypin.y
SetRotation 0
'SetColor 255,255,0
'DrawOval mypin.x - 5 , mypin.y - 5 , 10 , 10
'SetColor 255,255,255
End Method
Method drag(dx# , dy#)
'DrawRect dx , dy , 5 , 5
'DrawRect 0,0,10,10
If anchored
an#=ATan2(dy-mypin.y,dx-mypin.x)-offan
Else
mypin.x = dx-offx
mypin.y = dy-offy
EndIf
End Method
Method inside(dx# , dy#)
x1# = mypin.x - hx
y1# = mypin.y - hy
x2# = x1 + w
y2# = y1 + h
If dx >= x1 And dx <= x2 And dy >= y1 And dy <= y2
Return 1
Else
Return 0
EndIf
End Method
End Type
Type pin
Field an# , d#
Field x# , y#
Field rot#
Function create:pin(an# , d#)
pp:pin = New pin
pp.an = an
pp.d = d
Return pp
End Function
End Type
win:tgadget = CreateWindow("Gilliamoid" , 0 , 0 , 800 , 800 , Null , WINDOW_TITLEBAR | WINDOW_ACCEPTFILES)
canvas:tgadget=CreateCanvas(0,0,800,800,win)
CreateTimer 60
current:piece = Null
state = 0
Global offx#,offy#,offan#
Global mousestate[4]
While WaitEvent()
Select EventID()
Case EVENT_TIMERTICK
For p:piece = EachIn pieces
p.update()
Next
Select state
Case 0 'nowt
current=Null
For p:piece = EachIn pieces
If p.inside(mx,my)
current = p
offx = mx - p.mypin.x
offy = my - p.mypin.y
offan#=ATan2(offy,offx)-p.an
EndIf
Next
If mousestate[1]
If current
state = 1
EndIf
EndIf
Case 1 'dragging
current.drag(mx, my)
If Not mousestate[1]
state = 0
EndIf
End Select
RedrawGadget canvas
Case EVENT_GADGETPAINT
SetGraphics CanvasGraphics(canvas)
For p:piece = EachIn pieces
p.draw()
Next
Flip
Cls
Case EVENT_MOUSEMOVE
mx = EventX()
my = EventY()
Case EVENT_MOUSEDOWN
mousestate[EventData()] = 1
Case EVENT_MOUSEUP
mousestate[EventData()] = 0
If EventData()=2
If current
If current.anchored
under:piece=Null
For p:piece = EachIn pieces
If p <> current And p.inside(mx,my)
under = p
EndIf
Next
If under
current.attach(under , mx , my)
Else
current.anchored=0
EndIf
Else
current.anchored = 1
under:piece=Null
For p:piece = EachIn pieces
If p <> current And p.inside(mx,my)
under = p
EndIf
Next
If under
under.attach(current,mx,my)
Else
current.hx = mx - current.mypin.x + current.hx
current.hy = my - current.mypin.y + current.hy
current.mypin.x = mx
current.mypin.y = my
EndIf
EndIf
EndIf
EndIf
Case EVENT_WINDOWACCEPT
path$ = EventText()
typ$ = path[Len(path) - 3..]
If typ = "jpg" Or typ = "bmp" Or typ = "gif" Or typ="png"
img:timage = LoadImage(path)
EndIf
p:piece = piece.create(img , mx,my)
Case EVENT_WINDOWCLOSE
FreeGadget canvas
End
Case EVENT_APPTERMINATE
End
End Select
Wend