001: /*
002: * $RCSfile: LineAttributesRetained.java,v $
003: *
004: * Copyright 1998-2008 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
006: *
007: * This code is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License version 2 only, as
009: * published by the Free Software Foundation. Sun designates this
010: * particular file as subject to the "Classpath" exception as provided
011: * by Sun in the LICENSE file that accompanied this code.
012: *
013: * This code is distributed in the hope that it will be useful, but WITHOUT
014: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: * version 2 for more details (a copy is included in the LICENSE file that
017: * accompanied this code).
018: *
019: * You should have received a copy of the GNU General Public License version
020: * 2 along with this work; if not, write to the Free Software Foundation,
021: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
024: * CA 95054 USA or visit www.sun.com if you need additional information or
025: * have any questions.
026: *
027: * $Revision: 1.7 $
028: * $Date: 2008/02/28 20:17:25 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import java.util.ArrayList;
035:
036: /**
037: * The LineAttributesRetained object defines all rendering state that can be set
038: * as a component object of a Shape3D node.
039: */
040: class LineAttributesRetained extends NodeComponentRetained {
041: // A list of pre-defined bits to indicate which component
042: // in this LineAttributesRetained object changed.
043: static final int LINE_WIDTH_CHANGED = 0x01;
044: static final int LINE_PATTERN_CHANGED = 0x02;
045: static final int LINE_AA_CHANGED = 0x04;
046: static final int LINE_PATTERN_MASK_CHANGED = 0x08;
047: static final int LINE_PATTERN_SCALEFACTOR_CHANGED = 0x10;
048:
049: // Width, in pixels, of line primitives
050: float lineWidth = 1.0f;
051:
052: // The line pattern to be used
053: int linePattern = LineAttributes.PATTERN_SOLID;
054:
055: // Line antialiasing switch
056: boolean lineAntialiasing = false;
057:
058: // user-defined line pattern mask
059: int linePatternMask = 0xffff;
060:
061: // line mask pattern scale factor
062: int linePatternScaleFactor = 1;
063:
064: /**
065: * Sets the line width for this lineAttributes component object.
066: * @param lineWidth the width, in pixels, of line primitives
067: */
068: final void initLineWidth(float lineWidth) {
069: this .lineWidth = lineWidth;
070: }
071:
072: /**
073: * Sets the line width for this lineAttributes component object and sends a
074: * message notifying the interested structures of the change.
075: * @param lineWidth the width, in pixels, of line primitives
076: */
077: final void setLineWidth(float lineWidth) {
078: initLineWidth(lineWidth);
079: sendMessage(LINE_WIDTH_CHANGED, new Float(lineWidth));
080: }
081:
082: /**
083: * Gets the line width for this lineAttributes component object.
084: * @return the width, in pixels, of line primitives
085: */
086: final float getLineWidth() {
087: return lineWidth;
088: }
089:
090: /**
091: * Sets the line pattern for this lineAttributes component object
092: * @param linePattern the line pattern to be used, one of:
093: * PATTERN_SOLID, PATTERN_DASH, PATTERN_DOT, or PATTERN_DASH_DOT
094: */
095: final void initLinePattern(int linePattern) {
096: this .linePattern = linePattern;
097: }
098:
099: /**
100: * Sets the line pattern for this lineAttributes component object
101: * and sends a message notifying the interested structures of the change.
102: * @param linePattern the line pattern to be used, one of:
103: * PATTERN_SOLID, PATTERN_DASH, PATTERN_DOT, or PATTERN_DASH_DOT
104: */
105: final void setLinePattern(int linePattern) {
106: initLinePattern(linePattern);
107: sendMessage(LINE_PATTERN_CHANGED, new Integer(linePattern));
108: }
109:
110: /**
111: * Gets the line pattern for this lineAttributes component object.
112: * @return the line pattern
113: */
114: final int getLinePattern() {
115: return linePattern;
116: }
117:
118: /**
119: * Enables or disables line antialiasing
120: * for this lineAttributes component object and sends a
121: * message notifying the interested structures of the change.
122: * @param state true or false to enable or disable line antialiasing
123: */
124: final void initLineAntialiasingEnable(boolean state) {
125: lineAntialiasing = state;
126: }
127:
128: /**
129: * Enables or disables line antialiasing
130: * for this lineAttributes component object and sends a
131: * message notifying the interested structures of the change.
132: * @param state true or false to enable or disable line antialiasing
133: */
134: final void setLineAntialiasingEnable(boolean state) {
135: initLineAntialiasingEnable(state);
136: sendMessage(LINE_AA_CHANGED, (state ? Boolean.TRUE
137: : Boolean.FALSE));
138: }
139:
140: /**
141: * Retrieves the state of the line antialiasing flag.
142: * @return true if line antialiasing is enabled,
143: * false if line antialiasing is disabled
144: */
145: final boolean getLineAntialiasingEnable() {
146: return lineAntialiasing;
147: }
148:
149: /**
150: * Sets the pattern mask for this LineAttributes component object.
151: * This is used when the linePattern attribute is set to
152: * PATTERN_USER_DEFINED.
153: * @param mask the line pattern mask to be used.
154: */
155: final void initPatternMask(int mask) {
156: this .linePatternMask = mask;
157: }
158:
159: /**
160: * Sets the pattern mask for this LineAttributes component object
161: * and sends a message notifying the interested structures of change.
162: * This is used when the linePattern attribute is set to
163: * PATTERN_USER_DEFINED.
164: * @param mask the line pattern mask to be used.
165: */
166: final void setPatternMask(int mask) {
167: initPatternMask(mask);
168: sendMessage(LINE_PATTERN_MASK_CHANGED, new Integer(mask));
169: }
170:
171: /**
172: * Retrieves the pattern mask for this LineAttributes component object.
173: * @return the user-defined pattern mask
174: */
175: final int getPatternMask() {
176: return linePatternMask;
177: }
178:
179: /**
180: * Sets the pattern mask scale factor for this LineAttributes
181: * component object. This is used when the linePattern attribute
182: * is set to PATTERN_USER_DEFINED.
183: * @param scaleFactor the scale factor of mask, clamp to [1, 15]
184: */
185: final void initPatternScaleFactor(int scaleFactor) {
186: if (scaleFactor < 1) {
187: scaleFactor = 1;
188: } else if (scaleFactor > 15) {
189: scaleFactor = 15;
190: }
191: this .linePatternScaleFactor = scaleFactor;
192: }
193:
194: /**
195: * Sets the pattern mask scale factor for this LineAttributes
196: * component object and sends a message notifying the interested
197: * structures of change. This is used when the linePattern
198: * attribute is set to PATTERN_USER_DEFINED.
199: * @param scaleFactor the scale factor of mask, clamp to [1, 15]
200: */
201: final void setPatternScaleFactor(int scaleFactor) {
202: initPatternScaleFactor(scaleFactor);
203: sendMessage(LINE_PATTERN_SCALEFACTOR_CHANGED, new Integer(
204: scaleFactor));
205: }
206:
207: /**
208: * Retrieves the pattern scale factor for this LineAttributes
209: * component object.
210: * @return the pattern mask scale factor
211: */
212: final int getPatternScaleFactor() {
213: return linePatternScaleFactor;
214: }
215:
216: /**
217: * Creates and initializes a mirror object, point the mirror object
218: * to the retained object if the object is not editable
219: */
220: synchronized void createMirrorObject() {
221: if (mirror == null) {
222: // Check the capability bits and let the mirror object
223: // point to itself if is not editable
224: if (isStatic()) {
225: mirror = this ;
226: } else {
227: LineAttributesRetained mirrorLa = new LineAttributesRetained();
228: mirrorLa.source = source;
229: mirrorLa.set(this );
230: mirror = mirrorLa;
231: }
232: } else {
233: ((LineAttributesRetained) mirror).set(this );
234: }
235: }
236:
237: /**
238: * This method updates the native context.
239: */
240: void updateNative(Context ctx) {
241: Pipeline.getPipeline().updateLineAttributes(ctx, lineWidth,
242: linePattern, linePatternMask, linePatternScaleFactor,
243: lineAntialiasing);
244: }
245:
246: /**
247: * Initializes a mirror object, point the mirror object to the retained
248: * object if the object is not editable
249: */
250: synchronized void initMirrorObject() {
251: ((LineAttributesRetained) mirror).set(this );
252: }
253:
254: /** Update the "component" field of the mirror object with the
255: * given "value"
256: */
257: synchronized void updateMirrorObject(int component, Object value) {
258:
259: LineAttributesRetained mirrorLa = (LineAttributesRetained) mirror;
260:
261: if ((component & LINE_WIDTH_CHANGED) != 0) {
262: mirrorLa.lineWidth = ((Float) value).floatValue();
263: } else if ((component & LINE_PATTERN_CHANGED) != 0) {
264: mirrorLa.linePattern = ((Integer) value).intValue();
265: } else if ((component & LINE_AA_CHANGED) != 0) {
266: mirrorLa.lineAntialiasing = ((Boolean) value)
267: .booleanValue();
268: } else if ((component & LINE_PATTERN_MASK_CHANGED) != 0) {
269: mirrorLa.linePatternMask = ((Integer) value).intValue();
270: } else if ((component & LINE_PATTERN_SCALEFACTOR_CHANGED) != 0) {
271: mirrorLa.linePatternScaleFactor = ((Integer) value)
272: .intValue();
273: }
274: }
275:
276: boolean equivalent(LineAttributesRetained lr) {
277: return ((lr != null) && (lineWidth == lr.lineWidth)
278: && (linePattern == lr.linePattern)
279: && (lineAntialiasing == lr.lineAntialiasing)
280: && (linePatternMask == lr.linePatternMask) && (linePatternScaleFactor == lr.linePatternScaleFactor));
281:
282: }
283:
284: protected void set(LineAttributesRetained lr) {
285: super .set(lr);
286: lineWidth = lr.lineWidth;
287: linePattern = lr.linePattern;
288: linePatternScaleFactor = lr.linePatternScaleFactor;
289: linePatternMask = lr.linePatternMask;
290: lineAntialiasing = lr.lineAntialiasing;
291: }
292:
293: final void sendMessage(int attrMask, Object attr) {
294: ArrayList univList = new ArrayList();
295: ArrayList gaList = Shape3DRetained.getGeomAtomsList(
296: mirror.users, univList);
297:
298: // Send to rendering attribute structure, regardless of
299: // whether there are users or not (alternate appearance case ..)
300: J3dMessage createMessage = new J3dMessage();
301: createMessage.threads = J3dThread.UPDATE_RENDERING_ATTRIBUTES;
302: createMessage.type = J3dMessage.LINEATTRIBUTES_CHANGED;
303: createMessage.universe = null;
304: createMessage.args[0] = this ;
305: createMessage.args[1] = new Integer(attrMask);
306: createMessage.args[2] = attr;
307: createMessage.args[3] = new Integer(changedFrequent);
308: VirtualUniverse.mc.processMessage(createMessage);
309:
310: // System.err.println("univList.size is " + univList.size());
311: for (int i = 0; i < univList.size(); i++) {
312: createMessage = new J3dMessage();
313: createMessage.threads = J3dThread.UPDATE_RENDER;
314: createMessage.type = J3dMessage.LINEATTRIBUTES_CHANGED;
315:
316: createMessage.universe = (VirtualUniverse) univList.get(i);
317: createMessage.args[0] = this ;
318: createMessage.args[1] = new Integer(attrMask);
319: createMessage.args[2] = attr;
320:
321: ArrayList gL = (ArrayList) gaList.get(i);
322: GeometryAtom[] gaArr = new GeometryAtom[gL.size()];
323: gL.toArray(gaArr);
324: createMessage.args[3] = gaArr;
325:
326: VirtualUniverse.mc.processMessage(createMessage);
327: }
328:
329: }
330:
331: void handleFrequencyChange(int bit) {
332: if (bit == LineAttributes.ALLOW_WIDTH_WRITE
333: || bit == LineAttributes.ALLOW_PATTERN_WRITE
334: || bit == LineAttributes.ALLOW_ANTIALIASING_WRITE) {
335: setFrequencyChangeMask(bit, 0x1);
336: }
337: }
338:
339: }
|