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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/editors/tab/TGBeatGroup.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 org.herac.tuxguitar.gui.editors.tab.layout.ViewLayout;
 
4
 
 
5
public class TGBeatGroup {
 
6
        private static final int SCORE_MIDDLE_KEYS[] = new int[]{55,40,40,50};
 
7
        private static final int SCORE_SHARP_POSITIONS[] = new int[]{7,7,6,6,5,4,4,3,3,2,2,1};
 
8
        private static final int SCORE_FLAT_POSITIONS[] = new int[]{7,6,6,5,5,4,3,3,2,2,1,1};
 
9
        
 
10
        public static final int DIRECTION_UP = 1;
 
11
        public static final int DIRECTION_DOWN = 2;
 
12
        
 
13
        private static final int UP_OFFSET = 28;
 
14
        private static final int DOWN_OFFSET = 35;
 
15
        
 
16
        private int direction;
 
17
        private TGNoteImpl firstMinNote;
 
18
        private TGNoteImpl firstMaxNote;
 
19
        private TGNoteImpl lastMinNote;
 
20
        private TGNoteImpl lastMaxNote;
 
21
        private TGNoteImpl maxNote;
 
22
        private TGNoteImpl minNote;
 
23
        
 
24
        public TGBeatGroup(){
 
25
                this.direction = DIRECTION_UP;
 
26
                this.firstMinNote = null;
 
27
                this.firstMaxNote = null;
 
28
                this.lastMinNote = null;
 
29
                this.lastMaxNote = null;
 
30
                this.maxNote = null;
 
31
                this.minNote = null;
 
32
        }
 
33
        
 
34
        public void check(TGNoteImpl note,int clef){
 
35
                int value = note.getRealValue();
 
36
                
 
37
                //FIRST MIN NOTE
 
38
                if(this.firstMinNote == null || note.getBeat().getStart() < this.firstMinNote.getBeat().getStart()){
 
39
                        this.firstMinNote = note;
 
40
                }else if(note.getBeat().getStart() == this.firstMinNote.getBeat().getStart()){
 
41
                        if(note.getRealValue() < this.firstMinNote.getRealValue()){
 
42
                                this.firstMinNote = note;
 
43
                        }
 
44
                }
 
45
                //FIRST MAX NOTE
 
46
                if(this.firstMaxNote == null || note.getBeat().getStart() < this.firstMaxNote.getBeat().getStart()){
 
47
                        this.firstMaxNote = note;
 
48
                }else if(note.getBeat().getStart() == this.firstMaxNote.getBeat().getStart()){
 
49
                        if(note.getRealValue() > this.firstMaxNote.getRealValue()){
 
50
                                this.firstMaxNote = note;
 
51
                        }
 
52
                }
 
53
                
 
54
                //LAST MIN NOTE
 
55
                if(this.lastMinNote == null || note.getBeat().getStart() > this.lastMinNote.getBeat().getStart()){
 
56
                        this.lastMinNote = note;
 
57
                }else if(note.getBeat().getStart() == this.lastMinNote.getBeat().getStart()){
 
58
                        if(note.getRealValue() < this.lastMinNote.getRealValue()){
 
59
                                this.lastMinNote = note;
 
60
                        }
 
61
                }
 
62
                //LAST MIN NOTE
 
63
                if(this.lastMaxNote == null || note.getBeat().getStart() > this.lastMaxNote.getBeat().getStart()){
 
64
                        this.lastMaxNote = note;
 
65
                }else if(note.getBeat().getStart() == this.lastMaxNote.getBeat().getStart()){
 
66
                        if(note.getRealValue() > this.lastMaxNote.getRealValue()){
 
67
                                this.lastMaxNote = note;
 
68
                        }
 
69
                }
 
70
                
 
71
                if(this.maxNote == null || value > this.maxNote.getRealValue()){
 
72
                        this.maxNote = note;
 
73
                }
 
74
                if(this.minNote == null || value < this.minNote.getRealValue()){
 
75
                        this.minNote = note;
 
76
                }
 
77
                
 
78
                int max = Math.abs(this.minNote.getRealValue() - (SCORE_MIDDLE_KEYS[clef - 1] + 100));
 
79
                int min = Math.abs(this.maxNote.getRealValue() - (SCORE_MIDDLE_KEYS[clef - 1] - 100));
 
80
                if(max > min){
 
81
                        this.direction = DIRECTION_UP;
 
82
                }else{
 
83
                        this.direction = DIRECTION_DOWN;
 
84
                }
 
85
        }
 
86
        
 
87
        public int getY1(ViewLayout layout,TGNoteImpl note,int key,int clef){
 
88
                double scale = (layout.getScoreLineSpacing() / 2.00);
 
89
                int noteValue = note.getRealValue();
 
90
                
 
91
                int scoreLineY = 0;
 
92
                if(key <= 7){
 
93
                        scoreLineY = (int)((SCORE_SHARP_POSITIONS[noteValue % 12]) * scale - (7 * (noteValue / 12)) * scale);
 
94
                }else{
 
95
                        scoreLineY = (int)((SCORE_FLAT_POSITIONS[noteValue % 12]) * scale - (7 * (noteValue / 12)) * scale);
 
96
                }
 
97
                
 
98
                scoreLineY += TGMeasureImpl.SCORE_KEY_OFFSETS[clef - 1] * scale;
 
99
                
 
100
                return scoreLineY;
 
101
        }
 
102
        
 
103
        public int getY2(ViewLayout layout,int x,int key,int clef){
 
104
                int maxDistance = 10;
 
105
                float upOffset = TGBeatGroup.getUpOffset(layout);
 
106
                float downOffset = TGBeatGroup.getDownOffset(layout);
 
107
                if(this.direction == DIRECTION_DOWN){
 
108
                        if(this.minNote != this.firstMinNote && this.minNote != this.lastMinNote){
 
109
                                return (int) (getY1(layout,this.minNote,key,clef) + downOffset);
 
110
                        }
 
111
                        
 
112
                        int x1 = this.firstMinNote.getPosX() + this.firstMinNote.getBeatImpl().getSpacing();
 
113
                        int x2 = this.lastMinNote.getPosX() + this.lastMinNote.getBeatImpl().getSpacing();
 
114
                        int y1 =  (int) (getY1(layout,this.firstMinNote,key,clef) +  downOffset);
 
115
                        int y2 =  (int) (getY1(layout,this.lastMinNote,key,clef) +  downOffset);
 
116
                        
 
117
                        if(y1 > y2 && (y1 - y2) > maxDistance) y2 = (y1 - maxDistance);
 
118
                        if(y2 > y1 && (y2 - y1) > maxDistance) y1 = (y2 - maxDistance);
 
119
                        int y = (int)((((double)y1 -(double)y2) / ((double)x1 - (double)x2)) * ((double)x1 - (double)x));
 
120
                        return y1 - y;
 
121
                }else if(this.maxNote != this.firstMaxNote && this.maxNote != this.lastMaxNote){
 
122
                        return (int)(getY1(layout,this.maxNote,key,clef) - upOffset);
 
123
                }else{
 
124
                        int x1 = this.firstMaxNote.getPosX() + this.firstMaxNote.getBeatImpl().getSpacing();
 
125
                        int x2 = this.lastMaxNote.getPosX() + this.lastMaxNote.getBeatImpl().getSpacing();
 
126
                        int y1 = (int)(getY1(layout,this.firstMaxNote,key,clef) - upOffset);
 
127
                        int y2 = (int)(getY1(layout,this.lastMaxNote,key,clef) - upOffset);
 
128
                        
 
129
                        if(y1 < y2 && (y2 - y1) > maxDistance) y2 = (y1 + maxDistance);
 
130
                        if(y2 < y1 && (y1 - y2) > maxDistance) y1 = (y2 + maxDistance);
 
131
                        int y = (int)((((double)y1 -(double)y2) / ((double)x1 - (double)x2)) * ((double)x1 - (double)x));
 
132
                        return y1 - y;
 
133
                }
 
134
        }
 
135
        
 
136
        public int getDirection() {
 
137
                return this.direction;
 
138
        }
 
139
        
 
140
        public TGNoteImpl getMinNote(){
 
141
                return this.minNote;
 
142
        }
 
143
        
 
144
        public TGNoteImpl getMaxNote(){
 
145
                return this.maxNote;
 
146
        }
 
147
        
 
148
        public static float getUpOffset(ViewLayout layout){
 
149
                float scale = (layout.getScoreLineSpacing() / 8.0f);
 
150
                return (UP_OFFSET * scale);
 
151
        }
 
152
        
 
153
        public static float getDownOffset(ViewLayout layout){
 
154
                float scale = (layout.getScoreLineSpacing() / 8.0f);
 
155
                return (DOWN_OFFSET * scale);
 
156
        }
 
157
}