-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudce_gui_lua.pd_lua
More file actions
771 lines (646 loc) · 33.3 KB
/
Copy pathaudce_gui_lua.pd_lua
File metadata and controls
771 lines (646 loc) · 33.3 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
--link do tutorial de pd lua: https://agraef.github.io/pd-lua/tutorial/pd-lua-intro.html
local audce_gui_lua = pd.Class:new():register("audce_gui_lua")
-- Função utilitária para verificar se ponto está contido
function IsContained(x, y, posX, sizeX, posY, sizeY)
return x >= posX and x <= (posX + sizeX) and y >= posY and y <= (posY + sizeY)
end
-- ==========================================
-- Classes dos objetos bidimensionais.
-- ==========================================
BidimensionalElement = {}
BidimensionalElement.__index = BidimensionalElement
Canvas = setmetatable({}, BidimensionalElement)
Canvas.__index = Canvas
Slider = setmetatable({}, BidimensionalElement)
Slider.__index = Slider
Button = setmetatable({}, BidimensionalElement)
Button.__index = Button
function BidimensionalElement:new(position, size)
local obj = setmetatable({}, self)
obj.position = { x = position.x, y = position.y }
obj.size = { x = size.x, y = size.y }
return obj
end
function Canvas:new(position, size, interiorColor, borderColor, borderSize)
local obj = BidimensionalElement.new(self, position, size)
obj.interiorColor = { r = interiorColor.r, g = interiorColor.g, b = interiorColor.b, a = interiorColor.a or 1 }
obj.borderColor = { r = borderColor.r, g = borderColor.g, b = borderColor.b, a = borderColor.a or 1 }
obj.borderSize = borderSize
return obj
end
function Canvas:draw(g)
g:set_color(self.interiorColor.r, self.interiorColor.g, self.interiorColor.b, self.interiorColor.a)
g:fill_rect(self.position.x, self.position.y, self.size.x, self.size.y)
g:set_color(self.borderColor.r, self.borderColor.g, self.borderColor.b, self.borderColor.a)
g:stroke_rect(self.position.x, self.position.y, self.size.x, self.size.y, self.borderSize)
end
function Slider:new(position, size, interiorColor, borderColor, borderSize, sliderColor, fillColor)
local obj = BidimensionalElement.new(self, position, size)
obj.interiorColor = { r = interiorColor.r, g = interiorColor.g, b = interiorColor.b, a = interiorColor.a or 1 }
obj.borderColor = { r = borderColor.r, g = borderColor.g, b = borderColor.b, a = borderColor.a or 1 }
obj.borderSize = borderSize
obj.sliderColor = { r = sliderColor.r, g = sliderColor.g, b = sliderColor.b, a = sliderColor.a or 1 }
obj.fillColor = { r = fillColor.r, g = fillColor.g, b = fillColor.b, a = fillColor.a or 1 }
obj.sliderIconHeight = 20
obj.sliderCurrentHeight = obj.position.y + obj.size.y - obj.sliderIconHeight
obj.isClicked = false
return obj
end
function Slider:draw(g)
-- Fundo do Slider
g:set_color(self.interiorColor.r, self.interiorColor.g, self.interiorColor.b, self.interiorColor.a)
g:fill_rect(self.position.x, self.position.y, self.size.x, self.size.y)
-- Botão do Slider
g:set_color(self.sliderColor.r, self.sliderColor.g, self.sliderColor.b, self.sliderColor.a)
g:fill_rect(self.position.x, self.sliderCurrentHeight, self.size.x, self.sliderIconHeight)
g:set_color(self.borderColor.r, self.borderColor.g, self.borderColor.b, self.borderColor.a)
g:stroke_rect(self.position.x, self.sliderCurrentHeight, self.size.x, self.sliderIconHeight, self.borderSize)
-- Borda Externa do Slider
g:set_color(self.borderColor.r, self.borderColor.g, self.borderColor.b, self.borderColor.a)
g:stroke_rect(self.position.x, self.position.y, self.size.x, self.size.y, self.borderSize)
end
function Slider:updateClickedState(x, y, state)
if state == "down" then
if IsContained(x, y, self.position.x, self.size.x, self.sliderCurrentHeight, self.sliderIconHeight) then
self.isClicked = true
end
else
self.isClicked = false
end
return self.isClicked
end
function Slider:updatePosition(positionY)
if not self.isClicked then return end
local topLimit = self.position.y
local bottomLimit = self.position.y + self.size.y - self.sliderIconHeight
if positionY < topLimit then
self.sliderCurrentHeight = topLimit
elseif positionY > bottomLimit then
self.sliderCurrentHeight = bottomLimit
else
self.sliderCurrentHeight = positionY
end
end
function Slider:returnCurrentPercentage()
local maxMove = self.size.y - self.sliderIconHeight
return (self.sliderCurrentHeight - self.position.y) / maxMove
end
function Slider:setPercentage(perc)
local maxMove = self.size.y - self.sliderIconHeight
self.sliderCurrentHeight = self.position.y + perc * maxMove
end
function Button:new(position, size, text, interiorColor, borderColor, textColor)
local obj = BidimensionalElement.new(self, position, size)
obj.text = text
obj.interiorColor = { r = interiorColor.r, g = interiorColor.g, b = interiorColor.b, a = interiorColor.a or 1 }
obj.borderColor = { r = borderColor.r, g = borderColor.g, b = borderColor.b, a = borderColor.a or 1 }
obj.textColor = { r = textColor.r, g = textColor.g, b = textColor.b, a = textColor.a or 1 }
obj.isHovered = false
return obj
end
function Button:draw(g)
if self.isHovered then
g:set_color(self.interiorColor.r - 30, self.interiorColor.g - 30, self.interiorColor.b - 30, self.interiorColor.a)
else
g:set_color(self.interiorColor.r, self.interiorColor.g, self.interiorColor.b, self.interiorColor.a)
end
g:fill_rect(self.position.x, self.position.y, self.size.x, self.size.y)
g:set_color(self.borderColor.r, self.borderColor.g, self.borderColor.b, self.borderColor.a)
g:stroke_rect(self.position.x, self.position.y, self.size.x, self.size.y, 1)
g:set_color(self.textColor.r, self.textColor.g, self.textColor.b, self.textColor.a)
g:draw_text(self.text, self.position.x + 5, self.position.y + 12, self.size.x, 15)
end
-- ==========================================
-- Classes dos objetos tridimensionais.
-- ==========================================
TridimensionalElement = {}
TridimensionalElement.__index = TridimensionalElement
Room = setmetatable({}, TridimensionalElement)
Room.__index = Room
SoundObject = setmetatable({}, TridimensionalElement)
SoundObject.__index = SoundObject
function TridimensionalElement:new(position)
local obj = setmetatable({}, self)
obj.vertices = nil
obj.edges = nil
obj.position = { x = position.x, y = position.y, z = position.z }
return obj
end
function Room:new(position, size, wallColor, edgeColor, edgeSize)
local obj = TridimensionalElement.new(self, position)
obj.vertices, obj.edges = GenerateBox(size)
obj.wallColor = { r = wallColor.r, g = wallColor.g, b = wallColor.b, a = wallColor.a or 1 }
obj.edgeColor = { r = edgeColor.r, g = edgeColor.g, b = edgeColor.b, a = edgeColor.a or 1 }
obj.edgeSize = edgeSize
obj.size = {x = size.x, y = size.y, z = size.z}
return obj
end
function Room:draw(g, canvas, cameraDistance, globalAngleY, globalAngleX, showCeilingFlag)
-- Helper para limitar a renderização da sala às bordas do canvas (Clipping)
local function clamp(val, min, max)
if val < min then return min end
if val > max then return max end
return val
end
local minX = canvas.position.x
local maxX = canvas.position.x + canvas.size.x
local minY = canvas.position.y
local maxY = canvas.position.y + canvas.size.y
for i = 1, #self.edges do
local x1, y1 = ProjectVertex(self.vertices[self.edges[i][1]], self.position, cameraDistance, globalAngleY, globalAngleX)
local x2, y2 = ProjectVertex(self.vertices[self.edges[i][2]], self.position, cameraDistance, globalAngleY, globalAngleX)
-- Para não dobrar a linha contra a parede incorretamente, um line-clipping real seria ideal.
-- Mas para seguir a lógica simples:
local out1 = (x1 < minX or x1 > maxX or y1 < minY or y1 > maxY)
local out2 = (x2 < minX or x2 > maxX or y2 < minY or y2 > maxY)
-- Se pelo menos um ponto estiver dentro ou a linha cruzar a tela, nós desenhamos e fazemos clamp rápido.
-- Se ambos estão muito longe do mesmo lado, nem desenhamos:
if (x1 < minX and x2 < minX) or (x1 > maxX and x2 > maxX) or (y1 < minY and y2 < minY) or (y1 > maxY and y2 > maxY) then
-- Fora completamente
else
x1 = clamp(x1, minX, maxX)
x2 = clamp(x2, minX, maxX)
y1 = clamp(y1, minY, maxY)
y2 = clamp(y2, minY, maxY)
g:set_color(self.edgeColor.r, self.edgeColor.g, self.edgeColor.b, self.edgeColor.a)
g:draw_line(x1, y1, x2, y2, self.edgeSize)
end
end
if showCeilingFlag then
-- Vértices acústicos que formam o teto (y = -size.y/2, ou seja, vértices 1, 2, 6, 5)
local cx1, cy1 = ProjectVertex(self.vertices[1], self.position, cameraDistance, globalAngleY, globalAngleX)
local cx2, cy2 = ProjectVertex(self.vertices[2], self.position, cameraDistance, globalAngleY, globalAngleX)
local cx3, cy3 = ProjectVertex(self.vertices[6], self.position, cameraDistance, globalAngleY, globalAngleX)
local cx4, cy4 = ProjectVertex(self.vertices[5], self.position, cameraDistance, globalAngleY, globalAngleX)
local success = pcall(function()
g:set_color(255, 0, 0, 0.1) -- Vidro vermelho translúcido
g:fill_polygon({cx1, cy1, cx2, cy2, cx3, cy3, cx4, cy4})
end)
if not success then
-- Fallback seguro: se a versão do pd-lua não suportar preenchimento poligonal
g:set_color(255, 0, 0, 0.2)
g:draw_line(cx1, cy1, cx2, cy2, 3)
g:draw_line(cx2, cy2, cx3, cy3, 3)
g:draw_line(cx3, cy3, cx4, cy4, 3)
g:draw_line(cx4, cy4, cx1, cy1, 3)
g:draw_line(cx1, cy1, cx3, cy3, 2)
g:draw_line(cx2, cy2, cx4, cy4, 2)
end
end
end
function Room:showVerticesCoords(g, canvas, cameraDistance, globalAngleY, globalAngleX)
g:set_color(0, 0, 0, 1)
for i = 1, #self.vertices do
local x, y = ProjectVertex(self.vertices[i], self.position, cameraDistance, globalAngleY, globalAngleX)
if IsContained(x, y, canvas.position.x, canvas.size.x, canvas.position.y, canvas.size.y) then
local text = string.format("(%d,%d,%d)", self.vertices[i].x, self.vertices[i].y, self.vertices[i].z)
g:draw_text(text, x - 25, y - 5, 100, 15)
end
end
end
function Room:showSize(g, canvas)
g:set_color(0, 0, 0, 1)
local text = string.format("Room size: %d %d %d", self.size.x, self.size.y, self.size.z)
g:draw_text(text, canvas.position.x + canvas.size.x - #text * 6, canvas.position.y + canvas.size.y - 15, 200, 15)
end
function SoundObject:new(position, isActive, name, angle, aperture, id, objType)
local obj = TridimensionalElement.new(self, position)
obj.isActive = isActive
obj.name = name or ""
obj.angle = angle or 0
obj.aperture = aperture or 0
obj.id = id or -1
obj.type = objType or "obj"
return obj
end
function SoundObject:draw(g, x2d, y2d, scaleFactor, color, roomPosition, cameraDistance, globalAngleY, globalAngleX, canvas, objType, showLabelsFlag)
if not self.isActive then return end
local minX = canvas.position.x
local maxX = canvas.position.x + canvas.size.x
local minY = canvas.position.y
local maxY = canvas.position.y + canvas.size.y
-- Limite visual (Clipping) para não desenhar fora do mainCanvas
if x2d < minX or x2d > maxX or y2d < minY or y2d > maxY then
return
end
local raio = 2 * scaleFactor
if raio > 40 then raio = 40 end
if raio < 2 then raio = 2 end
-- Desenha a dispersão direcional se possuir uma abertura
if self.aperture and self.aperture > 0 then
local beamLength = 40
local angleRad = math.rad(self.angle or 0)
local apertureRad = math.rad(self.aperture) / 2
local dirX1 = math.sin(angleRad - apertureRad) * beamLength
local dirZ1 = -math.cos(angleRad - apertureRad) * beamLength
local dirX2 = math.sin(angleRad + apertureRad) * beamLength
local dirZ2 = -math.cos(angleRad + apertureRad) * beamLength
local p1 = { x = self.position.x + dirX1, y = self.position.y, z = self.position.z + dirZ1 }
local p2 = { x = self.position.x + dirX2, y = self.position.y, z = self.position.z + dirZ2 }
local p1x, p1y = ProjectVertex(p1, roomPosition, cameraDistance, globalAngleY, globalAngleX)
local p2x, p2y = ProjectVertex(p2, roomPosition, cameraDistance, globalAngleY, globalAngleX)
g:set_color(color.r, color.g, color.b, 1)
g:draw_line(x2d, y2d, p1x, p1y, 1)
g:draw_line(x2d, y2d, p2x, p2y, 1)
end
if objType == "spk" then
g:set_color(color.r, color.g, color.b, color.a or 1)
g:fill_rect(x2d - raio, y2d - raio, raio * 2, raio * 2)
g:set_color(0, 0, 0)
g:stroke_rect(x2d - raio, y2d - raio, raio * 2, raio * 2, 1)
else
g:set_color(color.r, color.g, color.b, color.a or 1)
g:fill_ellipse(x2d - raio, y2d - raio, raio * 2, raio * 2)
g:set_color(0, 0, 0)
g:stroke_ellipse(x2d - raio, y2d - raio, raio * 2, raio * 2, 1)
end
end
function SoundObject:showInfo(g, canvas, roomPosition, cameraDistance, globalAngleY, globalAngleX)
local x, y = ProjectVertex(self.position, roomPosition, cameraDistance, globalAngleY, globalAngleX)
if IsContained(x, y, canvas.position.x, canvas.size.x, canvas.position.y, canvas.size.y) then
local positionText = string.format("(%.2f, %.2f, %.2f)", self.position.x, self.position.y, self.position.z)
local IDtext = string.format("%s %d: %s", self.type, self.id, self.name)
g:set_color(0, 0, 0, 1)
g:draw_text(IDtext, x + 10, y - 10, 500, 15)
g:draw_text(positionText, x + 10, y + 2, 500, 15)
end
end
-- ==========================================
-- Inicialização e Loop Principal
-- ==========================================
function audce_gui_lua:initialize(name, args)
self.inlets = 1
self.outlets = 1
-- Argumentos: <roomX> <roomY> <roomZ> <canvasW> <canvasH>
self.roomSize = { x = args[1] or 100, y = args[2] or 100, z = args[3] or 100 }
self.canvasSize = { x = args[4] or 560, y = args[5] or 250 }
local toolBarWidth = 60
local bottomBarHeight = 35
self.bidimensionalElements = {
toolBarCanvas = Canvas:new({ x = 0, y = 0 }, { x = toolBarWidth, y = self.canvasSize.y - bottomBarHeight },
{ r = 253, g = 253, b = 253 }, { r = 0, g = 0, b = 0 }, 1),
mainCanvas = Canvas:new({ x = toolBarWidth, y = 0 },
{ x = self.canvasSize.x - toolBarWidth, y = self.canvasSize.y - bottomBarHeight }, { r = 250, g = 250, b = 250 },
{ r = 0, g = 0, b = 0 }, 1),
bottomCanvas = Canvas:new({ x = 0, y = self.canvasSize.y - bottomBarHeight },
{ x = self.canvasSize.x, y = bottomBarHeight }, { r = 230, g = 230, b = 230 }, { r = 0, g = 0, b = 0 }, 1),
zoomSlider = Slider:new({ x = 15, y = 25 }, { x = 30, y = self.canvasSize.y - bottomBarHeight - 50 }, { r = 200, g = 200, b = 200 },
{ r = 0, g = 0, b = 0 }, 1, { r = 100, g = 100, b = 250 }, { r = 0, g = 0, b = 0 }),
btnLabels = Button:new({ x = toolBarWidth + 10, y = self.canvasSize.y - bottomBarHeight + 5 }, { x = 60, y = 25 }, "LABELS", { r = 200, g = 200, b = 200 }, { r = 0, g = 0, b = 0 }, { r = 0, g = 0, b = 0 }),
btnTop = Button:new({ x = toolBarWidth + 80, y = self.canvasSize.y - bottomBarHeight + 5 }, { x = 60, y = 25 }, "PLANTA", { r = 200, g = 200, b = 200 }, { r = 0, g = 0, b = 0 }, { r = 0, g = 0, b = 0 }),
btnCeiling = Button:new({ x = toolBarWidth + 150, y = self.canvasSize.y - bottomBarHeight + 5 }, { x = 60, y = 25 }, "TETO", { r = 200, g = 200, b = 200 }, { r = 0, g = 0, b = 0 }, { r = 0, g = 0, b = 0 }),
btnReset = Button:new({ x = toolBarWidth + 220, y = self.canvasSize.y - bottomBarHeight + 5 }, { x = 60, y = 25 }, "RESET", { r = 200, g = 200, b = 200 }, { r = 0, g = 0, b = 0 }, { r = 0, g = 0, b = 0 })
}
local canvasPosition = {
x = self.bidimensionalElements.mainCanvas.position.x,
y = self.bidimensionalElements
.mainCanvas.position.y
}
local canvasSize = {
x = self.bidimensionalElements.mainCanvas.size.x,
y = self.bidimensionalElements.mainCanvas
.size.y
}
self.tridimensionalElements = {
room = Room:new({ x = canvasPosition.x + canvasSize.x / 2, y = canvasPosition.y + canvasSize.y / 2, z = 0 },
self.roomSize, { r = 0, g = 0, b = 0 }, { r = 0, g = 0, b = 0 }, 1),
sources = {},
speakers = {},
receivers = {},
mics = {}
}
self.maxCameraDistance = math.max(self.roomSize.x, self.roomSize.y, self.roomSize.z) * 2.2
self.cameraDistance = self.maxCameraDistance
self.globalAngleY = 0
self.globalAngleX = 0
self.showLabels = true
self.showCeiling = false
self:set_size(self.canvasSize.x, self.canvasSize.y)
self.lastMouseDrag = { x = 0, y = 0 }
self.draggedObject = nil
return true
end
function audce_gui_lua:paint(g)
self.bidimensionalElements.mainCanvas:draw(g)
self.bidimensionalElements.toolBarCanvas:draw(g)
self.bidimensionalElements.bottomCanvas:draw(g)
self.bidimensionalElements.zoomSlider:draw(g)
self.bidimensionalElements.btnLabels:draw(g)
self.bidimensionalElements.btnTop:draw(g)
self.bidimensionalElements.btnCeiling:draw(g)
self.bidimensionalElements.btnReset:draw(g)
self.tridimensionalElements.room:draw(g, self.bidimensionalElements.mainCanvas, self.cameraDistance,
self.globalAngleY, self.globalAngleX, self.showCeiling)
local renderList = {}
local function addToRenderList(dictionary, colorObj, typeName)
for id, obj in pairs(dictionary) do
local x2d, y2d, scaleFactor, zDepth = ProjectVertex(obj.position, self.tridimensionalElements.room.position,
self.cameraDistance, self.globalAngleY, self.globalAngleX)
table.insert(renderList,
{
type = typeName,
id = id,
object = obj,
color = colorObj,
x = x2d,
y = y2d,
scale = scaleFactor,
z =
zDepth
})
end
end
addToRenderList(self.tridimensionalElements.mics, { r = 255, g = 255, b = 0 }, "mic")
addToRenderList(self.tridimensionalElements.sources, { r = 255, g = 165, b = 0 }, "src")
addToRenderList(self.tridimensionalElements.receivers, { r = 50, g = 255, b = 50 }, "rcv")
addToRenderList(self.tridimensionalElements.speakers, { r = 50, g = 50, b = 255 }, "spk")
table.sort(renderList, function(a, b) return a.z > b.z end)
self.renderedObjectsList = renderList
for i = 1, #renderList do
local item = renderList[i]
item.object:draw(g, item.x, item.y, item.scale, item.color, self.tridimensionalElements.room.position,
self.cameraDistance, self.globalAngleY, self.globalAngleX, self.bidimensionalElements.mainCanvas, item.type, self.showLabels)
end
-- Mesclagem da telemetria visual (Textos e posições)
if self.showLabels then
self.tridimensionalElements.room:showVerticesCoords(g, self.bidimensionalElements.mainCanvas, self.cameraDistance, self.globalAngleY, self.globalAngleX)
for _, dict in pairs({self.tridimensionalElements.sources, self.tridimensionalElements.receivers, self.tridimensionalElements.speakers, self.tridimensionalElements.mics}) do
for id, obj in pairs(dict) do
obj:showInfo(g, self.bidimensionalElements.mainCanvas, self.tridimensionalElements.room.position, self.cameraDistance, self.globalAngleY, self.globalAngleX)
end
end
end
self.tridimensionalElements.room:showSize(g, self.bidimensionalElements.mainCanvas)
end
-- ==========================================
-- Interatividade (Mouse e Controles)
-- ==========================================
function audce_gui_lua:mouse_down(x, y)
self.lastMouseDrag.x = x
self.lastMouseDrag.y = y
if IsContained(x, y, self.bidimensionalElements.btnLabels.position.x, self.bidimensionalElements.btnLabels.size.x, self.bidimensionalElements.btnLabels.position.y, self.bidimensionalElements.btnLabels.size.y) then
self.showLabels = not self.showLabels
self:repaint()
return
end
if IsContained(x, y, self.bidimensionalElements.btnTop.position.x, self.bidimensionalElements.btnTop.size.x, self.bidimensionalElements.btnTop.position.y, self.bidimensionalElements.btnTop.size.y) then
self.globalAngleX = 90
self.globalAngleY = 0
self:repaint()
return
end
if IsContained(x, y, self.bidimensionalElements.btnCeiling.position.x, self.bidimensionalElements.btnCeiling.size.x, self.bidimensionalElements.btnCeiling.position.y, self.bidimensionalElements.btnCeiling.size.y) then
self.showCeiling = not self.showCeiling
self:repaint()
return
end
if IsContained(x, y, self.bidimensionalElements.btnReset.position.x, self.bidimensionalElements.btnReset.size.x, self.bidimensionalElements.btnReset.position.y, self.bidimensionalElements.btnReset.size.y) then
self.globalAngleX = 0
self.globalAngleY = 0
self:repaint()
return
end
if self.bidimensionalElements.zoomSlider:updateClickedState(x, y, "down") then
self:repaint()
return
end
if self.renderedObjectsList then
for i = #self.renderedObjectsList, 1, -1 do
local item = self.renderedObjectsList[i]
local raio = 2 * item.scale
if raio > 40 then raio = 40 end
if raio < 2 then raio = 2 end
local dist = math.sqrt((x - item.x) ^ 2 + (y - item.y) ^ 2)
if dist <= raio + 2 then
self.draggedObject = item
return
end
end
end
end
function audce_gui_lua:mouse_drag(x, y)
if self.bidimensionalElements.zoomSlider.isClicked then
self.bidimensionalElements.zoomSlider:updatePosition(y)
self.cameraDistance = self.bidimensionalElements.zoomSlider:returnCurrentPercentage() * self.maxCameraDistance
self:repaint()
return
end
if self.draggedObject then
local dx = x - self.lastMouseDrag.x
local dy = y - self.lastMouseDrag.y
local radY = math.rad(self.globalAngleY)
local cosY = math.cos(radY)
local sinY = math.sin(radY)
local radX = math.rad(self.globalAngleX)
local cosX = math.cos(radX)
local sinX = math.sin(radX)
local scaleFactor = self.draggedObject.scale
if scaleFactor <= 0 then scaleFactor = 1 end
local move3D_X = dx / scaleFactor
local move3D_Y = dy / scaleFactor
-- Rotação inversa (movendo objeto no eixo local da câmera)
-- Na tela, movemos em X (lateral) e Y (vertical).
-- Primeiro, desfazemos o Pitch (Eixo X da câmera)
local ry_after_pitch = move3D_Y * cosX
local rz_after_pitch = -move3D_Y * sinX
-- Depois, desfazemos o Yaw (Eixo Y global)
local objMoveX = move3D_X * cosY - rz_after_pitch * sinY
local objMoveY = ry_after_pitch
local objMoveZ = move3D_X * sinY + rz_after_pitch * cosY
local obj = self.draggedObject.object
obj.position.x = obj.position.x + objMoveX
obj.position.y = obj.position.y + objMoveY
obj.position.z = obj.position.z + objMoveZ
self:enforceBoundingBox(obj)
-- Aciona a nova função que calcula Cartesian e Ambisonic
self:outputCoordinates(self.draggedObject.type, self.draggedObject.id, obj.name, obj.position.x, obj.position.y,
obj.position.z)
self.lastMouseDrag.x = x
self.lastMouseDrag.y = y
self:repaint()
return
end
if IsContained(x, y, self.bidimensionalElements.mainCanvas.position.x, self.bidimensionalElements.mainCanvas.size.x,
self.bidimensionalElements.mainCanvas.position.y, self.bidimensionalElements.mainCanvas.size.y) then
local fatorY = 0.5
local fatorX = 0.5
self.globalAngleY = self.globalAngleY + (x - self.lastMouseDrag.x) * fatorY
self.globalAngleX = self.globalAngleX + (y - self.lastMouseDrag.y) * fatorX
-- Trava para a câmera não girar de ponta-cabeça (Gimbal lock visual e desorientação)
if self.globalAngleX > 90 then self.globalAngleX = 90 end
if self.globalAngleX < -90 then self.globalAngleX = -90 end
end
self.lastMouseDrag.x = x
self.lastMouseDrag.y = y
self:repaint()
end
function audce_gui_lua:mouse_up(x, y)
self.bidimensionalElements.zoomSlider:updateClickedState(x, y, "up")
self.draggedObject = nil
end
-- ==========================================
-- Comunicação com o Pure Data (Inlets e Outlets)
-- ==========================================
-- Função que traduz os dados de visualização para áudio/matemática
function audce_gui_lua:outputCoordinates(objType, id, objName, x, y, z)
-- 1. Coordenadas Cartesianas
-- Mantendo a lógica nativa em que o teto é negativo (Y < 0).
local cartX = x
local cartY = y
local cartZ = z
self:outlet(1, "list", { "pos_cart_" .. objType, id, objName, cartX, cartY, cartZ })
-- 2. Coordenadas Esféricas / Ambisonics (AED: Azimute, Elevação, Distância)
-- Calculando Distância
local dist = math.sqrt(cartX ^ 2 + cartY ^ 2 + cartZ ^ 2)
-- Azimute (Ângulo horizontal no plano XZ). Padrão: 0 é a frente, + para esquerda/direita dependendo do formato.
-- No Lua 5.3+, usa-se math.atan(y, x) no lugar de math.atan2.
local azi = math.deg(math.atan(cartX, cartZ))
-- Elevação (Ângulo vertical). Padrão: 0 é no horizonte (plano XZ), + é pra cima (Y).
local dist_xz = math.sqrt(cartX ^ 2 + cartZ ^ 2)
local ele = math.deg(math.atan(cartY, dist_xz))
self:outlet(1, "list", { "pos_ambi_" .. objType, id, objName, azi, ele, dist })
end
function audce_gui_lua:in_1_labels(atoms)
if type(atoms) == "table" and atoms[1] == 1 then self.showLabels = true else self.showLabels = false end
self:repaint()
end
function audce_gui_lua:in_1_bang()
-- Output global da sala
self:outlet(1, "list", { "room_info", self.roomSize.x, self.roomSize.y, self.roomSize.z, self.cameraDistance })
for id, mic in pairs(self.tridimensionalElements.mics) do
self:outputCoordinates("mic", id, mic.name, mic.position.x, mic.position.y, mic.position.z)
end
for id, src in pairs(self.tridimensionalElements.sources) do
self:outputCoordinates("src", id, src.name, src.position.x, src.position.y, src.position.z)
end
for id, spk in pairs(self.tridimensionalElements.speakers) do
self:outputCoordinates("spk", id, spk.name, spk.position.x, spk.position.y, spk.position.z)
end
for id, rcv in pairs(self.tridimensionalElements.receivers) do
self:outputCoordinates("rcv", id, rcv.name, rcv.position.x, rcv.position.y, rcv.position.z)
end
end
function audce_gui_lua:enforceBoundingBox(obj)
local halfX, halfY, halfZ = self.roomSize.x / 2, self.roomSize.y / 2, self.roomSize.z / 2
if obj.position.x < -halfX then obj.position.x = -halfX end
if obj.position.x > halfX then obj.position.x = halfX end
if obj.position.y < -halfY then obj.position.y = -halfY end
if obj.position.y > halfY then obj.position.y = halfY end
if obj.position.z < -halfZ then obj.position.z = -halfZ end
if obj.position.z > halfZ then obj.position.z = halfZ end
end
function audce_gui_lua:updateObject(dictionary, id, name, posX, posY, posZ, angle, aperture, objType)
if dictionary[id] then
dictionary[id].position = { x = posX, y = posY, z = posZ }
dictionary[id].name = name
dictionary[id].angle = angle or dictionary[id].angle or 0
dictionary[id].aperture = aperture or dictionary[id].aperture or 0
dictionary[id].type = objType or dictionary[id].type
self:enforceBoundingBox(dictionary[id])
else
dictionary[id] = SoundObject:new({ x = posX, y = posY, z = posZ }, true, name, angle, aperture, id, objType)
self:enforceBoundingBox(dictionary[id])
end
self:repaint()
end
function audce_gui_lua:in_1_source(atoms)
if type(atoms) ~= "table" then return end
local id = tonumber(atoms[1]); if not id then return end
self:updateObject(self.tridimensionalElements.sources, id, tostring(atoms[2] or "Src " .. id),
tonumber(atoms[3]) or 0, tonumber(atoms[4]) or 0, tonumber(atoms[5]) or 0, tonumber(atoms[6]), tonumber(atoms[7]), "SRC")
end
function audce_gui_lua:in_1_speaker(atoms)
if type(atoms) ~= "table" then return end
local id = tonumber(atoms[1]); if not id then return end
self:updateObject(self.tridimensionalElements.speakers, id, tostring(atoms[2] or "Spk " .. id),
tonumber(atoms[3]) or 0, tonumber(atoms[4]) or 0, tonumber(atoms[5]) or 0, tonumber(atoms[6]), tonumber(atoms[7]), "SPK")
end
function audce_gui_lua:in_1_receiver(atoms)
if type(atoms) ~= "table" then return end
local id = tonumber(atoms[1]); if not id then return end
self:updateObject(self.tridimensionalElements.receivers, id, tostring(atoms[2] or "Rcv " .. id),
tonumber(atoms[3]) or 0, tonumber(atoms[4]) or 0, tonumber(atoms[5]) or 0, tonumber(atoms[6]), tonumber(atoms[7]), "RCV")
end
function audce_gui_lua:in_1_mic(atoms)
if type(atoms) ~= "table" then return end
local id = tonumber(atoms[1]); if not id then return end
self:updateObject(self.tridimensionalElements.mics, id, tostring(atoms[2] or "Mic " .. id),
tonumber(atoms[3]) or 0, tonumber(atoms[4]) or 0, tonumber(atoms[5]) or 0, tonumber(atoms[6]), tonumber(atoms[7]), "MIC")
end
function audce_gui_lua:in_1_room_size(atoms)
if type(atoms) ~= "table" then return end
self.roomSize = {
x = tonumber(atoms[1]) or self.roomSize.x,
y = tonumber(atoms[2]) or self.roomSize.y,
z = tonumber(atoms[3]) or self.roomSize.z
}
self.tridimensionalElements.room.vertices, self.tridimensionalElements.room.edges = GenerateBox(self.roomSize)
self.maxCameraDistance = math.max(self.roomSize.x, self.roomSize.y, self.roomSize.z) * 2.2
self.cameraDistance = self.maxCameraDistance
self.bidimensionalElements.zoomSlider:setPercentage(1.0)
-- Força todos os objetos a respeitarem a nova área limite
for _, dict in pairs({self.tridimensionalElements.sources, self.tridimensionalElements.receivers, self.tridimensionalElements.speakers, self.tridimensionalElements.mics}) do
for _, obj in pairs(dict) do
self:enforceBoundingBox(obj)
end
end
self:repaint()
end
function audce_gui_lua:in_1_canvas_size(atoms)
if type(atoms) ~= "table" then return end
local w = tonumber(atoms[1]) or self.canvasSize.x
local h = tonumber(atoms[2]) or self.canvasSize.y
self.canvasSize = { x = w, y = h }
self:set_size(w, h)
local toolBarWidth = 60
local bottomBarHeight = 35
self.bidimensionalElements.toolBarCanvas.size.y = h - bottomBarHeight
self.bidimensionalElements.mainCanvas.size.x = w - toolBarWidth
self.bidimensionalElements.mainCanvas.size.y = h - bottomBarHeight
self.bidimensionalElements.bottomCanvas.position.y = h - bottomBarHeight
self.bidimensionalElements.bottomCanvas.size.x = w
self.bidimensionalElements.zoomSlider.size.y = h - bottomBarHeight - 50
self.bidimensionalElements.zoomSlider:setPercentage(self.cameraDistance / self.maxCameraDistance)
self.bidimensionalElements.btnLabels.position.y = h - bottomBarHeight + 5
self.bidimensionalElements.btnTop.position.y = h - bottomBarHeight + 5
self.bidimensionalElements.btnCeiling.position.y = h - bottomBarHeight + 5
self.bidimensionalElements.btnReset.position.y = h - bottomBarHeight + 5
local canvasPosition = { x = toolBarWidth, y = 0 }
self.tridimensionalElements.room.position = { x = canvasPosition.x + (w - toolBarWidth) / 2, y = canvasPosition.y + (h - bottomBarHeight) / 2, z = 0 }
self:repaint()
end
-- ==========================================
-- Funções de Geometria e Projeção (Core Matemático)
-- ==========================================
function GenerateBox(size)
local x, y, z = size.x / 2, size.y / 2, size.z / 2
local vertices = {
{ x = -x, y = -y, z = -z }, { x = x, y = -y, z = -z }, { x = -x, y = y, z = -z }, { x = x, y = y, z = -z },
{ x = -x, y = -y, z = z }, { x = x, y = -y, z = z }, { x = -x, y = y, z = z }, { x = x, y = y, z = z }
}
local edges = { { 1, 2 }, { 1, 3 }, { 1, 5 }, { 2, 4 }, { 2, 6 }, { 3, 7 }, { 3, 4 }, { 4, 8 }, { 5, 6 }, { 5, 7 }, { 6, 8 }, { 7, 8 } }
return vertices, edges
end
function ProjectVertex(vertex, roomPosition, cameraDistance, globalAngleY, globalAngleX)
local radY = math.rad(globalAngleY or 0)
local cosY = math.cos(radY)
local sinY = math.sin(radY)
local radX = math.rad(globalAngleX or 0)
local cosX = math.cos(radX)
local sinX = math.sin(radX)
-- Rotaciona os pontos em Y (Pan)
local rx1 = vertex.x * cosY + vertex.z * sinY
local ry1 = vertex.y
local rz1 = -vertex.x * sinY + vertex.z * cosY
-- Rotaciona os pontos em X (Pitch - Visão em planta)
local rx2 = rx1
local ry2 = ry1 * cosX - rz1 * sinX
local rz2 = ry1 * sinX + rz1 * cosX
local z = rz2 + cameraDistance
if z <= 0 then z = 0.001 end
local scale = 250
local scaleFactor = scale / z
local x2d = (rx2 / z) * scale
local y2d = (ry2 / z) * scale
return x2d + roomPosition.x, y2d + roomPosition.y, scaleFactor, rz2
end