Sound Bakery  v0.1.0
Open-source audio middleware for games
Loading...
Searching...
No Matches
parameter.h
1#pragma once
2
3#include "sound_bakery/core/core_include.h"
4
5namespace sbk::engine
6{
7 class named_parameter;
8 class float_parameter;
9 class named_parameter_value;
10
16 template <typename parameter_type>
17 class SB_CLASS parameter : public sbk::core::database_object
18 {
19 REGISTER_REFLECTION(parameter, database_object)
20
21 static_assert(std::is_arithmetic<parameter_type>::value);
22
23 public:
28
34 using local_parameter = std::pair<sbk_id, parameter_property>;
35
42 using local_parameter_value_pair = std::pair<sbk_id, parameter_type>;
43
44 parameter() = default;
45
51 parameter(parameter_type min, parameter_type max)
52 : sbk::core::database_object(), m_property(min, min, max), m_defaultValue(min)
53 {
54 }
55
59 [[nodiscard]] parameter_type get() const { return m_property.get(); }
60 [[nodiscard]] parameter_type get_default() const { return m_defaultValue; }
61
67 void set(parameter_type value) { m_property.set(value); }
68 void set_default(parameter_type value) { m_defaultValue = value; }
69
74 {
75 return m_property.get_delegate();
76 }
77
84 {
85 return local_parameter(get_database_id(), m_property);
86 }
87
88 private:
89 parameter_type m_defaultValue;
91 };
92
93 class SB_CLASS float_parameter : public parameter<float>
94 {
95 REGISTER_REFLECTION(float_parameter, parameter)
96 };
97
98 class SB_CLASS int_parameter : public parameter<int>
99 {
100 REGISTER_REFLECTION(int_parameter, parameter)
101 };
102
113 {
114 public:
116
117 REGISTER_REFLECTION(named_parameter_value, database_object)
118 };
119
127 class SB_CLASS named_parameter : public parameter<sbk_id>
128 {
129 REGISTER_REFLECTION(named_parameter, parameter)
130
131 public:
139 : m_values(), parameter((std::numeric_limits<sbk_id>::min)(), (std::numeric_limits<sbk_id>::max)())
140 {
141 }
142
149 {
151
152 if (!name.empty())
153 {
154 if (const std::shared_ptr<named_parameter_value> parameterValue = create_database_object<named_parameter_value>())
155 {
156 parameterValue->set_database_name(name);
157 parameterValue->parentParameter = this;
158
159 result = parameterValue;
160
161 m_values.emplace(parameterValue);
162 }
163 }
164
165 return result;
166 }
167
173 std::unordered_set<sbk::core::database_ptr<named_parameter_value>> get_values()
174 {
175 if (m_values.empty())
176 {
177 set_selected_value(add_new_value("None"));
178 }
179
180 return m_values;
181 }
182
190 {
191 if (m_values.contains(value))
192 {
193 set(value.id());
194 }
195 else
196 {
197 assert(false);
198 }
199 }
200
208 {
210 selected.lookup();
211 return selected;
212 }
213
214 private:
215 std::unordered_set<sbk::core::database_ptr<named_parameter_value>> m_values;
216 };
217
218 using global_float_parameter = sbk::core::database_ptr<float_parameter>;
219 using global_int_parameter = sbk::core::database_ptr<named_parameter>;
220
226 struct SB_CLASS global_parameter_list
227 {
228 std::unordered_set<global_float_parameter> floatParameters;
229 std::unordered_set<global_int_parameter> intParameters;
230 };
231
235 struct SB_CLASS local_parameter_list
236 {
237 std::unordered_map<global_float_parameter, float_parameter::parameter_property> floatParameters;
238 std::unordered_map<global_int_parameter, named_parameter::parameter_property> intParameters;
239 };
240} // namespace sbk::engine
Base object type for any object that can exist in the editor/database. Holds an ID and name.
Definition database_object.h:12
Definition database_ptr.h:23
sbk_id id() const noexcept
Get ID of the referenced object.
Definition database_ptr.h:84
bool lookup() const
Find the live object referenced by the ID and store it.
Definition database_ptr.h:153
Definition parameter.h:94
Definition parameter.h:99
Represents a discrete value for a named_parameter.
Definition parameter.h:113
Holds discrete named integer values.
Definition parameter.h:128
named_parameter()
Creates a named_parameter with min and max values.
Definition parameter.h:138
sbk::core::database_ptr< named_parameter_value > get_selected_value() const
Get the selected value as a DatabasePtr.
Definition parameter.h:207
std::unordered_set< sbk::core::database_ptr< named_parameter_value > > get_values()
Gets all named values in this parameter.
Definition parameter.h:173
sbk::core::database_ptr< named_parameter_value > add_new_value(const std::string_view name)
Adds a new value to the parameter.
Definition parameter.h:148
void set_selected_value(sbk::core::database_ptr< named_parameter_value > value)
Sets the parameter value with a DatabasePtr, ensuring the value exists in this parameter.
Definition parameter.h:189
Defines a database object with a changeable property.
Definition parameter.h:18
local_parameter create_local_parameter_from_this() const
Copies this parameter to a runtime version, suitable for handling unique variations per game object e...
Definition parameter.h:83
parameter_property::property_changed_delegate & get_delegate()
Get the parameter delegate that fires when changing the value.
Definition parameter.h:73
parameter(parameter_type min, parameter_type max)
Creates a parameter with min and max values set.
Definition parameter.h:51
void set(parameter_type value)
Set the value of the parameter.
Definition parameter.h:67
std::pair< sbk_id, parameter_property > local_parameter
Defines the type used for passing local versions/variations of this parameter.
Definition parameter.h:34
std::pair< sbk_id, parameter_type > local_parameter_value_pair
Defines an ID to a parameter and a value for that parameter.
Definition parameter.h:42
parameter_type get() const
Get the current value of the parameter.
Definition parameter.h:59
Holds a list of parameters.
Definition parameter.h:227
Holds a list of parameters and their local value.
Definition parameter.h:236