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.chord;
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.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;
29
* TODO To change the template for this generated type comment go to
30
* Window - Preferences - Java - Code Style - Code Templates
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;
39
private Canvas canvas;
40
private boolean[] fisrtFrets;
41
private int[] strings;
44
private short maxStrings;
49
public ChordEditor(Composite parent, int style) {
53
public ChordEditor(Composite parent, int style,short maxStrings) {
55
this.setLayout(new GridLayout());
56
this.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
57
this.init(maxStrings);
60
public void init(short maxStrings){
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();
70
for(int i = 0;i < fisrtFrets.length;i++){
71
this.fisrtFrets[i] = false;
73
for(int i = 0;i < strings.length;i++){
74
this.strings[i] = ((i + 1) * STRING_SPAN);
76
for(int i = 0;i < frets.length;i++){
77
this.frets[i] = ((i + 1) * FRET_SPAN);
83
Composite composite = new Composite(this,SWT.NONE);
84
composite.setLayout(new GridLayout());
85
composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
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) {
95
this.canvas.addMouseListener(new MouseListener() {
96
public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
99
public void mouseDown(org.eclipse.swt.events.MouseEvent e) {
102
public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
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);
119
this.canvas.setLayoutData(makeCanvasData());
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));
130
private void paintEditor(GC gc){
131
gc.setForeground(this.getDisplay().getSystemColor(SWT.COLOR_BLACK));
134
gc.drawLine((STRING_SPAN - 10),(FRET_SPAN - 10),STRING_SPAN + (this.width + 10),(FRET_SPAN - 10));
136
gc.drawString(Integer.toString(getFret()),FRET_SPAN - 20,STRING_SPAN);
139
for(int i = 0;i < strings.length;i++){
140
gc.drawLine(this.strings[i],FRET_SPAN,this.strings[i],FRET_SPAN + this.height);
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]);
149
//gc.setForeground(this.getDisplay().getSystemColor(SWT.COLOR_BLACK));
150
Iterator it = this.points.iterator();
152
Point point = (Point)it.next();
154
gc.drawOval(point.x - 2,point.y + (FRET_SPAN / 2),5,5);
158
//dibujo las notas al aire
159
for(int i = 0;i < fisrtFrets.length;i++){
162
int x = this.strings[i] - 7;
163
int y = FRET_SPAN - 27;
164
gc.drawOval(x,y,14,14);
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);
175
private void checkPoint(int x,int y){
176
int stringIndex = getStringIndex(x);
177
int fretIndex = getFretIndex(y);
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);
193
private boolean removePoint(Point point){
194
Iterator it = this.points.iterator();
196
Point currPoint = (Point)it.next();
197
if(currPoint.x == point.x && currPoint.y == point.y){
198
this.points.remove(point);
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){
214
this.points.remove(minPoint);
215
this.points.add(i,minPoint);
219
private void removePointsAtStringLine(int x){
220
Iterator it = this.points.iterator();
222
Point point = (Point)it.next();
224
this.points.remove(point);
230
private void addPoint(Point point){
231
this.points.add(point);
234
private int getStringIndex(int x){
236
for(int i = 0;i < this.strings.length;i++){
240
int distanceX = Math.abs(x - this.strings[index]);
241
int currDistanceX = Math.abs(x - this.strings[i]);
242
if(currDistanceX < distanceX){
251
private int getFretIndex(int y){
253
for(int i = 0;i < this.frets.length;i++){
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){
268
private boolean hasPoints(int stringIndex){
269
Iterator it = this.points.iterator();
271
Point point = (Point)it.next();
272
if(point.x == this.strings[stringIndex]){
279
public boolean isEmpty(){
280
return this.points.isEmpty();
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));
291
public int getValue(int string){
293
if(this.fisrtFrets[this.maxStrings - string]){
298
Iterator it = this.points.iterator();
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);
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]);
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]));
326
public short getFret() {
329
public void setFret(short fret) {
333
private void setFret(short fret,boolean updateScroll){
334
if(fret >= MIN_FRET && fret <= MAX_FRET){
338
canvas.getVerticalBar().setSelection(this.fret);
342
public short getMaxStrings() {
345
public void setMaxStrings(short maxStrings) {
346
this.maxStrings = maxStrings;
348
public int getWidth(){
352
public int getHeight(){
356
public void redraw(){
358
this.canvas.redraw();