From 4cbef9faa7b6d1667911ac743a8d947538469cec Mon Sep 17 00:00:00 2001 From: Yoshimasa Niwa Date: Mon, 29 Jan 2018 17:22:50 -0800 Subject: [PATCH] Add sound effect feature. --- HapticKey/Classes/HTKAppDelegate.m | 17 +++++++++++++++++ HapticKey/Classes/HTKHapticFeedback.h | 6 ++++++ HapticKey/Classes/HTKHapticFeedback.m | 21 +++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/HapticKey/Classes/HTKAppDelegate.m b/HapticKey/Classes/HTKAppDelegate.m index 86eacaf..6cc324b 100644 --- a/HapticKey/Classes/HTKAppDelegate.m +++ b/HapticKey/Classes/HTKAppDelegate.m @@ -168,6 +168,7 @@ - (void)_htk_main_updateHapticFeedback } [self _htk_main_updateHapticFeedbackType]; + [self _htk_main_updateSoundFeedbackType]; } - (void)_htk_main_updateHapticFeedbackType @@ -189,6 +190,22 @@ - (void)_htk_main_updateHapticFeedbackType } } +- (void)_htk_main_updateSoundFeedbackType +{ + if (!self.hapticFeedback) { + return; + } + + switch (self.soundEffectType) { + case HTKAppDelegateSoundEffectTypeNone: + self.hapticFeedback.soundType = HTKSoundFeedbackTypeNone; + break; + case HTKAppDelegateSoundEffectTypeDefault: + self.hapticFeedback.soundType = HTKSoundFeedbackTypeDefault; + break; + } +} + - (void)_htk_main_updateMainBundleLoginItem { if (!self.finishedLaunching) { diff --git a/HapticKey/Classes/HTKHapticFeedback.h b/HapticKey/Classes/HTKHapticFeedback.h index 96e7279..7b95965 100644 --- a/HapticKey/Classes/HTKHapticFeedback.h +++ b/HapticKey/Classes/HTKHapticFeedback.h @@ -18,11 +18,17 @@ typedef NS_ENUM(NSUInteger, HTKHapticFeedbackType) { HTKHapticFeedbackTypeStrong }; +typedef NS_ENUM(NSUInteger, HTKSoundFeedbackType) { + HTKSoundFeedbackTypeNone, + HTKSoundFeedbackTypeDefault +}; + @interface HTKHapticFeedback : NSObject @property (nonatomic, readonly) HTKEventListener *eventListener; @property (nonatomic, getter=isEnabled) BOOL enabled; @property (nonatomic) HTKHapticFeedbackType type; +@property (nonatomic) HTKSoundFeedbackType soundType; + (instancetype)new NS_UNAVAILABLE; - (instancetype)init NS_UNAVAILABLE; diff --git a/HapticKey/Classes/HTKHapticFeedback.m b/HapticKey/Classes/HTKHapticFeedback.m index 70c4263..e9cf5ed 100644 --- a/HapticKey/Classes/HTKHapticFeedback.m +++ b/HapticKey/Classes/HTKHapticFeedback.m @@ -10,15 +10,20 @@ #import "HTKEventListener.h" #import "HTKHapticFeedback.h" #import "HTKMultitouchActuator.h" +#import "HTKSystemSound.h" #import "HTKTimer.h" NS_ASSUME_NONNULL_BEGIN static const NSTimeInterval kMinimumActuationInterval = 0.05; +static NSString * const kDefaultSystemSoundsGroup = @"ink"; +static NSString * const kDefaultSystemSoundsName = @"InkSoundBecomeMouse.aif"; + @interface HTKHapticFeedback () @property (nonatomic, nullable) HTKTimer *timer; +@property (nonatomic, readonly) HTKSystemSound *defaultSystemSound; @end @@ -36,6 +41,8 @@ - (instancetype)initWithEventListener:(HTKEventListener *)eventListener _eventListener = eventListener; _eventListener.delegate = self; _type = HTKHapticFeedbackTypeMedium; + + _defaultSystemSound = [[HTKSystemSound alloc] initWithSystemSoundsGroup:kDefaultSystemSoundsGroup name:kDefaultSystemSoundsName]; } return self; } @@ -61,9 +68,13 @@ - (void)eventListener:(HTKEventListener *)eventListener didListenEvent:(HTKEvent self.timer = [[HTKTimer alloc] initWithInterval:kMinimumActuationInterval target:self selector:@selector(_htk_timer_didFire:)]; const SInt32 actuationID = [self _htk_main_actuationID]; + HTKSystemSound * const systemSound = [self _htk_main_systemSound]; switch (event.phase) { case HTKEventPhaseBegin: [[HTKMultitouchActuator sharedActuator] actuateActuationID:actuationID unknown1:0 unknown2:0.0 unknown3:2.0]; + if (systemSound) { + [systemSound play]; + } break; case HTKEventPhaseEnd: [[HTKMultitouchActuator sharedActuator] actuateActuationID:actuationID unknown1:0 unknown2:0.0 unknown3:0.0]; @@ -95,6 +106,16 @@ - (SInt32)_htk_main_actuationID return 0; } +- (nullable HTKSystemSound *)_htk_main_systemSound +{ + switch (self.soundType) { + case HTKSoundFeedbackTypeNone: + return nil; + case HTKSoundFeedbackTypeDefault: + return self.defaultSystemSound; + } +} + @end NS_ASSUME_NONNULL_END