import.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* import.cpp - "import" actions
  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. #include <core/node_info/node_info.h>
  18. #include <core/node/make.h>
  19. #include <core/action_stack.h>
  20. #include <core/actions/list.h>
  21. #include <core/renderable.h>
  22. #include <generic/process_node.h>
  23. #include "import.h"
  24. namespace rainynite::studio {
  25. struct ImportFilesTag {};
  26. struct ImportFramesTag {};
  27. struct ImportSvgLayersTag {};
  28. using ImportFilesProcessor = ProcessNode<string, ImportFilesTag>;
  29. using ImportFramesProcessor = ProcessNode<string, ImportFramesTag>;
  30. using ImportSvgLayersProcessor = ProcessNode<string, ImportSvgLayersTag>;
  31. class FillStringList : public ImportFilesProcessor {
  32. public:
  33. bool accept(core::NodeTreeIndex node) const override {
  34. return get_context()->tree()->type_of(node).accept(typeid(vector<string>));
  35. }
  36. void feed(string const& s) override {
  37. auto string_node = core::make_value<string>(std::move(s));
  38. auto ctx = no_null(get_context());
  39. ctx->action_stack()->emplace<core::actions::ListPush>(ctx->tree(), get_node_index(), string_node);
  40. }
  41. };
  42. REGISTER_PROCESS_NODE(FillStringList, ImportFilesProcessor)
  43. class FillRenderableList : public ImportFilesProcessor {
  44. public:
  45. bool accept(core::NodeTreeIndex node) const override {
  46. return get_context()->tree()->type_of(node).accept(typeid(vector<core::Renderable>));
  47. }
  48. void feed(string const& s) override {
  49. auto string_node = core::make_value<string>(std::move(s));
  50. auto image_node = core::make_node_with_name_as<core::AbstractNode>("Image");
  51. image_node->set_property("file_path", string_node);
  52. // TODO: set size
  53. auto ctx = no_null(get_context());
  54. ctx->action_stack()->emplace<core::actions::ListPush>(ctx->tree(), get_node_index(), abstract_value_cast(image_node));
  55. }
  56. };
  57. REGISTER_PROCESS_NODE(FillRenderableList, ImportFilesProcessor)
  58. class AddFramedAnimationToRenderableList : public ImportFramesProcessor {
  59. public:
  60. bool accept(core::NodeTreeIndex node) const override {
  61. return get_context()->tree()->type_of(node).accept(typeid(vector<core::Renderable>));
  62. }
  63. void start_list() override {
  64. file_name_list_node = core::make_node_with_name_as<core::AbstractListLinked>("List/String");
  65. }
  66. void feed(string const& s) override {
  67. push_value(file_name_list_node, std::move(s));
  68. }
  69. void end_list() override {
  70. auto image_node = core::make_node_with_name_as<core::AbstractNode>("Image");
  71. auto file_name_node = core::make_node_with_name_as<core::AbstractNode>("ListElement/String");
  72. auto linear_node = core::make_node_with_name_as<core::AbstractNode>("Linear");
  73. linear_node->set_property("speed", core::make_value<double>(get_core_context()->get_fps()));
  74. file_name_node->set_property("source", abstract_value_cast(file_name_list_node));
  75. file_name_node->set_property("n", abstract_value_cast(linear_node));
  76. image_node->set_property("file_path", abstract_value_cast(file_name_node));
  77. auto ctx = get_context();
  78. ctx->action_stack()->emplace<core::actions::ListPush>(ctx->tree(), get_node_index(), abstract_value_cast(image_node));
  79. }
  80. private:
  81. shared_ptr<core::AbstractListLinked> file_name_list_node;
  82. };
  83. REGISTER_PROCESS_NODE(AddFramedAnimationToRenderableList, ImportFramesProcessor)
  84. class FillRenderableListWithSvgs : public ImportSvgLayersProcessor {
  85. public:
  86. bool accept(core::NodeTreeIndex node) const override {
  87. return get_context()->tree()->type_of(node).accept(typeid(vector<core::Renderable>));
  88. }
  89. void feed(string const& s) override {
  90. auto string_node = core::make_value<string>(std::move(s));
  91. auto image_node = core::make_node_with_name_as<core::AbstractNode>("EmbedSvg");
  92. image_node->set_property("file_path", string_node);
  93. auto ctx = get_context();
  94. ctx->action_stack()->emplace<core::actions::ListPush>(ctx->tree(), get_node_index(), abstract_value_cast(image_node));
  95. }
  96. };
  97. REGISTER_PROCESS_NODE(FillRenderableListWithSvgs, ImportSvgLayersProcessor)
  98. namespace actions {
  99. /**
  100. * Import file paths as Image layers
  101. */
  102. class ImportRasterLayers :
  103. public ImportFiles<ImportFilesProcessor>,
  104. REGISTERED_ACTION(ImportRasterLayers)
  105. {
  106. ACTION_NAME("Import raster layers")
  107. };
  108. /**
  109. * Import file paths as frames
  110. */
  111. class ImportRasterFrames :
  112. public ImportFiles<ImportFramesProcessor>,
  113. REGISTERED_ACTION(ImportRasterFrames)
  114. {
  115. ACTION_NAME("Import raster frames")
  116. };
  117. /**
  118. * Import svgs
  119. */
  120. class ImportSvgLayers :
  121. public ImportFiles<ImportSvgLayersProcessor>,
  122. REGISTERED_ACTION(ImportSvgLayers)
  123. {
  124. ACTION_NAME("Import svg layers")
  125. };
  126. } // namespace actions
  127. } // namespace rainynite::studio