node_editor.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* node_editor.h - abstract node editor class
  2. * Copyright (C) 2017-2018 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef STUDIO_GENERIC_NODE_EDITOR_H_81406495_5D02_5256_A039_914B9CE337E8
  18. #define STUDIO_GENERIC_NODE_EDITOR_H_81406495_5D02_5256_A039_914B9CE337E8
  19. #include <QWidget>
  20. #include <core/std/memory.h>
  21. #include <core/all_types.h>
  22. #include <core/actions/change_value.h>
  23. #include <core/node/value.h>
  24. #include <core/serialize/exceptions.h>
  25. #include "context_listener.h"
  26. namespace rainynite::core {
  27. class AbstractValue;
  28. }
  29. namespace rainynite::studio {
  30. class NodeEditor : public ContextListener {
  31. public:
  32. virtual ~NodeEditor() {
  33. node_connection.disconnect();
  34. }
  35. void set_update_enabled(bool value) {
  36. update_enabled = value;
  37. }
  38. virtual void node_update() {
  39. }
  40. void set_node(core::NodeTreeIndex index);
  41. /// Make node associated with this editor active/current
  42. void activate_node() {
  43. get_context()->set_active_node(get_node_index());
  44. }
  45. shared_ptr<core::AbstractValue> get_node() const {
  46. return get_context()->get_node(get_node_index());
  47. }
  48. core::NodeTreeIndex get_node_index() const {
  49. return node_index;
  50. }
  51. template <typename T>
  52. optional<T> get_value() const {
  53. if (auto node = get_node_as<T>()) {
  54. return node->value(get_core_context());
  55. }
  56. return {};
  57. }
  58. template <typename T>
  59. shared_ptr<core::BaseValue<T>> get_node_as() const {
  60. return dynamic_pointer_cast<core::BaseValue<T>>(get_node());
  61. }
  62. void write_value(any value);
  63. void close_action();
  64. protected:
  65. bool update_enabled = true;
  66. private:
  67. core::NodeTreeIndex node_index;
  68. boost::signals2::connection node_connection;
  69. };
  70. /// Calculate transform helper
  71. Geom::Affine get_transform(NodeEditor const& editor);
  72. class NodeEditorShowChildren {
  73. public:
  74. virtual bool operator()() const = 0;
  75. };
  76. #define REGISTER_NODE_EDITOR_SHOW_CHILDREN_NAMED(Name, node_name, value) \
  77. class Name##ShowChildren : \
  78. public NodeEditorShowChildren, \
  79. private class_init::StringRegistered<Name##ShowChildren, NodeEditorShowChildren> \
  80. { \
  81. public: \
  82. static std::string name() { \
  83. return node_name; \
  84. } \
  85. public: \
  86. bool operator()() const override { \
  87. return value; \
  88. } \
  89. }
  90. #define REGISTER_NODE_EDITOR_SHOW_CHILDREN(Name, value) \
  91. REGISTER_NODE_EDITOR_SHOW_CHILDREN_NAMED(Name, #Name, value)
  92. #define REGISTER_TEMPLATE_NODE_EDITOR_SHOW_CHILDREN(Name, node_name, value) \
  93. template <typename T> \
  94. class Name##ShowChildren : \
  95. public NodeEditorShowChildren, \
  96. private class_init::StringRegistered< \
  97. Name##ShowChildren<T>, \
  98. NodeEditorShowChildren \
  99. > \
  100. { \
  101. public: \
  102. static std::string name() { \
  103. return node_name+string("/")+core::get_primitive_type(typeid(T))(); \
  104. } \
  105. public: \
  106. bool operator()() const override { \
  107. return value; \
  108. } \
  109. }; \
  110. TYPE_INSTANCES(Name##ShowChildren)
  111. } // namespace rainynite::studio
  112. #endif