git.s-ol.nu watch-cad / b651f4d
add line, arrow; hover events s-ol 2 years ago
9 changed file(s) with 123 addition(s) and 45 deletion(s). Raw diff Collapse all Expand all
99
1010 size: ->
1111 vec2 math.random! * 200 + 100, math.random! * 200 + 100
12
13 hit =
14 radius: (pos, r) ->
15 hit.radius2 pos, r*r
16
17 radius2: (pos, r2) ->
18 contains = (INPUT.mouse - pos)\len2! < r2
19 INPUT\mouse_event! or 'hover' if contains
20
21 line: (frm, to, r2=10) ->
22 delta = to - frm
23 mouse = INPUT.mouse - frm
24 dot = (mouse\dot delta) / delta\len2!
25 dot = math.min 1, math.max 0, dot
26 p = frm + delta * dot
27
28 hit.radius2 p, r2
29
30 rect: (min, max) ->
31 rect = bound2 min, max
32 contains = rect\contains INPUT.mouse
33 INPUT\mouse_event! or 'hover' if contains
1234
1335 draw =
1436 cross: (pos) ->
2042 lg.line mx, my, Mx, My
2143 lg.line mx, My, Mx, my
2244
23 contains = (INPUT.mouse - pos)\len2! < hs\len2!
24 INPUT\mouse_event! if contains
45 hit.radius2 pos, hs\len2!
2546
2647 line: (frm, to) ->
2748 lg.setColor 1, 1, 1
2849 lg.line frm.x, frm.y, to.x, to.y
2950
30 -- @TODO: hit checking
51 hit.line frm, to
52
53 arrow: (frm, to) ->
54 dir = (to - frm)\normalize!
55 up = dir\perpendicular!
56 draw.line to - dir * 15 + up * 7, to
57 draw.line to - dir * 15 - up * 7, to
58
59 draw.line frm, to
3160
3261 rect: (min, max) ->
3362 x, y = min\unpack!
3564 lg.setColor 1, 1, 1
3665 lg.rectangle 'line', x, y, w, h
3766
38 rect = bound2 min, max
39 contains = rect\contains INPUT.mouse
40 INPUT\mouse_event! if contains
67 hit.rect min, max
4168
4269 circle: (center, r) ->
4370 x, y = center\unpack!
4471 lg.setColor 1, 1, 1
4572 lg.circle 'line', x, y, r
4673
47 contains = (INPUT.mouse - center)\len! < r
48 INPUT\mouse_event! if contains
74 hit.radius2 center, r*r
4975
5076 half_handle = vec2 15, 15
5177 input =
108134 delta = last and pos - last
109135 return delta and delta\len2! > 0 and delta
110136
111 rectangle: do
137 line: (frm, to) =>
138 input.point @frm, frm
139 input.point @to, to
140
141 frm, to = @frm!, @to!
142 @set :frm, :to
143 draw.line frm, to
144
145 arrow: (frm, to) =>
146 input.point @frm, frm
147 input.point @to, to
148
149 frm, to = @frm!, @to!
150 @set :frm, :to
151 draw.line frm, to
152
153 rect: do
112154 meta = size: => @max - @min
113155 meta.__index = meta
114156
145187
146188 selection: (pattern) =>
147189 -- @TOOD: fix
148 @init {o for o in *SESSION.objects when match and o.name\match init}
149
150 lg.push 'all'
151 lg.setLineWidth 5
152 for obj in *OBJS
153 selected = @selection[obj]
190 @init {o,o for o in *SESSION.objects when match and o.name\match init}
191
192 selection = @!
193 index = {o,i for i,o in ipairs selection}
194
195 to_remove = {}
196 for obj in *SESSION.objects
197 selected = index[obj]
154198 if selected
155 obj\draw 'line', orng
156
157 if obj\hit!
158 @selection[obj] = if selected then nil else obj
159
160 lg.pop!
161
162 [o for o in pairs @selection]
199 draw.cross obj.pos
200 -- obj\draw 'line', orng
201
202 if obj\hit! == 'down'
203 if selected
204 table.insert to_remove, selected
205 else
206 table.insert selection, obj
207
208 table.sort to_remove
209 for i=#to_remove,1,-1
210 table.remove selection, to_remove[i]
211
212 op =
213 move: (obj, to) ->
214 draw.arrow obj.pos, to
215 obj\draw!
163216
164217 {
218 :hit
165219 :random
166220 :draw
167221 :input
222 :op
168223 }
44
55 =>
66 -- let's try to construct a pill-shape
7 input.rectangle @rect
7 input.rect @rect
88 rect = @rect!
99 hsize = (rect.max - rect.min) / 2
1010
+0
-17
examples/place_in_rect.lua less more
0 -- return function (state)
1 function place_in_rect(S)
2 input.selection(S.objs, '^led-')
3 input.rect(S.dest)
4
5 local start = S.min()
6 local step = S.dest():size()
7 step.y = 0
8
9 for i, obj in ipairs(S.objs()) do
10 local pos = start + step:scale(i)
11 draw.cross(pos)
12 -- op.move(obj, pos)
13 end
14 end
15
16 return place_in_rect
0 -- return function (state)
1 function place_in_rect(S)
2 input.selection(S.objs, '^led-')
3 input.line(S.dest)
4
5 local dest = S.dest()
6 local start = dest.frm
7 local step = (dest.to - dest.frm) / #S.objs()
8 start = start - step/2
9
10 for i, obj in ipairs(S.objs()) do
11 local pos = start + step * i
12 op.move(obj, pos)
13 end
14 end
15
16 return place_in_rect
22 =>
33 input.point @p
44
5 input.rectangle @re, max: @p!
5 input.rect @re, max: @p!
66
77 input.circle @circ, center: @p!
44
55 export ^
66
7 for k, v in pairs require 'api'
8 _G[k] = v
9
710 COMMIT = false
811 INPUT = Input!
912 STATE = State!
1013 SESSION = Session Script arg[#arg]
11
12 for k, v in pairs require 'api'
13 _G[k] = v
1414
1515 love.draw = ->
1616 p = INPUT\key_down 'p'
1919 return 'down' if @key_down key
2020 return 'up' if @key_up key
2121 return 'held' if @key_held key
22 return 'hover'
2223
2324 mouse_pos: => @mouse
2425 mouse_delta: => @mouse - @last_mouse
0 import vec2 from require 'cpml'
1 import random, hit, draw from require 'api'
2
3 class Object
4 new: (@pos=random.point!, @size=random.size!) =>
5
6 minmax: =>
7 @pos - @size/2, @pos + @size/2
8
9 hit: =>
10 hit.rect @minmax!
11
12 draw: =>
13 draw.rect @minmax!
14
15 {
16 :Object
17 }
6161 @error = nil
6262 msg
6363
64 import Object from require 'object'
65
6466 class Session
6567 new: (@script) =>
6668 @objects = {}
69 for i=1,4
70 table.insert @objects, Object nil, random.size!/8
6771
6872 frame: =>
6973 for obj in *@objects