2014-11-28 20:49:41 -05:00
|
|
|
CocoView = require 'views/core/CocoView'
|
2014-11-29 18:31:34 -05:00
|
|
|
template = require 'templates/account/wizard-settings-view'
|
2014-11-28 20:49:41 -05:00
|
|
|
{me} = require 'core/auth'
|
2014-01-12 14:54:50 -05:00
|
|
|
ThangType = require 'models/ThangType'
|
|
|
|
SpriteBuilder = require 'lib/sprites/SpriteBuilder'
|
2014-11-28 20:49:41 -05:00
|
|
|
{hslToHex, hexToHSL} = require 'core/utils'
|
2014-01-12 14:54:50 -05:00
|
|
|
|
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-13 12:26:21 -05:00
|
|
|
|
2014-01-12 14:54:50 -05:00
|
|
|
events:
|
2014-07-07 21:13:19 -04:00
|
|
|
'click .color-group': (e) ->
|
|
|
|
return if $(e.target).closest('.minicolors')[0]
|
|
|
|
return if $(e.target).closest('.color-group-checkbox')[0]
|
|
|
|
return if $(e.target).closest('label')[0]
|
|
|
|
$(e.target).closest('.color-group').find('.color-group-checkbox').click()
|
2014-01-12 14:54:50 -05:00
|
|
|
'change .color-group-checkbox': (e) ->
|
|
|
|
colorGroup = $(e.target).closest('.color-group')
|
|
|
|
@updateColorSettings(colorGroup)
|
2014-02-27 15:25:59 -05:00
|
|
|
@updateSwatchVisibility(colorGroup)
|
2014-01-12 14:54:50 -05:00
|
|
|
|
|
|
|
constructor: ->
|
|
|
|
super(arguments...)
|
|
|
|
@loadWizard()
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-01-12 14:54:50 -05:00
|
|
|
loadWizard: ->
|
|
|
|
@wizardThangType = new ThangType()
|
2014-08-26 20:34:00 -04:00
|
|
|
@wizardThangType.setURL '/db/thang.type/wizard'
|
|
|
|
@supermodel.loadModel @wizardThangType, 'wizard'
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-08-26 20:34:00 -04:00
|
|
|
onLoaded: ->
|
|
|
|
super()
|
2014-01-12 14:54:50 -05:00
|
|
|
@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-27 15:25:59 -05:00
|
|
|
f = (name) ->
|
|
|
|
hslObj = wizardSettings[name]
|
|
|
|
hsl = if hslObj then [hslObj.hue, hslObj.saturation, hslObj.lightness] else [0, 0.5, 0.5]
|
|
|
|
return {
|
|
|
|
dasherized: _.string.dasherize(name)
|
|
|
|
humanized: _.string.humanize name
|
|
|
|
name: name
|
|
|
|
exists: wizardSettings[name]
|
|
|
|
rgb: hslToHex(hsl)
|
|
|
|
}
|
2014-07-09 13:47:02 -04:00
|
|
|
c.colorGroups = (f(colorName) for colorName in _.keys colorGroups when colorName isnt 'team')
|
2014-01-12 14:54:50 -05:00
|
|
|
c
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-01-12 14:54:50 -05:00
|
|
|
afterRender: ->
|
2014-08-26 20:34:00 -04:00
|
|
|
return unless @supermodel.finished()
|
2014-01-12 14:54:50 -05:00
|
|
|
wizardSettings = me.get('wizard') or {}
|
|
|
|
wizardSettings.colorConfig ?= {}
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-02-27 15:25:59 -05:00
|
|
|
@$el.find('.minicolors').each (e, minicolor) =>
|
|
|
|
$(minicolor).minicolors({
|
|
|
|
change: => @updateColorSettings($(minicolor).closest('.color-group'))
|
|
|
|
changeDelay: 200
|
|
|
|
})
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-01-12 14:54:50 -05:00
|
|
|
@$el.find('.color-group').each (i, colorGroup) =>
|
2014-02-27 15:25:59 -05:00
|
|
|
@updateSwatchVisibility($(colorGroup))
|
2014-02-13 12:26:21 -05:00
|
|
|
|
2014-02-27 15:25:59 -05:00
|
|
|
updateSwatchVisibility: (colorGroup) ->
|
2014-01-12 14:54:50 -05:00
|
|
|
enabled = colorGroup.find('.color-group-checkbox').prop('checked')
|
2014-02-27 15:25:59 -05:00
|
|
|
colorGroup.find('.minicolors-swatch').toggle Boolean(enabled)
|
2014-03-11 21:30:25 -04:00
|
|
|
|
2014-02-27 15:25:59 -05:00
|
|
|
updateColorSettings: (colorGroup) =>
|
2014-03-18 16:08:26 -04:00
|
|
|
wizardSettings = $.extend(true, {}, 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')
|
2014-02-27 15:25:59 -05:00
|
|
|
input = colorGroup.find('.minicolors-input')
|
|
|
|
hex = input.val()
|
|
|
|
hsl = hexToHSL(hex)
|
2014-06-30 22:16:26 -04:00
|
|
|
config = {hue: hsl[0], saturation: hsl[1], lightness: hsl[2]}
|
2014-01-12 14:54:50 -05:00
|
|
|
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
|
|
|
initStage: ->
|
|
|
|
@stage = new createjs.Stage(@$el.find('canvas')[0])
|
|
|
|
@updateMovieClip()
|
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-03-11 21:30:25 -04:00
|
|
|
@stage.update()
|