Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MouseMoved を取得できる UI #99

Open
2 tasks
rrbox opened this issue Apr 8, 2023 · 2 comments
Open
2 tasks

MouseMoved を取得できる UI #99

rrbox opened this issue Apr 8, 2023 · 2 comments
Labels
type: Enhancement New feature or request

Comments

@rrbox
Copy link
Owner

rrbox commented Apr 8, 2023

  • Mouse 移動 -> SKView -> SKScene -> NotificationCenter -> MouseMoved を取得できる UI
  • MouseEnterd, MouseExeted は View と Scene のスケールの違いがあるため実装が難しい
  • MouseEnterd, MouseExeted は MouseMoved を使用して実装することが可能
  • WidgetNotificationSystem に mouseMoved(with:) を追加する #101
  • Button を hover 可能にする
@rrbox rrbox added type: Enhancement New feature or request type: Discussion 機能や利用方法などの考察をメモします labels Apr 8, 2023
@rrbox
Copy link
Owner Author

rrbox commented Apr 9, 2023

SKNode のサンプルです。

class MouseTrackableNode: SKSpriteNode {
    var isContainMouse = false
    
    override func mouseEntered(with event: NSEvent) {
        self.color = .red
    }
    
    override func mouseExited(with event: NSEvent) {
        self.color = .white
    }
    
    func register(notificarionHandler: UINotificaitionHandler) {
        notificarionHandler.notificationCenter.addObserver(self, selector: #selector(self.receiveMouseMoved(notification:)), name: .mouseMoved, object: nil)
    }
    
    func getEvent(from notification: Notification) -> NSEvent {
        notification.userInfo!["event"] as! NSEvent
    }
    
    override func mouseMoved(with event: NSEvent) {
        let pos = event.location(in: self.parent!)
        if self.contains(pos) && !self.isContainMouse {
            self.mouseEntered(with: event)
            self.isContainMouse.toggle()
        } else if !self.contains(pos) && self.isContainMouse {
            self.mouseExited(with: event)
            self.isContainMouse.toggle()
        }
    }
    
    @objc func receiveMouseMoved(notification: Notification) {
        let event = self.getEvent(from: notification)
        self.mouseMoved(with: event)
    }
}

@rrbox
Copy link
Owner Author

rrbox commented Apr 9, 2023

SKScene と SKView の設定サンプルです。

class View: SKView {
    var sceneTrackingArea: NSTrackingArea?
    
    func setTrackingArea() {
        let scene = self.scene!
        let trackingArea = NSTrackingArea(
            rect: frame,
            options: [.mouseMoved, .activeAlways],
            owner: scene)
        self.addTrackingArea(trackingArea)
        self.sceneTrackingArea = trackingArea
    }
    
    override func updateTrackingAreas() {
        self.setTrackingArea()
    }
    
}
class Scene: SKScene {
    
    let notificationSystem = UINotificaitionHandler()
    
    override func mouseMoved(with event: NSEvent) {
        self.notificationSystem.mouseMoved(with: event)
    }
}

@rrbox rrbox removed the type: Discussion 機能や利用方法などの考察をメモします label Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: Enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant