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
use super::ObsString; use obs_sys::{ obs_data_get_double, obs_data_get_int, obs_data_get_json, obs_data_t, obs_properties_add_float, obs_properties_add_float_slider, obs_properties_add_int, obs_properties_t, }; use std::ffi::CStr; use serde_json::Value; pub struct ParamBuilder {} pub(crate) struct Property { name: &'static str, property_type: PropertyType, } #[derive(Eq, PartialEq)] enum PropertyType { Float, Int, } pub struct Properties<'a> { pointer: *mut obs_properties_t, properties: &'a mut Vec<Property>, } impl<'a> Properties<'a> { pub(crate) unsafe fn from_raw( pointer: *mut obs_properties_t, properties: &'a mut Vec<Property>, ) -> Self { Self { pointer, properties, } } pub unsafe fn into_raw(self) -> *mut obs_properties_t { self.pointer } pub fn add_float_slider( &mut self, name: ObsString, description: ObsString, min: f64, max: f64, step: f64, ) -> &mut Self { unsafe { self.properties.push(Property { name: name.as_str(), property_type: PropertyType::Float, }); obs_properties_add_float_slider( self.pointer, name.as_ptr(), description.as_ptr(), min, max, step, ); } self } pub fn add_float( &mut self, name: ObsString, description: ObsString, min: f64, max: f64, step: f64, ) -> &mut Self { unsafe { self.properties.push(Property { name: name.as_str(), property_type: PropertyType::Float, }); obs_properties_add_float( self.pointer, name.as_ptr(), description.as_ptr(), min, max, step, ); } self } pub fn add_int( &mut self, name: ObsString, description: ObsString, min: i32, max: i32, step: i32, ) -> &mut Self { unsafe { self.properties.push(Property { name: name.as_str(), property_type: PropertyType::Int, }); obs_properties_add_int( self.pointer, name.as_ptr(), description.as_ptr(), min, max, step, ); } self } } pub struct SettingsContext<'a> { settings: *mut obs_data_t, properties: &'a Vec<Property>, init_data: Option<Value>, } impl<'a> SettingsContext<'a> { pub(crate) unsafe fn from_raw( settings: *mut obs_data_t, properties: &'a Vec<Property>, ) -> Self { SettingsContext { settings, properties, init_data: None, } } pub(crate) unsafe fn as_raw(&self) -> *mut obs_data_t { self.settings } fn get_data(&mut self) -> &Option<Value> { let mut json_data: Option<Value> = None; if self.init_data.is_none() { let data = unsafe { CStr::from_ptr(obs_data_get_json(self.settings)) }; if let Some(value) = data .to_str() .ok() .and_then(|x| serde_json::from_str(x).ok()) { json_data = Some(value); } } if json_data.is_some() { self.init_data.replace(json_data.unwrap()); } &self.init_data } pub fn get_float(&mut self, param: ObsString) -> Option<f64> { if self .properties .iter() .find( |Property { name, property_type, }| { property_type == &PropertyType::Float && *name == param.as_str() }, ) .is_some() { Some(unsafe { obs_data_get_double(self.settings, param.as_ptr()) }) } else { if let Some(data) = self.get_data() { let param = param.as_str(); if let Some(val) = data.get(¶m[..param.len() - 1]) { return val.as_f64(); } } None } } pub fn get_int(&mut self, param: ObsString) -> Option<i32> { if self .properties .iter() .find( |Property { name, property_type, }| { property_type == &PropertyType::Int && *name == param.as_str() }, ) .is_some() { Some(unsafe { obs_data_get_int(self.settings, param.as_ptr()) } as i32) } else { if let Some(data) = self.get_data() { let param = param.as_str(); if let Some(val) = data.get(¶m[..param.len() - 1]) { if let Some(val) = val.as_i64() { return Some(val as i32); } } } None } } }