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
17package com.badlogic.gdx.tools.flame;
18import java.awt.GridBagConstraints;
19import java.awt.Insets;
20import java.awt.event.ActionEvent;
21import java.awt.event.ActionListener;
22
23import javax.swing.BorderFactory;
24import javax.swing.JButton;
25import javax.swing.JLabel;
26import javax.swing.JPanel;
27import javax.swing.event.ChangeEvent;
28import javax.swing.event.ChangeListener;
29
30import com.badlogic.gdx.graphics.g3d.particles.values.RangedNumericValue;
31
32/** @author Inferno */
33class RangedNumericPanel extends ParticleValuePanel<RangedNumericValue> {
34	Slider minSlider, maxSlider;
35	JButton rangeButton;
36	JLabel label;
37
38	public RangedNumericPanel (FlameMain editor, RangedNumericValue value, String name, String description)
39	{
40		this(editor, value, name, description, true);
41	}
42
43	public RangedNumericPanel (FlameMain editor, RangedNumericValue value, String name, String description, boolean isAlwaysActive)
44	{
45		super(editor, name, description, isAlwaysActive);
46		setValue(value);
47	}
48
49	@Override
50	protected void initializeComponents () {
51		super.initializeComponents();
52		JPanel contentPanel = getContentPanel();
53		{
54			label = new JLabel("Value:");
55			contentPanel.add(label, new GridBagConstraints(2, 2, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE,
56				new Insets(0, 0, 0, 6), 0, 0));
57		}
58		{
59			minSlider = new Slider(0, -99999, 99999, 1);
60			contentPanel.add(minSlider, new GridBagConstraints(3, 2, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE,
61				new Insets(0, 0, 0, 0), 0, 0));
62		}
63		{
64			maxSlider = new Slider(0, -99999, 99999, 1);
65			contentPanel.add(maxSlider, new GridBagConstraints(4, 2, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE,
66				new Insets(0, 6, 0, 0), 0, 0));
67		}
68		{
69			rangeButton = new JButton("<");
70			rangeButton.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
71			contentPanel.add(rangeButton, new GridBagConstraints(5, 2, 1, 1, 1.0, 0, GridBagConstraints.WEST,
72				GridBagConstraints.NONE, new Insets(0, 1, 0, 0), 0, 0));
73		}
74
75
76		minSlider.addChangeListener(new ChangeListener() {
77			public void stateChanged (ChangeEvent event) {
78				RangedNumericPanel.this.value.setLowMin((Float)minSlider.getValue());
79				if (!maxSlider.isVisible()) RangedNumericPanel.this.value.setLowMax((Float)minSlider.getValue());
80			}
81		});
82
83		maxSlider.addChangeListener(new ChangeListener() {
84			public void stateChanged (ChangeEvent event) {
85				RangedNumericPanel.this.value.setLowMax((Float)maxSlider.getValue());
86			}
87		});
88
89		rangeButton.addActionListener(new ActionListener() {
90			public void actionPerformed (ActionEvent event) {
91				boolean visible = !maxSlider.isVisible();
92				maxSlider.setVisible(visible);
93				rangeButton.setText(visible ? "<" : ">");
94				Slider slider = visible ? maxSlider : minSlider;
95				RangedNumericPanel.this.value.setLowMax((Float)slider.getValue());
96			}
97		});
98	}
99
100	public void setValue (RangedNumericValue value) {
101		super.setValue(value);
102		if(value == null) return;
103		setValue(minSlider, value.getLowMin());
104		setValue(maxSlider, value.getLowMax());
105		//System.out.println("min "+value.getLowMin()+", max "+value.getLowMax());
106		if (minSlider.getValue() == maxSlider.getValue())
107			rangeButton.doClick(0);
108		else if(!maxSlider.isVisible())
109				maxSlider.setVisible(true);
110	}
111}
112