Subversion Repositories svn.mios

Rev

Rev 696 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
213 tk 1
/*
2
 * @(#)SIDV2librarian.java  beta1   2008/01/21
3
 *
4
 * Copyright (C) 2008    Rutger Vlek (rutgervlek@hotmail.com)
5
 *
6
 * This application is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This application is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this application; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20
 
21
package org.midibox.sidedit.gui.controls;
22
 
23
import java.awt.BorderLayout;
628 adamjking 24
import java.awt.Dimension;
213 tk 25
import java.awt.FlowLayout;
26
import java.awt.event.MouseWheelEvent;
27
import java.awt.event.MouseWheelListener;
28
 
29
import javax.swing.JPanel;
30
import javax.swing.JSlider;
31
import javax.swing.event.ChangeEvent;
32
import javax.swing.event.ChangeListener;
33
 
628 adamjking 34
import org.midibox.sidedit.SIDSysexParameterControl;
649 adamjking 35
import org.midibox.utils.gui.FaderUI;
36
import org.midibox.utils.gui.ImageLoader;
213 tk 37
 
38
public class SIDSysexParameterControlSlider extends SIDSysexParameterControlGUI
39
        implements ChangeListener, MouseWheelListener {
40
 
670 adamjking 41
    public static float mouseWheelResolution = 0.1f;
213 tk 42
 
43
    private JSlider slider;
44
 
628 adamjking 45
    public SIDSysexParameterControlSlider(
46
            SIDSysexParameterControl midiParameter, JSlider slider,
47
            boolean showLabel, String labelLocation, boolean valueBelow,
48
            boolean showValue) {
213 tk 49
        super(midiParameter, showLabel, labelLocation, valueBelow, showValue);
50
        this.slider = slider;
649 adamjking 51
 
668 adamjking 52
        slider.setMinimum(midiParameter.getMidiMinValue());
53
        slider.setMaximum(midiParameter.getMidiMaxValue());
54
 
657 adamjking 55
        FaderUI faderUI = new FaderUI(ImageLoader
676 adamjking 56
                .getImageIcon("plasticslider_horiz.png"), ImageLoader
57
                .getImageIcon("plasticslider_vert.png"), false);
649 adamjking 58
 
657 adamjking 59
        faderUI.setTrackWidth(3);
650 adamjking 60
 
657 adamjking 61
        slider.setUI(faderUI);
649 adamjking 62
 
628 adamjking 63
        slider.setMaximum(midiParameter.getMidiMaxValue()
64
                - midiParameter.getMidiMinValue());
213 tk 65
        slider.setOpaque(false);
66
        slider.setOrientation(JSlider.VERTICAL);
628 adamjking 67
        slider.setPreferredSize(new Dimension(20, 80));
213 tk 68
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
69
        panel.setOpaque(false);
70
        panel.add(slider);
71
        add(panel, BorderLayout.CENTER);
72
        slider.addChangeListener(this);
73
        slider.addMouseWheelListener(this);
74
        slider.addMouseListener(this);
75
        updateGraphics();
76
    }
628 adamjking 77
 
213 tk 78
    public void setHeight(int height) {
628 adamjking 79
        slider.setPreferredSize(new Dimension(20, height));
213 tk 80
    }
81
 
82
    public void mouseWheelMoved(MouseWheelEvent mwe) {
738 adamjking 83
        slider
84
                .setValue((int) (slider.getValue() - ((mwe.getWheelRotation() * Math
85
                        .max(mouseWheelResolution
86
                                * (slider.getMaximum() - slider.getMinimum()),
87
                                1)))));
213 tk 88
    }
89
 
90
    public void stateChanged(ChangeEvent ce) {
91
        if (update) {
92
            update = false;
696 rutgerv 93
            int newval;
738 adamjking 94
            if (midiParameter.snap) {
95
                int index = (int) (((float) slider.getValue() / (float) slider
96
                        .getMaximum()) * (midiParameter.snapVals.length - 1));
696 rutgerv 97
                newval = midiParameter.snapVals[index];
98
            } else {
99
                newval = slider.getValue();
100
            }
668 adamjking 101
 
213 tk 102
            midiParameter.setMidiValue(newval, true);
628 adamjking 103
 
213 tk 104
            for (int c = 0; c < midiParameters.size(); c++) {
738 adamjking 105
                SIDSysexParameterControl mp = (SIDSysexParameterControl) midiParameters
106
                        .elementAt(c);
272 rutgerv 107
                mp.setMidiValue(newval, false);
213 tk 108
            }
109
            update = true;
110
        }
111
    }
628 adamjking 112
 
213 tk 113
    public void updateGraphics() {
114
        super.updateGraphics();
738 adamjking 115
 
213 tk 116
        if (update) {
117
            update = false;
696 rutgerv 118
            int newval;
119
            if (midiParameter.snap) {
738 adamjking 120
                float ratio = (float) midiParameter.getMidiValSnapIndex()
121
                        / (float) (midiParameter.snapVals.length - 1);
122
                newval = (int) ((float) (slider.getMaximum() - slider
123
                        .getMinimum()) * ratio);
696 rutgerv 124
            } else {
125
                newval = midiParameter.getMidiValue();
126
            }
127
            slider.setValue(newval);
213 tk 128
            update = true;
129
        }
130
    }
131
}