Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cocos2d offset format #138

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion glue/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def __init__(self, path, config):

self.x = self.y = None
self.original_width = self.original_height = 0
# Cocos2dx offset
self.offsetX = self.offsetY = 0

with open(self.path, "rb") as img:
self._image_data = img.read()
Expand Down Expand Up @@ -77,7 +79,10 @@ def image(self):
# without losing any non-transparent pixel.
# This crop is only used if the crop flag is set in the config.
if self.config['crop']:
img = img.crop(img.split()[-1].getbbox())
croppedBox = img.split()[-1].getbbox()
img = img.crop(croppedBox)
self.offsetX = croppedBox[0] + (img.size[0]/2) - (self.original_width/2)
self.offsetY = (self.original_height/2) - croppedBox[1] - (img.size[1]/2)
return img

@property
Expand Down
4 changes: 4 additions & 0 deletions glue/formats/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ def get_context(self, *args, **kwargs):
y=round_up(base_y / self.sprite.max_ratio * r),
abs_x=round_up(base_abs_x / self.sprite.max_ratio * r),
abs_y=round_up(base_abs_y / self.sprite.max_ratio * r),
original_width=round_up(img.original_width / r),
original_height=round_up(img.original_height / r),
offsetX=round_up(img.offsetX / r),
offsetY=round_up(img.offsetY / r),
height=round_up((img.height + img.padding[0] + img.padding[2]) / self.sprite.max_ratio * r),
width=round_up((img.width + img.padding[1] + img.padding[3]) / self.sprite.max_ratio * r))

Expand Down
10 changes: 8 additions & 2 deletions glue/formats/cocos2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@ def get_context(self, ratio, *args, **kwargs):
'textureFileName': ratio_context['sprite_filename']
}
}

# frame: sprite location within the sprite-sheet as position and size values
# offset: difference between the original center of the sprite and the center of the cropped sprite
# rotated: whether or not the sprite has been rotated within the sprite-sheet
# sourceColorRect: rectangle with actual color information inside your source sprite
# sourceSize: size of the original sprite
for i in context['images']:
image_context = i['ratios'][ratio]
rect = '{{{{{abs_x}, {abs_y}}}, {{{width}, {height}}}}}'.format(**image_context)
data['frames'][i['filename']] = {'frame': rect,
'offset': '{0,0}',
'offset': '{{{offsetX},{offsetY}}}'.format(**image_context),
'rotated': False,
'sourceColorRect': rect,
'sourceSize': '{{{width}, {height}}}'.format(**image_context)}
'sourceSize': '{{{original_width}, {original_height}}}'.format(**image_context)}
return data