fmod/studio/event_description/
callback.rs

1// Copyright (c) 2024 Lily 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    event_callback_impl, EventCallbackMask, EventDescription, EventInstanceCallback,
12};
13
14#[cfg(feature = "userdata-abstraction")]
15use crate::userdata::{get_userdata, insert_userdata, set_userdata, Userdata};
16
17#[cfg(feature = "userdata-abstraction")]
18impl EventDescription {
19    pub fn set_userdata(&self, userdata: Userdata) -> Result<()> {
20        let pointer = self.get_raw_userdata()?;
21        if pointer.is_null() {
22            let key = insert_userdata(userdata, *self);
23            self.set_raw_userdata(key.into())?;
24        } else {
25            set_userdata(pointer.into(), userdata);
26        }
27
28        Ok(())
29    }
30
31    pub fn get_userdata(&self) -> Result<Option<Userdata>> {
32        let pointer = self.get_raw_userdata()?;
33        Ok(get_userdata(pointer.into()))
34    }
35}
36
37impl EventDescription {
38    #[allow(clippy::not_unsafe_ptr_arg_deref)] // fmod doesn't dereference the passed in pointer, and the user dereferencing it is unsafe anyway
39    pub fn set_raw_userdata(&self, userdata: *mut c_void) -> Result<()> {
40        unsafe { FMOD_Studio_EventDescription_SetUserData(self.inner, userdata).to_result() }
41    }
42
43    pub fn get_raw_userdata(&self) -> Result<*mut c_void> {
44        let mut userdata = std::ptr::null_mut();
45        unsafe {
46            FMOD_Studio_EventDescription_GetUserData(self.inner, &mut userdata).to_result()?;
47        }
48        Ok(userdata)
49    }
50
51    pub fn set_callback<C: EventInstanceCallback>(&self, mask: EventCallbackMask) -> Result<()> {
52        unsafe {
53            FMOD_Studio_EventDescription_SetCallback(
54                self.inner,
55                Some(event_callback_impl::<C>),
56                mask.into(),
57            )
58            .to_result()
59        }
60    }
61}