gdx-studio

#libgdx#java#desktop

git clone https://git.pyrossh.dev/gdx-studio

An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop


src/scene2d/Sprite.java
a9b2a64 1
package scene2d;
a62d533 2
import com.badlogic.gdx.graphics.g2d.Animation;
a62d533 3
import com.badlogic.gdx.graphics.g2d.Batch;
a62d533 4
import com.badlogic.gdx.graphics.g2d.TextureRegion;
a62d533 5
import com.badlogic.gdx.scenes.scene2d.Actor;
a62d533 6
import com.badlogic.gdx.utils.Array;
a62d533 7
a62d533 8
a62d533 9
public class Sprite extends Actor {
a62d533 10
	
a62d533 11
	private Array<String> textures = new Array<String>();
a62d533 12
	
a62d533 13
	private Animation animation;
a62d533 14
	private TextureRegion keyFrame;
a62d533 15
	private float duration = 1f;
a62d533 16
	private int frameCount = 1;
a62d533 17
	
a62d533 18
	public boolean isAnimationActive = true;
a62d533 19
	public boolean isAnimationLooping = true;
a62d533 20
a62d533 21
	public Sprite(String texName, int frameCount){
a62d533 22
		textures.add(texName);
a62d533 23
		setFrameCount(frameCount);
a62d533 24
	}
a62d533 25
	
a62d533 26
	public Sprite(String texName, int frameCount, float duration){
a62d533 27
		textures.add(texName);
a62d533 28
		setFrameCount(frameCount, duration);
a62d533 29
	}
a62d533 30
	
a62d533 31
	public Sprite(String... texNames){
a62d533 32
		setTextures(texNames);
a62d533 33
	}
a62d533 34
	
a62d533 35
	public Sprite(float duration, String... texNames){
a62d533 36
		setTextures(duration, texNames);
a62d533 37
		this.duration = duration;
a62d533 38
	}
a62d533 39
	
a62d533 40
	@Override
a62d533 41
	public void draw(Batch batch, float parentAlpha) {
a62d533 42
		super.draw(batch, parentAlpha);
a62d533 43
		if (isAnimationActive && animation != null) {
a9b2a64 44
			keyFrame = animation.getKeyFrame(Scene.stateTime, isAnimationLooping);
7c84877 45
			batch.draw(keyFrame, getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(),
7c84877 46
					getScaleX(), getScaleY(), getRotation());
a62d533 47
		}
a62d533 48
	}
a62d533 49
	
a62d533 50
	public void setTextures(String... texNames){
a62d533 51
		textures.clear();
a62d533 52
		if(texNames.length == 1){
a62d533 53
			textures.add(texNames[0]);
a62d533 54
			animation = Asset.anim(texNames[0], frameCount);
a62d533 55
			setSize(animation.getKeyFrame(0).getRegionWidth(), animation.getKeyFrame(0).getRegionHeight());
a62d533 56
			return;
a62d533 57
		}
a62d533 58
		Array<TextureRegion> array = new Array<TextureRegion>();
a62d533 59
		for(int i=0;i<texNames.length;i++){
a62d533 60
			if(!texNames[i].trim().isEmpty()){
a62d533 61
				array.add(Asset.tex(texNames[i]));
a62d533 62
				textures.add(texNames[i]);
a62d533 63
			}
a62d533 64
		}
a62d533 65
		animation = new Animation(1f/texNames.length, array);
a62d533 66
		duration = 1f/texNames.length;
a62d533 67
		setSize(array.first().getRegionWidth(), array.first().getRegionHeight());
a62d533 68
	}
a62d533 69
	
a62d533 70
	public void setTextures(float duration, String... texNames){
a62d533 71
		textures.clear();
a62d533 72
		if(texNames.length == 1){
a62d533 73
			textures.add(texNames[0]);
a62d533 74
			animation = Asset.anim(texNames[0], frameCount, duration);
a62d533 75
			setSize(animation.getKeyFrame(0).getRegionWidth(), animation.getKeyFrame(0).getRegionHeight());
a62d533 76
			return;
a62d533 77
		}
a62d533 78
		Array<TextureRegion> array = new Array<TextureRegion>(); 
a62d533 79
		for(int i=0;i<texNames.length;i++){
a62d533 80
			if(!texNames[i].trim().isEmpty()){
a62d533 81
				array.add(Asset.tex(texNames[i]));
a62d533 82
				textures.add(texNames[i]);
a62d533 83
			}
a62d533 84
		}
a62d533 85
		animation = new Animation(duration, array);
a62d533 86
		setSize(array.first().getRegionWidth(), array.first().getRegionHeight());
a62d533 87
	}
a62d533 88
	
a62d533 89
	public Array<String> getTextures(){
a62d533 90
		return textures;
a62d533 91
	}
a62d533 92
	
a62d533 93
	public void setDuration(float duration){
a62d533 94
		this.duration = duration;
a62d533 95
		StringBuilder sb = new StringBuilder();
a62d533 96
		for(String s: textures){
a62d533 97
			sb.append(s);
a62d533 98
			sb.append(",");
a62d533 99
		}
a62d533 100
		setTextures(duration, sb.toString().split(","));
a62d533 101
	}
a62d533 102
	
a62d533 103
	public float getDuration(){
a62d533 104
		return this.duration;
a62d533 105
	}
a62d533 106
	
a62d533 107
	public int getFrameCount(){
a62d533 108
		return frameCount;
a62d533 109
	}
a62d533 110
	
a62d533 111
	public void setFrameCount(int count){
a62d533 112
		frameCount = count;
a62d533 113
		animation = Asset.anim(textures.get(0), frameCount);
a62d533 114
		setSize(animation.getKeyFrame(0).getRegionWidth(), animation.getKeyFrame(0).getRegionHeight());
a62d533 115
	}
a62d533 116
	
a62d533 117
	public void setFrameCount(int count, float duration){
a62d533 118
		frameCount = count;
a62d533 119
		animation = Asset.anim(textures.get(0), frameCount, duration);
a62d533 120
		setSize(animation.getKeyFrame(0).getRegionWidth(), animation.getKeyFrame(0).getRegionHeight());
a62d533 121
	}
a62d533 122
	
a62d533 123
	@Override
a62d533 124
	public String toString(){
a62d533 125
		StringBuilder sb = new StringBuilder();
a62d533 126
		for(String tex: textures){
a62d533 127
			sb.append(tex);
a62d533 128
			sb.append(",");
a62d533 129
		}
a62d533 130
		sb.deleteCharAt(sb.lastIndexOf(","));
a62d533 131
		return sb.toString();
a62d533 132
	}
a62d533 133
}