Home | History | Annotate | Download | only in particleeditor
      1 /*******************************************************************************
      2  * Copyright 2011 See AUTHORS file.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *   http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  ******************************************************************************/
     16 
     17 package com.badlogic.gdx.tools.particleeditor;
     18 
     19 import java.awt.Cursor;
     20 import java.awt.Font;
     21 import java.awt.GridBagConstraints;
     22 import java.awt.GridBagLayout;
     23 import java.awt.Insets;
     24 import java.awt.event.ActionEvent;
     25 import java.awt.event.ActionListener;
     26 import java.awt.event.MouseAdapter;
     27 import java.awt.event.MouseEvent;
     28 
     29 import javax.swing.JLabel;
     30 import javax.swing.JPanel;
     31 import javax.swing.JToggleButton;
     32 
     33 import com.badlogic.gdx.graphics.g2d.ParticleEmitter.ParticleValue;
     34 
     35 class EditorPanel extends JPanel {
     36 	private final String name;
     37 	private final String description;
     38 	private final ParticleValue value;
     39 	private JPanel titlePanel;
     40 	JToggleButton activeButton;
     41 	private JPanel contentPanel;
     42 	JToggleButton advancedButton;
     43 	JPanel advancedPanel;
     44 	private boolean hasAdvanced;
     45 	JLabel descriptionLabel;
     46 
     47 	public EditorPanel (ParticleValue value, String name, String description) {
     48 		this.name = name;
     49 		this.value = value;
     50 		this.description = description;
     51 
     52 		initializeComponents();
     53 
     54 		titlePanel.addMouseListener(new MouseAdapter() {
     55 			public void mouseClicked (MouseEvent event) {
     56 				if (!activeButton.isVisible()) return;
     57 				activeButton.setSelected(!activeButton.isSelected());
     58 				updateActive();
     59 			}
     60 		});
     61 		activeButton.addActionListener(new ActionListener() {
     62 			public void actionPerformed (ActionEvent event) {
     63 				updateActive();
     64 			}
     65 		});
     66 		advancedButton.addActionListener(new ActionListener() {
     67 			public void actionPerformed (ActionEvent event) {
     68 				advancedPanel.setVisible(advancedButton.isSelected());
     69 			}
     70 		});
     71 
     72 		if (value != null) {
     73 			activeButton.setSelected(value.isActive());
     74 			updateActive();
     75 		}
     76 
     77 		boolean alwaysActive = value == null ? true : value.isAlwaysActive();
     78 		activeButton.setVisible(!alwaysActive);
     79 		if (alwaysActive) contentPanel.setVisible(true);
     80 		if (alwaysActive) titlePanel.setCursor(null);
     81 	}
     82 
     83 	void updateActive () {
     84 		contentPanel.setVisible(activeButton.isSelected());
     85 		advancedPanel.setVisible(activeButton.isSelected() && advancedButton.isSelected());
     86 		advancedButton.setVisible(activeButton.isSelected() && hasAdvanced);
     87 		descriptionLabel.setText(activeButton.isSelected() ? description : "");
     88 		if (value != null) value.setActive(activeButton.isSelected());
     89 	}
     90 
     91 	public void update (ParticleEditor editor) {
     92 	}
     93 
     94 	public void setHasAdvanced (boolean hasAdvanced) {
     95 		this.hasAdvanced = hasAdvanced;
     96 		advancedButton.setVisible(hasAdvanced && (value.isActive() || value.isAlwaysActive()));
     97 	}
     98 
     99 	public JPanel getContentPanel () {
    100 		return contentPanel;
    101 	}
    102 
    103 	public JPanel getAdvancedPanel () {
    104 		return advancedPanel;
    105 	}
    106 
    107 	public String getName () {
    108 		return name;
    109 	}
    110 
    111 	public void setEmbedded () {
    112 		GridBagLayout layout = (GridBagLayout)getLayout();
    113 		GridBagConstraints constraints = layout.getConstraints(contentPanel);
    114 		constraints.insets = new Insets(0, 0, 0, 0);
    115 		layout.setConstraints(contentPanel, constraints);
    116 
    117 		titlePanel.setVisible(false);
    118 	}
    119 
    120 	private void initializeComponents () {
    121 		setLayout(new GridBagLayout());
    122 		{
    123 			titlePanel = new JPanel(new GridBagLayout());
    124 			add(titlePanel, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
    125 				new Insets(3, 0, 3, 0), 0, 0));
    126 			titlePanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    127 			{
    128 				JLabel label = new JLabel(name);
    129 				titlePanel.add(label, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
    130 					new Insets(3, 6, 3, 6), 0, 0));
    131 				label.setFont(label.getFont().deriveFont(Font.BOLD));
    132 			}
    133 			{
    134 				descriptionLabel = new JLabel(description);
    135 				titlePanel.add(descriptionLabel, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST,
    136 					GridBagConstraints.NONE, new Insets(3, 6, 3, 6), 0, 0));
    137 			}
    138 			{
    139 				advancedButton = new JToggleButton("Advanced");
    140 				titlePanel.add(advancedButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
    141 					GridBagConstraints.NONE, new Insets(0, 0, 0, 6), 0, 0));
    142 				advancedButton.setVisible(false);
    143 			}
    144 			{
    145 				activeButton = new JToggleButton("Active");
    146 				titlePanel.add(activeButton, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
    147 					GridBagConstraints.NONE, new Insets(0, 0, 0, 6), 0, 0));
    148 			}
    149 		}
    150 		{
    151 			contentPanel = new JPanel(new GridBagLayout());
    152 			add(contentPanel, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    153 				new Insets(0, 6, 6, 6), 0, 0));
    154 			contentPanel.setVisible(false);
    155 		}
    156 		{
    157 			advancedPanel = new JPanel(new GridBagLayout());
    158 			add(advancedPanel, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    159 				new Insets(0, 6, 6, 6), 0, 0));
    160 			advancedPanel.setVisible(false);
    161 		}
    162 	}
    163 }
    164