From 4b26edfd490384e5063a15a356f005feb0b2321b Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Wed, 1 Aug 2012 22:53:10 +0200 Subject: [PATCH] Support for both new worker-based player and old one --- app/assets/javascripts/main_worker.js | 7 +- app/assets/javascripts/player.js | 5 ++ .../player/abstract_player.js.coffee | 75 +++++++++++++++++++ .../player/fallback_player.js.coffee | 21 ++++++ app/assets/javascripts/player/movie.js.coffee | 3 + .../javascripts/player/player.js.coffee | 71 +----------------- .../player/views/player_view.js.coffee | 2 +- app/helpers/asciicasts_helper.rb | 8 +- 8 files changed, 117 insertions(+), 75 deletions(-) create mode 100644 app/assets/javascripts/player/abstract_player.js.coffee create mode 100644 app/assets/javascripts/player/fallback_player.js.coffee diff --git a/app/assets/javascripts/main_worker.js b/app/assets/javascripts/main_worker.js index 222b014..daeb421 100644 --- a/app/assets/javascripts/main_worker.js +++ b/app/assets/javascripts/main_worker.js @@ -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 diff --git a/app/assets/javascripts/player.js b/app/assets/javascripts/player.js index 74084bc..fd83b5f 100644 --- a/app/assets/javascripts/player.js +++ b/app/assets/javascripts/player.js @@ -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 diff --git a/app/assets/javascripts/player/abstract_player.js.coffee b/app/assets/javascripts/player/abstract_player.js.coffee new file mode 100644 index 0000000..48776e6 --- /dev/null +++ b/app/assets/javascripts/player/abstract_player.js.coffee @@ -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" diff --git a/app/assets/javascripts/player/fallback_player.js.coffee b/app/assets/javascripts/player/fallback_player.js.coffee new file mode 100644 index 0000000..232458b --- /dev/null +++ b/app/assets/javascripts/player/fallback_player.js.coffee @@ -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 diff --git a/app/assets/javascripts/player/movie.js.coffee b/app/assets/javascripts/player/movie.js.coffee index d55df2a..7ae7f81 100644 --- a/app/assets/javascripts/player/movie.js.coffee +++ b/app/assets/javascripts/player/movie.js.coffee @@ -16,6 +16,9 @@ class AsciiIo.Movie @clearPauseState() @trigger 'reset' + call: (method, args...) -> + @[method].apply this, args + now: -> (new Date()).getTime() diff --git a/app/assets/javascripts/player/player.js.coffee b/app/assets/javascripts/player/player.js.coffee index bbc0d0a..960c4d4 100644 --- a/app/assets/javascripts/player/player.js.coffee +++ b/app/assets/javascripts/player/player.js.coffee @@ -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() diff --git a/app/assets/javascripts/player/views/player_view.js.coffee b/app/assets/javascripts/player/views/player_view.js.coffee index 4925185..b4d4065 100644 --- a/app/assets/javascripts/player/views/player_view.js.coffee +++ b/app/assets/javascripts/player/views/player_view.js.coffee @@ -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 diff --git a/app/helpers/asciicasts_helper.rb b/app/helpers/asciicasts_helper.rb index ebf5242..761abc2 100644 --- a/app/helpers/asciicasts_helper.rb +++ b/app/helpers/asciicasts_helper.rb @@ -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 < $(function() { - window.player = new AsciiIo.Player({ + window.player = new #{player_class}({ el: $('.player'), cols: #{asciicast.terminal_columns}, lines: #{asciicast.terminal_lines},