Skip to content

Commit

Permalink
Add GUI to edit Tile Square Assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJoeSmo committed May 30, 2021
1 parent 7a53466 commit fbe73e0
Show file tree
Hide file tree
Showing 7 changed files with 410 additions and 39 deletions.
29 changes: 28 additions & 1 deletion foundry/game/gfx/drawable/Tile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from functools import lru_cache

from PySide2.QtGui import QImage
from PySide2.QtCore import QPoint
from PySide2.QtGui import QImage, QColor, QPainter, Qt

from foundry.game.gfx.GraphicsSet import GraphicsSet
from foundry.game.gfx.Palette import NESPalette, PaletteGroup
Expand Down Expand Up @@ -46,6 +47,8 @@ def __init__(
else:
self.background_color_index = 0

self.bg_color = NESPalette[palette_group[palette_index][self.background_color_index]]

if mirrored:
self._mirror()

Expand Down Expand Up @@ -85,6 +88,30 @@ def as_image(self, tile_length=8):

return self.cached_tiles[tile_length]

def _replace_transparent_with_background(self, image):
# draw image on background layer, to fill transparent pixels
background = image.copy()
background.fill(self.bg_color)

_painter = QPainter(background)
_painter.drawImage(QPoint(), image)
_painter.end()

return background

def draw(self, painter: QPainter, x, y, tile_length, transparent=False):
if tile_length != Tile.WIDTH:
image = self.as_image(tile_length)

# mask out the transparent pixels first
mask = image.createMaskFromColor(QColor(*MASK_COLOR).rgb(), Qt.MaskOutColor)
image.setAlphaChannel(mask)

if not transparent: # or self._whole_block_is_transparent:
image = self._replace_transparent_with_background(image)

painter.drawImage(x, y, image)

def _mirror(self):
for byte in range(len(self.data)):
self.data[byte] = bit_reverse[self.data[byte]]
8 changes: 5 additions & 3 deletions foundry/game/gfx/objects/LevelObj/BlockGroupRenderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from PySide2.QtCore import QSize
from PySide2.QtGui import QImage, QPainter

from foundry.game.File import ROM
from foundry.game.gfx.GraphicsSet import GraphicsSet
from foundry.game.gfx.Palette import PaletteGroup, bg_color_for_object_set
from foundry.game.gfx.drawable.Block import Block, get_block

from foundry.gui.tsa_data import get_tsa_data


# todo: Create tests for BlockGroupRenderer
class BlockGroupRenderer:
Expand Down Expand Up @@ -51,7 +52,6 @@ def object_set_index(self) -> int:
@object_set_index.setter
def object_set_index(self, idx: int) -> None:
self._object_set_index = idx
self.tsa_data = ROM.get_tsa_data(idx)

@property
def x(self) -> int:
Expand Down Expand Up @@ -144,7 +144,9 @@ def _draw(self, painter: QPainter, block_length, transparent, x_offsets, y_offse

def _draw_block(self, painter: QPainter, block_index, x, y, block_length, transparent):
if block_index not in self.block_cache:
self.block_cache[block_index] = get_block(block_index, self.palette_group, self.graphics_set, self.tsa_data)
self.block_cache[block_index] = get_block(
block_index, self.palette_group, self.graphics_set, get_tsa_data(self.object_set_index)
)

self.block_cache[block_index].draw(
painter,
Expand Down
3 changes: 2 additions & 1 deletion foundry/game/level/WorldMap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from foundry.game.gfx.drawable.Block import Block
from foundry.game.gfx.objects.MapObject import MapObject
from foundry.game.level.LevelLike import LevelLike
from foundry.gui.tsa_data import get_tsa_data
from smb3parse.levels.world_map import (
WORLD_MAP_HEIGHT,
WORLD_MAP_SCREEN_SIZE,
Expand All @@ -29,7 +30,7 @@ def __init__(self, world_index):
self.palette_group = load_palette_group(WORLD_MAP_OBJECT_SET, 0)

self.object_set = WORLD_MAP_OBJECT_SET
self.tsa_data = ROM.get_tsa_data(self.object_set)
self.tsa_data = get_tsa_data(0)

self.world = 0
self.level_number = world_index
Expand Down
Loading

0 comments on commit fbe73e0

Please sign in to comment.