why did I use a dict here?
This commit is contained in:
parent
c3f128806b
commit
64da6f14ce
|
@ -4,7 +4,6 @@ import os
|
||||||
import math
|
import math
|
||||||
|
|
||||||
from component import Component
|
from component import Component
|
||||||
from toolkit import alphabetizeDict
|
|
||||||
from toolkit.frame import BlankFrame, scale
|
from toolkit.frame import BlankFrame, scale
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,7 +15,7 @@ class Component(Component):
|
||||||
super().widget(*args)
|
super().widget(*args)
|
||||||
self.scale = 32
|
self.scale = 32
|
||||||
self.updateGridSize()
|
self.updateGridSize()
|
||||||
self.startingGrid = {}
|
self.startingGrid = set()
|
||||||
self.page.pushButton_pickImage.clicked.connect(self.pickImage)
|
self.page.pushButton_pickImage.clicked.connect(self.pickImage)
|
||||||
self.trackWidgets({
|
self.trackWidgets({
|
||||||
'tickRate': self.page.spinBox_tickRate,
|
'tickRate': self.page.spinBox_tickRate,
|
||||||
|
@ -59,7 +58,7 @@ class Component(Component):
|
||||||
def shiftGrid(self, d):
|
def shiftGrid(self, d):
|
||||||
def newGrid(Xchange, Ychange):
|
def newGrid(Xchange, Ychange):
|
||||||
return {
|
return {
|
||||||
(x + Xchange, y + Ychange): True
|
(x + Xchange, y + Ychange)
|
||||||
for x, y in self.startingGrid
|
for x, y in self.startingGrid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,9 +104,9 @@ class Component(Component):
|
||||||
math.ceil((pos[1] / size[1]) * self.gridHeight) - 1
|
math.ceil((pos[1] / size[1]) * self.gridHeight) - 1
|
||||||
)
|
)
|
||||||
if button == 1:
|
if button == 1:
|
||||||
self.startingGrid[pos] = True
|
self.startingGrid.add(pos)
|
||||||
elif button == 2 and pos in self.startingGrid:
|
elif button == 2:
|
||||||
self.startingGrid.pop(pos)
|
self.startingGrid.discard(pos)
|
||||||
|
|
||||||
def updateGridSize(self):
|
def updateGridSize(self):
|
||||||
w, h = self.core.resolutions[-1].split('x')
|
w, h = self.core.resolutions[-1].split('x')
|
||||||
|
@ -223,7 +222,7 @@ class Component(Component):
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
for cell in nearbyCoords(x, y):
|
for cell in nearbyCoords(x, y):
|
||||||
if grid.get(cell) is None:
|
if cell not in grid:
|
||||||
continue
|
continue
|
||||||
if cell[0] == x:
|
if cell[0] == x:
|
||||||
if cell[1] < y:
|
if cell[1] < y:
|
||||||
|
@ -352,40 +351,40 @@ class Component(Component):
|
||||||
return frame
|
return frame
|
||||||
|
|
||||||
def gridForTick(self, tick):
|
def gridForTick(self, tick):
|
||||||
'''Given a tick number over 0, returns a new grid dict of tuples'''
|
'''Given a tick number over 0, returns a new grid set of tuples'''
|
||||||
lastGrid = self.tickGrids[tick - 1]
|
lastGrid = self.tickGrids[tick - 1]
|
||||||
|
|
||||||
def neighbours(x, y):
|
def neighbours(x, y):
|
||||||
return [
|
return {
|
||||||
cell for cell in nearbyCoords(x, y)
|
cell for cell in nearbyCoords(x, y)
|
||||||
if lastGrid.get(cell) is not None
|
if cell in lastGrid
|
||||||
]
|
}
|
||||||
|
|
||||||
newGrid = {}
|
newGrid = set()
|
||||||
for x, y in lastGrid:
|
for x, y in lastGrid:
|
||||||
surrounding = len(neighbours(x, y))
|
surrounding = len(neighbours(x, y))
|
||||||
if surrounding == 2 or surrounding == 3:
|
if surrounding == 2 or surrounding == 3:
|
||||||
newGrid[(x, y)] = True
|
newGrid.add((x, y))
|
||||||
potentialNewCells = set([
|
potentialNewCells = {
|
||||||
coordTup for origin in lastGrid
|
coordTup for origin in lastGrid
|
||||||
for coordTup in list(nearbyCoords(*origin))
|
for coordTup in list(nearbyCoords(*origin))
|
||||||
])
|
}
|
||||||
for x, y in potentialNewCells:
|
for x, y in potentialNewCells:
|
||||||
if (x, y) in newGrid:
|
if (x, y) in newGrid:
|
||||||
continue
|
continue
|
||||||
surrounding = len(neighbours(x, y))
|
surrounding = len(neighbours(x, y))
|
||||||
if surrounding == 3:
|
if surrounding == 3:
|
||||||
newGrid[(x, y)] = True
|
newGrid.add((x, y))
|
||||||
|
|
||||||
return newGrid
|
return newGrid
|
||||||
|
|
||||||
def savePreset(self):
|
def savePreset(self):
|
||||||
pr = super().savePreset()
|
pr = super().savePreset()
|
||||||
pr['GRID'] = alphabetizeDict(self.startingGrid)
|
pr['GRID'] = sorted(self.startingGrid)
|
||||||
return pr
|
return pr
|
||||||
|
|
||||||
def loadPreset(self, pr, *args):
|
def loadPreset(self, pr, *args):
|
||||||
self.startingGrid = dict(pr['GRID'])
|
self.startingGrid = set(pr['GRID'])
|
||||||
if self.startingGrid:
|
if self.startingGrid:
|
||||||
for widget in self.shiftButtons:
|
for widget in self.shiftButtons:
|
||||||
widget.setEnabled(True)
|
widget.setEnabled(True)
|
||||||
|
|
Reference in New Issue