-
Notifications
You must be signed in to change notification settings - Fork 41
Custom Elements
Exploring any one of the sample projects is a good way to understand how custom elements work.
Custom elements are defined in a class that descends from CustomElementsSuperclass
. The recommended file name is VgcCustomElements.swift
and the recommended class name is CustomElements
.
Here is the custom elements file from the sample projects - explanations contained in in-line comments:
import Foundation
import VirtualGameController
///
/// Create a case for each one of your custom elements, along with a raw value in
/// the range shown below (to prevent collisions with the standard elements).
public enum CustomElementType: Int {
case FiddlestickX = 50
case FiddlestickY = 51
case FiddlestickZ = 52
case Keyboard = 53
}
///
/// Your customElements class must descend from CustomElementsSuperclass
///
public class CustomElements: CustomElementsSuperclass {
override init() {
super.init()
///
/// CUSTOMIZE HERE
///
/// Create a constructor for each of your custom elements.
///
/// - parameter name: Human-readable name, used in logging
/// - parameter dataType: Supported types include .Float, .String and .Int
/// - parameter type: Unique identifier, numbered beginning with 100 to keep them out of collision with standard elements
///
customProfileElements = [
CustomElement(name: "Fiddlestick X", dataType: .Float, type:CustomElementType.FiddlestickX.rawValue),
CustomElement(name: "Fiddlestick Y", dataType: .Float, type:CustomElementType.FiddlestickY.rawValue),
CustomElement(name: "Fiddlestick Z", dataType: .Float, type:CustomElementType.FiddlestickZ.rawValue),
CustomElement(name: "Keyboard", dataType: .String, type:CustomElementType.Keyboard.rawValue)
]
}
}
Your custom element class must be passed into the startAs
used to initialize VirtualGameController functionality in your app, and it must be done for each of the app roles (Peripheral, Central and Bridge) that you have in use:
VgcManager.startAs(.Central, appIdentifier: "vgc", customElements: CustomElements(), customMappings: CustomMappings())
Note that when this method is used for start-up, you cannot pass nil to customMappings, and so you should include a customMappings class without any mappings, as described here.
The syntax for setting the value of a custom element is:
custom[CustomElementType.FiddlestickX.rawValue] = 1.0
If you are working with an element, you can obtain the value of a custom element this way:
controller.elements.custom[element.identifier]?.value
If you need to access it by name:
controller.elements.custom[.FiddlestickX]?.value
You set a value changed handler on the custom
profile the same way you do for the standard profiles:
Elements.customElements.valueChangedHandler = { (controller, element) in
// Do something here
}
You can pass a customElements()
with no elements defined to startAs
.