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/GradientPanel.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.Color;
a62d533 20
import java.awt.Dimension;
a62d533 21
import java.awt.GradientPaint;
a62d533 22
import java.awt.Graphics;
a62d533 23
import java.awt.Graphics2D;
a62d533 24
import java.awt.GridBagConstraints;
a62d533 25
import java.awt.GridBagLayout;
a62d533 26
import java.awt.Insets;
a62d533 27
import java.awt.event.MouseAdapter;
a62d533 28
import java.awt.event.MouseEvent;
a62d533 29
import java.awt.event.MouseMotionAdapter;
a62d533 30
import java.util.ArrayList;
a62d533 31
a62d533 32
import javax.swing.BorderFactory;
a62d533 33
import javax.swing.JColorChooser;
a62d533 34
import javax.swing.JPanel;
a62d533 35
import javax.swing.JSlider;
a62d533 36
import javax.swing.event.ChangeEvent;
a62d533 37
import javax.swing.event.ChangeListener;
a62d533 38
a62d533 39
import com.badlogic.gdx.graphics.g2d.ParticleEmitter.GradientColorValue;
a62d533 40
a62d533 41
class GradientPanel extends EditorPanel {
a62d533 42
	private final GradientColorValue value;
a62d533 43
	private GradientEditor gradientEditor;
a62d533 44
	ColorSlider saturationSlider, lightnessSlider;
a62d533 45
	JPanel colorPanel;
a62d533 46
	private ColorSlider hueSlider;
a62d533 47
a62d533 48
	public GradientPanel (GradientColorValue value, String name, String description) {
a62d533 49
		super(value, name, description);
a62d533 50
		this.value = value;
a62d533 51
a62d533 52
		initializeComponents();
a62d533 53
a62d533 54
		gradientEditor.percentages.clear();
a62d533 55
		for (float percent : value.getTimeline())
a62d533 56
			gradientEditor.percentages.add(percent);
a62d533 57
a62d533 58
		gradientEditor.colors.clear();
a62d533 59
		float[] colors = value.getColors();
a62d533 60
		for (int i = 0; i < colors.length;) {
a62d533 61
			float r = colors[i++];
a62d533 62
			float g = colors[i++];
a62d533 63
			float b = colors[i++];
a62d533 64
			gradientEditor.colors.add(new Color(r, g, b));
a62d533 65
		}
a62d533 66
		if (gradientEditor.colors.isEmpty() || gradientEditor.percentages.isEmpty()) {
a62d533 67
			gradientEditor.percentages.clear();
a62d533 68
			gradientEditor.percentages.add(0f);
a62d533 69
			gradientEditor.percentages.add(1f);
a62d533 70
			gradientEditor.colors.clear();
a62d533 71
			gradientEditor.colors.add(Color.white);
a62d533 72
		}
a62d533 73
		setColor(gradientEditor.colors.get(0));
a62d533 74
	}
a62d533 75
a62d533 76
	public Dimension getPreferredSize () {
a62d533 77
		Dimension size = super.getPreferredSize();
a62d533 78
		size.width = 10;
a62d533 79
		return size;
a62d533 80
	}
a62d533 81
a62d533 82
	private void initializeComponents () {
a62d533 83
		JPanel contentPanel = getContentPanel();
a62d533 84
		{
a62d533 85
			gradientEditor = new GradientEditor() {
a62d533 86
				public void handleSelected (Color color) {
a62d533 87
					GradientPanel.this.setColor(color);
a62d533 88
				}
a62d533 89
			};
a62d533 90
			contentPanel.add(gradientEditor, new GridBagConstraints(0, 1, 3, 1, 1.0, 0.0, GridBagConstraints.CENTER,
a62d533 91
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 6, 0), 0, 10));
a62d533 92
		}
a62d533 93
		{
a62d533 94
			hueSlider = new ColorSlider(new Color[] {Color.red, Color.yellow, Color.green, Color.cyan, Color.blue, Color.magenta,
a62d533 95
				Color.red}) {
a62d533 96
				protected void colorPicked () {
a62d533 97
					saturationSlider.setColors(new Color[] {new Color(Color.HSBtoRGB(getPercentage(), 1, 1)), Color.white});
a62d533 98
					updateColor();
a62d533 99
				}
a62d533 100
			};
a62d533 101
			contentPanel.add(hueSlider, new GridBagConstraints(1, 2, 2, 1, 1.0, 0.0, GridBagConstraints.CENTER,
a62d533 102
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 6, 0), 0, 0));
a62d533 103
		}
a62d533 104
		{
a62d533 105
			saturationSlider = new ColorSlider(new Color[] {Color.red, Color.white}) {
a62d533 106
				protected void colorPicked () {
a62d533 107
					updateColor();
a62d533 108
				}
a62d533 109
			};
a62d533 110
			contentPanel.add(saturationSlider, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER,
a62d533 111
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 6), 0, 0));
a62d533 112
		}
a62d533 113
		{
a62d533 114
			lightnessSlider = new ColorSlider(new Color[0]) {
a62d533 115
				protected void colorPicked () {
a62d533 116
					updateColor();
a62d533 117
				}
a62d533 118
			};
a62d533 119
			contentPanel.add(lightnessSlider, new GridBagConstraints(2, 3, 1, 1, 1, 0.0, GridBagConstraints.CENTER,
a62d533 120
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
a62d533 121
		}
a62d533 122
		{
a62d533 123
			colorPanel = new JPanel() {
a62d533 124
				public Dimension getPreferredSize () {
a62d533 125
					Dimension size = super.getPreferredSize();
a62d533 126
					size.width = 52;
a62d533 127
					return size;
a62d533 128
				}
a62d533 129
			};
a62d533 130
			contentPanel.add(colorPanel, new GridBagConstraints(0, 2, 1, 2, 0.0, 0.0, GridBagConstraints.CENTER,
a62d533 131
				GridBagConstraints.BOTH, new Insets(3, 0, 0, 6), 0, 0));
a62d533 132
		}
a62d533 133
a62d533 134
		colorPanel.addMouseListener(new MouseAdapter() {
a62d533 135
			public void mouseClicked (MouseEvent e) {
a62d533 136
				Color color = JColorChooser.showDialog(colorPanel, "Set Color", colorPanel.getBackground());
a62d533 137
				if (color != null) setColor(color);
a62d533 138
			}
a62d533 139
		});
a62d533 140
		colorPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black));
a62d533 141
	}
a62d533 142
a62d533 143
	public void setColor (Color color) {
a62d533 144
		float[] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
a62d533 145
		hueSlider.setPercentage(hsb[0]);
a62d533 146
		saturationSlider.setPercentage(1 - hsb[1]);
a62d533 147
		lightnessSlider.setPercentage(1 - hsb[2]);
a62d533 148
		colorPanel.setBackground(color);
a62d533 149
	}
a62d533 150
a62d533 151
	void updateColor () {
a62d533 152
		Color color = new Color(Color.HSBtoRGB(hueSlider.getPercentage(), 1 - saturationSlider.getPercentage(), 1));
a62d533 153
		lightnessSlider.setColors(new Color[] {color, Color.black});
a62d533 154
		color = new Color(Color.HSBtoRGB(hueSlider.getPercentage(), 1 - saturationSlider.getPercentage(),
a62d533 155
			1 - lightnessSlider.getPercentage()));
a62d533 156
		colorPanel.setBackground(color);
a62d533 157
		gradientEditor.setColor(color);
a62d533 158
a62d533 159
		float[] colors = new float[gradientEditor.colors.size() * 3];
a62d533 160
		int i = 0;
a62d533 161
		for (Color c : gradientEditor.colors) {
a62d533 162
			colors[i++] = c.getRed() / 255f;
a62d533 163
			colors[i++] = c.getGreen() / 255f;
a62d533 164
			colors[i++] = c.getBlue() / 255f;
a62d533 165
		}
a62d533 166
		float[] percentages = new float[gradientEditor.percentages.size()];
a62d533 167
		i = 0;
a62d533 168
		for (Float percent : gradientEditor.percentages)
a62d533 169
			percentages[i++] = percent;
a62d533 170
		value.setColors(colors);
a62d533 171
		value.setTimeline(percentages);
a62d533 172
	}
a62d533 173
a62d533 174
	public class GradientEditor extends JPanel {
a62d533 175
		ArrayList<Color> colors = new ArrayList();
a62d533 176
		ArrayList<Float> percentages = new ArrayList();
a62d533 177
a62d533 178
		int handleWidth = 12;
a62d533 179
		int handleHeight = 12;
a62d533 180
		int gradientX = handleWidth / 2;
a62d533 181
		int gradientY = 0;
a62d533 182
		int gradientWidth;
a62d533 183
		int gradientHeight;
a62d533 184
		int dragIndex = -1;
a62d533 185
		int selectedIndex;
a62d533 186
a62d533 187
		public GradientEditor () {
a62d533 188
			setPreferredSize(new Dimension(100, 30));
a62d533 189
a62d533 190
			addMouseListener(new MouseAdapter() {
a62d533 191
				public void mousePressed (MouseEvent event) {
a62d533 192
					dragIndex = -1;
a62d533 193
					int mouseX = event.getX();
a62d533 194
					int mouseY = event.getY();
a62d533 195
					int y = gradientY + gradientHeight;
a62d533 196
					for (int i = 0, n = colors.size(); i < n; i++) {
a62d533 197
						int x = gradientX + (int)(percentages.get(i) * gradientWidth) - handleWidth / 2;
a62d533 198
						if (mouseX >= x && mouseX <= x + handleWidth && mouseY >= gradientY && mouseY <= y + handleHeight) {
a62d533 199
							dragIndex = selectedIndex = i;
a62d533 200
							handleSelected(colors.get(selectedIndex));
a62d533 201
							repaint();
a62d533 202
							break;
a62d533 203
						}
a62d533 204
					}
a62d533 205
				}
a62d533 206
a62d533 207
				public void mouseReleased (MouseEvent event) {
a62d533 208
					if (dragIndex != -1) {
a62d533 209
						dragIndex = -1;
a62d533 210
						repaint();
a62d533 211
					}
a62d533 212
				}
a62d533 213
a62d533 214
				public void mouseClicked (MouseEvent event) {
a62d533 215
					int mouseX = event.getX();
a62d533 216
					int mouseY = event.getY();
a62d533 217
					if (event.getClickCount() == 2) {
a62d533 218
						if (percentages.size() <= 1) return;
a62d533 219
						if (selectedIndex == -1 || selectedIndex == 0) return;
a62d533 220
						int y = gradientY + gradientHeight;
a62d533 221
						int x = gradientX + (int)(percentages.get(selectedIndex) * gradientWidth) - handleWidth / 2;
a62d533 222
						if (mouseX >= x && mouseX <= x + handleWidth && mouseY >= gradientY && mouseY <= y + handleHeight) {
a62d533 223
							percentages.remove(selectedIndex);
a62d533 224
							colors.remove(selectedIndex);
a62d533 225
							selectedIndex--;
a62d533 226
							dragIndex = selectedIndex;
a62d533 227
							if (percentages.size() == 2) percentages.set(1, 1f);
a62d533 228
							handleSelected(colors.get(selectedIndex));
a62d533 229
							repaint();
a62d533 230
						}
a62d533 231
						return;
a62d533 232
					}
a62d533 233
					if (mouseX < gradientX || mouseX > gradientX + gradientWidth) return;
a62d533 234
					if (mouseY < gradientY || mouseY > gradientY + gradientHeight) return;
a62d533 235
					float percent = (event.getX() - gradientX) / (float)gradientWidth;
a62d533 236
					if (percentages.size() == 1) percent = 1f;
a62d533 237
					for (int i = 0, n = percentages.size(); i <= n; i++) {
a62d533 238
						if (i == n || percent < percentages.get(i)) {
a62d533 239
							percentages.add(i, percent);
a62d533 240
							colors.add(i, colors.get(i - 1));
a62d533 241
							dragIndex = selectedIndex = i;
a62d533 242
							handleSelected(colors.get(selectedIndex));
a62d533 243
							repaint();
a62d533 244
							break;
a62d533 245
						}
a62d533 246
					}
a62d533 247
				}
a62d533 248
			});
a62d533 249
			addMouseMotionListener(new MouseMotionAdapter() {
a62d533 250
				public void mouseDragged (MouseEvent event) {
a62d533 251
					if (dragIndex == -1 || dragIndex == 0 || dragIndex == percentages.size() - 1) return;
a62d533 252
					float percent = (event.getX() - gradientX) / (float)gradientWidth;
a62d533 253
					percent = Math.max(percent, percentages.get(dragIndex - 1) + 0.01f);
a62d533 254
					percent = Math.min(percent, percentages.get(dragIndex + 1) - 0.01f);
a62d533 255
					percentages.set(dragIndex, percent);
a62d533 256
					repaint();
a62d533 257
				}
a62d533 258
			});
a62d533 259
		}
a62d533 260
a62d533 261
		public void setColor (Color color) {
a62d533 262
			if (selectedIndex == -1) return;
a62d533 263
			colors.set(selectedIndex, color);
a62d533 264
			repaint();
a62d533 265
		}
a62d533 266
a62d533 267
		public void handleSelected (Color color) {
a62d533 268
		}
a62d533 269
a62d533 270
		protected void paintComponent (Graphics graphics) {
a62d533 271
			super.paintComponent(graphics);
a62d533 272
			Graphics2D g = (Graphics2D)graphics;
a62d533 273
			int width = getWidth() - 1;
a62d533 274
			int height = getHeight();
a62d533 275
a62d533 276
			gradientWidth = width - handleWidth;
a62d533 277
			gradientHeight = height - 16;
a62d533 278
a62d533 279
			g.translate(gradientX, gradientY);
a62d533 280
			for (int i = 0, n = colors.size() == 1 ? 1 : colors.size() - 1; i < n; i++) {
a62d533 281
				Color color1 = colors.get(i);
a62d533 282
				Color color2 = colors.size() == 1 ? color1 : colors.get(i + 1);
a62d533 283
				float percent1 = percentages.get(i);
a62d533 284
				float percent2 = colors.size() == 1 ? 1 : percentages.get(i + 1);
a62d533 285
				int point1 = (int)(percent1 * gradientWidth);
a62d533 286
				int point2 = (int)Math.ceil(percent2 * gradientWidth);
a62d533 287
				g.setPaint(new GradientPaint(point1, 0, color1, point2, 0, color2, false));
a62d533 288
				g.fillRect(point1, 0, point2 - point1, gradientHeight);
a62d533 289
			}
a62d533 290
			g.setPaint(null);
a62d533 291
			g.setColor(Color.black);
a62d533 292
			g.drawRect(0, 0, gradientWidth, gradientHeight);
a62d533 293
a62d533 294
			int y = gradientHeight;
a62d533 295
			int[] yPoints = new int[3];
a62d533 296
			yPoints[0] = y;
a62d533 297
			yPoints[1] = y + handleHeight;
a62d533 298
			yPoints[2] = y + handleHeight;
a62d533 299
			int[] xPoints = new int[3];
a62d533 300
			for (int i = 0, n = colors.size(); i < n; i++) {
a62d533 301
				int x = (int)(percentages.get(i) * gradientWidth);
a62d533 302
				xPoints[0] = x;
a62d533 303
				xPoints[1] = x - handleWidth / 2;
a62d533 304
				xPoints[2] = x + handleWidth / 2;
a62d533 305
				if (i == selectedIndex) {
a62d533 306
					g.setColor(colors.get(i));
a62d533 307
					g.fillPolygon(xPoints, yPoints, 3);
a62d533 308
					g.fillRect(xPoints[1], yPoints[1] + 2, handleWidth + 1, 2);
a62d533 309
					g.setColor(Color.black);
a62d533 310
				}
a62d533 311
				g.drawPolygon(xPoints, yPoints, 3);
a62d533 312
			}
a62d533 313
			g.translate(-gradientX, -gradientY);
a62d533 314
		}
a62d533 315
	}
a62d533 316
a62d533 317
	static public class ColorSlider extends JPanel {
a62d533 318
		Color[] paletteColors;
a62d533 319
		JSlider slider;
a62d533 320
		private ColorPicker colorPicker;
a62d533 321
a62d533 322
		public ColorSlider (Color[] paletteColors) {
a62d533 323
			this.paletteColors = paletteColors;
a62d533 324
			setLayout(new GridBagLayout());
a62d533 325
			{
a62d533 326
				slider = new JSlider(0, 1000, 0);
a62d533 327
				slider.setPaintTrack(false);
a62d533 328
				add(slider, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
a62d533 329
					new Insets(0, 6, 0, 6), 0, 0));
a62d533 330
			}
a62d533 331
			{
a62d533 332
				colorPicker = new ColorPicker();
a62d533 333
				add(colorPicker, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
a62d533 334
					GridBagConstraints.HORIZONTAL, new Insets(0, 6, 0, 6), 0, 0));
a62d533 335
			}
a62d533 336
a62d533 337
			slider.addChangeListener(new ChangeListener() {
a62d533 338
				public void stateChanged (ChangeEvent event) {
a62d533 339
					colorPicked();
a62d533 340
				}
a62d533 341
			});
a62d533 342
		}
a62d533 343
a62d533 344
		public Dimension getPreferredSize () {
a62d533 345
			Dimension size = super.getPreferredSize();
a62d533 346
			size.width = 10;
a62d533 347
			return size;
a62d533 348
		}
a62d533 349
a62d533 350
		public void setPercentage (float percent) {
a62d533 351
			slider.setValue((int)(1000 * percent));
a62d533 352
		}
a62d533 353
a62d533 354
		public float getPercentage () {
a62d533 355
			return slider.getValue() / 1000f;
a62d533 356
		}
a62d533 357
a62d533 358
		protected void colorPicked () {
a62d533 359
		}
a62d533 360
a62d533 361
		public void setColors (Color[] colors) {
a62d533 362
			paletteColors = colors;
a62d533 363
			repaint();
a62d533 364
		}
a62d533 365
a62d533 366
		public class ColorPicker extends JPanel {
a62d533 367
			public ColorPicker () {
a62d533 368
				addMouseListener(new MouseAdapter() {
a62d533 369
					public void mouseClicked (MouseEvent event) {
a62d533 370
						slider.setValue((int)(event.getX() / (float)getWidth() * 1000));
a62d533 371
					}
a62d533 372
				});
a62d533 373
			}
a62d533 374
a62d533 375
			protected void paintComponent (Graphics graphics) {
a62d533 376
				Graphics2D g = (Graphics2D)graphics;
a62d533 377
				int width = getWidth() - 1;
a62d533 378
				int height = getHeight() - 1;
a62d533 379
				for (int i = 0, n = paletteColors.length - 1; i < n; i++) {
a62d533 380
					Color color1 = paletteColors[i];
a62d533 381
					Color color2 = paletteColors[i + 1];
a62d533 382
					float point1 = i / (float)n * width;
a62d533 383
					float point2 = (i + 1) / (float)n * width;
a62d533 384
					g.setPaint(new GradientPaint(point1, 0, color1, point2, 0, color2, false));
a62d533 385
					g.fillRect((int)point1, 0, (int)Math.ceil(point2 - point1), height);
a62d533 386
				}
a62d533 387
				g.setPaint(null);
a62d533 388
				g.setColor(Color.black);
a62d533 389
				g.drawRect(0, 0, width, height);
a62d533 390
			}
a62d533 391
		}
a62d533 392
	}
a62d533 393
}