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

100 lines
3.4 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
View = require 'views/kinds/CocoView'
template = require 'templates/play/level/tome/cast_button'
{me} = require 'lib/auth'
module.exports = class CastButtonView extends View
id: 'cast-button-view'
template: template
subscriptions:
'tome:spell-changed': "onSpellChanged"
'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-01-03 13:32:13 -05:00
isMac = navigator.platform.toUpperCase().indexOf('MAC') isnt -1
@castShortcut = "⇧↩"
@castShortcutVerbose = "Shift+Enter"
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)
2014-02-12 23:45:29 -05:00
@castButton.on 'click', @onCastButtonClick
2014-02-13 20:43:42 -05:00
@castOptions.find('a').on 'click', @onCastOptionsClick
2014-01-03 13:32:13 -05:00
# TODO: use a User setting instead of localStorage
delay = localStorage.getItem 'autocastDelay'
delay ?= 5000
if @levelID is 'project-dota'
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) =>
console.log 'cast options click', $(e.target)
2014-02-12 15:41:41 -05:00
Backbone.Mediator.publish 'focus-editor'
@castButtonGroup.removeClass 'open'
@setAutocastDelay $(e.target).attr 'data-delay'
false
2014-01-03 13:32:13 -05:00
onSpellChanged: (e) ->
@updateCastButton()
onCastSpells: (e) ->
@casting = true
Backbone.Mediator.publish 'play-sound', trigger: 'cast', volume: 0.5
2014-01-03 13:32:13 -05:00
@updateCastButton()
@onWorldLoadProgressChanged progress: 0
2014-01-03 13:32:13 -05:00
onWorldLoadProgressChanged: (e) ->
overlay = @castButtonGroup.find '.button-progress-overlay'
overlay.css 'width', e.progress * @castButtonGroup.width() + 1
2014-01-03 13:32:13 -05:00
onNewWorld: (e) ->
@casting = false
Backbone.Mediator.publish 'play-sound', trigger: 'cast-end', volume: 0.5
2014-01-03 13:32:13 -05:00
@updateCastButton()
updateCastButton: ->
return if _.some @spells, (spell) => not spell.loaded
castable = _.some @spells, (spell) => spell.hasChangedSignificantly spell.getSource()
@castButtonGroup.toggleClass('castable', castable).toggleClass('casting', @casting)
if @casting
s = $.i18n.t("play_level.tome_cast_button_casting", defaultValue: "Casting")
else if castable
2014-02-19 21:20:21 -05:00
s = $.i18n.t("play_level.tome_cast_button_castable", defaultValue: "Cast Spell") + " " + @castShortcut
2014-01-03 13:32:13 -05:00
else
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) ->
#console.log "Set autocast delay to", delay
return unless delay
@autocastDelay = delay = parseInt delay
localStorage.setItem 'autocastDelay', delay
spell.view.setAutocastDelay delay for spellKey, spell of @spells
@castOptions.find('a').each ->
$(@).toggleClass('selected', parseInt($(@).attr('data-delay')) is delay)
destroy: ->
2014-02-12 23:45:29 -05:00
@castButton.off 'click', @onCastButtonClick
2014-02-13 20:43:42 -05:00
@castOptions.find('a').off 'click', @onCastOptionsClick
@onCastOptionsClick = null
super()