src/js/libs/popcorn.code.js
author hamidouk
Tue, 29 Nov 2011 11:09:08 +0100
branchpopcorn-port
changeset 345 8a088f7daa66
parent 110 048125f1a167
permissions -rw-r--r--
rollover over the interface buttons now works as expected. Also changed the width of the buttons to the correct size. Resized the width and height of the sprites to be the same as the boxes we display them in.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
110
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     1
// PLUGIN: Code
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     2
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     3
(function ( Popcorn ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     4
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     5
  /**
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     6
   * Code Popcorn Plug-in
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     7
   *
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     8
   * Adds the ability to run arbitrary code (JavaScript functions) according to video timing.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
     9
   *
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    10
   * @param {Object} options
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    11
   *
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    12
   * Required parameters: start, end, template, data, and target.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    13
   * Optional parameter: static.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    14
   *
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    15
   *   start: the time in seconds when the mustache template should be rendered
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    16
   *          in the target div.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    17
   *
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    18
   *   end: the time in seconds when the rendered mustache template should be
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    19
   *        removed from the target div.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    20
   *
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    21
   *   onStart: the function to be run when the start time is reached.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    22
   *
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    23
   *   onFrame: [optional] a function to be run on each paint call
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    24
   *            (e.g., called ~60 times per second) between the start and end times.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    25
   *
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    26
   *   onEnd: [optional] a function to be run when the end time is reached.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    27
   *
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    28
   * Example:
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    29
     var p = Popcorn('#video')
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    30
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    31
        // onStart function only
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    32
        .code({
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    33
          start: 1,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    34
          end: 4,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    35
          onStart: function( options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    36
            // called on start
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    37
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    38
        })
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    39
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    40
        // onStart + onEnd only
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    41
        .code({
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    42
          start: 6,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    43
          end: 8,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    44
          onStart: function( options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    45
            // called on start
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    46
          },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    47
          onEnd: function ( options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    48
            // called on end
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    49
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    50
        })
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    51
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    52
        // onStart, onEnd, onFrame
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    53
        .code({
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    54
          start: 10,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    55
          end: 14,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    56
          onStart: function( options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    57
            // called on start
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    58
          },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    59
          onFrame: function ( options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    60
            // called on every paint frame between start and end.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    61
            // uses mozRequestAnimationFrame, webkitRequestAnimationFrame,
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    62
            // or setTimeout with 16ms window.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    63
          },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    64
          onEnd: function ( options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    65
            // called on end
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    66
          }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    67
        });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    68
  *
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    69
  */
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    70
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    71
  Popcorn.plugin( "code" , function( options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    72
    var running = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    73
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    74
    // Setup a proper frame interval function (60fps), favouring paint events.
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    75
    var step = (function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    76
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    77
      var buildFrameRunner = function( runner ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    78
        return function( f, options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    79
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    80
          var _f = function() {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    81
            running && f();
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    82
            running && runner( _f );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    83
          };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    84
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    85
          _f();
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    86
        };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    87
      };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    88
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    89
      // Figure out which level of browser support we have for this
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    90
      if ( window.webkitRequestAnimationFrame ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    91
        return buildFrameRunner( window.webkitRequestAnimationFrame );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    92
      } else if ( window.mozRequestAnimationFrame ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    93
        return buildFrameRunner( window.mozRequestAnimationFrame );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    94
      } else {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    95
        return buildFrameRunner( function( f ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    96
          window.setTimeout( f, 16 );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    97
        });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    98
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
    99
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   100
    })();
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   101
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   102
    if ( !options.onStart || typeof options.onStart !== "function" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   103
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   104
      if ( Popcorn.plugin.debug ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   105
        throw new Error( "Popcorn Code Plugin Error: onStart must be a function." );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   106
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   107
      options.onStart = Popcorn.nop;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   108
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   109
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   110
    if ( options.onEnd && typeof options.onEnd !== "function" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   111
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   112
      if ( Popcorn.plugin.debug ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   113
        throw new Error( "Popcorn Code Plugin Error: onEnd  must be a function." );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   114
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   115
      options.onEnd = undefined;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   116
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   117
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   118
    if ( options.onFrame && typeof options.onFrame !== "function" ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   119
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   120
      if ( Popcorn.plugin.debug ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   121
        throw new Error( "Popcorn Code Plugin Error: onFrame  must be a function." );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   122
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   123
      options.onFrame = undefined;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   124
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   125
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   126
    return {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   127
      start: function( event, options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   128
        options.onStart( options );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   129
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   130
        if ( options.onFrame ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   131
          running = true;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   132
          step( options.onFrame, options );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   133
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   134
      },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   135
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   136
      end: function( event, options ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   137
        if ( options.onFrame ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   138
          running = false;
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   139
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   140
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   141
        if ( options.onEnd ) {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   142
          options.onEnd( options );
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   143
        }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   144
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   145
    };
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   146
  },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   147
  {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   148
    about: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   149
      name: "Popcorn Code Plugin",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   150
      version: "0.1",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   151
      author: "David Humphrey (@humphd)",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   152
      website: "http://vocamus.net/dave"
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   153
    },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   154
    options: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   155
      start: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   156
       elem: "input",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   157
       type: "text",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   158
       label: "In"
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   159
      },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   160
      end: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   161
        elem: "input",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   162
        type: "text",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   163
        label: "Out"
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   164
      },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   165
      onStart: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   166
        elem: "input",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   167
        type: "function",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   168
        label: "onStart"
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   169
      },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   170
      onFrame: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   171
        elem: "input",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   172
        type: "function",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   173
        label: "onFrame"
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   174
      },
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   175
      onEnd: {
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   176
        elem: "input",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   177
        type: "function",
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   178
        label: "onEnd"
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   179
      }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   180
    }
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   181
  });
048125f1a167 moved external libraries to their own subdir.
hamidouk
parents:
diff changeset
   182
})( Popcorn );