<commit_before>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
    isMac = navigator.platform.toUpperCase().indexOf('MAC') isnt -1
    @castShortcut = "⇧↩"
    @castShortcutVerbose = "Shift+Enter"

  getRenderData: (context={}) ->
    context = super context
    context.castShortcutVerbose = @castShortcutVerbose
    context

  afterRender: ->
    super()
    @castButton = $('.cast-button', @$el)
    @castButtonGroup = $('.cast-button-group', @$el)
    @castOptions = $('.autocast-delays', @$el)
    @castButton.on 'click', @onCastButtonClick
    @castOptions.find('a').on 'click', @onCastOptionsClick
    # TODO: use a User setting instead of localStorage
    delay = localStorage.getItem 'autocastDelay'
    delay ?= 5000
    if @levelID is 'project-dota'
      delay = 90019001
    @setAutocastDelay delay

  attachTo: (spellView) ->
    @$el.detach().prependTo(spellView.toolbarView.$el).show()

  onCastButtonClick: (e) ->
    Backbone.Mediator.publish 'tome:manual-cast', {}

  onCastOptionsClick: (e) =>
    console.log 'cast options click', $(e.target)
    Backbone.Mediator.publish 'focus-editor'
    @castButtonGroup.removeClass 'open'
    @setAutocastDelay $(e.target).attr 'data-delay'
    false

  onSpellChanged: (e) ->
    @updateCastButton()

  onCastSpells: (e) ->
    @casting = true
    Backbone.Mediator.publish 'play-sound', trigger: 'cast', volume: 0.25
    @updateCastButton()
    @onWorldLoadProgressChanged progress: 0

  onWorldLoadProgressChanged: (e) ->
    overlay = @castButtonGroup.find '.button-progress-overlay'
    overlay.css 'width', e.progress * @castButtonGroup.width() + 1

  onNewWorld: (e) ->
    @casting = false
    @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
      s = $.i18n.t("play_level.tome_cast_button_castable", defaultValue: "Cast Spell") + " " + @castShortcut
    else
      s = $.i18n.t("play_level.tome_cast_button_cast", defaultValue: "Spell Cast")
    @castButton.text s
    @castButton.prop 'disabled', not castable<commit_msg>Fixed #7 with a nice cast-end sound (just reverse of cast sound).<commit_after>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
    isMac = navigator.platform.toUpperCase().indexOf('MAC') isnt -1
    @castShortcut = "⇧↩"
    @castShortcutVerbose = "Shift+Enter"

  getRenderData: (context={}) ->
    context = super context
    context.castShortcutVerbose = @castShortcutVerbose
    context

  afterRender: ->
    super()
    @castButton = $('.cast-button', @$el)
    @castButtonGroup = $('.cast-button-group', @$el)
    @castOptions = $('.autocast-delays', @$el)
    @castButton.on 'click', @onCastButtonClick
    @castOptions.find('a').on 'click', @onCastOptionsClick
    # TODO: use a User setting instead of localStorage
    delay = localStorage.getItem 'autocastDelay'
    delay ?= 5000
    if @levelID is 'project-dota'
      delay = 90019001
    @setAutocastDelay delay

  attachTo: (spellView) ->
    @$el.detach().prependTo(spellView.toolbarView.$el).show()

  onCastButtonClick: (e) ->
    Backbone.Mediator.publish 'tome:manual-cast', {}

  onCastOptionsClick: (e) =>
    console.log 'cast options click', $(e.target)
    Backbone.Mediator.publish 'focus-editor'
    @castButtonGroup.removeClass 'open'
    @setAutocastDelay $(e.target).attr 'data-delay'
    false

  onSpellChanged: (e) ->
    @updateCastButton()

  onCastSpells: (e) ->
    @casting = true
    Backbone.Mediator.publish 'play-sound', trigger: 'cast', volume: 0.5
    @updateCastButton()
    @onWorldLoadProgressChanged progress: 0

  onWorldLoadProgressChanged: (e) ->
    overlay = @castButtonGroup.find '.button-progress-overlay'
    overlay.css 'width', e.progress * @castButtonGroup.width() + 1

  onNewWorld: (e) ->
    @casting = false
    Backbone.Mediator.publish 'play-sound', trigger: 'cast-end', volume: 0.5
    @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
      s = $.i18n.t("play_level.tome_cast_button_castable", defaultValue: "Cast Spell") + " " + @castShortcut
    else
      s = $.i18n.t("play_level.tome_cast_button_cast", defaultValue: "Spell Cast")
    @castButton.text s
    @castButton.prop 'disabled', not castable
{'fraction_non_alphanumeric': 0.1404901622367967, 'fraction_numerical': 0.00862961684501208, 'mean_word_length': 3.873843566021867, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7551396', 'n_tokens_mistral': 1984, 'n_tokens_neox': 1910, 'n_words': 390}