2
* Created on 30-nov-2005
4
* TODO To change the template for this generated file go to
5
* Window - Preferences - Java - Code Style - Code Templates
7
package org.herac.tuxguitar.gui.editors.tab;
9
import java.util.Iterator;
10
import java.util.List;
12
import org.eclipse.swt.graphics.GC;
13
import org.herac.tuxguitar.gui.TuxGuitar;
14
import org.herac.tuxguitar.gui.editors.tab.layout.TrackSpacing;
15
import org.herac.tuxguitar.gui.editors.tab.layout.ViewLayout;
16
import org.herac.tuxguitar.song.managers.SongManager;
17
import org.herac.tuxguitar.song.models.Duration;
18
import org.herac.tuxguitar.song.models.InstrumentString;
19
import org.herac.tuxguitar.song.models.Note;
20
import org.herac.tuxguitar.song.models.VelocityValues;
25
* TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
28
private Tablature tablature;
29
private SongManager songManager;
30
private SongCoords songCoords;
31
private SongTrackCoords selectedTrack;
32
private MeasureCoords selectedMeasure;
33
private MeasureComponent selectedComponent;
34
private Duration selectedDuration;
35
private long position;
37
private boolean changes;
40
private Note selectedNote;
42
public Caret(Tablature tablature,SongManager songManager, SongCoords songCoords) {
43
this.tablature = tablature;
44
this.songManager = songManager;
45
this.songCoords = songCoords;
46
this.selectedDuration = new Duration(Duration.QUARTER);
48
this.velocity = VelocityValues.DEFAULT;
52
public synchronized void update(){
53
int trackNumber = (selectedTrack != null)?selectedTrack.getTrack().getNumber():1;
54
update(trackNumber,position,string);
57
public synchronized void update(int trackNumber){
58
update(trackNumber,position,string);
61
public synchronized void update(int trackNumber,long position,int string){
62
update(trackNumber, position, string,getVelocity());
65
public synchronized void update(int trackNumber,long position,int string,int velocity) {
66
position = ((TuxGuitar.instance().getPlayer().isRunning())?TuxGuitar.instance().getPlayer().getTickPosition():position);
67
SongTrackCoords track = findTrack(trackNumber);
68
MeasureCoords measure = findMeasure(position,track);
69
MeasureComponent component = findComponent(position,string,measure);
70
if(track != null && measure != null && component != null){
71
moveTo(track, measure, component,string);
73
setVelocity(velocity);
76
public void moveTo(SongTrackCoords selectedTrack, MeasureCoords selectedMeasure, MeasureComponent selectedComponent,int string) {
77
this.selectedTrack = selectedTrack;
78
this.selectedMeasure = selectedMeasure;
79
this.selectedComponent = selectedComponent;
81
this.updatePosition();
82
this.updateDuration();
85
this.checkTransport();
86
this.setChanges(true);
89
private SongTrackCoords findTrack(int trackNumber){
90
SongTrackCoords trackCoords = songCoords.getTrack(trackNumber);
91
if(trackCoords == null){
92
trackCoords = songCoords.getFirstTrack();
97
private MeasureCoords findMeasure(long position,SongTrackCoords trackCoords){
98
MeasureCoords measureCoords = null;
99
if(trackCoords != null){
100
measureCoords = trackCoords.getMeasureCoords(position);
101
if(measureCoords == null){
102
measureCoords = trackCoords.getFirstMeasure();
105
return measureCoords;
108
private MeasureComponent findComponent(long position,int string,MeasureCoords measure){
109
MeasureComponent component = null;
111
component = measure.getComponentOrSilence(position,string);
112
if (component == null) {
113
component = measure.getFirstComponent();
119
public synchronized void goToTickPosition(){
120
if(!songManager.isAtPosition(selectedMeasure.getMeasure().getHeader(),TuxGuitar.instance().getPlayer().getTickPosition())){
121
this.update(selectedTrack.getTrack().getNumber(),TuxGuitar.instance().getPlayer().getTickPosition(),string);
122
this.setChanges(true);
126
public void paintCaret(ViewLayout layout,GC gc) {
127
if (selectedMeasure != null && selectedComponent != null) {
128
long start = this.selectedComponent.getStart();
129
int startPosition = TablatureUtil.getStartPosition(selectedMeasure.getMeasure(), start, selectedMeasure.getQuarterSpan()) + this.selectedComponent.getSpan();
130
int xSpan = MeasureHeaderGui.DEFAULT_LEFT_SPAN + selectedMeasure.getFirstNoteSpan();
132
if(layout.isTablatureEnabled()){
133
int stringSpan = this.tablature.getViewLayout().getStringSpan();
134
int x = selectedMeasure.getPosX() + xSpan + startPosition + 6;
135
int y = selectedMeasure.getPosY() + selectedMeasure.getTs().getPosition(TrackSpacing.POSITION_TABLATURE) + ((string * stringSpan) - stringSpan) - 7;
138
gc.drawRectangle(x, y, width, height);
140
if(layout.isScoreEnabled()){
141
//TODO: dibujar caret para partitura
148
public boolean moveRight() {
149
if (getSelectedComponent() != null) {
150
MeasureCoords nextMeasure = getMeasureCoords();
151
MeasureComponent nextComponent = getMeasureCoords().getNextComponent(getSelectedComponent());
152
if (nextComponent == null){
153
//si no habia mas componentes. busco el siguiente compas
154
nextMeasure = getSongTrackCoords().getNextMeasure(getMeasureCoords());
155
if (nextMeasure == null) {
158
nextComponent = nextMeasure.getFirstComponent();
160
moveTo(getSongTrackCoords(), nextMeasure, nextComponent, getStringNumber());
165
public void moveLeft() {
166
if (getSelectedComponent() != null) {
167
MeasureCoords prevMeasure = getMeasureCoords();
168
MeasureComponent prevComponent = getMeasureCoords().getPreviousComponent(getSelectedComponent());
169
if (prevComponent == null) {
170
//si no habia mas componentes. busco el compas anterior
171
prevMeasure = getSongTrackCoords().getPrevMeasure(getMeasureCoords());
172
if (prevMeasure == null) {
175
prevComponent = prevMeasure.getLastComponent();
177
moveTo(getSongTrackCoords(), prevMeasure, prevComponent, getStringNumber());
182
* Luego de mover el Caret. cambia la duracion seleccionada por la del componente. solo si lo que resta del compas no esta vacio
184
private void updateDuration() {
185
if (this.selectedComponent != null) {
186
boolean hasNotes = false;
187
Iterator it = getMeasureCoords().getComponentsBeforeEnd(getSelectedComponent().getStart()).iterator();
188
while (it.hasNext()) {
189
MeasureComponent component = (MeasureComponent) it.next();
190
if (component instanceof NoteCoords) {
196
if(this.selectedComponent instanceof SilenceCoords){
197
long length = this.selectedComponent.getDuration().getTime();
199
MeasureComponent nextComponent = getMeasureCoords().getNextComponent(this.selectedComponent);
200
while(nextComponent != null && nextComponent instanceof SilenceCoords){
201
length += nextComponent.getDuration().getTime();
202
nextComponent = getMeasureCoords().getNextComponent(nextComponent);
205
if(this.selectedDuration.getTime() > length){
206
this.selectedDuration = (Duration) this.selectedComponent.getDuration().clone();
209
this.selectedDuration = (Duration) this.selectedComponent.getDuration().clone();
215
public void moveUp() {
216
int stringCount = this.selectedTrack.getTrack().stringCount() ;
217
int nextString = (( (this.string - 2 + stringCount) % stringCount) + 1);
218
setStringNumber(nextString);
221
public void moveDown() {
222
int stringCount = this.selectedTrack.getTrack().stringCount() ;
223
int nextString = ( (this.string % stringCount) + 1);
224
setStringNumber(nextString);
227
public void setStringNumber(int number){
228
this.string = number;
232
public int getStringNumber(){
236
public long getPosition() {
237
return this.position;
240
public SongCoords getSongCoords() {
241
return this.songCoords;
244
public MeasureCoords getMeasureCoords() {
245
return this.selectedMeasure;
248
public SongTrackCoords getSongTrackCoords() {
249
return this.selectedTrack;
252
public MeasureComponent getSelectedComponent() {
253
return this.selectedComponent;
256
public Duration getDuration() {
257
return this.selectedDuration;
260
public void setSelectedDuration(Duration selectedDuration) {
261
this.selectedDuration = selectedDuration;
264
public InstrumentString getSelectedString() {
265
List strings = this.selectedTrack.getTrack().getStrings();
266
Iterator it = strings.iterator();
267
while (it.hasNext()) {
268
InstrumentString instrumentString = (InstrumentString) it.next();
269
if (instrumentString.getNumber() == this.string) {
270
return instrumentString;
276
public void changeDuration(Duration duration){
277
getSongCoords().getSongManager().getMeasureManager().changeDuration(getMeasureCoords().getMeasure(),getSelectedComponent().getComponent(),duration);
281
private void updatePosition(){
282
this.position = getSelectedComponent().getStart();
285
private void checkString(){
286
int stringCount = getSongTrackCoords().getTrack().getStrings().size();
287
if(this.string > stringCount){
288
this.string = stringCount;
292
private void checkTransport(){
293
TuxGuitar.instance().getTransport().gotoMeasure(getMeasureCoords().getMeasure().getHeader());
296
public boolean hasChanges() {
299
public void setChanges(boolean changes) {
300
this.changes = changes;
305
public int getVelocity() {
309
public void setVelocity(int velocity) {
310
this.velocity = velocity;
313
private void updateNote(){
314
this.selectedNote = this.songManager.getMeasureManager().getNote(getMeasureCoords().getMeasure(),getPosition(),getSelectedString().getNumber());
317
public Note getSelectedNote(){
318
return this.selectedNote;
b'\\ No newline at end of file'