Have LayerAdapter use prerendered spritesheets (unfinished)

This commit is contained in:
Scott Erickson 2015-10-13 16:43:56 -07:00
parent 3fc7b71f7a
commit 39bddb889e
7 changed files with 71 additions and 6 deletions

View file

@ -18,6 +18,7 @@ class SpriteExporter extends CocoClass
@colorConfig = options.colorConfig or {}
@resolutionFactor = options.resolutionFactor or 1
@actionNames = options.actionNames or (action.name for action in @thangType.getDefaultActions())
@spriteType = options.spriteType or @thangType.get('spriteType')
super()
build: (renderType) ->

View file

@ -148,10 +148,12 @@ module.exports = LayerAdapter = class LayerAdapter extends CocoClass
addLank: (lank) ->
lank.options.resolutionFactor = @resolutionFactor
lank.layer = @
@listenTo(lank, 'action-needs-render', @onActionNeedsRender)
@lanks.push lank
lank.thangType.initPrerenderedSpriteSheets()
prerenderedSpriteSheet = lank.thangType.getPrerenderedSpriteSheet(lank.options.colorConfig, @defaultSpriteType)
prerenderedSpriteSheet?.markToLoad()
@loadThangType(lank.thangType)
@addDefaultActionsToRender(lank)
@setSpriteToLank(lank)
@ -176,6 +178,10 @@ module.exports = LayerAdapter = class LayerAdapter extends CocoClass
thangType.loadRasterImage()
@listenToOnce(thangType, 'raster-image-loaded', @somethingLoaded)
@numThingsLoading++
else if prerenderedSpriteSheet = thangType.getPrerenderedSpriteSheetToLoad()
prerenderedSpriteSheet.loadImage()
@listenToOnce(prerenderedSpriteSheet, 'image-loaded', -> @somethingLoaded(thangType))
@numThingsLoading++
somethingLoaded: (thangType) ->
@numThingsLoading--

View file

@ -1,6 +1,7 @@
CocoModel = require './CocoModel'
SpriteBuilder = require 'lib/sprites/SpriteBuilder'
LevelComponent = require './LevelComponent'
CocoCollection = require 'collections/CocoCollection'
utils = require 'core/utils'
@ -509,3 +510,57 @@ module.exports = class ThangType extends CocoModel
animationContainers = @getContainersForAnimation(action.animation, action)
containersToRender[container.gn] = true for container in animationContainers
return _.keys(containersToRender)
initPrerenderedSpriteSheets: ->
return if @prerenderedSpriteSheets or not data = @get('prerenderedSpriteSheetData')
# creates a collection of prerendered sprite sheets
@prerenderedSpriteSheets = new PrerenderedSpriteSheets(data)
getPrerenderedSpriteSheet: (colorConfig, defaultSpriteType) ->
return unless @prerenderedSpriteSheets
spriteType = @get('spriteType') or defaultSpriteType
@prerenderedSpriteSheets.find (pss) ->
return false if pss.get('spriteType') isnt spriteType
otherColorConfig = pss.get('colorConfig')
return true if _.isEmpty(colorConfig) and _.isEmpty(otherColorConfig)
return _.isEqual(colorConfig and otherColorConfig)
getPrerenderedSpriteSheetToLoad: ->
return unless @prerenderedSpriteSheets
@prerenderedSpriteSheets.find (pss) -> pss.needToLoad and not pss.loadedImage
class PrerenderedSpriteSheet extends CocoModel
@className: 'PrerenderedSpriteSheet'
loadImage: ->
return if @loadingImage or @loadedImage
return unless imageURL = @get('image')
@image = $("<img src='/file/#{imageURL}' />")
@loadingImage = true
@image.one('load', =>
@loadingImage = false
@loadedImage = true
@buildSpriteSheet()
@trigger('image-loaded', @))
@image.one('error', =>
@loadingImage = false
@trigger('image-load-error', @)
)
buildSpriteSheet: ->
@spriteSheet = new createjs.SpriteSheet({
images: [@image[0]],
frames: @get('frames')
animations: @get('animations')
})
markToLoad: -> @needToLoad = true
needToLoad: false
loadedImage: false
loadingImage: false
class PrerenderedSpriteSheets extends CocoCollection
model: PrerenderedSpriteSheet

View file

@ -169,7 +169,7 @@ _.extend ThangTypeSchema.properties,
extendedName: {type: 'string', title: 'Extended Hero Name', description: 'The long form of the hero\'s name. Ex.: "Captain Anya Weston".'}
unlockLevelName: {type: 'string', title: 'Unlock Level Name', description: 'The name of the level in which the hero is unlocked.'}
tasks: c.array {title: 'Tasks', description: 'Tasks to be completed for this ThangType.'}, c.task
spriteSheets: c.array {title: 'SpriteSheets'},
prerenderedSpriteSheetData: c.array {title: 'Prerendered SpriteSheet Data'},
c.object {title: 'SpriteSheet'},
actionNames: { type: 'array' }
animations:

View file

@ -24,12 +24,14 @@ module.exports = class ExportThangTypeModal extends ModalView
getActionNames: -> _.map @$('input[name="action"]:checked'), (el) -> $(el).val()
getResolutionFactor: -> parseInt(@$('#resolution-input').val()) or SPRITE_RESOLUTION_FACTOR
getFilename: -> 'spritesheet-'+_.string.slugify(moment().format())+'.png'
getSpriteType: -> @thangType.get('spriteType') or 'segmented'
onClickSaveButton: ->
options = {
resolutionFactor: @getResolutionFactor()
actionNames: @getActionNames()
colorConfig: @getColorConfig()
spriteType: @getSpriteType()
}
@exporter = new SpriteExporter(@thangType, options)
@exporter.build()
@ -61,14 +63,15 @@ module.exports = class ExportThangTypeModal extends ModalView
] for f in @spriteSheet._frames)
image: "db/thang.type/#{@thangType.get('original')}/"+@getFilename()
resolutionFactor: @getResolutionFactor()
spriteType: @getSpriteType()
}
if config = @getColorConfig()
spriteSheetData.colorConfig = config
if label = @getColorLabel()
spriteSheetData.colorLabel = label
spriteSheets = _.clone(@thangType.get('spriteSheets') or [])
spriteSheets = _.clone(@thangType.get('prerenderedSpriteSheetData') or [])
spriteSheets.push(spriteSheetData)
@thangType.set('spriteSheets', spriteSheets)
@thangType.set('prerenderedSpriteSheetData', spriteSheets)
@thangType.save()
@listenToOnce @thangType, 'sync', @hide

View file

@ -235,7 +235,7 @@ module.exports = class Handler
# Hack: levels loading thang types need the components returned as well.
# Need a way to specify a projection for a query.
project = {name: 1, original: 1, kind: 1, components: 1}
project = {name: 1, original: 1, kind: 1, components: 1, prerenderedSpriteSheetData: 1}
sort = if nonVersioned then {} else {'version.major': -1, 'version.minor': -1}
makeFunc = (id) =>

View file

@ -35,7 +35,7 @@ ThangTypeHandler = class ThangTypeHandler extends Handler
'unlockLevelName'
'tasks'
'terrains'
'spriteSheets'
'prerenderedSpriteSheetData'
]
hasAccess: (req) ->