aboutsummaryrefslogtreecommitdiff
path: root/src/avp/components/life.py
blob: 374b29950feccd3ae9fdcea88ce23d9e7affadb8 (plain)
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
from PyQt6 import QtCore, QtWidgets
from PyQt6.QtGui import QUndoCommand
from PIL import Image, ImageDraw
import os
import math
import logging


from ..libcomponent import BaseComponent
from ..toolkit.frame import BlankFrame, scale, addShadow
from ..toolkit.visualizer import createSpectrumArray


log = logging.getLogger("AVP.Component.Life")


class Component(BaseComponent):
    name = "Conway's Game of Life"
    version = "2.0.1"

    def widget(self, *args):
        super().widget(*args)
        self.scale = 32
        self.updateGridSize()
        # The initial grid: a "Queen Bee Shuttle"
        # https://conwaylife.com/wiki/Queen_bee_shuttle
        self.startingGrid = set(
            [
                (3, 11),
                (3, 12),
                (4, 11),
                (4, 12),
                (8, 11),
                (9, 10),
                (9, 12),
                (10, 9),
                (10, 13),
                (11, 10),
                (11, 11),
                (11, 12),
                (12, 8),
                (12, 9),
                (12, 13),
                (12, 14),
                (23, 10),
                (23, 11),
                (24, 10),
                (24, 11),
            ]
        )

        # Amount of 'bleed' (off-canvas coordinates) on each side of the grid
        self.bleedSize = 40

        self.page.pushButton_pickImage.clicked.connect(self.pickImage)
        self.trackWidgets(
            {
                "tickRate": self.page.spinBox_tickRate,
                "scale": self.page.spinBox_scale,
                "color": self.page.lineEdit_color,
                "shapeType": self.page.comboBox_shapeType,
                "shadow": self.page.checkBox_shadow,
                "customImg": self.page.checkBox_customImg,
                "showGrid": self.page.checkBox_showGrid,
                "image": self.page.lineEdit_image,
                "kaleidoscope": self.page.checkBox_kaleidoscope,
                "sensitivity": self.page.spinBox_sensitivity,
            },
            colorWidgets={
                "color": self.page.pushButton_color,
            },
        )
        self.shiftButtons = (
            self.page.toolButton_up,
            self.page.toolButton_down,
            self.page.toolButton_left,
            self.page.toolButton_right,
        )

        def shiftFunc(i):
            def shift():
                self.shiftGrid(i)

            return shift

        shiftFuncs = [shiftFunc(i) for i in range(len(self.shiftButtons))]
        for i, widget in enumerate(self.shiftButtons):
            widget.clicked.connect(shiftFuncs[i])
        self.page.spinBox_scale.setValue(self.scale)
        self.page.spinBox_scale.valueChanged.connect(self.updateGridSize)

    def pickImage(self):
        imgDir = self.settings.value("componentDir", os.path.expanduser("~"))
        filename, _ = QtWidgets.QFileDialog.getOpenFileName(
            self.page,
            "Choose Image",
            imgDir,
            "Image Files (%s)" % " ".join(self.core.imageFormats),
        )
        if filename:
            self.settings.setValue("componentDir", os.path.dirname(filename))
            self.mergeUndo = False
            self.page.lineEdit_image.setText(filename)
            self.mergeUndo = True

    def shiftGrid(self, d):
        action = ShiftGrid(self, d)
        self.parent.undoStack.push(action)

    def update(self):
        self.updateGridSize()

        # Hide/show widgets depending on state of "custom image" checkbox
        if self.page.checkBox_customImg.isChecked():
            self.page.label_color.setVisible(False)
            self.page.lineEdit_color.setVisible(False)
            self.page.pushButton_color.setVisible(False)
            self.page.label_shape.setVisible(False)
            self.page.comboBox_shapeType.setVisible(False)
            self.page.label_image.setVisible(True)
            self.page.lineEdit_image.setVisible(True)
            self.page.pushButton_pickImage.setVisible(True)
        else:
            self.page.label_color.setVisible(True)
            self.page.lineEdit_color.setVisible(True)
            self.page.pushButton_color.setVisible(True)
            self.page.label_shape.setVisible(True)
            self.page.comboBox_shapeType.setVisible(True)
            self.page.label_image.setVisible(False)
            self.page.lineEdit_image.setVisible(False)
            self.page.pushButton_pickImage.setVisible(False)

        # Disable audio sensitivity spinbox if not relevant
        if (
            self.page.comboBox_shapeType.currentIndex() < 4
            or self.page.checkBox_customImg.isChecked()
        ):
            self.page.spinBox_sensitivity.setEnabled(True)
        else:
            self.page.spinBox_sensitivity.setEnabled(False)

        # Disable arrow buttons to shift the grid if the grid is empty
        enabled = len(self.startingGrid) > 0
        for widget in self.shiftButtons:
            widget.setEnabled(enabled)

    def previewClickEvent(self, pos, size, button):
        pos = (
            math.ceil((pos[0] / size[0]) * self.gridWidth) - 1,
            math.ceil((pos[1] / size[1]) * self.gridHeight) - 1,
        )
        action = ClickGrid(self, pos, button)
        self.parent.undoStack.push(action)

    def updateGridSize(self):
        w, h = self.core.resolutions[-1].split("x")
        self.gridWidth = int(int(w) / self.scale)
        self.gridHeight = int(int(h) / self.scale)
        self.pxWidth = math.ceil(self.width / self.gridWidth)
        self.pxHeight = math.ceil(self.height / self.gridHeight)

    def previewRender(self):
        image = self.drawGrid(self.startingGrid, self.color)
        image = self.addKaleidoscopeEffect(image)
        if self.shadow:
            image = addShadow(image, 5.00, -2, 2)
        image = self.addGridLines(image)
        return image

    def preFrameRender(self, *args, **kwargs):
        super().preFrameRender(*args, **kwargs)
        self.tickGrids = {0: self.startingGrid}
        if self.sensitivity == 0:
            return

        self.spectrumArray = createSpectrumArray(
            self,
            self.completeAudioArray,
            self.sampleSize,
            0.08,
            0.8,
            20,
            self.progressBarUpdate,
            self.progressBarSetText,
        )

    def properties(self):
        if self.customImg and (not self.image or not os.path.exists(self.image)):
            return ["error"]
        return ["pcm"] if self.sensitivity > 0 else []

    def error(self):
        return "No image selected to represent life."

    def frameRender(self, frameNo):
        tick = math.floor(frameNo / self.tickRate)

        # Compute grid evolution on this frame if it hasn't been computed yet
        if tick not in self.tickGrids:
            self.tickGrids[tick] = self.gridForTick(tick)
        grid = self.tickGrids[tick]

        # Delete old evolution data which we shouldn't need anymore
        if tick - 60 in self.tickGrids:
            del self.tickGrids[tick - 60]

        # Fade difference between previous and current grid
        previousGrid = self.tickGrids.get(tick - 1, set())
        newColor = self.color
        if not self.customImg:
            r, g, b = self.color
            decay = 255 / self.tickRate
            decayAmount = int(decay * (frameNo % self.tickRate))
            decayColor = (
                r,
                g,
                b,
                255 - decayAmount,
            )
            newColor = (r, g, b, min(255, decayAmount * 2))
            previousGridImage = self.drawGrid(
                previousGrid,
                decayColor,
                (
                    None
                    if (not self.customImg and self.shapeType > 3)
                    or self.sensitivity < 1
                    else self.spectrumArray[frameNo * self.sampleSize]
                ),
            )
        image = self.drawGrid(
            grid,
            newColor,
            (
                None
                if (not self.customImg and self.shapeType > 3) or self.sensitivity < 1
                else self.spectrumArray[frameNo * self.sampleSize]
            ),
            grid.intersection(previousGrid),
        )
        if not self.customImg:
            image = Image.alpha_composite(previousGridImage, image)
        image = self.addKaleidoscopeEffect(image)
        if self.shadow:
            image = addShadow(image, 5.00, -2, 2)
        image = self.addGridLines(image)
        return image

    def addGridLines(self, frame):
        if not self.showGrid:
            return frame

        drawer = ImageDraw.Draw(frame)
        w, h = scale(0.05, self.width, self.height, int)
        for x in range(self.pxWidth, self.width, self.pxWidth):
            drawer.rectangle(
                ((x, 0), (x + w, self.height)),
                fill=self.color,
            )
        for y in range(self.pxHeight, self.height, self.pxHeight):
            drawer.rectangle(
                ((0, y), (self.width, y + h)),
                fill=self.color,
            )
        return frame

    def addKaleidoscopeEffect(self, frame):
        if not self.kaleidoscope:
            return frame

        flippedImage = frame.transpose(Image.Transpose.FLIP_TOP_BOTTOM)
        frame.paste(flippedImage, (0, 0), mask=flippedImage)

        flippedImage = frame.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
        frame.paste(flippedImage, (0, 0), mask=flippedImage)

        return frame

    def drawGrid(self, grid, color, spectrumData=None, didntChange=None):
        frame = BlankFrame(self.width, self.height)
        if didntChange is None:
            # this set would contain cell coords that did not change
            # between the previous grid tick and this one
            didntChange = set()

        def drawCustomImg():
            try:
                img = Image.open(self.image)
            except Exception:
                return
            img = img.resize(
                (
                    (self.pxWidth + audioMorphWidth),
                    (self.pxHeight + audioMorphHeight),
                ),
                Image.Resampling.LANCZOS,
            )
            frame.paste(
                img,
                box=(
                    (drawPtX - (audioMorphWidth * 2)),
                    (drawPtY - (audioMorphHeight * 2)),
                ),
            )

        def drawShape(x, y):
            drawer = ImageDraw.Draw(frame)
            rect = (
                (drawPtX - audioMorphWidth, drawPtY - audioMorphHeight),
                (
                    drawPtX + self.pxWidth + audioMorphWidth,
                    drawPtY + self.pxHeight + audioMorphHeight,
                ),
            )
            shape = self.page.comboBox_shapeType.currentText().lower()
            thisCellColor = color if (x, y) not in didntChange else (*color[:3], 255)

            # Rectangle
            if shape == "rectangle":
                drawer.rectangle(rect, fill=thisCellColor)

            # Elliptical
            elif shape == "elliptical":
                drawer.ellipse(rect, fill=thisCellColor)

            tenthX, tenthY = scale(10, self.pxWidth, self.pxHeight, int)
            smallerShape = (
                (
                    drawPtX + tenthX + int(tenthX / 4) - int(audioMorphWidth / 2),
                    drawPtY + tenthY + int(tenthY / 2) - int(audioMorphHeight / 2),
                ),
                (
                    drawPtX
                    + self.pxWidth
                    - tenthX
                    - int(tenthX / 4)
                    + int(audioMorphWidth / 2),
                    drawPtY
                    + self.pxHeight
                    - (tenthY + int(tenthY / 2))
                    + int(audioMorphHeight / 2),
                ),
            )
            outlineShape = (
                (
                    drawPtX + int(tenthX / 4) - audioMorphWidth,
                    drawPtY + int(tenthY / 2) - audioMorphHeight,
                ),
                (
                    drawPtX + self.pxWidth - int(tenthX / 4) + audioMorphWidth,
                    drawPtY + self.pxHeight - int(tenthY / 2) + audioMorphHeight,
                ),
            )
            # Circle
            if shape == "circle":
                drawer.ellipse(outlineShape, fill=thisCellColor)
                drawer.ellipse(smallerShape, fill=(0, 0, 0, 0))

            # Lilypad
            elif shape == "lilypad":
                drawer.pieslice(smallerShape, 290, 250, fill=thisCellColor)

            # Pie
            elif shape == "pie":
                drawer.pieslice(outlineShape, 35, 320, fill=thisCellColor)

            hX, hY = scale(50, self.pxWidth, self.pxHeight, int)  # halfline
            tX, tY = scale(33, self.pxWidth, self.pxHeight, int)  # thirdline
            qX, qY = scale(20, self.pxWidth, self.pxHeight, int)  # quarterline

            # Path
            if shape == "path":
                drawer.ellipse(rect, fill=thisCellColor)
                rects = {
                    direction: False
                    for direction in (
                        "up",
                        "down",
                        "left",
                        "right",
                    )
                }
                for cell in self.nearbyCoords(x, y):
                    if cell not in grid:
                        continue
                    if cell[0] == x:
                        if cell[1] < y:
                            rects["up"] = True
                        if cell[1] > y:
                            rects["down"] = True
                    if cell[1] == y:
                        if cell[0] < x:
                            rects["left"] = True
                        if cell[0] > x:
                            rects["right"] = True

                for direction, rect in rects.items():
                    if rect:
                        if direction == "up":
                            sect = (
                                (drawPtX, drawPtY),
                                (drawPtX + self.pxWidth, drawPtY + hY),
                            )
                        elif direction == "down":
                            sect = (
                                (drawPtX, drawPtY + hY),
                                (
                                    drawPtX + self.pxWidth,
                                    drawPtY + self.pxHeight,
                                ),
                            )
                        elif direction == "left":
                            sect = (
                                (drawPtX, drawPtY),
                                (drawPtX + hX, drawPtY + self.pxHeight),
                            )
                        elif direction == "right":
                            sect = (
                                (drawPtX + hX, drawPtY),
                                (
                                    drawPtX + self.pxWidth,
                                    drawPtY + self.pxHeight,
                                ),
                            )
                        drawer.rectangle(sect, fill=thisCellColor)

            # Duck
            elif shape == "duck":
                duckHead = (
                    (drawPtX + qX, drawPtY + qY),
                    (drawPtX + int(qX * 3), drawPtY + int(tY * 2)),
                )
                duckBeak = (
                    (drawPtX + hX, drawPtY + qY),
                    (drawPtX + self.pxWidth + qX, drawPtY + int(qY * 3)),
                )
                duckWing = ((drawPtX, drawPtY + hY), rect[1])
                duckBody = (
                    (drawPtX + int(qX / 4), drawPtY + int(qY * 3)),
                    (drawPtX + int(tX * 2), drawPtY + self.pxHeight),
                )
                drawer.ellipse(duckBody, fill=thisCellColor)
                drawer.ellipse(duckHead, fill=thisCellColor)
                drawer.pieslice(duckWing, 130, 200, fill=thisCellColor)
                drawer.pieslice(duckBeak, 145, 200, fill=thisCellColor)

            # Peace
            elif shape == "peace":
                line = (
                    (
                        drawPtX + hX - int(tenthX / 2),
                        drawPtY + int(tenthY / 2),
                    ),
                    (
                        drawPtX + hX + int(tenthX / 2),
                        drawPtY + self.pxHeight - int(tenthY / 2),
                    ),
                )
                drawer.ellipse(outlineShape, fill=thisCellColor)
                drawer.ellipse(smallerShape, fill=(0, 0, 0, 0))
                drawer.rectangle(line, fill=thisCellColor)

                def slantLine(difference):
                    return (
                        (drawPtX + difference),
                        (drawPtY + self.pxHeight - qY),
                    ), (
                        (drawPtX + hX),
                        (drawPtY + hY),
                    )

                drawer.line(slantLine(qX), fill=thisCellColor, width=tenthX)
                drawer.line(
                    slantLine(self.pxWidth - qX), fill=thisCellColor, width=tenthX
                )

        for x, y in grid:
            drawPtX = x * self.pxWidth
            if drawPtX > self.width or drawPtX + self.pxWidth < 0:
                continue
            drawPtY = y * self.pxHeight
            if drawPtY > self.height or drawPtY + self.pxHeight < 0:
                continue

            audioMorphWidth = (
                0 if spectrumData is None else int(spectrumData[(drawPtX % 63) * 4] / 4)
            )
            audioMorphHeight = (
                0 if spectrumData is None else int(spectrumData[(drawPtY % 63) * 4] / 4)
            )
            if self.customImg:
                drawCustomImg()
            else:
                drawShape(x, y)

        return frame

    def gridForTick(self, tick):
        """
        Given a tick number over 0, returns a new grid (a set of tuples).
        This must compute the previous ticks' grids if not already computed
        """
        if tick - 1 not in self.tickGrids:
            self.tickGrids[tick - 1] = self.gridForTick(tick - 1)

        lastGrid = self.tickGrids[tick - 1]

        def neighbours(x, y):
            return {cell for cell in self.nearbyCoords(x, y) if cell in lastGrid}

        newGrid = set()
        # Copy cells from the previous grid if they have 2 or 3 neighbouring cells
        # and if they are within the grid or its bleed area (off-canvas area)
        for x, y in lastGrid:
            if (
                -self.bleedSize > x > self.gridWidth + self.bleedSize
                or -self.bleedSize > y > self.gridHeight + self.bleedSize
            ):
                continue
            surrounding = len(neighbours(x, y))
            if surrounding == 2 or surrounding == 3:
                newGrid.add((x, y))

        # Find positions around living cells which must be checked for reproduction
        potentialNewCells = {
            coordTup
            for origin in lastGrid
            for coordTup in list(self.nearbyCoords(*origin))
        }
        # Check for reproduction
        for x, y in potentialNewCells:
            if (x, y) in newGrid:
                # Ignore non-empty cell
                continue
            surrounding = len(neighbours(x, y))
            if surrounding == 3:
                newGrid.add((x, y))

        return newGrid

    def savePreset(self):
        pr = super().savePreset()
        pr["GRID"] = sorted(self.startingGrid)
        return pr

    def loadPreset(self, pr, *args):
        self.startingGrid = set(pr["GRID"])
        if self.startingGrid:
            for widget in self.shiftButtons:
                widget.setEnabled(True)
        super().loadPreset(pr, *args)

    def nearbyCoords(self, x, y):
        yield x + 1, y + 1
        yield x + 1, y - 1
        yield x - 1, y + 1
        yield x - 1, y - 1
        yield x, y + 1
        yield x, y - 1
        yield x + 1, y
        yield x - 1, y


class ClickGrid(QUndoCommand):
    def __init__(self, comp, pos, button):
        super().__init__("click %s component #%s" % (comp.name, comp.compPos))
        self.comp = comp
        self.pos = [pos]
        if button == QtCore.Qt.MouseButton.RightButton:
            self.button = 2
        else:
            self.button = 1

    def id(self):
        return self.button

    def mergeWith(self, other):
        self.pos.extend(other.pos)
        return True

    def add(self):
        for pos in self.pos[:]:
            self.comp.startingGrid.add(pos)
        self.comp.update(auto=True)

    def remove(self):
        for pos in self.pos[:]:
            self.comp.startingGrid.discard(pos)
        self.comp.update(auto=True)

    def redo(self):
        if self.button == 1:  # Left-click
            self.add()
        elif self.button == 2:  # Right-click
            self.remove()

    def undo(self):
        if self.button == 1:  # Left-click
            self.remove()
        elif self.button == 2:  # Right-click
            self.add()


class ShiftGrid(QUndoCommand):
    def __init__(self, comp, direction):
        super().__init__("change %s component #%s" % (comp.name, comp.compPos))
        self.comp = comp
        self.direction = direction
        self.distance = 1

    def id(self):
        return self.direction

    def mergeWith(self, other):
        self.distance += other.distance
        return True

    def newGrid(self, Xchange, Ychange):
        return {(x + Xchange, y + Ychange) for x, y in self.comp.startingGrid}

    def redo(self):
        if self.direction == 0:
            newGrid = self.newGrid(0, -self.distance)
        elif self.direction == 1:
            newGrid = self.newGrid(0, self.distance)
        elif self.direction == 2:
            newGrid = self.newGrid(-self.distance, 0)
        elif self.direction == 3:
            newGrid = self.newGrid(self.distance, 0)
        self.comp.startingGrid = newGrid
        self.comp._sendUpdateSignal()

    def undo(self):
        if self.direction == 0:
            newGrid = self.newGrid(0, self.distance)
        elif self.direction == 1:
            newGrid = self.newGrid(0, -self.distance)
        elif self.direction == 2:
            newGrid = self.newGrid(self.distance, 0)
        elif self.direction == 3:
            newGrid = self.newGrid(-self.distance, 0)
        self.comp.startingGrid = newGrid
        self.comp._sendUpdateSignal()