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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/editors/tab/TGChordImpl.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mfrom: (1.1.1 upstream) (2.1.3 hardy)
  • Revision ID: james.westby@ubuntu.com-20080619003030-agens2gvd5m4dacu
New upstream release (Closes: #481728) also (LP: #176979, #212207)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Created on 01-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.tab;
 
8
 
 
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;
 
20
/**
 
21
 * @author julian
 
22
 * 
 
23
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
 
24
 */
 
25
public class TGChordImpl extends TGChord {
 
26
        
 
27
        public static final int MAX_FRETS = 6;
 
28
        
 
29
        private int style;
 
30
        private int posX;
 
31
        private int posY;
 
32
        private int width;
 
33
        private int height;
 
34
        private int tonic;
 
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;
 
44
        private Color color;
 
45
        private Font font;
 
46
        private Font firstFretFont;
 
47
        private int firstFretSpacing;
 
48
        private int stringSpacing;
 
49
        private int fretSpacing;
 
50
        private int noteSize;
 
51
        
 
52
        private boolean editing;
 
53
        
 
54
        public TGChordImpl(int length) {
 
55
                super(length);
 
56
        }
 
57
        
 
58
        public void setStyle(int style) {
 
59
                this.style = style;
 
60
        }
 
61
        
 
62
        public void setTonic(int tonic){
 
63
                this.tonic = tonic;
 
64
        }
 
65
        
 
66
        public void setPosX(int posX){
 
67
                this.posX = posX;
 
68
        }
 
69
        
 
70
        public void setPosY(int posY){
 
71
                this.posY = posY;
 
72
        }
 
73
        
 
74
        public int getPosY() {
 
75
                return this.posY;
 
76
        }
 
77
        
 
78
        public int getWidth(){
 
79
                return this.width;
 
80
        }
 
81
        
 
82
        public int getHeight(){
 
83
                return this.height;
 
84
        }
 
85
        
 
86
        public Color getForegroundColor() {
 
87
                return this.foregroundColor;
 
88
        }
 
89
        
 
90
        public void setForegroundColor(Color foregroundColor) {
 
91
                this.foregroundColor = foregroundColor;
 
92
        }
 
93
        
 
94
        public Color getBackgroundColor() {
 
95
                return this.backgroundColor;
 
96
        }
 
97
        
 
98
        public void setBackgroundColor(Color backgroundColor) {
 
99
                this.backgroundColor = backgroundColor;
 
100
        }
 
101
        
 
102
        public Color getColor() {
 
103
                return this.color;
 
104
        }
 
105
        
 
106
        public void setColor(Color color) {
 
107
                this.color = color;
 
108
        }
 
109
        
 
110
        public Color getNoteColor() {
 
111
                return this.noteColor;
 
112
        }
 
113
        
 
114
        public void setNoteColor(Color noteColor) {
 
115
                this.noteColor = noteColor;
 
116
        }
 
117
        
 
118
        public Color getTonicColor() {
 
119
                return this.tonicColor;
 
120
        }
 
121
        
 
122
        public void setTonicColor(Color tonicColor) {
 
123
                this.tonicColor = tonicColor;
 
124
        }
 
125
        
 
126
        public int getFirstFretSpacing() {
 
127
                return this.firstFretSpacing;
 
128
        }
 
129
        
 
130
        public void setFirstFretSpacing(int firstFretSpacing) {
 
131
                this.firstFretSpacing = firstFretSpacing;
 
132
        }
 
133
        
 
134
        public int getFretSpacing() {
 
135
                return this.fretSpacing;
 
136
        }
 
137
        
 
138
        public void setFretSpacing(int fretSpacing) {
 
139
                this.fretSpacing = fretSpacing;
 
140
        }
 
141
        
 
142
        public int getNoteSize() {
 
143
                return this.noteSize;
 
144
        }
 
145
        
 
146
        public void setNoteSize(int noteSize) {
 
147
                this.noteSize = noteSize;
 
148
        }
 
149
        
 
150
        public int getStringSpacing() {
 
151
                return this.stringSpacing;
 
152
        }
 
153
        
 
154
        public void setStringSpacing(int stringSpacing) {
 
155
                this.stringSpacing = stringSpacing;
 
156
        }
 
157
        
 
158
        public Font getFont() {
 
159
                return this.font;
 
160
        }
 
161
        
 
162
        public void setFont(Font font) {
 
163
                this.font = font;
 
164
        }
 
165
        
 
166
        public Font getFirstFretFont() {
 
167
                return this.firstFretFont;
 
168
        }
 
169
        
 
170
        public void setFirstFretFont(Font firstFretFont) {
 
171
                this.firstFretFont = firstFretFont;
 
172
        }
 
173
        
 
174
        public boolean isEditing() {
 
175
                return this.editing;
 
176
        }
 
177
        
 
178
        public void setEditing(boolean editing) {
 
179
                this.editing = editing;
 
180
        }
 
181
        
 
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);
 
188
        }
 
189
        
 
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);
 
196
                        }else{
 
197
                                paintDiagram(painter,x - ( (this.diagramWidth - getFirstFretSpacing()) / 2) - getFirstFretSpacing() ,y);
 
198
                        }
 
199
                }
 
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 ) );
 
205
                }
 
206
        }
 
207
        
 
208
        public void update(TGPainter painter, boolean makeBuffer) {
 
209
                this.width = 0;
 
210
                this.height = 0;
 
211
                if(getFirstFret() <= 0 ){
 
212
                        this.calculateFirstFret();
 
213
                }
 
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;
 
218
                }
 
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;
 
223
                }
 
224
        }
 
225
        
 
226
        protected void updateName(TGPainter painter){
 
227
                String name = getName();
 
228
                if(painter == null || name == null || name.length() == 0){
 
229
                        this.nameWidth = 0;
 
230
                        this.nameHeight = 0;
 
231
                        return;
 
232
                }
 
233
                Point point = painter.getStringExtent(name);
 
234
                this.nameWidth = point.x;
 
235
                this.nameHeight = point.y;
 
236
        }
 
237
        
 
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);
 
246
                        painter.dispose();
 
247
                }
 
248
        }
 
249
        
 
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);
 
255
                painter.closePath();
 
256
                painter.setForeground(getColor());
 
257
                
 
258
                //dibujo las cuerdas
 
259
                int x = fromX + getStringSpacing();
 
260
                int y = fromY + getFretSpacing();
 
261
                
 
262
                if(font != null){
 
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();
 
268
                }
 
269
                
 
270
                painter.initPath();
 
271
                for(int i = 0;i < getStrings().length;i++){
 
272
                        int x1 = x + (i * getStringSpacing());
 
273
                        int x2 = x + (i * getStringSpacing());
 
274
                        int y1 = y;
 
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);
 
279
                }
 
280
                painter.closePath();
 
281
                
 
282
                //dibujo las cegillas
 
283
                painter.initPath();
 
284
                for(int i = 0;i < MAX_FRETS;i++){
 
285
                        int x1 = x;
 
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);
 
292
                }
 
293
                painter.closePath();
 
294
                
 
295
                painter.setLineWidth(1);
 
296
                //dibujo las notas
 
297
                for(int i = 0;i < getStrings().length;i++){
 
298
                        int fret = getFretValue(i);
 
299
                        int noteX = x + ((getStringSpacing() * (countStrings() - 1)) - (getStringSpacing() * i));
 
300
                        if(fret < 0){
 
301
                                painter.initPath();
 
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());
 
306
                                painter.closePath();
 
307
                        }
 
308
                        else if(fret == 0){
 
309
                                painter.initPath();
 
310
                                painter.addOval(noteX - (getNoteSize() / 2),fromY,getNoteSize(),getNoteSize());
 
311
                                painter.closePath();
 
312
                        }
 
313
                        else{
 
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));
 
319
                                painter.closePath();
 
320
                        }
 
321
                }
 
322
        }
 
323
        
 
324
        public void calculateFirstFret(){
 
325
                int minimun = -1;
 
326
                int maximun = -1;
 
327
                boolean zero = false;
 
328
                for (int i = 0; i < getStrings().length; i++) {
 
329
                        int fretValue = getFretValue(i);
 
330
                        zero = (zero || fretValue == 0);
 
331
                        if(fretValue > 0){
 
332
                                minimun = (minimun < 0)?fretValue:Math.min(minimun,fretValue);
 
333
                                maximun = (Math.max(maximun,fretValue));
 
334
                        }
 
335
                }
 
336
                int firstFret = (zero && maximun < MAX_FRETS)?1:minimun;
 
337
                setFirstFret( Math.max(firstFret,1) );
 
338
        }
 
339
        
 
340
        protected int getStringValue(int number){
 
341
                TGString string = getBeat().getMeasure().getTrack().getString(number);
 
342
                return string.getValue();
 
343
        }
 
344
        
 
345
        public boolean isDisposed(){
 
346
                return (this.diagram == null || this.diagram.isDisposed());
 
347
        }
 
348
        
 
349
        public void dispose(){
 
350
                if(!isDisposed()){
 
351
                        this.diagram.dispose();
 
352
                }
 
353
        }
 
354
        
 
355
        public int getPosX() {
 
356
                return (isEditing())?this.posX:getBeatImpl().getPosX();
 
357
        }
 
358
        
 
359
        public int getPaintPosition(int index){
 
360
                return getBeatImpl().getMeasureImpl().getTs().getPosition(index);
 
361
        }
 
362
        
 
363
        public TGBeatImpl getBeatImpl(){
 
364
                return (TGBeatImpl)getBeat();
 
365
        }
 
366
}
 
 
b'\\ No newline at end of file'