shape.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* shape.cpp - base for shape creating tools
  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/actions/list.h>
  19. #include <core/actions/change_link.h>
  20. #include <core/action_stack.h>
  21. #include <core/util/exceptions.h>
  22. #include <core/renderable.h>
  23. #include <core/node_tree/transform.h>
  24. #include <util/geom.h>
  25. #include "shape.h"
  26. namespace rainynite::studio::tools {
  27. bool Shape::canvas_event(QEvent* event) {
  28. if (get_canvas() == nullptr)
  29. return false;
  30. if (auto ctx = get_canvas()->get_context()) {
  31. if (auto node = ctx->get_active_node()) {
  32. if (accept(node)) {
  33. if (draw_shape_event(event))
  34. return true;
  35. }
  36. }
  37. }
  38. return false;
  39. return Base::canvas_event(event);
  40. }
  41. void Shape::new_shape_at(QPointF const& pos, MakeShape f) {
  42. if (auto ctx = get_canvas()->get_context()) {
  43. if (auto node = ctx->get_active_node_index()) {
  44. if (auto node_tree = ctx->tree()) {
  45. auto affine = core::get_transform(ctx->get_context(), *node_tree, node);
  46. auto node = f(util::point(pos) * affine.inverse());
  47. write_shape(node);
  48. }
  49. }
  50. }
  51. }
  52. bool Shape::draw_shape_event(QEvent* event) {
  53. if (auto mouse_event = dynamic_cast<QMouseEvent*>(event)) {
  54. switch (mouse_event->type()) {
  55. case QEvent::MouseButtonPress: {
  56. if (mouse_event->button() == Qt::LeftButton) {
  57. return mouse_press(mouse_event->pos());
  58. }
  59. } break;
  60. case QEvent::MouseMove: {
  61. return mouse_move(mouse_event->pos());
  62. } break;
  63. case QEvent::MouseButtonRelease: {
  64. if (mouse_event->button() == Qt::LeftButton) {
  65. return mouse_release(mouse_event->pos());
  66. }
  67. } break;
  68. case QEvent::MouseButtonDblClick: {
  69. if (mouse_event->button() == Qt::LeftButton) {
  70. return editing_done();
  71. }
  72. } break;
  73. default:
  74. break;
  75. }
  76. }
  77. return false;
  78. }
  79. bool Shape::accept(shared_ptr<core::AbstractValue> node) {
  80. // TODO: modularize, support replacing shapes
  81. using namespace core;
  82. auto name = node_name(*node);
  83. return name == "RenderShape"
  84. || name == "Composite"
  85. || node->get_type() == typeid(vector<Renderable>);
  86. }
  87. void Shape::write_shape(shared_ptr<core::AbstractValue> shape) {
  88. using namespace core;
  89. auto action_stack = get_canvas()->get_context()->action_stack();
  90. if (action_stack == nullptr)
  91. throw NullPointerException("Cannot write to empty action stack.");
  92. auto ctx = get_canvas()->get_context();
  93. if (ctx == nullptr)
  94. throw NullPointerException("Cannot write when context is empty.");
  95. auto tree = get_canvas()->get_context()->tree();
  96. if (tree == nullptr)
  97. throw NullPointerException("Tree is null");
  98. auto node_index = ctx->get_active_node_index();
  99. auto value = ctx->get_active_node();
  100. auto name = node_name(*value);
  101. auto node = dynamic_pointer_cast<AbstractNode>(value);
  102. auto add_renderable_to_list = [this, &tree, &action_stack](auto layers, auto layers_index, auto shape) {
  103. using namespace core;
  104. auto render_shape = make_node_with_name_as<AbstractNode>("RenderShape");
  105. render_shape->set_property("shape", shape);
  106. auto layers_path = tree_index_to_path(*tree, layers_index);
  107. action_stack->emplace<actions::ListPush>(tree, layers_index, abstract_value_cast(render_shape));
  108. target_node_index = tree_index_to_path(*tree, tree->index(tree_path_to_index(*tree, layers_path), layers->link_count()-1));
  109. };
  110. // TODO: modularize, support replacing shapes
  111. if (name == "RenderShape") {
  112. action_stack->emplace<actions::SetProperty>(tree, node_index, "shape", shape);
  113. target_node_index = tree_index_to_path(*tree, tree->index(node_index, node->get_name_id("shape")));
  114. } else if (value->get_type() == typeid(vector<Renderable>)) {
  115. if (auto layers = list_cast(std::move(value))) {
  116. add_renderable_to_list(layers, node_index, shape);
  117. } else {
  118. throw std::runtime_error("Has vector<Renderable> type, but isn't list: "+name);
  119. }
  120. } else if (name == "Composite") {
  121. auto layers_node = node->get_property("layers");
  122. if (auto layers = list_cast(layers_node)) {
  123. auto layers_index = tree->index(node_index, node->get_name_id("layers"));
  124. return add_renderable_to_list(layers, layers_index, shape);
  125. } else {
  126. throw std::runtime_error("Can't add layer to Composite node");
  127. }
  128. } else {
  129. throw std::runtime_error("Can't add shape to "+name);
  130. }
  131. }
  132. core::NodeTreeIndex Shape::get_index() const {
  133. auto tree = no_null(get_canvas()->get_context()->tree());
  134. return tree_path_to_index(*tree, target_node_index);
  135. }
  136. } // namespace rainynite::studio::tools