Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions PlayTools/Controls/ActionDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ public class ActionDispatcher {
return mapped
}

static public func dispatchClick(key: String) -> Bool {
guard buttonHandlers[key] != nil else {
return false
}
_ = dispatch(key: key, pressed: true)
// 延迟 30ms 自动释放,模拟一次物理点击
PlayInput.touchQueue.asyncAfter(deadline: .now() + 0.03, qos: .userInteractive, execute: {
_ = dispatch(key: key, pressed: false)
})
return true
}

static public func dispatch(key: String, valueX: CGFloat, valueY: CGFloat) -> Bool {
for priority in 0..<priorityCount {
if let handler = directionPadHandlers[priority].first(where: { handler in
Expand Down
3 changes: 2 additions & 1 deletion PlayTools/Controls/Frontend/ControlMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public class ControlMode: Equatable {

setupGameController()
setupKeyboard()
if PlaySettings.shared.enableScrollWheel {
// Initialize scroll wheel if either zoom or keymapping is enabled
if PlaySettings.shared.enableScrollWheelZoom || PlaySettings.shared.enableScrollWheelMapping {
setupScrollWheel()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,28 @@

public class CameraControlMouseEventAdapter: MouseEventAdapter {
public func handleScrollWheel(deltaX: CGFloat, deltaY: CGFloat) -> Bool {
_ = ActionDispatcher.dispatch(key: KeyCodeNames.scrollWheelScale, valueX: deltaX, valueY: deltaY)
// I dont know why but this is the logic before the refactor.
// Might be a mistake but keeping it for now
// Priority 1: Keymapping. If enabled and triggered, consume the event.
if PlaySettings.shared.enableScrollWheelMapping {
let threshold: CGFloat = 0.5
var handled = false
if deltaY > threshold {
handled = ActionDispatcher.dispatchClick(key: "ScrU")
} else if deltaY < -threshold {
handled = ActionDispatcher.dispatchClick(key: "ScrD")
}

Check failure on line 23 in PlayTools/Controls/Frontend/EventAdapter/Mouse/Instances/CameraControlMouseEventAdapter.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
// If mapping was triggered, return true to consume the event
if handled {
return true
}
}

// Priority 2: Zoom/Scale logic.
if PlaySettings.shared.enableScrollWheelZoom {
_ = ActionDispatcher.dispatch(key: KeyCodeNames.scrollWheelScale, valueX: deltaX, valueY: deltaY)
return true
}

Check failure on line 35 in PlayTools/Controls/Frontend/EventAdapter/Mouse/Instances/CameraControlMouseEventAdapter.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
return true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,21 @@ public class EditorMouseEventAdapter: MouseEventAdapter {
return true
}

private static var lastScrollTime: TimeInterval = 0

public func handleScrollWheel(deltaX: CGFloat, deltaY: CGFloat) -> Bool {
false
let currentTime = ProcessInfo.processInfo.systemUptime
// 阈值判断:deltaY 绝对值需大于 0.2 以防极小干扰,且 0.3s 内不重复触发
if abs(deltaY) > 0.2 && (currentTime - EditorMouseEventAdapter.lastScrollTime) > 0.3 {
EditorMouseEventAdapter.lastScrollTime = currentTime
let keyCode = deltaY > 0 ? -100 : -101
DispatchQueue.main.async(qos: .userInteractive, execute: {
EditorController.shared.setKey(keyCode)
Toucher.writeLog(logMessage: "mouse wheel editor set: \(keyCode)")
})
return true
}
return false
}

public func handleMove(deltaX: CGFloat, deltaY: CGFloat) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,30 @@
}

public func handleScrollWheel(deltaX: CGFloat, deltaY: CGFloat) -> Bool {
_ = ActionDispatcher.dispatch(key: KeyCodeNames.scrollWheelDrag, valueX: deltaX, valueY: deltaY)
// I dont know why but this is the logic before the refactor.
// Might be a mistake but keeping it for now
// Priority 1: Keymapping. If enabled and triggered, consume the event.
if PlaySettings.shared.enableScrollWheelMapping {
let threshold: CGFloat = 0.5
var handled = false
if deltaY > threshold {
handled = ActionDispatcher.dispatchClick(key: "ScrU")
} else if deltaY < -threshold {
handled = ActionDispatcher.dispatchClick(key: "ScrD")
}

Check failure on line 62 in PlayTools/Controls/Frontend/EventAdapter/Mouse/Instances/TouchscreenMouseEventAdapter.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
// If mapping was triggered, return true to consume the event
// and prevent it from reaching any other logic (like zoom).
if handled {
return true
}
}

// Priority 2: Zoom logic.
if PlaySettings.shared.enableScrollWheelZoom {
_ = ActionDispatcher.dispatch(key: KeyCodeNames.scrollWheelDrag, valueX: deltaX, valueY: deltaY)
// Generally return true after processing to keep behavior consistent
return true
}

Check failure on line 76 in PlayTools/Controls/Frontend/EventAdapter/Mouse/Instances/TouchscreenMouseEventAdapter.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
return false
}

Expand Down
2 changes: 2 additions & 0 deletions PlayTools/Keymap/KeyCodeNames.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class KeyCodeNames {
-1: "LMB",
-2: "RMB",
-3: "MMB",
-100: "ScrU",
-101: "ScrD",
41: "Esc",
44: "Spc",
225: "Lshft",
Expand Down
6 changes: 4 additions & 2 deletions PlayTools/PlaySettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ let settings = PlaySettings.shared

@objc lazy var noKMOnInput = settingsData.noKMOnInput

@objc lazy var enableScrollWheel = settingsData.enableScrollWheel
@objc lazy var enableScrollWheelZoom = settingsData.enableScrollWheelZoom
@objc lazy var enableScrollWheelMapping = settingsData.enableScrollWheelMapping

@objc lazy var hideTitleBar = settingsData.hideTitleBar

Expand Down Expand Up @@ -118,7 +119,8 @@ struct AppSettingsData: Codable {
var windowFixMethod = 0
var rootWorkDir = true
var noKMOnInput = false
var enableScrollWheel = true
var enableScrollWheelZoom = true // Original zoom logic
var enableScrollWheelMapping = false // New keymapping logic
var hideTitleBar = false
var floatingWindow = false
var checkMicPermissionSync = false
Expand Down
Loading