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 ==
"xyz")
95 parameter.setStyle(
Xyz{});
96 else if (styleName ==
"rangeCircle")
98 else if (styleName ==
"attribute")
101 throw std::runtime_error(
"unknown style " + styleName);
105template <
typename T>
bool holds(
const std::any& style)
107 return std::any_cast<std::shared_ptr<T>>(&style) !=
nullptr;
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";
126inline std::vector<std::shared_ptr<prm::Parameter>>
options(
const std::any& style)
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;
144template <
typename Parse>
146 const std::string& optionValue,
147 const std::vector<const prm::Template*>& parameters,
154 parseValue(optionValue);
160 if (parameter->getName() != *name)
continue;
162 for (
const prm::Name& choice : parameter->getOptions())
163 parseValue(choice.getToken());
167 throw std::runtime_error(
"no parameter named " + *name +
" to read a style option from");
173validateOptions(
const std::any& style,
const std::vector<const prm::Template*>& parameters)
175 const auto* attribute = std::any_cast<std::shared_ptr<Attribute>>(&style);
176 if (!attribute)
return;
184inline std::shared_ptr<prm::Parameter>
getOption(
const std::any& style,
const std::string& name)
186 for (
const std::shared_ptr<prm::Parameter>& option :
options(style))
187 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:105
void validateOptions(const std::any &style, const std::vector< const prm::Template * > ¶meters)
Checks the values a style's options hold.
Definition StyleAccess.h:173
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: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 * > ¶meters, 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