summaryrefslogtreecommitdiff
path: root/lasertext/src/ofApp.h
blob: a099cf6232d61b22ca81e9b58d4fafbe2564a776 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#pragma once

#include "ofMain.h"
#include "ofxSvg.h"
#include "ofxGui.h"
#include "ofxXmlSettings.h"

#include "ofxHelios.h"
#include "lineTransformer.h"
#include "colourPolyline.h"

class scannableColourPolyline: public colourPolyline{
public:
	bool operator < (const scannableColourPolyline& line) const {
		return (operator[](0).x < line[0].x);
	}
};

class star{
public:
	star(ofVec2f p,ofVec2f v,float l){
		pos=p;
		vel=v;
		birthday=ofGetElapsedTimef();
		lifespan=l;
	}
	void update(vector<star>& stars){
		pos+=vel;
	}
	ofVec2f pos;
	ofVec2f vel;
	float birthday;
	float lifespan;
};

class starSystem{
public:
	vector<star> stars;
	ofVec2f centre;
	float radius;
	float rate;
	float speed;
	float lifespan;
	float agevar;	
	float last;
	starSystem(ofVec2f c=ofVec2f(0,0), float _rad=100,float r=0.5,float s=5.0,float l=5.0,float v=1.0){
		init(c,_rad,r,s,l,v);
	}
	void init(ofVec2f c, float _rad,float r,float s,float l,float v){
		centre=c;
		radius=_rad;
		rate=r;
		speed=s;
		lifespan=l;
		agevar=v;
	}
	void update(){
		float now=ofGetElapsedTimef();
		float segment=now-last;
		last=now;
		for(auto it = stars.begin(); it != stars.end();)
		{
		    if(now-it->birthday>it->lifespan){
		    	//ofLog()<<"erase star";
		        it = stars.erase(it);
		    }
		    else {
		    	//ofLog()<<"update star";
		    	it->update(stars);
		        ++it;
		    }
		}
		if (ofRandom(rate)<segment){
				float a=ofRandom(3.57f);
				float r=ofRandom(radius);
				ofVec2f offset=ofVec2f(cos(a)*r,sin(a)*r);

				//avoid having stars that don't move, for safety
				a=ofRandom(3.57f);
				r=(ofRandom(0.8)+0.2)*speed;
				ofVec2f speed=ofVec2f(cos(a)*r,sin(a)*r);
		
				stars.push_back(star(
					offset,
					speed,
					lifespan+ofRandom(agevar)-(agevar/2)
					));
				/*
				ofLog()<<"create star: "<<
				ofVec2f(ofRandom(ofGetWidth()),ofRandom(ofGetHeight()))<<", "<<
				ofVec2f(ofRandom(speed)-(speed/2),ofRandom(speed)-(speed/2))<<" lifespan: "<<
				lifespan+ofRandom(agevar)-(agevar/2);
				*/
					

		}
	}
	vector<scannableColourPolyline> getPoints(){
		float now=ofGetElapsedTimef();
		vector<scannableColourPolyline> o;
		for(auto& s:stars){
			scannableColourPolyline l;
			//ofLog()<<"get star";
			l.addVertex(centre.x+s.pos.x,centre.y+s.pos.y,ofColor(1.0f-((now-s.birthday)/s.lifespan)*128.0f));
			l.addVertex(centre.x+s.pos.x+1,centre.y+s.pos.y+1,ofColor(1.0f-((now-s.birthday)/s.lifespan)*128.0f));
			o.push_back(l);
		}
		return o;
	}	
};

class glyph{
public:
	glyph(char c,float w,vector<ofPolyline> lines){
		glyph(c,w,lines,ofColor(0,0,0));
	}
	glyph(char c,float w,vector<ofPolyline> lines,ofColor col){
		code=c;
		width=w;
		outline=lines;
		colour=col;
	}
	void draw(float x, float y){
		ofSetColor(colour);
		ofPushMatrix();
		ofTranslate(x,y);
		for (auto& v:outline) v.draw();
		ofPopMatrix();
	}
	void randomiseColour(){
		colour=ofColor::fromHsb(ofRandom(255.0),225,255);
	}
	char code;
	float width;
	vector<ofPolyline> outline;
	ofColor colour;
};

class glyphWord{
public:
	glyphWord(){amount=1.0f;}
	vector<glyph> glyphs;
	float amount;
};

class glyphbanner{
	ofXml SVGFont;
	vector<glyphWord> words;
	vector<colourPolyline> outlines;
	ofVec2f centre;
	float lastUpdateTime;
	float playhead;
	float enspace;
	vector<ofColor> palette;
	struct {
		vector<ofColor> balanced={
			ofColor::fromHex(0xE27D60),
			ofColor::fromHex(0x085DCB),
			ofColor::fromHex(0xE8A87C),
			ofColor::fromHex(0xC38D9E),
			ofColor::fromHex(0x41B3A3)
		};
		vector<ofColor> uneasy={
			ofColor::fromHex(0x2154B9),
			ofColor::fromHex(0xC45A62),
			ofColor::fromHex(0x95A28A),
			ofColor::fromHex(0x98546D),
			ofColor::fromHex(0xE9DADA),
			ofColor::fromHex(0x9FF3E9),
			ofColor::fromHex(0xD07B37),
			ofColor::fromHex(0x741710),
			ofColor::fromHex(0x102ADC),
			ofColor::fromHex(0x9FA1AC)
		};
	}palettes;
	vector<string> split(string s) {
	    size_t pos_start = 0, pos_end;
	    string token;
	    vector<string> res;

	    while ((pos_end = s.find (" ", pos_start)) != string::npos) {
	        token = s.substr (pos_start, pos_end - pos_start);
	        pos_start = pos_end + 1;
	        res.push_back (token);
	    }

	    res.push_back (s.substr (pos_start));
	    return res;
	}
public:	
	glyphbanner(){
		palette=palettes.uneasy;
	};
	void init(string message){
		createWords(message);
		lastUpdateTime=ofGetElapsedTimef();
		playhead=0.0f;
	}
	int length(){
		int l=0;
		for (auto& w:words) {
			l+=w.glyphs.size();
		}
		return l+max(0,(int)words.size()-1);
	}
	float width(){
		float _w=0.0f;
		for (auto& w:words) {
			for (auto& g:w.glyphs) _w+=g.width;
		}
		return _w+max((float)(words.size()-1)*enspace,0.0f);
	}
	int glyphCount(){
		int c=0;
		for (auto& w:words){
			c+=w.glyphs.size();
		}
		return c;
	}
	string text(){
		string s;
		for (auto& w:words) {
			for (auto& g:w.glyphs) s+=ofToString(g.code);
		}
		return s;
	}
	void loadFont(filesystem::path path){
		if( SVGFont.load(path) ){
			vector<glyphWord> w=words;
			clear();
			createWords(w);
			enspace=getGlyph(' ').width;
	        ofLog()<<"loaded "<<path.stem().string();
	    }else{
	        ofLog()<<"unable to load "<<path<<" check data/ folder";
	    }

	}
	void createWords(string message,bool usePalette=false){
		clear();
		vector<string> m=split(message);
		for (auto& word: m){
			glyphWord w;
			for (auto& c: word){
				w.glyphs.push_back(getGlyph(c,
					usePalette?
					palette[ofRandom(palette.size())]:
					//112->231 hue sat 0->255 brightness 255
					ofColor::fromHsb(ofRandom(119)+112,ofRandom(255),255)
				));
			}
			words.push_back(w);
		}
		//ofLog()<<"created "<<words.size()<<" words";
	}
	void createWords(vector<glyphWord> _words){
		clear();
		for (auto& _w:_words) {
			glyphWord w;
			for (auto& g: _w.glyphs){
				w.glyphs.push_back(getGlyph(g.code,g.colour));
			}
			words.push_back(w);
		}
	}
	glyph getGlyph(char c,ofColor col=ofColor(255,255,255)){
		vector<ofPolyline> shapes;
		ofPolyline shape;
		string elementPath = "/svg/defs/font/glyph[@unicode='"+ofToString(c)+"']";

		if(SVGFont.findFirst(elementPath) == 0 ){
            elementPath = "/svg/defs/font/glyph[@unicode='?']";
        }
        
        ofXml xmlElement = SVGFont.findFirst(elementPath);
        
        float charWidth = ofToFloat(xmlElement.getAttribute("horiz-adv-x").getValue());
        
        vector<string> splitGlyphPath = ofSplitString(xmlElement.getAttribute("d").getValue(), " ");//glyph path data in SVG looks like this: "M 139 -9.45 L 230 18.9 L 299 22.1 L 227 25.2"
        
        for(int i=0; i<splitGlyphPath.size(); i+=3){
            if(splitGlyphPath[i] == "M"){
            	if (shape.size()) {
            		shapes.push_back(shape);
            		shape.clear();
            	}
                shape.addVertex(ofToFloat(splitGlyphPath[i+1]), ofToFloat(splitGlyphPath[i+2]));
            }else if(splitGlyphPath[i] == "L"){
                shape.lineTo(ofToFloat(splitGlyphPath[i+1]), ofToFloat(splitGlyphPath[i+2]));
            }
        }
        if (shape.size()) shapes.push_back(shape);
        return glyph(c,charWidth,shapes,col);
	}
	void addGlyph(char g,bool usePalette=false){
		if (g==' ') words.push_back(glyphWord());
		else {
			words[words.size()-1].glyphs.push_back(getGlyph(g,
				usePalette?
				palette[ofRandom(palette.size())]:
				ofColor::fromHsb(ofRandom(119)+112,ofRandom(255),255)
			));
		}
	}
	void removeGlyph(){
		if (words.size()){
			glyphWord lw=words[words.size()-1];
			lw.glyphs.pop_back();
			if (!lw.glyphs.size()){
				words.pop_back();
			}
		}
	}
	void clear(){words.clear();}
	void update(float speed=1.0f,float usePalette=false){

		float delta=ofGetElapsedTimef()-lastUpdateTime;
		lastUpdateTime=ofGetElapsedTimef();
		playhead+=delta*speed;

		int theword=0;
		float segment=0.0f;
		if (false){ //1 word per second
			theword=int(playhead)%words.size();
			segment=playhead-int(playhead);
		}
		else { //proportional to #of letters
			int theletter=int(playhead)%glyphCount();
			theword=0;
			while(theletter>words[theword].glyphs.size()){
				theletter-=words[theword].glyphs.size();
				theword++;
			}
			float playfraction=playhead-int(playhead);

			//segment=(((float)theletter+words[theword].glyphs.size()+playhead-int(playhead))/words[theword].glyphs.size());
			segment=(((float)theletter+playfraction-1)/words[theword].glyphs.size());
					}
		//calculate params for word/letter anim
		for (int i=0;i<words.size();i++){
			words[i].amount=(i==theword?sin(segment*3.14):0);
			for (auto& g:words[i].glyphs){
				if (ofRandom(100)<speed) {
					g.colour=
						usePalette?
						palette[ofRandom(palette.size())]:
						ofColor::fromHsb(ofRandom(119)+112,ofRandom(255),255);
				}
			}
		}


	}
	vector<colourPolyline>& getOutlines(float s=1.0f){
		outlines.clear();
		float p=(-width())/2;
		for (auto& w:words){
			for (auto& g:w.glyphs){
				if (w.amount>0.0f){
					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*w.amount));
					}
				}
				p+=g.width;
			}
			p+=enspace;
		}
		return outlines;
	}
};

class ofApp : public ofBaseApp{

	public:
		void setup();
		void update();
		void draw();
		void exit();
		void keyPressed(ofKeyEventArgs &keyargs);
		void keyReleased(int key);
		void mouseMoved(int x, int y );
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void mouseEntered(int x, int y);
		void mouseExited(int x, int y);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);

		void default_settings();
		void save_settings();

        ofxHelios laser;

        ofDirectory fonts;
        int currentFont;
        string displaytext;
        glyphbanner banner;

        ofxPanel starsgui;
		ofParameter<float> stars_x;
		ofParameter<float> stars_y;
		ofParameter<float> stars_rate;
        ofParameter<float> stars_radius;
        ofParameter<float> stars_speed;
        ofParameter<float> stars_life;
        ofParameter<bool> bScanStars;

        ofxPanel textgui;
		ofParameter<float> laser_scale;
		ofParameter<float> laser_pos_x;
		ofParameter<float> laser_pos_y;
        ofParameter<float> text_speed;

        //======= laser gui

		ofxPanel lasergui;
		ofParameter<bool> laser_power;
        ofParameter<int> laser_intensity;
        ofParameter<int> laser_points;
        ofParameter<int> laser_subdivide;
        ofParameter<int> laser_blank_num;
        ofParameter<float> laser_max_angle;

        ofxXmlSettings XML;

        starSystem stars;

        glm::vec2 warpframe[4];
		int select_warpframe;
		bool bDrawFrame;	

		ofPoint outputWindowSize;

		ofPoint outputPosition;
		float outputScale;

};