Last active
January 19, 2024 09:58
-
-
Save lijon/42f134900236b264e331ffd2936d8c2d to your computer and use it in GitHub Desktop.
AUv3 custom state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// How to store and recall custom non-parameter based state in AUv3 | |
- (NSDictionary*)customState { return nil; } // return your custom state here | |
- (void)setCustomState:(NSDictionary*)state { return; } // set your custom state here | |
- (void)setFullState:(NSDictionary<NSString *,id> *)fullState { | |
NSDictionary *custom = fullState[@"custom_data"]; | |
if(custom) { | |
NSMutableDictionary *m = [fullState mutableCopy]; | |
[m removeObjectForKey:@"custom_data"]; // to not disturb super implementation | |
fullState = m; | |
} | |
// Reset preset when loading state. We could also save preset name & number in state and restore it here, | |
// but that would be problematic when user saves user preset local to host, because then a name is given | |
// in host but we can't know about it here, so the old preset name will be shown in our UI. | |
[self willChangeValueForKey:@"currentPreset"]; | |
super.currentPreset = nil; | |
[self didChangeValueForKey:@"currentPreset"]; | |
[super setFullState:fullState]; | |
if(custom) | |
[self setCustomState:custom]; | |
} | |
- (NSDictionary<NSString *,id> *)fullState { | |
NSDictionary *custom = [self customState]; | |
if(!custom) return [super fullState]; | |
NSMutableDictionary *state = [[super fullState] mutableCopy]; | |
state[@"custom_data"] = [custom mutableCopy]; | |
return state; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment