Subversion Repositories Mobile Apps.GyroMouse

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
//
2
//  GyroPacket.swift
3
//  GyroMouse
4
//
5
//  Created by Matteo Riva on 29/08/15.
6
//  Copyright © 2015 Matteo Riva. All rights reserved.
7
//
8
 
9
import Foundation
10
 
11
enum GyroPacketType: Int {
12
    case movement = 0
13
    case click = 1
14
    case scroll = 2
15
    case keyTapped = 3
16
    case deleteBackward = 4
17
    case returnTapped = 5
18
    case resetPointerPosition = 6
19
}
20
 
21
enum ButtonType: Int {
22
    case left = 0
23
    case right = 1
24
}
25
 
26
enum ClickType: Int {
27
    case up = 0
28
    case down = 1
29
}
30
 
31
enum ShutdownType: Int {
32
    case shutdown = 0
33
    case reboot = 1
34
    case logout = 2
35
    case sleep = 3
36
}
37
 
38
@objc(GyroPacket)
39
class GyroPacket: NSObject, NSCoding {
40
 
41
    let type: GyroPacketType
42
    let minimumVersion: Int
43
 
44
    var gravX: Double?
45
    var gravY: Double?
46
    var gravZ: Double?
47
    var rotatX: Double?
48
    var rotatZ: Double?
49
    //var pitch: Double?
50
    //var yaw: Double?
51
    var accX: Double?
52
    var accY: Double?
53
    var accZ: Double?
54
 
55
    var roll: Double?
56
 
57
    var scrollVelocity: Double?
58
    var moveVelocity: Double?
59
 
60
    var button: ButtonType?
61
    var click: ClickType?
62
 
63
    var key: String?
64
 
65
    var shutdownType: ShutdownType?
66
 
67
    required init?(coder aDecoder: NSCoder) {
68
        type = GyroPacketType(rawValue: Int(aDecoder.decodeInt32(forKey: "type")))!
69
        minimumVersion = Int(aDecoder.decodeInt32(forKey: "minimumVersion"))
70
        super.init()
71
 
72
        switch type {
73
        case .scroll:
74
            roll = aDecoder.decodeDouble(forKey: "roll")
75
            scrollVelocity = aDecoder.decodeDouble(forKey: "scrollVelocity")
76
            fallthrough
77
        case .movement:
78
            rotatX = aDecoder.decodeDouble(forKey: "rotatX")
79
            rotatZ = aDecoder.decodeDouble(forKey: "rotatZ")
80
            //pitch = aDecoder.decodeDoubleForKey("pitch")
81
            //yaw = aDecoder.decodeDoubleForKey("yaw")
82
            gravX = aDecoder.decodeDouble(forKey: "gravX")
83
            gravY = aDecoder.decodeDouble(forKey: "gravY")
84
            gravZ = aDecoder.decodeDouble(forKey: "gravZ")
85
            accX = aDecoder.decodeDouble(forKey: "accX")
86
            accY = aDecoder.decodeDouble(forKey: "accY")
87
            accZ = aDecoder.decodeDouble(forKey: "accZ")
88
            moveVelocity = aDecoder.decodeDouble(forKey: "moveVelocity")
89
        case .click:
90
            button = ButtonType(rawValue: Int(aDecoder.decodeInt32(forKey: "button")))!
91
            click = ClickType(rawValue: Int(aDecoder.decodeInt32(forKey: "click")))!
92
        case .keyTapped:
93
            key = (aDecoder.decodeObject(forKey: "key") as! String)
94
        default: break
95
        }
96
    }
97
 
98
    required init(type: GyroPacketType, minimumVersion: Int) {
99
        self.type = type
100
        self.minimumVersion = minimumVersion
101
        super.init()
102
    }
103
 
104
    func encode(with aCoder: NSCoder) {
105
 
106
        aCoder.encode(Int32(type.rawValue), forKey: "type")
107
        aCoder.encode(Int32(minimumVersion), forKey: "minimumVersion")
108
 
109
        switch type {
110
        case .scroll:
111
            aCoder.encode(roll!, forKey: "roll")
112
            aCoder.encode(scrollVelocity!, forKey: "scrollVelocity")
113
            fallthrough
114
        case .movement:
115
            aCoder.encode(gravX!, forKey: "gravX")
116
            aCoder.encode(gravY!, forKey: "gravY")
117
            aCoder.encode(gravZ!, forKey: "gravZ")
118
            //aCoder.encodeDouble(pitch!, forKey: "pitch")
119
            //aCoder.encodeDouble(yaw!, forKey: "yaw")
120
            aCoder.encode(rotatX!, forKey: "rotatX")
121
            aCoder.encode(rotatZ!, forKey: "rotatZ")
122
            aCoder.encode(accX!, forKey: "accX")
123
            aCoder.encode(accY!, forKey: "accY")
124
            aCoder.encode(accZ!, forKey: "accZ")
125
            aCoder.encode(moveVelocity!, forKey: "moveVelocity")
126
        case .click:
127
            aCoder.encode(Int32(button!.rawValue), forKey: "button")
128
            aCoder.encode(Int32(click!.rawValue), forKey: "click")
129
        case .keyTapped:
130
            aCoder.encode(key!, forKey: "key")
131
        default: break
132
        }
133
    }
134
 
135
}