Repository navigation
Expand file tree
/
Copy pathApplication.java
More file actions
454 lines (378 loc) · 10.7 KB
/
Copy pathApplication.java
File metadata and controls
454 lines (378 loc) · 10.7 KB
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
443
444
445
446
447
448
449
450
451
452
453
454
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.MouseInfo;
import java.awt.Rectangle;
import java.awt.Robot;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.media.opengl.GL2;
import processing.core.PApplet;
import processing.opengl.PGL;
import processing.opengl.PJOGL;
import processing.opengl.PGraphicsOpenGL;
import oscP5.OscMessage;
import controlP5.ControlEvent;
import sojamo.drop.DropEvent;
import sojamo.drop.SDrop;
import dmxP512.DmxP512;
import codeanticode.syphon.SyphonServer;
public class Application extends PApplet {
public Layer layers[];
public boolean layerEnabled[];
/**
* layer currently selected in the GUI
*/
public int currentLayer = 0;
/**
* Used as identifier when running multiple instances
*/
public int id = (int)(Math.random()*100);
// Controllers
protected List<Controller> controllers = new ArrayList<Controller>();
protected PresetManager presetManager;
protected KeyboardController keyboardController;
protected MouseController mouseController;
protected SoundController soundController;
protected MIDIController midiController;
protected MIDILaunchpadController midiLaunchpadController;
protected GUIController guiController;
protected OscController oscController;
protected GlobalController globalController;
protected DmxP512 dmxP512;
protected ThreadsTime threadsTime;
protected SDrop drop;
//OpenGL stuff
protected GLTextureWindow previewWindow;
protected PGL pgl;
protected PGraphicsOpenGL offScreenPgl;
protected GL2 gl;
protected SyphonServer syphonServer;
protected ShaderRenderer shaderRenderer;
//mouse mover to keep screen saver out !
protected Robot robot;
protected long robotLastMove=0;
//frame recording
public boolean saveFrame=false;
//get my name.
protected static final String name=new CurrentClassGetter().getClassName();
public static class CurrentClassGetter extends SecurityManager {
public String getClassName() {return getClassContext()[1].getName();}
}
private static final long serialVersionUID = 9841879847L;
/**
* ran when launched as stand alone
* @param args
*/
public static void main(String args[]) {
PApplet.main(new String[] {name});
}
/**
* override default init method to handle full screen mode.
*/
@Override
public void init() {
frame.setTitle("Patterns! (#"+id+")");
//display display information
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] displayDevices = ge.getScreenDevices();
System.out.println("Displays:");
for(GraphicsDevice displayDevice : displayDevices){
GraphicsConfiguration[] gc = displayDevice.getConfigurations();
Rectangle monitor = gc[0].getBounds();
System.out.println("\t"+displayDevice.getIDstring()+"(" + monitor.width + "x" + monitor.height+"@" +monitor.x+"x"+monitor.y+")");
}
if(Config.PREVIEW || Config.SYPHON){
Config.OFFSCREEN=true;
}
//auto full screen based on screens definition
if(Config.SCREEN>=0 && Config.SCREEN<displayDevices.length){
Config.CURSOR = false;
Config.BORDERS = false;
GraphicsDevice displayDevice;
displayDevice=displayDevices[Config.SCREEN];
System.out.println("using display "+displayDevice.getIDstring());
Rectangle monitor = displayDevice.getConfigurations()[0].getBounds();
Config.X=monitor.x;
Config.Y=monitor.y;
Config.HEIGHT=monitor.height;
Config.WIDTH=monitor.width;
// remove window decoration while in full screen mode
frame.removeNotify();
frame.setUndecorated(true);
frame.setResizable(false);
frame.addNotify();
frame.setLocation(Config.X, Config.Y);
}else{
if(Config.SCREEN>=0){
System.out.println("screen "+Config.SCREEN+" does not exists. disabling fullscreen");
}
if(!Config.BORDERS){
frame.removeNotify();
frame.setUndecorated(true);
frame.setResizable(false);
frame.addNotify();
frame.setLocation(Config.X, Config.Y);
}else{
frame.setResizable(true);
}
}
super.init();
}
@Override
public void setup() {
//new Exception().printStackTrace();
if (!Config.BORDERS){
frame.setLocation(Config.X, Config.Y);
}
// init Layers
layers = new Layer[15];
layers[0] = new LayerNineBlockPattern(this);
//layers[0] = new LayerNoise(this);
layerEnabled = new boolean[layers.length];
//layerEnabled[0] = true;
for (int i = 1; i < layerEnabled.length; i++) {
layerEnabled[i] = false;
}
if (!Config.CURSOR){
this.noCursor();
}
//OPENGL
size(Config.WIDTH, Config.HEIGHT, OPENGL);
if(Config.OFFSCREEN){
//create offscreen renderer
offScreenPgl = (PGraphicsOpenGL)createGraphics(Config.WIDTH, Config.HEIGHT, OPENGL);
}
//if(!Config.SYPHON&&!Config.PREVIEW){
/*
if(Config.OPENGL_ANTIALIASING==2)
hint(ENABLE_OPENGL_2X_SMOOTH);
else if(Config.OPENGL_ANTIALIASING==4)
hint(ENABLE_OPENGL_4X_SMOOTH);
else
hint(DISABLE_OPENGL_2X_SMOOTH);
*/
//}
// controllers setups
// need to make the controller synchronised when gui is in main window
if(!Config.GUI_WINDOW){
if (Config.SOUND && soundController==null){
soundController = new SoundController(this);
}
guiController = new GUIController(this,Config.GUI_WINDOW,Config.GUI_VISIBLE);
}
lateControllersSetup();
}
/**
* make controller creation async to
* get the main window ready as quick as possible
*/
private void lateControllersSetup(){
LateControllersSetupThread async = new LateControllersSetupThread(this);
async.start();
//async.runSync();
}
private void killScreenSaver(){
FloatParameter.modulateRatio=0.1f;
long now = System.currentTimeMillis();
try{
if(robot!=null && (now - robotLastMove) > 1000 * 60 * 5){
int x = MouseInfo.getPointerInfo().getLocation().x;
int y = MouseInfo.getPointerInfo().getLocation().y;
//robot.
robot.mouseMove(x+2, y+2);
robot.mouseMove(x, y);
robotLastMove=now;
}
}catch(Exception e){
e.printStackTrace();
}
}
public void newLayerInCurrentLayer(){
this.newLayerInLayer(this.currentLayer);
}
public void newLayerInLayer(int layer){
if(guiController!=null && guiController.controlP5.controlWindow!=null){
layers[currentLayer]=Layer.newLayer(this,guiController.controlP5.controlWindow.getCurrentTab().getName());
Layer newlayer = layers[layer];
if(newlayer!=null){
FloatParameter bgAlpha = newlayer.getFloatParameter("bgAlpha");
if(bgAlpha!=null){
if(layer==0)
bgAlpha.setV(1);
else
bgAlpha.setV(0);
}
}
}
}
@Override
public void draw() {
killScreenSaver();
//start openGL
if(Config.OFFSCREEN){
pgl = offScreenPgl.beginPGL();
}else{
pgl = beginPGL();
}
gl = ((PJOGL)beginPGL()).gl.getGL2();
// set vertical sync on
if(Config.VERTICAL_SYNC){
gl.setSwapInterval(1);
}
//clear GL Depth
gl.glClear(GL2.GL_DEPTH_BUFFER_BIT);
//gl.glEnable(GL.GL_BLEND);
//resize screen
gl.glViewport(0, 0, width, height);
gl.getGL2().glMatrixMode(GL2.GL_PROJECTION);
gl.getGL2ES1().glLoadIdentity();
gl.getGL2ES1().glOrtho(0.0, width, 0.0, height, -100.0, +100.0);
gl.getGL2().glMatrixMode(GL2.GL_MODELVIEW);
gl.getGL2ES1().glLoadIdentity();
//controllers
for(Controller controller : controllers){
if(controller!=null){
controller.control();
controller.update();
}
}
drawLayers();
//end openGL
if(Config.OFFSCREEN){
//end draw
offScreenPgl.endDraw();
//render shader
if(shaderRenderer==null){
shaderRenderer=new ShaderRenderer(
this,
(PGraphicsOpenGL) g,
"data"+File.separator+"shaders"+File.separator
+"edges.glsl"
//+"pixelNoise.glsl"
);
}
if(shaderRenderer!=null){
shaderRenderer.update();
}
//draw texture
this.image(offScreenPgl,0,0);
//preview window
if(Config.PREVIEW && previewWindow!=null){
if(previewWindow.init(gl)){
previewWindow.show();
}
previewWindow.setTexture(((PGraphicsOpenGL) g).getTexture());
previewWindow.render();
}
//syphon
if(Config.SYPHON && syphonServer!=null){
syphonServer.sendImage(offScreenPgl);
}
}else{
//nothing to do non offscreen mode.
}
//saving frame on request
if(saveFrame){
saveFrame=false;
String filename = new SimpleDateFormat("yyyy-MM-yy_HH-mm-ss").format(new Date())+".png";
saveFrame(filename);
System.out.println("saved frame " + filename);
}
}
private void drawLayers(){
// paint patterns
for (int i = 0; i < layers.length; i++) {
//get the reference to prevent null pointers...
Layer layer=layers[i];
try{
if (layer != null){
layer.update();
//draw main background
if (i == 0){
layer.drawBackground(gl);
}
}
if (layerEnabled[i]) {
//create pattern if needed
if(layer==null){
newLayerInCurrentLayer();
}
if(layer!=null){
if (i != 0){
layer.drawBackground(gl);
}
layer.draw(gl);
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
//controlP5 callback
public void controlEvent(ControlEvent event) {
if(guiController!=null)
guiController.controlEvent(event);
}
//OscP5 callcack
void oscEvent(OscMessage theOscMessage) {
if( oscController!=null)
oscController.oscEvent(theOscMessage);
}
//keyboard callback
public void keyPressed(){
if(key==27){
key=0;
}
if(keyboardController!=null)
keyboardController.keyPressed(key,keyCode);
}
public void keyReleased(){
if(keyboardController!=null)
keyboardController.keyReleased(key,keyCode);
}
//mouse callback
public void mousePressed(){
if(mouseController!=null){
if(guiController!=null &&!Config.GUI_WINDOW && guiController.isVisible()){
guiController.mousePress(mouseX,mouseY,mouseButton);
}else{
mouseController.mousePressed(mouseX,mouseY,mouseButton);
}
}
}
public void mouseReleased(){
if(mouseController!=null){
if(guiController!=null &&!Config.GUI_WINDOW && guiController.isVisible()){
guiController.mouseRelease(mouseX,mouseY,mouseButton);
}else{
mouseController.mouseReleased(mouseX,mouseY,mouseButton);
}
}
}
public void mouseClicked(){
if(mouseController!=null){
if(guiController!=null &&!Config.GUI_WINDOW && guiController.isVisible()){
guiController.mouseClick(mouseX,mouseY,mouseButton);
}else{
mouseController.mouseClicked(mouseX,mouseY,mouseButton);
}
}
}
//sdrop callback
void dropEvent(DropEvent theDropEvent) {
if(theDropEvent.isFile() && this.presetManager!=null) {
File myFile = theDropEvent.file();
if(myFile.isFile() && !myFile.isDirectory()){
this.presetManager.load(myFile);
if(guiController!=null){
guiController.autoSelectTab();
}
}
}
}
}