Skip to content

Commit

Permalink
Merge pull request #72 from litwak913/platform-interface
Browse files Browse the repository at this point in the history
Abstract `HWndCtrl` for future cross-platform support
  • Loading branch information
isHarryh authored Aug 20, 2024
2 parents 6f07afe + 7f46737 commit 1f2658f
Show file tree
Hide file tree
Showing 7 changed files with 575 additions and 161 deletions.
72 changes: 39 additions & 33 deletions core/src/cn/harryh/arkpets/ArkPets.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import cn.harryh.arkpets.transitions.TransitionFloat;
import cn.harryh.arkpets.transitions.TransitionVector2;
import cn.harryh.arkpets.tray.MemberTrayImpl;
import cn.harryh.arkpets.utils.HWndCtrl;
import cn.harryh.arkpets.hwnd.HWndCtrl;
import cn.harryh.arkpets.hwnd.HWndCtrlFactory;
import cn.harryh.arkpets.utils.Logger;
import cn.harryh.arkpets.utils.Plane;
import com.badlogic.gdx.ApplicationAdapter;
Expand All @@ -36,10 +37,10 @@ public class ArkPets extends ApplicationAdapter implements InputProcessor {
public TransitionFloat windowAlpha; // Window Opacity Easing
public TransitionVector2 windowPosition; // Window Position Easing

private HWndCtrl hWndMine;
private HWndCtrl hWndTopmost;
private HWndCtrl<?> hWndMine;
private HWndCtrl<?> hWndTopmost;
private LoopCtrl getHWndLoopCtrl;
private List<HWndCtrl> hWndList;
private List<? extends HWndCtrl<?>> hWndList;

private final String APP_TITLE;
private final MouseStatus mouseStatus = new MouseStatus();
Expand Down Expand Up @@ -95,8 +96,12 @@ public void create() {
// 5.Window style setup
windowAlpha = new TransitionFloat(TernaryFunction.EASE_OUT_CUBIC, easingDuration);
windowAlpha.reset(1f);
hWndMine = new HWndCtrl(null, APP_TITLE);
hWndMine.setWindowExStyle(HWndCtrl.WS_EX_LAYERED | (config.window_style_topmost ? HWndCtrl.WS_EX_TOPMOST : 0));
hWndMine = HWndCtrlFactory.find(null, APP_TITLE);
hWndMine.setLayered(true);
if(config.window_style_topmost){
hWndMine.setTopMost(true);
}
//hWndMine.setWindowExStyle(HWndCtrl.WS_EX_LAYERED | (config.window_style_topmost ? HWndCtrl.WS_EX_TOPMOST : 0));
promiseToolwindowStyle(1000);

// 6.Tray icon setup
Expand Down Expand Up @@ -155,6 +160,7 @@ public void resize(int x, int y) {
@Override
public void dispose() {
Logger.info("App", "Dispose");
HWndCtrlFactory.free();
}

/* INTERFACES */
Expand Down Expand Up @@ -194,10 +200,10 @@ public boolean touchDown(int screenX, int screenY, int pointer, int button) {
RelativeWindowPosition rwp = getRelativeWindowPositionAt(screenX, screenY);
if (rwp != null)
rwp.sendMouseEvent(switch (button) {
case Input.Buttons.LEFT -> HWndCtrl.WM_LBUTTONDOWN;
case Input.Buttons.RIGHT -> HWndCtrl.WM_RBUTTONDOWN;
case Input.Buttons.MIDDLE -> HWndCtrl.WM_MBUTTONDOWN;
default -> 0;
case Input.Buttons.LEFT -> HWndCtrl.MouseEvent.LBUTTONDOWN;
case Input.Buttons.RIGHT -> HWndCtrl.MouseEvent.RBUTTONDOWN;
case Input.Buttons.MIDDLE -> HWndCtrl.MouseEvent.MBUTTONDOWN;
default -> HWndCtrl.MouseEvent.EMPTY;
});
} else {
if (button == Input.Buttons.LEFT) {
Expand Down Expand Up @@ -253,10 +259,10 @@ public boolean touchUp(int screenX, int screenY, int pointer, int button) {
RelativeWindowPosition rwp = getRelativeWindowPositionAt(screenX, screenY);
if (rwp != null)
rwp.sendMouseEvent(switch (button) {
case Input.Buttons.LEFT -> HWndCtrl.WM_LBUTTONUP;
case Input.Buttons.RIGHT -> HWndCtrl.WM_RBUTTONUP;
case Input.Buttons.MIDDLE -> HWndCtrl.WM_MBUTTONUP;
default -> 0;
case Input.Buttons.LEFT -> HWndCtrl.MouseEvent.LBUTTONUP;
case Input.Buttons.RIGHT -> HWndCtrl.MouseEvent.RBUTTONUP;
case Input.Buttons.MIDDLE -> HWndCtrl.MouseEvent.MBUTTONUP;
default -> HWndCtrl.MouseEvent.EMPTY;
});
} else if (button == Input.Buttons.LEFT) {
// Left Click: Play the specified animation
Expand Down Expand Up @@ -292,7 +298,7 @@ public boolean mouseMoved(int screenX, int screenY) {
// Transfer mouse event
RelativeWindowPosition rwp = getRelativeWindowPositionAt(screenX, screenY);
if (rwp != null)
rwp.sendMouseEvent(HWndCtrl.WM_MOUSEMOVE);
rwp.sendMouseEvent(HWndCtrl.MouseEvent.MOUSEMOVE);
}
return false;
}
Expand All @@ -312,19 +318,19 @@ private void setWindowPos() {
if (hWndMine == null) return;
if (getHWndLoopCtrl.isExecutable(Gdx.graphics.getDeltaTime())) {
refreshMonitorInfo();
HWndCtrl new_hwnd_topmost = refreshWindowIndex();
HWndCtrl<?> new_hwnd_topmost = refreshWindowIndex();
hWndTopmost = new_hwnd_topmost != hWndTopmost ? new_hwnd_topmost : hWndTopmost;
hWndMine.setWindowTransparent(isAlwaysTransparent);
}
hWndMine.setWindowPosition(hWndTopmost, (int)windowPosition.now().x, (int)windowPosition.now().y, width, height);
hWndMine.setWindowPosition((HWndCtrl)hWndTopmost, (int)windowPosition.now().x, (int)windowPosition.now().y, width, height);
}

private RelativeWindowPosition getRelativeWindowPositionAt(int x, int y) {
if (hWndList == null)
return null;
int absX = x + (int)(windowPosition.now().x);
int absY = y + (int)(windowPosition.now().y);
for (HWndCtrl hWndCtrl : hWndList) {
for (HWndCtrl<?> hWndCtrl : hWndList) {
if (coreTitleManager.getNumber(hWndCtrl) < 0)
if (hWndCtrl.posLeft <= absX && hWndCtrl.posRight > absX)
if (hWndCtrl.posTop <= absY && hWndCtrl.posBottom > absY) {
Expand All @@ -336,10 +342,10 @@ private RelativeWindowPosition getRelativeWindowPositionAt(int x, int y) {
return null;
}

private HWndCtrl refreshWindowIndex() {
hWndList = HWndCtrl.getWindowList(true);
HWndCtrl minWindow = null;
HashMap<Integer, HWndCtrl> line = new HashMap<>();
private HWndCtrl<?> refreshWindowIndex() {
hWndList = HWndCtrlFactory.getWindowList(true);
HWndCtrl<?> minWindow = null;
HashMap<Integer, HWndCtrl<?>> line = new HashMap<>();
int myPos = (int)(windowPosition.now().x + width / 2);
int minNum = 2048;
int myNum = coreTitleManager.getNumber(APP_TITLE);
Expand All @@ -349,7 +355,7 @@ private HWndCtrl refreshWindowIndex() {
plane.barriers.clear();
plane.pointCharges.clear();
}
for (HWndCtrl hWndCtrl : hWndList) {
for (HWndCtrl<?> hWndCtrl : hWndList) {
int wndNum = coreTitleManager.getNumber(hWndCtrl);
// Distinguish non-peer windows from peers.
if (wndNum == -1) {
Expand All @@ -360,7 +366,7 @@ private HWndCtrl refreshWindowIndex() {
for (int h = -hWndCtrl.posTop; h > -hWndCtrl.posBottom; h--) {
// Mark the window's y-position in the vertical line.
if (!line.containsKey(h))
line.put(h, (h == -hWndCtrl.posTop) ? hWndCtrl : HWndCtrl.EMPTY); // Record this window.
line.put(h, (h == -hWndCtrl.posTop) ? hWndCtrl : HWndCtrlFactory.EMPTY); // Record this window.
}
}
}
Expand All @@ -379,19 +385,19 @@ private HWndCtrl refreshWindowIndex() {
}
if (minWindow == null || minWindow.isEmpty()) {
// Set as the top window if there is no peer.
minWindow = new HWndCtrl(-1);
minWindow = HWndCtrlFactory.getTopMost();
}
if (plane != null) {
// Set barriers according to the vertical line.
for (int h = (int)plane.borderTop(); h > plane.borderBottom(); h--) {
if (line.containsKey(h)) {
HWndCtrl temp = line.get(h);
HWndCtrl<?> temp = line.get(h);
if (temp != null && temp.hWnd != null)
plane.setBarrier(-temp.posTop, temp.posLeft, temp.windowWidth, false);
}
}
}
return config.window_style_topmost ? minWindow : HWndCtrl.EMPTY; // Return the last peer window.
return config.window_style_topmost ? minWindow : HWndCtrlFactory.EMPTY; // Return the last peer window.
}

private ArkConfig.Monitor refreshMonitorInfo() {
Expand Down Expand Up @@ -419,7 +425,8 @@ private void promiseToolwindowStyle(int maxRetries) {
// Make sure ArkPets has been set as foreground window once
for (int i = 0; ; i++) {
if (hWndMine.isForeground()) {
hWndMine.setWindowExStyle(hWndMine.getWindowExStyle() | HWndCtrl.WS_EX_TOOLWINDOW);
hWndMine.setToolWindow(true);
//hWndMine.setWindowExStyle(hWndMine.getWindowExStyle() | HWndCtrl.WS_EX_TOOLWINDOW);
Logger.info("Window", "SetForegroundWindow succeeded");
isToolwindowStyle = true;
break;
Expand Down Expand Up @@ -507,12 +514,11 @@ public void updatePosition(int newX, int newY, int button) {
}


private record RelativeWindowPosition(HWndCtrl hWndCtrl, int relX, int relY) {
public void sendMouseEvent(int msg) {
if (msg == 0)
return;
private record RelativeWindowPosition(HWndCtrl<?> hWndCtrl, int relX, int relY) {
public void sendMouseEvent(HWndCtrl.MouseEvent msg) {
if(msg == HWndCtrl.MouseEvent.EMPTY) return;
//Logger.debug("Input", "Transfer mouse event " + msg + " to `" + hWndCtrl.windowText + "` @ " + relX + ", " + relY);
hWndCtrl.updated().sendMouseEvent(msg, relX, relY);
HWndCtrlFactory.update(hWndCtrl).sendMouseEvent(msg, relX, relY);
}
}
}
2 changes: 1 addition & 1 deletion core/src/cn/harryh/arkpets/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
package cn.harryh.arkpets;

import cn.harryh.arkpets.utils.HWndCtrl.NumberedTitleManager;
import cn.harryh.arkpets.hwnd.HWndCtrl.NumberedTitleManager;
import cn.harryh.arkpets.utils.Logger;
import cn.harryh.arkpets.utils.Version;
import javafx.util.Duration;
Expand Down
Loading

0 comments on commit 1f2658f

Please sign in to comment.