1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * Copyright (c) 2022 Contributors to the Rrise project
 */

//! Everything related to RTPC, Switch, States and Triggers.

use crate::bindings::root::AK::SoundEngine::{
    PostTrigger, PostTrigger2, ResetRTPCValue, ResetRTPCValue2, SetRTPCValue, SetRTPCValue2,
    SetRTPCValueByPlayingID, SetRTPCValueByPlayingID2, SetState, SetState2, SetSwitch, SetSwitch2,
};
use crate::{
    ak_call_result, with_cstring, AkCurveInterpolation, AkGameObjectID, AkID, AkPlayingID,
    AkResult, AkRtpcValue, AkTimeMs, AK_INVALID_GAME_OBJECT, AK_INVALID_PLAYING_ID,
};

/// Helper to set or reset RTPCs.
///
/// Use [SetRtpcValue::set] or [SetRtpcValue::reset] to post the change to Wwise.
#[derive(Debug, Copy, Clone)]
pub struct SetRtpcValue<'a> {
    rtpc_id: AkID<'a>,
    value: AkRtpcValue,
    target_game_obj_id: AkGameObjectID,
    target_playing_id: AkPlayingID,
    interp_ms: AkTimeMs,
    fade_curve: AkCurveInterpolation,
    bypass_designer_interp: bool,
}

impl<'a> SetRtpcValue<'a> {
    pub fn new<T: Into<AkID<'a>>>(rtpc_id: T, value: AkRtpcValue) -> Self {
        Self {
            rtpc_id: rtpc_id.into(),
            value,
            target_game_obj_id: AK_INVALID_GAME_OBJECT,
            target_playing_id: AK_INVALID_PLAYING_ID,
            interp_ms: 0,
            fade_curve: AkCurveInterpolation::AkCurveInterpolation_Linear,
            bypass_designer_interp: false,
        }
    }

    /// Value to set.
    ///
    /// *Note* Doesn't consume self so that it's easier to reuse the same SetRTPCValue struct with
    /// different values over time.
    pub fn with_value(&mut self, value: AkRtpcValue) -> &mut Self {
        self.value = value;
        self
    }

    /// Associated game object ID.
    ///
    /// With this function, you may set a game parameter value with global scope (default) or with game object scope.
    /// Game object scope superseeds global scope. (Once a value is set for the game object scope, it will not be affected by changes to the global scope value.)
    /// Game parameter values set with global scope are applied to all game objects that not yet registered, or already registered but not overridden with a value
    /// with game object scope.
    /// To set a game parameter value with global scope, leave the default or pass [AK_INVALID_GAME_OBJECT] as the game object.
    ///
    /// Default: [AK_INVALID_GAME_OBJECT]
    pub fn for_target(mut self, game_obj_id: AkGameObjectID) -> Self {
        self.target_game_obj_id = game_obj_id;
        self
    }

    /// Associated playing ID.
    ///
    /// With this function, you may set a game parameter value on playing ID scope.
    /// Playing id scope superseeds both game object scope and global scope.
    /// Ignores any target game object set with [for_target](Self::for_target).
    ///
    /// Default: [AK_INVALID_PLAYING_ID]
    pub fn for_playing_id(mut self, playing_id: AkPlayingID) -> Self {
        self.target_playing_id = playing_id;
        self
    }

    /// Duration during which the game parameter is interpolated towards in_value.
    ///
    /// With this function, you may change the value of a game parameter over time. To do so, specify a non-zero
    /// value for `ms`. At each audio frame, the game parameter value will be updated internally
    /// according to the interpolation curve (set with [with_interp_curve](Self::with_interp_curve)).
    /// If you call [set](Self::set) with `ms = 0` in the middle of an interpolation, the interpolation stops and
    /// the new value is set directly. Thus, if you call [set](Self::set) at every game frame, you
    /// should not use `with_interp_millis`, as it would have no effect and it is less efficient.
    ///
    /// Default: `0`
    pub fn with_interp_millis(mut self, ms: AkTimeMs) -> Self {
        self.interp_ms = ms;
        self
    }

    /// When [with_interp_millis](Self::with_interp_millis) is used and non-zero, describes the Curve
    /// type to be used for the game parameter interpolation.
    ///
    /// Default: [AkCurveInterpolation::AkCurveInterpolation_Linear]
    pub fn with_interp_curve(mut self, curve: AkCurveInterpolation) -> Self {
        self.fade_curve = curve;
        self
    }

    /// `True` if you want to bypass the internal "slew rate" or "over time filtering" specified by the sound designer.
    /// This is meant to be used when for example loading a level and you dont want the values to interpolate.
    ///
    /// Default: `false`
    pub fn should_bypass_designer_interp(mut self, should: bool) -> Self {
        self.bypass_designer_interp = should;
        self
    }

    /// Post this RTPC configuration to Wwise.
    pub fn set(&self) -> Result<(), AkResult> {
        if self.target_playing_id != AK_INVALID_PLAYING_ID {
            match self.rtpc_id {
                AkID::Name(name) => with_cstring![name => cname {
                    ak_call_result![SetRTPCValueByPlayingID2(
                        cname.as_ptr(),
                        self.value,
                        self.target_playing_id,
                        self.interp_ms,
                        self.fade_curve,
                        self.bypass_designer_interp
                    )
                ]}],
                AkID::ID(id) => ak_call_result![SetRTPCValueByPlayingID(
                    id,
                    self.value,
                    self.target_playing_id,
                    self.interp_ms,
                    self.fade_curve,
                    self.bypass_designer_interp
                )],
            }
        } else {
            match self.rtpc_id {
                AkID::Name(name) => with_cstring![name => cname {
                    ak_call_result![SetRTPCValue2(
                        cname.as_ptr(),
                        self.value,
                        self.target_game_obj_id,
                        self.interp_ms,
                        self.fade_curve,
                        self.bypass_designer_interp
                    )
                ]}],
                AkID::ID(id) => ak_call_result![SetRTPCValue(
                    id,
                    self.value,
                    self.target_game_obj_id,
                    self.interp_ms,
                    self.fade_curve,
                    self.bypass_designer_interp
                )],
            }
        }
    }

    /// Post a reset of this RTPC configuration to Wwise.
    ///
    /// Resets the value of the game parameter to its default value, as specified in the Wwise project.
    ///
    /// Incompatible with [for_playing_id](Self::for_playing_id); only works at global or game object
    /// scope, set with [for_target](Self::for_target).
    pub fn reset(&self) -> Result<(), AkResult> {
        match self.rtpc_id {
            AkID::Name(name) => with_cstring![name => cname {
                ak_call_result![ResetRTPCValue2(
                    cname.as_ptr(),
                    self.target_game_obj_id,
                    self.interp_ms,
                    self.fade_curve,
                    self.bypass_designer_interp
                )
            ]}],
            AkID::ID(id) => ak_call_result![ResetRTPCValue(
                id,
                self.target_game_obj_id,
                self.interp_ms,
                self.fade_curve,
                self.bypass_designer_interp
            )],
        }
    }
}

/// Sets the State of a Switch Group.
///
/// Always returns [AkResult::AK_Success].
///
/// Panics if switch_group and switch_id are not of the same variant.
pub fn set_switch<'a, T: Into<AkID<'a>>>(
    switch_group: T,
    switch_id: T,
    game_obj: AkGameObjectID,
) -> Result<(), AkResult> {
    match (switch_group.into(), switch_id.into()) {
        (AkID::Name(group), AkID::Name(switch)) => {
            with_cstring![group => groupc, switch => switchc {
                ak_call_result![SetSwitch2(
                    groupc.as_ptr(),
                    switchc.as_ptr(),
                    game_obj,
                )
            ]}]
        }
        (AkID::ID(group), AkID::ID(switch)) => {
            ak_call_result![SetSwitch(group, switch, game_obj)]
        }
        _ => panic!("Args switch_group and switch_id should be of the same variant"),
    }
}

/// Post the specified trigger.
///
/// Always returns [AkResult::AK_Success].
pub fn post_trigger<'a, T: Into<AkID<'a>>>(
    trigger: T,
    game_obj: AkGameObjectID,
) -> Result<(), AkResult> {
    match trigger.into() {
        AkID::Name(name) => {
            with_cstring![name => cname {
                ak_call_result![PostTrigger2(
                    cname.as_ptr(),
                    game_obj,
                )
            ]}]
        }
        AkID::ID(id) => {
            ak_call_result![PostTrigger(id, game_obj)]
        }
    }
}

/// Sets the state of a State Group.
///
/// *Return*
/// > - [AkResult::AK_Success] if successful
/// > - [AkResult::AK_IDNotFound] if the state or State Group name was not resolved to an existing ID.
/// Make sure that the banks were generated with the "include string" option.
///
/// Panics if state_group and state_id are not of the same variant.
pub fn set_state<'a, T: Into<AkID<'a>>>(state_group: T, state_id: T) -> Result<(), AkResult> {
    match (state_group.into(), state_id.into()) {
        (AkID::Name(group), AkID::Name(state)) => {
            with_cstring![group => groupc, state => statec {
                ak_call_result![SetState2(
                    groupc.as_ptr(),
                    statec.as_ptr()
                )
            ]}]
        }
        (AkID::ID(group), AkID::ID(state)) => {
            ak_call_result![SetState(group, state)]
        }
        _ => panic!("Args state_group and state_id should be of the same variant"),
    }
}