summaryrefslogtreecommitdiff
path: root/futuregael/src/show.h
blob: e3aeebeeab26f9f00fdbd30fa863b1efc28c8047 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "ofMain.h"
#include "ofxCsv.h"
#include "vectortext.h"

class ScriptLine{
public:
	ScriptLine(SVGFont &font,string audiofile,string cols,string message){

		vector<string> colours=ofSplitString(cols,",");
		for (auto c: colours){
			palette.push_back(ofColor::fromHex(ofHexToInt(c)));
		}

		clear();
		vector<string> m=ofSplitString(message," ");
		for (auto& word: m){
			glyphWord w;
			int pos=0;
			for (auto& c: word){
				string uniglyph = ofUTF8Substring(word, pos, 1);
				if (c<0){
					//ofLog()<<"got unicode glyph, "<<uniglyph;
				}
				w.glyphs.push_back(font.getGlyph(uniglyph,
					palette.size()?
					palette[ofRandom(palette.size())]:
					//112->231 hue sat 0->255 brightness 255
					ofColor::fromHsb(ofRandom(119)+112,ofRandom(255),255)
				));
				pos++;
			}
			words.push_back(w);
		}

		if (audio.load(audiofile)){
			//requires https://github.com/arturoc/openFrameworks/tree/feature-soundPlayerDuration
        	duration = ((float)audio.getDurationMS())/1000.0f;
        	
        	avgWordDuration = duration / words.size();

        	ofLog()<<"Line "<<audiofile<<", duration "<<duration<<"s, "<<words.size()<<" words avg "<<avgWordDuration<<"s";
		}
		else duration=0.0f;

		enspace=font.enspace;

	}
	void clear(){
		words.clear();
	}
	vector<colourPolyline>& getOutlines(float time){
		float phase=time/avgWordDuration;
		float phaseseg=fmod(phase,(int)phase);

		//ofLog()<<"phase "<<phase<<" phaseseg "<<phaseseg;

		/*
		int word1=(int)(phase+0.5f); 
			//segment 1 plays word 0 out for the first half 
			//then word 1 solid
		int word2=(int)(phase+1.5f); 
			//segment 2 plays word 1 solid for the first half
			//then word 2 in

			//deal with the ends?
			//extra padding words, 2 per end

		float word1amt=0.5-phaseseg;

		if (word1amt<0.0f) word1amt=1.0f;

		float word2mat=phaseseg-0.5f;

		if (word2amt<0.0f) word2amt=1.0f;
		*/

		//segment 1 plays word 0 solid for the first half
		//then word 0 out
		//at the very beginning, there's a buffer

		int word1=(int)(phase)-1; 

		//segment 2 plays word 1 in for the first half 
		//then word 1 solid

		int word2=word1+1;

	    float word1amt=2.0-(phaseseg*2.0);
	    if (word1amt>1.0f) word1amt=1.0f;

	    float word2amt=phaseseg*2.0;
	    if (word2amt>1.0f) word2amt=1.0f;

	    outlines.clear();
		float p=-ofGetWidth()/2;

		float s=0.1f;

		if (word1>-1&&word1<words.size()){
			for (auto& g:words[word1].glyphs){
				for (auto& o:g.outline){
					auto q=o;
					q.scale(s,-s);
					q.translate(glm::vec3(p*s,-ofGetHeight()/3,0));
					outlines.push_back(colourPolyline(q,g.colour*word1amt));
				}
				p+=g.width;
			}
			p+=enspace;
		}

		if (word2<words.size()){
			for (auto& g:words[word2].glyphs){
				for (auto& o:g.outline){
					auto q=o;
					q.scale(s,-s);
					q.translate(glm::vec3(p*s,-ofGetHeight()/3,0));
					outlines.push_back(colourPolyline(q,g.colour*word2amt));
				}
				p+=g.width;
			}
		}
	
		return outlines;

	}

	vector<ofColor> palette;
	vector<glyphWord> words;
	ofSoundPlayer audio;
	float duration;
	float avgWordDuration;
	vector<colourPolyline> outlines;
	float enspace;
	
};


class Show{
public:
	bool loadFont(filesystem::path fontpath){
		return font.load(fontpath);
	}
	Show(){
		bisPlaying=false;
	}
	Show(filesystem::path fontpath){
		font.load(fontpath);
		Show();
	}
	bool load(string file){

		if (!font.isLoaded()){
			ofLogError()<<"cannot load show without a font";
			return false;
		}

		// Load a CSV File.
		if(csv.load(file,"|")) {
			
			for (auto row:csv){
				if (row.size()<2){
					ofLog()<<"Error, found row with "<<row.size()<<" elements";
				}
				else script.push_back(ScriptLine(font,row[0],row[1],row[2]));
			}

			ofLog()<<"Loaded show, "<<csv.getNumRows()<<" lines";

			playline=script.begin();

			return true;
		}

		playline=script.end();

		return false;
	}

	void play(){
		if (playline==script.end()) return;

		if (playline->audio.isLoaded()){
			playline->audio.play();
			startTime=ofGetElapsedTimef();
			bisPlaying=true;
			ofLog()<<"Line playing "<<playline->duration;
		}
	}

	void stop(){
		if (isPlaying()){
			playline->audio.stop();
			bisPlaying=false;
			ofLog()<<"Line stopped";
		}
	}

	void update(){
		if (isPlaying()){
			if (ofGetElapsedTimef()-startTime>
					(playline->avgWordDuration*
						playline->words.size()+2)){
				playline->audio.stop();
				bisPlaying=false;
				ofLog()<<"Line ended, "<<playline->duration;;
				playline++;
				if (playline==script.end()){
					ofLog()<<"Show finished!";
				}
			}
		}
	}

	vector<colourPolyline>& getOutlines(){

		return playline->getOutlines(ofGetElapsedTimef()-startTime);
	}

	ofxCsv csv;

	vector<ScriptLine> script;

	vector<ScriptLine>::iterator playline;

	float startTime;
	bool bisPlaying;
	bool isPlaying() {
		return bisPlaying;
	}

	SVGFont font;
};