Skip to content

Instantly share code, notes, and snippets.

@foxicode
Created May 25, 2020 18:39
Show Gist options
  • Select an option

  • Save foxicode/84f1e005c8ae429565ca5a1a08392b2c to your computer and use it in GitHub Desktop.

Select an option

Save foxicode/84f1e005c8ae429565ca5a1a08392b2c to your computer and use it in GitHub Desktop.
Version of Lights.swift for dynamic linking
import Foundation
let circle1 = 1
let circle2 = 2
let circle3 = 3
var time = 0.0
var step = 0
var updateProperty: (_ tag: Int, _ property: String, _ value: Any) -> Void
var addWidget: (_ widget: String, _ properties: [String: Any]) -> Void
@_cdecl("set_update_property_func")
public func setUpdatePropertyFunc(f: @escaping (_ tag: Int, _ property: String, _ value: Any) -> Void) {
updateProperty = f
}
@_cdecl("set_add_widget_func")
public func setAddWidgetFunc(f: @escaping (_ widget: String, _ properties: [String: Any]) -> Void) {
addWidget = f
}
@_cdecl("lights_tick")
public func tick(delta: TimeInterval) {
time += delta
if time >= 1.0 {
time -= 1.0
loopStep()
}
}
@_cdecl("loop_step")
func loopStep() {
switch step {
case 0:
updateProperty(circle1, "color", "#50ff0000")
updateProperty(circle2, "color", "#ffffff00")
updateProperty(circle3, "color", "#5000ff00")
case 1:
updateProperty(circle1, "color", "#50ff0000")
updateProperty(circle2, "color", "#50ffff00")
updateProperty(circle3, "color", "#ff00ff00")
case 2:
updateProperty(circle1, "color", "#ffff0000")
updateProperty(circle2, "color", "#50ffff00")
updateProperty(circle3, "color", "#5000ff00")
default:
break
}
step = (step + 1) % 3
}
@_cdecl("lights_start")
public func start(width: Double, height: Double) {
let circleRadius = height / 10.0
addWidget("Circle", [
"tag": circle1,
"x": width / 2,
"y": circleRadius * 2,
"radius": circleRadius,
"color": "#ffff0000"
])
addWidget("Circle", [
"tag": circle2,
"x": width / 2,
"y": circleRadius * 5,
"radius": circleRadius,
"color": "#50ffff00"
])
addWidget("Circle", [
"tag": circle3,
"x": width / 2,
"y": circleRadius * 8,
"radius": circleRadius,
"color": "#5000ff00"
])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment