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