Subversion Repositories svn.mios

Rev

Rev 657 | Rev 670 | 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;
657 adamjking 47
 
213 tk 48
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
49
        panel.setOpaque(false);
50
        panel.add(knob);
668 adamjking 51
 
52
        knob.setMinimum(midiParameter.getMidiMinValue());
53
        knob.setMaximum(midiParameter.getMidiMaxValue());
54
 
213 tk 55
        add(panel, BorderLayout.CENTER);
56
        knob.addChangeListener(this);
57
        knob.addMouseWheelListener(this);
58
        knob.addMouseListener(this);
628 adamjking 59
        updateGraphics();
213 tk 60
    }
61
 
62
    public void mouseWheelMoved(MouseWheelEvent mwe) {
628 adamjking 63
        knob
657 adamjking 64
                .setValue((int) (knob.getValue() - (mwe.getWheelRotation() * ((mouseWheelResolution / 100) * (knob
668 adamjking 65
                        .getMaximum() - knob.getMinimum())))));
213 tk 66
    }
67
 
68
    public void stateChanged(ChangeEvent ce) {
668 adamjking 69
 
213 tk 70
        if (update) {
668 adamjking 71
 
213 tk 72
            update = false;
668 adamjking 73
 
213 tk 74
            int newval;
668 adamjking 75
 
213 tk 76
            if (midiParameter.snap) {
668 adamjking 77
 
78
                int index = (int) (((float) knob.getValue() / (float) knob
79
                        .getMaximum()) * (midiParameter.snapvals.length - 1));
628 adamjking 80
                newval = midiParameter.snapvals[index];
668 adamjking 81
 
213 tk 82
            } else {
668 adamjking 83
 
84
                newval = knob.getValue();
213 tk 85
            }
628 adamjking 86
 
213 tk 87
            midiParameter.setMidiValue(newval, true);
628 adamjking 88
 
213 tk 89
            for (int c = 0; c < midiParameters.size(); c++) {
628 adamjking 90
                SIDSysexParameterControl mp = (SIDSysexParameterControl) midiParameters
91
                        .elementAt(c);
272 rutgerv 92
                mp.setMidiValue(newval, false);
213 tk 93
            }
628 adamjking 94
 
213 tk 95
            update = true;
96
        }
668 adamjking 97
 
213 tk 98
    }
628 adamjking 99
 
213 tk 100
    private int findSnap(int val) {
101
        int outval = 0;
628 adamjking 102
        for (int i = 0; i < midiParameter.snapvals.length; i++) {
103
            if (midiParameter.snapvals[i] == val) {
213 tk 104
                outval = i;
105
                break;
628 adamjking 106
            }
213 tk 107
        }
108
        return outval;
109
    }
628 adamjking 110
 
213 tk 111
    public void setSnapvals(int[] vals) {
112
        midiParameter.useAlias = false;
628 adamjking 113
        midiParameter.snapvals = vals;
213 tk 114
        for (int c = 0; c < midiParameters.size(); c++) {
628 adamjking 115
            SIDSysexParameterControl mp = (SIDSysexParameterControl) midiParameters
116
                    .elementAt(c);
213 tk 117
            mp.snapvals = vals;
118
        }
119
    }
628 adamjking 120
 
213 tk 121
    public void updateGraphics() {
668 adamjking 122
 
213 tk 123
        super.updateGraphics();
668 adamjking 124
 
213 tk 125
        if (update) {
668 adamjking 126
 
213 tk 127
            update = false;
668 adamjking 128
 
129
            int newval;
130
 
213 tk 131
            if (midiParameter.snap) {
668 adamjking 132
 
213 tk 133
                int index = findSnap(midiParameter.getMidiValue());
668 adamjking 134
                newval = index / (midiParameter.snapvals.length - 1);
135
 
213 tk 136
            } else {
668 adamjking 137
 
138
                newval = midiParameter.getMidiValue();
213 tk 139
            }
668 adamjking 140
 
141
            knob.setValue(newval);
142
 
213 tk 143
            update = true;
144
        }
145
    }
146
}