Created
May 8, 2013 14:30
-
-
Save studioijeoma/5540836 to your computer and use it in GitHub Desktop.
Tweening Toxiclibs Vec3D with ijeomamotion using the IProperty interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ijeoma.motion.Motion; | |
import ijeoma.motion.tween.Tween; | |
import toxi.geom.Vec3D; | |
Tween t; | |
PFont f; | |
Vec3D v1, v2; | |
public void setup() { | |
size(400, 400); | |
smooth(); | |
rectMode(CENTER); | |
f = createFont("Arial", 12); | |
v1 = new Vec3D(0, 0, 0); | |
v2 = new Vec3D(0, 0, 0); | |
Motion.setup(this); | |
t = new Tween(100) | |
.add(new Vec3DProperty(this, "v1", new Vec3D(width, height, 0))) | |
.add(new Vec3DProperty(this, "v2", new Vec3D(width / 2, | |
height / 2, 0))).play(); | |
} | |
public void draw() { | |
background(255); | |
noStroke(); | |
fill(0); | |
rect(width / 2, height / 2, v1.x, v1.y); | |
fill(255, 0, 0); | |
rect(width / 2, height / 2, v2.x, v2.y); | |
fill(0); | |
String time = t.getTime() + " / " + t.getDuration(); | |
text(time, width - textWidth(time) - 10, height - 10); | |
} | |
public void keyPressed() { | |
t.play(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ijeoma.motion.tween.IProperty; | |
import java.lang.reflect.Field; | |
import toxi.geom.Vec3D; | |
public class Vec3DProperty implements IProperty { | |
protected Object object; | |
protected Class<? extends Object> objectType; | |
protected Field field; | |
protected Class<?> fieldType; | |
String name = ""; | |
protected Vec3D begin, end, change; | |
protected float position; | |
protected Vec3D value = new Vec3D(); | |
protected int order = 0; | |
public Vec3DProperty() { | |
} | |
public Vec3DProperty(Object object, String name, Vec3D end) { | |
setupObject(object, name); | |
setup(name, end); | |
} | |
public Vec3DProperty(Object object, String name, Vec3D begin, Vec3D end) { | |
setupObject(object, name); | |
setup(name, begin, end); | |
} | |
public Vec3DProperty(String name, Vec3D begin, Vec3D end) { | |
setup(name, begin, end); | |
} | |
private void setup(String name, Vec3D end) { | |
this.name = name; | |
setEnd(end); | |
position = 0; | |
} | |
private void setup(String name, Vec3D begin, Vec3D end) { | |
this.name = name; | |
setEnd(end); | |
setBegin(begin); | |
position = 0; | |
} | |
private void setupObject(Object propertyObject, String propertyName) { | |
object = propertyObject; | |
objectType = object.getClass(); | |
boolean found = false; | |
while (objectType != null) { | |
for (Field f : objectType.getDeclaredFields()) | |
if (f.getName().equals(propertyName)) { | |
fieldType = f.getType(); | |
found = true; | |
break; | |
} | |
if (found) | |
break; | |
else | |
objectType = objectType.getSuperclass(); | |
} | |
if (found) | |
try { | |
field = objectType.getDeclaredField(propertyName); | |
try { | |
field.setAccessible(true); | |
} catch (java.security.AccessControlException e) { | |
e.printStackTrace(); | |
} | |
} catch (NoSuchFieldException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void updateValue() { | |
if ((position > 0 && position <= 1) || (position == 0 && order == 0)) { | |
value = begin.interpolateTo(end, position); | |
if (field != null) | |
try { | |
((Vec3D) field.get(object)).set(value); | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
@Override | |
public String getName() { | |
return name; | |
} | |
@Override | |
public void setName(String _name) { | |
name = _name; | |
} | |
public Vec3D getBegin() { | |
return begin; | |
} | |
public void setBegin() { | |
try { | |
begin = ((Vec3D) field.get(object)).copy(); | |
change = end.sub(begin).copy(); | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void setBegin(Object begin) { | |
this.begin = (Vec3D) begin; | |
change = end.sub(this.begin).copy(); | |
} | |
public Vec3D getEnd() { | |
return end; | |
} | |
public void setEnd(Object end) { | |
if (field != null) { | |
try { | |
begin = ((Vec3D) field.get(object)).copy(); | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} else | |
begin = value; | |
this.end = (Vec3D) end; | |
change = this.end.sub(begin).copy(); | |
} | |
public Vec3D getChange() { | |
return change; | |
} | |
public void setChange(Object _change) { | |
change = (Vec3D) _change; | |
} | |
public Float getPosition() { | |
return position; | |
} | |
@Override | |
public void setPosition(Object _position) { | |
position = (Float) _position; | |
updateValue(); | |
} | |
public Vec3D getValue() { | |
if (field != null) { | |
try { | |
return ((Vec3D) field.get(object)).copy(); | |
} catch (IllegalArgumentException e) { | |
e.printStackTrace(); | |
return null; | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} else | |
return value; | |
} | |
public Object getObject() { | |
return object; | |
} | |
public void setOrder(int index) { | |
order = index; | |
} | |
public int getOrder() { | |
return order; | |
} | |
@Override | |
public String toString() { | |
return "NumberParameter[name: " + name + ", begin: " + begin | |
+ ", end: " + end + ", change: " + change + ", position: " | |
+ position + "]"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment