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

« back to all changes in this revision

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