Skip to content

Commit

Permalink
[#815] Windowed Fullscreen switch shortcut added.
Browse files Browse the repository at this point in the history
  • Loading branch information
DjThunder committed Jan 10, 2024
1 parent a80789f commit 533613c
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
/**
* Graphic factory implementation.
*/
// CHECKSTYLE IGNORE LINE: ClassDataAbstractionCoupling
public final class FactoryGraphicAwt implements FactoryGraphic
{
/** Reading image message. */
Expand All @@ -60,16 +59,7 @@ public Screen createScreen(Config config)
{
Check.notNull(config);

final Screen screen;
if (config.isWindowed())
{
screen = new ScreenWindowedAwt(config);
}
else
{
screen = new ScreenFullAwt(config);
}
return screen;
return new ScreenAwt(config);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
*/
package com.b3dgs.lionengine.awt.graphic;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import com.b3dgs.lionengine.Check;
import com.b3dgs.lionengine.Config;
Expand All @@ -27,22 +31,23 @@
import com.b3dgs.lionengine.LionEngineException;
import com.b3dgs.lionengine.Resolution;
import com.b3dgs.lionengine.awt.Keyboard;
import com.b3dgs.lionengine.awt.KeyboardAwt;
import com.b3dgs.lionengine.awt.Mouse;

/**
* Full screen implementation.
* Screen implementation.
*
* @see Keyboard
* @see Mouse
*/
final class ScreenFullAwt extends ScreenBaseAwt
final class ScreenAwt extends ScreenBaseAwt
{
/** Error message unsupported full screen. */
static final String ERROR_UNSUPPORTED_FULLSCREEN = "Unsupported resolution: ";
/** Unable to switch to full screen. */
static final String ERROR_SWITCH = "Unable to switch to full screen mode !";
/** Minimum length. */
private static final int MIN_LENGTH = 18;
private static final int MIN_LENGTH = 21;

/**
* Format resolution to string.
Expand All @@ -65,17 +70,28 @@ private static String formatResolution(Resolution resolution, int depth)
.toString();
}

/** Fullscreen mode. */
private java.awt.Window window;
/** Windowed mode. */
private Canvas canvas;
/** Flag to request switch. */
private boolean requestWindowed;
/** Flag to request switch release. */
private boolean requestAltEnter;
/** Flag to allow request. */
private boolean requestAllowed = true;

/**
* Internal constructor.
*
* @param config The config reference.
* @throws LionEngineException If renderer is <code>null</code> or no available display.
*/
ScreenFullAwt(Config config)
ScreenAwt(Config config)
{
super(config);

frame.setUndecorated(true);
requestWindowed = config.isWindowed();
}

/**
Expand All @@ -87,7 +103,7 @@ private static String formatResolution(Resolution resolution, int depth)
*/
private void initFullscreen(Resolution output, int depth)
{
final java.awt.Window window = new java.awt.Window(frame, conf);
window = new java.awt.Window(frame, conf);
window.setBackground(Color.BLACK);
window.setIgnoreRepaint(true);
window.setPreferredSize(new Dimension(output.getWidth(), output.getHeight()));
Expand All @@ -99,8 +115,9 @@ private void initFullscreen(Resolution output, int depth)
output.getRate()));
if (disp == null)
{
throw new LionEngineException(ScreenFullAwt.ERROR_UNSUPPORTED_FULLSCREEN
throw new LionEngineException(ScreenAwt.ERROR_UNSUPPORTED_FULLSCREEN
+ formatResolution(output, depth)
+ System.lineSeparator()
+ getSupportedResolutions());
}
checkDisplayChangeSupport();
Expand All @@ -116,6 +133,84 @@ private void initFullscreen(Resolution output, int depth)
componentForMouse = window;
componentForCursor = window;
frame.validate();

// CHECKSTYLE IGNORE LINE: AnonInnerLength
componentForKeyboard.addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(KeyEvent event)
{
if (requestAllowed
&& event.getModifiersEx() == InputEvent.ALT_DOWN_MASK
&& event.getKeyCode() == KeyboardAwt.ENTER.intValue())
{
requestAllowed = false;
requestAltEnter = true;
requestWindowed = true;
dev.setFullScreenWindow(null);
}
}

@Override
public void keyReleased(KeyEvent e)
{
requestAllowed = true;
}
});
}

/**
* Prepare windowed mode.
*
* @param output The output resolution
* @throws LionEngineException If unable to initialize windowed mode.
*/
private void initWindowed(Resolution output)
{
dev.setFullScreenWindow(null);

canvas = new Canvas(conf);
canvas.setBackground(Color.BLACK);
canvas.setEnabled(true);
canvas.setVisible(true);
canvas.setIgnoreRepaint(true);

frame.add(canvas, 0);

canvas.setPreferredSize(new Dimension(output.getWidth(), output.getHeight()));
frame.pack();
frame.setLocationRelativeTo(null);

ToolsAwt.createBufferStrategy(canvas, conf);
buf = canvas.getBufferStrategy();

// Set input listeners
componentForKeyboard = canvas;
componentForMouse = canvas;
componentForCursor = frame;
frame.validate();

componentForKeyboard.addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(KeyEvent event)
{
if (requestAllowed
&& event.getModifiersEx() == InputEvent.ALT_DOWN_MASK
&& event.getKeyCode() == KeyboardAwt.ENTER.intValue())
{
requestAllowed = false;
requestAltEnter = true;
requestWindowed = false;
}
}

@Override
public void keyReleased(KeyEvent e)
{
requestAllowed = true;
}
});
}

/**
Expand All @@ -126,7 +221,7 @@ private void checkDisplayChangeSupport()
{
if (!dev.isDisplayChangeSupported())
{
throw new LionEngineException(ScreenFullAwt.ERROR_SWITCH);
throw new LionEngineException(ScreenAwt.ERROR_SWITCH);
}
}

Expand All @@ -137,7 +232,7 @@ private void checkDisplayChangeSupport()
*/
private String getSupportedResolutions()
{
final StringBuilder builder = new StringBuilder(Constant.HUNDRED);
final StringBuilder builder = new StringBuilder("Supported resolution(s):" + System.lineSeparator());
int i = 0;
for (final DisplayMode display : dev.getDisplayModes())
{
Expand All @@ -151,17 +246,15 @@ private String getSupportedResolutions()
final int height = display.getHeight();
if (height < Constant.THOUSAND)
{
heightSpace.append(System.lineSeparator());
heightSpace.append(Constant.SPACE);
}
final StringBuilder freqSpace = new StringBuilder();
final int freq = display.getRefreshRate();
if (freq < Constant.HUNDRED)
{
freqSpace.append(Constant.SPACE);
}
builder.append("Supported display mode:")
.append(System.lineSeparator())
.append('[')
builder.append('[')
.append(widthSpace)
.append(width)
.append(Constant.STAR)
Expand Down Expand Up @@ -210,7 +303,14 @@ protected void setResolution(Resolution output)
{
Check.notNull(output);

initFullscreen(output, config.getDepth());
if (requestWindowed)
{
initWindowed(output);
}
else
{
initFullscreen(output, config.getDepth());
}
super.setResolution(output);
}

Expand All @@ -221,4 +321,45 @@ public void start()

super.start();
}

@Override
public void preUpdate()
{
if (requestAltEnter)
{
if (buf != null)
{
buf.dispose();
buf = null;
}
while (!requestAllowed)
{
try
{
Thread.sleep(Constant.DECADE);
}
catch (@SuppressWarnings("unused") final InterruptedException exception)
{
Thread.currentThread().interrupt();
}
}
frame.removeNotify();
if (window != null)
{
window.dispose();
}
canvas = null;
try
{
Thread.sleep(Constant.HUNDRED);
}
catch (@SuppressWarnings("unused") final InterruptedException exception)
{
Thread.currentThread().interrupt();
}
frame.addNotify();
start();
requestAltEnter = false;
}
}
}
Loading

0 comments on commit 533613c

Please sign in to comment.