bezier_editor.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* bezier_editor.cpp - edit beziers on canvas
  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 <fmt/format.h>
  18. #include <QGraphicsItem>
  19. #include <QMouseEvent>
  20. #include <QInputDialog>
  21. #include <QLineEdit>
  22. #include <core/actions/change_value.h>
  23. #include <core/util/nullptr.h>
  24. #include <core/node_tree/transform.h>
  25. #include <util/strings.h>
  26. #include <util/pen.h>
  27. #include <util/qt_path.h>
  28. #include <util/geom.h>
  29. #include <widgets/canvas.h>
  30. #include "point_item.h"
  31. #include "bezier_editor.h"
  32. using namespace fmt::literals;
  33. namespace rainynite::studio {
  34. BezierEditor::BezierEditor() {
  35. set_curve_pen(pens::cosmetic_dash());
  36. }
  37. BezierEditor::~BezierEditor() {
  38. uninit();
  39. }
  40. void BezierEditor::set_display_tags(bool display_tags_) {
  41. if (display_tags != display_tags_) {
  42. display_tags = display_tags_;
  43. if (display_tags) {
  44. add_tags();
  45. } else {
  46. remove_tags();
  47. }
  48. }
  49. }
  50. void BezierEditor::set_curve_pen(QPen const& pen) {
  51. curve_pen = pen;
  52. if (curve_item)
  53. curve_item->setPen(curve_pen);
  54. }
  55. struct BezierEditor::EventFilter : public QObject {
  56. EventFilter(BezierEditor* parent_) :
  57. parent(parent_)
  58. {}
  59. bool eventFilter(QObject* /*target*/, QEvent* event) override {
  60. return parent->canvas_event(event);
  61. }
  62. BezierEditor* parent;
  63. };
  64. void BezierEditor::setup_canvas() {
  65. uninit();
  66. if (auto canvas = get_canvas()) {
  67. if (get_bezier_node()) {
  68. init();
  69. }
  70. event_filter = make_unique<EventFilter>(this);
  71. canvas->installEventFilter(event_filter.get());
  72. }
  73. }
  74. bool BezierEditor::canvas_event(QEvent* event) {
  75. if (!appending)
  76. return false;
  77. if (auto mouse_event = dynamic_cast<QMouseEvent*>(event)) {
  78. auto pos = convert_pos(mouse_event->pos());
  79. if (mouse_event->type() == QEvent::MouseButtonPress) {
  80. if (mouse_event->button() == Qt::LeftButton) {
  81. drawing = true;
  82. if (auto node = get_bezier_node()) {
  83. if (node->can_set_any_at()) {
  84. auto path = get_path();
  85. path.emplace_back(pos);
  86. write_value(path);
  87. add_knot_editor(path.size()-1);
  88. reset_curve(path);
  89. }
  90. }
  91. }
  92. }
  93. }
  94. return false;
  95. }
  96. Geom::Point BezierEditor::convert_pos(QPoint const& src) const {
  97. auto affine = get_transform(*this);
  98. return util::point(no_null(get_canvas())->mapToScene(src)) * affine.inverse();
  99. }
  100. void BezierEditor::node_update() {
  101. redraw();
  102. }
  103. void BezierEditor::time_changed(core::Time) {
  104. redraw();
  105. }
  106. bool BezierEditor::is_readonly() const {
  107. return !get_scene() || !get_bezier_node() || !get_bezier_node()->can_set_any_at();
  108. }
  109. void BezierEditor::redraw() {
  110. if (get_scene() && get_bezier_node()) {
  111. auto path = get_path();
  112. // NOTE: this is to avoid full redraw while editing
  113. // TODO: make sure it gets properly updated on non-editing changes
  114. if ((ptrdiff_t)path.size() == old_size) {
  115. reset_curve(path);
  116. } else {
  117. uninit();
  118. init();
  119. }
  120. }
  121. }
  122. void BezierEditor::init() {
  123. auto bezier_node = no_null(get_bezier_node());
  124. auto path = get_path();
  125. old_size = path.size();
  126. reset_curve(path);
  127. if (bezier_node->can_set_any_at()) {
  128. for (ptrdiff_t i = 0; i < old_size; ++i) {
  129. add_knot_editor(i);
  130. }
  131. }
  132. if (display_tags) {
  133. add_tags();
  134. }
  135. }
  136. void BezierEditor::add_tags() {
  137. auto path = get_path();
  138. for (auto const& knot : path.knots) {
  139. if (!knot.uid.empty()) {
  140. auto e = no_null(get_scene())->addText(util::str(knot.uid));
  141. e->setX(knot.pos.x());
  142. e->setY(knot.pos.y());
  143. tag_items.emplace_back(e);
  144. }
  145. }
  146. }
  147. Geom::Point& flag_to_ref(Geom::Knot& source, BezierEditor::BezierPointFlag flag) {
  148. switch (flag) {
  149. case BezierEditor::Point: return source.pos;
  150. case BezierEditor::Tg1: return source.tg1;
  151. case BezierEditor::Tg2: return source.tg2;
  152. default: throw std::logic_error("Invalid BezierPointFlag value");
  153. }
  154. }
  155. void BezierEditor::point_moved(size_t point_id, QPointF const& pos) {
  156. auto path = get_path();
  157. auto [flag, i] = point_id_to_fn(point_id);
  158. auto& point = flag_to_ref(path.knots[i], flag);
  159. point = util::point(pos);
  160. // currently only enabled symmetric while appending..
  161. if (appending && flag > Point) {
  162. auto opposite = flag == Tg1 ? Tg2 : Tg1;
  163. auto& point_s = flag_to_ref(path.knots[i], opposite);
  164. point_s = -point;
  165. }
  166. write_value(path);
  167. }
  168. void BezierEditor::point_stopped_moving(size_t /*point_id*/) {
  169. close_action();
  170. }
  171. void BezierEditor::point_double_clicked(size_t point_id) {
  172. if (is_readonly())
  173. return;
  174. // TODO: different action for tangent points
  175. auto path = get_path();
  176. auto [_, i] = point_id_to_fn(point_id);
  177. auto& point = path.knots[i];
  178. auto tag = QInputDialog::getText(nullptr, "Bezier point tag", "Edit bezier point tag", QLineEdit::Normal, util::str(point.uid));
  179. point.uid = util::str(tag);
  180. write_value(path);
  181. }
  182. QGraphicsItem* BezierEditor::add_point_editor(size_t i, BezierPointFlag what, QGraphicsItem* parent) {
  183. auto e = new ListenerPointItem(this, point_id_from_fn(what, i));
  184. if (parent) {
  185. e->setParentItem(parent);
  186. e->set_color({0xff, 0xff, 0x88});
  187. } else {
  188. e->setParentItem(no_null(curve_item.get()));
  189. e->set_color({0xff, 0x66, 0x66});
  190. }
  191. auto path = get_path();
  192. auto point = flag_to_ref(path.knots[i], what);
  193. e->set_pos(point.x(), point.y());
  194. e->set_readonly(false);
  195. return e;
  196. }
  197. void BezierEditor::add_knot_editor(size_t i) {
  198. auto pos = add_point_editor(i, Point);
  199. add_point_editor(i, Tg1, pos);
  200. add_point_editor(i, Tg2, pos);
  201. knot_items.emplace_back(pos);
  202. }
  203. void BezierEditor::reset_curve(Geom::BezierKnots const& path) {
  204. auto scene = no_null(get_scene());
  205. if (!curve_item)
  206. curve_item.reset(scene->addPath(util::path_to_qt(path)));
  207. else
  208. curve_item->setPath(util::path_to_qt(path));
  209. curve_item->setPen(curve_pen);
  210. auto affine = get_transform(*this);
  211. curve_item->setTransform(QTransform{util::matrix(affine)});
  212. }
  213. void BezierEditor::uninit() {
  214. old_size = -1;
  215. for (auto const& e : knot_items) {
  216. e->setParentItem(nullptr);
  217. }
  218. if (auto scene = get_scene()) {
  219. for (auto const& e : knot_items)
  220. scene->removeItem(e.get());
  221. scene->removeItem(curve_item.get());
  222. }
  223. knot_items.clear();
  224. remove_tags();
  225. curve_item.reset();
  226. }
  227. void BezierEditor::remove_tags() {
  228. if (auto scene = get_scene()) {
  229. for (auto const& e : tag_items)
  230. scene->removeItem(e.get());
  231. }
  232. tag_items.clear();
  233. }
  234. shared_ptr<core::BaseValue<Geom::BezierKnots>> BezierEditor::get_bezier_node() const {
  235. return get_node_as<Geom::BezierKnots>();
  236. }
  237. Geom::BezierKnots BezierEditor::get_path() const {
  238. return no_null(get_bezier_node())->value(get_core_context());
  239. }
  240. pair<BezierEditor::BezierPointFlag, size_t> BezierEditor::point_id_to_fn(size_t point_id) const {
  241. return {
  242. (BezierPointFlag) (point_id % BezierPointFlag::Count),
  243. point_id / BezierPointFlag::Count
  244. };
  245. }
  246. size_t BezierEditor::point_id_from_fn(BezierPointFlag flag, size_t n) const {
  247. return n*BezierPointFlag::Count+flag;
  248. }
  249. } // namespace rainynite::studio