Enzo
Loading...
Searching...
No Matches
StyleAccess.h
Go to the documentation of this file.
1#pragma once
2
6
7#include "Engine/Parameter/Parameter.h"
9#include "Engine/Parameter/Template.h"
10
11#include <any>
12#include <memory>
13#include <optional>
14#include <sstream>
15#include <stdexcept>
16#include <string>
17#include <vector>
18
19namespace enzo::prm::style {
20
24inline std::vector<std::string> splitNames(const std::string& text)
25{
26 std::istringstream stream(text);
27 std::vector<std::string> names;
28 for (std::string name; stream >> name;)
29 names.push_back(name);
30 return names;
31}
32
35inline std::vector<attr::AttributeOwner> parseOwners(const std::string& text)
36{
37 std::vector<attr::AttributeOwner> owners;
38 for (const std::string& name : splitNames(text))
39 {
40 if (name == "all") return attr::getAllOwners();
41
42 const std::optional<attr::AttributeOwner> owner = attr::getOwner(name);
43 if (!owner) throw std::runtime_error("unknown attribute owner " + name);
44
45 owners.push_back(*owner);
46 }
47 return owners;
48}
49
52inline std::vector<attr::AttributeType> parseAttributeTypes(const std::string& text)
53{
54 std::vector<attr::AttributeType> types;
55 for (const std::string& name : splitNames(text))
56 {
57 if (name == "all") return attr::getAllTypes();
58
59 const std::optional<attr::AttributeType> type = attr::getType(name);
60 if (!type) throw std::runtime_error("unknown attribute type " + name);
61
62 types.push_back(*type);
63 }
64 return types;
65}
66
71inline std::optional<std::string> getReferencedParameterName(const std::string& text)
72{
73 const std::vector<std::string> words = splitNames(text);
74 if (words.size() != 1) return std::nullopt;
75
76 const std::string& word = words.front();
77 const std::string call = "prm(";
78 if (!word.starts_with(call) || !word.ends_with(")")) return std::nullopt;
79
80 const std::string name = word.substr(call.size(), word.size() - call.size() - 1);
81 if (name.empty()) return std::nullopt;
82
83 return name;
84}
85
88inline void attachStyle(prm::Template& parameter, const std::string& styleName)
89{
90 if (styleName == "boolSwitch")
91 parameter.setStyle(BoolSwitch{});
92 else if (styleName == "boolIcon")
93 parameter.setStyle(BoolIcon{});
94 else if (styleName == "code")
95 parameter.setStyle(Code{});
96 else if (styleName == "xyz")
97 parameter.setStyle(Xyz{});
98 else if (styleName == "rangeCircle")
99 parameter.setStyle(RangeCircle{});
100 else if (styleName == "attribute")
101 parameter.setStyle(Attribute{});
102 else
103 throw std::runtime_error("unknown style " + styleName);
104}
105
107template <typename T> bool holds(const std::any& style)
108{
109 return std::any_cast<std::shared_ptr<T>>(&style) != nullptr;
110}
111
114inline std::string toString(const std::any& style)
115{
116 if (holds<BoolSwitch>(style)) return "boolSwitch";
117 if (holds<BoolIcon>(style)) return "boolIcon";
118 if (holds<Code>(style)) return "code";
119 if (holds<Xyz>(style)) return "xyz";
120 if (holds<RangeCircle>(style)) return "rangeCircle";
121 if (holds<Attribute>(style)) return "attribute";
122 return "";
123}
124
129inline std::vector<std::shared_ptr<prm::Parameter>> options(const std::any& style)
130{
131 if (const auto* boolIcon = std::any_cast<std::shared_ptr<BoolIcon>>(&style))
132 return (*boolIcon)->options;
133 if (const auto* rangeCircle = std::any_cast<std::shared_ptr<RangeCircle>>(&style))
134 return (*rangeCircle)->options;
135 if (const auto* attribute = std::any_cast<std::shared_ptr<Attribute>>(&style))
136 return (*attribute)->options;
137 return {};
138}
139
147template <typename Parse>
149 const std::string& optionValue,
150 const std::vector<const prm::Template*>& parameters,
151 Parse parseValue
152)
153{
154 const std::optional<std::string> name = getReferencedParameterName(optionValue);
155 if (!name)
156 {
157 parseValue(optionValue);
158 return;
159 }
160
161 for (const prm::Template* parameter : parameters)
162 {
163 if (parameter->getName() != *name) continue;
164
165 for (const prm::Name& choice : parameter->getOptions())
166 parseValue(choice.getToken());
167 return;
168 }
169
170 throw std::runtime_error("no parameter named " + *name + " to read a style option from");
171}
172
175inline void
176validateOptions(const std::any& style, const std::vector<const prm::Template*>& parameters)
177{
178 const auto* attribute = std::any_cast<std::shared_ptr<Attribute>>(&style);
179 if (!attribute) return;
180
181 validateOption((*attribute)->owners(), parameters, parseOwners);
182 validateOption((*attribute)->attributeTypes(), parameters, parseAttributeTypes);
183}
184
187inline std::shared_ptr<prm::Parameter> getOption(const std::any& style, const std::string& name)
188{
189 for (const std::shared_ptr<prm::Parameter>& option : options(style))
190 if (option->getName() == name) return option;
191 return nullptr;
192}
193
194} // namespace enzo::prm::style
std::vector< std::string > splitNames(const std::string &text)
Returns the names in a option written as a space separated list.
Definition StyleAccess.h:24
std::vector< attr::AttributeOwner > parseOwners(const std::string &text)
Returns the attribute owners a option names, e.g. "point vertex".
Definition StyleAccess.h:35
bool holds(const std::any &style)
Returns whether a style is of a particular kind.
Definition StyleAccess.h:107
void validateOptions(const std::any &style, const std::vector< const prm::Template * > &parameters)
Checks the values a style's options hold.
Definition StyleAccess.h:176
void attachStyle(prm::Template &parameter, const std::string &styleName)
Attaches the style a node.yaml name asks for.
Definition StyleAccess.h:88
std::optional< std::string > getReferencedParameterName(const std::string &text)
Returns the name of the parameter a option reads its value from.
Definition StyleAccess.h:71
std::shared_ptr< prm::Parameter > getOption(const std::any &style, const std::string &name)
Returns the option a style exposes under a name, e.g. "scale".
Definition StyleAccess.h:187
std::vector< std::shared_ptr< prm::Parameter > > options(const std::any &style)
Returns the options a style exposes to node.yaml.
Definition StyleAccess.h:129
std::vector< attr::AttributeType > parseAttributeTypes(const std::string &text)
Returns the attribute types a option names, e.g. "float vector".
Definition StyleAccess.h:52
void validateOption(const std::string &optionValue, const std::vector< const prm::Template * > &parameters, Parse parseValue)
Checks the value a style option holds.
Definition StyleAccess.h:148
std::string toString(Type type)
The lowercase canonical name of a parameter type, e.g. "dropdown".
Definition Types.h:257
Definition PrmName.h:6
Definition Template.h:18
Definition Styles.h:84
Definition Styles.h:61
Definition Styles.h:20
Definition Styles.h:25
Definition Styles.h:35
Definition Styles.h:30