Skip to content

Commit

Permalink
Merge pull request #59 from half-nothing/v3.x
Browse files Browse the repository at this point in the history
System tray icon function enhanced
  • Loading branch information
isHarryh authored Jan 31, 2024
2 parents 7a64f88 + 0574a59 commit 0567d38
Show file tree
Hide file tree
Showing 21 changed files with 1,003 additions and 123 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ test*/
# Exclude runtime files
/ArkPetsConfig.json
/models_data.json
/desktop/src/main/resource/assets/models/
/desktop/src/main/resource/assets/models_data.json
4 changes: 3 additions & 1 deletion assets/ArkPetsConfigDefault.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
"physic_gravity_acc":800.0,
"physic_speed_limit_x":1000.0,
"physic_speed_limit_y":1000.0,
"physic_static_friction_acc":500.0
"physic_static_friction_acc":500.0,
"server_port": 8080,
"separate_arkpet_from_launcher": false
}
2 changes: 2 additions & 0 deletions core/src/cn/harryh/arkpets/ArkConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class ArkConfig {
public float physic_static_friction_acc;
public float physic_speed_limit_x;
public float physic_speed_limit_y;
public int server_port = 8080;
public boolean separate_arkpet_from_launcher;

private ArkConfig() {
}
Expand Down
21 changes: 15 additions & 6 deletions core/src/cn/harryh/arkpets/ArkPets.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
*/
package cn.harryh.arkpets;

import cn.harryh.arkpets.animations.*;
import cn.harryh.arkpets.animations.AnimData;
import cn.harryh.arkpets.animations.GeneralBehavior;
import cn.harryh.arkpets.assets.AssetItem;
import cn.harryh.arkpets.utils.*;
import cn.harryh.arkpets.transitions.*;
import cn.harryh.arkpets.socket.SocketClient;
import cn.harryh.arkpets.transitions.TernaryFunction;
import cn.harryh.arkpets.transitions.TransitionFloat;
import cn.harryh.arkpets.transitions.TransitionVector2;
import cn.harryh.arkpets.utils.HWndCtrl;
import cn.harryh.arkpets.utils.Logger;
import cn.harryh.arkpets.utils.Plane;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.math.Vector2;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;
import java.util.UUID;

import static cn.harryh.arkpets.Const.*;

Expand Down Expand Up @@ -79,7 +88,7 @@ public void create() {
ArkConfig.Monitor primaryMonitor = ArkConfig.Monitor.getMonitors()[0];
initWindow((int)(primaryMonitor.size[0] * 0.1f), (int)(primaryMonitor.size[0] * 0.1f));
// 5.Tray icon setup
tray = new ArkTray(this);
tray = new ArkTray(this, new SocketClient(config.server_port), UUID.randomUUID());
// Setup complete
Logger.info("App", "Render");
}
Expand Down Expand Up @@ -133,7 +142,7 @@ public void resize(int x, int y) {
@Override
public void dispose() {
Logger.info("App", "Dispose");
tray.remove();
tray.removeTray();
}

public boolean canChangeStage() {
Expand Down
203 changes: 101 additions & 102 deletions core/src/cn/harryh/arkpets/ArkTray.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,31 @@
package cn.harryh.arkpets;

import cn.harryh.arkpets.animations.AnimData;
import cn.harryh.arkpets.socket.SocketClient;
import cn.harryh.arkpets.socket.SocketData;
import cn.harryh.arkpets.tray.Tray;
import cn.harryh.arkpets.utils.Logger;
import com.badlogic.gdx.Gdx;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.UUID;

import static cn.harryh.arkpets.Const.*;
import static cn.harryh.arkpets.Const.fontFileRegular;
import static cn.harryh.arkpets.Const.linearEasingDuration;


public class ArkTray {
public class ArkTray extends Tray {
private final ArkPets arkPets;
private final SystemTray tray;
private final TrayIcon icon;
private final JDialog popWindow;
private final JPopupMenu popMenu;
private boolean isTrayIconApplied;
public String name;
public String title;
private final SocketClient socketClient;
public AnimData keepAnim;
public static Font font;
private final JDialog popWindow;
private final JPopupMenu popMenu;

static {
try {
Expand All @@ -49,18 +48,10 @@ public class ArkTray {
* Must be used after Gdx.app was initialized.
* @param boundArkPets The ArkPets instance that bound to the tray icon.
*/
public ArkTray(ArkPets boundArkPets) {
public ArkTray(ArkPets boundArkPets, SocketClient socket, UUID uuid) {
super(uuid);
arkPets = boundArkPets;
tray = SystemTray.getSystemTray();
name = (arkPets.config.character_label == null || arkPets.config.character_label.isEmpty()) ? "Unknown" : arkPets.config.character_label;
title = name + " - " + appName;

// Load the tray icon image.
Image image = Toolkit.getDefaultToolkit().createImage(getClass().getResource(iconFilePng));
icon = new TrayIcon(image, name);
icon.setImageAutoSize(true);

// This Dialog is the container (the "anchor") of the PopupMenu:
socketClient = socket;
popWindow = new JDialog();
popWindow.setUndecorated(true);
popWindow.setSize(1, 1);
Expand All @@ -72,101 +63,109 @@ public void firePopupMenuWillBecomeInvisible() {
popWindow.setVisible(false); // Hide the container when the menu is invisible.
}
};
name = (arkPets.config.character_label == null || arkPets.config.character_label.isEmpty()) ? "Unknown" : arkPets.config.character_label;
socketClient.connect(socketData -> {
if (socketData == null || socketData.uuid.compareTo(uuid) != 0)
return;
switch (socketData.operateType) {
case LOGOUT -> optExitHandler();
case KEEP_ACTION -> optKeepAnimEnHandler();
case NO_KEEP_ACTION -> optKeepAnimDisHandler();
case TRANSPARENT_MODE -> optTransparentEnHandler();
case NO_TRANSPARENT_MODE -> optTransparentDisHandler();
case CHANGE_STAGE -> optChangeStageHandler();
}
});
socketClient.sendRequest(new SocketData(this.uuid, SocketData.OperateType.LOGIN, name, arkPets.canChangeStage()));
addComponent();
}


@Override
protected void addComponent() {
JLabel innerLabel = new JLabel(" " + name + " ");
innerLabel.setAlignmentX(0.5f);
popMenu.add(innerLabel);

// Menu options:
JMenuItem optKeepAnimEn = new JMenuItem("保持动作");
JMenuItem optKeepAnimDis = new JMenuItem("取消保持");
JMenuItem optTransparentEn = new JMenuItem("透明模式");
JMenuItem optTransparentDis = new JMenuItem("取消透明");
JMenuItem optChangeStage = new JMenuItem("切换形态");
JMenuItem optExit = new JMenuItem("退出");
optKeepAnimEn.addActionListener(e -> {
Logger.info("Tray", "Keep-Anim enabled");
keepAnim = arkPets.cha.getPlaying();
popMenu.remove(optKeepAnimEn);
popMenu.add(optKeepAnimDis, 1);
});
optKeepAnimDis.addActionListener(e -> {
Logger.info("Tray","Keep-Anim disabled");
keepAnim = null;
popMenu.remove(optKeepAnimDis);
popMenu.add(optKeepAnimEn, 1);
});
optTransparentEn.addActionListener(e -> {
Logger.info("Tray", "Transparent enabled");
arkPets.windowAlpha.reset(0.75f);
arkPets.hWndMine.setWindowTransparent(true);
popMenu.remove(optTransparentEn);
popMenu.add(optTransparentDis, 2);
});
optTransparentDis.addActionListener(e -> {
Logger.info("Tray", "Transparent disabled");
arkPets.windowAlpha.reset(1f);
arkPets.hWndMine.setWindowTransparent(false);
popMenu.remove(optTransparentDis);
popMenu.add(optTransparentEn, 2);
});
optKeepAnimEn.addActionListener(e -> socketClient.sendRequest(new SocketData(uuid, SocketData.OperateType.KEEP_ACTION)));
optKeepAnimDis.addActionListener(e -> socketClient.sendRequest(new SocketData(uuid, SocketData.OperateType.NO_KEEP_ACTION)));
optTransparentEn.addActionListener(e -> socketClient.sendRequest(new SocketData(uuid, SocketData.OperateType.TRANSPARENT_MODE)));
optTransparentDis.addActionListener(e -> socketClient.sendRequest(new SocketData(uuid, SocketData.OperateType.NO_TRANSPARENT_MODE)));
optChangeStage.addActionListener(e -> {
Logger.info("Tray","Request to change stage");
arkPets.changeStage();
if (keepAnim != null) {
keepAnim = null;
popMenu.remove(optKeepAnimDis);
popMenu.add(optKeepAnimEn, 1);
}
});
optExit.addActionListener(e -> {
Logger.info("Tray","Request to exit");
arkPets.windowAlpha.reset(0f);
remove();
try {
Thread.sleep((long)(linearEasingDuration * 1000));
Gdx.app.exit();
} catch (InterruptedException ignored) {
}
if (keepAnim != null)
socketClient.sendRequest(new SocketData(uuid, SocketData.OperateType.CHANGE_STAGE));
});
optExit.addActionListener(e -> socketClient.sendRequest(new SocketData(uuid, SocketData.OperateType.LOGOUT)));

popMenu.add(optKeepAnimEn);
popMenu.add(optTransparentEn);
if (arkPets.canChangeStage()) popMenu.add(optChangeStage);
popMenu.add(optExit);
popMenu.setSize(100, 24 * popMenu.getSubElements().length);
}

// Mouse event listener:
icon.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if (e.getButton() == 3 && e.isPopupTrigger()) {
// After right-click on the tray icon.
int x = e.getX();
int y = e.getY();
showDialog(x + 5, y);
}
}
});

// Add the icon to the system tray.
@Override
protected void optExitHandler() {
Logger.info("Tray", "Request to exit");
arkPets.windowAlpha.reset(0f);
removeTray();
try {
tray.add(icon);
isTrayIconApplied = true;
Logger.info("Tray", "Tray icon applied, titled \"" + title + "\"");
} catch (AWTException e) {
isTrayIconApplied = false;
Logger.error("Tray", "Unable to apply tray icon, details see below", e);
Thread.sleep((long) (linearEasingDuration * 1000));
Gdx.app.exit();
} catch (InterruptedException ignored) {
}
}

/** Removes the icon from system tray.
*/
public void remove() {
if (isTrayIconApplied) {
tray.remove(icon);
popMenu.removeAll();
popWindow.dispose();
@Override
protected void optChangeStageHandler() {
Logger.info("Tray", "Request to change stage");
arkPets.changeStage();
if (keepAnim != null) {
keepAnim = null;
popMenu.remove(optKeepAnimDis);
popMenu.add(optKeepAnimEn, 1);
}
isTrayIconApplied = false;
}

@Override
protected void optTransparentDisHandler() {
Logger.info("Tray", "Transparent disabled");
arkPets.windowAlpha.reset(1f);
arkPets.hWndMine.setWindowTransparent(false);
popMenu.remove(optTransparentDis);
popMenu.add(optTransparentEn, 2);
}

@Override
protected void optTransparentEnHandler() {
Logger.info("Tray", "Transparent enabled");
arkPets.windowAlpha.reset(0.75f);
arkPets.hWndMine.setWindowTransparent(true);
popMenu.remove(optTransparentEn);
popMenu.add(optTransparentDis, 2);
}

@Override
protected void optKeepAnimDisHandler() {
Logger.info("Tray", "Keep-Anim disabled");
keepAnim = null;
popMenu.remove(optKeepAnimDis);
popMenu.add(optKeepAnimEn, 1);
}

@Override
protected void optKeepAnimEnHandler() {
Logger.info("Tray", "Keep-Anim enabled");
keepAnim = arkPets.cha.getPlaying();
popMenu.remove(optKeepAnimEn);
popMenu.add(optKeepAnimDis, 1);
}

@Override
public void removeTray() {
popMenu.removeAll();
popWindow.dispose();
socketClient.disconnect();
}

/** Hides the menu.
Expand All @@ -184,8 +183,8 @@ public void showDialog(int x, int y) {
/* Use `System.setProperty("sun.java2d.uiScale", "1")` can also avoid system scaling.
Here we will adapt the coordinate for system scaling artificially. See below. */
AffineTransform at = popWindow.getGraphicsConfiguration().getDefaultTransform();
int scaledX = (int)(x / at.getScaleX());
int scaledY = (int)(y / at.getScaleY());
int scaledX = (int) (x / at.getScaleX());
int scaledY = (int) (y / at.getScaleY());

// Show the JDialog together with the JPopupMenu.
popWindow.setVisible(true);
Expand Down
28 changes: 28 additions & 0 deletions core/src/cn/harryh/arkpets/process_pool/ProcessHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cn.harryh.arkpets.process_pool;

public class ProcessHolder {
private final Long processID;
private final Process process;

public Long getProcessID() {
return processID;
}

public Process getProcess() {
return process;
}

public static ProcessHolder holder(Process process) {
return new ProcessHolder(process);
}

private ProcessHolder(Process process) {
this.process = process;
this.processID = process.pid();
}

@Override
public String toString() {
return "ProcessHolder [processID=" + processID + "]";
}
}
Loading

0 comments on commit 0567d38

Please sign in to comment.