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.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.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;
32
* TODO To change the template for this generated type comment go to
33
* Window - Preferences - Java - Code Style - Code Templates
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;
41
private Composite editor;
48
private DefaultBend[] defaultBends;
49
private boolean isEditing;
51
private BendEffect result;
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();
64
for(int i = 0;i < x.length;i++){
65
this.x[i] = ((i + 1) * X_SPAN);
67
for(int i = 0;i < y.length;i++){
68
this.y[i] = ((i + 1) * Y_SPAN);
72
public BendEffect show(Shell shell,final Note note){
73
final Shell dialog = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
75
dialog.setLayout(new GridLayout());
76
dialog.setText(TuxGuitar.getProperty("bend.editor"));
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));
83
Composite leftComposite = new Composite(composite,SWT.NONE);
84
leftComposite.setLayout(new GridLayout());
85
leftComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
87
Composite rightComposite = new Composite(composite,SWT.NONE);
88
rightComposite.setLayout(new GridLayout());
89
rightComposite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
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) {
102
this.editor.addMouseListener(new MouseAdapter() {
103
public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
109
setBend(note.getEffect().getBend());
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());
118
defaultBendList.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
119
defaultBendList.addSelectionListener(new SelectionAdapter() {
120
public void widgetSelected(SelectionEvent e) {
122
setBend(defaultBends[defaultBendList.getSelectionIndex()].getBend());
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) {
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) {
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();
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);
164
while (!dialog.isDisposed()) {
165
if (!dialog.getDisplay().readAndDispatch()) {
166
dialog.getDisplay().sleep();
175
private GridData resizeData(GridData data,int minimumWidth,int minimumHeight){
176
data.minimumWidth = minimumWidth;
177
data.minimumHeight = minimumHeight;
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);
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]);
193
Point prevPoint = null;
194
gc.setLineStyle(SWT.LINE_SOLID);
196
gc.setForeground(this.editor.getDisplay().getSystemColor(SWT.COLOR_GRAY));
197
it = this.points.iterator();
199
Point point = (Point)it.next();
200
if(prevPoint != null){
201
gc.drawLine(prevPoint.x,prevPoint.y,point.x,point.y);
208
gc.setForeground(this.editor.getDisplay().getSystemColor(SWT.COLOR_BLACK));
209
it = this.points.iterator();
211
Point point = (Point)it.next();
213
gc.drawRectangle(point.x - 2,point.y - 2,5,5);
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));
225
gc.setForeground(this.editor.getDisplay().getSystemColor(SWT.COLOR_BLUE));
227
gc.setLineStyle(SWT.LINE_DOT);
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));
237
gc.setForeground(this.editor.getDisplay().getSystemColor(SWT.COLOR_RED));
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);
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);
259
private boolean removePoint(Point point){
260
Iterator it = this.points.iterator();
262
Point currPoint = (Point)it.next();
263
if(currPoint.x == point.x && currPoint.y == point.y){
264
this.points.remove(point);
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){
280
this.points.remove(minPoint);
281
this.points.add(i,minPoint);
285
private void removePointsAtXLine(int x){
286
Iterator it = this.points.iterator();
288
Point point = (Point)it.next();
290
this.points.remove(point);
296
private void addPoint(Point point){
297
this.points.add(point);
300
private int getX(int pointX){
302
for(int i = 0;i < this.x.length;i++){
304
currPointX = this.x[i];
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];
317
private int getY(int pointY){
319
for(int i = 0;i < this.y.length;i++){
321
currPointY = this.y[i];
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];
334
public boolean isEmpty(){
335
return this.points.isEmpty();
338
public BendEffect getBend(){
339
if(this.points != null && !this.points.isEmpty()){
340
BendEffect bend = new BendEffect();
341
Iterator it = this.points.iterator();
343
Point point = (Point)it.next();
344
addBendPoint(bend,point);
351
private void addBendPoint(BendEffect effect,Point point){
354
for(int i=0;i<this.x.length;i++){
355
if(point.x == this.x[i]){
359
for(int i=0;i<this.y.length;i++){
360
if(point.y == this.y[i]){
361
value = (this.y.length - i) -1;
364
effect.addPoint(position,value);
368
public void setBend(BendEffect effect){
370
Iterator it = effect.getPoints().iterator();
372
BendEffect.BendPoint bendPoint = (BendEffect.BendPoint)it.next();
373
this.makePoint(bendPoint);
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);
389
public int getWidth(){
393
public int getHeight(){
398
private void resetDefaultBends(){
399
this.defaultBends = new DefaultBend[5];
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));
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);
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));
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));
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);
435
private class DefaultBend{
437
private BendEffect bend;
439
public DefaultBend(String name,BendEffect bend){
443
public BendEffect getBend() {
446
public String getName() {