1 /*******************************************************************************
\r
2 * Copyright 2011 See AUTHORS file.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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 ******************************************************************************/
17 package com.badlogic.gdx.graphics.g3d;
19 import com.badlogic.gdx.utils.Array;
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
26 public abstract class Attribute {
27 /** The registered type aliases */
28 private final static Array<String> types = new Array<String>();
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)
38 /** @return The alias of the specified attribute type, or null if not available. */
39 public final static String getAttributeAlias(final long type) {
41 while (type != 0 && ++idx < 63 && (((type >> idx) & 1) == 0));
42 return (idx >= 0 && idx < types.size) ? types.get(idx) : null;
45 /** Use {@link Attribute#register(String)} instead */
46 protected final static long register(final String alias) {
47 long result = getAttributeType(alias);
51 return 1L << (types.size - 1);
54 /** The type of this attribute */
55 public final long type;
56 protected Attribute(final long type) {
60 /** @return An exact copy of this attribute */
61 public abstract Attribute copy();
63 protected abstract boolean equals(Attribute other);
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;
76 public String toString () {
77 return getAttributeAlias(type);