/
SoftFullScreen.java
257 lines (215 loc) · 6.91 KB
/
SoftFullScreen.java
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
/*
Part of the Processing Fullscreen API
Copyright (c) 2006-08 Hansi Raber
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package fullscreen;
import japplemenubar.JAppleMenuBar;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import processing.core.PApplet;
import processing.core.PConstants;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/**
* Creates a new softfullscreen object. <br>
*
* This will use undecorated frames to bring your sketch to the screen. <br>
* The advantages are:
*
* <ul>
* <li>You can create a sketch that spans across multiple monitors easily</li>
* </ul>
*
* The drawbacks are:
* <ul>
* <li>You cannot change resolution</li>
* <li>Screensaver must be disabled manually</li>
* <li>Notifications and other kinds of annoying popups might just show up on top of your sketch</li>
* </ul>
*/
public class SoftFullScreen extends FullScreenBase{
// We use this frame to go to fullScreen mode...
Frame fsFrame;
GraphicsDevice fsDevice;
//AWTEventListener fsKeyListener;
// the first time wait until the frame is displayed
boolean fsIsInitialized;
// Daddy...
PApplet dad;
/**
* Creates a new softfullscreen object.
*
* @param dad The parent sketch (aka "this")
*/
public SoftFullScreen( PApplet dad ){
this( dad, 0 );
}
/**
* Creates a new softfullscreen object on a specific screen
* (numbering starts at 0)
*
* @param dad The parent sketch (usually "this")
* @param screenNr The screen number.
*/
public SoftFullScreen( PApplet dad, int screenNr ){
super( dad );
this.dad = dad;
GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
if( screenNr >= devices.length ){
System.err.println( "FullScreen API: You requested to use screen nr. " + screenNr + ", " );
System.err.println( "however, there are only " + devices.length + " screens in your environment. " );
System.err.println( "Continuing with screen nr. 0" );
screenNr = 0;
}
fsDevice = devices[screenNr];
WindowListener listener = new WindowAdapter(){
public void windowDeiconified( WindowEvent w ){
if( isFullScreen() && PApplet.platform == PConstants.MACOSX ){
new JAppleMenuBar().setVisible( false );
}
}
};
fsFrame = new Frame( fsDevice.getDefaultConfiguration() );
fsFrame.addWindowListener(listener);
fsFrame.setTitle( dad.frame == null? "":dad.frame.getTitle() );
fsFrame.setIconImage( dad.frame.getIconImage() );
fsFrame.setUndecorated( true );
fsFrame.setBackground( Color.black );
fsFrame.setLayout( null );
fsFrame.setSize(
Math.max( fsDevice.getDisplayMode().getWidth(), dad.width ),
Math.max( fsDevice.getDisplayMode().getHeight(), dad.height )
);
registerFrame( fsFrame );
}
/**
* Are we in FullScreen mode?
*
* @return true if so, yes if not
*/
public boolean isFullScreen(){
return fsFrame.isVisible();
}
/**
* Allow for minimizing the frame
*/
public void minimize(){
if( isFullScreen() ){
if( PApplet.platform == PConstants.MACOSX ){
new JAppleMenuBar().setVisible( true );
}
fsFrame.setState( Frame.ICONIFIED );
}
else{
dad.frame.setState( Frame.ICONIFIED );
}
}
/**
* Restores the frame after it has been minimized.
* If it wasn't minimized this doesn't do much!
*/
public void restore(){
if( isFullScreen() ){
fsFrame.setState( Frame.NORMAL );
if( PApplet.platform == PConstants.MACOSX ){
new JAppleMenuBar().setVisible( false );
}
}
else{
dad.frame.setState( Frame.NORMAL );
}
}
/**
* FullScreen is only available is applications, not in applets!
*
* @return true if fullScreen mode is available
*/
public boolean available(){
return dad.frame != null;
}
/**
* Enters/Leaves fullScreen mode.
*
* @param fullScreen true or false
*/
public void setFullScreen( final boolean fullScreen ){
new DelayedAction( 2 ){
public void action(){
setFullScreenImpl( fullScreen );
}
};
}
@SuppressWarnings("deprecation")
private void setFullScreenImpl( boolean fullScreen ){
if( fullScreen == isFullScreen() ){
// no change required!
return;
}
else if( fullScreen ){
if( available() ){
// remove applet from processing frame and attach to fsFrame
dad.frame.setVisible( false );
fsFrame.add( dad );
if( PApplet.platform == PConstants.MACOSX ){
new JAppleMenuBar().setVisible( false );
}
fsFrame.setVisible( true );
fsFrame.setLocation( fsDevice.getDefaultConfiguration().getBounds().getLocation() );
boolean usesEntireScreen = fsDevice.getDefaultConfiguration().getBounds().getSize().equals( new Dimension( dad.width, dad.height ) );
int appleDriversSuck = PApplet.platform == PConstants.MACOSX && usesEntireScreen? 1:0;
dad.setLocation( ( fsFrame.getWidth() - dad.width ) / 2, ( fsFrame.getHeight() - dad.height ) / 2 - appleDriversSuck );
fsFrame.setExtendedState( Frame.MAXIMIZED_BOTH );
GLDrawableHelper.reAllocate( this );
GLTextureUpdateHelper.update( this );
requestFocus();
notifySketch( dad );
return;
}
else{
System.err.println( "FullScreen API: Fullscreen mode not available" );
return;
}
}
else{
// remove applet from fsFrame and attach to processing frame
fsFrame.setVisible( false );
fsFrame.removeAll();
dad.frame.add( dad );
dad.setLocation( dad.frame.insets().left, dad.frame.insets().top );
// processing.core.fullscreen_texturehelper.update( dad );
if( PApplet.platform == PConstants.MACOSX ){
new JAppleMenuBar().setVisible( true );
}
dad.frame.setVisible( true );
GLDrawableHelper.reAllocate( this );
GLTextureUpdateHelper.update( this );
requestFocus();
notifySketch( dad );
return;
}
}
/**
* Setting resolution is not possible with the SoftFullScreen object.
*/
@Override
public void setResolution( int xRes, int yRes ) {
System.err.println( "Changing resolution is not supported in SoftFullScreen mode. " );
System.err.println( "Use the normal FullScreen mode to make use of that functionality. " );
}
}