Subversion Repositories svn.mios

Rev

Rev 272 | Rev 657 | Go to most recent revision | 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;
24
import java.awt.FlowLayout;
25
import java.awt.event.MouseWheelEvent;
26
import java.awt.event.MouseWheelListener;
27
 
28
import javax.swing.JPanel;
29
import javax.swing.event.ChangeEvent;
30
import javax.swing.event.ChangeListener;
31
 
628 adamjking 32
import org.midibox.sidedit.SIDSysexParameterControl;
213 tk 33
import org.midibox.utils.gui.Knob;
34
 
628 adamjking 35
public class SIDSysexParameterControlKnob extends SIDSysexParameterControlGUI
36
        implements ChangeListener, MouseWheelListener {
213 tk 37
 
38
    public static float mouseWheelResolution = 10.0f;
39
 
40
    private Knob knob;
628 adamjking 41
 
42
    public SIDSysexParameterControlKnob(SIDSysexParameterControl midiParameter,
43
            Knob knob, boolean showLabel, String labelLocation,
44
            boolean valueBelow, boolean showValue) {
213 tk 45
        super(midiParameter, showLabel, labelLocation, valueBelow, showValue);
46
        this.knob = knob;
47
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
48
        panel.setOpaque(false);
49
        panel.add(knob);
50
        add(panel, BorderLayout.CENTER);
51
        knob.addChangeListener(this);
52
        knob.addMouseWheelListener(this);
53
        knob.addMouseListener(this);
628 adamjking 54
        updateGraphics();
213 tk 55
    }
56
 
57
    public void mouseWheelMoved(MouseWheelEvent mwe) {
628 adamjking 58
        knob
59
                .setValue(knob.getValue()
60
                        - (mwe.getWheelRotation() * ((mouseWheelResolution / 100) * (knob
61
                                .getMaxValue() - knob.getMinValue()))));
213 tk 62
    }
63
 
64
    public void stateChanged(ChangeEvent ce) {
65
        if (update) {
66
            update = false;
67
            int newval;
68
            if (midiParameter.snap) {
628 adamjking 69
                int index = (int) ((knob.getValue() / knob.getMaxValue()) * (midiParameter.snapvals.length - 1));
70
                newval = midiParameter.snapvals[index];
213 tk 71
            } else {
628 adamjking 72
                newval = (int) ((knob.getValue() / knob.getMaxValue()) * (midiParameter
73
                        .getMidiMaxValue() - midiParameter.getMidiMinValue()))
74
                        + midiParameter.getMidiMinValue();
213 tk 75
            }
628 adamjking 76
 
213 tk 77
            midiParameter.setMidiValue(newval, true);
628 adamjking 78
 
213 tk 79
            for (int c = 0; c < midiParameters.size(); c++) {
628 adamjking 80
                SIDSysexParameterControl mp = (SIDSysexParameterControl) midiParameters
81
                        .elementAt(c);
272 rutgerv 82
                mp.setMidiValue(newval, false);
213 tk 83
            }
628 adamjking 84
 
213 tk 85
            update = true;
86
        }
87
    }
628 adamjking 88
 
213 tk 89
    private int findSnap(int val) {
90
        int outval = 0;
628 adamjking 91
        for (int i = 0; i < midiParameter.snapvals.length; i++) {
92
            if (midiParameter.snapvals[i] == val) {
213 tk 93
                outval = i;
94
                break;
628 adamjking 95
            }
213 tk 96
        }
97
        return outval;
98
    }
628 adamjking 99
 
213 tk 100
    public void setSnapvals(int[] vals) {
101
        midiParameter.useAlias = false;
628 adamjking 102
        midiParameter.snapvals = vals;
213 tk 103
        for (int c = 0; c < midiParameters.size(); c++) {
628 adamjking 104
            SIDSysexParameterControl mp = (SIDSysexParameterControl) midiParameters
105
                    .elementAt(c);
213 tk 106
            mp.snapvals = vals;
107
        }
108
    }
628 adamjking 109
 
213 tk 110
    public void updateGraphics() {
111
        super.updateGraphics();
112
        if (update) {
113
            update = false;
114
            float newval;
115
            if (midiParameter.snap) {
116
                int index = findSnap(midiParameter.getMidiValue());
628 adamjking 117
                newval = ((float) index) / (midiParameter.snapvals.length - 1);
213 tk 118
            } else {
628 adamjking 119
                newval = ((float) midiParameter.getMidiValue() - (float) midiParameter
120
                        .getMidiMinValue())
121
                        / ((float) midiParameter.getMidiMaxValue() - (float) midiParameter
122
                                .getMidiMinValue());
213 tk 123
            }
124
            knob.setValue(newval);
125
            update = true;
126
        }
127
    }
128
}