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_libs/com/badlogic/gdx/tools/particleeditor/ParticleRenderer.java
a62d533 1
package com.badlogic.gdx.tools.particleeditor;
a62d533 2
a62d533 3
import java.awt.EventQueue;
a62d533 4
import java.io.File;
a62d533 5
a62d533 6
import javax.swing.JOptionPane;
a62d533 7
a62d533 8
import org.lwjgl.opengl.GL11;
a62d533 9
a62d533 10
import com.badlogic.gdx.ApplicationListener;
a62d533 11
import com.badlogic.gdx.Gdx;
a62d533 12
import com.badlogic.gdx.InputProcessor;
a62d533 13
import com.badlogic.gdx.Files.FileType;
a62d533 14
import com.badlogic.gdx.files.FileHandle;
a62d533 15
import com.badlogic.gdx.graphics.OrthographicCamera;
a62d533 16
import com.badlogic.gdx.graphics.Texture;
a62d533 17
import com.badlogic.gdx.graphics.g2d.BitmapFont;
a62d533 18
import com.badlogic.gdx.graphics.g2d.ParticleEmitter;
a62d533 19
import com.badlogic.gdx.graphics.g2d.Sprite;
a62d533 20
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
a62d533 21
import com.badlogic.gdx.graphics.g2d.ParticleEmitter.NumericValue;
a62d533 22
import com.badlogic.gdx.math.Vector3;
a62d533 23
import com.badlogic.gdx.utils.GdxRuntimeException;
a62d533 24
a62d533 25
public class ParticleRenderer implements ApplicationListener, InputProcessor {
a62d533 26
	/**
a62d533 27
	 * 
a62d533 28
	 */
a62d533 29
	private ParticlePanel particlePanel;
a62d533 30
a62d533 31
	/**
a62d533 32
	 * @param particlePanel
a62d533 33
	 */
a62d533 34
	public ParticleRenderer(ParticlePanel particlePanel) {
a62d533 35
		this.particlePanel = particlePanel;
a62d533 36
	}
a62d533 37
a62d533 38
	private float maxActiveTimer;
a62d533 39
	private int maxActive, lastMaxActive;
a62d533 40
	private boolean mouseDown;
a62d533 41
	private int activeCount;
a62d533 42
	private int mouseX, mouseY;
a62d533 43
	private BitmapFont font;
a62d533 44
	private SpriteBatch spriteBatch;
a62d533 45
	private Sprite bgImage; // BOZO - Add setting background image to UI.
a62d533 46
a62d533 47
	@Override
a62d533 48
	public void create () {
a62d533 49
		if (spriteBatch != null) return;
a62d533 50
a62d533 51
		Texture.setEnforcePotImages(false);
a62d533 52
a62d533 53
		spriteBatch = new SpriteBatch();
a62d533 54
a62d533 55
		this.particlePanel.worldCamera = new OrthographicCamera();
a62d533 56
		this.particlePanel.textCamera = new OrthographicCamera();
a62d533 57
a62d533 58
		this.particlePanel.pixelsPerMeter = new NumericValue();
a62d533 59
		this.particlePanel.pixelsPerMeter.setValue(1.0f);
a62d533 60
		this.particlePanel.pixelsPerMeter.setAlwaysActive(true);
a62d533 61
a62d533 62
		this.particlePanel.zoomLevel = new NumericValue();
a62d533 63
		this.particlePanel.zoomLevel.setValue(1.0f);
a62d533 64
		this.particlePanel.zoomLevel.setAlwaysActive(true);
a62d533 65
		
a62d533 66
		this.particlePanel.deltaMultiplier = new NumericValue();
a62d533 67
		this.particlePanel.deltaMultiplier.setValue(1.0f);
a62d533 68
		this.particlePanel.deltaMultiplier.setAlwaysActive(true);
a62d533 69
a62d533 70
		font = new BitmapFont(Gdx.files.getFileHandle("default.fnt", FileType.Internal), Gdx.files.getFileHandle("default.png",
a62d533 71
			FileType.Internal), true);
a62d533 72
		this.particlePanel.effectPanel.newExampleEmitter("Untitled", true);
a62d533 73
		// if (resources.openFile("/editor-bg.png") != null) bgImage = new Image(gl, "/editor-bg.png");
a62d533 74
		//Gdx.input.setInputProcessor(this);
a62d533 75
	}
a62d533 76
a62d533 77
	@Override
a62d533 78
	public void resize (int width, int height) {
a62d533 79
		Gdx.gl.glViewport(0, 0, width, height);
a62d533 80
a62d533 81
		if (this.particlePanel.pixelsPerMeter.getValue() <= 0) {
a62d533 82
			this.particlePanel.pixelsPerMeter.setValue(1);
a62d533 83
		}
a62d533 84
		this.particlePanel.worldCamera.setToOrtho(false, width / this.particlePanel.pixelsPerMeter.getValue(), height / this.particlePanel.pixelsPerMeter.getValue());
a62d533 85
		this.particlePanel.worldCamera.update();
a62d533 86
a62d533 87
		this.particlePanel.textCamera.setToOrtho(true, width, height);
a62d533 88
		this.particlePanel.textCamera.update();
a62d533 89
a62d533 90
		this.particlePanel.effect.setPosition(this.particlePanel.worldCamera.viewportWidth / 2, this.particlePanel.worldCamera.viewportHeight / 2);
a62d533 91
	}
a62d533 92
a62d533 93
	@Override
a62d533 94
	public void render () {
a62d533 95
		int viewWidth = Gdx.graphics.getWidth();
a62d533 96
		int viewHeight = Gdx.graphics.getHeight();
a62d533 97
a62d533 98
		float delta = Math.max(0, Gdx.graphics.getDeltaTime() * this.particlePanel.deltaMultiplier.getValue());
a62d533 99
a62d533 100
		Gdx.gl.glClear(GL11.GL_COLOR_BUFFER_BIT);
a62d533 101
a62d533 102
		if ((this.particlePanel.pixelsPerMeter.getValue() != this.particlePanel.pixelsPerMeterPrev) || (this.particlePanel.zoomLevel.getValue() != this.particlePanel.zoomLevelPrev)) {
a62d533 103
			if (this.particlePanel.pixelsPerMeter.getValue() <= 0) {
a62d533 104
				this.particlePanel.pixelsPerMeter.setValue(1);
a62d533 105
			}
a62d533 106
a62d533 107
			this.particlePanel.worldCamera.setToOrtho(false, viewWidth / this.particlePanel.pixelsPerMeter.getValue(), viewHeight / this.particlePanel.pixelsPerMeter.getValue());
a62d533 108
			this.particlePanel.worldCamera.zoom = this.particlePanel.zoomLevel.getValue();
a62d533 109
			this.particlePanel.worldCamera.update();
a62d533 110
			this.particlePanel.effect.setPosition(this.particlePanel.worldCamera.viewportWidth / 2, this.particlePanel.worldCamera.viewportHeight / 2);
a62d533 111
			this.particlePanel.zoomLevelPrev = this.particlePanel.zoomLevel.getValue();
a62d533 112
			this.particlePanel.pixelsPerMeterPrev = this.particlePanel.pixelsPerMeter.getValue();
a62d533 113
		}
a62d533 114
		
a62d533 115
		spriteBatch.setProjectionMatrix(this.particlePanel.worldCamera.combined);
a62d533 116
a62d533 117
		spriteBatch.begin();
a62d533 118
		spriteBatch.enableBlending();
a62d533 119
		spriteBatch.setBlendFunction(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
a62d533 120
a62d533 121
		if (bgImage != null) {
a62d533 122
			bgImage.setPosition(viewWidth / 2 - bgImage.getWidth() / 2, viewHeight / 2 - bgImage.getHeight() / 2);
a62d533 123
			bgImage.draw(spriteBatch);
a62d533 124
		}
a62d533 125
a62d533 126
		activeCount = 0;
a62d533 127
		boolean complete = true;
a62d533 128
		for (ParticleEmitter emitter : this.particlePanel.effect.getEmitters()) {
a62d533 129
			if (emitter.getSprite() == null && emitter.getImagePath() != null) loadImage(emitter);
a62d533 130
			boolean enabled = this.particlePanel.isEnabled(emitter);
a62d533 131
			if (enabled) {
a62d533 132
				if (emitter.getSprite() != null) emitter.draw(spriteBatch, delta);
a62d533 133
				activeCount += emitter.getActiveCount();
a62d533 134
				if (!emitter.isComplete()) complete = false;
a62d533 135
			}
a62d533 136
		}
a62d533 137
		if (complete) this.particlePanel.effect.start();
a62d533 138
a62d533 139
		maxActive = Math.max(maxActive, activeCount);
a62d533 140
		maxActiveTimer += delta;
a62d533 141
		if (maxActiveTimer > 3) {
a62d533 142
			maxActiveTimer = 0;
a62d533 143
			lastMaxActive = maxActive;
a62d533 144
			maxActive = 0;
a62d533 145
		}
a62d533 146
a62d533 147
		if (mouseDown) {
a62d533 148
			// gl.drawLine(mouseX - 6, mouseY, mouseX + 5, mouseY);
a62d533 149
			// gl.drawLine(mouseX, mouseY - 5, mouseX, mouseY + 6);
a62d533 150
		}
a62d533 151
a62d533 152
		spriteBatch.setProjectionMatrix(this.particlePanel.textCamera.combined);
a62d533 153
a62d533 154
		font.draw(spriteBatch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 5, 15);
a62d533 155
		font.draw(spriteBatch, "Count: " + activeCount, 5, 35);
a62d533 156
		font.draw(spriteBatch, "Max: " + lastMaxActive, 5, 55);
a62d533 157
		font.draw(spriteBatch, (int)(this.particlePanel.getEmitter().getPercentComplete() * 100) + "%", 5, 75);
a62d533 158
a62d533 159
		spriteBatch.end();
a62d533 160
a62d533 161
		// gl.drawLine((int)(viewWidth * getCurrentParticles().getPercentComplete()), viewHeight - 1, viewWidth, viewHeight -
a62d533 162
		// 1);
a62d533 163
	}
a62d533 164
a62d533 165
	private void loadImage (ParticleEmitter emitter) {
a62d533 166
		final String imagePath = emitter.getImagePath();
a62d533 167
		String imageName = new File(imagePath.replace('\\', '/')).getName();
a62d533 168
		try {
a62d533 169
			FileHandle file;
a62d533 170
			if (imagePath.equals("particle.png"))
a62d533 171
				file = Gdx.files.classpath(imagePath);
a62d533 172
			else
a62d533 173
				file = Gdx.files.absolute(imagePath);
a62d533 174
			emitter.setSprite(new Sprite(new Texture(file)));
a62d533 175
		} catch (GdxRuntimeException ex) {
a62d533 176
			ex.printStackTrace();
a62d533 177
			EventQueue.invokeLater(new Runnable() {
a62d533 178
				public void run () {
a62d533 179
					JOptionPane.showMessageDialog(ParticleRenderer.this.particlePanel, "Error loading image:\n" + imagePath);
a62d533 180
				}
a62d533 181
			});
a62d533 182
			emitter.setImagePath(null);
a62d533 183
		}
a62d533 184
	}
a62d533 185
a62d533 186
	public boolean keyDown (int keycode) {
a62d533 187
		return false;
a62d533 188
	}
a62d533 189
a62d533 190
	public boolean keyUp (int keycode) {
a62d533 191
		return false;
a62d533 192
	}
a62d533 193
a62d533 194
	public boolean keyTyped (char character) {
a62d533 195
		return false;
a62d533 196
	}
a62d533 197
a62d533 198
	public boolean touchDown (int x, int y, int pointer, int newParam) {
a62d533 199
		Vector3 touchPoint = new Vector3(x, y, 0);
a62d533 200
		this.particlePanel.worldCamera.unproject(touchPoint);
a62d533 201
		this.particlePanel.effect.setPosition(touchPoint.x, touchPoint.y);
a62d533 202
		return false;
a62d533 203
	}
a62d533 204
a62d533 205
	public boolean touchUp (int x, int y, int pointer, int button) {
a62d533 206
		//ParticleEditor.this.dispatchEvent(new WindowEvent(ParticleEditor.this, WindowEvent.WINDOW_LOST_FOCUS));
a62d533 207
		//ParticleEditor.this.dispatchEvent(new WindowEvent(ParticleEditor.this, WindowEvent.WINDOW_GAINED_FOCUS));
a62d533 208
		this.particlePanel.requestFocusInWindow();
a62d533 209
		return false;
a62d533 210
	}
a62d533 211
a62d533 212
	public boolean touchDragged (int x, int y, int pointer) {
a62d533 213
		Vector3 touchPoint = new Vector3(x, y, 0);
a62d533 214
		this.particlePanel.worldCamera.unproject(touchPoint);
a62d533 215
		this.particlePanel.effect.setPosition(touchPoint.x, touchPoint.y);
a62d533 216
		return false;
a62d533 217
	}
a62d533 218
a62d533 219
	@Override
a62d533 220
	public void dispose () {
a62d533 221
	}
a62d533 222
a62d533 223
	@Override
a62d533 224
	public void pause () {
a62d533 225
	}
a62d533 226
a62d533 227
	@Override
a62d533 228
	public void resume () {
a62d533 229
	}
a62d533 230
a62d533 231
	@Override
a62d533 232
	public boolean mouseMoved (int x, int y) {
a62d533 233
		return false;
a62d533 234
	}
a62d533 235
a62d533 236
	@Override
a62d533 237
	public boolean scrolled (int amount) {
a62d533 238
		return false;
a62d533 239
	}
a62d533 240
}