Hal Canary | 4119443 | 2019-09-09 17:03:38 -0400 | [diff] [blame] | 1 | // Copyright 2019 Google LLC. |
Brian Osman | b47704b | 2019-09-16 16:17:21 -0400 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
Hal Canary | 4119443 | 2019-09-09 17:03:38 -0400 | [diff] [blame] | 3 | |
Hal Canary | 118df7c | 2019-12-18 16:26:19 -0500 | [diff] [blame] | 4 | #include "tools/skottie_ios_app/SkiaContext.h" |
| 5 | #include "tools/skottie_ios_app/SkottieViewController.h" |
| 6 | |
Hal Canary | 2748306 | 2019-12-06 15:01:08 -0500 | [diff] [blame] | 7 | #include "include/core/SkTypes.h" |
Hal Canary | 4119443 | 2019-09-09 17:03:38 -0400 | [diff] [blame] | 8 | |
Hal Canary | 4119443 | 2019-09-09 17:03:38 -0400 | [diff] [blame] | 9 | #import <UIKit/UIKit.h> |
| 10 | |
Hal Canary | 118df7c | 2019-12-18 16:26:19 -0500 | [diff] [blame] | 11 | #include <cstdlib> |
Hal Canary | 2748306 | 2019-12-06 15:01:08 -0500 | [diff] [blame] | 12 | |
Hal Canary | 118df7c | 2019-12-18 16:26:19 -0500 | [diff] [blame] | 13 | @interface AppViewController : UIViewController |
| 14 | @property (strong) SkiaContext* skiaContext; |
| 15 | @property (strong) UIStackView* stackView; |
| 16 | @end |
| 17 | |
| 18 | @implementation AppViewController |
| 19 | |
| 20 | - (void)loadView { |
| 21 | [self setView:[[UIView alloc] init]]; |
| 22 | } |
| 23 | |
| 24 | - (void)viewDidLoad { |
| 25 | [super viewDidLoad]; |
| 26 | if (![self skiaContext]) { |
| 27 | #if (SK_SUPPORT_GPU && defined(SK_METAL) && !defined(SK_BUILD_FOR_GOOGLE3)) |
| 28 | [self setSkiaContext:MakeSkiaMetalContext()]; |
| 29 | #elif (SK_SUPPORT_GPU && defined(SK_GL) && !defined(SK_BUILD_FOR_GOOGLE3)) |
| 30 | [self setSkiaContext:MakeSkiaGLContext()]; |
| 31 | #else |
| 32 | [self setSkiaContext:MakeSkiaUIContext()]; |
| 33 | #endif |
| 34 | if (![self skiaContext]) { |
| 35 | NSLog(@"abort: failed to make skia context."); |
| 36 | std::abort(); |
| 37 | } |
| 38 | } |
| 39 | CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width; |
| 40 | |
Hal Canary | 4119443 | 2019-09-09 17:03:38 -0400 | [diff] [blame] | 41 | UIStackView* stack = [[UIStackView alloc] init]; |
| 42 | [stack setAxis:UILayoutConstraintAxisVertical]; |
| 43 | [stack setDistribution:UIStackViewDistributionEqualSpacing]; |
| 44 | |
| 45 | NSBundle* mainBundle = [NSBundle mainBundle]; |
| 46 | NSArray<NSString*>* paths = [mainBundle pathsForResourcesOfType:@"json" |
Hal Canary | 988b1e4 | 2019-09-26 14:29:51 -0400 | [diff] [blame] | 47 | inDirectory:@"data"]; |
Hal Canary | 4119443 | 2019-09-09 17:03:38 -0400 | [diff] [blame] | 48 | constexpr CGFloat kSpacing = 2; |
| 49 | CGFloat totalHeight = kSpacing; |
| 50 | for (NSUInteger i = 0; i < [paths count]; ++i) { |
| 51 | NSString* path = [paths objectAtIndex:i]; |
| 52 | NSData* content = [NSData dataWithContentsOfFile:path]; |
| 53 | if (!content) { |
| 54 | NSLog(@"'%@' not found", path); |
| 55 | continue; |
| 56 | } |
Hal Canary | 118df7c | 2019-12-18 16:26:19 -0500 | [diff] [blame] | 57 | SkottieViewController* controller = [[SkottieViewController alloc] init]; |
| 58 | if (![controller loadAnimation:content]) { |
Hal Canary | 4119443 | 2019-09-09 17:03:38 -0400 | [diff] [blame] | 59 | continue; |
| 60 | } |
Hal Canary | 118df7c | 2019-12-18 16:26:19 -0500 | [diff] [blame] | 61 | CGSize animSize = [controller size]; |
| 62 | CGFloat height = animSize.width ? (screenWidth * animSize.height / animSize.width) : 0; |
| 63 | CGRect frame = {{0, 0}, {screenWidth, height}}; |
| 64 | UIView* skiaView = [[self skiaContext] makeViewWithController:controller withFrame:frame]; |
| 65 | [[[skiaView heightAnchor] constraintEqualToConstant:height] setActive:true]; |
| 66 | [[[skiaView widthAnchor] constraintEqualToConstant:screenWidth] setActive:true]; |
| 67 | [skiaView setNeedsDisplay]; |
| 68 | [stack addArrangedSubview:skiaView]; |
Hal Canary | 4119443 | 2019-09-09 17:03:38 -0400 | [diff] [blame] | 69 | totalHeight += height + kSpacing; |
| 70 | } |
Hal Canary | 118df7c | 2019-12-18 16:26:19 -0500 | [diff] [blame] | 71 | [stack setFrame:{{0, 0}, {screenWidth, totalHeight}}]; |
| 72 | [stack setNeedsDisplay]; |
Hal Canary | 4119443 | 2019-09-09 17:03:38 -0400 | [diff] [blame] | 73 | |
| 74 | CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height; |
| 75 | CGSize mainScreenSize = [[UIScreen mainScreen] bounds].size; |
| 76 | CGRect scrollViewBounds = {{0, statusBarHeight}, |
| 77 | {mainScreenSize.width, mainScreenSize.height - statusBarHeight}}; |
| 78 | UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:scrollViewBounds]; |
Hal Canary | 118df7c | 2019-12-18 16:26:19 -0500 | [diff] [blame] | 79 | [scrollView setContentSize:[stack frame].size]; |
| 80 | [scrollView addSubview:stack]; |
Hal Canary | 4119443 | 2019-09-09 17:03:38 -0400 | [diff] [blame] | 81 | [scrollView setBackgroundColor:[UIColor blackColor]]; |
Hal Canary | 118df7c | 2019-12-18 16:26:19 -0500 | [diff] [blame] | 82 | [scrollView setNeedsDisplay]; |
| 83 | |
| 84 | [self setStackView:stack]; |
Hal Canary | 4119443 | 2019-09-09 17:03:38 -0400 | [diff] [blame] | 85 | |
| 86 | UIView* mainView = [self view]; |
| 87 | [mainView setBounds:{{0, 0}, mainScreenSize}]; |
| 88 | [mainView setBackgroundColor:[UIColor whiteColor]]; |
| 89 | [mainView addSubview:scrollView]; |
Hal Canary | 118df7c | 2019-12-18 16:26:19 -0500 | [diff] [blame] | 90 | [mainView setNeedsDisplay]; |
Hal Canary | 4119443 | 2019-09-09 17:03:38 -0400 | [diff] [blame] | 91 | |
| 92 | UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] init]; |
| 93 | [tapGestureRecognizer addTarget:self action:@selector(handleTap:)]; |
| 94 | [mainView addGestureRecognizer:tapGestureRecognizer]; |
| 95 | } |
| 96 | |
| 97 | - (void)handleTap:(UIGestureRecognizer*)sender { |
Hal Canary | 719a811 | 2019-12-17 10:01:42 -0500 | [diff] [blame] | 98 | if ([sender state] != UIGestureRecognizerStateEnded) { |
Hal Canary | 4119443 | 2019-09-09 17:03:38 -0400 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | NSArray<UIView*>* subviews = [[self stackView] subviews]; |
| 102 | for (NSUInteger i = 0; i < [subviews count]; ++i) { |
Hal Canary | 118df7c | 2019-12-18 16:26:19 -0500 | [diff] [blame] | 103 | UIView* uIView = [subviews objectAtIndex:i]; |
| 104 | if (SkiaViewController* controller = [[self skiaContext] getViewController:uIView]) { |
| 105 | [controller togglePaused]; |
| 106 | [uIView setNeedsDisplay]; |
Hal Canary | 4119443 | 2019-09-09 17:03:38 -0400 | [diff] [blame] | 107 | } |
Hal Canary | 4119443 | 2019-09-09 17:03:38 -0400 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | @end |
| 111 | |
| 112 | @interface AppDelegate : UIResponder <UIApplicationDelegate> |
Hal Canary | 118df7c | 2019-12-18 16:26:19 -0500 | [diff] [blame] | 113 | @property (strong, nonatomic) UIWindow* window; |
Hal Canary | 4119443 | 2019-09-09 17:03:38 -0400 | [diff] [blame] | 114 | @end |
| 115 | |
| 116 | @implementation AppDelegate |
| 117 | |
| 118 | - (BOOL)application:(UIApplication*)app didFinishLaunchingWithOptions:(NSDictionary*)ops { |
| 119 | [self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]]; |
| 120 | [[self window] setRootViewController:[[AppViewController alloc] init]]; |
| 121 | [[self window] makeKeyAndVisible]; |
| 122 | return YES; |
| 123 | } |
| 124 | @end |
| 125 | |
| 126 | int main(int argc, char* argv[]) { |
| 127 | @autoreleasepool { |
| 128 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); |
| 129 | } |
| 130 | } |