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

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2007-02-04 01:41:23 UTC
  • Revision ID: james.westby@ubuntu.com-20070204014123-9pv7okph0iaiqkvw
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

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