comparison CollidoscopeApp/src/Wave.cpp @ 5:75b744078d66

added license
author Fiore Martin <f.martin@qmul.ac.uk>
date Wed, 13 Jul 2016 16:06:46 +0200
parents ab6db404403a
children 4dad0b810f18
comparison
equal deleted inserted replaced
4:ab6db404403a 5:75b744078d66
1 /*
2
3 Copyright (C) 2015 Fiore Martin
4 Copyright (C) 2016 Queen Mary University of London
5 Author: Fiore Martin
6
7 This file is part of Collidoscope.
8
9 Collidoscope is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22 */
23
1 #include "Wave.h" 24 #include "Wave.h"
2 #include "DrawInfo.h" 25 #include "DrawInfo.h"
3 26
4 27
5 using namespace ci; 28 using namespace ci;
6 29
7 Wave::Wave( size_t numChunks, Color selectionColor ): 30 Wave::Wave( size_t numChunks, Color selectionColor ):
8 mNumChunks( numChunks ), 31 mNumChunks( numChunks ),
9 mSelection( this, selectionColor ), 32 mSelection( this, selectionColor ),
10 mColor(Color(0.5f, 0.5f, 0.5f)), 33 mColor(Color(0.5f, 0.5f, 0.5f)),
11 mFilterCoeff( 1.0f ) 34 mFilterCoeff( 1.0f )
12 { 35 {
13 mChunks.reserve( numChunks ); 36 mChunks.reserve( numChunks );
14 37
15 for ( size_t i = 0; i < numChunks; i++ ){ 38 for ( size_t i = 0; i < numChunks; i++ ){
16 mChunks.emplace_back( i ); 39 mChunks.emplace_back( i );
17 } 40 }
18 41
19 // init cinder batch drawing 42 // init cinder batch drawing
20 auto lambert = gl::ShaderDef().color(); 43 auto lambert = gl::ShaderDef().color();
21 gl::GlslProgRef shader = gl::getStockShader( lambert ); 44 gl::GlslProgRef shader = gl::getStockShader( lambert );
22 mChunkBatch = gl::Batch::create( geom::Rect( ci::Rectf( 0, 0, Chunk::kWidth, 1 ) ), shader ); 45 mChunkBatch = gl::Batch::create( geom::Rect( ci::Rectf( 0, 0, Chunk::kWidth, 1 ) ), shader );
23 } 46 }
24 47
25 void Wave::reset( bool onlyChunks ) 48 void Wave::reset( bool onlyChunks )
26 { 49 {
27 for (size_t i = 0; i < getSize(); i++){ 50 for (size_t i = 0; i < getSize(); i++){
28 mChunks[i].reset(); 51 mChunks[i].reset();
29 } 52 }
30 53
31 if (onlyChunks) 54 if (onlyChunks)
32 return; 55 return;
33 56
34 mSelection.setToNull(); 57 mSelection.setToNull();
35 } 58 }
36 59
37 60
38 void Wave::setChunk(size_t index, float bottom, float top) 61 void Wave::setChunk(size_t index, float bottom, float top)
39 { 62 {
40 Chunk &c = mChunks[index]; 63 Chunk &c = mChunks[index];
41 c.setTop(top); 64 c.setTop(top);
42 c.setBottom(bottom); 65 c.setBottom(bottom);
43 } 66 }
44 67
45 inline const Chunk & Wave::getChunk(size_t index) 68 inline const Chunk & Wave::getChunk(size_t index)
46 { 69 {
47 return mChunks[index]; 70 return mChunks[index];
48 } 71 }
49 72
50 void Wave::update( double secondsPerChunk, const DrawInfo& di ) { 73 void Wave::update( double secondsPerChunk, const DrawInfo& di ) {
51 typedef std::map<int, Cursor>::iterator MapItr; 74 typedef std::map<int, Cursor>::iterator MapItr;
52 75
87 } 110 }
88 111
89 void Wave::draw( const DrawInfo& di ){ 112 void Wave::draw( const DrawInfo& di ){
90 113
91 114
92 /* ########### draw the particles ########## */ 115 /* ########### draw the particles ########## */
93 #ifdef USE_PARTICLES 116 #ifdef USE_PARTICLES
94 mParticleController.draw(); 117 mParticleController.draw();
95 #endif 118 #endif
96 119
97 /* ########### draw the wave ########## */ 120 /* ########### draw the wave ########## */
98 /* scale the wave to fit the window */ 121 /* scale the wave to fit the window */
99 gl::pushModelView(); 122 gl::pushModelView();
100 123
101 124
102 const float wavePixelLen = ( mNumChunks * ( 2 + Chunk::kWidth ) ); 125 const float wavePixelLen = ( mNumChunks * ( 2 + Chunk::kWidth ) );
103 /* scale the x-axis for the wave to fit the window precisely */ 126 /* scale the x-axis for the wave to fit the window precisely */
104 gl::scale( ((float)di.getWindowWidth() ) / wavePixelLen , 1.0f); 127 gl::scale( ((float)di.getWindowWidth() ) / wavePixelLen , 1.0f);
105 /* draw the chunks */ 128 /* draw the chunks */
106 if (mSelection.isNull()){ 129 if (mSelection.isNull()){
107 /* no selection: all chunks the same color */ 130 /* no selection: all chunks the same color */
108 gl::color(mColor); 131 gl::color(mColor);
109 for (size_t i = 0; i < getSize(); i++){ 132 for (size_t i = 0; i < getSize(); i++){
110 mChunks[i].draw( di, mChunkBatch ); 133 mChunks[i].draw( di, mChunkBatch );
111 } 134 }
112 } 135 }
113 else{ 136 else{
114 // Selection not null 137 // Selection not null
115 gl::color(this->mColor); 138 gl::color(this->mColor);
116 139
117 // update the array with cursor positions 140 // update the array with cursor positions
118 mCursorsPos.clear(); 141 mCursorsPos.clear();
119 for ( auto cursor : mCursors ){ 142 for ( auto cursor : mCursors ){
120 mCursorsPos.push_back( cursor.second.pos ); 143 mCursorsPos.push_back( cursor.second.pos );
121 } 144 }
122 145
123 gl::enableAlphaBlending(); 146 gl::enableAlphaBlending();
124 147
125 const float selectionAlpha = 0.5f + mFilterCoeff * 0.5f; 148 const float selectionAlpha = 0.5f + mFilterCoeff * 0.5f;
126 149
127 150
128 for (size_t i = 0; i < getSize(); i++){ 151 for (size_t i = 0; i < getSize(); i++){
129 /* when in selection use selection color */ 152 /* when in selection use selection color */
130 153
131 if (i == mSelection.getStart()){ 154 if (i == mSelection.getStart()){
132 /* draw the selection bar with a transparent selection color */ 155 /* draw the selection bar with a transparent selection color */
133 gl::color(mSelection.getColor().r, mSelection.getColor().g, mSelection.getColor().b, 0.5f); 156 gl::color(mSelection.getColor().r, mSelection.getColor().g, mSelection.getColor().b, 0.5f);
134 mChunks[i].drawBar( di, mChunkBatch ); 157 mChunks[i].drawBar( di, mChunkBatch );
135 158
136 /* set the color to the selection */ 159 /* set the color to the selection */
137 gl::color(mSelection.getColor().r, mSelection.getColor().g, mSelection.getColor().b, selectionAlpha); 160 gl::color(mSelection.getColor().r, mSelection.getColor().g, mSelection.getColor().b, selectionAlpha);
138 } 161 }
139 162
140 // check if one of the cursors is positioned in this chunk 163 // check if one of the cursors is positioned in this chunk
141 if (std::find(mCursorsPos.begin(), mCursorsPos.end(),i) != mCursorsPos.end() ){ 164 if (std::find(mCursorsPos.begin(), mCursorsPos.end(),i) != mCursorsPos.end() ){
142 gl::color(CURSOR_CLR); 165 gl::color(CURSOR_CLR);
143 mChunks[i].draw( di, mChunkBatch ); 166 mChunks[i].draw( di, mChunkBatch );
144 gl::color(mSelection.getColor().r, mSelection.getColor().g, mSelection.getColor().b, selectionAlpha); 167 gl::color(mSelection.getColor().r, mSelection.getColor().g, mSelection.getColor().b, selectionAlpha);
145 } 168 }
146 else{ 169 else{
147 /* just draw with current color */ 170 /* just draw with current color */
148 mChunks[i].draw( di, mChunkBatch ); 171 mChunks[i].draw( di, mChunkBatch );
149 } 172 }
150 173
151 /* exit selection: go back to wave color */ 174 /* exit selection: go back to wave color */
152 if (i == mSelection.getEnd()){ 175 if (i == mSelection.getEnd()){
153 /* draw the selection bar with a transparent selection color */ 176 /* draw the selection bar with a transparent selection color */
154 gl::color(mSelection.getColor().r, mSelection.getColor().g, mSelection.getColor().b, 0.5f); 177 gl::color(mSelection.getColor().r, mSelection.getColor().g, mSelection.getColor().b, 0.5f);
155 mChunks[i].drawBar( di, mChunkBatch ); 178 mChunks[i].drawBar( di, mChunkBatch );
156 /* set the colo to the wave */ 179 /* set the colo to the wave */
157 gl::color(this->mColor); 180 gl::color(this->mColor);
158 } 181 }
159 } 182 }
160 gl::disableAlphaBlending(); 183 gl::disableAlphaBlending();
161 } 184 }
162 185
163 186
164 gl::popModelView(); 187 gl::popModelView();
165 188
166 } 189 }
167 190
168 191
169 192
178 {} 201 {}
179 202
180 203
181 void Wave::Selection::setStart(size_t start) { 204 void Wave::Selection::setStart(size_t start) {
182 205
183 /* deselect the previous */ 206 /* deselect the previous */
184 mWave->mChunks[mSelectionStart].setAsSelectionStart( false ); 207 mWave->mChunks[mSelectionStart].setAsSelectionStart( false );
185 /* select the next */ 208 /* select the next */
186 mWave->mChunks[start].setAsSelectionStart( true ); 209 mWave->mChunks[start].setAsSelectionStart( true );
187 210
188 mNull = false; 211 mNull = false;
189 212
190 size_t size = getSize(); 213 size_t size = getSize();
191 214
192 mSelectionStart = start; 215 mSelectionStart = start;
193 mSelectionEnd = start + size - 1; 216 mSelectionEnd = start + size - 1;
194 if ( mSelectionEnd > mWave->getSize() - 1 ) 217 if ( mSelectionEnd > mWave->getSize() - 1 )
195 mSelectionEnd = mWave->getSize() - 1; 218 mSelectionEnd = mWave->getSize() - 1;
196 219
197 } 220 }
208 // check boundaries: size cannot bring the selection end beyond the end of the wave 231 // check boundaries: size cannot bring the selection end beyond the end of the wave
209 if ( mSelectionStart+size >= mWave->mNumChunks ){ 232 if ( mSelectionStart+size >= mWave->mNumChunks ){
210 size = mWave->mNumChunks - mSelectionStart - 1; 233 size = mWave->mNumChunks - mSelectionStart - 1;
211 } 234 }
212 235
213 /* deselect the previous */ 236 /* deselect the previous */
214 mWave->mChunks[mSelectionEnd].setAsSelectionEnd( false ); 237 mWave->mChunks[mSelectionEnd].setAsSelectionEnd( false );
215 238
216 mSelectionEnd = mSelectionStart + size; 239 mSelectionEnd = mSelectionStart + size;
217 /* select the next */ 240 /* select the next */
218 mWave->mChunks[mSelectionEnd].setAsSelectionEnd( true ); 241 mWave->mChunks[mSelectionEnd].setAsSelectionEnd( true );
219 242
220 mNull = false; 243 mNull = false;
221 } 244 }
222 245
223 246
224 const cinder::Color Wave::CURSOR_CLR = Color(1.f, 1.f, 1.f); 247 const cinder::Color Wave::CURSOR_CLR = Color(1.f, 1.f, 1.f);
225 248