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.herac.tuxguitar.gui.TuxGuitar;
13
import org.herac.tuxguitar.gui.editors.TGPainter;
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.gui.util.MidiTickUtil;
17
import org.herac.tuxguitar.song.managers.TGMeasureManager;
18
import org.herac.tuxguitar.song.managers.TGSongManager;
19
import org.herac.tuxguitar.song.models.TGBeat;
20
import org.herac.tuxguitar.song.models.TGDuration;
21
import org.herac.tuxguitar.song.models.TGNote;
22
import org.herac.tuxguitar.song.models.TGString;
23
import org.herac.tuxguitar.song.models.TGVelocities;
28
* TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
31
private Tablature tablature;
32
private TGTrackImpl selectedTrack;
33
private TGMeasureImpl selectedMeasure;
34
//private TGDurationable selectedComponent;
35
private TGDuration selectedDuration;
36
private long position;
38
private boolean changes;
41
private TGNote selectedNote;
43
private TGBeat selectedBeat;
45
public Caret(Tablature tablature) {
46
this.tablature = tablature;
47
this.selectedDuration = getSongManager().getFactory().newDuration();
49
this.velocity = TGVelocities.DEFAULT;
53
public synchronized void update(){
54
int trackNumber = (this.selectedTrack != null)?this.selectedTrack.getNumber():1;
55
update(trackNumber,this.position,this.string);
58
public synchronized void update(int trackNumber){
59
update(trackNumber,this.position,this.string);
62
public synchronized void update(int trackNumber,long position,int string){
63
update(trackNumber, position, string,getVelocity());
66
public synchronized void update(int trackNumber,long position,int string,int velocity) {
67
long realPosition = ((TuxGuitar.instance().getPlayer().isRunning())?MidiTickUtil.getStart(TuxGuitar.instance().getPlayer().getTickPosition()):position);
68
TGTrackImpl track = findTrack(trackNumber);
69
TGMeasureImpl measure = findMeasure(realPosition,track);
70
TGBeat beat = findBeat(realPosition,measure);
71
if(track != null && measure != null && beat != null){
72
moveTo(track, measure, beat,string);
74
setVelocity(velocity);
77
public void moveTo(TGTrackImpl selectedTrack, TGMeasureImpl selectedMeasure, TGBeat selectedBeat,int string) {
78
this.selectedTrack = selectedTrack;
79
this.selectedMeasure = selectedMeasure;
80
this.selectedBeat = selectedBeat;
82
this.updatePosition();
83
this.updateDuration();
87
this.checkTransport();
88
this.setChanges(true);
91
private TGTrackImpl findTrack(int number){
92
TGTrackImpl track = (TGTrackImpl)getSongManager().getTrack(number);
94
track = (TGTrackImpl)getSongManager().getFirstTrack();
99
private TGMeasureImpl findMeasure(long position,TGTrackImpl track){
100
TGMeasureImpl measure = null;
102
measure = (TGMeasureImpl)getSongManager().getTrackManager().getMeasureAt(track,position);
104
measure = (TGMeasureImpl)getSongManager().getTrackManager().getFirstMeasure(track);
110
private TGBeat findBeat(long position,TGMeasureImpl measure){
113
TGMeasureManager manager = getSongManager().getMeasureManager();
114
beat = manager.getBeatIn(measure, position);
116
beat = manager.getFirstBeat(measure.getBeats());
122
public synchronized void goToTickPosition(){
123
long start = MidiTickUtil.getStart(TuxGuitar.instance().getPlayer().getTickPosition());
124
this.update(this.selectedTrack.getNumber(),start,this.string);
125
this.setChanges(true);
128
public void paintCaret(ViewLayout layout,TGPainter painter) {
129
if(!TuxGuitar.instance().getPlayer().isRunning()){
130
if (this.selectedMeasure != null && this.selectedBeat instanceof TGBeatImpl) {
132
TGBeatImpl beat = (TGBeatImpl)this.selectedBeat;
133
if( (layout.getStyle() & ViewLayout.DISPLAY_TABLATURE) != 0){
134
int stringSpacing = this.tablature.getViewLayout().getStringSpacing();
135
int leftSpacing = beat.getMeasureImpl().getHeaderImpl().getLeftSpacing(layout);
136
int x = this.selectedMeasure.getPosX() + beat.getPosX() + beat.getSpacing() + leftSpacing - 5;
137
int y = this.selectedMeasure.getPosY() + this.selectedMeasure.getTs().getPosition(TrackSpacing.POSITION_TABLATURE) + ((this.string * stringSpacing) - stringSpacing) - 7;
140
layout.setCaretStyle(painter);
142
painter.addRectangle(x, y, width, height);
145
else if( (layout.getStyle() & ViewLayout.DISPLAY_SCORE) != 0){
146
int line = this.tablature.getViewLayout().getScoreLineSpacing();
147
int leftSpacing = beat.getMeasureImpl().getHeaderImpl().getLeftSpacing(layout);
148
float xMargin = (2.0f * layout.getScale());
149
float x1 = this.selectedMeasure.getPosX() + beat.getPosX() + beat.getSpacing() + leftSpacing - xMargin;
150
float x2 = (x1 + layout.getResources().getScoreNoteWidth() + xMargin);
151
float y1 = this.selectedMeasure.getPosY() + this.selectedMeasure.getTs().getPosition(TrackSpacing.POSITION_TOP) - line;
152
float y2 = this.selectedMeasure.getPosY() + this.selectedMeasure.getTs().getPosition(TrackSpacing.POSITION_BOTTOM);
153
layout.setCaretStyle(painter);
155
painter.moveTo(x1, y1);
156
painter.lineTo(x1 + ((x2 - x1) / 2f), y1 + (line / 2f));
157
painter.lineTo(x2, y1);
158
painter.moveTo(x1, y2 + line);
159
painter.lineTo(x1 + ((x2 - x1) / 2f), y2 + (line / 2f));
160
painter.lineTo(x2, y2 + line);
167
public boolean moveRight() {
168
if (getSelectedBeat() != null) {
169
TGMeasureImpl measure = getMeasure();
170
TGBeat beat = getSongManager().getMeasureManager().getNextBeat(measure.getBeats(),getSelectedBeat());
172
//si no habia mas componentes. busco el siguiente compas
173
measure = (TGMeasureImpl)getSongManager().getTrackManager().getNextMeasure(getMeasure());
174
if (measure == null) {
177
beat = getSongManager().getMeasureManager().getFirstBeat(measure.getBeats());
179
moveTo(getTrack(), measure, beat, getStringNumber());
184
public void moveLeft() {
185
if (getSelectedBeat() != null) {
186
TGMeasureImpl measure = getMeasure();
187
TGBeat beat = getSongManager().getMeasureManager().getPreviousBeat(measure.getBeats(),getSelectedBeat());
189
//si no habia mas componentes. busco el compas anterior
190
measure = (TGMeasureImpl)getSongManager().getTrackManager().getPrevMeasure(getMeasure());
191
if (measure == null) {
194
beat = getSongManager().getMeasureManager().getLastBeat(measure.getBeats());
196
moveTo(getTrack(), measure, beat, getStringNumber());
201
* Luego de mover el Caret. cambia la duracion seleccionada por la del componente. solo si lo que resta del compas no esta vacio
203
private void updateDuration() {
204
if (this.selectedBeat != null && !this.selectedBeat.isRestBeat()) {
205
this.selectedBeat.getDuration().copy(this.selectedDuration);
207
boolean hasNotes = false;
208
Iterator it = getMeasure().getComponentsBeforeEnd(getSelectedComponent().getStart()).iterator();
209
while (it.hasNext()) {
210
TGDurationable component = (TGDurationable) it.next();
211
if (component instanceof TGNoteImpl) {
217
if(this.selectedComponent instanceof TGSilenceImpl){
218
long length = this.selectedComponent.getDuration().getTime();
220
List components = getMeasure().getComponents(TGMeasure.C_LIST_NOTATION);
221
TGDurationable nextComponent = getSongManager().getMeasureManager().getNextComponent(components,getSelectedComponent());
223
while(nextComponent != null && nextComponent instanceof TGSilenceImpl){
224
length += nextComponent.getDuration().getTime();
225
nextComponent = getSongManager().getMeasureManager().getNextComponent(components,nextComponent);
228
if(this.selectedDuration.getTime() > length){
229
this.selectedComponent.getDuration().copy(this.selectedDuration);
232
this.selectedComponent.getDuration().copy(this.selectedDuration);
239
public void moveUp() {
240
int stringCount = this.selectedTrack.stringCount() ;
241
int nextString = (( (this.string - 2 + stringCount) % stringCount) + 1);
242
setStringNumber(nextString);
245
public void moveDown() {
246
int stringCount = this.selectedTrack.stringCount() ;
247
int nextString = ( (this.string % stringCount) + 1);
248
setStringNumber(nextString);
251
public void setStringNumber(int number){
252
this.string = number;
256
public int getStringNumber(){
260
public long getPosition() {
261
return this.position;
264
public TGMeasureImpl getMeasure() {
265
return this.selectedMeasure;
268
public TGTrackImpl getTrack() {
269
return this.selectedTrack;
272
public TGDuration getDuration() {
273
return this.selectedDuration;
276
public void setSelectedDuration(TGDuration selectedDuration) {
277
this.selectedDuration = selectedDuration;
280
public TGString getSelectedString() {
281
List strings = this.selectedTrack.getStrings();
282
Iterator it = strings.iterator();
283
while (it.hasNext()) {
284
TGString instrumentString = (TGString) it.next();
285
if (instrumentString.getNumber() == this.string) {
286
return instrumentString;
292
public void changeDuration(TGDuration duration){
293
getSongManager().getMeasureManager().changeDuration(getMeasure(),getSelectedBeat(),duration, true);
297
private void updatePosition(){
298
this.position = getSelectedBeat().getStart();
301
private void checkString(){
302
int stringCount = getTrack().getStrings().size();
303
if(this.string > stringCount){
304
this.string = stringCount;
308
private void checkTransport(){
309
TuxGuitar.instance().getTransport().gotoMeasure(getMeasure().getHeader());
312
public boolean hasChanges() {
315
public void setChanges(boolean changes) {
316
this.changes = changes;
319
public int getVelocity() {
320
return this.velocity;
323
public void setVelocity(int velocity) {
324
this.velocity = velocity;
327
private void updateNote(){
328
this.selectedNote = getSongManager().getMeasureManager().getNote(getMeasure(),getPosition(),getSelectedString().getNumber());
331
public TGNote getSelectedNote(){
332
return this.selectedNote;
335
private void updateBeat(){
336
this.selectedBeat = getSongManager().getMeasureManager().getBeat(getMeasure(),getPosition());
339
public TGBeatImpl getSelectedBeat(){
340
return (TGBeatImpl)this.selectedBeat;
343
public TGSongManager getSongManager(){
344
return this.tablature.getSongManager();
b'\\ No newline at end of file'