Skip to content

Commit

Permalink
Merge pull request #24 from XuYicong/feat/draggable-button
Browse files Browse the repository at this point in the history
Feat: button-controlled mouse drag action
  • Loading branch information
JoseMoreville authored Aug 30, 2022
2 parents 5a28d3e + 80becf0 commit d18bf21
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
35 changes: 35 additions & 0 deletions PlayTools/Controls/PlayAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,41 @@ class ButtonAction: Action {
}
}

class DraggableButtonAction: ButtonAction {
static public var activeButton: DraggableButtonAction?

var releasePoint: CGPoint

override init(id: Int, keyid: Int, key: GCKeyCode, point: CGPoint) {
self.releasePoint = point
super.init(id: id, keyid: keyid, key: key, point: point)
PlayMice.shared.setupMouseMovedHandler()
}

override func update(pressed: Bool) {
if pressed {
Toucher.touchcam(point: point, phase: UITouch.Phase.began, tid: id)
self.releasePoint = point
DraggableButtonAction.activeButton = self
} else {
DraggableButtonAction.activeButton = nil
Toucher.touchcam(point: releasePoint, phase: UITouch.Phase.ended, tid: id)
}
}

override func invalidate() {
DraggableButtonAction.activeButton = nil
PlayMice.shared.stop()
super.invalidate()
}

func onMouseMoved(deltaX: CGFloat, deltaY: CGFloat) {
self.releasePoint.x += deltaX * CGFloat(PlaySettings.shared.sensivity)
self.releasePoint.y -= deltaY * CGFloat(PlaySettings.shared.sensivity)
Toucher.touchcam(point: self.releasePoint, phase: UITouch.Phase.moved, tid: id)
}
}

class JoystickAction: Action {
let keys: [GCKeyCode]
let center: CGPoint
Expand Down
6 changes: 6 additions & 0 deletions PlayTools/Controls/PlayInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ final class PlayInput: NSObject {
key: GCKeyCode.init(rawValue: CFIndex(key[0])),
point: CGPoint(x: key[1].absoluteX,
y: key[2].absoluteY)))
} else if key.count == 5 {
actions.append(DraggableButtonAction(id: counter,
keyid: Int(key[0]),
key: GCKeyCode.init(rawValue: CFIndex(key[0])),
point: CGPoint(x: key[1].absoluteX,
y: key[2].absoluteY)))
} else if key.count == 8 {
actions.append(JoystickAction(id: counter,
keys: [GCKeyCode.init(rawValue: CFIndex(key[0])),
Expand Down
10 changes: 9 additions & 1 deletion PlayTools/Controls/PlayMice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,19 @@ typealias ResponseBlockBool = @convention(block) (_ event: Any) -> Bool

public func setup(_ key: [CGFloat]) {
camera = CameraControl(centerX: key[0].absoluteX, centerY: key[1].absoluteY)
setupMouseMovedHandler()
}

public func setupMouseMovedHandler() {
for mouse in GCMouse.mice() {
mouse.mouseInput?.mouseMovedHandler = { _, deltaX, deltaY in
Toucher.touchQueue.async {
if !mode.visible {
self.camera?.updated(CGFloat(deltaX), CGFloat(deltaY))
if let draggableButton = DraggableButtonAction.activeButton {
draggableButton.onMouseMoved(deltaX: CGFloat(deltaX), deltaY: CGFloat(deltaY))
} else {
self.camera?.updated(CGFloat(deltaX), CGFloat(deltaY))
}
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions PlayTools/Keymap/ControlModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class ControlModel {
xCoord: data[1],
yCoord: data[2],
parent: nil))
} else if data.count == 5 {
return DraggableButtonModel(data: ControlData(keyCodes: [Int(data[0])],
size: data[3],
xCoord: data[1],
yCoord: data[2]))
} else if data.count == 8 {
return JoystickModel(data: ControlData(keyCodes: [Int(data[0]),
Int(data[1]), Int(data[2]),
Expand Down Expand Up @@ -232,6 +237,36 @@ class JoystickButtonModel: ControlModel {
}
}

class DraggableButtonModel: MouseAreaModel {
var childButton: JoystickButtonModel?

override func save() -> [CGFloat] {
return [CGFloat(childButton!.data.keyCodes[0]),
self.data.xCoord, self.data.yCoord, self.data.size, self.data.size]
}

override func setKeyCodes(keys: [Int]) {
childButton!.setKeyCodes(keys: keys)
}
override func focus(_ focus: Bool) {
super.focus(focus)
if !focus {
childButton?.focus(false)
}
}
override func update() {
super.update()
if childButton == nil {
childButton = JoystickButtonModel(data: ControlData(keyCodes: [data.keyCodes[0]], parent: self))
}
let btn = button.subviews[0]
let buttonSize = data.size.absoluteSize / 3
let coord = (button.frame.width - buttonSize) / 2
btn.frame = CGRect(x: coord, y: coord, width: buttonSize, height: buttonSize)
btn.layer.cornerRadius = 0.5 * btn.bounds.size.width
}
}

class JoystickModel: ControlModel {
var joystickButtons = [JoystickButtonModel]()

Expand Down Expand Up @@ -282,6 +317,11 @@ class JoystickModel: ControlModel {
}
}

override func setKeyCodes(keys: [Int]) {
// I'm trying to be an easter egg
Toast.showOver(msg: "U~w~U")
}

override func resize(down: Bool) {
let mod = down ? 0.9 : 1.1
data.size = (button.frame.width * CGFloat(mod)).relativeSize
Expand Down Expand Up @@ -333,6 +373,11 @@ class MouseAreaModel: ControlModel {
button.clipsToBounds = true
}

override func setKeyCodes(keys: [Int]) {
EditorController.shared.removeControl()
EditorController.shared.addDraggableButton(CGPoint(x: data.xCoord, y: data.yCoord), keys[0])
}

override init(data: ControlData) {
super.init(data: data)
update()
Expand Down
9 changes: 9 additions & 0 deletions PlayTools/Keymap/EditorController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ final class EditorController: NSObject {
}
}

@objc public func addDraggableButton(_ center: CGPoint, _ keyCode: Int) {
if editorMode {
addControlToView(control: DraggableButtonModel(data: ControlData(keyCodes: [keyCode],
size: 15,
xCoord: center.x,
yCoord: center.y)))
}
}

func updateEditorText(_ str: String) {
view.label?.text = str
}
Expand Down

0 comments on commit d18bf21

Please sign in to comment.