1
package org.herac.tuxguitar.gui.editors.tab;
3
import java.util.ArrayList;
4
import java.util.Iterator;
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;
26
public class TGResources {
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;
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;
66
private int scoreNoteWidth;
68
public TGResources(ViewLayout layout){
70
this.resources = new ArrayList();
80
public ViewLayout getLayout(){
84
public Font getDefaultFont() {
85
return this.defaultFont;
88
public Font getNoteFont() {
92
public Font getTimeSignatureFont() {
93
return this.timeSignatureFont;
96
public Font getLyricFont(){
97
return this.lyricFont;
100
public Font getTextFont(){
101
return this.textFont;
104
public Font getChordFont(){
105
return this.chordFont;
108
public Font getChordFretFont(){
109
return this.chordFretFont;
112
public Font getGraceFont() {
113
return this.graceFont;
116
public Font getPrinterDefaultFont() {
117
return this.printerDefaultFont;
120
public Font getPrinterNoteFont() {
121
return this.printerNoteFont;
124
public Font getPrinterTimeSignatureFont() {
125
return this.printerTimeSignatureFont;
128
public Font getPrinterLyricFont() {
129
return this.printerLyricFont;
132
public Font getPrinterTextFont() {
133
return this.printerTextFont;
136
public Font getPrinterGraceFont() {
137
return this.printerGraceFont;
140
public Font getPrinterChordFont() {
141
return this.printerChordFont;
144
public Color getBackgroundColor() {
145
return this.backgroundColor;
148
public Color getLineColor() {
149
return this.lineColor;
152
public Color getPlayNoteColor() {
153
return this.playNoteColor;
156
public Color getScoreNoteColor() {
157
return this.scoreNoteColor;
160
public Color getTabNoteColor() {
161
return this.tabNoteColor;
164
public Color getColorWhite() {
165
return this.colorWhite;
168
public Color getColorBlack() {
169
return this.colorBlack;
172
public Color getColorRed() {
173
return this.colorRed;
176
public Image getTempoImage() {
177
return this.tempoImage;
180
public Image getTripletFeel8(){
181
return this.tripletFeel8;
184
public Image getTripletFeelNone8(){
185
return this.tripletFeelNone8;
188
public Image getTripletFeel16(){
189
return this.tripletFeel16;
192
public Image getTripletFeelNone16(){
193
return this.tripletFeelNone16;
196
public Image getScoreNote(int value,boolean playing) {
198
index += ((playing)?1:0);
199
index += ((value >= TGDuration.QUARTER)?2:0);
200
return this.scoreNotes[index];
203
public Image getHarmonicNote(int value,boolean playing) {
205
index += ((playing)?1:0);
206
index += ((value >= TGDuration.QUARTER)?2:0);
207
return this.harmonicNotes[index];
210
public int getScoreNoteWidth(){
211
return this.scoreNoteWidth;
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);
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);
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);
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);
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);
258
this.tempoImage = getTempoImage(this.getColorBlack());
260
this.tripletFeel8 = getTripletFeel8(this.getColorBlack());
261
this.tripletFeelNone8 = getTripletFeelNone8(this.getColorBlack());
262
this.tripletFeel16 = getTripletFeel16(this.getColorBlack());
263
this.tripletFeelNone16 = getTripletFeelNone16(this.getColorBlack());
266
private Font getFont(String key, float scale){
267
FontData data = TuxGuitar.instance().getConfig().getFontDataConfigValue(key);
269
data = new FontData();
271
float height = ( data.getHeight() * scale );
273
data.setHeight( ( height > 1 ? Math.round(height) : 1 ) );
275
Font font = new Font(TuxGuitar.instance().getDisplay(),data);
276
this.resources.add( font );
280
private Color getColor(String key){
281
RGB rgb = TuxGuitar.instance().getConfig().getRGBConfigValue(key);
283
rgb = new RGB(0,0,0);
285
Color color = new Color(TuxGuitar.instance().getDisplay(),rgb);
286
this.resources.add( color );
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);
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 );
304
this.scoreNoteWidth = width;
306
return getImageMask(image, getBackgroundColor().getRGB(), color.getRGB());
309
private Image getArmonicImage(Color color,boolean full) {
310
int size = getLayout().getScoreLineSpacing();
314
int width = getScoreNoteWidth() - 1;
315
int height = size - 2;
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);
326
return getImageMask(image, getBackgroundColor().getRGB(), color.getRGB());
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));
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);
341
return getImageMask(image, getBackgroundColor().getRGB(), color.getRGB());
344
private Image getTripletFeelNone8(Color color) {
345
float scale = 5f * getLayout().getScale();
347
float horizontalSpacing = (1.5f * scale);
348
float verticalSpacing = (2.5f * scale);
349
float ovalWidth = (1.33f * scale);
350
float ovalHeight = (1.0f * scale);
352
int width = Math.round( ovalWidth + horizontalSpacing );
353
int height = Math.round( ovalHeight + verticalSpacing );
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);
362
return getImageMask(image, getBackgroundColor().getRGB(), color.getRGB());
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);
373
int width = Math.round( (ovalWidth * 2f) + horizontalSpacing );
374
int height = Math.round((topSpacing + ovalHeight + verticalSpacing));
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);
383
return getImageMask(image, getBackgroundColor().getRGB(), color.getRGB());
386
private Image getTripletFeelNone16(Color color) {
387
float scale = 5f * getLayout().getScale();
389
float horizontalSpacing = (1.5f * scale);
390
float verticalSpacing = (2.5f * scale);
391
float ovalWidth = (1.33f * scale);
392
float ovalHeight = (1.0f * scale);
394
int width = Math.round( ovalWidth + horizontalSpacing );
395
int height = Math.round( ovalHeight + verticalSpacing );
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);
404
return getImageMask(image, getBackgroundColor().getRGB(), color.getRGB());
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);
415
int width = Math.round( (ovalWidth * 2f) + horizontalSpacing );
416
int height = Math.round( topSpacing + ovalHeight + verticalSpacing );
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);
425
return getImageMask(image, getBackgroundColor().getRGB(), color.getRGB());
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);
439
private Image getImageMask(Image src,RGB alpha,RGB none){
440
ImageData srcData = src.getImageData();
441
ImageData maskData = ImageUtils.applyMask(srcData, alpha , none);
443
Image image = new Image(getLayout().getTablature().getDisplay(),srcData,maskData);
444
this.resources.add(image);
448
public void dispose(){
449
Iterator it = this.resources.iterator();
450
while( it.hasNext() ){
451
Resource resource = (Resource)it.next();
454
this.resources.clear();