OSDN Git Service

ran gdx-tools HeaderFixer tool
[mikumikustudio/libgdx-mikumikustudio.git] / gdx / src / com / badlogic / gdx / graphics / g3d / Attribute.java
1 /*******************************************************************************\r
2  * Copyright 2011 See AUTHORS file.\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  * \r
8  *   http://www.apache.org/licenses/LICENSE-2.0\r
9  * \r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  ******************************************************************************/
16
17 package com.badlogic.gdx.graphics.g3d;
18
19 import com.badlogic.gdx.utils.Array;
20
21 /** Extend this class to implement a material attribute.
22  * Register the attribute type by statically calling the {@link #register(String)} method, 
23  * whose return value should be used to instantiate the attribute. 
24  * A class can implement multiple types 
25  * @author Xoppa */
26 public abstract class Attribute {
27         /** The registered type aliases */
28         private final static Array<String> types = new Array<String>();
29         
30         /** @return The ID of the specified attribute type, or zero if not available */
31         public final static long getAttributeType(final String alias) {
32                 for (int i = 0; i < types.size; i++)
33                         if (types.get(i).compareTo(alias)==0)
34                                 return 1L << i;
35                 return 0;
36         }
37         
38         /** @return The alias of the specified attribute type, or null if not available. */
39         public final static String getAttributeAlias(final long type) {
40                 int idx = -1;
41                 while (type != 0 && ++idx < 63 && (((type >> idx) & 1) == 0));
42                 return (idx >= 0 && idx < types.size) ? types.get(idx) : null;
43         }
44         
45         /** Use {@link Attribute#register(String)} instead */ 
46         protected final static long register(final String alias) {
47                 long result = getAttributeType(alias);
48                 if (result > 0)
49                         return result;
50                 types.add(alias);
51                 return 1L << (types.size - 1);
52         }
53         
54         /** The type of this attribute */
55         public final long type;
56         protected Attribute(final long type) {
57                 this.type = type;
58         }
59         
60         /** @return An exact copy of this attribute */
61         public abstract Attribute copy();
62         
63         protected abstract boolean equals(Attribute other);
64         
65         @Override
66         public boolean equals (Object obj) {
67                 if (obj == null) return false;
68                 if (obj == this) return true;
69                 if (!(obj instanceof Attribute)) return false;
70                 final Attribute other = (Attribute)obj;
71                 if (this.type != other.type) return false;
72                 return equals(other);
73         }
74         
75         @Override
76         public String toString () {
77                 return getAttributeAlias(type);
78         }
79 }