abstract_editor.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* abstract_editor.h - abstract canvas editor
  2. * Copyright (C) 2017 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_CANVAS_ABSTRACT_EDITOR_H_78E5E734_53ED_511D_B6F7_AD0003B70709
  18. #define STUDIO_CANVAS_ABSTRACT_EDITOR_H_78E5E734_53ED_511D_B6F7_AD0003B70709
  19. #include <core/std/vector.h>
  20. #include <core/abstract_factory.h>
  21. #include <core/util/class_init.h>
  22. #include <core/node_tree/index.h>
  23. #include <core/node_info/node_info.h>
  24. #include "attachable.h"
  25. namespace rainynite::core {
  26. class AbstractValue;
  27. }
  28. namespace rainynite::studio {
  29. class AbstractCanvasEditor : public CanvasAttachable {
  30. };
  31. struct AbstractAbstractCanvasEditorFactory : public AbstractFactory<AbstractCanvasEditor> {
  32. };
  33. template <class CanvasT>
  34. struct AbstractCanvasEditorFactory : public AbstractAbstractCanvasEditorFactory {
  35. };
  36. template <class CanvasT>
  37. struct AbstractCanvasNodeEditorFactory : public AbstractAbstractCanvasEditorFactory {
  38. };
  39. template <class CanvasT, class EditorT>
  40. struct AbstractCanvasEditorFactoryI : public AbstractCanvasEditorFactory<CanvasT>
  41. {
  42. unique_ptr<AbstractCanvasEditor> operator()() const override {
  43. return make_unique<EditorT>();
  44. }
  45. };
  46. template <class CanvasT, class EditorT>
  47. struct AbstractCanvasNodeEditorFactoryI : public AbstractCanvasNodeEditorFactory<CanvasT>
  48. {
  49. unique_ptr<AbstractCanvasEditor> operator()() const override {
  50. return make_unique<EditorT>();
  51. }
  52. };
  53. template <class CanvasT, class EditorT, class Type>
  54. struct AbstractCanvasEditorFactoryInstance :
  55. public AbstractCanvasEditorFactoryI<CanvasT, EditorT>,
  56. private class_init::Registered<
  57. AbstractCanvasEditorFactoryInstance<CanvasT, EditorT, Type>,
  58. Type,
  59. AbstractCanvasEditorFactory<CanvasT>
  60. >
  61. {
  62. };
  63. template <class CanvasT, class EditorT, class NodeT>
  64. struct AbstractCanvasNodeEditorFactoryInstance :
  65. public AbstractCanvasNodeEditorFactoryI<CanvasT, EditorT>,
  66. private class_init::Registered<
  67. AbstractCanvasNodeEditorFactoryInstance<CanvasT, EditorT, NodeT>,
  68. NodeT,
  69. AbstractCanvasNodeEditorFactory<CanvasT>
  70. >
  71. {
  72. };
  73. #define REGISTER_CANVAS_EDITOR(CanvasT, EditorT, Type) \
  74. template struct AbstractCanvasEditorFactoryInstance<CanvasT, EditorT, Type>
  75. #define REGISTER_CANVAS_NODE_EDITOR(CanvasT, EditorT, NodeT) \
  76. template struct AbstractCanvasNodeEditorFactoryInstance<CanvasT, EditorT, NodeT>
  77. #define REGISTER_CANVAS_TEMPLATE_NODE_EDITOR(CanvasT, EditorT, Name) \
  78. template <typename T> \
  79. class Name##CanvasT##NodeEditorFactoryInstance : \
  80. public AbstractCanvasNodeEditorFactoryI<CanvasT, EditorT>, \
  81. private class_init::StringRegistered< \
  82. Name##CanvasT##NodeEditorFactoryInstance<T>, \
  83. AbstractCanvasNodeEditorFactory<CanvasT> \
  84. > \
  85. { \
  86. public: \
  87. static string name() {\
  88. return #Name+string("/")+core::get_primitive_type(typeid(T))(); \
  89. } \
  90. }; \
  91. TYPE_INSTANCES(Name##CanvasT##NodeEditorFactoryInstance)
  92. #define REGISTER_CANVAS_EDITOR_NAME(CanvasT, EditorT, Name) \
  93. struct Name##AbstractCanvasEditorNameInfoInstance : \
  94. public AbstractCanvasEditorFactoryI<CanvasT, EditorT>, \
  95. private class_init::StringRegistered< \
  96. Name##AbstractCanvasEditorNameInfoInstance, \
  97. AbstractAbstractCanvasEditorFactory \
  98. > \
  99. { \
  100. static string name() { \
  101. return #Name; \
  102. } \
  103. }
  104. template <class CanvasT>
  105. unique_ptr<AbstractCanvasEditor> make_canvas_editor(Type type) {
  106. return class_init::type_info<AbstractCanvasEditorFactory<CanvasT>,unique_ptr<AbstractCanvasEditor>>(type);
  107. }
  108. template <class CanvasT>
  109. unique_ptr<AbstractCanvasEditor> make_canvas_node_editor(std::type_index node_type) {
  110. using core::node_type_name;
  111. return class_init::name_info<AbstractCanvasNodeEditorFactory<CanvasT>>(node_type_name(node_type))();
  112. }
  113. /// Create & add canvas editor by name
  114. shared_ptr<AbstractCanvasEditor> add_canvas_named_editor(AbstractCanvas& canvas, string const& name);
  115. /// Create & add canvas node editor to canvas
  116. vector<shared_ptr<AbstractCanvasEditor>> add_canvas_node_editor(AbstractCanvas& canvas, core::NodeTreeIndex index);
  117. } // namespace rainynite::studio
  118. #endif