com.brashmonkey.spriter.Drawer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of overlap2d-runtime-libgdx Show documentation
Show all versions of overlap2d-runtime-libgdx Show documentation
overlap2d-runtime-libgdx provides functionality to load, manipulate and render scenes generated by Overlap2D.
package com.brashmonkey.spriter;
import java.util.Iterator;
import com.brashmonkey.spriter.Entity.CharacterMap;
import com.brashmonkey.spriter.Entity.ObjectInfo;
import com.brashmonkey.spriter.Entity.ObjectType;
import com.brashmonkey.spriter.Timeline.Key.Bone;
import com.brashmonkey.spriter.Timeline.Key.Object;
/**
* A Drawer is responsible for drawing a {@link Player}.
* Since this library is meant to be as generic as possible this class has to be abstract, because it cannot be assumed how to draw a resource.
* Anyone who wants to draw a {@link Player} has to know how to draw a resource. A resource can be e.g. a sprite, a texture or a texture region.
* To draw a {@link Player} call {@link #draw(Player)}. This method relies on {@link #draw(Object)}, which has to be implemented with the chosen backend.
* To debug draw a {@link Player} call {@link #drawBones(Player)}, {@link #drawBoxes(Player)} and {@link #drawPoints(Player)},
* which rely on {@link #rectangle(float, float, float, float)}, {@link #circle(float, float, float)}, {@link #line(float, float, float, float)} and {@link #setColor(float, float, float, float)}.
* @author Trixt0r
*
* @param The backend specific resource. In general such a resource is called "sprite", "texture" or "image".
*/
public abstract class Drawer {
/**
* The radius of a point for debug drawing purposes.
*/
public float pointRadius = 5f;
protected Loader loader;
/**
* Creates a new drawer based on the given loader.
* @param loader the loader containing resources
*/
public Drawer(Loader loader){
this.loader = loader;
}
/**
* Sets the loader of this drawer.
* @param loader the loader containing resources
* @throws SpriterException if the loader is null
*/
public void setLoader(Loader loader){
if(loader == null) throw new SpriterException("The loader instance can not be null!");
this.loader = loader;
}
/**
* Draws the bones of the given player composed of lines.
* @param player the player to draw
*/
public void drawBones(Player player){
this.setColor(1, 0, 0, 1);
Iterator it = player.boneIterator();
while(it.hasNext()){
Timeline.Key.Bone bone = it.next();
Timeline.Key key = player.getKeyFor(bone);
if(!key.active) continue;
ObjectInfo info = player.getObjectInfoFor(bone);
Dimension size = info.size;
drawBone(bone, size);
}
/*for(Mainline.Key.BoneRef ref: player.getCurrentKey().boneRefs){
Timeline.Key key = player.unmappedTweenedKeys[ref.timeline];
Timeline.Key.Bone bone = key.object();
if(player.animation.getTimeline(ref.timeline).objectInfo.type != ObjectType.Bone || !key.active) continue;
ObjectInfo info = player.animation.getTimeline(ref.timeline).objectInfo;
if(info == null) continue;
Dimension size = info.size;
drawBone(bone, size);
}*/
}
/**
* Draws the given bone composed of lines with the given size.
* @param bone the bone to draw
* @param size the size of the bone
*/
public void drawBone(Bone bone, Dimension size){
float halfHeight = size.height/2;
float xx = bone.position.x+(float)Math.cos(Math.toRadians(bone.angle))*size.height;
float yy = bone.position.y+(float)Math.sin(Math.toRadians(bone.angle))*size.height;
float x2 = (float)Math.cos(Math.toRadians(bone.angle+90))*halfHeight*bone.scale.y;
float y2 = (float)Math.sin(Math.toRadians(bone.angle+90))*halfHeight*bone.scale.y;
float targetX = bone.position.x+(float)Math.cos(Math.toRadians(bone.angle))*size.width*bone.scale.x,
targetY = bone.position.y+(float)Math.sin(Math.toRadians(bone.angle))*size.width*bone.scale.x;
float upperPointX = xx+x2, upperPointY = yy+y2;
this.line(bone.position.x, bone.position.y, upperPointX, upperPointY);
this.line(upperPointX, upperPointY, targetX, targetY);
float lowerPointX = xx-x2, lowerPointY = yy-y2;
this.line(bone.position.x, bone.position.y, lowerPointX, lowerPointY);
this.line(lowerPointX, lowerPointY, targetX, targetY);
this.line(bone.position.x, bone.position.y, targetX, targetY);
}
/**
* Draws the boxes of the player.
* @param player the player to draw the boxes from
*/
public void drawBoxes(Player player){
this.setColor(0f, 1f, 0f, 1f);
this.drawBoneBoxes(player);
this.drawObjectBoxes(player);
this.drawPoints(player);
}
/**
* Draws the boxes of all bones of the given player.
* @param player the player to draw the bone boxes of
*/
public void drawBoneBoxes(Player player){
drawBoneBoxes(player, player.boneIterator());
}
/**
* Draws the boxes of all bones of the given player based on the given iterator.
* @param player the player to draw the bone boxes of
* @param it the iterator iterating over the bones to draw
*/
public void drawBoneBoxes(Player player, Iterator it){
while(it.hasNext()){
Bone bone = it.next();
this.drawBox(player.getBox(bone));
}
}
/**
* Draws the boxes of the player objects, i.e. sprites and objects.
* @param player the player to draw the object boxes of
*/
public void drawObjectBoxes(Player player){
drawObjectBoxes(player, player.objectIterator());
}
/**
* Draws the boxes of sprites and boxes of the given player based on the given iterator.
* @param player player the player to draw the object boxes of
* @param it the iterator iterating over the object to draw
*/
public void drawObjectBoxes(Player player, Iterator