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
73 [[nodiscard]] typename parameter_property::property_changed_delegate& get_delegate()
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 =
155 create_database_object<named_parameter_value>())
156 {
157 parameterValue->set_database_name(name);
158 parameterValue->parentParameter = this;
159
160 result = parameterValue;
161
162 m_values.emplace(parameterValue);
163 }
164 }
165
166 return result;
167 }
168
174 std::unordered_set<sbk::core::database_ptr<named_parameter_value>> get_values()
175 {
176 if (m_values.empty())
177 {
178 set_selected_value(add_new_value("None"));
179 }
180
181 return m_values;
182 }
183
191 {
192 if (m_values.contains(value))
193 {
194 set(value.id());
195 }
196 else
197 {
198 BOOST_ASSERT(false);
199 }
200 }
201
209 {
211 selected.lookup();
212 return selected;
213 }
214
215 private:
216 std::unordered_set<sbk::core::database_ptr<named_parameter_value>> m_values;
217 };
218
219 using global_float_parameter = sbk::core::database_ptr<float_parameter>;
220 using global_int_parameter = sbk::core::database_ptr<named_parameter>;
221
227 struct SB_CLASS global_parameter_list
228 {
229 std::unordered_set<global_float_parameter> floatParameters;
230 std::unordered_set<global_int_parameter> intParameters;
231 };
232
236 struct SB_CLASS local_parameter_list
237 {
238 std::unordered_map<global_float_parameter, float_parameter::parameter_property> floatParameters;
239 std::unordered_map<global_int_parameter, named_parameter::parameter_property> intParameters;
240 };
241} // 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
auto lookup() const noexcept -> bool
Find the live object referenced by the ID and store it.
Definition database_ptr.h:147
auto id() const noexcept -> sbk_id
Get ID of the referenced object.
Definition database_ptr.h:85
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:208
std::unordered_set< sbk::core::database_ptr< named_parameter_value > > get_values()
Gets all named values in this parameter.
Definition parameter.h:174
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:190
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:228
Holds a list of parameters and their local value.
Definition parameter.h:237