From 68808d0e07c89ca24d360a3ac5cf1c173d71f99e Mon Sep 17 00:00:00 2001 From: Nick Winter Date: Thu, 11 Sep 2014 08:38:30 -0700 Subject: [PATCH] Some fixes for how audio is triggered. --- app/lib/surface/MusicPlayer.coffee | 5 +++-- app/views/game-menu/GameMenuModal.coffee | 11 ++++++++--- app/views/play/level/LevelGoalsView.coffee | 18 +++++++++++++++--- app/views/play/level/LevelLoadingView.coffee | 2 +- .../level/tome/SpellListTabEntryView.coffee | 3 ++- .../level/tome/SpellPaletteEntryView.coffee | 8 ++++---- 6 files changed, 33 insertions(+), 14 deletions(-) diff --git a/app/lib/surface/MusicPlayer.coffee b/app/lib/surface/MusicPlayer.coffee index 4d3e8b47d..606e7eed6 100644 --- a/app/lib/surface/MusicPlayer.coffee +++ b/app/lib/surface/MusicPlayer.coffee @@ -3,6 +3,7 @@ AudioPlayer = require 'lib/AudioPlayer' {me} = require 'lib/auth' CROSSFADE_LENGTH = 1500 +MUSIC_VOLUME = 0.6 module.exports = class MusicPlayer extends CocoClass currentMusic: null @@ -51,7 +52,7 @@ module.exports = class MusicPlayer extends CocoClass return unless @currentMusic @currentMusic.volume = 0.0 if me.get('music') - createjs.Tween.get(@currentMusic).to({volume: 1.0}, CROSSFADE_LENGTH) + createjs.Tween.get(@currentMusic).to({volume: MUSIC_VOLUME}, CROSSFADE_LENGTH) onMusicSettingChanged: -> @updateMusicVolume() @@ -59,7 +60,7 @@ module.exports = class MusicPlayer extends CocoClass updateMusicVolume: -> return unless @currentMusic createjs.Tween.removeTweens(@currentMusic) - @currentMusic.volume = if me.get('music') then 1.0 else 0.0 + @currentMusic.volume = if me.get('music') then MUSIC_VOLUME else 0.0 destroy: -> me.off 'change:music', @onMusicSettingChanged, @ diff --git a/app/views/game-menu/GameMenuModal.coffee b/app/views/game-menu/GameMenuModal.coffee index 2881cc4a7..042c1f9b5 100644 --- a/app/views/game-menu/GameMenuModal.coffee +++ b/app/views/game-menu/GameMenuModal.coffee @@ -15,14 +15,15 @@ module.exports = class GameMenuModal extends ModalView id: 'game-menu-modal' instant: true + events: + 'change input.select': 'onSelectionChanged' + 'shown.bs.tab .nav-tabs a': 'onTabShown' + constructor: (options) -> super options @options.showDevBits = me.isAdmin() or /https?:\/\/localhost/.test(window.location.href) @options.showInventory = @options.level.get('type', true) is 'hero' - events: - 'change input.select': 'onSelectionChanged' - getRenderData: (context={}) -> context = super(context) context.showDevBits = @options.showDevBits @@ -36,6 +37,10 @@ module.exports = class GameMenuModal extends ModalView (if @options.showInventory then @subviews.inventory_view else @subviews.choose_hero_view).$el.addClass 'active' Backbone.Mediator.publish 'audio-player:play-sound', trigger: 'game-menu-open', volume: 1 + onTabShown: -> + Backbone.Mediator.publish 'audio-player:play-sound', trigger: 'game-menu-tab-switch', volume: 1 + onHidden: -> subview.onHidden?() for subviewKey, subview of @subviews me.patch() + Backbone.Mediator.publish 'audio-player:play-sound', trigger: 'game-menu-close', volume: 1 diff --git a/app/views/play/level/LevelGoalsView.coffee b/app/views/play/level/LevelGoalsView.coffee index 15f0b5d7f..4a187879d 100644 --- a/app/views/play/level/LevelGoalsView.coffee +++ b/app/views/play/level/LevelGoalsView.coffee @@ -28,7 +28,7 @@ module.exports = class LevelGoalsView extends CocoView @updatePlacement() onNewGoalStates: (e) -> - firstRun = @previousGoalStatus? + firstRun = not @previousGoalStatus? @previousGoalStatus ?= {} @$el.find('.goal-status').addClass 'secret' classToShow = null @@ -63,8 +63,9 @@ module.exports = class LevelGoalsView extends CocoView goals.push goal if not firstRun and state.status is 'success' and @previousGoalStatus[goal.id] isnt 'success' Backbone.Mediator.publish 'audio-player:play-sound', trigger: 'goal-success', volume: 1 - else if not firstRun and state.status is 'incomplete' and @previousGoalStatus[goal.id] is 'success' + else if not firstRun and state.status isnt 'success' and @previousGoalStatus[goal.id] is 'success' Backbone.Mediator.publish 'audio-player:play-sound', trigger: 'goal-incomplete-again', volume: 1 + @previousGoalStatus[goal.id] = state.status @$el.removeClass('secret') if goals.length > 0 onSurfacePlaybackRestarted: -> @@ -91,9 +92,20 @@ module.exports = class LevelGoalsView extends CocoView sound = if expand then 'goals-expand' else 'goals-collapse' top = if expand then -10 else 26 - @$el.outerHeight() @$el.css 'top', top - Backbone.Mediator.publish 'audio-player:play-sound', trigger: sound, volume: 1 if @expanded? + if @soundTimeout + # Don't play the sound we were going to play after all; the transition has reversed. + clearTimeout @soundTimeout + @soundTimeout = null + else if @expanded? + # Play it when the transition ends, not when it begins. + @soundTimeout = _.delay @playToggleSound, 500, sound @expanded = expand + playToggleSound: (sound) => + return if @destroyed + Backbone.Mediator.publish 'audio-player:play-sound', trigger: sound, volume: 1 + @soundTimeout = null + onSetLetterbox: (e) -> @$el.toggle not e.on @updatePlacement() diff --git a/app/views/play/level/LevelLoadingView.coffee b/app/views/play/level/LevelLoadingView.coffee index 383d969c3..9d8ec311c 100644 --- a/app/views/play/level/LevelLoadingView.coffee +++ b/app/views/play/level/LevelLoadingView.coffee @@ -31,7 +31,7 @@ module.exports = class LevelLoadingView extends CocoView loadingDetails.css 'top', -loadingDetails.outerHeight(true) @$el.find('.left-wing').css left: '-100%', backgroundPosition: 'right -400px top 0' @$el.find('.right-wing').css right: '-100%', backgroundPosition: 'left -400px top 0' - Backbone.Mediator.publish 'audio-player:play-sound', trigger: 'loading-view-unveil', volume: 1 + Backbone.Mediator.publish 'audio-player:play-sound', trigger: 'loading-view-unveil', volume: 0.5 _.delay @onUnveilEnded, duration * 1000 onUnveilEnded: => diff --git a/app/views/play/level/tome/SpellListTabEntryView.coffee b/app/views/play/level/tome/SpellListTabEntryView.coffee index d00e3aa6f..5d30e395c 100644 --- a/app/views/play/level/tome/SpellListTabEntryView.coffee +++ b/app/views/play/level/tome/SpellListTabEntryView.coffee @@ -78,7 +78,8 @@ module.exports = class SpellListTabEntryView extends SpellListEntryView trigger: 'hover' content: docFormatter.formatPopover() container: @$el.parent() - ) + ).on 'show.bs.popover', => + Backbone.Mediator.publish 'audio-player:play-sound', trigger: "spell-tab-entry-open", volume: 0.75 onMouseEnterAvatar: (e) -> # Don't call super onMouseLeaveAvatar: (e) -> # Don't call super diff --git a/app/views/play/level/tome/SpellPaletteEntryView.coffee b/app/views/play/level/tome/SpellPaletteEntryView.coffee index 3f81a5d4c..434a12176 100644 --- a/app/views/play/level/tome/SpellPaletteEntryView.coffee +++ b/app/views/play/level/tome/SpellPaletteEntryView.coffee @@ -44,11 +44,10 @@ module.exports = class SpellPaletteEntryView extends CocoView content: @docFormatter.formatPopover() container: 'body' template: @overridePopoverTemplate - ) - window.element = @$el - @$el.on 'show.bs.popover', => + ).on 'show.bs.popover', => Backbone.Mediator.publish 'tome:palette-hovered', thang: @thang, prop: @doc.name, entry: @ - Backbone.Mediator.publish 'audio-player:play-sound', trigger: 'spell-palette-entry-open', volume: 1 + soundIndex = Math.floor(Math.random() * 4) + Backbone.Mediator.publish 'audio-player:play-sound', trigger: "spell-palette-entry-open-{soundIndex}", volume: 0.75 onMouseEnter: (e) -> # Make sure the doc has the updated Thang so it can regenerate its prop value @@ -65,6 +64,7 @@ module.exports = class SpellPaletteEntryView extends CocoView @$el.add('.spell-palette-popover.popover').removeClass 'pinned' $('.spell-palette-popover.popover .close').remove() @$el.popover 'hide' + Backbone.Mediator.publish 'audio-player:play-sound', trigger: 'spell-palette-entry-unpin', volume: 1 else @popoverPinned = true @$el.popover 'show'