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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/editors/tab/Caret.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
/*
 
2
 * Created on 30-nov-2005
 
3
 *
 
4
 * TODO To change the template for this generated file go to
 
5
 * Window - Preferences - Java - Code Style - Code Templates
 
6
 */
 
7
package org.herac.tuxguitar.gui.editors.tab;
 
8
 
 
9
import java.util.Iterator;
 
10
import java.util.List;
 
11
 
 
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;
 
24
 
 
25
/**
 
26
 * @author julian
 
27
 * 
 
28
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
 
29
 */
 
30
public class Caret {
 
31
        private Tablature tablature;
 
32
        private TGTrackImpl selectedTrack;
 
33
        private TGMeasureImpl selectedMeasure;
 
34
        //private TGDurationable selectedComponent;
 
35
        private TGDuration selectedDuration;
 
36
        private long position;
 
37
        private int string;
 
38
        private boolean changes;
 
39
        private int velocity;
 
40
        
 
41
        private TGNote selectedNote;
 
42
        
 
43
        private TGBeat selectedBeat;
 
44
        
 
45
        public Caret(Tablature tablature) {
 
46
                this.tablature = tablature;
 
47
                this.selectedDuration = getSongManager().getFactory().newDuration();
 
48
                this.string = 1;
 
49
                this.velocity = TGVelocities.DEFAULT;
 
50
                this.changes = false;
 
51
        }
 
52
        
 
53
        public synchronized void update(){
 
54
                int trackNumber = (this.selectedTrack != null)?this.selectedTrack.getNumber():1;
 
55
                update(trackNumber,this.position,this.string);
 
56
        }
 
57
        
 
58
        public synchronized void update(int trackNumber){
 
59
                update(trackNumber,this.position,this.string);
 
60
        }
 
61
        
 
62
        public synchronized void update(int trackNumber,long position,int string){
 
63
                update(trackNumber, position, string,getVelocity());
 
64
        }
 
65
        
 
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);
 
73
                }
 
74
                setVelocity(velocity);
 
75
        }
 
76
        
 
77
        public void moveTo(TGTrackImpl selectedTrack, TGMeasureImpl selectedMeasure, TGBeat selectedBeat,int string) {
 
78
                this.selectedTrack = selectedTrack;
 
79
                this.selectedMeasure = selectedMeasure;
 
80
                this.selectedBeat = selectedBeat;
 
81
                this.string = string;
 
82
                this.updatePosition();
 
83
                this.updateDuration();
 
84
                this.checkString();
 
85
                this.updateNote();
 
86
                this.updateBeat();
 
87
                this.checkTransport();
 
88
                this.setChanges(true);
 
89
        }
 
90
        
 
91
        private TGTrackImpl findTrack(int number){
 
92
                TGTrackImpl track = (TGTrackImpl)getSongManager().getTrack(number);
 
93
                if(track == null){
 
94
                        track = (TGTrackImpl)getSongManager().getFirstTrack();
 
95
                }
 
96
                return track;
 
97
        }
 
98
        
 
99
        private TGMeasureImpl findMeasure(long position,TGTrackImpl track){
 
100
                TGMeasureImpl measure = null;
 
101
                if(track != null){
 
102
                        measure = (TGMeasureImpl)getSongManager().getTrackManager().getMeasureAt(track,position);
 
103
                        if(measure == null){
 
104
                                measure = (TGMeasureImpl)getSongManager().getTrackManager().getFirstMeasure(track);
 
105
                        }
 
106
                }
 
107
                return measure;
 
108
        }
 
109
        
 
110
        private TGBeat findBeat(long position,TGMeasureImpl measure){
 
111
                TGBeat beat = null;
 
112
                if(measure != null){
 
113
                        TGMeasureManager manager = getSongManager().getMeasureManager();
 
114
                        beat = manager.getBeatIn(measure, position);
 
115
                        if (beat == null) {
 
116
                                beat = manager.getFirstBeat(measure.getBeats());
 
117
                        }
 
118
                }
 
119
                return beat;
 
120
        }
 
121
        
 
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);
 
126
        }
 
127
        
 
128
        public void paintCaret(ViewLayout layout,TGPainter painter) {
 
129
                if(!TuxGuitar.instance().getPlayer().isRunning()){
 
130
                        if (this.selectedMeasure != null && this.selectedBeat instanceof TGBeatImpl) {
 
131
                                
 
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;
 
138
                                        int width = 14;
 
139
                                        int height = 14;
 
140
                                        layout.setCaretStyle(painter);
 
141
                                        painter.initPath();
 
142
                                        painter.addRectangle(x, y, width, height);
 
143
                                        painter.closePath();
 
144
                                }
 
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);
 
154
                                        painter.initPath();
 
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);
 
161
                                        painter.closePath();
 
162
                                }
 
163
                        }
 
164
                }
 
165
        }
 
166
        
 
167
        public boolean moveRight() {
 
168
                if (getSelectedBeat() != null) {
 
169
                        TGMeasureImpl measure = getMeasure();
 
170
                        TGBeat beat = getSongManager().getMeasureManager().getNextBeat(measure.getBeats(),getSelectedBeat());
 
171
                        if (beat == null){
 
172
                                //si no habia mas componentes. busco el siguiente compas
 
173
                                measure = (TGMeasureImpl)getSongManager().getTrackManager().getNextMeasure(getMeasure());
 
174
                                if (measure == null) {
 
175
                                        return false;
 
176
                                }
 
177
                                beat = getSongManager().getMeasureManager().getFirstBeat(measure.getBeats());
 
178
                        }
 
179
                        moveTo(getTrack(), measure, beat, getStringNumber());
 
180
                }
 
181
                return true;
 
182
        }
 
183
        
 
184
        public void moveLeft() {
 
185
                if (getSelectedBeat() != null) {
 
186
                        TGMeasureImpl measure = getMeasure();
 
187
                        TGBeat beat = getSongManager().getMeasureManager().getPreviousBeat(measure.getBeats(),getSelectedBeat());
 
188
                        if (beat == null) {
 
189
                                //si no habia mas componentes. busco el compas anterior
 
190
                                measure = (TGMeasureImpl)getSongManager().getTrackManager().getPrevMeasure(getMeasure());
 
191
                                if (measure == null) {
 
192
                                        return;
 
193
                                }
 
194
                                beat = getSongManager().getMeasureManager().getLastBeat(measure.getBeats());
 
195
                        }
 
196
                        moveTo(getTrack(), measure, beat, getStringNumber());
 
197
                }
 
198
        }
 
199
        
 
200
        /**
 
201
         * Luego de mover el Caret. cambia la duracion seleccionada por la del componente. solo si lo que resta del compas no esta vacio
 
202
         */
 
203
        private void updateDuration() {
 
204
                if (this.selectedBeat != null && !this.selectedBeat.isRestBeat()) {
 
205
                        this.selectedBeat.getDuration().copy(this.selectedDuration);
 
206
                        /*
 
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) {
 
212
                                        hasNotes = true;
 
213
                                        break;
 
214
                                }
 
215
                        }
 
216
                        if (hasNotes) {
 
217
                                if(this.selectedComponent instanceof TGSilenceImpl){
 
218
                                        long length = this.selectedComponent.getDuration().getTime();
 
219
                                        
 
220
                                        List components = getMeasure().getComponents(TGMeasure.C_LIST_NOTATION);
 
221
                                        TGDurationable nextComponent =  getSongManager().getMeasureManager().getNextComponent(components,getSelectedComponent());
 
222
                                        
 
223
                                        while(nextComponent != null && nextComponent instanceof TGSilenceImpl){
 
224
                                                length += nextComponent.getDuration().getTime();
 
225
                                                nextComponent =  getSongManager().getMeasureManager().getNextComponent(components,nextComponent);
 
226
                                        }
 
227
                                        
 
228
                                        if(this.selectedDuration.getTime() > length){
 
229
                                                this.selectedComponent.getDuration().copy(this.selectedDuration);
 
230
                                        }
 
231
                                }else{
 
232
                                        this.selectedComponent.getDuration().copy(this.selectedDuration);
 
233
                                }
 
234
                        }
 
235
                        */
 
236
                }
 
237
        }
 
238
        
 
239
        public void moveUp() {
 
240
                int stringCount = this.selectedTrack.stringCount() ;
 
241
                int nextString = (( (this.string - 2 + stringCount) % stringCount) + 1);
 
242
                setStringNumber(nextString);
 
243
        }
 
244
        
 
245
        public void moveDown() {
 
246
                int stringCount = this.selectedTrack.stringCount() ;
 
247
                int nextString = ( (this.string  % stringCount) + 1);
 
248
                setStringNumber(nextString);
 
249
        }
 
250
        
 
251
        public void setStringNumber(int number){
 
252
                this.string = number;
 
253
                this.updateNote();
 
254
        }
 
255
        
 
256
        public int getStringNumber(){
 
257
                return this.string;
 
258
        }
 
259
        
 
260
        public long getPosition() {
 
261
                return this.position;
 
262
        }
 
263
        
 
264
        public TGMeasureImpl getMeasure() {
 
265
                return this.selectedMeasure;
 
266
        }
 
267
        
 
268
        public TGTrackImpl getTrack() {
 
269
                return this.selectedTrack;
 
270
        }
 
271
        
 
272
        public TGDuration getDuration() {
 
273
                return this.selectedDuration;
 
274
        }
 
275
        
 
276
        public void setSelectedDuration(TGDuration selectedDuration) {
 
277
                this.selectedDuration = selectedDuration;
 
278
        }
 
279
        
 
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;
 
287
                        }
 
288
                }
 
289
                return null;
 
290
        }
 
291
        
 
292
        public void changeDuration(TGDuration duration){
 
293
                getSongManager().getMeasureManager().changeDuration(getMeasure(),getSelectedBeat(),duration, true);
 
294
                setChanges(true);
 
295
        }
 
296
        
 
297
        private void updatePosition(){
 
298
                this.position = getSelectedBeat().getStart();
 
299
        }
 
300
        
 
301
        private void checkString(){
 
302
                int stringCount = getTrack().getStrings().size();
 
303
                if(this.string > stringCount){
 
304
                        this.string = stringCount;
 
305
                }
 
306
        }
 
307
        
 
308
        private void checkTransport(){
 
309
                TuxGuitar.instance().getTransport().gotoMeasure(getMeasure().getHeader());
 
310
        }
 
311
        
 
312
        public boolean hasChanges() {
 
313
                return this.changes;
 
314
        }
 
315
        public void setChanges(boolean changes) {
 
316
                this.changes = changes;
 
317
        }
 
318
        
 
319
        public int getVelocity() {
 
320
                return this.velocity;
 
321
        }
 
322
        
 
323
        public void setVelocity(int velocity) {
 
324
                this.velocity = velocity;
 
325
        }
 
326
        
 
327
        private void updateNote(){
 
328
                this.selectedNote = getSongManager().getMeasureManager().getNote(getMeasure(),getPosition(),getSelectedString().getNumber());
 
329
        }
 
330
        
 
331
        public TGNote getSelectedNote(){
 
332
                return this.selectedNote;
 
333
        }
 
334
        
 
335
        private void updateBeat(){
 
336
                this.selectedBeat = getSongManager().getMeasureManager().getBeat(getMeasure(),getPosition());
 
337
        }
 
338
        
 
339
        public TGBeatImpl getSelectedBeat(){
 
340
                return (TGBeatImpl)this.selectedBeat;
 
341
        }
 
342
        
 
343
        public TGSongManager getSongManager(){
 
344
                return this.tablature.getSongManager();
 
345
        }
 
346
}
 
 
b'\\ No newline at end of file'