2014 snapchat source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.0 KiB

  1. //
  2. // SCFeatureTapToFocusImpl.m
  3. // SCCamera
  4. //
  5. // Created by Michel Loenngren on 4/5/18.
  6. //
  7. #import "SCFeatureTapToFocusAndExposureImpl.h"
  8. #import "SCCameraTweaks.h"
  9. #import "SCCapturer.h"
  10. #import "SCFeatureContainerView.h"
  11. #import "SCTapAnimationView.h"
  12. #import <SCFoundation/SCLog.h>
  13. #import <SCFoundation/SCTraceODPCompatible.h>
  14. @interface SCFeatureTapToFocusAndExposureImpl ()
  15. @property (nonatomic, weak) id<SCCapturer> capturer;
  16. @property (nonatomic, weak) UIView<SCFeatureContainerView> *containerView;
  17. @property (nonatomic) BOOL userTappedToFocusAndExposure;
  18. @property (nonatomic) NSArray<id<SCFeatureCameraTapCommand>> *commands;
  19. @end
  20. @implementation SCFeatureTapToFocusAndExposureImpl
  21. - (instancetype)initWithCapturer:(id<SCCapturer>)capturer commands:(NSArray<id<SCFeatureCameraTapCommand>> *)commands
  22. {
  23. if (self = [super init]) {
  24. _capturer = capturer;
  25. _commands = commands;
  26. }
  27. return self;
  28. }
  29. - (void)reset
  30. {
  31. SC_GUARD_ELSE_RETURN(_userTappedToFocusAndExposure);
  32. _userTappedToFocusAndExposure = NO;
  33. [_capturer continuousAutofocusAndExposureAsynchronouslyWithCompletionHandler:nil context:SCCapturerContext];
  34. }
  35. #pragma mark - SCFeature
  36. - (void)configureWithView:(UIView<SCFeatureContainerView> *)view
  37. {
  38. SCTraceODPCompatibleStart(2);
  39. _containerView = view;
  40. }
  41. - (void)forwardCameraOverlayTapGesture:(UIGestureRecognizer *)gestureRecognizer
  42. {
  43. SCTraceODPCompatibleStart(2);
  44. CGPoint point = [gestureRecognizer locationInView:gestureRecognizer.view];
  45. @weakify(self);
  46. [_capturer convertViewCoordinates:[gestureRecognizer locationInView:_containerView]
  47. completionHandler:^(CGPoint pointOfInterest) {
  48. @strongify(self);
  49. SC_GUARD_ELSE_RETURN(self);
  50. SCLogCameraFeatureInfo(@"Tapped to focus: %@", NSStringFromCGPoint(pointOfInterest));
  51. [self _applyTapCommands:pointOfInterest];
  52. [self _showTapAnimationAtPoint:point forGesture:gestureRecognizer];
  53. }
  54. context:SCCapturerContext];
  55. }
  56. #pragma mark - Private helpers
  57. - (void)_applyTapCommands:(CGPoint)pointOfInterest
  58. {
  59. SCTraceODPCompatibleStart(2);
  60. for (id<SCFeatureCameraTapCommand> command in _commands) {
  61. [command execute:pointOfInterest capturer:_capturer];
  62. }
  63. self.userTappedToFocusAndExposure = YES;
  64. }
  65. - (void)_showTapAnimationAtPoint:(CGPoint)point forGesture:(UIGestureRecognizer *)gestureRecognizer
  66. {
  67. SCTraceODPCompatibleStart(2);
  68. SC_GUARD_ELSE_RETURN([self.containerView isTapGestureRecognizer:gestureRecognizer])
  69. SCTapAnimationView *tapAnimationView = [SCTapAnimationView tapAnimationView];
  70. [_containerView addSubview:tapAnimationView];
  71. tapAnimationView.center = point;
  72. [tapAnimationView showWithCompletion:^(SCTapAnimationView *view) {
  73. [view removeFromSuperview];
  74. }];
  75. }
  76. @end
  77. @implementation SCFeatureCameraFocusTapCommand
  78. - (void)execute:(CGPoint)pointOfInterest capturer:(id<SCCapturer>)capturer
  79. {
  80. [capturer setAutofocusPointOfInterestAsynchronously:pointOfInterest
  81. completionHandler:nil
  82. context:SCCapturerContext];
  83. }
  84. @end
  85. @implementation SCFeatureCameraExposureTapCommand
  86. - (void)execute:(CGPoint)pointOfInterest capturer:(id<SCCapturer>)capturer
  87. {
  88. [capturer setExposurePointOfInterestAsynchronously:pointOfInterest
  89. fromUser:YES
  90. completionHandler:nil
  91. context:SCCapturerContext];
  92. }
  93. @end
  94. @implementation SCFeatureCameraPortraitTapCommand
  95. - (void)execute:(CGPoint)pointOfInterest capturer:(id<SCCapturer>)capturer
  96. {
  97. [capturer setPortraitModePointOfInterestAsynchronously:pointOfInterest
  98. completionHandler:nil
  99. context:SCCapturerContext];
  100. }
  101. @end