Skip to content

MouseMoved を取得できる UI #99

Open

Description

@rrbox
  • Mouse 移動 -> SKView -> SKScene -> NotificationCenter -> MouseMoved を取得できる UI
  • MouseEnterd, MouseExeted は View と Scene のスケールの違いがあるため実装が難しい
  • MouseEnterd, MouseExeted は MouseMoved を使用して実装することが可能
  • WidgetNotificationSystem に mouseMoved(with:) を追加する #101
  • Button を hover 可能にする

Activity

  1. rrbox commented on Apr 9, 2023

    @rrbox
    OwnerAuthor

    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)
        }
    }
  2. rrbox commented on Apr 9, 2023

    @rrbox
    OwnerAuthor

    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)
        }
    }
  3. removed
    type: Discussion機能や利用方法などの考察をメモします
    on Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions