codecombat/app/views/play/level/tome/CastButtonView.coffee

104 lines
3.6 KiB
CoffeeScript
Raw Normal View History

2014-07-17 20:20:11 -04:00
CocoView = require 'views/kinds/CocoView'
2014-01-03 13:32:13 -05:00
template = require 'templates/play/level/tome/cast_button'
{me} = require 'lib/auth'
2014-07-17 20:20:11 -04:00
module.exports = class CastButtonView extends CocoView
2014-01-03 13:32:13 -05:00
id: 'cast-button-view'
template: template
2014-03-24 13:11:27 -04:00
events:
'click .cast-button': 'onCastButtonClick'
'click .autocast-delays a': 'onCastOptionsClick'
2014-01-03 13:32:13 -05:00
subscriptions:
2014-06-30 22:16:26 -04:00
'tome:spell-changed': 'onSpellChanged'
2014-01-03 13:32:13 -05:00
'tome:cast-spells': 'onCastSpells'
'god:world-load-progress-changed': 'onWorldLoadProgressChanged'
'god:new-world-created': 'onNewWorld'
constructor: (options) ->
super options
@spells = options.spells
@levelID = options.levelID
2014-06-30 22:16:26 -04:00
@castShortcut = '⇧↵'
@castShortcutVerbose = 'Shift+Enter'
2014-01-03 13:32:13 -05:00
2014-02-11 17:58:45 -05:00
getRenderData: (context={}) ->
2014-01-03 13:32:13 -05:00
context = super context
context.castShortcutVerbose = @castShortcutVerbose
context
afterRender: ->
super()
2014-02-12 15:41:41 -05:00
@castButton = $('.cast-button', @$el)
@castButtonGroup = $('.cast-button-group', @$el)
@castOptions = $('.autocast-delays', @$el)
delay = me.get('autocastDelay')
2014-01-03 13:32:13 -05:00
delay ?= 5000
2014-06-20 23:52:50 -04:00
unless @levelID in ['rescue-mission', 'grab-the-mushroom', 'drink-me', 'its-a-trap', 'break-the-prison', 'taunt', 'cowardly-taunt', 'commanding-followers', 'mobile-artillery']
delay = 90019001
2014-01-03 13:32:13 -05:00
@setAutocastDelay delay
2014-01-25 18:11:29 -05:00
attachTo: (spellView) ->
@$el.detach().prependTo(spellView.toolbarView.$el).show()
2014-02-12 23:45:29 -05:00
onCastButtonClick: (e) ->
Backbone.Mediator.publish 'tome:manual-cast', {}
2014-02-13 20:43:42 -05:00
onCastOptionsClick: (e) =>
Backbone.Mediator.publish 'tome:focus-editor'
2014-02-12 15:41:41 -05:00
@castButtonGroup.removeClass 'open'
@setAutocastDelay $(e.target).attr 'data-delay'
false
2014-01-03 13:32:13 -05:00
onSpellChanged: (e) ->
@updateCastButton()
onCastSpells: (e) ->
return if e.preload
2014-01-03 13:32:13 -05:00
@casting = true
if @hasStartedCastingOnce # Don't play this sound the first time
Backbone.Mediator.publish 'play-sound', trigger: 'cast', volume: 0.5
@hasStartedCastingOnce = true
2014-01-03 13:32:13 -05:00
@updateCastButton()
@onWorldLoadProgressChanged progress: 0
2014-01-03 13:32:13 -05:00
onWorldLoadProgressChanged: (e) ->
return # trying out showing progress on the canvas instead
2014-01-03 13:32:13 -05:00
overlay = @castButtonGroup.find '.button-progress-overlay'
overlay.css 'width', e.progress * @castButton.outerWidth() + 1
2014-01-03 13:32:13 -05:00
onNewWorld: (e) ->
@casting = false
if @hasCastOnce # Don't play this sound the first time
Backbone.Mediator.publish 'play-sound', trigger: 'cast-end', volume: 0.5
@hasCastOnce = true
2014-01-03 13:32:13 -05:00
@updateCastButton()
updateCastButton: ->
return if _.some @spells, (spell) => not spell.loaded
2014-04-22 14:04:56 -04:00
async.some _.values(@spells), (spell, callback) =>
spell.hasChangedSignificantly spell.getSource(), null, callback
, (castable) =>
Backbone.Mediator.publish 'tome:spell-has-changed-significantly-calculation', hasChangedSignificantly: castable
@castButtonGroup.toggleClass('castable', castable).toggleClass('casting', @casting)
if @casting
2014-06-30 22:16:26 -04:00
s = $.i18n.t('play_level.tome_cast_button_casting', defaultValue: 'Casting')
else if castable
2014-06-30 22:16:26 -04:00
s = $.i18n.t('play_level.tome_cast_button_castable', defaultValue: 'Cast Spell') + ' ' + @castShortcut
else
2014-06-30 22:16:26 -04:00
s = $.i18n.t('play_level.tome_cast_button_cast', defaultValue: 'Spell Cast')
@castButton.text s
@castButton.prop 'disabled', not castable
2014-01-03 13:32:13 -05:00
setAutocastDelay: (delay) ->
2014-06-30 22:16:26 -04:00
#console.log 'Set autocast delay to', delay
2014-01-03 13:32:13 -05:00
return unless delay
@autocastDelay = delay = parseInt delay
me.set('autocastDelay', delay)
me.patch()
2014-01-03 13:32:13 -05:00
spell.view.setAutocastDelay delay for spellKey, spell of @spells
@castOptions.find('a').each ->
$(@).toggleClass('selected', parseInt($(@).attr('data-delay')) is delay)