Home | History | Annotate | Download | only in flame
      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.flame;
     18 import java.awt.Dimension;
     19 import java.awt.GridBagConstraints;
     20 import java.awt.GridBagLayout;
     21 import java.awt.Insets;
     22 import java.awt.event.ActionEvent;
     23 import java.awt.event.ActionListener;
     24 
     25 import javax.swing.BorderFactory;
     26 import javax.swing.JButton;
     27 import javax.swing.JCheckBox;
     28 import javax.swing.JLabel;
     29 import javax.swing.JPanel;
     30 import javax.swing.event.ChangeEvent;
     31 import javax.swing.event.ChangeListener;
     32 
     33 import com.badlogic.gdx.graphics.g3d.particles.values.ScaledNumericValue;
     34 import com.badlogic.gdx.tools.particleeditor.Chart;
     35 
     36 /** @author Inferno */
     37 class ScaledNumericPanel extends ParticleValuePanel<ScaledNumericValue> {
     38 	Slider lowMinSlider, lowMaxSlider;
     39 	Slider highMinSlider, highMaxSlider;
     40 	JCheckBox relativeCheckBox;
     41 	Chart chart;
     42 	JPanel formPanel;
     43 	JButton expandButton;
     44 	JButton lowRangeButton;
     45 	JButton highRangeButton;
     46 
     47 	public ScaledNumericPanel (FlameMain editor, ScaledNumericValue value, String chartTitle, String name, String description){
     48 		this(editor, value, chartTitle, name, description, true);
     49 	}
     50 
     51 	public ScaledNumericPanel (FlameMain editor, ScaledNumericValue value,
     52 												String chartTitle, String name, String description, boolean isAlwaysActive){
     53 		super(editor, name, description, isAlwaysActive);
     54 		initializeComponents(chartTitle);
     55 		setValue(value);
     56 	}
     57 
     58 	public JPanel getFormPanel () {
     59 		return formPanel;
     60 	}
     61 
     62 	private void initializeComponents (String chartTitle) {
     63 		JPanel contentPanel = getContentPanel();
     64 		{
     65 			formPanel = new JPanel(new GridBagLayout());
     66 			contentPanel.add(formPanel, new GridBagConstraints(5, 5, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE,
     67 				new Insets(0, 0, 0, 6), 0, 0));
     68 			{
     69 				JLabel label = new JLabel("High:");
     70 				formPanel.add(label, new GridBagConstraints(2, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE,
     71 					new Insets(0, 0, 0, 6), 0, 0));
     72 			}
     73 			{
     74 				highMinSlider = new Slider(0, -999999, 999999, 1f);
     75 				formPanel.add(highMinSlider, new GridBagConstraints(3, 1, 1, 1, 0, 0, GridBagConstraints.WEST,
     76 					GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
     77 			}
     78 			{
     79 				highMaxSlider = new Slider(0, -999999, 999999, 1f);
     80 				formPanel.add(highMaxSlider, new GridBagConstraints(4, 1, 1, 1, 0, 0, GridBagConstraints.WEST,
     81 					GridBagConstraints.NONE, new Insets(0, 6, 0, 0), 0, 0));
     82 			}
     83 			{
     84 				highRangeButton = new JButton("<");
     85 				highRangeButton.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
     86 				formPanel.add(highRangeButton, new GridBagConstraints(5, 1, 1, 1, 0.0, 0, GridBagConstraints.WEST,
     87 					GridBagConstraints.NONE, new Insets(0, 1, 0, 0), 0, 0));
     88 			}
     89 			{
     90 				JLabel label = new JLabel("Low:");
     91 				formPanel.add(label, new GridBagConstraints(2, 2, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE,
     92 					new Insets(0, 0, 0, 6), 0, 0));
     93 			}
     94 			{
     95 				lowMinSlider = new Slider(0, -999999, 999999, 1f);
     96 				formPanel.add(lowMinSlider, new GridBagConstraints(3, 2, 1, 1, 0, 0, GridBagConstraints.WEST,
     97 					GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
     98 			}
     99 			{
    100 				lowMaxSlider = new Slider(0, -999999, 999999, 1f);
    101 				formPanel.add(lowMaxSlider, new GridBagConstraints(4, 2, 1, 1, 0, 0, GridBagConstraints.WEST,
    102 					GridBagConstraints.NONE, new Insets(0, 6, 0, 0), 0, 0));
    103 			}
    104 			{
    105 				lowRangeButton = new JButton("<");
    106 				lowRangeButton.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
    107 				formPanel.add(lowRangeButton, new GridBagConstraints(5, 2, 1, 1, 0.0, 0, GridBagConstraints.WEST,
    108 					GridBagConstraints.NONE, new Insets(0, 1, 0, 0), 0, 0));
    109 			}
    110 		}
    111 		{
    112 			chart = new Chart(chartTitle) {
    113 				public void pointsChanged () {
    114 					value.setTimeline(chart.getValuesX());
    115 					value.setScaling(chart.getValuesY());
    116 				}
    117 			};
    118 			contentPanel.add(chart, new GridBagConstraints(6, 5, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
    119 				new Insets(0, 0, 0, 0), 0, 0));
    120 			chart.setPreferredSize(new Dimension(150, 30));
    121 		}
    122 		{
    123 			expandButton = new JButton("+");
    124 			contentPanel.add(expandButton, new GridBagConstraints(7, 5, 1, 1, 1, 0, GridBagConstraints.SOUTHWEST,
    125 				GridBagConstraints.NONE, new Insets(0, 5, 0, 0), 0, 0));
    126 			expandButton.setBorder(BorderFactory.createEmptyBorder(4, 8, 4, 8));
    127 		}
    128 		{
    129 			relativeCheckBox = new JCheckBox("Relative");
    130 			contentPanel.add(relativeCheckBox, new GridBagConstraints(7, 5, 1, 1, 0, 0, GridBagConstraints.NORTHWEST,
    131 				GridBagConstraints.NONE, new Insets(0, 6, 0, 0), 0, 0));
    132 		}
    133 
    134 		lowMinSlider.addChangeListener(new ChangeListener() {
    135 			public void stateChanged (ChangeEvent event) {
    136 				ScaledNumericPanel.this.value.setLowMin(lowMinSlider.getValue());
    137 				if (!lowMaxSlider.isVisible()) ScaledNumericPanel.this.value.setLowMax(lowMinSlider.getValue());
    138 			}
    139 		});
    140 		lowMaxSlider.addChangeListener(new ChangeListener() {
    141 			public void stateChanged (ChangeEvent event) {
    142 				ScaledNumericPanel.this.value.setLowMax(lowMaxSlider.getValue());
    143 			}
    144 		});
    145 		highMinSlider.addChangeListener(new ChangeListener() {
    146 			public void stateChanged (ChangeEvent event) {
    147 				ScaledNumericPanel.this.value.setHighMin(highMinSlider.getValue());
    148 				if (!highMaxSlider.isVisible()) ScaledNumericPanel.this.value.setHighMax(highMinSlider.getValue());
    149 			}
    150 		});
    151 		highMaxSlider.addChangeListener(new ChangeListener() {
    152 			public void stateChanged (ChangeEvent event) {
    153 				ScaledNumericPanel.this.value.setHighMax(highMaxSlider.getValue());
    154 			}
    155 		});
    156 
    157 		relativeCheckBox.addActionListener(new ActionListener() {
    158 			public void actionPerformed (ActionEvent event) {
    159 				ScaledNumericPanel.this.value.setRelative(relativeCheckBox.isSelected());
    160 			}
    161 		});
    162 
    163 		lowRangeButton.addActionListener(new ActionListener() {
    164 			public void actionPerformed (ActionEvent event) {
    165 				boolean visible = !lowMaxSlider.isVisible();
    166 				lowMaxSlider.setVisible(visible);
    167 				lowRangeButton.setText(visible ? "<" : ">");
    168 				GridBagLayout layout = (GridBagLayout)formPanel.getLayout();
    169 				GridBagConstraints constraints = layout.getConstraints(lowRangeButton);
    170 				constraints.gridx = visible ? 5 : 4;
    171 				layout.setConstraints(lowRangeButton, constraints);
    172 				Slider slider = visible ? lowMaxSlider : lowMinSlider;
    173 				ScaledNumericPanel.this.value.setLowMax(slider.getValue());
    174 			}
    175 		});
    176 
    177 		highRangeButton.addActionListener(new ActionListener() {
    178 			public void actionPerformed (ActionEvent event) {
    179 				boolean visible = !highMaxSlider.isVisible();
    180 				highMaxSlider.setVisible(visible);
    181 				highRangeButton.setText(visible ? "<" : ">");
    182 				GridBagLayout layout = (GridBagLayout)formPanel.getLayout();
    183 				GridBagConstraints constraints = layout.getConstraints(highRangeButton);
    184 				constraints.gridx = visible ? 5 : 4;
    185 				layout.setConstraints(highRangeButton, constraints);
    186 				Slider slider = visible ? highMaxSlider : highMinSlider;
    187 				ScaledNumericPanel.this.value.setHighMax(slider.getValue());
    188 			}
    189 		});
    190 
    191 		expandButton.addActionListener(new ActionListener() {
    192 			public void actionPerformed (ActionEvent event) {
    193 				chart.setExpanded(!chart.isExpanded());
    194 				boolean expanded = chart.isExpanded();
    195 				GridBagLayout layout = (GridBagLayout)getContentPanel().getLayout();
    196 				GridBagConstraints chartConstraints = layout.getConstraints(chart);
    197 				GridBagConstraints expandButtonConstraints = layout.getConstraints(expandButton);
    198 				if (expanded) {
    199 					chart.setPreferredSize(new Dimension(150, 200));
    200 					expandButton.setText("-");
    201 					chartConstraints.weightx = 1;
    202 					expandButtonConstraints.weightx = 0;
    203 				} else {
    204 					chart.setPreferredSize(new Dimension(150, 30));
    205 					expandButton.setText("+");
    206 					chartConstraints.weightx = 0;
    207 					expandButtonConstraints.weightx = 1;
    208 				}
    209 				layout.setConstraints(chart, chartConstraints);
    210 				layout.setConstraints(expandButton, expandButtonConstraints);
    211 				relativeCheckBox.setVisible(!expanded);
    212 				formPanel.setVisible(!expanded);
    213 				chart.revalidate();
    214 			}
    215 		});
    216 
    217 	}
    218 
    219 	@Override
    220 	public void setValue(ScaledNumericValue value){
    221 		super.setValue(value);
    222 		if(this.value == null)return;
    223 		setValue(lowMinSlider, this.value.getLowMin());
    224 		setValue(lowMaxSlider, this.value.getLowMax());
    225 		setValue(highMinSlider, this.value.getHighMin());
    226 		setValue(highMaxSlider, this.value.getHighMax());
    227 		chart.setValues(this.value.getTimeline(), this.value.getScaling());
    228 		setValue(relativeCheckBox, this.value.isRelative());
    229 
    230 		if (	(this.value.getLowMin() == this.value.getLowMax() && lowMaxSlider.isVisible()) ||
    231 				(this.value.getLowMin() != this.value.getLowMax() && !lowMaxSlider.isVisible()) ) {
    232 			lowRangeButton.doClick(0);
    233 		}
    234 		if ( 	((this.value.getHighMin() == this.value.getHighMax()) && highMaxSlider.isVisible()) ||
    235 				((this.value.getHighMin() != this.value.getHighMax()) && !highMaxSlider.isVisible()) )
    236 			highRangeButton.doClick(0);
    237 	}
    238 
    239 	public Chart getChart(){
    240 		return chart;
    241 	}
    242 
    243 }
    244