001: /*
002: * $RCSfile: SwitchState.java,v $
003: *
004: * Copyright 2001-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.6 $
028: * $Date: 2008/02/28 20:17:31 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: class SwitchState {
035: // a bitmask to track the composite switchOn state in a nested switch
036: // tree. A bit is set if a branch is switched off at the switch
037: // level specified by the position of the bit in the bitmask
038: // It is an array of long in order to support infinite deep nested
039: // switch tree
040: long compositeSwitchMask[] = new long[] { 0 };
041:
042: // switchOn state cached in user thread
043: boolean cachedSwitchOn = true;
044:
045: // switchOn state in current time, is true if compositeSwitchMask == 0
046: boolean currentSwitchOn = true;
047:
048: // switchOn state in last time, is true if compositeSwitchMask == 0
049: boolean lastSwitchOn = true;
050:
051: boolean initialized = false;
052:
053: CachedTargets cachedTargets = null;
054:
055: boolean inSwitch = false;
056:
057: public SwitchState(boolean inSwitch) {
058: this .inSwitch = inSwitch;
059: initialized = !inSwitch;
060: }
061:
062: void dump() {
063: System.err.println(" MASK " + compositeSwitchMask[0] + " CACH "
064: + cachedSwitchOn + " CURR " + currentSwitchOn
065: + " LAST " + lastSwitchOn);
066: }
067:
068: void updateCompositeSwitchMask(int switchLevel, boolean switchOn) {
069: if (switchLevel < 64) {
070: if (switchOn) {
071: compositeSwitchMask[0] &= ~(1 << switchLevel);
072: } else {
073: compositeSwitchMask[0] |= (1 << switchLevel);
074: }
075: } else {
076: int i;
077: int index = switchLevel / 64;
078: int offset = switchLevel % 64;
079:
080: if (index > compositeSwitchMask.length) {
081: long newCompositeSwitchMask[] = new long[index + 1];
082: System.arraycopy(compositeSwitchMask, 0,
083: newCompositeSwitchMask, 0, index);
084: compositeSwitchMask = newCompositeSwitchMask;
085: }
086: if (switchOn) {
087: compositeSwitchMask[index] &= ~(1 << offset);
088: } else {
089: compositeSwitchMask[index] |= (1 << offset);
090: }
091: }
092: }
093:
094: void initSwitchOn() {
095: boolean switchOn = evalCompositeSwitchOn();
096: currentSwitchOn = lastSwitchOn = cachedSwitchOn = switchOn;
097: //currentSwitchOn = cachedSwitchOn = switchOn;
098: initialized = true;
099: }
100:
101: void updateCurrentSwitchOn() {
102: currentSwitchOn = !currentSwitchOn;
103: }
104:
105: void updateLastSwitchOn() {
106: lastSwitchOn = currentSwitchOn;
107: }
108:
109: void updateCachedSwitchOn() {
110: cachedSwitchOn = !cachedSwitchOn;
111: }
112:
113: boolean evalCompositeSwitchOn() {
114: boolean switchOn;
115: if (compositeSwitchMask.length == 1) {
116: switchOn = (compositeSwitchMask[0] == 0);
117: } else {
118: switchOn = true;
119: for (int i = 0; i < compositeSwitchMask.length; i++) {
120: if (compositeSwitchMask[i] != 0) {
121: switchOn = false;
122: break;
123: }
124: }
125: }
126: return switchOn;
127: }
128: }
|