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

« back to all changes in this revision

Viewing changes to 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.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;
21
 
 
22
 
/**
23
 
 * @author julian
24
 
 * 
25
 
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
26
 
 */
27
 
public class Caret {
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;
36
 
    private int string;
37
 
    private boolean changes;    
38
 
    private int velocity;
39
 
    
40
 
    private Note selectedNote;
41
 
    
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);
47
 
        this.string = 1;
48
 
        this.velocity = VelocityValues.DEFAULT;
49
 
        this.changes = false;
50
 
    }
51
 
 
52
 
    public synchronized void update(){
53
 
        int trackNumber = (selectedTrack != null)?selectedTrack.getTrack().getNumber():1;       
54
 
        update(trackNumber,position,string);
55
 
    }    
56
 
    
57
 
    public synchronized void update(int trackNumber){           
58
 
        update(trackNumber,position,string);
59
 
    }      
60
 
    
61
 
    public synchronized void update(int trackNumber,long position,int string){
62
 
        update(trackNumber, position, string,getVelocity());
63
 
    }
64
 
    
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);
72
 
        }        
73
 
        setVelocity(velocity);
74
 
    }
75
 
                
76
 
    public void moveTo(SongTrackCoords selectedTrack, MeasureCoords selectedMeasure, MeasureComponent selectedComponent,int string) {        
77
 
        this.selectedTrack = selectedTrack;
78
 
        this.selectedMeasure = selectedMeasure;
79
 
        this.selectedComponent = selectedComponent;
80
 
        this.string = string;        
81
 
        this.updatePosition();
82
 
        this.updateDuration();   
83
 
        this.checkString();
84
 
        this.updateNote();
85
 
        this.checkTransport();
86
 
        this.setChanges(true);
87
 
    }    
88
 
    
89
 
    private SongTrackCoords findTrack(int trackNumber){
90
 
        SongTrackCoords trackCoords = songCoords.getTrack(trackNumber);
91
 
        if(trackCoords == null){
92
 
                trackCoords = songCoords.getFirstTrack();
93
 
        }
94
 
        return trackCoords;
95
 
    }
96
 
    
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();
103
 
                }
104
 
        }
105
 
        return measureCoords;
106
 
    }
107
 
    
108
 
    private MeasureComponent findComponent(long position,int string,MeasureCoords measure){
109
 
        MeasureComponent component = null;
110
 
        if(measure != null){
111
 
                component = measure.getComponentOrSilence(position,string);
112
 
                if (component == null) {
113
 
                        component = measure.getFirstComponent();
114
 
                }               
115
 
        }
116
 
        return component;
117
 
    }
118
 
    
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);
123
 
        }
124
 
    }    
125
 
    
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(); 
131
 
            
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;                    
136
 
                int width = 14;
137
 
                int height = 14;
138
 
                gc.drawRectangle(x, y, width, height);     
139
 
            }
140
 
            if(layout.isScoreEnabled()){
141
 
                //TODO: dibujar caret para partitura
142
 
            }
143
 
                   
144
 
        }
145
 
        
146
 
    }
147
 
 
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) {
156
 
                    return false;
157
 
                }      
158
 
                nextComponent = nextMeasure.getFirstComponent();                
159
 
            }
160
 
            moveTo(getSongTrackCoords(), nextMeasure, nextComponent, getStringNumber());                                
161
 
        }
162
 
        return true;
163
 
    }
164
 
 
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) {
173
 
                        return;
174
 
                }                      
175
 
                prevComponent = prevMeasure.getLastComponent();                
176
 
            }      
177
 
            moveTo(getSongTrackCoords(), prevMeasure, prevComponent, getStringNumber());    
178
 
        }
179
 
    }
180
 
 
181
 
    /**
182
 
     * Luego de mover el Caret. cambia la duracion seleccionada por la del componente. solo si lo que resta del compas no esta vacio
183
 
     */
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) {
191
 
                    hasNotes = true;
192
 
                    break;
193
 
                }
194
 
            }
195
 
            if (hasNotes) {
196
 
                if(this.selectedComponent instanceof SilenceCoords){
197
 
                    long length = this.selectedComponent.getDuration().getTime();
198
 
                    
199
 
                    MeasureComponent nextComponent = getMeasureCoords().getNextComponent(this.selectedComponent);
200
 
                    while(nextComponent != null && nextComponent instanceof SilenceCoords){
201
 
                        length += nextComponent.getDuration().getTime();                        
202
 
                        nextComponent = getMeasureCoords().getNextComponent(nextComponent);
203
 
                    }
204
 
                    
205
 
                    if(this.selectedDuration.getTime() > length){
206
 
                        this.selectedDuration = (Duration) this.selectedComponent.getDuration().clone();   
207
 
                    }
208
 
                }else{
209
 
                    this.selectedDuration = (Duration) this.selectedComponent.getDuration().clone();
210
 
                }                                
211
 
            }
212
 
        }
213
 
    }
214
 
 
215
 
    public void moveUp() {
216
 
        int stringCount = this.selectedTrack.getTrack().stringCount() ;
217
 
        int nextString = (( (this.string - 2 + stringCount) % stringCount) + 1);        
218
 
        setStringNumber(nextString);
219
 
    }
220
 
 
221
 
    public void moveDown() {
222
 
        int stringCount = this.selectedTrack.getTrack().stringCount() ;
223
 
        int nextString = ( (this.string  % stringCount) + 1);           
224
 
        setStringNumber(nextString);
225
 
    }
226
 
 
227
 
    public void setStringNumber(int number){
228
 
        this.string = number;
229
 
        this.updateNote();
230
 
    }
231
 
    
232
 
    public int getStringNumber(){
233
 
        return this.string;
234
 
    }
235
 
    
236
 
    public long getPosition() {
237
 
        return this.position;
238
 
    }
239
 
 
240
 
    public SongCoords getSongCoords() {
241
 
        return this.songCoords;
242
 
    }
243
 
 
244
 
    public MeasureCoords getMeasureCoords() {
245
 
        return this.selectedMeasure;
246
 
    }
247
 
 
248
 
    public SongTrackCoords getSongTrackCoords() {
249
 
        return this.selectedTrack;
250
 
    }
251
 
 
252
 
    public MeasureComponent getSelectedComponent() {
253
 
        return this.selectedComponent;
254
 
    }
255
 
 
256
 
    public Duration getDuration() {
257
 
        return this.selectedDuration;
258
 
    }
259
 
 
260
 
    public void setSelectedDuration(Duration selectedDuration) {
261
 
        this.selectedDuration = selectedDuration;
262
 
    }
263
 
 
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;
271
 
            }
272
 
        }
273
 
        return null;
274
 
    }
275
 
    
276
 
    public void changeDuration(Duration duration){
277
 
        getSongCoords().getSongManager().getMeasureManager().changeDuration(getMeasureCoords().getMeasure(),getSelectedComponent().getComponent(),duration);
278
 
        setChanges(true);
279
 
    }
280
 
    
281
 
    private void updatePosition(){
282
 
        this.position = getSelectedComponent().getStart();
283
 
    }
284
 
    
285
 
    private void checkString(){
286
 
        int stringCount = getSongTrackCoords().getTrack().getStrings().size();
287
 
        if(this.string > stringCount){
288
 
            this.string = stringCount;
289
 
        }
290
 
    }    
291
 
    
292
 
    private void checkTransport(){
293
 
        TuxGuitar.instance().getTransport().gotoMeasure(getMeasureCoords().getMeasure().getHeader());
294
 
    }
295
 
    
296
 
    public boolean hasChanges() {
297
 
        return changes;
298
 
    }
299
 
    public void setChanges(boolean changes) {
300
 
        this.changes = changes;
301
 
    }
302
 
 
303
 
    
304
 
    
305
 
        public int getVelocity() {
306
 
                return velocity;
307
 
        }
308
 
        
309
 
        public void setVelocity(int velocity) {
310
 
                this.velocity = velocity;
311
 
        }
312
 
 
313
 
        private void updateNote(){
314
 
                this.selectedNote = this.songManager.getMeasureManager().getNote(getMeasureCoords().getMeasure(),getPosition(),getSelectedString().getNumber());
315
 
        }
316
 
        
317
 
        public Note getSelectedNote(){
318
 
                return this.selectedNote;
319
 
        }
320
 
}
 
 
b'\\ No newline at end of file'