summaryrefslogtreecommitdiff
path: root/gui/src/chaoslib.h
blob: 9196e88f1ac6f9d9713a57a444c59551c3d8c35e (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
#pragma once

#include "ofMain.h"
#include "ofxGui.h"
#include "ofxAChaoslib.h"


class AChaosplugin{
    public:
        AChaosBase* plugin;
        vector <ofParameter <float>> params;
        string name;
        AChaosplugin(AChaosBase* _plugin,string _name,vector <REAL> _params={0,0,0,0,0,0}){
            plugin=_plugin;
            name=_name;
            ofLog()<<"AChaosplugin creating "<<_name<<" with "<<_plugin->get_param_labels().size()<<" parameters";
            //ofLog()<<"AChaosplugin copied "<<name<<" with "<<plugin->get_param_labels().size()<<" parameters";
            plugin->setup();
            
            load_defaults();
            //ofLog()<<"AChaosplugin created "<<name<<" with "<<params.size()<<" parameters";
        }
        
        void load_defaults(){
            params.clear();
            for (int i=0;i<plugin->iv.size();i++){
                if (plugin->get_param_labels()[i].size()){
                    params.push_back(ofParameter <float>().set(plugin->get_param_labels()[i],plugin->iv[i],0,plugin->iv[i]*5));
                }
            }
        }
    
};

class Chaosplugin{
    public:
    	Chaosplugin(){

    	}
        vector <AChaosplugin*> plugins;
        int whichplugin;
        ofParameter <string> name;
};


class Chaoslib{
    public:
        vector <AChaosplugin*> plugins;
        vector <vector <REAL>> params;
        int whichplugin;
        ofParameter <string> name;
        Chaoslib(){
            //can only use those that have 2 output params?
            //or just make it 1 dimensional? only affect the x axis?
            //plugins.push_back(new AChaosplugin(new AChaosBaker(),"Baker"));
            plugins.push_back(new AChaosplugin(new AChaosClifford(),"Clifford"));
            plugins.push_back(new AChaosplugin(new AChaosCollatz(),"Collatz"));
            plugins.push_back(new AChaosplugin(new AChaosDuffing(),"Duffing"));
            plugins.push_back(new AChaosplugin(new AChaosGinger(),"Ginger"));
            plugins.push_back(new AChaosplugin(new AChaosHenon(),"Henon"));
            whichplugin=0;
            update_name();
        }



        AChaosbase* get_plugin(){
            return plugins[whichplugin]->plugin;
        }
        AChaosbase* get_params(){
            return plugins[whichplugin]->params;
        }
        void next(){
            whichplugin=(whichplugin+1)%plugins.size();
            update_name();
        }
        void previous(){
            whichplugin=whichplugin-1;
            if (whichplugin<0){
                whichplugin=plugins.size()-1;
            }
            update_name();
        }
        void update_name(){
            name=plugins[whichplugin]->name;
            //save old params & load new
        }
        std::string &get_name(){
            return plugins[whichplugin]->name;
        }
};

class chaosPanel : public ofxPanel {
	public:
	chaosPanel(){
		ofRegisterKeyEvents(this, defaultEventsPriority);
	}
	bool isSelected;
	ofParameter<bool> active;
    ofParameter<float> amount;
    ofParameter<float> scale; //?per plugin??
	bool mouseMoved(ofMouseEventArgs & args){
		if (args.x>getPosition().x&&
			args.x-getPosition().x<getWidth()&&
			args.y>getPosition().y&&
			args.y-getPosition().y<getHeight()){
			if (!isSelected){
				isSelected=true;	
				setBorderColor(ofColor(255,128,0));
				//ofLog()<<"IN> "<<args.x<<","<<args.y;
			}
		}
		else {
			if (isSelected){
				isSelected=false;
				setBorderColor(ofColor(0,0,0));
				//ofLog()<<"OUT> "<<args.x<<","<<args.y;
			}
		}
	}
	bool keyPressed(ofKeyEventArgs & args){
		if (isSelected){
			//ofLog()<<"KEY> "<<args.key;
			switch(args.key){
				case 3812:{
					chaosloader.previous();
					update_sliders();
					break;
				}
				case 3814:{
					chaosloader.next();
					update_sliders();
					break;
				}
			}
		}
	}
	bool keyReleased(ofKeyEventArgs & args){
		//required in order to call ofRegisterKeyEvents
	}
	//this is a bit tangled
	/*
	audioplotter stores drawings that it extracts from audio
	chaosloader manages a set of plugins that affect polylines
	audio plotter calls chaosloader a set of lines and it affects them
	chaosloader should own it's own params (use,scale,amount)
	should expose compute(vector polyline)

	so we could have 

	audio source
	<>
	transformer
	<>
	chaos
	<>
	drawing
	
	*/

	int shown=0;

	void update_sliders(){
		clear();
		add(plugin_label.setup(chaosloader.name));
    	add(active.set("use",false));
    	add(amount.set("amount", 0.0f, -0.1f, 0.1f));

	    for (int i=0;i<chaosloader.get_params().size();i++){
	    	if (chaosloader.get_plugin()->get_param_labels()[i].size()){
    			
    			add(chaosloader.get_params()[i]);

	   		}
	   	}
	}
	colourPolyline compute_chaos(colourPolyline& poly){
		float chaosscale=1.0f;
	    colourPolyline tempPoly;
	    for (int i=0;i<poly.size();i++){
	            REAL iv[chaosloader.get_plugin()->iv.size()];
	            iv[0]=poly[i].x-(ofGetWidth()/2)/chaosscale;
				iv[1]=poly[i].y-(ofGetHeight()/2)/chaosscale;
				//chaos_a,chaos_b,chaos_k,chaos_p};
	            //ofLog() << i<<": calculating chaos with: "<<poly[i].x<<"->"<<((poly[i].x-(ofGetWidth()/2))/chaosscale)<<" "<<poly[i].y<<"->"<<((poly[i].y-(ofGetHeight()/2))/chaosscale)<<" "<<attractor.a<<" "<<attractor.b<<" "<<attractor.k<<" "<<attractor.p;
	            chaosloader.get_plugin()->set(iv);
	            chaosloader.get_plugin()->calc();
	            //ofLog() << i<<": got points: "<<attractor.nx<<" "<<attractor.ny;
	            tempPoly.addVertex(
	            	ofPoint((chaosloader.get_plugin()->ov[0]*chaosscale)+(ofGetWidth()/2),
	            		(chaosloader.get_plugin()->ov[1]*chaosscale)+(ofGetHeight()/2)),
	            	poly.getColourAt(i));
	    }
    return tempPoly;
}
private: 
	Chaoslib chaosloader;
	ofxLabel plugin_label;
    ofParameter<string> plugin_name;


};