import.cpp 5.3 KB

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