front_idill/extern/fajran-tuiojs/examples/processingjs/processing.js
author bastiena
Thu, 19 Apr 2012 11:53:06 +0200
changeset 29 fcf435874395
parent 26 858e90c7cbaa
permissions -rw-r--r--
Middleware : Connection between middleware and front idill established with websockets.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
     1
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
     2
(function(window, document, Math, undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
     3
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
     4
  var nop = function(){};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
     5
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
     6
  var debug = (function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
     7
    if ("console" in window) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
     8
      return function(msg) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
     9
        window.console.log('Processing.js: ' + msg);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    10
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    11
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    12
    return nop();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    13
  }());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    14
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    15
  var ajax = function(url) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    16
    var xhr = new XMLHttpRequest();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    17
    xhr.open("GET", url, false);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    18
    if (xhr.overrideMimeType) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    19
      xhr.overrideMimeType("text/plain");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    20
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    21
    xhr.setRequestHeader("If-Modified-Since", "Fri, 01 Jan 1960 00:00:00 GMT");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    22
    xhr.send(null);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    23
    // failed request?
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    24
    if (xhr.status !== 200 && xhr.status !== 0) { throw ("XMLHttpRequest failed, status code " + xhr.status); }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    25
    return xhr.responseText;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    26
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    27
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    28
  var isDOMPresent = ("document" in this) && !("fake" in this.document);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    29
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    30
  // document.head polyfill for the benefit of Firefox 3.6
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    31
  document.head = document.head || document.getElementsByTagName('head')[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    32
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    33
  // Typed Arrays: fallback to WebGL arrays or Native JS arrays if unavailable
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    34
  function setupTypedArray(name, fallback) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    35
    // Check if TypedArray exists, and use if so.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    36
    if (name in window) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    37
      return window[name];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    38
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    39
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    40
    // Check if WebGLArray exists
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    41
    if (typeof window[fallback] === "function") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    42
      return window[fallback];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    43
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    44
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    45
    // Use Native JS array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    46
    return function(obj) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    47
      if (obj instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    48
        return obj;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    49
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    50
      if (typeof obj === "number") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    51
        var arr = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    52
        arr.length = obj;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    53
        return arr;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    54
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    55
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    56
  }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    57
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    58
  var Float32Array = setupTypedArray("Float32Array", "WebGLFloatArray"),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    59
      Int32Array   = setupTypedArray("Int32Array",   "WebGLIntArray"),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    60
      Uint16Array  = setupTypedArray("Uint16Array",  "WebGLUnsignedShortArray"),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    61
      Uint8Array   = setupTypedArray("Uint8Array",   "WebGLUnsignedByteArray");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    62
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    63
  /* Browsers fixes end */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    64
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    65
  /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    66
   * NOTE: in releases we replace symbolic PConstants.* names with their values.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    67
   * Using PConstants.* in code below is fine.  See tools/rewrite-pconstants.js.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    68
   */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    69
  var PConstants = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    70
    X: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    71
    Y: 1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    72
    Z: 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    73
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    74
    R: 3,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    75
    G: 4,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    76
    B: 5,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    77
    A: 6,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    78
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    79
    U: 7,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    80
    V: 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    81
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    82
    NX: 9,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    83
    NY: 10,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    84
    NZ: 11,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    85
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    86
    EDGE: 12,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    87
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    88
    // Stroke
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    89
    SR: 13,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    90
    SG: 14,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    91
    SB: 15,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    92
    SA: 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    93
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    94
    SW: 17,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    95
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    96
    // Transformations (2D and 3D)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    97
    TX: 18,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    98
    TY: 19,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
    99
    TZ: 20,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   100
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   101
    VX: 21,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   102
    VY: 22,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   103
    VZ: 23,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   104
    VW: 24,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   105
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   106
    // Material properties
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   107
    AR: 25,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   108
    AG: 26,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   109
    AB: 27,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   110
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   111
    DR: 3,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   112
    DG: 4,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   113
    DB: 5,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   114
    DA: 6,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   115
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   116
    SPR: 28,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   117
    SPG: 29,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   118
    SPB: 30,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   119
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   120
    SHINE: 31,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   121
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   122
    ER: 32,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   123
    EG: 33,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   124
    EB: 34,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   125
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   126
    BEEN_LIT: 35,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   127
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   128
    VERTEX_FIELD_COUNT: 36,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   129
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   130
    // Renderers
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   131
    P2D:    1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   132
    JAVA2D: 1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   133
    WEBGL:  2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   134
    P3D:    2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   135
    OPENGL: 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   136
    PDF:    0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   137
    DXF:    0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   138
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   139
    // Platform IDs
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   140
    OTHER:   0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   141
    WINDOWS: 1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   142
    MAXOSX:  2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   143
    LINUX:   3,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   144
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   145
    EPSILON: 0.0001,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   146
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   147
    MAX_FLOAT:  3.4028235e+38,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   148
    MIN_FLOAT: -3.4028235e+38,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   149
    MAX_INT:    2147483647,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   150
    MIN_INT:   -2147483648,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   151
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   152
    PI:         Math.PI,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   153
    TWO_PI:     2 * Math.PI,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   154
    HALF_PI:    Math.PI / 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   155
    THIRD_PI:   Math.PI / 3,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   156
    QUARTER_PI: Math.PI / 4,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   157
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   158
    DEG_TO_RAD: Math.PI / 180,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   159
    RAD_TO_DEG: 180 / Math.PI,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   160
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   161
    WHITESPACE: " \t\n\r\f\u00A0",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   162
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   163
    // Color modes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   164
    RGB:   1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   165
    ARGB:  2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   166
    HSB:   3,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   167
    ALPHA: 4,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   168
    CMYK:  5,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   169
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   170
    // Image file types
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   171
    TIFF:  0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   172
    TARGA: 1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   173
    JPEG:  2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   174
    GIF:   3,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   175
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   176
    // Filter/convert types
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   177
    BLUR:      11,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   178
    GRAY:      12,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   179
    INVERT:    13,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   180
    OPAQUE:    14,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   181
    POSTERIZE: 15,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   182
    THRESHOLD: 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   183
    ERODE:     17,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   184
    DILATE:    18,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   185
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   186
    // Blend modes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   187
    REPLACE:    0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   188
    BLEND:      1 << 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   189
    ADD:        1 << 1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   190
    SUBTRACT:   1 << 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   191
    LIGHTEST:   1 << 3,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   192
    DARKEST:    1 << 4,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   193
    DIFFERENCE: 1 << 5,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   194
    EXCLUSION:  1 << 6,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   195
    MULTIPLY:   1 << 7,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   196
    SCREEN:     1 << 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   197
    OVERLAY:    1 << 9,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   198
    HARD_LIGHT: 1 << 10,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   199
    SOFT_LIGHT: 1 << 11,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   200
    DODGE:      1 << 12,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   201
    BURN:       1 << 13,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   202
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   203
    // Color component bit masks
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   204
    ALPHA_MASK: 0xff000000,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   205
    RED_MASK:   0x00ff0000,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   206
    GREEN_MASK: 0x0000ff00,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   207
    BLUE_MASK:  0x000000ff,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   208
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   209
    // Projection matrices
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   210
    CUSTOM:       0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   211
    ORTHOGRAPHIC: 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   212
    PERSPECTIVE:  3,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   213
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   214
    // Shapes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   215
    POINT:          2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   216
    POINTS:         2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   217
    LINE:           4,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   218
    LINES:          4,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   219
    TRIANGLE:       8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   220
    TRIANGLES:      9,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   221
    TRIANGLE_STRIP: 10,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   222
    TRIANGLE_FAN:   11,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   223
    QUAD:           16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   224
    QUADS:          16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   225
    QUAD_STRIP:     17,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   226
    POLYGON:        20,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   227
    PATH:           21,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   228
    RECT:           30,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   229
    ELLIPSE:        31,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   230
    ARC:            32,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   231
    SPHERE:         40,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   232
    BOX:            41,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   233
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   234
    GROUP:          0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   235
    PRIMITIVE:      1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   236
    //PATH:         21, // shared with Shape PATH
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   237
    GEOMETRY:       3,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   238
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   239
    // Shape Vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   240
    VERTEX:        0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   241
    BEZIER_VERTEX: 1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   242
    CURVE_VERTEX:  2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   243
    BREAK:         3,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   244
    CLOSESHAPE:    4,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   245
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   246
    // Shape closing modes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   247
    OPEN:  1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   248
    CLOSE: 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   249
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   250
    // Shape drawing modes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   251
    CORNER:          0, // Draw mode convention to use (x, y) to (width, height)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   252
    CORNERS:         1, // Draw mode convention to use (x1, y1) to (x2, y2) coordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   253
    RADIUS:          2, // Draw mode from the center, and using the radius
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   254
    CENTER_RADIUS:   2, // Deprecated! Use RADIUS instead
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   255
    CENTER:          3, // Draw from the center, using second pair of values as the diameter
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   256
    DIAMETER:        3, // Synonym for the CENTER constant. Draw from the center
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   257
    CENTER_DIAMETER: 3, // Deprecated! Use DIAMETER instead
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   258
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   259
    // Text vertical alignment modes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   260
    BASELINE: 0,   // Default vertical alignment for text placement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   261
    TOP:      101, // Align text to the top
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   262
    BOTTOM:   102, // Align text from the bottom, using the baseline
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   263
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   264
    // UV Texture coordinate modes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   265
    NORMAL:     1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   266
    NORMALIZED: 1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   267
    IMAGE:      2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   268
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   269
    // Text placement modes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   270
    MODEL: 4,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   271
    SHAPE: 5,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   272
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   273
    // Stroke modes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   274
    SQUARE:  'butt',
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   275
    ROUND:   'round',
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   276
    PROJECT: 'square',
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   277
    MITER:   'miter',
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   278
    BEVEL:   'bevel',
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   279
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   280
    // Lighting modes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   281
    AMBIENT:     0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   282
    DIRECTIONAL: 1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   283
    //POINT:     2, Shared with Shape constant
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   284
    SPOT:        3,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   285
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   286
    // Key constants
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   287
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   288
    // Both key and keyCode will be equal to these values
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   289
    BACKSPACE: 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   290
    TAB:       9,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   291
    ENTER:     10,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   292
    RETURN:    13,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   293
    ESC:       27,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   294
    DELETE:    127,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   295
    CODED:     0xffff,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   296
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   297
    // p.key will be CODED and p.keyCode will be this value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   298
    SHIFT:     16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   299
    CONTROL:   17,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   300
    ALT:       18,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   301
    CAPSLK:    20,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   302
    PGUP:      33,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   303
    PGDN:      34,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   304
    END:       35,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   305
    HOME:      36,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   306
    LEFT:      37,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   307
    UP:        38,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   308
    RIGHT:     39,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   309
    DOWN:      40,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   310
    F1:        112,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   311
    F2:        113,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   312
    F3:        114,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   313
    F4:        115,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   314
    F5:        116,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   315
    F6:        117,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   316
    F7:        118,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   317
    F8:        119,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   318
    F9:        120,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   319
    F10:       121,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   320
    F11:       122,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   321
    F12:       123,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   322
    NUMLK:     144,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   323
    META:      157,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   324
    INSERT:    155,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   325
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   326
    // Cursor types
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   327
    ARROW:    'default',
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   328
    CROSS:    'crosshair',
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   329
    HAND:     'pointer',
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   330
    MOVE:     'move',
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   331
    TEXT:     'text',
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   332
    WAIT:     'wait',
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   333
    NOCURSOR: "url('data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='), auto",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   334
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   335
    // Hints
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   336
    DISABLE_OPENGL_2X_SMOOTH:     1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   337
    ENABLE_OPENGL_2X_SMOOTH:     -1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   338
    ENABLE_OPENGL_4X_SMOOTH:      2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   339
    ENABLE_NATIVE_FONTS:          3,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   340
    DISABLE_DEPTH_TEST:           4,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   341
    ENABLE_DEPTH_TEST:           -4,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   342
    ENABLE_DEPTH_SORT:            5,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   343
    DISABLE_DEPTH_SORT:          -5,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   344
    DISABLE_OPENGL_ERROR_REPORT:  6,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   345
    ENABLE_OPENGL_ERROR_REPORT:  -6,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   346
    ENABLE_ACCURATE_TEXTURES:     7,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   347
    DISABLE_ACCURATE_TEXTURES:   -7,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   348
    HINT_COUNT:                  10,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   349
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   350
    // PJS defined constants
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   351
    SINCOS_LENGTH:      720, // every half degree
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   352
    PRECISIONB:         15, // fixed point precision is limited to 15 bits!!
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   353
    PRECISIONF:         1 << 15,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   354
    PREC_MAXVAL:        (1 << 15) - 1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   355
    PREC_ALPHA_SHIFT:   24 - 15,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   356
    PREC_RED_SHIFT:     16 - 15,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   357
    NORMAL_MODE_AUTO:   0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   358
    NORMAL_MODE_SHAPE:  1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   359
    NORMAL_MODE_VERTEX: 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   360
    MAX_LIGHTS:         8
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   361
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   362
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   363
  /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   364
   * Returns Java hashCode() result for the object. If the object has the "hashCode" function,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   365
   * it preforms the call of this function. Otherwise it uses/creates the "$id" property,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   366
   * which is used as the hashCode.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   367
   *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   368
   * @param {Object} obj          The object.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   369
   * @returns {int}               The object's hash code.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   370
   */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   371
  function virtHashCode(obj) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   372
    if (typeof(obj) === "string") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   373
      var hash = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   374
      for (var i = 0; i < obj.length; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   375
        hash = (hash * 31 + obj.charCodeAt(i)) & 0xFFFFFFFF;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   376
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   377
      return hash;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   378
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   379
    if (typeof(obj) !== "object") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   380
      return obj & 0xFFFFFFFF;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   381
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   382
    if (obj.hashCode instanceof Function) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   383
      return obj.hashCode();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   384
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   385
    if (obj.$id === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   386
        obj.$id = ((Math.floor(Math.random() * 0x10000) - 0x8000) << 16) | Math.floor(Math.random() * 0x10000);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   387
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   388
    return obj.$id;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   389
  }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   390
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   391
  /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   392
   * Returns Java equals() result for two objects. If the first object
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   393
   * has the "equals" function, it preforms the call of this function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   394
   * Otherwise the method uses the JavaScript === operator.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   395
   *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   396
   * @param {Object} obj          The first object.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   397
   * @param {Object} other        The second object.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   398
   *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   399
   * @returns {boolean}           true if the objects are equal.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   400
   */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   401
  function virtEquals(obj, other) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   402
    if (obj === null || other === null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   403
      return (obj === null) && (other === null);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   404
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   405
    if (typeof (obj) === "string") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   406
      return obj === other;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   407
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   408
    if (typeof(obj) !== "object") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   409
      return obj === other;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   410
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   411
    if (obj.equals instanceof Function) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   412
      return obj.equals(other);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   413
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   414
    return obj === other;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   415
  }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   416
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   417
  /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   418
  * A ObjectIterator is an iterator wrapper for objects. If passed object contains
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   419
  * the iterator method, the object instance will be replaced by the result returned by
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   420
  * this method call. If passed object is an array, the ObjectIterator instance iterates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   421
  * through its items.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   422
  *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   423
  * @param {Object} obj          The object to be iterated.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   424
  */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   425
  var ObjectIterator = function(obj) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   426
    if (obj.iterator instanceof Function) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   427
      return obj.iterator();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   428
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   429
    if (obj instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   430
      // iterate through array items
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   431
      var index = -1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   432
      this.hasNext = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   433
        return ++index < obj.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   434
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   435
      this.next = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   436
        return obj[index];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   437
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   438
    } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   439
      throw "Unable to iterate: " + obj;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   440
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   441
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   442
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   443
  /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   444
   * An ArrayList stores a variable number of objects.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   445
   *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   446
   * @param {int} initialCapacity optional defines the initial capacity of the list, it's empty by default
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   447
   *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   448
   * @returns {ArrayList} new ArrayList object
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   449
   */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   450
  var ArrayList = (function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   451
    function Iterator(array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   452
      var index = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   453
      this.hasNext = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   454
        return index < array.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   455
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   456
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   457
      this.next = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   458
        return array[index++];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   459
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   460
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   461
      this.remove = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   462
        array.splice(index, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   463
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   464
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   465
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   466
    function ArrayList() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   467
      var array;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   468
      if (arguments.length === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   469
        array = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   470
      } else if (arguments.length > 0 && typeof arguments[0] !== 'number') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   471
        array = arguments[0].toArray();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   472
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   473
        array = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   474
        array.length = 0 | arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   475
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   476
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   477
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   478
       * @member ArrayList
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   479
       * ArrayList.get() Returns the element at the specified position in this list.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   480
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   481
       * @param {int} i index of element to return
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   482
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   483
       * @returns {Object} the element at the specified position in this list.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   484
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   485
      this.get = function(i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   486
        return array[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   487
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   488
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   489
       * @member ArrayList
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   490
       * ArrayList.contains() Returns true if this list contains the specified element.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   491
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   492
       * @param {Object} item element whose presence in this List is to be tested.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   493
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   494
       * @returns {boolean} true if the specified element is present; false otherwise.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   495
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   496
      this.contains = function(item) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   497
        return this.indexOf(item)>-1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   498
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   499
       /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   500
       * @member ArrayList
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   501
       * ArrayList.indexOf() Returns the position this element takes in the list, or -1 if the element is not found.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   502
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   503
       * @param {Object} item element whose position in this List is to be tested.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   504
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   505
       * @returns {int} the list position that the first match for this element holds in the list, or -1 if it is not in the list.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   506
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   507
      this.indexOf = function(item) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   508
        for (var i = 0, len = array.length; i < len; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   509
          if (virtEquals(item, array[i])) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   510
            return i;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   511
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   512
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   513
        return -1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   514
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   515
     /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   516
       * @member ArrayList
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   517
       * ArrayList.add() Adds the specified element to this list.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   518
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   519
       * @param {int}    index  optional index at which the specified element is to be inserted
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   520
       * @param {Object} object element to be added to the list
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   521
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   522
      this.add = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   523
        if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   524
          array.push(arguments[0]); // for add(Object)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   525
        } else if (arguments.length === 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   526
          var arg0 = arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   527
          if (typeof arg0 === 'number') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   528
            if (arg0 >= 0 && arg0 <= array.length) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   529
              array.splice(arg0, 0, arguments[1]); // for add(i, Object)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   530
            } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   531
              throw(arg0 + " is not a valid index");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   532
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   533
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   534
            throw(typeof arg0 + " is not a number");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   535
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   536
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   537
          throw("Please use the proper number of parameters.");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   538
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   539
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   540
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   541
       * @member ArrayList
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   542
       * ArrayList.addAll(collection) appends all of the elements in the specified
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   543
       * Collection to the end of this list, in the order that they are returned by
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   544
       * the specified Collection's Iterator.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   545
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   546
       * When called as addAll(index, collection) the elements are inserted into
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   547
       * this list at the position indicated by index.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   548
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   549
       * @param {index} Optional; specifies the position the colletion should be inserted at
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   550
       * @param {collection} Any iterable object (ArrayList, HashMap.keySet(), etc.)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   551
       * @throws out of bounds error for negative index, or index greater than list size.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   552
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   553
      this.addAll = function(arg1, arg2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   554
        // addAll(int, Collection)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   555
        var it;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   556
        if (typeof arg1 === "number") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   557
          if (arg1 < 0 || arg1 > array.length) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   558
            throw("Index out of bounds for addAll: " + arg1 + " greater or equal than " + array.length);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   559
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   560
          it = new ObjectIterator(arg2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   561
          while (it.hasNext()) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   562
            array.splice(arg1++, 0, it.next());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   563
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   564
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   565
        // addAll(Collection)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   566
        else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   567
          it = new ObjectIterator(arg1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   568
          while (it.hasNext()) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   569
            array.push(it.next());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   570
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   571
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   572
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   573
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   574
       * @member ArrayList
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   575
       * ArrayList.set() Replaces the element at the specified position in this list with the specified element.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   576
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   577
       * @param {int}    index  index of element to replace
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   578
       * @param {Object} object element to be stored at the specified position
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   579
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   580
      this.set = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   581
        if (arguments.length === 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   582
          var arg0 = arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   583
          if (typeof arg0 === 'number') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   584
            if (arg0 >= 0 && arg0 < array.length) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   585
              array.splice(arg0, 1, arguments[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   586
            } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   587
              throw(arg0 + " is not a valid index.");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   588
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   589
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   590
            throw(typeof arg0 + " is not a number");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   591
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   592
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   593
          throw("Please use the proper number of parameters.");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   594
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   595
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   596
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   597
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   598
       * @member ArrayList
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   599
       * ArrayList.size() Returns the number of elements in this list.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   600
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   601
       * @returns {int} the number of elements in this list
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   602
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   603
      this.size = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   604
        return array.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   605
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   606
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   607
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   608
       * @member ArrayList
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   609
       * ArrayList.clear() Removes all of the elements from this list. The list will be empty after this call returns.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   610
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   611
      this.clear = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   612
        array.length = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   613
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   614
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   615
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   616
       * @member ArrayList
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   617
       * ArrayList.remove() Removes an element either based on index, if the argument is a number, or
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   618
       * by equality check, if the argument is an object.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   619
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   620
       * @param {int|Object} item either the index of the element to be removed, or the element itself.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   621
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   622
       * @returns {Object|boolean} If removal is by index, the element that was removed, or null if nothing was removed. If removal is by object, true if removal occurred, otherwise false.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   623
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   624
      this.remove = function(item) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   625
        if (typeof item === 'number') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   626
          return array.splice(item, 1)[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   627
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   628
        item = this.indexOf(item);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   629
        if (item > -1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   630
          array.splice(item, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   631
          return true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   632
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   633
        return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   634
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   635
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   636
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   637
       * @member ArrayList
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   638
       * ArrayList.isEmpty() Tests if this list has no elements.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   639
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   640
       * @returns {boolean} true if this list has no elements; false otherwise
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   641
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   642
      this.isEmpty = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   643
         return !array.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   644
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   645
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   646
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   647
       * @member ArrayList
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   648
       * ArrayList.clone() Returns a shallow copy of this ArrayList instance. (The elements themselves are not copied.)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   649
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   650
       * @returns {ArrayList} a clone of this ArrayList instance
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   651
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   652
      this.clone = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   653
        return new ArrayList(this);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   654
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   655
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   656
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   657
       * @member ArrayList
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   658
       * ArrayList.toArray() Returns an array containing all of the elements in this list in the correct order.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   659
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   660
       * @returns {Object[]} Returns an array containing all of the elements in this list in the correct order
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   661
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   662
      this.toArray = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   663
        return array.slice(0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   664
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   665
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   666
      this.iterator = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   667
        return new Iterator(array);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   668
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   669
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   670
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   671
    return ArrayList;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   672
  }());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   673
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   674
  /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   675
  * A HashMap stores a collection of objects, each referenced by a key. This is similar to an Array, only
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   676
  * instead of accessing elements with a numeric index, a String  is used. (If you are familiar with
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   677
  * associative arrays from other languages, this is the same idea.)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   678
  *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   679
  * @param {int} initialCapacity          defines the initial capacity of the map, it's 16 by default
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   680
  * @param {float} loadFactor             the load factor for the map, the default is 0.75
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   681
  * @param {Map} m                        gives the new HashMap the same mappings as this Map
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   682
  */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   683
  var HashMap = (function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   684
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   685
    * @member HashMap
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   686
    * A HashMap stores a collection of objects, each referenced by a key. This is similar to an Array, only
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   687
    * instead of accessing elements with a numeric index, a String  is used. (If you are familiar with
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   688
    * associative arrays from other languages, this is the same idea.)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   689
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   690
    * @param {int} initialCapacity          defines the initial capacity of the map, it's 16 by default
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   691
    * @param {float} loadFactor             the load factor for the map, the default is 0.75
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   692
    * @param {Map} m                        gives the new HashMap the same mappings as this Map
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   693
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   694
    function HashMap() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   695
      if (arguments.length === 1 && arguments[0] instanceof HashMap) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   696
        return arguments[0].clone();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   697
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   698
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   699
      var initialCapacity = arguments.length > 0 ? arguments[0] : 16;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   700
      var loadFactor = arguments.length > 1 ? arguments[1] : 0.75;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   701
      var buckets = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   702
      buckets.length = initialCapacity;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   703
      var count = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   704
      var hashMap = this;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   705
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   706
      function getBucketIndex(key) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   707
        var index = virtHashCode(key) % buckets.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   708
        return index < 0 ? buckets.length + index : index;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   709
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   710
      function ensureLoad() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   711
        if (count <= loadFactor * buckets.length) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   712
          return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   713
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   714
        var allEntries = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   715
        for (var i = 0; i < buckets.length; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   716
          if (buckets[i] !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   717
            allEntries = allEntries.concat(buckets[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   718
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   719
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   720
        var newBucketsLength = buckets.length * 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   721
        buckets = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   722
        buckets.length = newBucketsLength;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   723
        for (var j = 0; j < allEntries.length; ++j) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   724
          var index = getBucketIndex(allEntries[j].key);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   725
          var bucket = buckets[index];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   726
          if (bucket === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   727
            buckets[index] = bucket = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   728
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   729
          bucket.push(allEntries[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   730
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   731
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   732
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   733
      function Iterator(conversion, removeItem) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   734
        var bucketIndex = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   735
        var itemIndex = -1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   736
        var endOfBuckets = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   737
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   738
        function findNext() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   739
          while (!endOfBuckets) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   740
            ++itemIndex;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   741
            if (bucketIndex >= buckets.length) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   742
              endOfBuckets = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   743
            } else if (buckets[bucketIndex] === undef || itemIndex >= buckets[bucketIndex].length) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   744
              itemIndex = -1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   745
              ++bucketIndex;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   746
            } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   747
              return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   748
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   749
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   750
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   751
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   752
        /*
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   753
        * @member Iterator
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   754
        * Checks if the Iterator has more items
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   755
        */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   756
        this.hasNext = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   757
          return !endOfBuckets;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   758
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   759
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   760
        /*
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   761
        * @member Iterator
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   762
        * Return the next Item
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   763
        */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   764
        this.next = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   765
          var result = conversion(buckets[bucketIndex][itemIndex]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   766
          findNext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   767
          return result;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   768
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   769
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   770
        /*
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   771
        * @member Iterator
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   772
        * Remove the current item
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   773
        */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   774
        this.remove = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   775
          removeItem(this.next());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   776
          --itemIndex;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   777
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   778
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   779
        findNext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   780
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   781
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   782
      function Set(conversion, isIn, removeItem) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   783
        this.clear = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   784
          hashMap.clear();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   785
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   786
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   787
        this.contains = function(o) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   788
          return isIn(o);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   789
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   790
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   791
        this.containsAll = function(o) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   792
          var it = o.iterator();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   793
          while (it.hasNext()) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   794
            if (!this.contains(it.next())) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   795
              return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   796
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   797
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   798
          return true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   799
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   800
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   801
        this.isEmpty = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   802
          return hashMap.isEmpty();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   803
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   804
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   805
        this.iterator = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   806
          return new Iterator(conversion, removeItem);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   807
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   808
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   809
        this.remove = function(o) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   810
          if (this.contains(o)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   811
            removeItem(o);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   812
            return true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   813
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   814
          return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   815
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   816
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   817
        this.removeAll = function(c) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   818
          var it = c.iterator();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   819
          var changed = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   820
          while (it.hasNext()) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   821
            var item = it.next();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   822
            if (this.contains(item)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   823
              removeItem(item);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   824
              changed = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   825
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   826
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   827
          return true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   828
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   829
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   830
        this.retainAll = function(c) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   831
          var it = this.iterator();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   832
          var toRemove = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   833
          while (it.hasNext()) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   834
            var entry = it.next();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   835
            if (!c.contains(entry)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   836
              toRemove.push(entry);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   837
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   838
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   839
          for (var i = 0; i < toRemove.length; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   840
            removeItem(toRemove[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   841
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   842
          return toRemove.length > 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   843
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   844
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   845
        this.size = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   846
          return hashMap.size();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   847
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   848
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   849
        this.toArray = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   850
          var result = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   851
          var it = this.iterator();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   852
          while (it.hasNext()) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   853
            result.push(it.next());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   854
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   855
          return result;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   856
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   857
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   858
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   859
      function Entry(pair) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   860
        this._isIn = function(map) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   861
          return map === hashMap && (pair.removed === undef);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   862
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   863
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   864
        this.equals = function(o) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   865
          return virtEquals(pair.key, o.getKey());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   866
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   867
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   868
        this.getKey = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   869
          return pair.key;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   870
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   871
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   872
        this.getValue = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   873
          return pair.value;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   874
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   875
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   876
        this.hashCode = function(o) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   877
          return virtHashCode(pair.key);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   878
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   879
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   880
        this.setValue = function(value) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   881
          var old = pair.value;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   882
          pair.value = value;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   883
          return old;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   884
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   885
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   886
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   887
      this.clear = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   888
        count = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   889
        buckets = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   890
        buckets.length = initialCapacity;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   891
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   892
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   893
      this.clone = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   894
        var map = new HashMap();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   895
        map.putAll(this);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   896
        return map;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   897
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   898
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   899
      this.containsKey = function(key) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   900
        var index = getBucketIndex(key);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   901
        var bucket = buckets[index];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   902
        if (bucket === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   903
          return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   904
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   905
        for (var i = 0; i < bucket.length; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   906
          if (virtEquals(bucket[i].key, key)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   907
            return true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   908
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   909
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   910
        return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   911
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   912
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   913
      this.containsValue = function(value) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   914
        for (var i = 0; i < buckets.length; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   915
          var bucket = buckets[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   916
          if (bucket === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   917
            continue;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   918
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   919
          for (var j = 0; j < bucket.length; ++j) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   920
            if (virtEquals(bucket[j].value, value)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   921
              return true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   922
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   923
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   924
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   925
        return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   926
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   927
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   928
      this.entrySet = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   929
        return new Set(
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   930
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   931
        function(pair) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   932
          return new Entry(pair);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   933
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   934
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   935
        function(pair) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   936
          return (pair instanceof Entry) && pair._isIn(hashMap);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   937
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   938
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   939
        function(pair) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   940
          return hashMap.remove(pair.getKey());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   941
        });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   942
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   943
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   944
      this.get = function(key) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   945
        var index = getBucketIndex(key);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   946
        var bucket = buckets[index];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   947
        if (bucket === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   948
          return null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   949
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   950
        for (var i = 0; i < bucket.length; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   951
          if (virtEquals(bucket[i].key, key)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   952
            return bucket[i].value;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   953
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   954
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   955
        return null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   956
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   957
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   958
      this.isEmpty = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   959
        return count === 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   960
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   961
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   962
      this.keySet = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   963
        return new Set(
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   964
          // get key from pair
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   965
          function(pair) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   966
            return pair.key;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   967
          },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   968
          // is-in test
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   969
          function(key) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   970
            return hashMap.containsKey(key);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   971
          },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   972
          // remove from hashmap by key
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   973
          function(key) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   974
            return hashMap.remove(key);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   975
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   976
        );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   977
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   978
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   979
      this.values = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   980
        return new Set(
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   981
          // get value from pair
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   982
          function(pair) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   983
            return pair.value;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   984
          },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   985
          // is-in test
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   986
          function(value) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   987
            return hashMap.containsValue(value);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   988
          },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   989
          // remove from hashmap by value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   990
          function(value) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   991
            return hashMap.removeByValue(value);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   992
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   993
        );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   994
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   995
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   996
      this.put = function(key, value) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   997
        var index = getBucketIndex(key);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   998
        var bucket = buckets[index];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
   999
        if (bucket === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1000
          ++count;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1001
          buckets[index] = [{
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1002
            key: key,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1003
            value: value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1004
          }];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1005
          ensureLoad();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1006
          return null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1007
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1008
        for (var i = 0; i < bucket.length; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1009
          if (virtEquals(bucket[i].key, key)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1010
            var previous = bucket[i].value;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1011
            bucket[i].value = value;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1012
            return previous;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1013
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1014
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1015
        ++count;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1016
        bucket.push({
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1017
          key: key,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1018
          value: value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1019
        });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1020
        ensureLoad();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1021
        return null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1022
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1023
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1024
      this.putAll = function(m) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1025
        var it = m.entrySet().iterator();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1026
        while (it.hasNext()) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1027
          var entry = it.next();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1028
          this.put(entry.getKey(), entry.getValue());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1029
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1030
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1031
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1032
      this.remove = function(key) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1033
        var index = getBucketIndex(key);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1034
        var bucket = buckets[index];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1035
        if (bucket === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1036
          return null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1037
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1038
        for (var i = 0; i < bucket.length; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1039
          if (virtEquals(bucket[i].key, key)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1040
            --count;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1041
            var previous = bucket[i].value;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1042
            bucket[i].removed = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1043
            if (bucket.length > 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1044
              bucket.splice(i, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1045
            } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1046
              buckets[index] = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1047
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1048
            return previous;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1049
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1050
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1051
        return null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1052
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1053
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1054
      this.removeByValue = function(value) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1055
        var bucket, i, ilen, pair;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1056
        for (bucket in buckets) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1057
          if (buckets.hasOwnProperty(bucket)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1058
            for (i = 0, ilen = buckets[bucket].length; i < ilen; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1059
              pair = buckets[bucket][i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1060
              // removal on values is based on identity, not equality
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1061
              if (pair.value === value) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1062
                buckets[bucket].splice(i, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1063
                return true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1064
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1065
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1066
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1067
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1068
        return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1069
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1070
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1071
      this.size = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1072
        return count;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1073
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1074
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1075
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1076
    return HashMap;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1077
  }());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1078
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1079
  var PVector = (function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1080
    function PVector(x, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1081
      this.x = x || 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1082
      this.y = y || 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1083
      this.z = z || 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1084
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1085
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1086
    PVector.dist = function(v1, v2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1087
      return v1.dist(v2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1088
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1089
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1090
    PVector.dot = function(v1, v2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1091
      return v1.dot(v2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1092
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1093
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1094
    PVector.cross = function(v1, v2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1095
      return v1.cross(v2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1096
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1097
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1098
    PVector.angleBetween = function(v1, v2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1099
      return Math.acos(v1.dot(v2) / (v1.mag() * v2.mag()));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1100
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1101
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1102
    // Common vector operations for PVector
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1103
    PVector.prototype = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1104
      set: function(v, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1105
        if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1106
          this.set(v.x || v[0] || 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1107
                   v.y || v[1] || 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1108
                   v.z || v[2] || 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1109
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1110
          this.x = v;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1111
          this.y = y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1112
          this.z = z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1113
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1114
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1115
      get: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1116
        return new PVector(this.x, this.y, this.z);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1117
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1118
      mag: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1119
        var x = this.x,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1120
            y = this.y,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1121
            z = this.z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1122
        return Math.sqrt(x * x + y * y + z * z);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1123
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1124
      add: function(v, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1125
        if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1126
          this.x += v.x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1127
          this.y += v.y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1128
          this.z += v.z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1129
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1130
          this.x += v;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1131
          this.y += y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1132
          this.z += z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1133
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1134
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1135
      sub: function(v, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1136
        if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1137
          this.x -= v.x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1138
          this.y -= v.y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1139
          this.z -= v.z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1140
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1141
          this.x -= v;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1142
          this.y -= y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1143
          this.z -= z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1144
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1145
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1146
      mult: function(v) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1147
        if (typeof v === 'number') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1148
          this.x *= v;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1149
          this.y *= v;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1150
          this.z *= v;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1151
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1152
          this.x *= v.x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1153
          this.y *= v.y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1154
          this.z *= v.z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1155
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1156
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1157
      div: function(v) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1158
        if (typeof v === 'number') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1159
          this.x /= v;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1160
          this.y /= v;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1161
          this.z /= v;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1162
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1163
          this.x /= v.x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1164
          this.y /= v.y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1165
          this.z /= v.z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1166
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1167
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1168
      dist: function(v) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1169
        var dx = this.x - v.x,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1170
            dy = this.y - v.y,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1171
            dz = this.z - v.z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1172
        return Math.sqrt(dx * dx + dy * dy + dz * dz);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1173
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1174
      dot: function(v, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1175
        if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1176
          return (this.x * v.x + this.y * v.y + this.z * v.z);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1177
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1178
        return (this.x * v + this.y * y + this.z * z);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1179
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1180
      cross: function(v) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1181
        var x = this.x,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1182
            y = this.y,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1183
            z = this.z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1184
        return new PVector(y * v.z - v.y * z,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1185
                           z * v.x - v.z * x,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1186
                           x * v.y - v.x * y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1187
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1188
      normalize: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1189
        var m = this.mag();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1190
        if (m > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1191
          this.div(m);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1192
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1193
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1194
      limit: function(high) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1195
        if (this.mag() > high) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1196
          this.normalize();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1197
          this.mult(high);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1198
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1199
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1200
      heading2D: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1201
        return (-Math.atan2(-this.y, this.x));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1202
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1203
      toString: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1204
        return "[" + this.x + ", " + this.y + ", " + this.z + "]";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1205
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1206
      array: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1207
        return [this.x, this.y, this.z];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1208
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1209
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1210
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1211
    function createPVectorMethod(method) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1212
      return function(v1, v2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1213
        var v = v1.get();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1214
        v[method](v2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1215
        return v;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1216
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1217
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1218
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1219
    for (var method in PVector.prototype) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1220
      if (PVector.prototype.hasOwnProperty(method) && !PVector.hasOwnProperty(method)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1221
        PVector[method] = createPVectorMethod(method);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1222
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1223
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1224
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1225
    return PVector;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1226
  }());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1227
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1228
  // Building defaultScope. Changing of the prototype protects
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1229
  // internal Processing code from the changes in defaultScope
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1230
  function DefaultScope() {}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1231
  DefaultScope.prototype = PConstants;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1232
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1233
  var defaultScope = new DefaultScope();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1234
  defaultScope.ArrayList   = ArrayList;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1235
  defaultScope.HashMap     = HashMap;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1236
  defaultScope.PVector     = PVector;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1237
  defaultScope.ObjectIterator = ObjectIterator;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1238
  defaultScope.PConstants  = PConstants;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1239
  //defaultScope.PImage    = PImage;     // TODO
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1240
  //defaultScope.PShape    = PShape;     // TODO
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1241
  //defaultScope.PShapeSVG = PShapeSVG;  // TODO
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1242
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1243
  ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1244
  // Class inheritance helper methods
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1245
  ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1246
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1247
  defaultScope.defineProperty = function(obj, name, desc) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1248
    if("defineProperty" in Object) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1249
      Object.defineProperty(obj, name, desc);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1250
    } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1251
      if (desc.hasOwnProperty("get")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1252
        obj.__defineGetter__(name, desc.get);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1253
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1254
      if (desc.hasOwnProperty("set")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1255
        obj.__defineSetter__(name, desc.set);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1256
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1257
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1258
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1259
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1260
  function extendClass(subClass, baseClass) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1261
    function extendGetterSetter(propertyName) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1262
      defaultScope.defineProperty(subClass, propertyName, {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1263
        get: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1264
          return baseClass[propertyName];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1265
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1266
        set: function(v) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1267
          baseClass[propertyName]=v;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1268
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1269
        enumerable: true
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1270
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1271
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1272
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1273
    var properties = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1274
    for (var propertyName in baseClass) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1275
      if (typeof baseClass[propertyName] === 'function') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1276
        // Overriding all non-overriden functions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1277
        if (!subClass.hasOwnProperty(propertyName)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1278
          subClass[propertyName] = baseClass[propertyName];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1279
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1280
      } else if(propertyName.charAt(0) !== "$" && !(propertyName in subClass)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1281
        // Delaying the properties extension due to the IE9 bug (see #918).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1282
        properties.push(propertyName);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1283
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1284
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1285
    while (properties.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1286
      extendGetterSetter(properties.shift());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1287
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1288
  }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1289
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1290
  defaultScope.extendClassChain = function(base) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1291
    var path = [base];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1292
    for (var self = base.$upcast; self; self = self.$upcast) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1293
      extendClass(self, base);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1294
      path.push(self);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1295
      base = self;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1296
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1297
    while (path.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1298
      path.pop().$self=base;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1299
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1300
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1301
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1302
  defaultScope.extendStaticMembers = function(derived, base) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1303
    extendClass(derived, base);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1304
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1305
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1306
  defaultScope.extendInterfaceMembers = function(derived, base) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1307
    extendClass(derived, base);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1308
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1309
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1310
  defaultScope.addMethod = function(object, name, fn, superAccessor) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1311
    if (object[name]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1312
      var args = fn.length,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1313
        oldfn = object[name];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1314
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1315
      object[name] = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1316
        if (arguments.length === args) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1317
          return fn.apply(this, arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1318
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1319
        return oldfn.apply(this, arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1320
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1321
    } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1322
      object[name] = fn;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1323
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1324
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1325
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1326
  defaultScope.createJavaArray = function(type, bounds) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1327
    var result = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1328
    if (typeof bounds[0] === 'number') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1329
      var itemsCount = 0 | bounds[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1330
      if (bounds.length <= 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1331
        result = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1332
        result.length = itemsCount;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1333
        for (var i = 0; i < itemsCount; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1334
          result[i] = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1335
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1336
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1337
        result = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1338
        var newBounds = bounds.slice(1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1339
        for (var j = 0; j < itemsCount; ++j) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1340
          result.push(defaultScope.createJavaArray(type, newBounds));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1341
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1342
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1343
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1344
    return result;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1345
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1346
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1347
  var colors = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1348
    aliceblue:            "#f0f8ff",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1349
    antiquewhite:         "#faebd7",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1350
    aqua:                 "#00ffff",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1351
    aquamarine:           "#7fffd4",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1352
    azure:                "#f0ffff",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1353
    beige:                "#f5f5dc",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1354
    bisque:               "#ffe4c4",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1355
    black:                "#000000",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1356
    blanchedalmond:       "#ffebcd",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1357
    blue:                 "#0000ff",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1358
    blueviolet:           "#8a2be2",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1359
    brown:                "#a52a2a",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1360
    burlywood:            "#deb887",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1361
    cadetblue:            "#5f9ea0",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1362
    chartreuse:           "#7fff00",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1363
    chocolate:            "#d2691e",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1364
    coral:                "#ff7f50",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1365
    cornflowerblue:       "#6495ed",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1366
    cornsilk:             "#fff8dc",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1367
    crimson:              "#dc143c",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1368
    cyan:                 "#00ffff",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1369
    darkblue:             "#00008b",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1370
    darkcyan:             "#008b8b",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1371
    darkgoldenrod:        "#b8860b",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1372
    darkgray:             "#a9a9a9",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1373
    darkgreen:            "#006400",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1374
    darkkhaki:            "#bdb76b",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1375
    darkmagenta:          "#8b008b",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1376
    darkolivegreen:       "#556b2f",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1377
    darkorange:           "#ff8c00",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1378
    darkorchid:           "#9932cc",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1379
    darkred:              "#8b0000",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1380
    darksalmon:           "#e9967a",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1381
    darkseagreen:         "#8fbc8f",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1382
    darkslateblue:        "#483d8b",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1383
    darkslategray:        "#2f4f4f",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1384
    darkturquoise:        "#00ced1",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1385
    darkviolet:           "#9400d3",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1386
    deeppink:             "#ff1493",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1387
    deepskyblue:          "#00bfff",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1388
    dimgray:              "#696969",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1389
    dodgerblue:           "#1e90ff",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1390
    firebrick:            "#b22222",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1391
    floralwhite:          "#fffaf0",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1392
    forestgreen:          "#228b22",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1393
    fuchsia:              "#ff00ff",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1394
    gainsboro:            "#dcdcdc",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1395
    ghostwhite:           "#f8f8ff",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1396
    gold:                 "#ffd700",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1397
    goldenrod:            "#daa520",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1398
    gray:                 "#808080",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1399
    green:                "#008000",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1400
    greenyellow:          "#adff2f",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1401
    honeydew:             "#f0fff0",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1402
    hotpink:              "#ff69b4",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1403
    indianred:            "#cd5c5c",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1404
    indigo:               "#4b0082",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1405
    ivory:                "#fffff0",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1406
    khaki:                "#f0e68c",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1407
    lavender:             "#e6e6fa",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1408
    lavenderblush:        "#fff0f5",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1409
    lawngreen:            "#7cfc00",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1410
    lemonchiffon:         "#fffacd",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1411
    lightblue:            "#add8e6",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1412
    lightcoral:           "#f08080",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1413
    lightcyan:            "#e0ffff",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1414
    lightgoldenrodyellow: "#fafad2",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1415
    lightgrey:            "#d3d3d3",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1416
    lightgreen:           "#90ee90",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1417
    lightpink:            "#ffb6c1",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1418
    lightsalmon:          "#ffa07a",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1419
    lightseagreen:        "#20b2aa",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1420
    lightskyblue:         "#87cefa",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1421
    lightslategray:       "#778899",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1422
    lightsteelblue:       "#b0c4de",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1423
    lightyellow:          "#ffffe0",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1424
    lime:                 "#00ff00",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1425
    limegreen:            "#32cd32",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1426
    linen:                "#faf0e6",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1427
    magenta:              "#ff00ff",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1428
    maroon:               "#800000",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1429
    mediumaquamarine:     "#66cdaa",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1430
    mediumblue:           "#0000cd",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1431
    mediumorchid:         "#ba55d3",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1432
    mediumpurple:         "#9370d8",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1433
    mediumseagreen:       "#3cb371",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1434
    mediumslateblue:      "#7b68ee",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1435
    mediumspringgreen:    "#00fa9a",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1436
    mediumturquoise:      "#48d1cc",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1437
    mediumvioletred:      "#c71585",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1438
    midnightblue:         "#191970",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1439
    mintcream:            "#f5fffa",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1440
    mistyrose:            "#ffe4e1",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1441
    moccasin:             "#ffe4b5",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1442
    navajowhite:          "#ffdead",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1443
    navy:                 "#000080",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1444
    oldlace:              "#fdf5e6",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1445
    olive:                "#808000",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1446
    olivedrab:            "#6b8e23",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1447
    orange:               "#ffa500",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1448
    orangered:            "#ff4500",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1449
    orchid:               "#da70d6",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1450
    palegoldenrod:        "#eee8aa",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1451
    palegreen:            "#98fb98",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1452
    paleturquoise:        "#afeeee",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1453
    palevioletred:        "#d87093",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1454
    papayawhip:           "#ffefd5",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1455
    peachpuff:            "#ffdab9",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1456
    peru:                 "#cd853f",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1457
    pink:                 "#ffc0cb",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1458
    plum:                 "#dda0dd",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1459
    powderblue:           "#b0e0e6",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1460
    purple:               "#800080",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1461
    red:                  "#ff0000",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1462
    rosybrown:            "#bc8f8f",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1463
    royalblue:            "#4169e1",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1464
    saddlebrown:          "#8b4513",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1465
    salmon:               "#fa8072",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1466
    sandybrown:           "#f4a460",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1467
    seagreen:             "#2e8b57",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1468
    seashell:             "#fff5ee",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1469
    sienna:               "#a0522d",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1470
    silver:               "#c0c0c0",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1471
    skyblue:              "#87ceeb",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1472
    slateblue:            "#6a5acd",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1473
    slategray:            "#708090",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1474
    snow:                 "#fffafa",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1475
    springgreen:          "#00ff7f",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1476
    steelblue:            "#4682b4",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1477
    tan:                  "#d2b48c",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1478
    teal:                 "#008080",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1479
    thistle:              "#d8bfd8",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1480
    tomato:               "#ff6347",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1481
    turquoise:            "#40e0d0",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1482
    violet:               "#ee82ee",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1483
    wheat:                "#f5deb3",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1484
    white:                "#ffffff",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1485
    whitesmoke:           "#f5f5f5",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1486
    yellow:               "#ffff00",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1487
    yellowgreen:          "#9acd32"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1488
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1489
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1490
  // Unsupported Processing File and I/O operations.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1491
  (function(Processing) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1492
    var unsupportedP5 = ("open() createOutput() createInput() BufferedReader selectFolder() " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1493
                         "dataPath() createWriter() selectOutput() beginRecord() " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1494
                         "saveStream() endRecord() selectInput() saveBytes() createReader() " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1495
                         "beginRaw() endRaw() PrintWriter delay()").split(" "),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1496
        count = unsupportedP5.length,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1497
        prettyName,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1498
        p5Name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1499
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1500
    function createUnsupportedFunc(n) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1501
      return function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1502
        throw "Processing.js does not support " + n + ".";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1503
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1504
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1505
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1506
    while (count--) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1507
      prettyName = unsupportedP5[count];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1508
      p5Name = prettyName.replace("()", "");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1509
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1510
      Processing[p5Name] = createUnsupportedFunc(prettyName);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1511
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1512
  }(defaultScope));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1513
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1514
  // screenWidth and screenHeight are shared by all instances.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1515
  // and return the width/height of the browser's viewport.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1516
  defaultScope.defineProperty(defaultScope, 'screenWidth',
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1517
    { get: function() { return window.innerWidth; } });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1518
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1519
  defaultScope.defineProperty(defaultScope, 'screenHeight',
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1520
    { get: function() { return window.innerHeight; } });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1521
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1522
  // Manage multiple Processing instances
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1523
  var processingInstances = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1524
  var processingInstanceIds = {};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1525
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1526
  var removeInstance = function(id) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1527
    processingInstances.splice(processingInstanceIds[id], 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1528
    delete processingInstanceIds[id];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1529
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1530
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1531
  var addInstance = function(processing) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1532
    if (processing.externals.canvas.id === undef || !processing.externals.canvas.id.length) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1533
      processing.externals.canvas.id = "__processing" + processingInstances.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1534
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1535
    processingInstanceIds[processing.externals.canvas.id] = processingInstances.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1536
    processingInstances.push(processing);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1537
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1538
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1539
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1540
  ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1541
  // PFONT.JS START
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1542
  ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1543
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1544
  /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1545
   * [internal function] computeFontMetrics() calculates various metrics for text
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1546
   * placement. Currently this function computes the ascent, descent and leading
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1547
   * (from "lead", used for vertical space) values for the currently active font.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1548
   */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1549
  function computeFontMetrics(pfont) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1550
    var emQuad = 250,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1551
        correctionFactor = pfont.size / emQuad,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1552
        canvas = document.createElement("canvas");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1553
    canvas.width = 2*emQuad;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1554
    canvas.height = 2*emQuad;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1555
    canvas.style.opacity = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1556
    var cfmFont = pfont.getCSSDefinition(emQuad+"px", "normal"),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1557
        ctx = canvas.getContext("2d");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1558
    ctx.font = cfmFont;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1559
    pfont.context2d = ctx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1560
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1561
    // Size the canvas using a string with common max-ascent and max-descent letters.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1562
    // Changing the canvas dimensions resets the context, so we must reset the font.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1563
    var protrusions = "dbflkhyjqpg";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1564
    canvas.width = ctx.measureText(protrusions).width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1565
    ctx.font = cfmFont;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1566
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1567
    // for text lead values, we meaure a multiline text container.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1568
    var leadDiv = document.createElement("div");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1569
    leadDiv.style.position = "absolute";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1570
    leadDiv.style.opacity = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1571
    leadDiv.style.fontFamily = '"' + pfont.name + '"';
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1572
    leadDiv.style.fontSize = emQuad + "px";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1573
    leadDiv.innerHTML = protrusions + "<br/>" + protrusions;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1574
    document.body.appendChild(leadDiv);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1575
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1576
    var w = canvas.width,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1577
        h = canvas.height,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1578
        baseline = h/2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1579
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1580
    // Set all canvas pixeldata values to 255, with all the content
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1581
    // data being 0. This lets us scan for data[i] != 255.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1582
    ctx.fillStyle = "white";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1583
    ctx.fillRect(0, 0, w, h);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1584
    ctx.fillStyle = "black";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1585
    ctx.fillText(protrusions, 0, baseline);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1586
    var pixelData = ctx.getImageData(0, 0, w, h).data;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1587
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1588
    // canvas pixel data is w*4 by h*4, because R, G, B and A are separate,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1589
    // consecutive values in the array, rather than stored as 32 bit ints.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1590
    var i = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1591
        w4 = w * 4,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1592
        len = pixelData.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1593
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1594
    // Finding the ascent uses a normal, forward scanline
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1595
    while (++i < len && pixelData[i] === 255) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1596
      nop();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1597
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1598
    var ascent = Math.round(i / w4);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1599
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1600
    // Finding the descent uses a reverse scanline
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1601
    i = len - 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1602
    while (--i > 0 && pixelData[i] === 255) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1603
      nop();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1604
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1605
    var descent = Math.round(i / w4);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1606
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1607
    // set font metrics
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1608
    pfont.ascent = correctionFactor * (baseline - ascent);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1609
    pfont.descent = correctionFactor * (descent - baseline);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1610
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1611
    // Then we try to get the real value from the browser
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1612
    if (document.defaultView.getComputedStyle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1613
      var leadDivHeight = document.defaultView.getComputedStyle(leadDiv,null).getPropertyValue("height");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1614
      leadDivHeight = correctionFactor * leadDivHeight.replace("px","");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1615
      if (leadDivHeight >= pfont.size * 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1616
        pfont.leading = Math.round(leadDivHeight/2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1617
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1618
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1619
    document.body.removeChild(leadDiv);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1620
  }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1621
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1622
  // Defines system (non-SVG) font.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1623
  function PFont(name, size) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1624
    // according to the P5 API, new PFont() is legal (albeit completely useless)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1625
    if (name === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1626
      name = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1627
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1628
    this.name = name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1629
    if (size === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1630
      size = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1631
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1632
    this.size = size;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1633
    this.glyph = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1634
    this.ascent = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1635
    this.descent = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1636
    // For leading, the "safe" value uses the standard TEX ratio
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1637
    this.leading = 1.2 * size;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1638
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1639
    // Note that an italic, bold font must used "... Bold Italic"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1640
    // in P5. "... Italic Bold" is treated as normal/normal.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1641
    var illegalIndicator = name.indexOf(" Italic Bold");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1642
    if (illegalIndicator !== -1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1643
      name = name.substring(0, illegalIndicator);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1644
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1645
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1646
    // determine font style
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1647
    this.style = "normal";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1648
    var italicsIndicator = name.indexOf(" Italic");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1649
    if (italicsIndicator !== -1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1650
      name = name.substring(0, italicsIndicator);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1651
      this.style = "italic";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1652
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1653
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1654
    // determine font weight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1655
    this.weight = "normal";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1656
    var boldIndicator = name.indexOf(" Bold");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1657
    if (boldIndicator !== -1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1658
      name = name.substring(0, boldIndicator);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1659
      this.weight = "bold";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1660
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1661
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1662
    // determine font-family name
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1663
    this.family = "sans-serif";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1664
    if (name !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1665
      switch(name) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1666
        case "sans-serif":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1667
        case "serif":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1668
        case "monospace":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1669
        case "fantasy":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1670
        case "cursive":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1671
          this.family = name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1672
          break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1673
        default:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1674
          this.family = '"' + name + '", sans-serif';
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1675
          break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1676
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1677
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1678
    // Calculate the ascent/descent/leading value based on
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1679
    // how the browser renders this font.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1680
    this.context2d = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1681
    computeFontMetrics(this);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1682
    this.css = this.getCSSDefinition();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1683
    this.context2d.font = this.css;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1684
  }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1685
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1686
  /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1687
  * This function generates the CSS "font" string for this PFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1688
  */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1689
  PFont.prototype.getCSSDefinition = function(fontSize, lineHeight) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1690
    if(fontSize===undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1691
      fontSize = this.size + "px";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1692
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1693
    if(lineHeight===undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1694
      lineHeight = this.leading + "px";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1695
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1696
    // CSS "font" definition: font-style font-variant font-weight font-size/line-height font-family
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1697
    var components = [this.style, "normal", this.weight, fontSize + "/" + lineHeight, this.family];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1698
    return components.join(" ");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1699
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1700
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1701
  /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1702
  * We cannot rely on there being a 2d context available,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1703
  * because we support OPENGL sketches, and canvas3d has
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1704
  * no "measureText" function in the API.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1705
  */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1706
  PFont.prototype.measureTextWidth = function(string) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1707
    return this.context2d.measureText(string).width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1708
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1709
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1710
  /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1711
  * Global "loaded fonts" list, internal to PFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1712
  */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1713
  PFont.PFontCache = {};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1714
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1715
  /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1716
  * This function acts as single access point for getting and caching
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1717
  * fonts across all sketches handled by an instance of Processing.js
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1718
  */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1719
  PFont.get = function(fontName, fontSize) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1720
    var cache = PFont.PFontCache;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1721
    var idx = fontName+"/"+fontSize;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1722
    if (!cache[idx]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1723
      cache[idx] = new PFont(fontName, fontSize);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1724
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1725
    return cache[idx];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1726
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1727
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1728
  /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1729
  * Lists all standard fonts. Due to browser limitations, this list is
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1730
  * not the system font list, like in P5, but the CSS "genre" list.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1731
  */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1732
  PFont.list = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1733
    return ["sans-serif", "serif", "monospace", "fantasy", "cursive"];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1734
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1735
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1736
  /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1737
  * Loading external fonts through @font-face rules is handled by PFont,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1738
  * to ensure fonts loaded in this way are globally available.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1739
  */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1740
  PFont.preloading = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1741
    // template element used to compare font sizes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1742
    template: {},
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1743
    // indicates whether or not the reference tiny font has been loaded
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1744
    initialized: false,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1745
    // load the reference tiny font via a css @font-face rule
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1746
    initialize: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1747
      var generateTinyFont = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1748
        var encoded = "#E3KAI2wAgT1MvMg7Eo3VmNtYX7ABi3CxnbHlm" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1749
                      "7Abw3kaGVhZ7ACs3OGhoZWE7A53CRobXR47AY3" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1750
                      "AGbG9jYQ7G03Bm1heH7ABC3CBuYW1l7Ae3AgcG" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1751
                      "9zd7AI3AE#B3AQ2kgTY18PPPUACwAg3ALSRoo3" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1752
                      "#yld0xg32QAB77#E777773B#E3C#I#Q77773E#" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1753
                      "Q7777777772CMAIw7AB77732B#M#Q3wAB#g3B#" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1754
                      "E#E2BB//82BB////w#B7#gAEg3E77x2B32B#E#" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1755
                      "Q#MTcBAQ32gAe#M#QQJ#E32M#QQJ#I#g32Q77#";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1756
        var expand = function(input) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1757
                       return "AAAAAAAA".substr(~~input ? 7-input : 6);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1758
                     };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1759
        return encoded.replace(/[#237]/g, expand);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1760
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1761
      var fontface = document.createElement("style");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1762
      fontface.setAttribute("type","text/css");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1763
      fontface.innerHTML =  "@font-face {\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1764
                            '  font-family: "PjsEmptyFont";' + "\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1765
                            "  src: url('data:application/x-font-ttf;base64,"+generateTinyFont()+"')\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1766
                            "       format('truetype');\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1767
                            "}";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1768
      document.head.appendChild(fontface);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1769
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1770
      // set up the template element
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1771
      var element = document.createElement("span");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1772
      element.style.cssText = 'position: absolute; top: 0; left: 0; opacity: 0; font-family: "PjsEmptyFont", fantasy;';
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1773
      element.innerHTML = "AAAAAAAA";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1774
      document.body.appendChild(element);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1775
      this.template = element;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1776
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1777
      this.initialized = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1778
    },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1779
    // Shorthand function to get the computed width for an element.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1780
    getElementWidth: function(element) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1781
      return document.defaultView.getComputedStyle(element,"").getPropertyValue("width");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1782
    },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1783
    // time taken so far in attempting to load a font
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1784
    timeAttempted: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1785
    // returns false if no fonts are pending load, or true otherwise.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1786
    pending: function(intervallength) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1787
      if (!this.initialized) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1788
        this.initialize();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1789
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1790
      var element,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1791
          computedWidthFont,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1792
          computedWidthRef = this.getElementWidth(this.template);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1793
      for (var i = 0; i < this.fontList.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1794
        // compares size of text in pixels. if equal, custom font is not yet loaded
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1795
        element = this.fontList[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1796
        computedWidthFont = this.getElementWidth(element);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1797
        if (this.timeAttempted < 4000 && computedWidthFont === computedWidthRef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1798
          this.timeAttempted += intervallength;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1799
          return true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1800
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1801
          document.body.removeChild(element);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1802
          this.fontList.splice(i--, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1803
          this.timeAttempted = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1804
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1805
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1806
      // if there are no more fonts to load, pending is false
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1807
      if (this.fontList.length === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1808
        return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1809
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1810
      // We should have already returned before getting here.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1811
      // But, if we do get here, length!=0 so fonts are pending.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1812
      return true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1813
    },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1814
    // fontList contains elements to compare font sizes against a template
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1815
    fontList: [],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1816
    // addedList contains the fontnames of all the fonts loaded via @font-face
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1817
    addedList: {},
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1818
    // adds a font to the font cache
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1819
    // creates an element using the font, to start loading the font,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1820
    // and compare against a default font to see if the custom font is loaded
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1821
    add: function(fontSrc) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1822
      if (!this.initialized) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1823
       this.initialize();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1824
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1825
      // fontSrc can be a string or a javascript object
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1826
      // acceptable fonts are .ttf, .otf, and data uri
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1827
      var fontName = (typeof fontSrc === 'object' ? fontSrc.fontFace : fontSrc),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1828
          fontUrl = (typeof fontSrc === 'object' ? fontSrc.url : fontSrc);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1829
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1830
      // check whether we already created the @font-face rule for this font
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1831
      if (this.addedList[fontName]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1832
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1833
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1834
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1835
      // if we didn't, create the @font-face rule
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1836
      var style = document.createElement("style");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1837
      style.setAttribute("type","text/css");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1838
      style.innerHTML = "@font-face{\n  font-family: '" + fontName + "';\n  src:  url('" + fontUrl + "');\n}\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1839
      document.head.appendChild(style);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1840
      this.addedList[fontName] = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1841
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1842
      // also create the element to load and compare the new font
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1843
      var element = document.createElement("span");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1844
      element.style.cssText = "position: absolute; top: 0; left: 0; opacity: 0;";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1845
      element.style.fontFamily = '"' + fontName + '", "PjsEmptyFont", fantasy';
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1846
      element.innerHTML = "AAAAAAAA";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1847
      document.body.appendChild(element);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1848
      this.fontList.push(element);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1849
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1850
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1851
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1852
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1853
  // add to the default scope
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1854
  defaultScope.PFont = PFont;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1855
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1856
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1857
  ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1858
  // PFONT.JS END
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1859
  ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1860
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1861
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1862
  var Processing = this.Processing = function(aCanvas, aCode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1863
    // Previously we allowed calling Processing as a func instead of ctor, but no longer.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1864
    if (!(this instanceof Processing)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1865
      throw("called Processing constructor as if it were a function: missing 'new'.");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1866
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1867
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1868
    var curElement,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1869
      pgraphicsMode = (aCanvas === undef && aCode === undef);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1870
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1871
    if (pgraphicsMode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1872
      curElement = document.createElement("canvas");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1873
    } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1874
      // We'll take a canvas element or a string for a canvas element's id
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1875
      curElement = typeof aCanvas === "string" ? document.getElementById(aCanvas) : aCanvas;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1876
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1877
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1878
    if (!(curElement instanceof HTMLCanvasElement)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1879
      throw("called Processing constructor without passing canvas element reference or id.");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1880
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1881
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1882
    function unimplemented(s) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1883
      Processing.debug('Unimplemented - ' + s);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1884
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1885
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1886
    // When something new is added to "p." it must also be added to the "names" array.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1887
    // The names array contains the names of everything that is inside "p."
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1888
    var p = this;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1889
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1890
    // PJS specific (non-p5) methods and properties to externalize
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1891
    p.externals = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1892
      canvas:  curElement,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1893
      context: undef,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1894
      sketch:  undef
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1895
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1896
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1897
    p.name            = 'Processing.js Instance'; // Set Processing defaults / environment variables
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1898
    p.use3DContext    = false; // default '2d' canvas context
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1899
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1900
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1901
     * Confirms if a Processing program is "focused", meaning that it is
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1902
     * active and will accept input from mouse or keyboard. This variable
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1903
     * is "true" if it is focused and "false" if not. This variable is
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1904
     * often used when you want to warn people they need to click on the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1905
     * browser before it will work.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1906
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1907
    p.focused         = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1908
    p.breakShape      = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1909
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1910
    // Glyph path storage for textFonts
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1911
    p.glyphTable      = {};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1912
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1913
    // Global vars for tracking mouse position
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1914
    p.pmouseX         = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1915
    p.pmouseY         = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1916
    p.mouseX          = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1917
    p.mouseY          = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1918
    p.mouseButton     = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1919
    p.mouseScroll     = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1920
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1921
    // Undefined event handlers to be replaced by user when needed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1922
    p.mouseClicked    = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1923
    p.mouseDragged    = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1924
    p.mouseMoved      = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1925
    p.mousePressed    = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1926
    p.mouseReleased   = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1927
    p.mouseScrolled   = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1928
    p.mouseOver       = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1929
    p.mouseOut        = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1930
    p.touchStart      = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1931
    p.touchEnd        = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1932
    p.touchMove       = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1933
    p.touchCancel     = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1934
    p.key             = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1935
    p.keyCode         = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1936
    p.keyPressed      = nop; // needed to remove function checks
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1937
    p.keyReleased     = nop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1938
    p.keyTyped        = nop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1939
    p.draw            = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1940
    p.setup           = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1941
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1942
    // Remapped vars
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1943
    p.__mousePressed  = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1944
    p.__keyPressed    = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1945
    p.__frameRate     = 60;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1946
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1947
    // The current animation frame
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1948
    p.frameCount      = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1949
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1950
    // The height/width of the canvas
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1951
    p.width           = 100;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1952
    p.height          = 100;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1953
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1954
    // "Private" variables used to maintain state
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1955
    var curContext,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1956
        curSketch,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1957
        drawing, // hold a Drawing2D or Drawing3D object
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1958
        online = true,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1959
        doFill = true,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1960
        fillStyle = [1.0, 1.0, 1.0, 1.0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1961
        currentFillColor = 0xFFFFFFFF,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1962
        isFillDirty = true,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1963
        doStroke = true,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1964
        strokeStyle = [0.0, 0.0, 0.0, 1.0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1965
        currentStrokeColor = 0xFF000000,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1966
        isStrokeDirty = true,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1967
        lineWidth = 1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1968
        loopStarted = false,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1969
        renderSmooth = false,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1970
        doLoop = true,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1971
        looping = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1972
        curRectMode = PConstants.CORNER,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1973
        curEllipseMode = PConstants.CENTER,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1974
        normalX = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1975
        normalY = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1976
        normalZ = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1977
        normalMode = PConstants.NORMAL_MODE_AUTO,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1978
        curFrameRate = 60,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1979
        curMsPerFrame = 1000/curFrameRate,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1980
        curCursor = PConstants.ARROW,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1981
        oldCursor = curElement.style.cursor,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1982
        curShape = PConstants.POLYGON,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1983
        curShapeCount = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1984
        curvePoints = [],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1985
        curTightness = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1986
        curveDet = 20,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1987
        curveInited = false,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1988
        backgroundObj = -3355444, // rgb(204, 204, 204) is the default gray background colour
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1989
        bezDetail = 20,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1990
        colorModeA = 255,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1991
        colorModeX = 255,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1992
        colorModeY = 255,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1993
        colorModeZ = 255,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1994
        pathOpen = false,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1995
        mouseDragging = false,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1996
        pmouseXLastFrame = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1997
        pmouseYLastFrame = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1998
        curColorMode = PConstants.RGB,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  1999
        curTint = null,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2000
        curTint3d = null,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2001
        getLoaded = false,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2002
        start = Date.now(),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2003
        timeSinceLastFPS = start,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2004
        framesSinceLastFPS = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2005
        textcanvas,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2006
        curveBasisMatrix,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2007
        curveToBezierMatrix,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2008
        curveDrawMatrix,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2009
        bezierDrawMatrix,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2010
        bezierBasisInverse,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2011
        bezierBasisMatrix,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2012
        curContextCache = { attributes: {}, locations: {} },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2013
        // Shaders
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2014
        programObject3D,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2015
        programObject2D,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2016
        programObjectUnlitShape,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2017
        boxBuffer,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2018
        boxNormBuffer,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2019
        boxOutlineBuffer,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2020
        rectBuffer,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2021
        rectNormBuffer,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2022
        sphereBuffer,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2023
        lineBuffer,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2024
        fillBuffer,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2025
        fillColorBuffer,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2026
        strokeColorBuffer,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2027
        pointBuffer,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2028
        shapeTexVBO,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2029
        canTex,   // texture for createGraphics
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2030
        textTex,   // texture for 3d tex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2031
        curTexture = {width:0,height:0},
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2032
        curTextureMode = PConstants.IMAGE,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2033
        usingTexture = false,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2034
        textBuffer,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2035
        textureBuffer,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2036
        indexBuffer,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2037
        // Text alignment
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2038
        horizontalTextAlignment = PConstants.LEFT,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2039
        verticalTextAlignment = PConstants.BASELINE,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2040
        textMode = PConstants.MODEL,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2041
        // Font state
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2042
        curFontName = "Arial",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2043
        curTextSize = 12,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2044
        curTextAscent = 9,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2045
        curTextDescent = 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2046
        curTextLeading = 14,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2047
        curTextFont = PFont.get(curFontName, curTextSize),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2048
        // Pixels cache
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2049
        originalContext,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2050
        proxyContext = null,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2051
        isContextReplaced = false,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2052
        setPixelsCached,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2053
        maxPixelsCached = 1000,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2054
        pressedKeysMap = [],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2055
        lastPressedKeyCode = null,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2056
        codedKeys = [ PConstants.SHIFT, PConstants.CONTROL, PConstants.ALT, PConstants.CAPSLK, PConstants.PGUP, PConstants.PGDN,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2057
                      PConstants.END, PConstants.HOME, PConstants.LEFT, PConstants.UP, PConstants.RIGHT, PConstants.DOWN, PConstants.NUMLK,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2058
                      PConstants.INSERT, PConstants.F1, PConstants.F2, PConstants.F3, PConstants.F4, PConstants.F5, PConstants.F6, PConstants.F7,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2059
                      PConstants.F8, PConstants.F9, PConstants.F10, PConstants.F11, PConstants.F12, PConstants.META ];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2060
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2061
    // Get padding and border style widths for mouse offsets
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2062
    var stylePaddingLeft, stylePaddingTop, styleBorderLeft, styleBorderTop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2063
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2064
    if (document.defaultView && document.defaultView.getComputedStyle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2065
      stylePaddingLeft = parseInt(document.defaultView.getComputedStyle(curElement, null)['paddingLeft'], 10)      || 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2066
      stylePaddingTop  = parseInt(document.defaultView.getComputedStyle(curElement, null)['paddingTop'], 10)       || 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2067
      styleBorderLeft  = parseInt(document.defaultView.getComputedStyle(curElement, null)['borderLeftWidth'], 10)  || 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2068
      styleBorderTop   = parseInt(document.defaultView.getComputedStyle(curElement, null)['borderTopWidth'], 10)   || 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2069
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2070
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2071
    // User can only have MAX_LIGHTS lights
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2072
    var lightCount = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2073
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2074
    //sphere stuff
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2075
    var sphereDetailV = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2076
        sphereDetailU = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2077
        sphereX = [],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2078
        sphereY = [],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2079
        sphereZ = [],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2080
        sinLUT = new Float32Array(PConstants.SINCOS_LENGTH),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2081
        cosLUT = new Float32Array(PConstants.SINCOS_LENGTH),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2082
        sphereVerts,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2083
        sphereNorms;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2084
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2085
    // Camera defaults and settings
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2086
    var cam,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2087
        cameraInv,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2088
        modelView,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2089
        modelViewInv,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2090
        userMatrixStack,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2091
        userReverseMatrixStack,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2092
        inverseCopy,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2093
        projection,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2094
        manipulatingCamera = false,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2095
        frustumMode = false,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2096
        cameraFOV = 60 * (Math.PI / 180),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2097
        cameraX = p.width / 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2098
        cameraY = p.height / 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2099
        cameraZ = cameraY / Math.tan(cameraFOV / 2),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2100
        cameraNear = cameraZ / 10,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2101
        cameraFar = cameraZ * 10,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2102
        cameraAspect = p.width / p.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2103
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2104
    var vertArray = [],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2105
        curveVertArray = [],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2106
        curveVertCount = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2107
        isCurve = false,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2108
        isBezier = false,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2109
        firstVert = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2110
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2111
    //PShape stuff
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2112
    var curShapeMode = PConstants.CORNER;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2113
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2114
    // Stores states for pushStyle() and popStyle().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2115
    var styleArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2116
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2117
    // Vertices are specified in a counter-clockwise order
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2118
    // triangles are in this order: back, front, right, bottom, left, top
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2119
    var boxVerts = new Float32Array([
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2120
       0.5,  0.5, -0.5,  0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5,  0.5, -0.5,  0.5,  0.5, -0.5,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2121
       0.5,  0.5,  0.5, -0.5,  0.5,  0.5, -0.5, -0.5,  0.5, -0.5, -0.5,  0.5,  0.5, -0.5,  0.5,  0.5,  0.5,  0.5,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2122
       0.5,  0.5, -0.5,  0.5,  0.5,  0.5,  0.5, -0.5,  0.5,  0.5, -0.5,  0.5,  0.5, -0.5, -0.5,  0.5,  0.5, -0.5,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2123
       0.5, -0.5, -0.5,  0.5, -0.5,  0.5, -0.5, -0.5,  0.5, -0.5, -0.5,  0.5, -0.5, -0.5, -0.5,  0.5, -0.5, -0.5,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2124
      -0.5, -0.5, -0.5, -0.5, -0.5,  0.5, -0.5,  0.5,  0.5, -0.5,  0.5,  0.5, -0.5,  0.5, -0.5, -0.5, -0.5, -0.5,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2125
       0.5,  0.5,  0.5,  0.5,  0.5, -0.5, -0.5,  0.5, -0.5, -0.5,  0.5, -0.5, -0.5,  0.5,  0.5,  0.5,  0.5,  0.5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2126
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2127
    var boxOutlineVerts = new Float32Array([
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2128
       0.5,  0.5,  0.5,  0.5, -0.5,  0.5,  0.5,  0.5, -0.5,  0.5, -0.5, -0.5,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2129
      -0.5,  0.5, -0.5, -0.5, -0.5, -0.5, -0.5,  0.5,  0.5, -0.5, -0.5,  0.5,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2130
       0.5,  0.5,  0.5,  0.5,  0.5, -0.5,  0.5,  0.5, -0.5, -0.5,  0.5, -0.5,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2131
      -0.5,  0.5, -0.5, -0.5,  0.5,  0.5, -0.5,  0.5,  0.5,  0.5,  0.5,  0.5,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2132
       0.5, -0.5,  0.5,  0.5, -0.5, -0.5,  0.5, -0.5, -0.5, -0.5, -0.5, -0.5,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2133
      -0.5, -0.5, -0.5, -0.5, -0.5,  0.5, -0.5, -0.5,  0.5,  0.5, -0.5,  0.5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2134
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2135
    var boxNorms = new Float32Array([
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2136
       0,  0, -1,  0,  0, -1,  0,  0, -1,  0,  0, -1,  0,  0, -1,  0,  0, -1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2137
       0,  0,  1,  0,  0,  1,  0,  0,  1,  0,  0,  1,  0,  0,  1,  0,  0,  1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2138
       1,  0,  0,  1,  0,  0,  1,  0,  0,  1,  0,  0,  1,  0,  0,  1,  0,  0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2139
       0, -1,  0,  0, -1,  0,  0, -1,  0,  0, -1,  0,  0, -1,  0,  0, -1,  0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2140
      -1,  0,  0, -1,  0,  0, -1,  0,  0, -1,  0,  0, -1,  0,  0, -1,  0,  0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2141
       0,  1,  0,  0,  1,  0,  0,  1,  0,  0,  1,  0,  0,  1,  0,  0,  1,  0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2142
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2143
    // These verts are used for the fill and stroke using TRIANGLE_FAN and LINE_LOOP
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2144
    var rectVerts = new Float32Array([0,0,0, 0,1,0, 1,1,0, 1,0,0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2145
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2146
    var rectNorms = new Float32Array([0,0,1, 0,0,1, 0,0,1, 0,0,1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2147
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2148
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2149
    // Shader for points and lines in begin/endShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2150
    var vShaderSrcUnlitShape =
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2151
      "varying vec4 frontColor;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2152
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2153
      "attribute vec3 aVertex;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2154
      "attribute vec4 aColor;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2155
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2156
      "uniform mat4 uView;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2157
      "uniform mat4 uProjection;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2158
      "uniform float pointSize;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2159
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2160
      "void main(void) {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2161
      "  frontColor = aColor;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2162
      "  gl_PointSize = pointSize;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2163
      "  gl_Position = uProjection * uView * vec4(aVertex, 1.0);" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2164
      "}";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2165
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2166
    var fShaderSrcUnlitShape =
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2167
      "#ifdef GL_ES\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2168
      "precision highp float;\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2169
      "#endif\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2170
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2171
      "varying vec4 frontColor;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2172
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2173
      "void main(void){" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2174
      "  gl_FragColor = frontColor;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2175
      "}";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2176
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2177
    // Shader for rect, text, box outlines, sphere outlines, point() and line()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2178
    var vertexShaderSource2D =
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2179
      "varying vec4 frontColor;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2180
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2181
      "attribute vec3 Vertex;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2182
      "attribute vec2 aTextureCoord;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2183
      "uniform vec4 color;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2184
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2185
      "uniform mat4 model;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2186
      "uniform mat4 view;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2187
      "uniform mat4 projection;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2188
      "uniform float pointSize;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2189
      "varying vec2 vTextureCoord;"+
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2190
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2191
      "void main(void) {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2192
      "  gl_PointSize = pointSize;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2193
      "  frontColor = color;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2194
      "  gl_Position = projection * view * model * vec4(Vertex, 1.0);" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2195
      "  vTextureCoord = aTextureCoord;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2196
      "}";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2197
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2198
    var fragmentShaderSource2D =
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2199
      "#ifdef GL_ES\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2200
      "precision highp float;\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2201
      "#endif\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2202
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2203
      "varying vec4 frontColor;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2204
      "varying vec2 vTextureCoord;"+
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2205
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2206
      "uniform sampler2D uSampler;"+
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2207
      "uniform int picktype;"+
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2208
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2209
      "void main(void){" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2210
      "  if(picktype == 0){"+
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2211
      "    gl_FragColor = frontColor;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2212
      "  }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2213
      "  else if(picktype == 1){"+
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2214
      "    float alpha = texture2D(uSampler, vTextureCoord).a;"+
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2215
      "    gl_FragColor = vec4(frontColor.rgb*alpha, alpha);\n"+
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2216
      "  }"+
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2217
      "}";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2218
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2219
    var webglMaxTempsWorkaround = /Windows/.test(navigator.userAgent);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2220
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2221
    // Vertex shader for boxes and spheres
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2222
    var vertexShaderSource3D =
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2223
      "varying vec4 frontColor;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2224
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2225
      "attribute vec3 Vertex;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2226
      "attribute vec3 Normal;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2227
      "attribute vec4 aColor;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2228
      "attribute vec2 aTexture;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2229
      "varying   vec2 vTexture;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2230
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2231
      "uniform vec4 color;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2232
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2233
      "uniform bool usingMat;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2234
      "uniform vec3 specular;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2235
      "uniform vec3 mat_emissive;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2236
      "uniform vec3 mat_ambient;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2237
      "uniform vec3 mat_specular;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2238
      "uniform float shininess;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2239
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2240
      "uniform mat4 model;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2241
      "uniform mat4 view;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2242
      "uniform mat4 projection;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2243
      "uniform mat4 normalTransform;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2244
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2245
      "uniform int lightCount;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2246
      "uniform vec3 falloff;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2247
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2248
      // careful changing the order of these fields. Some cards
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2249
      // have issues with memory alignment
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2250
      "struct Light {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2251
      "  int type;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2252
      "  vec3 color;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2253
      "  vec3 position;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2254
      "  vec3 direction;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2255
      "  float angle;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2256
      "  vec3 halfVector;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2257
      "  float concentration;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2258
      "};" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2259
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2260
      // nVidia cards have issues with arrays of structures
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2261
      // so instead we create 8 instances of Light
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2262
      "uniform Light lights0;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2263
      "uniform Light lights1;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2264
      "uniform Light lights2;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2265
      "uniform Light lights3;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2266
      "uniform Light lights4;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2267
      "uniform Light lights5;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2268
      "uniform Light lights6;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2269
      "uniform Light lights7;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2270
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2271
     // GLSL does not support switch
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2272
      "Light getLight(int index){" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2273
      "  if(index == 0) return lights0;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2274
      "  if(index == 1) return lights1;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2275
      "  if(index == 2) return lights2;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2276
      "  if(index == 3) return lights3;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2277
      "  if(index == 4) return lights4;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2278
      "  if(index == 5) return lights5;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2279
      "  if(index == 6) return lights6;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2280
      // Do not use a conditional for the last return statement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2281
      // because some video cards will fail and complain that
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2282
      // "not all paths return"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2283
      "  return lights7;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2284
      "}" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2285
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2286
      "void AmbientLight( inout vec3 totalAmbient, in vec3 ecPos, in Light light ) {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2287
      // Get the vector from the light to the vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2288
      // Get the distance from the current vector to the light position
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2289
      "  float d = length( light.position - ecPos );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2290
      "  float attenuation = 1.0 / ( falloff[0] + ( falloff[1] * d ) + ( falloff[2] * d * d ));" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2291
      "  totalAmbient += light.color * attenuation;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2292
      "}" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2293
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2294
      "void DirectionalLight( inout vec3 col, inout vec3 spec, in vec3 vertNormal, in vec3 ecPos, in Light light ) {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2295
      "  float powerfactor = 0.0;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2296
      "  float nDotVP = max(0.0, dot( vertNormal, normalize(-light.position) ));" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2297
      "  float nDotVH = max(0.0, dot( vertNormal, normalize(-light.position-normalize(ecPos) )));" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2298
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2299
      "  if( nDotVP != 0.0 ){" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2300
      "    powerfactor = pow( nDotVH, shininess );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2301
      "  }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2302
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2303
      "  col += light.color * nDotVP;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2304
      "  spec += specular * powerfactor;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2305
      "}" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2306
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2307
      "void PointLight( inout vec3 col, inout vec3 spec, in vec3 vertNormal, in vec3 ecPos, in Light light ) {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2308
      "  float powerfactor;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2309
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2310
      // Get the vector from the light to the vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2311
      "   vec3 VP = light.position - ecPos;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2312
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2313
      // Get the distance from the current vector to the light position
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2314
      "  float d = length( VP ); " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2315
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2316
      // Normalize the light ray so it can be used in the dot product operation.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2317
      "  VP = normalize( VP );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2318
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2319
      "  float attenuation = 1.0 / ( falloff[0] + ( falloff[1] * d ) + ( falloff[2] * d * d ));" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2320
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2321
      "  float nDotVP = max( 0.0, dot( vertNormal, VP ));" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2322
      "  vec3 halfVector = normalize( VP - normalize(ecPos) );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2323
      "  float nDotHV = max( 0.0, dot( vertNormal, halfVector ));" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2324
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2325
      "  if( nDotVP == 0.0) {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2326
      "    powerfactor = 0.0;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2327
      "  }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2328
      "  else{" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2329
      "    powerfactor = pow( nDotHV, shininess );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2330
      "  }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2331
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2332
      "  spec += specular * powerfactor * attenuation;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2333
      "  col += light.color * nDotVP * attenuation;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2334
      "}" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2335
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2336
      /*
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2337
      */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2338
      "void SpotLight( inout vec3 col, inout vec3 spec, in vec3 vertNormal, in vec3 ecPos, in Light light ) {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2339
      "  float spotAttenuation;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2340
      "  float powerfactor;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2341
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2342
      // calculate the vector from the current vertex to the light.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2343
      "  vec3 VP = light.position - ecPos; " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2344
      "  vec3 ldir = normalize( -light.direction );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2345
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2346
      // get the distance from the spotlight and the vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2347
      "  float d = length( VP );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2348
      "  VP = normalize( VP );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2349
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2350
      "  float attenuation = 1.0 / ( falloff[0] + ( falloff[1] * d ) + ( falloff[2] * d * d ) );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2351
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2352
      // dot product of the vector from vertex to light and light direction.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2353
      "  float spotDot = dot( VP, ldir );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2354
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2355
      // if the vertex falls inside the cone
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2356
      (webglMaxTempsWorkaround ? // Windows reports max temps error if light.angle is used
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2357
      "  spotAttenuation = 1.0; " :
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2358
      "  if( spotDot > cos( light.angle ) ) {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2359
      "    spotAttenuation = pow( spotDot, light.concentration );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2360
      "  }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2361
      "  else{" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2362
      "    spotAttenuation = 0.0;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2363
      "  }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2364
      "  attenuation *= spotAttenuation;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2365
      "") +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2366
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2367
      "  float nDotVP = max( 0.0, dot( vertNormal, VP ));" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2368
      "  vec3 halfVector = normalize( VP - normalize(ecPos) );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2369
      "  float nDotHV = max( 0.0, dot( vertNormal, halfVector ));" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2370
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2371
      "  if( nDotVP == 0.0 ) {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2372
      "    powerfactor = 0.0;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2373
      "  }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2374
      "  else {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2375
      "    powerfactor = pow( nDotHV, shininess );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2376
      "  }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2377
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2378
      "  spec += specular * powerfactor * attenuation;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2379
      "  col += light.color * nDotVP * attenuation;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2380
      "}" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2381
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2382
      "void main(void) {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2383
      "  vec3 finalAmbient = vec3( 0.0, 0.0, 0.0 );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2384
      "  vec3 finalDiffuse = vec3( 0.0, 0.0, 0.0 );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2385
      "  vec3 finalSpecular = vec3( 0.0, 0.0, 0.0 );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2386
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2387
      "  vec4 col = color;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2388
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2389
      "  if(color[0] == -1.0){" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2390
      "    col = aColor;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2391
      "  }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2392
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2393
      // We use the sphere vertices as the normals when we create the sphere buffer.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2394
      // But this only works if the sphere vertices are unit length, so we
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2395
      // have to normalize the normals here. Since this is only required for spheres
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2396
      // we could consider placing this in a conditional later on.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2397
      "  vec3 norm = normalize(vec3( normalTransform * vec4( Normal, 0.0 ) ));" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2398
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2399
      "  vec4 ecPos4 = view * model * vec4(Vertex,1.0);" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2400
      "  vec3 ecPos = (vec3(ecPos4))/ecPos4.w;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2401
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2402
      // If there were no lights this draw call, just use the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2403
      // assigned fill color of the shape and the specular value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2404
      "  if( lightCount == 0 ) {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2405
      "    frontColor = col + vec4(mat_specular,1.0);" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2406
      "  }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2407
      "  else {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2408
           // WebGL forces us to iterate over a constant value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2409
           // so we can't iterate using lightCount
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2410
      "    for( int i = 0; i < 8; i++ ) {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2411
      "      Light l = getLight(i);" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2412
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2413
      // We can stop iterating if we know we have gone past
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2414
      // the number of lights which are on
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2415
      "      if( i >= lightCount ){" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2416
      "        break;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2417
      "      }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2418
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2419
      "      if( l.type == 0 ) {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2420
      "        AmbientLight( finalAmbient, ecPos, l );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2421
      "      }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2422
      "      else if( l.type == 1 ) {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2423
      "        DirectionalLight( finalDiffuse, finalSpecular, norm, ecPos, l );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2424
      "      }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2425
      "      else if( l.type == 2 ) {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2426
      "        PointLight( finalDiffuse, finalSpecular, norm, ecPos, l );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2427
      "      }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2428
      "      else {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2429
      "        SpotLight( finalDiffuse, finalSpecular, norm, ecPos, l );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2430
      "      }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2431
      "    }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2432
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2433
      "   if( usingMat == false ) {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2434
      "     frontColor = vec4(" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2435
      "       vec3(col) * finalAmbient +" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2436
      "       vec3(col) * finalDiffuse +" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2437
      "       vec3(col) * finalSpecular," +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2438
      "       col[3] );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2439
      "   }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2440
      "   else{" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2441
      "     frontColor = vec4( " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2442
      "       mat_emissive + " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2443
      "       (vec3(col) * mat_ambient * finalAmbient) + " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2444
      "       (vec3(col) * finalDiffuse) + " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2445
      "       (mat_specular * finalSpecular), " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2446
      "       col[3] );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2447
      "    }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2448
      "  }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2449
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2450
      "  vTexture.xy = aTexture.xy;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2451
      "  gl_Position = projection * view * model * vec4( Vertex, 1.0 );" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2452
      "}";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2453
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2454
    var fragmentShaderSource3D =
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2455
      "#ifdef GL_ES\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2456
      "precision highp float;\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2457
      "#endif\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2458
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2459
      "varying vec4 frontColor;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2460
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2461
      "uniform sampler2D sampler;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2462
      "uniform bool usingTexture;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2463
      "varying vec2 vTexture;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2464
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2465
      // In Processing, when a texture is used, the fill color is ignored
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2466
      // vec4(1.0,1.0,1.0,0.5)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2467
      "void main(void){" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2468
      "  if(usingTexture){" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2469
      "    gl_FragColor = vec4(texture2D(sampler, vTexture.xy)) * frontColor;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2470
      "  }"+
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2471
      "  else{" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2472
      "    gl_FragColor = frontColor;" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2473
      "  }" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2474
      "}";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2475
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2476
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2477
    // 3D Functions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2478
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2479
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2480
    /*
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2481
     * Sets a uniform variable in a program object to a particular
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2482
     * value. Before calling this function, ensure the correct
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2483
     * program object has been installed as part of the current
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2484
     * rendering state by calling useProgram.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2485
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2486
     * On some systems, if the variable exists in the shader but isn't used,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2487
     * the compiler will optimize it out and this function will fail.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2488
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2489
     * @param {WebGLProgram} programObj program object returned from
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2490
     * createProgramObject
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2491
     * @param {String} varName the name of the variable in the shader
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2492
     * @param {float | Array} varValue either a scalar value or an Array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2493
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2494
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2495
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2496
     * @see uniformi
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2497
     * @see uniformMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2498
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2499
    function uniformf(cacheId, programObj, varName, varValue) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2500
      var varLocation = curContextCache.locations[cacheId];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2501
      if(varLocation === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2502
        varLocation = curContext.getUniformLocation(programObj, varName);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2503
        curContextCache.locations[cacheId] = varLocation;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2504
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2505
      // the variable won't be found if it was optimized out.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2506
      if (varLocation !== null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2507
        if (varValue.length === 4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2508
          curContext.uniform4fv(varLocation, varValue);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2509
        } else if (varValue.length === 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2510
          curContext.uniform3fv(varLocation, varValue);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2511
        } else if (varValue.length === 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2512
          curContext.uniform2fv(varLocation, varValue);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2513
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2514
          curContext.uniform1f(varLocation, varValue);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2515
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2516
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2517
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2518
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2519
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2520
     * Sets a uniform int or int array in a program object to a particular
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2521
     * value. Before calling this function, ensure the correct
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2522
     * program object has been installed as part of the current
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2523
     * rendering state.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2524
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2525
     * On some systems, if the variable exists in the shader but isn't used,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2526
     * the compiler will optimize it out and this function will fail.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2527
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2528
     * @param {WebGLProgram} programObj program object returned from
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2529
     * createProgramObject
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2530
     * @param {String} varName the name of the variable in the shader
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2531
     * @param {int | Array} varValue either a scalar value or an Array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2532
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2533
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2534
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2535
     * @see uniformf
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2536
     * @see uniformMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2537
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2538
    function uniformi(cacheId, programObj, varName, varValue) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2539
      var varLocation = curContextCache.locations[cacheId];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2540
      if(varLocation === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2541
        varLocation = curContext.getUniformLocation(programObj, varName);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2542
        curContextCache.locations[cacheId] = varLocation;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2543
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2544
      // the variable won't be found if it was optimized out.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2545
      if (varLocation !== null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2546
        if (varValue.length === 4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2547
          curContext.uniform4iv(varLocation, varValue);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2548
        } else if (varValue.length === 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2549
          curContext.uniform3iv(varLocation, varValue);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2550
        } else if (varValue.length === 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2551
          curContext.uniform2iv(varLocation, varValue);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2552
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2553
          curContext.uniform1i(varLocation, varValue);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2554
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2555
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2556
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2557
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2558
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2559
     * Sets the value of a uniform matrix variable in a program
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2560
     * object. Before calling this function, ensure the correct
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2561
     * program object has been installed as part of the current
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2562
     * rendering state.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2563
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2564
     * On some systems, if the variable exists in the shader but
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2565
     * isn't used, the compiler will optimize it out and this
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2566
     * function will fail.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2567
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2568
     * @param {WebGLProgram} programObj program object returned from
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2569
     * createProgramObject
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2570
     * @param {String} varName the name of the variable in the shader
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2571
     * @param {boolean} transpose must be false
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2572
     * @param {Array} matrix an array of 4, 9 or 16 values
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2573
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2574
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2575
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2576
     * @see uniformi
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2577
     * @see uniformf
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2578
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2579
    function uniformMatrix(cacheId, programObj, varName, transpose, matrix) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2580
      var varLocation = curContextCache.locations[cacheId];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2581
      if(varLocation === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2582
        varLocation = curContext.getUniformLocation(programObj, varName);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2583
        curContextCache.locations[cacheId] = varLocation;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2584
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2585
      // the variable won't be found if it was optimized out.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2586
      if (varLocation !== -1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2587
        if (matrix.length === 16) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2588
          curContext.uniformMatrix4fv(varLocation, transpose, matrix);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2589
        } else if (matrix.length === 9) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2590
          curContext.uniformMatrix3fv(varLocation, transpose, matrix);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2591
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2592
          curContext.uniformMatrix2fv(varLocation, transpose, matrix);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2593
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2594
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2595
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2596
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2597
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2598
     * Binds the VBO, sets the vertex attribute data for the program
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2599
     * object and enables the attribute.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2600
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2601
     * On some systems, if the attribute exists in the shader but
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2602
     * isn't used, the compiler will optimize it out and this
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2603
     * function will fail.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2604
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2605
     * @param {WebGLProgram} programObj program object returned from
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2606
     * createProgramObject
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2607
     * @param {String} varName the name of the variable in the shader
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2608
     * @param {int} size the number of components per vertex attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2609
     * @param {WebGLBuffer} VBO Vertex Buffer Object
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2610
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2611
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2612
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2613
     * @see disableVertexAttribPointer
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2614
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2615
    function vertexAttribPointer(cacheId, programObj, varName, size, VBO) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2616
      var varLocation = curContextCache.attributes[cacheId];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2617
      if(varLocation === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2618
        varLocation = curContext.getAttribLocation(programObj, varName);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2619
        curContextCache.attributes[cacheId] = varLocation;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2620
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2621
      if (varLocation !== -1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2622
        curContext.bindBuffer(curContext.ARRAY_BUFFER, VBO);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2623
        curContext.vertexAttribPointer(varLocation, size, curContext.FLOAT, false, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2624
        curContext.enableVertexAttribArray(varLocation);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2625
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2626
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2627
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2628
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2629
     * Disables a program object attribute from being sent to WebGL.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2630
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2631
     * @param {WebGLProgram} programObj program object returned from
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2632
     * createProgramObject
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2633
     * @param {String} varName name of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2634
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2635
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2636
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2637
     * @see vertexAttribPointer
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2638
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2639
    function disableVertexAttribPointer(cacheId, programObj, varName){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2640
      var varLocation = curContextCache.attributes[cacheId];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2641
      if(varLocation === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2642
        varLocation = curContext.getAttribLocation(programObj, varName);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2643
        curContextCache.attributes[cacheId] = varLocation;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2644
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2645
      if (varLocation !== -1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2646
        curContext.disableVertexAttribArray(varLocation);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2647
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2648
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2649
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2650
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2651
     * Creates a WebGL program object.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2652
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2653
     * @param {String} vetexShaderSource
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2654
     * @param {String} fragmentShaderSource
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2655
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2656
     * @returns {WebGLProgram} A program object
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2657
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2658
    var createProgramObject = function(curContext, vetexShaderSource, fragmentShaderSource) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2659
      var vertexShaderObject = curContext.createShader(curContext.VERTEX_SHADER);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2660
      curContext.shaderSource(vertexShaderObject, vetexShaderSource);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2661
      curContext.compileShader(vertexShaderObject);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2662
      if (!curContext.getShaderParameter(vertexShaderObject, curContext.COMPILE_STATUS)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2663
        throw curContext.getShaderInfoLog(vertexShaderObject);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2664
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2665
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2666
      var fragmentShaderObject = curContext.createShader(curContext.FRAGMENT_SHADER);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2667
      curContext.shaderSource(fragmentShaderObject, fragmentShaderSource);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2668
      curContext.compileShader(fragmentShaderObject);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2669
      if (!curContext.getShaderParameter(fragmentShaderObject, curContext.COMPILE_STATUS)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2670
        throw curContext.getShaderInfoLog(fragmentShaderObject);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2671
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2672
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2673
      var programObject = curContext.createProgram();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2674
      curContext.attachShader(programObject, vertexShaderObject);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2675
      curContext.attachShader(programObject, fragmentShaderObject);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2676
      curContext.linkProgram(programObject);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2677
      if (!curContext.getProgramParameter(programObject, curContext.LINK_STATUS)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2678
        throw "Error linking shaders.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2679
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2680
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2681
      return programObject;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2682
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2683
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2684
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2685
    // 2D/3D drawing handling
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2686
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2687
    var imageModeCorner = function(x, y, w, h, whAreSizes) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2688
      return {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2689
        x: x,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2690
        y: y,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2691
        w: w,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2692
        h: h
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2693
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2694
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2695
    var imageModeConvert = imageModeCorner;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2696
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2697
    var imageModeCorners = function(x, y, w, h, whAreSizes) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2698
      return {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2699
        x: x,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2700
        y: y,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2701
        w: whAreSizes ? w : w - x,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2702
        h: whAreSizes ? h : h - y
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2703
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2704
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2705
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2706
    var imageModeCenter = function(x, y, w, h, whAreSizes) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2707
      return {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2708
        x: x - w / 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2709
        y: y - h / 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2710
        w: w,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2711
        h: h
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2712
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2713
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2714
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2715
    // Objects for shared, 2D and 3D contexts
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2716
    var DrawingShared = function(){};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2717
    var Drawing2D = function(){};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2718
    var Drawing3D = function(){};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2719
    var DrawingPre = function(){};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2720
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2721
    // Setup the prototype chain
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2722
    Drawing2D.prototype = new DrawingShared();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2723
    Drawing2D.prototype.constructor = Drawing2D;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2724
    Drawing3D.prototype = new DrawingShared();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2725
    Drawing3D.prototype.constructor = Drawing3D;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2726
    DrawingPre.prototype = new DrawingShared();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2727
    DrawingPre.prototype.constructor = DrawingPre;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2728
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2729
    // A no-op function for when the user calls 3D functions from a 2D sketch
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2730
    // We can change this to a throw or console.error() later if we want
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2731
    DrawingShared.prototype.a3DOnlyFunction = nop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2732
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2733
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2734
    // Char handling
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2735
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2736
    var charMap = {};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2737
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2738
    var Char = p.Character = function(chr) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2739
      if (typeof chr === 'string' && chr.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2740
        this.code = chr.charCodeAt(0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2741
      } else if (typeof chr === 'number') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2742
        this.code = chr;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2743
      } else if (chr instanceof Char) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2744
        this.code = chr;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2745
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2746
        this.code = NaN;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2747
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2748
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2749
      return (charMap[this.code] === undef) ? charMap[this.code] = this : charMap[this.code];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2750
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2751
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2752
    Char.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2753
      return String.fromCharCode(this.code);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2754
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2755
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2756
    Char.prototype.valueOf = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2757
      return this.code;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2758
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2759
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2760
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2761
     * Datatype for storing shapes. Processing can currently load and display SVG (Scalable Vector Graphics) shapes.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2762
     * Before a shape is used, it must be loaded with the <b>loadShape()</b> function. The <b>shape()</b> function is used to draw the shape to the display window.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2763
     * The <b>PShape</b> object contain a group of methods, linked below, that can operate on the shape data.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2764
     * <br><br>The <b>loadShape()</b> method supports SVG files created with Inkscape and Adobe Illustrator.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2765
     * It is not a full SVG implementation, but offers some straightforward support for handling vector data.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2766
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2767
     * @param {int} family the shape type, one of GROUP, PRIMITIVE, PATH, or GEOMETRY
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2768
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2769
     * @see #shape()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2770
     * @see #loadShape()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2771
     * @see #shapeMode()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2772
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2773
    var PShape = p.PShape = function(family) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2774
      this.family    = family || PConstants.GROUP;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2775
      this.visible   = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2776
      this.style     = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2777
      this.children  = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2778
      this.nameTable = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2779
      this.params    = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2780
      this.name      = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2781
      this.image     = null;  //type PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2782
      this.matrix    = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2783
      this.kind      = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2784
      this.close     = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2785
      this.width     = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2786
      this.height    = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2787
      this.parent    = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2788
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2789
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2790
      * PShape methods
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2791
      * missing: findChild(), apply(), contains(), findChild(), getPrimitive(), getParams(), getVertex() , getVertexCount(),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2792
      * getVertexCode() , getVertexCodes() , getVertexCodeCount(), getVertexX(), getVertexY(), getVertexZ()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2793
      */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2794
    PShape.prototype = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2795
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2796
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2797
       * The isVisible() function returns a boolean value "true" if the image is set to be visible, "false" if not. This is modified with the <b>setVisible()</b> parameter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2798
       * <br><br>The visibility of a shape is usually controlled by whatever program created the SVG file.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2799
       * For instance, this parameter is controlled by showing or hiding the shape in the layers palette in Adobe Illustrator.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2800
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2801
       * @return {boolean}  returns "true" if the image is set to be visible, "false" if not
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2802
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2803
      isVisible: function(){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2804
        return this.visible;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2805
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2806
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2807
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2808
       * The setVisible() function sets the shape to be visible or invisible. This is determined by the value of the <b>visible</b> parameter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2809
       * <br><br>The visibility of a shape is usually controlled by whatever program created the SVG file.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2810
       * For instance, this parameter is controlled by showing or hiding the shape in the layers palette in Adobe Illustrator.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2811
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2812
       * @param {boolean} visible "false" makes the shape invisible and "true" makes it visible
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2813
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2814
      setVisible: function (visible){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2815
        this.visible = visible;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2816
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2817
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2818
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2819
       * The disableStyle() function disables the shape's style data and uses Processing's current styles. Styles include attributes such as colors, stroke weight, and stroke joints.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2820
       * Overrides this shape's style information and uses PGraphics styles and colors. Identical to ignoreStyles(true). Also disables styles for all child shapes.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2821
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2822
      disableStyle: function(){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2823
        this.style = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2824
        for(var i = 0, j=this.children.length; i<j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2825
          this.children[i].disableStyle();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2826
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2827
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2828
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2829
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2830
       * The enableStyle() function enables the shape's style data and ignores Processing's current styles. Styles include attributes such as colors, stroke weight, and stroke joints.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2831
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2832
      enableStyle: function(){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2833
        this.style = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2834
        for(var i = 0, j=this.children.length; i<j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2835
          this.children[i].enableStyle();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2836
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2837
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2838
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2839
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2840
       * The getFamily function returns the shape type
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2841
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2842
       * @return {int} the shape type, one of GROUP, PRIMITIVE, PATH, or GEOMETRY
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2843
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2844
      getFamily: function(){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2845
        return this.family;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2846
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2847
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2848
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2849
       * The getWidth() function gets the width of the drawing area (not necessarily the shape boundary).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2850
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2851
      getWidth: function(){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2852
        return this.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2853
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2854
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2855
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2856
       * The getHeight() function gets the height of the drawing area (not necessarily the shape boundary).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2857
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2858
      getHeight: function(){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2859
        return this.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2860
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2861
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2862
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2863
       * The setName() function sets the name of the shape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2864
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2865
       * @param {String} name the name of the shape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2866
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2867
      setName: function(name){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2868
        this.name = name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2869
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2870
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2871
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2872
       * The getName() function returns the name of the shape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2873
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2874
       * @return {String} the name of the shape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2875
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2876
      getName: function(){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2877
        return this.name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2878
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2879
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2880
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2881
       * Called by the following (the shape() command adds the g)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2882
       * PShape s = loadShapes("blah.svg");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2883
       * shape(s);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2884
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2885
      draw: function(){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2886
        if (this.visible) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2887
          this.pre();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2888
          this.drawImpl();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2889
          this.post();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2890
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2891
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2892
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2893
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2894
       * the drawImpl() function draws the SVG document.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2895
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2896
      drawImpl: function(){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2897
        if (this.family === PConstants.GROUP) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2898
          this.drawGroup();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2899
        } else if (this.family === PConstants.PRIMITIVE) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2900
          this.drawPrimitive();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2901
        } else if (this.family === PConstants.GEOMETRY) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2902
          this.drawGeometry();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2903
        } else if (this.family === PConstants.PATH) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2904
          this.drawPath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2905
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2906
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2907
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2908
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2909
       * The drawPath() function draws the <path> part of the SVG document.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2910
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2911
      drawPath: function(){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2912
        var i, j;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2913
        if (this.vertices.length === 0) { return; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2914
        p.beginShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2915
        if (this.vertexCodes.length === 0) {  // each point is a simple vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2916
          if (this.vertices[0].length === 2) {  // drawing 2D vertices
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2917
            for (i = 0, j = this.vertices.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2918
              p.vertex(this.vertices[i][0], this.vertices[i][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2919
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2920
          } else {  // drawing 3D vertices
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2921
            for (i = 0, j = this.vertices.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2922
              p.vertex(this.vertices[i][0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2923
                       this.vertices[i][1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2924
                       this.vertices[i][2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2925
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2926
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2927
        } else {  // coded set of vertices
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2928
          var index = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2929
          if (this.vertices[0].length === 2) {  // drawing a 2D path
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2930
            for (i = 0, j = this.vertexCodes.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2931
              if (this.vertexCodes[i] === PConstants.VERTEX) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2932
                p.vertex(this.vertices[index][0], this.vertices[index][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2933
                if ( this.vertices[index]["moveTo"] === true) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2934
                  vertArray[vertArray.length-1]["moveTo"] = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2935
                } else if ( this.vertices[index]["moveTo"] === false) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2936
                  vertArray[vertArray.length-1]["moveTo"] = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2937
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2938
                p.breakShape = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2939
                index++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2940
              } else if (this.vertexCodes[i] === PConstants.BEZIER_VERTEX) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2941
                p.bezierVertex(this.vertices[index+0][0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2942
                               this.vertices[index+0][1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2943
                               this.vertices[index+1][0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2944
                               this.vertices[index+1][1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2945
                               this.vertices[index+2][0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2946
                               this.vertices[index+2][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2947
                index += 3;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2948
              } else if (this.vertexCodes[i] === PConstants.CURVE_VERTEX) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2949
                p.curveVertex(this.vertices[index][0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2950
                              this.vertices[index][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2951
                index++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2952
              } else if (this.vertexCodes[i] ===  PConstants.BREAK) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2953
                p.breakShape = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2954
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2955
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2956
          } else {  // drawing a 3D path
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2957
            for (i = 0, j = this.vertexCodes.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2958
              if (this.vertexCodes[i] === PConstants.VERTEX) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2959
                p.vertex(this.vertices[index][0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2960
                         this.vertices[index][1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2961
                         this.vertices[index][2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2962
                if (this.vertices[index]["moveTo"] === true) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2963
                  vertArray[vertArray.length-1]["moveTo"] = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2964
                } else if (this.vertices[index]["moveTo"] === false) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2965
                  vertArray[vertArray.length-1]["moveTo"] = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2966
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2967
                p.breakShape = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2968
              } else if (this.vertexCodes[i] ===  PConstants.BEZIER_VERTEX) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2969
                p.bezierVertex(this.vertices[index+0][0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2970
                               this.vertices[index+0][1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2971
                               this.vertices[index+0][2],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2972
                               this.vertices[index+1][0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2973
                               this.vertices[index+1][1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2974
                               this.vertices[index+1][2],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2975
                               this.vertices[index+2][0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2976
                               this.vertices[index+2][1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2977
                               this.vertices[index+2][2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2978
                index += 3;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2979
              } else if (this.vertexCodes[i] === PConstants.CURVE_VERTEX) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2980
                p.curveVertex(this.vertices[index][0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2981
                              this.vertices[index][1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2982
                              this.vertices[index][2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2983
                index++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2984
              } else if (this.vertexCodes[i] === PConstants.BREAK) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2985
                p.breakShape = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2986
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2987
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2988
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2989
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2990
        p.endShape(this.close ? PConstants.CLOSE : PConstants.OPEN);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2991
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2992
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2993
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2994
       * The drawGeometry() function draws the geometry part of the SVG document.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2995
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2996
      drawGeometry: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2997
        var i, j;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2998
        p.beginShape(this.kind);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  2999
        if (this.style) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3000
          for (i = 0, j = this.vertices.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3001
            p.vertex(this.vertices[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3002
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3003
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3004
          for (i = 0, j = this.vertices.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3005
            var vert = this.vertices[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3006
            if (vert[2] === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3007
              p.vertex(vert[0], vert[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3008
            } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3009
              p.vertex(vert[0], vert[1], vert[2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3010
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3011
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3012
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3013
        p.endShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3014
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3015
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3016
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3017
       * The drawGroup() function draws the <g> part of the SVG document.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3018
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3019
      drawGroup: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3020
        for (var i = 0, j = this.children.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3021
          this.children[i].draw();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3022
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3023
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3024
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3025
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3026
       * The drawPrimitive() function draws SVG document shape elements. These can be point, line, triangle, quad, rect, ellipse, arc, box, or sphere.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3027
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3028
      drawPrimitive: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3029
        if (this.kind === PConstants.POINT) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3030
          p.point(this.params[0], this.params[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3031
        } else if (this.kind === PConstants.LINE) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3032
          if (this.params.length === 4) {  // 2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3033
            p.line(this.params[0], this.params[1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3034
                   this.params[2], this.params[3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3035
          } else {  // 3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3036
            p.line(this.params[0], this.params[1], this.params[2],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3037
                   this.params[3], this.params[4], this.params[5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3038
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3039
        } else if (this.kind === PConstants.TRIANGLE) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3040
          p.triangle(this.params[0], this.params[1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3041
                     this.params[2], this.params[3],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3042
                     this.params[4], this.params[5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3043
        } else if (this.kind === PConstants.QUAD) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3044
          p.quad(this.params[0], this.params[1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3045
                 this.params[2], this.params[3],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3046
                 this.params[4], this.params[5],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3047
                 this.params[6], this.params[7]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3048
        } else if (this.kind === PConstants.RECT) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3049
          if (this.image !== null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3050
            p.imageMode(PConstants.CORNER);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3051
            p.image(this.image,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3052
                    this.params[0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3053
                    this.params[1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3054
                    this.params[2],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3055
                    this.params[3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3056
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3057
            p.rectMode(PConstants.CORNER);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3058
            p.rect(this.params[0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3059
                   this.params[1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3060
                   this.params[2],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3061
                   this.params[3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3062
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3063
        } else if (this.kind === PConstants.ELLIPSE) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3064
          p.ellipseMode(PConstants.CORNER);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3065
          p.ellipse(this.params[0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3066
                    this.params[1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3067
                    this.params[2],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3068
                    this.params[3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3069
        } else if (this.kind === PConstants.ARC) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3070
          p.ellipseMode(PConstants.CORNER);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3071
          p.arc(this.params[0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3072
                this.params[1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3073
                this.params[2],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3074
                this.params[3],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3075
                this.params[4],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3076
                this.params[5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3077
        } else if (this.kind === PConstants.BOX) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3078
          if (this.params.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3079
            p.box(this.params[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3080
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3081
            p.box(this.params[0], this.params[1], this.params[2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3082
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3083
        } else if (this.kind === PConstants.SPHERE) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3084
          p.sphere(this.params[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3085
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3086
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3087
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3088
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3089
       * The pre() function performs the preparations before the SVG is drawn. This includes doing transformations and storing previous styles.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3090
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3091
      pre: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3092
        if (this.matrix) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3093
          p.pushMatrix();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3094
          curContext.transform(this.matrix.elements[0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3095
                               this.matrix.elements[3],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3096
                               this.matrix.elements[1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3097
                               this.matrix.elements[4],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3098
                               this.matrix.elements[2],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3099
                               this.matrix.elements[5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3100
          //p.applyMatrix(this.matrix.elements[0],this.matrix.elements[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3101
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3102
        if (this.style) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3103
          p.pushStyle();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3104
          this.styles();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3105
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3106
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3107
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3108
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3109
       * The post() function performs the necessary actions after the SVG is drawn. This includes removing transformations and removing added styles.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3110
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3111
      post: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3112
        if (this.matrix) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3113
          p.popMatrix();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3114
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3115
        if (this.style) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3116
          p.popStyle();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3117
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3118
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3119
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3120
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3121
       * The styles() function changes the Processing's current styles
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3122
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3123
      styles: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3124
        if (this.stroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3125
          p.stroke(this.strokeColor);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3126
          p.strokeWeight(this.strokeWeight);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3127
          p.strokeCap(this.strokeCap);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3128
          p.strokeJoin(this.strokeJoin);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3129
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3130
          p.noStroke();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3131
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3132
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3133
        if (this.fill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3134
          p.fill(this.fillColor);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3135
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3136
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3137
          p.noFill();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3138
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3139
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3140
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3141
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3142
       * The getChild() function extracts a child shape from a parent shape. Specify the name of the shape with the <b>target</b> parameter or the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3143
       * layer position of the shape to get with the <b>index</b> parameter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3144
       * The shape is returned as a <b>PShape</b> object, or <b>null</b> is returned if there is an error.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3145
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3146
       * @param {String} target   the name of the shape to get
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3147
       * @param {int} index   the layer position of the shape to get
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3148
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3149
       * @return {PShape} returns a child element of a shape as a PShape object or null if there is an error
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3150
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3151
      getChild: function(child) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3152
        var i, j;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3153
        if (typeof child === 'number') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3154
          return this.children[child];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3155
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3156
        var found;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3157
        if(child === "" || this.name === child){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3158
          return this;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3159
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3160
        if(this.nameTable.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3161
          for(i = 0, j = this.nameTable.length; i < j || found; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3162
            if(this.nameTable[i].getName === child) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3163
              found = this.nameTable[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3164
              break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3165
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3166
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3167
          if (found) { return found; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3168
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3169
        for(i = 0, j = this.children.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3170
          found = this.children[i].getChild(child);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3171
          if(found) { return found; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3172
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3173
        return null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3174
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3175
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3176
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3177
       * The getChildCount() returns the number of children
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3178
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3179
       * @return {int} returns a count of children
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3180
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3181
      getChildCount: function () {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3182
        return this.children.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3183
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3184
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3185
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3186
       * The addChild() adds a child to the PShape.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3187
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3188
       * @param {PShape} child the child to add
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3189
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3190
      addChild: function( child ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3191
        this.children.push(child);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3192
        child.parent = this;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3193
        if (child.getName() !== null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3194
          this.addName(child.getName(), child);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3195
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3196
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3197
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3198
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3199
       * The addName() functions adds a shape to the name lookup table.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3200
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3201
       * @param {String} name   the name to be added
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3202
       * @param {PShape} shape  the shape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3203
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3204
      addName: function(name,  shape) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3205
        if (this.parent !== null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3206
          this.parent.addName( name, shape );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3207
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3208
          this.nameTable.push( [name, shape] );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3209
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3210
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3211
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3212
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3213
       * The translate() function specifies an amount to displace the shape. The <b>x</b> parameter specifies left/right translation, the <b>y</b> parameter specifies up/down translation, and the <b>z</b> parameter specifies translations toward/away from the screen.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3214
       * Subsequent calls to the method accumulates the effect. For example, calling <b>translate(50, 0)</b> and then <b>translate(20, 0)</b> is the same as <b>translate(70, 0)</b>.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3215
       * This transformation is applied directly to the shape, it's not refreshed each time <b>draw()</b> is run.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3216
       * <br><br>Using this method with the <b>z</b> parameter requires using the P3D or OPENGL parameter in combination with size.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3217
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3218
       * @param {int|float} x left/right translation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3219
       * @param {int|float} y up/down translation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3220
       * @param {int|float} z forward/back translation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3221
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3222
       * @see PMatrix2D#translate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3223
       * @see PMatrix3D#translate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3224
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3225
      translate: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3226
        if(arguments.length === 2)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3227
        {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3228
          this.checkMatrix(2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3229
          this.matrix.translate(arguments[0], arguments[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3230
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3231
          this.checkMatrix(3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3232
          this.matrix.translate(arguments[0], arguments[1], 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3233
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3234
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3235
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3236
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3237
       * The checkMatrix() function makes sure that the shape's matrix is 1) not null, and 2) has a matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3238
       * that can handle <em>at least</em> the specified number of dimensions.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3239
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3240
       * @param {int} dimensions the specified number of dimensions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3241
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3242
      checkMatrix: function(dimensions) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3243
        if(this.matrix === null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3244
          if(dimensions === 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3245
            this.matrix = new p.PMatrix2D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3246
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3247
            this.matrix = new p.PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3248
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3249
        }else if(dimensions === 3 && this.matrix instanceof p.PMatrix2D) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3250
          this.matrix = new p.PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3251
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3252
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3253
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3254
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3255
       * The rotateX() function rotates a shape around the x-axis the amount specified by the <b>angle</b> parameter. Angles should be specified in radians (values from 0 to TWO_PI) or converted to radians with the <b>radians()</b> method.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3256
       * <br><br>Shapes are always rotated around the upper-left corner of their bounding box. Positive numbers rotate objects in a clockwise direction.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3257
       * Subsequent calls to the method accumulates the effect. For example, calling <b>rotateX(HALF_PI)</b> and then <b>rotateX(HALF_PI)</b> is the same as <b>rotateX(PI)</b>.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3258
       * This transformation is applied directly to the shape, it's not refreshed each time <b>draw()</b> is run.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3259
       * <br><br>This method requires a 3D renderer. You need to pass P3D or OPENGL as a third parameter into the <b>size()</b> method as shown in the example above.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3260
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3261
       * @param {float}angle angle of rotation specified in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3262
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3263
       * @see PMatrix3D#rotateX
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3264
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3265
      rotateX: function(angle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3266
        this.rotate(angle, 1, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3267
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3268
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3269
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3270
       * The rotateY() function rotates a shape around the y-axis the amount specified by the <b>angle</b> parameter. Angles should be specified in radians (values from 0 to TWO_PI) or converted to radians with the <b>radians()</b> method.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3271
       * <br><br>Shapes are always rotated around the upper-left corner of their bounding box. Positive numbers rotate objects in a clockwise direction.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3272
       * Subsequent calls to the method accumulates the effect. For example, calling <b>rotateY(HALF_PI)</b> and then <b>rotateY(HALF_PI)</b> is the same as <b>rotateY(PI)</b>.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3273
       * This transformation is applied directly to the shape, it's not refreshed each time <b>draw()</b> is run.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3274
       * <br><br>This method requires a 3D renderer. You need to pass P3D or OPENGL as a third parameter into the <b>size()</b> method as shown in the example above.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3275
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3276
       * @param {float}angle angle of rotation specified in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3277
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3278
       * @see PMatrix3D#rotateY
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3279
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3280
      rotateY: function(angle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3281
        this.rotate(angle, 0, 1, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3282
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3283
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3284
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3285
       * The rotateZ() function rotates a shape around the z-axis the amount specified by the <b>angle</b> parameter. Angles should be specified in radians (values from 0 to TWO_PI) or converted to radians with the <b>radians()</b> method.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3286
       * <br><br>Shapes are always rotated around the upper-left corner of their bounding box. Positive numbers rotate objects in a clockwise direction.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3287
       * Subsequent calls to the method accumulates the effect. For example, calling <b>rotateZ(HALF_PI)</b> and then <b>rotateZ(HALF_PI)</b> is the same as <b>rotateZ(PI)</b>.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3288
       * This transformation is applied directly to the shape, it's not refreshed each time <b>draw()</b> is run.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3289
       * <br><br>This method requires a 3D renderer. You need to pass P3D or OPENGL as a third parameter into the <b>size()</b> method as shown in the example above.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3290
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3291
       * @param {float}angle angle of rotation specified in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3292
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3293
       * @see PMatrix3D#rotateZ
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3294
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3295
      rotateZ: function(angle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3296
        this.rotate(angle, 0, 0, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3297
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3298
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3299
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3300
       * The rotate() function rotates a shape the amount specified by the <b>angle</b> parameter. Angles should be specified in radians (values from 0 to TWO_PI) or converted to radians with the <b>radians()</b> method.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3301
       * <br><br>Shapes are always rotated around the upper-left corner of their bounding box. Positive numbers rotate objects in a clockwise direction.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3302
       * Transformations apply to everything that happens after and subsequent calls to the method accumulates the effect.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3303
       * For example, calling <b>rotate(HALF_PI)</b> and then <b>rotate(HALF_PI)</b> is the same as <b>rotate(PI)</b>.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3304
       * This transformation is applied directly to the shape, it's not refreshed each time <b>draw()</b> is run.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3305
       * If optional parameters x,y,z are supplied, the rotate is about the point (x, y, z).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3306
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3307
       * @param {float}angle  angle of rotation specified in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3308
       * @param {float}x      x-coordinate of the point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3309
       * @param {float}y      y-coordinate of the point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3310
       * @param {float}z      z-coordinate of the point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3311
       * @see PMatrix2D#rotate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3312
       * @see PMatrix3D#rotate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3313
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3314
      rotate: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3315
        if(arguments.length === 1){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3316
          this.checkMatrix(2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3317
          this.matrix.rotate(arguments[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3318
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3319
          this.checkMatrix(3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3320
          this.matrix.rotate(arguments[0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3321
                             arguments[1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3322
                             arguments[2],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3323
                             arguments[3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3324
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3325
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3326
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3327
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3328
       * The scale() function increases or decreases the size of a shape by expanding and contracting vertices. Shapes always scale from the relative origin of their bounding box.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3329
       * Scale values are specified as decimal percentages. For example, the method call <b>scale(2.0)</b> increases the dimension of a shape by 200%.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3330
       * Subsequent calls to the method multiply the effect. For example, calling <b>scale(2.0)</b> and then <b>scale(1.5)</b> is the same as <b>scale(3.0)</b>.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3331
       * This transformation is applied directly to the shape, it's not refreshed each time <b>draw()</b> is run.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3332
       * <br><br>Using this fuction with the <b>z</b> parameter requires passing P3D or OPENGL into the size() parameter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3333
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3334
       * @param {float}s      percentage to scale the object
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3335
       * @param {float}x      percentage to scale the object in the x-axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3336
       * @param {float}y      percentage to scale the object in the y-axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3337
       * @param {float}z      percentage to scale the object in the z-axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3338
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3339
       * @see PMatrix2D#scale
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3340
       * @see PMatrix3D#scale
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3341
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3342
      scale: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3343
        if(arguments.length === 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3344
          this.checkMatrix(2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3345
          this.matrix.scale(arguments[0], arguments[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3346
        } else if (arguments.length === 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3347
          this.checkMatrix(2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3348
          this.matrix.scale(arguments[0], arguments[1], arguments[2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3349
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3350
          this.checkMatrix(2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3351
          this.matrix.scale(arguments[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3352
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3353
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3354
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3355
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3356
       * The resetMatrix() function resets the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3357
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3358
       * @see PMatrix2D#reset
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3359
       * @see PMatrix3D#reset
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3360
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3361
      resetMatrix: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3362
        this.checkMatrix(2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3363
        this.matrix.reset();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3364
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3365
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3366
       * @member PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3367
       * The applyMatrix() function multiplies this matrix by another matrix of type PMatrix3D or PMatrix2D.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3368
       * Individual elements can also be provided
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3369
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3370
       * @param {PMatrix3D|PMatrix2D} matrix   the matrix to multiply by
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3371
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3372
       * @see PMatrix2D#apply
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3373
       * @see PMatrix3D#apply
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3374
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3375
      applyMatrix: function(matrix) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3376
        if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3377
          this.applyMatrix(matrix.elements[0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3378
                           matrix.elements[1], 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3379
                           matrix.elements[2],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3380
                           matrix.elements[3],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3381
                           matrix.elements[4], 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3382
                           matrix.elements[5],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3383
                           0, 0, 1, 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3384
                           0, 0, 0, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3385
        } else if (arguments.length === 6) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3386
          this.checkMatrix(2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3387
          this.matrix.apply(arguments[0], arguments[1], arguments[2], 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3388
                            arguments[3], arguments[4], arguments[5], 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3389
                            0,   0,   1,   0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3390
                            0,   0,   0,   1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3391
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3392
        } else if (arguments.length === 16) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3393
          this.checkMatrix(3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3394
          this.matrix.apply(arguments[0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3395
                            arguments[1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3396
                            arguments[2],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3397
                            arguments[3],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3398
                            arguments[4],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3399
                            arguments[5],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3400
                            arguments[6],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3401
                            arguments[7],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3402
                            arguments[8],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3403
                            arguments[9],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3404
                            arguments[10],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3405
                            arguments[11],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3406
                            arguments[12],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3407
                            arguments[13],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3408
                            arguments[14],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3409
                            arguments[15]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3410
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3411
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3412
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3413
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3414
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3415
     * SVG stands for Scalable Vector Graphics, a portable graphics format. It is
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3416
     * a vector format so it allows for infinite resolution and relatively small
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3417
     * file sizes. Most modern media software can view SVG files, including Adobe
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3418
     * products, Firefox, etc. Illustrator and Inkscape can edit SVG files.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3419
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3420
     * @param {PApplet} parent     typically use "this"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3421
     * @param {String} filename    name of the SVG file to load
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3422
     * @param {XMLElement} xml     an XMLElement element
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3423
     * @param {PShapeSVG} parent   the parent PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3424
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3425
     * @see PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3426
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3427
    var PShapeSVG = p.PShapeSVG = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3428
      p.PShape.call( this ); // PShape is the base class.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3429
      if (arguments.length === 1) { //xml element coming in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3430
        this.element  = arguments[0] ;//new p.XMLElement(null, arguments[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3431
        // set values to their defaults according to the SVG spec
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3432
        this.vertexCodes         = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3433
        this.vertices            = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3434
        this.opacity             = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3435
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3436
        this.stroke              = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3437
        this.strokeColor         = PConstants.ALPHA_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3438
        this.strokeWeight        = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3439
        this.strokeCap           = PConstants.SQUARE;  // BUTT in svg spec
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3440
        this.strokeJoin          = PConstants.MITER;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3441
        this.strokeGradient      = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3442
        this.strokeGradientPaint = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3443
        this.strokeName          = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3444
        this.strokeOpacity       = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3445
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3446
        this.fill                = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3447
        this.fillColor           = PConstants.ALPHA_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3448
        this.fillGradient        = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3449
        this.fillGradientPaint   = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3450
        this.fillName            = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3451
        this.fillOpacity         = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3452
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3453
        if (this.element.getName() !== "svg") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3454
          throw("root is not <svg>, it's <" + this.element.getName() + ">");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3455
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3456
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3457
      else if (arguments.length === 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3458
        if (typeof arguments[1] === 'string') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3459
          if (arguments[1].indexOf(".svg") > -1) { //its a filename
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3460
            this.element = new p.XMLElement(null, arguments[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3461
            // set values to their defaults according to the SVG spec
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3462
            this.vertexCodes         = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3463
            this.vertices            = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3464
            this.opacity             = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3465
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3466
            this.stroke              = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3467
            this.strokeColor         = PConstants.ALPHA_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3468
            this.strokeWeight        = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3469
            this.strokeCap           = PConstants.SQUARE;  // BUTT in svg spec
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3470
            this.strokeJoin          = PConstants.MITER;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3471
            this.strokeGradient      = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3472
            this.strokeGradientPaint = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3473
            this.strokeName          = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3474
            this.strokeOpacity       = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3475
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3476
            this.fill                = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3477
            this.fillColor           = PConstants.ALPHA_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3478
            this.fillGradient        = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3479
            this.fillGradientPaint   = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3480
            this.fillOpacity         = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3481
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3482
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3483
        } else { // XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3484
          if (arguments[0]) { // PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3485
            this.element             = arguments[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3486
            this.vertexCodes         = arguments[0].vertexCodes.slice();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3487
            this.vertices            = arguments[0].vertices.slice();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3488
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3489
            this.stroke              = arguments[0].stroke;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3490
            this.strokeColor         = arguments[0].strokeColor;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3491
            this.strokeWeight        = arguments[0].strokeWeight;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3492
            this.strokeCap           = arguments[0].strokeCap;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3493
            this.strokeJoin          = arguments[0].strokeJoin;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3494
            this.strokeGradient      = arguments[0].strokeGradient;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3495
            this.strokeGradientPaint = arguments[0].strokeGradientPaint;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3496
            this.strokeName          = arguments[0].strokeName;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3497
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3498
            this.fill                = arguments[0].fill;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3499
            this.fillColor           = arguments[0].fillColor;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3500
            this.fillGradient        = arguments[0].fillGradient;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3501
            this.fillGradientPaint   = arguments[0].fillGradientPaint;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3502
            this.fillName            = arguments[0].fillName;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3503
            this.strokeOpacity       = arguments[0].strokeOpacity;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3504
            this.fillOpacity         = arguments[0].fillOpacity;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3505
            this.opacity             = arguments[0].opacity;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3506
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3507
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3508
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3509
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3510
      this.name      = this.element.getStringAttribute("id");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3511
      var displayStr = this.element.getStringAttribute("display", "inline");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3512
      this.visible   = displayStr !== "none";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3513
      var str = this.element.getAttribute("transform");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3514
      if (str) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3515
        this.matrix = this.parseMatrix(str);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3516
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3517
      // not proper parsing of the viewBox, but will cover us for cases where
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3518
      // the width and height of the object is not specified
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3519
      var viewBoxStr = this.element.getStringAttribute("viewBox");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3520
      if ( viewBoxStr !== null ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3521
        var viewBox = viewBoxStr.split(" ");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3522
        this.width  = viewBox[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3523
        this.height = viewBox[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3524
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3525
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3526
      // TODO if viewbox is not same as width/height, then use it to scale
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3527
      // the original objects. for now, viewbox only used when width/height
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3528
      // are empty values (which by the spec means w/h of "100%"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3529
      var unitWidth  = this.element.getStringAttribute("width");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3530
      var unitHeight = this.element.getStringAttribute("height");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3531
      if (unitWidth !== null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3532
        this.width  = this.parseUnitSize(unitWidth);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3533
        this.height = this.parseUnitSize(unitHeight);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3534
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3535
        if ((this.width === 0) || (this.height === 0)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3536
          // For the spec, the default is 100% and 100%. For purposes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3537
          // here, insert a dummy value because this is prolly just a
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3538
          // font or something for which the w/h doesn't matter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3539
          this.width  = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3540
          this.height = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3541
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3542
          //show warning
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3543
          throw("The width and/or height is not " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3544
                "readable in the <svg> tag of this file.");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3545
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3546
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3547
      this.parseColors(this.element);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3548
      this.parseChildren(this.element);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3549
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3550
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3551
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3552
     * PShapeSVG methods
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3553
     * missing: getChild(), print(), parseStyleAttributes(), styles() - deals with strokeGradient and fillGradient
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3554
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3555
    PShapeSVG.prototype = new PShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3556
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3557
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3558
     * The parseMatrix() function parses the specified SVG matrix into a PMatrix2D. Note that PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3559
     * is rotated relative to the SVG definition, so parameters are rearranged
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3560
     * here. More about the transformation matrices in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3561
     * <a href="http://www.w3.org/TR/SVG/coords.html#TransformAttribute">this section</a>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3562
     * of the SVG documentation.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3563
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3564
     * @param {String} str text of the matrix param.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3565
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3566
     * @return {PMatrix2D} a PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3567
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3568
    PShapeSVG.prototype.parseMatrix = (function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3569
      function getCoords(s) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3570
        var m = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3571
        s.replace(/\((.*?)\)/, (function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3572
          return function(all, params) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3573
            // get the coordinates that can be separated by spaces or a comma
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3574
            m = params.replace(/,+/g, " ").split(/\s+/);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3575
          };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3576
        }()));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3577
        return m;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3578
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3579
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3580
      return function(str) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3581
        this.checkMatrix(2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3582
        var pieces = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3583
        str.replace(/\s*(\w+)\((.*?)\)/g, function(all) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3584
          // get a list of transform definitions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3585
          pieces.push(p.trim(all));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3586
        });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3587
        if (pieces.length === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3588
          return null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3589
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3590
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3591
        for (var i = 0, j = pieces.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3592
          var m = getCoords(pieces[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3593
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3594
          if (pieces[i].indexOf("matrix") !== -1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3595
            this.matrix.set(m[0], m[2], m[4], m[1], m[3], m[5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3596
          } else if (pieces[i].indexOf("translate") !== -1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3597
            var tx = m[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3598
            var ty = (m.length === 2) ? m[1] : 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3599
            this.matrix.translate(tx,ty);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3600
          } else if (pieces[i].indexOf("scale") !== -1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3601
            var sx = m[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3602
            var sy = (m.length === 2) ? m[1] : m[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3603
            this.matrix.scale(sx,sy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3604
          } else if (pieces[i].indexOf("rotate") !== -1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3605
            var angle = m[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3606
            if (m.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3607
              this.matrix.rotate(p.radians(angle));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3608
            } else if (m.length === 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3609
              this.matrix.translate(m[1], m[2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3610
              this.matrix.rotate(p.radians(m[0]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3611
              this.matrix.translate(-m[1], -m[2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3612
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3613
          } else if (pieces[i].indexOf("skewX") !== -1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3614
            this.matrix.skewX(parseFloat(m[0]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3615
          } else if (pieces[i].indexOf("skewY") !== -1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3616
            this.matrix.skewY(m[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3617
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3618
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3619
        return this.matrix;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3620
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3621
    }());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3622
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3623
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3624
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3625
     * The parseChildren() function parses the specified XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3626
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3627
     * @param {XMLElement}element the XMLElement to parse
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3628
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3629
    PShapeSVG.prototype.parseChildren = function(element) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3630
      var newelement = element.getChildren();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3631
      var children   = new p.PShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3632
      for (var i = 0, j = newelement.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3633
        var kid = this.parseChild(newelement[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3634
        if (kid) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3635
          children.addChild(kid);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3636
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3637
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3638
      this.children.push(children);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3639
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3640
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3641
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3642
     * The getName() function returns the name
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3643
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3644
     * @return {String} the name
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3645
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3646
    PShapeSVG.prototype.getName = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3647
      return this.name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3648
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3649
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3650
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3651
     * The parseChild() function parses a child XML element.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3652
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3653
     * @param {XMLElement} elem the element to parse
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3654
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3655
     * @return {PShape} the newly created PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3656
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3657
    PShapeSVG.prototype.parseChild = function( elem ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3658
      var name = elem.getName();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3659
      var shape;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3660
      if (name === "g") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3661
        shape = new PShapeSVG(this, elem);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3662
      } else if (name === "defs") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3663
        // generally this will contain gradient info, so may
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3664
        // as well just throw it into a group element for parsing
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3665
        shape = new PShapeSVG(this, elem);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3666
      } else if (name === "line") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3667
        shape = new PShapeSVG(this, elem);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3668
        shape.parseLine();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3669
      } else if (name === "circle") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3670
        shape = new PShapeSVG(this, elem);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3671
        shape.parseEllipse(true);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3672
      } else if (name === "ellipse") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3673
        shape = new PShapeSVG(this, elem);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3674
        shape.parseEllipse(false);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3675
      } else if (name === "rect") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3676
        shape = new PShapeSVG(this, elem);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3677
        shape.parseRect();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3678
      } else if (name === "polygon") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3679
        shape = new PShapeSVG(this, elem);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3680
        shape.parsePoly(true);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3681
      } else if (name === "polyline") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3682
        shape = new PShapeSVG(this, elem);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3683
        shape.parsePoly(false);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3684
      } else if (name === "path") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3685
        shape = new PShapeSVG(this, elem);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3686
        shape.parsePath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3687
      } else if (name === "radialGradient") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3688
        //return new RadialGradient(this, elem);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3689
        unimplemented('PShapeSVG.prototype.parseChild, name = radialGradient');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3690
      } else if (name === "linearGradient") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3691
        //return new LinearGradient(this, elem);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3692
        unimplemented('PShapeSVG.prototype.parseChild, name = linearGradient');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3693
      } else if (name === "text") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3694
        unimplemented('PShapeSVG.prototype.parseChild, name = text');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3695
      } else if (name === "filter") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3696
        unimplemented('PShapeSVG.prototype.parseChild, name = filter');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3697
      } else if (name === "mask") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3698
        unimplemented('PShapeSVG.prototype.parseChild, name = mask');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3699
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3700
        // ignoring
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3701
        nop();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3702
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3703
      return shape;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3704
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3705
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3706
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3707
     * The parsePath() function parses the <path> element of the svg file
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3708
     * A path is defined by including a path element which contains a d="(path data)" attribute, where the d attribute contains
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3709
     * the moveto, line, curve (both cubic and quadratic Beziers), arc and closepath instructions.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3710
     **/
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3711
    PShapeSVG.prototype.parsePath = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3712
      this.family = PConstants.PATH;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3713
      this.kind = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3714
      var pathDataChars = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3715
      var c;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3716
      //change multiple spaces and commas to single space
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3717
      var pathData = p.trim(this.element.getStringAttribute("d")
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3718
                            .replace(/[\s,]+/g,' '));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3719
      if (pathData === null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3720
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3721
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3722
      pathData = p.__toCharArray(pathData);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3723
      var cx     = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3724
          cy     = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3725
          ctrlX  = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3726
          ctrlY  = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3727
          ctrlX1 = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3728
          ctrlX2 = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3729
          ctrlY1 = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3730
          ctrlY2 = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3731
          endX   = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3732
          endY   = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3733
          ppx    = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3734
          ppy    = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3735
          px     = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3736
          py     = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3737
          i      = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3738
          valOf  = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3739
      var str = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3740
      var tmpArray =[];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3741
      var flag = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3742
      var lastInstruction;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3743
      var command;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3744
      var j, k;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3745
      while (i< pathData.length) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3746
        valOf = pathData[i].valueOf();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3747
        if ((valOf >= 65 && valOf <= 90) || (valOf >= 97 && valOf <= 122)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3748
          // if it's a letter
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3749
          // populate the tmpArray with coordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3750
          j = i;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3751
          i++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3752
          if (i < pathData.length) { // don't go over boundary of array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3753
            tmpArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3754
            valOf = pathData[i].valueOf();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3755
            while (!((valOf >= 65 && valOf <= 90) ||
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3756
                     (valOf >= 97 && valOf <= 100) ||
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3757
                     (valOf >= 102 && valOf <= 122))
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3758
                     && flag === false) { // if its NOT a letter
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3759
              if (valOf === 32) { //if its a space and the str isn't empty
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3760
                // sometimes you get a space after the letter
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3761
                if (str !== "") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3762
                  tmpArray.push(parseFloat(str));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3763
                  str = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3764
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3765
                i++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3766
              } else if (valOf === 45) { //if it's a -
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3767
                // allow for 'e' notation in numbers, e.g. 2.10e-9
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3768
                if (pathData[i-1].valueOf() === 101) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3769
                  str += pathData[i].toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3770
                  i++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3771
                } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3772
                  // sometimes no space separator after (ex: 104.535-16.322)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3773
                  if (str !== "") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3774
                    tmpArray.push(parseFloat(str));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3775
                  }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3776
                  str = pathData[i].toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3777
                  i++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3778
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3779
              } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3780
                str += pathData[i].toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3781
                i++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3782
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3783
              if (i === pathData.length) { // don't go over boundary of array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3784
                flag = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3785
              } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3786
                valOf = pathData[i].valueOf();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3787
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3788
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3789
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3790
          if (str !== "") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3791
            tmpArray.push(parseFloat(str));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3792
            str = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3793
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3794
          command = pathData[j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3795
          valOf = command.valueOf();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3796
          if (valOf === 77) {  // M - move to (absolute)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3797
            if (tmpArray.length >= 2 && tmpArray.length % 2 ===0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3798
              // need one+ pairs of co-ordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3799
              cx = tmpArray[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3800
              cy = tmpArray[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3801
              this.parsePathMoveto(cx, cy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3802
              if (tmpArray.length > 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3803
                for (j = 2, k = tmpArray.length; j < k; j+=2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3804
                  // absolute line to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3805
                  cx = tmpArray[j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3806
                  cy = tmpArray[j+1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3807
                  this.parsePathLineto(cx,cy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3808
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3809
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3810
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3811
          } else if (valOf === 109) {  // m - move to (relative)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3812
            if (tmpArray.length >= 2 && tmpArray.length % 2 === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3813
              // need one+ pairs of co-ordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3814
              cx += tmpArray[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3815
              cy += tmpArray[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3816
              this.parsePathMoveto(cx,cy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3817
              if (tmpArray.length > 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3818
                for (j = 2, k = tmpArray.length; j < k; j+=2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3819
                  // relative line to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3820
                  cx += tmpArray[j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3821
                  cy += tmpArray[j + 1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3822
                  this.parsePathLineto(cx,cy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3823
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3824
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3825
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3826
          } else if (valOf === 76) { // L - lineto (absolute)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3827
            if (tmpArray.length >= 2 && tmpArray.length % 2 === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3828
              // need one+ pairs of co-ordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3829
              for (j = 0, k = tmpArray.length; j < k; j+=2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3830
                cx = tmpArray[j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3831
                cy = tmpArray[j + 1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3832
                this.parsePathLineto(cx,cy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3833
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3834
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3835
          } else if (valOf === 108) { // l - lineto (relative)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3836
            if (tmpArray.length >= 2 && tmpArray.length % 2 === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3837
              // need one+ pairs of co-ordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3838
              for (j = 0, k = tmpArray.length; j < k; j+=2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3839
                cx += tmpArray[j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3840
                cy += tmpArray[j+1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3841
                this.parsePathLineto(cx,cy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3842
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3843
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3844
          } else if (valOf === 72) { // H - horizontal lineto (absolute)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3845
            for (j = 0, k = tmpArray.length; j < k; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3846
              // multiple x co-ordinates can be provided
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3847
              cx = tmpArray[j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3848
              this.parsePathLineto(cx, cy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3849
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3850
          } else if (valOf === 104) { // h - horizontal lineto (relative)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3851
            for (j = 0, k = tmpArray.length; j < k; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3852
              // multiple x co-ordinates can be provided
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3853
              cx += tmpArray[j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3854
              this.parsePathLineto(cx, cy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3855
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3856
          } else if (valOf === 86) { // V - vertical lineto (absolute)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3857
            for (j = 0, k = tmpArray.length; j < k; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3858
              // multiple y co-ordinates can be provided
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3859
              cy = tmpArray[j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3860
              this.parsePathLineto(cx, cy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3861
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3862
          } else if (valOf === 118) { // v - vertical lineto (relative)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3863
            for (j = 0, k = tmpArray.length; j < k; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3864
              // multiple y co-ordinates can be provided
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3865
              cy += tmpArray[j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3866
              this.parsePathLineto(cx, cy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3867
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3868
          } else if (valOf === 67) { // C - curve to (absolute)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3869
            if (tmpArray.length >= 6 && tmpArray.length % 6 === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3870
              // need one+ multiples of 6 co-ordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3871
              for (j = 0, k = tmpArray.length; j < k; j+=6) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3872
                ctrlX1 = tmpArray[j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3873
                ctrlY1 = tmpArray[j + 1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3874
                ctrlX2 = tmpArray[j + 2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3875
                ctrlY2 = tmpArray[j + 3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3876
                endX   = tmpArray[j + 4];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3877
                endY   = tmpArray[j + 5];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3878
                this.parsePathCurveto(ctrlX1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3879
                                      ctrlY1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3880
                                      ctrlX2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3881
                                      ctrlY2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3882
                                      endX,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3883
                                      endY);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3884
                cx = endX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3885
                cy = endY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3886
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3887
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3888
          } else if (valOf === 99) { // c - curve to (relative)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3889
            if (tmpArray.length >= 6 && tmpArray.length % 6 === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3890
              // need one+ multiples of 6 co-ordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3891
              for (j = 0, k = tmpArray.length; j < k; j+=6) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3892
                ctrlX1 = cx + tmpArray[j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3893
                ctrlY1 = cy + tmpArray[j + 1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3894
                ctrlX2 = cx + tmpArray[j + 2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3895
                ctrlY2 = cy + tmpArray[j + 3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3896
                endX   = cx + tmpArray[j + 4];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3897
                endY   = cy + tmpArray[j + 5];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3898
                this.parsePathCurveto(ctrlX1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3899
                                      ctrlY1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3900
                                      ctrlX2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3901
                                      ctrlY2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3902
                                      endX,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3903
                                      endY);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3904
                cx = endX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3905
                cy = endY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3906
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3907
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3908
          } else if (valOf === 83) { // S - curve to shorthand (absolute)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3909
            if (tmpArray.length >= 4 && tmpArray.length % 4 === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3910
              // need one+ multiples of 4 co-ordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3911
              for (j = 0, k = tmpArray.length; j < k; j+=4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3912
                if (lastInstruction.toLowerCase() ===  "c" ||
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3913
                    lastInstruction.toLowerCase() ===  "s") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3914
                  ppx    = this.vertices[ this.vertices.length-2 ][0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3915
                  ppy    = this.vertices[ this.vertices.length-2 ][1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3916
                  px     = this.vertices[ this.vertices.length-1 ][0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3917
                  py     = this.vertices[ this.vertices.length-1 ][1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3918
                  ctrlX1 = px + (px - ppx);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3919
                  ctrlY1 = py + (py - ppy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3920
                } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3921
                  //If there is no previous curve,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3922
                  //the current point will be used as the first control point.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3923
                  ctrlX1 = this.vertices[this.vertices.length-1][0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3924
                  ctrlY1 = this.vertices[this.vertices.length-1][1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3925
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3926
                ctrlX2 = tmpArray[j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3927
                ctrlY2 = tmpArray[j + 1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3928
                endX   = tmpArray[j + 2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3929
                endY   = tmpArray[j + 3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3930
                this.parsePathCurveto(ctrlX1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3931
                                      ctrlY1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3932
                                      ctrlX2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3933
                                      ctrlY2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3934
                                      endX,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3935
                                      endY);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3936
                cx = endX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3937
                cy = endY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3938
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3939
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3940
          } else if (valOf === 115) { // s - curve to shorthand (relative)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3941
            if (tmpArray.length >= 4 && tmpArray.length % 4 === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3942
              // need one+ multiples of 4 co-ordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3943
              for (j = 0, k = tmpArray.length; j < k; j+=4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3944
                if (lastInstruction.toLowerCase() ===  "c" ||
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3945
                    lastInstruction.toLowerCase() ===  "s") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3946
                  ppx    = this.vertices[this.vertices.length-2][0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3947
                  ppy    = this.vertices[this.vertices.length-2][1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3948
                  px     = this.vertices[this.vertices.length-1][0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3949
                  py     = this.vertices[this.vertices.length-1][1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3950
                  ctrlX1 = px + (px - ppx);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3951
                  ctrlY1 = py + (py - ppy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3952
                } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3953
                  //If there is no previous curve,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3954
                  //the current point will be used as the first control point.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3955
                  ctrlX1 = this.vertices[this.vertices.length-1][0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3956
                  ctrlY1 = this.vertices[this.vertices.length-1][1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3957
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3958
                ctrlX2 = cx + tmpArray[j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3959
                ctrlY2 = cy + tmpArray[j + 1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3960
                endX   = cx + tmpArray[j + 2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3961
                endY   = cy + tmpArray[j + 3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3962
                this.parsePathCurveto(ctrlX1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3963
                                      ctrlY1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3964
                                      ctrlX2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3965
                                      ctrlY2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3966
                                      endX,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3967
                                      endY);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3968
                cx = endX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3969
                cy = endY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3970
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3971
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3972
          } else if (valOf === 81) { // Q - quadratic curve to (absolute)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3973
            if (tmpArray.length >= 4 && tmpArray.length % 4 === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3974
              // need one+ multiples of 4 co-ordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3975
              for (j = 0, k = tmpArray.length; j < k; j+=4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3976
                ctrlX = tmpArray[j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3977
                ctrlY = tmpArray[j + 1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3978
                endX  = tmpArray[j + 2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3979
                endY  = tmpArray[j + 3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3980
                this.parsePathQuadto(cx, cy, ctrlX, ctrlY, endX, endY);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3981
                cx = endX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3982
                cy = endY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3983
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3984
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3985
          } else if (valOf === 113) { // q - quadratic curve to (relative)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3986
            if (tmpArray.length >= 4 && tmpArray.length % 4 === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3987
              // need one+ multiples of 4 co-ordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3988
              for (j = 0, k = tmpArray.length; j < k; j+=4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3989
                ctrlX = cx + tmpArray[j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3990
                ctrlY = cy + tmpArray[j + 1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3991
                endX  = cx + tmpArray[j + 2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3992
                endY  = cy + tmpArray[j + 3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3993
                this.parsePathQuadto(cx, cy, ctrlX, ctrlY, endX, endY);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3994
                cx = endX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3995
                cy = endY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3996
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3997
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3998
          } else if (valOf === 84) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  3999
            // T - quadratic curve to shorthand (absolute)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4000
            if (tmpArray.length >= 2 && tmpArray.length % 2 === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4001
              // need one+ pairs of co-ordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4002
              for (j = 0, k = tmpArray.length; j < k; j+=2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4003
                if (lastInstruction.toLowerCase() ===  "q" ||
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4004
                    lastInstruction.toLowerCase() ===  "t") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4005
                  ppx   = this.vertices[this.vertices.length-2][0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4006
                  ppy   = this.vertices[this.vertices.length-2][1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4007
                  px    = this.vertices[this.vertices.length-1][0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4008
                  py    = this.vertices[this.vertices.length-1][1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4009
                  ctrlX = px + (px - ppx);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4010
                  ctrlY = py + (py - ppy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4011
                } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4012
                  // If there is no previous command or if the previous command
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4013
                  // was not a Q, q, T or t, assume the control point is
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4014
                  // coincident with the current point.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4015
                  ctrlX = cx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4016
                  ctrlY = cy;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4017
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4018
                endX  = tmpArray[j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4019
                endY  = tmpArray[j + 1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4020
                this.parsePathQuadto(cx, cy, ctrlX, ctrlY, endX, endY);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4021
                cx = endX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4022
                cy = endY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4023
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4024
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4025
          } else if (valOf === 116) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4026
            // t - quadratic curve to shorthand (relative)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4027
            if (tmpArray.length >= 2 && tmpArray.length % 2 === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4028
              // need one+ pairs of co-ordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4029
              for (j = 0, k = tmpArray.length; j < k; j+=2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4030
                if (lastInstruction.toLowerCase() ===  "q" ||
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4031
                    lastInstruction.toLowerCase() ===  "t") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4032
                  ppx   = this.vertices[this.vertices.length-2][0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4033
                  ppy   = this.vertices[this.vertices.length-2][1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4034
                  px    = this.vertices[this.vertices.length-1][0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4035
                  py    = this.vertices[this.vertices.length-1][1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4036
                  ctrlX = px + (px - ppx);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4037
                  ctrlY = py + (py - ppy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4038
                } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4039
                  // If there is no previous command or if the previous command
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4040
                  // was not a Q, q, T or t, assume the control point is
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4041
                  // coincident with the current point.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4042
                  ctrlX = cx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4043
                  ctrlY = cy;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4044
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4045
                endX  = cx + tmpArray[j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4046
                endY  = cy + tmpArray[j + 1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4047
                this.parsePathQuadto(cx, cy, ctrlX, ctrlY, endX, endY);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4048
                cx = endX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4049
                cy = endY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4050
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4051
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4052
          } else if (valOf === 90 || valOf === 122) { // Z or z (these do the same thing)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4053
            this.close = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4054
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4055
          lastInstruction = command.toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4056
        } else { i++;}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4057
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4058
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4059
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4060
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4061
     * PShapeSVG.parsePath() helper function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4062
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4063
     * @see PShapeSVG#parsePath
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4064
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4065
    PShapeSVG.prototype.parsePathQuadto = function(x1, y1, cx, cy, x2, y2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4066
      if (this.vertices.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4067
        this.parsePathCode(PConstants.BEZIER_VERTEX);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4068
        // x1/y1 already covered by last moveto, lineto, or curveto
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4069
        this.parsePathVertex(x1 + ((cx-x1)*2/3), y1 + ((cy-y1)*2/3));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4070
        this.parsePathVertex(x2 + ((cx-x2)*2/3), y2 + ((cy-y2)*2/3));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4071
        this.parsePathVertex(x2, y2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4072
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4073
        throw ("Path must start with M/m");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4074
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4075
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4076
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4077
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4078
     * PShapeSVG.parsePath() helper function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4079
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4080
     * @see PShapeSVG#parsePath
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4081
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4082
    PShapeSVG.prototype.parsePathCurveto = function(x1,  y1, x2, y2, x3, y3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4083
      if (this.vertices.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4084
        this.parsePathCode(PConstants.BEZIER_VERTEX );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4085
        this.parsePathVertex(x1, y1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4086
        this.parsePathVertex(x2, y2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4087
        this.parsePathVertex(x3, y3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4088
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4089
        throw ("Path must start with M/m");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4090
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4091
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4092
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4093
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4094
     * PShapeSVG.parsePath() helper function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4095
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4096
     * @see PShapeSVG#parsePath
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4097
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4098
    PShapeSVG.prototype.parsePathLineto = function(px, py) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4099
      if (this.vertices.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4100
        this.parsePathCode(PConstants.VERTEX);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4101
        this.parsePathVertex(px, py);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4102
        // add property to distinguish between curContext.moveTo
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4103
        // or curContext.lineTo
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4104
        this.vertices[this.vertices.length-1]["moveTo"] = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4105
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4106
        throw ("Path must start with M/m");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4107
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4108
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4109
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4110
    PShapeSVG.prototype.parsePathMoveto = function(px, py) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4111
      if (this.vertices.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4112
        this.parsePathCode(PConstants.BREAK);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4113
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4114
      this.parsePathCode(PConstants.VERTEX);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4115
      this.parsePathVertex(px, py);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4116
      // add property to distinguish between curContext.moveTo
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4117
      // or curContext.lineTo
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4118
      this.vertices[this.vertices.length-1]["moveTo"] = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4119
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4120
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4121
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4122
     * PShapeSVG.parsePath() helper function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4123
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4124
     * @see PShapeSVG#parsePath
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4125
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4126
    PShapeSVG.prototype.parsePathVertex = function(x,  y) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4127
      var verts = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4128
      verts[0]  = x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4129
      verts[1]  = y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4130
      this.vertices.push(verts);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4131
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4132
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4133
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4134
     * PShapeSVG.parsePath() helper function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4135
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4136
     * @see PShapeSVG#parsePath
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4137
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4138
    PShapeSVG.prototype.parsePathCode = function(what) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4139
      this.vertexCodes.push(what);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4140
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4141
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4142
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4143
     * The parsePoly() function parses a polyline or polygon from an SVG file.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4144
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4145
     * @param {boolean}val true if shape is closed (polygon), false if not (polyline)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4146
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4147
    PShapeSVG.prototype.parsePoly = function(val) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4148
      this.family    = PConstants.PATH;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4149
      this.close     = val;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4150
      var pointsAttr = p.trim(this.element.getStringAttribute("points")
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4151
                              .replace(/[,\s]+/g,' '));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4152
      if (pointsAttr !== null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4153
        //split into array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4154
        var pointsBuffer = pointsAttr.split(" ");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4155
        if (pointsBuffer.length % 2 === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4156
          for (var i = 0, j = pointsBuffer.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4157
            var verts = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4158
            verts[0]  = pointsBuffer[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4159
            verts[1]  = pointsBuffer[++i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4160
            this.vertices.push(verts);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4161
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4162
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4163
          throw("Error parsing polygon points: odd number of coordinates provided");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4164
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4165
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4166
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4167
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4168
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4169
     * The parseRect() function parses a rect from an SVG file.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4170
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4171
    PShapeSVG.prototype.parseRect = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4172
      this.kind      = PConstants.RECT;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4173
      this.family    = PConstants.PRIMITIVE;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4174
      this.params    = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4175
      this.params[0] = this.element.getFloatAttribute("x");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4176
      this.params[1] = this.element.getFloatAttribute("y");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4177
      this.params[2] = this.element.getFloatAttribute("width");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4178
      this.params[3] = this.element.getFloatAttribute("height");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4179
      if (this.params[2] < 0 || this.params[3] < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4180
        throw("svg error: negative width or height found while parsing <rect>");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4181
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4182
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4183
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4184
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4185
     * The parseEllipse() function handles parsing ellipse and circle tags.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4186
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4187
     * @param {boolean}val true if this is a circle and not an ellipse
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4188
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4189
    PShapeSVG.prototype.parseEllipse = function(val) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4190
      this.kind   = PConstants.ELLIPSE;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4191
      this.family = PConstants.PRIMITIVE;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4192
      this.params = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4193
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4194
      this.params[0] = this.element.getFloatAttribute("cx") | 0 ;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4195
      this.params[1] = this.element.getFloatAttribute("cy") | 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4196
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4197
      var rx, ry;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4198
      if (val) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4199
        rx = ry = this.element.getFloatAttribute("r");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4200
        if (rx < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4201
          throw("svg error: negative radius found while parsing <circle>");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4202
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4203
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4204
        rx = this.element.getFloatAttribute("rx");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4205
        ry = this.element.getFloatAttribute("ry");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4206
        if (rx < 0 || ry < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4207
          throw("svg error: negative x-axis radius or y-axis radius found while parsing <ellipse>");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4208
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4209
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4210
      this.params[0] -= rx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4211
      this.params[1] -= ry;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4212
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4213
      this.params[2] = rx*2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4214
      this.params[3] = ry*2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4215
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4216
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4217
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4218
     * The parseLine() function handles parsing line tags.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4219
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4220
     * @param {boolean}val true if this is a circle and not an ellipse
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4221
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4222
    PShapeSVG.prototype.parseLine = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4223
      this.kind = PConstants.LINE;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4224
      this.family = PConstants.PRIMITIVE;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4225
      this.params = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4226
      this.params[0] = this.element.getFloatAttribute("x1");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4227
      this.params[1] = this.element.getFloatAttribute("y1");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4228
      this.params[2] = this.element.getFloatAttribute("x2");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4229
      this.params[3] = this.element.getFloatAttribute("y2");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4230
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4231
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4232
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4233
     * The parseColors() function handles parsing the opacity, strijem stroke-width, stroke-linejoin,stroke-linecap, fill, and style attributes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4234
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4235
     * @param {XMLElement}element the element of which attributes to parse
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4236
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4237
    PShapeSVG.prototype.parseColors = function(element) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4238
      if (element.hasAttribute("opacity")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4239
        this.setOpacity(element.getAttribute("opacity"));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4240
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4241
      if (element.hasAttribute("stroke")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4242
        this.setStroke(element.getAttribute("stroke"));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4243
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4244
      if (element.hasAttribute("stroke-width")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4245
        // if NaN (i.e. if it's 'inherit') then default
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4246
        // back to the inherit setting
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4247
        this.setStrokeWeight(element.getAttribute("stroke-width"));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4248
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4249
      if (element.hasAttribute("stroke-linejoin") ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4250
        this.setStrokeJoin(element.getAttribute("stroke-linejoin"));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4251
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4252
      if (element.hasAttribute("stroke-linecap")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4253
        this.setStrokeCap(element.getStringAttribute("stroke-linecap"));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4254
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4255
      // fill defaults to black (though stroke defaults to "none")
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4256
      // http://www.w3.org/TR/SVG/painting.html#FillProperties
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4257
      if (element.hasAttribute("fill")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4258
        this.setFill(element.getStringAttribute("fill"));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4259
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4260
      if (element.hasAttribute("style")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4261
        var styleText   = element.getStringAttribute("style");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4262
        var styleTokens = styleText.toString().split( ";" );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4263
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4264
        for (var i = 0, j = styleTokens.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4265
          var tokens = p.trim(styleTokens[i].split( ":" ));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4266
          if (tokens[0] === "fill") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4267
              this.setFill(tokens[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4268
          } else if (tokens[0] === "fill-opacity") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4269
              this.setFillOpacity(tokens[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4270
          } else if (tokens[0] === "stroke") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4271
              this.setStroke(tokens[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4272
          } else if (tokens[0] === "stroke-width") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4273
              this.setStrokeWeight(tokens[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4274
          } else if (tokens[0] === "stroke-linecap") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4275
              this.setStrokeCap(tokens[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4276
          } else if (tokens[0] === "stroke-linejoin") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4277
              this.setStrokeJoin(tokens[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4278
          } else if (tokens[0] === "stroke-opacity") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4279
              this.setStrokeOpacity(tokens[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4280
          } else if (tokens[0] === "opacity") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4281
              this.setOpacity(tokens[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4282
          } // Other attributes are not yet implemented
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4283
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4284
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4285
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4286
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4287
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4288
     * PShapeSVG.parseColors() helper function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4289
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4290
     * @param {String} opacityText the value of fillOpacity
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4291
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4292
     * @see PShapeSVG#parseColors
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4293
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4294
    PShapeSVG.prototype.setFillOpacity = function(opacityText) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4295
      this.fillOpacity = parseFloat(opacityText);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4296
      this.fillColor   = this.fillOpacity * 255  << 24 |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4297
                         this.fillColor & 0xFFFFFF;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4298
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4299
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4300
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4301
     * PShapeSVG.parseColors() helper function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4302
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4303
     * @param {String} fillText the value of fill
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4304
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4305
     * @see PShapeSVG#parseColors
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4306
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4307
    PShapeSVG.prototype.setFill = function (fillText) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4308
      var opacityMask = this.fillColor & 0xFF000000;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4309
      if (fillText === "none") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4310
        this.fill = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4311
      } else if (fillText.indexOf("#") === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4312
        this.fill      = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4313
        if (fillText.length === 4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4314
          // convert #00F to #0000FF
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4315
          fillText = fillText.replace(/#(.)(.)(.)/,"#$1$1$2$2$3$3");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4316
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4317
        this.fillColor = opacityMask |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4318
                         (parseInt(fillText.substring(1), 16 )) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4319
                         0xFFFFFF;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4320
      } else if (fillText.indexOf("rgb") === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4321
        this.fill      = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4322
        this.fillColor = opacityMask | this.parseRGB(fillText);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4323
      } else if (fillText.indexOf("url(#") === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4324
        this.fillName = fillText.substring(5, fillText.length - 1 );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4325
      } else if (colors[fillText]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4326
        this.fill      = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4327
        this.fillColor = opacityMask |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4328
                         (parseInt(colors[fillText].substring(1), 16)) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4329
                         0xFFFFFF;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4330
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4331
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4332
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4333
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4334
     * PShapeSVG.parseColors() helper function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4335
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4336
     * @param {String} opacity the value of opacity
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4337
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4338
     * @see PShapeSVG#parseColors
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4339
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4340
    PShapeSVG.prototype.setOpacity = function(opacity) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4341
      this.strokeColor = parseFloat(opacity) * 255 << 24 |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4342
                         this.strokeColor & 0xFFFFFF;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4343
      this.fillColor   = parseFloat(opacity) * 255 << 24 |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4344
                         this.fillColor & 0xFFFFFF;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4345
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4346
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4347
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4348
     * PShapeSVG.parseColors() helper function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4349
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4350
     * @param {String} strokeText the value to set stroke to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4351
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4352
     * @see PShapeSVG#parseColors
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4353
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4354
    PShapeSVG.prototype.setStroke = function(strokeText) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4355
      var opacityMask = this.strokeColor & 0xFF000000;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4356
      if (strokeText === "none") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4357
        this.stroke = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4358
      } else if (strokeText.charAt( 0 ) === "#") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4359
        this.stroke      = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4360
        if (strokeText.length === 4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4361
          // convert #00F to #0000FF
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4362
          strokeText = strokeText.replace(/#(.)(.)(.)/,"#$1$1$2$2$3$3");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4363
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4364
        this.strokeColor = opacityMask |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4365
                           (parseInt( strokeText.substring( 1 ), 16 )) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4366
                           0xFFFFFF;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4367
      } else if (strokeText.indexOf( "rgb" ) === 0 ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4368
        this.stroke = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4369
        this.strokeColor = opacityMask | this.parseRGB(strokeText);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4370
      } else if (strokeText.indexOf( "url(#" ) === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4371
        this.strokeName = strokeText.substring(5, strokeText.length - 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4372
      } else if (colors[strokeText]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4373
        this.stroke      = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4374
        this.strokeColor = opacityMask |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4375
                           (parseInt(colors[strokeText].substring(1), 16)) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4376
                           0xFFFFFF;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4377
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4378
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4379
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4380
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4381
     * PShapeSVG.parseColors() helper function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4382
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4383
     * @param {String} weight the value to set strokeWeight to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4384
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4385
     * @see PShapeSVG#parseColors
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4386
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4387
    PShapeSVG.prototype.setStrokeWeight = function(weight) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4388
      this.strokeWeight = this.parseUnitSize(weight);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4389
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4390
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4391
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4392
     * PShapeSVG.parseColors() helper function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4393
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4394
     * @param {String} linejoin the value to set strokeJoin to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4395
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4396
     * @see PShapeSVG#parseColors
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4397
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4398
    PShapeSVG.prototype.setStrokeJoin = function(linejoin) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4399
      if (linejoin === "miter") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4400
        this.strokeJoin = PConstants.MITER;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4401
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4402
      } else if (linejoin === "round") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4403
        this.strokeJoin = PConstants.ROUND;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4404
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4405
      } else if (linejoin === "bevel") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4406
        this.strokeJoin = PConstants.BEVEL;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4407
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4408
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4409
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4410
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4411
     * PShapeSVG.parseColors() helper function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4412
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4413
     * @param {String} linecap the value to set strokeCap to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4414
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4415
     * @see PShapeSVG#parseColors
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4416
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4417
    PShapeSVG.prototype.setStrokeCap = function (linecap) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4418
      if (linecap === "butt") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4419
        this.strokeCap = PConstants.SQUARE;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4420
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4421
      } else if (linecap === "round") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4422
        this.strokeCap = PConstants.ROUND;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4423
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4424
      } else if (linecap === "square") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4425
        this.strokeCap = PConstants.PROJECT;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4426
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4427
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4428
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4429
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4430
     * PShapeSVG.parseColors() helper function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4431
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4432
     * @param {String} opacityText the value to set stroke opacity to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4433
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4434
     * @see PShapeSVG#parseColors
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4435
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4436
    PShapeSVG.prototype.setStrokeOpacity =  function (opacityText) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4437
      this.strokeOpacity = parseFloat(opacityText);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4438
      this.strokeColor   = this.strokeOpacity * 255 << 24 |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4439
                           this.strokeColor &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4440
                           0xFFFFFF;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4441
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4442
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4443
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4444
     * The parseRGB() function parses an rbg() color string and returns a color int
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4445
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4446
     * @param {String} color the color to parse in rbg() format
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4447
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4448
     * @return {int} the equivalent color int
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4449
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4450
    PShapeSVG.prototype.parseRGB = function(color) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4451
      var sub    = color.substring(color.indexOf('(') + 1, color.indexOf(')'));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4452
      var values = sub.split(", ");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4453
      return (values[0] << 16) | (values[1] << 8) | (values[2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4454
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4455
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4456
     * @member PShapeSVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4457
     * The parseUnitSize() function parse a size that may have a suffix for its units.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4458
     * Ignoring cases where this could also be a percentage.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4459
     * The <A HREF="http://www.w3.org/TR/SVG/coords.html#Units">units</A> spec:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4460
     * <UL>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4461
     * <LI>"1pt" equals "1.25px" (and therefore 1.25 user units)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4462
     * <LI>"1pc" equals "15px" (and therefore 15 user units)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4463
     * <LI>"1mm" would be "3.543307px" (3.543307 user units)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4464
     * <LI>"1cm" equals "35.43307px" (and therefore 35.43307 user units)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4465
     * <LI>"1in" equals "90px" (and therefore 90 user units)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4466
     * </UL>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4467
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4468
    PShapeSVG.prototype.parseUnitSize = function (text) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4469
      var len = text.length - 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4470
      if (len < 0) { return text; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4471
      if (text.indexOf("pt") === len) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4472
        return parseFloat(text.substring(0, len)) * 1.25;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4473
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4474
      if (text.indexOf("pc") === len) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4475
        return parseFloat( text.substring( 0, len)) * 15;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4476
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4477
      if (text.indexOf("mm") === len) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4478
        return parseFloat( text.substring(0, len)) * 3.543307;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4479
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4480
      if (text.indexOf("cm") === len) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4481
        return parseFloat(text.substring(0, len)) * 35.43307;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4482
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4483
      if (text.indexOf("in") === len) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4484
        return parseFloat(text.substring(0, len)) * 90;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4485
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4486
      if (text.indexOf("px") === len) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4487
        return parseFloat(text.substring(0, len));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4488
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4489
      return parseFloat(text);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4490
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4491
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4492
     * The shape() function displays shapes to the screen.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4493
     * Processing currently works with SVG shapes only.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4494
     * The <b>shape</b> parameter specifies the shape to display and the <b>x</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4495
     * and <b>y</b> parameters define the location of the shape from its
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4496
     * upper-left corner.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4497
     * The shape is displayed at its original size unless the <b>width</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4498
     * and <b>height</b> parameters specify a different size.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4499
     * The <b>shapeMode()</b> function changes the way the parameters work.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4500
     * A call to <b>shapeMode(CORNERS)</b>, for example, will change the width
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4501
     * and height parameters to define the x and y values of the opposite corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4502
     * of the shape.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4503
     * <br><br>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4504
     * Note complex shapes may draw awkwardly with P2D, P3D, and OPENGL. Those
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4505
     * renderers do not yet support shapes that have holes or complicated breaks.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4506
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4507
     * @param {PShape} shape       the shape to display
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4508
     * @param {int|float} x        x-coordinate of the shape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4509
     * @param {int|float} y        y-coordinate of the shape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4510
     * @param {int|float} width    width to display the shape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4511
     * @param {int|float} height   height to display the shape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4512
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4513
     * @see PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4514
     * @see loadShape()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4515
     * @see shapeMode()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4516
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4517
    p.shape = function(shape, x, y, width, height) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4518
      if (arguments.length >= 1 && arguments[0] !== null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4519
        if (shape.isVisible()) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4520
          p.pushMatrix();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4521
          if (curShapeMode === PConstants.CENTER) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4522
            if (arguments.length === 5) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4523
              p.translate(x - width/2, y - height/2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4524
              p.scale(width / shape.getWidth(), height / shape.getHeight());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4525
            } else if (arguments.length === 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4526
              p.translate(x - shape.getWidth()/2, - shape.getHeight()/2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4527
            } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4528
              p.translate(-shape.getWidth()/2, -shape.getHeight()/2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4529
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4530
          } else if (curShapeMode === PConstants.CORNER) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4531
            if (arguments.length === 5) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4532
              p.translate(x, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4533
              p.scale(width / shape.getWidth(), height / shape.getHeight());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4534
            } else if (arguments.length === 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4535
              p.translate(x, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4536
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4537
          } else if (curShapeMode === PConstants.CORNERS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4538
            if (arguments.length === 5) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4539
              width  -= x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4540
              height -= y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4541
              p.translate(x, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4542
              p.scale(width / shape.getWidth(), height / shape.getHeight());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4543
            } else if (arguments.length === 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4544
              p.translate(x, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4545
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4546
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4547
          shape.draw();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4548
          if ((arguments.length === 1 && curShapeMode === PConstants.CENTER ) || arguments.length > 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4549
            p.popMatrix();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4550
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4551
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4552
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4553
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4554
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4555
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4556
     * The shapeMode() function modifies the location from which shapes draw.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4557
     * The default mode is <b>shapeMode(CORNER)</b>, which specifies the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4558
     * location to be the upper left corner of the shape and uses the third
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4559
     * and fourth parameters of <b>shape()</b> to specify the width and height.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4560
     * The syntax <b>shapeMode(CORNERS)</b> uses the first and second parameters
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4561
     * of <b>shape()</b> to set the location of one corner and uses the third
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4562
     * and fourth parameters to set the opposite corner.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4563
     * The syntax <b>shapeMode(CENTER)</b> draws the shape from its center point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4564
     * and uses the third and forth parameters of <b>shape()</b> to specify the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4565
     * width and height.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4566
     * The parameter must be written in "ALL CAPS" because Processing syntax
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4567
     * is case sensitive.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4568
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4569
     * @param {int} mode One of CORNER, CORNERS, CENTER
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4570
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4571
     * @see shape()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4572
     * @see rectMode()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4573
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4574
    p.shapeMode = function (mode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4575
      curShapeMode = mode;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4576
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4577
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4578
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4579
     * The loadShape() function loads vector shapes into a variable of type PShape. Currently, only SVG files may be loaded.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4580
     * In most cases, <b>loadShape()</b> should be used inside <b>setup()</b> because loading shapes inside <b>draw()</b> will reduce the speed of a sketch.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4581
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4582
     * @param {String} filename     an SVG file
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4583
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4584
     * @return {PShape} a object of type PShape or null
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4585
     * @see PShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4586
     * @see PApplet#shape()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4587
     * @see PApplet#shapeMode()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4588
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4589
    p.loadShape = function (filename) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4590
      if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4591
        if (filename.indexOf(".svg") > -1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4592
          return new PShapeSVG(null, filename);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4593
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4594
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4595
      return null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4596
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4597
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4598
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4599
     * XMLAttribute is an attribute of a XML element. This is an internal class
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4600
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4601
     * @param {String} fname     the full name of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4602
     * @param {String} n         the short name of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4603
     * @param {String} namespace the namespace URI of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4604
     * @param {String} v         the value of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4605
     * @param {String }t         the type of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4606
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4607
     * @see XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4608
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4609
    var XMLAttribute = function(fname, n, nameSpace, v, t){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4610
      this.fullName = fname || "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4611
      this.name = n || "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4612
      this.namespace = nameSpace || "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4613
      this.value = v;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4614
      this.type = t;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4615
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4616
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4617
     * XMLAttribute methods
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4618
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4619
    XMLAttribute.prototype = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4620
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4621
       * @member XMLAttribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4622
       * The getName() function returns the short name of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4623
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4624
       * @return {String} the short name of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4625
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4626
      getName: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4627
        return this.name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4628
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4629
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4630
       * @member XMLAttribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4631
       * The getFullName() function returns the full name of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4632
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4633
       * @return {String} the full name of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4634
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4635
      getFullName: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4636
        return this.fullName;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4637
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4638
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4639
       * @member XMLAttribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4640
       * The getNamespace() function returns the namespace of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4641
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4642
       * @return {String} the namespace of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4643
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4644
      getNamespace: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4645
        return this.namespace;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4646
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4647
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4648
       * @member XMLAttribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4649
       * The getValue() function returns the value of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4650
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4651
       * @return {String} the value of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4652
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4653
      getValue: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4654
        return this.value;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4655
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4656
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4657
       * @member XMLAttribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4658
       * The getValue() function returns the type of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4659
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4660
       * @return {String} the type of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4661
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4662
      getType: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4663
        return this.type;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4664
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4665
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4666
       * @member XMLAttribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4667
       * The setValue() function sets the value of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4668
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4669
       * @param {String} newval the new value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4670
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4671
      setValue: function(newval) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4672
        this.value = newval;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4673
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4674
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4675
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4676
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4677
     * XMLElement is a representation of an XML object. The object is able to parse XML code
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4678
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4679
     * @param {PApplet} parent   typically use "this"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4680
     * @param {String} filename  name of the XML/SVG file to load
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4681
     * @param {String} xml       the xml/svg string
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4682
     * @param {String} fullname  the full name of the element
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4683
     * @param {String} namespace the namespace  of the URI
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4684
     * @param {String} systemID  the system ID of the XML data where the element starts
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4685
     * @param {Integer }lineNr   the line in the XML data where the element starts
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4686
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4687
    var XMLElement = p.XMLElement = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4688
      this.attributes = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4689
      this.children   = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4690
      this.fullName   = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4691
      this.name       = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4692
      this.namespace  = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4693
      this.content = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4694
      this.parent    = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4695
      this.lineNr     = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4696
      this.systemID   = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4697
      this.type = "ELEMENT";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4698
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4699
      if (arguments.length === 4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4700
        this.fullName   = arguments[0] || "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4701
        if (arguments[1]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4702
          this.name = arguments[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4703
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4704
          var index = this.fullName.indexOf(':');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4705
          if (index >= 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4706
            this.name = this.fullName.substring(index + 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4707
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4708
            this.name = this.fullName;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4709
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4710
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4711
        this.namespace = arguments[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4712
        this.lineNr    = arguments[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4713
        this.systemID  = arguments[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4714
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4715
      else if ((arguments.length === 2 && arguments[1].indexOf(".") > -1) ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4716
        // filename or svg xml element
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4717
        this.parse(arguments[arguments.length -1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4718
      } else if (arguments.length === 1 && typeof arguments[0] === "string"){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4719
        this.parse(arguments[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4720
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4721
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4722
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4723
     * XMLElement methods
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4724
     * missing: enumerateAttributeNames(), enumerateChildren(),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4725
     * NOTE: parse does not work when a url is passed in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4726
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4727
    XMLElement.prototype = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4728
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4729
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4730
       * The parse() function retrieves the file via ajax() and uses DOMParser()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4731
       * parseFromString method to make an XML document
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4732
       * @addon
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4733
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4734
       * @param {String} filename name of the XML/SVG file to load
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4735
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4736
       * @throws ExceptionType Error loading document
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4737
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4738
       * @see XMLElement#parseChildrenRecursive
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4739
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4740
      parse: function(textstring) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4741
        var xmlDoc;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4742
        try {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4743
          var extension = textstring.substring(textstring.length-4);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4744
          if (extension === ".xml" || extension === ".svg") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4745
            textstring = ajax(textstring);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4746
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4747
          xmlDoc = new DOMParser().parseFromString(textstring, "text/xml");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4748
          var elements = xmlDoc.documentElement;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4749
          if (elements) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4750
            this.parseChildrenRecursive(null, elements);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4751
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4752
            throw ("Error loading document");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4753
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4754
          return this;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4755
        } catch(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4756
          throw(e);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4757
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4758
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4759
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4760
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4761
       * Internal helper function for parse().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4762
       * Loops through the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4763
       * @addon
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4764
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4765
       * @param {XMLElement} parent                      the parent node
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4766
       * @param {XML document childNodes} elementpath    the remaining nodes that need parsing
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4767
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4768
       * @return {XMLElement} the new element and its children elements
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4769
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4770
      parseChildrenRecursive: function (parent , elementpath){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4771
        var xmlelement,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4772
          xmlattribute,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4773
          tmpattrib,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4774
          l, m,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4775
          child;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4776
        if (!parent) { // this element is the root element
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4777
          this.fullName = elementpath.localName;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4778
          this.name     = elementpath.nodeName;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4779
          xmlelement    = this;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4780
        } else { // this element has a parent
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4781
          xmlelement         = new XMLElement(elementpath.localName, elementpath.nodeName, "", "");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4782
          xmlelement.parent  = parent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4783
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4784
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4785
        // if this is a text node, return a PCData element, instead of an XML element.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4786
        if(elementpath.nodeType === 3 && elementpath.textContent !== "") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4787
          return this.createPCDataElement(elementpath.textContent);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4788
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4789
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4790
        // bind all attributes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4791
        for (l = 0, m = elementpath.attributes.length; l < m; l++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4792
          tmpattrib    = elementpath.attributes[l];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4793
          xmlattribute = new XMLAttribute(tmpattrib.getname,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4794
                                          tmpattrib.nodeName,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4795
                                          tmpattrib.namespaceURI,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4796
                                          tmpattrib.nodeValue,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4797
                                          tmpattrib.nodeType);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4798
          xmlelement.attributes.push(xmlattribute);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4799
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4800
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4801
        // bind all children
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4802
        for (l = 0, m = elementpath.childNodes.length; l < m; l++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4803
          var node = elementpath.childNodes[l];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4804
          if (node.nodeType === 1 || node.nodeType === 3) { // ELEMENT_NODE or TEXT_NODE
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4805
            child = xmlelement.parseChildrenRecursive(xmlelement, node);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4806
            if (child !== null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4807
              xmlelement.children.push(child);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4808
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4809
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4810
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4811
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4812
        return xmlelement;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4813
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4814
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4815
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4816
       * The createElement() function Creates an empty element
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4817
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4818
       * @param {String} fullName   the full name of the element
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4819
       * @param {String} namespace  the namespace URI
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4820
       * @param {String} systemID   the system ID of the XML data where the element starts
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4821
       * @param {int} lineNr    the line in the XML data where the element starts
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4822
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4823
      createElement: function () {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4824
        if (arguments.length === 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4825
          return new XMLElement(arguments[0], arguments[1], null, null);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4826
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4827
        return new XMLElement(arguments[0], arguments[1], arguments[2], arguments[3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4828
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4829
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4830
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4831
       * The createPCDataElement() function creates an element to be used for #PCDATA content.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4832
       * Because Processing discards whitespace TEXT nodes, this method will not build an element
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4833
       * if the passed content is empty after trimming for whitespace.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4834
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4835
       * @return {XMLElement} new "test" XMLElement, or null if content consists only of whitespace
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4836
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4837
      createPCDataElement: function (content) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4838
        if(content.replace(/^\s+$/g,"") === "") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4839
          return null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4840
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4841
        var pcdata = new XMLElement();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4842
        pcdata.content = content;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4843
        pcdata.type = "TEXT";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4844
        return pcdata;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4845
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4846
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4847
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4848
       * The hasAttribute() function returns whether an attribute exists
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4849
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4850
       * @param {String} name      name of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4851
       * @param {String} namespace the namespace URI of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4852
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4853
       * @return {boolean} true if the attribute exists
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4854
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4855
      hasAttribute: function () {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4856
        if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4857
          return this.getAttribute(arguments[0]) !== null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4858
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4859
        if (arguments.length === 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4860
          return this.getAttribute(arguments[0],arguments[1]) !== null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4861
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4862
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4863
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4864
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4865
       * The equals() function checks to see if the XMLElement being passed in equals another XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4866
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4867
       * @param {XMLElement} rawElement the element to compare to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4868
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4869
       * @return {boolean} true if the element equals another element
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4870
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4871
      equals: function(other) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4872
        if (!(other instanceof XMLElement)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4873
          return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4874
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4875
        var i, j;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4876
        if (this.name !== other.getLocalName()) { return false; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4877
        if (this.attributes.length !== other.getAttributeCount()) { return false; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4878
        // attributes may be ordered differently
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4879
        if (this.attributes.length !== other.attributes.length) { return false; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4880
        var attr_name, attr_ns, attr_value, attr_type, attr_other;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4881
        for (i = 0, j = this.attributes.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4882
          attr_name = this.attributes[i].getName();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4883
          attr_ns = this.attributes[i].getNamespace();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4884
          attr_other = other.findAttribute(attr_name, attr_ns);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4885
          if (attr_other === null) { return false; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4886
          if (this.attributes[i].getValue() !== attr_other.getValue()) { return false; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4887
          if (this.attributes[i].getType() !== attr_other.getType()) { return false; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4888
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4889
        // children must be ordered identically
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4890
        if (this.children.length !== other.getChildCount()) { return false; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4891
        if (this.children.length>0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4892
          var child1, child2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4893
          for (i = 0, j = this.children.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4894
            child1 = this.getChild(i);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4895
            child2 = other.getChild(i);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4896
            if (!child1.equals(child2)) { return false; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4897
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4898
          return true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4899
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4900
        return (this.content === other.content);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4901
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4902
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4903
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4904
       * The getContent() function returns the content of an element. If there is no such content, null is returned
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4905
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4906
       * @return {String} the (possibly null) content
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4907
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4908
      getContent: function(){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4909
        if (this.type === "TEXT") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4910
          return this.content;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4911
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4912
        var children = this.children;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4913
        if (children.length === 1 && children[0].type === "TEXT") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4914
          return children[0].content;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4915
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4916
        return null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4917
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4918
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4919
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4920
       * The getAttribute() function returns the value of an attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4921
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4922
       * @param {String} name         the non-null full name of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4923
       * @param {String} namespace    the namespace URI, which may be null
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4924
       * @param {String} defaultValue the default value of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4925
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4926
       * @return {String} the value, or defaultValue if the attribute does not exist
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4927
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4928
      getAttribute: function (){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4929
        var attribute;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4930
        if( arguments.length === 2 ){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4931
          attribute = this.findAttribute(arguments[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4932
          if (attribute) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4933
            return attribute.getValue();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4934
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4935
          return arguments[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4936
        } else if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4937
          attribute = this.findAttribute(arguments[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4938
          if (attribute) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4939
            return attribute.getValue();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4940
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4941
          return null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4942
        } else if (arguments.length === 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4943
          attribute = this.findAttribute(arguments[0],arguments[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4944
          if (attribute) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4945
            return attribute.getValue();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4946
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4947
          return arguments[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4948
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4949
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4950
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4951
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4952
       * The getStringAttribute() function returns the string attribute of the element
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4953
       * If the <b>defaultValue</b> parameter is used and the attribute doesn't exist, the <b>defaultValue</b> value is returned.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4954
       * When calling the function without the <b>defaultValue</b> parameter, if the attribute doesn't exist, the value 0 is returned.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4955
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4956
       * @param name         the name of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4957
       * @param defaultValue value returned if the attribute is not found
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4958
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4959
       * @return {String} the value, or defaultValue if the attribute does not exist
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4960
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4961
      getStringAttribute: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4962
        if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4963
          return this.getAttribute(arguments[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4964
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4965
        if (arguments.length === 2){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4966
          return this.getAttribute(arguments[0], arguments[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4967
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4968
        return this.getAttribute(arguments[0], arguments[1],arguments[2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4969
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4970
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4971
       * Processing 1.5 XML API wrapper for the generic String
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4972
       * attribute getter. This may only take one argument.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4973
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4974
      getString: function(attributeName) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4975
        return this.getStringAttribute(attributeName);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4976
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4977
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4978
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4979
       * The getFloatAttribute() function returns the float attribute of the element.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4980
       * If the <b>defaultValue</b> parameter is used and the attribute doesn't exist, the <b>defaultValue</b> value is returned.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4981
       * When calling the function without the <b>defaultValue</b> parameter, if the attribute doesn't exist, the value 0 is returned.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4982
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4983
       * @param name         the name of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4984
       * @param defaultValue value returned if the attribute is not found
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4985
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4986
       * @return {float} the value, or defaultValue if the attribute does not exist
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4987
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4988
      getFloatAttribute: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4989
        if (arguments.length === 1 ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4990
          return parseFloat(this.getAttribute(arguments[0], 0));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4991
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4992
        if (arguments.length === 2 ){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4993
          return this.getAttribute(arguments[0], arguments[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4994
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4995
        return this.getAttribute(arguments[0], arguments[1],arguments[2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4996
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4997
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4998
       * Processing 1.5 XML API wrapper for the generic float
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  4999
       * attribute getter. This may only take one argument.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5000
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5001
      getFloat: function(attributeName) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5002
        return this.getFloatAttribute(attributeName);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5003
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5004
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5005
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5006
       * The getIntAttribute() function returns the integer attribute of the element.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5007
       * If the <b>defaultValue</b> parameter is used and the attribute doesn't exist, the <b>defaultValue</b> value is returned.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5008
       * When calling the function without the <b>defaultValue</b> parameter, if the attribute doesn't exist, the value 0 is returned.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5009
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5010
       * @param name         the name of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5011
       * @param defaultValue value returned if the attribute is not found
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5012
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5013
       * @return {int} the value, or defaultValue if the attribute does not exist
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5014
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5015
      getIntAttribute: function () {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5016
        if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5017
          return this.getAttribute( arguments[0], 0 );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5018
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5019
        if (arguments.length === 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5020
          return this.getAttribute(arguments[0], arguments[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5021
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5022
        return this.getAttribute(arguments[0], arguments[1],arguments[2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5023
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5024
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5025
       * Processing 1.5 XML API wrapper for the generic int
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5026
       * attribute getter. This may only take one argument.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5027
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5028
      getInt: function(attributeName) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5029
        return this.getIntAttribute(attributeName);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5030
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5031
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5032
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5033
       * The hasChildren() function returns whether the element has children.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5034
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5035
       * @return {boolean} true if the element has children.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5036
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5037
      hasChildren: function () {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5038
        return this.children.length > 0 ;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5039
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5040
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5041
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5042
       * The addChild() function adds a child element
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5043
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5044
       * @param {XMLElement} child the non-null child to add.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5045
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5046
      addChild: function (child) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5047
        if (child !== null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5048
          child.parent = this;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5049
          this.children.push(child);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5050
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5051
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5052
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5053
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5054
       * The insertChild() function inserts a child element at the index provided
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5055
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5056
       * @param {XMLElement} child  the non-null child to add.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5057
       * @param {int} index     where to put the child.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5058
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5059
      insertChild: function (child, index) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5060
        if (child) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5061
          if ((child.getLocalName() === null) && (! this.hasChildren())) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5062
            var lastChild = this.children[this.children.length -1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5063
            if (lastChild.getLocalName() === null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5064
                lastChild.setContent(lastChild.getContent() + child.getContent());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5065
                return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5066
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5067
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5068
          child.parent = this;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5069
          this.children.splice(index,0,child);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5070
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5071
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5072
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5073
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5074
       * The getChild() returns the child XMLElement as specified by the <b>index</b> parameter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5075
       * The value of the <b>index</b> parameter must be less than the total number of children to avoid going out of the array storing the child elements.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5076
       * When the <b>path</b> parameter is specified, then it will return all children that match that path. The path is a series of elements and sub-elements, separated by slashes.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5077
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5078
       * @param {int} index     where to put the child.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5079
       * @param {String} path       path to a particular element
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5080
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5081
       * @return {XMLElement} the element
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5082
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5083
      getChild: function (){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5084
        if (typeof arguments[0]  === "number") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5085
          return this.children[arguments[0]];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5086
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5087
        if (arguments[0].indexOf('/') !== -1) { // path was given
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5088
          this.getChildRecursive(arguments[0].split("/"), 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5089
          return null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5090
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5091
        var kid, kidName;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5092
        for (var i = 0, j = this.getChildCount(); i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5093
          kid = this.getChild(i);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5094
          kidName = kid.getName();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5095
          if (kidName !== null && kidName === arguments[0]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5096
              return kid;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5097
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5098
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5099
        return null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5100
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5101
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5102
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5103
       * The getChildren() returns all of the children as an XMLElement array.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5104
       * When the <b>path</b> parameter is specified, then it will return all children that match that path.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5105
       * The path is a series of elements and sub-elements, separated by slashes.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5106
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5107
       * @param {String} path       element name or path/to/element
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5108
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5109
       * @return {XMLElement} array of child elements that match
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5110
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5111
       * @see XMLElement#getChildCount()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5112
       * @see XMLElement#getChild()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5113
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5114
      getChildren: function(){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5115
        if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5116
          if (typeof arguments[0]  === "number") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5117
            return this.getChild( arguments[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5118
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5119
          if (arguments[0].indexOf('/') !== -1) { // path was given
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5120
            return this.getChildrenRecursive( arguments[0].split("/"), 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5121
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5122
          var matches = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5123
          var kid, kidName;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5124
          for (var i = 0, j = this.getChildCount(); i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5125
            kid = this.getChild(i);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5126
            kidName = kid.getName();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5127
            if (kidName !== null && kidName === arguments[0]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5128
              matches.push(kid);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5129
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5130
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5131
          return matches;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5132
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5133
        return this.children;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5134
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5135
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5136
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5137
       * The getChildCount() returns the number of children for the element.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5138
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5139
       * @return {int} the count
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5140
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5141
       * @see XMLElement#getChild()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5142
       * @see XMLElement#getChildren()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5143
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5144
      getChildCount: function(){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5145
        return this.children.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5146
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5147
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5148
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5149
       * Internal helper function for getChild().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5150
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5151
       * @param {String[]} items   result of splitting the query on slashes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5152
       * @param {int} offset   where in the items[] array we're currently looking
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5153
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5154
       * @return {XMLElement} matching element or null if no match
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5155
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5156
      getChildRecursive: function (items, offset) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5157
        var kid, kidName;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5158
        for(var i = 0, j = this.getChildCount(); i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5159
            kid = this.getChild(i);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5160
            kidName = kid.getName();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5161
            if (kidName !== null && kidName === items[offset]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5162
              if (offset === items.length-1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5163
                return kid;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5164
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5165
              offset += 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5166
              return kid.getChildRecursive(items, offset);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5167
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5168
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5169
        return null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5170
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5171
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5172
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5173
       * Internal helper function for getChildren().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5174
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5175
       * @param {String[]} items   result of splitting the query on slashes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5176
       * @param {int} offset   where in the items[] array we're currently looking
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5177
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5178
       * @return {XMLElement[]} matching elements or empty array if no match
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5179
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5180
      getChildrenRecursive: function (items, offset) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5181
        if (offset === items.length-1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5182
          return this.getChildren(items[offset]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5183
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5184
        var matches = this.getChildren(items[offset]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5185
        var kidMatches = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5186
        for (var i = 0; i < matches.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5187
          kidMatches = kidMatches.concat(matches[i].getChildrenRecursive(items, offset+1));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5188
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5189
        return kidMatches;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5190
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5191
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5192
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5193
       * The isLeaf() function returns whether the element is a leaf element.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5194
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5195
       * @return {boolean} true if the element has no children.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5196
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5197
      isLeaf: function(){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5198
        return !this.hasChildren();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5199
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5200
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5201
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5202
       * The listChildren() function put the names of all children into an array. Same as looping through
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5203
       * each child and calling getName() on each XMLElement.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5204
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5205
       * @return {String[]} a list of element names.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5206
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5207
      listChildren: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5208
        var arr = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5209
        for (var i = 0, j = this.children.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5210
          arr.push( this.getChild(i).getName());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5211
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5212
        return arr;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5213
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5214
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5215
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5216
       * The removeAttribute() function removes an attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5217
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5218
       * @param {String} name        the non-null name of the attribute.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5219
       * @param {String} namespace   the namespace URI of the attribute, which may be null.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5220
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5221
      removeAttribute: function (name , namespace) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5222
        this.namespace = namespace || "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5223
        for (var i = 0, j = this.attributes.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5224
          if (this.attributes[i].getName() === name && this.attributes[i].getNamespace() === this.namespace) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5225
            this.attributes.splice(i, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5226
            break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5227
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5228
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5229
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5230
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5231
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5232
       * The removeChild() removes a child element.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5233
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5234
       * @param {XMLElement} child      the the non-null child to be renoved
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5235
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5236
      removeChild: function(child) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5237
        if (child) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5238
          for (var i = 0, j = this.children.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5239
            if (this.children[i].equals(child)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5240
              this.children.splice(i, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5241
              break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5242
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5243
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5244
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5245
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5246
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5247
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5248
       * The removeChildAtIndex() removes the child located at a certain index
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5249
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5250
       * @param {int} index      the index of the child, where the first child has index 0
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5251
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5252
      removeChildAtIndex: function(index) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5253
        if (this.children.length > index) { //make sure its not outofbounds
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5254
          this.children.splice(index, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5255
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5256
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5257
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5258
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5259
       * The findAttribute() function searches an attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5260
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5261
       * @param {String} name        fullName the non-null full name of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5262
       * @param {String} namespace   the name space, which may be null
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5263
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5264
       * @return {XMLAttribute} the attribute, or null if the attribute does not exist.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5265
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5266
      findAttribute: function (name, namespace) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5267
        this.namespace = namespace || "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5268
        for (var i = 0, j = this.attributes.length; i < j; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5269
          if (this.attributes[i].getName() === name && this.attributes[i].getNamespace() === this.namespace) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5270
             return this.attributes[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5271
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5272
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5273
        return null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5274
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5275
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5276
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5277
       * The setAttribute() function sets an attribute.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5278
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5279
       * @param {String} name        the non-null full name of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5280
       * @param {String} namespace   the non-null value of the attribute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5281
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5282
      setAttribute: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5283
        var attr;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5284
        if (arguments.length === 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5285
          var index = arguments[0].indexOf(':');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5286
          var name  = arguments[0].substring(index + 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5287
          attr      = this.findAttribute(name, arguments[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5288
          if (attr) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5289
            attr.setValue(arguments[2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5290
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5291
            attr = new XMLAttribute(arguments[0], name, arguments[1], arguments[2], "CDATA");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5292
            this.attributes.push(attr);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5293
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5294
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5295
          attr = this.findAttribute(arguments[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5296
          if (attr) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5297
            attr.setValue(arguments[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5298
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5299
            attr = new XMLAttribute(arguments[0], arguments[0], null, arguments[1], "CDATA");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5300
            this.attributes.push(attr);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5301
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5302
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5303
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5304
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5305
       * Processing 1.5 XML API wrapper for the generic String
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5306
       * attribute setter. This must take two arguments.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5307
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5308
      setString: function(attribute, value) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5309
        this.setAttribute(attribute, value);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5310
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5311
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5312
       * Processing 1.5 XML API wrapper for the generic int
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5313
       * attribute setter. This must take two arguments.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5314
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5315
      setInt: function(attribute, value) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5316
        this.setAttribute(attribute, value);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5317
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5318
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5319
       * Processing 1.5 XML API wrapper for the generic float
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5320
       * attribute setter. This must take two arguments.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5321
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5322
      setFloat: function(attribute, value) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5323
        this.setAttribute(attribute, value);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5324
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5325
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5326
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5327
       * The setContent() function sets the #PCDATA content. It is an error to call this method with a
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5328
       * non-null value if there are child objects.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5329
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5330
       * @param {String} content     the (possibly null) content
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5331
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5332
      setContent: function(content) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5333
        if (this.children.length>0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5334
          Processing.debug("Tried to set content for XMLElement with children"); }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5335
        this.content = content;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5336
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5337
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5338
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5339
       * The setName() function sets the full name. This method also sets the short name and clears the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5340
       * namespace URI.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5341
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5342
       * @param {String} name        the non-null name
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5343
       * @param {String} namespace   the namespace URI, which may be null.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5344
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5345
      setName: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5346
        if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5347
          this.name      = arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5348
          this.fullName  = arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5349
          this.namespace = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5350
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5351
          var index = arguments[0].indexOf(':');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5352
          if ((arguments[1] === null) || (index < 0)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5353
              this.name = arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5354
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5355
              this.name = arguments[0].substring(index + 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5356
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5357
          this.fullName  = arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5358
          this.namespace = arguments[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5359
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5360
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5361
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5362
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5363
       * The getName() function returns the full name (i.e. the name including an eventual namespace
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5364
       * prefix) of the element.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5365
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5366
       * @return {String} the name, or null if the element only contains #PCDATA.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5367
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5368
      getName: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5369
        return this.fullName;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5370
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5371
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5372
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5373
       * The getLocalName() function returns the local name (i.e. the name excluding an eventual namespace
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5374
       * prefix) of the element.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5375
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5376
       * @return {String} the name, or null if the element only contains #PCDATA.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5377
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5378
      getLocalName: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5379
        return this.name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5380
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5381
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5382
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5383
       * The getAttributeCount() function returns the number of attributes for the node
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5384
       * that this XMLElement represents.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5385
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5386
       * @return {int} the number of attributes in this XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5387
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5388
      getAttributeCount: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5389
        return this.attributes.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5390
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5391
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5392
       * @member XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5393
       * The toString() function returns the XML definition of an XMLElement.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5394
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5395
       * @return {String} the XML definition of this XMLElement
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5396
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5397
      toString: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5398
        // shortcut for text nodes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5399
        if(this.type==="TEXT") { return this.content; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5400
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5401
        // real XMLElements
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5402
        var tagstring = (this.namespace !== "" && this.namespace !== this.name ? this.namespace + ":" : "") + this.name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5403
        var xmlstring =  "<" + tagstring;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5404
        var a,c;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5405
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5406
        // serialize the attributes to XML string
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5407
        for (a = 0; a<this.attributes.length; a++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5408
          var attr = this.attributes[a];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5409
          xmlstring += " "  + attr.getName() + "=" + '"' + attr.getValue() + '"';
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5410
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5411
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5412
        // serialize all children to XML string
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5413
        if (this.children.length === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5414
          if (this.content==="") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5415
            xmlstring += "/>";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5416
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5417
            xmlstring += ">" + this.content + "</"+tagstring+">";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5418
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5419
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5420
          xmlstring += ">";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5421
          for (c = 0; c<this.children.length; c++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5422
            xmlstring += this.children[c].toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5423
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5424
          xmlstring += "</" + tagstring + ">";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5425
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5426
        return xmlstring;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5427
       }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5428
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5429
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5430
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5431
     * static Processing 1.5 XML API wrapper for the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5432
     * parse method. This may only take one argument.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5433
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5434
    XMLElement.parse = function(xmlstring) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5435
      var element = new XMLElement();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5436
      element.parse(xmlstring);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5437
      return element;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5438
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5439
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5440
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5441
    // 2D Matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5442
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5443
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5444
     * Helper function for printMatrix(). Finds the largest scalar
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5445
     * in the matrix, then number of digits left of the decimal.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5446
     * Call from PMatrix2D and PMatrix3D's print() function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5447
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5448
    var printMatrixHelper = function(elements) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5449
      var big = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5450
      for (var i = 0; i < elements.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5451
        if (i !== 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5452
          big = Math.max(big, Math.abs(elements[i]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5453
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5454
          big = Math.abs(elements[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5455
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5456
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5457
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5458
      var digits = (big + "").indexOf(".");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5459
      if (digits === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5460
        digits = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5461
      } else if (digits === -1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5462
        digits = (big + "").length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5463
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5464
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5465
      return digits;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5466
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5467
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5468
     * PMatrix2D is a 3x2 affine matrix implementation. The constructor accepts another PMatrix2D or a list of six float elements.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5469
     * If no parameters are provided the matrix is set to the identity matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5470
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5471
     * @param {PMatrix2D} matrix  the initial matrix to set to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5472
     * @param {float} m00         the first element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5473
     * @param {float} m01         the second element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5474
     * @param {float} m02         the third element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5475
     * @param {float} m10         the fourth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5476
     * @param {float} m11         the fifth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5477
     * @param {float} m12         the sixth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5478
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5479
    var PMatrix2D = p.PMatrix2D = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5480
      if (arguments.length === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5481
        this.reset();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5482
      } else if (arguments.length === 1 && arguments[0] instanceof PMatrix2D) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5483
        this.set(arguments[0].array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5484
      } else if (arguments.length === 6) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5485
        this.set(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5486
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5487
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5488
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5489
     * PMatrix2D methods
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5490
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5491
    PMatrix2D.prototype = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5492
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5493
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5494
       * The set() function sets the matrix elements. The function accepts either another PMatrix2D, an array of elements, or a list of six floats.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5495
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5496
       * @param {PMatrix2D} matrix    the matrix to set this matrix to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5497
       * @param {float[]} elements    an array of elements to set this matrix to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5498
       * @param {float} m00           the first element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5499
       * @param {float} m01           the third element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5500
       * @param {float} m10           the fourth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5501
       * @param {float} m11           the fith element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5502
       * @param {float} m12           the sixth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5503
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5504
      set: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5505
        if (arguments.length === 6) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5506
          var a = arguments;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5507
          this.set([a[0], a[1], a[2],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5508
                    a[3], a[4], a[5]]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5509
        } else if (arguments.length === 1 && arguments[0] instanceof PMatrix2D) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5510
          this.elements = arguments[0].array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5511
        } else if (arguments.length === 1 && arguments[0] instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5512
          this.elements = arguments[0].slice();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5513
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5514
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5515
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5516
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5517
       * The get() function returns a copy of this PMatrix2D.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5518
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5519
       * @return {PMatrix2D} a copy of this PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5520
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5521
      get: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5522
        var outgoing = new PMatrix2D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5523
        outgoing.set(this.elements);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5524
        return outgoing;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5525
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5526
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5527
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5528
       * The reset() function sets this PMatrix2D to the identity matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5529
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5530
      reset: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5531
        this.set([1, 0, 0, 0, 1, 0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5532
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5533
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5534
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5535
       * The array() function returns a copy of the element values.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5536
       * @addon
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5537
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5538
       * @return {float[]} returns a copy of the element values
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5539
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5540
      array: function array() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5541
        return this.elements.slice();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5542
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5543
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5544
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5545
       * The translate() function translates this matrix by moving the current coordinates to the location specified by tx and ty.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5546
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5547
       * @param {float} tx  the x-axis coordinate to move to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5548
       * @param {float} ty  the y-axis coordinate to move to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5549
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5550
      translate: function(tx, ty) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5551
        this.elements[2] = tx * this.elements[0] + ty * this.elements[1] + this.elements[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5552
        this.elements[5] = tx * this.elements[3] + ty * this.elements[4] + this.elements[5];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5553
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5554
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5555
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5556
       * The invTranslate() function translates this matrix by moving the current coordinates to the negative location specified by tx and ty.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5557
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5558
       * @param {float} tx  the x-axis coordinate to move to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5559
       * @param {float} ty  the y-axis coordinate to move to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5560
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5561
      invTranslate: function(tx, ty) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5562
        this.translate(-tx, -ty);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5563
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5564
       /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5565
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5566
       * The transpose() function is not used in processingjs.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5567
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5568
      transpose: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5569
        // Does nothing in Processing.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5570
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5571
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5572
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5573
       * The mult() function multiplied this matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5574
       * If two array elements are passed in the function will multiply a two element vector against this matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5575
       * If target is null or not length four, a new float array will be returned.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5576
       * The values for vec and target can be the same (though that's less efficient).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5577
       * If two PVectors are passed in the function multiply the x and y coordinates of a PVector against this matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5578
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5579
       * @param {PVector} source, target  the PVectors used to multiply this matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5580
       * @param {float[]} source, target  the arrays used to multiply this matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5581
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5582
       * @return {PVector|float[]} returns a PVector or an array representing the new matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5583
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5584
      mult: function(source, target) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5585
        var x, y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5586
        if (source instanceof PVector) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5587
          x = source.x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5588
          y = source.y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5589
          if (!target) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5590
            target = new PVector();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5591
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5592
        } else if (source instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5593
          x = source[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5594
          y = source[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5595
          if (!target) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5596
            target = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5597
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5598
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5599
        if (target instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5600
          target[0] = this.elements[0] * x + this.elements[1] * y + this.elements[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5601
          target[1] = this.elements[3] * x + this.elements[4] * y + this.elements[5];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5602
        } else if (target instanceof PVector) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5603
          target.x = this.elements[0] * x + this.elements[1] * y + this.elements[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5604
          target.y = this.elements[3] * x + this.elements[4] * y + this.elements[5];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5605
          target.z = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5606
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5607
        return target;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5608
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5609
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5610
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5611
       * The multX() function calculates the x component of a vector from a transformation.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5612
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5613
       * @param {float} x the x component of the vector being transformed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5614
       * @param {float} y the y component of the vector being transformed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5615
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5616
       * @return {float} returnes the result of the calculation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5617
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5618
      multX: function(x, y) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5619
        return (x * this.elements[0] + y * this.elements[1] + this.elements[2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5620
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5621
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5622
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5623
       * The multY() function calculates the y component of a vector from a transformation.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5624
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5625
       * @param {float} x the x component of the vector being transformed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5626
       * @param {float} y the y component of the vector being transformed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5627
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5628
       * @return {float} returnes the result of the calculation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5629
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5630
      multY: function(x, y) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5631
        return (x * this.elements[3] + y * this.elements[4] + this.elements[5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5632
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5633
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5634
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5635
       * The skewX() function skews the matrix along the x-axis the amount specified by the angle parameter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5636
       * Angles should be specified in radians (values from 0 to PI*2) or converted to radians with the <b>radians()</b> function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5637
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5638
       * @param {float} angle  angle of skew specified in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5639
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5640
      skewX: function(angle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5641
        this.apply(1, 0, 1, angle, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5642
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5643
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5644
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5645
       * The skewY() function skews the matrix along the y-axis the amount specified by the angle parameter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5646
       * Angles should be specified in radians (values from 0 to PI*2) or converted to radians with the <b>radians()</b> function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5647
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5648
       * @param {float} angle  angle of skew specified in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5649
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5650
      skewY: function(angle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5651
        this.apply(1, 0, 1,  0, angle, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5652
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5653
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5654
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5655
       * The determinant() function calvculates the determinant of this matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5656
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5657
       * @return {float} the determinant of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5658
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5659
      determinant: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5660
        return (this.elements[0] * this.elements[4] - this.elements[1] * this.elements[3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5661
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5662
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5663
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5664
       * The invert() function inverts this matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5665
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5666
       * @return {boolean} true if successful
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5667
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5668
      invert: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5669
        var d = this.determinant();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5670
        if (Math.abs( d ) > PConstants.MIN_INT) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5671
          var old00 = this.elements[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5672
          var old01 = this.elements[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5673
          var old02 = this.elements[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5674
          var old10 = this.elements[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5675
          var old11 = this.elements[4];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5676
          var old12 = this.elements[5];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5677
          this.elements[0] =  old11 / d;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5678
          this.elements[3] = -old10 / d;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5679
          this.elements[1] = -old01 / d;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5680
          this.elements[4] =  old00 / d;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5681
          this.elements[2] = (old01 * old12 - old11 * old02) / d;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5682
          this.elements[5] = (old10 * old02 - old00 * old12) / d;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5683
          return true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5684
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5685
        return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5686
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5687
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5688
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5689
       * The scale() function increases or decreases the size of a shape by expanding and contracting vertices. When only one parameter is specified scale will occur in all dimensions.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5690
       * This is equivalent to a two parameter call.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5691
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5692
       * @param {float} sx  the amount to scale on the x-axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5693
       * @param {float} sy  the amount to scale on the y-axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5694
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5695
      scale: function(sx, sy) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5696
        if (sx && !sy) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5697
          sy = sx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5698
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5699
        if (sx && sy) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5700
          this.elements[0] *= sx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5701
          this.elements[1] *= sy;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5702
          this.elements[3] *= sx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5703
          this.elements[4] *= sy;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5704
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5705
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5706
       /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5707
        * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5708
        * The invScale() function decreases or increases the size of a shape by contracting and expanding vertices. When only one parameter is specified scale will occur in all dimensions.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5709
        * This is equivalent to a two parameter call.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5710
        *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5711
        * @param {float} sx  the amount to scale on the x-axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5712
        * @param {float} sy  the amount to scale on the y-axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5713
        */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5714
      invScale: function(sx, sy) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5715
        if (sx && !sy) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5716
          sy = sx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5717
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5718
        this.scale(1 / sx, 1 / sy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5719
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5720
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5721
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5722
       * The apply() function multiplies the current matrix by the one specified through the parameters. Note that either a PMatrix2D or a list of floats can be passed in.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5723
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5724
       * @param {PMatrix2D} matrix    the matrix to apply this matrix to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5725
       * @param {float} m00           the first element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5726
       * @param {float} m01           the third element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5727
       * @param {float} m10           the fourth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5728
       * @param {float} m11           the fith element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5729
       * @param {float} m12           the sixth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5730
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5731
      apply: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5732
        var source;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5733
        if (arguments.length === 1 && arguments[0] instanceof PMatrix2D) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5734
          source = arguments[0].array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5735
        } else if (arguments.length === 6) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5736
          source = Array.prototype.slice.call(arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5737
        } else if (arguments.length === 1 && arguments[0] instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5738
          source = arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5739
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5740
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5741
        var result = [0, 0, this.elements[2],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5742
                      0, 0, this.elements[5]];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5743
        var e = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5744
        for (var row = 0; row < 2; row++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5745
          for (var col = 0; col < 3; col++, e++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5746
            result[e] += this.elements[row * 3 + 0] * source[col + 0] +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5747
                         this.elements[row * 3 + 1] * source[col + 3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5748
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5749
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5750
        this.elements = result.slice();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5751
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5752
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5753
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5754
       * The preApply() function applies another matrix to the left of this one. Note that either a PMatrix2D or elements of a matrix can be passed in.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5755
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5756
       * @param {PMatrix2D} matrix    the matrix to apply this matrix to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5757
       * @param {float} m00           the first element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5758
       * @param {float} m01           the third element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5759
       * @param {float} m10           the fourth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5760
       * @param {float} m11           the fith element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5761
       * @param {float} m12           the sixth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5762
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5763
      preApply: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5764
        var source;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5765
        if (arguments.length === 1 && arguments[0] instanceof PMatrix2D) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5766
          source = arguments[0].array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5767
        } else if (arguments.length === 6) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5768
          source = Array.prototype.slice.call(arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5769
        } else if (arguments.length === 1 && arguments[0] instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5770
          source = arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5771
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5772
        var result = [0, 0, source[2],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5773
                      0, 0, source[5]];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5774
        result[2] = source[2] + this.elements[2] * source[0] + this.elements[5] * source[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5775
        result[5] = source[5] + this.elements[2] * source[3] + this.elements[5] * source[4];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5776
        result[0] = this.elements[0] * source[0] + this.elements[3] * source[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5777
        result[3] = this.elements[0] * source[3] + this.elements[3] * source[4];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5778
        result[1] = this.elements[1] * source[0] + this.elements[4] * source[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5779
        result[4] = this.elements[1] * source[3] + this.elements[4] * source[4];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5780
        this.elements = result.slice();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5781
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5782
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5783
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5784
       * The rotate() function rotates the matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5785
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5786
       * @param {float} angle         the angle of rotation in radiants
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5787
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5788
      rotate: function(angle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5789
        var c = Math.cos(angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5790
        var s = Math.sin(angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5791
        var temp1 = this.elements[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5792
        var temp2 = this.elements[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5793
        this.elements[0] =  c * temp1 + s * temp2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5794
        this.elements[1] = -s * temp1 + c * temp2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5795
        temp1 = this.elements[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5796
        temp2 = this.elements[4];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5797
        this.elements[3] =  c * temp1 + s * temp2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5798
        this.elements[4] = -s * temp1 + c * temp2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5799
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5800
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5801
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5802
       * The rotateZ() function rotates the matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5803
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5804
       * @param {float} angle         the angle of rotation in radiants
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5805
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5806
      rotateZ: function(angle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5807
        this.rotate(angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5808
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5809
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5810
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5811
       * The invRotateZ() function rotates the matrix in opposite direction.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5812
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5813
       * @param {float} angle         the angle of rotation in radiants
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5814
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5815
      invRotateZ: function(angle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5816
        this.rotateZ(angle - Math.PI);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5817
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5818
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5819
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5820
       * The print() function prints out the elements of this matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5821
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5822
      print: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5823
        var digits = printMatrixHelper(this.elements);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5824
        var output = "" + p.nfs(this.elements[0], digits, 4) + " " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5825
                     p.nfs(this.elements[1], digits, 4) + " " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5826
                     p.nfs(this.elements[2], digits, 4) + "\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5827
                     p.nfs(this.elements[3], digits, 4) + " " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5828
                     p.nfs(this.elements[4], digits, 4) + " " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5829
                     p.nfs(this.elements[5], digits, 4) + "\n\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5830
        p.println(output);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5831
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5832
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5833
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5834
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5835
     * PMatrix3D is a 4x4  matrix implementation. The constructor accepts another PMatrix3D or a list of six or sixteen float elements.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5836
     * If no parameters are provided the matrix is set to the identity matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5837
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5838
    var PMatrix3D = p.PMatrix3D = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5839
      // When a matrix is created, it is set to an identity matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5840
      this.reset();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5841
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5842
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5843
     * PMatrix3D methods
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5844
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5845
    PMatrix3D.prototype = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5846
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5847
       * @member PMatrix2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5848
       * The set() function sets the matrix elements. The function accepts either another PMatrix3D, an array of elements, or a list of six or sixteen floats.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5849
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5850
       * @param {PMatrix3D} matrix    the initial matrix to set to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5851
       * @param {float[]} elements    an array of elements to set this matrix to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5852
       * @param {float} m00           the first element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5853
       * @param {float} m01           the second element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5854
       * @param {float} m02           the third element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5855
       * @param {float} m03           the fourth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5856
       * @param {float} m10           the fifth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5857
       * @param {float} m11           the sixth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5858
       * @param {float} m12           the seventh element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5859
       * @param {float} m13           the eight element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5860
       * @param {float} m20           the nineth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5861
       * @param {float} m21           the tenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5862
       * @param {float} m22           the eleventh element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5863
       * @param {float} m23           the twelveth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5864
       * @param {float} m30           the thirteenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5865
       * @param {float} m31           the fourtheenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5866
       * @param {float} m32           the fivetheenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5867
       * @param {float} m33           the sixteenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5868
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5869
      set: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5870
        if (arguments.length === 16) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5871
          this.elements = Array.prototype.slice.call(arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5872
        } else if (arguments.length === 1 && arguments[0] instanceof PMatrix3D) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5873
          this.elements = arguments[0].array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5874
        } else if (arguments.length === 1 && arguments[0] instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5875
          this.elements = arguments[0].slice();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5876
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5877
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5878
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5879
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5880
       * The get() function returns a copy of this PMatrix3D.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5881
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5882
       * @return {PMatrix3D} a copy of this PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5883
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5884
      get: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5885
        var outgoing = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5886
        outgoing.set(this.elements);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5887
        return outgoing;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5888
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5889
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5890
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5891
       * The reset() function sets this PMatrix3D to the identity matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5892
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5893
      reset: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5894
        this.elements = [1,0,0,0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5895
                         0,1,0,0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5896
                         0,0,1,0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5897
                         0,0,0,1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5898
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5899
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5900
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5901
       * The array() function returns a copy of the element values.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5902
       * @addon
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5903
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5904
       * @return {float[]} returns a copy of the element values
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5905
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5906
      array: function array() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5907
        return this.elements.slice();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5908
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5909
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5910
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5911
       * The translate() function translates this matrix by moving the current coordinates to the location specified by tx, ty, and tz.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5912
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5913
       * @param {float} tx  the x-axis coordinate to move to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5914
       * @param {float} ty  the y-axis coordinate to move to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5915
       * @param {float} tz  the z-axis coordinate to move to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5916
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5917
      translate: function(tx, ty, tz) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5918
        if (tz === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5919
          tz = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5920
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5921
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5922
        this.elements[3]  += tx * this.elements[0]  + ty * this.elements[1]  + tz * this.elements[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5923
        this.elements[7]  += tx * this.elements[4]  + ty * this.elements[5]  + tz * this.elements[6];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5924
        this.elements[11] += tx * this.elements[8]  + ty * this.elements[9]  + tz * this.elements[10];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5925
        this.elements[15] += tx * this.elements[12] + ty * this.elements[13] + tz * this.elements[14];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5926
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5927
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5928
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5929
       * The transpose() function transpose this matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5930
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5931
      transpose: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5932
        var temp = this.elements[4];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5933
        this.elements[4] = this.elements[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5934
        this.elements[1] = temp;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5935
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5936
        temp = this.elements[8];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5937
        this.elements[8] = this.elements[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5938
        this.elements[2] = temp;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5939
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5940
        temp = this.elements[6];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5941
        this.elements[6] = this.elements[9];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5942
        this.elements[9] = temp;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5943
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5944
        temp = this.elements[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5945
        this.elements[3] = this.elements[12];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5946
        this.elements[12] = temp;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5947
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5948
        temp = this.elements[7];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5949
        this.elements[7] = this.elements[13];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5950
        this.elements[13] = temp;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5951
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5952
        temp = this.elements[11];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5953
        this.elements[11] = this.elements[14];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5954
        this.elements[14] = temp;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5955
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5956
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5957
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5958
       * The mult() function multiplied this matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5959
       * If two array elements are passed in the function will multiply a two element vector against this matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5960
       * If target is null or not length four, a new float array will be returned.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5961
       * The values for vec and target can be the same (though that's less efficient).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5962
       * If two PVectors are passed in the function multiply the x and y coordinates of a PVector against this matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5963
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5964
       * @param {PVector} source, target  the PVectors used to multiply this matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5965
       * @param {float[]} source, target  the arrays used to multiply this matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5966
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5967
       * @return {PVector|float[]} returns a PVector or an array representing the new matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5968
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5969
      mult: function(source, target) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5970
        var x, y, z, w;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5971
        if (source instanceof PVector) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5972
          x = source.x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5973
          y = source.y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5974
          z = source.z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5975
          w = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5976
          if (!target) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5977
            target = new PVector();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5978
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5979
        } else if (source instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5980
          x = source[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5981
          y = source[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5982
          z = source[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5983
          w = source[3] || 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5984
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5985
          if ( !target || (target.length !== 3 && target.length !== 4) ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5986
            target = [0, 0, 0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5987
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5988
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5989
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5990
        if (target instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5991
          if (target.length === 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5992
            target[0] = this.elements[0] * x + this.elements[1] * y + this.elements[2] * z + this.elements[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5993
            target[1] = this.elements[4] * x + this.elements[5] * y + this.elements[6] * z + this.elements[7];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5994
            target[2] = this.elements[8] * x + this.elements[9] * y + this.elements[10] * z + this.elements[11];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5995
          } else if (target.length === 4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5996
            target[0] = this.elements[0] * x + this.elements[1] * y + this.elements[2] * z + this.elements[3] * w;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5997
            target[1] = this.elements[4] * x + this.elements[5] * y + this.elements[6] * z + this.elements[7] * w;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5998
            target[2] = this.elements[8] * x + this.elements[9] * y + this.elements[10] * z + this.elements[11] * w;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  5999
            target[3] = this.elements[12] * x + this.elements[13] * y + this.elements[14] * z + this.elements[15] * w;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6000
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6001
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6002
        if (target instanceof PVector) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6003
          target.x = this.elements[0] * x + this.elements[1] * y + this.elements[2] * z + this.elements[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6004
          target.y = this.elements[4] * x + this.elements[5] * y + this.elements[6] * z + this.elements[7];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6005
          target.z = this.elements[8] * x + this.elements[9] * y + this.elements[10] * z + this.elements[11];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6006
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6007
        return target;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6008
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6009
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6010
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6011
       * The preApply() function applies another matrix to the left of this one. Note that either a PMatrix3D or elements of a matrix can be passed in.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6012
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6013
       * @param {PMatrix3D} matrix    the matrix to apply this matrix to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6014
       * @param {float} m00           the first element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6015
       * @param {float} m01           the second element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6016
       * @param {float} m02           the third element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6017
       * @param {float} m03           the fourth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6018
       * @param {float} m10           the fifth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6019
       * @param {float} m11           the sixth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6020
       * @param {float} m12           the seventh element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6021
       * @param {float} m13           the eight element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6022
       * @param {float} m20           the nineth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6023
       * @param {float} m21           the tenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6024
       * @param {float} m22           the eleventh element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6025
       * @param {float} m23           the twelveth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6026
       * @param {float} m30           the thirteenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6027
       * @param {float} m31           the fourtheenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6028
       * @param {float} m32           the fivetheenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6029
       * @param {float} m33           the sixteenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6030
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6031
      preApply: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6032
        var source;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6033
        if (arguments.length === 1 && arguments[0] instanceof PMatrix3D) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6034
          source = arguments[0].array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6035
        } else if (arguments.length === 16) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6036
          source = Array.prototype.slice.call(arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6037
        } else if (arguments.length === 1 && arguments[0] instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6038
          source = arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6039
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6040
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6041
        var result = [0, 0, 0, 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6042
                      0, 0, 0, 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6043
                      0, 0, 0, 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6044
                      0, 0, 0, 0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6045
        var e = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6046
        for (var row = 0; row < 4; row++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6047
          for (var col = 0; col < 4; col++, e++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6048
            result[e] += this.elements[col + 0] * source[row * 4 + 0] + this.elements[col + 4] *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6049
                         source[row * 4 + 1] + this.elements[col + 8] * source[row * 4 + 2] +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6050
                         this.elements[col + 12] * source[row * 4 + 3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6051
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6052
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6053
        this.elements = result.slice();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6054
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6055
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6056
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6057
       * The apply() function multiplies the current matrix by the one specified through the parameters. Note that either a PMatrix3D or a list of floats can be passed in.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6058
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6059
       * @param {PMatrix3D} matrix    the matrix to apply this matrix to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6060
       * @param {float} m00           the first element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6061
       * @param {float} m01           the second element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6062
       * @param {float} m02           the third element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6063
       * @param {float} m03           the fourth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6064
       * @param {float} m10           the fifth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6065
       * @param {float} m11           the sixth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6066
       * @param {float} m12           the seventh element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6067
       * @param {float} m13           the eight element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6068
       * @param {float} m20           the nineth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6069
       * @param {float} m21           the tenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6070
       * @param {float} m22           the eleventh element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6071
       * @param {float} m23           the twelveth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6072
       * @param {float} m30           the thirteenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6073
       * @param {float} m31           the fourtheenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6074
       * @param {float} m32           the fivetheenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6075
       * @param {float} m33           the sixteenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6076
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6077
      apply: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6078
        var source;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6079
        if (arguments.length === 1 && arguments[0] instanceof PMatrix3D) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6080
          source = arguments[0].array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6081
        } else if (arguments.length === 16) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6082
          source = Array.prototype.slice.call(arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6083
        } else if (arguments.length === 1 && arguments[0] instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6084
          source = arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6085
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6086
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6087
        var result = [0, 0, 0, 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6088
                      0, 0, 0, 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6089
                      0, 0, 0, 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6090
                      0, 0, 0, 0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6091
        var e = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6092
        for (var row = 0; row < 4; row++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6093
          for (var col = 0; col < 4; col++, e++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6094
            result[e] += this.elements[row * 4 + 0] * source[col + 0] + this.elements[row * 4 + 1] *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6095
                         source[col + 4] + this.elements[row * 4 + 2] * source[col + 8] +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6096
                         this.elements[row * 4 + 3] * source[col + 12];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6097
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6098
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6099
        this.elements = result.slice();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6100
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6101
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6102
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6103
       * The rotate() function rotates the matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6104
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6105
       * @param {float} angle         the angle of rotation in radiants
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6106
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6107
      rotate: function(angle, v0, v1, v2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6108
        if (!v1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6109
          this.rotateZ(angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6110
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6111
          // TODO should make sure this vector is normalized
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6112
          var c = p.cos(angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6113
          var s = p.sin(angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6114
          var t = 1.0 - c;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6115
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6116
          this.apply((t * v0 * v0) + c,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6117
                     (t * v0 * v1) - (s * v2),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6118
                     (t * v0 * v2) + (s * v1),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6119
                     0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6120
                     (t * v0 * v1) + (s * v2),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6121
                     (t * v1 * v1) + c,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6122
                     (t * v1 * v2) - (s * v0),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6123
                     0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6124
                     (t * v0 * v2) - (s * v1),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6125
                     (t * v1 * v2) + (s * v0),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6126
                     (t * v2 * v2) + c,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6127
                     0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6128
                     0, 0, 0, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6129
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6130
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6131
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6132
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6133
       * The invApply() function applies the inverted matrix to this matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6134
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6135
       * @param {float} m00           the first element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6136
       * @param {float} m01           the second element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6137
       * @param {float} m02           the third element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6138
       * @param {float} m03           the fourth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6139
       * @param {float} m10           the fifth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6140
       * @param {float} m11           the sixth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6141
       * @param {float} m12           the seventh element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6142
       * @param {float} m13           the eight element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6143
       * @param {float} m20           the nineth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6144
       * @param {float} m21           the tenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6145
       * @param {float} m22           the eleventh element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6146
       * @param {float} m23           the twelveth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6147
       * @param {float} m30           the thirteenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6148
       * @param {float} m31           the fourtheenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6149
       * @param {float} m32           the fivetheenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6150
       * @param {float} m33           the sixteenth element of the matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6151
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6152
       * @return {boolean} returns true if the operation was successful.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6153
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6154
      invApply: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6155
        if (inverseCopy === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6156
          inverseCopy = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6157
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6158
        var a = arguments;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6159
        inverseCopy.set(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6160
                        a[9], a[10], a[11], a[12], a[13], a[14], a[15]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6161
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6162
        if (!inverseCopy.invert()) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6163
          return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6164
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6165
        this.preApply(inverseCopy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6166
        return true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6167
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6168
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6169
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6170
       * The rotateZ() function rotates the matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6171
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6172
       * @param {float} angle         the angle of rotation in radiants
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6173
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6174
      rotateX: function(angle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6175
        var c = p.cos(angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6176
        var s = p.sin(angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6177
        this.apply([1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0, 0, 0, 0, 1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6178
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6179
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6180
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6181
       * The rotateY() function rotates the matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6182
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6183
       * @param {float} angle         the angle of rotation in radiants
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6184
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6185
      rotateY: function(angle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6186
        var c = p.cos(angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6187
        var s = p.sin(angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6188
        this.apply([c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0, 0, 0, 0, 1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6189
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6190
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6191
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6192
       * The rotateZ() function rotates the matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6193
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6194
       * @param {float} angle         the angle of rotation in radiants
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6195
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6196
      rotateZ: function(angle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6197
        var c = Math.cos(angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6198
        var s = Math.sin(angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6199
        this.apply([c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6200
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6201
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6202
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6203
       * The scale() function increases or decreases the size of a matrix by expanding and contracting vertices. When only one parameter is specified scale will occur in all dimensions.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6204
       * This is equivalent to a three parameter call.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6205
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6206
       * @param {float} sx  the amount to scale on the x-axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6207
       * @param {float} sy  the amount to scale on the y-axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6208
       * @param {float} sz  the amount to scale on the z-axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6209
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6210
      scale: function(sx, sy, sz) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6211
        if (sx && !sy && !sz) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6212
          sy = sz = sx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6213
        } else if (sx && sy && !sz) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6214
          sz = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6215
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6216
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6217
        if (sx && sy && sz) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6218
          this.elements[0]  *= sx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6219
          this.elements[1]  *= sy;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6220
          this.elements[2]  *= sz;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6221
          this.elements[4]  *= sx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6222
          this.elements[5]  *= sy;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6223
          this.elements[6]  *= sz;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6224
          this.elements[8]  *= sx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6225
          this.elements[9]  *= sy;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6226
          this.elements[10] *= sz;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6227
          this.elements[12] *= sx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6228
          this.elements[13] *= sy;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6229
          this.elements[14] *= sz;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6230
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6231
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6232
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6233
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6234
       * The skewX() function skews the matrix along the x-axis the amount specified by the angle parameter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6235
       * Angles should be specified in radians (values from 0 to PI*2) or converted to radians with the <b>radians()</b> function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6236
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6237
       * @param {float} angle  angle of skew specified in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6238
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6239
      skewX: function(angle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6240
        var t = Math.tan(angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6241
        this.apply(1, t, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6242
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6243
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6244
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6245
       * The skewY() function skews the matrix along the y-axis the amount specified by the angle parameter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6246
       * Angles should be specified in radians (values from 0 to PI*2) or converted to radians with the <b>radians()</b> function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6247
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6248
       * @param {float} angle  angle of skew specified in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6249
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6250
      skewY: function(angle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6251
        var t = Math.tan(angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6252
        this.apply(1, 0, 0, 0, t, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6253
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6254
      multX: function(x, y, z, w) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6255
        if (!z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6256
          return this.elements[0] * x + this.elements[1] * y + this.elements[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6257
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6258
        if (!w) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6259
          return this.elements[0] * x + this.elements[1] * y + this.elements[2] * z + this.elements[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6260
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6261
        return this.elements[0] * x + this.elements[1] * y + this.elements[2] * z + this.elements[3] * w;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6262
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6263
      multY: function(x, y, z, w) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6264
        if (!z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6265
          return this.elements[4] * x + this.elements[5] * y + this.elements[7];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6266
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6267
        if (!w) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6268
          return this.elements[4] * x + this.elements[5] * y + this.elements[6] * z + this.elements[7];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6269
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6270
        return this.elements[4] * x + this.elements[5] * y + this.elements[6] * z + this.elements[7] * w;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6271
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6272
      multZ: function(x, y, z, w) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6273
        if (!w) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6274
          return this.elements[8] * x + this.elements[9] * y + this.elements[10] * z + this.elements[11];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6275
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6276
        return this.elements[8] * x + this.elements[9] * y + this.elements[10] * z + this.elements[11] * w;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6277
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6278
      multW: function(x, y, z, w) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6279
        if (!w) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6280
          return this.elements[12] * x + this.elements[13] * y + this.elements[14] * z + this.elements[15];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6281
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6282
        return this.elements[12] * x + this.elements[13] * y + this.elements[14] * z + this.elements[15] * w;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6283
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6284
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6285
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6286
       * The invert() function inverts this matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6287
       *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6288
       * @return {boolean} true if successful
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6289
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6290
      invert: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6291
        var fA0 = this.elements[0] * this.elements[5] - this.elements[1] * this.elements[4];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6292
        var fA1 = this.elements[0] * this.elements[6] - this.elements[2] * this.elements[4];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6293
        var fA2 = this.elements[0] * this.elements[7] - this.elements[3] * this.elements[4];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6294
        var fA3 = this.elements[1] * this.elements[6] - this.elements[2] * this.elements[5];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6295
        var fA4 = this.elements[1] * this.elements[7] - this.elements[3] * this.elements[5];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6296
        var fA5 = this.elements[2] * this.elements[7] - this.elements[3] * this.elements[6];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6297
        var fB0 = this.elements[8] * this.elements[13] - this.elements[9] * this.elements[12];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6298
        var fB1 = this.elements[8] * this.elements[14] - this.elements[10] * this.elements[12];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6299
        var fB2 = this.elements[8] * this.elements[15] - this.elements[11] * this.elements[12];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6300
        var fB3 = this.elements[9] * this.elements[14] - this.elements[10] * this.elements[13];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6301
        var fB4 = this.elements[9] * this.elements[15] - this.elements[11] * this.elements[13];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6302
        var fB5 = this.elements[10] * this.elements[15] - this.elements[11] * this.elements[14];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6303
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6304
        // Determinant
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6305
        var fDet = fA0 * fB5 - fA1 * fB4 + fA2 * fB3 + fA3 * fB2 - fA4 * fB1 + fA5 * fB0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6306
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6307
        // Account for a very small value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6308
        // return false if not successful.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6309
        if (Math.abs(fDet) <= 1e-9) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6310
          return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6311
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6312
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6313
        var kInv = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6314
        kInv[0]  = +this.elements[5] * fB5 - this.elements[6] * fB4 + this.elements[7] * fB3;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6315
        kInv[4]  = -this.elements[4] * fB5 + this.elements[6] * fB2 - this.elements[7] * fB1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6316
        kInv[8]  = +this.elements[4] * fB4 - this.elements[5] * fB2 + this.elements[7] * fB0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6317
        kInv[12] = -this.elements[4] * fB3 + this.elements[5] * fB1 - this.elements[6] * fB0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6318
        kInv[1]  = -this.elements[1] * fB5 + this.elements[2] * fB4 - this.elements[3] * fB3;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6319
        kInv[5]  = +this.elements[0] * fB5 - this.elements[2] * fB2 + this.elements[3] * fB1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6320
        kInv[9]  = -this.elements[0] * fB4 + this.elements[1] * fB2 - this.elements[3] * fB0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6321
        kInv[13] = +this.elements[0] * fB3 - this.elements[1] * fB1 + this.elements[2] * fB0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6322
        kInv[2]  = +this.elements[13] * fA5 - this.elements[14] * fA4 + this.elements[15] * fA3;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6323
        kInv[6]  = -this.elements[12] * fA5 + this.elements[14] * fA2 - this.elements[15] * fA1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6324
        kInv[10] = +this.elements[12] * fA4 - this.elements[13] * fA2 + this.elements[15] * fA0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6325
        kInv[14] = -this.elements[12] * fA3 + this.elements[13] * fA1 - this.elements[14] * fA0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6326
        kInv[3]  = -this.elements[9] * fA5 + this.elements[10] * fA4 - this.elements[11] * fA3;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6327
        kInv[7]  = +this.elements[8] * fA5 - this.elements[10] * fA2 + this.elements[11] * fA1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6328
        kInv[11] = -this.elements[8] * fA4 + this.elements[9] * fA2 - this.elements[11] * fA0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6329
        kInv[15] = +this.elements[8] * fA3 - this.elements[9] * fA1 + this.elements[10] * fA0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6330
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6331
        // Inverse using Determinant
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6332
        var fInvDet = 1.0 / fDet;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6333
        kInv[0]  *= fInvDet;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6334
        kInv[1]  *= fInvDet;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6335
        kInv[2]  *= fInvDet;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6336
        kInv[3]  *= fInvDet;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6337
        kInv[4]  *= fInvDet;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6338
        kInv[5]  *= fInvDet;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6339
        kInv[6]  *= fInvDet;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6340
        kInv[7]  *= fInvDet;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6341
        kInv[8]  *= fInvDet;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6342
        kInv[9]  *= fInvDet;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6343
        kInv[10] *= fInvDet;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6344
        kInv[11] *= fInvDet;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6345
        kInv[12] *= fInvDet;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6346
        kInv[13] *= fInvDet;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6347
        kInv[14] *= fInvDet;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6348
        kInv[15] *= fInvDet;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6349
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6350
        this.elements = kInv.slice();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6351
        return true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6352
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6353
      toString: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6354
        var str = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6355
        for (var i = 0; i < 15; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6356
          str += this.elements[i] + ", ";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6357
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6358
        str += this.elements[15];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6359
        return str;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6360
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6361
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6362
       * @member PMatrix3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6363
       * The print() function prints out the elements of this matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6364
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6365
      print: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6366
        var digits = printMatrixHelper(this.elements);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6367
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6368
        var output = "" + p.nfs(this.elements[0], digits, 4) + " " + p.nfs(this.elements[1], digits, 4) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6369
                     " " + p.nfs(this.elements[2], digits, 4) + " " + p.nfs(this.elements[3], digits, 4) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6370
                     "\n" + p.nfs(this.elements[4], digits, 4) + " " + p.nfs(this.elements[5], digits, 4) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6371
                     " " + p.nfs(this.elements[6], digits, 4) + " " + p.nfs(this.elements[7], digits, 4) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6372
                     "\n" + p.nfs(this.elements[8], digits, 4) + " " + p.nfs(this.elements[9], digits, 4) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6373
                     " " + p.nfs(this.elements[10], digits, 4) + " " + p.nfs(this.elements[11], digits, 4) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6374
                     "\n" + p.nfs(this.elements[12], digits, 4) + " " + p.nfs(this.elements[13], digits, 4) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6375
                     " " + p.nfs(this.elements[14], digits, 4) + " " + p.nfs(this.elements[15], digits, 4) + "\n\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6376
        p.println(output);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6377
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6378
      invTranslate: function(tx, ty, tz) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6379
        this.preApply(1, 0, 0, -tx, 0, 1, 0, -ty, 0, 0, 1, -tz, 0, 0, 0, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6380
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6381
      invRotateX: function(angle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6382
        var c = Math.cos(-angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6383
        var s = Math.sin(-angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6384
        this.preApply([1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0, 0, 0, 0, 1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6385
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6386
      invRotateY: function(angle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6387
        var c = Math.cos(-angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6388
        var s = Math.sin(-angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6389
        this.preApply([c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0, 0, 0, 0, 1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6390
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6391
      invRotateZ: function(angle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6392
        var c = Math.cos(-angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6393
        var s = Math.sin(-angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6394
        this.preApply([c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6395
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6396
      invScale: function(x, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6397
        this.preApply([1 / x, 0, 0, 0, 0, 1 / y, 0, 0, 0, 0, 1 / z, 0, 0, 0, 0, 1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6398
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6399
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6400
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6401
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6402
     * @private
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6403
     * The matrix stack stores the transformations and translations that occur within the space.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6404
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6405
    var PMatrixStack = p.PMatrixStack = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6406
      this.matrixStack = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6407
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6408
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6409
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6410
     * @member PMatrixStack
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6411
     * load pushes the matrix given in the function into the stack
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6412
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6413
     * @param {Object | Array} matrix the matrix to be pushed into the stack
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6414
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6415
    PMatrixStack.prototype.load = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6416
      var tmpMatrix = drawing.$newPMatrix();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6417
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6418
      if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6419
        tmpMatrix.set(arguments[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6420
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6421
        tmpMatrix.set(arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6422
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6423
      this.matrixStack.push(tmpMatrix);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6424
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6425
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6426
    Drawing2D.prototype.$newPMatrix = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6427
      return new PMatrix2D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6428
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6429
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6430
    Drawing3D.prototype.$newPMatrix = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6431
      return new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6432
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6433
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6434
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6435
     * @member PMatrixStack
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6436
     * push adds a duplicate of the top of the stack onto the stack - uses the peek function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6437
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6438
    PMatrixStack.prototype.push = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6439
      this.matrixStack.push(this.peek());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6440
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6441
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6442
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6443
     * @member PMatrixStack
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6444
     * pop removes returns the matrix at the top of the stack
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6445
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6446
     * @returns {Object} the matrix at the top of the stack
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6447
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6448
    PMatrixStack.prototype.pop = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6449
      return this.matrixStack.pop();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6450
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6451
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6452
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6453
     * @member PMatrixStack
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6454
     * peek returns but doesn't remove the matrix at the top of the stack
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6455
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6456
     * @returns {Object} the matrix at the top of the stack
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6457
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6458
    PMatrixStack.prototype.peek = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6459
      var tmpMatrix = drawing.$newPMatrix();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6460
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6461
      tmpMatrix.set(this.matrixStack[this.matrixStack.length - 1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6462
      return tmpMatrix;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6463
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6464
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6465
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6466
     * @member PMatrixStack
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6467
     * this function multiplies the matrix at the top of the stack with the matrix given as a parameter
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6468
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6469
     * @param {Object | Array} matrix the matrix to be multiplied into the stack
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6470
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6471
    PMatrixStack.prototype.mult = function(matrix) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6472
      this.matrixStack[this.matrixStack.length - 1].apply(matrix);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6473
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6474
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6475
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6476
    // Array handling
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6477
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6478
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6479
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6480
    * The split() function breaks a string into pieces using a character or string
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6481
    * as the divider. The delim  parameter specifies the character or characters that
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6482
    * mark the boundaries between each piece. A String[] array is returned that contains
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6483
    * each of the pieces.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6484
    * If the result is a set of numbers, you can convert the String[] array to to a float[]
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6485
    * or int[] array using the datatype conversion functions int() and float() (see example above).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6486
    * The splitTokens() function works in a similar fashion, except that it splits using a range
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6487
    * of characters instead of a specific character or sequence.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6488
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6489
    * @param {String} str       the String to be split
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6490
    * @param {String} delim     the character or String used to separate the data
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6491
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6492
    * @returns {string[]} The new string array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6493
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6494
    * @see splitTokens
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6495
    * @see join
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6496
    * @see trim
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6497
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6498
    p.split = function(str, delim) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6499
      return str.split(delim);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6500
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6501
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6502
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6503
    * The splitTokens() function splits a String at one or many character "tokens." The tokens
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6504
    * parameter specifies the character or characters to be used as a boundary.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6505
    * If no tokens character is specified, any whitespace character is used to split.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6506
    * Whitespace characters include tab (\t), line feed (\n), carriage return (\r), form
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6507
    * feed (\f), and space. To convert a String to an array of integers or floats, use the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6508
    * datatype conversion functions int() and float() to convert the array of Strings.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6509
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6510
    * @param {String} str       the String to be split
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6511
    * @param {Char[]} tokens    list of individual characters that will be used as separators
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6512
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6513
    * @returns {string[]} The new string array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6514
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6515
    * @see split
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6516
    * @see join
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6517
    * @see trim
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6518
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6519
    p.splitTokens = function(str, tokens) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6520
      if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6521
        tokens = "\n\t\r\f ";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6522
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6523
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6524
      tokens = "[" + tokens + "]";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6525
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6526
      var ary = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6527
      var index = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6528
      var pos = str.search(tokens);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6529
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6530
      while (pos >= 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6531
        if (pos === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6532
          str = str.substring(1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6533
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6534
          ary[index] = str.substring(0, pos);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6535
          index++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6536
          str = str.substring(pos);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6537
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6538
        pos = str.search(tokens);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6539
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6540
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6541
      if (str.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6542
        ary[index] = str;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6543
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6544
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6545
      if (ary.length === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6546
        ary = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6547
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6548
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6549
      return ary;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6550
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6551
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6552
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6553
    * Expands an array by one element and adds data to the new position. The datatype of
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6554
    * the element parameter must be the same as the datatype of the array.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6555
    * When using an array of objects, the data returned from the function must be cast to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6556
    * the object array's data type. For example: SomeClass[] items = (SomeClass[])
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6557
    * append(originalArray, element).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6558
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6559
    * @param {boolean[]|byte[]|char[]|int[]|float[]|String[]|array of objects} array boolean[],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6560
    * byte[], char[], int[], float[], or String[], or an array of objects
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6561
    * @param {boolean[]|byte[]|char[]|int[]|float[]|String[]|array of objects} element new data for the array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6562
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6563
    * @returns Array (the same datatype as the input)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6564
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6565
    * @see shorten
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6566
    * @see expand
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6567
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6568
    p.append = function(array, element) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6569
      array[array.length] = element;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6570
      return array;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6571
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6572
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6573
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6574
    * Concatenates two arrays. For example, concatenating the array { 1, 2, 3 } and the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6575
    * array { 4, 5, 6 } yields { 1, 2, 3, 4, 5, 6 }. Both parameters must be arrays of the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6576
    * same datatype.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6577
    * When using an array of objects, the data returned from the function must be cast to the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6578
    * object array's data type. For example: SomeClass[] items = (SomeClass[]) concat(array1, array2).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6579
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6580
    * @param {boolean[]|byte[]|char[]|int[]|float[]|String[]|array of objects} array1 boolean[],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6581
    * byte[], char[], int[], float[], String[], or an array of objects
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6582
    * @param {boolean[]|byte[]|char[]|int[]|float[]|String[]|array of objects} array2 boolean[],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6583
    * byte[], char[], int[], float[], String[], or an array of objects
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6584
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6585
    * @returns Array (the same datatype as the input)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6586
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6587
    * @see splice
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6588
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6589
    p.concat = function(array1, array2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6590
      return array1.concat(array2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6591
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6592
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6593
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6594
     * Sorts an array of numbers from smallest to largest and puts an array of
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6595
     * words in alphabetical order. The original array is not modified, a
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6596
     * re-ordered array is returned. The count parameter states the number of
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6597
     * elements to sort. For example if there are 12 elements in an array and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6598
     * if count is the value 5, only the first five elements on the array will
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6599
     * be sorted. Alphabetical ordering is case insensitive.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6600
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6601
     * @param {String[] | int[] | float[]}  array Array of elements to sort
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6602
     * @param {int}                         numElem Number of elements to sort
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6603
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6604
     * @returns {String[] | int[] | float[]} Array (same datatype as the input)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6605
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6606
     * @see reverse
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6607
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6608
    p.sort = function(array, numElem) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6609
      var ret = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6610
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6611
      // depending on the type used (int, float) or string
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6612
      // we'll need to use a different compare function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6613
      if (array.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6614
        // copy since we need to return another array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6615
        var elemsToCopy = numElem > 0 ? numElem : array.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6616
        for (var i = 0; i < elemsToCopy; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6617
          ret.push(array[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6618
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6619
        if (typeof array[0] === "string") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6620
          ret.sort();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6621
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6622
        // int or float
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6623
        else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6624
          ret.sort(function(a, b) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6625
            return a - b;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6626
          });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6627
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6628
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6629
        // copy on the rest of the elements that were not sorted in case the user
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6630
        // only wanted a subset of an array to be sorted.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6631
        if (numElem > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6632
          for (var j = ret.length; j < array.length; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6633
            ret.push(array[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6634
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6635
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6636
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6637
      return ret;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6638
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6639
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6640
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6641
    * Inserts a value or array of values into an existing array. The first two parameters must
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6642
    * be of the same datatype. The array parameter defines the array which will be modified
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6643
    * and the second parameter defines the data which will be inserted. When using an array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6644
    * of objects, the data returned from the function must be cast to the object array's data
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6645
    * type. For example: SomeClass[] items = (SomeClass[]) splice(array1, array2, index).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6646
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6647
    * @param {boolean[]|byte[]|char[]|int[]|float[]|String[]|array of objects} array boolean[],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6648
    * byte[], char[], int[], float[], String[], or an array of objects
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6649
    * @param {boolean|byte|char|int|float|String|boolean[]|byte[]|char[]|int[]|float[]|String[]|array of objects}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6650
    * value boolean, byte, char, int, float, String, boolean[], byte[], char[], int[],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6651
    * float[], String[], or other Object: value or an array of objects to be spliced in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6652
    * @param {int} index                position in the array from which to insert data
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6653
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6654
    * @returns Array (the same datatype as the input)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6655
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6656
    * @see contract
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6657
    * @see subset
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6658
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6659
    p.splice = function(array, value, index) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6660
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6661
      // Trying to splice an empty array into "array" in P5 won't do
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6662
      // anything, just return the original.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6663
      if(value.length === 0)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6664
      {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6665
        return array;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6666
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6667
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6668
      // If the second argument was an array, we'll need to iterate over all
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6669
      // the "value" elements and add one by one because
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6670
      // array.splice(index, 0, value);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6671
      // would create a multi-dimensional array which isn't what we want.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6672
      if(value instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6673
        for(var i = 0, j = index; i < value.length; j++,i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6674
          array.splice(j, 0, value[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6675
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6676
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6677
        array.splice(index, 0, value);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6678
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6679
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6680
      return array;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6681
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6682
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6683
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6684
    * Extracts an array of elements from an existing array. The array parameter defines the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6685
    * array from which the elements will be copied and the offset and length parameters determine
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6686
    * which elements to extract. If no length is given, elements will be extracted from the offset
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6687
    * to the end of the array. When specifying the offset remember the first array element is 0.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6688
    * This function does not change the source array.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6689
    * When using an array of objects, the data returned from the function must be cast to the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6690
    * object array's data type.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6691
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6692
    * @param {boolean[]|byte[]|char[]|int[]|float[]|String[]|array of objects} array boolean[],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6693
    * byte[], char[], int[], float[], String[], or an array of objects
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6694
    * @param {int} offset         position to begin
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6695
    * @param {int} length         number of values to extract
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6696
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6697
    * @returns Array (the same datatype as the input)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6698
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6699
    * @see splice
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6700
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6701
    p.subset = function(array, offset, length) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6702
      var end = (length !== undef) ? offset + length : array.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6703
      return array.slice(offset, end);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6704
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6705
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6706
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6707
    * Combines an array of Strings into one String, each separated by the character(s) used for
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6708
    * the separator parameter. To join arrays of ints or floats, it's necessary to first convert
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6709
    * them to strings using nf() or nfs().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6710
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6711
    * @param {Array} array              array of Strings
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6712
    * @param {char|String} separator    char or String to be placed between each item
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6713
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6714
    * @returns {String} The combined string
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6715
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6716
    * @see split
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6717
    * @see trim
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6718
    * @see nf
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6719
    * @see nfs
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6720
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6721
    p.join = function(array, seperator) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6722
      return array.join(seperator);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6723
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6724
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6725
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6726
    * Decreases an array by one element and returns the shortened array. When using an
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6727
    * array of objects, the data returned from the function must be cast to the object array's
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6728
    * data type. For example: SomeClass[] items = (SomeClass[]) shorten(originalArray).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6729
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6730
    * @param {boolean[]|byte[]|char[]|int[]|float[]|String[]|array of objects} array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6731
    * boolean[], byte[], char[], int[], float[], or String[], or an array of objects
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6732
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6733
    * @returns Array (the same datatype as the input)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6734
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6735
    * @see append
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6736
    * @see expand
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6737
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6738
    p.shorten = function(ary) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6739
      var newary = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6740
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6741
      // copy array into new array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6742
      var len = ary.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6743
      for (var i = 0; i < len; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6744
        newary[i] = ary[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6745
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6746
      newary.pop();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6747
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6748
      return newary;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6749
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6750
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6751
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6752
    * Increases the size of an array. By default, this function doubles the size of the array,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6753
    * but the optional newSize parameter provides precise control over the increase in size.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6754
    * When using an array of objects, the data returned from the function must be cast to the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6755
    * object array's data type. For example: SomeClass[] items = (SomeClass[]) expand(originalArray).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6756
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6757
    * @param {boolean[]|byte[]|char[]|int[]|float[]|String[]|array of objects} ary
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6758
    * boolean[], byte[], char[], int[], float[], String[], or an array of objects
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6759
    * @param {int} newSize              positive int: new size for the array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6760
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6761
    * @returns Array (the same datatype as the input)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6762
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6763
    * @see contract
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6764
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6765
    p.expand = function(ary, targetSize) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6766
      var temp = ary.slice(0),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6767
          newSize = targetSize || ary.length * 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6768
      temp.length = newSize;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6769
      return temp;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6770
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6771
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6772
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6773
    * Copies an array (or part of an array) to another array. The src array is copied to the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6774
    * dst array, beginning at the position specified by srcPos and into the position specified
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6775
    * by dstPos. The number of elements to copy is determined by length. The simplified version
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6776
    * with two arguments copies an entire array to another of the same size. It is equivalent
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6777
    * to "arrayCopy(src, 0, dst, 0, src.length)". This function is far more efficient for copying
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6778
    * array data than iterating through a for and copying each element.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6779
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6780
    * @param {Array} src an array of any data type: the source array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6781
    * @param {Array} dest an array of any data type (as long as it's the same as src): the destination array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6782
    * @param {int} srcPos     starting position in the source array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6783
    * @param {int} destPos    starting position in the destination array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6784
    * @param {int} length     number of array elements to be copied
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6785
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6786
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6787
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6788
    p.arrayCopy = function() { // src, srcPos, dest, destPos, length) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6789
      var src, srcPos = 0, dest, destPos = 0, length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6790
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6791
      if (arguments.length === 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6792
        // recall itself and copy src to dest from start index 0 to 0 of src.length
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6793
        src = arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6794
        dest = arguments[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6795
        length = src.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6796
      } else if (arguments.length === 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6797
        // recall itself and copy src to dest from start index 0 to 0 of length
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6798
        src = arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6799
        dest = arguments[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6800
        length = arguments[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6801
      } else if (arguments.length === 5) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6802
        src = arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6803
        srcPos = arguments[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6804
        dest = arguments[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6805
        destPos = arguments[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6806
        length = arguments[4];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6807
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6808
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6809
      // copy src to dest from index srcPos to index destPos of length recursivly on objects
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6810
      for (var i = srcPos, j = destPos; i < length + srcPos; i++, j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6811
        if (dest[j] !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6812
          dest[j] = src[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6813
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6814
          throw "array index out of bounds exception";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6815
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6816
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6817
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6818
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6819
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6820
    * Reverses the order of an array.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6821
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6822
    * @param {boolean[]|byte[]|char[]|int[]|float[]|String[]} array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6823
    * boolean[], byte[], char[], int[], float[], or String[]
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6824
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6825
    * @returns Array (the same datatype as the input)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6826
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6827
    * @see sort
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6828
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6829
    p.reverse = function(array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6830
      return array.reverse();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6831
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6832
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6833
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6834
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6835
    // Color functions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6836
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6837
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6838
    // helper functions for internal blending modes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6839
    p.mix = function(a, b, f) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6840
      return a + (((b - a) * f) >> 8);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6841
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6842
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6843
    p.peg = function(n) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6844
      return (n < 0) ? 0 : ((n > 255) ? 255 : n);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6845
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6846
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6847
    // blending modes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6848
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6849
    * These are internal blending modes used for BlendColor()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6850
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6851
    * @param {Color} c1       First Color to blend
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6852
    * @param {Color} c2       Second Color to blend
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6853
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6854
    * @returns {Color}        The blended Color
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6855
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6856
    * @see BlendColor
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6857
    * @see Blend
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6858
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6859
    p.modes = (function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6860
      var ALPHA_MASK = PConstants.ALPHA_MASK,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6861
        RED_MASK = PConstants.RED_MASK,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6862
        GREEN_MASK = PConstants.GREEN_MASK,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6863
        BLUE_MASK = PConstants.BLUE_MASK,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6864
        min = Math.min,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6865
        max = Math.max;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6866
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6867
      function applyMode(c1, f, ar, ag, ab, br, bg, bb, cr, cg, cb) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6868
        var a = min(((c1 & 0xff000000) >>> 24) + f, 0xff) << 24;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6869
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6870
        var r = (ar + (((cr - ar) * f) >> 8));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6871
        r = ((r < 0) ? 0 : ((r > 255) ? 255 : r)) << 16;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6872
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6873
        var g = (ag + (((cg - ag) * f) >> 8));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6874
        g = ((g < 0) ? 0 : ((g > 255) ? 255 : g)) << 8;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6875
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6876
        var b = ab + (((cb - ab) * f) >> 8);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6877
        b = (b < 0) ? 0 : ((b > 255) ? 255 : b);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6878
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6879
        return (a | r | g | b);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6880
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6881
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6882
      return {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6883
        replace: function(c1, c2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6884
          return c2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6885
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6886
        blend: function(c1, c2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6887
          var f = (c2 & ALPHA_MASK) >>> 24,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6888
            ar = (c1 & RED_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6889
            ag = (c1 & GREEN_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6890
            ab = (c1 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6891
            br = (c2 & RED_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6892
            bg = (c2 & GREEN_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6893
            bb = (c2 & BLUE_MASK);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6894
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6895
          return (min(((c1 & ALPHA_MASK) >>> 24) + f, 0xff) << 24 |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6896
                  (ar + (((br - ar) * f) >> 8)) & RED_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6897
                  (ag + (((bg - ag) * f) >> 8)) & GREEN_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6898
                  (ab + (((bb - ab) * f) >> 8)) & BLUE_MASK);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6899
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6900
        add: function(c1, c2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6901
          var f = (c2 & ALPHA_MASK) >>> 24;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6902
          return (min(((c1 & ALPHA_MASK) >>> 24) + f, 0xff) << 24 |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6903
                  min(((c1 & RED_MASK) + ((c2 & RED_MASK) >> 8) * f), RED_MASK) & RED_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6904
                  min(((c1 & GREEN_MASK) + ((c2 & GREEN_MASK) >> 8) * f), GREEN_MASK) & GREEN_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6905
                  min((c1 & BLUE_MASK) + (((c2 & BLUE_MASK) * f) >> 8), BLUE_MASK));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6906
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6907
        subtract: function(c1, c2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6908
          var f = (c2 & ALPHA_MASK) >>> 24;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6909
          return (min(((c1 & ALPHA_MASK) >>> 24) + f, 0xff) << 24 |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6910
                  max(((c1 & RED_MASK) - ((c2 & RED_MASK) >> 8) * f), GREEN_MASK) & RED_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6911
                  max(((c1 & GREEN_MASK) - ((c2 & GREEN_MASK) >> 8) * f), BLUE_MASK) & GREEN_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6912
                  max((c1 & BLUE_MASK) - (((c2 & BLUE_MASK) * f) >> 8), 0));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6913
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6914
        lightest: function(c1, c2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6915
          var f = (c2 & ALPHA_MASK) >>> 24;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6916
          return (min(((c1 & ALPHA_MASK) >>> 24) + f, 0xff) << 24 |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6917
                  max(c1 & RED_MASK, ((c2 & RED_MASK) >> 8) * f) & RED_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6918
                  max(c1 & GREEN_MASK, ((c2 & GREEN_MASK) >> 8) * f) & GREEN_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6919
                  max(c1 & BLUE_MASK, ((c2 & BLUE_MASK) * f) >> 8));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6920
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6921
        darkest: function(c1, c2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6922
          var f = (c2 & ALPHA_MASK) >>> 24,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6923
            ar = (c1 & RED_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6924
            ag = (c1 & GREEN_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6925
            ab = (c1 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6926
            br = min(c1 & RED_MASK, ((c2 & RED_MASK) >> 8) * f),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6927
            bg = min(c1 & GREEN_MASK, ((c2 & GREEN_MASK) >> 8) * f),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6928
            bb = min(c1 & BLUE_MASK, ((c2 & BLUE_MASK) * f) >> 8);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6929
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6930
          return (min(((c1 & ALPHA_MASK) >>> 24) + f, 0xff) << 24 |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6931
                  (ar + (((br - ar) * f) >> 8)) & RED_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6932
                  (ag + (((bg - ag) * f) >> 8)) & GREEN_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6933
                  (ab + (((bb - ab) * f) >> 8)) & BLUE_MASK);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6934
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6935
        difference: function(c1, c2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6936
          var f  = (c2 & ALPHA_MASK) >>> 24,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6937
            ar = (c1 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6938
            ag = (c1 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6939
            ab = (c1 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6940
            br = (c2 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6941
            bg = (c2 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6942
            bb = (c2 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6943
            cr = (ar > br) ? (ar - br) : (br - ar),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6944
            cg = (ag > bg) ? (ag - bg) : (bg - ag),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6945
            cb = (ab > bb) ? (ab - bb) : (bb - ab);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6946
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6947
          return applyMode(c1, f, ar, ag, ab, br, bg, bb, cr, cg, cb);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6948
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6949
        exclusion: function(c1, c2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6950
          var f  = (c2 & ALPHA_MASK) >>> 24,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6951
            ar = (c1 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6952
            ag = (c1 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6953
            ab = (c1 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6954
            br = (c2 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6955
            bg = (c2 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6956
            bb = (c2 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6957
            cr = ar + br - ((ar * br) >> 7),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6958
            cg = ag + bg - ((ag * bg) >> 7),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6959
            cb = ab + bb - ((ab * bb) >> 7);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6960
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6961
          return applyMode(c1, f, ar, ag, ab, br, bg, bb, cr, cg, cb);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6962
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6963
        multiply: function(c1, c2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6964
          var f  = (c2 & ALPHA_MASK) >>> 24,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6965
            ar = (c1 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6966
            ag = (c1 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6967
            ab = (c1 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6968
            br = (c2 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6969
            bg = (c2 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6970
            bb = (c2 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6971
            cr = (ar * br) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6972
            cg = (ag * bg) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6973
            cb = (ab * bb) >> 8;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6974
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6975
          return applyMode(c1, f, ar, ag, ab, br, bg, bb, cr, cg, cb);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6976
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6977
        screen: function(c1, c2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6978
          var f  = (c2 & ALPHA_MASK) >>> 24,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6979
            ar = (c1 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6980
            ag = (c1 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6981
            ab = (c1 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6982
            br = (c2 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6983
            bg = (c2 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6984
            bb = (c2 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6985
            cr = 255 - (((255 - ar) * (255 - br)) >> 8),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6986
            cg = 255 - (((255 - ag) * (255 - bg)) >> 8),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6987
            cb = 255 - (((255 - ab) * (255 - bb)) >> 8);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6988
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6989
          return applyMode(c1, f, ar, ag, ab, br, bg, bb, cr, cg, cb);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6990
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6991
        hard_light: function(c1, c2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6992
          var f  = (c2 & ALPHA_MASK) >>> 24,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6993
            ar = (c1 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6994
            ag = (c1 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6995
            ab = (c1 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6996
            br = (c2 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6997
            bg = (c2 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6998
            bb = (c2 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  6999
            cr = (br < 128) ? ((ar * br) >> 7) : (255 - (((255 - ar) * (255 - br)) >> 7)),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7000
            cg = (bg < 128) ? ((ag * bg) >> 7) : (255 - (((255 - ag) * (255 - bg)) >> 7)),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7001
            cb = (bb < 128) ? ((ab * bb) >> 7) : (255 - (((255 - ab) * (255 - bb)) >> 7));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7002
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7003
          return applyMode(c1, f, ar, ag, ab, br, bg, bb, cr, cg, cb);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7004
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7005
        soft_light: function(c1, c2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7006
          var f  = (c2 & ALPHA_MASK) >>> 24,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7007
            ar = (c1 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7008
            ag = (c1 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7009
            ab = (c1 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7010
            br = (c2 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7011
            bg = (c2 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7012
            bb = (c2 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7013
            cr = ((ar * br) >> 7) + ((ar * ar) >> 8) - ((ar * ar * br) >> 15),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7014
            cg = ((ag * bg) >> 7) + ((ag * ag) >> 8) - ((ag * ag * bg) >> 15),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7015
            cb = ((ab * bb) >> 7) + ((ab * ab) >> 8) - ((ab * ab * bb) >> 15);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7016
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7017
          return applyMode(c1, f, ar, ag, ab, br, bg, bb, cr, cg, cb);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7018
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7019
        overlay: function(c1, c2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7020
          var f  = (c2 & ALPHA_MASK) >>> 24,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7021
            ar = (c1 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7022
            ag = (c1 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7023
            ab = (c1 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7024
            br = (c2 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7025
            bg = (c2 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7026
            bb = (c2 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7027
            cr = (ar < 128) ? ((ar * br) >> 7) : (255 - (((255 - ar) * (255 - br)) >> 7)),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7028
            cg = (ag < 128) ? ((ag * bg) >> 7) : (255 - (((255 - ag) * (255 - bg)) >> 7)),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7029
            cb = (ab < 128) ? ((ab * bb) >> 7) : (255 - (((255 - ab) * (255 - bb)) >> 7));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7030
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7031
          return applyMode(c1, f, ar, ag, ab, br, bg, bb, cr, cg, cb);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7032
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7033
        dodge: function(c1, c2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7034
          var f  = (c2 & ALPHA_MASK) >>> 24,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7035
            ar = (c1 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7036
            ag = (c1 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7037
            ab = (c1 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7038
            br = (c2 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7039
            bg = (c2 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7040
            bb = (c2 & BLUE_MASK);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7041
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7042
          var cr = 255;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7043
          if (br !== 255) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7044
            cr = (ar << 8) / (255 - br);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7045
            cr = (cr < 0) ? 0 : ((cr > 255) ? 255 : cr);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7046
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7047
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7048
          var cg = 255;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7049
          if (bg !== 255) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7050
            cg = (ag << 8) / (255 - bg);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7051
            cg = (cg < 0) ? 0 : ((cg > 255) ? 255 : cg);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7052
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7053
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7054
          var cb = 255;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7055
          if (bb !== 255) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7056
            cb = (ab << 8) / (255 - bb);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7057
            cb = (cb < 0) ? 0 : ((cb > 255) ? 255 : cb);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7058
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7059
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7060
          return applyMode(c1, f, ar, ag, ab, br, bg, bb, cr, cg, cb);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7061
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7062
        burn: function(c1, c2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7063
          var f  = (c2 & ALPHA_MASK) >>> 24,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7064
            ar = (c1 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7065
            ag = (c1 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7066
            ab = (c1 & BLUE_MASK),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7067
            br = (c2 & RED_MASK) >> 16,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7068
            bg = (c2 & GREEN_MASK) >> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7069
            bb = (c2 & BLUE_MASK);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7070
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7071
          var cr = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7072
          if (br !== 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7073
            cr = ((255 - ar) << 8) / br;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7074
            cr = 255 - ((cr < 0) ? 0 : ((cr > 255) ? 255 : cr));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7075
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7076
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7077
          var cg = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7078
          if (bg !== 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7079
            cg = ((255 - ag) << 8) / bg;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7080
            cg = 255 - ((cg < 0) ? 0 : ((cg > 255) ? 255 : cg));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7081
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7082
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7083
          var cb = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7084
          if (bb !== 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7085
            cb = ((255 - ab) << 8) / bb;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7086
            cb = 255 - ((cb < 0) ? 0 : ((cb > 255) ? 255 : cb));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7087
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7088
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7089
          return applyMode(c1, f, ar, ag, ab, br, bg, bb, cr, cg, cb);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7090
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7091
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7092
    }());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7093
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7094
    function color$4(aValue1, aValue2, aValue3, aValue4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7095
      var r, g, b, a;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7096
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7097
      if (curColorMode === PConstants.HSB) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7098
        var rgb = p.color.toRGB(aValue1, aValue2, aValue3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7099
        r = rgb[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7100
        g = rgb[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7101
        b = rgb[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7102
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7103
        r = Math.round(255 * (aValue1 / colorModeX));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7104
        g = Math.round(255 * (aValue2 / colorModeY));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7105
        b = Math.round(255 * (aValue3 / colorModeZ));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7106
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7107
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7108
      a = Math.round(255 * (aValue4 / colorModeA));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7109
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7110
      // Limit values less than 0 and greater than 255
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7111
      r = (r < 0) ? 0 : r;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7112
      g = (g < 0) ? 0 : g;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7113
      b = (b < 0) ? 0 : b;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7114
      a = (a < 0) ? 0 : a;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7115
      r = (r > 255) ? 255 : r;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7116
      g = (g > 255) ? 255 : g;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7117
      b = (b > 255) ? 255 : b;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7118
      a = (a > 255) ? 255 : a;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7119
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7120
      // Create color int
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7121
      return (a << 24) & PConstants.ALPHA_MASK | (r << 16) & PConstants.RED_MASK | (g << 8) & PConstants.GREEN_MASK | b & PConstants.BLUE_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7122
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7123
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7124
    function color$2(aValue1, aValue2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7125
      var a;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7126
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7127
      // Color int and alpha
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7128
      if (aValue1 & PConstants.ALPHA_MASK) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7129
        a = Math.round(255 * (aValue2 / colorModeA));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7130
        // Limit values less than 0 and greater than 255
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7131
        a = (a > 255) ? 255 : a;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7132
        a = (a < 0) ? 0 : a;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7133
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7134
        return aValue1 - (aValue1 & PConstants.ALPHA_MASK) + ((a << 24) & PConstants.ALPHA_MASK);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7135
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7136
      // Grayscale and alpha
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7137
      if (curColorMode === PConstants.RGB) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7138
        return color$4(aValue1, aValue1, aValue1, aValue2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7139
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7140
      if (curColorMode === PConstants.HSB) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7141
        return color$4(0, 0, (aValue1 / colorModeX) * colorModeZ, aValue2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7142
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7143
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7144
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7145
    function color$1(aValue1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7146
      // Grayscale
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7147
      if (aValue1 <= colorModeX && aValue1 >= 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7148
          if (curColorMode === PConstants.RGB) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7149
            return color$4(aValue1, aValue1, aValue1, colorModeA);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7150
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7151
          if (curColorMode === PConstants.HSB) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7152
            return color$4(0, 0, (aValue1 / colorModeX) * colorModeZ, colorModeA);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7153
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7154
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7155
      // Color int
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7156
      if (aValue1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7157
        if (aValue1 > 2147483647) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7158
          // Java Overflow
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7159
          aValue1 -= 4294967296;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7160
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7161
        return aValue1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7162
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7163
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7164
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7165
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7166
    * Creates colors for storing in variables of the color datatype. The parameters are
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7167
    * interpreted as RGB or HSB values depending on the current colorMode(). The default
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7168
    * mode is RGB values from 0 to 255 and therefore, the function call color(255, 204, 0)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7169
    * will return a bright yellow color. More about how colors are stored can be found in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7170
    * the reference for the color datatype.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7171
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7172
    * @param {int|float} aValue1        red or hue or grey values relative to the current color range.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7173
    * Also can be color value in hexadecimal notation (i.e. #FFCC00 or 0xFFFFCC00)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7174
    * @param {int|float} aValue2        green or saturation values relative to the current color range
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7175
    * @param {int|float} aValue3        blue or brightness values relative to the current color range
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7176
    * @param {int|float} aValue4        relative to current color range. Represents alpha
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7177
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7178
    * @returns {color} the color
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7179
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7180
    * @see colorMode
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7181
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7182
    p.color = function(aValue1, aValue2, aValue3, aValue4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7183
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7184
      // 4 arguments: (R, G, B, A) or (H, S, B, A)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7185
      if (aValue1 !== undef && aValue2 !== undef && aValue3 !== undef && aValue4 !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7186
        return color$4(aValue1, aValue2, aValue3, aValue4);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7187
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7188
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7189
      // 3 arguments: (R, G, B) or (H, S, B)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7190
      if (aValue1 !== undef && aValue2 !== undef && aValue3 !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7191
        return color$4(aValue1, aValue2, aValue3, colorModeA);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7192
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7193
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7194
      // 2 arguments: (Color, A) or (Grayscale, A)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7195
      if (aValue1 !== undef && aValue2 !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7196
        return color$2(aValue1, aValue2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7197
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7198
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7199
      // 1 argument: (Grayscale) or (Color)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7200
      if (typeof aValue1 === "number") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7201
        return color$1(aValue1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7202
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7203
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7204
      // Default
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7205
      return color$4(colorModeX, colorModeY, colorModeZ, colorModeA);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7206
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7207
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7208
    // Ease of use function to extract the colour bits into a string
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7209
    p.color.toString = function(colorInt) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7210
      return "rgba(" + ((colorInt & PConstants.RED_MASK) >>> 16) + "," + ((colorInt & PConstants.GREEN_MASK) >>> 8) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7211
             "," + ((colorInt & PConstants.BLUE_MASK)) + "," + ((colorInt & PConstants.ALPHA_MASK) >>> 24) / 255 + ")";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7212
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7213
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7214
    // Easy of use function to pack rgba values into a single bit-shifted color int.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7215
    p.color.toInt = function(r, g, b, a) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7216
      return (a << 24) & PConstants.ALPHA_MASK | (r << 16) & PConstants.RED_MASK | (g << 8) & PConstants.GREEN_MASK | b & PConstants.BLUE_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7217
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7218
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7219
    // Creates a simple array in [R, G, B, A] format, [255, 255, 255, 255]
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7220
    p.color.toArray = function(colorInt) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7221
      return [(colorInt & PConstants.RED_MASK) >>> 16, (colorInt & PConstants.GREEN_MASK) >>> 8,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7222
              colorInt & PConstants.BLUE_MASK, (colorInt & PConstants.ALPHA_MASK) >>> 24];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7223
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7224
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7225
    // Creates a WebGL color array in [R, G, B, A] format. WebGL wants the color ranges between 0 and 1, [1, 1, 1, 1]
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7226
    p.color.toGLArray = function(colorInt) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7227
      return [((colorInt & PConstants.RED_MASK) >>> 16) / 255, ((colorInt & PConstants.GREEN_MASK) >>> 8) / 255,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7228
              (colorInt & PConstants.BLUE_MASK) / 255, ((colorInt & PConstants.ALPHA_MASK) >>> 24) / 255];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7229
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7230
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7231
    // HSB conversion function from Mootools, MIT Licensed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7232
    p.color.toRGB = function(h, s, b) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7233
      // Limit values greater than range
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7234
      h = (h > colorModeX) ? colorModeX : h;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7235
      s = (s > colorModeY) ? colorModeY : s;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7236
      b = (b > colorModeZ) ? colorModeZ : b;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7237
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7238
      h = (h / colorModeX) * 360;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7239
      s = (s / colorModeY) * 100;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7240
      b = (b / colorModeZ) * 100;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7241
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7242
      var br = Math.round(b / 100 * 255);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7243
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7244
      if (s === 0) { // Grayscale
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7245
        return [br, br, br];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7246
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7247
      var hue = h % 360;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7248
      var f = hue % 60;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7249
      var p = Math.round((b * (100 - s)) / 10000 * 255);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7250
      var q = Math.round((b * (6000 - s * f)) / 600000 * 255);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7251
      var t = Math.round((b * (6000 - s * (60 - f))) / 600000 * 255);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7252
      switch (Math.floor(hue / 60)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7253
      case 0:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7254
        return [br, t, p];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7255
      case 1:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7256
        return [q, br, p];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7257
      case 2:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7258
        return [p, br, t];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7259
      case 3:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7260
        return [p, q, br];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7261
      case 4:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7262
        return [t, p, br];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7263
      case 5:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7264
        return [br, p, q];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7265
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7266
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7267
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7268
    function colorToHSB(colorInt) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7269
      var red, green, blue;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7270
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7271
      red   = ((colorInt & PConstants.RED_MASK) >>> 16) / 255;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7272
      green = ((colorInt & PConstants.GREEN_MASK) >>> 8) / 255;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7273
      blue  = (colorInt & PConstants.BLUE_MASK) / 255;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7274
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7275
      var max = p.max(p.max(red,green), blue),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7276
          min = p.min(p.min(red,green), blue),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7277
          hue, saturation;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7278
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7279
      if (min === max) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7280
        return [0, 0, max*colorModeZ];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7281
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7282
      saturation = (max - min) / max;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7283
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7284
      if (red === max) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7285
        hue = (green - blue) / (max - min);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7286
      } else if (green === max) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7287
        hue = 2 + ((blue - red) / (max - min));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7288
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7289
        hue = 4 + ((red - green) / (max - min));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7290
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7291
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7292
      hue /= 6;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7293
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7294
      if (hue < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7295
        hue += 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7296
      } else if (hue > 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7297
        hue -= 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7298
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7299
      return [hue*colorModeX, saturation*colorModeY, max*colorModeZ];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7300
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7301
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7302
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7303
    * Extracts the brightness value from a color.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7304
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7305
    * @param {color} colInt any value of the color datatype
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7306
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7307
    * @returns {float} The brightness color value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7308
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7309
    * @see red
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7310
    * @see green
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7311
    * @see blue
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7312
    * @see hue
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7313
    * @see saturation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7314
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7315
    p.brightness = function(colInt){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7316
      return colorToHSB(colInt)[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7317
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7318
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7319
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7320
    * Extracts the saturation value from a color.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7321
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7322
    * @param {color} colInt any value of the color datatype
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7323
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7324
    * @returns {float} The saturation color value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7325
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7326
    * @see red
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7327
    * @see green
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7328
    * @see blue
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7329
    * @see hue
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7330
    * @see brightness
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7331
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7332
    p.saturation = function(colInt){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7333
      return colorToHSB(colInt)[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7334
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7335
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7336
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7337
    * Extracts the hue value from a color.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7338
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7339
    * @param {color} colInt any value of the color datatype
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7340
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7341
    * @returns {float} The hue color value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7342
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7343
    * @see red
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7344
    * @see green
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7345
    * @see blue
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7346
    * @see saturation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7347
    * @see brightness
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7348
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7349
    p.hue = function(colInt){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7350
      return colorToHSB(colInt)[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7351
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7352
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7353
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7354
    * Extracts the red value from a color, scaled to match current colorMode().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7355
    * This value is always returned as a float so be careful not to assign it to an int value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7356
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7357
    * @param {color} aColor any value of the color datatype
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7358
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7359
    * @returns {float} The red color value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7360
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7361
    * @see green
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7362
    * @see blue
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7363
    * @see alpha
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7364
    * @see >> right shift
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7365
    * @see hue
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7366
    * @see saturation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7367
    * @see brightness
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7368
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7369
    p.red = function(aColor) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7370
      return ((aColor & PConstants.RED_MASK) >>> 16) / 255 * colorModeX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7371
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7372
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7373
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7374
    * Extracts the green value from a color, scaled to match current colorMode().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7375
    * This value is always returned as a float so be careful not to assign it to an int value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7376
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7377
    * @param {color} aColor any value of the color datatype
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7378
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7379
    * @returns {float} The green color value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7380
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7381
    * @see red
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7382
    * @see blue
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7383
    * @see alpha
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7384
    * @see >> right shift
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7385
    * @see hue
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7386
    * @see saturation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7387
    * @see brightness
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7388
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7389
    p.green = function(aColor) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7390
      return ((aColor & PConstants.GREEN_MASK) >>> 8) / 255 * colorModeY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7391
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7392
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7393
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7394
    * Extracts the blue value from a color, scaled to match current colorMode().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7395
    * This value is always returned as a float so be careful not to assign it to an int value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7396
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7397
    * @param {color} aColor any value of the color datatype
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7398
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7399
    * @returns {float} The blue color value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7400
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7401
    * @see red
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7402
    * @see green
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7403
    * @see alpha
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7404
    * @see >> right shift
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7405
    * @see hue
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7406
    * @see saturation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7407
    * @see brightness
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7408
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7409
    p.blue = function(aColor) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7410
      return (aColor & PConstants.BLUE_MASK) / 255 * colorModeZ;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7411
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7412
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7413
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7414
    * Extracts the alpha value from a color, scaled to match current colorMode().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7415
    * This value is always returned as a float so be careful not to assign it to an int value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7416
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7417
    * @param {color} aColor any value of the color datatype
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7418
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7419
    * @returns {float} The alpha color value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7420
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7421
    * @see red
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7422
    * @see green
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7423
    * @see blue
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7424
    * @see >> right shift
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7425
    * @see hue
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7426
    * @see saturation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7427
    * @see brightness
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7428
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7429
    p.alpha = function(aColor) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7430
      return ((aColor & PConstants.ALPHA_MASK) >>> 24) / 255 * colorModeA;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7431
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7432
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7433
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7434
    * Calculates a color or colors between two colors at a specific increment.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7435
    * The amt parameter is the amount to interpolate between the two values where 0.0
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7436
    * equal to the first point, 0.1 is very near the first point, 0.5 is half-way in between, etc.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7437
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7438
    * @param {color} c1     interpolate from this color
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7439
    * @param {color} c2     interpolate to this color
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7440
    * @param {float} amt    between 0.0 and 1.0
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7441
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7442
    * @returns {float} The blended color.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7443
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7444
    * @see blendColor
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7445
    * @see color
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7446
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7447
    p.lerpColor = function(c1, c2, amt) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7448
      var r, g, b, a, r1, g1, b1, a1, r2, g2, b2, a2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7449
      var hsb1, hsb2, rgb, h, s;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7450
      var colorBits1 = p.color(c1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7451
      var colorBits2 = p.color(c2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7452
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7453
      if (curColorMode === PConstants.HSB) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7454
        // Special processing for HSB mode.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7455
        // Get HSB and Alpha values for Color 1 and 2
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7456
        hsb1 = colorToHSB(colorBits1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7457
        a1 = ((colorBits1 & PConstants.ALPHA_MASK) >>> 24) / colorModeA;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7458
        hsb2 = colorToHSB(colorBits2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7459
        a2 = ((colorBits2 & PConstants.ALPHA_MASK) >>> 24) / colorModeA;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7460
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7461
        // Return lerp value for each channel, for HSB components
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7462
        h = p.lerp(hsb1[0], hsb2[0], amt);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7463
        s = p.lerp(hsb1[1], hsb2[1], amt);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7464
        b = p.lerp(hsb1[2], hsb2[2], amt);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7465
        rgb = p.color.toRGB(h, s, b);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7466
        // ... and for Alpha-range
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7467
        a = p.lerp(a1, a2, amt) * colorModeA;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7468
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7469
        return (a << 24) & PConstants.ALPHA_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7470
               (rgb[0] << 16) & PConstants.RED_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7471
               (rgb[1] << 8) & PConstants.GREEN_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7472
               rgb[2] & PConstants.BLUE_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7473
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7474
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7475
      // Get RGBA values for Color 1 to floats
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7476
      r1 = (colorBits1 & PConstants.RED_MASK) >>> 16;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7477
      g1 = (colorBits1 & PConstants.GREEN_MASK) >>> 8;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7478
      b1 = (colorBits1 & PConstants.BLUE_MASK);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7479
      a1 = ((colorBits1 & PConstants.ALPHA_MASK) >>> 24) / colorModeA;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7480
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7481
      // Get RGBA values for Color 2 to floats
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7482
      r2 = (colorBits2 & PConstants.RED_MASK) >>> 16;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7483
      g2 = (colorBits2 & PConstants.GREEN_MASK) >>> 8;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7484
      b2 = (colorBits2 & PConstants.BLUE_MASK);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7485
      a2 = ((colorBits2 & PConstants.ALPHA_MASK) >>> 24) / colorModeA;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7486
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7487
      // Return lerp value for each channel, INT for color, Float for Alpha-range
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7488
      r = p.lerp(r1, r2, amt) | 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7489
      g = p.lerp(g1, g2, amt) | 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7490
      b = p.lerp(b1, b2, amt) | 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7491
      a = p.lerp(a1, a2, amt) * colorModeA;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7492
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7493
      return (a << 24) & PConstants.ALPHA_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7494
             (r << 16) & PConstants.RED_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7495
             (g << 8) & PConstants.GREEN_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7496
             b & PConstants.BLUE_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7497
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7498
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7499
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7500
    * Changes the way Processing interprets color data. By default, fill(), stroke(), and background()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7501
    * colors are set by values between 0 and 255 using the RGB color model. It is possible to change the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7502
    * numerical range used for specifying colors and to switch color systems. For example, calling colorMode(RGB, 1.0)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7503
    * will specify that values are specified between 0 and 1. The limits for defining colors are altered by setting the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7504
    * parameters range1, range2, range3, and range 4.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7505
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7506
    * @param {MODE} mode Either RGB or HSB, corresponding to Red/Green/Blue and Hue/Saturation/Brightness
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7507
    * @param {int|float} range              range for all color elements
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7508
    * @param {int|float} range1             range for the red or hue depending on the current color mode
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7509
    * @param {int|float} range2             range for the green or saturation depending on the current color mode
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7510
    * @param {int|float} range3             range for the blue or brightness depending on the current color mode
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7511
    * @param {int|float} range4             range for the alpha
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7512
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7513
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7514
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7515
    * @see background
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7516
    * @see fill
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7517
    * @see stroke
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7518
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7519
    p.colorMode = function() { // mode, range1, range2, range3, range4
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7520
      curColorMode = arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7521
      if (arguments.length > 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7522
        colorModeX   = arguments[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7523
        colorModeY   = arguments[2] || arguments[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7524
        colorModeZ   = arguments[3] || arguments[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7525
        colorModeA   = arguments[4] || arguments[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7526
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7527
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7528
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7529
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7530
    * Blends two color values together based on the blending mode given as the MODE parameter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7531
    * The possible modes are described in the reference for the blend() function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7532
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7533
    * @param {color} c1 color: the first color to blend
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7534
    * @param {color} c2 color: the second color to blend
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7535
    * @param {MODE} MODE Either BLEND, ADD, SUBTRACT, DARKEST, LIGHTEST, DIFFERENCE, EXCLUSION, MULTIPLY,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7536
    * SCREEN, OVERLAY, HARD_LIGHT, SOFT_LIGHT, DODGE, or BURN
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7537
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7538
    * @returns {float} The blended color.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7539
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7540
    * @see blend
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7541
    * @see color
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7542
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7543
    p.blendColor = function(c1, c2, mode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7544
      if (mode === PConstants.REPLACE) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7545
        return p.modes.replace(c1, c2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7546
      } else if (mode === PConstants.BLEND) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7547
        return p.modes.blend(c1, c2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7548
      } else if (mode === PConstants.ADD) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7549
        return p.modes.add(c1, c2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7550
      } else if (mode === PConstants.SUBTRACT) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7551
        return p.modes.subtract(c1, c2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7552
      } else if (mode === PConstants.LIGHTEST) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7553
        return p.modes.lightest(c1, c2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7554
      } else if (mode === PConstants.DARKEST) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7555
        return p.modes.darkest(c1, c2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7556
      } else if (mode === PConstants.DIFFERENCE) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7557
        return p.modes.difference(c1, c2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7558
      } else if (mode === PConstants.EXCLUSION) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7559
        return p.modes.exclusion(c1, c2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7560
      } else if (mode === PConstants.MULTIPLY) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7561
        return p.modes.multiply(c1, c2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7562
      } else if (mode === PConstants.SCREEN) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7563
        return p.modes.screen(c1, c2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7564
      } else if (mode === PConstants.HARD_LIGHT) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7565
        return p.modes.hard_light(c1, c2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7566
      } else if (mode === PConstants.SOFT_LIGHT) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7567
        return p.modes.soft_light(c1, c2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7568
      } else if (mode === PConstants.OVERLAY) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7569
        return p.modes.overlay(c1, c2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7570
      } else if (mode === PConstants.DODGE) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7571
        return p.modes.dodge(c1, c2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7572
      } else if (mode === PConstants.BURN) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7573
        return p.modes.burn(c1, c2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7574
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7575
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7576
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7577
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7578
    // Canvas-Matrix manipulation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7579
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7580
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7581
    function saveContext() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7582
      curContext.save();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7583
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7584
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7585
    function restoreContext() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7586
      curContext.restore();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7587
      isStrokeDirty = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7588
      isFillDirty = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7589
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7590
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7591
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7592
    * Prints the current matrix to the text window.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7593
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7594
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7595
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7596
    * @see pushMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7597
    * @see popMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7598
    * @see resetMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7599
    * @see applyMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7600
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7601
    p.printMatrix = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7602
      modelView.print();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7603
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7604
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7605
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7606
    * Specifies an amount to displace objects within the display window. The x parameter specifies left/right translation,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7607
    * the y parameter specifies up/down translation, and the z parameter specifies translations toward/away from the screen.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7608
    * Using this function with the z  parameter requires using the P3D or OPENGL parameter in combination with size as shown
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7609
    * in the above example. Transformations apply to everything that happens after and subsequent calls to the function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7610
    * accumulates the effect. For example, calling translate(50, 0) and then translate(20, 0) is the same as translate(70, 0).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7611
    * If translate() is called within draw(), the transformation is reset when the loop begins again.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7612
    * This function can be further controlled by the pushMatrix() and popMatrix().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7613
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7614
    * @param {int|float} x        left/right translation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7615
    * @param {int|float} y        up/down translation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7616
    * @param {int|float} z        forward/back translation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7617
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7618
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7619
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7620
    * @see pushMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7621
    * @see popMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7622
    * @see scale
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7623
    * @see rotate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7624
    * @see rotateX
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7625
    * @see rotateY
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7626
    * @see rotateZ
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7627
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7628
    Drawing2D.prototype.translate = function(x, y) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7629
      modelView.translate(x, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7630
      modelViewInv.invTranslate(x, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7631
      curContext.translate(x, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7632
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7633
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7634
    Drawing3D.prototype.translate = function(x, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7635
      modelView.translate(x, y, z);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7636
      modelViewInv.invTranslate(x, y, z);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7637
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7638
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7639
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7640
    * Increases or decreases the size of a shape by expanding and contracting vertices. Objects always scale from their
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7641
    * relative origin to the coordinate system. Scale values are specified as decimal percentages. For example, the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7642
    * function call scale(2.0) increases the dimension of a shape by 200%. Transformations apply to everything that
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7643
    * happens after and subsequent calls to the function multiply the effect. For example, calling scale(2.0) and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7644
    * then scale(1.5) is the same as scale(3.0). If scale() is called within draw(), the transformation is reset when
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7645
    * the loop begins again. Using this fuction with the z  parameter requires passing P3D or OPENGL into the size()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7646
    * parameter as shown in the example above. This function can be further controlled by pushMatrix() and popMatrix().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7647
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7648
    * @param {int|float} size     percentage to scale the object
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7649
    * @param {int|float} x        percentage to scale the object in the x-axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7650
    * @param {int|float} y        percentage to scale the object in the y-axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7651
    * @param {int|float} z        percentage to scale the object in the z-axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7652
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7653
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7654
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7655
    * @see pushMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7656
    * @see popMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7657
    * @see translate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7658
    * @see rotate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7659
    * @see rotateX
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7660
    * @see rotateY
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7661
    * @see rotateZ
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7662
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7663
    Drawing2D.prototype.scale = function(x, y) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7664
      modelView.scale(x, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7665
      modelViewInv.invScale(x, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7666
      curContext.scale(x, y || x);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7667
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7668
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7669
    Drawing3D.prototype.scale = function(x, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7670
      modelView.scale(x, y, z);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7671
      modelViewInv.invScale(x, y, z);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7672
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7673
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7674
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7675
    * Pushes the current transformation matrix onto the matrix stack. Understanding pushMatrix() and popMatrix()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7676
    * requires understanding the concept of a matrix stack. The pushMatrix() function saves the current coordinate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7677
    * system to the stack and popMatrix() restores the prior coordinate system. pushMatrix() and popMatrix() are
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7678
    * used in conjuction with the other transformation methods and may be embedded to control the scope of
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7679
    * the transformations.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7680
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7681
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7682
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7683
    * @see popMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7684
    * @see translate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7685
    * @see rotate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7686
    * @see rotateX
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7687
    * @see rotateY
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7688
    * @see rotateZ
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7689
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7690
    Drawing2D.prototype.pushMatrix = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7691
      userMatrixStack.load(modelView);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7692
      userReverseMatrixStack.load(modelViewInv);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7693
      saveContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7694
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7695
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7696
    Drawing3D.prototype.pushMatrix = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7697
      userMatrixStack.load(modelView);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7698
      userReverseMatrixStack.load(modelViewInv);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7699
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7700
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7701
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7702
    * Pops the current transformation matrix off the matrix stack. Understanding pushing and popping requires
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7703
    * understanding the concept of a matrix stack. The pushMatrix() function saves the current coordinate system to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7704
    * the stack and popMatrix() restores the prior coordinate system. pushMatrix() and popMatrix() are used in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7705
    * conjuction with the other transformation methods and may be embedded to control the scope of the transformations.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7706
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7707
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7708
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7709
    * @see popMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7710
    * @see pushMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7711
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7712
    Drawing2D.prototype.popMatrix = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7713
      modelView.set(userMatrixStack.pop());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7714
      modelViewInv.set(userReverseMatrixStack.pop());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7715
      restoreContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7716
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7717
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7718
    Drawing3D.prototype.popMatrix = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7719
      modelView.set(userMatrixStack.pop());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7720
      modelViewInv.set(userReverseMatrixStack.pop());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7721
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7722
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7723
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7724
    * Replaces the current matrix with the identity matrix. The equivalent function in OpenGL is glLoadIdentity().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7725
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7726
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7727
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7728
    * @see popMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7729
    * @see pushMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7730
    * @see applyMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7731
    * @see printMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7732
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7733
    Drawing2D.prototype.resetMatrix = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7734
      modelView.reset();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7735
      modelViewInv.reset();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7736
      curContext.setTransform(1,0,0,1,0,0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7737
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7738
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7739
    Drawing3D.prototype.resetMatrix = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7740
      modelView.reset();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7741
      modelViewInv.reset();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7742
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7743
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7744
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7745
    * Multiplies the current matrix by the one specified through the parameters. This is very slow because it will
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7746
    * try to calculate the inverse of the transform, so avoid it whenever possible. The equivalent function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7747
    * in OpenGL is glMultMatrix().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7748
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7749
    * @param {int|float} n00-n15      numbers which define the 4x4 matrix to be multiplied
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7750
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7751
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7752
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7753
    * @see popMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7754
    * @see pushMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7755
    * @see resetMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7756
    * @see printMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7757
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7758
    DrawingShared.prototype.applyMatrix = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7759
      var a = arguments;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7760
      modelView.apply(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7761
      modelViewInv.invApply(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7762
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7763
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7764
    Drawing2D.prototype.applyMatrix = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7765
      var a = arguments;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7766
      for (var cnt = a.length; cnt < 16; cnt++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7767
        a[cnt] = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7768
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7769
      a[10] = a[15] = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7770
      DrawingShared.prototype.applyMatrix.apply(this, a);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7771
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7772
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7773
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7774
    * Rotates a shape around the x-axis the amount specified by the angle parameter. Angles should be
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7775
    * specified in radians (values from 0 to PI*2) or converted to radians with the radians()  function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7776
    * Objects are always rotated around their relative position to the origin and positive numbers
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7777
    * rotate objects in a counterclockwise direction. Transformations apply to everything that happens
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7778
    * after and subsequent calls to the function accumulates the effect. For example, calling rotateX(PI/2)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7779
    * and then rotateX(PI/2) is the same as rotateX(PI). If rotateX() is called within the draw(), the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7780
    * transformation is reset when the loop begins again. This function requires passing P3D or OPENGL
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7781
    * into the size() parameter as shown in the example above.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7782
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7783
    * @param {int|float} angleInRadians     angle of rotation specified in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7784
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7785
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7786
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7787
    * @see rotateY
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7788
    * @see rotateZ
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7789
    * @see rotate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7790
    * @see translate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7791
    * @see scale
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7792
    * @see popMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7793
    * @see pushMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7794
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7795
    p.rotateX = function(angleInRadians) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7796
      modelView.rotateX(angleInRadians);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7797
      modelViewInv.invRotateX(angleInRadians);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7798
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7799
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7800
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7801
    * Rotates a shape around the z-axis the amount specified by the angle parameter. Angles should be
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7802
    * specified in radians (values from 0 to PI*2) or converted to radians with the radians()  function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7803
    * Objects are always rotated around their relative position to the origin and positive numbers
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7804
    * rotate objects in a counterclockwise direction. Transformations apply to everything that happens
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7805
    * after and subsequent calls to the function accumulates the effect. For example, calling rotateZ(PI/2)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7806
    * and then rotateZ(PI/2) is the same as rotateZ(PI). If rotateZ() is called within the draw(), the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7807
    * transformation is reset when the loop begins again. This function requires passing P3D or OPENGL
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7808
    * into the size() parameter as shown in the example above.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7809
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7810
    * @param {int|float} angleInRadians     angle of rotation specified in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7811
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7812
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7813
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7814
    * @see rotateX
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7815
    * @see rotateY
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7816
    * @see rotate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7817
    * @see translate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7818
    * @see scale
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7819
    * @see popMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7820
    * @see pushMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7821
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7822
    Drawing2D.prototype.rotateZ = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7823
      throw "rotateZ() is not supported in 2D mode. Use rotate(float) instead.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7824
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7825
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7826
    Drawing3D.prototype.rotateZ = function(angleInRadians) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7827
      modelView.rotateZ(angleInRadians);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7828
      modelViewInv.invRotateZ(angleInRadians);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7829
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7830
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7831
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7832
    * Rotates a shape around the y-axis the amount specified by the angle parameter. Angles should be
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7833
    * specified in radians (values from 0 to PI*2) or converted to radians with the radians()  function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7834
    * Objects are always rotated around their relative position to the origin and positive numbers
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7835
    * rotate objects in a counterclockwise direction. Transformations apply to everything that happens
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7836
    * after and subsequent calls to the function accumulates the effect. For example, calling rotateY(PI/2)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7837
    * and then rotateY(PI/2) is the same as rotateY(PI). If rotateY() is called within the draw(), the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7838
    * transformation is reset when the loop begins again. This function requires passing P3D or OPENGL
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7839
    * into the size() parameter as shown in the example above.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7840
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7841
    * @param {int|float} angleInRadians     angle of rotation specified in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7842
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7843
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7844
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7845
    * @see rotateX
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7846
    * @see rotateZ
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7847
    * @see rotate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7848
    * @see translate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7849
    * @see scale
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7850
    * @see popMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7851
    * @see pushMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7852
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7853
    p.rotateY = function(angleInRadians) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7854
      modelView.rotateY(angleInRadians);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7855
      modelViewInv.invRotateY(angleInRadians);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7856
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7857
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7858
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7859
    * Rotates a shape the amount specified by the angle parameter. Angles should be specified in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7860
    * (values from 0 to TWO_PI) or converted to radians with the radians() function. Objects are always
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7861
    * rotated around their relative position to the origin and positive numbers rotate objects in a
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7862
    * clockwise direction. Transformations apply to everything that happens after and subsequent calls
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7863
    * to the function accumulates the effect. For example, calling rotate(HALF_PI) and then rotate(HALF_PI)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7864
    * is the same as rotate(PI). All tranformations are reset when draw() begins again. Technically,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7865
    * rotate() multiplies the current transformation matrix by a rotation matrix. This function can be
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7866
    * further controlled by the pushMatrix() and popMatrix().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7867
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7868
    * @param {int|float} angleInRadians     angle of rotation specified in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7869
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7870
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7871
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7872
    * @see rotateX
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7873
    * @see rotateY
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7874
    * @see rotateZ
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7875
    * @see rotate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7876
    * @see translate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7877
    * @see scale
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7878
    * @see popMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7879
    * @see pushMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7880
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7881
    Drawing2D.prototype.rotate = function(angleInRadians) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7882
      modelView.rotateZ(angleInRadians);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7883
      modelViewInv.invRotateZ(angleInRadians);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7884
      curContext.rotate(angleInRadians);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7885
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7886
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7887
    Drawing3D.prototype.rotate = function(angleInRadians) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7888
      p.rotateZ(angleInRadians);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7889
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7890
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7891
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7892
    * The pushStyle() function saves the current style settings and popStyle()  restores the prior settings.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7893
    * Note that these functions are always used together. They allow you to change the style settings and later
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7894
    * return to what you had. When a new style is started with pushStyle(), it builds on the current style information.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7895
    * The pushStyle() and popStyle() functions can be embedded to provide more control (see the second example
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7896
    * above for a demonstration.)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7897
    * The style information controlled by the following functions are included in the style: fill(), stroke(), tint(),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7898
    * strokeWeight(), strokeCap(), strokeJoin(), imageMode(), rectMode(), ellipseMode(), shapeMode(), colorMode(),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7899
    * textAlign(), textFont(), textMode(), textSize(), textLeading(), emissive(), specular(), shininess(), ambient()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7900
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7901
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7902
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7903
    * @see popStyle
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7904
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7905
    p.pushStyle = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7906
      // Save the canvas state.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7907
      saveContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7908
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7909
      p.pushMatrix();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7910
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7911
      var newState = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7912
        'doFill': doFill,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7913
        'currentFillColor': currentFillColor,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7914
        'doStroke': doStroke,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7915
        'currentStrokeColor': currentStrokeColor,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7916
        'curTint': curTint,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7917
        'curRectMode': curRectMode,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7918
        'curColorMode': curColorMode,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7919
        'colorModeX': colorModeX,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7920
        'colorModeZ': colorModeZ,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7921
        'colorModeY': colorModeY,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7922
        'colorModeA': colorModeA,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7923
        'curTextFont': curTextFont,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7924
        'horizontalTextAlignment': horizontalTextAlignment,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7925
        'verticalTextAlignment': verticalTextAlignment,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7926
        'textMode': textMode,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7927
        'curFontName': curFontName,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7928
        'curTextSize': curTextSize,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7929
        'curTextAscent': curTextAscent,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7930
        'curTextDescent': curTextDescent,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7931
        'curTextLeading': curTextLeading
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7932
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7933
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7934
      styleArray.push(newState);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7935
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7936
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7937
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7938
    * The pushStyle() function saves the current style settings and popStyle()  restores the prior settings; these
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7939
    * functions are always used together. They allow you to change the style settings and later return to what you had.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7940
    * When a new style is started with pushStyle(), it builds on the current style information. The pushStyle() and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7941
    * popStyle() functions can be embedded to provide more control (see the second example above for a demonstration.)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7942
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7943
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7944
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7945
    * @see pushStyle
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7946
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7947
    p.popStyle = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7948
      var oldState = styleArray.pop();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7949
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7950
      if (oldState) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7951
        restoreContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7952
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7953
        p.popMatrix();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7954
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7955
        doFill = oldState.doFill;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7956
        currentFillColor = oldState.currentFillColor;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7957
        doStroke = oldState.doStroke;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7958
        currentStrokeColor = oldState.currentStrokeColor;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7959
        curTint = oldState.curTint;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7960
        curRectMode = oldState.curRectmode;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7961
        curColorMode = oldState.curColorMode;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7962
        colorModeX = oldState.colorModeX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7963
        colorModeZ = oldState.colorModeZ;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7964
        colorModeY = oldState.colorModeY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7965
        colorModeA = oldState.colorModeA;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7966
        curTextFont = oldState.curTextFont;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7967
        curFontName = oldState.curFontName;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7968
        curTextSize = oldState.curTextSize;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7969
        horizontalTextAlignment = oldState.horizontalTextAlignment;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7970
        verticalTextAlignment = oldState.verticalTextAlignment;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7971
        textMode = oldState.textMode;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7972
        curTextAscent = oldState.curTextAscent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7973
        curTextDescent = oldState.curTextDescent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7974
        curTextLeading = oldState.curTextLeading;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7975
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7976
        throw "Too many popStyle() without enough pushStyle()";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7977
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7978
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7979
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7980
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7981
    // Time based functions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7982
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7983
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7984
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7985
    * Processing communicates with the clock on your computer.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7986
    * The year() function returns the current year as an integer (2003, 2004, 2005, etc).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7987
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7988
    * @returns {float} The current year.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7989
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7990
    * @see millis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7991
    * @see second
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7992
    * @see minute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7993
    * @see hour
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7994
    * @see day
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7995
    * @see month
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7996
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7997
    p.year = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7998
      return new Date().getFullYear();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  7999
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8000
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8001
    * Processing communicates with the clock on your computer.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8002
    * The month() function returns the current month as a value from 1 - 12.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8003
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8004
    * @returns {float} The current month.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8005
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8006
    * @see millis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8007
    * @see second
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8008
    * @see minute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8009
    * @see hour
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8010
    * @see day
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8011
    * @see year
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8012
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8013
    p.month = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8014
      return new Date().getMonth() + 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8015
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8016
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8017
    * Processing communicates with the clock on your computer.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8018
    * The day() function returns the current day as a value from 1 - 31.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8019
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8020
    * @returns {float} The current day.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8021
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8022
    * @see millis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8023
    * @see second
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8024
    * @see minute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8025
    * @see hour
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8026
    * @see month
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8027
    * @see year
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8028
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8029
    p.day = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8030
      return new Date().getDate();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8031
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8032
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8033
    * Processing communicates with the clock on your computer.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8034
    * The hour() function returns the current hour as a value from 0 - 23.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8035
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8036
    * @returns {float} The current hour.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8037
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8038
    * @see millis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8039
    * @see second
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8040
    * @see minute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8041
    * @see month
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8042
    * @see day
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8043
    * @see year
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8044
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8045
    p.hour = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8046
      return new Date().getHours();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8047
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8048
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8049
    * Processing communicates with the clock on your computer.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8050
    * The minute() function returns the current minute as a value from 0 - 59.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8051
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8052
    * @returns {float} The current minute.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8053
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8054
    * @see millis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8055
    * @see second
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8056
    * @see month
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8057
    * @see hour
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8058
    * @see day
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8059
    * @see year
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8060
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8061
    p.minute = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8062
      return new Date().getMinutes();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8063
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8064
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8065
    * Processing communicates with the clock on your computer.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8066
    * The second() function returns the current second as a value from 0 - 59.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8067
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8068
    * @returns {float} The current minute.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8069
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8070
    * @see millis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8071
    * @see month
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8072
    * @see minute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8073
    * @see hour
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8074
    * @see day
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8075
    * @see year
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8076
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8077
    p.second = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8078
      return new Date().getSeconds();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8079
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8080
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8081
    * Returns the number of milliseconds (thousandths of a second) since starting a sketch.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8082
    * This information is often used for timing animation sequences.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8083
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8084
    * @returns {long} The number of milliseconds since starting the sketch.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8085
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8086
    * @see month
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8087
    * @see second
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8088
    * @see minute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8089
    * @see hour
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8090
    * @see day
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8091
    * @see year
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8092
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8093
    p.millis = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8094
      return Date.now() - start;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8095
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8096
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8097
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8098
    * Executes the code within draw() one time. This functions allows the program to update
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8099
    * the display window only when necessary, for example when an event registered by
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8100
    * mousePressed() or keyPressed() occurs.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8101
    * In structuring a program, it only makes sense to call redraw() within events such as
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8102
    * mousePressed(). This is because redraw() does not run draw() immediately (it only sets
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8103
    * a flag that indicates an update is needed).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8104
    * Calling redraw() within draw() has no effect because draw() is continuously called anyway.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8105
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8106
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8107
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8108
    * @see noLoop
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8109
    * @see loop
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8110
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8111
    function redrawHelper() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8112
      var sec = (Date.now() - timeSinceLastFPS) / 1000;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8113
      framesSinceLastFPS++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8114
      var fps = framesSinceLastFPS / sec;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8115
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8116
      // recalculate FPS every half second for better accuracy.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8117
      if (sec > 0.5) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8118
        timeSinceLastFPS = Date.now();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8119
        framesSinceLastFPS = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8120
        p.__frameRate = fps;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8121
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8122
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8123
      p.frameCount++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8124
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8125
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8126
    Drawing2D.prototype.redraw = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8127
      redrawHelper();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8128
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8129
      curContext.lineWidth = lineWidth;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8130
      var pmouseXLastEvent = p.pmouseX,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8131
          pmouseYLastEvent = p.pmouseY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8132
      p.pmouseX = pmouseXLastFrame;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8133
      p.pmouseY = pmouseYLastFrame;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8134
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8135
      saveContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8136
      p.draw();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8137
      restoreContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8138
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8139
      pmouseXLastFrame = p.mouseX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8140
      pmouseYLastFrame = p.mouseY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8141
      p.pmouseX = pmouseXLastEvent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8142
      p.pmouseY = pmouseYLastEvent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8143
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8144
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8145
    Drawing3D.prototype.redraw = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8146
      redrawHelper();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8147
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8148
      var pmouseXLastEvent = p.pmouseX,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8149
          pmouseYLastEvent = p.pmouseY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8150
      p.pmouseX = pmouseXLastFrame;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8151
      p.pmouseY = pmouseYLastFrame;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8152
      // even if the color buffer isn't cleared with background(),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8153
      // the depth buffer needs to be cleared regardless.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8154
      curContext.clear(curContext.DEPTH_BUFFER_BIT);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8155
      curContextCache = { attributes: {}, locations: {} };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8156
      // Delete all the lighting states and the materials the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8157
      // user set in the last draw() call.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8158
      p.noLights();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8159
      p.lightFalloff(1, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8160
      p.shininess(1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8161
      p.ambient(255, 255, 255);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8162
      p.specular(0, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8163
      p.emissive(0, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8164
      p.camera();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8165
      p.draw();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8166
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8167
      pmouseXLastFrame = p.mouseX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8168
      pmouseYLastFrame = p.mouseY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8169
      p.pmouseX = pmouseXLastEvent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8170
      p.pmouseY = pmouseYLastEvent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8171
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8172
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8173
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8174
    * Stops Processing from continuously executing the code within draw(). If loop() is
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8175
    * called, the code in draw() begin to run continuously again. If using noLoop() in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8176
    * setup(), it should be the last line inside the block.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8177
    * When noLoop() is used, it's not possible to manipulate or access the screen inside event
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8178
    * handling functions such as mousePressed() or keyPressed(). Instead, use those functions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8179
    * to call redraw() or loop(), which will run draw(), which can update the screen properly.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8180
    * This means that when noLoop() has been called, no drawing can happen, and functions like
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8181
    * saveFrame() or loadPixels() may not be used.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8182
    * Note that if the sketch is resized, redraw() will be called to update the sketch, even
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8183
    * after noLoop() has been specified. Otherwise, the sketch would enter an odd state until
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8184
    * loop() was called.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8185
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8186
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8187
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8188
    * @see redraw
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8189
    * @see draw
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8190
    * @see loop
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8191
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8192
    p.noLoop = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8193
      doLoop = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8194
      loopStarted = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8195
      clearInterval(looping);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8196
      curSketch.onPause();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8197
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8198
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8199
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8200
    * Causes Processing to continuously execute the code within draw(). If noLoop() is called,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8201
    * the code in draw() stops executing.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8202
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8203
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8204
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8205
    * @see noLoop
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8206
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8207
    p.loop = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8208
      if (loopStarted) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8209
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8210
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8211
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8212
      timeSinceLastFPS = Date.now();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8213
      framesSinceLastFPS = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8214
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8215
      looping = window.setInterval(function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8216
        try {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8217
          curSketch.onFrameStart();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8218
          p.redraw();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8219
          curSketch.onFrameEnd();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8220
        } catch(e_loop) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8221
          window.clearInterval(looping);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8222
          throw e_loop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8223
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8224
      }, curMsPerFrame);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8225
      doLoop = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8226
      loopStarted = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8227
      curSketch.onLoop();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8228
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8229
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8230
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8231
    * Specifies the number of frames to be displayed every second. If the processor is not
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8232
    * fast enough to maintain the specified rate, it will not be achieved. For example, the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8233
    * function call frameRate(30) will attempt to refresh 30 times a second. It is recommended
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8234
    * to set the frame rate within setup(). The default rate is 60 frames per second.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8235
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8236
    * @param {int} aRate        number of frames per second.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8237
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8238
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8239
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8240
    * @see delay
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8241
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8242
    p.frameRate = function(aRate) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8243
      curFrameRate = aRate;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8244
      curMsPerFrame = 1000 / curFrameRate;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8245
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8246
      // clear and reset interval
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8247
      if (doLoop) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8248
        p.noLoop();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8249
        p.loop();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8250
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8251
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8252
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8253
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8254
    // JavaScript event binding and releasing
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8255
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8256
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8257
    var eventHandlers = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8258
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8259
    function attachEventHandler(elem, type, fn) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8260
      if (elem.addEventListener) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8261
        elem.addEventListener(type, fn, false);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8262
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8263
        elem.attachEvent("on" + type, fn);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8264
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8265
      eventHandlers.push({elem: elem, type: type, fn: fn});
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8266
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8267
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8268
    function detachEventHandler(eventHandler) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8269
      var elem = eventHandler.elem,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8270
          type = eventHandler.type,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8271
          fn   = eventHandler.fn;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8272
      if (elem.removeEventListener) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8273
        elem.removeEventListener(type, fn, false);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8274
      } else if (elem.detachEvent) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8275
        elem.detachEvent("on" + type, fn);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8276
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8277
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8278
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8279
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8280
    * Quits/stops/exits the program. Programs without a draw() function exit automatically
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8281
    * after the last line has run, but programs with draw() run continuously until the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8282
    * program is manually stopped or exit() is run.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8283
    * Rather than terminating immediately, exit() will cause the sketch to exit after draw()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8284
    * has completed (or after setup() completes if called during the setup() method).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8285
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8286
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8287
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8288
    p.exit = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8289
      window.clearInterval(looping);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8290
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8291
      removeInstance(p.externals.canvas.id);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8292
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8293
      // Step through the libraries to detach them
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8294
      for (var lib in Processing.lib) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8295
        if (Processing.lib.hasOwnProperty(lib)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8296
          if (Processing.lib[lib].hasOwnProperty("detach")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8297
            Processing.lib[lib].detach(p);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8298
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8299
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8300
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8301
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8302
      var i = eventHandlers.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8303
      while (i--) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8304
        detachEventHandler(eventHandlers[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8305
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8306
      curSketch.onExit();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8307
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8308
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8309
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8310
    // MISC functions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8311
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8312
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8313
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8314
    * Sets the cursor to a predefined symbol, an image, or turns it on if already hidden.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8315
    * If you are trying to set an image as the cursor, it is recommended to make the size
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8316
    * 16x16 or 32x32 pixels. It is not possible to load an image as the cursor if you are
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8317
    * exporting your program for the Web. The values for parameters x and y must be less
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8318
    * than the dimensions of the image.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8319
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8320
    * @param {MODE} MODE either ARROW, CROSS, HAND, MOVE, TEXT, WAIT
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8321
    * @param {PImage} image       any variable of type PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8322
    * @param {int}    x           the horizonal active spot of the cursor
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8323
    * @param {int}    y           the vertical active spot of the cursor
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8324
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8325
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8326
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8327
    * @see noCursor
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8328
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8329
    p.cursor = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8330
      if (arguments.length > 1 || (arguments.length === 1 && arguments[0] instanceof p.PImage)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8331
        var image = arguments[0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8332
          x, y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8333
        if (arguments.length >= 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8334
          x = arguments[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8335
          y = arguments[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8336
          if (x < 0 || y < 0 || y >= image.height || x >= image.width) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8337
            throw "x and y must be non-negative and less than the dimensions of the image";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8338
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8339
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8340
          x = image.width >>> 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8341
          y = image.height >>> 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8342
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8343
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8344
        // see https://developer.mozilla.org/en/Using_URL_values_for_the_cursor_property
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8345
        var imageDataURL = image.toDataURL();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8346
        var style = "url(\"" + imageDataURL + "\") " + x + " " + y + ", default";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8347
        curCursor = curElement.style.cursor = style;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8348
      } else if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8349
        var mode = arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8350
        curCursor = curElement.style.cursor = mode;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8351
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8352
        curCursor = curElement.style.cursor = oldCursor;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8353
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8354
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8355
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8356
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8357
    * Hides the cursor from view.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8358
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8359
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8360
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8361
    * @see cursor
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8362
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8363
    p.noCursor = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8364
      curCursor = curElement.style.cursor = PConstants.NOCURSOR;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8365
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8366
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8367
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8368
    * Links to a webpage either in the same window or in a new window. The complete URL
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8369
    * must be specified.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8370
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8371
    * @param {String} href      complete url as a String in quotes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8372
    * @param {String} target    name of the window to load the URL as a string in quotes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8373
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8374
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8375
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8376
    p.link = function(href, target) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8377
      if (target !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8378
        window.open(href, target);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8379
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8380
        window.location = href;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8381
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8382
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8383
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8384
    // PGraphics methods
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8385
    // These functions exist only for compatibility with P5
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8386
    p.beginDraw = nop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8387
    p.endDraw = nop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8388
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8389
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8390
     * This function takes content from a canvas and turns it into an ImageData object to be used with a PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8391
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8392
     * @returns {ImageData}        ImageData object to attach to a PImage (1D array of pixel data)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8393
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8394
     * @see PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8395
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8396
    Drawing2D.prototype.toImageData = function(x, y, w, h) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8397
      x = x !== undef ? x : 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8398
      y = y !== undef ? y : 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8399
      w = w !== undef ? w : p.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8400
      h = h !== undef ? h : p.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8401
      return curContext.getImageData(x, y, w, h);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8402
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8403
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8404
    Drawing3D.prototype.toImageData = function(x, y, w, h) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8405
      x = x !== undef ? x : 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8406
      y = y !== undef ? y : 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8407
      w = w !== undef ? w : p.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8408
      h = h !== undef ? h : p.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8409
      var c = document.createElement("canvas"),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8410
          ctx = c.getContext("2d"),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8411
          obj = ctx.createImageData(w, h),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8412
          uBuff = new Uint8Array(w * h * 4);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8413
      curContext.readPixels(x, y, w, h, curContext.RGBA, curContext.UNSIGNED_BYTE, uBuff);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8414
      for (var i=0, ul=uBuff.length, obj_data=obj.data; i < ul; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8415
        obj_data[i] = uBuff[(h - 1 - Math.floor(i / 4 / w)) * w * 4 + (i % (w * 4))];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8416
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8417
      return obj;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8418
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8419
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8420
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8421
    * Displays message in the browser's status area. This is the text area in the lower
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8422
    * left corner of the browser. The status() function will only work when the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8423
    * Processing program is running in a web browser.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8424
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8425
    * @param {String} text      any valid String
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8426
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8427
    * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8428
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8429
    p.status = function(text) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8430
      window.status = text;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8431
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8432
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8433
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8434
    // Binary Functions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8435
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8436
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8437
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8438
    * Converts a byte, char, int, or color to a String containing the equivalent binary
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8439
    * notation. For example color(0, 102, 153, 255) will convert to the String
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8440
    * "11111111000000000110011010011001". This function can help make your geeky debugging
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8441
    * sessions much happier.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8442
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8443
    * @param {byte|char|int|color} num          byte, char, int, color: value to convert
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8444
    * @param {int} numBits                      number of digits to return
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8445
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8446
    * @returns {String}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8447
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8448
    * @see unhex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8449
    * @see hex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8450
    * @see unbinary
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8451
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8452
    p.binary = function(num, numBits) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8453
      var bit;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8454
      if (numBits > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8455
        bit = numBits;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8456
      } else if(num instanceof Char) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8457
        bit = 16;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8458
        num |= 0; // making it int
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8459
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8460
        // autodetect, skipping zeros
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8461
        bit = 32;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8462
        while (bit > 1 && !((num >>> (bit - 1)) & 1)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8463
          bit--;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8464
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8465
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8466
      var result = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8467
      while (bit > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8468
        result += ((num >>> (--bit)) & 1) ? "1" : "0";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8469
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8470
      return result;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8471
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8472
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8473
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8474
    * Converts a String representation of a binary number to its equivalent integer value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8475
    * For example, unbinary("00001000") will return 8.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8476
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8477
    * @param {String} binaryString String
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8478
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8479
    * @returns {Int}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8480
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8481
    * @see hex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8482
    * @see binary
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8483
    * @see unbinary
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8484
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8485
    p.unbinary = function(binaryString) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8486
      var i = binaryString.length - 1, mask = 1, result = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8487
      while (i >= 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8488
        var ch = binaryString[i--];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8489
        if (ch !== '0' && ch !== '1') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8490
          throw "the value passed into unbinary was not an 8 bit binary number";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8491
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8492
        if (ch === '1') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8493
          result += mask;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8494
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8495
        mask <<= 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8496
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8497
      return result;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8498
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8499
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8500
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8501
    * Number-to-String formatting function. Prepends "plus" or "minus" depending
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8502
    * on whether the value is positive or negative, respectively, after padding
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8503
    * the value with zeroes on the left and right, the number of zeroes used dictated
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8504
    * by the values 'leftDigits' and 'rightDigits'. 'value' cannot be an array.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8505
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8506
    * @param {int|float} value                 the number to format
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8507
    * @param {String} plus                     the prefix for positive numbers
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8508
    * @param {String} minus                    the prefix for negative numbers
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8509
    * @param {int} left                        number of digits to the left of the decimal point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8510
    * @param {int} right                       number of digits to the right of the decimal point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8511
    * @param {String} group                    string delimited for groups, such as the comma in "1,000"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8512
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8513
    * @returns {String or String[]}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8514
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8515
    * @see nfCore
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8516
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8517
    function nfCoreScalar(value, plus, minus, leftDigits, rightDigits, group) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8518
      var sign = (value < 0) ? minus : plus;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8519
      var autoDetectDecimals = rightDigits === 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8520
      var rightDigitsOfDefault = (rightDigits === undef || rightDigits < 0) ? 0 : rightDigits;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8521
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8522
      var absValue = Math.abs(value);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8523
      if (autoDetectDecimals) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8524
        rightDigitsOfDefault = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8525
        absValue *= 10;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8526
        while (Math.abs(Math.round(absValue) - absValue) > 1e-6 && rightDigitsOfDefault < 7) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8527
          ++rightDigitsOfDefault;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8528
          absValue *= 10;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8529
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8530
      } else if (rightDigitsOfDefault !== 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8531
        absValue *= Math.pow(10, rightDigitsOfDefault);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8532
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8533
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8534
      // Using Java's default rounding policy HALF_EVEN. This policy is based
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8535
      // on the idea that 0.5 values round to the nearest even number, and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8536
      // everything else is rounded normally.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8537
      var number, doubled = absValue * 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8538
      if (Math.floor(absValue) === absValue) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8539
        number = absValue;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8540
      } else if (Math.floor(doubled) === doubled) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8541
        var floored = Math.floor(absValue);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8542
        number = floored + (floored % 2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8543
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8544
        number = Math.round(absValue);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8545
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8546
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8547
      var buffer = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8548
      var totalDigits = leftDigits + rightDigitsOfDefault;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8549
      while (totalDigits > 0 || number > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8550
        totalDigits--;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8551
        buffer = "" + (number % 10) + buffer;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8552
        number = Math.floor(number / 10);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8553
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8554
      if (group !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8555
        var i = buffer.length - 3 - rightDigitsOfDefault;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8556
        while(i > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8557
          buffer = buffer.substring(0,i) + group + buffer.substring(i);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8558
          i-=3;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8559
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8560
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8561
      if (rightDigitsOfDefault > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8562
        return sign + buffer.substring(0, buffer.length - rightDigitsOfDefault) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8563
               "." + buffer.substring(buffer.length - rightDigitsOfDefault, buffer.length);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8564
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8565
      return sign + buffer;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8566
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8567
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8568
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8569
    * Number-to-String formatting function. Prepends "plus" or "minus" depending
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8570
    * on whether the value is positive or negative, respectively, after padding
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8571
    * the value with zeroes on the left and right, the number of zeroes used dictated
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8572
    * by the values 'leftDigits' and 'rightDigits'. 'value' can be an array;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8573
    * if the input is an array, each value in it is formatted separately, and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8574
    * an array with formatted values is returned.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8575
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8576
    * @param {int|int[]|float|float[]} value   the number(s) to format
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8577
    * @param {String} plus                     the prefix for positive numbers
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8578
    * @param {String} minus                    the prefix for negative numbers
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8579
    * @param {int} left                        number of digits to the left of the decimal point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8580
    * @param {int} right                       number of digits to the right of the decimal point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8581
    * @param {String} group                    string delimited for groups, such as the comma in "1,000"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8582
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8583
    * @returns {String or String[]}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8584
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8585
    * @see nfCoreScalar
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8586
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8587
    function nfCore(value, plus, minus, leftDigits, rightDigits, group) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8588
      if (value instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8589
        var arr = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8590
        for (var i = 0, len = value.length; i < len; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8591
          arr.push(nfCoreScalar(value[i], plus, minus, leftDigits, rightDigits, group));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8592
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8593
        return arr;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8594
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8595
      return nfCoreScalar(value, plus, minus, leftDigits, rightDigits, group);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8596
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8597
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8598
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8599
    * Utility function for formatting numbers into strings. There are two versions, one for
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8600
    * formatting floats and one for formatting ints. The values for the digits, left, and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8601
    * right parameters should always be positive integers.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8602
    * As shown in the above example, nf() is used to add zeros to the left and/or right
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8603
    * of a number. This is typically for aligning a list of numbers. To remove digits from
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8604
    * a floating-point number, use the int(), ceil(), floor(), or round() functions.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8605
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8606
    * @param {int|int[]|float|float[]} value   the number(s) to format
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8607
    * @param {int} left                        number of digits to the left of the decimal point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8608
    * @param {int} right                       number of digits to the right of the decimal point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8609
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8610
    * @returns {String or String[]}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8611
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8612
    * @see nfs
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8613
    * @see nfp
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8614
    * @see nfc
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8615
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8616
    p.nf = function(value, leftDigits, rightDigits) { return nfCore(value, "", "-", leftDigits, rightDigits); };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8617
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8618
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8619
    * Utility function for formatting numbers into strings. Similar to nf()  but leaves a blank space in front
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8620
    * of positive numbers so they align with negative numbers in spite of the minus symbol. There are two
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8621
    * versions, one for formatting floats and one for formatting ints. The values for the digits, left,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8622
    * and right parameters should always be positive integers.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8623
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8624
    * @param {int|int[]|float|float[]} value   the number(s) to format
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8625
    * @param {int} left                        number of digits to the left of the decimal point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8626
    * @param {int} right                       number of digits to the right of the decimal point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8627
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8628
    * @returns {String or String[]}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8629
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8630
    * @see nf
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8631
    * @see nfp
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8632
    * @see nfc
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8633
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8634
    p.nfs = function(value, leftDigits, rightDigits) { return nfCore(value, " ", "-", leftDigits, rightDigits); };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8635
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8636
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8637
    * Utility function for formatting numbers into strings. Similar to nf()  but puts a "+" in front of
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8638
    * positive numbers and a "-" in front of negative numbers. There are two versions, one for formatting
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8639
    * floats and one for formatting ints. The values for the digits, left, and right parameters should
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8640
    * always be positive integers.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8641
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8642
    * @param {int|int[]|float|float[]} value   the number(s) to format
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8643
    * @param {int} left                        number of digits to the left of the decimal point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8644
    * @param {int} right                       number of digits to the right of the decimal point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8645
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8646
    * @returns {String or String[]}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8647
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8648
    * @see nfs
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8649
    * @see nf
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8650
    * @see nfc
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8651
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8652
    p.nfp = function(value, leftDigits, rightDigits) { return nfCore(value, "+", "-", leftDigits, rightDigits); };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8653
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8654
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8655
    * Utility function for formatting numbers into strings and placing appropriate commas to mark
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8656
    * units of 1000. There are two versions, one for formatting ints and one for formatting an array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8657
    * of ints. The value for the digits parameter should always be a positive integer.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8658
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8659
    * @param {int|int[]|float|float[]} value   the number(s) to format
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8660
    * @param {int} left                        number of digits to the left of the decimal point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8661
    * @param {int} right                       number of digits to the right of the decimal point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8662
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8663
    * @returns {String or String[]}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8664
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8665
    * @see nf
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8666
    * @see nfs
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8667
    * @see nfp
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8668
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8669
    p.nfc = function(value, leftDigits, rightDigits) { return nfCore(value, "", "-", leftDigits, rightDigits, ","); };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8670
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8671
    var decimalToHex = function(d, padding) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8672
      //if there is no padding value added, default padding to 8 else go into while statement.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8673
      padding = (padding === undef || padding === null) ? padding = 8 : padding;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8674
      if (d < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8675
        d = 0xFFFFFFFF + d + 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8676
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8677
      var hex = Number(d).toString(16).toUpperCase();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8678
      while (hex.length < padding) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8679
        hex = "0" + hex;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8680
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8681
      if (hex.length >= padding) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8682
        hex = hex.substring(hex.length - padding, hex.length);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8683
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8684
      return hex;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8685
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8686
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8687
    // note: since we cannot keep track of byte, int types by default the returned string is 8 chars long
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8688
    // if no 2nd argument is passed.  closest compromise we can use to match java implementation Feb 5 2010
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8689
    // also the char parser has issues with chars that are not digits or letters IE: !@#$%^&*
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8690
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8691
    * Converts a byte, char, int, or color to a String containing the equivalent hexadecimal notation.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8692
    * For example color(0, 102, 153, 255) will convert to the String "FF006699". This function can help
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8693
    * make your geeky debugging sessions much happier.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8694
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8695
    * @param {byte|char|int|Color} value   the value to turn into a hex string
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8696
    * @param {int} digits                 the number of digits to return
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8697
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8698
    * @returns {String}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8699
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8700
    * @see unhex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8701
    * @see binary
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8702
    * @see unbinary
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8703
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8704
    p.hex = function(value, len) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8705
      if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8706
        if (value instanceof Char) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8707
          len = 4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8708
        } else { // int or byte, indistinguishable at the moment, default to 8
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8709
          len = 8;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8710
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8711
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8712
      return decimalToHex(value, len);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8713
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8714
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8715
    function unhexScalar(hex) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8716
      var value = parseInt("0x" + hex, 16);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8717
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8718
      // correct for int overflow java expectation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8719
      if (value > 2147483647) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8720
        value -= 4294967296;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8721
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8722
      return value;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8723
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8724
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8725
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8726
    * Converts a String representation of a hexadecimal number to its equivalent integer value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8727
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8728
    * @param {String} hex   the hex string to convert to an int
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8729
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8730
    * @returns {int}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8731
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8732
    * @see hex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8733
    * @see binary
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8734
    * @see unbinary
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8735
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8736
    p.unhex = function(hex) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8737
      if (hex instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8738
        var arr = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8739
        for (var i = 0; i < hex.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8740
          arr.push(unhexScalar(hex[i]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8741
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8742
        return arr;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8743
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8744
      return unhexScalar(hex);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8745
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8746
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8747
    // Load a file or URL into strings
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8748
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8749
    * Reads the contents of a file or url and creates a String array of its individual lines.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8750
    * The filename parameter can also be a URL to a file found online.  If the file is not available or an error occurs,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8751
    * null will be returned and an error message will be printed to the console. The error message does not halt
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8752
    * the program.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8753
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8754
    * @param {String} filename    name of the file or url to load
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8755
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8756
    * @returns {String[]}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8757
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8758
    * @see loadBytes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8759
    * @see saveStrings
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8760
    * @see saveBytes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8761
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8762
    p.loadStrings = function(filename) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8763
      if (localStorage[filename]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8764
        return localStorage[filename].split("\n");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8765
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8766
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8767
      var filecontent = ajax(filename);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8768
      if(typeof filecontent !== "string" || filecontent === "") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8769
        return [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8770
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8771
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8772
      // deal with the fact that Windows uses \r\n, Unix uses \n,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8773
      // Mac uses \r, and we actually expect \n
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8774
      filecontent = filecontent.replace(/(\r\n?)/g,"\n").replace(/\n$/,"");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8775
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8776
      return filecontent.split("\n");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8777
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8778
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8779
    // Writes an array of strings to a file, one line per string
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8780
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8781
    * Writes an array of strings to a file, one line per string. This file is saved to the localStorage.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8782
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8783
    * @param {String} filename    name of the file to save to localStorage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8784
    * @param {String[]} strings   string array to be written
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8785
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8786
    * @see loadBytes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8787
    * @see loadStrings
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8788
    * @see saveBytes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8789
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8790
    p.saveStrings = function(filename, strings) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8791
      localStorage[filename] = strings.join('\n');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8792
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8793
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8794
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8795
    * Reads the contents of a file or url and places it in a byte array. If a file is specified, it must be located in the localStorage.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8796
    * The filename parameter can also be a URL to a file found online.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8797
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8798
    * @param {String} filename   name of a file in the localStorage or a URL.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8799
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8800
    * @returns {byte[]}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8801
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8802
    * @see loadStrings
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8803
    * @see saveStrings
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8804
    * @see saveBytes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8805
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8806
    p.loadBytes = function(url) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8807
      var string = ajax(url);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8808
      var ret = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8809
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8810
      for (var i = 0; i < string.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8811
        ret.push(string.charCodeAt(i));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8812
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8813
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8814
      return ret;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8815
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8816
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8817
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8818
     * Removes the first argument from the arguments set -- shifts.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8819
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8820
     * @param {Arguments} args  The Arguments object.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8821
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8822
     * @return {Object[]}       Returns an array of arguments except first one.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8823
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8824
     * @see #match
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8825
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8826
    function removeFirstArgument(args) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8827
      return Array.prototype.slice.call(args, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8828
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8829
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8830
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8831
    // String Functions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8832
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8833
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8834
     * The matchAll() function is identical to match(), except that it returns an array of all matches in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8835
     * the specified String, rather than just the first.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8836
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8837
     * @param {String} aString  the String to search inside
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8838
     * @param {String} aRegExp  the regexp to be used for matching
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8839
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8840
     * @return {String[]} returns an array of matches
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8841
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8842
     * @see #match
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8843
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8844
    p.matchAll = function(aString, aRegExp) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8845
      var results = [],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8846
          latest;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8847
      var regexp = new RegExp(aRegExp, "g");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8848
      while ((latest = regexp.exec(aString)) !== null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8849
        results.push(latest);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8850
        if (latest[0].length === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8851
          ++regexp.lastIndex;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8852
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8853
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8854
      return results.length > 0 ? results : null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8855
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8856
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8857
     * The contains(string) function returns true if the string passed in the parameter
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8858
     * is a substring of this string. It returns false if the string passed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8859
     * in the parameter is not a substring of this string.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8860
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8861
     * @param {String} The string to look for in the current string
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8862
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8863
     * @return {boolean} returns true if this string contains
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8864
     * the string passed as parameter. returns false, otherwise.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8865
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8866
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8867
    p.__contains = function (subject, subStr) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8868
      if (typeof subject !== "string") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8869
        return subject.contains.apply(subject, removeFirstArgument(arguments));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8870
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8871
      //Parameter is not null AND
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8872
      //The type of the parameter is the same as this object (string)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8873
      //The javascript function that finds a substring returns 0 or higher
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8874
      return (
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8875
        (subject !== null) &&
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8876
        (subStr !== null) &&
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8877
        (typeof subStr === "string") &&
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8878
        (subject.indexOf(subStr) > -1)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8879
      );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8880
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8881
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8882
     * The __replaceAll() function searches all matches between a substring (or regular expression) and a string,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8883
     * and replaces the matched substring with a new substring
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8884
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8885
     * @param {String} subject    a substring
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8886
     * @param {String} regex      a substring or a regular expression
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8887
     * @param {String} replace    the string to replace the found value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8888
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8889
     * @return {String} returns result
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8890
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8891
     * @see #match
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8892
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8893
    p.__replaceAll = function(subject, regex, replacement) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8894
      if (typeof subject !== "string") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8895
        return subject.replaceAll.apply(subject, removeFirstArgument(arguments));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8896
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8897
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8898
      return subject.replace(new RegExp(regex, "g"), replacement);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8899
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8900
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8901
     * The __replaceFirst() function searches first matche between a substring (or regular expression) and a string,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8902
     * and replaces the matched substring with a new substring
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8903
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8904
     * @param {String} subject    a substring
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8905
     * @param {String} regex      a substring or a regular expression
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8906
     * @param {String} replace    the string to replace the found value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8907
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8908
     * @return {String} returns result
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8909
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8910
     * @see #match
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8911
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8912
    p.__replaceFirst = function(subject, regex, replacement) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8913
      if (typeof subject !== "string") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8914
        return subject.replaceFirst.apply(subject, removeFirstArgument(arguments));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8915
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8916
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8917
      return subject.replace(new RegExp(regex, ""), replacement);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8918
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8919
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8920
     * The __replace() function searches all matches between a substring and a string,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8921
     * and replaces the matched substring with a new substring
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8922
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8923
     * @param {String} subject         a substring
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8924
     * @param {String} what         a substring to find
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8925
     * @param {String} replacement    the string to replace the found value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8926
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8927
     * @return {String} returns result
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8928
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8929
    p.__replace = function(subject, what, replacement) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8930
      if (typeof subject !== "string") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8931
        return subject.replace.apply(subject, removeFirstArgument(arguments));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8932
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8933
      if (what instanceof RegExp) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8934
        return subject.replace(what, replacement);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8935
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8936
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8937
      if (typeof what !== "string") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8938
        what = what.toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8939
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8940
      if (what === "") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8941
        return subject;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8942
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8943
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8944
      var i = subject.indexOf(what);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8945
      if (i < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8946
        return subject;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8947
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8948
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8949
      var j = 0, result = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8950
      do {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8951
        result += subject.substring(j, i) + replacement;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8952
        j = i + what.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8953
      } while ( (i = subject.indexOf(what, j)) >= 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8954
      return result + subject.substring(j);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8955
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8956
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8957
     * The __equals() function compares two strings (or objects) to see if they are the same.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8958
     * This method is necessary because it's not possible to compare strings using the equality operator (==).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8959
     * Returns true if the strings are the same and false if they are not.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8960
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8961
     * @param {String} subject  a string used for comparison
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8962
     * @param {String} other  a string used for comparison with
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8963
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8964
     * @return {boolean} true is the strings are the same false otherwise
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8965
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8966
    p.__equals = function(subject, other) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8967
      if (subject.equals instanceof Function) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8968
        return subject.equals.apply(subject, removeFirstArgument(arguments));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8969
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8970
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8971
      // TODO use virtEquals for HashMap here
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8972
      return subject.valueOf() === other.valueOf();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8973
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8974
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8975
     * The __equalsIgnoreCase() function compares two strings to see if they are the same.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8976
     * Returns true if the strings are the same, either when forced to all lower case or
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8977
     * all upper case.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8978
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8979
     * @param {String} subject  a string used for comparison
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8980
     * @param {String} other  a string used for comparison with
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8981
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8982
     * @return {boolean} true is the strings are the same, ignoring case. false otherwise
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8983
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8984
    p.__equalsIgnoreCase = function(subject, other) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8985
      if (typeof subject !== "string") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8986
        return subject.equalsIgnoreCase.apply(subject, removeFirstArgument(arguments));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8987
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8988
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8989
      return subject.toLowerCase() === other.toLowerCase();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8990
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8991
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8992
     * The __toCharArray() function splits the string into a char array.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8993
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8994
     * @param {String} subject The string
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8995
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8996
     * @return {Char[]} a char array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8997
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8998
    p.__toCharArray = function(subject) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  8999
      if (typeof subject !== "string") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9000
        return subject.toCharArray.apply(subject, removeFirstArgument(arguments));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9001
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9002
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9003
      var chars = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9004
      for (var i = 0, len = subject.length; i < len; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9005
        chars[i] = new Char(subject.charAt(i));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9006
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9007
      return chars;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9008
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9009
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9010
     * The __split() function splits a string using the regex delimiter
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9011
     * specified. If limit is specified, the resultant array will have number
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9012
     * of elements equal to or less than the limit.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9013
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9014
     * @param {String} subject string to be split
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9015
     * @param {String} regexp  regex string used to split the subject
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9016
     * @param {int}    limit   max number of tokens to be returned
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9017
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9018
     * @return {String[]} an array of tokens from the split string
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9019
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9020
    p.__split = function(subject, regex, limit) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9021
      if (typeof subject !== "string") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9022
        return subject.split.apply(subject, removeFirstArgument(arguments));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9023
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9024
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9025
      var pattern = new RegExp(regex);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9026
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9027
      // If limit is not specified, use JavaScript's built-in String.split.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9028
      if ((limit === undef) || (limit < 1)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9029
        return subject.split(pattern);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9030
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9031
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9032
      // If limit is specified, JavaScript's built-in String.split has a
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9033
      // different behaviour than Java's. A Java-compatible implementation is
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9034
      // provided here.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9035
      var result = [], currSubject = subject, pos;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9036
      while (((pos = currSubject.search(pattern)) !== -1)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9037
          && (result.length < (limit - 1))) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9038
        var match = pattern.exec(currSubject).toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9039
        result.push(currSubject.substring(0, pos));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9040
        currSubject = currSubject.substring(pos + match.length);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9041
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9042
      if ((pos !== -1) || (currSubject !== "")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9043
        result.push(currSubject);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9044
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9045
      return result;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9046
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9047
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9048
     * The codePointAt() function returns the unicode value of the character at a given index of a string.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9049
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9050
     * @param  {int} idx         the index of the character
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9051
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9052
     * @return {String} code     the String containing the unicode value of the character
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9053
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9054
    p.__codePointAt = function(subject, idx) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9055
      var code = subject.charCodeAt(idx),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9056
          hi,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9057
          low;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9058
      if (0xD800 <= code && code <= 0xDBFF) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9059
        hi = code;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9060
        low = subject.charCodeAt(idx + 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9061
        return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9062
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9063
      return code;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9064
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9065
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9066
     * The match() function matches a string with a regular expression, and returns the match as an
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9067
     * array. The first index is the matching expression, and array elements
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9068
     * [1] and higher represent each of the groups (sequences found in parens).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9069
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9070
     * @param {String} str      the String to be searched
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9071
     * @param {String} regexp   the regexp to be used for matching
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9072
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9073
     * @return {String[]} an array of matching strings
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9074
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9075
    p.match = function(str, regexp) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9076
      return str.match(regexp);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9077
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9078
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9079
     * The startsWith() function tests if a string starts with the specified prefix.  If the prefix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9080
     * is the empty String or equal to the subject String, startsWith() will also return true.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9081
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9082
     * @param {String} prefix   the String used to compare against the start of the subject String.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9083
     * @param {int}    toffset  (optional) an offset into the subject String where searching should begin.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9084
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9085
     * @return {boolean} true if the subject String starts with the prefix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9086
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9087
    p.__startsWith = function(subject, prefix, toffset) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9088
      if (typeof subject !== "string") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9089
        return subject.startsWith.apply(subject, removeFirstArgument(arguments));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9090
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9091
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9092
      toffset = toffset || 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9093
      if (toffset < 0 || toffset > subject.length) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9094
        return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9095
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9096
      return (prefix === '' || prefix === subject) ? true : (subject.indexOf(prefix) === toffset);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9097
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9098
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9099
     * The endsWith() function tests if a string ends with the specified suffix.  If the suffix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9100
     * is the empty String, endsWith() will also return true.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9101
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9102
     * @param {String} suffix   the String used to compare against the end of the subject String.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9103
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9104
     * @return {boolean} true if the subject String starts with the prefix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9105
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9106
    p.__endsWith = function(subject, suffix) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9107
      if (typeof subject !== "string") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9108
        return subject.endsWith.apply(subject, removeFirstArgument(arguments));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9109
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9110
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9111
      var suffixLen = suffix ? suffix.length : 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9112
      return (suffix === '' || suffix === subject) ? true :
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9113
        (subject.indexOf(suffix) === subject.length - suffixLen);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9114
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9115
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9116
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9117
    // Other java specific functions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9118
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9119
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9120
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9121
     * The returns hash code of the.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9122
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9123
     * @param {Object} subject The string
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9124
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9125
     * @return {int} a hash code
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9126
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9127
    p.__hashCode = function(subject) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9128
      if (subject.hashCode instanceof Function) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9129
        return subject.hashCode.apply(subject, removeFirstArgument(arguments));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9130
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9131
      return virtHashCode(subject);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9132
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9133
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9134
     * The __printStackTrace() prints stack trace to the console.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9135
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9136
     * @param {Exception} subject The error
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9137
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9138
    p.__printStackTrace = function(subject) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9139
      p.println("Exception: " + subject.toString() );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9140
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9141
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9142
    var logBuffer = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9143
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9144
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9145
     * The println() function writes to the console area of the Processing environment.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9146
     * Each call to this function creates a new line of output. Individual elements can be separated with quotes ("") and joined with the string concatenation operator (+).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9147
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9148
     * @param {String} message the string to write to the console
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9149
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9150
     * @see #join
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9151
     * @see #print
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9152
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9153
    p.println = function(message) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9154
      var bufferLen = logBuffer.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9155
      if (bufferLen) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9156
        Processing.logger.log(logBuffer.join(""));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9157
        logBuffer.length = 0; // clear log buffer
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9158
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9159
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9160
      if (arguments.length === 0 && bufferLen === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9161
        Processing.logger.log("");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9162
      } else if (arguments.length !== 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9163
        Processing.logger.log(message);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9164
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9165
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9166
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9167
     * The print() function writes to the console area of the Processing environment.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9168
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9169
     * @param {String} message the string to write to the console
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9170
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9171
     * @see #join
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9172
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9173
    p.print = function(message) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9174
      logBuffer.push(message);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9175
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9176
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9177
    // Alphanumeric chars arguments automatically converted to numbers when
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9178
    // passed in, and will come out as numbers.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9179
    p.str = function(val) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9180
      if (val instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9181
        var arr = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9182
        for (var i = 0; i < val.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9183
          arr.push(val[i].toString() + "");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9184
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9185
        return arr;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9186
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9187
      return (val.toString() + "");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9188
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9189
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9190
     * Remove whitespace characters from the beginning and ending
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9191
     * of a String or a String array. Works like String.trim() but includes the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9192
     * unicode nbsp character as well. If an array is passed in the function will return a new array not effecting the array passed in.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9193
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9194
     * @param {String} str    the string to trim
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9195
     * @param {String[]} str  the string array to trim
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9196
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9197
     * @return {String|String[]} retrurns a string or an array will removed whitespaces
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9198
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9199
    p.trim = function(str) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9200
      if (str instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9201
        var arr = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9202
        for (var i = 0; i < str.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9203
          arr.push(str[i].replace(/^\s*/, '').replace(/\s*$/, '').replace(/\r*$/, ''));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9204
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9205
        return arr;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9206
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9207
      return str.replace(/^\s*/, '').replace(/\s*$/, '').replace(/\r*$/, '');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9208
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9209
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9210
    // Conversion
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9211
    function booleanScalar(val) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9212
      if (typeof val === 'number') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9213
        return val !== 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9214
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9215
      if (typeof val === 'boolean') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9216
        return val;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9217
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9218
      if (typeof val === 'string') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9219
        return val.toLowerCase() === 'true';
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9220
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9221
      if (val instanceof Char) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9222
        // 1, T or t
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9223
        return val.code === 49 || val.code === 84 || val.code === 116;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9224
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9225
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9226
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9227
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9228
     * Converts the passed parameter to the function to its boolean value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9229
     * It will return an array of booleans if an array is passed in.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9230
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9231
     * @param {int, byte, string} val          the parameter to be converted to boolean
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9232
     * @param {int[], byte[], string[]} val    the array to be converted to boolean[]
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9233
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9234
     * @return {boolean|boolean[]} returns a boolean or an array of booleans
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9235
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9236
    p.parseBoolean = function (val) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9237
      if (val instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9238
        var ret = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9239
        for (var i = 0; i < val.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9240
          ret.push(booleanScalar(val[i]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9241
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9242
        return ret;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9243
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9244
      return booleanScalar(val);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9245
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9246
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9247
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9248
     * Converts the passed parameter to the function to its byte value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9249
     * A byte is a number between -128 and 127.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9250
     * It will return an array of bytes if an array is passed in.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9251
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9252
     * @param {int, char} what        the parameter to be conveted to byte
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9253
     * @param {int[], char[]} what    the array to be converted to byte[]
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9254
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9255
     * @return {byte|byte[]} returns a byte or an array of bytes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9256
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9257
    p.parseByte = function(what) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9258
      if (what instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9259
        var bytes = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9260
        for (var i = 0; i < what.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9261
          bytes.push((0 - (what[i] & 0x80)) | (what[i] & 0x7F));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9262
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9263
        return bytes;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9264
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9265
      return (0 - (what & 0x80)) | (what & 0x7F);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9266
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9267
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9268
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9269
     * Converts the passed parameter to the function to its char value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9270
     * It will return an array of chars if an array is passed in.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9271
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9272
     * @param {int, byte} key        the parameter to be conveted to char
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9273
     * @param {int[], byte[]} key    the array to be converted to char[]
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9274
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9275
     * @return {char|char[]} returns a char or an array of chars
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9276
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9277
    p.parseChar = function(key) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9278
      if (typeof key === "number") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9279
        return new Char(String.fromCharCode(key & 0xFFFF));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9280
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9281
      if (key instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9282
        var ret = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9283
        for (var i = 0; i < key.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9284
          ret.push(new Char(String.fromCharCode(key[i] & 0xFFFF)));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9285
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9286
        return ret;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9287
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9288
      throw "char() may receive only one argument of type int, byte, int[], or byte[].";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9289
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9290
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9291
    // Processing doc claims good argument types are: int, char, byte, boolean,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9292
    // String, int[], char[], byte[], boolean[], String[].
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9293
    // floats should not work. However, floats with only zeroes right of the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9294
    // decimal will work because JS converts those to int.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9295
    function floatScalar(val) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9296
      if (typeof val === 'number') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9297
        return val;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9298
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9299
      if (typeof val === 'boolean') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9300
        return val ? 1 : 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9301
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9302
      if (typeof val === 'string') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9303
        return parseFloat(val);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9304
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9305
      if (val instanceof Char) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9306
        return val.code;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9307
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9308
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9309
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9310
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9311
     * Converts the passed parameter to the function to its float value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9312
     * It will return an array of floats if an array is passed in.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9313
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9314
     * @param {int, char, boolean, string} val            the parameter to be conveted to float
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9315
     * @param {int[], char[], boolean[], string[]} val    the array to be converted to float[]
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9316
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9317
     * @return {float|float[]} returns a float or an array of floats
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9318
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9319
    p.parseFloat = function(val) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9320
      if (val instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9321
        var ret = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9322
        for (var i = 0; i < val.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9323
          ret.push(floatScalar(val[i]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9324
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9325
        return ret;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9326
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9327
      return floatScalar(val);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9328
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9329
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9330
    function intScalar(val, radix) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9331
      if (typeof val === 'number') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9332
        return val & 0xFFFFFFFF;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9333
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9334
      if (typeof val === 'boolean') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9335
        return val ? 1 : 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9336
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9337
      if (typeof val === 'string') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9338
        var number = parseInt(val, radix || 10); // Default to decimal radix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9339
        return number & 0xFFFFFFFF;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9340
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9341
      if (val instanceof Char) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9342
        return val.code;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9343
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9344
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9345
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9346
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9347
     * Converts the passed parameter to the function to its int value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9348
     * It will return an array of ints if an array is passed in.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9349
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9350
     * @param {string, char, boolean, float} val            the parameter to be conveted to int
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9351
     * @param {string[], char[], boolean[], float[]} val    the array to be converted to int[]
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9352
     * @param {int} radix                                   optional the radix of the number (for js compatibility)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9353
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9354
     * @return {int|int[]} returns a int or an array of ints
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9355
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9356
    p.parseInt = function(val, radix) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9357
      if (val instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9358
        var ret = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9359
        for (var i = 0; i < val.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9360
          if (typeof val[i] === 'string' && !/^\s*[+\-]?\d+\s*$/.test(val[i])) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9361
            ret.push(0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9362
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9363
            ret.push(intScalar(val[i], radix));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9364
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9365
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9366
        return ret;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9367
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9368
      return intScalar(val, radix);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9369
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9370
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9371
    p.__int_cast = function(val) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9372
      return 0|val;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9373
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9374
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9375
    p.__instanceof = function(obj, type) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9376
      if (typeof type !== "function") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9377
        throw "Function is expected as type argument for instanceof operator";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9378
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9379
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9380
      if (typeof obj === "string") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9381
        // special case for strings
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9382
        return type === Object || type === String;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9383
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9384
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9385
      if (obj instanceof type) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9386
        // fast check if obj is already of type instance
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9387
        return true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9388
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9389
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9390
      if (typeof obj !== "object" || obj === null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9391
        return false; // not an object or null
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9392
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9393
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9394
      var objType = obj.constructor;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9395
      if (type.$isInterface) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9396
        // expecting the interface
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9397
        // queueing interfaces from type and its base classes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9398
        var interfaces = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9399
        while (objType) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9400
          if (objType.$interfaces) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9401
            interfaces = interfaces.concat(objType.$interfaces);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9402
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9403
          objType = objType.$base;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9404
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9405
        while (interfaces.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9406
          var i = interfaces.shift();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9407
          if (i === type) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9408
            return true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9409
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9410
          // wide search in base interfaces
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9411
          if (i.$interfaces) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9412
            interfaces = interfaces.concat(i.$interfaces);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9413
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9414
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9415
        return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9416
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9417
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9418
      while (objType.hasOwnProperty("$base")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9419
        objType = objType.$base;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9420
        if (objType === type) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9421
          return true; // object was found
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9422
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9423
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9424
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9425
      return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9426
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9427
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9428
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9429
    // Math functions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9430
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9431
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9432
    // Calculation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9433
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9434
    * Calculates the absolute value (magnitude) of a number. The absolute value of a number is always positive.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9435
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9436
    * @param {int|float} value   int or float
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9437
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9438
    * @returns {int|float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9439
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9440
    p.abs = Math.abs;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9441
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9442
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9443
    * Calculates the closest int value that is greater than or equal to the value of the parameter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9444
    * For example, ceil(9.03) returns the value 10.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9445
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9446
    * @param {float} value   float
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9447
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9448
    * @returns {int}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9449
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9450
    * @see floor
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9451
    * @see round
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9452
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9453
    p.ceil = Math.ceil;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9454
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9455
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9456
    * Constrains a value to not exceed a maximum and minimum value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9457
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9458
    * @param {int|float} value   the value to constrain
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9459
    * @param {int|float} value   minimum limit
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9460
    * @param {int|float} value   maximum limit
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9461
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9462
    * @returns {int|float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9463
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9464
    * @see max
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9465
    * @see min
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9466
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9467
    p.constrain = function(aNumber, aMin, aMax) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9468
      return aNumber > aMax ? aMax : aNumber < aMin ? aMin : aNumber;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9469
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9470
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9471
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9472
    * Calculates the distance between two points.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9473
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9474
    * @param {int|float} x1     int or float: x-coordinate of the first point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9475
    * @param {int|float} y1     int or float: y-coordinate of the first point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9476
    * @param {int|float} z1     int or float: z-coordinate of the first point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9477
    * @param {int|float} x2     int or float: x-coordinate of the second point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9478
    * @param {int|float} y2     int or float: y-coordinate of the second point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9479
    * @param {int|float} z2     int or float: z-coordinate of the second point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9480
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9481
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9482
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9483
    p.dist = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9484
      var dx, dy, dz;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9485
      if (arguments.length === 4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9486
        dx = arguments[0] - arguments[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9487
        dy = arguments[1] - arguments[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9488
        return Math.sqrt(dx * dx + dy * dy);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9489
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9490
      if (arguments.length === 6) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9491
        dx = arguments[0] - arguments[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9492
        dy = arguments[1] - arguments[4];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9493
        dz = arguments[2] - arguments[5];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9494
        return Math.sqrt(dx * dx + dy * dy + dz * dz);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9495
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9496
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9497
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9498
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9499
    * Returns Euler's number e (2.71828...) raised to the power of the value parameter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9500
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9501
    * @param {int|float} value   int or float: the exponent to raise e to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9502
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9503
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9504
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9505
    p.exp = Math.exp;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9506
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9507
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9508
    * Calculates the closest int value that is less than or equal to the value of the parameter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9509
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9510
    * @param {int|float} value        the value to floor
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9511
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9512
    * @returns {int|float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9513
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9514
    * @see ceil
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9515
    * @see round
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9516
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9517
    p.floor = Math.floor;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9518
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9519
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9520
    * Calculates a number between two numbers at a specific increment. The amt  parameter is the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9521
    * amount to interpolate between the two values where 0.0 equal to the first point, 0.1 is very
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9522
    * near the first point, 0.5 is half-way in between, etc. The lerp function is convenient for
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9523
    * creating motion along a straight path and for drawing dotted lines.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9524
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9525
    * @param {int|float} value1       float or int: first value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9526
    * @param {int|float} value2       float or int: second value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9527
    * @param {int|float} amt          float: between 0.0 and 1.0
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9528
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9529
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9530
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9531
    * @see curvePoint
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9532
    * @see bezierPoint
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9533
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9534
    p.lerp = function(value1, value2, amt) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9535
      return ((value2 - value1) * amt) + value1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9536
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9537
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9538
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9539
    * Calculates the natural logarithm (the base-e logarithm) of a number. This function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9540
    * expects the values greater than 0.0.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9541
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9542
    * @param {int|float} value        int or float: number must be greater then 0.0
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9543
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9544
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9545
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9546
    p.log = Math.log;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9547
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9548
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9549
    * Calculates the magnitude (or length) of a vector. A vector is a direction in space commonly
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9550
    * used in computer graphics and linear algebra. Because it has no "start" position, the magnitude
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9551
    * of a vector can be thought of as the distance from coordinate (0,0) to its (x,y) value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9552
    * Therefore, mag() is a shortcut for writing "dist(0, 0, x, y)".
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9553
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9554
    * @param {int|float} a       float or int: first value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9555
    * @param {int|float} b       float or int: second value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9556
    * @param {int|float} c       float or int: third value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9557
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9558
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9559
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9560
    * @see dist
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9561
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9562
    p.mag = function(a, b, c) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9563
      if (c) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9564
        return Math.sqrt(a * a + b * b + c * c);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9565
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9566
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9567
      return Math.sqrt(a * a + b * b);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9568
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9569
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9570
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9571
    * Re-maps a number from one range to another. In the example above, the number '25' is converted from
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9572
    * a value in the range 0..100 into a value that ranges from the left edge (0) to the right edge (width) of the screen.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9573
    * Numbers outside the range are not clamped to 0 and 1, because out-of-range values are often intentional and useful.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9574
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9575
    * @param {float} value        The incoming value to be converted
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9576
    * @param {float} istart       Lower bound of the value's current range
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9577
    * @param {float} istop        Upper bound of the value's current range
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9578
    * @param {float} ostart       Lower bound of the value's target range
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9579
    * @param {float} ostop        Upper bound of the value's target range
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9580
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9581
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9582
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9583
    * @see norm
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9584
    * @see lerp
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9585
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9586
    p.map = function(value, istart, istop, ostart, ostop) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9587
      return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9588
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9589
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9590
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9591
    * Determines the largest value in a sequence of numbers.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9592
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9593
    * @param {int|float} value1         int or float
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9594
    * @param {int|float} value2         int or float
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9595
    * @param {int|float} value3         int or float
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9596
    * @param {int|float} array          int or float array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9597
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9598
    * @returns {int|float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9599
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9600
    * @see min
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9601
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9602
    p.max = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9603
      if (arguments.length === 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9604
        return arguments[0] < arguments[1] ? arguments[1] : arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9605
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9606
      var numbers = arguments.length === 1 ? arguments[0] : arguments; // if single argument, array is used
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9607
      if (! ("length" in numbers && numbers.length > 0)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9608
        throw "Non-empty array is expected";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9609
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9610
      var max = numbers[0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9611
        count = numbers.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9612
      for (var i = 1; i < count; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9613
        if (max < numbers[i]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9614
          max = numbers[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9615
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9616
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9617
      return max;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9618
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9619
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9620
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9621
    * Determines the smallest value in a sequence of numbers.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9622
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9623
    * @param {int|float} value1         int or float
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9624
    * @param {int|float} value2         int or float
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9625
    * @param {int|float} value3         int or float
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9626
    * @param {int|float} array          int or float array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9627
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9628
    * @returns {int|float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9629
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9630
    * @see max
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9631
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9632
    p.min = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9633
      if (arguments.length === 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9634
        return arguments[0] < arguments[1] ? arguments[0] : arguments[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9635
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9636
      var numbers = arguments.length === 1 ? arguments[0] : arguments; // if single argument, array is used
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9637
      if (! ("length" in numbers && numbers.length > 0)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9638
        throw "Non-empty array is expected";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9639
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9640
      var min = numbers[0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9641
        count = numbers.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9642
      for (var i = 1; i < count; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9643
        if (min > numbers[i]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9644
          min = numbers[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9645
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9646
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9647
      return min;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9648
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9649
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9650
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9651
    * Normalizes a number from another range into a value between 0 and 1.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9652
    * Identical to map(value, low, high, 0, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9653
    * Numbers outside the range are not clamped to 0 and 1, because out-of-range
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9654
    * values are often intentional and useful.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9655
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9656
    * @param {float} aNumber    The incoming value to be converted
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9657
    * @param {float} low        Lower bound of the value's current range
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9658
    * @param {float} high       Upper bound of the value's current range
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9659
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9660
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9661
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9662
    * @see map
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9663
    * @see lerp
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9664
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9665
    p.norm = function(aNumber, low, high) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9666
      return (aNumber - low) / (high - low);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9667
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9668
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9669
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9670
    * Facilitates exponential expressions. The pow() function is an efficient way of
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9671
    * multiplying numbers by themselves (or their reciprocal) in large quantities.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9672
    * For example, pow(3, 5) is equivalent to the expression 3*3*3*3*3 and pow(3, -5)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9673
    * is equivalent to 1 / 3*3*3*3*3.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9674
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9675
    * @param {int|float} num        base of the exponential expression
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9676
    * @param {int|float} exponent   power of which to raise the base
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9677
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9678
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9679
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9680
    * @see sqrt
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9681
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9682
    p.pow = Math.pow;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9683
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9684
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9685
    * Calculates the integer closest to the value parameter. For example, round(9.2) returns the value 9.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9686
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9687
    * @param {float} value        number to round
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9688
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9689
    * @returns {int}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9690
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9691
    * @see floor
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9692
    * @see ceil
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9693
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9694
    p.round = Math.round;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9695
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9696
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9697
    * Squares a number (multiplies a number by itself). The result is always a positive number,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9698
    * as multiplying two negative numbers always yields a positive result. For example, -1 * -1 = 1.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9699
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9700
    * @param {float} value        int or float
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9701
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9702
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9703
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9704
    * @see sqrt
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9705
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9706
    p.sq = function(aNumber) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9707
      return aNumber * aNumber;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9708
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9709
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9710
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9711
    * Calculates the square root of a number. The square root of a number is always positive,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9712
    * even though there may be a valid negative root. The square root s of number a is such
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9713
    * that s*s = a. It is the opposite of squaring.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9714
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9715
    * @param {float} value        int or float, non negative
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9716
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9717
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9718
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9719
    * @see pow
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9720
    * @see sq
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9721
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9722
    p.sqrt = Math.sqrt;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9723
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9724
    // Trigonometry
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9725
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9726
    * The inverse of cos(), returns the arc cosine of a value. This function expects the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9727
    * values in the range of -1 to 1 and values are returned in the range 0 to PI (3.1415927).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9728
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9729
    * @param {float} value        the value whose arc cosine is to be returned
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9730
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9731
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9732
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9733
    * @see cos
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9734
    * @see asin
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9735
    * @see atan
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9736
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9737
    p.acos = Math.acos;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9738
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9739
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9740
    * The inverse of sin(), returns the arc sine of a value. This function expects the values
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9741
    * in the range of -1 to 1 and values are returned in the range -PI/2 to PI/2.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9742
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9743
    * @param {float} value        the value whose arc sine is to be returned
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9744
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9745
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9746
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9747
    * @see sin
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9748
    * @see acos
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9749
    * @see atan
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9750
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9751
    p.asin = Math.asin;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9752
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9753
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9754
    * The inverse of tan(), returns the arc tangent of a value. This function expects the values
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9755
    * in the range of -Infinity to Infinity (exclusive) and values are returned in the range -PI/2 to PI/2 .
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9756
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9757
    * @param {float} value        -Infinity to Infinity (exclusive)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9758
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9759
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9760
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9761
    * @see tan
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9762
    * @see asin
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9763
    * @see acos
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9764
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9765
    p.atan = Math.atan;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9766
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9767
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9768
    * Calculates the angle (in radians) from a specified point to the coordinate origin as measured from
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9769
    * the positive x-axis. Values are returned as a float in the range from PI to -PI. The atan2() function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9770
    * is most often used for orienting geometry to the position of the cursor. Note: The y-coordinate of the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9771
    * point is the first parameter and the x-coordinate is the second due the the structure of calculating the tangent.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9772
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9773
    * @param {float} y        y-coordinate of the point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9774
    * @param {float} x        x-coordinate of the point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9775
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9776
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9777
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9778
    * @see tan
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9779
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9780
    p.atan2 = Math.atan2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9781
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9782
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9783
    * Calculates the cosine of an angle. This function expects the values of the angle parameter to be provided
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9784
    * in radians (values from 0 to PI*2). Values are returned in the range -1 to 1.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9785
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9786
    * @param {float} value        an angle in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9787
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9788
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9789
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9790
    * @see tan
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9791
    * @see sin
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9792
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9793
    p.cos = Math.cos;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9794
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9795
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9796
    * Converts a radian measurement to its corresponding value in degrees. Radians and degrees are two ways of
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9797
    * measuring the same thing. There are 360 degrees in a circle and 2*PI radians in a circle. For example,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9798
    * 90 degrees = PI/2 = 1.5707964. All trigonometric methods in Processing require their parameters to be specified in radians.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9799
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9800
    * @param {int|float} value        an angle in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9801
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9802
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9803
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9804
    * @see radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9805
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9806
    p.degrees = function(aAngle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9807
      return (aAngle * 180) / Math.PI;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9808
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9809
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9810
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9811
    * Converts a degree measurement to its corresponding value in radians. Radians and degrees are two ways of
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9812
    * measuring the same thing. There are 360 degrees in a circle and 2*PI radians in a circle. For example,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9813
    * 90 degrees = PI/2 = 1.5707964. All trigonometric methods in Processing require their parameters to be specified in radians.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9814
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9815
    * @param {int|float} value        an angle in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9816
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9817
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9818
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9819
    * @see degrees
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9820
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9821
    p.radians = function(aAngle) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9822
      return (aAngle / 180) * Math.PI;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9823
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9824
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9825
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9826
    * Calculates the sine of an angle. This function expects the values of the angle parameter to be provided in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9827
    * radians (values from 0 to 6.28). Values are returned in the range -1 to 1.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9828
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9829
    * @param {float} value        an angle in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9830
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9831
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9832
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9833
    * @see cos
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9834
    * @see radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9835
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9836
    p.sin = Math.sin;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9837
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9838
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9839
    * Calculates the ratio of the sine and cosine of an angle. This function expects the values of the angle
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9840
    * parameter to be provided in radians (values from 0 to PI*2). Values are returned in the range infinity to -infinity.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9841
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9842
    * @param {float} value        an angle in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9843
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9844
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9845
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9846
    * @see cos
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9847
    * @see sin
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9848
    * @see radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9849
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9850
    p.tan = Math.tan;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9851
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9852
    var currentRandom = Math.random;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9853
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9854
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9855
    * Generates random numbers. Each time the random() function is called, it returns an unexpected value within
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9856
    * the specified range. If one parameter is passed to the function it will return a float between zero and the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9857
    * value of the high parameter. The function call random(5) returns values between 0 and 5 (starting at zero,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9858
    * up to but not including 5). If two parameters are passed, it will return a float with a value between the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9859
    * parameters. The function call random(-5, 10.2) returns values starting at -5 up to (but not including) 10.2.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9860
    * To convert a floating-point random number to an integer, use the int() function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9861
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9862
    * @param {int|float} value1         if one parameter is used, the top end to random from, if two params the low end
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9863
    * @param {int|float} value2         the top end of the random range
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9864
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9865
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9866
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9867
    * @see randomSeed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9868
    * @see noise
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9869
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9870
    p.random = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9871
      if(arguments.length === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9872
        return currentRandom();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9873
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9874
      if(arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9875
        return currentRandom() * arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9876
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9877
      var aMin = arguments[0], aMax = arguments[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9878
      return currentRandom() * (aMax - aMin) + aMin;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9879
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9880
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9881
    // Pseudo-random generator
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9882
    function Marsaglia(i1, i2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9883
      // from http://www.math.uni-bielefeld.de/~sillke/ALGORITHMS/random/marsaglia-c
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9884
      var z=i1 || 362436069, w= i2 || 521288629;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9885
      var nextInt = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9886
        z=(36969*(z&65535)+(z>>>16)) & 0xFFFFFFFF;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9887
        w=(18000*(w&65535)+(w>>>16)) & 0xFFFFFFFF;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9888
        return (((z&0xFFFF)<<16) | (w&0xFFFF)) & 0xFFFFFFFF;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9889
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9890
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9891
      this.nextDouble = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9892
        var i = nextInt() / 4294967296;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9893
        return i < 0 ? 1 + i : i;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9894
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9895
      this.nextInt = nextInt;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9896
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9897
    Marsaglia.createRandomized = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9898
      var now = new Date();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9899
      return new Marsaglia((now / 60000) & 0xFFFFFFFF, now & 0xFFFFFFFF);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9900
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9901
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9902
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9903
    * Sets the seed value for random(). By default, random() produces different results each time the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9904
    * program is run. Set the value parameter to a constant to return the same pseudo-random numbers
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9905
    * each time the software is run.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9906
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9907
    * @param {int|float} seed         int
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9908
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9909
    * @see random
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9910
    * @see noise
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9911
    * @see noiseSeed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9912
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9913
    p.randomSeed = function(seed) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9914
      currentRandom = (new Marsaglia(seed)).nextDouble;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9915
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9916
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9917
    // Random
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9918
    // We have two random()'s in the code... what does this do ? and which one is current ?
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9919
    p.Random = function(seed) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9920
      var haveNextNextGaussian = false, nextNextGaussian, random;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9921
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9922
      this.nextGaussian = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9923
        if (haveNextNextGaussian) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9924
          haveNextNextGaussian = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9925
          return nextNextGaussian;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9926
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9927
        var v1, v2, s;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9928
        do {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9929
          v1 = 2 * random() - 1; // between -1.0 and 1.0
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9930
          v2 = 2 * random() - 1; // between -1.0 and 1.0
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9931
          s = v1 * v1 + v2 * v2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9932
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9933
        while (s >= 1 || s === 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9934
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9935
        var multiplier = Math.sqrt(-2 * Math.log(s) / s);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9936
        nextNextGaussian = v2 * multiplier;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9937
        haveNextNextGaussian = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9938
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9939
        return v1 * multiplier;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9940
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9941
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9942
      // by default use standard random, otherwise seeded
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9943
      random = (seed === undef) ? Math.random : (new Marsaglia(seed)).nextDouble;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9944
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9945
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9946
    // Noise functions and helpers
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9947
    function PerlinNoise(seed) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9948
      var rnd = seed !== undef ? new Marsaglia(seed) : Marsaglia.createRandomized();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9949
      var i, j;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9950
      // http://www.noisemachine.com/talk1/17b.html
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9951
      // http://mrl.nyu.edu/~perlin/noise/
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9952
      // generate permutation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9953
      var perm = new Uint8Array(512);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9954
      for(i=0;i<256;++i) { perm[i] = i; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9955
      for(i=0;i<256;++i) { var t = perm[j = rnd.nextInt() & 0xFF]; perm[j] = perm[i]; perm[i] = t; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9956
      // copy to avoid taking mod in perm[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9957
      for(i=0;i<256;++i) { perm[i + 256] = perm[i]; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9958
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9959
      function grad3d(i,x,y,z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9960
        var h = i & 15; // convert into 12 gradient directions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9961
        var u = h<8 ? x : y,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9962
            v = h<4 ? y : h===12||h===14 ? x : z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9963
        return ((h&1) === 0 ? u : -u) + ((h&2) === 0 ? v : -v);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9964
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9965
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9966
      function grad2d(i,x,y) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9967
        var v = (i & 1) === 0 ? x : y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9968
        return (i&2) === 0 ? -v : v;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9969
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9970
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9971
      function grad1d(i,x) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9972
        return (i&1) === 0 ? -x : x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9973
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9974
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9975
      function lerp(t,a,b) { return a + t * (b - a); }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9976
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9977
      this.noise3d = function(x, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9978
        var X = Math.floor(x)&255, Y = Math.floor(y)&255, Z = Math.floor(z)&255;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9979
        x -= Math.floor(x); y -= Math.floor(y); z -= Math.floor(z);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9980
        var fx = (3-2*x)*x*x, fy = (3-2*y)*y*y, fz = (3-2*z)*z*z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9981
        var p0 = perm[X]+Y, p00 = perm[p0] + Z, p01 = perm[p0 + 1] + Z,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9982
            p1 = perm[X + 1] + Y, p10 = perm[p1] + Z, p11 = perm[p1 + 1] + Z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9983
        return lerp(fz,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9984
          lerp(fy, lerp(fx, grad3d(perm[p00], x, y, z), grad3d(perm[p10], x-1, y, z)),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9985
                   lerp(fx, grad3d(perm[p01], x, y-1, z), grad3d(perm[p11], x-1, y-1,z))),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9986
          lerp(fy, lerp(fx, grad3d(perm[p00 + 1], x, y, z-1), grad3d(perm[p10 + 1], x-1, y, z-1)),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9987
                   lerp(fx, grad3d(perm[p01 + 1], x, y-1, z-1), grad3d(perm[p11 + 1], x-1, y-1,z-1))));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9988
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9989
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9990
      this.noise2d = function(x, y) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9991
        var X = Math.floor(x)&255, Y = Math.floor(y)&255;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9992
        x -= Math.floor(x); y -= Math.floor(y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9993
        var fx = (3-2*x)*x*x, fy = (3-2*y)*y*y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9994
        var p0 = perm[X]+Y, p1 = perm[X + 1] + Y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9995
        return lerp(fy,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9996
          lerp(fx, grad2d(perm[p0], x, y), grad2d(perm[p1], x-1, y)),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9997
          lerp(fx, grad2d(perm[p0 + 1], x, y-1), grad2d(perm[p1 + 1], x-1, y-1)));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9998
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
  9999
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10000
      this.noise1d = function(x) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10001
        var X = Math.floor(x)&255;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10002
        x -= Math.floor(x);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10003
        var fx = (3-2*x)*x*x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10004
        return lerp(fx, grad1d(perm[X], x), grad1d(perm[X+1], x-1));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10005
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10006
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10007
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10008
    // processing defaults
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10009
    var noiseProfile = { generator: undef, octaves: 4, fallout: 0.5, seed: undef};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10010
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10011
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10012
    * Returns the Perlin noise value at specified coordinates. Perlin noise is a random sequence
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10013
    * generator producing a more natural ordered, harmonic succession of numbers compared to the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10014
    * standard random() function. It was invented by Ken Perlin in the 1980s and been used since
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10015
    * in graphical applications to produce procedural textures, natural motion, shapes, terrains etc.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10016
    * The main difference to the random() function is that Perlin noise is defined in an infinite
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10017
    * n-dimensional space where each pair of coordinates corresponds to a fixed semi-random value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10018
    * (fixed only for the lifespan of the program). The resulting value will always be between 0.0
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10019
    * and 1.0. Processing can compute 1D, 2D and 3D noise, depending on the number of coordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10020
    * given. The noise value can be animated by moving through the noise space as demonstrated in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10021
    * the example above. The 2nd and 3rd dimension can also be interpreted as time.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10022
    * The actual noise is structured similar to an audio signal, in respect to the function's use
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10023
    * of frequencies. Similar to the concept of harmonics in physics, perlin noise is computed over
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10024
    * several octaves which are added together for the final result.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10025
    * Another way to adjust the character of the resulting sequence is the scale of the input
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10026
    * coordinates. As the function works within an infinite space the value of the coordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10027
    * doesn't matter as such, only the distance between successive coordinates does (eg. when using
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10028
    * noise() within a loop). As a general rule the smaller the difference between coordinates, the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10029
    * smoother the resulting noise sequence will be. Steps of 0.005-0.03 work best for most applications,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10030
    * but this will differ depending on use.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10031
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10032
    * @param {float} x          x coordinate in noise space
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10033
    * @param {float} y          y coordinate in noise space
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10034
    * @param {float} z          z coordinate in noise space
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10035
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10036
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10037
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10038
    * @see random
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10039
    * @see noiseDetail
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10040
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10041
    p.noise = function(x, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10042
      if(noiseProfile.generator === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10043
        // caching
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10044
        noiseProfile.generator = new PerlinNoise(noiseProfile.seed);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10045
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10046
      var generator = noiseProfile.generator;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10047
      var effect = 1, k = 1, sum = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10048
      for(var i=0; i<noiseProfile.octaves; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10049
        effect *= noiseProfile.fallout;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10050
        switch (arguments.length) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10051
        case 1:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10052
          sum += effect * (1 + generator.noise1d(k*x))/2; break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10053
        case 2:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10054
          sum += effect * (1 + generator.noise2d(k*x, k*y))/2; break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10055
        case 3:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10056
          sum += effect * (1 + generator.noise3d(k*x, k*y, k*z))/2; break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10057
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10058
        k *= 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10059
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10060
      return sum;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10061
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10062
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10063
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10064
    * Adjusts the character and level of detail produced by the Perlin noise function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10065
    * Similar to harmonics in physics, noise is computed over several octaves. Lower octaves
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10066
    * contribute more to the output signal and as such define the overal intensity of the noise,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10067
    * whereas higher octaves create finer grained details in the noise sequence. By default,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10068
    * noise is computed over 4 octaves with each octave contributing exactly half than its
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10069
    * predecessor, starting at 50% strength for the 1st octave. This falloff amount can be
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10070
    * changed by adding an additional function parameter. Eg. a falloff factor of 0.75 means
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10071
    * each octave will now have 75% impact (25% less) of the previous lower octave. Any value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10072
    * between 0.0 and 1.0 is valid, however note that values greater than 0.5 might result in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10073
    * greater than 1.0 values returned by noise(). By changing these parameters, the signal
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10074
    * created by the noise() function can be adapted to fit very specific needs and characteristics.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10075
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10076
    * @param {int} octaves          number of octaves to be used by the noise() function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10077
    * @param {float} falloff        falloff factor for each octave
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10078
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10079
    * @see noise
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10080
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10081
    p.noiseDetail = function(octaves, fallout) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10082
      noiseProfile.octaves = octaves;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10083
      if(fallout !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10084
        noiseProfile.fallout = fallout;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10085
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10086
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10087
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10088
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10089
    * Sets the seed value for noise(). By default, noise() produces different results each
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10090
    * time the program is run. Set the value parameter to a constant to return the same
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10091
    * pseudo-random numbers each time the software is run.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10092
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10093
    * @param {int} seed         int
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10094
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10095
    * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10096
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10097
    * @see random
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10098
    * @see radomSeed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10099
    * @see noise
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10100
    * @see noiseDetail
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10101
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10102
    p.noiseSeed = function(seed) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10103
      noiseProfile.seed = seed;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10104
      noiseProfile.generator = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10105
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10106
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10107
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10108
    * Defines the dimension of the display window in units of pixels. The size() function must
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10109
    * be the first line in setup(). If size() is not called, the default size of the window is
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10110
    * 100x100 pixels. The system variables width and height are set by the parameters passed to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10111
    * the size() function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10112
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10113
    * @param {int} aWidth     width of the display window in units of pixels
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10114
    * @param {int} aHeight    height of the display window in units of pixels
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10115
    * @param {MODE} aMode     Either P2D, P3D, JAVA2D, or OPENGL
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10116
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10117
    * @see createGraphics
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10118
    * @see screen
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10119
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10120
    DrawingShared.prototype.size = function(aWidth, aHeight, aMode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10121
      if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10122
        p.stroke(0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10123
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10124
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10125
      if (doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10126
        p.fill(255);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10127
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10128
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10129
      // The default 2d context has already been created in the p.init() stage if
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10130
      // a 3d context was not specified. This is so that a 2d context will be
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10131
      // available if size() was not called.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10132
      var savedProperties = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10133
        fillStyle: curContext.fillStyle,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10134
        strokeStyle: curContext.strokeStyle,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10135
        lineCap: curContext.lineCap,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10136
        lineJoin: curContext.lineJoin
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10137
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10138
      // remove the style width and height properties to ensure that the canvas gets set to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10139
      // aWidth and aHeight coming in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10140
      if (curElement.style.length > 0 ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10141
        curElement.style.removeProperty("width");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10142
        curElement.style.removeProperty("height");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10143
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10144
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10145
      curElement.width = p.width = aWidth || 100;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10146
      curElement.height = p.height = aHeight || 100;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10147
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10148
      for (var prop in savedProperties) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10149
        if (savedProperties.hasOwnProperty(prop)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10150
          curContext[prop] = savedProperties[prop];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10151
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10152
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10153
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10154
      // make sure to set the default font the first time round.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10155
      p.textFont(curTextFont);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10156
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10157
      // Set the background to whatever it was called last as if background() was called before size()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10158
      // If background() hasn't been called before, set background() to a light gray
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10159
      p.background();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10160
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10161
      // set 5% for pixels to cache (or 1000)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10162
      maxPixelsCached = Math.max(1000, aWidth * aHeight * 0.05);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10163
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10164
      // Externalize the context
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10165
      p.externals.context = curContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10166
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10167
      for (var i = 0; i < PConstants.SINCOS_LENGTH; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10168
        sinLUT[i] = p.sin(i * (PConstants.PI / 180) * 0.5);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10169
        cosLUT[i] = p.cos(i * (PConstants.PI / 180) * 0.5);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10170
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10171
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10172
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10173
    Drawing2D.prototype.size = function(aWidth, aHeight, aMode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10174
      if (curContext === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10175
        // size() was called without p.init() default context, i.e. p.createGraphics()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10176
        curContext = curElement.getContext("2d");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10177
        userMatrixStack = new PMatrixStack();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10178
        userReverseMatrixStack = new PMatrixStack();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10179
        modelView = new PMatrix2D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10180
        modelViewInv = new PMatrix2D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10181
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10182
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10183
      DrawingShared.prototype.size.apply(this, arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10184
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10185
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10186
    Drawing3D.prototype.size = (function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10187
      var size3DCalled = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10188
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10189
      return function size(aWidth, aHeight, aMode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10190
        if (size3DCalled) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10191
          throw "Multiple calls to size() for 3D renders are not allowed.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10192
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10193
        size3DCalled = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10194
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10195
        function getGLContext(canvas) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10196
          var ctxNames = ['experimental-webgl', 'webgl', 'webkit-3d'],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10197
              gl;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10198
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10199
          for (var i=0, l=ctxNames.length; i<l; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10200
            gl = canvas.getContext(ctxNames[i], {antialias: false});
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10201
            if (gl) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10202
              break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10203
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10204
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10205
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10206
          return gl;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10207
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10208
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10209
        // get the 3D rendering context
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10210
        try {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10211
          // If the HTML <canvas> dimensions differ from the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10212
          // dimensions specified in the size() call in the sketch, for
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10213
          // 3D sketches, browsers will either not render or render the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10214
          // scene incorrectly. To fix this, we need to adjust the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10215
          // width and height attributes of the canvas.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10216
          curElement.width = p.width = aWidth || 100;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10217
          curElement.height = p.height = aHeight || 100;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10218
          curContext = getGLContext(curElement);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10219
          canTex = curContext.createTexture(); // texture
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10220
          textTex = curContext.createTexture(); // texture
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10221
        } catch(e_size) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10222
          Processing.debug(e_size);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10223
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10224
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10225
        if (!curContext) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10226
          throw "WebGL context is not supported on this browser.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10227
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10228
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10229
        // Set defaults
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10230
        curContext.viewport(0, 0, curElement.width, curElement.height);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10231
        curContext.enable(curContext.DEPTH_TEST);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10232
        curContext.enable(curContext.BLEND);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10233
        curContext.blendFunc(curContext.SRC_ALPHA, curContext.ONE_MINUS_SRC_ALPHA);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10234
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10235
        // Create the program objects to render 2D (points, lines) and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10236
        // 3D (spheres, boxes) shapes. Because 2D shapes are not lit,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10237
        // lighting calculations could be ommitted from that program object.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10238
        programObject2D = createProgramObject(curContext, vertexShaderSource2D, fragmentShaderSource2D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10239
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10240
        programObjectUnlitShape = createProgramObject(curContext, vShaderSrcUnlitShape, fShaderSrcUnlitShape);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10241
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10242
        // Set the default point and line width for the 2D and unlit shapes.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10243
        p.strokeWeight(1.0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10244
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10245
        // Now that the programs have been compiled, we can set the default
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10246
        // states for the lights.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10247
        programObject3D = createProgramObject(curContext, vertexShaderSource3D, fragmentShaderSource3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10248
        curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10249
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10250
        // assume we aren't using textures by default
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10251
        uniformi("usingTexture3d", programObject3D, "usingTexture", usingTexture);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10252
        // assume that we arn't tinting by default
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10253
        p.lightFalloff(1, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10254
        p.shininess(1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10255
        p.ambient(255, 255, 255);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10256
        p.specular(0, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10257
        p.emissive(0, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10258
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10259
        // Create buffers for 3D primitives
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10260
        boxBuffer = curContext.createBuffer();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10261
        curContext.bindBuffer(curContext.ARRAY_BUFFER, boxBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10262
        curContext.bufferData(curContext.ARRAY_BUFFER, boxVerts, curContext.STATIC_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10263
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10264
        boxNormBuffer = curContext.createBuffer();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10265
        curContext.bindBuffer(curContext.ARRAY_BUFFER, boxNormBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10266
        curContext.bufferData(curContext.ARRAY_BUFFER, boxNorms, curContext.STATIC_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10267
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10268
        boxOutlineBuffer = curContext.createBuffer();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10269
        curContext.bindBuffer(curContext.ARRAY_BUFFER, boxOutlineBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10270
        curContext.bufferData(curContext.ARRAY_BUFFER, boxOutlineVerts, curContext.STATIC_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10271
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10272
        // used to draw the rectangle and the outline
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10273
        rectBuffer = curContext.createBuffer();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10274
        curContext.bindBuffer(curContext.ARRAY_BUFFER, rectBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10275
        curContext.bufferData(curContext.ARRAY_BUFFER, rectVerts, curContext.STATIC_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10276
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10277
        rectNormBuffer = curContext.createBuffer();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10278
        curContext.bindBuffer(curContext.ARRAY_BUFFER, rectNormBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10279
        curContext.bufferData(curContext.ARRAY_BUFFER, rectNorms, curContext.STATIC_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10280
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10281
        // The sphere vertices are specified dynamically since the user
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10282
        // can change the level of detail. Everytime the user does that
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10283
        // using sphereDetail(), the new vertices are calculated.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10284
        sphereBuffer = curContext.createBuffer();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10285
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10286
        lineBuffer = curContext.createBuffer();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10287
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10288
        // Shape buffers
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10289
        fillBuffer = curContext.createBuffer();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10290
        fillColorBuffer = curContext.createBuffer();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10291
        strokeColorBuffer = curContext.createBuffer();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10292
        shapeTexVBO = curContext.createBuffer();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10293
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10294
        pointBuffer = curContext.createBuffer();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10295
        curContext.bindBuffer(curContext.ARRAY_BUFFER, pointBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10296
        curContext.bufferData(curContext.ARRAY_BUFFER, new Float32Array([0, 0, 0]), curContext.STATIC_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10297
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10298
        textBuffer = curContext.createBuffer();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10299
        curContext.bindBuffer(curContext.ARRAY_BUFFER, textBuffer );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10300
        curContext.bufferData(curContext.ARRAY_BUFFER, new Float32Array([1,1,0,-1,1,0,-1,-1,0,1,-1,0]), curContext.STATIC_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10301
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10302
        textureBuffer = curContext.createBuffer();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10303
        curContext.bindBuffer(curContext.ARRAY_BUFFER, textureBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10304
        curContext.bufferData(curContext.ARRAY_BUFFER, new Float32Array([0,0,1,0,1,1,0,1]), curContext.STATIC_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10305
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10306
        indexBuffer = curContext.createBuffer();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10307
        curContext.bindBuffer(curContext.ELEMENT_ARRAY_BUFFER, indexBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10308
        curContext.bufferData(curContext.ELEMENT_ARRAY_BUFFER, new Uint16Array([0,1,2,2,3,0]), curContext.STATIC_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10309
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10310
        cam = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10311
        cameraInv = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10312
        modelView = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10313
        modelViewInv = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10314
        projection = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10315
        p.camera();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10316
        p.perspective();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10317
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10318
        userMatrixStack = new PMatrixStack();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10319
        userReverseMatrixStack = new PMatrixStack();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10320
        // used by both curve and bezier, so just init here
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10321
        curveBasisMatrix = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10322
        curveToBezierMatrix = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10323
        curveDrawMatrix = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10324
        bezierDrawMatrix = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10325
        bezierBasisInverse = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10326
        bezierBasisMatrix = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10327
        bezierBasisMatrix.set(-1, 3, -3, 1, 3, -6, 3, 0, -3, 3, 0, 0, 1, 0, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10328
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10329
        DrawingShared.prototype.size.apply(this, arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10330
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10331
    }());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10332
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10333
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10334
    // Lights
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10335
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10336
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10337
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10338
     * Adds an ambient light. Ambient light doesn't come from a specific direction,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10339
     * the rays have light have bounced around so much that objects are evenly lit
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10340
     * from all sides. Ambient lights are almost always used in combination with
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10341
     * other types of lights. Lights need to be included in the <b>draw()</b> to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10342
     * remain persistent in a looping program. Placing them in the <b>setup()</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10343
     * of a looping program will cause them to only have an effect the first time
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10344
     * through the loop. The effect of the parameters is determined by the current
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10345
     * color mode.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10346
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10347
     * @param {int | float} r red or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10348
     * @param {int | float} g green or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10349
     * @param {int | float} b blue or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10350
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10351
     * @param {int | float} x x position of light (used for falloff)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10352
     * @param {int | float} y y position of light (used for falloff)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10353
     * @param {int | float} z z position of light (used for falloff)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10354
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10355
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10356
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10357
     * @see lights
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10358
     * @see directionalLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10359
     * @see pointLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10360
     * @see spotLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10361
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10362
    Drawing2D.prototype.ambientLight = DrawingShared.prototype.a3DOnlyFunction;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10363
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10364
    Drawing3D.prototype.ambientLight = function(r, g, b, x, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10365
      if (lightCount === PConstants.MAX_LIGHTS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10366
        throw "can only create " + PConstants.MAX_LIGHTS + " lights";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10367
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10368
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10369
      var pos = new PVector(x, y, z);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10370
      var view = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10371
      view.scale(1, -1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10372
      view.apply(modelView.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10373
      view.mult(pos, pos);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10374
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10375
      // Instead of calling p.color, we do the calculations ourselves to 
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10376
      // reduce property lookups.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10377
      var col = color$4(r, g, b, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10378
      var normalizedCol = [ ((col & PConstants.RED_MASK) >>> 16) / 255,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10379
                            ((col & PConstants.GREEN_MASK) >>> 8) / 255,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10380
                             (col & PConstants.BLUE_MASK) / 255 ];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10381
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10382
      curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10383
      uniformf("lights.color.3d." + lightCount, programObject3D, "lights" + lightCount + ".color", normalizedCol);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10384
      uniformf("lights.position.3d." + lightCount, programObject3D, "lights" + lightCount + ".position", pos.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10385
      uniformi("lights.type.3d." + lightCount, programObject3D, "lights" + lightCount + ".type", 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10386
      uniformi("lightCount3d", programObject3D, "lightCount", ++lightCount);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10387
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10388
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10389
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10390
     * Adds a directional light. Directional light comes from one direction and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10391
     * is stronger when hitting a surface squarely and weaker if it hits at a
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10392
     * gentle angle. After hitting a surface, a directional lights scatters in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10393
     * all directions. Lights need to be included in the <b>draw()</b> to remain
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10394
     * persistent in a looping program. Placing them in the <b>setup()</b> of a
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10395
     * looping program will cause them to only have an effect the first time
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10396
     * through the loop. The affect of the <br>r</b>, <br>g</b>, and <br>b</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10397
     * parameters is determined by the current color mode. The <b>nx</b>,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10398
     * <b>ny</b>, and <b>nz</b> parameters specify the direction the light is
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10399
     * facing. For example, setting <b>ny</b> to -1 will cause the geometry to be
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10400
     * lit from below (the light is facing directly upward).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10401
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10402
     * @param {int | float} r red or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10403
     * @param {int | float} g green or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10404
     * @param {int | float} b blue or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10405
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10406
     * @param {int | float} nx direction along the x axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10407
     * @param {int | float} ny direction along the y axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10408
     * @param {int | float} nz direction along the z axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10409
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10410
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10411
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10412
     * @see lights
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10413
     * @see ambientLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10414
     * @see pointLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10415
     * @see spotLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10416
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10417
    Drawing2D.prototype.directionalLight = DrawingShared.prototype.a3DOnlyFunction;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10418
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10419
    Drawing3D.prototype.directionalLight = function(r, g, b, nx, ny, nz) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10420
      if (lightCount === PConstants.MAX_LIGHTS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10421
        throw "can only create " + PConstants.MAX_LIGHTS + " lights";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10422
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10423
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10424
      curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10425
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10426
      var mvm = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10427
      mvm.scale(1, -1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10428
      mvm.apply(modelView.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10429
      mvm = mvm.array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10430
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10431
      // We need to multiply the direction by the model view matrix, but
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10432
      // the mult function checks the w component of the vector, if it isn't
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10433
      // present, it uses 1, so we manually multiply.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10434
      var dir = [
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10435
        mvm[0] * nx + mvm[4] * ny + mvm[8] * nz,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10436
        mvm[1] * nx + mvm[5] * ny + mvm[9] * nz,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10437
        mvm[2] * nx + mvm[6] * ny + mvm[10] * nz
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10438
      ];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10439
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10440
      // Instead of calling p.color, we do the calculations ourselves to 
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10441
      // reduce property lookups.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10442
      var col = color$4(r, g, b, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10443
      var normalizedCol = [ ((col & PConstants.RED_MASK) >>> 16) / 255,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10444
                            ((col & PConstants.GREEN_MASK) >>> 8) / 255,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10445
                             (col & PConstants.BLUE_MASK) / 255 ];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10446
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10447
      uniformf("lights.color.3d." + lightCount, programObject3D, "lights" + lightCount + ".color", normalizedCol);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10448
      uniformf("lights.position.3d." + lightCount, programObject3D, "lights" + lightCount + ".position", dir);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10449
      uniformi("lights.type.3d." + lightCount, programObject3D, "lights" + lightCount + ".type", 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10450
      uniformi("lightCount3d", programObject3D, "lightCount", ++lightCount);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10451
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10452
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10453
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10454
     * Sets the falloff rates for point lights, spot lights, and ambient lights.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10455
     * The parameters are used to determine the falloff with the following equation:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10456
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10457
     * d = distance from light position to vertex position
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10458
     * falloff = 1 / (CONSTANT + d * LINEAR + (d*d) * QUADRATIC)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10459
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10460
     * Like <b>fill()</b>, it affects only the elements which are created after it in the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10461
     * code. The default value if <b>LightFalloff(1.0, 0.0, 0.0)</b>. Thinking about an
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10462
     * ambient light with a falloff can be tricky. It is used, for example, if you
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10463
     * wanted a region of your scene to be lit ambiently one color and another region
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10464
     * to be lit ambiently by another color, you would use an ambient light with location
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10465
     * and falloff. You can think of it as a point light that doesn't care which direction
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10466
     * a surface is facing.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10467
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10468
     * @param {int | float} constant constant value for determining falloff
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10469
     * @param {int | float} linear linear value for determining falloff
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10470
     * @param {int | float} quadratic quadratic value for determining falloff
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10471
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10472
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10473
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10474
     * @see lights
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10475
     * @see ambientLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10476
     * @see pointLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10477
     * @see spotLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10478
     * @see lightSpecular
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10479
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10480
    Drawing2D.prototype.lightFalloff = DrawingShared.prototype.a3DOnlyFunction;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10481
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10482
    Drawing3D.prototype.lightFalloff = function(constant, linear, quadratic) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10483
      curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10484
      uniformf("falloff3d", programObject3D, "falloff", [constant, linear, quadratic]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10485
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10486
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10487
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10488
     * Sets the specular color for lights. Like <b>fill()</b>, it affects only the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10489
     * elements which are created after it in the code. Specular refers to light
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10490
     * which bounces off a surface in a perferred direction (rather than bouncing
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10491
     * in all directions like a diffuse light) and is used for creating highlights.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10492
     * The specular quality of a light interacts with the specular material qualities
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10493
     * set through the <b>specular()</b> and <b>shininess()</b> functions.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10494
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10495
     * @param {int | float} r red or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10496
     * @param {int | float} g green or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10497
     * @param {int | float} b blue or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10498
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10499
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10500
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10501
     * @see lights
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10502
     * @see ambientLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10503
     * @see pointLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10504
     * @see spotLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10505
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10506
    Drawing2D.prototype.lightSpecular = DrawingShared.prototype.a3DOnlyFunction;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10507
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10508
    Drawing3D.prototype.lightSpecular = function(r, g, b) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10509
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10510
      // Instead of calling p.color, we do the calculations ourselves to 
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10511
      // reduce property lookups.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10512
      var col = color$4(r, g, b, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10513
      var normalizedCol = [ ((col & PConstants.RED_MASK) >>> 16) / 255,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10514
                            ((col & PConstants.GREEN_MASK) >>> 8) / 255,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10515
                             (col & PConstants.BLUE_MASK) / 255 ];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10516
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10517
      curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10518
      uniformf("specular3d", programObject3D, "specular", normalizedCol);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10519
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10520
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10521
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10522
     * Sets the default ambient light, directional light, falloff, and specular
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10523
     * values. The defaults are ambientLight(128, 128, 128) and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10524
     * directionalLight(128, 128, 128, 0, 0, -1), lightFalloff(1, 0, 0), and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10525
     * lightSpecular(0, 0, 0). Lights need to be included in the draw() to remain
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10526
     * persistent in a looping program. Placing them in the setup() of a looping
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10527
     * program will cause them to only have an effect the first time through the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10528
     * loop.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10529
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10530
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10531
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10532
     * @see ambientLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10533
     * @see directionalLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10534
     * @see pointLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10535
     * @see spotLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10536
     * @see noLights
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10537
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10538
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10539
    p.lights = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10540
      p.ambientLight(128, 128, 128);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10541
      p.directionalLight(128, 128, 128, 0, 0, -1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10542
      p.lightFalloff(1, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10543
      p.lightSpecular(0, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10544
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10545
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10546
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10547
     * Adds a point light. Lights need to be included in the <b>draw()</b> to remain
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10548
     * persistent in a looping program. Placing them in the <b>setup()</b> of a
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10549
     * looping program will cause them to only have an effect the first time through
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10550
     * the loop. The affect of the <b>r</b>, <b>g</b>, and <b>b</b> parameters
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10551
     * is determined by the current color mode. The <b>x</b>, <b>y</b>, and <b>z</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10552
     * parameters set the position of the light.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10553
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10554
     * @param {int | float} r red or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10555
     * @param {int | float} g green or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10556
     * @param {int | float} b blue or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10557
     * @param {int | float} x x coordinate of the light
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10558
     * @param {int | float} y y coordinate of the light
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10559
     * @param {int | float} z z coordinate of the light
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10560
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10561
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10562
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10563
     * @see lights
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10564
     * @see directionalLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10565
     * @see ambientLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10566
     * @see spotLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10567
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10568
    Drawing2D.prototype.pointLight = DrawingShared.prototype.a3DOnlyFunction;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10569
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10570
    Drawing3D.prototype.pointLight = function(r, g, b, x, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10571
      if (lightCount === PConstants.MAX_LIGHTS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10572
        throw "can only create " + PConstants.MAX_LIGHTS + " lights";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10573
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10574
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10575
      // Place the point in view space once instead of once per vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10576
      // in the shader.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10577
      var pos = new PVector(x, y, z);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10578
      var view = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10579
      view.scale(1, -1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10580
      view.apply(modelView.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10581
      view.mult(pos, pos);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10582
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10583
      // Instead of calling p.color, we do the calculations ourselves to 
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10584
      // reduce property lookups.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10585
      var col = color$4(r, g, b, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10586
      var normalizedCol = [ ((col & PConstants.RED_MASK) >>> 16) / 255,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10587
                            ((col & PConstants.GREEN_MASK) >>> 8) / 255,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10588
                             (col & PConstants.BLUE_MASK) / 255 ];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10589
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10590
      curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10591
      uniformf("lights.color.3d." + lightCount, programObject3D, "lights" + lightCount + ".color", normalizedCol);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10592
      uniformf("lights.position.3d." + lightCount, programObject3D, "lights" + lightCount + ".position", pos.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10593
      uniformi("lights.type.3d." + lightCount, programObject3D, "lights" + lightCount + ".type", 2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10594
      uniformi("lightCount3d", programObject3D, "lightCount", ++lightCount);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10595
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10596
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10597
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10598
     * Disable all lighting. Lighting is turned off by default and enabled with
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10599
     * the lights() method. This function can be used to disable lighting so
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10600
     * that 2D geometry (which does not require lighting) can be drawn after a
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10601
     * set of lighted 3D geometry.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10602
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10603
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10604
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10605
     * @see lights
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10606
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10607
    Drawing2D.prototype.noLights = DrawingShared.prototype.a3DOnlyFunction;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10608
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10609
    Drawing3D.prototype.noLights = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10610
      lightCount = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10611
      curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10612
      uniformi("lightCount3d", programObject3D, "lightCount", lightCount);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10613
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10614
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10615
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10616
     * Adds a spot light. Lights need to be included in the <b>draw()</b> to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10617
     * remain persistent in a looping program. Placing them in the <b>setup()</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10618
     * of a looping program will cause them to only have an effect the first time
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10619
     * through the loop. The affect of the <b>r</b>, <b>g</b>, and <b>b</b> parameters
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10620
     * is determined by the current color mode. The <b>x</b>, <b>y</b>, and <b>z</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10621
     * parameters specify the position of the light and <b>nx</b>, <b>ny</b>, <b>nz</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10622
     * specify the direction or light. The angle parameter affects <b>angle</b> of the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10623
     * spotlight cone.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10624
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10625
     * @param {int | float} r red or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10626
     * @param {int | float} g green or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10627
     * @param {int | float} b blue or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10628
     * @param {int | float} x coordinate of the light
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10629
     * @param {int | float} y coordinate of the light
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10630
     * @param {int | float} z coordinate of the light
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10631
     * @param {int | float} nx direction along the x axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10632
     * @param {int | float} ny direction along the y axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10633
     * @param {int | float} nz direction along the z axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10634
     * @param {float} angle angle of the spotlight cone
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10635
     * @param {float} concentration exponent determining the center bias of the cone
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10636
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10637
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10638
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10639
     * @see lights
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10640
     * @see directionalLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10641
     * @see ambientLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10642
     * @see pointLight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10643
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10644
    Drawing2D.prototype.spotLight = DrawingShared.prototype.a3DOnlyFunction;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10645
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10646
    Drawing3D.prototype.spotLight = function(r, g, b, x, y, z, nx, ny, nz, angle, concentration) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10647
      if (lightCount === PConstants.MAX_LIGHTS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10648
        throw "can only create " + PConstants.MAX_LIGHTS + " lights";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10649
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10650
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10651
      curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10652
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10653
      // multiply the position and direction by the model view matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10654
      // once per object rather than once per vertex.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10655
      var pos = new PVector(x, y, z);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10656
      var mvm = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10657
      mvm.scale(1, -1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10658
      mvm.apply(modelView.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10659
      mvm.mult(pos, pos);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10660
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10661
      // Convert to array since we need to directly access the elements.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10662
      mvm = mvm.array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10663
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10664
      // We need to multiply the direction by the model view matrix, but
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10665
      // the mult function checks the w component of the vector, if it isn't
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10666
      // present, it uses 1, so we use a very small value as a work around.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10667
      var dir = [
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10668
          mvm[0] * nx + mvm[4] * ny + mvm[8] * nz,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10669
          mvm[1] * nx + mvm[5] * ny + mvm[9] * nz,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10670
          mvm[2] * nx + mvm[6] * ny + mvm[10] * nz
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10671
      ];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10672
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10673
      // Instead of calling p.color, we do the calculations ourselves to 
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10674
      // reduce property lookups.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10675
      var col = color$4(r, g, b, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10676
      var normalizedCol = [ ((col & PConstants.RED_MASK) >>> 16) / 255,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10677
                            ((col & PConstants.GREEN_MASK) >>> 8) / 255,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10678
                             (col & PConstants.BLUE_MASK) / 255 ];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10679
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10680
      uniformf("lights.color.3d." + lightCount, programObject3D, "lights" + lightCount + ".color", normalizedCol);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10681
      uniformf("lights.position.3d." + lightCount, programObject3D, "lights" + lightCount + ".position", pos.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10682
      uniformf("lights.direction.3d." + lightCount, programObject3D, "lights" + lightCount + ".direction", dir);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10683
      uniformf("lights.concentration.3d." + lightCount, programObject3D, "lights" + lightCount + ".concentration", concentration);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10684
      uniformf("lights.angle.3d." + lightCount, programObject3D, "lights" + lightCount + ".angle", angle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10685
      uniformi("lights.type.3d." + lightCount, programObject3D, "lights" + lightCount + ".type", 3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10686
      uniformi("lightCount3d", programObject3D, "lightCount", ++lightCount);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10687
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10688
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10689
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10690
    // Camera functions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10691
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10692
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10693
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10694
     * The <b>beginCamera()</b> and <b>endCamera()</b> functions enable advanced customization of the camera space.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10695
     * The functions are useful if you want to more control over camera movement, however for most users, the <b>camera()</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10696
     * function will be sufficient.<br /><br />The camera functions will replace any transformations (such as <b>rotate()</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10697
     * or <b>translate()</b>) that occur before them in <b>draw()</b>, but they will not automatically replace the camera
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10698
     * transform itself. For this reason, camera functions should be placed at the beginning of <b>draw()</b> (so that
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10699
     * transformations happen afterwards), and the <b>camera()</b> function can be used after <b>beginCamera()</b> if
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10700
     * you want to reset the camera before applying transformations.<br /><br />This function sets the matrix mode to the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10701
     * camera matrix so calls such as <b>translate()</b>, <b>rotate()</b>, applyMatrix() and resetMatrix() affect the camera.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10702
     * <b>beginCamera()</b> should always be used with a following <b>endCamera()</b> and pairs of <b>beginCamera()</b> and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10703
     * <b>endCamera()</b> cannot be nested.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10704
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10705
     * @see camera
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10706
     * @see endCamera
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10707
     * @see applyMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10708
     * @see resetMatrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10709
     * @see translate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10710
     * @see rotate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10711
     * @see scale
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10712
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10713
    Drawing2D.prototype.beginCamera = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10714
      throw ("beginCamera() is not available in 2D mode");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10715
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10716
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10717
    Drawing3D.prototype.beginCamera = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10718
      if (manipulatingCamera) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10719
        throw ("You cannot call beginCamera() again before calling endCamera()");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10720
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10721
      manipulatingCamera = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10722
      modelView = cameraInv;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10723
      modelViewInv = cam;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10724
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10725
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10726
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10727
     * The <b>beginCamera()</b> and <b>endCamera()</b> functions enable advanced customization of the camera space.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10728
     * Please see the reference for <b>beginCamera()</b> for a description of how the functions are used.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10729
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10730
     * @see beginCamera
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10731
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10732
    Drawing2D.prototype.endCamera = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10733
      throw ("endCamera() is not available in 2D mode");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10734
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10735
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10736
    Drawing3D.prototype.endCamera = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10737
      if (!manipulatingCamera) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10738
        throw ("You cannot call endCamera() before calling beginCamera()");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10739
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10740
      modelView.set(cam);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10741
      modelViewInv.set(cameraInv);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10742
      manipulatingCamera = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10743
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10744
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10745
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10746
     * Sets the position of the camera through setting the eye position, the center of the scene, and which axis is facing
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10747
     * upward. Moving the eye position and the direction it is pointing (the center of the scene) allows the images to be
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10748
     * seen from different angles. The version without any parameters sets the camera to the default position, pointing to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10749
     * the center of the display window with the Y axis as up. The default values are camera(width/2.0, height/2.0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10750
     * (height/2.0) / tan(PI*60.0 / 360.0), width/2.0, height/2.0, 0, 0, 1, 0). This function is similar to gluLookAt()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10751
     * in OpenGL, but it first clears the current camera settings.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10752
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10753
     * @param {float} eyeX    x-coordinate for the eye
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10754
     * @param {float} eyeY    y-coordinate for the eye
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10755
     * @param {float} eyeZ    z-coordinate for the eye
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10756
     * @param {float} centerX x-coordinate for the center of the scene
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10757
     * @param {float} centerY y-coordinate for the center of the scene
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10758
     * @param {float} centerZ z-coordinate for the center of the scene
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10759
     * @param {float} upX     usually 0.0, 1.0, -1.0
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10760
     * @param {float} upY     usually 0.0, 1.0, -1.0
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10761
     * @param {float} upZ     usually 0.0, 1.0, -1.0
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10762
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10763
     * @see beginCamera
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10764
     * @see endCamera
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10765
     * @see frustum
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10766
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10767
    p.camera = function(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10768
      if (eyeX === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10769
        // Workaround if createGraphics is used. 
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10770
        cameraX = p.width / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10771
        cameraY = p.height / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10772
        cameraZ = cameraY / Math.tan(cameraFOV / 2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10773
        eyeX = cameraX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10774
        eyeY = cameraY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10775
        eyeZ = cameraZ;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10776
        centerX = cameraX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10777
        centerY = cameraY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10778
        centerZ = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10779
        upX = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10780
        upY = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10781
        upZ = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10782
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10783
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10784
      var z = new PVector(eyeX - centerX, eyeY - centerY, eyeZ - centerZ);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10785
      var y = new PVector(upX, upY, upZ);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10786
      z.normalize();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10787
      var x = PVector.cross(y, z);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10788
      y = PVector.cross(z, x);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10789
      x.normalize();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10790
      y.normalize();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10791
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10792
      var xX = x.x,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10793
          xY = x.y,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10794
          xZ = x.z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10795
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10796
      var yX = y.x,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10797
          yY = y.y,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10798
          yZ = y.z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10799
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10800
      var zX = z.x,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10801
          zY = z.y,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10802
          zZ = z.z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10803
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10804
      cam.set(xX, xY, xZ, 0, yX, yY, yZ, 0, zX, zY, zZ, 0, 0, 0, 0, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10805
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10806
      cam.translate(-eyeX, -eyeY, -eyeZ);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10807
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10808
      cameraInv.reset();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10809
      cameraInv.invApply(xX, xY, xZ, 0, yX, yY, yZ, 0, zX, zY, zZ, 0, 0, 0, 0, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10810
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10811
      cameraInv.translate(eyeX, eyeY, eyeZ);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10812
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10813
      modelView.set(cam);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10814
      modelViewInv.set(cameraInv);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10815
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10816
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10817
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10818
     * Sets a perspective projection applying foreshortening, making distant objects appear smaller than closer ones. The
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10819
     * parameters define a viewing volume with the shape of truncated pyramid. Objects near to the front of the volume appear
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10820
     * their actual size, while farther objects appear smaller. This projection simulates the perspective of the world more
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10821
     * accurately than orthographic projection. The version of perspective without parameters sets the default perspective and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10822
     * the version with four parameters allows the programmer to set the area precisely. The default values are:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10823
     * perspective(PI/3.0, width/height, cameraZ/10.0, cameraZ*10.0) where cameraZ is ((height/2.0) / tan(PI*60.0/360.0));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10824
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10825
     * @param {float} fov     field-of-view angle (in radians) for vertical direction
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10826
     * @param {float} aspect  ratio of width to height
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10827
     * @param {float} zNear   z-position of nearest clipping plane
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10828
     * @param {float} zFar    z-positions of farthest clipping plane
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10829
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10830
    p.perspective = function(fov, aspect, near, far) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10831
      if (arguments.length === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10832
        //in case canvas is resized
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10833
        cameraY = curElement.height / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10834
        cameraZ = cameraY / Math.tan(cameraFOV / 2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10835
        cameraNear = cameraZ / 10;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10836
        cameraFar = cameraZ * 10;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10837
        cameraAspect = p.width / p.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10838
        fov = cameraFOV;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10839
        aspect = cameraAspect;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10840
        near = cameraNear;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10841
        far = cameraFar;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10842
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10843
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10844
      var yMax, yMin, xMax, xMin;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10845
      yMax = near * Math.tan(fov / 2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10846
      yMin = -yMax;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10847
      xMax = yMax * aspect;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10848
      xMin = yMin * aspect;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10849
      p.frustum(xMin, xMax, yMin, yMax, near, far);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10850
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10851
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10852
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10853
     * Sets a perspective matrix defined through the parameters. Works like glFrustum, except it wipes out the current
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10854
     * perspective matrix rather than muliplying itself with it.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10855
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10856
     * @param {float} left   left coordinate of the clipping plane
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10857
     * @param {float} right  right coordinate of the clipping plane
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10858
     * @param {float} bottom bottom coordinate of the clipping plane
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10859
     * @param {float} top    top coordinate of the clipping plane
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10860
     * @param {float} near   near coordinate of the clipping plane
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10861
     * @param {float} far    far coordinate of the clipping plane
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10862
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10863
     * @see beginCamera
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10864
     * @see camera
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10865
     * @see endCamera
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10866
     * @see perspective
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10867
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10868
    Drawing2D.prototype.frustum = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10869
      throw("Processing.js: frustum() is not supported in 2D mode");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10870
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10871
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10872
    Drawing3D.prototype.frustum = function(left, right, bottom, top, near, far) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10873
      frustumMode = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10874
      projection = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10875
      projection.set((2 * near) / (right - left), 0, (right + left) / (right - left),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10876
                     0, 0, (2 * near) / (top - bottom), (top + bottom) / (top - bottom),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10877
                     0, 0, 0, -(far + near) / (far - near), -(2 * far * near) / (far - near),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10878
                     0, 0, -1, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10879
      var proj = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10880
      proj.set(projection);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10881
      proj.transpose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10882
      curContext.useProgram(programObject2D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10883
      uniformMatrix("projection2d", programObject2D, "projection", false, proj.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10884
      curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10885
      uniformMatrix("projection3d", programObject3D, "projection", false, proj.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10886
      curContext.useProgram(programObjectUnlitShape);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10887
      uniformMatrix("uProjectionUS", programObjectUnlitShape, "uProjection", false, proj.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10888
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10889
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10890
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10891
     * Sets an orthographic projection and defines a parallel clipping volume. All objects with the same dimension appear
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10892
     * the same size, regardless of whether they are near or far from the camera. The parameters to this function specify
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10893
     * the clipping volume where left and right are the minimum and maximum x values, top and bottom are the minimum and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10894
     * maximum y values, and near and far are the minimum and maximum z values. If no parameters are given, the default
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10895
     * is used: ortho(0, width, 0, height, -10, 10).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10896
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10897
     * @param {float} left   left plane of the clipping volume
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10898
     * @param {float} right  right plane of the clipping volume
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10899
     * @param {float} bottom bottom plane of the clipping volume
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10900
     * @param {float} top    top plane of the clipping volume
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10901
     * @param {float} near   maximum distance from the origin to the viewer
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10902
     * @param {float} far    maximum distance from the origin away from the viewer
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10903
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10904
    p.ortho = function(left, right, bottom, top, near, far) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10905
      if (arguments.length === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10906
        left = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10907
        right = p.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10908
        bottom = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10909
        top = p.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10910
        near = -10;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10911
        far = 10;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10912
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10913
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10914
      var x = 2 / (right - left);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10915
      var y = 2 / (top - bottom);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10916
      var z = -2 / (far - near);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10917
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10918
      var tx = -(right + left) / (right - left);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10919
      var ty = -(top + bottom) / (top - bottom);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10920
      var tz = -(far + near) / (far - near);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10921
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10922
      projection = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10923
      projection.set(x, 0, 0, tx, 0, y, 0, ty, 0, 0, z, tz, 0, 0, 0, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10924
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10925
      var proj = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10926
      proj.set(projection);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10927
      proj.transpose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10928
      curContext.useProgram(programObject2D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10929
      uniformMatrix("projection2d", programObject2D, "projection", false, proj.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10930
      curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10931
      uniformMatrix("projection3d", programObject3D, "projection", false, proj.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10932
      curContext.useProgram(programObjectUnlitShape);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10933
      uniformMatrix("uProjectionUS", programObjectUnlitShape, "uProjection", false, proj.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10934
      frustumMode = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10935
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10936
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10937
     * The printProjection() prints the current projection matrix to the text window.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10938
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10939
    p.printProjection = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10940
      projection.print();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10941
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10942
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10943
     * The printCamera() function prints the current camera matrix.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10944
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10945
    p.printCamera = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10946
      cam.print();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10947
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10948
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10949
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10950
    // Shapes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10951
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10952
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10953
     * The box() function renders a box. A box is an extruded rectangle. A box with equal dimension on all sides is a cube.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10954
     * Calling this function with only one parameter will create a cube.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10955
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10956
     * @param {int|float} w  dimension of the box in the x-dimension
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10957
     * @param {int|float} h  dimension of the box in the y-dimension
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10958
     * @param {int|float} d  dimension of the box in the z-dimension
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10959
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10960
    Drawing2D.prototype.box = DrawingShared.prototype.a3DOnlyFunction;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10961
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10962
    Drawing3D.prototype.box = function(w, h, d) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10963
      // user can uniformly scale the box by
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10964
      // passing in only one argument.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10965
      if (!h || !d) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10966
        h = d = w;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10967
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10968
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10969
      // Modeling transformation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10970
      var model = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10971
      model.scale(w, h, d);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10972
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10973
      // viewing transformation needs to have Y flipped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10974
      // becuase that's what Processing does.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10975
      var view = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10976
      view.scale(1, -1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10977
      view.apply(modelView.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10978
      view.transpose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10979
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10980
      if (doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10981
        curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10982
        uniformMatrix("model3d", programObject3D, "model", false, model.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10983
        uniformMatrix("view3d", programObject3D, "view", false, view.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10984
        // fix stitching problems. (lines get occluded by triangles
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10985
        // since they share the same depth values). This is not entirely
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10986
        // working, but it's a start for drawing the outline. So
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10987
        // developers can start playing around with styles.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10988
        curContext.enable(curContext.POLYGON_OFFSET_FILL);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10989
        curContext.polygonOffset(1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10990
        uniformf("color3d", programObject3D, "color", fillStyle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10991
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10992
        // Calculating the normal matrix can be expensive, so only
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10993
        // do it if it's necessary
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10994
        if(lightCount > 0){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10995
          // Create the normal transformation matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10996
          var v = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10997
          v.set(view);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10998
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 10999
          var m = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11000
          m.set(model);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11001
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11002
          v.mult(m);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11003
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11004
          var normalMatrix = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11005
          normalMatrix.set(v);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11006
          normalMatrix.invert();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11007
          normalMatrix.transpose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11008
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11009
          uniformMatrix("normalTransform3d", programObject3D, "normalTransform", false, normalMatrix.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11010
          vertexAttribPointer("normal3d", programObject3D, "Normal", 3, boxNormBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11011
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11012
        else{
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11013
          disableVertexAttribPointer("normal3d", programObject3D, "Normal");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11014
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11015
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11016
        vertexAttribPointer("vertex3d", programObject3D, "Vertex", 3, boxBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11017
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11018
        // Turn off per vertex colors
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11019
        disableVertexAttribPointer("aColor3d", programObject3D, "aColor");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11020
        disableVertexAttribPointer("aTexture3d", programObject3D, "aTexture");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11021
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11022
        curContext.drawArrays(curContext.TRIANGLES, 0, boxVerts.length / 3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11023
        curContext.disable(curContext.POLYGON_OFFSET_FILL);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11024
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11025
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11026
      if (lineWidth > 0 && doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11027
        curContext.useProgram(programObject2D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11028
        uniformMatrix("model2d", programObject2D, "model", false, model.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11029
        uniformMatrix("view2d", programObject2D, "view", false, view.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11030
        uniformf("color2d", programObject2D, "color", strokeStyle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11031
        uniformi("picktype2d", programObject2D, "picktype", 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11032
        vertexAttribPointer("vertex2d", programObject2D, "Vertex", 3, boxOutlineBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11033
        disableVertexAttribPointer("aTextureCoord2d", programObject2D, "aTextureCoord");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11034
        curContext.drawArrays(curContext.LINES, 0, boxOutlineVerts.length / 3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11035
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11036
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11037
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11038
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11039
     * The initSphere() function is a helper function used by <b>sphereDetail()</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11040
     * This function creates and stores sphere vertices every time the user changes sphere detail.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11041
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11042
     * @see #sphereDetail
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11043
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11044
    var initSphere = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11045
      var i;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11046
      sphereVerts = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11047
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11048
      for (i = 0; i < sphereDetailU; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11049
        sphereVerts.push(0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11050
        sphereVerts.push(-1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11051
        sphereVerts.push(0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11052
        sphereVerts.push(sphereX[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11053
        sphereVerts.push(sphereY[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11054
        sphereVerts.push(sphereZ[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11055
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11056
      sphereVerts.push(0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11057
      sphereVerts.push(-1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11058
      sphereVerts.push(0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11059
      sphereVerts.push(sphereX[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11060
      sphereVerts.push(sphereY[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11061
      sphereVerts.push(sphereZ[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11062
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11063
      var v1, v11, v2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11064
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11065
      // middle rings
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11066
      var voff = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11067
      for (i = 2; i < sphereDetailV; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11068
        v1 = v11 = voff;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11069
        voff += sphereDetailU;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11070
        v2 = voff;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11071
        for (var j = 0; j < sphereDetailU; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11072
          sphereVerts.push(sphereX[v1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11073
          sphereVerts.push(sphereY[v1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11074
          sphereVerts.push(sphereZ[v1++]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11075
          sphereVerts.push(sphereX[v2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11076
          sphereVerts.push(sphereY[v2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11077
          sphereVerts.push(sphereZ[v2++]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11078
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11079
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11080
        // close each ring
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11081
        v1 = v11;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11082
        v2 = voff;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11083
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11084
        sphereVerts.push(sphereX[v1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11085
        sphereVerts.push(sphereY[v1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11086
        sphereVerts.push(sphereZ[v1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11087
        sphereVerts.push(sphereX[v2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11088
        sphereVerts.push(sphereY[v2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11089
        sphereVerts.push(sphereZ[v2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11090
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11091
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11092
      // add the northern cap
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11093
      for (i = 0; i < sphereDetailU; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11094
        v2 = voff + i;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11095
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11096
        sphereVerts.push(sphereX[v2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11097
        sphereVerts.push(sphereY[v2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11098
        sphereVerts.push(sphereZ[v2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11099
        sphereVerts.push(0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11100
        sphereVerts.push(1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11101
        sphereVerts.push(0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11102
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11103
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11104
      sphereVerts.push(sphereX[voff]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11105
      sphereVerts.push(sphereY[voff]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11106
      sphereVerts.push(sphereZ[voff]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11107
      sphereVerts.push(0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11108
      sphereVerts.push(1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11109
      sphereVerts.push(0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11110
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11111
      //set the buffer data
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11112
      curContext.bindBuffer(curContext.ARRAY_BUFFER, sphereBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11113
      curContext.bufferData(curContext.ARRAY_BUFFER, new Float32Array(sphereVerts), curContext.STATIC_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11114
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11115
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11116
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11117
     * The sphereDetail() function controls the detail used to render a sphere by adjusting the number of
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11118
     * vertices of the sphere mesh. The default resolution is 30, which creates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11119
     * a fairly detailed sphere definition with vertices every 360/30 = 12
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11120
     * degrees. If you're going to render a great number of spheres per frame,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11121
     * it is advised to reduce the level of detail using this function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11122
     * The setting stays active until <b>sphereDetail()</b> is called again with
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11123
     * a new parameter and so should <i>not</i> be called prior to every
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11124
     * <b>sphere()</b> statement, unless you wish to render spheres with
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11125
     * different settings, e.g. using less detail for smaller spheres or ones
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11126
     * further away from the camera. To control the detail of the horizontal
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11127
     * and vertical resolution independently, use the version of the functions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11128
     * with two parameters. Calling this function with one parameter sets the number of segments
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11129
     *(minimum of 3) used per full circle revolution. This is equivalent to calling the function with
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11130
     * two identical values.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11131
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11132
     * @param {int} ures    number of segments used horizontally (longitudinally) per full circle revolution
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11133
     * @param {int} vres    number of segments used vertically (latitudinally) from top to bottom
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11134
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11135
     * @see #sphere()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11136
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11137
    p.sphereDetail = function(ures, vres) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11138
      var i;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11139
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11140
      if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11141
        ures = vres = arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11142
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11143
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11144
      if (ures < 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11145
        ures = 3;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11146
      } // force a minimum res
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11147
      if (vres < 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11148
        vres = 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11149
      } // force a minimum res
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11150
      // if it hasn't changed do nothing
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11151
      if ((ures === sphereDetailU) && (vres === sphereDetailV)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11152
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11153
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11154
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11155
      var delta = PConstants.SINCOS_LENGTH / ures;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11156
      var cx = new Float32Array(ures);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11157
      var cz = new Float32Array(ures);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11158
      // calc unit circle in XZ plane
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11159
      for (i = 0; i < ures; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11160
        cx[i] = cosLUT[((i * delta) % PConstants.SINCOS_LENGTH) | 0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11161
        cz[i] = sinLUT[((i * delta) % PConstants.SINCOS_LENGTH) | 0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11162
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11163
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11164
      // computing vertexlist
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11165
      // vertexlist starts at south pole
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11166
      var vertCount = ures * (vres - 1) + 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11167
      var currVert = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11168
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11169
      // re-init arrays to store vertices
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11170
      sphereX = new Float32Array(vertCount);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11171
      sphereY = new Float32Array(vertCount);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11172
      sphereZ = new Float32Array(vertCount);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11173
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11174
      var angle_step = (PConstants.SINCOS_LENGTH * 0.5) / vres;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11175
      var angle = angle_step;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11176
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11177
      // step along Y axis
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11178
      for (i = 1; i < vres; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11179
        var curradius = sinLUT[(angle % PConstants.SINCOS_LENGTH) | 0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11180
        var currY = -cosLUT[(angle % PConstants.SINCOS_LENGTH) | 0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11181
        for (var j = 0; j < ures; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11182
          sphereX[currVert] = cx[j] * curradius;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11183
          sphereY[currVert] = currY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11184
          sphereZ[currVert++] = cz[j] * curradius;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11185
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11186
        angle += angle_step;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11187
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11188
      sphereDetailU = ures;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11189
      sphereDetailV = vres;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11190
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11191
      // make the sphere verts and norms
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11192
      initSphere();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11193
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11194
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11195
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11196
     * The sphere() function draws a sphere with radius r centered at coordinate 0, 0, 0.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11197
     * A sphere is a hollow ball made from tessellated triangles.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11198
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11199
     * @param {int|float} r the radius of the sphere
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11200
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11201
    Drawing2D.prototype.sphere = DrawingShared.prototype.a3DOnlyFunction;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11202
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11203
    Drawing3D.prototype.sphere = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11204
      var sRad = arguments[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11205
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11206
      if ((sphereDetailU < 3) || (sphereDetailV < 2)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11207
        p.sphereDetail(30);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11208
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11209
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11210
      // Modeling transformation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11211
      var model = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11212
      model.scale(sRad, sRad, sRad);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11213
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11214
      // viewing transformation needs to have Y flipped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11215
      // becuase that's what Processing does.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11216
      var view = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11217
      view.scale(1, -1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11218
      view.apply(modelView.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11219
      view.transpose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11220
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11221
      if (doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11222
        // Calculating the normal matrix can be expensive, so only
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11223
        // do it if it's necessary
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11224
        if(lightCount > 0){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11225
          // Create a normal transformation matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11226
          var v = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11227
          v.set(view);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11228
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11229
          var m = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11230
          m.set(model);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11231
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11232
          v.mult(m);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11233
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11234
          var normalMatrix = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11235
          normalMatrix.set(v);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11236
          normalMatrix.invert();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11237
          normalMatrix.transpose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11238
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11239
          uniformMatrix("normalTransform3d", programObject3D, "normalTransform", false, normalMatrix.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11240
          vertexAttribPointer("normal3d", programObject3D, "Normal", 3, sphereBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11241
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11242
        else{
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11243
          disableVertexAttribPointer("normal3d", programObject3D, "Normal");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11244
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11245
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11246
        curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11247
        disableVertexAttribPointer("aTexture3d", programObject3D, "aTexture");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11248
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11249
        uniformMatrix("model3d", programObject3D, "model", false, model.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11250
        uniformMatrix("view3d", programObject3D, "view", false, view.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11251
        vertexAttribPointer("vertex3d", programObject3D, "Vertex", 3, sphereBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11252
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11253
        // Turn off per vertex colors
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11254
        disableVertexAttribPointer("aColor3d", programObject3D, "aColor");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11255
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11256
        // fix stitching problems. (lines get occluded by triangles
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11257
        // since they share the same depth values). This is not entirely
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11258
        // working, but it's a start for drawing the outline. So
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11259
        // developers can start playing around with styles.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11260
        curContext.enable(curContext.POLYGON_OFFSET_FILL);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11261
        curContext.polygonOffset(1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11262
        uniformf("color3d", programObject3D, "color", fillStyle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11263
        curContext.drawArrays(curContext.TRIANGLE_STRIP, 0, sphereVerts.length / 3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11264
        curContext.disable(curContext.POLYGON_OFFSET_FILL);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11265
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11266
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11267
      if (lineWidth > 0 && doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11268
        curContext.useProgram(programObject2D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11269
        uniformMatrix("model2d", programObject2D, "model", false, model.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11270
        uniformMatrix("view2d", programObject2D, "view", false, view.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11271
        vertexAttribPointer("vertex2d", programObject2D, "Vertex", 3, sphereBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11272
        disableVertexAttribPointer("aTextureCoord2d", programObject2D, "aTextureCoord");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11273
        uniformf("color2d", programObject2D, "color", strokeStyle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11274
        uniformi("picktype2d", programObject2D, "picktype", 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11275
        curContext.drawArrays(curContext.LINE_STRIP, 0, sphereVerts.length / 3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11276
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11277
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11278
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11279
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11280
    // Coordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11281
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11282
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11283
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11284
     * Returns the three-dimensional X, Y, Z position in model space. This returns
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11285
     * the X value for a given coordinate based on the current set of transformations
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11286
     * (scale, rotate, translate, etc.) The X value can be used to place an object
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11287
     * in space relative to the location of the original point once the transformations
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11288
     * are no longer in use.<br />
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11289
     * <br />
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11290
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11291
     * @param {int | float} x 3D x coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11292
     * @param {int | float} y 3D y coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11293
     * @param {int | float} z 3D z coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11294
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11295
     * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11296
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11297
     * @see modelY
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11298
     * @see modelZ
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11299
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11300
    p.modelX = function(x, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11301
      var mv = modelView.array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11302
      var ci = cameraInv.array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11303
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11304
      var ax = mv[0] * x + mv[1] * y + mv[2] * z + mv[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11305
      var ay = mv[4] * x + mv[5] * y + mv[6] * z + mv[7];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11306
      var az = mv[8] * x + mv[9] * y + mv[10] * z + mv[11];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11307
      var aw = mv[12] * x + mv[13] * y + mv[14] * z + mv[15];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11308
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11309
      var ox = ci[0] * ax + ci[1] * ay + ci[2] * az + ci[3] * aw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11310
      var ow = ci[12] * ax + ci[13] * ay + ci[14] * az + ci[15] * aw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11311
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11312
      return (ow !== 0) ? ox / ow : ox;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11313
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11314
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11315
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11316
     * Returns the three-dimensional X, Y, Z position in model space. This returns
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11317
     * the Y value for a given coordinate based on the current set of transformations
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11318
     * (scale, rotate, translate, etc.) The Y value can be used to place an object in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11319
     * space relative to the location of the original point once the transformations
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11320
     * are no longer in use.<br />
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11321
     * <br />
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11322
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11323
     * @param {int | float} x 3D x coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11324
     * @param {int | float} y 3D y coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11325
     * @param {int | float} z 3D z coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11326
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11327
     * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11328
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11329
     * @see modelX
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11330
     * @see modelZ
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11331
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11332
    p.modelY = function(x, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11333
      var mv = modelView.array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11334
      var ci = cameraInv.array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11335
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11336
      var ax = mv[0] * x + mv[1] * y + mv[2] * z + mv[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11337
      var ay = mv[4] * x + mv[5] * y + mv[6] * z + mv[7];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11338
      var az = mv[8] * x + mv[9] * y + mv[10] * z + mv[11];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11339
      var aw = mv[12] * x + mv[13] * y + mv[14] * z + mv[15];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11340
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11341
      var oy = ci[4] * ax + ci[5] * ay + ci[6] * az + ci[7] * aw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11342
      var ow = ci[12] * ax + ci[13] * ay + ci[14] * az + ci[15] * aw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11343
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11344
      return (ow !== 0) ? oy / ow : oy;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11345
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11346
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11347
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11348
     * Returns the three-dimensional X, Y, Z position in model space. This returns
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11349
     * the Z value for a given coordinate based on the current set of transformations
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11350
     * (scale, rotate, translate, etc.) The Z value can be used to place an object in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11351
     * space relative to the location of the original point once the transformations
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11352
     * are no longer in use.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11353
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11354
     * @param {int | float} x 3D x coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11355
     * @param {int | float} y 3D y coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11356
     * @param {int | float} z 3D z coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11357
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11358
     * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11359
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11360
     * @see modelX
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11361
     * @see modelY
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11362
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11363
    p.modelZ = function(x, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11364
      var mv = modelView.array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11365
      var ci = cameraInv.array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11366
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11367
      var ax = mv[0] * x + mv[1] * y + mv[2] * z + mv[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11368
      var ay = mv[4] * x + mv[5] * y + mv[6] * z + mv[7];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11369
      var az = mv[8] * x + mv[9] * y + mv[10] * z + mv[11];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11370
      var aw = mv[12] * x + mv[13] * y + mv[14] * z + mv[15];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11371
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11372
      var oz = ci[8] * ax + ci[9] * ay + ci[10] * az + ci[11] * aw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11373
      var ow = ci[12] * ax + ci[13] * ay + ci[14] * az + ci[15] * aw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11374
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11375
      return (ow !== 0) ? oz / ow : oz;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11376
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11377
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11378
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11379
    // Material Properties
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11380
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11381
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11382
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11383
     * Sets the ambient reflectance for shapes drawn to the screen. This is
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11384
     * combined with the ambient light component of environment. The color
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11385
     * components set through the parameters define the reflectance. For example in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11386
     * the default color mode, setting v1=255, v2=126, v3=0, would cause all the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11387
     * red light to reflect and half of the green light to reflect. Used in combination
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11388
     * with <b>emissive()</b>, <b>specular()</b>, and <b>shininess()</b> in setting
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11389
     * the materal properties of shapes.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11390
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11391
     * @param {int | float} gray
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11392
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11393
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11394
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11395
     * @see emissive
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11396
     * @see specular
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11397
     * @see shininess
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11398
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11399
    Drawing2D.prototype.ambient = DrawingShared.prototype.a3DOnlyFunction;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11400
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11401
    Drawing3D.prototype.ambient = function(v1, v2, v3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11402
      curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11403
      uniformi("usingMat3d", programObject3D, "usingMat", true);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11404
      var col = p.color(v1, v2, v3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11405
      uniformf("mat_ambient3d", programObject3D, "mat_ambient", p.color.toGLArray(col).slice(0, 3));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11406
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11407
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11408
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11409
     * Sets the emissive color of the material used for drawing shapes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11410
     * drawn to the screen. Used in combination with ambient(), specular(),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11411
     * and shininess() in setting the material properties of shapes.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11412
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11413
     * Can be called in the following ways:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11414
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11415
     * emissive(gray)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11416
     * @param {int | float} gray number specifying value between white and black
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11417
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11418
     * emissive(color)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11419
     * @param {color} color any value of the color datatype
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11420
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11421
     * emissive(v1, v2, v3)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11422
     * @param {int | float} v1 red or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11423
     * @param {int | float} v2 green or saturation value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11424
     * @param {int | float} v3 blue or brightness value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11425
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11426
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11427
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11428
     * @see ambient
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11429
     * @see specular
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11430
     * @see shininess
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11431
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11432
    Drawing2D.prototype.emissive = DrawingShared.prototype.a3DOnlyFunction;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11433
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11434
    Drawing3D.prototype.emissive = function(v1, v2, v3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11435
      curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11436
      uniformi("usingMat3d", programObject3D, "usingMat", true);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11437
      var col = p.color(v1, v2, v3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11438
      uniformf("mat_emissive3d", programObject3D, "mat_emissive", p.color.toGLArray(col).slice(0, 3));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11439
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11440
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11441
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11442
     * Sets the amount of gloss in the surface of shapes. Used in combination with
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11443
     * <b>ambient()</b>, <b>specular()</b>, and <b>emissive()</b> in setting the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11444
     * material properties of shapes.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11445
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11446
     * @param {float} shine degree of shininess
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11447
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11448
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11449
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11450
    Drawing2D.prototype.shininess = DrawingShared.prototype.a3DOnlyFunction;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11451
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11452
    Drawing3D.prototype.shininess = function(shine) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11453
      curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11454
      uniformi("usingMat3d", programObject3D, "usingMat", true);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11455
      uniformf("shininess3d", programObject3D, "shininess", shine);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11456
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11457
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11458
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11459
     * Sets the specular color of the materials used for shapes drawn to the screen,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11460
     * which sets the color of hightlights. Specular refers to light which bounces
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11461
     * off a surface in a perferred direction (rather than bouncing in all directions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11462
     * like a diffuse light). Used in combination with emissive(), ambient(), and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11463
     * shininess() in setting the material properties of shapes.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11464
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11465
     * Can be called in the following ways:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11466
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11467
     * specular(gray)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11468
     * @param {int | float} gray number specifying value between white and black
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11469
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11470
     * specular(gray, alpha)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11471
     * @param {int | float} gray number specifying value between white and black
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11472
     * @param {int | float} alpha opacity
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11473
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11474
     * specular(color)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11475
     * @param {color} color any value of the color datatype
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11476
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11477
     * specular(v1, v2, v3)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11478
     * @param {int | float} v1 red or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11479
     * @param {int | float} v2 green or saturation value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11480
     * @param {int | float} v3 blue or brightness value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11481
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11482
     * specular(v1, v2, v3, alpha)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11483
     * @param {int | float} v1 red or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11484
     * @param {int | float} v2 green or saturation value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11485
     * @param {int | float} v3 blue or brightness value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11486
     * @param {int | float} alpha opacity
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11487
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11488
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11489
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11490
     * @see ambient
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11491
     * @see emissive
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11492
     * @see shininess
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11493
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11494
    Drawing2D.prototype.specular = DrawingShared.prototype.a3DOnlyFunction;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11495
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11496
    Drawing3D.prototype.specular = function(v1, v2, v3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11497
      curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11498
      uniformi("usingMat3d", programObject3D, "usingMat", true);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11499
      var col = p.color(v1, v2, v3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11500
      uniformf("mat_specular3d", programObject3D, "mat_specular", p.color.toGLArray(col).slice(0, 3));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11501
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11502
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11503
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11504
    // Coordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11505
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11506
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11507
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11508
     * Takes a three-dimensional X, Y, Z position and returns the X value for
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11509
     * where it will appear on a (two-dimensional) screen.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11510
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11511
     * @param {int | float} x 3D x coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11512
     * @param {int | float} y 3D y coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11513
     * @param {int | float} z 3D z optional coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11514
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11515
     * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11516
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11517
     * @see screenY
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11518
     * @see screenZ
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11519
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11520
    p.screenX = function( x, y, z ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11521
      var mv = modelView.array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11522
      if( mv.length === 16 )
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11523
      {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11524
        var ax = mv[ 0]*x + mv[ 1]*y + mv[ 2]*z + mv[ 3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11525
        var ay = mv[ 4]*x + mv[ 5]*y + mv[ 6]*z + mv[ 7];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11526
        var az = mv[ 8]*x + mv[ 9]*y + mv[10]*z + mv[11];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11527
        var aw = mv[12]*x + mv[13]*y + mv[14]*z + mv[15];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11528
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11529
        var pj = projection.array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11530
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11531
        var ox = pj[ 0]*ax + pj[ 1]*ay + pj[ 2]*az + pj[ 3]*aw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11532
        var ow = pj[12]*ax + pj[13]*ay + pj[14]*az + pj[15]*aw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11533
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11534
        if ( ow !== 0 ){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11535
          ox /= ow;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11536
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11537
        return p.width * ( 1 + ox ) / 2.0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11538
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11539
      // We assume that we're in 2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11540
      return modelView.multX(x, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11541
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11542
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11543
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11544
     * Takes a three-dimensional X, Y, Z position and returns the Y value for
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11545
     * where it will appear on a (two-dimensional) screen.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11546
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11547
     * @param {int | float} x 3D x coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11548
     * @param {int | float} y 3D y coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11549
     * @param {int | float} z 3D z optional coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11550
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11551
     * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11552
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11553
     * @see screenX
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11554
     * @see screenZ
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11555
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11556
    p.screenY = function screenY( x, y, z ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11557
      var mv = modelView.array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11558
      if( mv.length === 16 ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11559
        var ax = mv[ 0]*x + mv[ 1]*y + mv[ 2]*z + mv[ 3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11560
        var ay = mv[ 4]*x + mv[ 5]*y + mv[ 6]*z + mv[ 7];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11561
        var az = mv[ 8]*x + mv[ 9]*y + mv[10]*z + mv[11];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11562
        var aw = mv[12]*x + mv[13]*y + mv[14]*z + mv[15];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11563
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11564
        var pj = projection.array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11565
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11566
        var oy = pj[ 4]*ax + pj[ 5]*ay + pj[ 6]*az + pj[ 7]*aw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11567
        var ow = pj[12]*ax + pj[13]*ay + pj[14]*az + pj[15]*aw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11568
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11569
        if ( ow !== 0 ){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11570
          oy /= ow;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11571
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11572
        return p.height * ( 1 + oy ) / 2.0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11573
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11574
      // We assume that we're in 2D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11575
      return modelView.multY(x, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11576
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11577
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11578
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11579
     * Takes a three-dimensional X, Y, Z position and returns the Z value for
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11580
     * where it will appear on a (two-dimensional) screen.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11581
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11582
     * @param {int | float} x 3D x coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11583
     * @param {int | float} y 3D y coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11584
     * @param {int | float} z 3D z coordinate to be mapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11585
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11586
     * @returns {float}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11587
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11588
     * @see screenX
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11589
     * @see screenY
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11590
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11591
    p.screenZ = function screenZ( x, y, z ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11592
      var mv = modelView.array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11593
      if( mv.length !== 16 ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11594
        return 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11595
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11596
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11597
      var pj = projection.array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11598
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11599
      var ax = mv[ 0]*x + mv[ 1]*y + mv[ 2]*z + mv[ 3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11600
      var ay = mv[ 4]*x + mv[ 5]*y + mv[ 6]*z + mv[ 7];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11601
      var az = mv[ 8]*x + mv[ 9]*y + mv[10]*z + mv[11];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11602
      var aw = mv[12]*x + mv[13]*y + mv[14]*z + mv[15];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11603
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11604
      var oz = pj[ 8]*ax + pj[ 9]*ay + pj[10]*az + pj[11]*aw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11605
      var ow = pj[12]*ax + pj[13]*ay + pj[14]*az + pj[15]*aw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11606
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11607
      if ( ow !== 0 ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11608
        oz /= ow;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11609
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11610
      return ( oz + 1 ) / 2.0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11611
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11612
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11613
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11614
    // Style functions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11615
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11616
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11617
     * The fill() function sets the color used to fill shapes. For example, if you run <b>fill(204, 102, 0)</b>, all subsequent shapes will be filled with orange.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11618
     * This color is either specified in terms of the RGB or HSB color depending on the current <b>colorMode()</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11619
     *(the default color space is RGB, with each value in the range from 0 to 255).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11620
     * <br><br>When using hexadecimal notation to specify a color, use "#" or "0x" before the values (e.g. #CCFFAA, 0xFFCCFFAA).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11621
     * The # syntax uses six digits to specify a color (the way colors are specified in HTML and CSS). When using the hexadecimal notation starting with "0x",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11622
     * the hexadecimal value must be specified with eight characters; the first two characters define the alpha component and the remainder the red, green, and blue components.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11623
     * <br><br>The value for the parameter "gray" must be less than or equal to the current maximum value as specified by <b>colorMode()</b>. The default maximum value is 255.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11624
     * <br><br>To change the color of an image (or a texture), use tint().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11625
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11626
     * @param {int|float} gray    number specifying value between white and black
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11627
     * @param {int|float} value1  red or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11628
     * @param {int|float} value2  green or saturation value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11629
     * @param {int|float} value3  blue or brightness value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11630
     * @param {int|float} alpha   opacity of the fill
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11631
     * @param {Color} color       any value of the color datatype
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11632
     * @param {int} hex           color value in hexadecimal notation (i.e. #FFCC00 or 0xFFFFCC00)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11633
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11634
     * @see #noFill()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11635
     * @see #stroke()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11636
     * @see #tint()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11637
     * @see #background()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11638
     * @see #colorMode()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11639
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11640
    DrawingShared.prototype.fill = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11641
      var color = p.color(arguments[0], arguments[1], arguments[2], arguments[3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11642
      if(color === currentFillColor && doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11643
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11644
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11645
      doFill = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11646
      currentFillColor = color;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11647
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11648
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11649
    Drawing2D.prototype.fill = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11650
      DrawingShared.prototype.fill.apply(this, arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11651
      isFillDirty = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11652
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11653
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11654
    Drawing3D.prototype.fill = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11655
      DrawingShared.prototype.fill.apply(this, arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11656
      fillStyle = p.color.toGLArray(currentFillColor);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11657
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11658
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11659
    function executeContextFill() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11660
      if(doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11661
        if(isFillDirty) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11662
          curContext.fillStyle = p.color.toString(currentFillColor);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11663
          isFillDirty = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11664
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11665
        curContext.fill();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11666
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11667
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11668
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11669
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11670
     * The noFill() function disables filling geometry. If both <b>noStroke()</b> and <b>noFill()</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11671
     * are called, no shapes will be drawn to the screen.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11672
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11673
     * @see #fill()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11674
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11675
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11676
    p.noFill = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11677
      doFill = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11678
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11679
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11680
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11681
     * The stroke() function sets the color used to draw lines and borders around shapes. This color
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11682
     * is either specified in terms of the RGB or HSB color depending on the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11683
     * current <b>colorMode()</b> (the default color space is RGB, with each
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11684
     * value in the range from 0 to 255).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11685
     * <br><br>When using hexadecimal notation to specify a color, use "#" or
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11686
     * "0x" before the values (e.g. #CCFFAA, 0xFFCCFFAA). The # syntax uses six
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11687
     * digits to specify a color (the way colors are specified in HTML and CSS).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11688
     * When using the hexadecimal notation starting with "0x", the hexadecimal
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11689
     * value must be specified with eight characters; the first two characters
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11690
     * define the alpha component and the remainder the red, green, and blue
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11691
     * components.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11692
     * <br><br>The value for the parameter "gray" must be less than or equal
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11693
     * to the current maximum value as specified by <b>colorMode()</b>.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11694
     * The default maximum value is 255.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11695
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11696
     * @param {int|float} gray    number specifying value between white and black
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11697
     * @param {int|float} value1  red or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11698
     * @param {int|float} value2  green or saturation value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11699
     * @param {int|float} value3  blue or brightness value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11700
     * @param {int|float} alpha   opacity of the stroke
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11701
     * @param {Color} color       any value of the color datatype
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11702
     * @param {int} hex           color value in hexadecimal notation (i.e. #FFCC00 or 0xFFFFCC00)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11703
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11704
     * @see #fill()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11705
     * @see #noStroke()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11706
     * @see #tint()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11707
     * @see #background()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11708
     * @see #colorMode()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11709
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11710
    DrawingShared.prototype.stroke = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11711
      var color = p.color(arguments[0], arguments[1], arguments[2], arguments[3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11712
      if(color === currentStrokeColor && doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11713
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11714
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11715
      doStroke = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11716
      currentStrokeColor = color;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11717
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11718
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11719
    Drawing2D.prototype.stroke = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11720
      DrawingShared.prototype.stroke.apply(this, arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11721
      isStrokeDirty = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11722
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11723
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11724
    Drawing3D.prototype.stroke = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11725
      DrawingShared.prototype.stroke.apply(this, arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11726
      strokeStyle = p.color.toGLArray(currentStrokeColor);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11727
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11728
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11729
    function executeContextStroke() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11730
      if(doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11731
        if(isStrokeDirty) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11732
          curContext.strokeStyle = p.color.toString(currentStrokeColor);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11733
          isStrokeDirty = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11734
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11735
        curContext.stroke();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11736
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11737
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11738
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11739
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11740
     * The noStroke() function disables drawing the stroke (outline). If both <b>noStroke()</b> and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11741
     * <b>noFill()</b> are called, no shapes will be drawn to the screen.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11742
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11743
     * @see #stroke()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11744
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11745
    p.noStroke = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11746
      doStroke = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11747
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11748
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11749
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11750
     * The strokeWeight() function sets the width of the stroke used for lines, points, and the border around shapes.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11751
     * All widths are set in units of pixels.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11752
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11753
     * @param {int|float} w the weight (in pixels) of the stroke
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11754
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11755
    DrawingShared.prototype.strokeWeight = function(w) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11756
      lineWidth = w;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11757
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11758
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11759
    Drawing2D.prototype.strokeWeight = function(w) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11760
      DrawingShared.prototype.strokeWeight.apply(this, arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11761
      curContext.lineWidth = w;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11762
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11763
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11764
    Drawing3D.prototype.strokeWeight = function(w) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11765
      DrawingShared.prototype.strokeWeight.apply(this, arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11766
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11767
      // Processing groups the weight of points and lines under this one function,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11768
      // but for WebGL, we need to set a uniform for points and call a function for line.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11769
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11770
      curContext.useProgram(programObject2D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11771
      uniformf("pointSize2d", programObject2D, "pointSize", w);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11772
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11773
      curContext.useProgram(programObjectUnlitShape);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11774
      uniformf("pointSizeUnlitShape", programObjectUnlitShape, "pointSize", w);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11775
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11776
      curContext.lineWidth(w);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11777
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11778
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11779
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11780
     * The strokeCap() function sets the style for rendering line endings. These ends are either squared, extended, or rounded and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11781
     * specified with the corresponding parameters SQUARE, PROJECT, and ROUND. The default cap is ROUND.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11782
     * This function is not available with the P2D, P3D, or OPENGL renderers
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11783
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11784
     * @param {int} value Either SQUARE, PROJECT, or ROUND
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11785
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11786
    p.strokeCap = function(value) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11787
      drawing.$ensureContext().lineCap = value;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11788
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11789
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11790
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11791
     * The strokeJoin() function sets the style of the joints which connect line segments.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11792
     * These joints are either mitered, beveled, or rounded and specified with the corresponding parameters MITER, BEVEL, and ROUND. The default joint is MITER.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11793
     * This function is not available with the P2D, P3D, or OPENGL renderers
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11794
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11795
     * @param {int} value Either SQUARE, PROJECT, or ROUND
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11796
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11797
    p.strokeJoin = function(value) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11798
      drawing.$ensureContext().lineJoin = value;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11799
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11800
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11801
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11802
     * The smooth() function draws all geometry with smooth (anti-aliased) edges. This will slow down the frame rate of the application,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11803
     * but will enhance the visual refinement. <br/><br/>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11804
     * Note that smooth() will also improve image quality of resized images, and noSmooth() will disable image (and font) smoothing altogether.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11805
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11806
     * @see #noSmooth()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11807
     * @see #hint()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11808
     * @see #size()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11809
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11810
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11811
    Drawing2D.prototype.smooth = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11812
      renderSmooth = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11813
      var style = curElement.style;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11814
      style.setProperty("image-rendering", "optimizeQuality", "important");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11815
      style.setProperty("-ms-interpolation-mode", "bicubic", "important");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11816
      if (curContext.hasOwnProperty("mozImageSmoothingEnabled")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11817
        curContext.mozImageSmoothingEnabled = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11818
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11819
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11820
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11821
    Drawing3D.prototype.smooth = nop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11822
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11823
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11824
     * The noSmooth() function draws all geometry with jagged (aliased) edges.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11825
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11826
     * @see #smooth()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11827
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11828
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11829
    Drawing2D.prototype.noSmooth = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11830
      renderSmooth = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11831
      var style = curElement.style;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11832
      style.setProperty("image-rendering", "optimizeSpeed", "important");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11833
      style.setProperty("image-rendering", "-moz-crisp-edges", "important");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11834
      style.setProperty("image-rendering", "-webkit-optimize-contrast", "important");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11835
      style.setProperty("image-rendering", "optimize-contrast", "important");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11836
      style.setProperty("-ms-interpolation-mode", "nearest-neighbor", "important");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11837
      if (curContext.hasOwnProperty("mozImageSmoothingEnabled")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11838
        curContext.mozImageSmoothingEnabled = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11839
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11840
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11841
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11842
    Drawing3D.prototype.noSmooth = nop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11843
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11844
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11845
    // Vector drawing functions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11846
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11847
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11848
     * The point() function draws a point, a coordinate in space at the dimension of one pixel.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11849
     * The first parameter is the horizontal value for the point, the second
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11850
     * value is the vertical value for the point, and the optional third value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11851
     * is the depth value. Drawing this shape in 3D using the <b>z</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11852
     * parameter requires the P3D or OPENGL parameter in combination with
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11853
     * size as shown in the above example.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11854
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11855
     * @param {int|float} x x-coordinate of the point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11856
     * @param {int|float} y y-coordinate of the point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11857
     * @param {int|float} z z-coordinate of the point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11858
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11859
     * @see #beginShape()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11860
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11861
    Drawing2D.prototype.point = function(x, y) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11862
      if (!doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11863
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11864
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11865
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11866
      x = Math.round(x);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11867
      y = Math.round(y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11868
      curContext.fillStyle = p.color.toString(currentStrokeColor);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11869
      isFillDirty = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11870
      // Draw a circle for any point larger than 1px
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11871
      if (lineWidth > 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11872
        curContext.beginPath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11873
        curContext.arc(x, y, lineWidth / 2, 0, PConstants.TWO_PI, false);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11874
        curContext.fill();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11875
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11876
        curContext.fillRect(x, y, 1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11877
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11878
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11879
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11880
    Drawing3D.prototype.point = function(x, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11881
      var model = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11882
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11883
      // move point to position
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11884
      model.translate(x, y, z || 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11885
      model.transpose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11886
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11887
      var view = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11888
      view.scale(1, -1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11889
      view.apply(modelView.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11890
      view.transpose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11891
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11892
      curContext.useProgram(programObject2D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11893
      uniformMatrix("model2d", programObject2D, "model", false, model.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11894
      uniformMatrix("view2d", programObject2D, "view", false, view.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11895
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11896
      if (lineWidth > 0 && doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11897
        // this will be replaced with the new bit shifting color code
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11898
        uniformf("color2d", programObject2D, "color", strokeStyle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11899
        uniformi("picktype2d", programObject2D, "picktype", 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11900
        vertexAttribPointer("vertex2d", programObject2D, "Vertex", 3, pointBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11901
        disableVertexAttribPointer("aTextureCoord2d", programObject2D, "aTextureCoord");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11902
        curContext.drawArrays(curContext.POINTS, 0, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11903
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11904
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11905
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11906
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11907
     * Using the <b>beginShape()</b> and <b>endShape()</b> functions allow creating more complex forms.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11908
     * <b>beginShape()</b> begins recording vertices for a shape and <b>endShape()</b> stops recording.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11909
     * The value of the <b>MODE</b> parameter tells it which types of shapes to create from the provided vertices.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11910
     * With no mode specified, the shape can be any irregular polygon. After calling the <b>beginShape()</b> function,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11911
     * a series of <b>vertex()</b> commands must follow. To stop drawing the shape, call <b>endShape()</b>.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11912
     * The <b>vertex()</b> function with two parameters specifies a position in 2D and the <b>vertex()</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11913
     * function with three parameters specifies a position in 3D. Each shape will be outlined with the current
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11914
     * stroke color and filled with the fill color.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11915
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11916
     * @param {int} MODE either POINTS, LINES, TRIANGLES, TRIANGLE_FAN, TRIANGLE_STRIP, QUADS, and QUAD_STRIP.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11917
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11918
     * @see endShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11919
     * @see vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11920
     * @see curveVertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11921
     * @see bezierVertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11922
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11923
    p.beginShape = function(type) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11924
      curShape = type;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11925
      curvePoints = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11926
      vertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11927
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11928
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11929
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11930
     * All shapes are constructed by connecting a series of vertices. <b>vertex()</b> is used to specify the vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11931
     * coordinates for points, lines, triangles, quads, and polygons and is used exclusively within the <b>beginShape()</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11932
     * and <b>endShape()</b> function. <br /><br />Drawing a vertex in 3D using the <b>z</b> parameter requires the P3D or
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11933
     * OPENGL parameter in combination with size as shown in the above example.<br /><br />This function is also used to map a
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11934
     * texture onto the geometry. The <b>texture()</b> function declares the texture to apply to the geometry and the <b>u</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11935
     * and <b>v</b> coordinates set define the mapping of this texture to the form. By default, the coordinates used for
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11936
     * <b>u</b> and <b>v</b> are specified in relation to the image's size in pixels, but this relation can be changed with
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11937
     * <b>textureMode()</b>.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11938
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11939
     * @param {int | float} x x-coordinate of the vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11940
     * @param {int | float} y y-coordinate of the vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11941
     * @param {int | float} z z-coordinate of the vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11942
     * @param {int | float} u horizontal coordinate for the texture mapping
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11943
     * @param {int | float} v vertical coordinate for the texture mapping
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11944
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11945
     * @see beginShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11946
     * @see endShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11947
     * @see bezierVertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11948
     * @see curveVertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11949
     * @see texture
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11950
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11951
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11952
    Drawing2D.prototype.vertex = function(x, y, u, v) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11953
      var vert = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11954
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11955
      if (firstVert) { firstVert = false; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11956
      vert["isVert"] = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11957
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11958
      vert[0] = x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11959
      vert[1] = y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11960
      vert[2] = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11961
      vert[3] = u;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11962
      vert[4] = v;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11963
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11964
      // fill and stroke color
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11965
      vert[5] = currentFillColor;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11966
      vert[6] = currentStrokeColor;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11967
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11968
      vertArray.push(vert);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11969
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11970
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11971
    Drawing3D.prototype.vertex = function(x, y, z, u, v) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11972
      var vert = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11973
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11974
      if (firstVert) { firstVert = false; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11975
      vert["isVert"] = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11976
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11977
      if (v === undef && usingTexture) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11978
        v = u;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11979
        u = z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11980
        z = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11981
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11982
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11983
      // Convert u and v to normalized coordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11984
      if (u !== undef && v !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11985
        if (curTextureMode === PConstants.IMAGE) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11986
          u /= curTexture.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11987
          v /= curTexture.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11988
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11989
        u = u > 1 ? 1 : u;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11990
        u = u < 0 ? 0 : u;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11991
        v = v > 1 ? 1 : v;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11992
        v = v < 0 ? 0 : v;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11993
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11994
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11995
      vert[0] = x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11996
      vert[1] = y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11997
      vert[2] = z || 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11998
      vert[3] = u || 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 11999
      vert[4] = v || 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12000
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12001
      // fill rgba
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12002
      vert[5] = fillStyle[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12003
      vert[6] = fillStyle[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12004
      vert[7] = fillStyle[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12005
      vert[8] = fillStyle[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12006
      // stroke rgba
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12007
      vert[9] = strokeStyle[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12008
      vert[10] = strokeStyle[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12009
      vert[11] = strokeStyle[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12010
      vert[12] = strokeStyle[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12011
      //normals
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12012
      vert[13] = normalX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12013
      vert[14] = normalY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12014
      vert[15] = normalZ;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12015
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12016
      vertArray.push(vert);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12017
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12018
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12019
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12020
     * @private
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12021
     * Renders 3D points created from calls to vertex and beginShape/endShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12022
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12023
     * @param {Array} vArray an array of vertex coordinate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12024
     * @param {Array} cArray an array of colours used for the vertices
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12025
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12026
     * @see beginShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12027
     * @see endShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12028
     * @see vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12029
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12030
    var point3D = function(vArray, cArray){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12031
      var view = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12032
      view.scale(1, -1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12033
      view.apply(modelView.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12034
      view.transpose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12035
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12036
      curContext.useProgram(programObjectUnlitShape);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12037
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12038
      uniformMatrix("uViewUS", programObjectUnlitShape, "uView", false, view.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12039
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12040
      vertexAttribPointer("aVertexUS", programObjectUnlitShape, "aVertex", 3, pointBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12041
      curContext.bufferData(curContext.ARRAY_BUFFER, new Float32Array(vArray), curContext.STREAM_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12042
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12043
      vertexAttribPointer("aColorUS", programObjectUnlitShape, "aColor", 4, fillColorBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12044
      curContext.bufferData(curContext.ARRAY_BUFFER, new Float32Array(cArray), curContext.STREAM_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12045
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12046
      curContext.drawArrays(curContext.POINTS, 0, vArray.length/3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12047
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12048
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12049
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12050
     * @private
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12051
     * Renders 3D lines created from calls to beginShape/vertex/endShape - based on the mode specified LINES, LINE_LOOP, etc.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12052
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12053
     * @param {Array} vArray an array of vertex coordinate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12054
     * @param {String} mode  either LINES, LINE_LOOP, or LINE_STRIP
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12055
     * @param {Array} cArray an array of colours used for the vertices
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12056
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12057
     * @see beginShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12058
     * @see endShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12059
     * @see vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12060
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12061
    var line3D = function(vArray, mode, cArray){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12062
      var ctxMode;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12063
      if (mode === "LINES"){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12064
        ctxMode = curContext.LINES;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12065
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12066
      else if(mode === "LINE_LOOP"){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12067
        ctxMode = curContext.LINE_LOOP;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12068
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12069
      else{
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12070
        ctxMode = curContext.LINE_STRIP;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12071
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12072
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12073
      var view = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12074
      view.scale(1, -1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12075
      view.apply(modelView.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12076
      view.transpose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12077
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12078
      curContext.useProgram(programObjectUnlitShape);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12079
      uniformMatrix("uViewUS", programObjectUnlitShape, "uView", false, view.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12080
      vertexAttribPointer("aVertexUS", programObjectUnlitShape, "aVertex", 3, lineBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12081
      curContext.bufferData(curContext.ARRAY_BUFFER, new Float32Array(vArray), curContext.STREAM_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12082
      vertexAttribPointer("aColorUS", programObjectUnlitShape, "aColor", 4, strokeColorBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12083
      curContext.bufferData(curContext.ARRAY_BUFFER, new Float32Array(cArray), curContext.STREAM_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12084
      curContext.drawArrays(ctxMode, 0, vArray.length/3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12085
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12086
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12087
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12088
     * @private
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12089
     * Render filled shapes created from calls to beginShape/vertex/endShape - based on the mode specified TRIANGLES, etc.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12090
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12091
     * @param {Array} vArray an array of vertex coordinate
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12092
     * @param {String} mode  either LINES, LINE_LOOP, or LINE_STRIP
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12093
     * @param {Array} cArray an array of colours used for the vertices
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12094
     * @param {Array} tArray an array of u,v coordinates for textures
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12095
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12096
     * @see beginShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12097
     * @see endShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12098
     * @see vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12099
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12100
    var fill3D = function(vArray, mode, cArray, tArray){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12101
      var ctxMode;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12102
      if (mode === "TRIANGLES") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12103
        ctxMode = curContext.TRIANGLES;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12104
      } else if(mode === "TRIANGLE_FAN") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12105
        ctxMode = curContext.TRIANGLE_FAN;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12106
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12107
        ctxMode = curContext.TRIANGLE_STRIP;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12108
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12109
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12110
      var view = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12111
      view.scale(1, -1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12112
      view.apply(modelView.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12113
      view.transpose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12114
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12115
      curContext.useProgram( programObject3D );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12116
      uniformMatrix("model3d", programObject3D, "model", false,  [1,0,0,0,  0,1,0,0,   0,0,1,0,   0,0,0,1] );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12117
      uniformMatrix("view3d", programObject3D, "view", false, view.array() );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12118
      curContext.enable( curContext.POLYGON_OFFSET_FILL );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12119
      curContext.polygonOffset( 1, 1 );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12120
      uniformf("color3d", programObject3D, "color", [-1,0,0,0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12121
      vertexAttribPointer("vertex3d", programObject3D, "Vertex", 3, fillBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12122
      curContext.bufferData(curContext.ARRAY_BUFFER, new Float32Array(vArray), curContext.STREAM_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12123
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12124
      // if we are using a texture and a tint, then overwrite the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12125
      // contents of the color buffer with the current tint
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12126
      if (usingTexture && curTint !== null){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12127
        curTint3d(cArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12128
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12129
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12130
      vertexAttribPointer("aColor3d", programObject3D, "aColor", 4, fillColorBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12131
      curContext.bufferData(curContext.ARRAY_BUFFER, new Float32Array(cArray), curContext.STREAM_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12132
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12133
      // No support for lights....yet
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12134
      disableVertexAttribPointer("normal3d", programObject3D, "Normal");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12135
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12136
      if (usingTexture) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12137
        uniformi("usingTexture3d", programObject3D, "usingTexture", usingTexture);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12138
        vertexAttribPointer("aTexture3d", programObject3D, "aTexture", 2, shapeTexVBO);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12139
        curContext.bufferData(curContext.ARRAY_BUFFER, new Float32Array(tArray), curContext.STREAM_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12140
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12141
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12142
      curContext.drawArrays( ctxMode, 0, vArray.length/3 );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12143
      curContext.disable( curContext.POLYGON_OFFSET_FILL );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12144
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12145
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12146
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12147
     * this series of three operations is used a lot in Drawing2D.prototype.endShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12148
     * and has been split off as its own function, to tighten the code and allow for
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12149
     * fewer bugs.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12150
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12151
    function fillStrokeClose() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12152
      executeContextFill();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12153
      executeContextStroke();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12154
      curContext.closePath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12155
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12156
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12157
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12158
     * The endShape() function is the companion to beginShape() and may only be called after beginShape().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12159
     * When endshape() is called, all of image data defined since the previous call to beginShape() is written
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12160
     * into the image buffer.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12161
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12162
     * @param {int} MODE Use CLOSE to close the shape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12163
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12164
     * @see beginShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12165
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12166
    Drawing2D.prototype.endShape = function(mode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12167
      // Duplicated in Drawing3D; too many variables used
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12168
      if (vertArray.length === 0) { return; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12169
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12170
      var closeShape = mode === PConstants.CLOSE;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12171
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12172
      // if the shape is closed, the first element is also the last element
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12173
      if (closeShape) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12174
        vertArray.push(vertArray[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12175
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12176
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12177
      var lineVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12178
      var fillVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12179
      var colorVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12180
      var strokeVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12181
      var texVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12182
      var cachedVertArray;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12183
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12184
      firstVert = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12185
      var i, j, k;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12186
      var vertArrayLength = vertArray.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12187
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12188
      for (i = 0; i < vertArrayLength; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12189
        cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12190
        for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12191
          fillVertArray.push(cachedVertArray[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12192
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12193
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12194
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12195
      // 5,6,7,8
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12196
      // R,G,B,A - fill colour
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12197
      for (i = 0; i < vertArrayLength; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12198
        cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12199
        for (j = 5; j < 9; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12200
          colorVertArray.push(cachedVertArray[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12201
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12202
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12203
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12204
      // 9,10,11,12
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12205
      // R, G, B, A - stroke colour
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12206
      for (i = 0; i < vertArrayLength; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12207
        cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12208
        for (j = 9; j < 13; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12209
          strokeVertArray.push(cachedVertArray[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12210
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12211
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12212
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12213
      // texture u,v
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12214
      for (i = 0; i < vertArrayLength; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12215
        cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12216
        texVertArray.push(cachedVertArray[3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12217
        texVertArray.push(cachedVertArray[4]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12218
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12219
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12220
      // curveVertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12221
      if ( isCurve && (curShape === PConstants.POLYGON || curShape === undef) ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12222
        if (vertArrayLength > 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12223
          var b = [],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12224
              s = 1 - curTightness;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12225
          curContext.beginPath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12226
          curContext.moveTo(vertArray[1][0], vertArray[1][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12227
            /*
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12228
            * Matrix to convert from Catmull-Rom to cubic Bezier
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12229
            * where t = curTightness
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12230
            * |0         1          0         0       |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12231
            * |(t-1)/6   1          (1-t)/6   0       |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12232
            * |0         (1-t)/6    1         (t-1)/6 |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12233
            * |0         0          0         0       |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12234
            */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12235
          for (i = 1; (i+2) < vertArrayLength; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12236
            cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12237
            b[0] = [cachedVertArray[0], cachedVertArray[1]];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12238
            b[1] = [cachedVertArray[0] + (s * vertArray[i+1][0] - s * vertArray[i-1][0]) / 6,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12239
                   cachedVertArray[1] + (s * vertArray[i+1][1] - s * vertArray[i-1][1]) / 6];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12240
            b[2] = [vertArray[i+1][0] + (s * vertArray[i][0] - s * vertArray[i+2][0]) / 6,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12241
                   vertArray[i+1][1] + (s * vertArray[i][1] - s * vertArray[i+2][1]) / 6];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12242
            b[3] = [vertArray[i+1][0], vertArray[i+1][1]];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12243
            curContext.bezierCurveTo(b[1][0], b[1][1], b[2][0], b[2][1], b[3][0], b[3][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12244
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12245
          fillStrokeClose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12246
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12247
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12248
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12249
      // bezierVertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12250
      else if ( isBezier && (curShape === PConstants.POLYGON || curShape === undef) ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12251
        curContext.beginPath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12252
        for (i = 0; i < vertArrayLength; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12253
          cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12254
          if (vertArray[i]["isVert"]) { //if it is a vertex move to the position
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12255
            if (vertArray[i]["moveTo"]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12256
              curContext.moveTo(cachedVertArray[0], cachedVertArray[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12257
            } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12258
              curContext.lineTo(cachedVertArray[0], cachedVertArray[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12259
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12260
          } else { //otherwise continue drawing bezier
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12261
            curContext.bezierCurveTo(vertArray[i][0], vertArray[i][1], vertArray[i][2], vertArray[i][3], vertArray[i][4], vertArray[i][5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12262
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12263
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12264
        fillStrokeClose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12265
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12266
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12267
      // render the vertices provided
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12268
      else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12269
        if (curShape === PConstants.POINTS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12270
          for (i = 0; i < vertArrayLength; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12271
            cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12272
            if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12273
              p.stroke(cachedVertArray[6]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12274
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12275
            p.point(cachedVertArray[0], cachedVertArray[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12276
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12277
        } else if (curShape === PConstants.LINES) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12278
          for (i = 0; (i + 1) < vertArrayLength; i+=2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12279
            cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12280
            if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12281
              p.stroke(vertArray[i+1][6]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12282
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12283
            p.line(cachedVertArray[0], cachedVertArray[1], vertArray[i+1][0], vertArray[i+1][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12284
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12285
        } else if (curShape === PConstants.TRIANGLES) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12286
          for (i = 0; (i + 2) < vertArrayLength; i+=3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12287
            cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12288
            curContext.beginPath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12289
            curContext.moveTo(cachedVertArray[0], cachedVertArray[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12290
            curContext.lineTo(vertArray[i+1][0], vertArray[i+1][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12291
            curContext.lineTo(vertArray[i+2][0], vertArray[i+2][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12292
            curContext.lineTo(cachedVertArray[0], cachedVertArray[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12293
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12294
            if (doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12295
              p.fill(vertArray[i+2][5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12296
              executeContextFill();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12297
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12298
            if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12299
              p.stroke(vertArray[i+2][6]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12300
              executeContextStroke();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12301
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12302
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12303
            curContext.closePath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12304
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12305
        } else if (curShape === PConstants.TRIANGLE_STRIP) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12306
          for (i = 0; (i+1) < vertArrayLength; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12307
            cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12308
            curContext.beginPath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12309
            curContext.moveTo(vertArray[i+1][0], vertArray[i+1][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12310
            curContext.lineTo(cachedVertArray[0], cachedVertArray[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12311
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12312
            if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12313
              p.stroke(vertArray[i+1][6]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12314
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12315
            if (doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12316
              p.fill(vertArray[i+1][5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12317
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12318
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12319
            if (i + 2 < vertArrayLength) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12320
              curContext.lineTo(vertArray[i+2][0], vertArray[i+2][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12321
              if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12322
                p.stroke(vertArray[i+2][6]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12323
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12324
              if (doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12325
                p.fill(vertArray[i+2][5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12326
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12327
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12328
            fillStrokeClose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12329
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12330
        } else if (curShape === PConstants.TRIANGLE_FAN) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12331
          if (vertArrayLength > 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12332
            curContext.beginPath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12333
            curContext.moveTo(vertArray[0][0], vertArray[0][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12334
            curContext.lineTo(vertArray[1][0], vertArray[1][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12335
            curContext.lineTo(vertArray[2][0], vertArray[2][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12336
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12337
            if (doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12338
              p.fill(vertArray[2][5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12339
              executeContextFill();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12340
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12341
            if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12342
              p.stroke(vertArray[2][6]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12343
              executeContextStroke();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12344
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12345
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12346
            curContext.closePath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12347
            for (i = 3; i < vertArrayLength; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12348
              cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12349
              curContext.beginPath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12350
              curContext.moveTo(vertArray[0][0], vertArray[0][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12351
              curContext.lineTo(vertArray[i-1][0], vertArray[i-1][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12352
              curContext.lineTo(cachedVertArray[0], cachedVertArray[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12353
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12354
              if (doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12355
                p.fill(cachedVertArray[5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12356
                executeContextFill();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12357
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12358
              if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12359
                p.stroke(cachedVertArray[6]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12360
                executeContextStroke();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12361
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12362
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12363
              curContext.closePath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12364
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12365
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12366
        } else if (curShape === PConstants.QUADS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12367
          for (i = 0; (i + 3) < vertArrayLength; i+=4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12368
            cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12369
            curContext.beginPath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12370
            curContext.moveTo(cachedVertArray[0], cachedVertArray[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12371
            for (j = 1; j < 4; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12372
              curContext.lineTo(vertArray[i+j][0], vertArray[i+j][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12373
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12374
            curContext.lineTo(cachedVertArray[0], cachedVertArray[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12375
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12376
            if (doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12377
              p.fill(vertArray[i+3][5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12378
              executeContextFill();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12379
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12380
            if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12381
              p.stroke(vertArray[i+3][6]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12382
              executeContextStroke();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12383
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12384
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12385
            curContext.closePath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12386
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12387
        } else if (curShape === PConstants.QUAD_STRIP) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12388
          if (vertArrayLength > 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12389
            for (i = 0; (i+1) < vertArrayLength; i+=2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12390
              cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12391
              curContext.beginPath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12392
              if (i+3 < vertArrayLength) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12393
                curContext.moveTo(vertArray[i+2][0], vertArray[i+2][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12394
                curContext.lineTo(cachedVertArray[0], cachedVertArray[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12395
                curContext.lineTo(vertArray[i+1][0], vertArray[i+1][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12396
                curContext.lineTo(vertArray[i+3][0], vertArray[i+3][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12397
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12398
                if (doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12399
                  p.fill(vertArray[i+3][5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12400
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12401
                if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12402
                  p.stroke(vertArray[i+3][6]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12403
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12404
              } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12405
                curContext.moveTo(cachedVertArray[0], cachedVertArray[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12406
                curContext.lineTo(vertArray[i+1][0], vertArray[i+1][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12407
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12408
              fillStrokeClose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12409
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12410
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12411
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12412
          curContext.beginPath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12413
          curContext.moveTo(vertArray[0][0], vertArray[0][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12414
          for (i = 1; i < vertArrayLength; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12415
            cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12416
            if (cachedVertArray["isVert"]) { //if it is a vertex move to the position
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12417
              if (cachedVertArray["moveTo"]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12418
                curContext.moveTo(cachedVertArray[0], cachedVertArray[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12419
              } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12420
                curContext.lineTo(cachedVertArray[0], cachedVertArray[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12421
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12422
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12423
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12424
          fillStrokeClose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12425
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12426
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12427
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12428
      // Reset some settings
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12429
      isCurve = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12430
      isBezier = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12431
      curveVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12432
      curveVertCount = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12433
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12434
      // If the shape is closed, the first element was added as last element.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12435
      // We must remove it again to prevent the list of vertices from growing
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12436
      // over successive calls to endShape(CLOSE)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12437
      if (closeShape) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12438
        vertArray.pop();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12439
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12440
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12441
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12442
    Drawing3D.prototype.endShape = function(mode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12443
      // Duplicated in Drawing3D; too many variables used
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12444
      if (vertArray.length === 0) { return; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12445
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12446
      var closeShape = mode === PConstants.CLOSE;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12447
      var lineVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12448
      var fillVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12449
      var colorVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12450
      var strokeVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12451
      var texVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12452
      var cachedVertArray;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12453
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12454
      firstVert = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12455
      var i, j, k;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12456
      var vertArrayLength = vertArray.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12457
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12458
      for (i = 0; i < vertArrayLength; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12459
        cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12460
        for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12461
          fillVertArray.push(cachedVertArray[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12462
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12463
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12464
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12465
      // 5,6,7,8
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12466
      // R,G,B,A - fill colour
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12467
      for (i = 0; i < vertArrayLength; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12468
        cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12469
        for (j = 5; j < 9; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12470
          colorVertArray.push(cachedVertArray[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12471
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12472
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12473
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12474
      // 9,10,11,12
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12475
      // R, G, B, A - stroke colour
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12476
      for (i = 0; i < vertArrayLength; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12477
        cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12478
        for (j = 9; j < 13; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12479
          strokeVertArray.push(cachedVertArray[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12480
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12481
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12482
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12483
      // texture u,v
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12484
      for (i = 0; i < vertArrayLength; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12485
        cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12486
        texVertArray.push(cachedVertArray[3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12487
        texVertArray.push(cachedVertArray[4]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12488
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12489
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12490
      // if shape is closed, push the first point into the last point (including colours)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12491
      if (closeShape) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12492
        fillVertArray.push(vertArray[0][0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12493
        fillVertArray.push(vertArray[0][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12494
        fillVertArray.push(vertArray[0][2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12495
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12496
        for (i = 5; i < 9; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12497
          colorVertArray.push(vertArray[0][i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12498
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12499
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12500
        for (i = 9; i < 13; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12501
          strokeVertArray.push(vertArray[0][i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12502
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12503
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12504
        texVertArray.push(vertArray[0][3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12505
        texVertArray.push(vertArray[0][4]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12506
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12507
      // End duplication
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12508
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12509
      // curveVertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12510
      if ( isCurve && (curShape === PConstants.POLYGON || curShape === undef) ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12511
        lineVertArray = fillVertArray;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12512
        if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12513
          line3D(lineVertArray, null, strokeVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12514
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12515
        if (doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12516
          fill3D(fillVertArray, null, colorVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12517
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12518
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12519
      // bezierVertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12520
      else if ( isBezier && (curShape === PConstants.POLYGON || curShape === undef) ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12521
        lineVertArray = fillVertArray;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12522
        lineVertArray.splice(lineVertArray.length - 3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12523
        strokeVertArray.splice(strokeVertArray.length - 4);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12524
        if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12525
          line3D(lineVertArray, null, strokeVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12526
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12527
        if (doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12528
          fill3D(fillVertArray, "TRIANGLES", colorVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12529
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12530
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12531
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12532
      // render the vertices provided
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12533
      else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12534
        if (curShape === PConstants.POINTS) {       // if POINTS was the specified parameter in beginShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12535
          for (i = 0; i < vertArrayLength; i++) {  // loop through and push the point location information to the array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12536
            cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12537
            for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12538
              lineVertArray.push(cachedVertArray[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12539
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12540
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12541
          point3D(lineVertArray, strokeVertArray);  // render function for points
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12542
        } else if (curShape === PConstants.LINES) { // if LINES was the specified parameter in beginShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12543
          for (i = 0; i < vertArrayLength; i++) {  // loop through and push the point location information to the array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12544
            cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12545
            for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12546
              lineVertArray.push(cachedVertArray[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12547
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12548
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12549
          for (i = 0; i < vertArrayLength; i++) {  // loop through and push the color information to the array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12550
            cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12551
            for (j = 5; j < 9; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12552
              colorVertArray.push(cachedVertArray[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12553
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12554
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12555
          line3D(lineVertArray, "LINES", strokeVertArray);  // render function for lines
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12556
        } else if (curShape === PConstants.TRIANGLES) {     // if TRIANGLES was the specified parameter in beginShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12557
          if (vertArrayLength > 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12558
            for (i = 0; (i+2) < vertArrayLength; i+=3) {   // loop through the array per triangle
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12559
              fillVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12560
              texVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12561
              lineVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12562
              colorVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12563
              strokeVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12564
              for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12565
                for (k = 0; k < 3; k++) {                   // loop through and push
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12566
                  lineVertArray.push(vertArray[i+j][k]);    // the line point location information
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12567
                  fillVertArray.push(vertArray[i+j][k]);    // and fill point location information
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12568
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12569
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12570
              for (j = 0; j < 3; j++) {                     // loop through and push the texture information
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12571
                for (k = 3; k < 5; k++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12572
                  texVertArray.push(vertArray[i+j][k]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12573
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12574
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12575
              for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12576
                for (k = 5; k < 9; k++) {                   // loop through and push
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12577
                  colorVertArray.push(vertArray[i+j][k]);   // the colour information
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12578
                  strokeVertArray.push(vertArray[i+j][k+4]);// and the stroke information
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12579
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12580
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12581
              if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12582
                line3D(lineVertArray, "LINE_LOOP", strokeVertArray );               // line render function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12583
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12584
              if (doFill || usingTexture) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12585
                fill3D(fillVertArray, "TRIANGLES", colorVertArray, texVertArray);   // fill shape render function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12586
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12587
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12588
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12589
        } else if (curShape === PConstants.TRIANGLE_STRIP) {    // if TRIANGLE_STRIP was the specified parameter in beginShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12590
          if (vertArrayLength > 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12591
            for (i = 0; (i+2) < vertArrayLength; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12592
              lineVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12593
              fillVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12594
              strokeVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12595
              colorVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12596
              texVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12597
              for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12598
                for (k = 0; k < 3; k++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12599
                  lineVertArray.push(vertArray[i+j][k]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12600
                  fillVertArray.push(vertArray[i+j][k]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12601
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12602
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12603
              for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12604
                for (k = 3; k < 5; k++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12605
                  texVertArray.push(vertArray[i+j][k]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12606
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12607
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12608
              for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12609
                for (k = 5; k < 9; k++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12610
                  strokeVertArray.push(vertArray[i+j][k+4]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12611
                  colorVertArray.push(vertArray[i+j][k]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12612
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12613
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12614
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12615
              if (doFill || usingTexture) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12616
                fill3D(fillVertArray, "TRIANGLE_STRIP", colorVertArray, texVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12617
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12618
              if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12619
                line3D(lineVertArray, "LINE_LOOP", strokeVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12620
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12621
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12622
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12623
        } else if (curShape === PConstants.TRIANGLE_FAN) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12624
          if (vertArrayLength > 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12625
            for (i = 0; i < 3; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12626
              cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12627
              for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12628
                lineVertArray.push(cachedVertArray[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12629
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12630
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12631
            for (i = 0; i < 3; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12632
              cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12633
              for (j = 9; j < 13; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12634
                strokeVertArray.push(cachedVertArray[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12635
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12636
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12637
            if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12638
              line3D(lineVertArray, "LINE_LOOP", strokeVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12639
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12640
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12641
            for (i = 2; (i+1) < vertArrayLength; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12642
              lineVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12643
              strokeVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12644
              lineVertArray.push(vertArray[0][0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12645
              lineVertArray.push(vertArray[0][1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12646
              lineVertArray.push(vertArray[0][2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12647
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12648
              strokeVertArray.push(vertArray[0][9]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12649
              strokeVertArray.push(vertArray[0][10]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12650
              strokeVertArray.push(vertArray[0][11]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12651
              strokeVertArray.push(vertArray[0][12]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12652
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12653
              for (j = 0; j < 2; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12654
                for (k = 0; k < 3; k++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12655
                  lineVertArray.push(vertArray[i+j][k]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12656
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12657
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12658
              for (j = 0; j < 2; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12659
                for (k = 9; k < 13; k++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12660
                  strokeVertArray.push(vertArray[i+j][k]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12661
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12662
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12663
              if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12664
                line3D(lineVertArray, "LINE_STRIP",strokeVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12665
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12666
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12667
            if (doFill || usingTexture) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12668
              fill3D(fillVertArray, "TRIANGLE_FAN", colorVertArray, texVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12669
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12670
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12671
        } else if (curShape === PConstants.QUADS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12672
          for (i = 0; (i + 3) < vertArrayLength; i+=4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12673
            lineVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12674
            for (j = 0; j < 4; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12675
              cachedVertArray = vertArray[i+j];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12676
              for (k = 0; k < 3; k++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12677
                lineVertArray.push(cachedVertArray[k]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12678
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12679
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12680
            if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12681
              line3D(lineVertArray, "LINE_LOOP",strokeVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12682
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12683
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12684
            if (doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12685
              fillVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12686
              colorVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12687
              texVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12688
              for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12689
                fillVertArray.push(vertArray[i][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12690
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12691
              for (j = 5; j < 9; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12692
                colorVertArray.push(vertArray[i][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12693
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12694
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12695
              for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12696
                fillVertArray.push(vertArray[i+1][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12697
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12698
              for (j = 5; j < 9; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12699
                colorVertArray.push(vertArray[i+1][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12700
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12701
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12702
              for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12703
                fillVertArray.push(vertArray[i+3][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12704
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12705
              for (j = 5; j < 9; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12706
                colorVertArray.push(vertArray[i+3][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12707
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12708
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12709
              for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12710
                fillVertArray.push(vertArray[i+2][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12711
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12712
              for (j = 5; j < 9; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12713
                colorVertArray.push(vertArray[i+2][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12714
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12715
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12716
              if (usingTexture) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12717
                texVertArray.push(vertArray[i+0][3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12718
                texVertArray.push(vertArray[i+0][4]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12719
                texVertArray.push(vertArray[i+1][3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12720
                texVertArray.push(vertArray[i+1][4]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12721
                texVertArray.push(vertArray[i+3][3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12722
                texVertArray.push(vertArray[i+3][4]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12723
                texVertArray.push(vertArray[i+2][3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12724
                texVertArray.push(vertArray[i+2][4]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12725
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12726
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12727
              fill3D(fillVertArray, "TRIANGLE_STRIP", colorVertArray, texVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12728
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12729
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12730
        } else if (curShape === PConstants.QUAD_STRIP) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12731
          var tempArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12732
          if (vertArrayLength > 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12733
            for (i = 0; i < 2; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12734
              cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12735
              for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12736
                lineVertArray.push(cachedVertArray[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12737
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12738
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12739
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12740
            for (i = 0; i < 2; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12741
              cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12742
              for (j = 9; j < 13; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12743
                strokeVertArray.push(cachedVertArray[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12744
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12745
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12746
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12747
            line3D(lineVertArray, "LINE_STRIP", strokeVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12748
            if (vertArrayLength > 4 && vertArrayLength % 2 > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12749
              tempArray = fillVertArray.splice(fillVertArray.length - 3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12750
              vertArray.pop();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12751
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12752
            for (i = 0; (i+3) < vertArrayLength; i+=2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12753
              lineVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12754
              strokeVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12755
              for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12756
                lineVertArray.push(vertArray[i+1][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12757
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12758
              for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12759
                lineVertArray.push(vertArray[i+3][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12760
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12761
              for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12762
                lineVertArray.push(vertArray[i+2][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12763
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12764
              for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12765
                lineVertArray.push(vertArray[i+0][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12766
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12767
              for (j = 9; j < 13; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12768
                strokeVertArray.push(vertArray[i+1][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12769
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12770
              for (j = 9; j < 13; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12771
                strokeVertArray.push(vertArray[i+3][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12772
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12773
              for (j = 9; j < 13; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12774
                strokeVertArray.push(vertArray[i+2][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12775
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12776
              for (j = 9; j < 13; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12777
                strokeVertArray.push(vertArray[i+0][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12778
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12779
              if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12780
                line3D(lineVertArray, "LINE_STRIP", strokeVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12781
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12782
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12783
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12784
            if (doFill || usingTexture) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12785
              fill3D(fillVertArray, "TRIANGLE_LIST", colorVertArray, texVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12786
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12787
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12788
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12789
        // If the user didn't specify a type (LINES, TRIANGLES, etc)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12790
        else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12791
          // If only one vertex was specified, it must be a point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12792
          if (vertArrayLength === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12793
            for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12794
              lineVertArray.push(vertArray[0][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12795
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12796
            for (j = 9; j < 13; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12797
              strokeVertArray.push(vertArray[0][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12798
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12799
            point3D(lineVertArray,strokeVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12800
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12801
            for (i = 0; i < vertArrayLength; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12802
              cachedVertArray = vertArray[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12803
              for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12804
                lineVertArray.push(cachedVertArray[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12805
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12806
              for (j = 5; j < 9; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12807
                strokeVertArray.push(cachedVertArray[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12808
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12809
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12810
            if (doStroke && closeShape) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12811
              line3D(lineVertArray, "LINE_LOOP", strokeVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12812
            } else if (doStroke && !closeShape) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12813
              line3D(lineVertArray, "LINE_STRIP", strokeVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12814
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12815
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12816
            // fill is ignored if textures are used
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12817
            if (doFill || usingTexture) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12818
              fill3D(fillVertArray, "TRIANGLE_FAN", colorVertArray, texVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12819
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12820
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12821
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12822
        // everytime beginShape is followed by a call to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12823
        // texture(), texturing it turned back on. We do this to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12824
        // figure out if the shape should be textured or filled
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12825
        // with a color.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12826
        usingTexture = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12827
        curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12828
        uniformi("usingTexture3d", programObject3D, "usingTexture", usingTexture);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12829
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12830
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12831
      // Reset some settings
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12832
      isCurve = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12833
      isBezier = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12834
      curveVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12835
      curveVertCount = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12836
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12837
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12838
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12839
     * The function splineForward() setup forward-differencing matrix to be used for speedy
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12840
     * curve rendering. It's based on using a specific number
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12841
     * of curve segments and just doing incremental adds for each
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12842
     * vertex of the segment, rather than running the mathematically
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12843
     * expensive cubic equation. This function is used by both curveDetail and bezierDetail.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12844
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12845
     * @param {int} segments      number of curve segments to use when drawing
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12846
     * @param {PMatrix3D} matrix  target object for the new matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12847
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12848
    var splineForward = function(segments, matrix) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12849
      var f = 1.0 / segments;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12850
      var ff = f * f;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12851
      var fff = ff * f;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12852
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12853
      matrix.set(0, 0, 0, 1, fff, ff, f, 0, 6 * fff, 2 * ff, 0, 0, 6 * fff, 0, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12854
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12855
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12856
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12857
     * The curveInit() function set the number of segments to use when drawing a Catmull-Rom
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12858
     * curve, and setting the s parameter, which defines how tightly
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12859
     * the curve fits to each vertex. Catmull-Rom curves are actually
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12860
     * a subset of this curve type where the s is set to zero.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12861
     * This in an internal function used by curveDetail() and curveTightness().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12862
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12863
    var curveInit = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12864
      // allocate only if/when used to save startup time
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12865
      if (!curveDrawMatrix) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12866
        curveBasisMatrix = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12867
        curveDrawMatrix = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12868
        curveInited = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12869
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12870
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12871
      var s = curTightness;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12872
      curveBasisMatrix.set((s - 1) / 2, (s + 3) / 2, (-3 - s) / 2, (1 - s) / 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12873
                           (1 - s), (-5 - s) / 2, (s + 2), (s - 1) / 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12874
                           (s - 1) / 2, 0, (1 - s) / 2, 0, 0, 1, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12875
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12876
      splineForward(curveDet, curveDrawMatrix);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12877
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12878
      if (!bezierBasisInverse) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12879
        //bezierBasisInverse = bezierBasisMatrix.get();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12880
        //bezierBasisInverse.invert();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12881
        curveToBezierMatrix = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12882
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12883
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12884
      // TODO only needed for PGraphicsJava2D? if so, move it there
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12885
      // actually, it's generally useful for other renderers, so keep it
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12886
      // or hide the implementation elsewhere.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12887
      curveToBezierMatrix.set(curveBasisMatrix);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12888
      curveToBezierMatrix.preApply(bezierBasisInverse);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12889
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12890
      // multiply the basis and forward diff matrices together
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12891
      // saves much time since this needn't be done for each curve
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12892
      curveDrawMatrix.apply(curveBasisMatrix);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12893
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12894
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12895
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12896
     * Specifies vertex coordinates for Bezier curves. Each call to <b>bezierVertex()</b> defines the position of two control
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12897
     * points and one anchor point of a Bezier curve, adding a new segment to a line or shape. The first time
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12898
     * <b>bezierVertex()</b> is used within a <b>beginShape()</b> call, it must be prefaced with a call to <b>vertex()</b>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12899
     * to set the first anchor point. This function must be used between <b>beginShape()</b> and <b>endShape()</b> and only
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12900
     * when there is no MODE parameter specified to <b>beginShape()</b>. Using the 3D version of requires rendering with P3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12901
     * or OPENGL (see the Environment reference for more information). <br /> <br /> <b>NOTE: </b> Fill does not work properly yet.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12902
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12903
     * @param {float | int} cx1 The x-coordinate of 1st control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12904
     * @param {float | int} cy1 The y-coordinate of 1st control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12905
     * @param {float | int} cz1 The z-coordinate of 1st control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12906
     * @param {float | int} cx2 The x-coordinate of 2nd control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12907
     * @param {float | int} cy2 The y-coordinate of 2nd control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12908
     * @param {float | int} cz2 The z-coordinate of 2nd control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12909
     * @param {float | int} x   The x-coordinate of the anchor point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12910
     * @param {float | int} y   The y-coordinate of the anchor point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12911
     * @param {float | int} z   The z-coordinate of the anchor point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12912
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12913
     * @see curveVertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12914
     * @see vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12915
     * @see bezier
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12916
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12917
    Drawing2D.prototype.bezierVertex = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12918
      isBezier = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12919
      var vert = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12920
      if (firstVert) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12921
        throw ("vertex() must be used at least once before calling bezierVertex()");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12922
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12923
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12924
      for (var i = 0; i < arguments.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12925
        vert[i] = arguments[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12926
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12927
      vertArray.push(vert);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12928
      vertArray[vertArray.length -1]["isVert"] = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12929
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12930
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12931
    Drawing3D.prototype.bezierVertex = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12932
      isBezier = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12933
      var vert = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12934
      if (firstVert) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12935
        throw ("vertex() must be used at least once before calling bezierVertex()");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12936
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12937
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12938
      if (arguments.length === 9) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12939
        if (bezierDrawMatrix === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12940
          bezierDrawMatrix = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12941
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12942
        // setup matrix for forward differencing to speed up drawing
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12943
        var lastPoint = vertArray.length - 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12944
        splineForward( bezDetail, bezierDrawMatrix );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12945
        bezierDrawMatrix.apply( bezierBasisMatrix );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12946
        var draw = bezierDrawMatrix.array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12947
        var x1 = vertArray[lastPoint][0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12948
            y1 = vertArray[lastPoint][1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12949
            z1 = vertArray[lastPoint][2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12950
        var xplot1 = draw[4] * x1 + draw[5] * arguments[0] + draw[6] * arguments[3] + draw[7] * arguments[6];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12951
        var xplot2 = draw[8] * x1 + draw[9] * arguments[0] + draw[10]* arguments[3] + draw[11]* arguments[6];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12952
        var xplot3 = draw[12]* x1 + draw[13]* arguments[0] + draw[14]* arguments[3] + draw[15]* arguments[6];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12953
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12954
        var yplot1 = draw[4] * y1 + draw[5] * arguments[1] + draw[6] * arguments[4] + draw[7] * arguments[7];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12955
        var yplot2 = draw[8] * y1 + draw[9] * arguments[1] + draw[10]* arguments[4] + draw[11]* arguments[7];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12956
        var yplot3 = draw[12]* y1 + draw[13]* arguments[1] + draw[14]* arguments[4] + draw[15]* arguments[7];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12957
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12958
        var zplot1 = draw[4] * z1 + draw[5] * arguments[2] + draw[6] * arguments[5] + draw[7] * arguments[8];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12959
        var zplot2 = draw[8] * z1 + draw[9] * arguments[2] + draw[10]* arguments[5] + draw[11]* arguments[8];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12960
        var zplot3 = draw[12]* z1 + draw[13]* arguments[2] + draw[14]* arguments[5] + draw[15]* arguments[8];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12961
        for (var j = 0; j < bezDetail; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12962
          x1 += xplot1; xplot1 += xplot2; xplot2 += xplot3;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12963
          y1 += yplot1; yplot1 += yplot2; yplot2 += yplot3;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12964
          z1 += zplot1; zplot1 += zplot2; zplot2 += zplot3;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12965
          p.vertex(x1, y1, z1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12966
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12967
        p.vertex(arguments[6], arguments[7], arguments[8]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12968
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12969
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12970
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12971
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12972
     * Sets a texture to be applied to vertex points. The <b>texture()</b> function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12973
     * must be called between <b>beginShape()</b> and <b>endShape()</b> and before
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12974
     * any calls to vertex().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12975
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12976
     * When textures are in use, the fill color is ignored. Instead, use tint() to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12977
     * specify the color of the texture as it is applied to the shape.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12978
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12979
     * @param {PImage} pimage the texture to apply
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12980
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12981
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12982
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12983
     * @see textureMode
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12984
     * @see beginShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12985
     * @see endShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12986
     * @see vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12987
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12988
    p.texture = function(pimage) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12989
      var curContext = drawing.$ensureContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12990
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12991
      if (pimage.__texture) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12992
        curContext.bindTexture(curContext.TEXTURE_2D, pimage.__texture);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12993
      } else if (pimage.localName === "canvas") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12994
        curContext.bindTexture(curContext.TEXTURE_2D, canTex);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12995
        curContext.texImage2D(curContext.TEXTURE_2D, 0, curContext.RGBA, curContext.RGBA, curContext.UNSIGNED_BYTE, pimage);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12996
        curContext.texParameteri(curContext.TEXTURE_2D, curContext.TEXTURE_MAG_FILTER, curContext.LINEAR);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12997
        curContext.texParameteri(curContext.TEXTURE_2D, curContext.TEXTURE_MIN_FILTER, curContext.LINEAR);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12998
        curContext.generateMipmap(curContext.TEXTURE_2D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 12999
        curTexture.width = pimage.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13000
        curTexture.height = pimage.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13001
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13002
        var texture = curContext.createTexture(),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13003
            cvs = document.createElement('canvas'),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13004
            cvsTextureCtx = cvs.getContext('2d'),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13005
            pot;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13006
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13007
        // WebGL requires power of two textures
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13008
        if (pimage.width & (pimage.width-1) === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13009
          cvs.width = pimage.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13010
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13011
          pot = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13012
          while (pot < pimage.width) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13013
            pot *= 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13014
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13015
          cvs.width = pot;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13016
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13017
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13018
        if (pimage.height & (pimage.height-1) === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13019
          cvs.height = pimage.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13020
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13021
          pot = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13022
          while (pot < pimage.height) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13023
            pot *= 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13024
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13025
          cvs.height = pot;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13026
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13027
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13028
        cvsTextureCtx.drawImage(pimage.sourceImg, 0, 0, pimage.width, pimage.height, 0, 0, cvs.width, cvs.height);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13029
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13030
        curContext.bindTexture(curContext.TEXTURE_2D, texture);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13031
        curContext.texParameteri(curContext.TEXTURE_2D, curContext.TEXTURE_MIN_FILTER, curContext.LINEAR_MIPMAP_LINEAR);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13032
        curContext.texParameteri(curContext.TEXTURE_2D, curContext.TEXTURE_MAG_FILTER, curContext.LINEAR);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13033
        curContext.texParameteri(curContext.TEXTURE_2D, curContext.TEXTURE_WRAP_T, curContext.CLAMP_TO_EDGE);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13034
        curContext.texParameteri(curContext.TEXTURE_2D, curContext.TEXTURE_WRAP_S, curContext.CLAMP_TO_EDGE);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13035
        curContext.texImage2D(curContext.TEXTURE_2D, 0, curContext.RGBA, curContext.RGBA, curContext.UNSIGNED_BYTE, cvs);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13036
        curContext.generateMipmap(curContext.TEXTURE_2D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13037
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13038
        pimage.__texture = texture;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13039
        curTexture.width = pimage.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13040
        curTexture.height = pimage.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13041
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13042
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13043
      usingTexture = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13044
      curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13045
      uniformi("usingTexture3d", programObject3D, "usingTexture", usingTexture);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13046
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13047
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13048
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13049
     * Sets the coordinate space for texture mapping. There are two options, IMAGE,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13050
     * which refers to the actual coordinates of the image, and NORMALIZED, which
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13051
     * refers to a normalized space of values ranging from 0 to 1. The default mode
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13052
     * is IMAGE. In IMAGE, if an image is 100 x 200 pixels, mapping the image onto
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13053
     * the entire size of a quad would require the points (0,0) (0,100) (100,200) (0,200).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13054
     * The same mapping in NORMAL_SPACE is (0,0) (0,1) (1,1) (0,1).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13055
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13056
     * @param MODE either IMAGE or NORMALIZED
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13057
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13058
     * @returns none
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13059
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13060
     * @see texture
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13061
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13062
    p.textureMode = function(mode){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13063
      curTextureMode = mode;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13064
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13065
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13066
     * The curveVertexSegment() function handle emitting a specific segment of Catmull-Rom curve. Internal helper function used by <b>curveVertex()</b>.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13067
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13068
    var curveVertexSegment = function(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13069
      var x0 = x2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13070
      var y0 = y2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13071
      var z0 = z2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13072
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13073
      var draw = curveDrawMatrix.array();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13074
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13075
      var xplot1 = draw[4] * x1 + draw[5] * x2 + draw[6] * x3 + draw[7] * x4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13076
      var xplot2 = draw[8] * x1 + draw[9] * x2 + draw[10] * x3 + draw[11] * x4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13077
      var xplot3 = draw[12] * x1 + draw[13] * x2 + draw[14] * x3 + draw[15] * x4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13078
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13079
      var yplot1 = draw[4] * y1 + draw[5] * y2 + draw[6] * y3 + draw[7] * y4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13080
      var yplot2 = draw[8] * y1 + draw[9] * y2 + draw[10] * y3 + draw[11] * y4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13081
      var yplot3 = draw[12] * y1 + draw[13] * y2 + draw[14] * y3 + draw[15] * y4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13082
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13083
      var zplot1 = draw[4] * z1 + draw[5] * z2 + draw[6] * z3 + draw[7] * z4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13084
      var zplot2 = draw[8] * z1 + draw[9] * z2 + draw[10] * z3 + draw[11] * z4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13085
      var zplot3 = draw[12] * z1 + draw[13] * z2 + draw[14] * z3 + draw[15] * z4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13086
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13087
      p.vertex(x0, y0, z0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13088
      for (var j = 0; j < curveDet; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13089
        x0 += xplot1; xplot1 += xplot2; xplot2 += xplot3;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13090
        y0 += yplot1; yplot1 += yplot2; yplot2 += yplot3;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13091
        z0 += zplot1; zplot1 += zplot2; zplot2 += zplot3;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13092
        p.vertex(x0, y0, z0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13093
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13094
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13095
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13096
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13097
     * Specifies vertex coordinates for curves. This function may only be used between <b>beginShape()</b> and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13098
     * <b>endShape()</b> and only when there is no MODE parameter specified to <b>beginShape()</b>. The first and last points
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13099
     * in a series of <b>curveVertex()</b> lines will be used to guide the beginning and end of a the curve. A minimum of four
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13100
     * points is required to draw a tiny curve between the second and third points. Adding a fifth point with
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13101
     * <b>curveVertex()</b> will draw the curve between the second, third, and fourth points. The <b>curveVertex()</b> function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13102
     * is an implementation of Catmull-Rom splines. Using the 3D version of requires rendering with P3D or OPENGL (see the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13103
     * Environment reference for more information). <br /> <br /><b>NOTE: </b> Fill does not work properly yet.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13104
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13105
     * @param {float | int} x The x-coordinate of the vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13106
     * @param {float | int} y The y-coordinate of the vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13107
     * @param {float | int} z The z-coordinate of the vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13108
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13109
     * @see curve
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13110
     * @see beginShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13111
     * @see endShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13112
     * @see vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13113
     * @see bezierVertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13114
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13115
    Drawing2D.prototype.curveVertex = function(x, y) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13116
      isCurve = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13117
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13118
      p.vertex(x, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13119
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13120
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13121
    Drawing3D.prototype.curveVertex = function(x, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13122
      isCurve = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13123
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13124
      if (!curveInited) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13125
        curveInit();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13126
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13127
      var vert = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13128
      vert[0] = x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13129
      vert[1] = y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13130
      vert[2] = z;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13131
      curveVertArray.push(vert);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13132
      curveVertCount++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13133
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13134
      if (curveVertCount > 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13135
        curveVertexSegment( curveVertArray[curveVertCount-4][0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13136
                            curveVertArray[curveVertCount-4][1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13137
                            curveVertArray[curveVertCount-4][2],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13138
                            curveVertArray[curveVertCount-3][0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13139
                            curveVertArray[curveVertCount-3][1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13140
                            curveVertArray[curveVertCount-3][2],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13141
                            curveVertArray[curveVertCount-2][0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13142
                            curveVertArray[curveVertCount-2][1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13143
                            curveVertArray[curveVertCount-2][2],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13144
                            curveVertArray[curveVertCount-1][0],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13145
                            curveVertArray[curveVertCount-1][1],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13146
                            curveVertArray[curveVertCount-1][2] );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13147
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13148
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13149
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13150
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13151
     * The curve() function draws a curved line on the screen. The first and second parameters
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13152
     * specify the beginning control point and the last two parameters specify
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13153
     * the ending control point. The middle parameters specify the start and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13154
     * stop of the curve. Longer curves can be created by putting a series of
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13155
     * <b>curve()</b> functions together or using <b>curveVertex()</b>.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13156
     * An additional function called <b>curveTightness()</b> provides control
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13157
     * for the visual quality of the curve. The <b>curve()</b> function is an
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13158
     * implementation of Catmull-Rom splines. Using the 3D version of requires
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13159
     * rendering with P3D or OPENGL (see the Environment reference for more
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13160
     * information).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13161
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13162
     * @param {int|float} x1 coordinates for the beginning control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13163
     * @param {int|float} y1 coordinates for the beginning control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13164
     * @param {int|float} z1 coordinates for the beginning control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13165
     * @param {int|float} x2 coordinates for the first point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13166
     * @param {int|float} y2 coordinates for the first point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13167
     * @param {int|float} z2 coordinates for the first point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13168
     * @param {int|float} x3 coordinates for the second point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13169
     * @param {int|float} y3 coordinates for the second point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13170
     * @param {int|float} z3 coordinates for the second point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13171
     * @param {int|float} x4 coordinates for the ending control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13172
     * @param {int|float} y4 coordinates for the ending control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13173
     * @param {int|float} z4 coordinates for the ending control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13174
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13175
     * @see #curveVertex()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13176
     * @see #curveTightness()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13177
     * @see #bezier()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13178
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13179
    Drawing2D.prototype.curve = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13180
      if (arguments.length === 8) { // curve(x1, y1, x2, y2, x3, y3, x4, y4)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13181
        p.beginShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13182
        p.curveVertex(arguments[0], arguments[1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13183
        p.curveVertex(arguments[2], arguments[3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13184
        p.curveVertex(arguments[4], arguments[5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13185
        p.curveVertex(arguments[6], arguments[7]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13186
        p.endShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13187
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13188
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13189
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13190
    Drawing3D.prototype.curve = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13191
      if (arguments.length === 12) { // curve( x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13192
        p.beginShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13193
        p.curveVertex(arguments[0], arguments[1], arguments[2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13194
        p.curveVertex(arguments[3], arguments[4], arguments[5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13195
        p.curveVertex(arguments[6], arguments[7], arguments[8]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13196
        p.curveVertex(arguments[9], arguments[10], arguments[11]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13197
        p.endShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13198
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13199
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13200
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13201
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13202
     * The curveTightness() function modifies the quality of forms created with <b>curve()</b> and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13203
     * <b>curveVertex()</b>. The parameter <b>squishy</b> determines how the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13204
     * curve fits to the vertex points. The value 0.0 is the default value for
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13205
     * <b>squishy</b> (this value defines the curves to be Catmull-Rom splines)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13206
     * and the value 1.0 connects all the points with straight lines.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13207
     * Values within the range -5.0 and 5.0 will deform the curves but
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13208
     * will leave them recognizable and as values increase in magnitude,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13209
     * they will continue to deform.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13210
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13211
     * @param {float} tightness amount of deformation from the original vertices
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13212
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13213
     * @see #curve()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13214
     * @see #curveVertex()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13215
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13216
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13217
    p.curveTightness = function(tightness) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13218
      curTightness = tightness;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13219
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13220
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13221
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13222
     * The curveDetail() function sets the resolution at which curves display. The default value is 20.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13223
     * This function is only useful when using the P3D or OPENGL renderer.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13224
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13225
     * @param {int} detail resolution of the curves
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13226
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13227
     * @see curve()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13228
     * @see curveVertex()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13229
     * @see curveTightness()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13230
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13231
    p.curveDetail = function(detail) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13232
      curveDet = detail;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13233
      curveInit();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13234
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13235
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13236
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13237
    * Modifies the location from which rectangles draw. The default mode is rectMode(CORNER), which
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13238
    * specifies the location to be the upper left corner of the shape and uses the third and fourth
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13239
    * parameters of rect() to specify the width and height. The syntax rectMode(CORNERS) uses the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13240
    * first and second parameters of rect() to set the location of one corner and uses the third and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13241
    * fourth parameters to set the opposite corner. The syntax rectMode(CENTER) draws the image from
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13242
    * its center point and uses the third and forth parameters of rect() to specify the image's width
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13243
    * and height. The syntax rectMode(RADIUS) draws the image from its center point and uses the third
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13244
    * and forth parameters of rect()  to specify half of the image's width and height. The parameter must
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13245
    * be written in ALL CAPS because Processing is a case sensitive language. Note: In version 125, the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13246
    * mode named CENTER_RADIUS was shortened to RADIUS.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13247
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13248
    * @param {MODE} MODE      Either CORNER, CORNERS, CENTER, or RADIUS
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13249
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13250
    * @see rect
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13251
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13252
    p.rectMode = function(aRectMode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13253
      curRectMode = aRectMode;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13254
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13255
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13256
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13257
    * Modifies the location from which images draw. The default mode is imageMode(CORNER), which specifies
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13258
    * the location to be the upper left corner and uses the fourth and fifth parameters of image() to set
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13259
    * the image's width and height. The syntax imageMode(CORNERS) uses the second and third parameters of
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13260
    * image() to set the location of one corner of the image and uses the fourth and fifth parameters to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13261
    * set the opposite corner. Use imageMode(CENTER) to draw images centered at the given x and y position.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13262
    * The parameter to imageMode() must be written in ALL CAPS because Processing is a case sensitive language.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13263
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13264
    * @param {MODE} MODE      Either CORNER, CORNERS, or CENTER
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13265
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13266
    * @see loadImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13267
    * @see PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13268
    * @see image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13269
    * @see background
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13270
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13271
    p.imageMode = function(mode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13272
      switch (mode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13273
      case PConstants.CORNER:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13274
        imageModeConvert = imageModeCorner;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13275
        break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13276
      case PConstants.CORNERS:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13277
        imageModeConvert = imageModeCorners;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13278
        break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13279
      case PConstants.CENTER:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13280
        imageModeConvert = imageModeCenter;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13281
        break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13282
      default:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13283
        throw "Invalid imageMode";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13284
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13285
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13286
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13287
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13288
    * The origin of the ellipse is modified by the ellipseMode() function. The default configuration is
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13289
    * ellipseMode(CENTER), which specifies the location of the ellipse as the center of the shape. The RADIUS
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13290
    * mode is the same, but the width and height parameters to ellipse()  specify the radius of the ellipse,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13291
    * rather than the diameter. The CORNER mode draws the shape from the upper-left corner of its bounding box.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13292
    * The CORNERS mode uses the four parameters to ellipse() to set two opposing corners of the ellipse's bounding
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13293
    * box. The parameter must be written in "ALL CAPS" because Processing is a case sensitive language.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13294
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13295
    * @param {MODE} MODE      Either CENTER, RADIUS, CORNER, or CORNERS.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13296
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13297
    * @see ellipse
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13298
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13299
    p.ellipseMode = function(aEllipseMode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13300
      curEllipseMode = aEllipseMode;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13301
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13302
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13303
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13304
     * The arc() function draws an arc in the display window.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13305
     * Arcs are drawn along the outer edge of an ellipse defined by the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13306
     * <b>x</b>, <b>y</b>, <b>width</b> and <b>height</b> parameters.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13307
     * The origin or the arc's ellipse may be changed with the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13308
     * <b>ellipseMode()</b> function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13309
     * The <b>start</b> and <b>stop</b> parameters specify the angles
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13310
     * at which to draw the arc.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13311
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13312
     * @param {float} a       x-coordinate of the arc's ellipse
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13313
     * @param {float} b       y-coordinate of the arc's ellipse
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13314
     * @param {float} c       width of the arc's ellipse
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13315
     * @param {float} d       height of the arc's ellipse
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13316
     * @param {float} start   angle to start the arc, specified in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13317
     * @param {float} stop    angle to stop the arc, specified in radians
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13318
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13319
     * @see #ellipseMode()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13320
     * @see #ellipse()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13321
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13322
    p.arc = function(x, y, width, height, start, stop) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13323
      if (width <= 0 || stop < start) { return; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13324
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13325
      if (curEllipseMode === PConstants.CORNERS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13326
        width = width - x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13327
        height = height - y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13328
      } else if (curEllipseMode === PConstants.RADIUS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13329
        x = x - width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13330
        y = y - height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13331
        width = width * 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13332
        height = height * 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13333
      } else if (curEllipseMode === PConstants.CENTER) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13334
        x = x - width/2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13335
        y = y - height/2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13336
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13337
      // make sure that we're starting at a useful point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13338
      while (start < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13339
        start += PConstants.TWO_PI;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13340
        stop += PConstants.TWO_PI;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13341
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13342
      if (stop - start > PConstants.TWO_PI) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13343
        start = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13344
        stop = PConstants.TWO_PI;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13345
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13346
      var hr = width / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13347
      var vr = height / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13348
      var centerX = x + hr;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13349
      var centerY = y + vr;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13350
      var startLUT = 0 | (-0.5 + start * p.RAD_TO_DEG * 2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13351
      var stopLUT  = 0 | (0.5 + stop * p.RAD_TO_DEG * 2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13352
      var i, j;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13353
      if (doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13354
        // shut off stroke for a minute
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13355
        var savedStroke = doStroke;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13356
        doStroke = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13357
        p.beginShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13358
        p.vertex(centerX, centerY);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13359
        for (i = startLUT; i <= stopLUT; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13360
          j = i % PConstants.SINCOS_LENGTH;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13361
          p.vertex(centerX + cosLUT[j] * hr, centerY + sinLUT[j] * vr);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13362
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13363
        p.endShape(PConstants.CLOSE);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13364
        doStroke = savedStroke;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13365
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13366
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13367
      if (doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13368
        // and doesn't include the first (center) vertex.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13369
        var savedFill = doFill;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13370
        doFill = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13371
        p.beginShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13372
        for (i = startLUT; i <= stopLUT; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13373
          j = i % PConstants.SINCOS_LENGTH;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13374
          p.vertex(centerX + cosLUT[j] * hr, centerY + sinLUT[j] * vr);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13375
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13376
        p.endShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13377
        doFill = savedFill;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13378
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13379
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13380
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13381
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13382
    * Draws a line (a direct path between two points) to the screen. The version of line() with four parameters
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13383
    * draws the line in 2D. To color a line, use the stroke() function. A line cannot be filled, therefore the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13384
    * fill()  method will not affect the color of a line. 2D lines are drawn with a width of one pixel by default,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13385
    * but this can be changed with the strokeWeight()  function. The version with six parameters allows the line
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13386
    * to be placed anywhere within XYZ space. Drawing this shape in 3D using the z parameter requires the P3D or
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13387
    * OPENGL parameter in combination with size.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13388
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13389
    * @param {int|float} x1       x-coordinate of the first point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13390
    * @param {int|float} y1       y-coordinate of the first point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13391
    * @param {int|float} z1       z-coordinate of the first point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13392
    * @param {int|float} x2       x-coordinate of the second point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13393
    * @param {int|float} y2       y-coordinate of the second point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13394
    * @param {int|float} z2       z-coordinate of the second point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13395
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13396
    * @see strokeWeight
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13397
    * @see strokeJoin
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13398
    * @see strokeCap
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13399
    * @see beginShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13400
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13401
    Drawing2D.prototype.line = function(x1, y1, x2, y2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13402
      if (!doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13403
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13404
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13405
      x1 = Math.round(x1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13406
      x2 = Math.round(x2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13407
      y1 = Math.round(y1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13408
      y2 = Math.round(y2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13409
      // A line is only defined if it has different start and end coordinates.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13410
      // If they are the same, we call point instead.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13411
      if (x1 === x2 && y1 === y2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13412
        p.point(x1, y1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13413
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13414
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13415
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13416
      var swap = undef,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13417
          lineCap = undef,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13418
          drawCrisp = true,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13419
          currentModelView = modelView.array(),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13420
          identityMatrix = [1, 0, 0, 0, 1, 0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13421
      // Test if any transformations have been applied to the sketch
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13422
      for (var i = 0; i < 6 && drawCrisp; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13423
        drawCrisp = currentModelView[i] === identityMatrix[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13424
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13425
      /* Draw crisp lines if the line is vertical or horizontal with the following method
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13426
       * If any transformations have been applied to the sketch, don't make the line crisp
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13427
       * If the line is directed up or to the left, reverse it by swapping x1/x2 or y1/y2
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13428
       * Make the line 1 pixel longer to work around cross-platform canvas implementations
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13429
       * If the lineWidth is odd, translate the line by 0.5 in the perpendicular direction
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13430
       * Even lineWidths do not need to be translated because the canvas will draw them on pixel boundaries
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13431
       * Change the cap to butt-end to work around cross-platform canvas implementations
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13432
       * Reverse the translate and lineCap canvas state changes after drawing the line
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13433
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13434
      if (drawCrisp) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13435
        if (x1 === x2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13436
          if (y1 > y2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13437
            swap = y1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13438
            y1 = y2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13439
            y2 = swap;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13440
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13441
          y2++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13442
          if (lineWidth % 2 === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13443
            curContext.translate(0.5, 0.0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13444
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13445
        } else if (y1 === y2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13446
          if (x1 > x2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13447
            swap = x1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13448
            x1 = x2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13449
            x2 = swap;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13450
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13451
          x2++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13452
          if (lineWidth % 2 === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13453
            curContext.translate(0.0, 0.5);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13454
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13455
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13456
        if (lineWidth === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13457
          lineCap = curContext.lineCap;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13458
          curContext.lineCap = 'butt';
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13459
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13460
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13461
      curContext.beginPath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13462
      curContext.moveTo(x1 || 0, y1 || 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13463
      curContext.lineTo(x2 || 0, y2 || 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13464
      executeContextStroke();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13465
      if (drawCrisp) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13466
        if (x1 === x2 && lineWidth % 2 === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13467
          curContext.translate(-0.5, 0.0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13468
        } else if (y1 === y2 && lineWidth % 2 === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13469
          curContext.translate(0.0, -0.5);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13470
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13471
        if (lineWidth === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13472
          curContext.lineCap = lineCap;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13473
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13474
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13475
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13476
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13477
    Drawing3D.prototype.line = function(x1, y1, z1, x2, y2, z2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13478
      if (y2 === undef || z2 === undef) { // 2D line called in 3D context
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13479
        z2 = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13480
        y2 = x2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13481
        x2 = z1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13482
        z1 = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13483
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13484
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13485
      // a line is only defined if it has different start and end coordinates.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13486
      // If they are the same, we call point instead.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13487
      if (x1===x2 && y1===y2 && z1===z2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13488
        p.point(x1,y1,z1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13489
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13490
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13491
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13492
      var lineVerts = [x1, y1, z1, x2, y2, z2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13493
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13494
      var view = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13495
      view.scale(1, -1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13496
      view.apply(modelView.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13497
      view.transpose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13498
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13499
      if (lineWidth > 0 && doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13500
        curContext.useProgram(programObject2D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13501
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13502
        uniformMatrix("model2d", programObject2D, "model", false, [1,0,0,0,  0,1,0,0,  0,0,1,0,  0,0,0,1]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13503
        uniformMatrix("view2d", programObject2D, "view", false, view.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13504
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13505
        uniformf("color2d", programObject2D, "color", strokeStyle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13506
        uniformi("picktype2d", programObject2D, "picktype", 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13507
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13508
        vertexAttribPointer("vertex2d", programObject2D, "Vertex", 3, lineBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13509
        disableVertexAttribPointer("aTextureCoord2d", programObject2D, "aTextureCoord");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13510
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13511
        curContext.bufferData(curContext.ARRAY_BUFFER, new Float32Array(lineVerts), curContext.STREAM_DRAW);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13512
        curContext.drawArrays(curContext.LINES, 0, 2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13513
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13514
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13515
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13516
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13517
     * Draws a Bezier curve on the screen. These curves are defined by a series of anchor and control points. The first
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13518
     * two parameters specify the first anchor point and the last two parameters specify the other anchor point. The
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13519
     * middle parameters specify the control points which define the shape of the curve. Bezier curves were developed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13520
     * by French engineer Pierre Bezier. Using the 3D version of requires rendering with P3D or OPENGL (see the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13521
     * Environment reference for more information).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13522
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13523
     * @param {int | float} x1,y1,z1    coordinates for the first anchor point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13524
     * @param {int | float} cx1,cy1,cz1 coordinates for the first control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13525
     * @param {int | float} cx2,cy2,cz2 coordinates for the second control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13526
     * @param {int | float} x2,y2,z2    coordinates for the second anchor point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13527
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13528
     * @see bezierVertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13529
     * @see curve
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13530
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13531
    Drawing2D.prototype.bezier = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13532
      if (arguments.length !== 8) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13533
        throw("You must use 8 parameters for bezier() in 2D mode");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13534
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13535
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13536
      p.beginShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13537
      p.vertex( arguments[0], arguments[1] );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13538
      p.bezierVertex( arguments[2], arguments[3],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13539
                      arguments[4], arguments[5],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13540
                      arguments[6], arguments[7] );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13541
      p.endShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13542
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13543
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13544
    Drawing3D.prototype.bezier = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13545
      if (arguments.length !== 12) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13546
        throw("You must use 12 parameters for bezier() in 3D mode");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13547
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13548
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13549
      p.beginShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13550
      p.vertex( arguments[0], arguments[1], arguments[2] );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13551
      p.bezierVertex( arguments[3], arguments[4], arguments[5],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13552
                      arguments[6], arguments[7], arguments[8],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13553
                      arguments[9], arguments[10], arguments[11] );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13554
      p.endShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13555
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13556
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13557
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13558
     * Sets the resolution at which Beziers display. The default value is 20. This function is only useful when using the P3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13559
     * or OPENGL renderer as the default (JAVA2D) renderer does not use this information.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13560
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13561
     * @param {int} detail resolution of the curves
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13562
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13563
     * @see curve
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13564
     * @see curveVertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13565
     * @see curveTightness
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13566
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13567
    p.bezierDetail = function( detail ){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13568
      bezDetail = detail;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13569
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13570
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13571
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13572
     * The bezierPoint() function evalutes quadratic bezier at point t for points a, b, c, d.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13573
     * The parameter t varies between 0 and 1. The a and d parameters are the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13574
     * on-curve points, b and c are the control points. To make a two-dimensional
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13575
     * curve, call this function once with the x coordinates and a second time
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13576
     * with the y coordinates to get the location of a bezier curve at t.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13577
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13578
     * @param {float} a   coordinate of first point on the curve
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13579
     * @param {float} b   coordinate of first control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13580
     * @param {float} c   coordinate of second control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13581
     * @param {float} d   coordinate of second point on the curve
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13582
     * @param {float} t   value between 0 and 1
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13583
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13584
     * @see #bezier()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13585
     * @see #bezierVertex()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13586
     * @see #curvePoint()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13587
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13588
    p.bezierPoint = function(a, b, c, d, t) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13589
      return (1 - t) * (1 - t) * (1 - t) * a + 3 * (1 - t) * (1 - t) * t * b + 3 * (1 - t) * t * t * c + t * t * t * d;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13590
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13591
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13592
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13593
     * The bezierTangent() function calculates the tangent of a point on a Bezier curve. There is a good
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13594
     * definition of "tangent" at Wikipedia: <a href="http://en.wikipedia.org/wiki/Tangent" target="new">http://en.wikipedia.org/wiki/Tangent</a>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13595
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13596
     * @param {float} a   coordinate of first point on the curve
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13597
     * @param {float} b   coordinate of first control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13598
     * @param {float} c   coordinate of second control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13599
     * @param {float} d   coordinate of second point on the curve
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13600
     * @param {float} t   value between 0 and 1
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13601
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13602
     * @see #bezier()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13603
     * @see #bezierVertex()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13604
     * @see #curvePoint()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13605
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13606
    p.bezierTangent = function(a, b, c, d, t) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13607
      return (3 * t * t * (-a + 3 * b - 3 * c + d) + 6 * t * (a - 2 * b + c) + 3 * (-a + b));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13608
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13609
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13610
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13611
     * The curvePoint() function evalutes the Catmull-Rom curve at point t for points a, b, c, d. The
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13612
     * parameter t varies between 0 and 1, a and d are points on the curve,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13613
     * and b and c are the control points. This can be done once with the x
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13614
     * coordinates and a second time with the y coordinates to get the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13615
     * location of a curve at t.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13616
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13617
     * @param {int|float} a   coordinate of first point on the curve
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13618
     * @param {int|float} b   coordinate of second point on the curve
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13619
     * @param {int|float} c   coordinate of third point on the curve
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13620
     * @param {int|float} d   coordinate of fourth point on the curve
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13621
     * @param {float} t       value between 0 and 1
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13622
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13623
     * @see #curve()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13624
     * @see #curveVertex()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13625
     * @see #bezierPoint()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13626
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13627
    p.curvePoint = function(a, b, c, d, t) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13628
      return 0.5 * ((2 * b) + (-a + c) * t + (2 * a - 5 * b + 4 * c - d) * t * t + (-a + 3 * b - 3 * c + d) * t * t * t);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13629
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13630
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13631
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13632
     * The curveTangent() function calculates the tangent of a point on a Catmull-Rom curve.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13633
     * There is a good definition of "tangent" at Wikipedia: <a href="http://en.wikipedia.org/wiki/Tangent" target="new">http://en.wikipedia.org/wiki/Tangent</a>.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13634
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13635
     * @param {int|float} a   coordinate of first point on the curve
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13636
     * @param {int|float} b   coordinate of first control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13637
     * @param {int|float} c   coordinate of second control point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13638
     * @param {int|float} d   coordinate of second point on the curve
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13639
     * @param {float} t       value between 0 and 1
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13640
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13641
     * @see #curve()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13642
     * @see #curveVertex()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13643
     * @see #curvePoint()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13644
     * @see #bezierTangent()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13645
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13646
    p.curveTangent = function(a, b, c, d, t) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13647
      return 0.5 * ((-a + c) + 2 * (2 * a - 5 * b + 4 * c - d) * t + 3 * (-a + 3 * b - 3 * c + d) * t * t);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13648
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13649
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13650
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13651
     * A triangle is a plane created by connecting three points. The first two arguments specify the first point,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13652
     * the middle two arguments specify the second point, and the last two arguments specify the third point.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13653
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13654
     * @param {int | float} x1 x-coordinate of the first point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13655
     * @param {int | float} y1 y-coordinate of the first point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13656
     * @param {int | float} x2 x-coordinate of the second point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13657
     * @param {int | float} y2 y-coordinate of the second point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13658
     * @param {int | float} x3 x-coordinate of the third point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13659
     * @param {int | float} y3 y-coordinate of the third point
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13660
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13661
    p.triangle = function(x1, y1, x2, y2, x3, y3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13662
      p.beginShape(PConstants.TRIANGLES);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13663
      p.vertex(x1, y1, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13664
      p.vertex(x2, y2, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13665
      p.vertex(x3, y3, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13666
      p.endShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13667
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13668
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13669
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13670
     * A quad is a quadrilateral, a four sided polygon. It is similar to a rectangle, but the angles between its
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13671
     * edges are not constrained to ninety degrees. The first pair of parameters (x1,y1) sets the first vertex
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13672
     * and the subsequent pairs should proceed clockwise or counter-clockwise around the defined shape.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13673
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13674
     * @param {float | int} x1 x-coordinate of the first corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13675
     * @param {float | int} y1 y-coordinate of the first corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13676
     * @param {float | int} x2 x-coordinate of the second corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13677
     * @param {float | int} y2 y-coordinate of the second corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13678
     * @param {float | int} x3 x-coordinate of the third corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13679
     * @param {float | int} y3 y-coordinate of the third corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13680
     * @param {float | int} x4 x-coordinate of the fourth corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13681
     * @param {float | int} y4 y-coordinate of the fourth corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13682
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13683
    p.quad = function(x1, y1, x2, y2, x3, y3, x4, y4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13684
      p.beginShape(PConstants.QUADS);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13685
      p.vertex(x1, y1, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13686
      p.vertex(x2, y2, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13687
      p.vertex(x3, y3, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13688
      p.vertex(x4, y4, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13689
      p.endShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13690
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13691
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13692
    var roundedRect$2d = function(x, y, width, height, tl, tr, br, bl) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13693
      if (bl === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13694
        tr = tl;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13695
        br = tl;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13696
        bl = tl;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13697
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13698
      var halfWidth = width / 2, 
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13699
          halfHeight = height / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13700
      if (tl > halfWidth || tl > halfHeight) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13701
        tl = Math.min(halfWidth, halfHeight);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13702
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13703
      if (tr > halfWidth || tr > halfHeight) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13704
        tr = Math.min(halfWidth, halfHeight);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13705
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13706
      if (br > halfWidth || br > halfHeight) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13707
        br = Math.min(halfWidth, halfHeight);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13708
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13709
      if (bl > halfWidth || bl > halfHeight) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13710
        bl = Math.min(halfWidth, halfHeight);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13711
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13712
      // Translate the stroke by (0.5, 0.5) to draw a crisp border
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13713
      if (!doFill || doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13714
        curContext.translate(0.5, 0.5);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13715
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13716
      curContext.beginPath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13717
      curContext.moveTo(x + tl, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13718
      curContext.lineTo(x + width - tr, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13719
      curContext.quadraticCurveTo(x + width, y, x + width, y + tr);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13720
      curContext.lineTo(x + width, y + height - br);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13721
      curContext.quadraticCurveTo(x + width, y + height, x + width - br, y + height);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13722
      curContext.lineTo(x + bl, y + height);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13723
      curContext.quadraticCurveTo(x, y + height, x, y + height - bl);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13724
      curContext.lineTo(x, y + tl);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13725
      curContext.quadraticCurveTo(x, y, x + tl, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13726
      if (!doFill || doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13727
        curContext.translate(-0.5, -0.5);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13728
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13729
      executeContextFill();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13730
      executeContextStroke();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13731
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13732
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13733
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13734
    * Draws a rectangle to the screen. A rectangle is a four-sided shape with every angle at ninety
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13735
    * degrees. The first two parameters set the location, the third sets the width, and the fourth
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13736
    * sets the height. The origin is changed with the rectMode() function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13737
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13738
    * @param {int|float} x        x-coordinate of the rectangle
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13739
    * @param {int|float} y        y-coordinate of the rectangle
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13740
    * @param {int|float} width    width of the rectangle
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13741
    * @param {int|float} height   height of the rectangle
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13742
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13743
    * @see rectMode
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13744
    * @see quad
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13745
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13746
    Drawing2D.prototype.rect = function(x, y, width, height, tl, tr, br, bl) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13747
      if (!width && !height) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13748
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13749
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13750
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13751
      if (curRectMode === PConstants.CORNERS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13752
        width -= x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13753
        height -= y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13754
      } else if (curRectMode === PConstants.RADIUS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13755
        width *= 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13756
        height *= 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13757
        x -= width / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13758
        y -= height / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13759
      } else if (curRectMode === PConstants.CENTER) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13760
        x -= width / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13761
        y -= height / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13762
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13763
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13764
      x = Math.round(x);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13765
      y = Math.round(y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13766
      width = Math.round(width);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13767
      height = Math.round(height);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13768
      if (tl !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13769
        roundedRect$2d(x, y, width, height, tl, tr, br, bl);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13770
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13771
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13772
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13773
      // Translate the line by (0.5, 0.5) to draw a crisp rectangle border
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13774
      if (doStroke && lineWidth % 2 === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13775
        curContext.translate(0.5, 0.5);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13776
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13777
      curContext.beginPath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13778
      curContext.rect(x, y, width, height);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13779
      executeContextFill();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13780
      executeContextStroke();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13781
      if (doStroke && lineWidth % 2 === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13782
        curContext.translate(-0.5, -0.5);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13783
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13784
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13785
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13786
    Drawing3D.prototype.rect = function(x, y, width, height, tl, tr, br, bl) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13787
      if (tl !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13788
        throw "rect() with rounded corners is not supported in 3D mode";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13789
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13790
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13791
      if (curRectMode === PConstants.CORNERS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13792
        width -= x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13793
        height -= y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13794
      } else if (curRectMode === PConstants.RADIUS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13795
        width *= 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13796
        height *= 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13797
        x -= width / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13798
        y -= height / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13799
      } else if (curRectMode === PConstants.CENTER) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13800
        x -= width / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13801
        y -= height / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13802
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13803
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13804
      // Modeling transformation
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13805
      var model = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13806
      model.translate(x, y, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13807
      model.scale(width, height, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13808
      model.transpose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13809
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13810
      // viewing transformation needs to have Y flipped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13811
      // becuase that's what Processing does.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13812
      var view = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13813
      view.scale(1, -1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13814
      view.apply(modelView.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13815
      view.transpose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13816
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13817
      if (lineWidth > 0 && doStroke) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13818
        curContext.useProgram(programObject2D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13819
        uniformMatrix("model2d", programObject2D, "model", false, model.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13820
        uniformMatrix("view2d", programObject2D, "view", false, view.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13821
        uniformf("color2d", programObject2D, "color", strokeStyle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13822
        uniformi("picktype2d", programObject2D, "picktype", 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13823
        vertexAttribPointer("vertex2d", programObject2D, "Vertex", 3, rectBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13824
        disableVertexAttribPointer("aTextureCoord2d", programObject2D, "aTextureCoord");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13825
        curContext.drawArrays(curContext.LINE_LOOP, 0, rectVerts.length / 3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13826
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13827
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13828
      if (doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13829
        curContext.useProgram(programObject3D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13830
        uniformMatrix("model3d", programObject3D, "model", false, model.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13831
        uniformMatrix("view3d", programObject3D, "view", false, view.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13832
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13833
        // fix stitching problems. (lines get occluded by triangles
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13834
        // since they share the same depth values). This is not entirely
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13835
        // working, but it's a start for drawing the outline. So
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13836
        // developers can start playing around with styles.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13837
        curContext.enable(curContext.POLYGON_OFFSET_FILL);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13838
        curContext.polygonOffset(1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13839
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13840
        uniformf("color3d", programObject3D, "color", fillStyle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13841
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13842
        if(lightCount > 0){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13843
          var v = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13844
          v.set(view);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13845
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13846
          var m = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13847
          m.set(model);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13848
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13849
          v.mult(m);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13850
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13851
          var normalMatrix = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13852
          normalMatrix.set(v);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13853
          normalMatrix.invert();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13854
          normalMatrix.transpose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13855
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13856
          uniformMatrix("normalTransform3d", programObject3D, "normalTransform", false, normalMatrix.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13857
          vertexAttribPointer("normal3d", programObject3D, "Normal", 3, rectNormBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13858
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13859
        else{
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13860
          disableVertexAttribPointer("normal3d", programObject3D, "Normal");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13861
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13862
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13863
        vertexAttribPointer("vertex3d", programObject3D, "Vertex", 3, rectBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13864
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13865
        curContext.drawArrays(curContext.TRIANGLE_FAN, 0, rectVerts.length / 3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13866
        curContext.disable(curContext.POLYGON_OFFSET_FILL);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13867
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13868
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13869
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13870
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13871
     * Draws an ellipse (oval) in the display window. An ellipse with an equal <b>width</b> and <b>height</b> is a circle.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13872
     * The first two parameters set the location, the third sets the width, and the fourth sets the height. The origin may be
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13873
     * changed with the <b>ellipseMode()</b> function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13874
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13875
     * @param {float|int} x      x-coordinate of the ellipse
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13876
     * @param {float|int} y      y-coordinate of the ellipse
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13877
     * @param {float|int} width  width of the ellipse
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13878
     * @param {float|int} height height of the ellipse
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13879
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13880
     * @see ellipseMode
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13881
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13882
    Drawing2D.prototype.ellipse = function(x, y, width, height) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13883
      x = x || 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13884
      y = y || 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13885
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13886
      if (width <= 0 && height <= 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13887
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13888
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13889
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13890
      if (curEllipseMode === PConstants.RADIUS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13891
        width *= 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13892
        height *= 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13893
      } else if (curEllipseMode === PConstants.CORNERS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13894
        width = width - x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13895
        height = height - y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13896
        x += width / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13897
        y += height / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13898
      } else if (curEllipseMode === PConstants.CORNER) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13899
        x += width / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13900
        y += height / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13901
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13902
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13903
      // Shortcut for drawing a 2D circle
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13904
      if (width === height) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13905
        curContext.beginPath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13906
        curContext.arc(x, y, width / 2, 0, PConstants.TWO_PI, false);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13907
        executeContextFill();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13908
        executeContextStroke();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13909
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13910
        var w = width / 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13911
            h = height / 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13912
            C = 0.5522847498307933,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13913
            c_x = C * w,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13914
            c_y = C * h;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13915
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13916
        p.beginShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13917
        p.vertex(x + w, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13918
        p.bezierVertex(x + w, y - c_y, x + c_x, y - h, x, y - h);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13919
        p.bezierVertex(x - c_x, y - h, x - w, y - c_y, x - w, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13920
        p.bezierVertex(x - w, y + c_y, x - c_x, y + h, x, y + h);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13921
        p.bezierVertex(x + c_x, y + h, x + w, y + c_y, x + w, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13922
        p.endShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13923
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13924
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13925
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13926
    Drawing3D.prototype.ellipse = function(x, y, width, height) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13927
      x = x || 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13928
      y = y || 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13929
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13930
      if (width <= 0 && height <= 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13931
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13932
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13933
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13934
      if (curEllipseMode === PConstants.RADIUS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13935
        width *= 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13936
        height *= 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13937
      } else if (curEllipseMode === PConstants.CORNERS) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13938
        width = width - x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13939
        height = height - y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13940
        x += width / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13941
        y += height / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13942
      } else if (curEllipseMode === PConstants.CORNER) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13943
        x += width / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13944
        y += height / 2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13945
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13946
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13947
      var w = width / 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13948
          h = height / 2,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13949
          C = 0.5522847498307933,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13950
          c_x = C * w,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13951
          c_y = C * h;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13952
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13953
      p.beginShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13954
      p.vertex(x + w, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13955
      p.bezierVertex(x + w, y - c_y, 0, x + c_x, y - h, 0, x, y - h, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13956
      p.bezierVertex(x - c_x, y - h, 0, x - w, y - c_y, 0, x - w, y, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13957
      p.bezierVertex(x - w, y + c_y, 0, x - c_x, y + h, 0, x, y + h, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13958
      p.bezierVertex(x + c_x, y + h, 0, x + w, y + c_y, 0, x + w, y, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13959
      p.endShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13960
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13961
      if (doFill) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13962
        //temporary workaround to not working fills for bezier -- will fix later
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13963
        var xAv = 0, yAv = 0, i, j;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13964
        for (i = 0; i < vertArray.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13965
          xAv += vertArray[i][0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13966
          yAv += vertArray[i][1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13967
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13968
        xAv /= vertArray.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13969
        yAv /= vertArray.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13970
        var vert = [],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13971
            fillVertArray = [],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13972
            colorVertArray = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13973
        vert[0] = xAv;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13974
        vert[1] = yAv;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13975
        vert[2] = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13976
        vert[3] = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13977
        vert[4] = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13978
        vert[5] = fillStyle[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13979
        vert[6] = fillStyle[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13980
        vert[7] = fillStyle[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13981
        vert[8] = fillStyle[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13982
        vert[9] = strokeStyle[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13983
        vert[10] = strokeStyle[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13984
        vert[11] = strokeStyle[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13985
        vert[12] = strokeStyle[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13986
        vert[13] = normalX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13987
        vert[14] = normalY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13988
        vert[15] = normalZ;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13989
        vertArray.unshift(vert);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13990
        for (i = 0; i < vertArray.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13991
          for (j = 0; j < 3; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13992
            fillVertArray.push(vertArray[i][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13993
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13994
          for (j = 5; j < 9; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13995
            colorVertArray.push(vertArray[i][j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13996
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13997
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13998
        fill3D(fillVertArray, "TRIANGLE_FAN", colorVertArray);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 13999
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14000
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14001
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14002
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14003
    * Sets the current normal vector. This is for drawing three dimensional shapes and surfaces and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14004
    * specifies a vector perpendicular to the surface of the shape which determines how lighting affects
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14005
    * it. Processing attempts to automatically assign normals to shapes, but since that's imperfect,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14006
    * this is a better option when you want more control. This function is identical to glNormal3f() in OpenGL.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14007
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14008
    * @param {float} nx       x direction
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14009
    * @param {float} ny       y direction
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14010
    * @param {float} nz       z direction
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14011
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14012
    * @see beginShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14013
    * @see endShape
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14014
    * @see lights
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14015
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14016
    p.normal = function(nx, ny, nz) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14017
      if (arguments.length !== 3 || !(typeof nx === "number" && typeof ny === "number" && typeof nz === "number")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14018
        throw "normal() requires three numeric arguments.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14019
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14020
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14021
      normalX = nx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14022
      normalY = ny;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14023
      normalZ = nz;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14024
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14025
      if (curShape !== 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14026
        if (normalMode === PConstants.NORMAL_MODE_AUTO) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14027
          normalMode = PConstants.NORMAL_MODE_SHAPE;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14028
        } else if (normalMode === PConstants.NORMAL_MODE_SHAPE) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14029
          normalMode = PConstants.NORMAL_MODE_VERTEX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14030
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14031
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14032
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14033
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14034
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14035
    // Raster drawing functions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14036
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14037
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14038
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14039
    * Saves an image from the display window. Images are saved in TIFF, TARGA, JPEG, and PNG format
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14040
    * depending on the extension within the filename  parameter. For example, "image.tif" will have
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14041
    * a TIFF image and "image.png" will save a PNG image. If no extension is included in the filename,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14042
    * the image will save in TIFF format and .tif will be added to the name. These files are saved to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14043
    * the sketch's folder, which may be opened by selecting "Show sketch folder" from the "Sketch" menu.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14044
    * It is not possible to use save() while running the program in a web browser.  All images saved
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14045
    * from the main drawing window will be opaque. To save images without a background, use createGraphics().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14046
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14047
    * @param {String} filename      any sequence of letters and numbers
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14048
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14049
    * @see saveFrame
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14050
    * @see createGraphics
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14051
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14052
    p.save = function(file, img) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14053
      // file is unused at the moment
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14054
      // may implement this differently in later release
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14055
      if (img !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14056
        return window.open(img.toDataURL(),"_blank");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14057
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14058
      return window.open(p.externals.canvas.toDataURL(),"_blank");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14059
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14060
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14061
    var saveNumber = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14062
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14063
    p.saveFrame = function(file) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14064
      if(file === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14065
        // use default name template if parameter is not specified
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14066
        file = "screen-####.png";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14067
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14068
      // Increment changeable part: screen-0000.png, screen-0001.png, ...
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14069
      var frameFilename = file.replace(/#+/, function(all) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14070
        var s = "" + (saveNumber++);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14071
        while(s.length < all.length) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14072
          s = "0" + s;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14073
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14074
        return s;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14075
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14076
      p.save(frameFilename);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14077
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14078
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14079
    var utilityContext2d = document.createElement("canvas").getContext("2d");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14080
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14081
    var canvasDataCache = [undef, undef, undef]; // we need three for now
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14082
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14083
    function getCanvasData(obj, w, h) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14084
      var canvasData = canvasDataCache.shift();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14085
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14086
      if (canvasData === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14087
        canvasData = {};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14088
        canvasData.canvas = document.createElement("canvas");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14089
        canvasData.context = canvasData.canvas.getContext('2d');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14090
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14091
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14092
      canvasDataCache.push(canvasData);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14093
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14094
      var canvas = canvasData.canvas, context = canvasData.context,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14095
          width = w || obj.width, height = h || obj.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14096
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14097
      canvas.width = width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14098
      canvas.height = height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14099
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14100
      if (!obj) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14101
        context.clearRect(0, 0, width, height);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14102
      } else if ("data" in obj) { // ImageData
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14103
        context.putImageData(obj, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14104
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14105
        context.clearRect(0, 0, width, height);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14106
        context.drawImage(obj, 0, 0, width, height);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14107
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14108
      return canvasData;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14109
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14110
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14111
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14112
     * Handle the sketch code for pixels[] and pixels.length
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14113
     * parser code converts pixels[] to getPixels()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14114
     * or setPixels(), .length becomes getLength()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14115
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14116
    function buildPixelsObject(pImage) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14117
      return {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14118
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14119
        getLength: (function(aImg) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14120
          return function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14121
            if (aImg.isRemote) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14122
              throw "Image is loaded remotely. Cannot get length.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14123
            } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14124
              return aImg.imageData.data.length ? aImg.imageData.data.length/4 : 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14125
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14126
          };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14127
        }(pImage)),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14128
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14129
        getPixel: (function(aImg) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14130
          return function(i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14131
            var offset = i*4,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14132
              data = aImg.imageData.data;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14133
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14134
            if (aImg.isRemote) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14135
              throw "Image is loaded remotely. Cannot get pixels.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14136
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14137
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14138
            return (data[offset+3] << 24) & PConstants.ALPHA_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14139
                   (data[offset] << 16) & PConstants.RED_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14140
                   (data[offset+1] << 8) & PConstants.GREEN_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14141
                   data[offset+2] & PConstants.BLUE_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14142
          };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14143
        }(pImage)),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14144
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14145
        setPixel: (function(aImg) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14146
          return function(i, c) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14147
            var offset = i*4,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14148
              data = aImg.imageData.data;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14149
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14150
            if (aImg.isRemote) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14151
              throw "Image is loaded remotely. Cannot set pixel.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14152
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14153
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14154
            data[offset+0] = (c & PConstants.RED_MASK) >>> 16;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14155
            data[offset+1] = (c & PConstants.GREEN_MASK) >>> 8;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14156
            data[offset+2] = (c & PConstants.BLUE_MASK);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14157
            data[offset+3] = (c & PConstants.ALPHA_MASK) >>> 24;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14158
            aImg.__isDirty = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14159
          };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14160
        }(pImage)),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14161
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14162
        toArray: (function(aImg) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14163
          return function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14164
            var arr = [],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14165
              data = aImg.imageData.data,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14166
              length = aImg.width * aImg.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14167
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14168
            if (aImg.isRemote) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14169
              throw "Image is loaded remotely. Cannot get pixels.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14170
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14171
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14172
            for (var i = 0, offset = 0; i < length; i++, offset += 4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14173
              arr.push( (data[offset+3] << 24) & PConstants.ALPHA_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14174
                        (data[offset] << 16) & PConstants.RED_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14175
                        (data[offset+1] << 8) & PConstants.GREEN_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14176
                        data[offset+2] & PConstants.BLUE_MASK );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14177
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14178
            return arr;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14179
          };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14180
        }(pImage)),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14181
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14182
        set: (function(aImg) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14183
          return function(arr) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14184
            var offset,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14185
              data,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14186
              c;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14187
            if (this.isRemote) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14188
              throw "Image is loaded remotely. Cannot set pixels.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14189
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14190
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14191
            data = aImg.imageData.data;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14192
            for (var i = 0, aL = arr.length; i < aL; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14193
              c = arr[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14194
              offset = i*4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14195
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14196
              data[offset+0] = (c & PConstants.RED_MASK) >>> 16;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14197
              data[offset+1] = (c & PConstants.GREEN_MASK) >>> 8;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14198
              data[offset+2] = (c & PConstants.BLUE_MASK);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14199
              data[offset+3] = (c & PConstants.ALPHA_MASK) >>> 24;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14200
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14201
            aImg.__isDirty = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14202
          };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14203
        }(pImage))
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14204
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14205
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14206
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14207
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14208
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14209
    * Datatype for storing images. Processing can display .gif, .jpg, .tga, and .png images. Images may be
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14210
    * displayed in 2D and 3D space. Before an image is used, it must be loaded with the loadImage() function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14211
    * The PImage object contains fields for the width and height of the image, as well as an array called
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14212
    * pixels[]  which contains the values for every pixel in the image. A group of methods, described below,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14213
    * allow easy access to the image's pixels and alpha channel and simplify the process of compositing.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14214
    * Before using the pixels[] array, be sure to use the loadPixels() method on the image to make sure that the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14215
    * pixel data is properly loaded. To create a new image, use the createImage() function (do not use new PImage()).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14216
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14217
    * @param {int} width                image width
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14218
    * @param {int} height               image height
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14219
    * @param {MODE} format              Either RGB, ARGB, ALPHA (grayscale alpha channel)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14220
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14221
    * @returns {PImage}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14222
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14223
    * @see loadImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14224
    * @see imageMode
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14225
    * @see createImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14226
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14227
    var PImage = function(aWidth, aHeight, aFormat) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14228
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14229
      // Keep track of whether or not the cached imageData has been touched.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14230
      this.__isDirty = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14231
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14232
      if (aWidth instanceof HTMLImageElement) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14233
        // convert an <img> to a PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14234
        this.fromHTMLImageData(aWidth);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14235
      } else if (aHeight || aFormat) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14236
        this.width = aWidth || 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14237
        this.height = aHeight || 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14238
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14239
        // Stuff a canvas into sourceImg so image() calls can use drawImage like an <img>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14240
        var canvas = this.sourceImg = document.createElement("canvas");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14241
        canvas.width = this.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14242
        canvas.height = this.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14243
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14244
        var imageData = this.imageData = canvas.getContext('2d').createImageData(this.width, this.height);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14245
        this.format = (aFormat === PConstants.ARGB || aFormat === PConstants.ALPHA) ? aFormat : PConstants.RGB;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14246
        if (this.format === PConstants.RGB) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14247
          // Set the alpha channel of an RGB image to opaque.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14248
          for (var i = 3, data = this.imageData.data, len = data.length; i < len; i += 4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14249
            data[i] = 255;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14250
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14251
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14252
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14253
        this.__isDirty = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14254
        this.updatePixels();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14255
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14256
        this.width = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14257
        this.height = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14258
        this.imageData = utilityContext2d.createImageData(1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14259
        this.format = PConstants.ARGB;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14260
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14261
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14262
      this.pixels = buildPixelsObject(this);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14263
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14264
    PImage.prototype = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14265
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14266
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14267
       * Temporary hack to deal with cross-Processing-instance created PImage.  See
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14268
       * tickets #1623 and #1644.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14269
       */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14270
      __isPImage: true,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14271
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14272
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14273
      * @member PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14274
      * Updates the image with the data in its pixels[] array. Use in conjunction with loadPixels(). If
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14275
      * you're only reading pixels from the array, there's no need to call updatePixels().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14276
      * Certain renderers may or may not seem to require loadPixels() or updatePixels(). However, the rule
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14277
      * is that any time you want to manipulate the pixels[] array, you must first call loadPixels(), and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14278
      * after changes have been made, call updatePixels(). Even if the renderer may not seem to use this
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14279
      * function in the current Processing release, this will always be subject to change.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14280
      * Currently, none of the renderers use the additional parameters to updatePixels().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14281
      */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14282
      updatePixels: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14283
        var canvas = this.sourceImg;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14284
        if (canvas && canvas instanceof HTMLCanvasElement && this.__isDirty) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14285
          canvas.getContext('2d').putImageData(this.imageData, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14286
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14287
        this.__isDirty = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14288
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14289
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14290
      fromHTMLImageData: function(htmlImg) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14291
        // convert an <img> to a PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14292
        var canvasData = getCanvasData(htmlImg);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14293
        try {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14294
          var imageData = canvasData.context.getImageData(0, 0, htmlImg.width, htmlImg.height);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14295
          this.fromImageData(imageData);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14296
        } catch(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14297
          if (htmlImg.width && htmlImg.height) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14298
            this.isRemote = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14299
            this.width = htmlImg.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14300
            this.height = htmlImg.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14301
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14302
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14303
        this.sourceImg = htmlImg;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14304
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14305
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14306
      'get': function(x, y, w, h) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14307
        if (!arguments.length) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14308
          return p.get(this);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14309
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14310
        if (arguments.length === 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14311
          return p.get(x, y, this);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14312
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14313
        if (arguments.length === 4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14314
          return p.get(x, y, w, h, this);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14315
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14316
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14317
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14318
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14319
      * @member PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14320
      * Changes the color of any pixel or writes an image directly into the image. The x and y parameter
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14321
      * specify the pixel or the upper-left corner of the image. The color parameter specifies the color value.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14322
      * Setting the color of a single pixel with set(x, y) is easy, but not as fast as putting the data
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14323
      * directly into pixels[]. The equivalent statement to "set(x, y, #000000)" using pixels[] is
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14324
      * "pixels[y*width+x] = #000000". Processing requires calling loadPixels() to load the display window
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14325
      * data into the pixels[] array before getting the values and calling updatePixels() to update the window.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14326
      *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14327
      * @param {int} x        x-coordinate of the pixel or upper-left corner of the image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14328
      * @param {int} y        y-coordinate of the pixel or upper-left corner of the image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14329
      * @param {color} color  any value of the color datatype
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14330
      *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14331
      * @see get
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14332
      * @see pixels[]
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14333
      * @see copy
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14334
      */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14335
      'set': function(x, y, c) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14336
        p.set(x, y, c, this);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14337
        this.__isDirty = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14338
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14339
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14340
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14341
      * @member PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14342
      * Blends a region of pixels into the image specified by the img parameter. These copies utilize full
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14343
      * alpha channel support and a choice of the following modes to blend the colors of source pixels (A)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14344
      * with the ones of pixels in the destination image (B):
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14345
      * BLEND - linear interpolation of colours: C = A*factor + B
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14346
      * ADD - additive blending with white clip: C = min(A*factor + B, 255)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14347
      * SUBTRACT - subtractive blending with black clip: C = max(B - A*factor, 0)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14348
      * DARKEST - only the darkest colour succeeds: C = min(A*factor, B)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14349
      * LIGHTEST - only the lightest colour succeeds: C = max(A*factor, B)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14350
      * DIFFERENCE - subtract colors from underlying image.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14351
      * EXCLUSION - similar to DIFFERENCE, but less extreme.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14352
      * MULTIPLY - Multiply the colors, result will always be darker.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14353
      * SCREEN - Opposite multiply, uses inverse values of the colors.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14354
      * OVERLAY - A mix of MULTIPLY and SCREEN. Multiplies dark values, and screens light values.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14355
      * HARD_LIGHT - SCREEN when greater than 50% gray, MULTIPLY when lower.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14356
      * SOFT_LIGHT - Mix of DARKEST and LIGHTEST. Works like OVERLAY, but not as harsh.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14357
      * DODGE - Lightens light tones and increases contrast, ignores darks. Called "Color Dodge" in Illustrator and Photoshop.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14358
      * BURN - Darker areas are applied, increasing contrast, ignores lights. Called "Color Burn" in Illustrator and Photoshop.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14359
      * All modes use the alpha information (highest byte) of source image pixels as the blending factor.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14360
      * If the source and destination regions are different sizes, the image will be automatically resized to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14361
      * match the destination size. If the srcImg parameter is not used, the display window is used as the source image.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14362
      * This function ignores imageMode().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14363
      *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14364
      * @param {int} x              X coordinate of the source's upper left corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14365
      * @param {int} y              Y coordinate of the source's upper left corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14366
      * @param {int} width          source image width
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14367
      * @param {int} height         source image height
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14368
      * @param {int} dx             X coordinate of the destinations's upper left corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14369
      * @param {int} dy             Y coordinate of the destinations's upper left corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14370
      * @param {int} dwidth         destination image width
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14371
      * @param {int} dheight        destination image height
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14372
      * @param {PImage} srcImg      an image variable referring to the source image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14373
      * @param {MODE} MODE          Either BLEND, ADD, SUBTRACT, LIGHTEST, DARKEST, DIFFERENCE, EXCLUSION,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14374
      * MULTIPLY, SCREEN, OVERLAY, HARD_LIGHT, SOFT_LIGHT, DODGE, BURN
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14375
      *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14376
      * @see alpha
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14377
      * @see copy
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14378
      */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14379
      blend: function(srcImg, x, y, width, height, dx, dy, dwidth, dheight, MODE) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14380
        if (arguments.length === 9) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14381
          p.blend(this, srcImg, x, y, width, height, dx, dy, dwidth, dheight, this);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14382
        } else if (arguments.length === 10) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14383
          p.blend(srcImg, x, y, width, height, dx, dy, dwidth, dheight, MODE, this);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14384
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14385
        delete this.sourceImg;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14386
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14387
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14388
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14389
      * @member PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14390
      * Copies a region of pixels from one image into another. If the source and destination regions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14391
      * aren't the same size, it will automatically resize source pixels to fit the specified target region.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14392
      * No alpha information is used in the process, however if the source image has an alpha channel set,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14393
      * it will be copied as well. This function ignores imageMode().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14394
      *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14395
      * @param {int} sx             X coordinate of the source's upper left corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14396
      * @param {int} sy             Y coordinate of the source's upper left corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14397
      * @param {int} swidth         source image width
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14398
      * @param {int} sheight        source image height
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14399
      * @param {int} dx             X coordinate of the destinations's upper left corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14400
      * @param {int} dy             Y coordinate of the destinations's upper left corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14401
      * @param {int} dwidth         destination image width
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14402
      * @param {int} dheight        destination image height
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14403
      * @param {PImage} srcImg      an image variable referring to the source image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14404
      *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14405
      * @see alpha
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14406
      * @see blend
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14407
      */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14408
      copy: function(srcImg, sx, sy, swidth, sheight, dx, dy, dwidth, dheight) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14409
        if (arguments.length === 8) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14410
          p.blend(this, srcImg, sx, sy, swidth, sheight, dx, dy, dwidth, PConstants.REPLACE, this);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14411
        } else if (arguments.length === 9) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14412
          p.blend(srcImg, sx, sy, swidth, sheight, dx, dy, dwidth, dheight, PConstants.REPLACE, this);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14413
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14414
        delete this.sourceImg;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14415
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14416
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14417
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14418
      * @member PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14419
      * Filters an image as defined by one of the following modes:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14420
      * THRESHOLD - converts the image to black and white pixels depending if they are above or below
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14421
      * the threshold defined by the level parameter. The level must be between 0.0 (black) and 1.0(white).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14422
      * If no level is specified, 0.5 is used.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14423
      * GRAY - converts any colors in the image to grayscale equivalents
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14424
      * INVERT - sets each pixel to its inverse value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14425
      * POSTERIZE - limits each channel of the image to the number of colors specified as the level parameter
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14426
      * BLUR - executes a Guassian blur with the level parameter specifying the extent of the blurring.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14427
      * If no level parameter is used, the blur is equivalent to Guassian blur of radius 1.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14428
      * OPAQUE - sets the alpha channel to entirely opaque.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14429
      * ERODE - reduces the light areas with the amount defined by the level parameter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14430
      * DILATE - increases the light areas with the amount defined by the level parameter
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14431
      *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14432
      * @param {MODE} MODE        Either THRESHOLD, GRAY, INVERT, POSTERIZE, BLUR, OPAQUE, ERODE, or DILATE
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14433
      * @param {int|float} param  in the range from 0 to 1
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14434
      */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14435
      filter: function(mode, param) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14436
        if (arguments.length === 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14437
          p.filter(mode, param, this);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14438
        } else if (arguments.length === 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14439
          // no param specified, send null to show its invalid
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14440
          p.filter(mode, null, this);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14441
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14442
        delete this.sourceImg;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14443
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14444
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14445
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14446
      * @member PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14447
      * Saves the image into a file. Images are saved in TIFF, TARGA, JPEG, and PNG format depending on
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14448
      * the extension within the filename  parameter. For example, "image.tif" will have a TIFF image and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14449
      * "image.png" will save a PNG image. If no extension is included in the filename, the image will save
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14450
      * in TIFF format and .tif will be added to the name. These files are saved to the sketch's folder,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14451
      * which may be opened by selecting "Show sketch folder" from the "Sketch" menu. It is not possible to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14452
      * use save() while running the program in a web browser.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14453
      * To save an image created within the code, rather than through loading, it's necessary to make the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14454
      * image with the createImage() function so it is aware of the location of the program and can therefore
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14455
      * save the file to the right place. See the createImage() reference for more information.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14456
      *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14457
      * @param {String} filename        a sequence of letters and numbers
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14458
      */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14459
      save: function(file){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14460
        p.save(file,this);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14461
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14462
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14463
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14464
      * @member PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14465
      * Resize the image to a new width and height. To make the image scale proportionally, use 0 as the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14466
      * value for the wide or high parameter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14467
      *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14468
      * @param {int} wide         the resized image width
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14469
      * @param {int} high         the resized image height
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14470
      *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14471
      * @see get
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14472
      */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14473
      resize: function(w, h) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14474
        if (this.isRemote) { // Remote images cannot access imageData
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14475
          throw "Image is loaded remotely. Cannot resize.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14476
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14477
        if (this.width !== 0 || this.height !== 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14478
          // make aspect ratio if w or h is 0
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14479
          if (w === 0 && h !== 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14480
            w = Math.floor(this.width / this.height * h);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14481
          } else if (h === 0 && w !== 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14482
            h = Math.floor(this.height / this.width * w);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14483
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14484
          // put 'this.imageData' into a new canvas
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14485
          var canvas = getCanvasData(this.imageData).canvas;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14486
          // pull imageData object out of canvas into ImageData object
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14487
          var imageData = getCanvasData(canvas, w, h).context.getImageData(0, 0, w, h);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14488
          // set this as new pimage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14489
          this.fromImageData(imageData);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14490
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14491
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14492
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14493
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14494
      * @member PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14495
      * Masks part of an image from displaying by loading another image and using it as an alpha channel.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14496
      * This mask image should only contain grayscale data, but only the blue color channel is used. The
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14497
      * mask image needs to be the same size as the image to which it is applied.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14498
      * In addition to using a mask image, an integer array containing the alpha channel data can be
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14499
      * specified directly. This method is useful for creating dynamically generated alpha masks. This
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14500
      * array must be of the same length as the target image's pixels array and should contain only grayscale
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14501
      * data of values between 0-255.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14502
      *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14503
      * @param {PImage} maskImg         any PImage object used as the alpha channel for "img", needs to be same
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14504
      *                                 size as "img"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14505
      * @param {int[]} maskArray        any array of Integer numbers used as the alpha channel, needs to be same
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14506
      *                                 length as the image's pixel array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14507
      */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14508
      mask: function(mask) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14509
        var obj = this.toImageData(),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14510
            i,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14511
            size;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14512
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14513
        if (mask instanceof PImage || mask.__isPImage) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14514
          if (mask.width === this.width && mask.height === this.height) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14515
            mask = mask.toImageData();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14516
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14517
            for (i = 2, size = this.width * this.height * 4; i < size; i += 4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14518
              // using it as an alpha channel
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14519
              obj.data[i + 1] = mask.data[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14520
              // but only the blue color channel
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14521
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14522
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14523
            throw "mask must have the same dimensions as PImage.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14524
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14525
        } else if (mask instanceof Array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14526
          if (this.width * this.height === mask.length) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14527
            for (i = 0, size = mask.length; i < size; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14528
              obj.data[i * 4 + 3] = mask[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14529
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14530
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14531
            throw "mask array must be the same length as PImage pixels array.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14532
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14533
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14534
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14535
        this.fromImageData(obj);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14536
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14537
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14538
      // These are intentionally left blank for PImages, we work live with pixels and draw as necessary
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14539
      /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14540
      * @member PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14541
      * Loads the pixel data for the image into its pixels[] array. This function must always be called
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14542
      * before reading from or writing to pixels[].
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14543
      * Certain renderers may or may not seem to require loadPixels() or updatePixels(). However, the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14544
      * rule is that any time you want to manipulate the pixels[] array, you must first call loadPixels(),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14545
      * and after changes have been made, call updatePixels(). Even if the renderer may not seem to use
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14546
      * this function in the current Processing release, this will always be subject to change.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14547
      */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14548
      loadPixels: nop,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14549
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14550
      toImageData: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14551
        if (this.isRemote) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14552
          return this.sourceImg;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14553
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14554
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14555
        if (!this.__isDirty) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14556
          return this.imageData;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14557
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14558
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14559
        var canvasData = getCanvasData(this.imageData);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14560
        return canvasData.context.getImageData(0, 0, this.width, this.height);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14561
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14562
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14563
      toDataURL: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14564
        if (this.isRemote) { // Remote images cannot access imageData
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14565
          throw "Image is loaded remotely. Cannot create dataURI.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14566
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14567
        var canvasData = getCanvasData(this.imageData);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14568
        return canvasData.canvas.toDataURL();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14569
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14570
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14571
      fromImageData: function(canvasImg) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14572
        var w = canvasImg.width,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14573
          h = canvasImg.height,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14574
          canvas = document.createElement('canvas'),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14575
          ctx = canvas.getContext('2d');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14576
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14577
        this.width = canvas.width = w;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14578
        this.height = canvas.height = h;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14579
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14580
        ctx.putImageData(canvasImg, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14581
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14582
        // changed for 0.9
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14583
        this.format = PConstants.ARGB;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14584
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14585
        this.imageData = canvasImg;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14586
        this.sourceImg = canvas;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14587
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14588
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14589
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14590
    p.PImage = PImage;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14591
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14592
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14593
    * Creates a new PImage (the datatype for storing images). This provides a fresh buffer of pixels to play
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14594
    * with. Set the size of the buffer with the width and height parameters. The format parameter defines how
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14595
    * the pixels are stored. See the PImage reference for more information.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14596
    * Be sure to include all three parameters, specifying only the width and height (but no format) will
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14597
    * produce a strange error.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14598
    * Advanced users please note that createImage() should be used instead of the syntax new PImage().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14599
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14600
    * @param {int} width                image width
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14601
    * @param {int} height               image height
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14602
    * @param {MODE} format              Either RGB, ARGB, ALPHA (grayscale alpha channel)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14603
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14604
    * @returns {PImage}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14605
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14606
    * @see PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14607
    * @see PGraphics
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14608
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14609
    p.createImage = function(w, h, mode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14610
      return new PImage(w,h,mode);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14611
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14612
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14613
    // Loads an image for display. Type is an extension. Callback is fired on load.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14614
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14615
    * Loads an image into a variable of type PImage. Four types of images ( .gif, .jpg, .tga, .png) images may
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14616
    * be loaded. To load correctly, images must be located in the data directory of the current sketch. In most
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14617
    * cases, load all images in setup() to preload them at the start of the program. Loading images inside draw()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14618
    * will reduce the speed of a program.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14619
    * The filename parameter can also be a URL to a file found online. For security reasons, a Processing sketch
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14620
    * found online can only download files from the same server from which it came. Getting around this restriction
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14621
    * requires a signed applet.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14622
    * The extension parameter is used to determine the image type in cases where the image filename does not end
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14623
    * with a proper extension. Specify the extension as the second parameter to loadImage(), as shown in the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14624
    * third example on this page.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14625
    * If an image is not loaded successfully, the null value is returned and an error message will be printed to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14626
    * the console. The error message does not halt the program, however the null value may cause a NullPointerException
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14627
    * if your code does not check whether the value returned from loadImage() is null.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14628
    * Depending on the type of error, a PImage object may still be returned, but the width and height of the image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14629
    * will be set to -1. This happens if bad image data is returned or cannot be decoded properly. Sometimes this happens
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14630
    * with image URLs that produce a 403 error or that redirect to a password prompt, because loadImage() will attempt
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14631
    * to interpret the HTML as image data.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14632
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14633
    * @param {String} filename        name of file to load, can be .gif, .jpg, .tga, or a handful of other image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14634
    *                                 types depending on your platform.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14635
    * @param {String} extension       the type of image to load, for example "png", "gif", "jpg"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14636
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14637
    * @returns {PImage}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14638
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14639
    * @see PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14640
    * @see image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14641
    * @see imageMode
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14642
    * @see background
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14643
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14644
    p.loadImage = function(file, type, callback) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14645
      // if type is specified add it with a . to file to make the filename
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14646
      if (type) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14647
        file = file + "." + type;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14648
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14649
      var pimg;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14650
      // if image is in the preloader cache return a new PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14651
      if (curSketch.imageCache.images[file]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14652
        pimg = new PImage(curSketch.imageCache.images[file]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14653
        pimg.loaded = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14654
        return pimg;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14655
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14656
      // else async load it
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14657
      pimg = new PImage();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14658
      var img = document.createElement('img');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14659
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14660
      pimg.sourceImg = img;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14661
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14662
      img.onload = (function(aImage, aPImage, aCallback) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14663
        var image = aImage;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14664
        var pimg = aPImage;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14665
        var callback = aCallback;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14666
        return function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14667
          // change the <img> object into a PImage now that its loaded
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14668
          pimg.fromHTMLImageData(image);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14669
          pimg.loaded = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14670
          if (callback) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14671
            callback();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14672
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14673
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14674
      }(img, pimg, callback));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14675
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14676
      img.src = file; // needs to be called after the img.onload function is declared or it wont work in opera
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14677
      return pimg;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14678
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14679
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14680
    // async loading of large images, same functionality as loadImage above
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14681
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14682
    * This function load images on a separate thread so that your sketch does not freeze while images load during
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14683
    * setup(). While the image is loading, its width and height will be 0. If an error occurs while loading the image,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14684
    * its width and height will be set to -1. You'll know when the image has loaded properly because its width and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14685
    * height will be greater than 0. Asynchronous image loading (particularly when downloading from a server) can
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14686
    * dramatically improve performance.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14687
    * The extension parameter is used to determine the image type in cases where the image filename does not end
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14688
    * with a proper extension. Specify the extension as the second parameter to requestImage().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14689
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14690
    * @param {String} filename        name of file to load, can be .gif, .jpg, .tga, or a handful of other image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14691
    *                                 types depending on your platform.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14692
    * @param {String} extension       the type of image to load, for example "png", "gif", "jpg"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14693
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14694
    * @returns {PImage}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14695
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14696
    * @see PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14697
    * @see loadImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14698
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14699
    p.requestImage = p.loadImage;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14700
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14701
    function get$2(x,y) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14702
      var data;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14703
      // return the color at x,y (int) of curContext
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14704
      if (x >= p.width || x < 0 || y < 0 || y >= p.height) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14705
        // x,y is outside image return transparent black
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14706
        return 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14707
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14708
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14709
      // loadPixels() has been called
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14710
      if (isContextReplaced) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14711
        var offset = ((0|x) + p.width * (0|y)) * 4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14712
        data = p.imageData.data;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14713
        return (data[offset + 3] << 24) & PConstants.ALPHA_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14714
               (data[offset] << 16) & PConstants.RED_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14715
               (data[offset + 1] << 8) & PConstants.GREEN_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14716
               data[offset + 2] & PConstants.BLUE_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14717
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14718
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14719
      // x,y is inside canvas space
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14720
      data = p.toImageData(0|x, 0|y, 1, 1).data;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14721
      return (data[3] << 24) & PConstants.ALPHA_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14722
             (data[0] << 16) & PConstants.RED_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14723
             (data[1] << 8) & PConstants.GREEN_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14724
             data[2] & PConstants.BLUE_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14725
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14726
    function get$3(x,y,img) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14727
      if (img.isRemote) { // Remote images cannot access imageData
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14728
        throw "Image is loaded remotely. Cannot get x,y.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14729
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14730
      // PImage.get(x,y) was called, return the color (int) at x,y of img
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14731
      var offset = y * img.width * 4 + (x * 4),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14732
          data = img.imageData.data;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14733
      return (data[offset + 3] << 24) & PConstants.ALPHA_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14734
             (data[offset] << 16) & PConstants.RED_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14735
             (data[offset + 1] << 8) & PConstants.GREEN_MASK |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14736
             data[offset + 2] & PConstants.BLUE_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14737
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14738
    function get$4(x, y, w, h) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14739
      // return a PImage of w and h from cood x,y of curContext
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14740
      var c = new PImage(w, h, PConstants.ARGB);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14741
      c.fromImageData(p.toImageData(x, y, w, h));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14742
      return c;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14743
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14744
    function get$5(x, y, w, h, img) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14745
      if (img.isRemote) { // Remote images cannot access imageData
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14746
        throw "Image is loaded remotely. Cannot get x,y,w,h.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14747
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14748
      // PImage.get(x,y,w,h) was called, return x,y,w,h PImage of img
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14749
      // offset start point needs to be *4
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14750
      var c = new PImage(w, h, PConstants.ARGB), cData = c.imageData.data,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14751
        imgWidth = img.width, imgHeight = img.height, imgData = img.imageData.data;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14752
      // Don't need to copy pixels from the image outside ranges.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14753
      var startRow = Math.max(0, -y), startColumn = Math.max(0, -x),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14754
        stopRow = Math.min(h, imgHeight - y), stopColumn = Math.min(w, imgWidth - x);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14755
      for (var i = startRow; i < stopRow; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14756
        var sourceOffset = ((y + i) * imgWidth + (x + startColumn)) * 4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14757
        var targetOffset = (i * w + startColumn) * 4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14758
        for (var j = startColumn; j < stopColumn; ++j) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14759
          cData[targetOffset++] = imgData[sourceOffset++];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14760
          cData[targetOffset++] = imgData[sourceOffset++];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14761
          cData[targetOffset++] = imgData[sourceOffset++];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14762
          cData[targetOffset++] = imgData[sourceOffset++];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14763
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14764
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14765
      c.__isDirty = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14766
      return c;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14767
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14768
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14769
    // Gets a single pixel or block of pixels from the current Canvas Context or a PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14770
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14771
    * Reads the color of any pixel or grabs a section of an image. If no parameters are specified, the entire
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14772
    * image is returned. Get the value of one pixel by specifying an x,y coordinate. Get a section of the display
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14773
    * window by specifying an additional width and height parameter. If the pixel requested is outside of the image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14774
    * window, black is returned. The numbers returned are scaled according to the current color ranges, but only RGB
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14775
    * values are returned by this function. For example, even though you may have drawn a shape with colorMode(HSB),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14776
    * the numbers returned will be in RGB.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14777
    * Getting the color of a single pixel with get(x, y) is easy, but not as fast as grabbing the data directly
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14778
    * from pixels[]. The equivalent statement to "get(x, y)" using pixels[] is "pixels[y*width+x]". Processing
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14779
    * requires calling loadPixels() to load the display window data into the pixels[] array before getting the values.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14780
    * This function ignores imageMode().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14781
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14782
    * @param {int} x            x-coordinate of the pixel
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14783
    * @param {int} y            y-coordinate of the pixel
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14784
    * @param {int} width        width of pixel rectangle to get
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14785
    * @param {int} height       height of pixel rectangle to get
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14786
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14787
    * @returns {Color|PImage}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14788
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14789
    * @see set
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14790
    * @see pixels[]
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14791
    * @see imageMode
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14792
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14793
    p.get = function(x, y, w, h, img) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14794
      // for 0 2 and 4 arguments use curContext, otherwise PImage.get was called
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14795
      if (img !== undefined) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14796
        return get$5(x, y, w, h, img);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14797
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14798
      if (h !== undefined) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14799
        return get$4(x, y, w, h);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14800
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14801
      if (w !== undefined) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14802
        return get$3(x, y, w);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14803
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14804
      if (y !== undefined) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14805
        return get$2(x, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14806
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14807
      if (x !== undefined) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14808
        // PImage.get() was called, return a new PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14809
        return get$5(0, 0, x.width, x.height, x);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14810
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14811
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14812
      return get$4(0, 0, p.width, p.height);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14813
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14814
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14815
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14816
     * Creates and returns a new <b>PGraphics</b> object of the types P2D, P3D, and JAVA2D. Use this class if you need to draw
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14817
     * into an off-screen graphics buffer. It's not possible to use <b>createGraphics()</b> with OPENGL, because it doesn't
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14818
     * allow offscreen use. The DXF and PDF renderers require the filename parameter. <br /><br /> It's important to call
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14819
     * any drawing commands between beginDraw() and endDraw() statements. This is also true for any commands that affect
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14820
     * drawing, such as smooth() or colorMode().<br /><br /> Unlike the main drawing surface which is completely opaque,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14821
     * surfaces created with createGraphics() can have transparency. This makes it possible to draw into a graphics and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14822
     * maintain the alpha channel.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14823
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14824
     * @param {int} width       width in pixels
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14825
     * @param {int} height      height in pixels
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14826
     * @param {int} renderer    Either P2D, P3D, JAVA2D, PDF, DXF
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14827
     * @param {String} filename the name of the file (not supported yet)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14828
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14829
    p.createGraphics = function(w, h, render) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14830
      var pg = new Processing();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14831
      pg.size(w, h, render);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14832
      return pg;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14833
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14834
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14835
    // pixels caching
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14836
    function resetContext() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14837
      if(isContextReplaced) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14838
        curContext = originalContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14839
        isContextReplaced = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14840
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14841
        p.updatePixels();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14842
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14843
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14844
    function SetPixelContextWrapper() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14845
      function wrapFunction(newContext, name) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14846
        function wrapper() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14847
          resetContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14848
          curContext[name].apply(curContext, arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14849
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14850
        newContext[name] = wrapper;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14851
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14852
      function wrapProperty(newContext, name) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14853
        function getter() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14854
          resetContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14855
          return curContext[name];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14856
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14857
        function setter(value) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14858
          resetContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14859
          curContext[name] = value;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14860
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14861
        p.defineProperty(newContext, name, { get: getter, set: setter });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14862
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14863
      for(var n in curContext) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14864
        if(typeof curContext[n] === 'function') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14865
          wrapFunction(this, n);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14866
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14867
          wrapProperty(this, n);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14868
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14869
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14870
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14871
    function replaceContext() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14872
      if(isContextReplaced) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14873
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14874
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14875
      p.loadPixels();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14876
      if(proxyContext === null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14877
        originalContext = curContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14878
        proxyContext = new SetPixelContextWrapper();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14879
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14880
      isContextReplaced = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14881
      curContext = proxyContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14882
      setPixelsCached = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14883
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14884
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14885
    function set$3(x, y, c) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14886
      if (x < p.width && x >= 0 && y >= 0 && y < p.height) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14887
        replaceContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14888
        p.pixels.setPixel((0|x)+p.width*(0|y), c);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14889
        if(++setPixelsCached > maxPixelsCached) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14890
          resetContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14891
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14892
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14893
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14894
    function set$4(x, y, obj, img) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14895
      if (img.isRemote) { // Remote images cannot access imageData
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14896
        throw "Image is loaded remotely. Cannot set x,y.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14897
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14898
      var c = p.color.toArray(obj);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14899
      var offset = y * img.width * 4 + (x*4);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14900
      var data = img.imageData.data;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14901
      data[offset] = c[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14902
      data[offset+1] = c[1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14903
      data[offset+2] = c[2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14904
      data[offset+3] = c[3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14905
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14906
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14907
    // Paints a pixel array into the canvas
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14908
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14909
    * Changes the color of any pixel or writes an image directly into the display window. The x and y parameters
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14910
    * specify the pixel to change and the color  parameter specifies the color value. The color parameter is affected
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14911
    * by the current color mode (the default is RGB values from 0 to 255). When setting an image, the x and y
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14912
    * parameters define the coordinates for the upper-left corner of the image.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14913
    * Setting the color of a single pixel with set(x, y) is easy, but not as fast as putting the data directly
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14914
    * into pixels[]. The equivalent statement to "set(x, y, #000000)" using pixels[] is "pixels[y*width+x] = #000000".
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14915
    * You must call loadPixels() to load the display window data into the pixels[] array before setting the values
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14916
    * and calling updatePixels() to update the window with any changes. This function ignores imageMode().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14917
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14918
    * @param {int} x            x-coordinate of the pixel
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14919
    * @param {int} y            y-coordinate of the pixel
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14920
    * @param {Color} obj        any value of the color datatype
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14921
    * @param {PImage} img       any valid variable of type PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14922
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14923
    * @see get
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14924
    * @see pixels[]
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14925
    * @see imageMode
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14926
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14927
    p.set = function(x, y, obj, img) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14928
      var color, oldFill;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14929
      if (arguments.length === 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14930
        // called p.set(), was it with a color or a img ?
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14931
        if (typeof obj === "number") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14932
          set$3(x, y, obj);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14933
        } else if (obj instanceof PImage || obj.__isPImage) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14934
          p.image(obj, x, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14935
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14936
      } else if (arguments.length === 4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14937
        // PImage.set(x,y,c) was called, set coordinate x,y color to c of img
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14938
        set$4(x, y, obj, img);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14939
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14940
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14941
    p.imageData = {};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14942
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14943
    // handle the sketch code for pixels[]
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14944
    // parser code converts pixels[] to getPixels() or setPixels(),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14945
    // .length becomes getLength()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14946
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14947
    * Array containing the values for all the pixels in the display window. These values are of the color datatype.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14948
    * This array is the size of the display window. For example, if the image is 100x100 pixels, there will be 10000
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14949
    * values and if the window is 200x300 pixels, there will be 60000 values. The index value defines the position
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14950
    * of a value within the array. For example, the statment color b = pixels[230] will set the variable b to be
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14951
    * equal to the value at that location in the array.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14952
    * Before accessing this array, the data must loaded with the loadPixels() function. After the array data has
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14953
    * been modified, the updatePixels() function must be run to update the changes.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14954
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14955
    * @param {int} index      must not exceed the size of the array
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14956
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14957
    * @see loadPixels
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14958
    * @see updatePixels
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14959
    * @see get
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14960
    * @see set
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14961
    * @see PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14962
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14963
    p.pixels = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14964
      getLength: function() { return p.imageData.data.length ? p.imageData.data.length/4 : 0; },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14965
      getPixel: function(i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14966
        var offset = i*4, data = p.imageData.data;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14967
        return (data[offset+3] << 24) & 0xff000000 |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14968
               (data[offset+0] << 16) & 0x00ff0000 |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14969
               (data[offset+1] << 8) & 0x0000ff00 |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14970
               data[offset+2] & 0x000000ff;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14971
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14972
      setPixel: function(i,c) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14973
        var offset = i*4, data = p.imageData.data;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14974
        data[offset+0] = (c & 0x00ff0000) >>> 16; // RED_MASK
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14975
        data[offset+1] = (c & 0x0000ff00) >>> 8;  // GREEN_MASK
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14976
        data[offset+2] = (c & 0x000000ff);        // BLUE_MASK
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14977
        data[offset+3] = (c & 0xff000000) >>> 24; // ALPHA_MASK
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14978
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14979
      toArray: function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14980
        var arr = [], length = p.imageData.width * p.imageData.height, data = p.imageData.data;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14981
        for (var i = 0, offset = 0; i < length; i++, offset += 4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14982
          arr.push((data[offset+3] << 24) & 0xff000000 |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14983
                   (data[offset+0] << 16) & 0x00ff0000 |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14984
                   (data[offset+1] << 8) & 0x0000ff00 |
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14985
                   data[offset+2] & 0x000000ff);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14986
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14987
        return arr;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14988
      },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14989
      set: function(arr) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14990
        for (var i = 0, aL = arr.length; i < aL; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14991
          this.setPixel(i, arr[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14992
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14993
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14994
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14995
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14996
    // Gets a 1-Dimensional pixel array from Canvas
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14997
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14998
    * Loads the pixel data for the display window into the pixels[] array. This function must always be called
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 14999
    * before reading from or writing to pixels[].
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15000
    * Certain renderers may or may not seem to require loadPixels() or updatePixels(). However, the rule is that
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15001
    * any time you want to manipulate the pixels[] array, you must first call loadPixels(), and after changes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15002
    * have been made, call updatePixels(). Even if the renderer may not seem to use this function in the current
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15003
    * Processing release, this will always be subject to change.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15004
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15005
    * @see pixels[]
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15006
    * @see updatePixels
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15007
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15008
    p.loadPixels = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15009
      p.imageData = drawing.$ensureContext().getImageData(0, 0, p.width, p.height);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15010
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15011
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15012
    // Draws a 1-Dimensional pixel array to Canvas
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15013
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15014
    * Updates the display window with the data in the pixels[] array. Use in conjunction with loadPixels(). If
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15015
    * you're only reading pixels from the array, there's no need to call updatePixels() unless there are changes.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15016
    * Certain renderers may or may not seem to require loadPixels() or updatePixels(). However, the rule is that
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15017
    * any time you want to manipulate the pixels[] array, you must first call loadPixels(), and after changes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15018
    * have been made, call updatePixels(). Even if the renderer may not seem to use this function in the current
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15019
    * Processing release, this will always be subject to change.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15020
    * Currently, none of the renderers use the additional parameters to updatePixels(), however this may be
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15021
    * implemented in the future.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15022
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15023
    * @see loadPixels
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15024
    * @see pixels[]
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15025
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15026
    p.updatePixels = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15027
      if (p.imageData) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15028
        drawing.$ensureContext().putImageData(p.imageData, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15029
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15030
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15031
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15032
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15033
    * Set various hints and hacks for the renderer. This is used to handle obscure rendering features that cannot be
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15034
    * implemented in a consistent manner across renderers. Many options will often graduate to standard features
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15035
    * instead of hints over time.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15036
    * hint(ENABLE_OPENGL_4X_SMOOTH) - Enable 4x anti-aliasing for OpenGL. This can help force anti-aliasing if
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15037
    * it has not been enabled by the user. On some graphics cards, this can also be set by the graphics driver's
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15038
    * control panel, however not all cards make this available. This hint must be called immediately after the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15039
    * size() command because it resets the renderer, obliterating any settings and anything drawn (and like size(),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15040
    * re-running the code that came before it again).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15041
    * hint(DISABLE_OPENGL_2X_SMOOTH) - In Processing 1.0, Processing always enables 2x smoothing when the OpenGL
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15042
    * renderer is used. This hint disables the default 2x smoothing and returns the smoothing behavior found in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15043
    * earlier releases, where smooth() and noSmooth() could be used to enable and disable smoothing, though the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15044
    * quality was inferior.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15045
    * hint(ENABLE_NATIVE_FONTS) - Use the native version fonts when they are installed, rather than the bitmapped
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15046
    * version from a .vlw file. This is useful with the JAVA2D renderer setting, as it will improve font rendering
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15047
    * speed. This is not enabled by default, because it can be misleading while testing because the type will look
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15048
    * great on your machine (because you have the font installed) but lousy on others' machines if the identical
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15049
    * font is unavailable. This option can only be set per-sketch, and must be called before any use of textFont().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15050
    * hint(DISABLE_DEPTH_TEST) - Disable the zbuffer, allowing you to draw on top of everything at will. When depth
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15051
    * testing is disabled, items will be drawn to the screen sequentially, like a painting. This hint is most often
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15052
    * used to draw in 3D, then draw in 2D on top of it (for instance, to draw GUI controls in 2D on top of a 3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15053
    * interface). Starting in release 0149, this will also clear the depth buffer. Restore the default with
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15054
    * hint(ENABLE_DEPTH_TEST), but note that with the depth buffer cleared, any 3D drawing that happens later in
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15055
    * draw() will ignore existing shapes on the screen.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15056
    * hint(ENABLE_DEPTH_SORT) - Enable primitive z-sorting of triangles and lines in P3D and OPENGL. This can slow
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15057
    * performance considerably, and the algorithm is not yet perfect. Restore the default with hint(DISABLE_DEPTH_SORT).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15058
    * hint(DISABLE_OPENGL_ERROR_REPORT) - Speeds up the OPENGL renderer setting by not checking for errors while
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15059
    * running. Undo with hint(ENABLE_OPENGL_ERROR_REPORT).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15060
    * As of release 0149, unhint() has been removed in favor of adding additional ENABLE/DISABLE constants to reset
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15061
    * the default behavior. This prevents the double negatives, and also reinforces which hints can be enabled or disabled.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15062
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15063
    * @param {MODE} item          constant: name of the hint to be enabled or disabled
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15064
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15065
    * @see PGraphics
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15066
    * @see createGraphics
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15067
    * @see size
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15068
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15069
    p.hint = function(which) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15070
      var curContext = drawing.$ensureContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15071
      if (which === PConstants.DISABLE_DEPTH_TEST) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15072
         curContext.disable(curContext.DEPTH_TEST);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15073
         curContext.depthMask(false);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15074
         curContext.clear(curContext.DEPTH_BUFFER_BIT);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15075
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15076
      else if (which === PConstants.ENABLE_DEPTH_TEST) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15077
         curContext.enable(curContext.DEPTH_TEST);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15078
         curContext.depthMask(true);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15079
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15080
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15081
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15082
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15083
     * The background() function sets the color used for the background of the Processing window.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15084
     * The default background is light gray. In the <b>draw()</b> function, the background color is used to clear the display window at the beginning of each frame.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15085
     * An image can also be used as the background for a sketch, however its width and height must be the same size as the sketch window.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15086
     * To resize an image 'b' to the size of the sketch window, use b.resize(width, height).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15087
     * Images used as background will ignore the current <b>tint()</b> setting.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15088
     * For the main drawing surface, the alpha value will be ignored. However,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15089
     * alpha can be used on PGraphics objects from <b>createGraphics()</b>. This is
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15090
     * the only way to set all the pixels partially transparent, for instance.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15091
     * If the 'gray' parameter is passed in the function sets the background to a grayscale value, based on the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15092
     * current colorMode.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15093
     * <p>
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15094
     * Note that background() should be called before any transformations occur,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15095
     * because some implementations may require the current transformation matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15096
     * to be identity before drawing.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15097
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15098
     * @param {int|float} gray    specifies a value between white and black
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15099
     * @param {int|float} value1  red or hue value (depending on the current color mode)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15100
     * @param {int|float} value2  green or saturation value (depending on the current color mode)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15101
     * @param {int|float} value3  blue or brightness value (depending on the current color mode)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15102
     * @param {int|float} alpha   opacity of the background
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15103
     * @param {Color} color       any value of the color datatype
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15104
     * @param {int} hex           color value in hexadecimal notation (i.e. #FFCC00 or 0xFFFFCC00)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15105
     * @param {PImage} image      an instance of a PImage to use as a background
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15106
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15107
     * @see #stroke()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15108
     * @see #fill()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15109
     * @see #tint()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15110
     * @see #colorMode()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15111
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15112
    var backgroundHelper = function(arg1, arg2, arg3, arg4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15113
      var obj;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15114
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15115
      if (arg1 instanceof PImage || arg1.__isPImage) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15116
        obj = arg1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15117
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15118
        if (!obj.loaded) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15119
          throw "Error using image in background(): PImage not loaded.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15120
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15121
        if(obj.width !== p.width || obj.height !== p.height){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15122
          throw "Background image must be the same dimensions as the canvas.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15123
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15124
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15125
        obj = p.color(arg1, arg2, arg3, arg4);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15126
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15127
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15128
      backgroundObj = obj;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15129
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15130
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15131
    Drawing2D.prototype.background = function(arg1, arg2, arg3, arg4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15132
      if (arg1 !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15133
        backgroundHelper(arg1, arg2, arg3, arg4);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15134
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15135
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15136
      if (backgroundObj instanceof PImage || backgroundObj.__isPImage) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15137
        saveContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15138
        curContext.setTransform(1, 0, 0, 1, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15139
        p.image(backgroundObj, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15140
        restoreContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15141
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15142
        saveContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15143
        curContext.setTransform(1, 0, 0, 1, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15144
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15145
        // If the background is transparent
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15146
        if (p.alpha(backgroundObj) !== colorModeA) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15147
          curContext.clearRect(0,0, p.width, p.height);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15148
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15149
        curContext.fillStyle = p.color.toString(backgroundObj);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15150
        curContext.fillRect(0, 0, p.width, p.height);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15151
        isFillDirty = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15152
        restoreContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15153
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15154
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15155
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15156
    Drawing3D.prototype.background = function(arg1, arg2, arg3, arg4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15157
      if (arguments.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15158
        backgroundHelper(arg1, arg2, arg3, arg4);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15159
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15160
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15161
      var c = p.color.toGLArray(backgroundObj);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15162
      curContext.clearColor(c[0], c[1], c[2], c[3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15163
      curContext.clear(curContext.COLOR_BUFFER_BIT | curContext.DEPTH_BUFFER_BIT);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15164
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15165
      // An image as a background in 3D is not implemented yet
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15166
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15167
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15168
    // Draws an image to the Canvas
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15169
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15170
    * Displays images to the screen. The images must be in the sketch's "data" directory to load correctly. Select "Add
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15171
    * file..." from the "Sketch" menu to add the image. Processing currently works with GIF, JPEG, and Targa images. The
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15172
    * color of an image may be modified with the tint() function and if a GIF has transparency, it will maintain its
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15173
    * transparency. The img parameter specifies the image to display and the x and y parameters define the location of
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15174
    * the image from its upper-left corner. The image is displayed at its original size unless the width and height
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15175
    * parameters specify a different size. The imageMode() function changes the way the parameters work. A call to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15176
    * imageMode(CORNERS) will change the width and height parameters to define the x and y values of the opposite
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15177
    * corner of the image.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15178
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15179
    * @param {PImage} img            the image to display
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15180
    * @param {int|float} x           x-coordinate of the image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15181
    * @param {int|float} y           y-coordinate of the image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15182
    * @param {int|float} width       width to display the image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15183
    * @param {int|float} height      height to display the image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15184
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15185
    * @see loadImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15186
    * @see PImage
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15187
    * @see imageMode
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15188
    * @see tint
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15189
    * @see background
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15190
    * @see alpha
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15191
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15192
    Drawing2D.prototype.image = function(img, x, y, w, h) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15193
      // Fix fractional positions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15194
      x = Math.round(x);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15195
      y = Math.round(y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15196
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15197
      if (img.width > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15198
        var wid = w || img.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15199
        var hgt = h || img.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15200
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15201
        var bounds = imageModeConvert(x || 0, y || 0, w || img.width, h || img.height, arguments.length < 4);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15202
        var fastImage = !!img.sourceImg && curTint === null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15203
        if (fastImage) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15204
          var htmlElement = img.sourceImg;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15205
          if (img.__isDirty) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15206
            img.updatePixels();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15207
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15208
          // Using HTML element's width and height in case if the image was resized.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15209
          curContext.drawImage(htmlElement, 0, 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15210
            htmlElement.width, htmlElement.height, bounds.x, bounds.y, bounds.w, bounds.h);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15211
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15212
          var obj = img.toImageData();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15213
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15214
          // Tint the image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15215
          if (curTint !== null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15216
            curTint(obj);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15217
            img.__isDirty = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15218
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15219
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15220
          curContext.drawImage(getCanvasData(obj).canvas, 0, 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15221
            img.width, img.height, bounds.x, bounds.y, bounds.w, bounds.h);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15222
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15223
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15224
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15225
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15226
    Drawing3D.prototype.image = function(img, x, y, w, h) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15227
      if (img.width > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15228
        // Fix fractional positions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15229
        x = Math.round(x);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15230
        y = Math.round(y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15231
        w = w || img.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15232
        h = h || img.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15233
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15234
        p.beginShape(p.QUADS);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15235
        p.texture(img);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15236
        p.vertex(x, y, 0, 0, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15237
        p.vertex(x, y+h, 0, 0, h);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15238
        p.vertex(x+w, y+h, 0, w, h);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15239
        p.vertex(x+w, y, 0, w, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15240
        p.endShape();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15241
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15242
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15243
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15244
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15245
     * The tint() function sets the fill value for displaying images. Images can be tinted to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15246
     * specified colors or made transparent by setting the alpha.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15247
     * <br><br>To make an image transparent, but not change it's color,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15248
     * use white as the tint color and specify an alpha value. For instance,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15249
     * tint(255, 128) will make an image 50% transparent (unless
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15250
     * <b>colorMode()</b> has been used).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15251
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15252
     * <br><br>When using hexadecimal notation to specify a color, use "#" or
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15253
     * "0x" before the values (e.g. #CCFFAA, 0xFFCCFFAA). The # syntax uses six
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15254
     * digits to specify a color (the way colors are specified in HTML and CSS).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15255
     * When using the hexadecimal notation starting with "0x", the hexadecimal
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15256
     * value must be specified with eight characters; the first two characters
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15257
     * define the alpha component and the remainder the red, green, and blue
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15258
     * components.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15259
     * <br><br>The value for the parameter "gray" must be less than or equal
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15260
     * to the current maximum value as specified by <b>colorMode()</b>.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15261
     * The default maximum value is 255.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15262
     * <br><br>The tint() method is also used to control the coloring of
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15263
     * textures in 3D.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15264
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15265
     * @param {int|float} gray    any valid number
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15266
     * @param {int|float} alpha    opacity of the image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15267
     * @param {int|float} value1  red or hue value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15268
     * @param {int|float} value2  green or saturation value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15269
     * @param {int|float} value3  blue or brightness value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15270
     * @param {int|float} color    any value of the color datatype
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15271
     * @param {int} hex            color value in hexadecimal notation (i.e. #FFCC00 or 0xFFFFCC00)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15272
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15273
     * @see #noTint()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15274
     * @see #image()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15275
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15276
    p.tint = function(a1, a2, a3, a4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15277
      var tintColor = p.color(a1, a2, a3, a4);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15278
      var r = p.red(tintColor) / colorModeX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15279
      var g = p.green(tintColor) / colorModeY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15280
      var b = p.blue(tintColor) / colorModeZ;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15281
      var a = p.alpha(tintColor) / colorModeA;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15282
      curTint = function(obj) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15283
        var data = obj.data,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15284
            length = 4 * obj.width * obj.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15285
        for (var i = 0; i < length;) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15286
          data[i++] *= r;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15287
          data[i++] *= g;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15288
          data[i++] *= b;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15289
          data[i++] *= a;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15290
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15291
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15292
      // for overriding the color buffer when 3d rendering
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15293
      curTint3d = function(data){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15294
        for (var i = 0; i < data.length;) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15295
          data[i++] = r;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15296
          data[i++] = g;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15297
          data[i++] = b;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15298
          data[i++] = a;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15299
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15300
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15301
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15302
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15303
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15304
     * The noTint() function removes the current fill value for displaying images and reverts to displaying images with their original hues.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15305
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15306
     * @see #tint()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15307
     * @see #image()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15308
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15309
    p.noTint = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15310
      curTint = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15311
      curTint3d = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15312
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15313
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15314
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15315
    * Copies a region of pixels from the display window to another area of the display window and copies a region of pixels from an
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15316
    * image used as the srcImg  parameter into the display window. If the source and destination regions aren't the same size, it will
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15317
    * automatically resize the source pixels to fit the specified target region. No alpha information is used in the process, however
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15318
    * if the source image has an alpha channel set, it will be copied as well. This function ignores imageMode().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15319
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15320
    * @param {int} x            X coordinate of the source's upper left corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15321
    * @param {int} y            Y coordinate of the source's upper left corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15322
    * @param {int} width        source image width
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15323
    * @param {int} height       source image height
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15324
    * @param {int} dx           X coordinate of the destination's upper left corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15325
    * @param {int} dy           Y coordinate of the destination's upper left corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15326
    * @param {int} dwidth       destination image width
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15327
    * @param {int} dheight      destination image height
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15328
    * @param {PImage} srcImg    image variable referring to the source image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15329
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15330
    * @see blend
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15331
    * @see get
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15332
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15333
    p.copy = function(src, sx, sy, sw, sh, dx, dy, dw, dh) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15334
      if (dh === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15335
        // shift everything, and introduce p
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15336
        dh = dw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15337
        dw = dy;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15338
        dy = dx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15339
        dx = sh;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15340
        sh = sw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15341
        sw = sy;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15342
        sy = sx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15343
        sx = src;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15344
        src = p;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15345
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15346
      p.blend(src, sx, sy, sw, sh, dx, dy, dw, dh, PConstants.REPLACE);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15347
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15348
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15349
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15350
    * Blends a region of pixels from one image into another (or in itself again) with full alpha channel support. There
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15351
    * is a choice of the following modes to blend the source pixels (A) with the ones of pixels in the destination image (B):
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15352
    * BLEND - linear interpolation of colours: C = A*factor + B
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15353
    * ADD - additive blending with white clip: C = min(A*factor + B, 255)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15354
    * SUBTRACT - subtractive blending with black clip: C = max(B - A*factor, 0)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15355
    * DARKEST - only the darkest colour succeeds: C = min(A*factor, B)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15356
    * LIGHTEST - only the lightest colour succeeds: C = max(A*factor, B)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15357
    * DIFFERENCE - subtract colors from underlying image.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15358
    * EXCLUSION - similar to DIFFERENCE, but less extreme.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15359
    * MULTIPLY - Multiply the colors, result will always be darker.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15360
    * SCREEN - Opposite multiply, uses inverse values of the colors.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15361
    * OVERLAY - A mix of MULTIPLY and SCREEN. Multiplies dark values, and screens light values.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15362
    * HARD_LIGHT - SCREEN when greater than 50% gray, MULTIPLY when lower.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15363
    * SOFT_LIGHT - Mix of DARKEST and LIGHTEST. Works like OVERLAY, but not as harsh.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15364
    * DODGE - Lightens light tones and increases contrast, ignores darks. Called "Color Dodge" in Illustrator and Photoshop.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15365
    * BURN - Darker areas are applied, increasing contrast, ignores lights. Called "Color Burn" in Illustrator and Photoshop.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15366
    * All modes use the alpha information (highest byte) of source image pixels as the blending factor. If the source and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15367
    * destination regions are different sizes, the image will be automatically resized to match the destination size. If the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15368
    * srcImg parameter is not used, the display window is used as the source image.  This function ignores imageMode().
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15369
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15370
    * @param {int} x            X coordinate of the source's upper left corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15371
    * @param {int} y            Y coordinate of the source's upper left corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15372
    * @param {int} width        source image width
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15373
    * @param {int} height       source image height
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15374
    * @param {int} dx           X coordinate of the destination's upper left corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15375
    * @param {int} dy           Y coordinate of the destination's upper left corner
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15376
    * @param {int} dwidth       destination image width
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15377
    * @param {int} dheight      destination image height
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15378
    * @param {PImage} srcImg    image variable referring to the source image
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15379
    * @param {PImage} MODE      Either BLEND, ADD, SUBTRACT, LIGHTEST, DARKEST, DIFFERENCE, EXCLUSION, MULTIPLY, SCREEN,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15380
    *                           OVERLAY, HARD_LIGHT, SOFT_LIGHT, DODGE, BURN
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15381
    * @see filter
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15382
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15383
    p.blend = function(src, sx, sy, sw, sh, dx, dy, dw, dh, mode, pimgdest) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15384
      if (src.isRemote) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15385
        throw "Image is loaded remotely. Cannot blend image.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15386
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15387
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15388
      if (mode === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15389
        // shift everything, and introduce p
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15390
        mode = dh;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15391
        dh = dw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15392
        dw = dy;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15393
        dy = dx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15394
        dx = sh;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15395
        sh = sw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15396
        sw = sy;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15397
        sy = sx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15398
        sx = src;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15399
        src = p;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15400
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15401
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15402
      var sx2 = sx + sw,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15403
        sy2 = sy + sh,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15404
        dx2 = dx + dw,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15405
        dy2 = dy + dh,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15406
        dest = pimgdest || p;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15407
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15408
      // check if pimgdest is there and pixels, if so this was a call from pimg.blend
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15409
      if (pimgdest === undef || mode === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15410
        p.loadPixels();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15411
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15412
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15413
      src.loadPixels();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15414
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15415
      if (src === p && p.intersect(sx, sy, sx2, sy2, dx, dy, dx2, dy2)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15416
        p.blit_resize(p.get(sx, sy, sx2 - sx, sy2 - sy), 0, 0, sx2 - sx - 1, sy2 - sy - 1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15417
                      dest.imageData.data, dest.width, dest.height, dx, dy, dx2, dy2, mode);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15418
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15419
        p.blit_resize(src, sx, sy, sx2, sy2, dest.imageData.data, dest.width, dest.height, dx, dy, dx2, dy2, mode);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15420
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15421
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15422
      if (pimgdest === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15423
        p.updatePixels();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15424
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15425
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15426
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15427
    // helper function for filter()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15428
    var buildBlurKernel = function(r) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15429
      var radius = p.floor(r * 3.5), i, radiusi;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15430
      radius = (radius < 1) ? 1 : ((radius < 248) ? radius : 248);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15431
      if (p.shared.blurRadius !== radius) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15432
        p.shared.blurRadius = radius;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15433
        p.shared.blurKernelSize = 1 + (p.shared.blurRadius<<1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15434
        p.shared.blurKernel = new Float32Array(p.shared.blurKernelSize);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15435
        var sharedBlurKernal = p.shared.blurKernel;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15436
        var sharedBlurKernelSize = p.shared.blurKernelSize;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15437
        var sharedBlurRadius = p.shared.blurRadius;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15438
        // init blurKernel
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15439
        for (i = 0; i < sharedBlurKernelSize; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15440
          sharedBlurKernal[i] = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15441
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15442
        var radiusiSquared = (radius - 1) * (radius - 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15443
        for (i = 1; i < radius; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15444
          sharedBlurKernal[radius + i] = sharedBlurKernal[radiusi] = radiusiSquared;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15445
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15446
        sharedBlurKernal[radius] = radius * radius;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15447
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15448
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15449
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15450
    var blurARGB = function(r, aImg) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15451
      var sum, cr, cg, cb, ca, c, m;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15452
      var read, ri, ym, ymi, bk0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15453
      var wh = aImg.pixels.getLength();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15454
      var r2 = new Float32Array(wh);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15455
      var g2 = new Float32Array(wh);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15456
      var b2 = new Float32Array(wh);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15457
      var a2 = new Float32Array(wh);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15458
      var yi = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15459
      var x, y, i, offset;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15460
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15461
      buildBlurKernel(r);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15462
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15463
      var aImgHeight = aImg.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15464
      var aImgWidth = aImg.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15465
      var sharedBlurKernelSize = p.shared.blurKernelSize;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15466
      var sharedBlurRadius = p.shared.blurRadius;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15467
      var sharedBlurKernal = p.shared.blurKernel;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15468
      var pix = aImg.imageData.data;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15469
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15470
      for (y = 0; y < aImgHeight; y++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15471
        for (x = 0; x < aImgWidth; x++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15472
          cb = cg = cr = ca = sum = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15473
          read = x - sharedBlurRadius;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15474
          if (read<0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15475
            bk0 = -read;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15476
            read = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15477
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15478
            if (read >= aImgWidth) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15479
              break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15480
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15481
            bk0=0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15482
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15483
          for (i = bk0; i < sharedBlurKernelSize; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15484
            if (read >= aImgWidth) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15485
              break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15486
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15487
            offset = (read + yi) *4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15488
            m = sharedBlurKernal[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15489
            ca += m * pix[offset + 3];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15490
            cr += m * pix[offset];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15491
            cg += m * pix[offset + 1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15492
            cb += m * pix[offset + 2];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15493
            sum += m;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15494
            read++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15495
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15496
          ri = yi + x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15497
          a2[ri] = ca / sum;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15498
          r2[ri] = cr / sum;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15499
          g2[ri] = cg / sum;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15500
          b2[ri] = cb / sum;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15501
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15502
        yi += aImgWidth;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15503
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15504
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15505
      yi = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15506
      ym = -sharedBlurRadius;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15507
      ymi = ym*aImgWidth;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15508
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15509
      for (y = 0; y < aImgHeight; y++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15510
        for (x = 0; x < aImgWidth; x++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15511
          cb = cg = cr = ca = sum = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15512
          if (ym<0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15513
            bk0 = ri = -ym;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15514
            read = x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15515
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15516
            if (ym >= aImgHeight) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15517
              break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15518
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15519
            bk0 = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15520
            ri = ym;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15521
            read = x + ymi;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15522
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15523
          for (i = bk0; i < sharedBlurKernelSize; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15524
            if (ri >= aImgHeight) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15525
              break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15526
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15527
            m = sharedBlurKernal[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15528
            ca += m * a2[read];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15529
            cr += m * r2[read];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15530
            cg += m * g2[read];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15531
            cb += m * b2[read];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15532
            sum += m;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15533
            ri++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15534
            read += aImgWidth;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15535
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15536
          offset = (x + yi) *4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15537
          pix[offset] = cr / sum;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15538
          pix[offset + 1] = cg / sum;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15539
          pix[offset + 2] = cb / sum;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15540
          pix[offset + 3] = ca / sum;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15541
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15542
        yi += aImgWidth;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15543
        ymi += aImgWidth;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15544
        ym++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15545
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15546
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15547
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15548
    // helper funtion for ERODE and DILATE modes of filter()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15549
    var dilate = function(isInverted, aImg) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15550
      var currIdx = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15551
      var maxIdx = aImg.pixels.getLength();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15552
      var out = new Int32Array(maxIdx);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15553
      var currRowIdx, maxRowIdx, colOrig, colOut, currLum;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15554
      var idxRight, idxLeft, idxUp, idxDown,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15555
          colRight, colLeft, colUp, colDown,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15556
          lumRight, lumLeft, lumUp, lumDown;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15557
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15558
      if (!isInverted) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15559
        // erosion (grow light areas)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15560
        while (currIdx<maxIdx) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15561
          currRowIdx = currIdx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15562
          maxRowIdx = currIdx + aImg.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15563
          while (currIdx < maxRowIdx) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15564
            colOrig = colOut = aImg.pixels.getPixel(currIdx);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15565
            idxLeft = currIdx - 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15566
            idxRight = currIdx + 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15567
            idxUp = currIdx - aImg.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15568
            idxDown = currIdx + aImg.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15569
            if (idxLeft < currRowIdx) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15570
              idxLeft = currIdx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15571
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15572
            if (idxRight >= maxRowIdx) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15573
              idxRight = currIdx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15574
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15575
            if (idxUp < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15576
              idxUp = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15577
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15578
            if (idxDown >= maxIdx) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15579
              idxDown = currIdx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15580
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15581
            colUp = aImg.pixels.getPixel(idxUp);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15582
            colLeft = aImg.pixels.getPixel(idxLeft);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15583
            colDown = aImg.pixels.getPixel(idxDown);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15584
            colRight = aImg.pixels.getPixel(idxRight);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15585
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15586
            // compute luminance
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15587
            currLum = 77*(colOrig>>16&0xff) + 151*(colOrig>>8&0xff) + 28*(colOrig&0xff);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15588
            lumLeft = 77*(colLeft>>16&0xff) + 151*(colLeft>>8&0xff) + 28*(colLeft&0xff);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15589
            lumRight = 77*(colRight>>16&0xff) + 151*(colRight>>8&0xff) + 28*(colRight&0xff);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15590
            lumUp = 77*(colUp>>16&0xff) + 151*(colUp>>8&0xff) + 28*(colUp&0xff);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15591
            lumDown = 77*(colDown>>16&0xff) + 151*(colDown>>8&0xff) + 28*(colDown&0xff);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15592
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15593
            if (lumLeft > currLum) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15594
              colOut = colLeft;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15595
              currLum = lumLeft;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15596
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15597
            if (lumRight > currLum) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15598
              colOut = colRight;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15599
              currLum = lumRight;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15600
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15601
            if (lumUp > currLum) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15602
              colOut = colUp;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15603
              currLum = lumUp;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15604
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15605
            if (lumDown > currLum) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15606
              colOut = colDown;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15607
              currLum = lumDown;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15608
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15609
            out[currIdx++] = colOut;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15610
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15611
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15612
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15613
        // dilate (grow dark areas)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15614
        while (currIdx < maxIdx) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15615
          currRowIdx = currIdx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15616
          maxRowIdx = currIdx + aImg.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15617
          while (currIdx < maxRowIdx) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15618
            colOrig = colOut = aImg.pixels.getPixel(currIdx);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15619
            idxLeft = currIdx - 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15620
            idxRight = currIdx + 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15621
            idxUp = currIdx - aImg.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15622
            idxDown = currIdx + aImg.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15623
            if (idxLeft < currRowIdx) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15624
              idxLeft = currIdx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15625
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15626
            if (idxRight >= maxRowIdx) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15627
              idxRight = currIdx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15628
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15629
            if (idxUp < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15630
              idxUp = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15631
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15632
            if (idxDown >= maxIdx) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15633
              idxDown = currIdx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15634
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15635
            colUp = aImg.pixels.getPixel(idxUp);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15636
            colLeft = aImg.pixels.getPixel(idxLeft);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15637
            colDown = aImg.pixels.getPixel(idxDown);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15638
            colRight = aImg.pixels.getPixel(idxRight);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15639
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15640
            // compute luminance
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15641
            currLum = 77*(colOrig>>16&0xff) + 151*(colOrig>>8&0xff) + 28*(colOrig&0xff);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15642
            lumLeft = 77*(colLeft>>16&0xff) + 151*(colLeft>>8&0xff) + 28*(colLeft&0xff);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15643
            lumRight = 77*(colRight>>16&0xff) + 151*(colRight>>8&0xff) + 28*(colRight&0xff);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15644
            lumUp = 77*(colUp>>16&0xff) + 151*(colUp>>8&0xff) + 28*(colUp&0xff);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15645
            lumDown = 77*(colDown>>16&0xff) + 151*(colDown>>8&0xff) + 28*(colDown&0xff);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15646
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15647
            if (lumLeft < currLum) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15648
              colOut = colLeft;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15649
              currLum = lumLeft;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15650
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15651
            if (lumRight < currLum) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15652
              colOut = colRight;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15653
              currLum = lumRight;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15654
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15655
            if (lumUp < currLum) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15656
              colOut = colUp;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15657
              currLum = lumUp;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15658
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15659
            if (lumDown < currLum) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15660
              colOut = colDown;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15661
              currLum = lumDown;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15662
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15663
            out[currIdx++]=colOut;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15664
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15665
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15666
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15667
      aImg.pixels.set(out);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15668
      //p.arraycopy(out,0,pixels,0,maxIdx);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15669
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15670
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15671
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15672
    * Filters the display window as defined by one of the following modes:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15673
    * THRESHOLD - converts the image to black and white pixels depending if they are above or below the threshold
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15674
    * defined by the level parameter. The level must be between 0.0 (black) and 1.0(white). If no level is specified, 0.5 is used.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15675
    * GRAY - converts any colors in the image to grayscale equivalents
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15676
    * INVERT - sets each pixel to its inverse value
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15677
    * POSTERIZE - limits each channel of the image to the number of colors specified as the level parameter
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15678
    * BLUR - executes a Guassian blur with the level parameter specifying the extent of the blurring. If no level parameter is
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15679
    * used, the blur is equivalent to Guassian blur of radius 1.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15680
    * OPAQUE - sets the alpha channel to entirely opaque.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15681
    * ERODE - reduces the light areas with the amount defined by the level parameter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15682
    * DILATE - increases the light areas with the amount defined by the level parameter.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15683
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15684
    * @param {MODE} MODE          Either THRESHOLD, GRAY, INVERT, POSTERIZE, BLUR, OPAQUE, ERODE, or DILATE
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15685
    * @param {int|float} level    defines the quality of the filter
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15686
    *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15687
    * @see blend
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15688
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15689
    p.filter = function(kind, param, aImg){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15690
      var img, col, lum, i;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15691
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15692
      if (arguments.length === 3) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15693
        aImg.loadPixels();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15694
        img = aImg;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15695
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15696
        p.loadPixels();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15697
        img = p;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15698
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15699
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15700
      if (param === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15701
        param = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15702
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15703
      if (img.isRemote) { // Remote images cannot access imageData
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15704
        throw "Image is loaded remotely. Cannot filter image.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15705
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15706
      // begin filter process
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15707
      var imglen = img.pixels.getLength();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15708
      switch (kind) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15709
        case PConstants.BLUR:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15710
          var radius = param || 1; // if no param specified, use 1 (default for p5)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15711
          blurARGB(radius, img);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15712
          break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15713
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15714
        case PConstants.GRAY:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15715
          if (img.format === PConstants.ALPHA) { //trouble
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15716
            // for an alpha image, convert it to an opaque grayscale
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15717
            for (i = 0; i < imglen; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15718
              col = 255 - img.pixels.getPixel(i);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15719
              img.pixels.setPixel(i,(0xff000000 | (col << 16) | (col << 8) | col));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15720
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15721
            img.format = PConstants.RGB; //trouble
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15722
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15723
            for (i = 0; i < imglen; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15724
              col = img.pixels.getPixel(i);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15725
              lum = (77*(col>>16&0xff) + 151*(col>>8&0xff) + 28*(col&0xff))>>8;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15726
              img.pixels.setPixel(i,((col & PConstants.ALPHA_MASK) | lum<<16 | lum<<8 | lum));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15727
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15728
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15729
          break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15730
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15731
        case PConstants.INVERT:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15732
          for (i = 0; i < imglen; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15733
            img.pixels.setPixel(i, (img.pixels.getPixel(i) ^ 0xffffff));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15734
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15735
          break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15736
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15737
        case PConstants.POSTERIZE:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15738
          if (param === null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15739
            throw "Use filter(POSTERIZE, int levels) instead of filter(POSTERIZE)";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15740
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15741
          var levels = p.floor(param);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15742
          if ((levels < 2) || (levels > 255)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15743
            throw "Levels must be between 2 and 255 for filter(POSTERIZE, levels)";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15744
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15745
          var levels1 = levels - 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15746
          for (i = 0; i < imglen; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15747
            var rlevel = (img.pixels.getPixel(i) >> 16) & 0xff;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15748
            var glevel = (img.pixels.getPixel(i) >> 8) & 0xff;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15749
            var blevel = img.pixels.getPixel(i) & 0xff;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15750
            rlevel = (((rlevel * levels) >> 8) * 255) / levels1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15751
            glevel = (((glevel * levels) >> 8) * 255) / levels1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15752
            blevel = (((blevel * levels) >> 8) * 255) / levels1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15753
            img.pixels.setPixel(i, ((0xff000000 & img.pixels.getPixel(i)) | (rlevel << 16) | (glevel << 8) | blevel));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15754
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15755
          break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15756
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15757
        case PConstants.OPAQUE:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15758
          for (i = 0; i < imglen; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15759
            img.pixels.setPixel(i, (img.pixels.getPixel(i) | 0xff000000));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15760
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15761
          img.format = PConstants.RGB; //trouble
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15762
          break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15763
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15764
        case PConstants.THRESHOLD:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15765
          if (param === null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15766
            param = 0.5;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15767
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15768
          if ((param < 0) || (param > 1)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15769
            throw "Level must be between 0 and 1 for filter(THRESHOLD, level)";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15770
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15771
          var thresh = p.floor(param * 255);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15772
          for (i = 0; i < imglen; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15773
            var max = p.max((img.pixels.getPixel(i) & PConstants.RED_MASK) >> 16, p.max((img.pixels.getPixel(i) & PConstants.GREEN_MASK) >> 8, (img.pixels.getPixel(i) & PConstants.BLUE_MASK)));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15774
            img.pixels.setPixel(i, ((img.pixels.getPixel(i) & PConstants.ALPHA_MASK) | ((max < thresh) ? 0x000000 : 0xffffff)));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15775
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15776
          break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15777
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15778
        case PConstants.ERODE:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15779
          dilate(true, img);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15780
          break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15781
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15782
        case PConstants.DILATE:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15783
          dilate(false, img);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15784
          break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15785
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15786
      img.updatePixels();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15787
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15788
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15789
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15790
    // shared variables for blit_resize(), filter_new_scanline(), filter_bilinear(), filter()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15791
    // change this in the future to not be exposed to p
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15792
    p.shared = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15793
      fracU: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15794
      ifU: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15795
      fracV: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15796
      ifV: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15797
      u1: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15798
      u2: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15799
      v1: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15800
      v2: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15801
      sX: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15802
      sY: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15803
      iw: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15804
      iw1: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15805
      ih1: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15806
      ul: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15807
      ll: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15808
      ur: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15809
      lr: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15810
      cUL: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15811
      cLL: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15812
      cUR: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15813
      cLR: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15814
      srcXOffset: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15815
      srcYOffset: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15816
      r: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15817
      g: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15818
      b: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15819
      a: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15820
      srcBuffer: null,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15821
      blurRadius: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15822
      blurKernelSize: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15823
      blurKernel: null
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15824
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15825
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15826
    p.intersect = function(sx1, sy1, sx2, sy2, dx1, dy1, dx2, dy2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15827
      var sw = sx2 - sx1 + 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15828
      var sh = sy2 - sy1 + 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15829
      var dw = dx2 - dx1 + 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15830
      var dh = dy2 - dy1 + 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15831
      if (dx1 < sx1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15832
        dw += dx1 - sx1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15833
        if (dw > sw) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15834
          dw = sw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15835
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15836
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15837
        var w = sw + sx1 - dx1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15838
        if (dw > w) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15839
          dw = w;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15840
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15841
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15842
      if (dy1 < sy1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15843
        dh += dy1 - sy1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15844
        if (dh > sh) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15845
          dh = sh;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15846
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15847
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15848
        var h = sh + sy1 - dy1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15849
        if (dh > h) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15850
          dh = h;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15851
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15852
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15853
      return ! (dw <= 0 || dh <= 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15854
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15855
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15856
    var blendFuncs = {};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15857
    blendFuncs[PConstants.BLEND] = p.modes.blend;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15858
    blendFuncs[PConstants.ADD] = p.modes.add;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15859
    blendFuncs[PConstants.SUBTRACT] = p.modes.subtract;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15860
    blendFuncs[PConstants.LIGHTEST] = p.modes.lightest;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15861
    blendFuncs[PConstants.DARKEST] = p.modes.darkest;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15862
    blendFuncs[PConstants.REPLACE] = p.modes.replace;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15863
    blendFuncs[PConstants.DIFFERENCE] = p.modes.difference;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15864
    blendFuncs[PConstants.EXCLUSION] = p.modes.exclusion;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15865
    blendFuncs[PConstants.MULTIPLY] = p.modes.multiply;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15866
    blendFuncs[PConstants.SCREEN] = p.modes.screen;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15867
    blendFuncs[PConstants.OVERLAY] = p.modes.overlay;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15868
    blendFuncs[PConstants.HARD_LIGHT] = p.modes.hard_light;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15869
    blendFuncs[PConstants.SOFT_LIGHT] = p.modes.soft_light;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15870
    blendFuncs[PConstants.DODGE] = p.modes.dodge;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15871
    blendFuncs[PConstants.BURN] = p.modes.burn;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15872
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15873
    p.blit_resize = function(img, srcX1, srcY1, srcX2, srcY2, destPixels,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15874
                             screenW, screenH, destX1, destY1, destX2, destY2, mode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15875
      var x, y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15876
      if (srcX1 < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15877
        srcX1 = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15878
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15879
      if (srcY1 < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15880
        srcY1 = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15881
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15882
      if (srcX2 >= img.width) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15883
        srcX2 = img.width - 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15884
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15885
      if (srcY2 >= img.height) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15886
        srcY2 = img.height - 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15887
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15888
      var srcW = srcX2 - srcX1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15889
      var srcH = srcY2 - srcY1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15890
      var destW = destX2 - destX1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15891
      var destH = destY2 - destY1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15892
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15893
      if (destW <= 0 || destH <= 0 || srcW <= 0 || srcH <= 0 || destX1 >= screenW ||
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15894
          destY1 >= screenH || srcX1 >= img.width || srcY1 >= img.height) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15895
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15896
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15897
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15898
      var dx = Math.floor(srcW / destW * PConstants.PRECISIONF);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15899
      var dy = Math.floor(srcH / destH * PConstants.PRECISIONF);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15900
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15901
      var pshared = p.shared;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15902
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15903
      pshared.srcXOffset = Math.floor(destX1 < 0 ? -destX1 * dx : srcX1 * PConstants.PRECISIONF);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15904
      pshared.srcYOffset = Math.floor(destY1 < 0 ? -destY1 * dy : srcY1 * PConstants.PRECISIONF);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15905
      if (destX1 < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15906
        destW += destX1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15907
        destX1 = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15908
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15909
      if (destY1 < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15910
        destH += destY1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15911
        destY1 = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15912
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15913
      destW = Math.min(destW, screenW - destX1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15914
      destH = Math.min(destH, screenH - destY1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15915
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15916
      var destOffset = destY1 * screenW + destX1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15917
      var destColor;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15918
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15919
      pshared.srcBuffer = img.imageData.data;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15920
      pshared.iw = img.width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15921
      pshared.iw1 = img.width - 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15922
      pshared.ih1 = img.height - 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15923
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15924
      // cache for speed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15925
      var filterBilinear = p.filter_bilinear,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15926
        filterNewScanline = p.filter_new_scanline,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15927
        blendFunc = blendFuncs[mode],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15928
        blendedColor,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15929
        idx,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15930
        cULoffset,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15931
        cURoffset,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15932
        cLLoffset,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15933
        cLRoffset,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15934
        ALPHA_MASK = PConstants.ALPHA_MASK,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15935
        RED_MASK = PConstants.RED_MASK,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15936
        GREEN_MASK = PConstants.GREEN_MASK,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15937
        BLUE_MASK = PConstants.BLUE_MASK,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15938
        PREC_MAXVAL = PConstants.PREC_MAXVAL,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15939
        PRECISIONB = PConstants.PRECISIONB,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15940
        PREC_RED_SHIFT = PConstants.PREC_RED_SHIFT,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15941
        PREC_ALPHA_SHIFT = PConstants.PREC_ALPHA_SHIFT,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15942
        srcBuffer = pshared.srcBuffer,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15943
        min = Math.min;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15944
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15945
      for (y = 0; y < destH; y++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15946
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15947
        pshared.sX = pshared.srcXOffset;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15948
        pshared.fracV = pshared.srcYOffset & PREC_MAXVAL;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15949
        pshared.ifV = PREC_MAXVAL - pshared.fracV;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15950
        pshared.v1 = (pshared.srcYOffset >> PRECISIONB) * pshared.iw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15951
        pshared.v2 = min((pshared.srcYOffset >> PRECISIONB) + 1, pshared.ih1) * pshared.iw;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15952
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15953
        for (x = 0; x < destW; x++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15954
          idx = (destOffset + x) * 4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15955
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15956
          destColor = (destPixels[idx + 3] << 24) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15957
                      ALPHA_MASK | (destPixels[idx] << 16) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15958
                      RED_MASK   | (destPixels[idx + 1] << 8) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15959
                      GREEN_MASK |  destPixels[idx + 2] & BLUE_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15960
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15961
          pshared.fracU = pshared.sX & PREC_MAXVAL;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15962
          pshared.ifU = PREC_MAXVAL - pshared.fracU;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15963
          pshared.ul = (pshared.ifU * pshared.ifV) >> PRECISIONB;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15964
          pshared.ll = (pshared.ifU * pshared.fracV) >> PRECISIONB;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15965
          pshared.ur = (pshared.fracU * pshared.ifV) >> PRECISIONB;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15966
          pshared.lr = (pshared.fracU * pshared.fracV) >> PRECISIONB;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15967
          pshared.u1 = (pshared.sX >> PRECISIONB);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15968
          pshared.u2 = min(pshared.u1 + 1, pshared.iw1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15969
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15970
          cULoffset = (pshared.v1 + pshared.u1) * 4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15971
          cURoffset = (pshared.v1 + pshared.u2) * 4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15972
          cLLoffset = (pshared.v2 + pshared.u1) * 4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15973
          cLRoffset = (pshared.v2 + pshared.u2) * 4;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15974
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15975
          pshared.cUL = (srcBuffer[cULoffset + 3] << 24) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15976
                        ALPHA_MASK | (srcBuffer[cULoffset] << 16) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15977
                        RED_MASK   | (srcBuffer[cULoffset + 1] << 8) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15978
                        GREEN_MASK |  srcBuffer[cULoffset + 2] & BLUE_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15979
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15980
          pshared.cUR = (srcBuffer[cURoffset + 3] << 24) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15981
                        ALPHA_MASK | (srcBuffer[cURoffset] << 16) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15982
                        RED_MASK   | (srcBuffer[cURoffset + 1] << 8) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15983
                        GREEN_MASK |  srcBuffer[cURoffset + 2] & BLUE_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15984
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15985
          pshared.cLL = (srcBuffer[cLLoffset + 3] << 24) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15986
                        ALPHA_MASK | (srcBuffer[cLLoffset] << 16) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15987
                        RED_MASK   | (srcBuffer[cLLoffset + 1] << 8) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15988
                        GREEN_MASK |  srcBuffer[cLLoffset + 2] & BLUE_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15989
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15990
          pshared.cLR = (srcBuffer[cLRoffset + 3] << 24) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15991
                        ALPHA_MASK | (srcBuffer[cLRoffset] << 16) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15992
                        RED_MASK   | (srcBuffer[cLRoffset + 1] << 8) &
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15993
                        GREEN_MASK |  srcBuffer[cLRoffset + 2] & BLUE_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15994
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15995
          pshared.r = ((pshared.ul * ((pshared.cUL & RED_MASK) >> 16) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15996
                       pshared.ll * ((pshared.cLL & RED_MASK) >> 16) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15997
                       pshared.ur * ((pshared.cUR & RED_MASK) >> 16) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15998
                       pshared.lr * ((pshared.cLR & RED_MASK) >> 16)) << PREC_RED_SHIFT) & RED_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 15999
          pshared.g = ((pshared.ul * (pshared.cUL & GREEN_MASK) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16000
                       pshared.ll * (pshared.cLL & GREEN_MASK) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16001
                       pshared.ur * (pshared.cUR & GREEN_MASK) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16002
                       pshared.lr * (pshared.cLR & GREEN_MASK)) >>> PRECISIONB) & GREEN_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16003
          pshared.b = (pshared.ul * (pshared.cUL & BLUE_MASK) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16004
                       pshared.ll * (pshared.cLL & BLUE_MASK) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16005
                       pshared.ur * (pshared.cUR & BLUE_MASK) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16006
                       pshared.lr * (pshared.cLR & BLUE_MASK)) >>> PRECISIONB;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16007
          pshared.a = ((pshared.ul * ((pshared.cUL & ALPHA_MASK) >>> 24) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16008
                       pshared.ll * ((pshared.cLL & ALPHA_MASK) >>> 24) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16009
                       pshared.ur * ((pshared.cUR & ALPHA_MASK) >>> 24) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16010
                       pshared.lr * ((pshared.cLR & ALPHA_MASK) >>> 24)) << PREC_ALPHA_SHIFT) & ALPHA_MASK;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16011
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16012
          blendedColor = blendFunc(destColor, (pshared.a | pshared.r | pshared.g | pshared.b));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16013
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16014
          destPixels[idx]     = (blendedColor & RED_MASK) >>> 16;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16015
          destPixels[idx + 1] = (blendedColor & GREEN_MASK) >>> 8;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16016
          destPixels[idx + 2] = (blendedColor & BLUE_MASK);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16017
          destPixels[idx + 3] = (blendedColor & ALPHA_MASK) >>> 24;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16018
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16019
          pshared.sX += dx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16020
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16021
        destOffset += screenW;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16022
        pshared.srcYOffset += dy;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16023
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16024
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16025
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16026
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16027
    // Font handling
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16028
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16029
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16030
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16031
     * loadFont() Loads a font into a variable of type PFont.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16032
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16033
     * @param {String} name filename of the font to load
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16034
     * @param {int|float} size option font size (used internally)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16035
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16036
     * @returns {PFont} new PFont object
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16037
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16038
     * @see #PFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16039
     * @see #textFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16040
     * @see #text
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16041
     * @see #createFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16042
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16043
    p.loadFont = function(name, size) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16044
      if (name === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16045
        throw("font name required in loadFont.");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16046
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16047
      if (name.indexOf(".svg") === -1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16048
        if (size === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16049
          size = curTextFont.size;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16050
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16051
        return PFont.get(name, size);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16052
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16053
      // If the font is a glyph, calculate by SVG table
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16054
      var font = p.loadGlyphs(name);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16055
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16056
      return {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16057
        name: name,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16058
        css: '12px sans-serif',
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16059
        glyph: true,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16060
        units_per_em: font.units_per_em,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16061
        horiz_adv_x: 1 / font.units_per_em * font.horiz_adv_x,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16062
        ascent: font.ascent,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16063
        descent: font.descent,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16064
        width: function(str) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16065
          var width = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16066
          var len = str.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16067
          for (var i = 0; i < len; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16068
            try {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16069
              width += parseFloat(p.glyphLook(p.glyphTable[name], str[i]).horiz_adv_x);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16070
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16071
            catch(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16072
              Processing.debug(e);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16073
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16074
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16075
          return width / p.glyphTable[name].units_per_em;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16076
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16077
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16078
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16079
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16080
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16081
     * createFont() Loads a font into a variable of type PFont.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16082
     * Smooth and charset are ignored in Processing.js.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16083
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16084
     * @param {String}    name    filename of the font to load
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16085
     * @param {int|float} size    font size in pixels
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16086
     * @param {boolean}   smooth  not used in Processing.js
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16087
     * @param {char[]}    charset not used in Processing.js
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16088
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16089
     * @returns {PFont} new PFont object
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16090
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16091
     * @see #PFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16092
     * @see #textFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16093
     * @see #text
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16094
     * @see #loadFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16095
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16096
    p.createFont = function(name, size) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16097
      // because Processing.js only deals with real fonts,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16098
      // createFont is simply a wrapper for loadFont/2
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16099
      return p.loadFont(name, size);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16100
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16101
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16102
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16103
     * textFont() Sets the current font.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16104
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16105
     * @param {PFont}     pfont the PFont to load as current text font
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16106
     * @param {int|float} size optional font size in pixels
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16107
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16108
     * @see #createFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16109
     * @see #loadFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16110
     * @see #PFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16111
     * @see #text
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16112
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16113
    p.textFont = function(pfont, size) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16114
      if (size !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16115
        // If we're using an SVG glyph font, don't load from cache
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16116
        if (!pfont.glyph) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16117
          pfont = PFont.get(pfont.name, size);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16118
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16119
        curTextSize = size;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16120
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16121
      curTextFont = pfont;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16122
      curFontName = curTextFont.name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16123
      curTextAscent = curTextFont.ascent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16124
      curTextDescent = curTextFont.descent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16125
      curTextLeading = curTextFont.leading;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16126
      var curContext = drawing.$ensureContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16127
      curContext.font = curTextFont.css;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16128
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16129
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16130
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16131
     * textSize() Sets the current font size in pixels.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16132
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16133
     * @param {int|float} size font size in pixels
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16134
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16135
     * @see #textFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16136
     * @see #loadFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16137
     * @see #PFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16138
     * @see #text
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16139
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16140
    p.textSize = function(size) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16141
      if (size !== curTextSize) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16142
        curTextFont = PFont.get(curFontName, size);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16143
        curTextSize = size;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16144
        // recache metrics
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16145
        curTextAscent = curTextFont.ascent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16146
        curTextDescent = curTextFont.descent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16147
        curTextLeading = curTextFont.leading;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16148
        var curContext = drawing.$ensureContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16149
        curContext.font = curTextFont.css;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16150
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16151
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16152
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16153
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16154
     * textAscent() returns the maximum height a character extends above the baseline of the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16155
     * current font at its current size, in pixels.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16156
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16157
     * @returns {float} height of the current font above the baseline, at its current size, in pixels
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16158
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16159
     * @see #textDescent
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16160
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16161
    p.textAscent = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16162
      return curTextAscent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16163
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16164
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16165
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16166
     * textDescent() returns the maximum depth a character will protrude below the baseline of
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16167
     * the current font at its current size, in pixels.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16168
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16169
     * @returns {float} depth of the current font below the baseline, at its current size, in pixels
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16170
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16171
     * @see #textAscent
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16172
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16173
    p.textDescent = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16174
      return curTextDescent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16175
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16176
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16177
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16178
     * textLeading() Sets the current font's leading, which is the distance
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16179
     * from baseline to baseline over consecutive lines, with additional vertical
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16180
     * spacing taking into account. Usually this value is 1.2 or 1.25 times the
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16181
     * textsize, but this value can be changed to effect vertically compressed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16182
     * or stretched text.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16183
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16184
     * @param {int|float} the desired baseline-to-baseline size in pixels
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16185
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16186
    p.textLeading = function(leading) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16187
      curTextLeading = leading;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16188
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16189
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16190
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16191
     * textAlign() Sets the current alignment for drawing text.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16192
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16193
     * @param {int} ALIGN  Horizontal alignment, either LEFT, CENTER, or RIGHT
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16194
     * @param {int} YALIGN optional vertical alignment, either TOP, BOTTOM, CENTER, or BASELINE
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16195
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16196
     * @see #loadFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16197
     * @see #PFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16198
     * @see #text
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16199
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16200
    p.textAlign = function(xalign, yalign) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16201
      horizontalTextAlignment = xalign;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16202
      verticalTextAlignment = yalign || PConstants.BASELINE;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16203
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16204
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16205
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16206
     * toP5String converts things with arbitrary data type into
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16207
     * string values, for text rendering.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16208
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16209
     * @param {any} any object that can be converted into a string
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16210
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16211
     * @return {String} the string representation of the input
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16212
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16213
    function toP5String(obj) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16214
      if(obj instanceof String) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16215
        return obj;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16216
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16217
      if(typeof obj === 'number') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16218
        // check if an int
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16219
        if(obj === (0 | obj)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16220
          return obj.toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16221
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16222
        return p.nf(obj, 0, 3);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16223
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16224
      if(obj === null || obj === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16225
        return "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16226
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16227
      return obj.toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16228
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16229
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16230
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16231
     * textWidth() Calculates and returns the width of any character or text string in pixels.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16232
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16233
     * @param {char|String} str char or String to be measured
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16234
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16235
     * @return {float} width of char or String in pixels
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16236
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16237
     * @see #loadFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16238
     * @see #PFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16239
     * @see #text
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16240
     * @see #textFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16241
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16242
    Drawing2D.prototype.textWidth = function(str) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16243
      var lines = toP5String(str).split(/\r?\n/g), width = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16244
      var i, linesCount = lines.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16245
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16246
      curContext.font = curTextFont.css;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16247
      for (i = 0; i < linesCount; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16248
        width = Math.max(width, curTextFont.measureTextWidth(lines[i]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16249
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16250
      return width | 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16251
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16252
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16253
    Drawing3D.prototype.textWidth = function(str) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16254
      var lines = toP5String(str).split(/\r?\n/g), width = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16255
      var i, linesCount = lines.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16256
      if (textcanvas === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16257
        textcanvas = document.createElement("canvas");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16258
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16259
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16260
      var textContext = textcanvas.getContext("2d");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16261
      textContext.font = curTextFont.css;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16262
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16263
      for (i = 0; i < linesCount; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16264
        width = Math.max(width, textContext.measureText(lines[i]).width);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16265
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16266
      return width | 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16267
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16268
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16269
    // A lookup table for characters that can not be referenced by Object
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16270
    p.glyphLook = function(font, chr) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16271
      try {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16272
        switch (chr) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16273
        case "1":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16274
          return font.one;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16275
        case "2":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16276
          return font.two;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16277
        case "3":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16278
          return font.three;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16279
        case "4":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16280
          return font.four;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16281
        case "5":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16282
          return font.five;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16283
        case "6":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16284
          return font.six;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16285
        case "7":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16286
          return font.seven;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16287
        case "8":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16288
          return font.eight;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16289
        case "9":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16290
          return font.nine;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16291
        case "0":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16292
          return font.zero;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16293
        case " ":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16294
          return font.space;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16295
        case "$":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16296
          return font.dollar;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16297
        case "!":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16298
          return font.exclam;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16299
        case '"':
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16300
          return font.quotedbl;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16301
        case "#":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16302
          return font.numbersign;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16303
        case "%":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16304
          return font.percent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16305
        case "&":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16306
          return font.ampersand;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16307
        case "'":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16308
          return font.quotesingle;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16309
        case "(":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16310
          return font.parenleft;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16311
        case ")":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16312
          return font.parenright;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16313
        case "*":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16314
          return font.asterisk;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16315
        case "+":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16316
          return font.plus;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16317
        case ",":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16318
          return font.comma;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16319
        case "-":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16320
          return font.hyphen;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16321
        case ".":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16322
          return font.period;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16323
        case "/":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16324
          return font.slash;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16325
        case "_":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16326
          return font.underscore;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16327
        case ":":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16328
          return font.colon;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16329
        case ";":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16330
          return font.semicolon;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16331
        case "<":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16332
          return font.less;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16333
        case "=":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16334
          return font.equal;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16335
        case ">":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16336
          return font.greater;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16337
        case "?":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16338
          return font.question;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16339
        case "@":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16340
          return font.at;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16341
        case "[":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16342
          return font.bracketleft;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16343
        case "\\":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16344
          return font.backslash;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16345
        case "]":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16346
          return font.bracketright;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16347
        case "^":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16348
          return font.asciicircum;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16349
        case "`":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16350
          return font.grave;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16351
        case "{":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16352
          return font.braceleft;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16353
        case "|":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16354
          return font.bar;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16355
        case "}":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16356
          return font.braceright;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16357
        case "~":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16358
          return font.asciitilde;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16359
          // If the character is not 'special', access it by object reference
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16360
        default:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16361
          return font[chr];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16362
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16363
      } catch(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16364
        Processing.debug(e);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16365
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16366
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16367
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16368
    // Print some text to the Canvas
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16369
    Drawing2D.prototype.text$line = function(str, x, y, z, align) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16370
      var textWidth = 0, xOffset = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16371
      // If the font is a standard Canvas font...
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16372
      if (!curTextFont.glyph) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16373
        if (str && ("fillText" in curContext)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16374
          if (isFillDirty) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16375
            curContext.fillStyle = p.color.toString(currentFillColor);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16376
            isFillDirty = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16377
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16378
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16379
          // horizontal offset/alignment
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16380
          if(align === PConstants.RIGHT || align === PConstants.CENTER) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16381
            textWidth = curTextFont.measureTextWidth(str);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16382
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16383
            if(align === PConstants.RIGHT) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16384
              xOffset = -textWidth;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16385
            } else { // if(align === PConstants.CENTER)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16386
              xOffset = -textWidth/2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16387
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16388
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16389
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16390
          curContext.fillText(str, x+xOffset, y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16391
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16392
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16393
        // If the font is a Batik SVG font...
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16394
        var font = p.glyphTable[curFontName];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16395
        saveContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16396
        curContext.translate(x, y + curTextSize);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16397
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16398
        // horizontal offset/alignment
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16399
        if(align === PConstants.RIGHT || align === PConstants.CENTER) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16400
          textWidth = font.width(str);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16401
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16402
          if(align === PConstants.RIGHT) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16403
            xOffset = -textWidth;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16404
          } else { // if(align === PConstants.CENTER)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16405
            xOffset = -textWidth/2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16406
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16407
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16408
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16409
        var upem   = font.units_per_em,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16410
          newScale = 1 / upem * curTextSize;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16411
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16412
        curContext.scale(newScale, newScale);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16413
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16414
        for (var i=0, len=str.length; i < len; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16415
          // Test character against glyph table
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16416
          try {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16417
            p.glyphLook(font, str[i]).draw();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16418
          } catch(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16419
            Processing.debug(e);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16420
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16421
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16422
        restoreContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16423
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16424
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16425
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16426
    Drawing3D.prototype.text$line = function(str, x, y, z, align) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16427
      // handle case for 3d text
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16428
      if (textcanvas === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16429
        textcanvas = document.createElement("canvas");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16430
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16431
      var oldContext = curContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16432
      curContext = textcanvas.getContext("2d");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16433
      curContext.font = curTextFont.css;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16434
      var textWidth = curTextFont.measureTextWidth(str);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16435
      textcanvas.width = textWidth;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16436
      textcanvas.height = curTextSize;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16437
      curContext = textcanvas.getContext("2d"); // refreshes curContext
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16438
      curContext.font = curTextFont.css;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16439
      curContext.textBaseline="top";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16440
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16441
      // paint on 2D canvas
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16442
      Drawing2D.prototype.text$line(str,0,0,0,PConstants.LEFT);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16443
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16444
      // use it as a texture
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16445
      var aspect = textcanvas.width/textcanvas.height;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16446
      curContext = oldContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16447
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16448
      curContext.bindTexture(curContext.TEXTURE_2D, textTex);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16449
      curContext.texImage2D(curContext.TEXTURE_2D, 0, curContext.RGBA, curContext.RGBA, curContext.UNSIGNED_BYTE, textcanvas);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16450
      curContext.texParameteri(curContext.TEXTURE_2D, curContext.TEXTURE_MAG_FILTER, curContext.LINEAR);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16451
      curContext.texParameteri(curContext.TEXTURE_2D, curContext.TEXTURE_MIN_FILTER, curContext.LINEAR);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16452
      curContext.texParameteri(curContext.TEXTURE_2D, curContext.TEXTURE_WRAP_T, curContext.CLAMP_TO_EDGE);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16453
      curContext.texParameteri(curContext.TEXTURE_2D, curContext.TEXTURE_WRAP_S, curContext.CLAMP_TO_EDGE);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16454
      // If we don't have a power of two texture, we can't mipmap it.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16455
      // curContext.generateMipmap(curContext.TEXTURE_2D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16456
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16457
      // horizontal offset/alignment
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16458
      var xOffset = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16459
      if (align === PConstants.RIGHT) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16460
        xOffset = -textWidth;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16461
      } else if(align === PConstants.CENTER) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16462
        xOffset = -textWidth/2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16463
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16464
      var model = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16465
      var scalefactor = curTextSize * 0.5;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16466
      model.translate(x+xOffset-scalefactor/2, y-scalefactor, z);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16467
      model.scale(-aspect*scalefactor, -scalefactor, scalefactor);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16468
      model.translate(-1, -1, -1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16469
      model.transpose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16470
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16471
      var view = new PMatrix3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16472
      view.scale(1, -1, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16473
      view.apply(modelView.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16474
      view.transpose();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16475
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16476
      curContext.useProgram(programObject2D);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16477
      vertexAttribPointer("vertex2d", programObject2D, "Vertex", 3, textBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16478
      vertexAttribPointer("aTextureCoord2d", programObject2D, "aTextureCoord", 2, textureBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16479
      uniformi("uSampler2d", programObject2D, "uSampler", [0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16480
      uniformi("picktype2d", programObject2D, "picktype", 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16481
      uniformMatrix("model2d", programObject2D, "model", false,  model.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16482
      uniformMatrix("view2d", programObject2D, "view", false, view.array());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16483
      uniformf("color2d", programObject2D, "color", fillStyle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16484
      curContext.bindBuffer(curContext.ELEMENT_ARRAY_BUFFER, indexBuffer);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16485
      curContext.drawElements(curContext.TRIANGLES, 6, curContext.UNSIGNED_SHORT, 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16486
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16487
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16488
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16489
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16490
    * unbounded text function (z is an optional argument)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16491
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16492
    function text$4(str, x, y, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16493
      var lines, linesCount;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16494
      if(str.indexOf('\n') < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16495
        lines = [str];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16496
        linesCount = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16497
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16498
        lines = str.split(/\r?\n/g);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16499
        linesCount = lines.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16500
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16501
      // handle text line-by-line
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16502
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16503
      var yOffset = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16504
      if(verticalTextAlignment === PConstants.TOP) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16505
        yOffset = curTextAscent + curTextDescent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16506
      } else if(verticalTextAlignment === PConstants.CENTER) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16507
        yOffset = curTextAscent/2 - (linesCount-1)*curTextLeading/2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16508
      } else if(verticalTextAlignment === PConstants.BOTTOM) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16509
        yOffset = -(curTextDescent + (linesCount-1)*curTextLeading);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16510
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16511
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16512
      for(var i=0;i<linesCount;++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16513
        var line = lines[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16514
        drawing.text$line(line, x, y + yOffset, z, horizontalTextAlignment);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16515
        yOffset += curTextLeading;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16516
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16517
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16518
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16519
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16520
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16521
    * box-bounded text function (z is an optional argument)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16522
    */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16523
    function text$6(str, x, y, width, height, z) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16524
      // 'fail' on 0-valued dimensions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16525
      if (str.length === 0 || width === 0 || height === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16526
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16527
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16528
      // also 'fail' if the text height is larger than the bounding height
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16529
      if(curTextSize > height) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16530
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16531
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16532
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16533
      var spaceMark = -1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16534
      var start = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16535
      var lineWidth = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16536
      var drawCommands = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16537
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16538
      // run through text, character-by-character
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16539
      for (var charPos=0, len=str.length; charPos < len; charPos++)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16540
      {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16541
        var currentChar = str[charPos];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16542
        var spaceChar = (currentChar === " ");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16543
        var letterWidth = curTextFont.measureTextWidth(currentChar);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16544
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16545
        // if we aren't looking at a newline, and the text still fits, keep processing
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16546
        if (currentChar !== "\n" && (lineWidth + letterWidth <= width)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16547
          if (spaceChar) { spaceMark = charPos; }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16548
          lineWidth += letterWidth;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16549
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16550
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16551
        // if we're looking at a newline, or the text no longer fits, push the section that fit into the drawcommand list
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16552
        else
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16553
        {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16554
          if (spaceMark + 1 === start) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16555
            if(charPos>0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16556
              // Whole line without spaces so far.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16557
              spaceMark = charPos;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16558
            } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16559
              // 'fail', because the line can't even fit the first character
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16560
              return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16561
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16562
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16563
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16564
          if (currentChar === "\n") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16565
            drawCommands.push({text:str.substring(start, charPos), width: lineWidth});
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16566
            start = charPos + 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16567
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16568
            // current is not a newline, which means the line doesn't fit in box. push text.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16569
            // In Processing 1.5.1, the space is also pushed, so we push up to spaceMark+1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16570
            // rather than up to spaceMark, as was the case for Processing 1.5 and earlier.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16571
            drawCommands.push({text:str.substring(start, spaceMark+1), width: lineWidth});
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16572
            start = spaceMark + 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16573
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16574
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16575
          // newline + return
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16576
          lineWidth = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16577
          charPos = start - 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16578
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16579
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16580
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16581
      // push the remaining text
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16582
      if (start < len) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16583
        drawCommands.push({text:str.substring(start), width: lineWidth});
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16584
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16585
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16586
      // resolve horizontal alignment
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16587
      var xOffset = 1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16588
          yOffset = curTextAscent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16589
      if (horizontalTextAlignment === PConstants.CENTER) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16590
        xOffset = width/2;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16591
      } else if (horizontalTextAlignment === PConstants.RIGHT) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16592
        xOffset = width;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16593
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16594
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16595
      // resolve vertical alignment
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16596
      var linesCount = drawCommands.length,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16597
          visibleLines = Math.min(linesCount, Math.floor(height/curTextLeading));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16598
      if(verticalTextAlignment === PConstants.TOP) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16599
        yOffset = curTextAscent + curTextDescent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16600
      } else if(verticalTextAlignment === PConstants.CENTER) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16601
        yOffset = (height/2) - curTextLeading * (visibleLines/2 - 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16602
      } else if(verticalTextAlignment === PConstants.BOTTOM) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16603
        yOffset = curTextDescent + curTextLeading;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16604
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16605
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16606
      var command,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16607
          drawCommand,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16608
          leading;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16609
      for (command = 0; command < linesCount; command++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16610
        leading = command * curTextLeading;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16611
        // stop if not enough space for one more line draw
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16612
        if (yOffset + leading > height - curTextDescent) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16613
          break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16614
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16615
        drawCommand = drawCommands[command];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16616
        drawing.text$line(drawCommand.text, x + xOffset, y + yOffset + leading, z, horizontalTextAlignment);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16617
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16618
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16619
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16620
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16621
     * text() Draws text to the screen.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16622
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16623
     * @param {String|char|int|float} data       the alphanumeric symbols to be displayed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16624
     * @param {int|float}             x          x-coordinate of text
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16625
     * @param {int|float}             y          y-coordinate of text
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16626
     * @param {int|float}             z          optional z-coordinate of text
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16627
     * @param {String}                stringdata optional letters to be displayed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16628
     * @param {int|float}             width      optional width of text box
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16629
     * @param {int|float}             height     optional height of text box
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16630
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16631
     * @see #textAlign
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16632
     * @see #textMode
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16633
     * @see #loadFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16634
     * @see #PFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16635
     * @see #textFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16636
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16637
    p.text = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16638
      if (textMode === PConstants.SHAPE) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16639
        // TODO: requires beginRaw function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16640
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16641
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16642
      if (arguments.length === 3) { // for text( str, x, y)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16643
        text$4(toP5String(arguments[0]), arguments[1], arguments[2], 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16644
      } else if (arguments.length === 4) { // for text( str, x, y, z)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16645
        text$4(toP5String(arguments[0]), arguments[1], arguments[2], arguments[3]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16646
      } else if (arguments.length === 5) { // for text( str, x, y , width, height)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16647
        text$6(toP5String(arguments[0]), arguments[1], arguments[2], arguments[3], arguments[4], 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16648
      } else if (arguments.length === 6) { // for text( stringdata, x, y , width, height, z)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16649
        text$6(toP5String(arguments[0]), arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16650
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16651
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16652
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16653
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16654
     * Sets the way text draws to the screen. In the default configuration (the MODEL mode), it's possible to rotate,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16655
     * scale, and place letters in two and three dimensional space. <br /><br /> Changing to SCREEN mode draws letters
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16656
     * directly to the front of the window and greatly increases rendering quality and speed when used with the P2D and
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16657
     * P3D renderers. textMode(SCREEN) with OPENGL and JAVA2D (the default) renderers will generally be slower, though
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16658
     * pixel accurate with P2D and P3D. With textMode(SCREEN), the letters draw at the actual size of the font (in pixels)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16659
     * and therefore calls to <b>textSize()</b> will not affect the size of the letters. To create a font at the size you
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16660
     * desire, use the "Create font..." option in the Tools menu, or use the createFont() function. When using textMode(SCREEN),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16661
     * any z-coordinate passed to a text() command will be ignored, because your computer screen is...flat!
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16662
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16663
     * @param {int} MODE Either MODEL, SCREEN or SHAPE (not yet supported)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16664
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16665
     * @see loadFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16666
     * @see PFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16667
     * @see text
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16668
     * @see textFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16669
     * @see createFont
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16670
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16671
    p.textMode = function(mode){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16672
      textMode = mode;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16673
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16674
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16675
    // Load Batik SVG Fonts and parse to pre-def objects for quick rendering
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16676
    p.loadGlyphs = function(url) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16677
      var x, y, cx, cy, nx, ny, d, a, lastCom, lenC, horiz_adv_x, getXY = '[0-9\\-]+', path;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16678
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16679
      // Return arrays of SVG commands and coords
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16680
      // get this to use p.matchAll() - will need to work around the lack of null return
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16681
      var regex = function(needle, hay) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16682
        var i = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16683
          results = [],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16684
          latest, regexp = new RegExp(needle, "g");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16685
        latest = results[i] = regexp.exec(hay);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16686
        while (latest) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16687
          i++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16688
          latest = results[i] = regexp.exec(hay);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16689
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16690
        return results;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16691
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16692
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16693
      var buildPath = function(d) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16694
        var c = regex("[A-Za-z][0-9\\- ]+|Z", d);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16695
        var beforePathDraw = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16696
          saveContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16697
          return drawing.$ensureContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16698
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16699
        var afterPathDraw = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16700
          executeContextFill();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16701
          executeContextStroke();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16702
          restoreContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16703
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16704
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16705
        // Begin storing path object
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16706
        path = "return {draw:function(){var curContext=beforePathDraw();curContext.beginPath();";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16707
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16708
        x = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16709
        y = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16710
        cx = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16711
        cy = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16712
        nx = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16713
        ny = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16714
        d = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16715
        a = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16716
        lastCom = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16717
        lenC = c.length - 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16718
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16719
        // Loop through SVG commands translating to canvas eqivs functions in path object
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16720
        for (var j = 0; j < lenC; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16721
          var com = c[j][0], xy = regex(getXY, com);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16722
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16723
          switch (com[0]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16724
            case "M":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16725
              //curContext.moveTo(x,-y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16726
              x = parseFloat(xy[0][0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16727
              y = parseFloat(xy[1][0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16728
              path += "curContext.moveTo(" + x + "," + (-y) + ");";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16729
              break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16730
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16731
            case "L":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16732
              //curContext.lineTo(x,-y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16733
              x = parseFloat(xy[0][0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16734
              y = parseFloat(xy[1][0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16735
              path += "curContext.lineTo(" + x + "," + (-y) + ");";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16736
              break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16737
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16738
            case "H":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16739
              //curContext.lineTo(x,-y)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16740
              x = parseFloat(xy[0][0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16741
              path += "curContext.lineTo(" + x + "," + (-y) + ");";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16742
              break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16743
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16744
            case "V":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16745
              //curContext.lineTo(x,-y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16746
              y = parseFloat(xy[0][0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16747
              path += "curContext.lineTo(" + x + "," + (-y) + ");";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16748
              break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16749
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16750
            case "T":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16751
              //curContext.quadraticCurveTo(cx,-cy,nx,-ny);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16752
              nx = parseFloat(xy[0][0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16753
              ny = parseFloat(xy[1][0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16754
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16755
              if (lastCom === "Q" || lastCom === "T") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16756
                d = Math.sqrt(Math.pow(x - cx, 2) + Math.pow(cy - y, 2));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16757
                a = Math.PI + Math.atan2(cx - x, cy - y);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16758
                cx = x + (Math.sin(a) * (d));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16759
                cy = y + (Math.cos(a) * (d));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16760
              } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16761
                cx = x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16762
                cy = y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16763
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16764
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16765
              path += "curContext.quadraticCurveTo(" + cx + "," + (-cy) + "," + nx + "," + (-ny) + ");";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16766
              x = nx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16767
              y = ny;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16768
              break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16769
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16770
            case "Q":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16771
              //curContext.quadraticCurveTo(cx,-cy,nx,-ny);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16772
              cx = parseFloat(xy[0][0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16773
              cy = parseFloat(xy[1][0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16774
              nx = parseFloat(xy[2][0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16775
              ny = parseFloat(xy[3][0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16776
              path += "curContext.quadraticCurveTo(" + cx + "," + (-cy) + "," + nx + "," + (-ny) + ");";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16777
              x = nx;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16778
              y = ny;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16779
              break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16780
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16781
            case "Z":
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16782
              //curContext.closePath();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16783
              path += "curContext.closePath();";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16784
              break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16785
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16786
          lastCom = com[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16787
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16788
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16789
        path += "afterPathDraw();";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16790
        path += "curContext.translate(" + horiz_adv_x + ",0);";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16791
        path += "}}";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16792
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16793
        return ((new Function("beforePathDraw", "afterPathDraw", path))(beforePathDraw, afterPathDraw));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16794
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16795
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16796
      // Parse SVG font-file into block of Canvas commands
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16797
      var parseSVGFont = function(svg) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16798
        // Store font attributes
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16799
        var font = svg.getElementsByTagName("font");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16800
        p.glyphTable[url].horiz_adv_x = font[0].getAttribute("horiz-adv-x");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16801
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16802
        var font_face = svg.getElementsByTagName("font-face")[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16803
        p.glyphTable[url].units_per_em = parseFloat(font_face.getAttribute("units-per-em"));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16804
        p.glyphTable[url].ascent = parseFloat(font_face.getAttribute("ascent"));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16805
        p.glyphTable[url].descent = parseFloat(font_face.getAttribute("descent"));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16806
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16807
        var glyph = svg.getElementsByTagName("glyph"),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16808
          len = glyph.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16809
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16810
        // Loop through each glyph in the SVG
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16811
        for (var i = 0; i < len; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16812
          // Store attributes for this glyph
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16813
          var unicode = glyph[i].getAttribute("unicode");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16814
          var name = glyph[i].getAttribute("glyph-name");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16815
          horiz_adv_x = glyph[i].getAttribute("horiz-adv-x");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16816
          if (horiz_adv_x === null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16817
            horiz_adv_x = p.glyphTable[url].horiz_adv_x;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16818
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16819
          d = glyph[i].getAttribute("d");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16820
          // Split path commands in glpyh
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16821
          if (d !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16822
            path = buildPath(d);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16823
            // Store glyph data to table object
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16824
            p.glyphTable[url][name] = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16825
              name: name,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16826
              unicode: unicode,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16827
              horiz_adv_x: horiz_adv_x,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16828
              draw: path.draw
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16829
            };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16830
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16831
        } // finished adding glyphs to table
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16832
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16833
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16834
      // Load and parse Batik SVG font as XML into a Processing Glyph object
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16835
      var loadXML = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16836
        var xmlDoc;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16837
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16838
        try {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16839
          xmlDoc = document.implementation.createDocument("", "", null);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16840
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16841
        catch(e_fx_op) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16842
          Processing.debug(e_fx_op.message);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16843
          return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16844
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16845
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16846
        try {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16847
          xmlDoc.async = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16848
          xmlDoc.load(url);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16849
          parseSVGFont(xmlDoc.getElementsByTagName("svg")[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16850
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16851
        catch(e_sf_ch) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16852
          // Google Chrome, Safari etc.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16853
          Processing.debug(e_sf_ch);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16854
          try {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16855
            var xmlhttp = new window.XMLHttpRequest();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16856
            xmlhttp.open("GET", url, false);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16857
            xmlhttp.send(null);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16858
            parseSVGFont(xmlhttp.responseXML.documentElement);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16859
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16860
          catch(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16861
            Processing.debug(e_sf_ch);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16862
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16863
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16864
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16865
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16866
      // Create a new object in glyphTable to store this font
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16867
      p.glyphTable[url] = {};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16868
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16869
      // Begin loading the Batik SVG font...
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16870
      loadXML(url);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16871
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16872
      // Return the loaded font for attribute grabbing
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16873
      return p.glyphTable[url];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16874
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16875
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16876
    /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16877
     * Gets the sketch parameter value. The parameter can be defined as the canvas attribute with
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16878
     * the "data-processing-" prefix or provided in the pjs directive (e.g. param-test="52").
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16879
     * The function tries the canvas attributes, then the pjs directive content.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16880
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16881
     * @param   {String}    name          The name of the param to read.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16882
     *
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16883
     * @returns {String}    The parameter value, or null if parameter is not defined.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16884
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16885
    p.param = function(name) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16886
      // trying attribute that was specified in CANVAS
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16887
      var attributeName = "data-processing-" + name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16888
      if (curElement.hasAttribute(attributeName)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16889
        return curElement.getAttribute(attributeName);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16890
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16891
      // trying child PARAM elements of the CANVAS
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16892
      for (var i = 0, len = curElement.childNodes.length; i < len; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16893
        var item = curElement.childNodes.item(i);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16894
        if (item.nodeType !== 1 || item.tagName.toLowerCase() !== "param") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16895
          continue;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16896
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16897
        if (item.getAttribute("name") === name) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16898
          return item.getAttribute("value");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16899
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16900
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16901
      // fallback to default params
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16902
      if (curSketch.params.hasOwnProperty(name)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16903
        return curSketch.params[name];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16904
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16905
      return null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16906
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16907
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16908
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16909
    // 2D/3D methods wiring utils
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16910
    ////////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16911
    function wireDimensionalFunctions(mode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16912
      // Drawing2D/Drawing3D
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16913
      if (mode === '3D') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16914
        drawing = new Drawing3D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16915
      } else if (mode === '2D') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16916
        drawing = new Drawing2D();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16917
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16918
        drawing = new DrawingPre();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16919
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16920
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16921
      // Wire up functions (Use DrawingPre properties names)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16922
      for (var i in DrawingPre.prototype) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16923
        if (DrawingPre.prototype.hasOwnProperty(i) && i.indexOf("$") < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16924
          p[i] = drawing[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16925
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16926
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16927
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16928
      // Run initialization
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16929
      drawing.$init();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16930
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16931
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16932
    function createDrawingPreFunction(name) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16933
      return function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16934
        wireDimensionalFunctions("2D");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16935
        return drawing[name].apply(this, arguments);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16936
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16937
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16938
    DrawingPre.prototype.translate = createDrawingPreFunction("translate");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16939
    DrawingPre.prototype.scale = createDrawingPreFunction("scale");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16940
    DrawingPre.prototype.pushMatrix = createDrawingPreFunction("pushMatrix");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16941
    DrawingPre.prototype.popMatrix = createDrawingPreFunction("popMatrix");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16942
    DrawingPre.prototype.resetMatrix = createDrawingPreFunction("resetMatrix");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16943
    DrawingPre.prototype.applyMatrix = createDrawingPreFunction("applyMatrix");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16944
    DrawingPre.prototype.rotate = createDrawingPreFunction("rotate");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16945
    DrawingPre.prototype.rotateZ = createDrawingPreFunction("rotateZ");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16946
    DrawingPre.prototype.redraw = createDrawingPreFunction("redraw");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16947
    DrawingPre.prototype.toImageData = createDrawingPreFunction("toImageData");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16948
    DrawingPre.prototype.ambientLight = createDrawingPreFunction("ambientLight");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16949
    DrawingPre.prototype.directionalLight = createDrawingPreFunction("directionalLight");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16950
    DrawingPre.prototype.lightFalloff = createDrawingPreFunction("lightFalloff");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16951
    DrawingPre.prototype.lightSpecular = createDrawingPreFunction("lightSpecular");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16952
    DrawingPre.prototype.pointLight = createDrawingPreFunction("pointLight");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16953
    DrawingPre.prototype.noLights = createDrawingPreFunction("noLights");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16954
    DrawingPre.prototype.spotLight = createDrawingPreFunction("spotLight");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16955
    DrawingPre.prototype.beginCamera = createDrawingPreFunction("beginCamera");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16956
    DrawingPre.prototype.endCamera = createDrawingPreFunction("endCamera");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16957
    DrawingPre.prototype.frustum = createDrawingPreFunction("frustum");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16958
    DrawingPre.prototype.box = createDrawingPreFunction("box");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16959
    DrawingPre.prototype.sphere = createDrawingPreFunction("sphere");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16960
    DrawingPre.prototype.ambient = createDrawingPreFunction("ambient");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16961
    DrawingPre.prototype.emissive = createDrawingPreFunction("emissive");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16962
    DrawingPre.prototype.shininess = createDrawingPreFunction("shininess");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16963
    DrawingPre.prototype.specular = createDrawingPreFunction("specular");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16964
    DrawingPre.prototype.fill = createDrawingPreFunction("fill");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16965
    DrawingPre.prototype.stroke = createDrawingPreFunction("stroke");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16966
    DrawingPre.prototype.strokeWeight = createDrawingPreFunction("strokeWeight");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16967
    DrawingPre.prototype.smooth = createDrawingPreFunction("smooth");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16968
    DrawingPre.prototype.noSmooth = createDrawingPreFunction("noSmooth");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16969
    DrawingPre.prototype.point = createDrawingPreFunction("point");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16970
    DrawingPre.prototype.vertex = createDrawingPreFunction("vertex");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16971
    DrawingPre.prototype.endShape = createDrawingPreFunction("endShape");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16972
    DrawingPre.prototype.bezierVertex = createDrawingPreFunction("bezierVertex");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16973
    DrawingPre.prototype.curveVertex = createDrawingPreFunction("curveVertex");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16974
    DrawingPre.prototype.curve = createDrawingPreFunction("curve");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16975
    DrawingPre.prototype.line = createDrawingPreFunction("line");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16976
    DrawingPre.prototype.bezier = createDrawingPreFunction("bezier");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16977
    DrawingPre.prototype.rect = createDrawingPreFunction("rect");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16978
    DrawingPre.prototype.ellipse = createDrawingPreFunction("ellipse");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16979
    DrawingPre.prototype.background = createDrawingPreFunction("background");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16980
    DrawingPre.prototype.image = createDrawingPreFunction("image");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16981
    DrawingPre.prototype.textWidth = createDrawingPreFunction("textWidth");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16982
    DrawingPre.prototype.text$line = createDrawingPreFunction("text$line");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16983
    DrawingPre.prototype.$ensureContext = createDrawingPreFunction("$ensureContext");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16984
    DrawingPre.prototype.$newPMatrix = createDrawingPreFunction("$newPMatrix");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16985
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16986
    DrawingPre.prototype.size = function(aWidth, aHeight, aMode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16987
      wireDimensionalFunctions(aMode === PConstants.WEBGL ? "3D" : "2D");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16988
      p.size(aWidth, aHeight, aMode);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16989
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16990
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16991
    DrawingPre.prototype.$init = nop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16992
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16993
    Drawing2D.prototype.$init = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16994
      // Setup default 2d canvas context.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16995
      // Moving this here removes the number of times we need to check the 3D variable
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16996
      p.size(p.width, p.height);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16997
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16998
      curContext.lineCap = 'round';
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 16999
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17000
      // Set default stroke and fill color
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17001
      p.noSmooth();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17002
      p.disableContextMenu();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17003
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17004
    Drawing3D.prototype.$init = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17005
      // For ref/perf test compatibility until those are fixed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17006
      p.use3DContext = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17007
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17008
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17009
    DrawingShared.prototype.$ensureContext = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17010
      return curContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17011
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17012
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17013
    //////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17014
    // Touch and Mouse event handling
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17015
    //////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17016
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17017
    function calculateOffset(curElement, event) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17018
      var element = curElement,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17019
        offsetX = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17020
        offsetY = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17021
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17022
      p.pmouseX = p.mouseX;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17023
      p.pmouseY = p.mouseY;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17024
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17025
      // Find element offset
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17026
      if (element.offsetParent) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17027
        do {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17028
          offsetX += element.offsetLeft;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17029
          offsetY += element.offsetTop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17030
        } while (!!(element = element.offsetParent));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17031
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17032
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17033
      // Find Scroll offset
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17034
      element = curElement;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17035
      do {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17036
        offsetX -= element.scrollLeft || 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17037
        offsetY -= element.scrollTop || 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17038
      } while (!!(element = element.parentNode));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17039
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17040
      // Add padding and border style widths to offset
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17041
      offsetX += stylePaddingLeft;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17042
      offsetY += stylePaddingTop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17043
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17044
      offsetX += styleBorderLeft;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17045
      offsetY += styleBorderTop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17046
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17047
      // Take into account any scrolling done
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17048
      offsetX += window.pageXOffset;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17049
      offsetY += window.pageYOffset;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17050
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17051
      return {'X':offsetX,'Y':offsetY};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17052
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17053
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17054
    function updateMousePosition(curElement, event) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17055
      var offset = calculateOffset(curElement, event);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17056
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17057
      // Dropping support for IE clientX and clientY, switching to pageX and pageY so we don't have to calculate scroll offset.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17058
      // Removed in ticket #184. See rev: 2f106d1c7017fed92d045ba918db47d28e5c16f4
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17059
      p.mouseX = event.pageX - offset.X;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17060
      p.mouseY = event.pageY - offset.Y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17061
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17062
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17063
    // Return a TouchEvent with canvas-specific x/y co-ordinates
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17064
    function addTouchEventOffset(t) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17065
      var offset = calculateOffset(t.changedTouches[0].target, t.changedTouches[0]),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17066
          i;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17067
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17068
      for (i = 0; i < t.touches.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17069
        var touch = t.touches[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17070
        touch.offsetX = touch.pageX - offset.X;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17071
        touch.offsetY = touch.pageY - offset.Y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17072
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17073
      for (i = 0; i < t.targetTouches.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17074
        var targetTouch = t.targetTouches[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17075
        targetTouch.offsetX = targetTouch.pageX - offset.X;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17076
        targetTouch.offsetY = targetTouch.pageY - offset.Y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17077
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17078
      for (i = 0; i < t.changedTouches.length; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17079
        var changedTouch = t.changedTouches[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17080
        changedTouch.offsetX = changedTouch.pageX - offset.X;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17081
        changedTouch.offsetY = changedTouch.pageY - offset.Y;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17082
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17083
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17084
      return t;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17085
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17086
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17087
    attachEventHandler(curElement, "touchstart", function (t) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17088
      // Removes unwanted behaviour of the canvas when touching canvas
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17089
      curElement.setAttribute("style","-webkit-user-select: none");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17090
      curElement.setAttribute("onclick","void(0)");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17091
      curElement.setAttribute("style","-webkit-tap-highlight-color:rgba(0,0,0,0)");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17092
      // Loop though eventHandlers and remove mouse listeners
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17093
      for (var i=0, ehl=eventHandlers.length; i<ehl; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17094
        var type = eventHandlers[i].type;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17095
        // Have this function remove itself from the eventHandlers list too
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17096
        if (type === "mouseout" ||  type === "mousemove" ||
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17097
            type === "mousedown" || type === "mouseup" ||
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17098
            type === "DOMMouseScroll" || type === "mousewheel" || type === "touchstart") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17099
          detachEventHandler(eventHandlers[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17100
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17101
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17102
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17103
      // If there are any native touch events defined in the sketch, connect all of them
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17104
      // Otherwise, connect all of the emulated mouse events
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17105
      if (p.touchStart !== undef || p.touchMove !== undef ||
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17106
          p.touchEnd !== undef || p.touchCancel !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17107
        attachEventHandler(curElement, "touchstart", function(t) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17108
          if (p.touchStart !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17109
            t = addTouchEventOffset(t);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17110
            p.touchStart(t);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17111
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17112
        });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17113
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17114
        attachEventHandler(curElement, "touchmove", function(t) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17115
          if (p.touchMove !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17116
            t.preventDefault(); // Stop the viewport from scrolling
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17117
            t = addTouchEventOffset(t);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17118
            p.touchMove(t);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17119
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17120
        });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17121
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17122
        attachEventHandler(curElement, "touchend", function(t) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17123
          if (p.touchEnd !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17124
            t = addTouchEventOffset(t);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17125
            p.touchEnd(t);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17126
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17127
        });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17128
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17129
        attachEventHandler(curElement, "touchcancel", function(t) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17130
          if (p.touchCancel !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17131
            t = addTouchEventOffset(t);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17132
            p.touchCancel(t);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17133
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17134
        });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17135
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17136
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17137
        // Emulated touch start/mouse down event
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17138
        attachEventHandler(curElement, "touchstart", function(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17139
          updateMousePosition(curElement, e.touches[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17140
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17141
          p.__mousePressed = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17142
          p.mouseDragging = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17143
          p.mouseButton = PConstants.LEFT;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17144
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17145
          if (typeof p.mousePressed === "function") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17146
            p.mousePressed();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17147
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17148
        });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17149
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17150
        // Emulated touch move/mouse move event
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17151
        attachEventHandler(curElement, "touchmove", function(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17152
          e.preventDefault();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17153
          updateMousePosition(curElement, e.touches[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17154
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17155
          if (typeof p.mouseMoved === "function" && !p.__mousePressed) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17156
            p.mouseMoved();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17157
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17158
          if (typeof p.mouseDragged === "function" && p.__mousePressed) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17159
            p.mouseDragged();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17160
            p.mouseDragging = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17161
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17162
        });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17163
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17164
        // Emulated touch up/mouse up event
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17165
        attachEventHandler(curElement, "touchend", function(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17166
          p.__mousePressed = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17167
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17168
          if (typeof p.mouseClicked === "function" && !p.mouseDragging) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17169
            p.mouseClicked();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17170
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17171
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17172
          if (typeof p.mouseReleased === "function") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17173
            p.mouseReleased();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17174
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17175
        });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17176
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17177
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17178
      // Refire the touch start event we consumed in this function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17179
      curElement.dispatchEvent(t);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17180
    });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17181
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17182
    (function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17183
      var enabled = true,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17184
          contextMenu = function(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17185
            e.preventDefault();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17186
            e.stopPropagation();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17187
          };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17188
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17189
      p.disableContextMenu = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17190
        if (!enabled) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17191
          return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17192
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17193
        attachEventHandler(curElement, 'contextmenu', contextMenu);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17194
        enabled = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17195
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17196
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17197
      p.enableContextMenu = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17198
        if (enabled) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17199
          return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17200
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17201
        detachEventHandler({elem: curElement, type: 'contextmenu', fn: contextMenu});
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17202
        enabled = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17203
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17204
    }());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17205
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17206
    attachEventHandler(curElement, "mousemove", function(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17207
      updateMousePosition(curElement, e);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17208
      if (typeof p.mouseMoved === "function" && !p.__mousePressed) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17209
        p.mouseMoved();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17210
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17211
      if (typeof p.mouseDragged === "function" && p.__mousePressed) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17212
        p.mouseDragged();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17213
        p.mouseDragging = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17214
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17215
    });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17216
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17217
    attachEventHandler(curElement, "mouseout", function(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17218
      if (typeof p.mouseOut === "function") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17219
        p.mouseOut();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17220
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17221
    });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17222
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17223
    attachEventHandler(curElement, "mouseover", function(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17224
      updateMousePosition(curElement, e);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17225
      if (typeof p.mouseOver === "function") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17226
        p.mouseOver();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17227
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17228
    });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17229
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17230
    attachEventHandler(curElement, "mousedown", function(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17231
      p.__mousePressed = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17232
      p.mouseDragging = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17233
      switch (e.which) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17234
      case 1:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17235
        p.mouseButton = PConstants.LEFT;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17236
        break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17237
      case 2:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17238
        p.mouseButton = PConstants.CENTER;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17239
        break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17240
      case 3:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17241
        p.mouseButton = PConstants.RIGHT;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17242
        break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17243
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17244
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17245
      if (typeof p.mousePressed === "function") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17246
        p.mousePressed();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17247
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17248
    });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17249
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17250
    attachEventHandler(curElement, "mouseup", function(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17251
      p.__mousePressed = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17252
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17253
      if (typeof p.mouseClicked === "function" && !p.mouseDragging) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17254
        p.mouseClicked();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17255
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17256
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17257
      if (typeof p.mouseReleased === "function") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17258
        p.mouseReleased();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17259
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17260
    });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17261
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17262
    var mouseWheelHandler = function(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17263
      var delta = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17264
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17265
      if (e.wheelDelta) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17266
        delta = e.wheelDelta / 120;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17267
        if (window.opera) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17268
          delta = -delta;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17269
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17270
      } else if (e.detail) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17271
        delta = -e.detail / 3;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17272
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17273
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17274
      p.mouseScroll = delta;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17275
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17276
      if (delta && typeof p.mouseScrolled === 'function') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17277
        p.mouseScrolled();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17278
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17279
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17280
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17281
    // Support Gecko and non-Gecko scroll events
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17282
    attachEventHandler(document, 'DOMMouseScroll', mouseWheelHandler);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17283
    attachEventHandler(document, 'mousewheel', mouseWheelHandler);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17284
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17285
    //////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17286
    // Keyboard Events
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17287
    //////////////////////////////////////////////////////////////////////////
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17288
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17289
    // Get the DOM element if string was passed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17290
    if (typeof curElement === "string") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17291
      curElement = document.getElementById(curElement);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17292
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17293
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17294
    // In order to catch key events in a canvas, it needs to be "specially focusable",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17295
    // by assigning it a tabindex. If no tabindex is specified on-page, set this to 0.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17296
    if (!curElement.getAttribute("tabindex")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17297
      curElement.setAttribute("tabindex", 0);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17298
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17299
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17300
    function getKeyCode(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17301
      var code = e.which || e.keyCode;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17302
      switch (code) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17303
        case 13: // ENTER
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17304
          return 10;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17305
        case 91: // META L (Saf/Mac)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17306
        case 93: // META R (Saf/Mac)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17307
        case 224: // META (FF/Mac)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17308
          return 157;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17309
        case 57392: // CONTROL (Op/Mac)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17310
          return 17;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17311
        case 46: // DELETE
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17312
          return 127;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17313
        case 45: // INSERT
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17314
          return 155;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17315
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17316
      return code;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17317
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17318
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17319
    function getKeyChar(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17320
      var c = e.which || e.keyCode;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17321
      var anyShiftPressed = e.shiftKey || e.ctrlKey || e.altKey || e.metaKey;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17322
      switch (c) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17323
        case 13:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17324
          c = anyShiftPressed ? 13 : 10; // RETURN vs ENTER (Mac)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17325
          break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17326
        case 8:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17327
          c = anyShiftPressed ? 127 : 8; // DELETE vs BACKSPACE (Mac)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17328
          break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17329
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17330
      return new Char(c);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17331
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17332
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17333
    function suppressKeyEvent(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17334
      if (typeof e.preventDefault === "function") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17335
        e.preventDefault();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17336
      } else if (typeof e.stopPropagation === "function") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17337
        e.stopPropagation();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17338
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17339
      return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17340
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17341
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17342
    function updateKeyPressed() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17343
      var ch;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17344
      for (ch in pressedKeysMap) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17345
        if (pressedKeysMap.hasOwnProperty(ch)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17346
          p.__keyPressed = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17347
          return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17348
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17349
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17350
      p.__keyPressed = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17351
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17352
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17353
    function resetKeyPressed() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17354
      p.__keyPressed = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17355
      pressedKeysMap = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17356
      lastPressedKeyCode = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17357
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17358
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17359
    function simulateKeyTyped(code, c) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17360
      pressedKeysMap[code] = c;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17361
      lastPressedKeyCode = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17362
      p.key = c;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17363
      p.keyCode = code;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17364
      p.keyPressed();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17365
      p.keyCode = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17366
      p.keyTyped();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17367
      updateKeyPressed();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17368
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17369
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17370
    function handleKeydown(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17371
      var code = getKeyCode(e);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17372
      if (code === PConstants.DELETE) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17373
        simulateKeyTyped(code, new Char(127));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17374
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17375
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17376
      if (codedKeys.indexOf(code) < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17377
        lastPressedKeyCode = code;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17378
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17379
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17380
      var c = new Char(PConstants.CODED);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17381
      p.key = c;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17382
      p.keyCode = code;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17383
      pressedKeysMap[code] = c;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17384
      p.keyPressed();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17385
      lastPressedKeyCode = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17386
      updateKeyPressed();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17387
      return suppressKeyEvent(e);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17388
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17389
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17390
    function handleKeypress(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17391
      if (lastPressedKeyCode === null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17392
        return; // processed in handleKeydown
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17393
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17394
      var code = lastPressedKeyCode, c = getKeyChar(e);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17395
      simulateKeyTyped(code, c);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17396
      return suppressKeyEvent(e);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17397
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17398
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17399
    function handleKeyup(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17400
      var code = getKeyCode(e), c = pressedKeysMap[code];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17401
      if (c === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17402
        return; // no keyPressed event was generated.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17403
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17404
      p.key = c;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17405
      p.keyCode = code;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17406
      p.keyReleased();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17407
      delete pressedKeysMap[code];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17408
      updateKeyPressed();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17409
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17410
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17411
    // Send aCode Processing syntax to be converted to JavaScript
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17412
    if (!pgraphicsMode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17413
      if (aCode instanceof Processing.Sketch) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17414
        // Use sketch as is
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17415
        curSketch = aCode;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17416
      } else if (typeof aCode === "function") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17417
        // Wrap function with default sketch parameters
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17418
        curSketch = new Processing.Sketch(aCode);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17419
      } else if (!aCode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17420
        // Empty sketch
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17421
        curSketch = new Processing.Sketch(function (){});
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17422
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17423
//#if PARSER
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17424
        // Compile the code
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17425
        curSketch = Processing.compile(aCode);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17426
//#else
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17427
//      throw "PJS compile is not supported";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17428
//#endif
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17429
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17430
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17431
      // Expose internal field for diagnostics and testing
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17432
      p.externals.sketch = curSketch;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17433
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17434
      wireDimensionalFunctions();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17435
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17436
      // the onfocus and onblur events are handled in two parts.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17437
      // 1) the p.focused property is handled per sketch
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17438
      curElement.onfocus = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17439
        p.focused = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17440
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17441
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17442
      curElement.onblur = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17443
        p.focused = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17444
        if (!curSketch.options.globalKeyEvents) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17445
          resetKeyPressed();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17446
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17447
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17448
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17449
      // 2) looping status is handled per page, based on the pauseOnBlur @pjs directive
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17450
      if (curSketch.options.pauseOnBlur) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17451
        attachEventHandler(window, 'focus', function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17452
          if (doLoop) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17453
            p.loop();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17454
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17455
        });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17456
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17457
        attachEventHandler(window, 'blur', function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17458
          if (doLoop && loopStarted) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17459
            p.noLoop();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17460
            doLoop = true; // make sure to keep this true after the noLoop call
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17461
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17462
          resetKeyPressed();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17463
        });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17464
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17465
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17466
      // if keyboard events should be handled globally, the listeners should
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17467
      // be bound to the document window, rather than to the current canvas
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17468
      var keyTrigger = curSketch.options.globalKeyEvents ? window : curElement;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17469
      attachEventHandler(keyTrigger, "keydown", handleKeydown);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17470
      attachEventHandler(keyTrigger, "keypress", handleKeypress);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17471
      attachEventHandler(keyTrigger, "keyup", handleKeyup);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17472
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17473
      // Step through the libraries that were attached at doc load...
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17474
      for (var i in Processing.lib) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17475
        if (Processing.lib.hasOwnProperty(i)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17476
          if(Processing.lib[i].hasOwnProperty("attach")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17477
            // use attach function if present
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17478
            Processing.lib[i].attach(p);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17479
          } else if(Processing.lib[i] instanceof Function)  {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17480
            // Init the libraries in the context of this p_instance (legacy)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17481
            Processing.lib[i].call(this);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17482
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17483
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17484
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17485
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17486
      // sketch execute test interval, used to reschedule
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17487
      // an execute when preloads have not yet finished.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17488
      var retryInterval = 100;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17489
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17490
      var executeSketch = function(processing) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17491
        // Don't start until all specified images and fonts in the cache are preloaded
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17492
        if (!(curSketch.imageCache.pending || PFont.preloading.pending(retryInterval))) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17493
          // the opera preload cache can only be cleared once we start
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17494
          if (window.opera) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17495
            var link,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17496
                element,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17497
                operaCache=curSketch.imageCache.operaCache;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17498
            for (link in operaCache) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17499
              if(operaCache.hasOwnProperty(link)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17500
                element = operaCache[link];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17501
                if (element !== null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17502
                  document.body.removeChild(element);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17503
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17504
                delete(operaCache[link]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17505
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17506
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17507
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17508
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17509
          curSketch.attach(processing, defaultScope);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17510
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17511
          // pass a reference to the p instance for this sketch.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17512
          curSketch.onLoad(processing);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17513
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17514
          // Run void setup()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17515
          if (processing.setup) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17516
            processing.setup();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17517
            // if any transforms were performed in setup reset to identity matrix
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17518
            // so draw loop is unpolluted
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17519
            processing.resetMatrix();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17520
            curSketch.onSetup();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17521
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17522
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17523
          // some pixels can be cached, flushing
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17524
          resetContext();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17525
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17526
          if (processing.draw) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17527
            if (!doLoop) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17528
              processing.redraw();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17529
            } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17530
              processing.loop();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17531
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17532
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17533
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17534
          window.setTimeout(function() { executeSketch(processing); }, retryInterval);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17535
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17536
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17537
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17538
      // Only store an instance of non-createGraphics instances.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17539
      addInstance(this);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17540
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17541
      // The parser adds custom methods to the processing context
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17542
      // this renames p to processing so these methods will run
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17543
      executeSketch(p);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17544
    } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17545
      // No executable sketch was specified
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17546
      // or called via createGraphics
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17547
      curSketch = new Processing.Sketch();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17548
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17549
      wireDimensionalFunctions();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17550
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17551
      // Hack to make PGraphics work again after splitting size()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17552
      p.size = function(w, h, render) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17553
        if (render && render === PConstants.WEBGL) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17554
          wireDimensionalFunctions('3D');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17555
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17556
          wireDimensionalFunctions('2D');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17557
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17558
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17559
        p.size(w, h, render);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17560
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17561
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17562
  }; // Processing() ends
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17563
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17564
  // Place-holder for overridable debugging function
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17565
  Processing.debug = debug;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17566
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17567
  Processing.prototype = defaultScope;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17568
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17569
//#if PARSER
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17570
  // Processing global methods and constants for the parser
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17571
  function getGlobalMembers() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17572
    // The names array contains the names of everything that is inside "p."
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17573
    // When something new is added to "p." it must also be added to this list.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17574
    var names = [ /* this code is generated by jsglobals.js */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17575
      "abs", "acos", "alpha", "ambient", "ambientLight", "append", "applyMatrix",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17576
      "arc", "arrayCopy", "asin", "atan", "atan2", "background", "beginCamera",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17577
      "beginDraw", "beginShape", "bezier", "bezierDetail", "bezierPoint",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17578
      "bezierTangent", "bezierVertex", "binary", "blend", "blendColor",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17579
      "blit_resize", "blue", "box", "breakShape", "brightness",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17580
      "camera", "ceil", "Character", "color", "colorMode",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17581
      "concat", "constrain", "copy", "cos", "createFont",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17582
      "createGraphics", "createImage", "cursor", "curve", "curveDetail",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17583
      "curvePoint", "curveTangent", "curveTightness", "curveVertex", "day",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17584
      "degrees", "directionalLight", "disableContextMenu",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17585
      "dist", "draw", "ellipse", "ellipseMode", "emissive", "enableContextMenu",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17586
      "endCamera", "endDraw", "endShape", "exit", "exp", "expand", "externals",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17587
      "fill", "filter", "floor", "focused", "frameCount", "frameRate", "frustum",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17588
      "get", "glyphLook", "glyphTable", "green", "height", "hex", "hint", "hour",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17589
      "hue", "image", "imageMode", "intersect", "join", "key",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17590
      "keyCode", "keyPressed", "keyReleased", "keyTyped", "lerp", "lerpColor",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17591
      "lightFalloff", "lights", "lightSpecular", "line", "link", "loadBytes",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17592
      "loadFont", "loadGlyphs", "loadImage", "loadPixels", "loadShape",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17593
      "loadStrings", "log", "loop", "mag", "map", "match", "matchAll", "max",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17594
      "millis", "min", "minute", "mix", "modelX", "modelY", "modelZ", "modes",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17595
      "month", "mouseButton", "mouseClicked", "mouseDragged", "mouseMoved",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17596
      "mouseOut", "mouseOver", "mousePressed", "mouseReleased", "mouseScroll",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17597
      "mouseScrolled", "mouseX", "mouseY", "name", "nf", "nfc", "nfp", "nfs",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17598
      "noCursor", "noFill", "noise", "noiseDetail", "noiseSeed", "noLights",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17599
      "noLoop", "norm", "normal", "noSmooth", "noStroke", "noTint", "ortho",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17600
      "param", "parseBoolean", "parseByte", "parseChar", "parseFloat",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17601
      "parseInt", "peg", "perspective", "PImage", "pixels", "PMatrix2D",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17602
      "PMatrix3D", "PMatrixStack", "pmouseX", "pmouseY", "point",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17603
      "pointLight", "popMatrix", "popStyle", "pow", "print", "printCamera",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17604
      "println", "printMatrix", "printProjection", "PShape", "PShapeSVG",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17605
      "pushMatrix", "pushStyle", "quad", "radians", "random", "Random",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17606
      "randomSeed", "rect", "rectMode", "red", "redraw", "requestImage",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17607
      "resetMatrix", "reverse", "rotate", "rotateX", "rotateY", "rotateZ",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17608
      "round", "saturation", "save", "saveFrame", "saveStrings", "scale",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17609
      "screenX", "screenY", "screenZ", "second", "set", "setup", "shape",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17610
      "shapeMode", "shared", "shininess", "shorten", "sin", "size", "smooth",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17611
      "sort", "specular", "sphere", "sphereDetail", "splice", "split",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17612
      "splitTokens", "spotLight", "sq", "sqrt", "status", "str", "stroke",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17613
      "strokeCap", "strokeJoin", "strokeWeight", "subset", "tan", "text",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17614
      "textAlign", "textAscent", "textDescent", "textFont", "textLeading",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17615
      "textMode", "textSize", "texture", "textureMode", "textWidth", "tint", "toImageData",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17616
      "touchCancel", "touchEnd", "touchMove", "touchStart", "translate",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17617
      "triangle", "trim", "unbinary", "unhex", "updatePixels", "use3DContext",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17618
      "vertex", "width", "XMLElement", "year", "__contains", "__equals",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17619
      "__equalsIgnoreCase", "__frameRate", "__hashCode", "__int_cast",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17620
      "__instanceof", "__keyPressed", "__mousePressed", "__printStackTrace",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17621
      "__replace", "__replaceAll", "__replaceFirst", "__toCharArray", "__split",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17622
      "__codePointAt", "__startsWith", "__endsWith"];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17623
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17624
    var members = {};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17625
    var i, l;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17626
    for (i = 0, l = names.length; i < l ; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17627
      members[names[i]] = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17628
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17629
    for (var lib in Processing.lib) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17630
      if (Processing.lib.hasOwnProperty(lib)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17631
        if (Processing.lib[lib].exports) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17632
          var exportedNames = Processing.lib[lib].exports;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17633
          for (i = 0, l = exportedNames.length; i < l; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17634
           members[exportedNames[i]] = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17635
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17636
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17637
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17638
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17639
    return members;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17640
  }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17641
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17642
/*
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17643
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17644
    Parser converts Java-like syntax into JavaScript.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17645
    Creates an Abstract Syntax Tree -- "Light AST" from the Java-like code.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17646
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17647
    It is an object tree. The root object is created from the AstRoot class, which contains statements.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17648
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17649
    A statement object can be of type: AstForStatement, AstCatchStatement, AstPrefixStatement, AstMethod, AstClass,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17650
    AstInterface, AstFunction, AstStatementBlock and AstLabel.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17651
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17652
    AstPrefixStatement can be a statement of type: if, switch, while, with, do, else, finally, return, throw, try, break, and continue.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17653
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17654
    These object's toString function returns the JavaScript code for the statement.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17655
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17656
    Any processing calls need "processing." prepended to them.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17657
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17658
    Similarly, calls from inside classes need "$this_1.", prepended to them,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17659
    with 1 being the depth level for inner classes.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17660
    This includes members passed down from inheritance.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17661
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17662
    The resulting code is then eval'd and run.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17663
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17664
*/
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17665
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17666
  function parseProcessing(code) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17667
    var globalMembers = getGlobalMembers();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17668
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17669
    // masks parentheses, brackets and braces with '"A5"'
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17670
    // where A is the bracket type, and 5 is the index in an array containing all brackets split into atoms
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17671
    // 'while(true){}' -> 'while"B1""A2"'
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17672
    // parentheses() = B, brackets[] = C and braces{} = A
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17673
    function splitToAtoms(code) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17674
      var atoms = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17675
      var items = code.split(/([\{\[\(\)\]\}])/);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17676
      var result = items[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17677
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17678
      var stack = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17679
      for(var i=1; i < items.length; i += 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17680
        var item = items[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17681
        if(item === '[' || item === '{' || item === '(') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17682
          stack.push(result); result = item;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17683
        } else if(item === ']' || item === '}' || item === ')') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17684
          var kind = item === '}' ? 'A' : item === ')' ? 'B' : 'C';
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17685
          var index = atoms.length; atoms.push(result + item);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17686
          result = stack.pop() + '"' + kind + (index + 1) + '"';
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17687
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17688
        result += items[i + 1];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17689
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17690
      atoms.unshift(result);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17691
      return atoms;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17692
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17693
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17694
    // replaces strings and regexs keyed by index with an array of strings
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17695
    function injectStrings(code, strings) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17696
      return code.replace(/'(\d+)'/g, function(all, index) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17697
        var val = strings[index];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17698
        if(val.charAt(0) === "/") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17699
          return val;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17700
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17701
        return (/^'((?:[^'\\\n])|(?:\\.[0-9A-Fa-f]*))'$/).test(val) ? "(new $p.Character(" + val + "))" : val;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17702
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17703
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17704
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17705
    // trims off leading and trailing spaces
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17706
    // returns an object. object.left, object.middle, object.right, object.untrim
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17707
    function trimSpaces(string) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17708
      var m1 = /^\s*/.exec(string), result;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17709
      if(m1[0].length === string.length) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17710
        result = {left: m1[0], middle: "", right: ""};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17711
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17712
        var m2 = /\s*$/.exec(string);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17713
        result = {left: m1[0], middle: string.substring(m1[0].length, m2.index), right: m2[0]};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17714
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17715
      result.untrim = function(t) { return this.left + t + this.right; };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17716
      return result;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17717
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17718
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17719
    // simple trim of leading and trailing spaces
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17720
    function trim(string) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17721
      return string.replace(/^\s+/,'').replace(/\s+$/,'');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17722
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17723
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17724
    function appendToLookupTable(table, array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17725
      for(var i=0,l=array.length;i<l;++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17726
        table[array[i]] = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17727
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17728
      return table;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17729
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17730
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17731
    function isLookupTableEmpty(table) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17732
      for(var i in table) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17733
        if(table.hasOwnProperty(i)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17734
          return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17735
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17736
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17737
      return true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17738
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17739
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17740
    function getAtomIndex(templ) { return templ.substring(2, templ.length - 1); }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17741
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17742
    // remove carriage returns "\r"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17743
    var codeWoExtraCr = code.replace(/\r\n?|\n\r/g, "\n");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17744
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17745
    // masks strings and regexs with "'5'", where 5 is the index in an array containing all strings and regexs
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17746
    // also removes all comments
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17747
    var strings = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17748
    var codeWoStrings = codeWoExtraCr.replace(/("(?:[^"\\\n]|\\.)*")|('(?:[^'\\\n]|\\.)*')|(([\[\(=|&!\^:?]\s*)(\/(?![*\/])(?:[^\/\\\n]|\\.)*\/[gim]*)\b)|(\/\/[^\n]*\n)|(\/\*(?:(?!\*\/)(?:.|\n))*\*\/)/g,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17749
    function(all, quoted, aposed, regexCtx, prefix, regex, singleComment, comment) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17750
      var index;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17751
      if(quoted || aposed) { // replace strings
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17752
        index = strings.length; strings.push(all);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17753
        return "'" + index + "'";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17754
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17755
      if(regexCtx) { // replace RegExps
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17756
        index = strings.length; strings.push(regex);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17757
        return prefix + "'" + index + "'";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17758
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17759
      // kill comments
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17760
      return comment !== "" ? " " : "\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17761
    });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17762
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17763
    // removes generics
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17764
    var genericsWereRemoved;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17765
    var codeWoGenerics = codeWoStrings;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17766
    var replaceFunc = function(all, before, types, after) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17767
      if(!!before || !!after) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17768
        return all;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17769
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17770
      genericsWereRemoved = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17771
      return "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17772
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17773
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17774
    do {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17775
      genericsWereRemoved = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17776
      codeWoGenerics = codeWoGenerics.replace(/([<]?)<\s*((?:\?|[A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*)(?:\s+(?:extends|super)\s+[A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*)?(?:\s*,\s*(?:\?|[A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*)(?:\s+(?:extends|super)\s+[A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*)?)*)\s*>([=]?)/g, replaceFunc);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17777
    } while (genericsWereRemoved);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17778
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17779
    var atoms = splitToAtoms(codeWoGenerics);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17780
    var replaceContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17781
    var declaredClasses = {}, currentClassId, classIdSeed = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17782
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17783
    function addAtom(text, type) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17784
      var lastIndex = atoms.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17785
      atoms.push(text);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17786
      return '"' + type + lastIndex + '"';
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17787
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17788
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17789
    function generateClassId() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17790
      return "class" + (++classIdSeed);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17791
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17792
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17793
    function appendClass(class_, classId, scopeId) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17794
      class_.classId = classId;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17795
      class_.scopeId = scopeId;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17796
      declaredClasses[classId] = class_;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17797
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17798
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17799
    // functions defined below
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17800
    var transformClassBody, transformInterfaceBody, transformStatementsBlock, transformStatements, transformMain, transformExpression;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17801
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17802
    var classesRegex = /\b((?:(?:public|private|final|protected|static|abstract)\s+)*)(class|interface)\s+([A-Za-z_$][\w$]*\b)(\s+extends\s+[A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*(?:\s*,\s*[A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*\b)*)?(\s+implements\s+[A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*(?:\s*,\s*[A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*\b)*)?\s*("A\d+")/g;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17803
    var methodsRegex = /\b((?:(?:public|private|final|protected|static|abstract|synchronized)\s+)*)((?!(?:else|new|return|throw|function|public|private|protected)\b)[A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*(?:\s*"C\d+")*)\s*([A-Za-z_$][\w$]*\b)\s*("B\d+")(\s*throws\s+[A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*(?:\s*,\s*[A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*)*)?\s*("A\d+"|;)/g;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17804
    var fieldTest = /^((?:(?:public|private|final|protected|static)\s+)*)((?!(?:else|new|return|throw)\b)[A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*(?:\s*"C\d+")*)\s*([A-Za-z_$][\w$]*\b)\s*(?:"C\d+"\s*)*([=,]|$)/;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17805
    var cstrsRegex = /\b((?:(?:public|private|final|protected|static|abstract)\s+)*)((?!(?:new|return|throw)\b)[A-Za-z_$][\w$]*\b)\s*("B\d+")(\s*throws\s+[A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*(?:\s*,\s*[A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*)*)?\s*("A\d+")/g;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17806
    var attrAndTypeRegex = /^((?:(?:public|private|final|protected|static)\s+)*)((?!(?:new|return|throw)\b)[A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*(?:\s*"C\d+")*)\s*/;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17807
    var functionsRegex = /\bfunction(?:\s+([A-Za-z_$][\w$]*))?\s*("B\d+")\s*("A\d+")/g;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17808
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17809
    // This converts classes, methods and functions into atoms, and adds them to the atoms array.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17810
    // classes = E, methods = D and functions = H
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17811
    function extractClassesAndMethods(code) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17812
      var s = code;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17813
      s = s.replace(classesRegex, function(all) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17814
        return addAtom(all, 'E');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17815
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17816
      s = s.replace(methodsRegex, function(all) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17817
        return addAtom(all, 'D');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17818
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17819
      s = s.replace(functionsRegex, function(all) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17820
        return addAtom(all, 'H');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17821
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17822
      return s;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17823
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17824
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17825
    // This converts constructors into atoms, and adds them to the atoms array.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17826
    // constructors = G
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17827
    function extractConstructors(code, className) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17828
      var result = code.replace(cstrsRegex, function(all, attr, name, params, throws_, body) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17829
        if(name !== className) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17830
          return all;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17831
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17832
        return addAtom(all, 'G');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17833
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17834
      return result;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17835
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17836
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17837
    // AstParam contains the name of a parameter inside a function declaration
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17838
    function AstParam(name) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17839
      this.name = name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17840
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17841
    AstParam.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17842
      return this.name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17843
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17844
    // AstParams contains an array of AstParam objects
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17845
    function AstParams(params) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17846
      this.params = params;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17847
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17848
    AstParams.prototype.getNames = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17849
      var names = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17850
      for(var i=0,l=this.params.length;i<l;++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17851
        names.push(this.params[i].name);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17852
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17853
      return names;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17854
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17855
    AstParams.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17856
      if(this.params.length === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17857
        return "()";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17858
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17859
      var result = "(";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17860
      for(var i=0,l=this.params.length;i<l;++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17861
        result += this.params[i] + ", ";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17862
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17863
      return result.substring(0, result.length - 2) + ")";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17864
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17865
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17866
    function transformParams(params) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17867
      var paramsWoPars = trim(params.substring(1, params.length - 1));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17868
      var result = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17869
      if(paramsWoPars !== "") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17870
        var paramList = paramsWoPars.split(",");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17871
        for(var i=0; i < paramList.length; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17872
          var param = /\b([A-Za-z_$][\w$]*\b)(\s*"[ABC][\d]*")*\s*$/.exec(paramList[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17873
          result.push(new AstParam(param[1]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17874
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17875
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17876
      return new AstParams(result);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17877
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17878
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17879
    function preExpressionTransform(expr) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17880
      var s = expr;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17881
      // new type[] {...} --> {...}
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17882
      s = s.replace(/\bnew\s+([A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*)(?:\s*"C\d+")+\s*("A\d+")/g, function(all, type, init) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17883
        return init;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17884
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17885
      // new Runnable() {...} --> "F???"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17886
      s = s.replace(/\bnew\s+([A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*)(?:\s*"B\d+")\s*("A\d+")/g, function(all, type, init) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17887
        return addAtom(all, 'F');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17888
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17889
      // function(...) { } --> "H???"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17890
      s = s.replace(functionsRegex, function(all) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17891
        return addAtom(all, 'H');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17892
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17893
      // new type[?] --> createJavaArray('type', [?])
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17894
      s = s.replace(/\bnew\s+([A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*)\s*("C\d+"(?:\s*"C\d+")*)/g, function(all, type, index) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17895
        var args = index.replace(/"C(\d+)"/g, function(all, j) { return atoms[j]; })
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17896
          .replace(/\[\s*\]/g, "[null]").replace(/\s*\]\s*\[\s*/g, ", ");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17897
        var arrayInitializer = "{" + args.substring(1, args.length - 1) + "}";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17898
        var createArrayArgs = "('" + type + "', " + addAtom(arrayInitializer, 'A') + ")";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17899
        return '$p.createJavaArray' + addAtom(createArrayArgs, 'B');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17900
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17901
      // .length() --> .length
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17902
      s = s.replace(/(\.\s*length)\s*"B\d+"/g, "$1");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17903
      // #000000 --> 0x000000
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17904
      s = s.replace(/#([0-9A-Fa-f]{6})\b/g, function(all, digits) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17905
        return "0xFF" + digits;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17906
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17907
      // delete (type)???, except (int)???
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17908
      s = s.replace(/"B(\d+)"(\s*(?:[\w$']|"B))/g, function(all, index, next) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17909
        var atom = atoms[index];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17910
        if(!/^\(\s*[A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*\s*(?:"C\d+"\s*)*\)$/.test(atom)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17911
          return all;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17912
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17913
        if(/^\(\s*int\s*\)$/.test(atom)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17914
          return "(int)" + next;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17915
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17916
        var indexParts = atom.split(/"C(\d+)"/g);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17917
        if(indexParts.length > 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17918
          // even items contains atom numbers, can check only first
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17919
          if(! /^\[\s*\]$/.test(atoms[indexParts[1]])) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17920
            return all; // fallback - not a cast
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17921
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17922
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17923
        return "" + next;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17924
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17925
      // (int)??? -> __int_cast(???)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17926
      s = s.replace(/\(int\)([^,\]\)\}\?\:\*\+\-\/\^\|\%\&\~<\>\=]+)/g, function(all, arg) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17927
        var trimmed = trimSpaces(arg);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17928
        return trimmed.untrim("__int_cast(" + trimmed.middle + ")");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17929
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17930
      // super() -> $superCstr(), super. -> $super.;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17931
      s = s.replace(/\bsuper(\s*"B\d+")/g, "$$superCstr$1").replace(/\bsuper(\s*\.)/g, "$$super$1");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17932
      // 000.43->0.43 and 0010f->10, but not 0010
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17933
      s = s.replace(/\b0+((\d*)(?:\.[\d*])?(?:[eE][\-\+]?\d+)?[fF]?)\b/, function(all, numberWo0, intPart) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17934
        if( numberWo0 === intPart) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17935
          return all;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17936
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17937
        return intPart === "" ? "0" + numberWo0 : numberWo0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17938
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17939
      // 3.0f -> 3.0
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17940
      s = s.replace(/\b(\.?\d+\.?)[fF]\b/g, "$1");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17941
      // Weird (?) parsing errors with %
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17942
      s = s.replace(/([^\s])%([^=\s])/g, "$1 % $2");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17943
      // Since frameRate() and frameRate are different things,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17944
      // we need to differentiate them somehow. So when we parse
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17945
      // the Processing.js source, replace frameRate so it isn't
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17946
      // confused with frameRate(), as well as keyPressed and mousePressed
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17947
      s = s.replace(/\b(frameRate|keyPressed|mousePressed)\b(?!\s*"B)/g, "__$1");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17948
      // "boolean", "byte", "int", etc. => "parseBoolean", "parseByte", "parseInt", etc.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17949
      s = s.replace(/\b(boolean|byte|char|float|int)\s*"B/g, function(all, name) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17950
        return "parse" + name.substring(0, 1).toUpperCase() + name.substring(1) + "\"B";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17951
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17952
      // "pixels" replacements:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17953
      //   pixels[i] = c => pixels.setPixel(i,c) | pixels[i] => pixels.getPixel(i)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17954
      //   pixels.length => pixels.getLength()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17955
      //   pixels = ar => pixels.set(ar) | pixels => pixels.toArray()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17956
      s = s.replace(/\bpixels\b\s*(("C(\d+)")|\.length)?(\s*=(?!=)([^,\]\)\}]+))?/g,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17957
        function(all, indexOrLength, index, atomIndex, equalsPart, rightSide) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17958
          if(index) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17959
            var atom = atoms[atomIndex];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17960
            if(equalsPart) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17961
              return "pixels.setPixel" + addAtom("(" +atom.substring(1, atom.length - 1) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17962
                "," + rightSide + ")", 'B');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17963
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17964
            return "pixels.getPixel" + addAtom("(" + atom.substring(1, atom.length - 1) +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17965
              ")", 'B');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17966
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17967
          if(indexOrLength) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17968
            // length
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17969
            return "pixels.getLength" + addAtom("()", 'B');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17970
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17971
          if(equalsPart) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17972
            return "pixels.set" + addAtom("(" + rightSide + ")", 'B');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17973
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17974
          return "pixels.toArray" + addAtom("()", 'B');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17975
        });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17976
      // Java method replacements for: replace, replaceAll, replaceFirst, equals, hashCode, etc.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17977
      //   xxx.replace(yyy) -> __replace(xxx, yyy)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17978
      //   "xx".replace(yyy) -> __replace("xx", yyy)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17979
      var repeatJavaReplacement;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17980
      function replacePrototypeMethods(all, subject, method, atomIndex) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17981
        var atom = atoms[atomIndex];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17982
        repeatJavaReplacement = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17983
        var trimmed = trimSpaces(atom.substring(1, atom.length - 1));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17984
        return "__" + method  + ( trimmed.middle === "" ? addAtom("(" + subject.replace(/\.\s*$/, "") + ")", 'B') :
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17985
          addAtom("(" + subject.replace(/\.\s*$/, "") + "," + trimmed.middle + ")", 'B') );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17986
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17987
      do {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17988
        repeatJavaReplacement = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17989
        s = s.replace(/((?:'\d+'|\b[A-Za-z_$][\w$]*\s*(?:"[BC]\d+")*)\s*\.\s*(?:[A-Za-z_$][\w$]*\s*(?:"[BC]\d+"\s*)*\.\s*)*)(replace|replaceAll|replaceFirst|contains|equals|equalsIgnoreCase|hashCode|toCharArray|printStackTrace|split|startsWith|endsWith|codePointAt)\s*"B(\d+)"/g,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17990
          replacePrototypeMethods);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17991
      } while (repeatJavaReplacement);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17992
      // xxx instanceof yyy -> __instanceof(xxx, yyy)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17993
      function replaceInstanceof(all, subject, type) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17994
        repeatJavaReplacement = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17995
        return "__instanceof" + addAtom("(" + subject + ", " + type + ")", 'B');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17996
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17997
      do {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17998
        repeatJavaReplacement = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 17999
        s = s.replace(/((?:'\d+'|\b[A-Za-z_$][\w$]*\s*(?:"[BC]\d+")*)\s*(?:\.\s*[A-Za-z_$][\w$]*\s*(?:"[BC]\d+"\s*)*)*)instanceof\s+([A-Za-z_$][\w$]*\s*(?:\.\s*[A-Za-z_$][\w$]*)*)/g,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18000
          replaceInstanceof);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18001
      } while (repeatJavaReplacement);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18002
      // this() -> $constr()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18003
      s = s.replace(/\bthis(\s*"B\d+")/g, "$$constr$1");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18004
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18005
      return s;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18006
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18007
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18008
    function AstInlineClass(baseInterfaceName, body) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18009
      this.baseInterfaceName = baseInterfaceName;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18010
      this.body = body;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18011
      body.owner = this;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18012
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18013
    AstInlineClass.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18014
      return "new (" + this.body + ")";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18015
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18016
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18017
    function transformInlineClass(class_) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18018
      var m = new RegExp(/\bnew\s*([A-Za-z_$][\w$]*\s*(?:\.\s*[A-Za-z_$][\w$]*)*)\s*"B\d+"\s*"A(\d+)"/).exec(class_);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18019
      var oldClassId = currentClassId, newClassId = generateClassId();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18020
      currentClassId = newClassId;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18021
      var uniqueClassName = m[1] + "$" + newClassId;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18022
      var inlineClass = new AstInlineClass(uniqueClassName,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18023
        transformClassBody(atoms[m[2]], uniqueClassName, "", "implements " + m[1]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18024
      appendClass(inlineClass, newClassId, oldClassId);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18025
      currentClassId = oldClassId;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18026
      return inlineClass;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18027
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18028
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18029
    function AstFunction(name, params, body) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18030
      this.name = name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18031
      this.params = params;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18032
      this.body = body;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18033
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18034
    AstFunction.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18035
      var oldContext = replaceContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18036
      // saving "this." and parameters
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18037
      var names = appendToLookupTable({"this":null}, this.params.getNames());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18038
      replaceContext = function (subject) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18039
        return names.hasOwnProperty(subject.name) ? subject.name : oldContext(subject);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18040
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18041
      var result = "function";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18042
      if(this.name) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18043
        result += " " + this.name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18044
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18045
      result += this.params + " " + this.body;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18046
      replaceContext = oldContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18047
      return result;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18048
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18049
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18050
    function transformFunction(class_) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18051
      var m = new RegExp(/\b([A-Za-z_$][\w$]*)\s*"B(\d+)"\s*"A(\d+)"/).exec(class_);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18052
      return new AstFunction( m[1] !== "function" ? m[1] : null,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18053
        transformParams(atoms[m[2]]), transformStatementsBlock(atoms[m[3]]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18054
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18055
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18056
    function AstInlineObject(members) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18057
      this.members = members;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18058
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18059
    AstInlineObject.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18060
      var oldContext = replaceContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18061
      replaceContext = function (subject) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18062
          return subject.name === "this" ? "this" : oldContext(subject); // saving "this."
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18063
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18064
      var result = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18065
      for(var i=0,l=this.members.length;i<l;++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18066
        if(this.members[i].label) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18067
          result += this.members[i].label + ": ";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18068
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18069
        result += this.members[i].value.toString() + ", ";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18070
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18071
      replaceContext = oldContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18072
      return result.substring(0, result.length - 2);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18073
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18074
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18075
    function transformInlineObject(obj) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18076
      var members = obj.split(',');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18077
      for(var i=0; i < members.length; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18078
        var label = members[i].indexOf(':');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18079
        if(label < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18080
          members[i] = { value: transformExpression(members[i]) };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18081
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18082
          members[i] = { label: trim(members[i].substring(0, label)),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18083
            value: transformExpression( trim(members[i].substring(label + 1)) ) };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18084
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18085
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18086
      return new AstInlineObject(members);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18087
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18088
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18089
    function expandExpression(expr) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18090
      if(expr.charAt(0) === '(' || expr.charAt(0) === '[') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18091
        return expr.charAt(0) + expandExpression(expr.substring(1, expr.length - 1)) + expr.charAt(expr.length - 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18092
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18093
      if(expr.charAt(0) === '{') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18094
        if(/^\{\s*(?:[A-Za-z_$][\w$]*|'\d+')\s*:/.test(expr)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18095
          return "{" + addAtom(expr.substring(1, expr.length - 1), 'I') + "}";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18096
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18097
        return "[" + expandExpression(expr.substring(1, expr.length - 1)) + "]";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18098
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18099
      var trimmed = trimSpaces(expr);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18100
      var result = preExpressionTransform(trimmed.middle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18101
      result = result.replace(/"[ABC](\d+)"/g, function(all, index) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18102
        return expandExpression(atoms[index]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18103
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18104
      return trimmed.untrim(result);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18105
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18106
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18107
    function replaceContextInVars(expr) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18108
      return expr.replace(/(\.\s*)?((?:\b[A-Za-z_]|\$)[\w$]*)(\s*\.\s*([A-Za-z_$][\w$]*)(\s*\()?)?/g,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18109
        function(all, memberAccessSign, identifier, suffix, subMember, callSign) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18110
          if(memberAccessSign) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18111
            return all;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18112
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18113
          var subject = { name: identifier, member: subMember, callSign: !!callSign };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18114
          return replaceContext(subject) + (suffix === undef ? "" : suffix);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18115
        });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18116
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18117
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18118
    function AstExpression(expr, transforms) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18119
      this.expr = expr;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18120
      this.transforms = transforms;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18121
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18122
    AstExpression.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18123
      var transforms = this.transforms;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18124
      var expr = replaceContextInVars(this.expr);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18125
      return expr.replace(/"!(\d+)"/g, function(all, index) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18126
        return transforms[index].toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18127
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18128
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18129
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18130
    transformExpression = function(expr) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18131
      var transforms = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18132
      var s = expandExpression(expr);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18133
      s = s.replace(/"H(\d+)"/g, function(all, index) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18134
        transforms.push(transformFunction(atoms[index]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18135
        return '"!' + (transforms.length - 1) + '"';
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18136
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18137
      s = s.replace(/"F(\d+)"/g, function(all, index) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18138
        transforms.push(transformInlineClass(atoms[index]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18139
        return '"!' + (transforms.length - 1) + '"';
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18140
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18141
      s = s.replace(/"I(\d+)"/g, function(all, index) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18142
        transforms.push(transformInlineObject(atoms[index]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18143
        return '"!' + (transforms.length - 1) + '"';
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18144
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18145
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18146
      return new AstExpression(s, transforms);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18147
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18148
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18149
    function AstVarDefinition(name, value, isDefault) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18150
      this.name = name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18151
      this.value = value;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18152
      this.isDefault = isDefault;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18153
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18154
    AstVarDefinition.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18155
      return this.name + ' = ' + this.value;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18156
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18157
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18158
    function transformVarDefinition(def, defaultTypeValue) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18159
      var eqIndex = def.indexOf("=");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18160
      var name, value, isDefault;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18161
      if(eqIndex < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18162
        name = def;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18163
        value = defaultTypeValue;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18164
        isDefault = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18165
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18166
        name = def.substring(0, eqIndex);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18167
        value = transformExpression(def.substring(eqIndex + 1));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18168
        isDefault = false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18169
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18170
      return new AstVarDefinition( trim(name.replace(/(\s*"C\d+")+/g, "")),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18171
        value, isDefault);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18172
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18173
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18174
    function getDefaultValueForType(type) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18175
        if(type === "int" || type === "float") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18176
          return "0";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18177
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18178
        if(type === "boolean") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18179
          return "false";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18180
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18181
        if(type === "color") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18182
          return "0x00000000";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18183
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18184
        return "null";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18185
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18186
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18187
    function AstVar(definitions, varType) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18188
      this.definitions = definitions;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18189
      this.varType = varType;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18190
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18191
    AstVar.prototype.getNames = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18192
      var names = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18193
      for(var i=0,l=this.definitions.length;i<l;++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18194
        names.push(this.definitions[i].name);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18195
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18196
      return names;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18197
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18198
    AstVar.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18199
      return "var " + this.definitions.join(",");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18200
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18201
    function AstStatement(expression) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18202
      this.expression = expression;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18203
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18204
    AstStatement.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18205
      return this.expression.toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18206
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18207
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18208
    function transformStatement(statement) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18209
      if(fieldTest.test(statement)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18210
        var attrAndType = attrAndTypeRegex.exec(statement);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18211
        var definitions = statement.substring(attrAndType[0].length).split(",");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18212
        var defaultTypeValue = getDefaultValueForType(attrAndType[2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18213
        for(var i=0; i < definitions.length; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18214
          definitions[i] = transformVarDefinition(definitions[i], defaultTypeValue);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18215
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18216
        return new AstVar(definitions, attrAndType[2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18217
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18218
      return new AstStatement(transformExpression(statement));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18219
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18220
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18221
    function AstForExpression(initStatement, condition, step) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18222
      this.initStatement = initStatement;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18223
      this.condition = condition;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18224
      this.step = step;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18225
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18226
    AstForExpression.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18227
      return "(" + this.initStatement + "; " + this.condition + "; " + this.step + ")";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18228
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18229
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18230
    function AstForInExpression(initStatement, container) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18231
      this.initStatement = initStatement;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18232
      this.container = container;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18233
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18234
    AstForInExpression.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18235
      var init = this.initStatement.toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18236
      if(init.indexOf("=") >= 0) { // can be without var declaration
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18237
        init = init.substring(0, init.indexOf("="));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18238
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18239
      return "(" + init + " in " + this.container + ")";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18240
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18241
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18242
    function AstForEachExpression(initStatement, container) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18243
      this.initStatement = initStatement;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18244
      this.container = container;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18245
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18246
    AstForEachExpression.iteratorId = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18247
    AstForEachExpression.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18248
      var init = this.initStatement.toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18249
      var iterator = "$it" + (AstForEachExpression.iteratorId++);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18250
      var variableName = init.replace(/^\s*var\s*/, "").split("=")[0];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18251
      var initIteratorAndVariable = "var " + iterator + " = new $p.ObjectIterator(" + this.container + "), " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18252
         variableName + " = void(0)";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18253
      var nextIterationCondition = iterator + ".hasNext() && ((" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18254
         variableName + " = " + iterator + ".next()) || true)";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18255
      return "(" + initIteratorAndVariable + "; " + nextIterationCondition + ";)";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18256
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18257
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18258
    function transformForExpression(expr) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18259
      var content;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18260
      if (/\bin\b/.test(expr)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18261
        content = expr.substring(1, expr.length - 1).split(/\bin\b/g);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18262
        return new AstForInExpression( transformStatement(trim(content[0])),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18263
          transformExpression(content[1]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18264
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18265
      if (expr.indexOf(":") >= 0 && expr.indexOf(";") < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18266
        content = expr.substring(1, expr.length - 1).split(":");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18267
        return new AstForEachExpression( transformStatement(trim(content[0])),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18268
          transformExpression(content[1]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18269
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18270
      content = expr.substring(1, expr.length - 1).split(";");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18271
      return new AstForExpression( transformStatement(trim(content[0])),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18272
        transformExpression(content[1]), transformExpression(content[2]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18273
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18274
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18275
    function sortByWeight(array) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18276
      array.sort(function (a,b) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18277
        return b.weight - a.weight;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18278
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18279
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18280
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18281
    function AstInnerInterface(name, body, isStatic) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18282
      this.name = name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18283
      this.body = body;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18284
      this.isStatic = isStatic;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18285
      body.owner = this;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18286
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18287
    AstInnerInterface.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18288
      return "" + this.body;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18289
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18290
    function AstInnerClass(name, body, isStatic) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18291
      this.name = name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18292
      this.body = body;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18293
      this.isStatic = isStatic;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18294
      body.owner = this;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18295
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18296
    AstInnerClass.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18297
      return "" + this.body;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18298
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18299
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18300
    function transformInnerClass(class_) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18301
      var m = classesRegex.exec(class_); // 1 - attr, 2 - class|int, 3 - name, 4 - extends, 5 - implements, 6 - body
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18302
      classesRegex.lastIndex = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18303
      var isStatic = m[1].indexOf("static") >= 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18304
      var body = atoms[getAtomIndex(m[6])], innerClass;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18305
      var oldClassId = currentClassId, newClassId = generateClassId();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18306
      currentClassId = newClassId;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18307
      if(m[2] === "interface") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18308
        innerClass = new AstInnerInterface(m[3], transformInterfaceBody(body, m[3], m[4]), isStatic);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18309
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18310
        innerClass = new AstInnerClass(m[3], transformClassBody(body, m[3], m[4], m[5]), isStatic);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18311
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18312
      appendClass(innerClass, newClassId, oldClassId);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18313
      currentClassId = oldClassId;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18314
      return innerClass;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18315
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18316
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18317
    function AstClassMethod(name, params, body, isStatic) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18318
      this.name = name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18319
      this.params = params;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18320
      this.body = body;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18321
      this.isStatic = isStatic;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18322
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18323
    AstClassMethod.prototype.toString = function(){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18324
      var paramNames = appendToLookupTable({}, this.params.getNames());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18325
      var oldContext = replaceContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18326
      replaceContext = function (subject) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18327
        return paramNames.hasOwnProperty(subject.name) ? subject.name : oldContext(subject);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18328
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18329
      var result = "function " + this.methodId + this.params + " " + this.body +"\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18330
      replaceContext = oldContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18331
      return result;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18332
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18333
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18334
    function transformClassMethod(method) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18335
      var m = methodsRegex.exec(method);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18336
      methodsRegex.lastIndex = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18337
      var isStatic = m[1].indexOf("static") >= 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18338
      var body = m[6] !== ';' ? atoms[getAtomIndex(m[6])] : "{}";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18339
      return new AstClassMethod(m[3], transformParams(atoms[getAtomIndex(m[4])]),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18340
        transformStatementsBlock(body), isStatic );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18341
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18342
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18343
    function AstClassField(definitions, fieldType, isStatic) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18344
      this.definitions = definitions;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18345
      this.fieldType = fieldType;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18346
      this.isStatic = isStatic;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18347
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18348
    AstClassField.prototype.getNames = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18349
      var names = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18350
      for(var i=0,l=this.definitions.length;i<l;++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18351
        names.push(this.definitions[i].name);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18352
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18353
      return names;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18354
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18355
    AstClassField.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18356
      var thisPrefix = replaceContext({ name: "[this]" });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18357
      if(this.isStatic) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18358
        var className = this.owner.name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18359
        var staticDeclarations = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18360
        for(var i=0,l=this.definitions.length;i<l;++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18361
          var definition = this.definitions[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18362
          var name = definition.name, staticName = className + "." + name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18363
          var declaration = "if(" + staticName + " === void(0)) {\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18364
            " " + staticName + " = " + definition.value + "; }\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18365
            "$p.defineProperty(" + thisPrefix + ", " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18366
            "'" + name + "', { get: function(){return " + staticName + ";}, " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18367
            "set: function(val){" + staticName + " = val;} });\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18368
          staticDeclarations.push(declaration);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18369
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18370
        return staticDeclarations.join("");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18371
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18372
      return thisPrefix + "." + this.definitions.join("; " + thisPrefix + ".");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18373
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18374
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18375
    function transformClassField(statement) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18376
      var attrAndType = attrAndTypeRegex.exec(statement);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18377
      var isStatic = attrAndType[1].indexOf("static") >= 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18378
      var definitions = statement.substring(attrAndType[0].length).split(/,\s*/g);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18379
      var defaultTypeValue = getDefaultValueForType(attrAndType[2]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18380
      for(var i=0; i < definitions.length; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18381
        definitions[i] = transformVarDefinition(definitions[i], defaultTypeValue);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18382
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18383
      return new AstClassField(definitions, attrAndType[2], isStatic);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18384
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18385
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18386
    function AstConstructor(params, body) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18387
      this.params = params;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18388
      this.body = body;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18389
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18390
    AstConstructor.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18391
      var paramNames = appendToLookupTable({}, this.params.getNames());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18392
      var oldContext = replaceContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18393
      replaceContext = function (subject) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18394
        return paramNames.hasOwnProperty(subject.name) ? subject.name : oldContext(subject);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18395
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18396
      var prefix = "function $constr_" + this.params.params.length + this.params.toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18397
      var body = this.body.toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18398
      if(!/\$(superCstr|constr)\b/.test(body)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18399
        body = "{\n$superCstr();\n" + body.substring(1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18400
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18401
      replaceContext = oldContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18402
      return prefix + body + "\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18403
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18404
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18405
    function transformConstructor(cstr) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18406
      var m = new RegExp(/"B(\d+)"\s*"A(\d+)"/).exec(cstr);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18407
      var params = transformParams(atoms[m[1]]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18408
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18409
      return new AstConstructor(params, transformStatementsBlock(atoms[m[2]]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18410
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18411
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18412
    function AstInterfaceBody(name, interfacesNames, methodsNames, fields, innerClasses, misc) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18413
      var i,l;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18414
      this.name = name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18415
      this.interfacesNames = interfacesNames;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18416
      this.methodsNames = methodsNames;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18417
      this.fields = fields;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18418
      this.innerClasses = innerClasses;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18419
      this.misc = misc;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18420
      for(i=0,l=fields.length; i<l; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18421
        fields[i].owner = this;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18422
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18423
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18424
    AstInterfaceBody.prototype.getMembers = function(classFields, classMethods, classInners) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18425
      if(this.owner.base) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18426
        this.owner.base.body.getMembers(classFields, classMethods, classInners);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18427
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18428
      var i, j, l, m;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18429
      for(i=0,l=this.fields.length;i<l;++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18430
        var fieldNames = this.fields[i].getNames();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18431
        for(j=0,m=fieldNames.length;j<m;++j) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18432
          classFields[fieldNames[j]] = this.fields[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18433
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18434
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18435
      for(i=0,l=this.methodsNames.length;i<l;++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18436
        var methodName = this.methodsNames[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18437
        classMethods[methodName] = true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18438
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18439
      for(i=0,l=this.innerClasses.length;i<l;++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18440
        var innerClass = this.innerClasses[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18441
        classInners[innerClass.name] = innerClass;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18442
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18443
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18444
    AstInterfaceBody.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18445
      function getScopeLevel(p) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18446
        var i = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18447
        while(p) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18448
          ++i;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18449
          p=p.scope;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18450
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18451
        return i;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18452
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18453
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18454
      var scopeLevel = getScopeLevel(this.owner);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18455
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18456
      var className = this.name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18457
      var staticDefinitions = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18458
      var metadata = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18459
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18460
      var thisClassFields = {}, thisClassMethods = {}, thisClassInners = {};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18461
      this.getMembers(thisClassFields, thisClassMethods, thisClassInners);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18462
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18463
      var i, l, j, m;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18464
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18465
      if (this.owner.interfaces) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18466
        // interface name can be present, but interface is not
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18467
        var resolvedInterfaces = [], resolvedInterface;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18468
        for (i = 0, l = this.interfacesNames.length; i < l; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18469
          if (!this.owner.interfaces[i]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18470
            continue;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18471
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18472
          resolvedInterface = replaceContext({name: this.interfacesNames[i]});
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18473
          resolvedInterfaces.push(resolvedInterface);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18474
          staticDefinitions += "$p.extendInterfaceMembers(" + className + ", " + resolvedInterface + ");\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18475
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18476
        metadata += className + ".$interfaces = [" + resolvedInterfaces.join(", ") + "];\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18477
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18478
      metadata += className + ".$isInterface = true;\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18479
      metadata += className + ".$methods = [\'" + this.methodsNames.join("\', \'") + "\'];\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18480
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18481
      sortByWeight(this.innerClasses);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18482
      for (i = 0, l = this.innerClasses.length; i < l; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18483
        var innerClass = this.innerClasses[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18484
        if (innerClass.isStatic) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18485
          staticDefinitions += className + "." + innerClass.name + " = " + innerClass + ";\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18486
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18487
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18488
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18489
      for (i = 0, l = this.fields.length; i < l; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18490
        var field = this.fields[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18491
        if (field.isStatic) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18492
          staticDefinitions += className + "." + field.definitions.join(";\n" + className + ".") + ";\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18493
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18494
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18495
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18496
      return "(function() {\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18497
        "function " + className + "() { throw \'Unable to create the interface\'; }\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18498
        staticDefinitions +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18499
        metadata +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18500
        "return " + className + ";\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18501
        "})()";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18502
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18503
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18504
    transformInterfaceBody = function(body, name, baseInterfaces) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18505
      var declarations = body.substring(1, body.length - 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18506
      declarations = extractClassesAndMethods(declarations);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18507
      declarations = extractConstructors(declarations, name);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18508
      var methodsNames = [], classes = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18509
      declarations = declarations.replace(/"([DE])(\d+)"/g, function(all, type, index) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18510
        if(type === 'D') { methodsNames.push(index); }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18511
        else if(type === 'E') { classes.push(index); }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18512
        return "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18513
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18514
      var fields = declarations.split(/;(?:\s*;)*/g);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18515
      var baseInterfaceNames;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18516
      var i, l;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18517
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18518
      if(baseInterfaces !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18519
        baseInterfaceNames = baseInterfaces.replace(/^\s*extends\s+(.+?)\s*$/g, "$1").split(/\s*,\s*/g);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18520
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18521
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18522
      for(i = 0, l = methodsNames.length; i < l; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18523
        var method = transformClassMethod(atoms[methodsNames[i]]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18524
        methodsNames[i] = method.name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18525
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18526
      for(i = 0, l = fields.length - 1; i < l; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18527
        var field = trimSpaces(fields[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18528
        fields[i] = transformClassField(field.middle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18529
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18530
      var tail = fields.pop();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18531
      for(i = 0, l = classes.length; i < l; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18532
        classes[i] = transformInnerClass(atoms[classes[i]]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18533
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18534
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18535
      return new AstInterfaceBody(name, baseInterfaceNames, methodsNames, fields, classes, { tail: tail });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18536
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18537
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18538
    function AstClassBody(name, baseClassName, interfacesNames, functions, methods, fields, cstrs, innerClasses, misc) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18539
      var i,l;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18540
      this.name = name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18541
      this.baseClassName = baseClassName;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18542
      this.interfacesNames = interfacesNames;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18543
      this.functions = functions;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18544
      this.methods = methods;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18545
      this.fields = fields;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18546
      this.cstrs = cstrs;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18547
      this.innerClasses = innerClasses;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18548
      this.misc = misc;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18549
      for(i=0,l=fields.length; i<l; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18550
        fields[i].owner = this;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18551
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18552
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18553
    AstClassBody.prototype.getMembers = function(classFields, classMethods, classInners) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18554
      if(this.owner.base) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18555
        this.owner.base.body.getMembers(classFields, classMethods, classInners);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18556
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18557
      var i, j, l, m;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18558
      for(i=0,l=this.fields.length;i<l;++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18559
        var fieldNames = this.fields[i].getNames();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18560
        for(j=0,m=fieldNames.length;j<m;++j) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18561
          classFields[fieldNames[j]] = this.fields[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18562
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18563
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18564
      for(i=0,l=this.methods.length;i<l;++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18565
        var method = this.methods[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18566
        classMethods[method.name] = method;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18567
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18568
      for(i=0,l=this.innerClasses.length;i<l;++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18569
        var innerClass = this.innerClasses[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18570
        classInners[innerClass.name] = innerClass;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18571
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18572
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18573
    AstClassBody.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18574
      function getScopeLevel(p) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18575
        var i = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18576
        while(p) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18577
          ++i;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18578
          p=p.scope;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18579
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18580
        return i;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18581
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18582
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18583
      var scopeLevel = getScopeLevel(this.owner);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18584
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18585
      var selfId = "$this_" + scopeLevel;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18586
      var className = this.name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18587
      var result = "var " + selfId + " = this;\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18588
      var staticDefinitions = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18589
      var metadata = "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18590
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18591
      var thisClassFields = {}, thisClassMethods = {}, thisClassInners = {};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18592
      this.getMembers(thisClassFields, thisClassMethods, thisClassInners);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18593
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18594
      var oldContext = replaceContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18595
      replaceContext = function (subject) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18596
        var name = subject.name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18597
        if(name === "this") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18598
          // returns "$this_N.$self" pointer instead of "this" in cases:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18599
          // "this()", "this.XXX()", "this", but not for "this.XXX"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18600
          return subject.callSign || !subject.member ? selfId + ".$self" : selfId;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18601
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18602
        if(thisClassFields.hasOwnProperty(name)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18603
          return thisClassFields[name].isStatic ? className + "." + name : selfId + "." + name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18604
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18605
        if(thisClassInners.hasOwnProperty(name)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18606
          return selfId + "." + name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18607
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18608
        if(thisClassMethods.hasOwnProperty(name)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18609
          return thisClassMethods[name].isStatic ? className + "." + name : selfId + ".$self." + name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18610
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18611
        return oldContext(subject);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18612
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18613
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18614
      var resolvedBaseClassName;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18615
      if (this.baseClassName) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18616
        resolvedBaseClassName = oldContext({name: this.baseClassName});
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18617
        result += "var $super = { $upcast: " + selfId + " };\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18618
        result += "function $superCstr(){" + resolvedBaseClassName +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18619
          ".apply($super,arguments);if(!('$self' in $super)) $p.extendClassChain($super)}\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18620
        metadata += className + ".$base = " + resolvedBaseClassName + ";\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18621
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18622
        result += "function $superCstr(){$p.extendClassChain("+ selfId +")}\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18623
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18624
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18625
      if (this.owner.base) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18626
        // base class name can be present, but class is not
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18627
        staticDefinitions += "$p.extendStaticMembers(" + className + ", " + resolvedBaseClassName + ");\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18628
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18629
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18630
      var i, l, j, m;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18631
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18632
      if (this.owner.interfaces) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18633
        // interface name can be present, but interface is not
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18634
        var resolvedInterfaces = [], resolvedInterface;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18635
        for (i = 0, l = this.interfacesNames.length; i < l; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18636
          if (!this.owner.interfaces[i]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18637
            continue;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18638
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18639
          resolvedInterface = oldContext({name: this.interfacesNames[i]});
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18640
          resolvedInterfaces.push(resolvedInterface);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18641
          staticDefinitions += "$p.extendInterfaceMembers(" + className + ", " + resolvedInterface + ");\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18642
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18643
        metadata += className + ".$interfaces = [" + resolvedInterfaces.join(", ") + "];\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18644
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18645
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18646
      if (this.functions.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18647
        result += this.functions.join('\n') + '\n';
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18648
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18649
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18650
      sortByWeight(this.innerClasses);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18651
      for (i = 0, l = this.innerClasses.length; i < l; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18652
        var innerClass = this.innerClasses[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18653
        if (innerClass.isStatic) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18654
          staticDefinitions += className + "." + innerClass.name + " = " + innerClass + ";\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18655
          result += selfId + "." + innerClass.name + " = " + className + "." + innerClass.name + ";\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18656
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18657
          result += selfId + "." + innerClass.name + " = " + innerClass + ";\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18658
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18659
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18660
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18661
      for (i = 0, l = this.fields.length; i < l; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18662
        var field = this.fields[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18663
        if (field.isStatic) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18664
          staticDefinitions += className + "." + field.definitions.join(";\n" + className + ".") + ";\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18665
          for (j = 0, m = field.definitions.length; j < m; ++j) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18666
            var fieldName = field.definitions[j].name, staticName = className + "." + fieldName;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18667
            result += "$p.defineProperty(" + selfId + ", '" + fieldName + "', {" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18668
              "get: function(){return " + staticName + "}, " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18669
              "set: function(val){" + staticName + " = val}});\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18670
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18671
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18672
          result += selfId + "." + field.definitions.join(";\n" + selfId + ".") + ";\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18673
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18674
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18675
      var methodOverloads = {};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18676
      for (i = 0, l = this.methods.length; i < l; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18677
        var method = this.methods[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18678
        var overload = methodOverloads[method.name];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18679
        var methodId = method.name + "$" + method.params.params.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18680
        if (overload) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18681
          ++overload;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18682
          methodId += "_" + overload;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18683
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18684
          overload = 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18685
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18686
        method.methodId = methodId;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18687
        methodOverloads[method.name] = overload;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18688
        if (method.isStatic) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18689
          staticDefinitions += method;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18690
          staticDefinitions += "$p.addMethod(" + className + ", '" + method.name + "', " + methodId + ");\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18691
          result += "$p.addMethod(" + selfId + ", '" + method.name + "', " + methodId + ");\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18692
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18693
          result += method;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18694
          result += "$p.addMethod(" + selfId + ", '" + method.name + "', " + methodId + ");\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18695
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18696
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18697
      result += trim(this.misc.tail);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18698
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18699
      if (this.cstrs.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18700
        result += this.cstrs.join('\n') + '\n';
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18701
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18702
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18703
      result += "function $constr() {\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18704
      var cstrsIfs = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18705
      for (i = 0, l = this.cstrs.length; i < l; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18706
        var paramsLength = this.cstrs[i].params.params.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18707
        cstrsIfs.push("if(arguments.length === " + paramsLength + ") { " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18708
          "$constr_" + paramsLength + ".apply(" + selfId + ", arguments); }");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18709
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18710
      if(cstrsIfs.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18711
        result += cstrsIfs.join(" else ") + " else ";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18712
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18713
      // ??? add check if length is 0, otherwise fail
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18714
      result += "$superCstr();\n}\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18715
      result += "$constr.apply(null, arguments);\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18716
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18717
      replaceContext = oldContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18718
      return "(function() {\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18719
        "function " + className + "() {\n" + result + "}\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18720
        staticDefinitions +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18721
        metadata +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18722
        "return " + className + ";\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18723
        "})()";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18724
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18725
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18726
    transformClassBody = function(body, name, baseName, interfaces) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18727
      var declarations = body.substring(1, body.length - 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18728
      declarations = extractClassesAndMethods(declarations);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18729
      declarations = extractConstructors(declarations, name);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18730
      var methods = [], classes = [], cstrs = [], functions = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18731
      declarations = declarations.replace(/"([DEGH])(\d+)"/g, function(all, type, index) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18732
        if(type === 'D') { methods.push(index); }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18733
        else if(type === 'E') { classes.push(index); }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18734
        else if(type === 'H') { functions.push(index); }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18735
        else { cstrs.push(index); }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18736
        return "";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18737
      });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18738
      var fields = declarations.replace(/^(?:\s*;)+/, "").split(/;(?:\s*;)*/g);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18739
      var baseClassName, interfacesNames;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18740
      var i;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18741
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18742
      if(baseName !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18743
        baseClassName = baseName.replace(/^\s*extends\s+([A-Za-z_$][\w$]*\b(?:\s*\.\s*[A-Za-z_$][\w$]*\b)*)\s*$/g, "$1");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18744
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18745
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18746
      if(interfaces !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18747
        interfacesNames = interfaces.replace(/^\s*implements\s+(.+?)\s*$/g, "$1").split(/\s*,\s*/g);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18748
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18749
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18750
      for(i = 0; i < functions.length; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18751
        functions[i] = transformFunction(atoms[functions[i]]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18752
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18753
      for(i = 0; i < methods.length; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18754
        methods[i] = transformClassMethod(atoms[methods[i]]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18755
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18756
      for(i = 0; i < fields.length - 1; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18757
        var field = trimSpaces(fields[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18758
        fields[i] = transformClassField(field.middle);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18759
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18760
      var tail = fields.pop();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18761
      for(i = 0; i < cstrs.length; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18762
        cstrs[i] = transformConstructor(atoms[cstrs[i]]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18763
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18764
      for(i = 0; i < classes.length; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18765
        classes[i] = transformInnerClass(atoms[classes[i]]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18766
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18767
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18768
      return new AstClassBody(name, baseClassName, interfacesNames, functions, methods, fields, cstrs,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18769
        classes, { tail: tail });
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18770
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18771
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18772
    function AstInterface(name, body) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18773
      this.name = name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18774
      this.body = body;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18775
      body.owner = this;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18776
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18777
    AstInterface.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18778
      return "var " + this.name + " = " + this.body + ";\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18779
        "$p." + this.name + " = " + this.name + ";\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18780
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18781
    function AstClass(name, body) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18782
      this.name = name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18783
      this.body = body;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18784
      body.owner = this;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18785
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18786
    AstClass.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18787
      return "var " + this.name + " = " + this.body + ";\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18788
        "$p." + this.name + " = " + this.name + ";\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18789
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18790
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18791
    function transformGlobalClass(class_) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18792
      var m = classesRegex.exec(class_); // 1 - attr, 2 - class|int, 3 - name, 4 - extends, 5 - implements, 6 - body
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18793
      classesRegex.lastIndex = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18794
      var body = atoms[getAtomIndex(m[6])];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18795
      var oldClassId = currentClassId, newClassId = generateClassId();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18796
      currentClassId = newClassId;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18797
      var globalClass;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18798
      if(m[2] === "interface") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18799
        globalClass = new AstInterface(m[3], transformInterfaceBody(body, m[3], m[4]) );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18800
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18801
        globalClass = new AstClass(m[3], transformClassBody(body, m[3], m[4], m[5]) );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18802
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18803
      appendClass(globalClass, newClassId, oldClassId);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18804
      currentClassId = oldClassId;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18805
      return globalClass;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18806
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18807
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18808
    function AstMethod(name, params, body) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18809
      this.name = name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18810
      this.params = params;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18811
      this.body = body;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18812
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18813
    AstMethod.prototype.toString = function(){
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18814
      var paramNames = appendToLookupTable({}, this.params.getNames());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18815
      var oldContext = replaceContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18816
      replaceContext = function (subject) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18817
        return paramNames.hasOwnProperty(subject.name) ? subject.name : oldContext(subject);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18818
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18819
      var result = "function " + this.name + this.params + " " + this.body + "\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18820
        "$p." + this.name + " = " + this.name + ";";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18821
      replaceContext = oldContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18822
      return result;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18823
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18824
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18825
    function transformGlobalMethod(method) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18826
      var m = methodsRegex.exec(method);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18827
      var result =
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18828
      methodsRegex.lastIndex = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18829
      return new AstMethod(m[3], transformParams(atoms[getAtomIndex(m[4])]),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18830
        transformStatementsBlock(atoms[getAtomIndex(m[6])]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18831
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18832
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18833
    function preStatementsTransform(statements) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18834
      var s = statements;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18835
      // turns multiple catch blocks into one, because we have no way to properly get into them anyway.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18836
      s = s.replace(/\b(catch\s*"B\d+"\s*"A\d+")(\s*catch\s*"B\d+"\s*"A\d+")+/g, "$1");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18837
      return s;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18838
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18839
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18840
    function AstForStatement(argument, misc) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18841
      this.argument = argument;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18842
      this.misc = misc;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18843
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18844
    AstForStatement.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18845
      return this.misc.prefix + this.argument.toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18846
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18847
    function AstCatchStatement(argument, misc) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18848
      this.argument = argument;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18849
      this.misc = misc;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18850
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18851
    AstCatchStatement.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18852
      return this.misc.prefix + this.argument.toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18853
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18854
    function AstPrefixStatement(name, argument, misc) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18855
      this.name = name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18856
      this.argument = argument;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18857
      this.misc = misc;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18858
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18859
    AstPrefixStatement.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18860
      var result = this.misc.prefix;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18861
      if(this.argument !== undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18862
        result += this.argument.toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18863
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18864
      return result;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18865
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18866
    function AstSwitchCase(expr) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18867
      this.expr = expr;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18868
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18869
    AstSwitchCase.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18870
      return "case " + this.expr + ":";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18871
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18872
    function AstLabel(label) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18873
      this.label = label;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18874
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18875
    AstLabel.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18876
      return this.label;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18877
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18878
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18879
    transformStatements = function(statements, transformMethod, transformClass) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18880
      var nextStatement = new RegExp(/\b(catch|for|if|switch|while|with)\s*"B(\d+)"|\b(do|else|finally|return|throw|try|break|continue)\b|("[ADEH](\d+)")|\b(case)\s+([^:]+):|\b([A-Za-z_$][\w$]*\s*:)|(;)/g);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18881
      var res = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18882
      statements = preStatementsTransform(statements);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18883
      var lastIndex = 0, m, space;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18884
      // m contains the matches from the nextStatement regexp, null if there are no matches.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18885
      // nextStatement.exec starts searching at nextStatement.lastIndex.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18886
      while((m = nextStatement.exec(statements)) !== null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18887
        if(m[1] !== undef) { // catch, for ...
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18888
          var i = statements.lastIndexOf('"B', nextStatement.lastIndex);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18889
          var statementsPrefix = statements.substring(lastIndex, i);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18890
          if(m[1] === "for") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18891
            res.push(new AstForStatement(transformForExpression(atoms[m[2]]),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18892
              { prefix: statementsPrefix }) );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18893
          } else if(m[1] === "catch") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18894
            res.push(new AstCatchStatement(transformParams(atoms[m[2]]),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18895
              { prefix: statementsPrefix }) );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18896
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18897
            res.push(new AstPrefixStatement(m[1], transformExpression(atoms[m[2]]),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18898
              { prefix: statementsPrefix }) );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18899
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18900
        } else if(m[3] !== undef) { // do, else, ...
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18901
            res.push(new AstPrefixStatement(m[3], undef,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18902
              { prefix: statements.substring(lastIndex, nextStatement.lastIndex) }) );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18903
        } else if(m[4] !== undef) { // block, class and methods
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18904
          space = statements.substring(lastIndex, nextStatement.lastIndex - m[4].length);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18905
          if(trim(space).length !== 0) { continue; } // avoiding new type[] {} construct
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18906
          res.push(space);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18907
          var kind = m[4].charAt(1), atomIndex = m[5];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18908
          if(kind === 'D') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18909
            res.push(transformMethod(atoms[atomIndex]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18910
          } else if(kind === 'E') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18911
            res.push(transformClass(atoms[atomIndex]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18912
          } else if(kind === 'H') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18913
            res.push(transformFunction(atoms[atomIndex]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18914
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18915
            res.push(transformStatementsBlock(atoms[atomIndex]));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18916
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18917
        } else if(m[6] !== undef) { // switch case
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18918
          res.push(new AstSwitchCase(transformExpression(trim(m[7]))));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18919
        } else if(m[8] !== undef) { // label
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18920
          space = statements.substring(lastIndex, nextStatement.lastIndex - m[8].length);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18921
          if(trim(space).length !== 0) { continue; } // avoiding ?: construct
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18922
          res.push(new AstLabel(statements.substring(lastIndex, nextStatement.lastIndex)) );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18923
        } else { // semicolon
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18924
          var statement = trimSpaces(statements.substring(lastIndex, nextStatement.lastIndex - 1));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18925
          res.push(statement.left);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18926
          res.push(transformStatement(statement.middle));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18927
          res.push(statement.right + ";");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18928
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18929
        lastIndex = nextStatement.lastIndex;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18930
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18931
      var statementsTail = trimSpaces(statements.substring(lastIndex));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18932
      res.push(statementsTail.left);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18933
      if(statementsTail.middle !== "") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18934
        res.push(transformStatement(statementsTail.middle));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18935
        res.push(";" + statementsTail.right);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18936
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18937
      return res;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18938
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18939
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18940
    function getLocalNames(statements) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18941
      var localNames = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18942
      for(var i=0,l=statements.length;i<l;++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18943
        var statement = statements[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18944
        if(statement instanceof AstVar) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18945
          localNames = localNames.concat(statement.getNames());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18946
        } else if(statement instanceof AstForStatement &&
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18947
          statement.argument.initStatement instanceof AstVar) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18948
          localNames = localNames.concat(statement.argument.initStatement.getNames());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18949
        } else if(statement instanceof AstInnerInterface || statement instanceof AstInnerClass ||
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18950
          statement instanceof AstInterface || statement instanceof AstClass ||
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18951
          statement instanceof AstMethod || statement instanceof AstFunction) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18952
          localNames.push(statement.name);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18953
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18954
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18955
      return appendToLookupTable({}, localNames);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18956
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18957
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18958
    function AstStatementsBlock(statements) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18959
      this.statements = statements;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18960
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18961
    AstStatementsBlock.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18962
      var localNames = getLocalNames(this.statements);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18963
      var oldContext = replaceContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18964
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18965
      // replacing context only when necessary
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18966
      if(!isLookupTableEmpty(localNames)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18967
        replaceContext = function (subject) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18968
          return localNames.hasOwnProperty(subject.name) ? subject.name : oldContext(subject);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18969
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18970
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18971
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18972
      var result = "{\n" + this.statements.join('') + "\n}";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18973
      replaceContext = oldContext;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18974
      return result;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18975
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18976
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18977
    transformStatementsBlock = function(block) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18978
      var content = trimSpaces(block.substring(1, block.length - 1));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18979
      return new AstStatementsBlock(transformStatements(content.middle));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18980
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18981
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18982
    function AstRoot(statements) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18983
      this.statements = statements;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18984
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18985
    AstRoot.prototype.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18986
      var classes = [], otherStatements = [], statement;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18987
      for (var i = 0, len = this.statements.length; i < len; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18988
        statement = this.statements[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18989
        if (statement instanceof AstClass || statement instanceof AstInterface) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18990
          classes.push(statement);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18991
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18992
          otherStatements.push(statement);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18993
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18994
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18995
      sortByWeight(classes);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18996
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18997
      var localNames = getLocalNames(this.statements);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18998
      replaceContext = function (subject) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 18999
        var name = subject.name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19000
        if(localNames.hasOwnProperty(name)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19001
          return name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19002
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19003
        if(globalMembers.hasOwnProperty(name) ||
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19004
           PConstants.hasOwnProperty(name) ||
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19005
           defaultScope.hasOwnProperty(name)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19006
          return "$p." + name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19007
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19008
        return name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19009
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19010
      var result = "// this code was autogenerated from PJS\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19011
        "(function($p) {\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19012
        classes.join('') + "\n" +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19013
        otherStatements.join('') + "\n})";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19014
      replaceContext = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19015
      return result;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19016
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19017
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19018
    transformMain = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19019
      var statements = extractClassesAndMethods(atoms[0]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19020
      statements = statements.replace(/\bimport\s+[^;]+;/g, "");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19021
      return new AstRoot( transformStatements(statements,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19022
        transformGlobalMethod, transformGlobalClass) );
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19023
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19024
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19025
    function generateMetadata(ast) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19026
      var globalScope = {};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19027
      var id, class_;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19028
      for(id in declaredClasses) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19029
        if(declaredClasses.hasOwnProperty(id)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19030
          class_ = declaredClasses[id];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19031
          var scopeId = class_.scopeId, name = class_.name;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19032
          if(scopeId) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19033
            var scope = declaredClasses[scopeId];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19034
            class_.scope = scope;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19035
            if(scope.inScope === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19036
              scope.inScope = {};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19037
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19038
            scope.inScope[name] = class_;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19039
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19040
            globalScope[name] = class_;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19041
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19042
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19043
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19044
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19045
      function findInScopes(class_, name) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19046
        var parts = name.split('.');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19047
        var currentScope = class_.scope, found;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19048
        while(currentScope) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19049
          if(currentScope.hasOwnProperty(parts[0])) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19050
            found = currentScope[parts[0]]; break;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19051
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19052
          currentScope = currentScope.scope;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19053
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19054
        if(found === undef) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19055
          found = globalScope[parts[0]];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19056
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19057
        for(var i=1,l=parts.length;i<l && found;++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19058
          found = found.inScope[parts[i]];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19059
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19060
        return found;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19061
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19062
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19063
      for(id in declaredClasses) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19064
        if(declaredClasses.hasOwnProperty(id)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19065
          class_ = declaredClasses[id];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19066
          var baseClassName = class_.body.baseClassName;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19067
          if(baseClassName) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19068
            var parent = findInScopes(class_, baseClassName);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19069
            if (parent) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19070
              class_.base = parent;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19071
              if (!parent.derived) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19072
                parent.derived = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19073
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19074
              parent.derived.push(class_);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19075
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19076
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19077
          var interfacesNames = class_.body.interfacesNames,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19078
            interfaces = [], i, l;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19079
          if (interfacesNames && interfacesNames.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19080
            for (i = 0, l = interfacesNames.length; i < l; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19081
              var interface_ = findInScopes(class_, interfacesNames[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19082
              interfaces.push(interface_);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19083
              if (!interface_) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19084
                continue;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19085
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19086
              if (!interface_.derived) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19087
                interface_.derived = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19088
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19089
              interface_.derived.push(class_);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19090
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19091
            if (interfaces.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19092
              class_.interfaces = interfaces;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19093
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19094
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19095
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19096
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19097
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19098
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19099
    function setWeight(ast) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19100
      var queue = [], tocheck = {};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19101
      var id, scopeId, class_;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19102
      // queue most inner and non-inherited
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19103
      for (id in declaredClasses) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19104
        if (declaredClasses.hasOwnProperty(id)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19105
          class_ = declaredClasses[id];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19106
          if (!class_.inScope && !class_.derived) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19107
            queue.push(id);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19108
            class_.weight = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19109
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19110
            var dependsOn = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19111
            if (class_.inScope) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19112
              for (scopeId in class_.inScope) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19113
                if (class_.inScope.hasOwnProperty(scopeId)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19114
                  dependsOn.push(class_.inScope[scopeId]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19115
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19116
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19117
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19118
            if (class_.derived) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19119
              dependsOn = dependsOn.concat(class_.derived);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19120
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19121
            tocheck[id] = dependsOn;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19122
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19123
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19124
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19125
      function removeDependentAndCheck(targetId, from) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19126
        var dependsOn = tocheck[targetId];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19127
        if (!dependsOn) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19128
          return false; // no need to process
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19129
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19130
        var i = dependsOn.indexOf(from);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19131
        if (i < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19132
          return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19133
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19134
        dependsOn.splice(i, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19135
        if (dependsOn.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19136
          return false;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19137
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19138
        delete tocheck[targetId];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19139
        return true;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19140
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19141
      while (queue.length > 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19142
        id = queue.shift();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19143
        class_ = declaredClasses[id];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19144
        if (class_.scopeId && removeDependentAndCheck(class_.scopeId, class_)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19145
          queue.push(class_.scopeId);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19146
          declaredClasses[class_.scopeId].weight = class_.weight + 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19147
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19148
        if (class_.base && removeDependentAndCheck(class_.base.classId, class_)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19149
          queue.push(class_.base.classId);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19150
          class_.base.weight = class_.weight + 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19151
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19152
        if (class_.interfaces) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19153
          var i, l;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19154
          for (i = 0, l = class_.interfaces.length; i < l; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19155
            if (!class_.interfaces[i] ||
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19156
                !removeDependentAndCheck(class_.interfaces[i].classId, class_)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19157
              continue;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19158
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19159
            queue.push(class_.interfaces[i].classId);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19160
            class_.interfaces[i].weight = class_.weight + 1;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19161
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19162
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19163
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19164
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19165
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19166
    var transformed = transformMain();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19167
    generateMetadata(transformed);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19168
    setWeight(transformed);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19169
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19170
    var redendered = transformed.toString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19171
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19172
    // remove empty extra lines with space
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19173
    redendered = redendered.replace(/\s*\n(?:[\t ]*\n)+/g, "\n\n");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19174
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19175
    return injectStrings(redendered, strings);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19176
  }// Parser ends
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19177
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19178
  function preprocessCode(aCode, sketch) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19179
    // Parse out @pjs directive, if any.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19180
    var dm = new RegExp(/\/\*\s*@pjs\s+((?:[^\*]|\*+[^\*\/])*)\*\//g).exec(aCode);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19181
    if (dm && dm.length === 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19182
      // masks contents of a JSON to be replaced later
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19183
      // to protect the contents from further parsing
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19184
      var jsonItems = [],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19185
          directives = dm.splice(1, 2)[0].replace(/\{([\s\S]*?)\}/g, (function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19186
            return function(all, item) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19187
              jsonItems.push(item);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19188
              return "{" + (jsonItems.length-1) + "}";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19189
            };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19190
          }())).replace('\n', '').replace('\r', '').split(";");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19191
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19192
      // We'll L/RTrim, and also remove any surrounding double quotes (e.g., just take string contents)
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19193
      var clean = function(s) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19194
        return s.replace(/^\s*["']?/, '').replace(/["']?\s*$/, '');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19195
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19196
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19197
      for (var i = 0, dl = directives.length; i < dl; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19198
        var pair = directives[i].split('=');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19199
        if (pair && pair.length === 2) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19200
          var key = clean(pair[0]),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19201
              value = clean(pair[1]),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19202
              list = [];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19203
          // A few directives require work beyond storying key/value pairings
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19204
          if (key === "preload") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19205
            list = value.split(',');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19206
            // All pre-loaded images will get put in imageCache, keyed on filename
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19207
            for (var j = 0, jl = list.length; j < jl; j++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19208
              var imageName = clean(list[j]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19209
              sketch.imageCache.add(imageName);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19210
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19211
          // fonts can be declared as a string containing a url,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19212
          // or a JSON object, containing a font name, and a url
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19213
          } else if (key === "font") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19214
            list = value.split(",");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19215
            for (var x = 0, xl = list.length; x < xl; x++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19216
              var fontName = clean(list[x]),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19217
                  index = /^\{(\d*?)\}$/.exec(fontName);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19218
              // if index is not null, send JSON, otherwise, send string
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19219
              PFont.preloading.add(index ? JSON.parse("{" + jsonItems[index[1]] + "}") : fontName);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19220
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19221
          } else if (key === "pauseOnBlur") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19222
            sketch.options.pauseOnBlur = value === "true";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19223
          } else if (key === "globalKeyEvents") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19224
            sketch.options.globalKeyEvents = value === "true";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19225
          } else if (key.substring(0, 6) === "param-") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19226
            sketch.params[key.substring(6)] = value;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19227
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19228
            sketch.options[key] = value;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19229
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19230
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19231
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19232
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19233
    return aCode;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19234
  }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19235
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19236
  // Parse/compiles Processing (Java-like) syntax to JavaScript syntax
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19237
  Processing.compile = function(pdeCode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19238
    var sketch = new Processing.Sketch();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19239
    var code = preprocessCode(pdeCode, sketch);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19240
    var compiledPde = parseProcessing(code);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19241
    sketch.sourceCode = compiledPde;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19242
    return sketch;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19243
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19244
//#endif
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19245
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19246
  // tinylog lite JavaScript library
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19247
  // http://purl.eligrey.com/tinylog/lite
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19248
  /*global tinylog,print*/
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19249
  var tinylogLite = (function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19250
    "use strict";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19251
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19252
    var tinylogLite = {},
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19253
      undef = "undefined",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19254
      func = "function",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19255
      False = !1,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19256
      True = !0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19257
      logLimit = 512,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19258
      log = "log";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19259
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19260
    if (typeof tinylog !== undef && typeof tinylog[log] === func) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19261
      // pre-existing tinylog present
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19262
      tinylogLite[log] = tinylog[log];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19263
    } else if (typeof document !== undef && !document.fake) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19264
      (function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19265
        // DOM document
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19266
        var doc = document,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19267
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19268
        $div = "div",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19269
        $style = "style",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19270
        $title = "title",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19271
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19272
        containerStyles = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19273
          zIndex: 10000,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19274
          position: "fixed",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19275
          bottom: "0px",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19276
          width: "100%",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19277
          height: "15%",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19278
          fontFamily: "sans-serif",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19279
          color: "#ccc",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19280
          backgroundColor: "black"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19281
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19282
        outputStyles = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19283
          position: "relative",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19284
          fontFamily: "monospace",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19285
          overflow: "auto",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19286
          height: "100%",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19287
          paddingTop: "5px"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19288
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19289
        resizerStyles = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19290
          height: "5px",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19291
          marginTop: "-5px",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19292
          cursor: "n-resize",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19293
          backgroundColor: "darkgrey"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19294
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19295
        closeButtonStyles = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19296
          position: "absolute",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19297
          top: "5px",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19298
          right: "20px",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19299
          color: "#111",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19300
          MozBorderRadius: "4px",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19301
          webkitBorderRadius: "4px",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19302
          borderRadius: "4px",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19303
          cursor: "pointer",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19304
          fontWeight: "normal",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19305
          textAlign: "center",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19306
          padding: "3px 5px",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19307
          backgroundColor: "#333",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19308
          fontSize: "12px"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19309
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19310
        entryStyles = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19311
          //borderBottom: "1px solid #d3d3d3",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19312
          minHeight: "16px"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19313
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19314
        entryTextStyles = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19315
          fontSize: "12px",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19316
          margin: "0 8px 0 8px",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19317
          maxWidth: "100%",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19318
          whiteSpace: "pre-wrap",
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19319
          overflow: "auto"
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19320
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19321
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19322
        view = doc.defaultView,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19323
          docElem = doc.documentElement,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19324
          docElemStyle = docElem[$style],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19325
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19326
        setStyles = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19327
          var i = arguments.length,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19328
            elemStyle, styles, style;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19329
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19330
          while (i--) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19331
            styles = arguments[i--];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19332
            elemStyle = arguments[i][$style];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19333
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19334
            for (style in styles) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19335
              if (styles.hasOwnProperty(style)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19336
                elemStyle[style] = styles[style];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19337
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19338
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19339
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19340
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19341
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19342
        observer = function(obj, event, handler) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19343
          if (obj.addEventListener) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19344
            obj.addEventListener(event, handler, False);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19345
          } else if (obj.attachEvent) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19346
            obj.attachEvent("on" + event, handler);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19347
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19348
          return [obj, event, handler];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19349
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19350
        unobserve = function(obj, event, handler) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19351
          if (obj.removeEventListener) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19352
            obj.removeEventListener(event, handler, False);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19353
          } else if (obj.detachEvent) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19354
            obj.detachEvent("on" + event, handler);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19355
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19356
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19357
        clearChildren = function(node) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19358
          var children = node.childNodes,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19359
            child = children.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19360
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19361
          while (child--) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19362
            node.removeChild(children.item(0));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19363
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19364
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19365
        append = function(to, elem) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19366
          return to.appendChild(elem);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19367
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19368
        createElement = function(localName) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19369
          return doc.createElement(localName);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19370
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19371
        createTextNode = function(text) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19372
          return doc.createTextNode(text);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19373
        },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19374
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19375
        createLog = tinylogLite[log] = function(message) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19376
          // don't show output log until called once
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19377
          var uninit,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19378
            originalPadding = docElemStyle.paddingBottom,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19379
            container = createElement($div),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19380
            containerStyle = container[$style],
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19381
            resizer = append(container, createElement($div)),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19382
            output = append(container, createElement($div)),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19383
            closeButton = append(container, createElement($div)),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19384
            resizingLog = False,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19385
            previousHeight = False,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19386
            previousScrollTop = False,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19387
            messages = 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19388
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19389
            updateSafetyMargin = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19390
              // have a blank space large enough to fit the output box at the page bottom
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19391
              docElemStyle.paddingBottom = container.clientHeight + "px";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19392
            },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19393
            setContainerHeight = function(height) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19394
              var viewHeight = view.innerHeight,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19395
                resizerHeight = resizer.clientHeight;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19396
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19397
              // constrain the container inside the viewport's dimensions
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19398
              if (height < 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19399
                height = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19400
              } else if (height + resizerHeight > viewHeight) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19401
                height = viewHeight - resizerHeight;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19402
              }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19403
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19404
              containerStyle.height = height / viewHeight * 100 + "%";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19405
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19406
              updateSafetyMargin();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19407
            },
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19408
            observers = [
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19409
              observer(doc, "mousemove", function(evt) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19410
                if (resizingLog) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19411
                  setContainerHeight(view.innerHeight - evt.clientY);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19412
                  output.scrollTop = previousScrollTop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19413
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19414
              }),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19415
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19416
              observer(doc, "mouseup", function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19417
                if (resizingLog) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19418
                  resizingLog = previousScrollTop = False;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19419
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19420
              }),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19421
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19422
              observer(resizer, "dblclick", function(evt) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19423
                evt.preventDefault();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19424
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19425
                if (previousHeight) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19426
                  setContainerHeight(previousHeight);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19427
                  previousHeight = False;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19428
                } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19429
                  previousHeight = container.clientHeight;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19430
                  containerStyle.height = "0px";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19431
                }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19432
              }),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19433
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19434
              observer(resizer, "mousedown", function(evt) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19435
                evt.preventDefault();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19436
                resizingLog = True;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19437
                previousScrollTop = output.scrollTop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19438
              }),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19439
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19440
              observer(resizer, "contextmenu", function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19441
                resizingLog = False;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19442
              }),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19443
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19444
              observer(closeButton, "click", function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19445
                uninit();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19446
              })
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19447
            ];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19448
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19449
          uninit = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19450
            // remove observers
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19451
            var i = observers.length;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19452
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19453
            while (i--) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19454
              unobserve.apply(tinylogLite, observers[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19455
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19456
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19457
            // remove tinylog lite from the DOM
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19458
            docElem.removeChild(container);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19459
            docElemStyle.paddingBottom = originalPadding;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19460
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19461
            clearChildren(output);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19462
            clearChildren(container);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19463
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19464
            tinylogLite[log] = createLog;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19465
          };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19466
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19467
          setStyles(
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19468
          container, containerStyles, output, outputStyles, resizer, resizerStyles, closeButton, closeButtonStyles);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19469
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19470
          closeButton[$title] = "Close Log";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19471
          append(closeButton, createTextNode("\u2716"));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19472
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19473
          resizer[$title] = "Double-click to toggle log minimization";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19474
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19475
          docElem.insertBefore(container, docElem.firstChild);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19476
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19477
          tinylogLite[log] = function(message) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19478
            if (messages === logLimit) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19479
              output.removeChild(output.firstChild);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19480
            } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19481
              messages++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19482
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19483
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19484
            var entry = append(output, createElement($div)),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19485
              entryText = append(entry, createElement($div));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19486
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19487
            entry[$title] = (new Date()).toLocaleTimeString();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19488
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19489
            setStyles(
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19490
            entry, entryStyles, entryText, entryTextStyles);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19491
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19492
            append(entryText, createTextNode(message));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19493
            output.scrollTop = output.scrollHeight;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19494
          };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19495
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19496
          tinylogLite[log](message);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19497
          updateSafetyMargin();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19498
        };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19499
      }());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19500
    } else if (typeof print === func) { // JS shell
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19501
      tinylogLite[log] = print;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19502
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19503
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19504
    return tinylogLite;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19505
  }());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19506
  // end of tinylog lite JavaScript library
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19507
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19508
  Processing.logger = tinylogLite;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19509
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19510
  Processing.version = "@VERSION@";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19511
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19512
  // Share lib space
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19513
  Processing.lib = {};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19514
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19515
  Processing.registerLibrary = function(name, desc) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19516
    Processing.lib[name] = desc;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19517
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19518
    if(desc.hasOwnProperty("init")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19519
      desc.init(defaultScope);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19520
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19521
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19522
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19523
  // Store Processing instances. Only Processing.instances,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19524
  // Processing.getInstanceById are exposed.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19525
  Processing.instances = processingInstances;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19526
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19527
  Processing.getInstanceById = function(name) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19528
    return processingInstances[processingInstanceIds[name]];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19529
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19530
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19531
  Processing.Sketch = function(attachFunction) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19532
    this.attachFunction = attachFunction; // can be optional
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19533
    this.options = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19534
      pauseOnBlur: false,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19535
      globalKeyEvents: false
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19536
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19537
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19538
    /* Optional Sketch event hooks:
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19539
     *   onLoad - parsing/preloading is done, before sketch starts
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19540
     *   onSetup - setup() has been called, before first draw()
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19541
     *   onPause - noLoop() has been called, pausing draw loop
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19542
     *   onLoop - loop() has been called, resuming draw loop
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19543
     *   onFrameStart - draw() loop about to begin
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19544
     *   onFrameEnd - draw() loop finished
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19545
     *   onExit - exit() done being called
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19546
     */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19547
    this.onLoad = nop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19548
    this.onSetup = nop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19549
    this.onPause = nop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19550
    this.onLoop = nop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19551
    this.onFrameStart = nop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19552
    this.onFrameEnd = nop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19553
    this.onExit = nop;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19554
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19555
    this.params = {};
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19556
    this.imageCache = {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19557
      pending: 0,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19558
      images: {},
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19559
      // Opera requires special administration for preloading
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19560
      operaCache: {},
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19561
      // Specify an optional img arg if the image is already loaded in the DOM,
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19562
      // otherwise href will get loaded.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19563
      add: function(href, img) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19564
        // Prevent muliple loads for an image, in case it gets
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19565
        // preloaded more than once, or is added via JS and then preloaded.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19566
        if (this.images[href]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19567
          return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19568
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19569
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19570
        if (!isDOMPresent) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19571
          this.images[href] = null;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19572
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19573
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19574
        // No image in the DOM, kick-off a background load
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19575
        if (!img) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19576
          img = new Image();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19577
          img.onload = (function(owner) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19578
            return function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19579
              owner.pending--;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19580
            };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19581
          }(this));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19582
          this.pending++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19583
          img.src = href;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19584
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19585
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19586
        this.images[href] = img;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19587
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19588
        // Opera will not load images until they are inserted into the DOM.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19589
        if (window.opera) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19590
          var div = document.createElement("div");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19591
          div.appendChild(img);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19592
          // we can't use "display: none", since that makes it invisible, and thus not load
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19593
          div.style.position = "absolute";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19594
          div.style.opacity = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19595
          div.style.width = "1px";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19596
          div.style.height= "1px";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19597
          if (!this.operaCache[href]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19598
            document.body.appendChild(div);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19599
            this.operaCache[href] = div;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19600
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19601
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19602
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19603
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19604
    this.sourceCode = undefined;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19605
    this.attach = function(processing) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19606
      // either attachFunction or sourceCode must be present on attach
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19607
      if(typeof this.attachFunction === "function") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19608
        this.attachFunction(processing);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19609
      } else if(this.sourceCode) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19610
        var func = ((new Function("return (" + this.sourceCode + ");"))());
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19611
        func(processing);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19612
        this.attachFunction = func;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19613
      } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19614
        throw "Unable to attach sketch to the processing instance";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19615
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19616
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19617
//#if PARSER
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19618
    this.toString = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19619
      var i;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19620
      var code = "((function(Sketch) {\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19621
      code += "var sketch = new Sketch(\n" + this.sourceCode + ");\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19622
      for(i in this.options) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19623
        if(this.options.hasOwnProperty(i)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19624
          var value = this.options[i];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19625
          code += "sketch.options." + i + " = " +
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19626
            (typeof value === 'string' ? '\"' + value + '\"' : "" + value) + ";\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19627
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19628
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19629
      for(i in this.imageCache) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19630
        if(this.options.hasOwnProperty(i)) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19631
          code += "sketch.imageCache.add(\"" + i + "\");\n";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19632
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19633
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19634
      // TODO serialize fonts
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19635
      code += "return sketch;\n})(Processing.Sketch))";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19636
      return code;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19637
    };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19638
//#endif
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19639
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19640
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19641
//#if PARSER
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19642
  /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19643
   * aggregate all source code into a single file, then rewrite that
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19644
   * source and bind to canvas via new Processing(canvas, sourcestring).
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19645
   * @param {CANVAS} canvas The html canvas element to bind to
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19646
   * @param {String[]} source The array of files that must be loaded
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19647
   */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19648
  var loadSketchFromSources = function(canvas, sources) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19649
    var code = [], errors = [], sourcesCount = sources.length, loaded = 0;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19650
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19651
    function ajaxAsync(url, callback) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19652
      var xhr = new XMLHttpRequest();
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19653
      xhr.onreadystatechange = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19654
        if (xhr.readyState === 4) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19655
          var error;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19656
          if (xhr.status !== 200 && xhr.status !== 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19657
            error = "Invalid XHR status " + xhr.status;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19658
          } else if (xhr.responseText === "") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19659
            // Give a hint when loading fails due to same-origin issues on file:/// urls
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19660
            if ( ("withCredentials" in new XMLHttpRequest()) &&
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19661
                 (new XMLHttpRequest()).withCredentials === false &&
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19662
                 window.location.protocol === "file:" ) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19663
              error = "XMLHttpRequest failure, possibly due to a same-origin policy violation. You can try loading this page in another browser, or load it from http://localhost using a local webserver. See the Processing.js README for a more detailed explanation of this problem and solutions.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19664
            } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19665
              error = "File is empty.";
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19666
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19667
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19668
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19669
          callback(xhr.responseText, error);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19670
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19671
      };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19672
      xhr.open("GET", url, true);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19673
      if (xhr.overrideMimeType) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19674
        xhr.overrideMimeType("application/json");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19675
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19676
      xhr.setRequestHeader("If-Modified-Since", "Fri, 01 Jan 1960 00:00:00 GMT"); // no cache
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19677
      xhr.send(null);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19678
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19679
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19680
    function loadBlock(index, filename) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19681
      function callback(block, error) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19682
        code[index] = block;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19683
        ++loaded;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19684
        if (error) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19685
          errors.push(filename + " ==> " + error);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19686
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19687
        if (loaded === sourcesCount) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19688
          if (errors.length === 0) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19689
            try {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19690
              return new Processing(canvas, code.join("\n"));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19691
            } catch(e) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19692
              throw "Processing.js: Unable to execute pjs sketch: " + e;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19693
            }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19694
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19695
            throw "Processing.js: Unable to load pjs sketch files: " + errors.join("\n");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19696
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19697
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19698
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19699
      if (filename.charAt(0) === '#') {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19700
        // trying to get script from the element
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19701
        var scriptElement = document.getElementById(filename.substring(1));
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19702
        if (scriptElement) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19703
          callback(scriptElement.text || scriptElement.textContent);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19704
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19705
          callback("", "Unable to load pjs sketch: element with id \'" + filename.substring(1) + "\' was not found");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19706
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19707
        return;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19708
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19709
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19710
      ajaxAsync(filename, callback);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19711
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19712
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19713
    for (var i = 0; i < sourcesCount; ++i) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19714
      loadBlock(i, sources[i]);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19715
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19716
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19717
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19718
  /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19719
   * Automatic initialization function.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19720
   */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19721
  var init = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19722
    document.removeEventListener('DOMContentLoaded', init, false);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19723
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19724
    var canvas = document.getElementsByTagName('canvas'),
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19725
      filenames;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19726
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19727
    for (var i = 0, l = canvas.length; i < l; i++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19728
      // datasrc and data-src are deprecated.
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19729
      var processingSources = canvas[i].getAttribute('data-processing-sources');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19730
      if (processingSources === null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19731
        // Temporary fallback for datasrc and data-src
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19732
        processingSources = canvas[i].getAttribute('data-src');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19733
        if (processingSources === null) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19734
          processingSources = canvas[i].getAttribute('datasrc');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19735
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19736
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19737
      if (processingSources) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19738
        filenames = processingSources.split(' ');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19739
        for (var j = 0; j < filenames.length;) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19740
          if (filenames[j]) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19741
            j++;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19742
          } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19743
            filenames.splice(j, 1);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19744
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19745
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19746
        loadSketchFromSources(canvas[i], filenames);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19747
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19748
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19749
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19750
    // also process all <script>-indicated sketches, if there are any
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19751
    var scripts = document.getElementsByTagName('script');
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19752
    var s, source, instance;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19753
    for (s = 0; s < scripts.length; s++) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19754
      var script = scripts[s];
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19755
      if (!script.getAttribute) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19756
        continue;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19757
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19758
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19759
      var type = script.getAttribute("type");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19760
      if (type && (type.toLowerCase() === "text/processing" || type.toLowerCase() === "application/processing")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19761
        var target = script.getAttribute("data-processing-target");
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19762
        canvas = undef;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19763
        if (target) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19764
          canvas = document.getElementById(target);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19765
        } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19766
          var nextSibling = script.nextSibling;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19767
          while (nextSibling && nextSibling.nodeType !== 1) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19768
            nextSibling = nextSibling.nextSibling;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19769
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19770
          if (nextSibling.nodeName.toLowerCase() === "canvas") {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19771
            canvas = nextSibling;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19772
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19773
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19774
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19775
        if (canvas) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19776
          if (script.getAttribute("src")) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19777
            filenames = script.getAttribute("src").split(/\s+/);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19778
            loadSketchFromSources(canvas, filenames);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19779
            continue;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19780
          }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19781
          source =  script.textContent || script.text;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19782
          instance = new Processing(canvas, source);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19783
        }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19784
      }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19785
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19786
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19787
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19788
  /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19789
   * Make loadSketchFromSources publically visible
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19790
   */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19791
  Processing.loadSketchFromSources = loadSketchFromSources;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19792
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19793
  /**
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19794
   * Disable the automatic loading of all sketches on the page
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19795
   */
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19796
  Processing.disableInit = function() {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19797
    if(isDOMPresent) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19798
      document.removeEventListener('DOMContentLoaded', init, false);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19799
    }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19800
  };
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19801
//#endif
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19802
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19803
  if(isDOMPresent) {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19804
    window['Processing'] = Processing;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19805
//#if PARSER
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19806
    document.addEventListener('DOMContentLoaded', init, false);
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19807
//#endif
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19808
  } else {
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19809
    // DOM is not found
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19810
    this.Processing = Processing;
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19811
  }
858e90c7cbaa Front IDILL :
bastiena
parents:
diff changeset
 19812
}(window, window.document, Math));