001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/scenegrapheditor/nodeeditors/panels/TransObj.java,v 1.1 2005/04/20 22:21:11 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is the Java 3D(tm) Scene Graph Editor.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dedit.scenegrapheditor.nodeeditors.panels;
019:
020: import javax.vecmath.Vector3f;
021:
022: /**
023: * @author Paul Byrne
024: * @version 1.5, 01/18/02
025: */
026: public class TransObj {
027:
028: public static final int ROT_X = 0;
029: public static final int ROT_Y = 1;
030: public static final int ROT_Z = 2;
031: public static final int TRANSLATE = 3;
032: public static final int SCALE = 4;
033: public static final int NONUNISCALE = 5;
034:
035: int type;
036: Object value;
037:
038: public TransObj(int type, Object value) {
039: this .type = type;
040: this .value = value;
041: }
042:
043: public TransObj(String string) {
044: parseString(string);
045: }
046:
047: public int getType() {
048: return type;
049: }
050:
051: public Object getValue() {
052: return value;
053: }
054:
055: public float getFloat() {
056: return ((Float) value).floatValue();
057: }
058:
059: public String getFloatString() {
060: return ((Float) value).toString();
061: }
062:
063: public String getPointXString() {
064: return Float.toString(((Vector3f) value).x);
065: }
066:
067: public String getPointYString() {
068: return Float.toString(((Vector3f) value).y);
069: }
070:
071: public String getPointZString() {
072: return Float.toString(((Vector3f) value).z);
073: }
074:
075: public Vector3f getVector() {
076: return (Vector3f) value;
077: }
078:
079: public String toString() {
080: switch (type) {
081: case 0:
082: return new String("Rotate X " + getFloatString());
083: case 1:
084: return new String("Rotate Y " + getFloatString());
085: case 2:
086: return new String("Rotate Z " + getFloatString());
087: case 3:
088: return new String("Translate " + getPointXString() + ", "
089: + getPointYString() + ", " + getPointZString());
090: case 4:
091: return new String("Scale " + getFloatString());
092: case 5:
093: return new String("Non-uniform Scale " + getPointXString()
094: + ", " + getPointYString() + ", "
095: + getPointZString());
096: }
097:
098: return new String("Illegal TransObj");
099: }
100:
101: private void parseString(String str) {
102: if (str.startsWith("Rotate X")) {
103: type = ROT_X;
104: value = new Float(str.substring(str.lastIndexOf(' ')));
105: } else if (str.startsWith("Rotate Y")) {
106: type = ROT_Y;
107: value = new Float(str.substring(str.lastIndexOf(' ')));
108: } else if (str.startsWith("Rotate Z")) {
109: type = ROT_Z;
110: value = new Float(str.substring(str.lastIndexOf(' ')));
111: } else if (str.startsWith("Translate")) {
112: type = TRANSLATE;
113: value = extractVector(str.substring(9));
114: } else if (str.startsWith("Scale")) {
115: type = SCALE;
116: value = new Float(str.substring(str.lastIndexOf(' ')));
117: } else if (str.startsWith("Non-uniform Scale")) {
118: type = NONUNISCALE;
119: value = extractVector(str.substring(17));
120: } else
121: throw new RuntimeException(
122: "Unsupported Transform in TransObj");
123: }
124:
125: /**
126: * Given a string of the form 1, 2, 3
127: * create and return a vector
128: */
129: private Vector3f extractVector(String str) {
130: int firstComma = str.indexOf(',', 0);
131: int secondComma = str.indexOf(',', firstComma + 1);
132:
133: float x = 0, y = 0, z = 0;
134: try {
135: x = Float.parseFloat(str.substring(0, firstComma - 1));
136: y = Float.parseFloat(str.substring(firstComma + 1,
137: secondComma - 1));
138: z = Float.parseFloat(str.substring(secondComma + 1));
139: } catch (Exception e) {
140: e.printStackTrace();
141: }
142:
143: return new Vector3f(x, y, z);
144: }
145: }
|