-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.moon
187 lines (156 loc) · 5.16 KB
/
module.moon
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
export love, vec2, lmath, ltable, aabb2
buffer = require 'buffer'
lg = love.graphics
class Module
name: 'undefined'
font: lg.newFont 'font/Quicksand-SemiBold.ttf', 22
labelHeight: 40
inputLabels: {}
outputLabels: {}
padNormal: 32
padTop: 24
new: (@workspace, @pos = vec2!) =>
@widgets = {}
@inputs = {}
@outputs = {}
@inputConnections = {}
@size = vec2 64 * 5, 64 * 4
_process: => error 'process is not implemented'
process: =>
if @generation ~= @workspace.generation
@generation = @workspace.generation
@receiveInputs!
@_process! if @_process
update: (dt) =>
for widget in *@widgets
widget\update dt
@_update dt if @_update
@hoveredWidget = if @workspace.hoveredModule == @
@getWidgetAtPos @getMousePos!
else nil
draw: =>
lg.push 'all'
lg.stencil -> lg.rectangle 'fill', 0, 0, @size.x, @size.y, @labelHeight / 2, nil, 32
lg.setStencilTest "greater", 0
-- Body
lg.setColor 0.15, 0.15, 0.17, 1
lg.rectangle 'fill', 0, 0, @size.x, @size.y
-- Header
lg.setColor 0.17, 0.17, 0.19, 1
lg.rectangle 'fill', 0, 0, @size.x, @labelHeight
-- Title
lg.setFont @font
fx = lmath.round (@size.x - @font\getWidth @name) / 2
fy = lmath.round (@labelHeight - @font\getHeight!) / 2
lg.setColor 0.4, 0.4, 0.45, 1
lg.print @name, fx, fy
@_draw! if @_draw
-- Draw widgets
for _, widget in ltable.ripairs @widgets
lg.push 'all'
lg.translate widget.pos.x, widget.pos.y + @labelHeight
widget\draw!
lg.pop!
lg.setStencilTest!
-- Draw selectedness
if @selected
lg.push 'all'
radius = @labelHeight / 2
lg.setLineWidth 3
lg.setColor 0.4, 0.4, 0.7, 1
lg.rectangle 'line', 0, 0, @size.x, @size.y, radius, nil, 32
lg.pop!
lg.pop!
getWidgetAtPos: (pos) =>
for _, widget in ltable.ripairs @widgets
tl = widget.pos
br = tl + widget.size
return widget if pos.x >= tl.x and pos.y >= tl.y and pos.x < br.x and pos.y < br.y
return nil
getMousePos: => @workspace\getMousePos! - @pos - vec2 0, @labelHeight
getBBox: => aabb2.new @pos.x, @pos.x + @size.x, @pos.y, @pos.y + @size.y
getClientBBox: => @getBBox!\padTop -@labelHeight
getLayoutBBox: => @getClientBBox!\setPosition(0, 0)\padHorizontal(-@padNormal)\padBottom(-@padNormal)\padTop(-@padTop)
snapToGrid: =>
grid = @workspace.gridSize
@pos.x = lmath.roundStep @pos.x, grid.x
@pos.y = lmath.roundStep @pos.y, grid.y
snapIfNeeded: =>
@snapToGrid! if @workspace.snapping
mousepressed: (x, y, button) =>
widget = @getWidgetAtPos vec2 x, y
if widget
wpos = widget.pos
widget\mousepressed x - wpos.x, y - wpos.y, button
if button == 1
@activeWidget = widget
elseif button == 2
@ractiveWidget = widget
return
@_mousepressed x, y, button if @_mousepressed
mousereleased: (x, y, button) =>
if button == 1 and @activeWidget
wpos = @activeWidget.pos
@activeWidget\mousereleased x - wpos.x, y - wpos.y, button
@activeWidget = nil
return
if button == 2 and @ractiveWidget
wpos = @ractiveWidget.pos
@ractiveWidget\mousereleased x - wpos.x, y - wpos.y, button
@ractiveWidget = nil
return
@_mousereleased x, y, button if @_mousereleased
drawSockets: =>
for i = 1, @getInputCount! do @drawSocket i, true
for i = 1, @getOutputCount! do @drawSocket i, false
drawSocket: (index, isInput) =>
pos = isInput and (@getInputPosition index) or (@getOutputPosition index)
if @isSocketHovered index, isInput
lg.setColor 0.7, 0.6, 0.8, 0.1
lg.circle 'fill', pos.x, pos.y, 20, 64
lg.setColor 0.7, 0.6, 0.8, 1
lg.circle 'fill', pos.x, pos.y, 11, 64
else
lg.setColor 0.4, 0.35, 0.7, 1
lg.circle 'fill', pos.x, pos.y, 10, 64
isHovered: => @workspace.hoveredModule == @
isSocketHovered: (index, isInput) =>
hovered = @workspace.hoveredSocket
return false unless hovered
return false if hovered[1] != @
return false if hovered[2] != index
return false if (not not hovered[3]) != (not not isInput)
return true
getBufferSize: => @workspace.bufferSize
getStart: => @generation * @workspace.bufferSize
hasInput: (index) =>
for connection in *@inputConnections
return true if connection[3] == index
return false
setInputCount: (count) =>
while @getInputCount! < count
table.insert @inputs, buffer.new @getBufferSize!
while @getInputCount! > count
table.remove @inputs
setOutputCount: (count) =>
while @getOutputCount! < count
table.insert @outputs, buffer.new @getBufferSize!
while @getOutputCount! > count
table.remove @outputs
connect: (other, otherOutput, input) =>
table.insert @inputConnections, {other, otherOutput, input}
receiveInputs: =>
for i = 1, @getInputCount!
buffer.zero @getInput(i), @getBufferSize!
for _, connection in ipairs @inputConnections
connection[1]\process!
ibuf = @getInput connection[3]
obuf = connection[1]\getOutput connection[2]
for i = 0, @getBufferSize! - 1
ibuf[i] = ibuf[i] + obuf[i]
getInputPosition: (index) => vec2 2, @labelHeight + 24 + 40 * (index - 1)
getOutputPosition: (index) => vec2 @size.x - 2, @labelHeight + 24 + 40 * (index - 1)
getInput: (index) => assert @inputs[index]
getInputCount: => #@inputs
getOutput: (index) => assert @outputs[index]
getOutputCount: => #@outputs