more love2d transforms
s-ol
1 year, 14 days ago
0 | import Constant, Op, PureOp, Input, T, Struct, sig, evt from require 'alv.base' | |
0 | import Constant, Op, PureOp, Input, T, Array, any from require 'alv.base' | |
1 | 1 | |
2 | 2 | unpack or= table.unpack |
3 | ||
4 | vec2 = Array 2, T.num | |
5 | vec3 = Array 3, T.num | |
6 | vec4 = Array 4, T.num | |
3 | 7 | |
4 | 8 | class DrawId |
5 | 9 | new: => |
15 | 19 | super ... |
16 | 20 | @state or= DrawId! |
17 | 21 | |
18 | pattern = sig['love/shape']*0 | |
22 | pattern = any['love/shape']*0 | |
19 | 23 | setup: (inputs, scope) => |
20 | 24 | shapes = pattern\match inputs |
21 | super [Input.hot shape for shape in *shapes] | |
25 | inputs = [Input.hot shape for shape in *shapes] | |
26 | inputs.num = Input.cold Constant.num #shapes | |
27 | super inputs | |
22 | 28 | |
23 | 29 | tick: => |
24 | 30 | shapes = @unwrap_all! |
31 | for i=1, shapes.num do shapes[i] or= -> | |
25 | 32 | COPILOT.drawlist[@state] = shapes |
26 | 33 | |
27 | 34 | destroy: => |
31 | 38 | meta: |
32 | 39 | name: 'rectangle' |
33 | 40 | summary: "create a rectangle shape." |
34 | examples: { '(love/rectangle mode w h)' } | |
35 | ||
36 | value: class extends PureOp | |
37 | pattern: sig.str + sig.num + sig.num | |
38 | type: T['love/shape'] | |
39 | ||
40 | tick: => | |
41 | { mode, w, h } = @unwrap_all! | |
41 | examples: { '(love/rectangle mode size)', '(love/rectangle mode w h)' } | |
42 | ||
43 | value: class extends PureOp | |
44 | pattern: any.str + (any(vec2) / (any.num + any.num)) | |
45 | type: T['love/shape'] | |
46 | ||
47 | tick: => | |
48 | { mode, size } = @unwrap_all! | |
49 | { w, h } = size | |
42 | 50 | x, y = -w/2, -h/2 |
43 | 51 | |
44 | 52 | @out\set -> |
51 | 59 | examples: { '(love/color r g b [a] shape)' } |
52 | 60 | |
53 | 61 | value: class extends PureOp |
54 | pattern: sig.num\rep(3, 4) + sig['love/shape'] | |
62 | pattern: any.num\rep(3, 4) + any['love/shape'] | |
55 | 63 | type: T['love/shape'] |
56 | 64 | |
57 | 65 | tick: => |
67 | 75 | meta: |
68 | 76 | name: 'translate' |
69 | 77 | summary: "translate a shape." |
70 | examples: { '(love/translate x y hape)' } | |
71 | ||
72 | value: class extends PureOp | |
73 | pattern: sig.num + sig.num + sig['love/shape'] | |
74 | type: T['love/shape'] | |
75 | ||
76 | tick: => | |
77 | { x, y, shape } = @unwrap_all! | |
78 | examples: { '(love/translate [delta] shape)', '(love/translate x y shape)' } | |
79 | ||
80 | value: class extends PureOp | |
81 | pattern: (any(vec2) / (any.num + any.num)) + any['love/shape'] | |
82 | type: T['love/shape'] | |
83 | ||
84 | tick: => | |
85 | { pos, shape } = @unwrap_all! | |
86 | { x, y } = pos | |
78 | 87 | |
79 | 88 | @out\set -> |
80 | 89 | love.graphics.push! |
86 | 95 | meta: |
87 | 96 | name: 'rotate' |
88 | 97 | summary: "rotate a shape." |
89 | examples: { '(love/rotate angle hape)' } | |
90 | ||
91 | value: class extends PureOp | |
92 | pattern: sig.num + sig['love/shape'] | |
98 | examples: { '(love/rotate angle shape)' } | |
99 | ||
100 | value: class extends PureOp | |
101 | pattern: any.num + any['love/shape'] | |
93 | 102 | type: T['love/shape'] |
94 | 103 | |
95 | 104 | tick: => |
100 | 109 | love.graphics.rotate angle |
101 | 110 | shape! |
102 | 111 | love.graphics.pop! |
112 | ||
113 | scale = Constant.meta | |
114 | meta: | |
115 | name: 'scale' | |
116 | summary: "scale a shape." | |
117 | examples: { '(love/scale scale shape)', '(love/scale sx [sy] shape)' } | |
118 | ||
119 | value: class extends PureOp | |
120 | pattern: (any(vec2) / (any.num + -any.num)) + any['love/shape'] | |
121 | type: T['love/shape'] | |
122 | ||
123 | tick: => | |
124 | { pos, shape } = @unwrap_all! | |
125 | { sx, sy } = pos | |
126 | ||
127 | @out\set -> | |
128 | love.graphics.push! | |
129 | love.graphics.scale sx, sy | |
130 | shape! | |
131 | love.graphics.pop! | |
132 | ||
133 | shear = Constant.meta | |
134 | meta: | |
135 | name: 'shear' | |
136 | summary: "shear a shape." | |
137 | examples: { '(love/shear x y shape)' } | |
138 | ||
139 | value: class extends PureOp | |
140 | pattern: (any(vec2) / (any.num + any.num)) + any['love/shape'] | |
141 | type: T['love/shape'] | |
142 | ||
143 | tick: => | |
144 | { pos, shape } = @unwrap_all! | |
145 | { sx, sy } = pos | |
146 | ||
147 | @out\set -> | |
148 | love.graphics.push! | |
149 | love.graphics.shear sx, sy | |
150 | shape! | |
151 | love.graphics.pop! | |
152 | ||
153 | mouse_pos = Constant.meta | |
154 | meta: | |
155 | name: 'mouse-pos' | |
156 | summary: "outputs current mouse position." | |
157 | examples: { '(love/mouse-pos)' } | |
158 | ||
159 | value: class extends Op | |
160 | new: (...) => | |
161 | super ... | |
162 | @out or= vec2\mk_evt! | |
163 | @state = {} | |
164 | ||
165 | setup: => | |
166 | super io: Input.hot T.bang\mk_evt! | |
167 | ||
168 | poll: => | |
169 | x, y = love.mouse.getPosition! | |
170 | if x != @state.x or y != @state.y | |
171 | @state.x, @state.y = x, y | |
172 | if not @inputs.io\dirty! | |
173 | @inputs.io.result\set true | |
174 | true | |
175 | ||
176 | tick: => | |
177 | @out\set { @state.x, @state.y } | |
103 | 178 | |
104 | 179 | Constant.meta |
105 | 180 | meta: |
138 | 213 | |
139 | 214 | :rectangle |
140 | 215 | |
141 | :translate, :rotate | |
216 | :translate, :rotate, :scale, :shear | |
142 | 217 | :color |
218 | ||
219 | 'mouse-pos': mouse_pos |