Enzo
Loading...
Searching...
No Matches
Parameter.h
1#pragma once
2#include "Engine/Core/Types.h"
3#include "Engine/Parameter/Template.h"
4#include <boost/signals2.hpp>
5#include <memory>
6#include <optional>
7#include <string_view>
8#include <variant>
9
10namespace enzo::expr {
11class ExpressionContext;
12}
13
14namespace enzo::prm {
15
16using PrmValues = std::variant<std::vector<floatT>, std::vector<intT>, std::vector<String>>;
17
19{
20 public:
21 Parameter(Template prmTemplate);
22 virtual ~Parameter() = default;
23
24 std::string getName() const;
25 std::string getLabel() const;
26 enzo::prm::Type getType() const;
27 enzo::prm::ValueType getValueType() const;
28 unsigned int getVectorSize() const;
29
30 floatT evalFloat(unsigned int index = 0) const;
31 String evalString(unsigned int index = 0) const;
32 intT evalInt(unsigned int index = 0) const;
33
34 // Variants that report a driving expression's failure. The value still falls
35 // back to the stored literal on failure, and error is left empty on success
36 // or when the component holds no expression.
37 floatT evalFloat(unsigned int index, String& error) const;
38 intT evalInt(unsigned int index, String& error) const;
39 String evalString(unsigned int index, String& error) const;
40
41 // Read every value the parameter holds as a list. A scalar parameter
42 // returns one element, a vector parameter one per component, and a
43 // multi-value parameter such as a multi select dropdown one per selection.
44 std::vector<floatT> evalFloats() const;
45 std::vector<String> evalStrings() const;
46 std::vector<intT> evalInts() const;
47
48 void setInt(intT value, unsigned int index = 0);
49 void setFloat(floatT value, unsigned int index = 0);
50 void setString(String value, unsigned int index = 0);
51
52 // A component may hold a daslang expression instead of a literal. When set,
53 // the eval functions run the expression to produce the value, and setting a
54 // literal clears it.
55 void setExpression(String expression, unsigned int index = 0);
56 void clearExpression(unsigned int index = 0);
57 bool hasExpression(unsigned int index = 0) const;
58
61 std::optional<String> getExpression(unsigned int index = 0) const;
62
70 std::optional<String> getExpressionError(unsigned int index = 0) const;
71
72 PrmValues getValues() const;
73 void setValues(const PrmValues& values);
74
75 // Multiparm instances. Each instance is a group of field parameters cloned
76 // from the instance template. The live count is the number of instances.
77 unsigned int getInstanceCount() const;
78 const std::vector<std::shared_ptr<Parameter>>& getInstance(unsigned int instanceIndex) const;
79 std::shared_ptr<Parameter>
80 getInstanceField(unsigned int instanceIndex, std::string_view fieldName) const;
81 void addInstance();
82 void removeInstance(unsigned int instanceIndex);
83 void moveInstance(unsigned int fromIndex, unsigned int toIndex);
84
85 const Template& getTemplate();
86
87 boost::signals2::signal<void()> valueChanged;
88
89 protected:
90 virtual void onFloatSet_(const PrmValues& before) {}
91 void handleValueChange_();
92
96 virtual std::unique_ptr<expr::ExpressionContext> makeExpressionContext_() const;
97
101 virtual void
102 submitExpressionDependencies_(const expr::ExpressionContext& context, unsigned int index) const
103 {
104 }
105
107 floatT clampToRange_(floatT value, unsigned int index) const;
108 intT clampToRange_(intT value, unsigned int index) const;
109
110 // Reads a component's stored literal converted to the requested type. A float
111 // and an int store convert to each other with a plain numeric cast, while any
112 // other mismatch yields the type's default. e.g. an int parameter read as a
113 // float casts cleanly, but a string parameter read as a float gives zero.
114 floatT readFloatLiteral_(unsigned int index) const;
115 intT readIntLiteral_(unsigned int index) const;
116 String readStringLiteral_(unsigned int index) const;
117 std::vector<std::shared_ptr<Parameter>> buildInstance_(unsigned int instanceIndex);
118
119 Template template_;
120 PrmValues values_;
121 // One optional expression per component, parallel to the value vector. An
122 // empty slot means the component uses its literal value.
123 std::vector<std::optional<String>> expressions_;
124 std::vector<std::vector<std::shared_ptr<Parameter>>> instances_;
125};
126} // namespace enzo::prm
Basic attribute, parameter, and node types for Enzo.
ValueType
Which kind of value a parameter stores.
Definition Types.h:127
The world a single expression evaluation reads and writes to.
Definition ExpressionContext.h:21
Definition Parameter.h:19
std::optional< String > getExpressionError(unsigned int index=0) const
Evaluates a component's expression and reports any failure.
std::optional< String > getExpression(unsigned int index=0) const
The expression on a component.
Definition Parameter.cpp:302
virtual void submitExpressionDependencies_(const expr::ExpressionContext &context, unsigned int index) const
Hands the dependencies an expression captured to the network graph.
Definition Parameter.h:102
floatT clampToRange_(floatT value, unsigned int index) const
Returns the value pulled within a component's locked range bounds.
Definition Parameter.cpp:316
virtual std::unique_ptr< expr::ExpressionContext > makeExpressionContext_() const
The world an expression on this parameter reads, e.g. for prm().
Definition Parameter.cpp:133
Definition Template.h:18