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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/effects/BendEditor.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.effects;
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.MouseAdapter;
15
 
import org.eclipse.swt.events.PaintEvent;
16
 
import org.eclipse.swt.events.PaintListener;
17
 
import org.eclipse.swt.events.SelectionAdapter;
18
 
import org.eclipse.swt.events.SelectionEvent;
19
 
import org.eclipse.swt.graphics.GC;
20
 
import org.eclipse.swt.graphics.Point;
21
 
import org.eclipse.swt.layout.GridData;
22
 
import org.eclipse.swt.layout.GridLayout;
23
 
import org.eclipse.swt.widgets.Button;
24
 
import org.eclipse.swt.widgets.Composite;
25
 
import org.eclipse.swt.widgets.Shell;
26
 
import org.herac.tuxguitar.gui.TuxGuitar;
27
 
import org.herac.tuxguitar.song.models.Note;
28
 
import org.herac.tuxguitar.song.models.effects.BendEffect;
29
 
/**
30
 
 * @author julian
31
 
 *
32
 
 * TODO To change the template for this generated type comment go to
33
 
 * Window - Preferences - Java - Code Style - Code Templates
34
 
 */
35
 
public class BendEditor{
36
 
    public static final int X_SPAN = 30;
37
 
    public static final int Y_SPAN = 15;    
38
 
    private static final int X_LENGTH = BendEffect.MAX_POSITION_LENGTH + 1;
39
 
    private static final int Y_LENGTH = BendEffect.MAX_VALUE_LENGTH + 1;
40
 
 
41
 
    private Composite editor;
42
 
    private int[] x; 
43
 
    private int[] y;
44
 
    private int width;
45
 
    private int height;    
46
 
    private List points;
47
 
    
48
 
    private DefaultBend[] defaultBends;
49
 
    private boolean isEditing;    
50
 
    
51
 
    private BendEffect result;
52
 
    
53
 
    public BendEditor() {
54
 
        this.init();                
55
 
    }
56
 
 
57
 
    private void init(){
58
 
        this.x = new int[X_LENGTH];
59
 
        this.y = new int[Y_LENGTH];
60
 
        this.width = ((X_SPAN * X_LENGTH) - X_SPAN);
61
 
        this.height = ((Y_SPAN * Y_LENGTH) - Y_SPAN);
62
 
        this.points = new ArrayList();
63
 
        
64
 
        for(int i = 0;i < x.length;i++){
65
 
            this.x[i] = ((i + 1) * X_SPAN);            
66
 
        }
67
 
        for(int i = 0;i < y.length;i++){
68
 
            this.y[i] = ((i + 1) * Y_SPAN);
69
 
        }        
70
 
    }    
71
 
    
72
 
    public BendEffect show(Shell shell,final Note note){
73
 
        final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
74
 
   
75
 
        dialog.setLayout(new GridLayout());        
76
 
        dialog.setText(TuxGuitar.getProperty("bend.editor"));
77
 
        
78
 
        //----------------------------------------------------------------------
79
 
        Composite composite = new Composite(dialog,SWT.NONE);
80
 
        composite.setLayout(new GridLayout(3,false));
81
 
        composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));         
82
 
        
83
 
        Composite leftComposite = new Composite(composite,SWT.NONE); 
84
 
        leftComposite.setLayout(new GridLayout());              
85
 
        leftComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));        
86
 
        
87
 
        Composite rightComposite = new Composite(composite,SWT.NONE); 
88
 
        rightComposite.setLayout(new GridLayout());              
89
 
        rightComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));        
90
 
        
91
 
        //-------------EDITOR---------------------------------------------------        
92
 
        this.isEditing = (note.getEffect().isBend());                
93
 
        this.editor = new Composite(leftComposite, SWT.BORDER | SWT.DOUBLE_BUFFERED);
94
 
        this.editor.setBackground(editor.getDisplay().getSystemColor(SWT.COLOR_WHITE));        
95
 
        this.editor.setLayoutData(resizeData(new GridData(SWT.FILL,SWT.FILL,true,true) , getWidth() + (X_SPAN * 2),getHeight() + (Y_SPAN * 2))  );
96
 
        this.editor.addPaintListener(new PaintListener() {
97
 
            public void paintControl(PaintEvent e) {
98
 
                paintEditor(e.gc);
99
 
                e.gc.dispose();
100
 
            }
101
 
        });
102
 
        this.editor.addMouseListener(new MouseAdapter() {
103
 
            public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
104
 
                checkPoint(e.x,e.y);
105
 
                editor.redraw();
106
 
            }
107
 
        });         
108
 
        if(isEditing){
109
 
            setBend(note.getEffect().getBend());
110
 
        }                
111
 
 
112
 
        //-------------DEFAULT BEND LIST---------------------------------------------------       
113
 
        final org.eclipse.swt.widgets.List defaultBendList = new org.eclipse.swt.widgets.List(rightComposite,SWT.BORDER);         
114
 
        this.resetDefaultBends();
115
 
        for(int i = 0;i < defaultBends.length;i++){
116
 
            defaultBendList.add(this.defaultBends[i].getName());
117
 
        }
118
 
        defaultBendList.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));  
119
 
        defaultBendList.addSelectionListener(new SelectionAdapter() {
120
 
            public void widgetSelected(SelectionEvent e) {
121
 
                if(!isEditing){
122
 
                    setBend(defaultBends[defaultBendList.getSelectionIndex()].getBend());
123
 
                    editor.redraw();
124
 
                }
125
 
                isEditing = false;
126
 
            }
127
 
        });                                    
128
 
 
129
 
        //------------------BUTTONS--------------------------  
130
 
        Button buttonClean = new Button(rightComposite, SWT.PUSH);
131
 
        buttonClean.setLayoutData(resizeData(new GridData(SWT.FILL,SWT.BOTTOM,true,true), 80,25));         
132
 
        buttonClean.setText(TuxGuitar.getProperty("clean"));
133
 
        buttonClean.addSelectionListener(new SelectionAdapter() {
134
 
            public void widgetSelected(SelectionEvent arg0) {
135
 
                result = null;
136
 
                dialog.dispose();
137
 
            }
138
 
        });                
139
 
        Button buttonOK = new Button(rightComposite, SWT.PUSH);
140
 
        buttonOK.setLayoutData(resizeData(new GridData(SWT.FILL,SWT.BOTTOM,true,false), 80,25));         
141
 
        buttonOK.setText(TuxGuitar.getProperty("ok"));
142
 
        buttonOK.addSelectionListener(new SelectionAdapter() {
143
 
            public void widgetSelected(SelectionEvent arg0) {  
144
 
                result = getBend();
145
 
                dialog.dispose();
146
 
            }
147
 
        });
148
 
        Button buttonCancel = new Button(rightComposite, SWT.PUSH);
149
 
        buttonCancel.setLayoutData(resizeData(new GridData(SWT.FILL,SWT.BOTTOM,true,false), 80,25));         
150
 
        buttonCancel.setText(TuxGuitar.getProperty("cancel"));
151
 
        buttonCancel.addSelectionListener(new SelectionAdapter() {
152
 
            public void widgetSelected(SelectionEvent arg0) {
153
 
                result = note.getEffect().getBend();
154
 
                dialog.dispose();
155
 
            }
156
 
        });
157
 
 
158
 
        dialog.pack();
159
 
        int x = shell.getBounds().x + (shell.getBounds().width - dialog.getSize().x) / 2;
160
 
        int y = shell.getBounds().y + (shell.getBounds().height - dialog.getSize().y) / 2;        
161
 
        dialog.setLocation(x, y);
162
 
        dialog.open(); 
163
 
        
164
 
                while (!dialog.isDisposed()) {
165
 
            if (!dialog.getDisplay().readAndDispatch()) {
166
 
                dialog.getDisplay().sleep();
167
 
            }
168
 
        }
169
 
        
170
 
                return result;          
171
 
        
172
 
    }    
173
 
    
174
 
    
175
 
    private GridData resizeData(GridData data,int minimumWidth,int minimumHeight){    
176
 
        data.minimumWidth = minimumWidth;
177
 
        data.minimumHeight = minimumHeight;    
178
 
        return data;
179
 
    }
180
 
    
181
 
    private void paintEditor(GC gc){        
182
 
        for(int i = 0;i < x.length;i++){
183
 
            this.setStyleX(gc,i);
184
 
            gc.drawLine(this.x[i],Y_SPAN,this.x[i],Y_SPAN + this.height);
185
 
        }
186
 
        for(int i = 0;i < y.length;i++){
187
 
            this.setStyleY(gc,i);
188
 
            gc.drawLine(X_SPAN,this.y[i],X_SPAN + this.width,this.y[i]);
189
 
        }       
190
 
        
191
 
        
192
 
        Iterator it = null;
193
 
        Point prevPoint = null;
194
 
        gc.setLineStyle(SWT.LINE_SOLID);
195
 
        gc.setLineWidth(2);
196
 
        gc.setForeground(this.editor.getDisplay().getSystemColor(SWT.COLOR_GRAY));        
197
 
        it = this.points.iterator();
198
 
        while(it.hasNext()){
199
 
            Point point = (Point)it.next();            
200
 
            if(prevPoint != null){                                
201
 
                gc.drawLine(prevPoint.x,prevPoint.y,point.x,point.y);
202
 
            }            
203
 
            prevPoint = point;
204
 
        }        
205
 
        
206
 
        
207
 
        gc.setLineWidth(5);
208
 
        gc.setForeground(this.editor.getDisplay().getSystemColor(SWT.COLOR_BLACK));                                       
209
 
        it = this.points.iterator();
210
 
        while(it.hasNext()){
211
 
            Point point = (Point)it.next();                       
212
 
 
213
 
            gc.drawRectangle(point.x - 2,point.y - 2,5,5);           
214
 
        }
215
 
        gc.setLineWidth(1);
216
 
    }
217
 
 
218
 
 
219
 
    
220
 
    private void setStyleX(GC gc,int i){
221
 
        gc.setLineStyle(SWT.LINE_SOLID);
222
 
        if(i == 0 || i == (X_LENGTH - 1)){
223
 
            gc.setForeground(this.editor.getDisplay().getSystemColor(SWT.COLOR_BLACK));
224
 
        }else{            
225
 
            gc.setForeground(this.editor.getDisplay().getSystemColor(SWT.COLOR_BLUE));
226
 
            if((i % 3) > 0){
227
 
                gc.setLineStyle(SWT.LINE_DOT);            
228
 
            }
229
 
        }
230
 
    }    
231
 
    
232
 
    private void setStyleY(GC gc,int i){
233
 
        gc.setLineStyle(SWT.LINE_SOLID);
234
 
        if(i == 0 || i == (Y_LENGTH - 1)){
235
 
            gc.setForeground(this.editor.getDisplay().getSystemColor(SWT.COLOR_BLACK));
236
 
        }else{         
237
 
            gc.setForeground(this.editor.getDisplay().getSystemColor(SWT.COLOR_RED));
238
 
        
239
 
            if((i % 2) > 0){
240
 
                gc.setLineStyle(SWT.LINE_DOT);
241
 
                gc.setForeground(this.editor.getDisplay().getSystemColor(SWT.COLOR_GRAY));
242
 
            }else if((i % 4) > 0){
243
 
                gc.setLineStyle(SWT.LINE_DOT);         
244
 
            }
245
 
        }
246
 
    }
247
 
    
248
 
    
249
 
    
250
 
    private void checkPoint(int x,int y){        
251
 
        Point point = new Point(this.getX(x),this.getY(y));
252
 
        if(!this.removePoint(point)){
253
 
            this.removePointsAtXLine(point.x);
254
 
            this.addPoint(point);
255
 
            this.orderPoints();
256
 
        }
257
 
    }
258
 
    
259
 
    private boolean removePoint(Point point){
260
 
        Iterator it = this.points.iterator();
261
 
        while(it.hasNext()){
262
 
            Point currPoint = (Point)it.next();
263
 
            if(currPoint.x == point.x && currPoint.y == point.y){
264
 
                this.points.remove(point);
265
 
                return true;
266
 
            }
267
 
        }  
268
 
        return false;
269
 
    }
270
 
    
271
 
    private void orderPoints(){
272
 
        for(int i = 0;i < this.points.size();i++){
273
 
            Point minPoint = null;
274
 
            for(int noteIdx = i;noteIdx < this.points.size();noteIdx++){
275
 
                Point point = (Point)this.points.get(noteIdx);
276
 
                if(minPoint == null || point.x < minPoint.x){
277
 
                    minPoint = point;
278
 
                }
279
 
            }
280
 
            this.points.remove(minPoint);
281
 
            this.points.add(i,minPoint);
282
 
        }
283
 
    }    
284
 
    
285
 
    private void removePointsAtXLine(int x){
286
 
        Iterator it = this.points.iterator();
287
 
        while(it.hasNext()){
288
 
            Point point = (Point)it.next();
289
 
            if(point.x == x){
290
 
                this.points.remove(point);
291
 
                break;
292
 
            }
293
 
        }  
294
 
    }     
295
 
    
296
 
    private void addPoint(Point point){
297
 
        this.points.add(point);
298
 
    }
299
 
    
300
 
    private int getX(int pointX){  
301
 
        int currPointX = -1;        
302
 
        for(int i = 0;i < this.x.length;i++){                    
303
 
            if(currPointX < 0){
304
 
                currPointX = this.x[i];
305
 
            }else{                    
306
 
                int distanceX = Math.abs(pointX - currPointX);
307
 
                int currDistanceX = Math.abs(pointX - this.x[i]);
308
 
                if(currDistanceX < distanceX){
309
 
                    currPointX = this.x[i];
310
 
                }
311
 
            }
312
 
            
313
 
        }
314
 
        return currPointX;
315
 
    }     
316
 
    
317
 
    private int getY(int pointY){  
318
 
        int currPointY = -1;        
319
 
        for(int i = 0;i < this.y.length;i++){                    
320
 
            if(currPointY < 0){
321
 
                currPointY = this.y[i];
322
 
            }else{                    
323
 
                int distanceX = Math.abs(pointY - currPointY);
324
 
                int currDistanceX = Math.abs(pointY - this.y[i]);
325
 
                if(currDistanceX < distanceX){
326
 
                    currPointY = this.y[i];
327
 
                }
328
 
            }
329
 
            
330
 
        }
331
 
        return currPointY;
332
 
    }  
333
 
    
334
 
    public boolean isEmpty(){
335
 
        return this.points.isEmpty();
336
 
    }
337
 
    
338
 
    public BendEffect getBend(){
339
 
        if(this.points != null && !this.points.isEmpty()){
340
 
                BendEffect bend = new BendEffect();     
341
 
                Iterator it = this.points.iterator();
342
 
                while(it.hasNext()){
343
 
                        Point point = (Point)it.next();            
344
 
                        addBendPoint(bend,point);
345
 
                }              
346
 
                return bend;
347
 
        }
348
 
        return null;
349
 
    }
350
 
    
351
 
    private void addBendPoint(BendEffect effect,Point point){
352
 
        int position = 0;
353
 
        int value = 0;
354
 
        for(int i=0;i<this.x.length;i++){
355
 
            if(point.x == this.x[i]){
356
 
                position = i;
357
 
            }
358
 
        }
359
 
        for(int i=0;i<this.y.length;i++){
360
 
            if(point.y == this.y[i]){
361
 
                value = (this.y.length - i) -1;
362
 
            }
363
 
        }        
364
 
        effect.addPoint(position,value); 
365
 
    }
366
 
    
367
 
    
368
 
    public void setBend(BendEffect effect){
369
 
        this.points.clear();
370
 
        Iterator it = effect.getPoints().iterator();
371
 
        while(it.hasNext()){
372
 
            BendEffect.BendPoint bendPoint = (BendEffect.BendPoint)it.next();
373
 
            this.makePoint(bendPoint);
374
 
        }
375
 
    }
376
 
    
377
 
    private void makePoint(BendEffect.BendPoint bendPoint){
378
 
        int indexX = bendPoint.getPosition();
379
 
        int indexY = (this.y.length - bendPoint.getValue()) - 1;        
380
 
        if(indexX >= 0 && indexX < this.x.length && indexY >= 0 && indexY < this.y.length){
381
 
                Point point = new Point(0,0);
382
 
                point.x = this.x[indexX];
383
 
                point.y = this.y[indexY];         
384
 
                this.points.add(point);
385
 
        }
386
 
    }
387
 
    
388
 
    
389
 
    public int getWidth(){
390
 
        return this.width;        
391
 
    }
392
 
    
393
 
    public int getHeight(){
394
 
        return this.height;
395
 
    }
396
 
    
397
 
  
398
 
    private void resetDefaultBends(){
399
 
        this.defaultBends = new DefaultBend[5];
400
 
        
401
 
        this.defaultBends[0] = new DefaultBend(TuxGuitar.getProperty("bend.bend"),new BendEffect());        
402
 
        this.defaultBends[0].getBend().addPoint(0,0);
403
 
        this.defaultBends[0].getBend().addPoint(6,(BendEffect.SEMITONE_LENGTH * 2));
404
 
        this.defaultBends[0].getBend().addPoint(12,(BendEffect.SEMITONE_LENGTH * 2));               
405
 
        
406
 
        this.defaultBends[1] = new DefaultBend(TuxGuitar.getProperty("bend.bend-release"),new BendEffect());         
407
 
        this.defaultBends[1].getBend().addPoint(0,0);
408
 
        this.defaultBends[1].getBend().addPoint(3,(BendEffect.SEMITONE_LENGTH * 2));
409
 
        this.defaultBends[1].getBend().addPoint(6,(BendEffect.SEMITONE_LENGTH * 2));
410
 
        this.defaultBends[1].getBend().addPoint(9,0);
411
 
        this.defaultBends[1].getBend().addPoint(12,0);
412
 
                
413
 
        this.defaultBends[2] = new DefaultBend(TuxGuitar.getProperty("bend.bend-release-bend"),new BendEffect());         
414
 
        this.defaultBends[2].getBend().addPoint(0,0);
415
 
        this.defaultBends[2].getBend().addPoint(2,(BendEffect.SEMITONE_LENGTH * 2));
416
 
        this.defaultBends[2].getBend().addPoint(4,(BendEffect.SEMITONE_LENGTH * 2));
417
 
        this.defaultBends[2].getBend().addPoint(6,0);
418
 
        this.defaultBends[2].getBend().addPoint(8,0);
419
 
        this.defaultBends[2].getBend().addPoint(10,(BendEffect.SEMITONE_LENGTH * 2));
420
 
        this.defaultBends[2].getBend().addPoint(12,(BendEffect.SEMITONE_LENGTH * 2));
421
 
        
422
 
        this.defaultBends[3] = new DefaultBend(TuxGuitar.getProperty("bend.prebend"),new BendEffect());         
423
 
        this.defaultBends[3].getBend().addPoint(0,(BendEffect.SEMITONE_LENGTH * 2));
424
 
        this.defaultBends[3].getBend().addPoint(12,(BendEffect.SEMITONE_LENGTH * 2));
425
 
        
426
 
        this.defaultBends[4] = new DefaultBend(TuxGuitar.getProperty("bend.prebend-release"),new BendEffect());         
427
 
        this.defaultBends[4].getBend().addPoint(0,(BendEffect.SEMITONE_LENGTH * 2));
428
 
        this.defaultBends[4].getBend().addPoint(4,(BendEffect.SEMITONE_LENGTH * 2));
429
 
        this.defaultBends[4].getBend().addPoint(8,0);
430
 
        this.defaultBends[4].getBend().addPoint(12,0);
431
 
    }       
432
 
    
433
 
 
434
 
    
435
 
    private class DefaultBend{
436
 
        private String name;
437
 
        private BendEffect bend;
438
 
        
439
 
        public DefaultBend(String name,BendEffect bend){
440
 
            this.name = name;
441
 
            this.bend = bend;
442
 
        }                
443
 
        public BendEffect getBend() {
444
 
            return bend;
445
 
        }
446
 
        public String getName() {
447
 
            return name;
448
 
        }
449
 
    }        
450
 
}