Subversion Repositories Mobile Apps.GyroMouse

Rev

Rev 1 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1 Rev 7
Line 32... Line 32...
32
                                                       height: UIScreen.main.bounds.size.width * 1 / 16))
32
                                                       height: UIScreen.main.bounds.size.width * 1 / 16))
33
    
33
    
34
    private var lButton = UIButton(frame: CGRect(x: 0,
34
    private var lButton = UIButton(frame: CGRect(x: 0,
35
                                                 y: UIScreen.main.bounds.size.height * 2 / 16,
35
                                                 y: UIScreen.main.bounds.size.height * 2 / 16,
36
                                                 width: UIScreen.main.bounds.size.width * 3 / 4,
36
                                                 width: UIScreen.main.bounds.size.width * 3 / 4,
37
                                                 height: UIScreen.main.bounds.size.height * 14 / 16))
37
                                                 height: UIScreen.main.bounds.size.height * 7 / 16))
38
    private var rButton = UIButton(frame: CGRect(x: UIScreen.main.bounds.size.width * 3 / 4,
38
    private var rButton = UIButton(frame: CGRect(x: UIScreen.main.bounds.size.width * 3 / 4,
39
                                                 y: UIScreen.main.bounds.size.height * 2 / 16,
39
                                                 y: UIScreen.main.bounds.size.height * 2 / 16,
40
                                                 width: UIScreen.main.bounds.size.width * 1 / 4,
40
                                                 width: UIScreen.main.bounds.size.width * 1 / 4,
41
                                                 height: UIScreen.main.bounds.size.height * 14 / 16))
41
                                                 height: UIScreen.main.bounds.size.height * 5 / 16))
-
 
42
    private var resetButton = UIButton(frame: CGRect(x: UIScreen.main.bounds.size.width * 3 / 4,
-
 
43
                                                     y: UIScreen.main.bounds.size.height * 7 / 16,
-
 
44
                                                     width: UIScreen.main.bounds.size.width * 1 / 4,
-
 
45
                                                     height: UIScreen.main.bounds.size.height * 2 / 16))
42
    
46
    
43
    private let client = (UIApplication.shared.delegate as! AppDelegate).client
47
    private let client = (UIApplication.shared.delegate as! AppDelegate).client
44
    private let manager = CMMotionManager()
48
    private let manager = CMMotionManager()
45
    
49
    
46
    private var moveVelocity = UserDefaults.standard.double(forKey: "moveVelocity")
50
    private var moveVelocity = UserDefaults.standard.double(forKey: "moveVelocity")
47
    private var scrollVelocity = UserDefaults.standard.double(forKey: "scrollVelocity")
51
    private var scrollVelocity = UserDefaults.standard.double(forKey: "scrollVelocity")
48
    private var shakeToReset = UserDefaults.standard.bool(forKey: "shakeToReset")
52
    private var shakeToReset = UserDefaults.standard.bool(forKey: "shakeToReset")
-
 
53
 
-
 
54
    private var suspendMotionPacketsUntil: Date = Date()
49
    
55
    
50
    private lazy var handler: CMDeviceMotionHandler = {
56
    private lazy var handler: CMDeviceMotionHandler = {
51
        return {[weak self] (data, error) -> Void in
57
        return {[weak self] (data, error) -> Void in
52
            if error == nil {
-
 
53
                
-
 
54
                let roll = data!.attitude.roll
58
            if (error == nil) && (self != nil) && (Date() > self!.suspendMotionPacketsUntil) {
55
                
-
 
56
                var type = GyroPacketType.scroll
59
                self?.client.sendPacket(
57
                
-
 
58
                if roll > -0.45 &&; roll < 0.45 {
60
                    ((data!.attitude.roll > -0.45) && (data!.attitude.roll < 0.45) ? "move" : &quot;scroll&quot;),
59
                    type = .movement
61
                    keyTapped: "",
60
                }
62
                    isButtonDown: false,
61
                
-
 
62
                let packet = GyroPacket(type: type, minimumVersion: minimumVersion)
-
 
63
                
-
 
64
                packet.gravX = data!.gravity.x
63
                    isRightButton: false,
65
                packet.gravY = data!.gravity.y
64
                    roll: data!.attitude.roll,
66
                packet.gravZ = data!.gravity.z
65
                    scrollVelocity: (self?.scrollVelocity ?? 0),
67
                packet.rotatX =  data!.rotationRate.x//data!.attitude.pitch
66
                    grav: (X: data!.gravity.x, Y: data!.gravity.y, Z: data!.gravity.z),
68
                packet.roll = roll
-
 
69
                packet.rotatZ = data!.rotationRate.z//data!.attitude.yaw
67
                    rotat: (X: data!.rotationRate.x, Z: data!.rotationRate.z),
70
                packet.accX = data!.userAcceleration.x
-
 
71
                packet.accY = data!.userAcceleration.y
-
 
72
                packet.accZ = data!.userAcceleration.z
68
                    acc: (X: data!.userAcceleration.x, Y: data!.userAcceleration.y, Z: data!.userAcceleration.z),
73
                packet.moveVelocity = self?.moveVelocity ?? 0
69
                    moveVelocity: (self?.moveVelocity ?? 0)
74
                packet.scrollVelocity = self?.scrollVelocity ?? 0
-
 
75
                
70
                )
76
                self?.client.sendPacket(packet)
-
 
77
            }
71
            }
78
        }
72
        }
79
    }()
73
    }()
80
    
74
    
81
    override func viewDidLoad() {
75
    override func viewDidLoad() {
Line 84... Line 78...
84
        if manager.isDeviceMotionAvailable {
78
        if manager.isDeviceMotionAvailable {
85
            manager.deviceMotionUpdateInterval = 0.01
79
            manager.deviceMotionUpdateInterval = 0.01
86
            manager.startDeviceMotionUpdates(to: OperationQueue.main, withHandler: handler)
80
            manager.startDeviceMotionUpdates(to: OperationQueue.main, withHandler: handler)
87
        }
81
        }
88
        
82
        
89
        NotificationCenter.default.addObserver(forName: ClientDidDisconnectNotification, object: client, queue: OperationQueue.main) {[weak self] (_) -> Void in
83
        NotificationCenter.default.addObserver(forName: GyroMouseDisconnectedNotification, object: client, queue: OperationQueue.main) {[weak self] (_) -> Void in
90
            _=self?.navigationController?.popViewController(animated: true)
84
            _=self?.navigationController?.popViewController(animated: true)
91
        }
85
        }
92
 
86
 
93
        txtInput.tintColor = UIColor.white
87
        txtInput.tintColor = UIColor.white
94
        txtInput.backgroundColor = UIColor.darkGray.withAlphaComponent(0.7)
88
        txtInput.backgroundColor = UIColor.darkGray.withAlphaComponent(0.7)
Line 115... Line 109...
115
        rButton.layer.borderWidth = CGFloat(1.0)
109
        rButton.layer.borderWidth = CGFloat(1.0)
116
        rButton.layer.borderColor = UIColor.darkGray.cgColor//CGColor(red: 0.25, green: 0.25, blue: 0.25, alpha: 1)
110
        rButton.layer.borderColor = UIColor.darkGray.cgColor//CGColor(red: 0.25, green: 0.25, blue: 0.25, alpha: 1)
117
        rButton.addTarget(self, action: #selector(rButtonDown), for: .touchDown)
111
        rButton.addTarget(self, action: #selector(rButtonDown), for: .touchDown)
118
        rButton.addTarget(self, action: #selector(rButtonUp), for: .touchUpInside)
112
        rButton.addTarget(self, action: #selector(rButtonUp), for: .touchUpInside)
119
        self.view.addSubview(rButton)
113
        self.view.addSubview(rButton)
-
 
114
 
-
 
115
        resetButton.backgroundColor = .black
-
 
116
        resetButton.layer.borderWidth = CGFloat(1.0)
-
 
117
        resetButton.layer.borderColor = UIColor.darkGray.cgColor//CGColor(red: 0.25, green: 0.25, blue: 0.25, alpha: 1)
-
 
118
        resetButton.addTarget(self, action: #selector(resetButtonTouched), for: .touchDown)
-
 
119
        self.view.addSubview(resetButton)
120
    }
120
    }
121
 
121
 
122
    @objc func lButtonDown()
122
    @objc func lButtonDown()
123
    {
123
    {
124
        let packet = GyroPacket(type: .click, minimumVersion: minimumVersion)
-
 
125
        packet.button = ButtonType.left
-
 
126
        packet.click = .down
-
 
127
        lButton.backgroundColor = UIColor (red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5) // grey
124
        lButton.backgroundColor = UIColor (red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5) // grey
128
        client.sendPacket(packet)
125
        self.suspendMotionPacketsUntil = Date() + TimeInterval(0.25) // block motion packets for 250 milliseconds to facilitate precise clicks
-
 
126
        self.client.sendPacket("click", keyTapped: "", isButtonDown: true, isRightButton: false, roll: 0, scrollVelocity: 0, grav: (X: 0, Y: 0, Z: 0), rotat: (X: 0, Z: 0), acc: (X: 0, Y: 0, Z: 0), moveVelocity: 0)
129
    }
127
    }
130
    @objc func lButtonUp()
128
    @objc func lButtonUp()
131
    {
129
    {
132
        let packet = GyroPacket(type: .click, minimumVersion: minimumVersion)
-
 
133
        packet.button = ButtonType.left
-
 
134
        packet.click = .up
-
 
135
        lButton.backgroundColor = UIColor (red: 0, green: 0, blue: 0, alpha: 0.8) // black
130
        lButton.backgroundColor = UIColor (red: 0, green: 0, blue: 0, alpha: 0.8) // black
136
        client.sendPacket(packet)
131
        self.client.sendPacket("click", keyTapped: "", isButtonDown: false, isRightButton: false, roll: 0, scrollVelocity: 0, grav: (X: 0, Y: 0, Z: 0), rotat: (X: 0, Z: 0), acc: (X: 0, Y: 0, Z: 0), moveVelocity: 0)
137
    }
132
    }
138
 
133
 
139
    @objc func rButtonDown()
134
    @objc func rButtonDown()
140
    {
135
    {
141
        let packet = GyroPacket(type: .click, minimumVersion: minimumVersion)
-
 
142
        packet.button = ButtonType.right
-
 
143
        packet.click = .down
-
 
144
        rButton.backgroundColor = UIColor (red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5) // grey
136
        rButton.backgroundColor = UIColor (red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5) // grey
145
        client.sendPacket(packet)
137
        self.suspendMotionPacketsUntil = Date() + TimeInterval(0.25) // block motion packets for 250 milliseconds to facilitate precise clicks
-
 
138
        self.client.sendPacket("click", keyTapped: "", isButtonDown: true, isRightButton: true, roll: 0, scrollVelocity: 0, grav: (X: 0, Y: 0, Z: 0), rotat: (X: 0, Z: 0), acc: (X: 0, Y: 0, Z: 0), moveVelocity: 0)
146
    }
139
    }
147
 
140
 
148
    @objc func rButtonUp()
141
    @objc func rButtonUp()
149
    {
142
    {
150
        let packet = GyroPacket(type: .click, minimumVersion: minimumVersion)
-
 
151
        packet.button = ButtonType.right
-
 
152
        packet.click = .up
-
 
153
        rButton.backgroundColor = UIColor (red: 0, green: 0, blue: 0, alpha: 0.8) // black
143
        rButton.backgroundColor = UIColor (red: 0, green: 0, blue: 0, alpha: 0.8) // black
154
        client.sendPacket(packet)
144
        self.client.sendPacket("click", keyTapped: "", isButtonDown: false, isRightButton: true, roll: 0, scrollVelocity: 0, grav: (X: 0, Y: 0, Z: 0), rotat: (X: 0, Z: 0), acc: (X: 0, Y: 0, Z: 0), moveVelocity: 0)
155
    }
145
    }
156
 
146
 
-
 
147
    @objc func resetButtonTouched()
-
 
148
    {
-
 
149
        self.client.sendPacket("reset", keyTapped: "", isButtonDown: false, isRightButton: false, roll: 0, scrollVelocity: 0, grav: (X: 0, Y: 0, Z: 0), rotat: (X: 0, Z: 0), acc: (X: 0, Y: 0, Z: 0), moveVelocity: 0)
-
 
150
    }
-
 
151
    
157
    deinit {
152
    deinit {
158
        NotificationCenter.default.removeObserver(self)
153
        NotificationCenter.default.removeObserver(self)
159
        manager.stopDeviceMotionUpdates()
154
        manager.stopDeviceMotionUpdates()
160
        client.endConnection()
155
        client.endConnection()
161
    }
156
    }
Line 169... Line 164...
169
    }
164
    }
170
    
165
    
171
    //MARK: - Button actions
166
    //MARK: - Button actions
172
 
167
 
173
    @IBAction func resetPointerPositionAction() {
168
    @IBAction func resetPointerPositionAction() {
174
        let packet = GyroPacket(type: .resetPointerPosition, minimumVersion: minimumVersion)
169
        self.client.sendPacket("reset", keyTapped: "", isButtonDown: false, isRightButton: false, roll: 0, scrollVelocity: 0, grav: (X: 0, Y: 0, Z: 0), rotat: (X: 0, Z: 0), acc: (X: 0, Y: 0, Z: 0), moveVelocity: 0)
175
        client.sendPacket(packet)
-
 
176
    }
170
    }
177
    
171
    
178
    //MARK: - KeyboardHandlerDelegate
172
    //MARK: - KeyboardHandlerDelegate
179
    
173
    
180
    override func viewDidAppear(_ animated: Bool) {
174
    override func viewDidAppear(_ animated: Bool) {
181
        txtInput.becomeFirstResponder() // forces iOS keyboard to appear
175
        txtInput.becomeFirstResponder() // forces iOS keyboard to appear
182
    }
176
    }
183
 
177
 
184
    func didPressBackspace(_ textField: MyUITextField) {
178
    func didPressBackspace(_ textField: MyUITextField) {
185
        let packet = GyroPacket(type: .deleteBackward, minimumVersion: minimumVersion)
179
        self.client.sendPacket("backspace", keyTapped: "", isButtonDown: false, isRightButton: false, roll: 0, scrollVelocity: 0, grav: (X: 0, Y: 0, Z: 0), rotat: (X: 0, Z: 0), acc: (X: 0, Y: 0, Z: 0), moveVelocity: 0)
186
        client.sendPacket(packet)
-
 
187
    }
180
    }
188
    
181
    
189
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
182
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
190
        let packet = GyroPacket(type: .keyTapped, minimumVersion: minimumVersion)
183
        self.client.sendPacket("key", keyTapped: string, isButtonDown: false, isRightButton: false, roll: 0, scrollVelocity: 0, grav: (X: 0, Y: 0, Z: 0), rotat: (X: 0, Z: 0), acc: (X: 0, Y: 0, Z: 0), moveVelocity: 0)
191
        packet.key = string
-
 
192
        client.sendPacket(packet)
-
 
193
        return true
184
        return true
194
    }
185
    }
195
    
186
    
196
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
187
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
197
        let packet = GyroPacket(type: .returnTapped, minimumVersion: minimumVersion)
188
        self.client.sendPacket("enter", keyTapped: "", isButtonDown: false, isRightButton: false, roll: 0, scrollVelocity: 0, grav: (X: 0, Y: 0, Z: 0), rotat: (X: 0, Z: 0), acc: (X: 0, Y: 0, Z: 0), moveVelocity: 0)
198
        client.sendPacket(packet)
-
 
199
        txtInput.text = ""
189
        txtInput.text = ""
200
        return false
190
        return false
201
    }
191
    }
202
    
192
    
203
    //MARK: - SettingsViewControllerDelegate
193
    //MARK: - SettingsViewControllerDelegate