Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds clean up and deallocation #61

Merged
merged 2 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Example-iOS/Source/SwiftUI/Button/RiveButtonBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ struct RiveButtonBridge: UIViewRepresentable {
play ? riveView.play() : riveView.pause()
}

static func dismantleUIView(_ riveView: RiveView, coordinator: Self.Coordinator) {
riveView.stop()
}

func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
Expand Down
5 changes: 4 additions & 1 deletion Example-iOS/Source/SwiftUI/Explorer/RiveExplorerBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ struct RiveExplorerBridge: UIViewRepresentable {
if uiView.animationNames().contains(playAnimation) {
uiView.play(animationName: playAnimation)
} else if uiView.stateMachineNames().contains(playAnimation) {
print("3")
uiView.play(animationName: playAnimation, isStateMachine: true)
}
} else {
Expand All @@ -136,6 +135,10 @@ struct RiveExplorerBridge: UIViewRepresentable {
}
}

static func dismantleUIView(_ uiView: RiveView, coordinator: Self.Coordinator) {
uiView.stop()
}

// Constructs a coordinator for managing updating state
func makeCoordinator() -> Coordinator {
Coordinator(controller: controller, loopAction: loopAction, playAction: playAction, pauseAction: pauseAction, inputsAction: inputsAction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ struct RiveProgressBarBridge: UIViewRepresentable {

// Always keep the 100 set; just how this state machine works
riveView.setBooleanState(stateMachine, inputName: input100Name, value: true)

return riveView
}


static func dismantleUIView(_ riveView: RiveView, coordinator: Self.Coordinator) {
riveView.stop()
}

func updateUIView(_ riveView: RiveView, context: UIViewRepresentableContext<RiveProgressBarBridge>) {
riveView.setBooleanState(stateMachine, inputName: input75Name, value: health < 100)
Expand Down
4 changes: 4 additions & 0 deletions Example-iOS/Source/SwiftUI/Switch/RiveSwitchBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ struct RiveSwitchBridge: UIViewRepresentable {
riveView.play(animationName: offAnimation)
}
}

static func dismantleUIView(_ riveView: RiveView, coordinator: Self.Coordinator) {
riveView.stop()
}
}
5 changes: 5 additions & 0 deletions Example-iOS/Source/UIkit/Layout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,9 @@ class LayoutViewController: UIViewController {
layoutView.fitButtonAction = setFit
layoutView.alignmentButtonAction = setAlignmnet
}

override public func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
(view as! LayoutView).riveView.stop()
}
}
7 changes: 5 additions & 2 deletions Example-iOS/Source/UIkit/LoopMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,11 @@ class LoopModeController: UIViewController {
loopModeView.triggeredLtrPingPongButton = {
loopModeView.riveView.play(animationName:"pingpong", loop: Loop.loopPingPong, direction: self.direction)
}


}

override public func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
(view as! LoopMode).riveView.stop()
}
}

9 changes: 9 additions & 0 deletions Example-iOS/Source/UIkit/MultipleAnimations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,13 @@ class MultipleAnimationsController: UIViewController {
andArtboard: "Star"
)
}

override public func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
(view as! MultipleAnimations).squareGoAround.stop()
(view as! MultipleAnimations).squareRollAround.stop()
(view as! MultipleAnimations).circle.stop()
(view as! MultipleAnimations).star.stop()

}
}
6 changes: 6 additions & 0 deletions Example-iOS/Source/UIkit/SimpleAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ class SimpleAnimationViewController: UIViewController {
super.loadView()

let view = RiveView()
view.fit = Fit.fitCover
guard let riveFile = RiveFile(resource: resourceName) else {
fatalError("Failed to load RiveFile")
}

view.configure(riveFile)
self.view = view
}

override public func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
(view as! RiveView).stop()
}
}

/*
Expand Down
5 changes: 5 additions & 0 deletions Example-iOS/Source/UIkit/StateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ class StateMachineViewController: UIViewController {
)
}
}

override public func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
(view as! StateMachineView).riveView.stop()
}
}
5 changes: 5 additions & 0 deletions Example-iOS/Source/UIkit/iosPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,9 @@ class IOSPlayerViewController: UIViewController {
load(name:fileChoices.chosen)

}

override public func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
(view as! IOSPlayerView).riveView.stop()
}
}
4 changes: 2 additions & 2 deletions RiveRuntime.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2328,7 +2328,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.6;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -2387,7 +2387,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.6;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = iphoneos;
Expand Down
5 changes: 5 additions & 0 deletions Source/Renderer/RiveFile.mm
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,9 @@ - (NSArray *)artboardNames {
return artboardNames;
}

/// Clean up rive file
- (void)dealloc {
delete riveFile;
}

@end
5 changes: 5 additions & 0 deletions Source/Renderer/RiveLinearAnimationInstance.mm
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ - (NSString *)name {
std::string str = instance->animation()->name();
return [NSString stringWithCString:str.c_str() encoding:[NSString defaultCStringEncoding]];
}

- (void)dealloc {
delete instance;
}

@end
2 changes: 1 addition & 1 deletion Source/Renderer/RiveRenderer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
*/

RiveRenderer::~RiveRenderer() {
// NSLog(@"Releasing renderer c++");
// NSLog(@"Releasing renderer c++");
}

void RiveRenderer::save() {
Expand Down
51 changes: 45 additions & 6 deletions Source/Renderer/RiveStateMachineInstance.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
#import <Rive.h>
#import <RivePrivateHeaders.h>

@interface RiveStateMachineInstance ()

/// Holds references to SMIInputs
@property NSMutableDictionary *inputs;

@end

/*
* RiveStateMachineInstance
*/
Expand All @@ -22,6 +29,7 @@ - (instancetype)initWithStateMachine:(const rive::StateMachine *)stateMachine {
if (self = [super init]) {
self->stateMachine = stateMachine;
instance = new rive::StateMachineInstance(stateMachine);
_inputs = [[NSMutableDictionary alloc] init];
return self;
} else {
return nil;
Expand All @@ -38,33 +46,60 @@ - (RiveStateMachine *)stateMachine {
return [[RiveStateMachine alloc] initWithStateMachine: stateMachine];
}

- (RiveSMIBool *)getBool:(NSString *) name {
- (RiveSMIBool *)getBool:(NSString *)name {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

talk me through this today :D i think i get it i just wanna be sure. i guess we call alloc. without freeing it so its bad?

// Create a unique dictionary name for numbers;
// this lets us use one dictionary for the three different types
NSString * dictName = [NSString stringWithFormat:@"%@%s", name, "_boo"];
// Check if the input is already instanced
if ([_inputs objectForKey:dictName] != nil) {
return _inputs[dictName];
}
// Otherwise, try to retrieve from runtime
std::string stdName = std::string([name UTF8String]);
rive::SMIBool *smi = instance->getBool(stdName);
if (smi == nullptr) {
return NULL;
} else {
return [[RiveSMIBool alloc] initWithSMIInput: smi];
_inputs[dictName] = [[RiveSMIBool alloc] initWithSMIInput: smi];
return _inputs[dictName];
}
}

- (RiveSMITrigger *)getTrigger:(NSString *) name {
- (RiveSMITrigger *)getTrigger:(NSString *)name {
// Create a unique dictionary name for numbers;
// this lets us use one dictionary for the three different types
NSString * dictName = [NSString stringWithFormat:@"%@%s", name, "_trg"];
// Check if the input is already instanced
if ([_inputs objectForKey:dictName] != nil) {
return _inputs[dictName];
}
// Otherwise, try to retrieve from runtime
std::string stdName = std::string([name UTF8String]);
rive::SMITrigger *smi = instance->getTrigger(stdName);
if (smi == nullptr) {
return NULL;
} else {
return [[RiveSMITrigger alloc] initWithSMIInput: smi];
_inputs[dictName] = [[RiveSMITrigger alloc] initWithSMIInput: smi];
return _inputs[dictName];
}
}

- (RiveSMINumber *)getNumber:(NSString *) name {
- (RiveSMINumber *)getNumber:(NSString *)name {
// Create a unique dictionary name for numbers;
// this lets us use one dictionary for the three different types
NSString * dictName = [NSString stringWithFormat:@"%@%s", name, "_num"];
// Check if the input is already instanced
if ([_inputs objectForKey:dictName] != nil) {
return _inputs[dictName];
}
// Otherwise, try to retrieve from runtime
std::string stdName = std::string([name UTF8String]);
rive::SMINumber *smi = instance->getNumber(stdName);
if (smi == nullptr) {
return NULL;
} else {
return [[RiveSMINumber alloc] initWithSMIInput: smi];
_inputs[dictName] = [[RiveSMINumber alloc] initWithSMIInput: smi];;
return _inputs[dictName];
}
}

Expand Down Expand Up @@ -163,4 +198,8 @@ - (NSArray *)stateChanges{
return inputNames;
}

- (void)dealloc {
delete instance;
}

@end
4 changes: 2 additions & 2 deletions Source/Views/RiveView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ extension RiveView {
}

/// Returns a list of valid state machine inputs for any instanced state machine
/// - Returns a list of valid state machine inputs and thir types
/// - Returns a list of valid state machine inputs and their types
open func stateMachineInputs() -> [StateMachineInput] {
var inputs: [StateMachineInput] = []
stateMachines.forEach({ machine in
Expand Down Expand Up @@ -419,7 +419,7 @@ extension RiveView {
let elapsedTime = timestamp - lastTime;
lastTime = timestamp;
advance(delta: elapsedTime)
if(!isPlaying){
if(!isPlaying) {
stopTimer()
}
}
Expand Down