diff options
author | Valerio Virgillito | 2012-05-07 17:03:30 -0700 |
---|---|---|
committer | Valerio Virgillito | 2012-05-07 17:03:30 -0700 |
commit | 4d949f141247215b5f2a6ec0cfc7d2d31cf2bb1f (patch) | |
tree | bade422fe2471a18944e846cfec1dd188a6cdae3 /js/document/models/html.js | |
parent | a117cbed5df85c15a42776d3381904eff12e751f (diff) | |
parent | 95d23ae18efb9b314517cae07212ab840f284c3c (diff) | |
download | ninja-4d949f141247215b5f2a6ec0cfc7d2d31cf2bb1f.tar.gz |
Merge pull request #199 from joseeight/Document
Cleaning up webGL
Diffstat (limited to 'js/document/models/html.js')
-rwxr-xr-x | js/document/models/html.js | 196 |
1 files changed, 6 insertions, 190 deletions
diff --git a/js/document/models/html.js b/js/document/models/html.js index e0a18850..1639a8e2 100755 --- a/js/document/models/html.js +++ b/js/document/models/html.js | |||
@@ -6,11 +6,9 @@ No rights, expressed or implied, whatsoever to this software are provided by Mot | |||
6 | 6 | ||
7 | //////////////////////////////////////////////////////////////////////// | 7 | //////////////////////////////////////////////////////////////////////// |
8 | // | 8 | // |
9 | var Montage = require("montage/core/core").Montage, | 9 | var Montage = require("montage/core/core").Montage, |
10 | BaseDocumentModel = require("js/document/models/base").BaseDocumentModel, | 10 | BaseDocumentModel = require("js/document/models/base").BaseDocumentModel, |
11 | MaterialsModel = require("js/models/materials-model").MaterialsModel, | 11 | webGlDocumentHelper = require("js/document/helpers/webgl-helper").webGlDocumentHelper; |
12 | NJUtils = require("js/lib/NJUtils").NJUtils, | ||
13 | GLWorld = require("js/lib/drawing/world").World; | ||
14 | //////////////////////////////////////////////////////////////////////// | 12 | //////////////////////////////////////////////////////////////////////// |
15 | // | 13 | // |
16 | exports.HtmlDocumentModel = Montage.create(BaseDocumentModel, { | 14 | exports.HtmlDocumentModel = Montage.create(BaseDocumentModel, { |
@@ -26,191 +24,9 @@ exports.HtmlDocumentModel = Montage.create(BaseDocumentModel, { | |||
26 | }, | 24 | }, |
27 | //////////////////////////////////////////////////////////////////// | 25 | //////////////////////////////////////////////////////////////////// |
28 | // | 26 | // |
29 | _glData: { | 27 | webGlHelper: { |
30 | value: null | 28 | value: webGlDocumentHelper |
31 | }, | 29 | } |
32 | //////////////////////////////////////////////////////////////////// | ||
33 | // | ||
34 | glData: { | ||
35 | // | ||
36 | get: function() { | ||
37 | // | ||
38 | var elt = this.views.design.iframe.contentWindow.document.body; | ||
39 | // | ||
40 | if (elt) { | ||
41 | var matLib = MaterialsModel.exportMaterials(); | ||
42 | this._glData = [matLib]; | ||
43 | this.collectGLData(elt, this._glData ); | ||
44 | } else { | ||
45 | this._glData = null | ||
46 | } | ||
47 | // | ||
48 | return this._glData; | ||
49 | }, | ||
50 | // | ||
51 | set: function(value) { | ||
52 | // | ||
53 | var elt = this.views.design.iframe.contentWindow.document.body; | ||
54 | // | ||
55 | if (elt) { | ||
56 | /* | ||
57 | // Use this code to test the runtime version of WebGL | ||
58 | var cdm = new NinjaCvsRt.CanvasDataManager(); | ||
59 | cdm.loadGLData(elt, value, null ); | ||
60 | */ | ||
61 | |||
62 | // | ||
63 | var i, nWorlds= value.length; | ||
64 | // | ||
65 | for (i = 0; i < nWorlds; i++) { | ||
66 | // get the data for the next canvas | ||
67 | var importStr = value[i], id, jObj, index = importStr.indexOf(';'), matLibStr, matLibObj, startIndex, endIndex, canvas, useWebGL, world; | ||
68 | // determine if it is the new (JSON) or old style format | ||
69 | if ((importStr[0] === 'v') && (index < 24)) { | ||
70 | // JSON format. pull off the | ||
71 | importStr = importStr.substr(index+1); | ||
72 | jObj = JSON.parse(importStr); | ||
73 | id = jObj.id; | ||
74 | } else { | ||
75 | // at this point the data could be either the materials library or | ||
76 | // an old style world. We can determine which by converting the string | ||
77 | // to an object via JSON.parse. That operation will fail if the string | ||
78 | // is an old style world. | ||
79 | matLibStr = 'materialLibrary;'; | ||
80 | index = importStr.indexOf(matLibStr); | ||
81 | if (index == 0) { | ||
82 | importStr = importStr.substr(matLibStr.length); | ||
83 | matLibObj = JSON.parse(importStr); | ||
84 | MaterialsModel.importMaterials(matLibObj); | ||
85 | } else { | ||
86 | startIndex = importStr.indexOf("id: "); | ||
87 | if (startIndex >= 0) { | ||
88 | endIndex = importStr.indexOf("\n", startIndex); | ||
89 | if (endIndex > 0) id = importStr.substring(startIndex+4, endIndex); | ||
90 | } | ||
91 | } | ||
92 | } | ||
93 | // | ||
94 | if (id != null) { | ||
95 | // | ||
96 | canvas = this.findCanvasWithID(id, elt); | ||
97 | // | ||
98 | if (canvas) { | ||
99 | // | ||
100 | if (!canvas.elementModel) { | ||
101 | NJUtils.makeElementModel(canvas, "Canvas", "shape", true); | ||
102 | } | ||
103 | // | ||
104 | if (canvas.elementModel) { | ||
105 | if (canvas.elementModel.shapeModel.GLWorld) { | ||
106 | canvas.elementModel.shapeModel.GLWorld.clearTree(); | ||
107 | } | ||
108 | // | ||
109 | if (jObj) { | ||
110 | useWebGL = jObj.webGL; | ||
111 | world = new GLWorld(canvas, useWebGL); | ||
112 | world.importJSON(jObj); | ||
113 | } | ||
114 | // | ||
115 | this.buildShapeModel(canvas.elementModel, world); | ||
116 | } | ||
117 | } | ||
118 | } | ||
119 | } | ||
120 | } | ||
121 | } | ||
122 | }, | ||
123 | //////////////////////////////////////////////////////////////////// | ||
124 | // | ||
125 | findCanvasWithID: { | ||
126 | value: function(id, elt) { | ||
127 | // | ||
128 | var i, child, nKids, foundElt, cid = elt.getAttribute("data-RDGE-id"); | ||
129 | // | ||
130 | if (cid == id) return elt; | ||
131 | // | ||
132 | if (elt.children) { | ||
133 | nKids = elt.children.length; | ||
134 | for (i=0; i<nKids; i++) { | ||
135 | child = elt.children[i]; | ||
136 | foundElt = this.findCanvasWithID( id, child ); | ||
137 | if (foundElt) return foundElt; | ||
138 | } | ||
139 | } | ||
140 | } | ||
141 | }, | ||
142 | //////////////////////////////////////////////////////////////////// | ||
143 | // | ||
144 | buildShapeModel: { | ||
145 | value: function(elementModel, world) { | ||
146 | // | ||
147 | var shapeModel = elementModel.shapeModel, root; | ||
148 | shapeModel.shapeCount = 1; // for now... | ||
149 | shapeModel.useWebGl = world._useWebGL; | ||
150 | shapeModel.GLWorld = world; | ||
151 | // | ||
152 | root = world.getGeomRoot(); | ||
153 | // | ||
154 | if (root) { | ||
155 | shapeModel.GLGeomObj = root; | ||
156 | shapeModel.strokeSize = root._strokeWidth; | ||
157 | shapeModel.strokeStyle = "solid"; | ||
158 | //shapeModel.strokeStyleIndex | ||
159 | switch (root.geomType()) { | ||
160 | case root.GEOM_TYPE_RECTANGLE: | ||
161 | elementModel.selection = "Rectangle"; | ||
162 | elementModel.pi = "RectanglePi"; | ||
163 | shapeModel.tlRadius = root._tlRadius; | ||
164 | shapeModel.trRadius = root._trRadius; | ||
165 | shapeModel.blRadius = root._blRadius; | ||
166 | shapeModel.brRadius = root._brRadius; | ||
167 | break; | ||
168 | case root.GEOM_TYPE_CIRCLE: | ||
169 | elementModel.selection = "Oval"; | ||
170 | elementModel.pi = "OvalPi"; | ||
171 | shapeModel.innerRadius = root._innerRadius; | ||
172 | break; | ||
173 | case root.GEOM_TYPE_LINE: | ||
174 | elementModel.selection = "Line"; | ||
175 | elementModel.pi = "LinePi"; | ||
176 | shapeModel.slope = root._slope; | ||
177 | break; | ||
178 | case root.GEOM_TYPE_BRUSH_STROKE: | ||
179 | elementModel.selection = "BrushStroke"; | ||
180 | elementModel.pi = "BrushStrokePi"; | ||
181 | break; | ||
182 | case root.GEOM_TYPE_CUBIC_BEZIER: | ||
183 | elementModel.selection = "Subpath"; | ||
184 | elementModel.pi = "SubpathPi"; | ||
185 | break; | ||
186 | default: | ||
187 | console.log("geometry type not supported for file I/O, " + root.geomType()); | ||
188 | break; | ||
189 | } | ||
190 | } | ||
191 | } | ||
192 | }, | ||
193 | //////////////////////////////////////////////////////////////////// | ||
194 | // | ||
195 | collectGLData: { | ||
196 | value: function( elt, dataArray ) { | ||
197 | // | ||
198 | var i, data, nKids, child; | ||
199 | // | ||
200 | if (elt.elementModel && elt.elementModel.shapeModel && elt.elementModel.shapeModel.GLWorld) { | ||
201 | data = elt.elementModel.shapeModel.GLWorld.exportJSON(); | ||
202 | dataArray.push(data); | ||
203 | } | ||
204 | // | ||
205 | if (elt.children) { | ||
206 | nKids = elt.children.length; | ||
207 | for (i=0; i<nKids; i++) { | ||
208 | child = elt.children[i]; | ||
209 | this.collectGLData( child, dataArray ); | ||
210 | } | ||
211 | } | ||
212 | } | ||
213 | } | ||
214 | //////////////////////////////////////////////////////////////////// | 30 | //////////////////////////////////////////////////////////////////// |
215 | //////////////////////////////////////////////////////////////////// | 31 | //////////////////////////////////////////////////////////////////// |
216 | }); |