2019-09-09 21:03:38 +00:00
|
|
|
// Copyright 2019 Google LLC.
|
2019-09-16 20:17:21 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
2019-09-09 21:03:38 +00:00
|
|
|
|
2019-12-18 21:26:19 +00:00
|
|
|
#include "tools/skottie_ios_app/SkiaContext.h"
|
|
|
|
#include "tools/skottie_ios_app/SkottieViewController.h"
|
|
|
|
|
2019-12-06 20:01:08 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2019-09-09 21:03:38 +00:00
|
|
|
|
2019-12-18 21:26:19 +00:00
|
|
|
#import <UIKit/UIKit.h>
|
2019-12-06 20:01:08 +00:00
|
|
|
|
2019-12-18 21:26:19 +00:00
|
|
|
#include <cstdlib>
|
2019-09-09 21:03:38 +00:00
|
|
|
|
2019-12-18 21:26:19 +00:00
|
|
|
@interface AppViewController : UIViewController
|
|
|
|
@property (strong) SkiaContext* skiaContext;
|
|
|
|
@property (strong) UIStackView* stackView;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AppViewController
|
|
|
|
|
|
|
|
- (void)loadView {
|
|
|
|
[self setView:[[UIView alloc] init]];
|
|
|
|
}
|
2019-09-09 21:03:38 +00:00
|
|
|
|
2019-12-18 21:26:19 +00:00
|
|
|
- (void)viewDidLoad {
|
|
|
|
[super viewDidLoad];
|
|
|
|
if (![self skiaContext]) {
|
|
|
|
#if (SK_SUPPORT_GPU && defined(SK_METAL) && !defined(SK_BUILD_FOR_GOOGLE3))
|
|
|
|
[self setSkiaContext:MakeSkiaMetalContext()];
|
|
|
|
#elif (SK_SUPPORT_GPU && defined(SK_GL) && !defined(SK_BUILD_FOR_GOOGLE3))
|
|
|
|
[self setSkiaContext:MakeSkiaGLContext()];
|
|
|
|
#else
|
|
|
|
[self setSkiaContext:MakeSkiaUIContext()];
|
|
|
|
#endif
|
|
|
|
if (![self skiaContext]) {
|
|
|
|
NSLog(@"abort: failed to make skia context.");
|
|
|
|
std::abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
|
2019-12-06 20:01:08 +00:00
|
|
|
|
2019-09-09 21:03:38 +00:00
|
|
|
UIStackView* stack = [[UIStackView alloc] init];
|
|
|
|
[stack setAxis:UILayoutConstraintAxisVertical];
|
|
|
|
[stack setDistribution:UIStackViewDistributionEqualSpacing];
|
|
|
|
|
|
|
|
NSBundle* mainBundle = [NSBundle mainBundle];
|
|
|
|
NSArray<NSString*>* paths = [mainBundle pathsForResourcesOfType:@"json"
|
2019-09-26 18:29:51 +00:00
|
|
|
inDirectory:@"data"];
|
2019-09-09 21:03:38 +00:00
|
|
|
constexpr CGFloat kSpacing = 2;
|
|
|
|
CGFloat totalHeight = kSpacing;
|
|
|
|
for (NSUInteger i = 0; i < [paths count]; ++i) {
|
|
|
|
NSString* path = [paths objectAtIndex:i];
|
|
|
|
NSData* content = [NSData dataWithContentsOfFile:path];
|
|
|
|
if (!content) {
|
|
|
|
NSLog(@"'%@' not found", path);
|
|
|
|
continue;
|
|
|
|
}
|
2019-12-18 21:26:19 +00:00
|
|
|
SkottieViewController* controller = [[SkottieViewController alloc] init];
|
|
|
|
if (![controller loadAnimation:content]) {
|
2019-09-09 21:03:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-12-18 21:26:19 +00:00
|
|
|
CGSize animSize = [controller size];
|
|
|
|
CGFloat height = animSize.width ? (screenWidth * animSize.height / animSize.width) : 0;
|
|
|
|
CGRect frame = {{0, 0}, {screenWidth, height}};
|
|
|
|
UIView* skiaView = [[self skiaContext] makeViewWithController:controller withFrame:frame];
|
|
|
|
[[[skiaView heightAnchor] constraintEqualToConstant:height] setActive:true];
|
|
|
|
[[[skiaView widthAnchor] constraintEqualToConstant:screenWidth] setActive:true];
|
|
|
|
[skiaView setNeedsDisplay];
|
|
|
|
[stack addArrangedSubview:skiaView];
|
2019-09-09 21:03:38 +00:00
|
|
|
totalHeight += height + kSpacing;
|
|
|
|
}
|
2019-12-18 21:26:19 +00:00
|
|
|
[stack setFrame:{{0, 0}, {screenWidth, totalHeight}}];
|
|
|
|
[stack setNeedsDisplay];
|
2019-09-09 21:03:38 +00:00
|
|
|
|
|
|
|
CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
|
|
|
|
CGSize mainScreenSize = [[UIScreen mainScreen] bounds].size;
|
|
|
|
CGRect scrollViewBounds = {{0, statusBarHeight},
|
|
|
|
{mainScreenSize.width, mainScreenSize.height - statusBarHeight}};
|
|
|
|
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:scrollViewBounds];
|
2019-12-18 21:26:19 +00:00
|
|
|
[scrollView setContentSize:[stack frame].size];
|
|
|
|
[scrollView addSubview:stack];
|
2019-09-09 21:03:38 +00:00
|
|
|
[scrollView setBackgroundColor:[UIColor blackColor]];
|
2019-12-18 21:26:19 +00:00
|
|
|
[scrollView setNeedsDisplay];
|
|
|
|
|
|
|
|
[self setStackView:stack];
|
2019-09-09 21:03:38 +00:00
|
|
|
|
|
|
|
UIView* mainView = [self view];
|
|
|
|
[mainView setBounds:{{0, 0}, mainScreenSize}];
|
|
|
|
[mainView setBackgroundColor:[UIColor whiteColor]];
|
|
|
|
[mainView addSubview:scrollView];
|
2019-12-18 21:26:19 +00:00
|
|
|
[mainView setNeedsDisplay];
|
2019-09-09 21:03:38 +00:00
|
|
|
|
|
|
|
UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] init];
|
|
|
|
[tapGestureRecognizer addTarget:self action:@selector(handleTap:)];
|
|
|
|
[mainView addGestureRecognizer:tapGestureRecognizer];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)handleTap:(UIGestureRecognizer*)sender {
|
2019-12-17 15:01:42 +00:00
|
|
|
if ([sender state] != UIGestureRecognizerStateEnded) {
|
2019-09-09 21:03:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
NSArray<UIView*>* subviews = [[self stackView] subviews];
|
|
|
|
for (NSUInteger i = 0; i < [subviews count]; ++i) {
|
2019-12-18 21:26:19 +00:00
|
|
|
UIView* uIView = [subviews objectAtIndex:i];
|
|
|
|
if (SkiaViewController* controller = [[self skiaContext] getViewController:uIView]) {
|
|
|
|
[controller togglePaused];
|
|
|
|
[uIView setNeedsDisplay];
|
2019-09-09 21:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface AppDelegate : UIResponder <UIApplicationDelegate>
|
2019-12-18 21:26:19 +00:00
|
|
|
@property (strong, nonatomic) UIWindow* window;
|
2019-09-09 21:03:38 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation AppDelegate
|
|
|
|
|
|
|
|
- (BOOL)application:(UIApplication*)app didFinishLaunchingWithOptions:(NSDictionary*)ops {
|
|
|
|
[self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
|
|
|
|
[[self window] setRootViewController:[[AppViewController alloc] init]];
|
|
|
|
[[self window] makeKeyAndVisible];
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
@autoreleasepool {
|
|
|
|
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
|
|
|
|
}
|
|
|
|
}
|