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 == "xyz")
95 parameter.setStyle(Xyz{});
96 else if (styleName == "rangeCircle")
97 parameter.setStyle(RangeCircle{});
98 else if (styleName == "attribute")
99 parameter.setStyle(Attribute{});
100 else
101 throw std::runtime_error("unknown style " + styleName);
102}
103
105template <typename T> bool holds(const std::any& style)
106{
107 return std::any_cast<std::shared_ptr<T>>(&style) != nullptr;
108}
109
112inline std::string toString(const std::any& style)
113{
114 if (holds<BoolSwitch>(style)) return "boolSwitch";
115 if (holds<BoolIcon>(style)) return "boolIcon";
116 if (holds<Xyz>(style)) return "xyz";
117 if (holds<RangeCircle>(style)) return "rangeCircle";
118 if (holds<Attribute>(style)) return "attribute";
119 return "";
120}
121
126inline std::vector<std::shared_ptr<prm::Parameter>> options(const std::any& style)
127{
128 if (const auto* boolIcon = std::any_cast<std::shared_ptr<BoolIcon>>(&style))
129 return (*boolIcon)->options;
130 if (const auto* rangeCircle = std::any_cast<std::shared_ptr<RangeCircle>>(&style))
131 return (*rangeCircle)->options;
132 if (const auto* attribute = std::any_cast<std::shared_ptr<Attribute>>(&style))
133 return (*attribute)->options;
134 return {};
135}
136
144template <typename Parse>
146 const std::string& optionValue,
147 const std::vector<const prm::Template*>& parameters,
148 Parse parseValue
149)
150{
151 const std::optional<std::string> name = getReferencedParameterName(optionValue);
152 if (!name)
153 {
154 parseValue(optionValue);
155 return;
156 }
157
158 for (const prm::Template* parameter : parameters)
159 {
160 if (parameter->getName() != *name) continue;
161
162 for (const prm::Name& choice : parameter->getOptions())
163 parseValue(choice.getToken());
164 return;
165 }
166
167 throw std::runtime_error("no parameter named " + *name + " to read a style option from");
168}
169
172inline void
173validateOptions(const std::any& style, const std::vector<const prm::Template*>& parameters)
174{
175 const auto* attribute = std::any_cast<std::shared_ptr<Attribute>>(&style);
176 if (!attribute) return;
177
178 validateOption((*attribute)->owners(), parameters, parseOwners);
179 validateOption((*attribute)->attributeTypes(), parameters, parseAttributeTypes);
180}
181
184inline std::shared_ptr<prm::Parameter> getOption(const std::any& style, const std::string& name)
185{
186 for (const std::shared_ptr<prm::Parameter>& option : options(style))
187 if (option->getName() == name) return option;
188 return nullptr;
189}
190
191} // 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:105
void validateOptions(const std::any &style, const std::vector< const prm::Template * > &parameters)
Checks the values a style's options hold.
Definition StyleAccess.h:173
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:184
std::vector< std::shared_ptr< prm::Parameter > > options(const std::any &style)
Returns the options a style exposes to node.yaml.
Definition StyleAccess.h:126
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:145
std::string toString(Type type)
The lowercase canonical name of a parameter type, e.g. "dropdown".
Definition Types.h:228
Definition PrmName.h:6
Definition Template.h:18
Definition Styles.h:79
Definition Styles.h:56
Definition Styles.h:20
Definition Styles.h:30
Definition Styles.h:25