Skip to content

Commit

Permalink
Add a "Stress Test" to the example iOS app
Browse files Browse the repository at this point in the history
Diffs=
a7e5cc22f Add a "Stress Test" to the example iOS app (#5986)

Co-authored-by: Chris Dalton <99840794+csmartdalton@users.noreply.github.com>
Co-authored-by: Zach Plata <plata.zach@gmail.com>
  • Loading branch information
3 people committed Oct 5, 2023
1 parent 19dbb8e commit afbd9e2
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
54d736fec499e15bcd2420e61cd370bf11b9b70c
a7e5cc22f727fc5eb6a56ddd1f7ad85b17db2a59
2 changes: 1 addition & 1 deletion .rive_renderer
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c1e10dc4e600d465a09afcc162b65ad5d7e44ee8
cfd378a136e7d4a3a4afc8e9be97b13514034a96
Binary file added Example-iOS/Assets/Bear.riv
Binary file not shown.
Binary file added Example-iOS/Assets/marty.riv
Binary file not shown.
Binary file added Example-iOS/Assets/paper.riv
Binary file not shown.
16 changes: 16 additions & 0 deletions Example-iOS/RiveExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
04E51C702A151C230075E473 /* blendmodes.riv in Resources */ = {isa = PBXBuildFile; fileRef = 046AFA6E2673AF04004ED497 /* blendmodes.riv */; };
04F1C80B26A8442300CEE6BE /* two_bone_ik.riv in Resources */ = {isa = PBXBuildFile; fileRef = 04F1C80826A8442300CEE6BE /* two_bone_ik.riv */; };
27108F2F282C96E700A99D81 /* light_switch.riv in Resources */ = {isa = PBXBuildFile; fileRef = 27108F2C282C96E700A99D81 /* light_switch.riv */; };
83C89ACB29886ECB00044C17 /* StressTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83C89ACA29886ECB00044C17 /* StressTest.swift */; };
83C89ACF2988709400044C17 /* marty.riv in Resources */ = {isa = PBXBuildFile; fileRef = 83C89ACE2988709400044C17 /* marty.riv */; };
83C89AD1298870A700044C17 /* paper.riv in Resources */ = {isa = PBXBuildFile; fileRef = 83C89AD0298870A700044C17 /* paper.riv */; };
83DE4CB52AB397A800B88B72 /* Bear.riv in Resources */ = {isa = PBXBuildFile; fileRef = 83DE4CB42AB3974300B88B72 /* Bear.riv */; };
C324DB5628071EB80060589F /* RiveSwitch.swift in Sources */ = {isa = PBXBuildFile; fileRef = C324DB5528071EB80060589F /* RiveSwitch.swift */; };
C324DB5B2807216B0060589F /* RiveSlider.swift in Sources */ = {isa = PBXBuildFile; fileRef = C324DB5A2807216B0060589F /* RiveSlider.swift */; };
C324DB5D280728690060589F /* RiveButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = C324DB5C280728690060589F /* RiveButton.swift */; };
Expand Down Expand Up @@ -228,6 +232,10 @@
04E51C3E2A151A1F0075E473 /* Example__macOS_.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Example__macOS_.entitlements; sourceTree = "<group>"; };
04F1C80826A8442300CEE6BE /* two_bone_ik.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = two_bone_ik.riv; sourceTree = "<group>"; };
27108F2C282C96E700A99D81 /* light_switch.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = light_switch.riv; sourceTree = "<group>"; };
83C89ACA29886ECB00044C17 /* StressTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StressTest.swift; sourceTree = "<group>"; };
83C89ACE2988709400044C17 /* marty.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = marty.riv; sourceTree = "<group>"; };
83C89AD0298870A700044C17 /* paper.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = paper.riv; sourceTree = "<group>"; };
83DE4CB42AB3974300B88B72 /* Bear.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = Bear.riv; sourceTree = "<group>"; };
C324DB5528071EB80060589F /* RiveSwitch.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RiveSwitch.swift; sourceTree = "<group>"; };
C324DB5A2807216B0060589F /* RiveSlider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RiveSlider.swift; sourceTree = "<group>"; };
C324DB5C280728690060589F /* RiveButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RiveButton.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -303,6 +311,7 @@
042C888F2644250D00E7DBB2 /* MultipleAnimations.swift */,
046AFA722673B00B004ED497 /* BlendModes.swift */,
04C4C83D2646FE410047E614 /* StateMachine.swift */,
83C89ACA29886ECB00044C17 /* StressTest.swift */,
);
path = Storyboard;
sourceTree = "<group>";
Expand Down Expand Up @@ -369,6 +378,9 @@
C9696B0E24FC6FD10041502A /* Assets */ = {
isa = PBXGroup;
children = (
83DE4CB42AB3974300B88B72 /* Bear.riv */,
83C89AD0298870A700044C17 /* paper.riv */,
83C89ACE2988709400044C17 /* marty.riv */,
C3C074F1284161E400E8EB33 /* halloween.riv */,
C3C074A7283FC75800E8EB33 /* testanimation.riv */,
C3C074AA283FC75900E8EB33 /* teststatemachine.riv */,
Expand Down Expand Up @@ -672,6 +684,7 @@
042C88E62644447500E7DBB2 /* artboard_animations.riv in Resources */,
042C88E72644447500E7DBB2 /* trailblaze.riv in Resources */,
C3D187F728075B6C008B739A /* riveslider.riv in Resources */,
83C89ACF2988709400044C17 /* marty.riv in Resources */,
C3ECAC2C281837B300A81123 /* leg_day_events_example.riv in Resources */,
042C88E12644447500E7DBB2 /* basketball.riv in Resources */,
042C88DE2644447500E7DBB2 /* rope.riv in Resources */,
Expand All @@ -692,8 +705,10 @@
046AFA712673AF04004ED497 /* blendmodes.riv in Resources */,
042C88EC2644447500E7DBB2 /* vader.riv in Resources */,
E5964AB12A9CDB2100140479 /* rating_animation.riv in Resources */,
83C89AD1298870A700044C17 /* paper.riv in Resources */,
C9C73E9E24FC471E00EF9516 /* Assets.xcassets in Resources */,
0450446126B3F71E007B25CA /* constrained.riv in Resources */,
83DE4CB52AB397A800B88B72 /* Bear.riv in Resources */,
C9D3DE68264F49F4001BA265 /* life_bar.riv in Resources */,
042C88DB2644447500E7DBB2 /* wacky.riv in Resources */,
E5A7874A27E115170056F24B /* energy_bar_example.riv in Resources */,
Expand Down Expand Up @@ -731,6 +746,7 @@
042C88902644250D00E7DBB2 /* MultipleAnimations.swift in Sources */,
C3468E6227FDCBC6008652FD /* SimpleSlider.swift in Sources */,
C3C074EE28414F4600E8EB33 /* SwiftTestParityAnimSM.swift in Sources */,
83C89ACB29886ECB00044C17 /* StressTest.swift in Sources */,
C3357CA1280F42EC00F03B6F /* ExamplesMaster.swift in Sources */,
C324DB5628071EB80060589F /* RiveSwitch.swift in Sources */,
C3ECAC272817BE4600A81123 /* SwiftTouchEvents.swift in Sources */,
Expand Down
86 changes: 86 additions & 0 deletions Example-iOS/Source/Examples/Storyboard/StressTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// StressTest.swift
// RiveExample
//
// Created by Chris Dalton on 1/30/23.
// Copyright © 2023 Rive. All rights reserved.
//

import UIKit
import RiveRuntime
import SwiftUI

// Example to test drawing multiple times within a single view
class StressTestViewController: UIViewController {
var viewModel: RiveViewModel?
var rView: CustomRiveView?

@objc func onTap(_ sender:UITapGestureRecognizer) {
if let riveView = rView {
riveView.drawRepeat += 3;
self.title = "Stress Test (x\(riveView.drawRepeat))"
}
}

override func viewWillAppear(_ animated: Bool) {
let rModel = try! RiveModel(fileName: "marty", extension: ".riv", in: .main)
rView = CustomRiveView(model: rModel, autoPlay: true)
viewModel = RiveViewModel(rModel, animationName: "Animation2")
viewModel!.fit = RiveFit.contain
viewModel!.setView(rView!)
view.addSubview(rView!)
let f = view.frame
let h = UIApplication.shared.statusBarFrame.height + 40
rView!.frame = CGRect(x:f.minX, y:f.minY + h, width:f.width, height:f.height - h)

let gesture = UITapGestureRecognizer(target: self, action: #selector (self.onTap (_:)))
rView!.addGestureRecognizer(gesture)

rView!.showFPS = true
}
}

// New RiveView that overrides the drawing logic to re-draw the view multiple times.
class CustomRiveView: RiveView {
private var rModel: RiveModel?
public var drawRepeat: Int32 = 1
init(model: RiveModel, autoPlay: Bool = true) {
super.init()
rModel = model
}

required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func drawRive(_ rect: CGRect, size: CGSize) {
// This prevents breaking when loading RiveFile async
guard let artboard = rModel?.artboard else { return }

let newFrame = CGRect(origin: rect.origin, size: size)
align(with: newFrame, contentRect: artboard.bounds(), alignment: .center, fit: .contain)

// Find a delta by which to advance our animation each repeated draw, that will loop back
// around to the original starting point once we're done drawing.
var delta = 0.0
if let animation = rModel?.animation {
delta = Double(animation.effectiveDurationInSeconds()) / Double(drawRepeat)
}
let pad:Float = 100.0
let r = min(drawRepeat, 8)
let x0:Float = Float(r - 1) * 0.5 * -pad
var x:Float = x0
var y:Float = -pad * 4
for i in 1...drawRepeat {
if (i & 0x7) == 0 {
y += pad
x = x0
}
save()
transform(1, xy:0, yx:0, yy:1, tx:x, ty:y);
draw(with: artboard)
restore()
rModel?.animation?.advance(by: delta)
x += pad
}
}
}
3 changes: 2 additions & 1 deletion Example-iOS/Source/ExamplesMaster.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class ExamplesMasterTableViewController: UITableViewController {
"MultipleAnimations",
"State Machine",
"Blend Mode",
"Slider Widget"
"Slider Widget",
"Stress Test"
]


Expand Down
16 changes: 16 additions & 0 deletions Example-iOS/Source/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,22 @@
</objects>
<point key="canvasLocation" x="5304" y="-358"/>
</scene>
<!--Stress Test Example-->
<scene sceneID="BJl-SY-XuL">
<objects>
<viewController storyboardIdentifier="Stress Test" title="Stress Test Example" id="z8m-Vh-cMa" customClass="StressTestViewController" customModule="RiveExample" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="DfU-QA-wiO">
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<viewLayoutGuide key="safeArea" id="i01-I9-9w7"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
</view>
<navigationItem key="navigationItem" title="Stress Test" id="bxs-ia-TOh"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="LZR-GS-Huz" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="2377" y="-358"/>
</scene>
</scenes>
<resources>
<systemColor name="systemBackgroundColor">
Expand Down
3 changes: 3 additions & 0 deletions Source/Renderer/include/rive_renderer_view.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
contentRect:(CGRect)contentRect
alignment:(RiveAlignment)alignment
fit:(RiveFit)fit;
- (void)save;
- (void)restore;
- (void)transform:(float)xx xy:(float)xy yx:(float)yx yy:(float)yy tx:(float)tx ty:(float)ty;
- (void)drawWithArtboard:(RiveArtboard*)artboard;
- (void)drawRive:(CGRect)rect size:(CGSize)size;
- (bool)isPaused;
Expand Down
18 changes: 18 additions & 0 deletions Source/Renderer/rive_renderer_view.mm
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,27 @@ - (void)alignWithRect:(CGRect)rect

_renderer->align(riveFit, riveAlignment, frame, content);
}
- (void)save
{
assert(_renderer != nil);
_renderer->save();
}

- (void)restore
{
assert(_renderer != nil);
_renderer->restore();
}

- (void)transform:(float)xx xy:(float)xy yx:(float)yx yy:(float)yy tx:(float)tx ty:(float)ty
{
assert(_renderer != nil);
_renderer->transform(rive::Mat2D{xx, xy, yx, yy, tx, ty});
}

- (void)drawWithArtboard:(RiveArtboard*)artboard
{
assert(_renderer != nil);
[artboard artboardInstance]->draw(_renderer);
}

Expand Down
6 changes: 3 additions & 3 deletions Source/RiveView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ open class RiveView: RiveRendererView {
// Trigger a redraw
needsDisplay()
}

/// This is called in the middle of drawRect
override public func drawRive(_ rect: CGRect, size: CGSize) {
/// This is called in the middle of drawRect. Override this method to implement
/// custom draw logic
override open func drawRive(_ rect: CGRect, size: CGSize) {
// This prevents breaking when loading RiveFile async
guard let artboard = riveModel?.artboard else { return }

Expand Down

0 comments on commit afbd9e2

Please sign in to comment.