Skip to content

Commit

Permalink
Add sound effect feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
niw committed Jan 30, 2018
1 parent c14497c commit 4cbef9f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
17 changes: 17 additions & 0 deletions HapticKey/Classes/HTKAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ - (void)_htk_main_updateHapticFeedback
}

[self _htk_main_updateHapticFeedbackType];
[self _htk_main_updateSoundFeedbackType];
}

- (void)_htk_main_updateHapticFeedbackType
Expand All @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions HapticKey/Classes/HTKHapticFeedback.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions HapticKey/Classes/HTKHapticFeedback.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 () <HTKEventListenerDelegate>

@property (nonatomic, nullable) HTKTimer *timer;
@property (nonatomic, readonly) HTKSystemSound *defaultSystemSound;

@end

Expand All @@ -36,6 +41,8 @@ - (instancetype)initWithEventListener:(HTKEventListener *)eventListener
_eventListener = eventListener;
_eventListener.delegate = self;
_type = HTKHapticFeedbackTypeMedium;

_defaultSystemSound = [[HTKSystemSound alloc] initWithSystemSoundsGroup:kDefaultSystemSoundsGroup name:kDefaultSystemSoundsName];
}
return self;
}
Expand All @@ -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];
Expand Down Expand Up @@ -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

0 comments on commit 4cbef9f

Please sign in to comment.