2
* Created on 28-dic-2005
4
* TODO To change the template for this generated file go to
5
* Window - Preferences - Java - Code Style - Code Templates
7
package org.herac.tuxguitar.gui.editors.effects;
9
import java.util.ArrayList;
10
import java.util.Iterator;
11
import java.util.List;
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;
33
* TODO To change the template for this generated type comment go to
34
* Window - Preferences - Java - Code Style - Code Templates
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;
47
protected Composite editor;
48
protected DefaultBend[] defaultBends;
49
protected TGEffectBend result;
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();
62
for(int i = 0;i < this.x.length;i++){
63
this.x[i] = ((i + 1) * X_SPACING);
65
for(int i = 0;i < this.y.length;i++){
66
this.y[i] = ((i + 1) * Y_SPACING);
70
public TGEffectBend show(Shell shell,final TGNote note){
71
final Shell dialog = DialogUtils.newDialog(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
73
dialog.setLayout(new GridLayout());
74
dialog.setText(TuxGuitar.getProperty("bend.editor"));
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));
81
Composite leftComposite = new Composite(composite,SWT.NONE);
82
leftComposite.setLayout(new GridLayout());
83
leftComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
85
Composite rightComposite = new Composite(composite,SWT.NONE);
86
rightComposite.setLayout(new GridLayout());
87
rightComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
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);
99
this.editor.addMouseListener(new MouseAdapter() {
100
public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
102
BendEditor.this.editor.redraw();
106
//-------------DEFAULT BEND LIST---------------------------------------------------
107
this.resetDefaultBends();
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());
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();
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;
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();
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();
154
if(note.getEffect().isBend()){
155
setBend(note.getEffect().getBend());
157
setBend(this.defaultBends[0].getBend());
160
dialog.setDefaultButton( buttonOK );
162
DialogUtils.openDialog(dialog, DialogUtils.OPEN_STYLE_CENTER | DialogUtils.OPEN_STYLE_PACK | DialogUtils.OPEN_STYLE_WAIT);
167
private GridData resizeData(GridData data,int minimumWidth,int minimumHeight){
168
data.minimumWidth = minimumWidth;
169
data.minimumHeight = minimumHeight;
173
protected void paintEditor(TGPainter painter){
174
for(int i = 0;i < this.x.length;i++){
175
this.setStyleX(painter,i);
177
painter.moveTo(this.x[i],Y_SPACING);
178
painter.lineTo(this.x[i],Y_SPACING + this.height);
181
for(int i = 0;i < this.y.length;i++){
182
this.setStyleY(painter,i);
184
painter.moveTo(X_SPACING,this.y[i]);
185
painter.lineTo(X_SPACING + this.width,this.y[i]);
190
Point prevPoint = null;
191
painter.setLineStyle(SWT.LINE_SOLID);
192
painter.setLineWidth(2);
193
painter.setForeground(this.editor.getDisplay().getSystemColor(SWT.COLOR_GRAY));
195
it = this.points.iterator();
197
Point point = (Point)it.next();
198
if(prevPoint != null){
199
//painter.drawLine(prevPoint.x,prevPoint.y,point.x,point.y);
201
painter.moveTo(prevPoint.x,prevPoint.y);
202
painter.lineTo(point.x,point.y);
208
painter.setLineWidth(5);
209
painter.setForeground(this.editor.getDisplay().getSystemColor(SWT.COLOR_BLACK));
211
it = this.points.iterator();
213
Point point = (Point)it.next();
215
painter.addRectangle(point.x - 2,point.y - 2,5,5);
218
painter.setLineWidth(1);
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));
226
painter.setForeground(this.editor.getDisplay().getSystemColor(SWT.COLOR_BLUE));
228
painter.setLineStyle(SWT.LINE_DOT);
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));
238
painter.setForeground(this.editor.getDisplay().getSystemColor(SWT.COLOR_RED));
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);
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);
258
protected boolean removePoint(Point point){
259
Iterator it = this.points.iterator();
261
Point currPoint = (Point)it.next();
262
if(currPoint.x == point.x && currPoint.y == point.y){
263
this.points.remove(point);
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){
279
this.points.remove(minPoint);
280
this.points.add(i,minPoint);
284
protected void removePointsAtXLine(int x){
285
Iterator it = this.points.iterator();
287
Point point = (Point)it.next();
289
this.points.remove(point);
295
protected void addPoint(Point point){
296
this.points.add(point);
299
protected int getX(int pointX){
301
for(int i = 0;i < this.x.length;i++){
303
currPointX = this.x[i];
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];
315
protected int getY(int pointY){
317
for(int i = 0;i < this.y.length;i++){
319
currPointY = this.y[i];
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];
331
public boolean isEmpty(){
332
return this.points.isEmpty();
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();
340
Point point = (Point)it.next();
341
addBendPoint(bend,point);
348
private void addBendPoint(TGEffectBend effect,Point point){
351
for(int i=0;i<this.x.length;i++){
352
if(point.x == this.x[i]){
356
for(int i=0;i<this.y.length;i++){
357
if(point.y == this.y[i]){
358
value = (this.y.length - i) -1;
361
effect.addPoint(position,value);
364
public void setBend(TGEffectBend effect){
366
Iterator it = effect.getPoints().iterator();
368
TGEffectBend.BendPoint bendPoint = (TGEffectBend.BendPoint)it.next();
369
this.makePoint(bendPoint);
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);
384
public int getWidth(){
388
public int getHeight(){
392
private void resetDefaultBends(){
393
this.defaultBends = new DefaultBend[5];
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));
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);
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));
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));
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);
427
private class DefaultBend{
429
private TGEffectBend bend;
431
public DefaultBend(String name,TGEffectBend bend){
436
public TGEffectBend getBend() {
440
public String getName() {