PageSourceSearch

https://renderdiagrams.org/lib/old_v1/scene.js?v=1.1

js renderdiagrams.org collected 2026-10-08 02:13:40 UTC 6,129 bytes, 314 lines download raw bytes

1function Scene()
2{
3	this.objects			= [];
4	this.onChangeListeners	= [];
5	this.boundsMin			= new Vector(100000,100000);
6	this.boundsMax			= new Vector(-100000,-100000);
7	this.name				= "Unnamed Scene";
8
9	this.getBoundsMin = function()
10	{
11		return this.boundsMin;
12	}
13
14	this.getBoundsMax = function()
15	{
16		return this.boundsMax;
17	}
18
19	this.addChangeListener = function(listener)
20	{
21		this.onChangeListeners.push(listener);
22	}
23
24	this.onChange = function()
25	{
26		this.boundsMin = new Vector(100000,100000);
27		this.boundsMax = new Vector(-100000,-100000);
28
29		for (var i=0; i<this.objects.length; ++i)
30		{
31			if (this.objects[i].getBoundsMin !== undefined)
32			{
33				this.boundsMin = min(this.boundsMin, this.objects[i].getBoundsMin());
34				this.boundsMax = max(this.boundsMax, this.objects[i].getBoundsMax());
35			}
36		}
37
38		for (var i=0; i!=this.onChangeListeners.length; ++i)
39		{
40			this.onChangeListeners[i](this);
41		}
42	}
43
44	this.addObject = function (object, index)
45	{
46		if (index == undefined)
47		{
48			this.objects.push(object);
49		}
50		else
51		{
52			this.objects.splice(index, 0, object);
53		}
54
55		if (object.scene !== null && object.scene !== this)
56		{
57			object.scene.deleteObjects([object]);
58		}
59
60		object.scene = this;
61
62		if (object.addChangeListener !== undefined)
63		{
64			object.addChangeListener(this.onChange.bind(this));
65		}
66
67		if (object.onChange !== undefined)
68		{
69			object.onChange();
70		}
71
72		this.onChange();
73	}
74	
75	this.deleteObjects = function(objectList)
76	{
77		for (var i=0; i<this.objects.length; ++i)
78		{
79			if (objectList.indexOf(this.objects[i])>=0)
80			{
81				if (this.objects[i].scene !== null)
82				{
83					this.objects[i].scene = null;
84				}
85
86				this.objects.splice(i, 1);
87				--i;
88			}
89		}
90
91		this.onChange();
92	}
93
94	this.deleteAllObjects = function()
95	{
96		this.objects = [];
97		this.onChange();
98	}
99
100	this.getObjectIndex = function (object)
101	{
102		return this.objects.indexOf(object);
103	}
104
105	this.setObjectIndex = function (object, index, relative)
106	{
107		var objectIndex = this.objects.indexOf(object);
108
109		if (objectIndex<0)
110			return;
111
112		var newIndex = index;
113
114		if (relative == true)
115			newIndex = objectIndex + index;
116
117		this.objects.splice(objectIndex, 1);
118		this.objects.splice(newIndex, 0, object);
119
120		this.onChange();
121	}
122
123	this.draw = function (camera)
124	{
125		for (var i=0; i<this.objects.length; ++i)
126		{
127			if (this.objects[i].isVisible != undefined && this.objects[i].isVisible())
128			{
129				this.objects[i].draw(camera);
130			}
131		}
132	}
133	
134	this.hitTest = function(a, b)
135	{
136		var results = [];
137		
138		for (var i=0; i<this.objects.length; ++i)
139		{
140			var temp = this.objects[i].hitTest;
141			
142			if (this.objects[i].hitTest !== undefined && this.objects[i].hitTest(a,b) == true && !this.objects[i].isFrozen()&& this.objects[i].isVisible())
143			{
144				results.push(this.objects[i]);
145			}
146		}
147		
148		return results;
149	}
150
151	this.rayHitTest = function(rayPos, rayDir)
152	{
153		var bestHit = {tRay:1000};
154			
155		for (var o=0; o<this.objects.length; ++o)
156		{
157			if (this.objects[o].rayHitTest === undefined)
158				continue;
159
160			var hits = this.objects[o].rayHitTest(rayPos, rayDir);
161				
162			for (var h = 0; h != hits.length; ++h)
163			{
164				if (hits[h].hit && hits[h].tRay<bestHit.tRay)
165				{
166					bestHit = hits[h];
167				}
168			}
169		}
170
171		return bestHit;
172	}
173
174	this.getSnapPoint = function(mousePos, extraSnapPoints, threshold, ignoreObjectList, enabledTypes)
175	{
176		var bestPoint = null;
177		var bestDistance = 1000;
178
179		if (ignoreObjectList == undefined)
180			ignoreObjectList = [];
181
182		for (var i=0; i<this.objects.length; ++i)
183		{
184			if (this.objects[i].getSnapPoints === undefined)
185				continue;
186
187			if (!this.objects[i].isVisible())
188				continue;
189
190			if (ignoreObjectList.indexOf(this.objects[i]) >= 0)
191				continue;
192
193			var points = this.objects[i].getSnapPoints();
194
195			if (points.length == 0)
196				continue;
197
198			for (var p=0; p<points.length; ++p)
199			{
200				if (enabledTypes[points[p].type] === false)
201					continue;
202
203				var d = length(sub(points[p].p, mousePos));
204
205				if (d<threshold && d<bestDistance)
206				{
207					bestDistance = d;
208					bestPoint = {type: points[p].type, p:points[p].p, N:points[p].N, object: this.objects[i]};
209				}
210			}
211		}
212
213		for (var p=0; p<extraSnapPoints.length; ++p)
214		{
215			if (enabledTypes[extraSnapPoints[p].type] === false)
216				continue;
217
218			var skip = false;
219
220			for (var o = 0; o < extraSnapPoints[p].objects.length; ++o)
221			{
222				if (ignoreObjectList.indexOf(extraSnapPoints[p].objects[o]) >= 0)
223				{
224					skip = true;
225					break;
226				}
227			}
228
229			if (skip)
230				continue;
231
232			var d;
233
234			if (extraSnapPoints[p].testP !== undefined)
235			{
236				d = length(sub(extraSnapPoints[p].testP, mousePos));
237			}
238			else
239			{
240				d = length(sub(extraSnapPoints[p].p, mousePos));
241			}
242
243			if (d<threshold && d<bestDistance)
244			{
245				bestDistance = d;
246				bestPoint = extraSnapPoints[p];//{type: extraSnapPoints[p].type, p:extraSnapPoints[p].p, N:extraSnapPoints[p].N};
247			}
248		}
249
250		return bestPoint;
251	}
252
253	this.getDragPoint = function(mousePos, camera, localSpace, ignoreObjectList)
254	{
255		var bestPoint = null;
256		var bestObject = null;
257		var bestIndex = null;
258		var bestDistance = 1000;
259
260		var threshold = camera.invScale(30);
261
262		if (ignoreObjectList == undefined)
263			ignoreObjectList = [];
264
265		for (var i=0; i<this.objects.length; ++i)
266		{
267			if (this.objects[i].getDragPoints === undefined)
268				continue;
269
270			if (!this.objects[i].isVisible())
271				continue;
272
273			if (ignoreObjectList.indexOf(this.objects[i]) >= 0)
274				continue;
275
276			var points = this.objects[i].getDragPoints(localSpace, camera);
277
278			if (points.length == 0)
279				continue;
280
281			for (var p=0; p<points.length; ++p)
282			{
283				var d = length(sub(points[p], mousePos));
284
285				if (d<threshold && d<bestDistance)
286				{
287					bestDistance = d;
288					bestPoint = points[p].copy();
289					bestObject = this.objects[i];
290					bestIndex = p;
291				}
292			}
293		}
294
295		return {object: bestObject, index:bestIndex, point: bestPoint, local: localSpace };
296	}
297
298	this.saveAsJavascript = function()
299	{
300		str = "";
301
302		for (var i=0; i<this.objects.length; ++i)
303		{
304			if (this.objects[i].saveAsJavascript === undefined)
305				continue;
306
307			str += this.objects[i].saveAsJavascript();
308
309			str += "\n";
310		}
311
312		return str;
313	}
314}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.