Skip to content

Commit

Permalink
Fixes loop const cast and background transparency issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mjohnsullivan committed May 7, 2021
1 parent e28bd6e commit 587ebde
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 35 deletions.
2 changes: 2 additions & 0 deletions Example-iOS/Source/SwiftUI/RiveSwiftUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct RiveSwiftUIView: View {
Button(action: { alignment = RiveRuntime.Alignment.BottomRight },
label: { Text("Bottom Right") })
}
.padding()
HStack {
Button(action: { fit = Fit.Contain },
label: { Text("Contain") })
Expand All @@ -46,6 +47,7 @@ struct RiveSwiftUIView: View {
}
.padding()
}
.background(Color.init(white: 0, opacity: 0.75))
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Source/Renderer/Rive.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ typedef NS_ENUM(NSInteger, Alignment) {
- (bool)advanceBy:(double)elapsedSeconds;
- (int)direction;
- (void)direction:(int)direction;
- (int)loop;
- (void)loop:(int)loopMode;

@end

Expand All @@ -110,8 +112,6 @@ typedef NS_ENUM(NSInteger, Alignment) {
- (NSInteger)duration;
- (float)endTime;
- (NSInteger)fps;
- (int)loop;
- (void)loop:(int)loopMode;
- (void)apply:(float)time to:(RiveArtboard *)artboard;

@end
Expand All @@ -121,8 +121,8 @@ typedef NS_ENUM(NSInteger, Alignment) {
*/
@interface RiveRenderer : NSObject

-(instancetype) initWithContext:(nonnull CGContextRef) context;
-(void) alignWithRect:(CGRect)rect withContentRect:(CGRect)contentRect withAlignment:(Alignment)alignment withFit:(Fit)fit;
- (instancetype)initWithContext:(nonnull CGContextRef)context;
- (void)alignWithRect:(CGRect)rect withContentRect:(CGRect)contentRect withAlignment:(Alignment)alignment withFit:(Fit)fit;

@end

Expand Down
47 changes: 25 additions & 22 deletions Source/Renderer/Rive.mm
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ @implementation RiveLinearAnimation {
const rive::LinearAnimation *animation;
}

-(instancetype) initWithAnimation:(const rive::LinearAnimation *) riveAnimation {
- (instancetype)initWithAnimation:(const rive::LinearAnimation *) riveAnimation {
if (self = [super init]) {
animation = riveAnimation;
return self;
Expand All @@ -308,43 +308,38 @@ - (NSString *)name {
return [NSString stringWithCString:str.c_str() encoding:[NSString defaultCStringEncoding]];
}

-(RiveLinearAnimationInstance *) instance {
- (RiveLinearAnimationInstance *)instance {
return [[RiveLinearAnimationInstance alloc] initWithAnimation: animation];
}

-(NSInteger) workStart {
- (NSInteger)workStart {
return animation->workStart();
}

-(NSInteger) workEnd {
- (NSInteger)workEnd {
return animation->workEnd();
}

-(NSInteger) duration {
- (NSInteger)duration {
return animation->duration();
}

-(float) endTime {
- (float)endTime {
if (animation->enableWorkArea()){
return animation->workEnd()/animation->fps();
}
return animation->duration()/animation->fps();
}

-(NSInteger) fps {
- (NSInteger)fps {
return animation->fps();
}

-(void) apply:(float) time to:(RiveArtboard *) artboard {
- (void)apply:(float)time to:(RiveArtboard *)artboard {
animation->apply(artboard.artboard, time);
}

-(void) loop:(int)loopType {
// TODO: fix this. we shoudln't be casting the const out of this.
((rive::LinearAnimation*) animation)->loopValue(loopType);
}

-(int) loop {
- (int)loop {
return animation->loopValue();
}

Expand All @@ -357,7 +352,7 @@ @implementation RiveLinearAnimationInstance {
rive::LinearAnimationInstance *instance;
}

-(instancetype) initWithAnimation:(const rive::LinearAnimation *) riveAnimation {
- (instancetype)initWithAnimation:(const rive::LinearAnimation *)riveAnimation {
if (self = [super init]) {
instance = new rive::LinearAnimationInstance(riveAnimation);
return self;
Expand All @@ -366,33 +361,41 @@ -(instancetype) initWithAnimation:(const rive::LinearAnimation *) riveAnimation
}
}

-(RiveLinearAnimation *) animation {
- (RiveLinearAnimation *)animation {
const rive::LinearAnimation *linearAnimation = instance->animation();
return [[RiveLinearAnimation alloc] initWithAnimation: linearAnimation];
}

-(float) time {
- (float)time {
return instance->time();
}

-(void) setTime:(float) time {
- (void)setTime:(float) time {
instance->time(time);
}

-(void) applyTo:(RiveArtboard*) artboard {
- (void)applyTo:(RiveArtboard*) artboard {
instance->apply(artboard.artboard);
}

-(bool) advanceBy:(double)elapsedSeconds {
- (bool)advanceBy:(double)elapsedSeconds {
return instance->advance(elapsedSeconds);
}
-(void) direction:(int)direction {
- (void)direction:(int)direction {
instance->direction(direction);
}
-(int) direction{
- (int)direction {
return instance->direction();
}

- (int)loop {
return instance->loopValue();
}

- (void)loop:(int)loopType {
instance->loopValue(loopType);
}

@end

/*
Expand Down
19 changes: 11 additions & 8 deletions Source/Views/RiveView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ public class RiveView: UIView {
}

public init() {
super.init(frame: .zero)
super.init(frame: .zero)
}

required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
super.init(coder: aDecoder)
}


Expand All @@ -63,6 +63,9 @@ public class RiveView: UIView {
andAnimation animation: String?=nil,
andAutoPlay autoPlay: Bool=true
) {
// Ensure the view's transparent
self.isOpaque = false

self.riveFile = riveFile
self.autoPlay = autoPlay

Expand All @@ -82,7 +85,7 @@ public class RiveView: UIView {

// Advance the artboard, this will ensure the first
// frame is displayed when the artboard is drawn
// artboard.advance(by: 0)
// artboard.advance(by: 0)

// Start the animation loop
if autoPlay {
Expand All @@ -103,9 +106,9 @@ public class RiveView: UIView {
guard let context = UIGraphicsGetCurrentContext(), let artboard = self.artboard else {
return
}
let renderer = RiveRenderer(context: context);
renderer.align(with: rect, withContentRect: artboard.bounds(), with: alignment, with: fit)
artboard.draw(renderer)
let renderer = RiveRenderer(context: context);
renderer.align(with: rect, withContentRect: artboard.bounds(), with: alignment, with: fit)
artboard.draw(renderer)
}

// Starts the animation timer
Expand Down Expand Up @@ -179,7 +182,7 @@ public class RiveView: UIView {
animation.apply(to: artboard)
if !stillPlaying {
playingAnimations.remove(animation)
if (animation.animation().loop() == Loop.LoopOneShot.rawValue) {
if (animation.loop() == Loop.LoopOneShot.rawValue) {
animations.removeAll(where: {animationInstance in
return animationInstance == animation
})
Expand Down Expand Up @@ -284,7 +287,7 @@ public class RiveView: UIView {
direction: Direction
) {
if (loop != Loop.LoopAuto) {
animationInstance.animation().loop(Int32(loop.rawValue))
animationInstance.loop(Int32(loop.rawValue))
}
if (!animations.contains(animationInstance)) {
if (direction == Direction.DirectionBackwards) {
Expand Down
2 changes: 1 addition & 1 deletion submodules/rive-cpp

0 comments on commit 587ebde

Please sign in to comment.