codecombat/app/views/account/wizard_settings_view.coffee

107 lines
3.4 KiB
CoffeeScript
Raw Normal View History

CocoView = require 'views/kinds/CocoView'
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'
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:
'change .color-group-checkbox': (e) ->
colorGroup = $(e.target).closest('.color-group')
@updateSliderVisibility(colorGroup)
@updateColorSettings(colorGroup)
constructor: ->
super(arguments...)
@loadWizard()
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-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) -> {
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-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 ?= {}
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-01-12 14:54:50 -05:00
@$el.find('.color-group').each (i, colorGroup) =>
@updateSliderVisibility($(colorGroup))
2014-01-12 14:54:50 -05:00
updateSliderVisibility: (colorGroup) ->
enabled = colorGroup.find('.color-group-checkbox').prop('checked')
colorGroup.find('.sliders').toggle Boolean(enabled)
2014-01-12 14:54:50 -05:00
updateColorSettings: (colorGroup) ->
wizardSettings = 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')
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-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-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
2014-02-24 15:52:35 -05:00
@stage.update()