Loading [MathJax]/extensions/tex2jax.js
Radium Engine  1.5.29
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BasicUiBuilder.hpp
1#pragma once
2
3#include <Gui/RaGui.hpp>
4
5#include <QGroupBox>
6#include <QLabel>
7#include <QPlainTextEdit>
8#include <QVBoxLayout>
9#include <QWidget>
10
11#include <nlohmann/json.hpp>
12
13#include <Core/Containers/DynamicVisitor.hpp>
14#include <Core/Containers/VariableSet.hpp>
15#include <Core/Containers/VariableSetEnumManagement.hpp>
16#include <Gui/ParameterSetEditor/ParameterSetEditor.hpp>
17#include <Gui/Widgets/ControlPanel.hpp>
18
19namespace Ra {
20
21namespace Gui {
22
23class BasicUiBuilder : public Ra::Core::DynamicVisitor
24{
25 public:
26 using VariableSet = Ra::Core::VariableSet;
27 using RenderParameters = Ra::Engine::Data::RenderParameters;
28
29 BasicUiBuilder( VariableSet& params,
30 VariableSetEditor* pse,
31 const nlohmann::json& constraints ) :
32 m_params { params }, m_pse { pse }, m_constraints { constraints } {
33 addOperator<bool>( *this );
34 addOperator<int>( *this );
36 addOperator<Scalar>( *this );
45 }
46
47 virtual ~BasicUiBuilder() = default;
48
49 void operator()( const std::string& name, bool& p ) {
50 auto onBoolParameterChanged = [pse = this->m_pse, &p, nm = name]( bool val ) {
51 p = val;
52 emit pse->parameterModified( nm );
53 };
54 if ( m_constraints.contains( name ) ) {
55 if ( m_constraints[name]["editable"] ) {
56 const auto& m = m_constraints[name];
57 std::string description = m.contains( "description" ) ? m["description"] : "";
58 std::string nm = m.contains( "name" ) ? std::string { m["name"] } : name;
59 m_pse->addOption( nm, onBoolParameterChanged, p, description );
60 }
61 }
62 else if ( m_pse->showUnspecified() ) {
63 m_pse->addOption( name, onBoolParameterChanged, p );
64 }
65 }
66
67 template <typename TParam, std::enable_if_t<std::is_arithmetic<TParam>::value, bool> = true>
68 void operator()( const std::string& name, TParam& p ) {
69 using namespace Ra::Core::VariableSetEnumManagement;
70 if ( getEnumConverter<TParam>( m_params, name ) ) {
71 // known enum
72 m_pse->addEnumWidget( name, p, m_params, m_constraints );
73 }
74 else {
75 // number (or unknown enum)
76 m_pse->addNumberWidget( name, p, m_params, m_constraints );
77 }
78 }
79
80 template <typename TParam,
81 typename TAllocator,
82 std::enable_if_t<std::is_arithmetic<TParam>::value, bool> = true>
83 void operator()( const std::string& name, std::vector<TParam, TAllocator>& p ) {
84 m_pse->addVectorWidget( name, p, m_params, m_constraints );
85 }
86
87 void operator()( const std::string& name, Ra::Core::Utils::Color& p ) {
88 auto onColorParameterChanged =
89 [pse = this->m_pse, &p, nm = name]( const Ra::Core::Utils::Color& val ) {
90 p = val;
91 emit pse->parameterModified( nm );
92 };
93 if ( m_constraints.contains( name ) ) {
94 const auto& m = m_constraints[name];
95 std::string description = m.contains( "description" ) ? m["description"] : "";
96 std::string nm = m.contains( "name" ) ? std::string { m["name"] } : name;
97 m_pse->addColorInput( nm, onColorParameterChanged, p, m["maxItems"] == 4, description );
98 }
99 else if ( m_pse->showUnspecified() ) {
100 m_pse->addColorInput( name, onColorParameterChanged, p );
101 }
102 }
103
104 template <typename T, int... dim>
105 void operator()( const std::string& name, Eigen::Matrix<T, dim...>& p ) {
106 std::cerr << "ui builder matrix\n";
107 m_pse->addMatrixWidget( name, p, m_params, m_constraints );
108 }
109
110 template <typename T,
111 std::enable_if_t<std::is_assignable_v<VariableSet, typename std::decay<T>::type>,
112 bool> = true>
113 void operator()( const std::string& name, T& p ) {
114 m_pse->addLabel( name );
115 p.visit( *this, p );
116 }
117
118 VariableSet& variable_set() { return m_params; }
119 VariableSetEditor* variable_set_editor() { return m_pse; }
120 const nlohmann::json& constraints() { return m_constraints; }
121
122 private:
123 VariableSet& m_params;
124 VariableSetEditor* m_pse { nullptr };
125 const nlohmann::json& m_constraints;
126};
127
128} // namespace Gui
129} // namespace Ra
Base class for visitors with configurable per-type callbacks. Visiting will be prepared at running ti...
bool addOperator(F &&f)
Add a visiting operator.
Heterogeneous container storing "Variables", that maps a name (std::string) to a value (of any type T...
Management of shader parameters with automatic binding of a named parameter to the corresponding glsl...
Simple Widget for RenderParameter editing The editor will expose a control panel containing all of th...
void addNumberWidget(const std::string &name, T &initial, Core::VariableSet &params, const nlohmann::json &metadata)
void addVectorWidget(const std::string &key, std::vector< T > &initial, Core::VariableSet &params, const nlohmann::json &metadata)
void addEnumWidget(const std::string &name, T &initial, Core::VariableSet &params, const nlohmann::json &paramMetadata)
Add a combobox allowing to chose the value of an enumerator.
void addMatrixWidget(const std::string &key, T &initial, Core::VariableSet &params, const nlohmann::json &metadata)
void addColorInput(const std::string &name, const std::function< void(const Ra::Core::Utils::Color &clr)> &callback, Ra::Core::Utils::Color color=Ra::Core::Utils::Color::Black(), bool withAlpha=true, const std::string &tooltip="")
void addLabel(const std::string &text)
void addOption(const std::string &name, std::function< void(bool)> callback, bool set=false, const std::string &tooltip="")
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:4