editor.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* editor.cpp - 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. #include <core/node_info.h>
  18. #include <generic/node_editor.h>
  19. #include "editor.h"
  20. #include "registry.h"
  21. namespace rainynite::studio {
  22. shared_ptr<CanvasEditor> add_canvas_named_editor(AbstractCanvas& canvas, string const& name) {
  23. try {
  24. shared_ptr<CanvasEditor> editor = class_init::name_info<AbstractCanvasEditorFactory>(name)();
  25. if (auto context_listener = dynamic_cast<ContextListener*>(editor.get()))
  26. context_listener->set_context(canvas.get_context());
  27. canvas.add_editor(editor);
  28. return editor;
  29. } catch (class_init::RuntimeTypeError const&) {
  30. return nullptr;
  31. }
  32. }
  33. shared_ptr<CanvasEditor> add_canvas_node_editor(AbstractCanvas& canvas, shared_ptr<core::AbstractValue> node) {
  34. if (node == nullptr)
  35. return nullptr;
  36. shared_ptr<CanvasEditor> editor = nullptr;
  37. try {
  38. editor = make_canvas_editor_for(canvas, node->get_type());
  39. } catch (class_init::RuntimeTypeError const&) {
  40. }
  41. if (editor) {
  42. if (auto node_editor = dynamic_cast<NodeEditor*>(editor.get()))
  43. node_editor->set_node(node);
  44. if (auto context_listener = dynamic_cast<ContextListener*>(editor.get()))
  45. context_listener->set_context(canvas.get_context());
  46. canvas.add_editor(editor);
  47. }
  48. bool show_children = false;
  49. try {
  50. show_children = class_init::name_info<NodeEditorShowChildren>(core::node_name(*node))();
  51. } catch (class_init::TypeLookupError const&) {
  52. }
  53. if (show_children) {
  54. // NOTE: this may lead to infinite recursion if node tree is looped
  55. if (auto parent = dynamic_cast<core::AbstractListLinked*>(node.get())) {
  56. for (auto child : parent->get_links())
  57. add_canvas_node_editor(canvas, child);
  58. }
  59. }
  60. return editor;
  61. }
  62. } // namespace rainynite::studio