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
73 floatT previewFloat(const String& expression, String& error) const;
75 intT previewInt(const String& expression, String& error) const;
77 String previewString(const String& expression, String& error) const;
78
79 PrmValues getValues() const;
80 void setValues(const PrmValues& values);
81
82 // Multiparm instances. Each instance is a group of field parameters cloned
83 // from the instance template. The live count is the number of instances.
84 unsigned int getInstanceCount() const;
85 const std::vector<std::shared_ptr<Parameter>>& getInstance(unsigned int instanceIndex) const;
86 std::shared_ptr<Parameter>
87 getInstanceField(unsigned int instanceIndex, std::string_view fieldName) const;
88 void addInstance();
89 void removeInstance(unsigned int instanceIndex);
90 void moveInstance(unsigned int fromIndex, unsigned int toIndex);
91
92 const Template& getTemplate();
93
94 boost::signals2::signal<void()> valueChanged;
95
96 protected:
97 void handleValueChange_();
98
102 virtual std::unique_ptr<expr::ExpressionContext> makeExpressionContext_() const;
103
107 virtual void
108 submitExpressionDependencies_(const expr::ExpressionContext& context, unsigned int index) const
109 {
110 }
111
113 floatT clampToRange_(floatT value, unsigned int index) const;
114 intT clampToRange_(intT value, unsigned int index) const;
115
116 // Reads a component's stored literal converted to the requested type. A float
117 // and an int store convert to each other with a plain numeric cast, while any
118 // other mismatch yields the type's default. e.g. an int parameter read as a
119 // float casts cleanly, but a string parameter read as a float gives zero.
120 floatT readFloatLiteral_(unsigned int index) const;
121 intT readIntLiteral_(unsigned int index) const;
122 String readStringLiteral_(unsigned int index) const;
123 std::vector<std::shared_ptr<Parameter>> buildInstance_(unsigned int instanceIndex);
124
125 Template template_;
126 PrmValues values_;
127 // One optional expression per component, parallel to the value vector. An
128 // empty slot means the component uses its literal value.
129 std::vector<std::optional<String>> expressions_;
130 std::vector<std::vector<std::shared_ptr<Parameter>>> instances_;
131};
132} // namespace enzo::prm
Basic attribute, parameter, and node types for Enzo.
ValueType
Which kind of value a parameter stores.
Definition Types.h:221
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:303
floatT previewFloat(const String &expression, String &error) const
Evaluates arbitrary text as an uncommitted live preview.
Definition Parameter.cpp:165
virtual void submitExpressionDependencies_(const expr::ExpressionContext &context, unsigned int index) const
Hands the dependencies an expression captured to the network graph.
Definition Parameter.h:108
floatT clampToRange_(floatT value, unsigned int index) const
Returns the value pulled within a component's locked range bounds.
Definition Parameter.cpp:317
intT previewInt(const String &expression, String &error) const
Evaluates arbitrary text as an uncommitted live preview.
Definition Parameter.cpp:174
String previewString(const String &expression, String &error) const
Evaluates arbitrary text as an uncommitted live preview.
Definition Parameter.cpp:183
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