7#include "Engine/Parameter/Parameter.h"
9#include "Engine/Parameter/Template.h"
19namespace enzo::prm::style {
24inline std::vector<std::string>
splitNames(
const std::string& text)
26 std::istringstream stream(text);
27 std::vector<std::string> names;
28 for (std::string name; stream >> name;)
29 names.push_back(name);
35inline std::vector<attr::AttributeOwner>
parseOwners(
const std::string& text)
37 std::vector<attr::AttributeOwner> owners;
38 for (
const std::string& name :
splitNames(text))
40 if (name ==
"all")
return attr::getAllOwners();
42 const std::optional<attr::AttributeOwner> owner = attr::getOwner(name);
43 if (!owner)
throw std::runtime_error(
"unknown attribute owner " + name);
45 owners.push_back(*owner);
54 std::vector<attr::AttributeType> types;
55 for (
const std::string& name :
splitNames(text))
57 if (name ==
"all")
return attr::getAllTypes();
59 const std::optional<attr::AttributeType> type = attr::getType(name);
60 if (!type)
throw std::runtime_error(
"unknown attribute type " + name);
62 types.push_back(*type);
73 const std::vector<std::string> words =
splitNames(text);
74 if (words.size() != 1)
return std::nullopt;
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;
80 const std::string name = word.substr(call.size(), word.size() - call.size() - 1);
81 if (name.empty())
return std::nullopt;
90 if (styleName ==
"boolSwitch")
92 else if (styleName ==
"boolIcon")
94 else if (styleName ==
"code")
95 parameter.setStyle(
Code{});
96 else if (styleName ==
"xyz")
97 parameter.setStyle(
Xyz{});
98 else if (styleName ==
"rangeCircle")
100 else if (styleName ==
"attribute")
103 throw std::runtime_error(
"unknown style " + styleName);
107template <
typename T>
bool holds(
const std::any& style)
109 return std::any_cast<std::shared_ptr<T>>(&style) !=
nullptr;
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";
129inline std::vector<std::shared_ptr<prm::Parameter>>
options(
const std::any& style)
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;
147template <
typename Parse>
149 const std::string& optionValue,
150 const std::vector<const prm::Template*>& parameters,
157 parseValue(optionValue);
163 if (parameter->getName() != *name)
continue;
165 for (
const prm::Name& choice : parameter->getOptions())
166 parseValue(choice.getToken());
170 throw std::runtime_error(
"no parameter named " + *name +
" to read a style option from");
176validateOptions(
const std::any& style,
const std::vector<const prm::Template*>& parameters)
178 const auto* attribute = std::any_cast<std::shared_ptr<Attribute>>(&style);
179 if (!attribute)
return;
187inline std::shared_ptr<prm::Parameter>
getOption(
const std::any& style,
const std::string& name)
189 for (
const std::shared_ptr<prm::Parameter>& option :
options(style))
190 if (option->getName() == name)
return option;
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 * > ¶meters)
Checks the values a style's options hold.
Definition StyleAccess.h:176
void attachStyle(prm::Template ¶meter, 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 * > ¶meters, 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