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/EffectPanel.java
a62d533 1
/*******************************************************************************
a62d533 2
 * Copyright 2011 See AUTHORS file.
a62d533 3
 * 
a62d533 4
 * Licensed under the Apache License, Version 2.0 (the "License");
a62d533 5
 * you may not use this file except in compliance with the License.
a62d533 6
 * You may obtain a copy of the License at
a62d533 7
 * 
a62d533 8
 *   http://www.apache.org/licenses/LICENSE-2.0
a62d533 9
 * 
a62d533 10
 * Unless required by applicable law or agreed to in writing, software
a62d533 11
 * distributed under the License is distributed on an "AS IS" BASIS,
a62d533 12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
a62d533 13
 * See the License for the specific language governing permissions and
a62d533 14
 * limitations under the License.
a62d533 15
 ******************************************************************************/
a62d533 16
a62d533 17
package com.badlogic.gdx.tools.particleeditor;
a62d533 18
a62d533 19
import java.awt.FileDialog;
a62d533 20
import java.awt.GridBagConstraints;
a62d533 21
import java.awt.GridBagLayout;
a62d533 22
import java.awt.Insets;
a62d533 23
import java.awt.event.ActionEvent;
a62d533 24
import java.awt.event.ActionListener;
a62d533 25
import java.io.File;
a62d533 26
import java.io.FileWriter;
a62d533 27
a62d533 28
import javax.swing.JButton;
a62d533 29
import javax.swing.JOptionPane;
a62d533 30
import javax.swing.JPanel;
a62d533 31
import javax.swing.JScrollPane;
a62d533 32
import javax.swing.JSeparator;
a62d533 33
import javax.swing.JTable;
a62d533 34
import javax.swing.ListSelectionModel;
a62d533 35
import javax.swing.event.ListSelectionEvent;
a62d533 36
import javax.swing.event.ListSelectionListener;
a62d533 37
import javax.swing.event.TableModelEvent;
a62d533 38
import javax.swing.event.TableModelListener;
a62d533 39
import javax.swing.table.DefaultTableModel;
a62d533 40
a62d533 41
import com.badlogic.gdx.Gdx;
a62d533 42
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
a62d533 43
import com.badlogic.gdx.graphics.g2d.ParticleEmitter;
a62d533 44
import com.badlogic.gdx.utils.Array;
a62d533 45
a62d533 46
import java.awt.Frame;
a62d533 47
a62d533 48
class EffectPanel extends JPanel {
a62d533 49
	ParticlePanel editor;
a62d533 50
	JTable emitterTable;
a62d533 51
	DefaultTableModel emitterTableModel;
a62d533 52
	int editIndex;
a62d533 53
	String lastDir;
a62d533 54
a62d533 55
	public EffectPanel (ParticlePanel editor) {
a62d533 56
		this.editor = editor;
a62d533 57
		initializeComponents();
a62d533 58
	}
a62d533 59
a62d533 60
	public ParticleEmitter newEmitter (String name, boolean select) {
a62d533 61
		final ParticleEmitter emitter = new ParticleEmitter();
a62d533 62
a62d533 63
		emitter.getDuration().setLow(1000);
a62d533 64
		emitter.getEmission().setHigh(50);
a62d533 65
		emitter.getLife().setHigh(500);
a62d533 66
		emitter.getScale().setHigh(32, 32);
a62d533 67
a62d533 68
		emitter.getTint().setColors(new float[] {1, 0.12156863f, 0.047058824f});
a62d533 69
		emitter.getTransparency().setHigh(1);
a62d533 70
a62d533 71
		emitter.setFlip(false, true);
a62d533 72
		emitter.setMaxParticleCount(25);
a62d533 73
		emitter.setImagePath("particle.png");
a62d533 74
a62d533 75
		addEmitter(name, select, emitter);
a62d533 76
		return emitter;
a62d533 77
	}
a62d533 78
a62d533 79
	public ParticleEmitter newExampleEmitter (String name, boolean select) {
a62d533 80
		final ParticleEmitter emitter = new ParticleEmitter();
a62d533 81
		
a62d533 82
		emitter.getDuration().setLow(3000);
a62d533 83
		
a62d533 84
		emitter.getEmission().setHigh(250);
a62d533 85
		
a62d533 86
		emitter.getLife().setHigh(500, 1000);
a62d533 87
		emitter.getLife().setTimeline(new float[] {0, 0.66f, 1});
a62d533 88
		emitter.getLife().setScaling(new float[] {1, 1, 0.3f});
a62d533 89
		
a62d533 90
		emitter.getScale().setHigh(32, 32);
a62d533 91
		
a62d533 92
		emitter.getRotation().setLow(1, 360);
a62d533 93
		emitter.getRotation().setHigh(180, 180);
a62d533 94
		emitter.getRotation().setTimeline(new float[] {0, 1});
a62d533 95
		emitter.getRotation().setScaling(new float[] {0, 1});
a62d533 96
		emitter.getRotation().setRelative(true);
a62d533 97
		
a62d533 98
		emitter.getAngle().setHigh(45, 135);
a62d533 99
		emitter.getAngle().setLow(90);
a62d533 100
		emitter.getAngle().setTimeline(new float[] {0, 0.5f, 1});
a62d533 101
		emitter.getAngle().setScaling(new float[] {1, 0, 0});
a62d533 102
		emitter.getAngle().setActive(true);
a62d533 103
		
a62d533 104
		emitter.getVelocity().setHigh(30, 300);
a62d533 105
		emitter.getVelocity().setActive(true);
a62d533 106
		
a62d533 107
		emitter.getTint().setColors(new float[] {1, 0.12156863f, 0.047058824f});
a62d533 108
		
a62d533 109
		emitter.getTransparency().setHigh(1, 1);
a62d533 110
		emitter.getTransparency().setTimeline(new float[] {0, 0.2f, 0.8f, 1});
a62d533 111
		emitter.getTransparency().setScaling(new float[] {0, 1, 0.75f, 0});
a62d533 112
		
a62d533 113
		emitter.setFlip(false, true);
a62d533 114
		emitter.setMaxParticleCount(200);
a62d533 115
		emitter.setImagePath("particle.png");
a62d533 116
		
a62d533 117
		addEmitter(name, select, emitter);
a62d533 118
		return emitter;
a62d533 119
	}
a62d533 120
	
a62d533 121
	private void addEmitter (String name, boolean select, final ParticleEmitter emitter) {
a62d533 122
		Array<ParticleEmitter> emitters = editor.effect.getEmitters();
a62d533 123
		if (emitters.size == 0)
a62d533 124
			emitter.setPosition(editor.worldCamera.viewportWidth / 2, editor.worldCamera.viewportHeight / 2);
a62d533 125
		else {
a62d533 126
			ParticleEmitter p = emitters.get(0);
a62d533 127
			emitter.setPosition(p.getX(), p.getY());
a62d533 128
		}
a62d533 129
		emitters.add(emitter);
a62d533 130
a62d533 131
		emitterTableModel.addRow(new Object[] {name, true});
a62d533 132
		if (select) {
a62d533 133
			editor.reloadRows();
a62d533 134
			int row = emitterTableModel.getRowCount() - 1;
a62d533 135
			emitterTable.getSelectionModel().setSelectionInterval(row, row);
a62d533 136
		}
a62d533 137
	}
a62d533 138
a62d533 139
	void emitterSelected () {
a62d533 140
		int row = emitterTable.getSelectedRow();
a62d533 141
		if (row == -1) {
a62d533 142
			row = editIndex;
a62d533 143
			emitterTable.getSelectionModel().setSelectionInterval(row, row);
a62d533 144
		}
a62d533 145
		if (row == editIndex) return;
a62d533 146
		editIndex = row;
a62d533 147
		editor.reloadRows();
a62d533 148
	}
a62d533 149
a62d533 150
	void openEffect () {
a62d533 151
		FileDialog dialog = new FileDialog((Frame) null, "Open Effect", FileDialog.LOAD);
a62d533 152
		if (lastDir != null) dialog.setDirectory(lastDir);
a62d533 153
		dialog.setVisible(true);
a62d533 154
		final String file = dialog.getFile();
a62d533 155
		final String dir = dialog.getDirectory();
a62d533 156
		if (dir == null || file == null || file.trim().length() == 0) return;
a62d533 157
		lastDir = dir;
a62d533 158
		ParticleEffect effect = new ParticleEffect();
a62d533 159
		try {
a62d533 160
			effect.loadEmitters(Gdx.files.absolute(new File(dir, file).getAbsolutePath()));
a62d533 161
			editor.effect = effect;
a62d533 162
			emitterTableModel.getDataVector().removeAllElements();
a62d533 163
			editor.particleData.clear();
a62d533 164
		} catch (Exception ex) {
a62d533 165
			System.out.println("Error loading effect: " + new File(dir, file).getAbsolutePath());
a62d533 166
			ex.printStackTrace();
a62d533 167
			JOptionPane.showMessageDialog(editor, "Error opening effect.");
a62d533 168
			return;
a62d533 169
		}
a62d533 170
		for (ParticleEmitter emitter : effect.getEmitters()) {
a62d533 171
			emitter.setPosition(editor.worldCamera.viewportWidth / 2, editor.worldCamera.viewportHeight / 2);
a62d533 172
			emitterTableModel.addRow(new Object[] {emitter.getName(), true});
a62d533 173
		}
a62d533 174
		editIndex = 0;
a62d533 175
		emitterTable.getSelectionModel().setSelectionInterval(editIndex, editIndex);
a62d533 176
		editor.reloadRows();
a62d533 177
	}
a62d533 178
a62d533 179
	void saveEffect () {
a62d533 180
		FileDialog dialog = new FileDialog((Frame) null, "Save Effect", FileDialog.SAVE);
a62d533 181
		if (lastDir != null) dialog.setDirectory(lastDir);
a62d533 182
		dialog.setVisible(true);
a62d533 183
		String file = dialog.getFile();
a62d533 184
		String dir = dialog.getDirectory();
a62d533 185
		if (dir == null || file == null || file.trim().length() == 0) return;
a62d533 186
		lastDir = dir;
a62d533 187
		int index = 0;
a62d533 188
		for (ParticleEmitter emitter : editor.effect.getEmitters())
a62d533 189
			emitter.setName((String)emitterTableModel.getValueAt(index++, 0));
a62d533 190
		try {
a62d533 191
			editor.effect.save(new File(dir, file));
a62d533 192
		} catch (Exception ex) {
a62d533 193
			System.out.println("Error saving effect: " + new File(dir, file).getAbsolutePath());
a62d533 194
			ex.printStackTrace();
a62d533 195
			JOptionPane.showMessageDialog(editor, "Error saving effect.");
a62d533 196
		}
a62d533 197
	}
a62d533 198
a62d533 199
	void deleteEmitter () {
a62d533 200
		if (editor.effect.getEmitters().size == 1) return;
a62d533 201
		int row = emitterTable.getSelectedRow();
a62d533 202
		if (row == -1) return;
a62d533 203
		if (row <= editIndex) {
a62d533 204
			int oldEditIndex = editIndex;
a62d533 205
			editIndex = Math.max(0, editIndex - 1);
a62d533 206
			if (oldEditIndex == row) editor.reloadRows();
a62d533 207
		}
a62d533 208
		editor.effect.getEmitters().removeIndex(row);
a62d533 209
		emitterTableModel.removeRow(row);
a62d533 210
		emitterTable.getSelectionModel().setSelectionInterval(editIndex, editIndex);
a62d533 211
	}
a62d533 212
a62d533 213
	void move (int direction) {
a62d533 214
		if (direction < 0 && editIndex == 0) return;
a62d533 215
		Array<ParticleEmitter> emitters = editor.effect.getEmitters();
a62d533 216
		if (direction > 0 && editIndex == emitters.size - 1) return;
a62d533 217
		int insertIndex = editIndex + direction;
a62d533 218
		Object name = emitterTableModel.getValueAt(editIndex, 0);
a62d533 219
		emitterTableModel.removeRow(editIndex);
a62d533 220
		ParticleEmitter emitter = emitters.removeIndex(editIndex);
a62d533 221
		emitterTableModel.insertRow(insertIndex, new Object[] {name});
a62d533 222
		emitters.insert(insertIndex, emitter);
a62d533 223
		editIndex = insertIndex;
a62d533 224
		emitterTable.getSelectionModel().setSelectionInterval(editIndex, editIndex);
a62d533 225
	}
a62d533 226
a62d533 227
	void emitterChecked (int index, boolean checked) {
a62d533 228
		editor.setEnabled(editor.effect.getEmitters().get(index), checked);
a62d533 229
		editor.effect.start();
a62d533 230
	}
a62d533 231
a62d533 232
	private void initializeComponents () {
a62d533 233
		setLayout(new GridBagLayout());
a62d533 234
		{
a62d533 235
			JPanel sideButtons = new JPanel(new GridBagLayout());
a62d533 236
			add(sideButtons, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
a62d533 237
				new Insets(0, 0, 0, 0), 0, 0));
a62d533 238
			{
a62d533 239
				JButton newButton = new JButton("New");
a62d533 240
				sideButtons.add(newButton, new GridBagConstraints(0, -1, 1, 1, 0, 0, GridBagConstraints.CENTER,
a62d533 241
					GridBagConstraints.HORIZONTAL, new Insets(0, 0, 6, 0), 0, 0));
a62d533 242
				newButton.addActionListener(new ActionListener() {
a62d533 243
					public void actionPerformed (ActionEvent event) {
a62d533 244
						newEmitter("Untitled", true);
a62d533 245
					}
a62d533 246
				});
a62d533 247
			}
a62d533 248
			{
a62d533 249
				JButton deleteButton = new JButton("Delete");
a62d533 250
				sideButtons.add(deleteButton, new GridBagConstraints(0, -1, 1, 1, 0, 0, GridBagConstraints.CENTER,
a62d533 251
					GridBagConstraints.HORIZONTAL, new Insets(0, 0, 6, 0), 0, 0));
a62d533 252
				deleteButton.addActionListener(new ActionListener() {
a62d533 253
					public void actionPerformed (ActionEvent event) {
a62d533 254
						deleteEmitter();
a62d533 255
					}
a62d533 256
				});
a62d533 257
			}
a62d533 258
			{
a62d533 259
				sideButtons.add(new JSeparator(JSeparator.HORIZONTAL), new GridBagConstraints(0, -1, 1, 1, 0, 0,
a62d533 260
					GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 6, 0), 0, 0));
a62d533 261
			}
a62d533 262
			{
a62d533 263
				JButton saveButton = new JButton("Save");
a62d533 264
				sideButtons.add(saveButton, new GridBagConstraints(0, -1, 1, 1, 0, 0, GridBagConstraints.CENTER,
a62d533 265
					GridBagConstraints.HORIZONTAL, new Insets(0, 0, 6, 0), 0, 0));
a62d533 266
				saveButton.addActionListener(new ActionListener() {
a62d533 267
					public void actionPerformed (ActionEvent event) {
a62d533 268
						saveEffect();
a62d533 269
					}
a62d533 270
				});
a62d533 271
			}
a62d533 272
			{
a62d533 273
				JButton openButton = new JButton("Open");
a62d533 274
				sideButtons.add(openButton, new GridBagConstraints(0, -1, 1, 1, 0, 0, GridBagConstraints.CENTER,
a62d533 275
					GridBagConstraints.HORIZONTAL, new Insets(0, 0, 6, 0), 0, 0));
a62d533 276
				openButton.addActionListener(new ActionListener() {
a62d533 277
					public void actionPerformed (ActionEvent event) {
a62d533 278
						openEffect();
a62d533 279
					}
a62d533 280
				});
a62d533 281
			}
a62d533 282
			{
a62d533 283
				JButton upButton = new JButton("Up");
a62d533 284
				sideButtons.add(upButton, new GridBagConstraints(0, -1, 1, 1, 0, 1, GridBagConstraints.SOUTH,
a62d533 285
					GridBagConstraints.HORIZONTAL, new Insets(0, 0, 6, 0), 0, 0));
a62d533 286
				upButton.addActionListener(new ActionListener() {
a62d533 287
					public void actionPerformed (ActionEvent event) {
a62d533 288
						move(-1);
a62d533 289
					}
a62d533 290
				});
a62d533 291
			}
a62d533 292
			{
a62d533 293
				JButton downButton = new JButton("Down");
a62d533 294
				sideButtons.add(downButton, new GridBagConstraints(0, -1, 1, 1, 0, 0, GridBagConstraints.CENTER,
a62d533 295
					GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
a62d533 296
				downButton.addActionListener(new ActionListener() {
a62d533 297
					public void actionPerformed (ActionEvent event) {
a62d533 298
						move(1);
a62d533 299
					}
a62d533 300
				});
a62d533 301
			}
a62d533 302
		}
a62d533 303
		{
a62d533 304
			JScrollPane scroll = new JScrollPane();
a62d533 305
			add(scroll, new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,
a62d533 306
				0, 0, 6), 0, 0));
a62d533 307
			{
a62d533 308
				emitterTable = new JTable() {
a62d533 309
					public Class getColumnClass (int column) {
a62d533 310
						return column == 1 ? Boolean.class : super.getColumnClass(column);
a62d533 311
					}
a62d533 312
				};
a62d533 313
				emitterTable.getTableHeader().setReorderingAllowed(false);
a62d533 314
				emitterTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
a62d533 315
				scroll.setViewportView(emitterTable);
a62d533 316
				emitterTableModel = new DefaultTableModel(new String[0][0], new String[] {"Emitter", ""});
a62d533 317
				emitterTable.setModel(emitterTableModel);
a62d533 318
				emitterTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
a62d533 319
					public void valueChanged (ListSelectionEvent event) {
a62d533 320
						if (event.getValueIsAdjusting()) return;
a62d533 321
						emitterSelected();
a62d533 322
					}
a62d533 323
				});
a62d533 324
				emitterTableModel.addTableModelListener(new TableModelListener() {
a62d533 325
					public void tableChanged (TableModelEvent event) {
a62d533 326
						if (event.getColumn() != 1) return;
a62d533 327
						emitterChecked(event.getFirstRow(), (Boolean)emitterTable.getValueAt(event.getFirstRow(), 1));
a62d533 328
					}
a62d533 329
				});
a62d533 330
			}
a62d533 331
		}
a62d533 332
	}
a62d533 333
}