Loading [MathJax]/extensions/TeX/AMSmath.js
Radium Engine  1.6.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ParameterSetEditor.cpp
1#include <Gui/ParameterSetEditor/ParameterSetEditor.hpp>
2
3#include <Engine/Data/Material.hpp>
4#include <Gui/Widgets/ControlPanel.hpp>
5
6#include <QString>
7#include <QWidget>
8
9#include <limits>
10#include <memory>
11#include <type_traits>
12
13using json = nlohmann::json;
14
15namespace Ra {
16using namespace Engine;
17namespace Gui {
18
19namespace internal {
22{
23 public:
25
26 RenderParameterUiBuilder( VariableSetEditor* pse, const json& constraints ) :
27 m_pse { pse }, m_constraints { constraints } {}
28
29 void operator()( const std::string& name, bool& p, Core::VariableSet&& /* params */ ) {
30 auto onBoolParameterChanged = [pse = this->m_pse, &p, nm = name]( bool val ) {
31 p = val;
32 emit pse->parameterModified( nm );
33 };
34 if ( m_constraints.contains( name ) ) {
35 if ( m_constraints[name]["editable"] ) {
36 const auto& m = m_constraints[name];
37 std::string description = m.contains( "description" ) ? m["description"] : "";
38 std::string nm = m.contains( "name" ) ? std::string { m["name"] } : name;
39 m_pse->addOption( nm, onBoolParameterChanged, p, description );
40 }
41 }
42 else if ( m_pse->showUnspecified() ) {
43 m_pse->addOption( name, onBoolParameterChanged, p );
44 }
45 }
46
47 template <typename TParam, std::enable_if_t<std::is_arithmetic<TParam>::value, bool> = true>
48 void operator()( const std::string& name, TParam& p, Core::VariableSet&& params ) {
49 using namespace Ra::Core::VariableSetEnumManagement;
50 if ( getEnumConverter<TParam>( params, name ) ) {
51 m_pse->addEnumWidget( name, p, params, m_constraints );
52 }
53 else {
54 // case number
55 m_pse->addNumberWidget( name, p, params, m_constraints );
56 }
57 }
58
59 template <typename TParam,
60 typename TAllocator,
61 std::enable_if_t<std::is_arithmetic<TParam>::value, bool> = true>
62 void operator()( const std::string& name,
64 Core::VariableSet&& params ) {
65 m_pse->addVectorWidget( name, p, params, m_constraints );
66 }
67
68 void
69 operator()( const std::string& name, Ra::Core::Utils::Color& p, Core::VariableSet&& params ) {
70 auto onColorParameterChanged =
71 [pse = this->m_pse, &params, nm = name]( const Ra::Core::Utils::Color& val ) {
72 params.setVariable( nm, val );
73 emit pse->parameterModified( nm );
74 };
75 if ( m_constraints.contains( name ) ) {
76 const auto& m = m_constraints[name];
77 std::string description = m.contains( "description" ) ? m["description"] : "";
78 std::string nm = m.contains( "name" ) ? std::string { m["name"] } : name;
79 m_pse->addColorInput( nm, onColorParameterChanged, p, m["maxItems"] == 4, description );
80 }
81 else if ( m_pse->showUnspecified() ) {
82 m_pse->addColorInput( name, onColorParameterChanged, p );
83 }
84 }
85
86 template <template <typename, int...> typename M, typename T, int... dim>
87 void operator()( const std::string& name, M<T, dim...>& p, Core::VariableSet&& params ) {
88 m_pse->addMatrixWidget( name, p, params, m_constraints );
89 }
90
91 void operator()( const std::string& /*name*/,
93 Core::VariableSet&& /*params*/ ) {
94 // textures are not yet editable
95 }
96
97 void operator()( const std::string& name,
99 Core::VariableSet&& /*params*/ ) {
100 m_pse->addLabel( name );
101 p.visit( *this, p );
102 }
103
104 private:
105 VariableSetEditor* m_pse { nullptr };
106 const json& m_constraints;
107};
108} // namespace internal
109
110VariableSetEditor::VariableSetEditor( const std::string& name, QWidget* parent ) :
111 ControlPanel( name, !name.empty(), parent ) {}
112
113void VariableSetEditor::setupUi( Core::VariableSet& params, const nlohmann::json& constraints ) {
114
115 internal::RenderParameterUiBuilder uiBuilder { this, constraints };
116 params.visit( uiBuilder, params );
117 addStretch( 0 );
118 setVisible( true );
119}
120
121} // namespace Gui
122} // namespace Ra
Heterogeneous container storing "Variables", that maps a name (std::string) to a value (of any type T...
void visit(F &&visitor) const
Visit the container using a user defined visitor.
Management of shader parameters with automatic binding of a named parameter to the corresponding glsl...
Core::Utils::TypeList< bool, Core::Utils::Color, int, uint, Scalar, TextureInfo, std::vector< int >, std::vector< uint >, std::vector< Scalar >, Core::Vector2, Core::Vector3, Core::Vector4, Core::Matrix2, Core::Matrix3, Core::Matrix4, RenderParameters > BindableTypes
List of bindable types, to be used with static visitors.
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 setupUi(Core::VariableSet &params, const nlohmann::json &constraints)
Setup UI according to params and constraints.
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 parameterModified(const std::string &name)
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="")
T internal(T... args)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:4