fmod/studio/event_description/
callback.rs

1// Copyright (c) 2024 Melody Madeline Lyons
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
7use fmod_sys::*;
8use std::ffi::c_void;
9
10use crate::studio::{
11    EventCallbackMask, EventDescription, EventInstanceCallback, event_callback_impl,
12};
13use crate::{FmodResultExt, Result};
14
15impl EventDescription {
16    /// Sets the event user data.
17    #[allow(clippy::not_unsafe_ptr_arg_deref)] // fmod doesn't dereference the passed in pointer, and the user dereferencing it is unsafe anyway
18    pub fn set_userdata(&self, userdata: *mut c_void) -> Result<()> {
19        unsafe {
20            FMOD_Studio_EventDescription_SetUserData(self.inner.as_ptr(), userdata).to_result()
21        }
22    }
23
24    /// Retrieves the event user data.
25    pub fn get_userdata(&self) -> Result<*mut c_void> {
26        let mut userdata = std::ptr::null_mut();
27        unsafe {
28            FMOD_Studio_EventDescription_GetUserData(self.inner.as_ptr(), &raw mut userdata)
29                .to_result()?;
30        }
31        Ok(userdata)
32    }
33
34    /// Sets the user callback.
35    ///
36    /// This function sets a user callback which will be assigned to all event instances subsequently created from the event.
37    /// The callback for individual instances can be set with `EventInstance::set_callback`.
38    pub fn set_callback<C: EventInstanceCallback>(&self, mask: EventCallbackMask) -> Result<()> {
39        unsafe {
40            FMOD_Studio_EventDescription_SetCallback(
41                self.inner.as_ptr(),
42                Some(event_callback_impl::<C>),
43                mask.into(),
44            )
45            .to_result()
46        }
47    }
48}