From 0e2443a5409df0dbca7f8f4bb0abb1d085741f41 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Thu, 16 Jul 2020 22:38:01 +0300 Subject: [PATCH] Video avatar fixes --- .../LegacyComponents/TGPhotoAvatarCropView.h | 3 + .../TGPhotoEditorTabController.h | 2 + .../Sources/TGPhotoAvatarCropView.m | 11 ++++ .../Sources/TGPhotoAvatarPreviewController.m | 65 ++++++++++++++++++- .../Sources/TGPhotoEditorController.m | 4 +- .../Sources/TGPhotoEditorTabController.m | 34 ++++++---- .../Sources/TGPhotoPaintController.m | 14 ++-- .../Sources/TGPhotoToolsController.m | 29 ++++----- 8 files changed, 124 insertions(+), 38 deletions(-) diff --git a/submodules/LegacyComponents/PublicHeaders/LegacyComponents/TGPhotoAvatarCropView.h b/submodules/LegacyComponents/PublicHeaders/LegacyComponents/TGPhotoAvatarCropView.h index 2510d6f309..d80f9a63ce 100644 --- a/submodules/LegacyComponents/PublicHeaders/LegacyComponents/TGPhotoAvatarCropView.h +++ b/submodules/LegacyComponents/PublicHeaders/LegacyComponents/TGPhotoAvatarCropView.h @@ -7,6 +7,8 @@ @property (nonatomic, strong) UIImage *image; +@property (nonatomic, readonly) UIView *clipView; + @property (nonatomic, readonly) CGSize originalSize; @property (nonatomic, assign) CGRect cropRect; @property (nonatomic, assign) UIImageOrientation cropOrientation; @@ -37,6 +39,7 @@ - (void)animateTransitionOut; - (void)transitionInFinishedFromCamera:(bool)fromCamera; +- (void)attachEntitiesView; - (void)closeCurtains; - (void)openCurtains; diff --git a/submodules/LegacyComponents/PublicHeaders/LegacyComponents/TGPhotoEditorTabController.h b/submodules/LegacyComponents/PublicHeaders/LegacyComponents/TGPhotoEditorTabController.h index ee21c29ebe..82c1414690 100644 --- a/submodules/LegacyComponents/PublicHeaders/LegacyComponents/TGPhotoEditorTabController.h +++ b/submodules/LegacyComponents/PublicHeaders/LegacyComponents/TGPhotoEditorTabController.h @@ -9,6 +9,8 @@ bool _dismissing; UIView *_transitionView; bool _noTransitionToSnapshot; + + bool _animateScale; } @property (nonatomic, weak) id item; diff --git a/submodules/LegacyComponents/Sources/TGPhotoAvatarCropView.m b/submodules/LegacyComponents/Sources/TGPhotoAvatarCropView.m index ddc7a2d393..26bc80caa5 100644 --- a/submodules/LegacyComponents/Sources/TGPhotoAvatarCropView.m +++ b/submodules/LegacyComponents/Sources/TGPhotoAvatarCropView.m @@ -146,10 +146,19 @@ const CGFloat TGPhotoAvatarCropViewCurtainMargin = 200; UITapGestureRecognizer *tapRecognier = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; [_wrapperView addGestureRecognizer:tapRecognier]; + + _clipView = [[UIView alloc] init]; + _clipView.clipsToBounds = true; + _clipView.userInteractionEnabled = false; + [self addSubview:_clipView]; } return self; } +- (void)attachEntitiesView { + [_entitiesWrapperView addSubview:_fullEntitiesView]; +} + - (void)dealloc { _scrollView.delegate = nil; @@ -644,6 +653,8 @@ const CGFloat TGPhotoAvatarCropViewCurtainMargin = 200; { [self _layoutOverlayViews]; + _clipView.frame = self.bounds; + _flashView.frame = self.bounds; if (_scrollView.superview == nil) diff --git a/submodules/LegacyComponents/Sources/TGPhotoAvatarPreviewController.m b/submodules/LegacyComponents/Sources/TGPhotoAvatarPreviewController.m index 76dbafa2f5..e7edb527d3 100644 --- a/submodules/LegacyComponents/Sources/TGPhotoAvatarPreviewController.m +++ b/submodules/LegacyComponents/Sources/TGPhotoAvatarPreviewController.m @@ -16,6 +16,7 @@ #import "TGMediaPickerGalleryVideoScrubber.h" #import "TGModernGalleryVideoView.h" +#import "TGPhotoEntitiesContainerView.h" #import "TGPhotoPaintController.h" @@ -257,6 +258,50 @@ const CGFloat TGPhotoAvatarPreviewLandscapePanelSize = TGPhotoAvatarPreviewPanel [self.view insertSubview:_transitionView belowSubview:_wrapperView]; } +- (void)animateTransitionIn { + if (self.initialAppearance) { + [super animateTransitionIn]; + return; + } else { + _animateScale = true; + + [self transitEntities:_previewView]; + + [super animateTransitionIn]; + } +} + ++ (CGRect)fittedCropRect:(CGRect)cropRect originalSize:(CGSize)originalSize fitSize:(CGSize)fitSize { + CGSize fittedOriginalSize = TGScaleToSize(originalSize, fitSize); + CGFloat scale = fittedOriginalSize.width / originalSize.width; + + CGSize size = fittedOriginalSize; + + return CGRectMake(-cropRect.origin.x * scale, -cropRect.origin.y * scale, size.width, size.height); +} + +- (void)transitEntities:(UIView *)parentView { + UIView *containerView = [[UIView alloc] init]; + [parentView addSubview:containerView]; + + containerView.frame = CGRectMake(0.0, 0.0, _fullEntitiesView.frame.size.width, _fullEntitiesView.frame.size.height); + [containerView addSubview:_fullEntitiesView]; + + CGFloat paintingScale = _fullEntitiesView.frame.size.width / _photoEditor.originalSize.width; + _fullEntitiesView.frame = CGRectMake(-_photoEditor.cropRect.origin.x * paintingScale, -_photoEditor.cropRect.origin.y * paintingScale, _fullEntitiesView.frame.size.width, _fullEntitiesView.frame.size.height); + + CGFloat cropScale = 1.0; + if (_photoEditor.originalSize.width > _photoEditor.originalSize.height) { + cropScale = _photoEditor.originalSize.height / _photoEditor.cropRect.size.height; + } else { + cropScale = _photoEditor.originalSize.width / _photoEditor.cropRect.size.width; + } + + CGFloat scale = parentView.frame.size.width / _fullEntitiesView.frame.size.width; + containerView.transform = CGAffineTransformMakeScale(scale * cropScale, scale * cropScale); + containerView.frame = CGRectMake(0.0, 0.0, parentView.frame.size.width, parentView.frame.size.height); +} + - (void)transitionIn { if (_portraitToolsWrapperView.frame.size.height < FLT_EPSILON) { @@ -373,13 +418,22 @@ const CGFloat TGPhotoAvatarPreviewLandscapePanelSize = TGPhotoAvatarPreviewPanel [_cropView closeCurtains]; + [self transitEntities:_cropView.clipView]; + + CGAffineTransform initialTransform = _previewView.transform; [UIView animateWithDuration:0.3f delay:0.0f options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionLayoutSubviews animations:^ { - _previewView.frame = targetFrame; + CGFloat scale = targetFrame.size.width / _previewView.frame.size.width; + _previewView.center = CGPointMake(CGRectGetMidX(targetFrame), CGRectGetMidY(targetFrame)); + _previewView.transform = CGAffineTransformScale(initialTransform, scale, scale); + _cropView.center = CGPointMake(CGRectGetMidX(targetCropViewFrame), CGRectGetMidY(targetCropViewFrame)); _cropView.transform = CGAffineTransformMakeScale(targetCropViewScale, targetCropViewScale); } completion:^(__unused BOOL finished) - { + { + _fullEntitiesView.frame = CGRectMake(0, 0, _fullEntitiesView.frame.size.width, _fullEntitiesView.frame.size.height); + _previewView.transform = initialTransform; + _previewView.frame = targetFrame; [_cropView removeFromSuperview]; _previewView.alpha = 1.0; if (self.finishedTransitionOut != nil) @@ -504,8 +558,13 @@ const CGFloat TGPhotoAvatarPreviewLandscapePanelSize = TGPhotoAvatarPreviewPanel { _appeared = true; + if (!self.initialAppearance) { + [_fullEntitiesView.superview removeFromSuperview]; + _fullEntitiesView.frame = CGRectMake(0, 0, _fullEntitiesView.frame.size.width, _fullEntitiesView.frame.size.height); + [_cropView attachEntitiesView]; + } + if ([transitionView isKindOfClass:[TGPhotoEditorPreviewView class]]) { - } else { [transitionView removeFromSuperview]; } diff --git a/submodules/LegacyComponents/Sources/TGPhotoEditorController.m b/submodules/LegacyComponents/Sources/TGPhotoEditorController.m index ed1d18f949..7f7479dd3f 100644 --- a/submodules/LegacyComponents/Sources/TGPhotoEditorController.m +++ b/submodules/LegacyComponents/Sources/TGPhotoEditorController.m @@ -1281,7 +1281,7 @@ cropController.toolbarLandscapeSize = TGPhotoEditorToolbarSize; cropController.controlVideoPlayback = ^(bool play) { __strong TGPhotoEditorController *strongSelf = weakSelf; - if (strongSelf == nil) + if (strongSelf == nil || strongSelf->_progressVisible) return; if (play) { [strongSelf startVideoPlayback:false]; @@ -1297,7 +1297,7 @@ }; cropController.togglePlayback = ^{ __strong TGPhotoEditorController *strongSelf = weakSelf; - if (strongSelf == nil || !strongSelf->_item.isVideo) + if (strongSelf == nil || !strongSelf->_item.isVideo || strongSelf->_progressVisible) return; if (strongSelf->_isPlaying) { diff --git a/submodules/LegacyComponents/Sources/TGPhotoEditorTabController.m b/submodules/LegacyComponents/Sources/TGPhotoEditorTabController.m index 842172cf41..be182acce2 100644 --- a/submodules/LegacyComponents/Sources/TGPhotoEditorTabController.m +++ b/submodules/LegacyComponents/Sources/TGPhotoEditorTabController.m @@ -169,22 +169,34 @@ const CGFloat TGPhotoEditorToolbarSize = 49.0f; _transitionInProgress = true; + CGAffineTransform initialTransform = _transitionView.transform; [UIView animateWithDuration:0.3f delay:0.0f options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionLayoutSubviews animations:^ { - _transitionView.frame = _transitionTargetFrame; + if (_animateScale) { + CGFloat scale = _transitionTargetFrame.size.width / _transitionView.frame.size.width; + _transitionView.center = CGPointMake(CGRectGetMidX(_transitionTargetFrame), CGRectGetMidY(_transitionTargetFrame)); + _transitionView.transform = CGAffineTransformScale(initialTransform, scale, scale); + } else { + _transitionView.frame = _transitionTargetFrame; + } } completion:^(BOOL finished) { _transitionInProgress = false; - UIView *transitionView = _transitionView; - _transitionView = nil; - - if (self.finishedTransitionIn != nil) - { - self.finishedTransitionIn(); - self.finishedTransitionIn = nil; - } - - [self _finishedTransitionInWithView:transitionView]; + UIView *transitionView = _transitionView; + _transitionView = nil; + + if (_animateScale) { + _transitionView.transform = initialTransform; + _transitionView.frame = _transitionTargetFrame; + } + + if (self.finishedTransitionIn != nil) + { + self.finishedTransitionIn(); + self.finishedTransitionIn = nil; + } + + [self _finishedTransitionInWithView:transitionView]; }]; } diff --git a/submodules/LegacyComponents/Sources/TGPhotoPaintController.m b/submodules/LegacyComponents/Sources/TGPhotoPaintController.m index e554aef9a2..07f157b9ff 100644 --- a/submodules/LegacyComponents/Sources/TGPhotoPaintController.m +++ b/submodules/LegacyComponents/Sources/TGPhotoPaintController.m @@ -99,6 +99,7 @@ const CGFloat TGPhotoPaintStickerKeyboardSize = 260.0f; bool _appeared; bool _skipEntitiesSetup; + bool _entitiesReady; TGPhotoPaintFont *_selectedTextFont; TGPhotoPaintTextEntityStyle _selectedTextStyle; @@ -268,7 +269,9 @@ const CGFloat TGPhotoPaintStickerKeyboardSize = 260.0f; [strongSelf updateSettingsButton]; }; - [_contentWrapperView addSubview:_entitiesContainerView]; + if (!_skipEntitiesSetup) { + [_contentWrapperView addSubview:_entitiesContainerView]; + } _undoManager.entitiesContainer = _entitiesContainerView; _dimView = [[UIView alloc] init]; @@ -1816,7 +1819,6 @@ const CGFloat TGPhotoPaintStickerKeyboardSize = 260.0f; if (self.presentedForAvatarCreation) { _canvasView.hidden = true; - _entitiesContainerView.hidden = true; } } @@ -1865,7 +1867,7 @@ const CGFloat TGPhotoPaintStickerKeyboardSize = 260.0f; [self setupCanvas]; _entitiesContainerView.hidden = false; - + TGPhotoEditorPreviewView *previewView = _previewView; [previewView setPaintingHidden:true]; previewView.hidden = false; @@ -1887,8 +1889,10 @@ const CGFloat TGPhotoPaintStickerKeyboardSize = 260.0f; CGPoint boundsCenter = TGPaintCenterOfRect(_contentWrapperView.bounds); _entitiesContainerView.center = TGPaintAddPoints(boundsCenter, offset); - [_contentWrapperView addSubview:_entitiesContainerView]; - + if (!_skipEntitiesSetup || _entitiesReady) { + [_contentWrapperView addSubview:_entitiesContainerView]; + } + _entitiesReady = true; [self resetScrollView]; } diff --git a/submodules/LegacyComponents/Sources/TGPhotoToolsController.m b/submodules/LegacyComponents/Sources/TGPhotoToolsController.m index 1d610335e2..8daf0740e6 100644 --- a/submodules/LegacyComponents/Sources/TGPhotoToolsController.m +++ b/submodules/LegacyComponents/Sources/TGPhotoToolsController.m @@ -107,26 +107,21 @@ const CGFloat TGPhotoEditorToolsLandscapePanelSize = TGPhotoEditorToolsPanelSize } - (void)layoutEntitiesView { - CGSize fittedContentSize = [TGPhotoPaintController fittedContentSize:_photoEditor.cropRect orientation:_photoEditor.cropOrientation originalSize:_photoEditor.originalSize]; - CGRect fittedCropRect = [TGPhotoPaintController fittedCropRect:_photoEditor.cropRect originalSize:_photoEditor.originalSize keepOriginalSize:false]; - _entitiesWrapperView.frame = CGRectMake(0.0f, 0.0f, fittedContentSize.width, fittedContentSize.height); + _entitiesWrapperView.frame = CGRectMake(0.0, 0.0, _entitiesView.frame.size.width, _entitiesView.frame.size.height); - CGRect rect = [TGPhotoPaintController fittedCropRect:self.photoEditor.cropRect originalSize:self.photoEditor.originalSize keepOriginalSize:true]; - _entitiesView.frame = CGRectMake(0, 0, rect.size.width, rect.size.height); - _entitiesView.transform = CGAffineTransformMakeRotation(_photoEditor.cropRotation); + CGFloat paintingScale = _entitiesView.frame.size.width / _photoEditor.originalSize.width; + _entitiesView.frame = CGRectMake(-_photoEditor.cropRect.origin.x * paintingScale, -_photoEditor.cropRect.origin.y * paintingScale, _entitiesView.frame.size.width, _entitiesView.frame.size.height); - CGSize fittedOriginalSize = TGScaleToSize(_photoEditor.originalSize, [TGPhotoPaintController maximumPaintingSize]); - CGSize rotatedSize = TGRotatedContentSize(fittedOriginalSize, _photoEditor.cropRotation); - CGPoint centerPoint = CGPointMake(rotatedSize.width / 2.0f, rotatedSize.height / 2.0f); - - CGFloat scale = fittedOriginalSize.width / _photoEditor.originalSize.width; - CGPoint offset = TGPaintSubtractPoints(centerPoint, [TGPhotoPaintController fittedCropRect:_photoEditor.cropRect centerScale:scale]); - - CGPoint boundsCenter = TGPaintCenterOfRect(_entitiesWrapperView.bounds); - _entitiesView.center = TGPaintAddPoints(boundsCenter, offset); - if (_entitiesView.superview != _entitiesWrapperView) { - [_entitiesWrapperView addSubview:_entitiesView]; + CGFloat cropScale = 1.0; + if (_photoEditor.originalSize.width > _photoEditor.originalSize.height) { + cropScale = _photoEditor.originalSize.height / _photoEditor.cropRect.size.height; + } else { + cropScale = _photoEditor.originalSize.width / _photoEditor.cropRect.size.width; } + + CGFloat scale = _previewView.frame.size.width / _entitiesView.frame.size.width; + _entitiesWrapperView.transform = CGAffineTransformMakeScale(scale * cropScale, scale * cropScale); + _entitiesWrapperView.frame = CGRectMake(0.0, 0.0, _previewView.frame.size.width, _previewView.frame.size.height); } - (void)loadView