git.s-ol.nu watch-cad / 157f6c0
update examples, fixes s-ol 16 days ago
8 changed file(s) with 106 addition(s) and 78 deletion(s). Raw diff Collapse all Expand all
2323
2424 contains = (INPUT.mouse - pos)\len2! < hs\len2!
2525 INPUT\mouse_event! if contains
26
27 line: (frm, to) ->
28 lg.setColor 1, 1, 1
29 lg.line frm.x, frm.y, to.x, to.y
30
31 -- @TODO: hit checking
2632
2733 rect: (min, max) ->
2834 x, y = min\unpack!
8187 delta = last and pos - last
8288 return delta and delta\len2! > 0 and delta
8389
84 rectangle: (fixed={}) =>
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
90 rectangle: do
91 meta = size: => @max - @min
92 meta.__index = meta
8893
89 min, max = @min!, @max!
90 draw.rect min, max
91 @set { :min, :max }
94 (fixed={}) =>
95 @init setmetatable {}, meta
96 delta = input.point @min, fixed.min or Once random.point! * 0.8
97 input.point @max, fixed.max or Once @min! + random.size!
98 @max\set @max! + delta if delta
99
100 min, max = @min!, @max!
101 draw.rect min, max
102
103 self = @get!
104 self.min, self.max = min, max
92105
93106 circle: (fixed={}) =>
94107 local tangent
98111 radius = (is_once fixed.radius) or math.random! * 100 + 50
99112 init_tangent = vec2.from_cartesian radius, math.random! * 2 * math.pi
100113 input.point @tangent, fixed.tangent or Once @center! + init_tangent
101 @center!\dist @tangent!
114 tangent = @tangent!
115 radius = @center!\dist tangent
116 radius, tangent
102117
103 @tangent\set @tangent! + delta if delta and tangent
118 if delta and tangent
119 @tangent\set @tangent! + delta
104120
105121 center, tangent = @center!, @tangent!
106122 draw.circle center, radius
107 { :center, :tangent, :radius }
123 @set { :center, :tangent, :radius }
108124
109 selection: (init, fixed) =>
110 init or= {o for o in *OBJS when match and o.name\match init}
125 selection: (pattern) =>
126 -- @TOOD: fix
127 @init {o for o in *SESSION.objects when match and o.name\match init}
111128
112129 lg.push 'all'
113130 lg.setLineWidth 5
1 -- let's load a vector library for doing some
2 -- more interesting transformations
3
4 import vec2 from require 'cpml'
5
6 =>
7 -- let's try to construct a pill-shape
8 input.rectangle @rect
9 rect = @rect!
10 hsize = (rect.max - rect.min) / 2
11
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 :)
1 local vec2 = (require 'cpml').vec2
2
3 function draw_star(S)
4 input.circle(S.inner)
5 input.circle(S.outer, { center = S.inner().center })
6
7 local inner, outer = S.inner(), S.outer()
8
9 local cnt = 20
10 local step = 2*math.pi/cnt
11 local lag = inner.center + vec2(inner.radius, 0)
12 for i = 0, 20 do
13 local r = (i % 2 == 0) and inner.radius or outer.radius
14 local pos = inner.center + vec2.from_cartesian(r, step * i)
15
16 draw.cross(pos)
17 draw.line(pos, lag)
18
19 -- obj = Object("object_"..i, pos, vec2(20, 20))
20 -- op.add(obj)
21
22 lag = pos
23 end
24 end
25
26 return draw_star
+0
-40
examples/demo.lua less more
1 -- return function (state)
2 function place_in_rect(state)
3 selection = Select_objs(state.objs, '^led-')
4 rect = Select_rect(state.dest)
5
6 sx = rect.w / (#selection + 1)
7
8 x = sx
9 for i, obj in ipairs(selection) do
10 Move(obj, rect.x + x - obj.w/2, rect.y)
11 x = x + sx
12 end
13 end
14
15 function create_star(state)
16 local inner = Select_circle(state.inner)
17 local outer = Select_circle(state.outer, inner.cx, inner.cy)
18
19 cnt = 20
20 step = 2*math.pi/cnt
21 lag_x, lag_y = inner.cx + inner.r, inner.cy
22 for i = 0, 20 do
23 r = (i % 2 == 0) and inner.r or outer.r
24 x = inner.cx + math.cos(step * i) * r
25 y = inner.cy + math.sin(step * i) * r
26 obj = Object("object_"..i, x-10, y-10, 20, 20)
27 Add(obj)
28
29 love.graphics.line(lag_x, lag_y, obj:center())
30 lag_x, lag_y = obj:center()
31 end
32 end
33
34 function test(S)
35 rect = input.rectangle(S.rect)
36
37 input.circle(S.circle, { center = rect.min, radius = 100 }, { center = true, radius = true })
38 end
39
40 return test
+0
-25
examples/demo.moon less more
1 -- let's load a vector library for doing some
2 -- more interesting transformations
3
4 import vec2 from require 'cpml'
5
6 =>
7 -- let's try to construct a pill-shape
8 input.rectangle @rect
9 rect = @rect!
10 hsize = (rect.max - rect.min) / 2
11
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 :)
1 -- return function (state)
2 function place_in_rect(S)
3 input.selection(S.objs, '^led-')
4 input.rect(S.dest)
5
6 local start = S.min()
7 local step = S.dest():size()
8 step.y = 0
9
10 for i, obj in ipairs(S.objs()) do
11 local pos = start + step:scale(i)
12 draw.cross(pos)
13 -- op.move(obj, pos)
14 end
15 end
16
17 return place_in_rect
1 import vec2 from require 'cpml'
2
3 =>
4 input.point @p
5
6 input.rectangle @re, max: @p!
7
8 input.circle @circ, center: @p!
4242 print @error.msg
4343
4444 reload: =>
45 { :mode, :modification } = lfs.attributes @file
45 { :mode, :modification } = (lfs.attributes @file) or {}
4646 if mode != 'file'
4747 @error = at: 'parse', msg: "script doesn't exist or is not a file: '#{@file}'"
4848 return