-
Notifications
You must be signed in to change notification settings - Fork 2
/
FM_extra.pde
220 lines (184 loc) · 5.18 KB
/
FM_extra.pde
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
// #####################################
// ############## PROJECT ##############
// #####################################
public void project_selector() {
flatmap_path = "";
JFileChooser fc = new JFileChooser();
int i = fc.showDialog( null, "select flatmap" );
if ( i == 0 ) {
flatmap_path = fc.getSelectedFile().getPath();
if ( !flatmap_path.substring( flatmap_path.length() - 8, flatmap_path.length() ).equals( ".flatmap" ) ) {
flatmap_path += ".flatmap";
}
System.out.println( flatmap_path );
}
}
// #####################################
// ################ OSC ################
// #####################################
public void start_osc() {
oscP5 = new OscP5( this, oscP5_port );
}
public void oscEvent(OscMessage msg) {
if ( editmode != ControlFrame.EDITMODE_NORMAL ) {
return;
}
if ( msg.checkAddrPattern("/tube/length") ) {
demo_rt_length = msg.get(0).floatValue();
}
for( Mappable m : map.ms ) {
m.parse( msg );
}
}
// #####################################
// ############# TEXTURES ##############
// #####################################
class TextureRef {
public PImage im = null;
public Object src = null;
public String type = "undefined";
}
public PImage get_default_texture() {
return get_texture( default_texture_path );
}
public PImage get_texture( String path ) {
if ( !texture_atlas.containsKey( path ) && !load_texture( path ) ) {
return null;
}
return (PImage) texture_atlas.get( path ).src;
}
public TextureRef get_texture_ref( int id ) {
String[] names = new String[texture_atlas.keySet().size()];
texture_atlas.keySet().toArray(names);
if ( id < 0 || id >= names.length ) {
return null;
}
return texture_atlas.get( names[ id ] );
}
public TextureRef get_texture_ref( Object src ) {
String[] names = new String[texture_atlas.keySet().size()];
texture_atlas.keySet().toArray(names);
for ( int i = 0; i < names.length; ++i ) {
if ( texture_atlas.get( names[ i ] ).src == src ) {
return texture_atlas.get( names[ i ] );
}
}
return null;
}
public PImage get_texture( int id ) {
TextureRef tr = get_texture_ref( id );
if ( tr == null ) {
return null;
}
return (PImage) tr.src;
}
public PImage get_texture_tumb( int id ) {
TextureRef tr = get_texture_ref( id );
if ( tr == null ) {
return null;
}
return tr.im;
}
private void load_default_texture() {
texture_atlas = new java.util.HashMap<String,TextureRef>();
load_texture( default_texture_path );
}
private boolean load_texture( String path ) {
try{
TextureRef tr = new TextureRef();
tr.src = loadImage( path );
tr.im = (PImage) tr.src;
tr.type = "PImage";
texture_atlas.put( path, tr );
return true;
} catch( Exception e ) {
}
return false;
}
private void texture_register_movie( String name, Movie mov ) {
TextureRef tr = new TextureRef();
tr.im = mov;
tr.src = mov;
tr.type = "Movie";
texture_atlas.put( name, tr );
}
private void texture_register_pgraphic( String name, PGraphics pg ) {
TextureRef tr = new TextureRef();
tr.src = pg;
tr.type = "PGraphics";
texture_atlas.put( name, tr );
}
private PImage texture_thumb( PGraphics src ) {
PImage thumb = createImage( src.width, src.height, RGB );
src.loadPixels();
thumb.loadPixels();
for( int i = 0; i < src.pixels.length; ++i ) {
thumb.pixels[i] = src.pixels[i];
}
thumb.updatePixels();
TextureRef tr = get_texture_ref( src );
if ( tr != null ) {
tr.im = thumb;
}
return thumb;
}
// #####################################
// ########### SERIALISATION ###########
// #####################################
public String serialisation_path() {
if ( flatmap_path == null || flatmap_path.equals("") ) {
flatmap_path = dataPath("") + "/flatmap.flatmap";
}
return flatmap_path;
}
public synchronized void save_flatmap() {
try {
JSONObject _data = new JSONObject();
_data.setJSONObject("ResolutionConfig", ResolutionConfig.json() );
int i;
JSONArray mappables = new JSONArray();
i = 0;
for ( Mappable m : map.ms ) {
mappables.setJSONObject( i, m.json() );
++i;
}
_data.setJSONArray( "Mappable", mappables );
saveJSONObject( _data, serialisation_path() );
} catch ( Exception e ) {
}
}
public synchronized void load_flatmap() {
if ( map == null ) {
map = new FlatMap();
} else {
map.purge();
}
JSONObject _data;
try {
_data = loadJSONObject( serialisation_path() );
} catch ( Exception e ) {
e.printStackTrace();
return;
}
JSONArray mappables_data = _data.getJSONArray("Mappable");
for (int i = 0; i < mappables_data.size(); i++) {
JSONObject m_data = mappables_data.getJSONObject(i);
if ( m_data.getString( "type" ).equals( "Line" ) ) {
Line l = new Line();
if ( l.json( m_data ) ) {
map.ms.add( l );
map.ls.add( l );
}
} else if ( m_data.getString( "type" ).equals( "Plane" ) ) {
Plane p = new Plane();
if ( p.json( m_data ) ) {
System.out.println( "Plane sucessfully decompressed" );
map.ms.add( p );
map.ps.add( p );
}
}
for ( Mappable m : map.ms ) {
m.set_parent( this );
}
}
}