• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2009-2010 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17  *   may be used to endorse or promote products derived from this software
18  *   without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package com.jme3.scene;
33 
34 import com.jme3.export.*;
35 import java.io.IOException;
36 
37 /**
38  * <code>UserData</code> is used to contain user data objects
39  * set on spatials (primarily primitives) that do not implement
40  * the {@link Savable} interface. Note that attempting
41  * to export any models which have non-savable objects
42  * attached to them will fail.
43  */
44 public final class UserData implements Savable {
45 
46     /**
47      * Boolean type on Geometries to indicate that physics collision
48      * shape generation should ignore them.
49      */
50     public static final String JME_PHYSICSIGNORE = "JmePhysicsIgnore";
51 
52     /**
53      * For geometries using shared mesh, this will specify the shared
54      * mesh reference.
55      */
56     public static final String JME_SHAREDMESH = "JmeSharedMesh";
57 
58     protected byte type;
59     protected Object value;
60 
UserData()61     public UserData() {
62     }
63 
64     /**
65      * Creates a new <code>UserData</code> with the given
66      * type and value.
67      *
68      * @param type Type of data, should be between 0 and 4.
69      * @param value Value of the data
70      */
UserData(byte type, Object value)71     public UserData(byte type, Object value) {
72         assert type >= 0 && type <= 4;
73         this.type = type;
74         this.value = value;
75     }
76 
getValue()77     public Object getValue() {
78         return value;
79     }
80 
81     @Override
toString()82     public String toString() {
83         return value.toString();
84     }
85 
getObjectType(Object type)86     public static byte getObjectType(Object type) {
87         if (type instanceof Integer) {
88             return 0;
89         } else if (type instanceof Float) {
90             return 1;
91         } else if (type instanceof Boolean) {
92             return 2;
93         } else if (type instanceof String) {
94             return 3;
95         } else if (type instanceof Long) {
96             return 4;
97         } else {
98             throw new IllegalArgumentException("Unsupported type: " + type.getClass().getName());
99         }
100     }
101 
write(JmeExporter ex)102     public void write(JmeExporter ex) throws IOException {
103         OutputCapsule oc = ex.getCapsule(this);
104         oc.write(type, "type", (byte)0);
105 
106         switch (type) {
107             case 0:
108                 int i = (Integer) value;
109                 oc.write(i, "intVal", 0);
110                 break;
111             case 1:
112                 float f = (Float) value;
113                 oc.write(f, "floatVal", 0f);
114                 break;
115             case 2:
116                 boolean b = (Boolean) value;
117                 oc.write(b, "boolVal", false);
118                 break;
119             case 3:
120                 String s = (String) value;
121                 oc.write(s, "strVal", null);
122                 break;
123             case 4:
124                 Long l = (Long) value;
125                 oc.write(l, "longVal", 0l);
126                 break;
127             default:
128                 throw new UnsupportedOperationException();
129         }
130     }
131 
read(JmeImporter im)132     public void read(JmeImporter im) throws IOException {
133         InputCapsule ic = im.getCapsule(this);
134         type = ic.readByte("type", (byte) 0);
135 
136         switch (type) {
137             case 0:
138                 value = ic.readInt("intVal", 0);
139                 break;
140             case 1:
141                 value = ic.readFloat("floatVal", 0f);
142                 break;
143             case 2:
144                 value = ic.readBoolean("boolVal", false);
145                 break;
146             case 3:
147                 value = ic.readString("strVal", null);
148                 break;
149             case 4:
150                 value = ic.readLong("longVal", 0l);
151                 break;
152             default:
153                 throw new UnsupportedOperationException();
154         }
155     }
156 }
157