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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.gui.editors.tab;
 
2
 
 
3
import java.util.ArrayList;
 
4
import java.util.Iterator;
 
5
import java.util.List;
 
6
 
 
7
import org.eclipse.swt.SWT;
 
8
import org.eclipse.swt.graphics.Color;
 
9
import org.eclipse.swt.graphics.Font;
 
10
import org.eclipse.swt.graphics.FontData;
 
11
import org.eclipse.swt.graphics.GC;
 
12
import org.eclipse.swt.graphics.Image;
 
13
import org.eclipse.swt.graphics.ImageData;
 
14
import org.eclipse.swt.graphics.RGB;
 
15
import org.eclipse.swt.graphics.Resource;
 
16
import org.herac.tuxguitar.gui.TuxGuitar;
 
17
import org.herac.tuxguitar.gui.editors.TGPainter;
 
18
import org.herac.tuxguitar.gui.editors.tab.layout.ViewLayout;
 
19
import org.herac.tuxguitar.gui.editors.tab.painters.TGNotePainter;
 
20
import org.herac.tuxguitar.gui.editors.tab.painters.TGTempoPainter;
 
21
import org.herac.tuxguitar.gui.editors.tab.painters.TGTripletFeelPainter;
 
22
import org.herac.tuxguitar.gui.system.config.TGConfigKeys;
 
23
import org.herac.tuxguitar.gui.util.ImageUtils;
 
24
import org.herac.tuxguitar.song.models.TGDuration;
 
25
 
 
26
public class TGResources {
 
27
        
 
28
        private static final int SCORE_NOTE_EMPTY_NORMAL_MODE = 0;
 
29
        private static final int SCORE_NOTE_EMPTY_PLAY_MODE = 1;
 
30
        private static final int SCORE_NOTE_FULL_NORMAL_MODE = 2;
 
31
        private static final int SCORE_NOTE_FULL_PLAY_MODE = 3;
 
32
        
 
33
        private ViewLayout layout;
 
34
        private List resources;
 
35
        private Font defaultFont;
 
36
        private Font noteFont;
 
37
        private Font timeSignatureFont;
 
38
        private Font lyricFont;
 
39
        private Font textFont;
 
40
        private Font graceFont;
 
41
        private Font chordFont;
 
42
        private Font chordFretFont;
 
43
        private Font printerDefaultFont;
 
44
        private Font printerNoteFont;
 
45
        private Font printerTimeSignatureFont;
 
46
        private Font printerLyricFont;
 
47
        private Font printerTextFont;
 
48
        private Font printerGraceFont;
 
49
        private Font printerChordFont;
 
50
        private Color backgroundColor;
 
51
        private Color lineColor;
 
52
        private Color scoreNoteColor;
 
53
        private Color tabNoteColor;
 
54
        private Color playNoteColor;
 
55
        private Color colorWhite;
 
56
        private Color colorBlack;
 
57
        private Color colorRed;
 
58
        private Image[] scoreNotes;
 
59
        private Image[] harmonicNotes;
 
60
        private Image tempoImage;
 
61
        private Image tripletFeel8;
 
62
        private Image tripletFeelNone8;
 
63
        private Image tripletFeel16;
 
64
        private Image tripletFeelNone16;
 
65
        
 
66
        private int scoreNoteWidth;
 
67
        
 
68
        public TGResources(ViewLayout layout){
 
69
                this.layout = layout;
 
70
                this.resources = new ArrayList();
 
71
        }
 
72
        
 
73
        public void load(){
 
74
                this.dispose();
 
75
                this.initFonts();
 
76
                this.initColors();
 
77
                this.initImages();
 
78
        }
 
79
        
 
80
        public ViewLayout getLayout(){
 
81
                return this.layout;
 
82
        }
 
83
        
 
84
        public Font getDefaultFont() {
 
85
                return this.defaultFont;
 
86
        }
 
87
        
 
88
        public Font getNoteFont() {
 
89
                return this.noteFont;
 
90
        }
 
91
        
 
92
        public Font getTimeSignatureFont() {
 
93
                return this.timeSignatureFont;
 
94
        }
 
95
        
 
96
        public Font getLyricFont(){
 
97
                return this.lyricFont;
 
98
        }
 
99
        
 
100
        public Font getTextFont(){
 
101
                return this.textFont;
 
102
        }
 
103
        
 
104
        public Font getChordFont(){
 
105
                return this.chordFont;
 
106
        }
 
107
        
 
108
        public Font getChordFretFont(){
 
109
                return this.chordFretFont;
 
110
        }
 
111
        
 
112
        public Font getGraceFont() {
 
113
                return this.graceFont;
 
114
        }
 
115
        
 
116
        public Font getPrinterDefaultFont() {
 
117
                return this.printerDefaultFont;
 
118
        }
 
119
        
 
120
        public Font getPrinterNoteFont() {
 
121
                return this.printerNoteFont;
 
122
        }
 
123
        
 
124
        public Font getPrinterTimeSignatureFont() {
 
125
                return this.printerTimeSignatureFont;
 
126
        }
 
127
        
 
128
        public Font getPrinterLyricFont() {
 
129
                return this.printerLyricFont;
 
130
        }
 
131
        
 
132
        public Font getPrinterTextFont() {
 
133
                return this.printerTextFont;
 
134
        }
 
135
        
 
136
        public Font getPrinterGraceFont() {
 
137
                return this.printerGraceFont;
 
138
        }
 
139
        
 
140
        public Font getPrinterChordFont() {
 
141
                return this.printerChordFont;
 
142
        }
 
143
        
 
144
        public Color getBackgroundColor() {
 
145
                return this.backgroundColor;
 
146
        }
 
147
        
 
148
        public Color getLineColor() {
 
149
                return this.lineColor;
 
150
        }
 
151
        
 
152
        public Color getPlayNoteColor() {
 
153
                return this.playNoteColor;
 
154
        }
 
155
        
 
156
        public Color getScoreNoteColor() {
 
157
                return this.scoreNoteColor;
 
158
        }
 
159
        
 
160
        public Color getTabNoteColor() {
 
161
                return this.tabNoteColor;
 
162
        }
 
163
        
 
164
        public Color getColorWhite() {
 
165
                return this.colorWhite;
 
166
        }
 
167
        
 
168
        public Color getColorBlack() {
 
169
                return this.colorBlack;
 
170
        }
 
171
        
 
172
        public Color getColorRed() {
 
173
                return this.colorRed;
 
174
        }
 
175
        
 
176
        public Image getTempoImage() {
 
177
                return this.tempoImage;
 
178
        }
 
179
        
 
180
        public Image getTripletFeel8(){
 
181
                return this.tripletFeel8;
 
182
        }
 
183
        
 
184
        public Image getTripletFeelNone8(){
 
185
                return this.tripletFeelNone8;
 
186
        }
 
187
        
 
188
        public Image getTripletFeel16(){
 
189
                return this.tripletFeel16;
 
190
        }
 
191
        
 
192
        public Image getTripletFeelNone16(){
 
193
                return this.tripletFeelNone16;
 
194
        }
 
195
        
 
196
        public Image getScoreNote(int value,boolean playing) {
 
197
                int index = 0;
 
198
                index += ((playing)?1:0);
 
199
                index += ((value >= TGDuration.QUARTER)?2:0);
 
200
                return this.scoreNotes[index];
 
201
        }
 
202
        
 
203
        public Image getHarmonicNote(int value,boolean playing) {
 
204
                int index = 0;
 
205
                index += ((playing)?1:0);
 
206
                index += ((value >= TGDuration.QUARTER)?2:0);
 
207
                return this.harmonicNotes[index];
 
208
        }
 
209
        
 
210
        public int getScoreNoteWidth(){
 
211
                return this.scoreNoteWidth;
 
212
        }
 
213
        
 
214
        private void initFonts(){
 
215
                float scale = this.layout.getFontScale() ;
 
216
                this.defaultFont = getFont(TGConfigKeys.FONT_DEFAULT, scale);
 
217
                this.noteFont = getFont(TGConfigKeys.FONT_NOTE, scale);
 
218
                this.timeSignatureFont = getFont(TGConfigKeys.FONT_TIME_SIGNATURE, scale);
 
219
                this.lyricFont = getFont(TGConfigKeys.FONT_LYRIC, scale);
 
220
                this.textFont =  getFont(TGConfigKeys.FONT_TEXT, scale);
 
221
                this.graceFont = getFont(TGConfigKeys.FONT_GRACE, scale);
 
222
                this.chordFont = getFont(TGConfigKeys.FONT_CHORD, scale);
 
223
                this.chordFretFont = getFont(TGConfigKeys.FONT_CHORD_FRET, scale);
 
224
                this.printerDefaultFont = getFont(TGConfigKeys.FONT_PRINTER_DEFAULT, scale);
 
225
                this.printerNoteFont = getFont(TGConfigKeys.FONT_PRINTER_NOTE, scale);
 
226
                this.printerTimeSignatureFont = getFont(TGConfigKeys.FONT_PRINTER_TIME_SIGNATURE, scale);
 
227
                this.printerLyricFont = getFont(TGConfigKeys.FONT_PRINTER_LYRIC, scale);
 
228
                this.printerTextFont =  getFont(TGConfigKeys.FONT_PRINTER_TEXT, scale);
 
229
                this.printerGraceFont = getFont(TGConfigKeys.FONT_PRINTER_GRACE, scale);
 
230
                this.printerChordFont = getFont(TGConfigKeys.FONT_PRINTER_CHORD, scale);
 
231
        }
 
232
        
 
233
        private void initColors(){
 
234
                this.backgroundColor = getColor(TGConfigKeys.COLOR_BACKGROUND);
 
235
                this.lineColor = getColor(TGConfigKeys.COLOR_LINE);
 
236
                this.scoreNoteColor = getColor(TGConfigKeys.COLOR_SCORE_NOTE);
 
237
                this.tabNoteColor = getColor(TGConfigKeys.COLOR_TAB_NOTE);
 
238
                this.playNoteColor = getColor(TGConfigKeys.COLOR_PLAY_NOTE);
 
239
                // Static colors
 
240
                this.colorWhite = TuxGuitar.instance().getDisplay().getSystemColor(SWT.COLOR_WHITE);
 
241
                this.colorBlack = TuxGuitar.instance().getDisplay().getSystemColor(SWT.COLOR_BLACK);
 
242
                this.colorRed = TuxGuitar.instance().getDisplay().getSystemColor(SWT.COLOR_RED);
 
243
        }
 
244
        
 
245
        private void initImages(){
 
246
                this.scoreNotes = new Image[4];
 
247
                this.scoreNotes[SCORE_NOTE_EMPTY_NORMAL_MODE] = getScoreNoteImage( getScoreNoteColor(),false);
 
248
                this.scoreNotes[SCORE_NOTE_EMPTY_PLAY_MODE] = getScoreNoteImage( getPlayNoteColor(),false);
 
249
                this.scoreNotes[SCORE_NOTE_FULL_NORMAL_MODE] = getScoreNoteImage( getScoreNoteColor(),true);
 
250
                this.scoreNotes[SCORE_NOTE_FULL_PLAY_MODE] = getScoreNoteImage( getPlayNoteColor(),true);
 
251
                
 
252
                this.harmonicNotes = new Image[4];
 
253
                this.harmonicNotes[SCORE_NOTE_EMPTY_NORMAL_MODE] = getArmonicImage( getScoreNoteColor(),false);
 
254
                this.harmonicNotes[SCORE_NOTE_EMPTY_PLAY_MODE] = getArmonicImage( getPlayNoteColor(),false);
 
255
                this.harmonicNotes[SCORE_NOTE_FULL_NORMAL_MODE] = getArmonicImage( getScoreNoteColor(),true);
 
256
                this.harmonicNotes[SCORE_NOTE_FULL_PLAY_MODE] = getArmonicImage( getPlayNoteColor(),true);
 
257
                
 
258
                this.tempoImage = getTempoImage(this.getColorBlack());
 
259
                
 
260
                this.tripletFeel8 = getTripletFeel8(this.getColorBlack());
 
261
                this.tripletFeelNone8 = getTripletFeelNone8(this.getColorBlack());
 
262
                this.tripletFeel16 = getTripletFeel16(this.getColorBlack());
 
263
                this.tripletFeelNone16 = getTripletFeelNone16(this.getColorBlack());
 
264
        }
 
265
        
 
266
        private Font getFont(String key, float scale){
 
267
                FontData data = TuxGuitar.instance().getConfig().getFontDataConfigValue(key);
 
268
                if(data == null){
 
269
                        data = new FontData();
 
270
                }
 
271
                float height = ( data.getHeight() * scale );
 
272
                
 
273
                data.setHeight( ( height > 1 ? Math.round(height) : 1 ) );
 
274
                
 
275
                Font font = new Font(TuxGuitar.instance().getDisplay(),data);
 
276
                this.resources.add( font );
 
277
                return font;
 
278
        }
 
279
        
 
280
        private Color getColor(String key){
 
281
                RGB rgb = TuxGuitar.instance().getConfig().getRGBConfigValue(key);
 
282
                if(rgb == null){
 
283
                        rgb = new RGB(0,0,0);
 
284
                }
 
285
                Color color = new Color(TuxGuitar.instance().getDisplay(),rgb);
 
286
                this.resources.add( color );
 
287
                return color;
 
288
        }
 
289
        
 
290
        private Image getScoreNoteImage(Color color,boolean full) {
 
291
                float scale = (full ? getLayout().getScoreLineSpacing() + 1 : getLayout().getScoreLineSpacing() ) - 2;
 
292
                int width = Math.round(scale * 1.33f);
 
293
                int height = Math.round(scale * 1.0f);
 
294
                
 
295
                Image image = getImage(width + 1, height + 2);
 
296
                TGPainter painter = new TGPainter(new GC(image));
 
297
                painter.setBackground(color);
 
298
                painter.setForeground(color);
 
299
                painter.initPath( (full ? TGPainter.PATH_FILL : TGPainter.PATH_DRAW) );
 
300
                TGNotePainter.paintNote(painter,0,1, scale );
 
301
                painter.closePath();
 
302
                painter.dispose();
 
303
                
 
304
                this.scoreNoteWidth = width;
 
305
                
 
306
                return getImageMask(image, getBackgroundColor().getRGB(), color.getRGB());
 
307
        }
 
308
        
 
309
        private Image getArmonicImage(Color color,boolean full) {
 
310
                int size = getLayout().getScoreLineSpacing();
 
311
                
 
312
                int x = 0;
 
313
                int y = 1;
 
314
                int width = getScoreNoteWidth() - 1;
 
315
                int height = size - 2;
 
316
                
 
317
                Image image = getImage(x + width + 2,y + height + 2);
 
318
                TGPainter painter = new TGPainter(new GC(image));
 
319
                painter.setForeground(color);
 
320
                painter.setBackground(color);
 
321
                painter.initPath( ( full ? TGPainter.PATH_DRAW | TGPainter.PATH_FILL : TGPainter.PATH_DRAW )  );
 
322
                TGNotePainter.paintHarmonic(painter, x, y, height);
 
323
                painter.closePath();
 
324
                painter.dispose();
 
325
                
 
326
                return getImageMask(image, getBackgroundColor().getRGB(), color.getRGB());
 
327
        }
 
328
        
 
329
        private Image getTempoImage(Color color) {
 
330
                float scale = 5f * getLayout().getScale();
 
331
                int width = Math.round(scale * 1.33f);
 
332
                int height = Math.round(scale * (1.0f + 2.5f));
 
333
                
 
334
                Image image = getImage(width + 1, height + 2);
 
335
                TGPainter painter = new TGPainter(new GC(image));
 
336
                painter.setBackground(color);
 
337
                painter.setForeground(color);
 
338
                TGTempoPainter.paintTempo(painter,0,0, scale);
 
339
                painter.dispose();
 
340
                
 
341
                return getImageMask(image, getBackgroundColor().getRGB(), color.getRGB());
 
342
        }
 
343
        
 
344
        private Image getTripletFeelNone8(Color color) {
 
345
                float scale = 5f * getLayout().getScale();
 
346
                
 
347
                float horizontalSpacing = (1.5f * scale);
 
348
                float verticalSpacing = (2.5f * scale);
 
349
                float ovalWidth = (1.33f * scale);
 
350
                float ovalHeight = (1.0f * scale);
 
351
                
 
352
                int width = Math.round( ovalWidth + horizontalSpacing );
 
353
                int height = Math.round( ovalHeight + verticalSpacing );
 
354
                
 
355
                Image image = getImage(width + 1, height + 2);
 
356
                TGPainter painter = new TGPainter(new GC(image));
 
357
                painter.setBackground(color);
 
358
                painter.setForeground(color);
 
359
                TGTripletFeelPainter.paintTripletFeelNone8(painter,0,0, scale);
 
360
                painter.dispose();
 
361
                
 
362
                return getImageMask(image, getBackgroundColor().getRGB(), color.getRGB());
 
363
        }
 
364
        
 
365
        private Image getTripletFeel8(Color color) {
 
366
                float scale = 5f * getLayout().getScale();
 
367
                float topSpacing = (1.0f * scale);
 
368
                float horizontalSpacing = (1.5f * scale);
 
369
                float verticalSpacing = (2.5f * scale);
 
370
                float ovalWidth = (1.33f * scale);
 
371
                float ovalHeight = (1.0f * scale);
 
372
                
 
373
                int width = Math.round( (ovalWidth * 2f) + horizontalSpacing );
 
374
                int height = Math.round((topSpacing + ovalHeight + verticalSpacing));
 
375
                
 
376
                Image image = getImage(width + 1, height + 2);
 
377
                TGPainter painter = new TGPainter(new GC(image));
 
378
                painter.setBackground(color);
 
379
                painter.setForeground(color);
 
380
                TGTripletFeelPainter.paintTripletFeel8(painter,0,0, scale);
 
381
                painter.dispose();
 
382
                
 
383
                return getImageMask(image, getBackgroundColor().getRGB(), color.getRGB());
 
384
        }       
 
385
        
 
386
        private Image getTripletFeelNone16(Color color) {
 
387
                float scale = 5f * getLayout().getScale();
 
388
                
 
389
                float horizontalSpacing = (1.5f * scale);
 
390
                float verticalSpacing = (2.5f * scale);
 
391
                float ovalWidth = (1.33f * scale);
 
392
                float ovalHeight = (1.0f * scale);
 
393
                
 
394
                int width = Math.round( ovalWidth + horizontalSpacing );
 
395
                int height = Math.round( ovalHeight + verticalSpacing );
 
396
                
 
397
                Image image = getImage(width + 1, height + 2);
 
398
                TGPainter painter = new TGPainter(new GC(image));
 
399
                painter.setBackground(color);
 
400
                painter.setForeground(color);
 
401
                TGTripletFeelPainter.paintTripletFeelNone16(painter,0,0, scale);
 
402
                painter.dispose();
 
403
                
 
404
                return getImageMask(image, getBackgroundColor().getRGB(), color.getRGB());
 
405
        }
 
406
        
 
407
        private Image getTripletFeel16(Color color) {
 
408
                float scale = 5f * getLayout().getScale();
 
409
                float topSpacing = (1.0f * scale);
 
410
                float horizontalSpacing = (1.5f * scale);
 
411
                float verticalSpacing = (2.5f * scale);
 
412
                float ovalWidth = (1.33f * scale);
 
413
                float ovalHeight = (1.0f * scale);
 
414
                
 
415
                int width = Math.round( (ovalWidth * 2f) + horizontalSpacing );
 
416
                int height = Math.round( topSpacing + ovalHeight + verticalSpacing );
 
417
                
 
418
                Image image = getImage(width + 1, height + 2);
 
419
                TGPainter painter = new TGPainter(new GC(image));
 
420
                painter.setBackground(color);
 
421
                painter.setForeground(color);
 
422
                TGTripletFeelPainter.paintTripletFeel16(painter,0,0, scale);
 
423
                painter.dispose();
 
424
                
 
425
                return getImageMask(image, getBackgroundColor().getRGB(), color.getRGB());
 
426
        }
 
427
        
 
428
        private Image getImage(int width, int height){
 
429
                Image image = new Image(getLayout().getTablature().getDisplay(),width, height);
 
430
                TGPainter painter = new TGPainter(new GC(image));
 
431
                painter.setBackground( getBackgroundColor());
 
432
                painter.initPath(TGPainter.PATH_FILL);
 
433
                painter.addRectangle(0,0,width, height);
 
434
                painter.closePath();
 
435
                painter.dispose();
 
436
                return image;
 
437
        }
 
438
        
 
439
        private Image getImageMask(Image src,RGB alpha,RGB none){
 
440
                ImageData srcData = src.getImageData();
 
441
                ImageData maskData = ImageUtils.applyMask(srcData, alpha , none);
 
442
                src.dispose();
 
443
                Image image = new Image(getLayout().getTablature().getDisplay(),srcData,maskData);
 
444
                this.resources.add(image);
 
445
                return image;
 
446
        }
 
447
        
 
448
        public void dispose(){
 
449
                Iterator it = this.resources.iterator();
 
450
                while( it.hasNext() ){
 
451
                        Resource resource = (Resource)it.next();
 
452
                        resource.dispose();
 
453
                }
 
454
                this.resources.clear();
 
455
        }
 
456
}