2
* Created on 01-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.tab;
9
import org.eclipse.swt.graphics.Color;
10
import org.eclipse.swt.graphics.Device;
11
import org.eclipse.swt.graphics.Font;
12
import org.eclipse.swt.graphics.GC;
13
import org.eclipse.swt.graphics.Image;
14
import org.eclipse.swt.graphics.Point;
15
import org.herac.tuxguitar.gui.editors.TGPainter;
16
import org.herac.tuxguitar.gui.editors.tab.layout.TrackSpacing;
17
import org.herac.tuxguitar.gui.editors.tab.layout.ViewLayout;
18
import org.herac.tuxguitar.song.models.TGChord;
19
import org.herac.tuxguitar.song.models.TGString;
23
* TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
25
public class TGChordImpl extends TGChord {
27
public static final int MAX_FRETS = 6;
35
private int diagramWidth;
36
private int diagramHeight;
37
private int nameWidth;
38
private int nameHeight;
39
private Image diagram;
40
private Color foregroundColor;
41
private Color backgroundColor;
42
private Color noteColor;
43
private Color tonicColor;
46
private Font firstFretFont;
47
private int firstFretSpacing;
48
private int stringSpacing;
49
private int fretSpacing;
52
private boolean editing;
54
public TGChordImpl(int length) {
58
public void setStyle(int style) {
62
public void setTonic(int tonic){
66
public void setPosX(int posX){
70
public void setPosY(int posY){
74
public int getPosY() {
78
public int getWidth(){
82
public int getHeight(){
86
public Color getForegroundColor() {
87
return this.foregroundColor;
90
public void setForegroundColor(Color foregroundColor) {
91
this.foregroundColor = foregroundColor;
94
public Color getBackgroundColor() {
95
return this.backgroundColor;
98
public void setBackgroundColor(Color backgroundColor) {
99
this.backgroundColor = backgroundColor;
102
public Color getColor() {
106
public void setColor(Color color) {
110
public Color getNoteColor() {
111
return this.noteColor;
114
public void setNoteColor(Color noteColor) {
115
this.noteColor = noteColor;
118
public Color getTonicColor() {
119
return this.tonicColor;
122
public void setTonicColor(Color tonicColor) {
123
this.tonicColor = tonicColor;
126
public int getFirstFretSpacing() {
127
return this.firstFretSpacing;
130
public void setFirstFretSpacing(int firstFretSpacing) {
131
this.firstFretSpacing = firstFretSpacing;
134
public int getFretSpacing() {
135
return this.fretSpacing;
138
public void setFretSpacing(int fretSpacing) {
139
this.fretSpacing = fretSpacing;
142
public int getNoteSize() {
143
return this.noteSize;
146
public void setNoteSize(int noteSize) {
147
this.noteSize = noteSize;
150
public int getStringSpacing() {
151
return this.stringSpacing;
154
public void setStringSpacing(int stringSpacing) {
155
this.stringSpacing = stringSpacing;
158
public Font getFont() {
162
public void setFont(Font font) {
166
public Font getFirstFretFont() {
167
return this.firstFretFont;
170
public void setFirstFretFont(Font firstFretFont) {
171
this.firstFretFont = firstFretFont;
174
public boolean isEditing() {
178
public void setEditing(boolean editing) {
179
this.editing = editing;
182
public void paint(ViewLayout layout, TGPainter painter, int fromX, int fromY) {
183
layout.setChordStyle(this);
184
this.setPosY(getPaintPosition(TrackSpacing.POSITION_CHORD));
185
this.setEditing(false);
186
this.update(painter, layout.isBufferEnabled());
187
this.paint(painter,getBeatImpl().getSpacing() + fromX + Math.round(4f * layout.getScale()), fromY);
190
public void paint(TGPainter painter, int fromX, int fromY){
191
int x = (fromX + getPosX());
192
int y = (fromY + getPosY());
193
if( (this.style & ViewLayout.DISPLAY_CHORD_DIAGRAM) != 0 ){
194
if(this.diagram != null){
195
painter.drawImage(this.diagram,x - ( (this.diagramWidth - getFirstFretSpacing()) / 2) - getFirstFretSpacing() ,y);
197
paintDiagram(painter,x - ( (this.diagramWidth - getFirstFretSpacing()) / 2) - getFirstFretSpacing() ,y);
200
if( (this.style & ViewLayout.DISPLAY_CHORD_NAME) != 0 && getName() != null && getName().length() > 0){
201
painter.setFont(getFont());
202
painter.setForeground(getForegroundColor());
203
painter.setBackground(getBackgroundColor());
204
painter.drawString(getName(),x - (this.nameWidth / 2) , y + (this.height - this.nameHeight ) );
208
public void update(TGPainter painter, boolean makeBuffer) {
211
if(getFirstFret() <= 0 ){
212
this.calculateFirstFret();
214
if( (this.style & ViewLayout.DISPLAY_CHORD_NAME) != 0 ){
215
this.updateName(painter);
216
this.width = Math.max(this.width,this.nameWidth);
217
this.height += this.nameHeight;
219
if( (this.style & ViewLayout.DISPLAY_CHORD_DIAGRAM) != 0 ){
220
this.updateDiagram( (makeBuffer ? painter.getGC().getDevice() : null ) );
221
this.width = Math.max(this.width,this.diagramWidth);
222
this.height += this.diagramHeight;
226
protected void updateName(TGPainter painter){
227
String name = getName();
228
if(painter == null || name == null || name.length() == 0){
233
Point point = painter.getStringExtent(name);
234
this.nameWidth = point.x;
235
this.nameHeight = point.y;
238
protected void updateDiagram(Device device){
239
Font font = getFirstFretFont();
240
this.diagramWidth = getStringSpacing() + (getStringSpacing() * countStrings()) + ((font != null)?getFirstFretSpacing():0);
241
this.diagramHeight = getFretSpacing() + (getFretSpacing() * MAX_FRETS);
242
if(device != null && (this.diagram == null || this.diagram.isDisposed())){
243
this.diagram = new Image(device,this.diagramWidth,this.diagramHeight);
244
TGPainter painter = new TGPainter(new GC(this.diagram));
245
paintDiagram(painter, 0, 0);
250
protected void paintDiagram(TGPainter painter, int fromX, int fromY){
251
Font font = getFirstFretFont();
252
painter.setBackground(getBackgroundColor());
253
painter.initPath(TGPainter.PATH_FILL);
254
painter.addRectangle(fromX, fromY, this.diagramWidth, this.diagramHeight);
256
painter.setForeground(getColor());
259
int x = fromX + getStringSpacing();
260
int y = fromY + getFretSpacing();
263
String firstFretString = Integer.toString(getFirstFret());
264
painter.setFont(font);
265
Point size = painter.getStringExtent(firstFretString);
266
painter.drawString(firstFretString,fromX + (getFirstFretSpacing() - size.x),Math.round(y + ((getFretSpacing() / 2f) - (size.y / 2f))));
267
x += getFirstFretSpacing();
271
for(int i = 0;i < getStrings().length;i++){
272
int x1 = x + (i * getStringSpacing());
273
int x2 = x + (i * getStringSpacing());
275
int y2 = y + ((getFretSpacing() * (MAX_FRETS - 1)));
276
//painter.drawLine(x1,y1,x2,y2);
277
painter.moveTo(x1,y1);
278
painter.lineTo(x2,y2);
282
//dibujo las cegillas
284
for(int i = 0;i < MAX_FRETS;i++){
286
int x2 = x + ((getStringSpacing() * (countStrings() - 1)));
287
int y1 = y + (i * getFretSpacing());
288
int y2 = y + (i * getFretSpacing());
289
//painter.drawLine(x1,y1,x2,y2);
290
painter.moveTo(x1,y1);
291
painter.lineTo(x2,y2);
295
painter.setLineWidth(1);
297
for(int i = 0;i < getStrings().length;i++){
298
int fret = getFretValue(i);
299
int noteX = x + ((getStringSpacing() * (countStrings() - 1)) - (getStringSpacing() * i));
302
painter.moveTo((noteX - (getNoteSize() / 2)), fromY);
303
painter.lineTo((noteX + (getNoteSize() / 2)), fromY + getNoteSize());
304
painter.moveTo((noteX + (getNoteSize() / 2)), fromY);
305
painter.lineTo((noteX - (getNoteSize() / 2)), fromY + getNoteSize());
310
painter.addOval(noteX - (getNoteSize() / 2),fromY,getNoteSize(),getNoteSize());
314
painter.setBackground( (this.tonic >= 0 && ( (getStringValue(i + 1) + fret) % 12) == this.tonic)?getTonicColor():getNoteColor());
315
painter.initPath(TGPainter.PATH_FILL);
316
fret -= (getFirstFret() - 1);
317
int noteY = y + ((getFretSpacing() * fret) - (getFretSpacing() / 2 ));
318
painter.addOval(noteX - (getNoteSize() / 2),noteY - (getNoteSize() / 2),(getNoteSize() + 1),(getNoteSize() + 1));
324
public void calculateFirstFret(){
327
boolean zero = false;
328
for (int i = 0; i < getStrings().length; i++) {
329
int fretValue = getFretValue(i);
330
zero = (zero || fretValue == 0);
332
minimun = (minimun < 0)?fretValue:Math.min(minimun,fretValue);
333
maximun = (Math.max(maximun,fretValue));
336
int firstFret = (zero && maximun < MAX_FRETS)?1:minimun;
337
setFirstFret( Math.max(firstFret,1) );
340
protected int getStringValue(int number){
341
TGString string = getBeat().getMeasure().getTrack().getString(number);
342
return string.getValue();
345
public boolean isDisposed(){
346
return (this.diagram == null || this.diagram.isDisposed());
349
public void dispose(){
351
this.diagram.dispose();
355
public int getPosX() {
356
return (isEditing())?this.posX:getBeatImpl().getPosX();
359
public int getPaintPosition(int index){
360
return getBeatImpl().getMeasureImpl().getTs().getPosition(index);
363
public TGBeatImpl getBeatImpl(){
364
return (TGBeatImpl)getBeat();
b'\\ No newline at end of file'