2014-11-28 20:49:41 -05:00
|
|
|
CocoView = require 'views/core/CocoView'
|
2014-01-21 12:03:04 -05:00
|
|
|
template = require 'templates/play/level/tome/spell_toolbar'
|
|
|
|
|
2014-07-17 20:20:11 -04:00
|
|
|
module.exports = class SpellToolbarView extends CocoView
|
2014-01-21 12:03:04 -05:00
|
|
|
className: 'spell-toolbar-view'
|
|
|
|
template: template
|
2014-01-25 18:11:29 -05:00
|
|
|
progressHoverDelay: 500
|
2014-01-21 12:03:04 -05:00
|
|
|
|
|
|
|
subscriptions:
|
2014-08-27 15:24:03 -04:00
|
|
|
'tome:spell-step-backward': 'onStepBackward'
|
|
|
|
'tome:spell-step-forward': 'onStepForward'
|
2014-01-21 12:03:04 -05:00
|
|
|
|
|
|
|
events:
|
2014-11-05 20:34:46 -05:00
|
|
|
'mousedown .spell-progress': 'onProgressMouseDown'
|
|
|
|
'mouseup .spell-progress': 'onProgressMouseUp'
|
|
|
|
'mousemove .spell-progress': 'onProgressMouseMove'
|
|
|
|
'tapstart .spell-progress': 'onProgressTapStart'
|
|
|
|
'tapend .spell-progress': 'onProgressTapEnd'
|
|
|
|
'tapmove .spell-progress': 'onProgressTapMove'
|
2014-01-21 12:03:04 -05:00
|
|
|
'click .step-backward': 'onStepBackward'
|
|
|
|
'click .step-forward': 'onStepForward'
|
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super options
|
|
|
|
@ace = options.ace
|
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
|
2014-01-25 18:11:29 -05:00
|
|
|
toggleFlow: (to) ->
|
2014-06-30 22:16:26 -04:00
|
|
|
@$el.find('.flow').toggle to
|
2014-01-25 18:11:29 -05:00
|
|
|
|
2014-01-21 12:03:04 -05:00
|
|
|
setStatementIndex: (statementIndex) ->
|
|
|
|
return unless total = @callState?.statementsExecuted
|
|
|
|
@statementIndex = Math.min(total - 1, Math.max(0, statementIndex))
|
|
|
|
@statementRatio = @statementIndex / (total - 1)
|
2014-01-22 13:56:49 -05:00
|
|
|
@statementTime = @callState.statements[@statementIndex]?.userInfo.time ? 0
|
2014-01-29 15:18:37 -05:00
|
|
|
@$el.find('.progress-bar').css('width', 100 * @statementRatio + '%')
|
2014-01-21 12:03:04 -05:00
|
|
|
@$el.find('.step-backward').prop('disabled', @statementIndex is 0)
|
|
|
|
@$el.find('.step-forward').prop('disabled', @statementIndex is total - 1)
|
|
|
|
@updateMetrics()
|
2014-01-25 18:11:29 -05:00
|
|
|
_.defer =>
|
|
|
|
Backbone.Mediator.publish 'tome:spell-statement-index-updated', statementIndex: @statementIndex, ace: @ace
|
2014-01-21 12:03:04 -05:00
|
|
|
|
|
|
|
updateMetrics: ->
|
|
|
|
statementsExecuted = @callState.statementsExecuted
|
|
|
|
$metrics = @$el.find('.metrics')
|
2014-01-25 18:11:29 -05:00
|
|
|
return $metrics.hide() if @suppressMetricsUpdates or not (statementsExecuted or @metrics.statementsExecuted)
|
2014-01-21 12:03:04 -05:00
|
|
|
if @metrics.callsExecuted > 1
|
|
|
|
$metrics.find('.call-index').text @callIndex + 1
|
|
|
|
$metrics.find('.calls-executed').text @metrics.callsExecuted
|
|
|
|
$metrics.find('.calls-metric').show().attr('title', "Method call #{@callIndex + 1} of #{@metrics.callsExecuted} calls")
|
|
|
|
else
|
|
|
|
$metrics.find('.calls-metric').hide()
|
|
|
|
if @metrics.statementsExecuted
|
|
|
|
$metrics.find('.statement-index').text @statementIndex + 1
|
|
|
|
$metrics.find('.statements-executed').text statementsExecuted
|
|
|
|
if @metrics.statementsExecuted > statementsExecuted
|
|
|
|
$metrics.find('.statements-executed-total').text " (#{@metrics.statementsExecuted})"
|
|
|
|
titleSuffix = " (#{@metrics.statementsExecuted} statements total)"
|
|
|
|
else
|
2014-06-30 22:16:26 -04:00
|
|
|
$metrics.find('.statements-executed-total').text ''
|
|
|
|
titleSuffix = ''
|
2014-01-21 12:03:04 -05:00
|
|
|
$metrics.find('.statements-metric').show().attr('title', "Statement #{@statementIndex + 1} of #{statementsExecuted} this call#{titleSuffix}")
|
|
|
|
else
|
|
|
|
$metrics.find('.statements-metric').hide()
|
2014-01-25 18:11:29 -05:00
|
|
|
left = @$el.find('.scrubber-handle').position().left + @$el.find('.spell-progress').position().left
|
|
|
|
$metrics.finish().show().css({left: left - $metrics.width() / 2}).delay(2000).fadeOut('fast')
|
2014-01-21 12:03:04 -05:00
|
|
|
|
|
|
|
setStatementRatio: (ratio) ->
|
|
|
|
return unless total = @callState?.statementsExecuted
|
2014-01-25 18:11:29 -05:00
|
|
|
statementIndex = Math.floor ratio * total
|
|
|
|
@setStatementIndex statementIndex unless statementIndex is @statementIndex
|
2014-01-21 12:03:04 -05:00
|
|
|
|
2014-11-05 20:34:46 -05:00
|
|
|
onProgressMouseDown: (e) ->
|
|
|
|
@dragging = true
|
|
|
|
@scrubProgress e
|
|
|
|
Backbone.Mediator.publish 'level:set-playing', playing: false
|
2014-01-25 18:11:29 -05:00
|
|
|
|
2014-11-05 20:34:46 -05:00
|
|
|
onProgressMouseUp: (e) ->
|
|
|
|
@dragging = false
|
|
|
|
|
|
|
|
onProgressMouseMove: (e) ->
|
|
|
|
return unless @dragging
|
|
|
|
@scrubProgress e
|
|
|
|
|
|
|
|
onProgressTapStart: (e, touchData) ->
|
|
|
|
# Haven't tested tap versions, don't even need them for iPad app, but hey, it worked for the playback scrubber.
|
|
|
|
@dragging = true
|
|
|
|
@scrubProgress e, touchData
|
|
|
|
|
|
|
|
onProgressTapEnd: (e, touchData) ->
|
|
|
|
@dragging = false
|
|
|
|
|
|
|
|
onProgressTapMove: (e, touchData) ->
|
|
|
|
return unless @dragging
|
|
|
|
@scrubProgress e, touchData
|
|
|
|
|
|
|
|
scrubProgress: (e, touchData) ->
|
|
|
|
screenOffsetX = e.clientX ? touchData?.position.x ? 0
|
|
|
|
offsetX = screenOffsetX - @$el.find('.spell-progress').offset().left
|
|
|
|
offsetX = Math.max offsetX, 0
|
|
|
|
@setStatementRatio offsetX / @$el.find('.spell-progress').width()
|
2014-01-21 12:03:04 -05:00
|
|
|
@updateTime()
|
2014-01-24 11:01:00 -05:00
|
|
|
@updateScroll()
|
2014-01-21 12:03:04 -05:00
|
|
|
|
|
|
|
onStepBackward: (e) -> @step -1
|
|
|
|
onStepForward: (e) -> @step 1
|
|
|
|
step: (delta) ->
|
|
|
|
lastTime = @statementTime
|
|
|
|
@setStatementIndex @statementIndex + delta
|
2014-01-25 18:11:29 -05:00
|
|
|
@updateTime() if @statementTime isnt lastTime
|
2014-01-24 11:01:00 -05:00
|
|
|
@updateScroll()
|
2014-11-05 20:34:46 -05:00
|
|
|
Backbone.Mediator.publish 'level:set-playing', playing: false
|
2014-01-21 12:03:04 -05:00
|
|
|
|
|
|
|
updateTime: ->
|
|
|
|
@maintainIndexScrub = true
|
|
|
|
clearTimeout @maintainIndexScrubTimeout if @maintainIndexScrubTimeout
|
|
|
|
@maintainIndexScrubTimeout = _.delay (=> @maintainIndexScrub = false), 500
|
2014-08-27 15:24:03 -04:00
|
|
|
Backbone.Mediator.publish 'level:set-time', time: @statementTime, scrubDuration: 500
|
2014-01-21 12:03:04 -05:00
|
|
|
|
2014-01-24 11:01:00 -05:00
|
|
|
updateScroll: ->
|
|
|
|
return unless statementStart = @callState?.statements?[@statementIndex]?.range[0]
|
2014-03-24 10:54:59 -04:00
|
|
|
text = @ace.getValue() # code in editor
|
|
|
|
currentLine = statementStart.row
|
2014-01-24 11:01:00 -05:00
|
|
|
@ace.scrollToLine currentLine, true, true
|
|
|
|
|
2014-01-21 12:03:04 -05:00
|
|
|
setCallState: (callState, statementIndex, @callIndex, @metrics) ->
|
|
|
|
return if callState is @callState and statementIndex is @statementIndex
|
|
|
|
return unless @callState = callState
|
2014-01-25 18:11:29 -05:00
|
|
|
@suppressMetricsUpdates = true
|
2014-11-05 20:34:46 -05:00
|
|
|
if not @maintainIndexScrub and not @dragging and statementIndex? and callState.statements[statementIndex]?.userInfo.time isnt @statementTime
|
2014-01-21 12:03:04 -05:00
|
|
|
@setStatementIndex statementIndex
|
|
|
|
else
|
|
|
|
@setStatementRatio @statementRatio
|
2014-01-25 18:11:29 -05:00
|
|
|
@suppressMetricsUpdates = false
|