Support for both new worker-based player and old one

This commit is contained in:
Marcin Kulik 2012-08-01 22:53:10 +02:00
parent 1672176335
commit 4b26edfd49
8 changed files with 117 additions and 75 deletions

View File

@ -5,12 +5,7 @@
//= require vendor/utf8
//= require extensions
//= require player/brush
//= require player/vt/sgr_interpreter
//= require player/vt/special_charset
//= require player/vt/scroll_region
//= require player/vt/tab_stops
//= require player/vt/screen_buffer
//= require player/vt/vt
//= require_tree ./player/vt
//= require player/movie
//= require player/workers/main_worker

View File

@ -1,13 +1,18 @@
//= require vendor/rAF
//= require vendor/base64.min
//= require vendor/bzip2
//= require vendor/utf8
//= require extensions
//= require namespace
//= require player/views/renderers/base
//= require_tree ./player/views/renderers
//= require player/views/hud_view
//= require player/data_unpacker
//= require player/abstract_player
//= require_tree ./player/vt
//= require player/movie
//= require player/player
//= require player/fallback_player
//= require player/views/player_view
//= require player/workers/worker_object_proxy
//= require player/workers/worker_proxy

View File

@ -0,0 +1,75 @@
class AsciiIo.AbstractPlayer
constructor: (@options) ->
@model = @options.model
@createView()
@fetchModel()
createView: ->
@view = new AsciiIo.PlayerView
el: @options.el
model: @model
cols: @options.cols
lines: @options.lines
hud: @options.hud
rendererClass: @options.rendererClass
snapshot: @options.snapshot
createVT: ->
throw 'not implemented'
createMovie: ->
throw 'not implemented'
movieOptions: ->
timing: @model.get 'stdout_timing_data'
stdout_data: @model.get 'stdout_data'
duration: @model.get 'duration'
speed: @options.speed
benchmark: @options.benchmark
cols: @options.cols
lines: @options.lines
fetchModel: ->
@model.fetch success: @onModelFetched
onModelFetched: =>
data = @model.get('escaped_stdout_data')
unpacker = new AsciiIo.DataUnpacker
unpacker.unpack data, @onModelDataUnpacked
onModelDataUnpacked: (data) =>
@model.set stdout_data: data
@onModelReady()
onModelReady: ->
@createVT()
@createMovie()
@bindEvents()
@view.onModelReady()
if @options.autoPlay
@movie.call 'play'
else
@view.showPlayOverlay()
bindEvents: ->
@view.on 'play-clicked', => @movie.call 'togglePlay'
@view.on 'seek-clicked', (percent) => @movie.call 'seek', percent
@vt.on 'cursor-visibility', (show) => @view.showCursor show
@movie.on 'started', => @view.onStateChanged 'playing'
@movie.on 'paused', => @view.onStateChanged 'paused'
@movie.on 'finished', => @view.onStateChanged 'finished'
@movie.on 'resumed', => @view.onStateChanged 'resumed'
@movie.on 'wakeup', => @view.restartCursorBlink()
@movie.on 'time', (time) => @view.updateTime time
@movie.on 'render', (state) => @view.renderState state
if @options.benchmark
@movie.on 'started', => @startedAt = (new Date).getTime()
@movie.on 'finished', =>
now = (new Date).getTime()
console.log "finished in #{(now - @startedAt) / 1000.0}s"

View File

@ -0,0 +1,21 @@
class AsciiIo.FallbackPlayer extends AsciiIo.AbstractPlayer
createVT: ->
@vt = new AsciiIo.VT @options.cols, @options.lines
createMovie: ->
@movie = new AsciiIo.Movie @movieOptions()
onModelReady: ->
super
bindEvents: ->
super
@movie.on 'reset', => @vt.reset()
@movie.on 'data', (data) =>
@vt.feed data
state = @vt.state()
@vt.clearChanges()
@movie.trigger 'render', state

View File

@ -16,6 +16,9 @@ class AsciiIo.Movie
@clearPauseState()
@trigger 'reset'
call: (method, args...) ->
@[method].apply this, args
now: ->
(new Date()).getTime()

View File

@ -1,84 +1,21 @@
class AsciiIo.Player
class AsciiIo.Player extends AsciiIo.AbstractPlayer
constructor: (@options) ->
@model = @options.model
@createWorkerProxy()
@createView()
@createVT()
@createMovie()
@fetchModel()
super
createWorkerProxy: ->
@workerProxy = new AsciiIo.WorkerProxy(window.mainWorkerPath)
createView: ->
@view = new AsciiIo.PlayerView
el: @options.el
model: @model
cols: @options.cols
lines: @options.lines
hud: @options.hud
rendererClass: @options.rendererClass
snapshot: @options.snapshot
createVT: ->
@vt = @workerProxy.getObjectProxy 'vt'
createMovie: ->
@movie = @workerProxy.getObjectProxy 'movie'
fetchModel: ->
@model.fetch success: @onModelFetched
onModelFetched: =>
data = @model.get('escaped_stdout_data')
unpacker = new AsciiIo.DataUnpacker
unpacker.unpack data, @onModelDataUnpacked
onModelDataUnpacked: (data) =>
@model.set stdout_data: data
@onModelReady()
onModelReady: ->
@initWorkerProxy()
@bindEvents()
@view.onModelReady()
if @options.autoPlay
@movie.call 'play'
else
@view.showPlayOverlay()
super
initWorkerProxy: ->
@workerProxy.init
timing: @model.get 'stdout_timing_data'
stdout_data: @model.get 'stdout_data'
duration: @model.get 'duration'
speed: @options.speed
benchmark: @options.benchmark
cols: @options.cols
lines: @options.lines
bindEvents: ->
@view.on 'play-clicked', => @movie.call 'togglePlay'
@view.on 'seek-clicked', (percent) => @movie.call 'seek', percent
@vt.on 'cursor-visibility', (show) => @view.showCursor show
@movie.on 'started', => @view.onStateChanged 'playing'
@movie.on 'paused', => @view.onStateChanged 'paused'
@movie.on 'finished', => @view.onStateChanged 'finished'
@movie.on 'resumed', => @view.onStateChanged 'resumed'
@movie.on 'wakeup', => @view.restartCursorBlink()
@movie.on 'time', (time) => @view.updateTime time
@movie.on 'render', (state) => @view.renderState state
if @options.benchmark
@movie.on 'started', => @startedAt = (new Date).getTime()
@movie.on 'finished', =>
now = (new Date).getTime()
console.log "finished in #{(now - @startedAt) / 1000.0}s"
@workerProxy.init @movieOptions()

View File

@ -21,7 +21,7 @@ class AsciiIo.PlayerView extends Backbone.View
@hudView = new AsciiIo.HudView(cols: @options.cols)
@hudView.on 'play-click', => @onPlayClicked()
@hudView.on 'seek-click', (percent) => @onSeekClicked()
@hudView.on 'seek-click', (percent) => @onSeekClicked percent
@$el.append @hudView.$el

View File

@ -6,6 +6,12 @@ module AsciicastsHelper
auto_play = options.key?(:auto_play) ? !!options[:auto_play] : false
hud = options.key?(:hud) ? !!options[:hud] : true
if params[:fallback]
player_class = "AsciiIo.FallbackPlayer"
else
player_class = "AsciiIo.Player"
end
if custom_renderer = params[:renderer]
renderer_class = "AsciiIo.Renderer.#{custom_renderer.capitalize}"
else
@ -15,7 +21,7 @@ module AsciicastsHelper
return <<EOS.html_safe
<script>
$(function() {
window.player = new AsciiIo.Player({
window.player = new #{player_class}({
el: $('.player'),
cols: #{asciicast.terminal_columns},
lines: #{asciicast.terminal_lines},