git.s-ol.nu watch-cad / 16d2531
update/fix API s-ol 16 days ago
3 changed file(s) with 65 addition(s) and 54 deletion(s). Raw diff Collapse all Expand all
55
66 random =
77 point: ->
8 w, h = love.graphics.getDimensions!
8 w, h = lg.getDimensions!
99 vec2 w*math.random!, h*math.random!
1010
1111 size: ->
4545 half_handle = vec2 15, 15
4646 input =
4747 point: (fixed={}) =>
48 if vec = is_once fixed
49 fixed = {
50 x: Once vec.x
51 y: Once vec.y
52 }
48 last = @init random.point!
49 pos = last\clone!
5350
54 init = random.point!
55 @store 'x', fixed.x, init.x
56 @store 'y', fixed.y, init.y
57 pos = vec2 @x, @y
51 live_x, live_y = (is_live fixed.x), is_live fixed.y
52
53 pos.x = fixed.x if live_x
54 pos.y = fixed.y if live_y
5855
5956 -- both values are 'live', no UI necessary
60 if are_live fixed.x, fixed.y
57 if live_x and live_y
6158 draw.cross pos
62 return pos
59 @set pos
60 delta = pos - last
61 return delta\len2! > 0 and delta
6362
63 -- handle size (square or rect)
6464 hh = half_handle\clone!
65 hh.x /= 2 if is_live fixed.x
66 hh.y /= 2 if is_live fixed.y
65 hh.x /= 2 if live_x
66 hh.y /= 2 if live_y
6767
68 @init 'drag', false
6968 if 'down' == draw.rect pos - hh, pos + hh
70 @drag = true
69 @drag\set true
7170
72 if @drag
73 @drag = false if INPUT\mouse_up!
71 if @drag!
72 if INPUT\mouse_up!
73 @drag\set false
7474
75 pos += INPUT\mouse_delta!
75 delta = INPUT\mouse_delta!
76 delta.x = 0 if live_x
77 delta.y = 0 if live_y
78 pos += delta
7679
77 @store 'x', pos.x unless is_live fixed.x
78 @store 'y', pos.y unless is_live fixed.y
79
80 vec2 @x, @y
81
82 point_relative_to: (other, fixed) =>
83 assert (is_live other), "other needs to be live!"
84
85 @init 'last_other', other
86 @init 'last', fixed or random.point!
87 delta = other - @last_other
88
89 val = if delta\len2! > 0 and not is_live fixed
90 input.point @, @last + delta
91 else
92 input.point @, fixed or Once @last
93
94 @store 'last_other', other
95 @store 'last', val
96
97 val
80 @set pos
81 delta = last and pos - last
82 return delta and delta\len2! > 0 and delta
9883
9984 rectangle: (fixed={}) =>
100 min = input.point @min, fixed.min or Once random.point! * 0.8
101 max = input.point_relative_to @max, min, fixed.max or Once min + random.size!
85 delta = input.point @min, fixed.min or Once random.point! * 0.8
86 input.point @max, fixed.max or Once @min! + random.size!
87 @max\set @max! + delta if delta
10288
89 min, max = @min!, @max!
10390 draw.rect min, max
104 { :min, :max }
91 @set { :min, :max }
10592
10693 circle: (fixed={}) =>
10794 local tangent
108 center = input.point @center, fixed.center
95 delta = input.point @center, fixed.center
96
10997 radius = (is_live fixed.radius) or do
11098 radius = (is_once fixed.radius) or math.random! * 100 + 50
11199 init_tangent = vec2.from_cartesian radius, math.random! * 2 * math.pi
112 tangent = input.point_relative_to @tangent, center, fixed.tangent or Once center + init_tangent
113 center\dist tangent
100 input.point @tangent, fixed.tangent or Once @center! + init_tangent
101 @center!\dist @tangent!
114102
103 @tangent\set @tangent! + delta if delta and tangent
104
105 center, tangent = @center!, @tangent!
115106 draw.circle center, radius
116107 { :center, :tangent, :radius }
117108
1 -- let's load a vector library for doing some
2 -- more interesting transformations
3
14 import vec2 from require 'cpml'
25
36 =>
4 a = input.point @point, x: 200
5 rect = input.rectangle @rect, min: a + (vec2 30, 0), max: { y: 200 }
7 -- let's try to construct a pill-shape
8 input.rectangle @rect
9 rect = @rect!
10 hsize = (rect.max - rect.min) / 2
611
7 circle = input.circle @circ, center: rect.min, tangent: rect.max
12 input.circle @left, {
13 center: rect.min + vec2 0, hsize.y
14 radius: hsize.y
15 }
16 input.circle @left, {
17 center: rect.min + vec2 hsize.x*2, hsize.y
18 radius: hsize.y
19 }
20
21 -- right, so that's that....
22 -- kinda forgetting whether there was something
23 -- else I wanted to show.
24 -- does this seem interesting to you?
25 -- let me know :)
55
66 class Script
77 new: (@file) =>
8 print "loading script '#{@file}'..."
98 @last_modification = 0
10 @func = -> print 'idle'
9 @func = ->
1110
1211 commit: =>
1312 export COMMIT
1514
1615 return unless @func
1716
18 ok, msg = xpcall @func, trace, STATE.state
17 ok, msg = xpcall @func, trace, STATE.root
1918 if not ok
2019 @error = at: 'commit', :msg
2120 return
2625 func = @reload!
2726 @func = func if func
2827
29 ok, msg = xpcall @func, trace, STATE.state
28 ok, msg = xpcall @func, trace, STATE.root
3029 if not ok
3130 @error = at: 'exec', :msg
3231
4342 print @error.msg
4443
4544 reload: =>
46 modification = lfs.attributes @file, 'modification'
45 { :mode, :modification } = lfs.attributes @file
46 if mode != 'file'
47 @error = at: 'parse', msg: "script doesn't exist or is not a file: '#{@file}'"
48 return
4749
4850 if @last_modification < modification
4951 @last_modification = modification