Skip to content

Commit

Permalink
Use the new setting in the screenshot captor
Browse files Browse the repository at this point in the history
  • Loading branch information
EtienneLamoureux committed Dec 12, 2024
1 parent 78e1eca commit c032aeb
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
11 changes: 3 additions & 8 deletions src/main/java/tools/sctrade/companion/gui/SettingsTab.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package tools.sctrade.companion.gui;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JLabel;
Expand All @@ -19,6 +15,7 @@
import tools.sctrade.companion.domain.setting.Setting;
import tools.sctrade.companion.domain.setting.SettingRepository;
import tools.sctrade.companion.domain.user.UserService;
import tools.sctrade.companion.utils.GraphicsDeviceUtil;
import tools.sctrade.companion.utils.IncrementingInt;
import tools.sctrade.companion.utils.LocalizationUtil;

Expand Down Expand Up @@ -115,11 +112,9 @@ private void updateStarCitizenLivePath() {
}

private void buildStarCitizenMonitorComboBox(SettingRepository settings) {
GraphicsDevice[] monitors =
GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
List<String> monitorIds = Arrays.stream(monitors).map(n -> n.getIDstring()).toList();
var monitorIds = GraphicsDeviceUtil.getIds();
String selectedMonitorId =
settings.get(Setting.STAR_CITIZEN_MONITOR, monitors[0].getIDstring());
settings.get(Setting.STAR_CITIZEN_MONITOR, GraphicsDeviceUtil.getPrimaryId());

var starCitizenMonitorLabel =
buildLabel(rowIndex.get(), LocalizationUtil.get("labelStarCitizenMonitor"));
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/tools/sctrade/companion/input/ScreenPrinter.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package tools.sctrade.companion.input;

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -13,7 +11,10 @@
import tools.sctrade.companion.domain.image.ImageType;
import tools.sctrade.companion.domain.image.ImageWriter;
import tools.sctrade.companion.domain.notification.NotificationService;
import tools.sctrade.companion.domain.setting.Setting;
import tools.sctrade.companion.domain.setting.SettingRepository;
import tools.sctrade.companion.utils.AsynchronousProcessor;
import tools.sctrade.companion.utils.GraphicsDeviceUtil;
import tools.sctrade.companion.utils.LocalizationUtil;
import tools.sctrade.companion.utils.SoundUtil;

Expand All @@ -27,29 +28,35 @@ public class ScreenPrinter implements Runnable {
private ImageWriter imageWriter;
private SoundUtil soundPlayer;
private NotificationService notificationService;
private SettingRepository settings;

public ScreenPrinter(Collection<AsynchronousProcessor<BufferedImage>> imageProcessors,
ImageWriter imageWriter, SoundUtil soundPlayer, NotificationService notificationService) {
this(imageProcessors, Collections.emptyList(), imageWriter, soundPlayer, notificationService);
ImageWriter imageWriter, SoundUtil soundPlayer, NotificationService notificationService,
SettingRepository settings) {
this(imageProcessors, Collections.emptyList(), imageWriter, soundPlayer, notificationService,
settings);
}

public ScreenPrinter(Collection<AsynchronousProcessor<BufferedImage>> imageProcessors,
List<ImageManipulation> postprocessingManipulations, ImageWriter imageWriter,
SoundUtil soundPlayer, NotificationService notificationService) {
SoundUtil soundPlayer, NotificationService notificationService, SettingRepository settings) {
this.imageProcessors = imageProcessors;
this.postprocessingManipulations = postprocessingManipulations;
this.imageWriter = imageWriter;
this.soundPlayer = soundPlayer;
this.notificationService = notificationService;
this.settings = settings;
}

@Override
public void run() {
try {
logger.debug("Printing screen...");
soundPlayer.play(CAMERA_SHUTTER);
var screenRectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
var screenCapture = postProcess(new Robot().createScreenCapture(screenRectangle));
var monitor = GraphicsDeviceUtil
.get(settings.get(Setting.STAR_CITIZEN_MONITOR, GraphicsDeviceUtil.getPrimaryId()));
var screenRectangle = monitor.getDefaultConfiguration().getBounds();
var screenCapture = postProcess(new Robot(monitor).createScreenCapture(screenRectangle));
logger.debug("Printed screen");

logger.debug("Calling image processors...");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/tools/sctrade/companion/spring/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,12 @@ public SoundUtil buildSoundUtil() {
@Bean("ScreenPrinter")
public ScreenPrinter buildScreenPrinter(
@Qualifier("CommodityService") CommodityService commodityService, ImageWriter imageWriter,
SoundUtil soundPlayer, NotificationService notificationService) {
SoundUtil soundPlayer, NotificationService notificationService, SettingRepository settings) {
List<ImageManipulation> postprocessingManipulations = new ArrayList<>();
postprocessingManipulations.add(new UpscaleTo4k());

return new ScreenPrinter(Arrays.asList(commodityService), postprocessingManipulations,
imageWriter, soundPlayer, notificationService);
imageWriter, soundPlayer, notificationService, settings);
}

@Bean("KeyListener")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package tools.sctrade.companion.utils;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

public class GraphicsDeviceUtil {
public static GraphicsDevice get(String id) {
return getAllById().get(id);
}

public static String getPrimaryId() {
return getPrimary().getIDstring();
}

public static GraphicsDevice getPrimary() {
return getAll().iterator().next();
}

public static Collection<String> getIds() {
return getAll().stream().map(n -> n.getIDstring()).toList();
}

public static Map<String, GraphicsDevice> getAllById() {
return getAll().stream().collect(Collectors.toMap(n -> n.getIDstring(), n -> n));
}

public static Collection<GraphicsDevice> getAll() {
return Arrays.asList(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices());
}

private GraphicsDeviceUtil() {
// Intentionally empty
}
}

0 comments on commit c032aeb

Please sign in to comment.