|
1 /* |
|
2 * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package javax.microedition.m3g; |
|
20 |
|
21 import java.util.Vector; |
|
22 |
|
23 public class Group extends Node |
|
24 { |
|
25 //------------------------------------------------------------------ |
|
26 // Instance data |
|
27 //------------------------------------------------------------------ |
|
28 |
|
29 // The child links are duplicated on the Java side for the same |
|
30 // reason as the other node->node references; see Node.java |
|
31 |
|
32 Vector children; |
|
33 |
|
34 //------------------------------------------------------------------ |
|
35 // Constructors |
|
36 //------------------------------------------------------------------ |
|
37 |
|
38 public Group() |
|
39 { |
|
40 super(_ctor(Interface.getHandle())); |
|
41 } |
|
42 |
|
43 Group(int handle) |
|
44 { |
|
45 super(handle); |
|
46 int n = _getChildCount(handle); |
|
47 while (n-- > 0) |
|
48 { |
|
49 linkChild((Node) getInstance(_getChild(handle, n))); |
|
50 } |
|
51 } |
|
52 |
|
53 //------------------------------------------------------------------ |
|
54 // Public methods |
|
55 //------------------------------------------------------------------ |
|
56 |
|
57 public void addChild(Node child) |
|
58 { |
|
59 _addChild(handle, child != null ? child.handle : 0); |
|
60 if (child != null) |
|
61 { |
|
62 linkChild(child); |
|
63 } |
|
64 } |
|
65 |
|
66 public void removeChild(Node child) |
|
67 { |
|
68 if (child != null) |
|
69 { |
|
70 _removeChild(handle, child.handle); |
|
71 detachChild(child); |
|
72 } |
|
73 } |
|
74 |
|
75 public int getChildCount() |
|
76 { |
|
77 return _getChildCount(handle); |
|
78 } |
|
79 |
|
80 public Node getChild(int index) |
|
81 { |
|
82 |
|
83 /* Instead of trying to match the indexing of children on the |
|
84 * native side, we just call the native getter. This may have |
|
85 * some performance penalty, but likely not enough to make it |
|
86 * worth the extra maintenance burden of duplicating the |
|
87 * native ordering here. */ |
|
88 |
|
89 return (Node) getInstance(_getChild(handle, index)); |
|
90 } |
|
91 |
|
92 public boolean pick(int mask, |
|
93 float ox, float oy, float oz, |
|
94 float dx, float dy, float dz, |
|
95 RayIntersection ri) |
|
96 { |
|
97 float[] result = RayIntersection.createResult(); |
|
98 float[] ray = {ox, oy, oz, dx, dy, dz}; |
|
99 int hIntersected; |
|
100 |
|
101 hIntersected = _pick3D(handle, mask, ray, result); |
|
102 |
|
103 if (hIntersected != 0) |
|
104 { |
|
105 if (ri != null) |
|
106 { |
|
107 ri.fill(hIntersected, result); |
|
108 } |
|
109 return true; |
|
110 } |
|
111 else |
|
112 { |
|
113 return false; |
|
114 } |
|
115 } |
|
116 |
|
117 public boolean pick(int mask, float x, float y, Camera camera, RayIntersection ri) |
|
118 { |
|
119 float[] result = RayIntersection.createResult(); |
|
120 int hIntersected; |
|
121 |
|
122 hIntersected = _pick2D(handle, mask, x, y, camera != null ? camera.handle : 0, result); |
|
123 |
|
124 if (hIntersected != 0) |
|
125 { |
|
126 if (ri != null) |
|
127 { |
|
128 ri.fill(hIntersected, result); |
|
129 } |
|
130 return true; |
|
131 } |
|
132 else |
|
133 { |
|
134 return false; |
|
135 } |
|
136 } |
|
137 |
|
138 //------------------------------------------------------------------ |
|
139 // Private methods |
|
140 //------------------------------------------------------------------ |
|
141 |
|
142 /** |
|
143 * Adds a Java-side child link in this Group. |
|
144 */ |
|
145 private void linkChild(Node child) |
|
146 { |
|
147 if (child == null) |
|
148 { |
|
149 throw new Error(); // DEBUG |
|
150 } |
|
151 if (children == null) |
|
152 { |
|
153 children = new Vector(); |
|
154 } |
|
155 children.addElement(child); |
|
156 child.setParent(this); |
|
157 } |
|
158 |
|
159 /** |
|
160 * Removes a Java-side child link from this Group. |
|
161 */ |
|
162 private void detachChild(Node child) |
|
163 { |
|
164 if (children != null) |
|
165 { |
|
166 if (children.removeElement(child)) |
|
167 { |
|
168 /* If no children remain, we delete the array to free some |
|
169 * memory. If a Group is frequently cleared and |
|
170 * re-populated, this should be covered by the free list |
|
171 * used by most VM implementations without causing |
|
172 * significant performance degradation. */ |
|
173 if (children.isEmpty()) |
|
174 { |
|
175 children = null; |
|
176 } |
|
177 |
|
178 child.setParent(null); |
|
179 } |
|
180 } |
|
181 } |
|
182 |
|
183 // Native methods |
|
184 private static native int _ctor(int hInterface); |
|
185 private static native void _addChild(int handle, int hNode); |
|
186 private static native void _removeChild(int handle, int hNode); |
|
187 private static native int _getChildCount(int handle); |
|
188 private static native int _getChild(int handle, int index); |
|
189 private static native int _pick3D(int handle, int mask, float[] ray, float[] result); |
|
190 private static native int _pick2D(int handle, int mask, float x, float y, int hCamera, float[] result); |
|
191 } |