From 9f8afb520bcb3102da7b5c050fd601b4a0a29e13 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 26 May 2013 17:43:32 +0200 Subject: [PATCH] Add knob controller panel. --- .../pterodactylus/sonitus/gui/ControlledPane.java | 3 + .../net/pterodactylus/sonitus/gui/KnobPanel.java | 70 ++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sonitus/gui/KnobPanel.java diff --git a/src/main/java/net/pterodactylus/sonitus/gui/ControlledPane.java b/src/main/java/net/pterodactylus/sonitus/gui/ControlledPane.java index 76fb8d0..19b9fac 100644 --- a/src/main/java/net/pterodactylus/sonitus/gui/ControlledPane.java +++ b/src/main/java/net/pterodactylus/sonitus/gui/ControlledPane.java @@ -27,6 +27,7 @@ import javax.swing.JPanel; import net.pterodactylus.sonitus.data.Controlled; import net.pterodactylus.sonitus.data.Controller; import net.pterodactylus.sonitus.data.controller.Fader; +import net.pterodactylus.sonitus.data.controller.Knob; import net.pterodactylus.sonitus.data.controller.Switch; /** @@ -54,6 +55,8 @@ public class ControlledPane extends JPanel { add(new FaderPanel((Fader) controller), new GridBagConstraints(1, controllerIndex, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0)); } else if (controller instanceof Switch) { add(new SwitchPanel((Switch) controller), new GridBagConstraints(1, controllerIndex, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0)); + } else if (controller instanceof Knob) { + add(new KnobPanel((Knob) controller), new GridBagConstraints(1, controllerIndex, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(6, 0, 0, 0), 0, 0)); } ++controllerIndex; } diff --git a/src/main/java/net/pterodactylus/sonitus/gui/KnobPanel.java b/src/main/java/net/pterodactylus/sonitus/gui/KnobPanel.java new file mode 100644 index 0000000..1901566 --- /dev/null +++ b/src/main/java/net/pterodactylus/sonitus/gui/KnobPanel.java @@ -0,0 +1,70 @@ +/* + * Sonitus - KnobPanel.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sonitus.gui; + +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import javax.swing.DefaultBoundedRangeModel; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JSlider; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; + +import net.pterodactylus.sonitus.data.controller.Knob; + +/** + * A panel that displays a {@link Knob}. A knob panel is very similar to a + * {@link FaderPanel}, it just has added tick marks at the center of the + * slider. + * + * @author David ‘Bombe’ Roden + */ +public class KnobPanel extends JPanel { + + /** + * Creates a new knob panel. + * + * @param knob + * The knob being controlled + */ + public KnobPanel(final Knob knob) { + super(new GridBagLayout()); + + /* create knob labels. */ + add(new JLabel("-1"), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 6, 0, 0), 0, 0)); + add(new JLabel("1"), new GridBagConstraints(2, 0, 1, 1, 0, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 6, 0, 0), 0, 0)); + + /* create fader. */ + JSlider slider = new JSlider(new DefaultBoundedRangeModel((int) (knob.value() * 65536), 0, -65536, 65536)); + slider.addChangeListener(new ChangeListener() { + + @Override + public void stateChanged(ChangeEvent changeEvent) { + knob.value(((JSlider) changeEvent.getSource()).getValue() / 65536.0); + } + }); + if (knob.centered()) { + slider.setMajorTickSpacing(65536); + slider.setPaintTicks(true); + } + add(slider, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); + } + +} -- 2.7.4