Skip to content

Commit

Permalink
Merge pull request #30 from easy-dungeon-company/load-assetpack-using…
Browse files Browse the repository at this point in the history
…-path

Load assetpack using path
  • Loading branch information
jacksonj04 committed Mar 17, 2019
2 parents 43e212a + 748802b commit b193f11
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 64 deletions.
22 changes: 11 additions & 11 deletions ddl/asset.py
Expand Up @@ -13,15 +13,15 @@ class Asset:
The superclass for components and images. Implements methods common to
both to allow for easier implementation of global asset lists.
"""
def __init__(self, data, assetpack_name):
def __init__(self, data, assetpack_id):
self.data = data
self.assetpack_name = assetpack_name
self.assetpack_id = assetpack_id
self.name = data['name']
self.asset_id = data['id']

def get_full_id(self):
"""Default. Should always be overriden"""
return (self.assetpack_name + '.' + self.asset_id)
""" Return the full ID of this asset, including the Assetpack ID"""
return (self.assetpack_id + '.' + self.asset_id)

def rescale(self, scale_ratio_x, scale_ratio_y):
"""Passes. Implementations of this method exists in subclasses"""
Expand All @@ -40,8 +40,8 @@ class ComponentAsset(Asset):
"""This is a space saving measure that records
multiple image_assets and components (collectively known as assets)
and their respective positions within the Asset Pack's grid."""
def __init__(self, data, assetpack_name):
super().__init__(data, assetpack_name)
def __init__(self, data, assetpack_id):
super().__init__(data, assetpack_id)
if "parts" in data.keys():
self.parts = data["parts"]
self.reset_sub_parts()
Expand Down Expand Up @@ -75,12 +75,12 @@ def get_part_full_id(self, sub_part):
if sub_part['type'] == 'image':
# Naive check to see if this already has an assetpack name
if len(sub_part["image_id"].split('.')) != 2:
return(self.assetpack_name + '.' + sub_part["image_id"])
return(self.assetpack_id + '.' + sub_part["image_id"])
else:
return(sub_part["image_id"])
else:
if len(sub_part["component_id"].split('.')) != 2:
return(self.assetpack_name + '.' + sub_part["component_id"])
return(self.assetpack_id + '.' + sub_part["component_id"])
else:
return(sub_part["component_id"])

Expand Down Expand Up @@ -135,14 +135,14 @@ def get_json(self):
class ImageAsset(Asset):
"""A representation of an actual image file and the pixel offsets required
to put it in the correct location."""
def __init__(self, data, assetpack_name):
super().__init__(data, assetpack_name)
def __init__(self, data, assetpack_id, assetpack_path):
super().__init__(data, assetpack_id)
if "top_left" in data.keys():
self.top_left = data["top_left"]
else:
self.top_left = {"x": 0, "y": 0}

self.image = Image.open('assetpacks/' + assetpack_name + '/art/' +
self.image = Image.open(assetpack_path + '/art/' +
data["image"])

def resize(self, size_ratio_x, size_ratio_y):
Expand Down
39 changes: 24 additions & 15 deletions ddl/assetpack.py
Expand Up @@ -3,6 +3,8 @@
"""

import json
import os

from ddl.projection import IsometricProjection, TopDownProjection
from ddl.asset import ComponentAsset, ImageAsset
from ddl.taglist import TagList
Expand All @@ -22,36 +24,43 @@ class ProjectionGridException(Exception):
class AssetpackFactory:
"""A factory for creating AssetPacks"""
@staticmethod
def load(name):
def load(path):
"""
Validates and loads AssetPacks from their component and Image packs,
given an appropriate name
"""
Validator.validate_file('assetpacks/' + name + '/pack.json',

pack_path = os.path.abspath(path)

Validator.validate_file(pack_path + '/pack.json',
'pack')
Validator.validate_file('assetpacks/' + name + '/images.json',
Validator.validate_file(pack_path + '/images.json',
'images')
Validator.validate_file('assetpacks/' + name + '/components.json',
Validator.validate_file(pack_path + '/components.json',
'components')
with open(
'assetpacks/' + name + '/components.json'
pack_path + '/components.json'
) as component_file, open(
'assetpacks/' + name + '/images.json'
pack_path + '/images.json'
) as imagepack_file:
components_and_grid = json.load(component_file)
imagepack = json.load(imagepack_file)
return Assetpack(name, imagepack, components_and_grid)

pack_id = path.split('/')[-1]

return Assetpack(pack_id, pack_path, imagepack, components_and_grid)


class Assetpack:
"""This class records all information needed to
position and render the various images located in an assetpack.
Please see the assetpack and imagepack schema for more info"""
def __init__(self, name, imagepack, components_and_grid):
def __init__(self, pack_id, pack_path, imagepack, components_and_grid):

self.components = {}
self.images = {}
self.name = name
self.pack_id = pack_id
self.pack_path = pack_path
self.grid = components_and_grid['grid']
self.taglist = TagList()
if self.grid['type'] == 'isometric':
Expand All @@ -62,11 +71,11 @@ def __init__(self, name, imagepack, components_and_grid):
self.grid['height'])

for image in imagepack['images']:
new_image = ImageAsset(image, assetpack_name=name)
new_image = ImageAsset(image, assetpack_id=pack_id, assetpack_path=pack_path)
self.add_image(new_image)

for component in components_and_grid['components']:
new_component = ComponentAsset(component, assetpack_name=name)
new_component = ComponentAsset(component, assetpack_id=pack_id)
self.taglist.add_component(new_component)
self.add_component(new_component)

Expand Down Expand Up @@ -103,7 +112,7 @@ def append_assetpack(self, assetpack):

self.taglist.append(assetpack.taglist)

def change_assetpack_name(self, new_name):
def change_assetpack_id(self, new_id):
"""
Changes the name of the assetpack and all asset's assetpack
identifiers.
Expand All @@ -113,14 +122,14 @@ def change_assetpack_name(self, new_name):
self.images = {}
self.components = {}
for component in new_components.values():
component.assetpack_name = new_name
component.assetpack_id = new_id
component.reset_sub_parts()
self.add_component(component)
for image in new_images.values():
image.assetpack_name = new_name
image.assetpack_id = new_id
image.reset_sub_parts()
self.add_image(image)
self.name = new_name
self.pack_id = new_id

def resize_images(self, desired_projection):
"""Accepts a desired grid size definition and uses it to rescale all
Expand Down
18 changes: 9 additions & 9 deletions ddl/helper_classes.py
Expand Up @@ -47,27 +47,27 @@ def new_component(self, component_id, name='', tags=None, parts=None):
"parts": parts,
"tags": tags
}
self.component = ComponentAsset(data, self.assetpack.name)
self.component = ComponentAsset(data, self.assetpack.pack_id)

def add_image(self, image_id, x_coordinate, y_coordinate,
assetpack_name=None):
assetpack_id=None):
"""Adds a specific image asset to the component at grid co-ordinates
x and y."""
if assetpack_name is None:
assetpack_name = self.assetpack.name
image_key = assetpack_name+'.'+image_id
if assetpack_id is None:
assetpack_id = self.assetpack.pack_id
image_key = assetpack_id + '.' + image_id
if image_key not in self.assetpack.images.keys():
raise Exception('This image ID doesnt exist in this assetpack.')
image = self.assetpack.images[image_key]
self.component.add_image(image, x_coordinate, y_coordinate)

def add_component(self, component_id, x_coordinate, y_coordinate,
assetpack_name=None):
assetpack_id=None):
"""Adds a specific component to the component at grid co-ordinates
x and y."""
if assetpack_name is None:
assetpack_name = self.assetpack.name
component_key = assetpack_name+'.'+component_id
if assetpack_id is None:
assetpack_id = self.assetpack.pack_id
component_key = assetpack_id + '.' + component_id
if component_key not in self.assetpack.components.keys():
raise Exception('This component ID isn\'t in this assetpack.')
component = self.assetpack.components[component_key]
Expand Down
27 changes: 15 additions & 12 deletions tests/test_assetpack.py
Expand Up @@ -2,6 +2,8 @@
Tests Assetpacks
"""

import os

from ddl.assetpack import AssetpackFactory, Assetpack
from ddl.asset import ComponentAsset

Expand All @@ -15,14 +17,15 @@ def __init__(self, width, height):

def test_factory_creates_assetpack():
""" Ensure the AssetpackFactory returns an Assetpack. """
assetpack = AssetpackFactory.load('example_isometric')
pack_path = os.path.abspath('assetpacks/example_isometric')
assetpack = AssetpackFactory.load(pack_path)
if not isinstance(assetpack, Assetpack):
raise AssertionError()


def test_assetpack_resize():
"""Test resizing an assetpack's images to match a new projection."""
assetpack = AssetpackFactory.load('example_isometric')
assetpack = AssetpackFactory.load('assetpacks/example_isometric')
projection2 = FakeProjection(29, 17)
assetpack.resize_images(projection2)
if not assetpack.projection.width == 29:
Expand All @@ -39,7 +42,7 @@ def test_assetpack_resize():

def test_assetpack_rescale():
"""Test rescaling an assetpack's components to match a new projection."""
assetpack = AssetpackFactory.load('example_isometric')
assetpack = AssetpackFactory.load('assetpacks/example_isometric')
projection2 = FakeProjection(29, 17)
assetpack.rescale_components(projection2)
if not assetpack.projection.width == 29:
Expand All @@ -62,8 +65,8 @@ def test_assetpack_rescale():

def test_change_assetpack_name():
"""Tests changing the name of an assetpack."""
assetpack = AssetpackFactory.load('example_isometric')
assetpack.change_assetpack_name('new_name')
assetpack = AssetpackFactory.load('assetpacks/example_isometric')
assetpack.change_assetpack_id('new_name')
if len(assetpack.images) != 4:
raise AssertionError()
if len(assetpack.components) != 4:
Expand All @@ -78,24 +81,24 @@ def test_change_assetpack_name():
raise AssertionError()

for component in assetpack.components.values():
if component.assetpack_name != 'new_name':
if component.assetpack_id != 'new_name':
raise AssertionError()
if isinstance(component, ComponentAsset):
for sub_part in component.parts:
if sub_part["asset_id"].split('.')[0] != 'new_name':
raise AssertionError()

if assetpack.name != 'new_name':
if assetpack.pack_id != 'new_name':
raise AssertionError()


def test_append_assetpacks():
"""
Tests that appending one assetpack to another gives a bigger assetpack
"""
assetpack = AssetpackFactory.load('example_isometric')
assetpack2 = AssetpackFactory.load('example_isometric')
assetpack2.change_assetpack_name('new_name')
assetpack = AssetpackFactory.load('assetpacks/example_isometric')
assetpack2 = AssetpackFactory.load('assetpacks/example_isometric')
assetpack2.change_assetpack_id('new_name')
assetpack.append_assetpack(assetpack2)
if len(assetpack.images) != 8:
raise AssertionError('%s != 14' % len(assetpack.images))
Expand All @@ -106,7 +109,7 @@ def test_append_assetpacks():
def test_simple_image_location_list():
"""Tests an assetpack will return an imagelocationlist for a simple
component if asked. No Nesting."""
assetpack = AssetpackFactory.load('example_isometric')
assetpack = AssetpackFactory.load('assetpacks/example_isometric')
component = assetpack.components['example_isometric.floor-wall-exact']
ill = assetpack.get_image_location_list(2, 3, component)
if len(ill) != 2:
Expand All @@ -121,7 +124,7 @@ def test_simple_image_location_list():
def test_nested_image_location_list():
"""Tests an assetpack will return an imagelocationlist for a complex
component if asked. Nesting involved."""
assetpack = AssetpackFactory.load('example_isometric')
assetpack = AssetpackFactory.load('assetpacks/example_isometric')
component = assetpack.components['example_isometric.nested-component-test']
ill = assetpack.get_image_location_list(2, 3, component)
if len(ill) != 3:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_componentasset.py
Expand Up @@ -61,7 +61,7 @@ def get_test_component():
def test_component_init():
"""Tests the initialisation of components"""
component = get_test_component()
if not component.assetpack_name == 'test_assetpack_name':
if not component.assetpack_id == 'test_assetpack_name':
raise AssertionError()
if not len(component.parts) == 2:
raise AssertionError()
Expand Down
10 changes: 5 additions & 5 deletions tests/test_imageasset.py
Expand Up @@ -14,8 +14,8 @@ def test_zero_top_left():
data = {"name": "test_name",
"id": "test",
"image": "1x1_floor_fuzzy.png"}
image = ImageAsset(data, "example_isometric")
if not image.assetpack_name == "example_isometric":
image = ImageAsset(data, "example_isometric", "assetpacks/example_isometric")
if not image.assetpack_id == "example_isometric":
raise AssertionError()
if not image.name == "test_name":
raise AssertionError()
Expand All @@ -35,8 +35,8 @@ def test_nonzero_top_left():
"id": "test",
"top_left": {"x": 152, "y": 6},
"image": "1x1_floor_fuzzy.png"}
image = ImageAsset(data, "example_isometric")
if not image.assetpack_name == "example_isometric":
image = ImageAsset(data, "example_isometric", "assetpacks/example_isometric")
if not image.assetpack_id == "example_isometric":
raise AssertionError()
if not image.name == "test_name":
raise AssertionError()
Expand All @@ -56,7 +56,7 @@ def test_resize():
"id": "test",
"top_left": {"x": 152, "y": 6},
"image": "1x1_floor_fuzzy.png"}
image = ImageAsset(data, "example_isometric")
image = ImageAsset(data, "example_isometric", "assetpacks/example_isometric")
image.resize(2, 3)
if not image.image.width == 608:
raise AssertionError()
Expand Down

0 comments on commit b193f11

Please sign in to comment.