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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/editors/EditorCache.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;
2
 
 
3
 
import java.util.ArrayList;
4
 
import java.util.Iterator;
5
 
import java.util.List;
6
 
 
7
 
import org.herac.tuxguitar.gui.TuxGuitar;
8
 
import org.herac.tuxguitar.gui.editors.tab.Caret;
9
 
import org.herac.tuxguitar.gui.editors.tab.MeasureComponent;
10
 
import org.herac.tuxguitar.gui.editors.tab.MeasureCoords;
11
 
import org.herac.tuxguitar.song.managers.SongManager;
12
 
import org.herac.tuxguitar.song.models.SongTrack;
13
 
 
14
 
public class EditorCache {
15
 
 
16
 
        private List playingComponents;
17
 
        private List caretComponents;
18
 
        private boolean playingUpdate;  
19
 
        private boolean caretUpdate;
20
 
        
21
 
        //Variables para el cache de reproduccion
22
 
        private MeasureCoords playingMeasure;
23
 
        private int lastTrack;
24
 
        private long lastTick;
25
 
        private long playBeatEnd;
26
 
        private boolean playingChanges;
27
 
        
28
 
        
29
 
        public EditorCache(){
30
 
                this.reset();
31
 
        }
32
 
                
33
 
        public void reset(){
34
 
                this.playingComponents = new ArrayList();
35
 
                this.caretComponents = new ArrayList();
36
 
                this.playingUpdate = false;
37
 
                this.caretUpdate = false;               
38
 
                
39
 
                this.playingMeasure = null;
40
 
                this.lastTrack =  0;
41
 
                this.lastTick = 0;
42
 
                this.playBeatEnd = 0;
43
 
                this.playingChanges = false;
44
 
        }
45
 
        
46
 
        public void updateCaretMode(){
47
 
                this.caretUpdate = true;        
48
 
        }
49
 
 
50
 
        public void updatePlayingMode(){
51
 
                this.playingUpdate = true;
52
 
                this.getPlayingComponents();
53
 
        }
54
 
        
55
 
        public List getCaretComponents() {              
56
 
                if(this.caretUpdate){
57
 
                        this.caretComponents.clear();           
58
 
                        Caret caret = TuxGuitar.instance().getTablatureEditor().getTablature().getCaret();
59
 
                        MeasureCoords measure = caret.getMeasureCoords();
60
 
                        if (measure != null) {
61
 
                                this.caretComponents = measure.getComponents(caret.getPosition());
62
 
                        }
63
 
                        this.caretUpdate = false;
64
 
                }
65
 
                return this.caretComponents;
66
 
        }
67
 
 
68
 
        public List getPlayingComponents(){             
69
 
                if(this.playingUpdate){
70
 
                        playingChanges = false;
71
 
                        
72
 
                        SongManager manager = TuxGuitar.instance().getSongManager();
73
 
                        if(TuxGuitar.instance().getPlayer().isRunning()){                                                       
74
 
                                long tick = TuxGuitar.instance().getPlayer().getTickPosition();
75
 
                                Caret caret = TuxGuitar.instance().getTablatureEditor().getTablature().getCaret();
76
 
                                SongTrack track = caret.getSongTrackCoords().getTrack();
77
 
                                                                                
78
 
                                if(playingMeasure == null || playBeatEnd == 0 || tick > playBeatEnd || tick < lastTick || track.getNumber() != lastTrack){
79
 
                                        this.playingComponents.clear();
80
 
                                        this.playBeatEnd = 0;
81
 
                                        this.playingChanges = true;                                     
82
 
 
83
 
                                        if(playingMeasure == null || !playingMeasure.hasTrack((int)track.getNumber())  || !playingMeasure.isPlaying()){
84
 
                                                playingMeasure = caret.getSongTrackCoords().getMeasure(manager.getTrackManager().getMeasureAt(track,tick));
85
 
                                        }
86
 
                                        if (playingMeasure != null) {                                   
87
 
                                                this.playingComponents = playingMeasure.getComponentsBetween(tick);                                     
88
 
                                                
89
 
                                                Iterator it = this.playingComponents.iterator();
90
 
                                                while(it.hasNext()){
91
 
                                                        MeasureComponent component = (MeasureComponent)it.next();
92
 
                                                        long componentEnd = (component.getStart() + component.getDuration().getTime());
93
 
                                                        if(playBeatEnd == 0 || componentEnd < playBeatEnd){
94
 
                                                                playBeatEnd = componentEnd;
95
 
                                                        }
96
 
                                                }
97
 
                                        }                                       
98
 
                                }
99
 
                                this.lastTrack = track.getNumber();
100
 
                                this.lastTick = tick;
101
 
                        }
102
 
                                
103
 
                        this.playingUpdate = false;
104
 
                }
105
 
                
106
 
                return this.playingComponents;
107
 
        }
108
 
 
109
 
        public MeasureCoords getPlayingMeasure(){
110
 
                return this.playingMeasure;
111
 
        }
112
 
        
113
 
        
114
 
        public boolean shouldRedraw(){
115
 
                return this.playingChanges;
116
 
        }
117
 
}