Skip to content

Commit

Permalink
Switches to passing classes around instead of instances
Browse files Browse the repository at this point in the history
  • Loading branch information
jinglemansweep committed Nov 19, 2023
1 parent a25dca1 commit c015b09
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
56 changes: 30 additions & 26 deletions testing/sprites/lib/tile_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import random
import time
from datetime import datetime
from typing import Callable, List, Dict, Tuple, cast
from typing import Any, Callable, List, Dict, Tuple, Type, TypeVar, cast

# NOTES

Expand Down Expand Up @@ -103,14 +103,15 @@ class TileGridCell(BaseSprite):
visible: bool = True
label: str = ""

def __init__(self):
super().__init__()
def __init__(self, state):
super().__init__(state)
self.state = state
self.image = pygame.Surface((self.width, self.height))
self.image.fill(self.color_background)
self.rect = self.image.get_rect()

def update(self, state):
self.render(state)
def update(self):
self.render(self.state)

def render(self, state):
self.image = pygame.Surface((self.rect.width, self.rect.height))
Expand All @@ -124,23 +125,25 @@ def __repr__(self):
class TileGridColumn(BaseSprite):
width: int = TILE_GRID_CELL_WIDTH
height: int = 0
cells: List[TileGridCell]
cells: List[Any]

def __init__(self):
def __init__(self, state):
super().__init__()
self.state = state
self.cells_inst = [cell(self.state) for cell in self.cells]
self.image = pygame.Surface((self.width, self.height))
self.rect = self.image.get_rect()

def update(self, state):
self.render(state)
def update(self):
self.render()

def render(self, state):
def render(self):
ch = 0
mh = sum([cell.image.get_height() for cell in self.cells])
mh = sum([cell.image.get_height() for cell in self.cells_inst])
self.image = pygame.Surface((self.rect.width, mh))
self.image.fill((0, 0, 0))
for cell in self.cells:
cell.update(state)
for cell in self.cells_inst:
cell.update()
self.image.blit(cell.image, (0, ch))
ch += cell.image.get_height()

Expand All @@ -149,12 +152,13 @@ def __repr__(self):


class TileGrid(BaseSprite):
columns: List[TileGridColumn]
columns: List[Any]
state: Dict = dict()

def __init__(self, state, x=0, y=0):
super().__init__()
self.state = state
self.columns_inst = [column(self.state) for column in self.columns]
self.image = pygame.Surface((0, 0))
self.rect = self.image.get_rect()

Expand All @@ -166,14 +170,14 @@ def update(self):

def render(self):
cw = 0
mw = sum([column.image.get_width() for column in self.columns])
mh = max([column.image.get_width() for column in self.columns])
mw = sum([column.image.get_width() for column in self.columns_inst])
mh = max([column.image.get_width() for column in self.columns_inst])
self.image = pygame.Surface((mw, mh))
self.rect.width = mw
self.rect.height = mh
self.image.fill((0, 0, 0, 0))
for column in self.columns:
column.update(self.state)
for column in self.columns_inst:
column.update()
self.image.blit(column.image, (cw, 0))
cw += column.image.get_width()

Expand All @@ -185,14 +189,14 @@ class VerticalCollapseTileGridCell(TileGridCell):
width: int = TILE_GRID_CELL_WIDTH
height_animator: Animator

def __init__(self):
super().__init__()
def __init__(self, state):
super().__init__(state)
self.height_animator = Animator(range=(0.0, 12.0), open=True, speed=1.0)

def update(self, state):
def update(self):
self.height_animator.update()
self.rect.height = self.height_animator.value
super().update(state)
super().update()

@property
def open(self):
Expand All @@ -206,15 +210,15 @@ class HorizontalCollapseTileGridColumn(TileGridColumn):
height: int = TILE_GRID_CELL_HEIGHT
width_animator: Animator

def __init__(self):
super().__init__()
def __init__(self, state):
super().__init__(state)
self.width_animator = Animator(range=(2.0, 64.0), open=True, speed=1.0)

def update(self, state):
def update(self):
self.width_animator.set(self.open)
self.width_animator.update()
self.rect.width = self.width_animator.value
super().update(state)
super().update()

@property
def open(self):
Expand Down
18 changes: 9 additions & 9 deletions testing/sprites/tiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class CellSpeedTestDownload(VerticalCollapseTileGridCell):
label = "Download"
color_background = pygame.Color(32, 0, 0, 255)

def update(self, state):
super().update(state)
v = int(state.get("download", 0))
def update(self):
super().update()
v = int(self.state.get("download", 0))
open = v > 500
self.height_animator.set(open)

Expand All @@ -23,9 +23,9 @@ class CellSpeedTestUpload(VerticalCollapseTileGridCell):
label = "Upload"
color_background = pygame.Color(0, 32, 0, 255)

def update(self, state):
super().update(state)
v = int(state.get("upload", 0))
def update(self):
super().update()
v = int(self.state.get("upload", 0))
open = v > 500
self.height_animator.set(open)

Expand All @@ -34,15 +34,15 @@ def update(self, state):


class GridColumn1(HorizontalCollapseTileGridColumn):
cells = [CellSpeedTestUpload(), CellSpeedTestDownload()]
cells = [CellSpeedTestUpload, CellSpeedTestDownload]


class GridColumn2(HorizontalCollapseTileGridColumn):
cells = [CellSpeedTestDownload()]
cells = [CellSpeedTestDownload]


# CUSTOM GRID


class CustomTileGrid(TileGrid):
columns = [GridColumn1(), GridColumn2()]
columns = [GridColumn1, GridColumn2]

0 comments on commit c015b09

Please sign in to comment.