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 accX: Double?
50
    var accY: Double?
51
    var accZ: Double?
52
 
53
    var roll: Double?
54
 
55
    var scrollVelocity: Double?
56
    var moveVelocity: Double?
57
 
58
    var button: ButtonType?
59
    var click: ClickType?
60
 
61
    var key: String?
62
 
63
    var shutdownType: ShutdownType?
64
 
65
    required init?(coder aDecoder: NSCoder) {
66
        type = GyroPacketType(rawValue: Int(aDecoder.decodeInt32(forKey: "type")))!
67
        minimumVersion = Int(aDecoder.decodeInt32(forKey: "minimumVersion"))
68
        super.init()
69
 
70
        switch type {
71
        case .scroll:
72
            roll = aDecoder.decodeDouble(forKey: "roll")
73
            scrollVelocity = aDecoder.decodeDouble(forKey: "scrollVelocity")
74
            fallthrough
75
        case .movement:
76
            rotatX = aDecoder.decodeDouble(forKey: "rotatX")
77
            rotatZ = aDecoder.decodeDouble(forKey: "rotatZ")
78
            gravX = aDecoder.decodeDouble(forKey: "gravX")
79
            gravY = aDecoder.decodeDouble(forKey: "gravY")
80
            gravZ = aDecoder.decodeDouble(forKey: "gravZ")
81
            accX = aDecoder.decodeDouble(forKey: "accX")
82
            accY = aDecoder.decodeDouble(forKey: "accY")
83
            accZ = aDecoder.decodeDouble(forKey: "accZ")
84
            moveVelocity = aDecoder.decodeDouble(forKey: "moveVelocity")
85
        case .click:
86
            button = ButtonType(rawValue: Int(aDecoder.decodeInt32(forKey: "button")))!
87
            click = ClickType(rawValue: Int(aDecoder.decodeInt32(forKey: "click")))!
88
        case .keyTapped:
89
            key = (aDecoder.decodeObject(forKey: "key") as! String)
90
        default: break
91
        }
92
    }
93
 
94
    required init(type: GyroPacketType, minimumVersion: Int) {
95
        self.type = type
96
        self.minimumVersion = minimumVersion
97
        super.init()
98
    }
99
 
100
    func encode(with aCoder: NSCoder) {
101
 
102
        aCoder.encode(Int32(type.rawValue), forKey: "type")
103
        aCoder.encode(Int32(minimumVersion), forKey: "minimumVersion")
104
 
105
        switch type {
106
        case .scroll:
107
            aCoder.encode(roll!, forKey: "roll")
108
            aCoder.encode(scrollVelocity!, forKey: "scrollVelocity")
109
            fallthrough
110
        case .movement:
111
            aCoder.encode(gravX!, forKey: "gravX")
112
            aCoder.encode(gravY!, forKey: "gravY")
113
            aCoder.encode(gravZ!, forKey: "gravZ")
114
            aCoder.encode(rotatX!, forKey: "rotatX")
115
            aCoder.encode(rotatZ!, forKey: "rotatZ")
116
            aCoder.encode(accX!, forKey: "accX")
117
            aCoder.encode(accY!, forKey: "accY")
118
            aCoder.encode(accZ!, forKey: "accZ")
119
            aCoder.encode(moveVelocity!, forKey: "moveVelocity")
120
        case .click:
121
            aCoder.encode(Int32(button!.rawValue), forKey: "button")
122
            aCoder.encode(Int32(click!.rawValue), forKey: "click")
123
        case .keyTapped:
124
            aCoder.encode(key!, forKey: "key")
125
        default: break
126
        }
127
    }
128
 
129
}