summaryrefslogtreecommitdiff
path: root/nextus/src/ofApp.h
blob: 47f5a7f56a20d61d045e04dbf04a1966237b00b6 (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
#pragma once

#include "ofMain.h"


#include "ofxGui.h"
#include "ofxXmlSettings.h"
#include "ofxSVG.h"
#include "ofxClipper.h"
#include "ofxMidi.h"	
#include "ofxPONK.h"

#include "lineTransformer.h"
#include "lineSegmenter.h"

static ofVec2f DISPLAYSIZE(200,200);
static int panelnum=1;

class vectorPanel {
	public:
		vectorPanel(
			string _title="",
			ofVec2f _size=DISPLAYSIZE,
			ofVec2f _pos=ofPoint(5,5))
			{
			size=_size;
			position=_pos;
			panel.setup(_title,"",0,size.y+5);
		}
		void draw(){
			ofPushMatrix();
			ofTranslate(position);
			ofSetColor(255);
			ofNoFill();
			ofDrawRectangle(0,0,size.x,size.y);
			panel.draw();
			ofPushMatrix();
			ofTranslate(size/2);
			ofScale(DISPLAYSIZE.x/2.0f);
			vector<colourPolyline> lines=getLines();
			for (auto& line:lines){
				line.draw();
			}
			ofPopMatrix();
			ofPopMatrix();
		}
		virtual vector<colourPolyline> getLines() {};

	private:
		ofVec2f size;
		ofPoint position;
		ofxPanel panel;
};

class svgPanel: public vectorPanel{
	public:
		svgPanel(
			string _title="",
			ofVec2f _size=DISPLAYSIZE,
			ofVec2f _pos=ofPoint(5,5)
			) : vectorPanel(_title,_size,_pos){}
		void load(string filename){
			
			ofxSVG svg;
			svg.load(filename);

			//normalise the SVG paths to a square format

			float dimension=max(svg.getWidth(),svg.getHeight());

			glm::vec2 src[]={
		    	glm::vec2(0,0),
		    	glm::vec2(dimension,0),
		    	glm::vec2(dimension,dimension),
		    	glm::vec2(0,dimension)
		    };

		    glm::vec2 dst[]={ //PONK coords
		    	glm::vec2(-1,-1),
		    	glm::vec2(1,-1),
		    	glm::vec2(1,1),
		    	glm::vec2(-1,1)
		    };

			ofMatrix4x4 xform=lineTransformer::getPerspectiveTransformMatrix(src,dst);
			
			vector <ofPath> imagepaths= svg.getPaths();

			std::stringstream strm;

			if (imagepaths.size()){
				segmenters.clear();
				//shape_selection_durations.clear();
				for (auto& path:imagepaths){
					path.setPolyWindingMode(OF_POLY_WINDING_ODD);

					vector <ofPolyline> outlines= path.getOutline();
					for (auto& outline:outlines){
						//strm << outline.size() << "->";
						//outline.simplify(contour_simplify);
						//strm << outline.size() << " ";
						ofPolyline xformed=lineTransformer::polyLineTransform(xform,outline);
						ofColor c1=path.getStrokeColor();
						ofColor colour=path.getFillColor();
						if (colour.r==colour.g&&colour.g==colour.b){
							colour=c1;
						}

						segmenters.push_back(
							colourLineSegmenter(
								xformed,
								path.getStrokeColor()
								)
							);
						//shape_selection_durations.push_back(0.0f);
					}
		
					//strm << " , ";	
				}

				//std::stringstream strom;

				//shape_selection.clear();
				//while (shape_selection.size()<(segmenters.size()*shapes_amount)){
				//	int selection=(int)ofRandom(0,segmenters.size());
				//	shape_selection.insert(selection);
				//}
				//for (auto s:shape_selection){
				//	float spawn=(ofRandom(0,(float)shapes_duration));
				//	shape_selection_durations[s]=spawn;
				//	strom << s << ":"<<spawn<<" ";
				//}
				//last_frame_time=ofGetElapsedTimef();
				//cout << "SVG: selected paths [ "<<strom.str() << " ]" <<std::endl;
				//cout << "SVG: found " << imagepaths.size() << " paths with " << segmenters.size() << " shapes [ " << strm.str() << " ]" <<std::endl;
				//select_random_shapes(shapes_duration);
			ofLog()<<"loaded SVG "<<filename<<", "<<segmenters.size()<<" paths";

			}
			else {
				ofLog()<<"could not load SVG "<<filename;
			}
		}
		vector<colourPolyline> getLines(){
			vector<colourPolyline> output;
			for (auto& shape:segmenters){
				output.push_back(shape.getPoly());
			}
			//ofLog()<<"svg found "<<output.size()<<" lines";
			return output;
		}

	private:
		//ofxFloatSlider contour_simplify;

		//svg gui
		//ofxToggle	 shapes_randomise;
		//ofxFloatSlider shapes_amount;
		//ofxFloatSlider shapes_duration;
		//ofxToggle use_mask;
		//ofxToggle invert_mask;

		//segmenter
		//ofxToggle use_segmenter;
		//ofxToggle colour_segmenter;
		//ofxFloatSlider segmenter_speed;
		//ofxFloatSlider segmenter_length;
		//ofxIntSlider segmenter_number;

		
		vector <colourLineSegmenter> segmenters;
		//vector <float> shape_selection_durations;
		//set <int> shape_selection;
		//int framecounter;
		//float phase,prev_time; //to calculate phase
		//float rotate_amt;
		//float scale_phase,scale_amt;
};

class ofApp: public ofBaseApp, public ofxMidiListener {

	public:

		ofApp() : 
			networkinput("network",DISPLAYSIZE,ofPoint(5,5)),
			svginput("svg",DISPLAYSIZE,ofPoint(210,5)),
			textinput("text",DISPLAYSIZE,ofPoint(415,5)) {}

		void setup();
		void update();
		void draw();
		void exit();

		void keyPressed(ofKeyEventArgs &keyargs);
		void keyReleased(ofKeyEventArgs & args);
		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 newMidiMessage(ofxMidiMessage& eventArgs);

		//======================================= //MIDI

		ofxMidiIn midiIn;
		ofxMidiMessage midiMessage;
		
		//======================================= //saving settings

		ofxXmlSettings XML;

		void default_settings();
		void save_settings();
		void load_settings();

		//======================================= //inputs

		svgPanel networkinput;
		svgPanel svginput;
		svgPanel textinput;

		//======================================= //output

		ofxPONKSenderPanel madlaser;

};