~holger-seelig/cobweb.js/trunk

« back to all changes in this revision

Viewing changes to src/cobweb/Browser/Core/X3DCoreContext.js

  • Committer: Holger Seelig
  • Date: 2017-08-22 04:53:24 UTC
  • Revision ID: holger.seelig@yahoo.de-20170822045324-4of4xxgt79669gbt
Switched to npm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: JavaScript; coding: utf-8; tab-width: 3; indent-tabs-mode: tab; c-basic-offset: 3 -*-
 
2
 *******************************************************************************
 
3
 *
 
4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
5
 *
 
6
 * Copyright create3000, Scheffelstraße 31a, Leipzig, Germany 2011.
 
7
 *
 
8
 * All rights reserved. Holger Seelig <holger.seelig@yahoo.de>.
 
9
 *
 
10
 * The copyright notice above does not evidence any actual of intended
 
11
 * publication of such source code, and is an unpublished work by create3000.
 
12
 * This material contains CONFIDENTIAL INFORMATION that is the property of
 
13
 * create3000.
 
14
 *
 
15
 * No permission is granted to copy, distribute, or create derivative works from
 
16
 * the contents of this software, in whole or in part, without the prior written
 
17
 * permission of create3000.
 
18
 *
 
19
 * NON-MILITARY USE ONLY
 
20
 *
 
21
 * All create3000 software are effectively free software with a non-military use
 
22
 * restriction. It is free. Well commented source is provided. You may reuse the
 
23
 * source in any way you please with the exception anything that uses it must be
 
24
 * marked to indicate is contains 'non-military use only' components.
 
25
 *
 
26
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
27
 *
 
28
 * Copyright 2015, 2016 Holger Seelig <holger.seelig@yahoo.de>.
 
29
 *
 
30
 * This file is part of the Cobweb Project.
 
31
 *
 
32
 * Cobweb is free software: you can redistribute it and/or modify it under the
 
33
 * terms of the GNU General Public License version 3 only, as published by the
 
34
 * Free Software Foundation.
 
35
 *
 
36
 * Cobweb is distributed in the hope that it will be useful, but WITHOUT ANY
 
37
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 
38
 * A PARTICULAR PURPOSE. See the GNU General Public License version 3 for more
 
39
 * details (a copy is included in the LICENSE file that accompanied this code).
 
40
 *
 
41
 * You should have received a copy of the GNU General Public License version 3
 
42
 * along with Cobweb.  If not, see <http://www.gnu.org/licenses/gpl.html> for a
 
43
 * copy of the GPLv3 License.
 
44
 *
 
45
 * For Silvio, Joy and Adi.
 
46
 *
 
47
 ******************************************************************************/
 
48
 
 
49
 
 
50
define ([
 
51
        "cobweb/Fields",
 
52
        "cobweb/Browser/Core/BrowserOptions",
 
53
        "cobweb/Browser/Core/BrowserProperties",
 
54
        "cobweb/Browser/Core/RenderingProperties",
 
55
        "cobweb/Browser/Core/Notification",
 
56
        "cobweb/Browser/Core/BrowserTimings",
 
57
        "cobweb/Browser/Core/ContextMenu",
 
58
        "cobweb/Execution/Scene",
 
59
        "cobweb/Parser/Parser",
 
60
        "lib/DataStorage",
 
61
],
 
62
function (Fields,
 
63
          BrowserOptions,
 
64
          BrowserProperties,
 
65
          RenderingProperties,
 
66
          Notification,
 
67
          BrowserTimings,
 
68
          ContextMenu,
 
69
          Scene,
 
70
          Parser,
 
71
          DataStorage)
 
72
{
 
73
"use strict";
 
74
        
 
75
        var browserNumber = 0;
 
76
 
 
77
        function getContext (canvas)
 
78
        {
 
79
                var gl = canvas .getContext ("webgl") ||
 
80
                         canvas .getContext ("experimental-webgl");
 
81
 
 
82
                if (! gl)
 
83
                        throw new Error ("Couldn't create WebGL context.");
 
84
 
 
85
                // Feature detection:
 
86
                
 
87
                // If the aliased linewidth ranges are both 1, gl.lineWidth is probably not possible,
 
88
                // thus we disable it completely to prevent webgl errors.
 
89
                
 
90
                var aliasedLineWidthRange = gl .getParameter (gl .ALIASED_LINE_WIDTH_RANGE);
 
91
                
 
92
                if (aliasedLineWidthRange [0] === 1 && aliasedLineWidthRange [1] === 1)
 
93
                {
 
94
                        gl .lineWidth = Function .prototype;
 
95
                }
 
96
                
 
97
                // Return context.
 
98
                
 
99
                return gl;
 
100
        }
 
101
 
 
102
        function X3DCoreContext (element)
 
103
        {
 
104
                this .number  = ++ browserNumber;
 
105
                this .element = element;
 
106
 
 
107
                // Get canvas & context.
 
108
 
 
109
                var browser      = $("<div></div>") .addClass ("cobweb-browser")  .prependTo (this .element);
 
110
                var splashScreen = $("<div></div>") .addClass ("cobweb-splash-screen") .appendTo (browser);
 
111
                var spinner      = $("<div></div>") .addClass ("cobweb-spinner")  .appendTo (splashScreen);
 
112
                var progress     = $("<div></div>") .addClass ("cobweb-progress") .appendTo (splashScreen);
 
113
                var surface      = $("<div></div>") .addClass ("cobweb-surface cobweb-surface-" + this .getId ()) .appendTo (browser);
 
114
 
 
115
                $("<div></div>") .addClass ("cobweb-spinner-one")   .appendTo (spinner);
 
116
                $("<div></div>") .addClass ("cobweb-spinner-two")   .appendTo (spinner);
 
117
                $("<div></div>") .addClass ("cobweb-spinner-three") .appendTo (spinner);
 
118
                $("<div></div>") .addClass ("cobweb-spinner-text")  .appendTo (progress) .text ("Lade 0 Dateien");
 
119
                $("<div></div>") .addClass ("cobweb-progressbar")   .appendTo (progress) .append ($("<div></div>"));
 
120
 
 
121
                this .splashScreen = splashScreen;
 
122
                this .canvas       = $("<canvas></canvas>") .prependTo (surface);
 
123
                this .context      = getContext (this .canvas [0]);
 
124
 
 
125
                this .privateScene = new Scene (this); // Scene for default nodes.
 
126
 
 
127
                this .browserOptions      = new BrowserOptions      (this .getPrivateScene ());
 
128
                this .browserProperties   = new BrowserProperties   (this .getPrivateScene ());
 
129
                this .renderingProperties = new RenderingProperties (this .getPrivateScene ());
 
130
                this .notification        = new Notification        (this .getPrivateScene ());
 
131
                this .browserTimings      = new BrowserTimings      (this .getPrivateScene ());
 
132
                this .contextMenu         = new ContextMenu         (this .getPrivateScene ());
 
133
 
 
134
                this .dataStorage = new DataStorage ("X3DBrowser(" + this .number + ").");
 
135
                this .mobile      = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i .test (navigator .userAgent);
 
136
 
 
137
                this .getCanvas () .fadeOut (0);
 
138
 
 
139
                if (this .getBrowserOptions () .getSplashScreen ())
 
140
                        this .getSplashScreen () .fadeIn (0);
 
141
 
 
142
                $(".cobweb-console") .empty ();
 
143
        }
 
144
 
 
145
        X3DCoreContext .prototype =
 
146
        {
 
147
                initialize: function ()
 
148
                {
 
149
                        // Scene for default nodes.
 
150
 
 
151
                        this .privateScene .setPrivate (true);
 
152
                        this .privateScene .setLive (true);
 
153
                        this .privateScene .setup ();
 
154
 
 
155
                        // Setup browser nodes.
 
156
 
 
157
                        this .browserOptions      .setup ()
 
158
                        this .browserProperties   .setup ()
 
159
                        this .renderingProperties .setup ();
 
160
                        this .notification        .setup ();
 
161
                        this .browserTimings      .setup ();
 
162
                        this .contextMenu         .setup ();
 
163
 
 
164
                        // Observe Element's attributes.
 
165
 
 
166
                        this .observer = new MutationObserver (this .processMutations .bind (this));
 
167
 
 
168
                        this .observer .observe (this .element [0], { attributes: true, childList: false, characterData: false, subtree: false });
 
169
                },
 
170
                getNumber: function ()
 
171
                {
 
172
                        return this .number;
 
173
                },
 
174
                isStrict: function ()
 
175
                {
 
176
                        return false;
 
177
                },
 
178
                getElement: function ()
 
179
                {
 
180
                        return this .element;
 
181
                },
 
182
                getSplashScreen: function ()
 
183
                {
 
184
                        return this .splashScreen;
 
185
                },
 
186
                getCanvas: function ()
 
187
                {
 
188
                        return this .canvas;
 
189
                },
 
190
                getContext: function ()
 
191
                {
 
192
                        return this .context;
 
193
                },
 
194
                getBrowserOptions: function ()
 
195
                {
 
196
                        return this .browserOptions;
 
197
                },
 
198
                getBrowserProperties: function ()
 
199
                {
 
200
                        return this .browserProperties;
 
201
                },
 
202
                getRenderingProperties: function ()
 
203
                {
 
204
                        return this .renderingProperties;
 
205
                },
 
206
                getNotification: function ()
 
207
                {
 
208
                        return this .notification;
 
209
                },
 
210
                getBrowserTimings: function ()
 
211
                {
 
212
                        return this .browserTimings;
 
213
                },
 
214
                getDataStorage: function ()
 
215
                {
 
216
                        return this .dataStorage;
 
217
                },
 
218
                getMobile: function ()
 
219
                {
 
220
                        return this .mobile;
 
221
                },
 
222
                processMutations: function (mutations)
 
223
                {
 
224
                        mutations .forEach (function (mutation)
 
225
                        {
 
226
                                this .processMutation (mutation);
 
227
                        },
 
228
                        this);
 
229
                },
 
230
                processMutation: function (mutation)
 
231
                {
 
232
                        var element = mutation .target;
 
233
                        
 
234
                        switch (mutation .type)
 
235
                        {
 
236
                                case "attributes":
 
237
                                {
 
238
                                        this .processAttribute (mutation, element);
 
239
                                        break;
 
240
                                }
 
241
                        }
 
242
                },
 
243
                processAttribute: function (mutation, element)
 
244
                {
 
245
                        var attributeName = mutation .attributeName;
 
246
 
 
247
                        switch (attributeName .toLowerCase())
 
248
                        {
 
249
                                case "src":
 
250
                                        var urlCharacters = this .getElement () .attr ("src");
 
251
                
 
252
                                        if (urlCharacters)
 
253
                                                this .load ('"' + urlCharacters + '"');
 
254
 
 
255
                                        break;
 
256
                                case "url":
 
257
                                        this .load (this .getElement () .attr ("url"));
 
258
                                        break;
 
259
                                case "splashscreen":
 
260
                                        this .getBrowserOptions () .setAttributeSplashScreen ();
 
261
                                        break;
 
262
                        }
 
263
                },
 
264
                load: function (urlCharacters)
 
265
                {
 
266
                        if (urlCharacters)
 
267
                        {
 
268
                           var
 
269
                              parser    = new Parser (this .getExecutionContext (), true),
 
270
                              url       = new Fields .MFString (),
 
271
                                        parameter = new Fields .MFString ();
 
272
 
 
273
                                parser .setInput (urlCharacters);
 
274
                                parser .sfstringValues (url);
 
275
 
 
276
                                if (url .length)
 
277
                                        this .loadURL (url, parameter);
 
278
                        }
 
279
                        else
 
280
                        {
 
281
                                if (! this .getLoading ())
 
282
                                        this .getCanvas () .fadeIn (0);
 
283
                        }
 
284
                },
 
285
                getPrivateScene: function ()
 
286
                {
 
287
                        return this .privateScene;
 
288
                },
 
289
        };
 
290
 
 
291
        return X3DCoreContext;
 
292
});