2014-02-24 12:46:40 -05:00
|
|
|
CocoView = require 'views/kinds/CocoView'
|
2014-02-24 12:06:22 -05:00
|
|
|
template = require 'templates/account/wizard_settings'
|
2014-01-12 14:54:50 -05:00
|
|
|
{me} = require('lib/auth')
|
|
|
|
ThangType = require 'models/ThangType'
|
|
|
|
SpriteBuilder = require 'lib/sprites/SpriteBuilder'
|
|
|
|
|
2014-02-24 12:46:40 -05:00
|
|
|
module.exports = class WizardSettingsView extends CocoView
|
2014-02-24 12:06:22 -05:00
|
|
|
id: 'wizard-settings-view'
|
2014-01-12 14:54:50 -05:00
|
|
|
template: template
|
2014-02-24 12:46:40 -05:00
|
|
|
startsLoading: true
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-01-12 14:54:50 -05:00
|
|
|
events:
|
|
|
|
'change .color-group-checkbox': (e) ->
|
|
|
|
colorGroup = $(e.target).closest('.color-group')
|
|
|
|
@updateSliderVisibility(colorGroup)
|
|
|
|
@updateColorSettings(colorGroup)
|
|
|
|
|
|
|
|
constructor: ->
|
|
|
|
super(arguments...)
|
|
|
|
@loadWizard()
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-01-12 14:54:50 -05:00
|
|
|
loadWizard: ->
|
|
|
|
@wizardThangType = new ThangType()
|
|
|
|
@wizardThangType.url = -> '/db/thang_type/wizard'
|
|
|
|
@wizardThangType.fetch()
|
|
|
|
@wizardThangType.once 'sync', @initCanvas, @
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-01-12 14:54:50 -05:00
|
|
|
initCanvas: ->
|
2014-02-24 12:46:40 -05:00
|
|
|
@startsLoading = false
|
2014-01-12 14:54:50 -05:00
|
|
|
@render()
|
|
|
|
@spriteBuilder = new SpriteBuilder(@wizardThangType)
|
|
|
|
@initStage()
|
|
|
|
|
|
|
|
getRenderData: ->
|
|
|
|
c = super()
|
|
|
|
wizardSettings = me.get('wizard')?.colorConfig or {}
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-01-12 14:54:50 -05:00
|
|
|
colorGroups = @wizardThangType.get('colorGroups') or {}
|
2014-02-13 12:26:21 -05:00
|
|
|
f = (name) -> {
|
2014-01-12 14:54:50 -05:00
|
|
|
dasherized: _.string.dasherize(name)
|
|
|
|
humanized: _.string.humanize name
|
|
|
|
name: name
|
|
|
|
exists: wizardSettings[name]
|
|
|
|
}
|
|
|
|
c.colorGroups = (f(colorName) for colorName in _.keys colorGroups)
|
|
|
|
c
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-01-12 14:54:50 -05:00
|
|
|
afterRender: ->
|
2014-02-24 12:46:40 -05:00
|
|
|
return if @startsLoading
|
2014-01-12 14:54:50 -05:00
|
|
|
wizardSettings = me.get('wizard') or {}
|
|
|
|
wizardSettings.colorConfig ?= {}
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-01-12 14:54:50 -05:00
|
|
|
@$el.find('.selector').each (i, slider) =>
|
|
|
|
[groupName, prop] = $(slider).attr('name').split('.')
|
2014-01-15 18:16:31 -05:00
|
|
|
value = 100 * (wizardSettings.colorConfig[groupName]?[prop] ? 0.5)
|
2014-01-12 14:54:50 -05:00
|
|
|
@initSlider $(slider), value, @onSliderChanged
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-01-12 14:54:50 -05:00
|
|
|
@$el.find('.color-group').each (i, colorGroup) =>
|
|
|
|
@updateSliderVisibility($(colorGroup))
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-01-12 14:54:50 -05:00
|
|
|
updateSliderVisibility: (colorGroup) ->
|
|
|
|
enabled = colorGroup.find('.color-group-checkbox').prop('checked')
|
2014-02-13 12:26:21 -05:00
|
|
|
colorGroup.find('.sliders').toggle Boolean(enabled)
|
|
|
|
|
2014-01-12 14:54:50 -05:00
|
|
|
updateColorSettings: (colorGroup) ->
|
|
|
|
wizardSettings = me.get('wizard') or {}
|
2014-02-13 12:26:21 -05:00
|
|
|
wizardSettings.colorConfig ?= {}
|
2014-01-12 14:54:50 -05:00
|
|
|
colorName = colorGroup.data('name')
|
|
|
|
wizardSettings.colorConfig[colorName] ?= {}
|
|
|
|
if colorGroup.find('.color-group-checkbox').prop('checked')
|
|
|
|
config = {}
|
|
|
|
colorGroup.find('.selector').each (i, slider) ->
|
|
|
|
config[$(slider).data('key')] = $(slider).slider('value') / 100
|
|
|
|
wizardSettings.colorConfig[colorName] = config
|
|
|
|
else
|
|
|
|
delete wizardSettings.colorConfig[colorName]
|
|
|
|
|
|
|
|
me.set('wizard', wizardSettings)
|
|
|
|
@updateMovieClip()
|
|
|
|
@trigger 'change'
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-01-12 14:54:50 -05:00
|
|
|
onSliderChanged: (e, result) =>
|
|
|
|
@updateColorSettings $(result.handle).closest('.color-group')
|
|
|
|
|
|
|
|
initStage: ->
|
|
|
|
@stage = new createjs.Stage(@$el.find('canvas')[0])
|
|
|
|
@updateMovieClip()
|
2014-01-15 18:16:31 -05:00
|
|
|
createjs.Ticker.setFPS 20
|
|
|
|
createjs.Ticker.addEventListener("tick", @stage)
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-01-12 14:54:50 -05:00
|
|
|
updateMovieClip: ->
|
|
|
|
return unless @wizardThangType.loaded
|
|
|
|
wizardSettings = me.get('wizard') or {}
|
|
|
|
wizardSettings.colorConfig ?= {}
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-01-12 14:54:50 -05:00
|
|
|
@stage.removeChild(@movieClip) if @movieClip
|
|
|
|
options = {colorConfig: wizardSettings.colorConfig}
|
|
|
|
@spriteBuilder.setOptions options
|
|
|
|
@spriteBuilder.buildColorMaps()
|
2014-01-15 18:16:31 -05:00
|
|
|
castAction = @wizardThangType.get('actions')?.cast
|
|
|
|
return unless castAction?.animation
|
|
|
|
@movieClip = @spriteBuilder.buildMovieClip castAction.animation
|
|
|
|
@movieClip.scaleY = @movieClip.scaleX = 1.7 * (castAction.scale or 1)
|
|
|
|
reg = castAction.positions?.registration
|
2014-01-12 14:54:50 -05:00
|
|
|
if reg
|
2014-02-13 12:26:21 -05:00
|
|
|
@movieClip.regX = reg.x
|
2014-01-12 14:54:50 -05:00
|
|
|
@movieClip.regY = reg.y
|
|
|
|
@stage.addChild @movieClip
|
2014-01-15 18:16:31 -05:00
|
|
|
@stage.update()
|
|
|
|
|
|
|
|
destroy: ->
|
2014-02-13 12:26:21 -05:00
|
|
|
@stage?.removeAllEventListeners()
|
2014-02-24 12:06:22 -05:00
|
|
|
super()
|