~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/editors/chord/ChordEditor.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Created on 28-dic-2005
3
 
 *
4
 
 * TODO To change the template for this generated file go to
5
 
 * Window - Preferences - Java - Code Style - Code Templates
6
 
 */
7
 
package org.herac.tuxguitar.gui.editors.chord;
8
 
 
9
 
import java.util.ArrayList;
10
 
import java.util.Iterator;
11
 
import java.util.List;
12
 
 
13
 
import org.eclipse.swt.SWT;
14
 
import org.eclipse.swt.events.MouseListener;
15
 
import org.eclipse.swt.events.PaintEvent;
16
 
import org.eclipse.swt.events.PaintListener;
17
 
import org.eclipse.swt.graphics.GC;
18
 
import org.eclipse.swt.graphics.Point;
19
 
import org.eclipse.swt.layout.GridData;
20
 
import org.eclipse.swt.layout.GridLayout;
21
 
import org.eclipse.swt.widgets.Canvas;
22
 
import org.eclipse.swt.widgets.Composite;
23
 
import org.eclipse.swt.widgets.Event;
24
 
import org.eclipse.swt.widgets.Listener;
25
 
import org.herac.tuxguitar.song.models.Chord;
26
 
/**
27
 
 * @author julian
28
 
 *
29
 
 * TODO To change the template for this generated type comment go to
30
 
 * Window - Preferences - Java - Code Style - Code Templates
31
 
 */
32
 
public class ChordEditor extends Composite{
33
 
    public static final int STRING_SPAN = 30;
34
 
    public static final int FRET_SPAN = 30;    
35
 
    public static final short MIN_FRET = 1;        
36
 
    public static final short MAX_FRET = 24; 
37
 
    public static final short VIEWING_FRETS = 6;        
38
 
    
39
 
    private Canvas canvas;
40
 
    private boolean[] fisrtFrets;
41
 
    private int[] strings; 
42
 
    private int[] frets;
43
 
    private short fret;
44
 
    private short maxStrings;
45
 
    private int width;
46
 
    private int height;    
47
 
    private List points;
48
 
    
49
 
    public ChordEditor(Composite parent, int style) {
50
 
        super(parent, style);        
51
 
    }
52
 
    
53
 
    public ChordEditor(Composite parent, int style,short maxStrings) {
54
 
        this(parent, style);
55
 
        this.setLayout(new GridLayout());
56
 
        this.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
57
 
        this.init(maxStrings);
58
 
    }
59
 
 
60
 
    public void init(short maxStrings){
61
 
        this.fret = MIN_FRET;
62
 
        this.maxStrings = maxStrings;
63
 
        this.fisrtFrets = new boolean[this.maxStrings];
64
 
        this.strings = new int[this.maxStrings];
65
 
        this.frets = new int[VIEWING_FRETS];
66
 
        this.width = ((STRING_SPAN * this.maxStrings) - STRING_SPAN);
67
 
        this.height = ((FRET_SPAN * VIEWING_FRETS) - FRET_SPAN);
68
 
        this.points = new ArrayList();
69
 
        
70
 
        for(int i = 0;i < fisrtFrets.length;i++){
71
 
            this.fisrtFrets[i] = false;            
72
 
        }                
73
 
        for(int i = 0;i < strings.length;i++){
74
 
            this.strings[i] = ((i + 1) * STRING_SPAN);            
75
 
        }
76
 
        for(int i = 0;i < frets.length;i++){
77
 
            this.frets[i] = ((i + 1) * FRET_SPAN);
78
 
        }
79
 
     
80
 
        
81
 
 
82
 
                
83
 
        Composite composite = new Composite(this,SWT.NONE);
84
 
        composite.setLayout(new GridLayout());
85
 
        composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
86
 
        
87
 
        this.canvas = new Canvas(composite,SWT.BORDER | SWT.V_SCROLL | SWT.DOUBLE_BUFFERED);
88
 
        this.canvas.setBackground(this.getDisplay().getSystemColor(SWT.COLOR_WHITE));        
89
 
        this.canvas.addPaintListener(new PaintListener() {
90
 
            public void paintControl(PaintEvent e) {
91
 
                paintEditor(e.gc);
92
 
                e.gc.dispose();
93
 
            }
94
 
        });
95
 
        this.canvas.addMouseListener(new MouseListener() {
96
 
            public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
97
 
            }
98
 
 
99
 
            public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
100
 
            }
101
 
 
102
 
            public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
103
 
                checkPoint(e.x,e.y);
104
 
                redraw();
105
 
            }
106
 
        });
107
 
                
108
 
        this.canvas.getVerticalBar().setMaximum(((MAX_FRET + MIN_FRET) - (VIEWING_FRETS - 1) + 1));
109
 
        this.canvas.getVerticalBar().setMinimum(MIN_FRET);
110
 
        this.canvas.getVerticalBar().setThumb(1);
111
 
        this.canvas.getVerticalBar().addListener(SWT.Selection, new Listener() {
112
 
            public void handleEvent(Event event) {
113
 
                setFret((short)canvas.getVerticalBar().getSelection(),false);
114
 
                redraw();
115
 
            }
116
 
        });           
117
 
        
118
 
        
119
 
        this.canvas.setLayoutData(makeCanvasData());
120
 
        
121
 
    }
122
 
    
123
 
    private GridData makeCanvasData(){
124
 
        GridData data = new GridData(SWT.FILL,SWT.FILL,true,true);
125
 
        data.minimumWidth = (getWidth() + (STRING_SPAN * 2) + canvas.getVerticalBar().getSize().x );
126
 
        data.minimumHeight = (getHeight() + (FRET_SPAN * 2));
127
 
        return data;
128
 
    }
129
 
    
130
 
    private void paintEditor(GC gc){
131
 
        gc.setForeground(this.getDisplay().getSystemColor(SWT.COLOR_BLACK));
132
 
        
133
 
        //dibujo el puente
134
 
        gc.drawLine((STRING_SPAN - 10),(FRET_SPAN - 10),STRING_SPAN + (this.width + 10),(FRET_SPAN - 10));
135
 
        
136
 
        gc.drawString(Integer.toString(getFret()),FRET_SPAN - 20,STRING_SPAN);
137
 
        
138
 
        //dibujo las cuerdas
139
 
        for(int i = 0;i < strings.length;i++){
140
 
            gc.drawLine(this.strings[i],FRET_SPAN,this.strings[i],FRET_SPAN + this.height);
141
 
        }
142
 
        //dibujo las cegillas
143
 
        for(int i = 0;i < frets.length;i++){
144
 
            gc.drawLine(STRING_SPAN,this.frets[i],STRING_SPAN + this.width,this.frets[i]);
145
 
        }       
146
 
                
147
 
        //dibujo las notas
148
 
        gc.setLineWidth(10);
149
 
        //gc.setForeground(this.getDisplay().getSystemColor(SWT.COLOR_BLACK));                                       
150
 
        Iterator it = this.points.iterator();
151
 
        while(it.hasNext()){
152
 
            Point point = (Point)it.next();                       
153
 
 
154
 
            gc.drawOval(point.x - 2,point.y + (FRET_SPAN / 2),5,5);           
155
 
        }
156
 
        gc.setLineWidth(1);
157
 
 
158
 
        //dibujo las notas al aire        
159
 
        for(int i = 0;i < fisrtFrets.length;i++){
160
 
            if(!hasPoints(i)){
161
 
                if(fisrtFrets[i]){
162
 
                    int x = this.strings[i] - 7;
163
 
                    int y = FRET_SPAN - 27;                    
164
 
                    gc.drawOval(x,y,14,14);   
165
 
                }else{
166
 
                    int x = this.strings[i];
167
 
                    int y = FRET_SPAN - 20;
168
 
                    gc.drawLine(x - 6,y + 6,x + 7,y - 7);
169
 
                    gc.drawLine(x - 6,y - 6,x + 7,y + 7);                    
170
 
                }
171
 
            }
172
 
        }        
173
 
    }
174
 
 
175
 
    private void checkPoint(int x,int y){        
176
 
        int stringIndex = getStringIndex(x);
177
 
        int fretIndex = getFretIndex(y);                
178
 
        
179
 
        if(y < FRET_SPAN){
180
 
            this.fisrtFrets[stringIndex] = !this.fisrtFrets[stringIndex];
181
 
            this.removePointsAtStringLine(this.strings[stringIndex]);
182
 
        }else if(y < (FRET_SPAN * VIEWING_FRETS)){                
183
 
            Point point = new Point(this.strings[stringIndex],this.frets[fretIndex]);
184
 
            if(!this.removePoint(point)){
185
 
                this.fisrtFrets[stringIndex] = false;
186
 
                this.removePointsAtStringLine(this.strings[stringIndex]);
187
 
                this.addPoint(point);
188
 
                this.orderPoints();
189
 
            }
190
 
        }
191
 
    }
192
 
    
193
 
    private boolean removePoint(Point point){
194
 
        Iterator it = this.points.iterator();
195
 
        while(it.hasNext()){
196
 
            Point currPoint = (Point)it.next();
197
 
            if(currPoint.x == point.x && currPoint.y == point.y){
198
 
                this.points.remove(point);
199
 
                return true;
200
 
            }
201
 
        }  
202
 
        return false;
203
 
    }
204
 
    
205
 
    private void orderPoints(){
206
 
        for(int i = 0;i < this.points.size();i++){
207
 
            Point minPoint = null;
208
 
            for(int noteIdx = i;noteIdx < this.points.size();noteIdx++){
209
 
                Point point = (Point)this.points.get(noteIdx);
210
 
                if(minPoint == null || point.x < minPoint.x){
211
 
                    minPoint = point;
212
 
                }
213
 
            }
214
 
            this.points.remove(minPoint);
215
 
            this.points.add(i,minPoint);
216
 
        }
217
 
    }    
218
 
    
219
 
    private void removePointsAtStringLine(int x){
220
 
        Iterator it = this.points.iterator();
221
 
        while(it.hasNext()){
222
 
            Point point = (Point)it.next();
223
 
            if(point.x == x){
224
 
                this.points.remove(point);
225
 
                break;
226
 
            }
227
 
        }  
228
 
    }     
229
 
    
230
 
    private void addPoint(Point point){
231
 
        this.points.add(point);
232
 
    }
233
 
    
234
 
    private int getStringIndex(int x){  
235
 
        int index = -1;          
236
 
        for(int i = 0;i < this.strings.length;i++){                    
237
 
            if(index < 0){
238
 
                index = i;
239
 
            }else{                    
240
 
                int distanceX = Math.abs(x - this.strings[index]);
241
 
                int currDistanceX = Math.abs(x - this.strings[i]);
242
 
                if(currDistanceX < distanceX){
243
 
                    index = i;
244
 
                }
245
 
            }
246
 
            
247
 
        }
248
 
        return index;
249
 
    }     
250
 
    
251
 
    private int getFretIndex(int y){
252
 
        int index = -1;
253
 
        for(int i = 0;i < this.frets.length;i++){                    
254
 
            if(index < 0){
255
 
                index = i;
256
 
            }else{                    
257
 
                int distanceX = Math.abs(y - (this.frets[index] + (FRET_SPAN / 2)));
258
 
                int currDistanceX = Math.abs(y - (this.frets[i] + (FRET_SPAN / 2)));
259
 
                if(currDistanceX < distanceX){
260
 
                    index = i;
261
 
                }
262
 
            }
263
 
            
264
 
        }
265
 
        return index;
266
 
    }  
267
 
    
268
 
    private boolean hasPoints(int stringIndex){                          
269
 
        Iterator it = this.points.iterator();
270
 
        while(it.hasNext()){
271
 
            Point point = (Point)it.next();                       
272
 
            if(point.x == this.strings[stringIndex]){
273
 
                return true;
274
 
            }
275
 
        }                        
276
 
        return false;
277
 
    }
278
 
    
279
 
    public boolean isEmpty(){
280
 
        return this.points.isEmpty();
281
 
    }
282
 
 
283
 
    public Chord getCurrentChord(){
284
 
        Chord chord = new Chord(this.strings.length);
285
 
        for(int i = 0;i < chord.getStrings().length;i ++){
286
 
                chord.addFretValue(i,getValue(i + 1));          
287
 
        }
288
 
        return chord;
289
 
    }
290
 
    
291
 
    public int getValue(int string){
292
 
        int value = -1;
293
 
        if(this.fisrtFrets[this.maxStrings - string]){
294
 
            value = 0;
295
 
        }
296
 
        
297
 
        if(value < 0){
298
 
            Iterator it = this.points.iterator();
299
 
            while(it.hasNext()){
300
 
                Point point = (Point)it.next();                                   
301
 
                if(string == (this.maxStrings - getStringIndex(point.x))){
302
 
                    value = (getFretIndex(point.y + (FRET_SPAN / 2)) + 1);
303
 
                    value += (getFret() - 1);
304
 
                }
305
 
            }               
306
 
        }
307
 
        
308
 
        return value;
309
 
    }
310
 
    
311
 
    public void addValue(int value,int string){
312
 
        if(string >= 1 && string <= this.maxStrings){
313
 
            this.fisrtFrets[this.maxStrings - string] = false;
314
 
            this.removePointsAtStringLine(this.strings[this.maxStrings - string]);        
315
 
            if(value == 0){
316
 
                this.fisrtFrets[this.maxStrings - string] = true;
317
 
            }else if(value >= 0){
318
 
                value -= (getFret() - 1);
319
 
                if(value > 0 && value <= VIEWING_FRETS){
320
 
                    this.addPoint(new Point(this.strings[this.maxStrings - string],this.frets[value - 1]));
321
 
                }
322
 
            }
323
 
        }
324
 
    }
325
 
    
326
 
    public short getFret() {
327
 
        return fret;
328
 
    }
329
 
    public void setFret(short fret) {
330
 
        setFret(fret,true);
331
 
    }
332
 
 
333
 
    private void setFret(short fret,boolean updateScroll){
334
 
        if(fret >= MIN_FRET && fret <= MAX_FRET){
335
 
            this.fret = fret;
336
 
        }        
337
 
        if(updateScroll){
338
 
            canvas.getVerticalBar().setSelection(this.fret);
339
 
        }
340
 
    }
341
 
    
342
 
    public short getMaxStrings() {
343
 
        return maxStrings;
344
 
    }
345
 
    public void setMaxStrings(short maxStrings) {
346
 
        this.maxStrings = maxStrings;
347
 
    }
348
 
    public int getWidth(){
349
 
        return this.width;        
350
 
    }
351
 
    
352
 
    public int getHeight(){
353
 
        return this.height;
354
 
    }
355
 
 
356
 
    public void redraw(){
357
 
        super.redraw();
358
 
        this.canvas.redraw();
359
 
    }
360
 
}