Skip to content

Commit

Permalink
Added an FPS counter to RiveView that can be easily toggled. Moved co…
Browse files Browse the repository at this point in the history
…mmon init code into a sharedInit() function. Added Jellyfish asset to the TouchEvents example.
  • Loading branch information
duncandoit committed May 11, 2022
1 parent 6fa9937 commit 518fa49
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
Binary file added Example-iOS/Assets/hero_editor.riv
Binary file not shown.
4 changes: 4 additions & 0 deletions Example-iOS/RiveExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
C3357CA1280F42EC00F03B6F /* ExamplesMaster.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3357CA0280F42EC00F03B6F /* ExamplesMaster.swift */; };
C3460A002800A6CE002DBCB7 /* bird.riv in Resources */ = {isa = PBXBuildFile; fileRef = C34609FD2800A6CE002DBCB7 /* bird.riv */; };
C3468E6227FDCBC6008652FD /* SimpleSlider.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3468E6127FDCBC6008652FD /* SimpleSlider.swift */; };
C3745FCE282AE2320087F4AF /* hero_editor.riv in Resources */ = {isa = PBXBuildFile; fileRef = C3745FCB282AE2320087F4AF /* hero_editor.riv */; };
C3D187F3280751A8008B739A /* RiveProgressBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3D187F2280751A8008B739A /* RiveProgressBar.swift */; };
C3D187F728075B6C008B739A /* riveslider.riv in Resources */ = {isa = PBXBuildFile; fileRef = C3D187F628075B6C008B739A /* riveslider.riv */; };
C3D187F9280770EA008B739A /* truck.riv in Resources */ = {isa = PBXBuildFile; fileRef = C3D187F8280770EA008B739A /* truck.riv */; };
Expand Down Expand Up @@ -151,6 +152,7 @@
C3357CA0280F42EC00F03B6F /* ExamplesMaster.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExamplesMaster.swift; sourceTree = "<group>"; };
C34609FD2800A6CE002DBCB7 /* bird.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = bird.riv; sourceTree = "<group>"; };
C3468E6127FDCBC6008652FD /* SimpleSlider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimpleSlider.swift; sourceTree = "<group>"; };
C3745FCB282AE2320087F4AF /* hero_editor.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = hero_editor.riv; sourceTree = "<group>"; };
C3D187F2280751A8008B739A /* RiveProgressBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RiveProgressBar.swift; sourceTree = "<group>"; };
C3D187F628075B6C008B739A /* riveslider.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = riveslider.riv; sourceTree = "<group>"; };
C3D187F8280770EA008B739A /* truck.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = truck.riv; sourceTree = "<group>"; };
Expand Down Expand Up @@ -245,6 +247,7 @@
C9696B0E24FC6FD10041502A /* Assets */ = {
isa = PBXGroup;
children = (
C3745FCB282AE2320087F4AF /* hero_editor.riv */,
C3ECAC30281840EF00A81123 /* watch_v1.riv */,
C3ECAC29281837B300A81123 /* leg_day_events_example.riv */,
C3ECAC28281837B300A81123 /* play_button_event_example.riv */,
Expand Down Expand Up @@ -463,6 +466,7 @@
C3ECAC2D281837B300A81123 /* switch_event_example.riv in Resources */,
0480028B2729AA4400F7132B /* clean_icon_set.riv in Resources */,
04F1C80B26A8442300CEE6BE /* two_bone_ik.riv in Resources */,
C3745FCE282AE2320087F4AF /* hero_editor.riv in Resources */,
C3D187F9280770EA008B739A /* truck.riv in Resources */,
C9C73EA124FC471E00EF9516 /* Preview Assets.xcassets in Resources */,
042C88DD2644447500E7DBB2 /* ui_swipe_left_to_delete.riv in Resources */,
Expand Down
8 changes: 6 additions & 2 deletions Example-iOS/Source/Examples/SwiftUI/SwiftTouchEvents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ struct SwiftTouchEvents: DismissableView {
var body: some View {
ScrollView {
VStack {
RiveViewModel(fileName: "play_button_event_example", stateMachineName: "State Machine")
RiveViewModel(fileName: "hero_editor", stateMachineName: "Jellyfish")
.view()
.aspectRatio(1, contentMode: .fit)

RiveViewModel(fileName: "play_button_event_example", stateMachineName: "State Machine")
.view()
.aspectRatio(1, contentMode: .fit)

RiveViewModel(fileName: "switch_event_example", stateMachineName: "Main State Machine")
.view()
.aspectRatio(1, contentMode: .fit)

RiveViewModel(fileName: "magic_8-ball_v2", stateMachineName: "Main State Machine")
.view()
.aspectRatio(1, contentMode: .fit)
Expand Down
79 changes: 63 additions & 16 deletions Source/Components/RiveView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,32 @@ open class RiveView: RiveRendererView {

private var lastTime: CFTimeInterval = 0
private var displayLinkProxy: CADisplayLinkProxy?
private var fpsLabel: UILabel? = nil
private var fpsFormatter: NumberFormatter? = nil
public var showFPS: Bool = false {
didSet {
if showFPS {
fpsLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 75, height: 30))
addSubview(fpsLabel!)
fpsLabel!.backgroundColor = .darkGray
fpsLabel!.textColor = .white
fpsLabel!.textAlignment = .center
fpsLabel!.alpha = 0.75
fpsLabel!.clipsToBounds = true
fpsLabel!.layer.cornerRadius = 10
fpsLabel!.text = "..."

fpsFormatter = NumberFormatter()
fpsFormatter!.minimumFractionDigits = 2
fpsFormatter!.maximumFractionDigits = 2
fpsFormatter!.roundingMode = .down
} else {
fpsLabel?.removeFromSuperview()
fpsLabel = nil
fpsFormatter = nil
}
}
}

// Delegates
public weak var playerDelegate: RivePlayerDelegate?
Expand Down Expand Up @@ -158,13 +184,11 @@ open class RiveView: RiveRendererView {
stateChangeDelegate: RStateDelegate? = nil
) throws {
super.init(frame: .zero)
self.fit = fit
self.alignment = alignment
self.playerDelegate = playerDelegate
self.inputsDelegate = inputsDelegate
self.stateChangeDelegate = stateChangeDelegate

try configure(riveFile, artboardName: artboardName, animationName: animationName, stateMachineName: stateMachineName, autoPlay: autoplay)
try sharedInit(
riveFile, artboardName: artboardName, animationName: animationName, stateMachineName: stateMachineName,
fit: fit, alignment: alignment, autoplay: autoplay,
playerDelegate: playerDelegate, inputsDelegate: inputsDelegate, stateChangeDelegate: stateChangeDelegate
)
}

/// Constructor with a .riv file name.
Expand Down Expand Up @@ -192,14 +216,11 @@ open class RiveView: RiveRendererView {
stateChangeDelegate: RStateDelegate? = nil
) throws {
super.init(frame: .zero)
let riveFile = try RiveFile(name: fileName)
self.fit = fit
self.alignment = alignment
self.playerDelegate = playerDelegate
self.inputsDelegate = inputsDelegate
self.stateChangeDelegate = stateChangeDelegate

try configure(riveFile, artboardName: artboardName, animationName: animationName, stateMachineName: stateMachineName, autoPlay: autoplay)
try sharedInit(
try RiveFile(name: fileName), artboardName: artboardName, animationName: animationName, stateMachineName: stateMachineName,
fit: fit, alignment: alignment, autoplay: autoplay,
playerDelegate: playerDelegate, inputsDelegate: inputsDelegate, stateChangeDelegate: stateChangeDelegate
)
}

/// Constructor with a resource file.
Expand Down Expand Up @@ -227,7 +248,25 @@ open class RiveView: RiveRendererView {
stateChangeDelegate: RStateDelegate? = nil
) throws {
super.init(frame: .zero)
let riveFile = RiveFile(httpUrl: webURL, with:self)!
try sharedInit(
RiveFile(httpUrl: webURL, with:self)!, artboardName: artboardName, animationName: animationName, stateMachineName: stateMachineName,
fit: fit, alignment: alignment, autoplay: autoplay,
playerDelegate: playerDelegate, inputsDelegate: inputsDelegate, stateChangeDelegate: stateChangeDelegate
)
}

private func sharedInit(
_ riveFile: RiveFile,
artboardName: String?,
animationName: String?,
stateMachineName: String?,
fit: Fit,
alignment: Alignment,
autoplay: Bool,
playerDelegate: RivePlayerDelegate?,
inputsDelegate: RInputDelegate?,
stateChangeDelegate: RStateDelegate?
) throws {
self.fit = fit
self.alignment = alignment
self.playerDelegate = playerDelegate
Expand Down Expand Up @@ -339,6 +378,8 @@ extension RiveView {
} else {
advance(delta: 0)
}

showFPS = true
}

/// Stop playback, clear any created animation or state machine instances.
Expand Down Expand Up @@ -464,6 +505,7 @@ extension RiveView {
displayLinkProxy?.invalidate()
displayLinkProxy = nil
lastTime = 0
fpsLabel?.text = "Stopped"
}


Expand Down Expand Up @@ -493,6 +535,11 @@ extension RiveView {
// Calculate the time elapsed between ticks
let elapsedTime = timestamp - lastTime
lastTime = timestamp

if elapsedTime != 0 {
fpsLabel?.text = fpsFormatter!.string(from: NSNumber(value: 1 / elapsedTime))! + "fps"
}

advance(delta: elapsedTime)
if !isPlaying {
stopTimer()
Expand Down

0 comments on commit 518fa49

Please sign in to comment.