codecombat/app/views/account/wizard_settings_view.coffee

117 lines
3.8 KiB
CoffeeScript
Raw Normal View History

CocoView = require 'views/kinds/CocoView'
template = require 'templates/account/wizard_settings'
2014-06-30 22:16:26 -04:00
{me} = require 'lib/auth'
2014-01-12 14:54:50 -05:00
ThangType = require 'models/ThangType'
SpriteBuilder = require 'lib/sprites/SpriteBuilder'
{hslToHex, hexToHSL} = require 'lib/utils'
2014-01-12 14:54:50 -05:00
module.exports = class WizardSettingsView extends CocoView
id: 'wizard-settings-view'
2014-01-12 14:54:50 -05:00
template: template
startsLoading: true
2014-01-12 14:54:50 -05:00
events:
'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)
@updateSwatchVisibility(colorGroup)
2014-01-12 14:54:50 -05:00
constructor: ->
super(arguments...)
@loadWizard()
2014-01-12 14:54:50 -05:00
loadWizard: ->
@wizardThangType = new ThangType()
@wizardThangType.url = -> '/db/thang.type/wizard'
2014-01-12 14:54:50 -05:00
@wizardThangType.fetch()
@listenToOnce(@wizardThangType, 'sync', @initCanvas)
2014-01-12 14:54:50 -05:00
initCanvas: ->
@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-01-12 14:54:50 -05:00
colorGroups = @wizardThangType.get('colorGroups') or {}
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-01-12 14:54:50 -05:00
afterRender: ->
return if @startsLoading
2014-01-12 14:54:50 -05:00
wizardSettings = me.get('wizard') or {}
wizardSettings.colorConfig ?= {}
@$el.find('.minicolors').each (e, minicolor) =>
$(minicolor).minicolors({
change: => @updateColorSettings($(minicolor).closest('.color-group'))
changeDelay: 200
})
2014-01-12 14:54:50 -05:00
@$el.find('.color-group').each (i, colorGroup) =>
@updateSwatchVisibility($(colorGroup))
updateSwatchVisibility: (colorGroup) ->
2014-01-12 14:54:50 -05:00
enabled = colorGroup.find('.color-group-checkbox').prop('checked')
colorGroup.find('.minicolors-swatch').toggle Boolean(enabled)
updateColorSettings: (colorGroup) =>
wizardSettings = $.extend(true, {}, me.get('wizard')) or {}
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')
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-01-12 14:54:50 -05:00
initStage: ->
@stage = new createjs.Stage(@$el.find('canvas')[0])
@updateMovieClip()
2014-01-12 14:54:50 -05:00
updateMovieClip: ->
return unless @wizardThangType.loaded
wizardSettings = me.get('wizard') or {}
wizardSettings.colorConfig ?= {}
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
@movieClip.regX = reg.x
2014-01-12 14:54:50 -05:00
@movieClip.regY = reg.y
@stage.addChild @movieClip
@stage.update()