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

« back to all changes in this revision

Viewing changes to TuxGuitar/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 org.herac.tuxguitar.gui.TuxGuitar;
 
4
import org.herac.tuxguitar.gui.editors.tab.Caret;
 
5
import org.herac.tuxguitar.gui.editors.tab.TGBeatImpl;
 
6
import org.herac.tuxguitar.gui.editors.tab.TGMeasureImpl;
 
7
import org.herac.tuxguitar.gui.util.MidiTickUtil;
 
8
import org.herac.tuxguitar.song.managers.TGSongManager;
 
9
import org.herac.tuxguitar.song.models.TGBeat;
 
10
import org.herac.tuxguitar.song.models.TGMeasure;
 
11
import org.herac.tuxguitar.song.models.TGTrack;
 
12
 
 
13
public class EditorCache {
 
14
        
 
15
        //Modo edition
 
16
        private boolean editUpdate;
 
17
        private TGBeatImpl editBeat;
 
18
        
 
19
        //Modo reproduccion
 
20
        private int playTrack;
 
21
        private long playTick;
 
22
        private long playStart;
 
23
        private long playBeatEnd;
 
24
        private boolean playChanges;
 
25
        private boolean playUpdate;
 
26
        private TGBeatImpl playBeat;
 
27
        private TGMeasureImpl playMeasure;
 
28
        
 
29
        public EditorCache(){
 
30
                this.reset();
 
31
        }
 
32
        
 
33
        public void reset(){
 
34
                this.resetEditMode();
 
35
                this.resetPlayMode();
 
36
        }
 
37
        
 
38
        private void resetEditMode(){
 
39
                this.editBeat = null;
 
40
                this.editUpdate = false;
 
41
        }
 
42
        
 
43
        private void resetPlayMode(){
 
44
                this.playBeat = null;
 
45
                this.playMeasure = null;
 
46
                this.playUpdate = false;
 
47
                this.playChanges = false;
 
48
                this.playTrack =  0;
 
49
                this.playTick = 0;
 
50
                this.playStart = 0;
 
51
                this.playBeatEnd = 0;
 
52
        }
 
53
        
 
54
        public void updateEditMode(){
 
55
                this.editUpdate = true;
 
56
                this.resetPlayMode();
 
57
                this.getEditBeat();
 
58
        }
 
59
        
 
60
        public void updatePlayMode(){
 
61
                this.playUpdate = true;
 
62
                this.resetEditMode();
 
63
                this.getPlayBeat();
 
64
        }
 
65
        
 
66
        public TGBeatImpl getEditBeat() {
 
67
                if(this.editUpdate){
 
68
                        this.editBeat =  TuxGuitar.instance().getTablatureEditor().getTablature().getCaret().getSelectedBeat();
 
69
                        this.editUpdate = false;
 
70
                }
 
71
                return this.editBeat;
 
72
        }
 
73
        
 
74
        public TGBeatImpl getPlayBeat(){
 
75
                if(this.playUpdate){
 
76
                        this.playChanges = false;
 
77
                        
 
78
                        TGSongManager manager = TuxGuitar.instance().getSongManager();
 
79
                        if(TuxGuitar.instance().getPlayer().isRunning()){
 
80
                                Caret caret = TuxGuitar.instance().getTablatureEditor().getTablature().getCaret();
 
81
                                TGTrack track = caret.getTrack();
 
82
                                
 
83
                                long tick = TuxGuitar.instance().getPlayer().getTickPosition();
 
84
                                long start = this.playStart + (tick - this.playTick);
 
85
                                if(this.playMeasure == null || start < this.playMeasure.getStart() || start > (this.playMeasure.getStart() + this.playMeasure.getLength())){
 
86
                                        this.playMeasure = null;
 
87
                                        start = MidiTickUtil.getStart(tick);
 
88
                                }
 
89
                                
 
90
                                if(this.playMeasure == null || this.playBeatEnd == 0 || start > this.playBeatEnd || start < this.playStart || track.getNumber() != this.playTrack){
 
91
                                        this.playBeat = null;
 
92
                                        this.playBeatEnd = 0;
 
93
                                        this.playChanges = true;
 
94
                                        
 
95
                                        if(this.playMeasure == null || !this.playMeasure.hasTrack(track.getNumber())  || !isPlaying(this.playMeasure)){
 
96
                                                this.playMeasure = (TGMeasureImpl)manager.getTrackManager().getMeasureAt(track,start);
 
97
                                        }
 
98
                                        if (this.playMeasure != null) {
 
99
                                                this.playBeat = (TGBeatImpl)manager.getMeasureManager().getBeatIn(this.playMeasure, start);
 
100
                                                if(this.playBeat != null){
 
101
                                                        this.playBeatEnd = (this.playBeat.getStart() + this.playBeat.getDuration().getTime());
 
102
                                                }
 
103
                                        }
 
104
                                }
 
105
                                this.playTrack = track.getNumber();
 
106
                                this.playTick = tick;
 
107
                                this.playStart = start;
 
108
                        }
 
109
                        this.playUpdate = false;
 
110
                }
 
111
                return this.playBeat;
 
112
        }
 
113
        
 
114
        public long getPlayTick(){
 
115
                return this.playTick;
 
116
        }
 
117
        
 
118
        public long getPlayStart(){
 
119
                return this.playStart;
 
120
        }
 
121
        
 
122
        public TGMeasureImpl getPlayMeasure(){
 
123
                return this.playMeasure;
 
124
        }
 
125
        
 
126
        public boolean shouldRedraw(){
 
127
                return this.playChanges;
 
128
        }
 
129
        
 
130
        public boolean isPlaying(TGMeasure measure){
 
131
                return (TuxGuitar.instance().getPlayer().isRunning() && this.playMeasure != null && measure.equals(this.playMeasure));
 
132
        }
 
133
        
 
134
        public boolean isPlaying(TGMeasure measure,TGBeat b){
 
135
                return (isPlaying(measure) && this.playStart >= b.getStart() && this.playStart < (b.getStart() + b.getDuration().getTime()));
 
136
        }
 
137
}