Vlad Shcherban 559fcbfcb9 * [Lua] Adding Spine atlas parser
* [Corona] Adding ability to load atlas files
* [Corona] Added example of using atlas

# Documentation:

## Lua runtime: `spine.Atlas.parse`

`Atlas.lua` introduces `spine.Atlas.parse(atlasPath, baseDir)` funciton. This function can be use across Lua runtimes to implement their their own image sheet bindings. It will return Array of `Page`s or `nil` if error.

### `Page` properties:
* `name` - filename of the page (`"raptor.png"`)
* `size` - dimmensions of original image (`[1022,1022]`)
* `format` - texture format (`"RGBA8888"`)
* `filter` - filter for texture scalin up and down (`["Linear","Linear"]`)
* `wrap` - wrap modes (`"none"`)
* `regions` - Array of `Region`s on this page

### `Region` properties:
* `name` - region name (`"torso"`)
* `rotate` - is image rotated in texture (`true`)
* `xy` - image frame position on texture (`[610,647]`)
* `size` - image frame size on texture (`[54,91]`)
* `splits` - (optional)
* `pad` - (optional)
* `orig` - original image size (`[54,91]`)
* `offset` - offset if image was trimmed (`[0,0]`)
* `index` - `-1`

## Corona runtime: `spine.GetAtlasSprites`

`spine.lua` introduces `spine.GetAtlasSprites(atlasName, baseDir)`, which returns `Sprites` table. This funciton would parse atlas, locate atlas textures, load them to Corona's image sheets and create convinience bindings and functions. User can use convenience functions, or write own wrappers.

### `Sprites` table
#### Convenience functions:
* `ATLAS_HELPER_createImage` - can be assigned to `skeleton.createImage`
* `ATLAS_HELPER_createMesh` - can be assigned to `skeleton.createMesh`
* `ATLAS_HELPER_setupSkeleton` - use this on skeleton to assing both creation functions (`sprites.setupSkeleton(skeleton)`)

### Region name bindings:
All other elements of `Sprites` table would contain binding `sprites["region_name"] = sheetReference`, where each `sheetReference` contains following elements:

* `frame` - number of frame in image sheet (`42`)
* `region` - reference to `Region` table
* `page` -  reference to `Page` table. `Page` table would also get new property - `sheet`, reference to Corona's image sheet where sprites can be accessed.

As result, to create image for `attachment` code would look something like `display.newImage( sprites[attachment.name].page.sheet, sprites[attachment.name].frame )`. For more rubust example see `ATLAS_HELPER_createImage` in `spine-corona/spine.lua`.

# Known Issues:

* Corona would not work with padded/cropped atlases.
* Possible (but unprobable) name collision with region names and `ATLAS_HELPER_*` functions.
2016-06-23 11:07:12 -04:00

88 lines
2.8 KiB
Lua

-- This example shows simple usage of displaying a skeleton with queued animations.
local spine = require "spine-corona.spine"
local json = spine.SkeletonJson.new()
json.scale = 0.6
local skeletonData = json:readSkeletonDataFile("examples/spineboy-atlas/spineboy.json")
local skeleton = spine.Skeleton.new(skeletonData)
local sprites = spine.GetAtlasSprites( "examples/spineboy-atlas/spineboy.atlas" )
sprites.ATLAS_HELPER_setup(skeleton)
skeleton.group.x = display.contentWidth * 0.75
skeleton.group.y = display.contentHeight * 0.9
skeleton.flipX = false
skeleton.flipY = false
skeleton.debug = true -- Omit or set to false to not draw debug lines on top of the images.
skeleton.debugAabb = true
skeleton:setToSetupPose()
local bounds = spine.SkeletonBounds.new()
-- AnimationStateData defines crossfade durations between animations.
local stateData = spine.AnimationStateData.new(skeletonData)
stateData:setMix("walk", "jump", 0.2)
stateData:setMix("jump", "run", 0.2)
-- AnimationState has a queue of animations and can apply them with crossfading.
local state = spine.AnimationState.new(stateData)
-- state:setAnimationByName(0, "test")
state:setAnimationByName(0, "walk", true)
state:addAnimationByName(0, "jump", false, 3)
state:addAnimationByName(0, "run", true, 0)
state.onStart = function (trackIndex)
-- print(trackIndex.." start: "..state:getCurrent(trackIndex).animation.name)
end
state.onEnd = function (trackIndex)
-- print(trackIndex.." end: "..state:getCurrent(trackIndex).animation.name)
end
state.onComplete = function (trackIndex, loopCount)
-- print(trackIndex.." complete: "..state:getCurrent(trackIndex).animation.name..", "..loopCount)
end
state.onEvent = function (trackIndex, event)
-- print(trackIndex.." event: "..state:getCurrent(trackIndex).animation.name..", "..event.data.name..", "..event.intValue..", "..event.floatValue..", '"..(event.stringValue or "").."'")
end
local lastTime = 0
local touchX = 999999
local touchY = 999999
local headSlot = skeleton:findSlot("head")
Runtime:addEventListener("enterFrame", function (event)
-- Compute time in seconds since last frame.
local currentTime = event.time / 1000
local delta = currentTime - lastTime
lastTime = currentTime
-- Bounding box hit detection.
bounds:update(skeleton, true)
if bounds:containsPoint(touchX, touchY) then
headSlot.g = 0;
headSlot.b = 0;
else
headSlot.g = 1;
headSlot.b = 1;
end
-- Update the state with the delta time, apply it, and update the world transforms.
state:update(delta)
state:apply(skeleton)
skeleton:updateWorldTransform()
end)
Runtime:addEventListener("touch", function (event)
if event.phase ~= "ended" and event.phase ~= "cancelled" then
-- Make the coordinates relative to the skeleton's group.
touchX = event.x - skeleton.group.x
touchY = skeleton.group.y - event.y
else
touchX = 999999
touchY = 999999
end
end)