To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / CollidoscopeApp / src / Wave.cpp @ 0:02467299402e

History | View | Annotate | Download (5.4 KB)

1 0:02467299402e f
#include "Wave.h"
2
#include "DrawInfo.h"
3
4
5
using namespace ci;
6
7
Wave::Wave( size_t numChunks, Color selectionColor ):
8
    mNumChunks( numChunks ),
9
        mSelection( this, selectionColor ),
10
        mColor(Color(0.5f, 0.5f, 0.5f)),
11
    mFilterCoeff( 1.0f )
12
{
13
        mChunks.reserve( numChunks );
14
15
        for ( size_t i = 0; i < numChunks; i++ ){
16
                mChunks.emplace_back( i );
17
        }
18
19
    auto lambert = gl::ShaderDef().color();
20
    gl::GlslProgRef shader = gl::getStockShader( lambert );
21
    mChunkBatch = gl::Batch::create( geom::Rect( ci::Rectf( 0, 0, Chunk::kWidth, 1 ) ), shader );
22
}
23
24
void Wave::reset( bool onlyChunks )
25
{
26
        for (size_t i = 0; i < getSize(); i++){
27
                mChunks[i].reset();
28
        }
29
30
        if (onlyChunks)
31
                return;
32
33
        mSelection.setToNull();
34
}
35
36
37
void Wave::setChunk(size_t index, float bottom, float top)
38
{
39
        Chunk &c = mChunks[index];
40
        c.setTop(top);
41
        c.setBottom(bottom);
42
}
43
44
inline const Chunk & Wave::getChunk(size_t index)
45
{
46
        return mChunks[index];
47
}
48
49
void Wave::update( double secondsPerChunk, const DrawInfo& di ) {
50
    typedef std::map<int, Cursor>::iterator MapItr;
51
52
53
    // update the cursor positions
54
    double now = ci::app::getElapsedSeconds();
55
    for (MapItr itr = mCursors.begin(); itr != mCursors.end(); ++itr){
56
        if (mSelection.isNull()){
57
            itr->second.pos = Cursor::kNoPosition;
58
        }
59
60
        if ( itr->second.pos == Cursor::kNoPosition )
61
            continue;
62
63
64
        double elapsed = now - itr->second.lastUpdate;
65
66
        itr->second.pos = mSelection.getStart() + int( elapsed / secondsPerChunk );
67
68
        /* check we don't go too far off */
69
        if (itr->second.pos > mSelection.getEnd()){
70
            itr->second.pos = Cursor::kNoPosition;
71
        }
72
    }
73
74
    // update chunks for animation
75
    for ( auto &chunk : mChunks ){
76
        chunk.update( di );
77
    }
78
79
#ifdef USE_PARTICLES
80
    mParticleController.updateParticles();
81
#endif
82
83
}
84
85
void Wave::draw( const DrawInfo& di ){
86
87
88
        /* ########### draw the particles ########## */
89
#ifdef USE_PARTICLES
90
        mParticleController.draw();
91
#endif
92
93
        /* ########### draw the wave ########## */
94
        /* scale the wave to fit the window */
95
        gl::pushModelView();
96
97
98
        const float wavePixelLen =  ( mNumChunks * ( 2 + Chunk::kWidth ) );
99
        /* scale the x-axis for the wave to fit the window */
100
        gl::scale( ((float)di.getWindowWidth() ) / wavePixelLen , 1.0f);
101
        /* draw the chunks */
102
        if (mSelection.isNull()){
103
                /* no selection: all chunks the same color */
104
                gl::color(mColor);
105
                for (size_t i = 0; i < getSize(); i++){
106
                        mChunks[i].draw( di, mChunkBatch );
107
                }
108
        }
109
    else{
110
        // Selection not null
111
                gl::color(this->mColor);
112
113
        // update the array with cursor positions
114
        mCursorsPos.clear();
115
        for ( auto cursor : mCursors ){
116
            mCursorsPos.push_back( cursor.second.pos );
117
        }
118
119
                gl::enableAlphaBlending();
120
121
                const float selectionAlpha = 0.5f + mFilterCoeff * 0.5f;
122
123
124
                for (size_t i = 0; i < getSize(); i++){
125
                        /* when in selection use selection color */
126
127
                        if (i == mSelection.getStart()){
128
                                /* draw the selection bar with a transparent selection color */
129
                                gl::color(mSelection.getColor().r, mSelection.getColor().g, mSelection.getColor().b, 0.5f);
130
                mChunks[i].drawBar( di, mChunkBatch );
131
132
                                /* set the color to the selection */
133
                                gl::color(mSelection.getColor().r, mSelection.getColor().g, mSelection.getColor().b, selectionAlpha);
134
                        }
135
136
            // check if one of the cursors is positioned in this chunk
137
                        if (std::find(mCursorsPos.begin(), mCursorsPos.end(),i) != mCursorsPos.end() ){
138
                                gl::color(CURSOR_CLR);
139
                                mChunks[i].draw( di, mChunkBatch );
140
                                gl::color(mSelection.getColor().r, mSelection.getColor().g, mSelection.getColor().b, selectionAlpha);
141
                        }
142
                        else{
143
                                /* just draw with current color */
144
                                mChunks[i].draw( di, mChunkBatch );
145
                        }
146
147
                        /* exit selection: go back to wave color */
148
                        if (i == mSelection.getEnd()){
149
                                /* draw the selection bar with a transparent selection color */
150
                                gl::color(mSelection.getColor().r, mSelection.getColor().g, mSelection.getColor().b, 0.5f);
151
                mChunks[i].drawBar( di, mChunkBatch );
152
                                /* set the colo to the wave */
153
                                gl::color(this->mColor);
154
                        }
155
                }
156
                gl::disableAlphaBlending();
157
        }
158
159
160
        gl::popModelView();
161
162
}
163
164
165
166
//**************** Selection ***************//
167
168
Wave::Selection::Selection(Wave * w, Color color) :
169
    mWave( w ),
170
    mSelectionStart( 0 ),
171
    mSelectionEnd( 0 ),
172
    mColor( color ),
173
    mParticleSpread( 1 )
174
{}
175
176
177
void Wave::Selection::setStart(size_t start)  {
178
179
        /* deselect the previous */
180
    mWave->mChunks[mSelectionStart].setAsSelectionStart( false );
181
        /* select the next */
182
    mWave->mChunks[start].setAsSelectionStart( true );
183
184
        mNull = false;
185
186
    size_t size = getSize();
187
188
        mSelectionStart = start;
189
    mSelectionEnd = start + size - 1;
190
    if ( mSelectionEnd > mWave->getSize() - 1 )
191
        mSelectionEnd = mWave->getSize() - 1;
192
193
}
194
195
void Wave::Selection::setSize(size_t size)  {
196
197
    if ( size <= 0 ){
198
        mNull = true;
199
        return;
200
    }
201
202
    size -= 1;
203
204
    // check boundaries: size cannot bring the selection end beyond the end of the wave
205
    if ( mSelectionStart+size >= mWave->mNumChunks ){
206
        size = mWave->mNumChunks - mSelectionStart - 1;
207
    }
208
209
        /* deselect the previous */
210
    mWave->mChunks[mSelectionEnd].setAsSelectionEnd( false );
211
212
    mSelectionEnd = mSelectionStart + size;
213
        /* select the next */
214
    mWave->mChunks[mSelectionEnd].setAsSelectionEnd( true );
215
216
        mNull = false;
217
}
218
219
220
const cinder::Color Wave::CURSOR_CLR = Color(1.f, 1.f, 1.f);
221