diff options
| -rw-r--r-- | src/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/livecode/CMakeLists.txt | 12 | ||||
| -rw-r--r-- | src/livecode/api.cpp | 289 | ||||
| -rw-r--r-- | src/livecode/api.h | 101 | ||||
| -rw-r--r-- | src/livecode/input.cpp | 103 | ||||
| -rw-r--r-- | src/livecode/input.h | 74 | ||||
| -rw-r--r-- | src/ui/tools/livecode-tool.cpp | 1392 | ||||
| -rw-r--r-- | src/ui/tools/livecode-tool.h | 62 |
8 files changed, 614 insertions, 1420 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d317cbeb1..61fb15708 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -278,6 +278,7 @@ add_subdirectory(util) add_subdirectory(widgets) add_subdirectory(xml) add_subdirectory(2geom) +add_subdirectory(livecode) # Directories containing lists files that describe building internal libraries add_subdirectory(3rdparty) diff --git a/src/livecode/CMakeLists.txt b/src/livecode/CMakeLists.txt new file mode 100644 index 000000000..ba266998e --- /dev/null +++ b/src/livecode/CMakeLists.txt @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0-or-later + +set(livecode_SRC + api.cpp + input.cpp + + # HEADERS + api.h + input.h + ) + +add_inkscape_source("${livecode_SRC}") diff --git a/src/livecode/api.cpp b/src/livecode/api.cpp new file mode 100644 index 000000000..4617968c2 --- /dev/null +++ b/src/livecode/api.cpp @@ -0,0 +1,289 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * API for the livecoding script language + * + * Authors: + * Sol Bekic <s+inkscape@s-ol.nu> + * Copyright (C) 2019 Authors + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include "svg/stringstream.h" +#include "svg/svg-color.h" +#include "svg/svg.h" +#include "document.h" +#include "display/sp-canvas-item.h" + +#include "livecode/api.h" + +namespace Inkscape { +namespace Livecode { + +API::API(SPDesktop *desktop) + : desktop(desktop) + , doc_root(nullptr) + , ui_root(nullptr) + , _mouse(*this) +{ +} + +API::~API() { + if (doc_root) { + doc_root->deleteObject(true, true); + doc_root = nullptr; + } + + if (ui_root) { + ui_root->deleteObject(true, true); + ui_root = nullptr; + } +} + +Inkscape::XML::Node *API::make_rect(Geom::Rect const &rect, SPCSSAttr *css) { + SPDocument *doc = desktop->getDocument(); + Inkscape::XML::Document *xml_doc = doc->getReprDoc(); + + Inkscape::XML::Node *repr = xml_doc->createElement("svg:rect"); + + sp_repr_set_svg_double(repr, "x", rect.left()); + sp_repr_set_svg_double(repr, "y", rect.top()); + sp_repr_set_svg_double(repr, "width", rect.width()); + sp_repr_set_svg_double(repr, "height", rect.height()); + + if (css) { + Glib::ustring css_str; + sp_repr_css_write_string(css, css_str); + sp_repr_css_attr_unref(css); + repr->setAttribute("style", css_str.c_str()); + } + + return repr; +} + +Inkscape::XML::Node *API::make_line(Geom::Point const &p1, Geom::Point const &p2, SPCSSAttr *css) { + SPDocument *doc = desktop->getDocument(); + Inkscape::XML::Document *xml_doc = doc->getReprDoc(); + + Inkscape::XML::Node *repr = xml_doc->createElement("svg:line"); + + sp_repr_set_svg_double(repr, "x1", p1.x()); + sp_repr_set_svg_double(repr, "y1", p1.y()); + sp_repr_set_svg_double(repr, "x2", p2.x()); + sp_repr_set_svg_double(repr, "y2", p2.y()); + + if (css) { + Glib::ustring css_str; + sp_repr_css_write_string(css, css_str); + sp_repr_css_attr_unref(css); + repr->setAttribute("style", css_str.c_str()); + } + + return repr; +} + +Inkscape::XML::Node *API::make_path(Glib::ustring d, Geom::Point const &pos, SPCSSAttr *css) { + SPDocument *doc = desktop->getDocument(); + Inkscape::XML::Document *xml_doc = doc->getReprDoc(); + + Inkscape::XML::Node *repr = xml_doc->createElement("svg:path"); + + sp_repr_set_svg_double(repr, "x", pos.x()); + sp_repr_set_svg_double(repr, "y", pos.y()); + repr->setAttribute("d", d.c_str()); + + if (css) { + Glib::ustring css_str; + sp_repr_css_write_string(css, css_str); + sp_repr_css_attr_unref(css); + repr->setAttribute("style", css_str.c_str()); + } + + return repr; +} + +Inkscape::XML::Node *API::make_arrow(Geom::Point const &from, Geom::Point const &to, SPCSSAttr *css) { + SPDocument *doc = desktop->getDocument(); + Inkscape::XML::Document *xml_doc = doc->getReprDoc(); + + Inkscape::XML::Node *repr = xml_doc->createElement("svg:path"); + + Geom::Point back = from - to; + back.normalize(); + Geom::Point const cross_a = back.cw() + back * 2.0; + Geom::Point const cross_b = back.cw() + back * 2.0; + + gchar* d = g_strdup_printf("M %f,%f %f,%f l %f,%f M %f,%f l %f,%f", + from.x(), from.y(), + to.x(), to.y(), + cross_a.x(), cross_a.y(), + to.x(), to.y(), + cross_b.x(), cross_b.y()); + repr->setAttribute("d", d); + g_free(d); + + Glib::ustring css_str; + if (css) { + sp_repr_css_write_string(css, css_str); + sp_repr_css_attr_unref(css); + } else { + css_str = "stroke: #000000;"; + } + repr->setAttribute("style", css_str.c_str()); + + return repr; +} + +namespace { +SPCSSAttr *handle_style() { + SPCSSAttr *css = sp_repr_css_attr_new(); + sp_repr_css_set_property(css, "fill", "#ffffff"); + sp_repr_css_set_property(css, "stroke-width", "0.2"); + sp_repr_css_set_property(css, "stroke", "#000000"); + + return css; +} +} + +bool API::input_point(Glib::ustring const &id, Geom::Point *point) { + auto const half_handle_size = Geom::Point(2, 2); + auto ui_point = *point * desktop->w2d().inverse(); + Geom::Rect const rect(ui_point - half_handle_size, ui_point + half_handle_size); + + SPCSSAttr *css = handle_style(); + guint32 color = 0xffaaaaff; + + if (rect.interiorContains(_mouse.ui_pos())) { + hot = id; + sp_repr_css_set_property(css, "fill", "#ff0000"); + } + + if (hot == id && _mouse.pressed()) { + active = id; + } + + bool change = false; + + if (active == id) { + sp_repr_css_set_property(css, "fill", "#0000ff"); + + auto const delta = _mouse.delta(); + change = !delta.isZero(); + *point += delta; + if (_mouse.released()) { + active = ""; + } + } + + Inkscape::XML::Node *repr = make_rect(rect, css); + draw_ui(id, repr); + return change; +} + +bool API::input_line(Glib::ustring const &id, Geom::Point *p1, Geom::Point *p2) { + bool change = false; + if (input_point(id + "_p1", p1)) { + change = true; + } + if (input_point(id + "_p2", p2)) { + change = true; + } + draw_doc("", make_line(*p1, *p2)); + return change; +} + +bool API::input_arrow(Glib::ustring const &id, Geom::Point *from, Geom::Point *to) { + bool change = false; + if (input_point(id + "_from", from)) { + change = true; + } + if (input_point(id + "_to", to)) { + change = true; + } + draw_doc("", make_line(*from, *to)); + return change; +} + +bool API::input_rect(Glib::ustring const &id, Geom::Rect *rect) { + Geom::Point min = rect->min(); + Geom::Point max = rect->max(); + + bool change = false; + if (input_point(id + "_min", &min)) { + Geom::Point const delta = min - rect->min(); + *rect += delta; + change = true; + } + if (input_point(id + "_max", &max)) { + rect->setMax(max); + change = true; + } + + draw_doc("", make_rect(*rect)); + return change; +} + +Mouse const &API::mouse() { + return _mouse; +} + +void API::push_event(GdkEvent *event) { + _mouse.push_event(event); +} + +void API::setup_frame() { + hot = ""; + + if (doc_root) { + doc_root->deleteObject(true, true); + doc_root = nullptr; + } + + if (ui_root) { + ui_root->deleteObject(true, true); + ui_root = nullptr; + } + + Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc(); + Inkscape::XML::Node *rdoc = xml_doc->createElement("svg:g"); + Inkscape::XML::Node *rui = xml_doc->createElement("svg:g"); + + doc_root = SP_ITEM(desktop->currentLayer()->appendChildRepr(rdoc)); + doc_root->updateRepr(); + Inkscape::GC::release(rdoc); + + ui_root = SP_ITEM(desktop->currentLayer()->appendChildRepr(rui)); + ui_root->doWriteTransform(ui2doc(), nullptr, true); + Inkscape::GC::release(rui); +} + +void API::finish_frame() { + doc_root->updateRepr(); + ui_root->updateRepr(); + _mouse.finish_frame(); +} + +void API::draw_doc(Glib::ustring const &id, Inkscape::XML::Node *repr) { + repr->setAttribute("inkscape:livecode-id", id); + doc_root->appendChildRepr(repr); + Inkscape::GC::release(repr); +} +void API::draw_ui(Glib::ustring const &id, Inkscape::XML::Node *repr) { + repr->setAttribute("inkscape:livecode-id", id); + ui_root->appendChildRepr(repr); + Inkscape::GC::release(repr); +} + +} +} + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : diff --git a/src/livecode/api.h b/src/livecode/api.h new file mode 100644 index 000000000..d2974a01d --- /dev/null +++ b/src/livecode/api.h @@ -0,0 +1,101 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#ifndef INK_LIVECODE_API_H +#define INK_LIVECODE_API_H + +/* + * API for the livecoding script language + * + * Authors: + * Sol Bekic <s+inkscape@s-ol.nu> + * Copyright (C) 2019 Authors + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include <cstddef> +#include <2geom/point.h> +#include <2geom/rect.h> +#include <gdk/gdk.h> + +#include "xml/repr.h" +#include "desktop.h" +#include "livecode/input.h" + + +class SPDocument; +class SPItem; + +namespace Inkscape { +namespace Livecode { + +class Mouse; + +class API { +public: + API(SPDesktop *desktop); + ~API(); + + Inkscape::XML::Node *make_rect(Geom::Rect const &rect, SPCSSAttr *css = nullptr); + Inkscape::XML::Node *make_line(Geom::Point const &p1, Geom::Point const &p2, SPCSSAttr *css = nullptr); + Inkscape::XML::Node *make_path(Glib::ustring d, Geom::Point const &pos = Geom::Point(), SPCSSAttr *css = nullptr); + Inkscape::XML::Node *make_arrow(Geom::Point const &from, Geom::Point const &to, SPCSSAttr *css = nullptr); + + bool input_point(Glib::ustring const &id, Geom::Point *point); + bool input_line(Glib::ustring const &id, Geom::Point *p1, Geom::Point *p2); + bool input_arrow(Glib::ustring const &id, Geom::Point *from, Geom::Point *to); + bool input_rect(Glib::ustring const &id, Geom::Rect *rect); + + void draw_doc(Glib::ustring const &id, Inkscape::XML::Node *item); + void draw_ui(Glib::ustring const &id, Inkscape::XML::Node *item); + + void push_event(GdkEvent *event); + void setup_frame(); + void finish_frame(); + + inline Geom::Affine ui2dt() const { + return doc_root->i2doc_affine(); + } + inline Geom::Affine dt2ui() const { + return ui2dt().inverse(); + } + + inline Geom::Affine ui2doc() const { + return desktop->w2d(); + } + inline Geom::Affine doc2ui() const { + return ui2doc().inverse(); + } + inline Geom::Affine ui2doc_vec() const { + return desktop->w2d().withoutTranslation(); + } + inline Geom::Affine doc2ui_vec() const { + return ui2doc_vec().inverse(); + } + + Mouse const &mouse(); + +private: + SPDesktop *desktop; + SPItem *doc_root, *ui_root; + + Mouse _mouse; + + Glib::ustring hot, active; + std::vector<SPItem *> drawn_items; +}; + +} +} + +#endif // INK_LIVECODE_API_H + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : diff --git a/src/livecode/input.cpp b/src/livecode/input.cpp new file mode 100644 index 000000000..4dab43fc9 --- /dev/null +++ b/src/livecode/input.cpp @@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Input state keeper for the livecoding tool + * + * Authors: + * Sol Bekic <s+inkscape@s-ol.nu> + * Copyright (C) 2019 Authors + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include "livecode/input.h" +#include "livecode/api.h" +#include "desktop.h" + +namespace Inkscape { +namespace Livecode { + + +Mouse::Mouse(API &api) + : api(api) +{ +} + +void Mouse::finish_frame() +{ + prev_mask = last_mask; + prev_pos = last_pos; +} + +void Mouse::push_event(GdkEvent *event) +{ + switch (event->type) { + case GDK_BUTTON_PRESS: { + last_mask |= 1 << event->button.button - 1; + break; + } + case GDK_BUTTON_RELEASE: { + last_mask &= ~(1 << event->button.button - 1); + break; + } + case GDK_MOTION_NOTIFY: { + last_pos = Geom::Point(event->motion.x, event->motion.y) * api.dt2ui(); + break; + } + default: + break; + } +} + +InputState Mouse::event(guint button) const { + guint const flag = 1 << button - 1; + bool last = last_mask & flag; + bool prev = prev_mask & flag; + + if (last && prev) return INPUTSTATE_HELD; + else if (last) return INPUTSTATE_PRESSED; + else if (prev) return INPUTSTATE_RELEASED; + return INPUTSTATE_NONE; +} + +bool Mouse::pressed(guint button) const { + guint const flag = 1 << button - 1; + return (last_mask & flag) && !(prev_mask & flag); +} + +bool Mouse::held(guint button) const { + return last_mask & (1 << button -1); +} + +bool Mouse::released(guint button) const { + guint const flag = 1 << button - 1; + return (prev_mask & flag) && !(last_mask & flag); +} + +Geom::Point Mouse::pos() const { + return ui_pos() * api.ui2doc(); +} +Geom::Point Mouse::delta() const { + return ui_delta() * api.ui2doc_vec(); +} + +Geom::Point Mouse::ui_pos() const { + return last_pos; +} +Geom::Point Mouse::ui_delta() const { + return last_pos - prev_pos; +} + + +} +} + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : diff --git a/src/livecode/input.h b/src/livecode/input.h new file mode 100644 index 000000000..0b9977af8 --- /dev/null +++ b/src/livecode/input.h @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#ifndef INK_LIVECODE_INPUT_H +#define INK_LIVECODE_INPUT_H + +/* + * Input state keeper for the livecoding tool + * + * Authors: + * Sol Bekic <s+inkscape@s-ol.nu> + * Copyright (C) 2019 Authors + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include <gdk/gdk.h> +#include <2geom/point.h> +#include <2geom/geom.h> + + +namespace Inkscape { +namespace Livecode { + +class API; + +enum InputState { + INPUTSTATE_NONE, + INPUTSTATE_HOVER, + INPUTSTATE_PRESSED, + INPUTSTATE_HELD, + INPUTSTATE_RELEASED, +}; + +class Mouse { +public: + Geom::Point pos() const; + Geom::Point delta() const; + + Geom::Point ui_pos() const; + Geom::Point ui_delta() const; + + InputState event(guint button = 1) const; + bool pressed(guint button = 1) const; + bool held(guint button = 1) const; + bool released(guint button = 1) const; + +private: + API& api; + + guint last_mask, prev_mask; + Geom::Point last_pos, prev_pos; + + Mouse(API &api); + + void push_event(GdkEvent *event); + void finish_frame(); + + friend class API; +}; + +} +} + +#endif // INK_LIVECODE_INPUT_H + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : diff --git a/src/ui/tools/livecode-tool.cpp b/src/ui/tools/livecode-tool.cpp index cb8c0675c..0c125ac05 100644 --- a/src/ui/tools/livecode-tool.cpp +++ b/src/ui/tools/livecode-tool.cpp @@ -1,13 +1,10 @@ // SPDX-License-Identifier: GPL-2.0-or-later /* - * Our nice measuring tool + * An experimental Livecoding Tool * * Authors: - * Felipe Correa da Silva Sanches <juca@members.fsf.org> - * Jon A. Cruz <jon@joncruz.org> - * Jabiertxo Arraiza <jabier.arraiza@marker.es> - * - * Copyright (C) 2011 Authors + * Sol Bekic <s+inkscape@s-ol.nu> + * Copyright (C) 2019 Authors * * Released under GNU GPL v2+, read the file 'COPYING' for more information. */ @@ -17,24 +14,14 @@ #include <boost/none_t.hpp> -#include <2geom/line.h> -#include <2geom/path-intersection.h> - #include "desktop-style.h" #include "desktop.h" -#include "document-undo.h" #include "inkscape.h" -#include "path-chemistry.h" -#include "rubberband.h" -#include "text-editing.h" #include "verbs.h" -#include "display/curve.h" #include "display/sodipodi-ctrl.h" #include "display/sp-canvas-util.h" #include "display/sp-canvas.h" -#include "display/sp-ctrlcurve.h" -#include "display/sp-ctrlline.h" #include "object/sp-defs.h" #include "object/sp-flowtext.h" @@ -43,25 +30,14 @@ #include "object/sp-shape.h" #include "object/sp-text.h" -#include "ui/pixmaps/cursor-measure.xpm" - #include "svg/stringstream.h" #include "svg/svg-color.h" #include "svg/svg.h" -#include "ui/dialog/knot-properties.h" -#include "ui/tools/freehand-base.h" #include "ui/tools/livecode-tool.h" #include "util/units.h" -using Inkscape::ControlManager; -using Inkscape::CTLINE_SECONDARY; -using Inkscape::Util::unit_table; -using Inkscape::DocumentUndo; - -#define MT_KNOT_COLOR_NORMAL 0xffffff00 -#define MT_KNOT_COLOR_MOUSEOVER 0xff000000 namespace Inkscape { @@ -73,1382 +49,62 @@ const std::string& LivecodeTool::getPrefsPath() return LivecodeTool::prefsPath; } -const std::string LivecodeTool::prefsPath = "/tools/livecode"; - -namespace { - -/** - * Simple class to use for removing label overlap. - */ -class LabelPlacement { -public: - - double lengthVal; - double offset; - Geom::Point start; - Geom::Point end; -}; - -bool SortLabelPlacement(LabelPlacement const &first, LabelPlacement const &second) -{ - if (first.end[Geom::Y] == second.end[Geom::Y]) { - return first.end[Geom::X] < second.end[Geom::X]; - } else { - return first.end[Geom::Y] < second.end[Geom::Y]; - } -} - -//precision is for give the number of decimal positions -//of the label to calculate label width -void repositionOverlappingLabels(std::vector<LabelPlacement> &placements, SPDesktop *desktop, Geom::Point const &normal, double fontsize, int precision) -{ - std::sort(placements.begin(), placements.end(), SortLabelPlacement); - - double border = 3; - Geom::Rect box; - { - Geom::Point tmp(fontsize * (6 + precision) + (border * 2), fontsize + (border * 2)); - tmp = desktop->w2d(tmp); - box = Geom::Rect(-tmp[Geom::X] / 2, -tmp[Geom::Y] / 2, tmp[Geom::X] / 2, tmp[Geom::Y] / 2); - } - - // Using index since vector may be re-ordered as we go. - // Starting at one, since the first item can't overlap itself - for (size_t i = 1; i < placements.size(); i++) { - LabelPlacement &place = placements[i]; - - bool changed = false; - do { - Geom::Rect current(box + place.end); - - changed = false; - bool overlaps = false; - for (size_t j = i; (j > 0) && !overlaps; --j) { - LabelPlacement &otherPlace = placements[j - 1]; - Geom::Rect target(box + otherPlace.end); - if (current.intersects(target)) { - overlaps = true; - } - } - if (overlaps) { - place.offset += (fontsize + border); - place.end = place.start - desktop->w2d(normal * place.offset); - changed = true; - } - } while (changed); - - std::sort(placements.begin(), placements.begin() + i + 1, SortLabelPlacement); - } -} - -/** - * Calculates where to place the anchor for the display text and arc. - * - * @param desktop the desktop that is being used. - * @param angle the angle to be displaying. - * @param baseAngle the angle of the initial baseline. - * @param startPoint the point that is the vertex of the selected angle. - * @param endPoint the point that is the end the user is manipulating for measurement. - * @param fontsize the size to display the text label at. - */ -Geom::Point calcAngleDisplayAnchor(SPDesktop *desktop, double angle, double baseAngle, - Geom::Point const &startPoint, Geom::Point const &endPoint, - double fontsize) -{ - // Time for the trick work of figuring out where things should go, and how. - double lengthVal = (endPoint - startPoint).length(); - double effective = baseAngle + (angle / 2); - Geom::Point where(lengthVal, 0); - where *= Geom::Affine(Geom::Rotate(effective)) * Geom::Affine(Geom::Translate(startPoint)); - - // When the angle is tight, the label would end up under the cursor and/or lines. Bump it - double scaledFontsize = std::abs(fontsize * desktop->w2d(Geom::Point(0, 1.0))[Geom::Y]); - if (std::abs((where - endPoint).length()) < scaledFontsize) { - where[Geom::Y] += scaledFontsize * 2; - } - - // We now have the ideal position, but need to see if it will fit/work. - - Geom::Rect visibleArea = desktop->get_display_area(); - // Bring it in to "title safe" for the anchor point - Geom::Point textBox = desktop->w2d(Geom::Point(fontsize * 3, fontsize / 2)); - textBox[Geom::Y] = std::abs(textBox[Geom::Y]); - - visibleArea = Geom::Rect(visibleArea.min()[Geom::X] + textBox[Geom::X], - visibleArea.min()[Geom::Y] + textBox[Geom::Y], - visibleArea.max()[Geom::X] - textBox[Geom::X], - visibleArea.max()[Geom::Y] - textBox[Geom::Y]); - - where[Geom::X] = std::min(where[Geom::X], visibleArea.max()[Geom::X]); - where[Geom::X] = std::max(where[Geom::X], visibleArea.min()[Geom::X]); - where[Geom::Y] = std::min(where[Geom::Y], visibleArea.max()[Geom::Y]); - where[Geom::Y] = std::max(where[Geom::Y], visibleArea.min()[Geom::Y]); - - return where; -} - -/** - * Create a measure item in current document. - * - * @param pathv the path to create. - * @param markers if the path results get markers. - * @param color of the stroke. - * @param measure_repr container element. - */ -void setMeasureItem(Geom::PathVector pathv, bool is_curve, bool markers, guint32 color, Inkscape::XML::Node *measure_repr) -{ - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if(!desktop) { - return; - } - SPDocument *doc = desktop->getDocument(); - Inkscape::XML::Document *xml_doc = doc->getReprDoc(); - Inkscape::XML::Node *repr; - repr = xml_doc->createElement("svg:path"); - gchar *str = sp_svg_write_path(pathv); - SPCSSAttr *css = sp_repr_css_attr_new(); - Geom::Coord strokewidth = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse().expansionX(); - std::stringstream stroke_width; - stroke_width.imbue(std::locale::classic()); - if(measure_repr) { - stroke_width << strokewidth / desktop->current_zoom(); - } else { - stroke_width << strokewidth; - } - sp_repr_css_set_property (css, "stroke-width", stroke_width.str().c_str()); - sp_repr_css_set_property (css, "fill", "none"); - if(color) { - gchar color_line[64]; - sp_svg_write_color (color_line, sizeof(color_line), color); - sp_repr_css_set_property (css, "stroke", color_line); - } else { - sp_repr_css_set_property (css, "stroke", "#ff0000"); - } - char const * stroke_linecap = is_curve ? "butt" : "square"; - sp_repr_css_set_property (css, "stroke-linecap", stroke_linecap); - sp_repr_css_set_property (css, "stroke-linejoin", "miter"); - sp_repr_css_set_property (css, "stroke-miterlimit", "4"); - sp_repr_css_set_property (css, "stroke-dasharray", "none"); - if(measure_repr) { - sp_repr_css_set_property (css, "stroke-opacity", "0.5"); - } else { - sp_repr_css_set_property (css, "stroke-opacity", "1"); - } - if(markers) { - sp_repr_css_set_property (css, "marker-start", "url(#Arrow2Sstart)"); - sp_repr_css_set_property (css, "marker-end", "url(#Arrow2Send)"); - } - Glib::ustring css_str; - sp_repr_css_write_string(css,css_str); - repr->setAttribute("style", css_str.c_str()); - sp_repr_css_attr_unref (css); - g_assert( str != nullptr ); - repr->setAttribute("d", str); - g_free(str); - if(measure_repr) { - measure_repr->addChild(repr, nullptr); - Inkscape::GC::release(repr); - } else { - SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr)); - Inkscape::GC::release(repr); - item->updateRepr(); - desktop->getSelection()->clear(); - desktop->getSelection()->add(item); - } -} - -/** - * Given an angle, the arc center and edge point, draw an arc segment centered around that edge point. - * - * @param desktop the desktop that is being used. - * @param center the center point for the arc. - * @param end the point that ends at the edge of the arc segment. - * @param anchor the anchor point for displaying the text label. - * @param angle the angle of the arc segment to draw. - * @param measure_rpr the container of the curve if converted to items. - */ -void createAngleDisplayCurve(SPDesktop *desktop, Geom::Point const ¢er, Geom::Point const &end, Geom::Point const &anchor, double angle, bool to_phantom, std::vector<SPCanvasItem *> &measure_phantom_items , std::vector<SPCanvasItem *> &measure_tmp_items , Inkscape::XML::Node *measure_repr = nullptr) -{ - // Given that we have a point on the arc's edge and the angle of the arc, we need to get the two endpoints. - - double textLen = std::abs((anchor - center).length()); - double sideLen = std::abs((end - center).length()); - if (sideLen > 0.0) { - double factor = std::min(1.0, textLen / sideLen); - - // arc start - Geom::Point p1 = end * (Geom::Affine(Geom::Translate(-center)) - * Geom::Affine(Geom::Scale(factor)) - * Geom::Affine(Geom::Translate(center))); - - // arc end - Geom::Point p4 = p1 * (Geom::Affine(Geom::Translate(-center)) - * Geom::Affine(Geom::Rotate(-angle)) - * Geom::Affine(Geom::Translate(center))); - - // from Riskus - double xc = center[Geom::X]; - double yc = center[Geom::Y]; - double ax = p1[Geom::X] - xc; - double ay = p1[Geom::Y] - yc; - double bx = p4[Geom::X] - xc; - double by = p4[Geom::Y] - yc; - double q1 = (ax * ax) + (ay * ay); - double q2 = q1 + (ax * bx) + (ay * by); - - double k2 = (4.0 / 3.0) * (std::sqrt(2 * q1 * q2) - q2) / ((ax * by) - (ay * bx)); - - Geom::Point p2(xc + ax - (k2 * ay), - yc + ay + (k2 * ax)); - Geom::Point p3(xc + bx + (k2 * by), - yc + by - (k2 * bx)); - SPCtrlCurve *curve = ControlManager::getManager().createControlCurve(desktop->getTempGroup(), p1, p2, p3, p4, CTLINE_SECONDARY); - if(to_phantom){ - curve->rgba = 0x8888887f; - measure_phantom_items.push_back(SP_CANVAS_ITEM(curve)); - } else { - measure_tmp_items.push_back(SP_CANVAS_ITEM(curve)); - } - sp_canvas_item_move_to_z(SP_CANVAS_ITEM(curve), 0); - sp_canvas_item_show(SP_CANVAS_ITEM(curve)); - if(measure_repr) { - Geom::PathVector pathv; - Geom::Path path; - path.start(desktop->doc2dt(p1)); - path.appendNew<Geom::CubicBezier>(desktop->doc2dt(p2),desktop->doc2dt(p3),desktop->doc2dt(p4)); - pathv.push_back(path); - pathv *= SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse(); - if(!pathv.empty()) { - setMeasureItem(pathv, true, false, 0xff00007f, measure_repr); - } - } - } -} - -boost::optional<Geom::Point> explicit_base_tmp = boost::none; - -} // namespace +const std::string LivecodeTool::prefsPath = "/tools/ersaser"; LivecodeTool::LivecodeTool() - : ToolBase(cursor_measure_xpm) - , grabbed(nullptr) + : ToolBase(nullptr, false) + , api(SP_ACTIVE_DESKTOP) { - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - start_p = readMeasurePoint(true); - end_p = readMeasurePoint(false); - dimension_offset = 35; - last_pos = Geom::Point(0,0); - // create the knots - this->knot_start = new SPKnot(desktop, _("Measure start, <b>Shift+Click</b> for position dialog")); - this->knot_start->setMode(SP_KNOT_MODE_XOR); - this->knot_start->setFill(MT_KNOT_COLOR_NORMAL, MT_KNOT_COLOR_MOUSEOVER, MT_KNOT_COLOR_MOUSEOVER, MT_KNOT_COLOR_MOUSEOVER); - this->knot_start->setStroke(0x0000007f, 0x0000007f, 0x0000007f, 0x0000007f); - this->knot_start->setShape(SP_KNOT_SHAPE_CIRCLE); - this->knot_start->updateCtrl(); - this->knot_end = new SPKnot(desktop, _("Measure end, <b>Shift+Click</b> for position dialog")); - this->knot_end->setMode(SP_KNOT_MODE_XOR); - this->knot_end->setFill(MT_KNOT_COLOR_NORMAL, MT_KNOT_COLOR_MOUSEOVER, MT_KNOT_COLOR_MOUSEOVER, MT_KNOT_COLOR_MOUSEOVER); - this->knot_end->setStroke(0x0000007f, 0x0000007f, 0x0000007f, 0x0000007f); - this->knot_end->setShape(SP_KNOT_SHAPE_CIRCLE); - this->knot_end->updateCtrl(); - Geom::Rect display_area = desktop->get_display_area(); - if(display_area.interiorContains(start_p) && display_area.interiorContains(end_p) && end_p != Geom::Point()) { - this->knot_start->moveto(start_p); - this->knot_start->show(); - this->knot_end->moveto(end_p); - this->knot_end->show(); - showCanvasItems(); - } else { - start_p = Geom::Point(0,0); - end_p = Geom::Point(0,0); - writeMeasurePoint(start_p, true); - writeMeasurePoint(end_p, false); - } - this->_knot_start_moved_connection = this->knot_start->moved_signal.connect(sigc::mem_fun(*this, &LivecodeTool::knotStartMovedHandler)); - this->_knot_start_click_connection = this->knot_start->click_signal.connect(sigc::mem_fun(*this, &LivecodeTool::knotClickHandler)); - this->_knot_start_ungrabbed_connection = this->knot_start->ungrabbed_signal.connect(sigc::mem_fun(*this, &LivecodeTool::knotUngrabbedHandler)); - this->_knot_end_moved_connection = this->knot_end->moved_signal.connect(sigc::mem_fun(*this, &LivecodeTool::knotEndMovedHandler)); - this->_knot_end_click_connection = this->knot_end->click_signal.connect(sigc::mem_fun(*this, &LivecodeTool::knotClickHandler)); - this->_knot_end_ungrabbed_connection = this->knot_end->ungrabbed_signal.connect(sigc::mem_fun(*this, &LivecodeTool::knotUngrabbedHandler)); - } LivecodeTool::~LivecodeTool() { - this->_knot_start_moved_connection.disconnect(); - this->_knot_start_ungrabbed_connection.disconnect(); - this->_knot_end_moved_connection.disconnect(); - this->_knot_end_ungrabbed_connection.disconnect(); - - /* unref should call destroy */ - knot_unref(this->knot_start); - knot_unref(this->knot_end); - for (auto & measure_tmp_item : measure_tmp_items) { - sp_canvas_item_destroy(measure_tmp_item); - } - measure_tmp_items.clear(); - for (auto & idx : measure_item) { - sp_canvas_item_destroy(idx); - } - measure_item.clear(); - for (auto & measure_phantom_item : measure_phantom_items) { - sp_canvas_item_destroy(measure_phantom_item); - } - measure_phantom_items.clear(); -} - -Geom::Point LivecodeTool::readMeasurePoint(bool is_start) { - Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - Glib::ustring measure_point = is_start ? "/tools/measure/measure-start" : "/tools/measure/measure-end"; - return prefs->getPoint(measure_point, Geom::Point(Geom::infinity(),Geom::infinity())); } -void LivecodeTool::writeMeasurePoint(Geom::Point point, bool is_start) { - Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - Glib::ustring measure_point = is_start ? "/tools/measure/measure-start" : "/tools/measure/measure-end"; - prefs->setPoint(measure_point, point); -} - -//This function is used to reverse the Measure, I do it in two steps because when -//we move the knot the start_ or the end_p are overwritten so I need the original values. -void LivecodeTool::reverseKnots() +void LivecodeTool::setup() { - Geom::Point start = start_p; - Geom::Point end = end_p; - this->knot_start->moveto(end); - this->knot_start->show(); - this->knot_end->moveto(start); - this->knot_end->show(); - start_p = end; - end_p = start; - this->showCanvasItems(); -} + ToolBase::setup(); -void LivecodeTool::knotClickHandler(SPKnot *knot, guint state) -{ - if (state & GDK_SHIFT_MASK) { - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - Glib::ustring const unit_name = prefs->getString("/tools/measure/unit"); - explicit_base = explicit_base_tmp; - Inkscape::UI::Dialogs::KnotPropertiesDialog::showDialog(desktop, knot, unit_name); - } + Gtk::Widget* w = Glib::wrap(GTK_WIDGET(desktop->getCanvas())); + tick_callback = w->add_tick_callback(sigc::mem_fun(this, &LivecodeTool::handle_tick)); } -void LivecodeTool::knotStartMovedHandler(SPKnot */*knot*/, Geom::Point const &ppointer, guint state) -{ - Geom::Point point = this->knot_start->position(); - if (state & GDK_CONTROL_MASK) { - spdc_endpoint_snap_rotation(this, point, end_p, state); - } else if (!(state & GDK_SHIFT_MASK)) { - SnapManager &snap_manager = desktop->namedview->snap_manager; - snap_manager.setup(desktop); - Inkscape::SnapCandidatePoint scp(point, Inkscape::SNAPSOURCE_OTHER_HANDLE); - scp.addOrigin(this->knot_end->position()); - Inkscape::SnappedPoint sp = snap_manager.freeSnap(scp); - point = sp.getPoint(); - snap_manager.unSetup(); - } - if(start_p != point) { - start_p = point; - this->knot_start->moveto(start_p); - } - showCanvasItems(); -} - -void LivecodeTool::knotEndMovedHandler(SPKnot */*knot*/, Geom::Point const &ppointer, guint state) -{ - Geom::Point point = this->knot_end->position(); - if (state & GDK_CONTROL_MASK) { - spdc_endpoint_snap_rotation(this, point, start_p, state); - } else if (!(state & GDK_SHIFT_MASK)) { - SnapManager &snap_manager = desktop->namedview->snap_manager; - snap_manager.setup(desktop); - Inkscape::SnapCandidatePoint scp(point, Inkscape::SNAPSOURCE_OTHER_HANDLE); - scp.addOrigin(this->knot_start->position()); - Inkscape::SnappedPoint sp = snap_manager.freeSnap(scp); - point = sp.getPoint(); - snap_manager.unSetup(); - } - if(end_p != point) { - end_p = point; - this->knot_end->moveto(end_p); - } - showCanvasItems(); -} - -void LivecodeTool::knotUngrabbedHandler(SPKnot */*knot*/, unsigned int state) -{ - this->knot_start->moveto(start_p); - this->knot_end->moveto(end_p); - showCanvasItems(); -} - - -//todo: we need this function? void LivecodeTool::finish() { - this->enableGrDrag(false); - - if (this->grabbed) { - sp_canvas_item_ungrab(this->grabbed); - this->grabbed = nullptr; - } + Gtk::Widget* w = Glib::wrap(GTK_WIDGET(desktop->getCanvas())); + w->remove_tick_callback(tick_callback); ToolBase::finish(); } -static void calculate_intersections(SPDesktop * /*desktop*/, SPItem* item, Geom::PathVector const &lineseg, SPCurve *curve, std::vector<double> &intersections) -{ - curve->transform(item->i2doc_affine()); - // Find all intersections of the control-line with this shape - Geom::CrossingSet cs = Geom::crossings(lineseg, curve->get_pathvector()); - Geom::delete_duplicates(cs[0]); - - // Reconstruct and store the points of intersection - Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - bool show_hidden = prefs->getBool("/tools/measure/show_hidden", true); - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - for (Geom::Crossings::const_iterator m = cs[0].begin(); m != cs[0].end(); ++m) { - if (!show_hidden) { - double eps = 0.0001; - if (((*m).ta > eps && - item == desktop->getItemAtPoint(desktop->d2w(desktop->dt2doc(lineseg[0].pointAt((*m).ta - eps))), true, nullptr)) || - ((*m).ta + eps < 1 && - item == desktop->getItemAtPoint(desktop->d2w(desktop->dt2doc(lineseg[0].pointAt((*m).ta + eps))), true, nullptr))) { - intersections.push_back((*m).ta); - } - } else { - intersections.push_back((*m).ta); - } - } -} - bool LivecodeTool::root_handler(GdkEvent* event) { gint ret = FALSE; - switch (event->type) { - case GDK_BUTTON_PRESS: { - this->knot_start->hide(); - this->knot_end->hide(); - Geom::Point const button_w(event->button.x, event->button.y); - explicit_base = boost::none; - explicit_base_tmp = boost::none; - last_end = boost::none; - - if (event->button.button == 1 && !this->space_panning) { - // save drag origin - start_p = desktop->w2d(Geom::Point(event->button.x, event->button.y)); - within_tolerance = true; - - ret = TRUE; - } - - SnapManager &snap_manager = desktop->namedview->snap_manager; - snap_manager.setup(desktop); - snap_manager.freeSnapReturnByRef(start_p, Inkscape::SNAPSOURCE_OTHER_HANDLE); - snap_manager.unSetup(); - - sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate), - GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK, - nullptr, event->button.time); - this->grabbed = SP_CANVAS_ITEM(desktop->acetate); - break; - } - case GDK_KEY_PRESS: { - if ((event->key.keyval == GDK_KEY_Control_L) || (event->key.keyval == GDK_KEY_Control_R)) { - explicit_base_tmp = explicit_base; - explicit_base = end_p; - showInfoBox(last_pos, true); - } - break; - } - case GDK_KEY_RELEASE: { - if ((event->key.keyval == GDK_KEY_Control_L) || (event->key.keyval == GDK_KEY_Control_R)) { - showInfoBox(last_pos, false); - } - break; - } - case GDK_MOTION_NOTIFY: { - if (!(event->motion.state & GDK_BUTTON1_MASK)) { - if(!(event->motion.state & GDK_SHIFT_MASK)) { - Geom::Point const motion_w(event->motion.x, event->motion.y); - Geom::Point const motion_dt(desktop->w2d(motion_w)); - - SnapManager &snap_manager = desktop->namedview->snap_manager; - snap_manager.setup(desktop); - - Inkscape::SnapCandidatePoint scp(motion_dt, Inkscape::SNAPSOURCE_OTHER_HANDLE); - scp.addOrigin(start_p); - - snap_manager.preSnap(scp); - snap_manager.unSetup(); - } - last_pos = Geom::Point(event->motion.x, event->motion.y); - if (event->motion.state & GDK_CONTROL_MASK) { - showInfoBox(last_pos, true); - } else { - showInfoBox(last_pos, false); - } - } else { - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - //Inkscape::Util::Unit const * unit = desktop->getNamedView()->getDisplayUnit(); - for (auto & idx : measure_item) { - sp_canvas_item_destroy(idx); - } - measure_item.clear(); - ret = TRUE; - Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100); - Geom::Point const motion_w(event->motion.x, event->motion.y); - if ( within_tolerance) { - if ( Geom::LInfty( motion_w - start_p ) < tolerance) { - return FALSE; // Do not drag if we're within tolerance from origin. - } - } - // Once the user has moved farther than tolerance from the original location - // (indicating they intend to move the object, not click), then always process the - // motion notify coordinates as given (no snapping back to origin) - within_tolerance = false; - if(event->motion.time == 0 || !last_end || Geom::LInfty( motion_w - *last_end ) > (tolerance/4.0)) { - Geom::Point const motion_dt(desktop->w2d(motion_w)); - end_p = motion_dt; - - if (event->motion.state & GDK_CONTROL_MASK) { - spdc_endpoint_snap_rotation(this, end_p, start_p, event->motion.state); - } else if (!(event->motion.state & GDK_SHIFT_MASK)) { - SnapManager &snap_manager = desktop->namedview->snap_manager; - snap_manager.setup(desktop); - Inkscape::SnapCandidatePoint scp(end_p, Inkscape::SNAPSOURCE_OTHER_HANDLE); - scp.addOrigin(start_p); - Inkscape::SnappedPoint sp = snap_manager.freeSnap(scp); - end_p = sp.getPoint(); - snap_manager.unSetup(); - } - showCanvasItems(); - last_end = motion_w ; - } - gobble_motion_events(GDK_BUTTON1_MASK); - } - break; - } - case GDK_BUTTON_RELEASE: { - this->knot_start->moveto(start_p); - this->knot_start->show(); - if(last_end) { - end_p = desktop->w2d(*last_end); - if (event->button.state & GDK_CONTROL_MASK) { - spdc_endpoint_snap_rotation(this, end_p, start_p, event->motion.state); - } else if (!(event->button.state & GDK_SHIFT_MASK)) { - SnapManager &snap_manager = desktop->namedview->snap_manager; - snap_manager.setup(desktop); - Inkscape::SnapCandidatePoint scp(end_p, Inkscape::SNAPSOURCE_OTHER_HANDLE); - scp.addOrigin(start_p); - Inkscape::SnappedPoint sp = snap_manager.freeSnap(scp); - end_p = sp.getPoint(); - snap_manager.unSetup(); - } - } - this->knot_end->moveto(end_p); - this->knot_end->show(); - showCanvasItems(); - - if (this->grabbed) { - sp_canvas_item_ungrab(this->grabbed); - this->grabbed = nullptr; - } - break; - } - default: - break; - } + ret = ToolBase::root_handler(event); if (!ret) { - ret = ToolBase::root_handler(event); + api.push_event(event); } return ret; } -void LivecodeTool::setMarkers() -{ - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - SPDocument *doc = desktop->getDocument(); - SPObject *arrowStart = doc->getObjectById("Arrow2Sstart"); - SPObject *arrowEnd = doc->getObjectById("Arrow2Send"); - if (!arrowStart) { - setMarker(true); - } - if(!arrowEnd) { - setMarker(false); - } -} -void LivecodeTool::setMarker(bool isStart) -{ - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - SPDocument *doc = desktop->getDocument(); - SPDefs *defs = doc->getDefs(); - Inkscape::XML::Node *rmarker; - Inkscape::XML::Document *xml_doc = doc->getReprDoc(); - rmarker = xml_doc->createElement("svg:marker"); - rmarker->setAttribute("id", isStart ? "Arrow2Sstart" : "Arrow2Send"); - rmarker->setAttribute("inkscape:isstock", "true"); - rmarker->setAttribute("inkscape:stockid", isStart ? "Arrow2Sstart" : "Arrow2Send"); - rmarker->setAttribute("orient", "auto"); - rmarker->setAttribute("refX", "0.0"); - rmarker->setAttribute("refY", "0.0"); - rmarker->setAttribute("style", "overflow:visible;"); - SPItem *marker = SP_ITEM(defs->appendChildRepr(rmarker)); - Inkscape::GC::release(rmarker); - marker->updateRepr(); - Inkscape::XML::Node *rpath; - rpath = xml_doc->createElement("svg:path"); - rpath->setAttribute("d", "M 8.72,4.03 L -2.21,0.02 L 8.72,-4.00 C 6.97,-1.63 6.98,1.62 8.72,4.03 z"); - rpath->setAttribute("id", isStart ? "Arrow2SstartPath" : "Arrow2SendPath"); - SPCSSAttr *css = sp_repr_css_attr_new(); - sp_repr_css_set_property (css, "stroke", "none"); - sp_repr_css_set_property (css, "fill", "#000000"); - sp_repr_css_set_property (css, "fill-opacity", "1"); - Glib::ustring css_str; - sp_repr_css_write_string(css,css_str); - rpath->setAttribute("style", css_str.c_str()); - sp_repr_css_attr_unref (css); - rpath->setAttribute("transform", isStart ? "scale(0.3) translate(-2.3,0)" : "scale(0.3) rotate(180) translate(-2.3,0)"); - SPItem *path = SP_ITEM(marker->appendChildRepr(rpath)); - Inkscape::GC::release(rpath); - path->updateRepr(); -} +static Geom::Point p(100, 100); +static Geom::Point a(50, 100), b(120, 300); +static Geom::Rect rect(Geom::Point(20, 200), Geom::Point(220, 20)); -void LivecodeTool::toGuides() +bool LivecodeTool::handle_tick(Glib::RefPtr<Gdk::FrameClock> const &frame_clock) { - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if(!desktop || !start_p.isFinite() || !end_p.isFinite() || start_p == end_p) { - return; - } - SPDocument *doc = desktop->getDocument(); - Geom::Point start = desktop->doc2dt(start_p) * desktop->doc2dt(); - Geom::Point end = desktop->doc2dt(end_p) * desktop->doc2dt(); - Geom::Ray ray(start,end); - SPNamedView *namedview = desktop->namedview; - if(!namedview) { - return; - } - setGuide(start,ray.angle(), _("Measure")); - if(explicit_base) { - explicit_base = *explicit_base * SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse(); - ray.setPoints(start, *explicit_base); - if(ray.angle() != 0) { - setGuide(start,ray.angle(), _("Base")); - } - } - setGuide(start,0,""); - setGuide(start,Geom::rad_from_deg(90),_("Start")); - setGuide(end,0,_("End")); - setGuide(end,Geom::rad_from_deg(90),""); - showCanvasItems(true); - doc->ensureUpToDate(); - DocumentUndo::done(desktop->getDocument(), SP_VERB_CONTEXT_MEASURE,_("Add guides from measure tool")); -} - -void LivecodeTool::toPhantom() -{ - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if(!desktop || !start_p.isFinite() || !end_p.isFinite() || start_p == end_p) { - return; - } - SPDocument *doc = desktop->getDocument(); - for (auto & measure_phantom_item : measure_phantom_items) { - sp_canvas_item_destroy(measure_phantom_item); - } - measure_phantom_items.clear(); - for (auto & measure_tmp_item : measure_tmp_items) { - sp_canvas_item_destroy(measure_tmp_item); - } - measure_tmp_items.clear(); - showCanvasItems(false, false, true); - doc->ensureUpToDate(); - DocumentUndo::done(desktop->getDocument(), SP_VERB_CONTEXT_MEASURE,_("Keep last measure on the canvas, for reference")); -} - -void LivecodeTool::toItem() -{ - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if(!desktop || !start_p.isFinite() || !end_p.isFinite() || start_p == end_p) { - return; - } - SPDocument *doc = desktop->getDocument(); - Geom::Ray ray(start_p,end_p); - guint32 line_color_primary = 0x0000ff7f; - Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc(); - Inkscape::XML::Node *rgroup = xml_doc->createElement("svg:g"); - showCanvasItems(false, true, false, rgroup); - setLine(start_p,end_p, false, line_color_primary, rgroup); - SPItem *measure_item = SP_ITEM(desktop->currentLayer()->appendChildRepr(rgroup)); - Inkscape::GC::release(rgroup); - measure_item->updateRepr(); - doc->ensureUpToDate(); - DocumentUndo::done(desktop->getDocument(), SP_VERB_CONTEXT_MEASURE,_("Convert measure to items")); - reset(); -} - -void LivecodeTool::toMarkDimension() -{ - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if(!desktop || !start_p.isFinite() || !end_p.isFinite() || start_p == end_p) { - return; - } - SPDocument *doc = desktop->getDocument(); - setMarkers(); - Geom::Ray ray(start_p,end_p); - Geom::Point start = start_p + Geom::Point::polar(ray.angle(), 5); - Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - dimension_offset = prefs->getDouble("/tools/measure/offset", 5.0); - start = start + Geom::Point::polar(ray.angle() + Geom::rad_from_deg(90), -dimension_offset); - Geom::Point end = end_p + Geom::Point::polar(ray.angle(), -5); - end = end+ Geom::Point::polar(ray.angle() + Geom::rad_from_deg(90), -dimension_offset); - guint32 color = 0x000000ff; - setLine(start, end, true, color); - Glib::ustring unit_name = prefs->getString("/tools/measure/unit"); - if (!unit_name.compare("")) { - unit_name = DEFAULT_UNIT_NAME; - } - double fontsize = prefs->getDouble("/tools/measure/fontsize", 10.0); - int precision = prefs->getInt("/tools/measure/precision", 2); - std::stringstream precision_str; - precision_str.imbue(std::locale::classic()); - precision_str << "%." << precision << "f %s"; - Geom::Point middle = Geom::middle_point(start, end); - double totallengthval = (end_p - start_p).length(); - totallengthval = Inkscape::Util::Quantity::convert(totallengthval, "px", unit_name); - double scale = prefs->getDouble("/tools/measure/scale", 100.0) / 100.0; - gchar *totallength_str = g_strdup_printf(precision_str.str().c_str(), totallengthval * scale, unit_name.c_str()); - double textangle = Geom::rad_from_deg(180) - ray.angle(); - if (desktop->is_yaxisdown()) { - textangle = ray.angle() - Geom::rad_from_deg(180); - } - setLabelText(totallength_str, middle, fontsize, textangle, color); - g_free(totallength_str); - doc->ensureUpToDate(); - DocumentUndo::done(desktop->getDocument(), SP_VERB_CONTEXT_MEASURE,_("Add global measure line")); -} - -void LivecodeTool::setGuide(Geom::Point origin,double angle, const char *label) -{ - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - SPDocument *doc = desktop->getDocument(); - Inkscape::XML::Document *xml_doc = doc->getReprDoc(); - SPRoot const *root = doc->getRoot(); - Geom::Affine affine(Geom::identity()); - if(root) { - affine *= root->c2p.inverse(); - } - SPNamedView *namedview = desktop->namedview; - if(!namedview) { - return; - } - - // <sodipodi:guide> stores inverted y-axis coordinates - if (desktop->is_yaxisdown()) { - origin[Geom::Y] = doc->getHeight().value("px") - origin[Geom::Y]; - angle *= -1.0; - } - - origin *= affine; - //measure angle - Inkscape::XML::Node *guide; - guide = xml_doc->createElement("sodipodi:guide"); - std::stringstream position; - position.imbue(std::locale::classic()); - position << origin[Geom::X] << "," << origin[Geom::Y]; - guide->setAttribute("position", position.str().c_str() ); - guide->setAttribute("inkscape:color", "rgb(167,0,255)"); - guide->setAttribute("inkscape:label", label); - Geom::Point unit_vector = Geom::rot90(origin.polar(angle)); - std::stringstream angle_str; - angle_str.imbue(std::locale::classic()); - angle_str << unit_vector[Geom::X] << "," << unit_vector[Geom::Y]; - guide->setAttribute("orientation", angle_str.str().c_str()); - namedview->appendChild(guide); - Inkscape::GC::release(guide); -} - -void LivecodeTool::setLine(Geom::Point start_point,Geom::Point end_point, bool markers, guint32 color, Inkscape::XML::Node *measure_repr) -{ - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if(!desktop || !start_p.isFinite() || !end_p.isFinite()) { - return; - } - Geom::PathVector pathv; - Geom::Path path; - path.start(desktop->doc2dt(start_point)); - path.appendNew<Geom::LineSegment>(desktop->doc2dt(end_point)); - pathv.push_back(path); - pathv *= SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse(); - if(!pathv.empty()) { - setMeasureItem(pathv, false, markers, color, measure_repr); - } -} - -void LivecodeTool::setPoint(Geom::Point origin, Inkscape::XML::Node *measure_repr) -{ - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if(!desktop || !origin.isFinite()) { - return; - } - char const * svgd; - svgd = "m 0.707,0.707 6.586,6.586 m 0,-6.586 -6.586,6.586"; - Geom::PathVector pathv = sp_svg_read_pathv(svgd); - Geom::Scale scale = Geom::Scale(desktop->current_zoom()).inverse(); - pathv *= Geom::Translate(Geom::Point(-3.5,-3.5)); - pathv *= scale; - pathv *= Geom::Translate(Geom::Point() - (scale.vector() * 0.5)); - pathv *= Geom::Translate(desktop->doc2dt(origin)); - pathv *= SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse(); - if (!pathv.empty()) { - guint32 line_color_secondary = 0xff0000ff; - setMeasureItem(pathv, false, false, line_color_secondary, measure_repr); - } -} - -void LivecodeTool::setLabelText(const char *value, Geom::Point pos, double fontsize, Geom::Coord angle, guint32 background, Inkscape::XML::Node *measure_repr, CanvasTextAnchorPositionEnum text_anchor) -{ - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - Inkscape::XML::Document *xml_doc = desktop->doc()->getReprDoc(); - /* Create <text> */ - pos = desktop->doc2dt(pos); - Inkscape::XML::Node *rtext = xml_doc->createElement("svg:text"); - rtext->setAttribute("xml:space", "preserve"); - - - /* Set style */ - sp_desktop_apply_style_tool(desktop, rtext, "/tools/text", true); - if(measure_repr) { - sp_repr_set_svg_double(rtext, "x", 2); - sp_repr_set_svg_double(rtext, "y", 2); - } else { - sp_repr_set_svg_double(rtext, "x", 0); - sp_repr_set_svg_double(rtext, "y", 0); - } - - /* Create <tspan> */ - Inkscape::XML::Node *rtspan = xml_doc->createElement("svg:tspan"); - rtspan->setAttribute("sodipodi:role", "line"); - SPCSSAttr *css = sp_repr_css_attr_new(); - std::stringstream font_size; - font_size.imbue(std::locale::classic()); - if(measure_repr) { - font_size << fontsize; - } else { - font_size << fontsize << "pt"; - } - sp_repr_css_set_property (css, "font-size", font_size.str().c_str()); - sp_repr_css_set_property (css, "font-style", "normal"); - sp_repr_css_set_property (css, "font-weight", "normal"); - sp_repr_css_set_property (css, "line-height", "125%"); - sp_repr_css_set_property (css, "letter-spacing", "0"); - sp_repr_css_set_property (css, "word-spacing", "0"); - sp_repr_css_set_property (css, "text-align", "center"); - sp_repr_css_set_property (css, "text-anchor", "middle"); - if(measure_repr) { - sp_repr_css_set_property (css, "fill", "#FFFFFF"); - } else { - sp_repr_css_set_property (css, "fill", "#000000"); - } - sp_repr_css_set_property (css, "fill-opacity", "1"); - sp_repr_css_set_property (css, "stroke", "none"); - Glib::ustring css_str; - sp_repr_css_write_string(css,css_str); - rtspan->setAttribute("style", css_str.c_str()); - sp_repr_css_attr_unref (css); - rtext->addChild(rtspan, nullptr); - Inkscape::GC::release(rtspan); - /* Create TEXT */ - Inkscape::XML::Node *rstring = xml_doc->createTextNode(value); - rtspan->addChild(rstring, nullptr); - Inkscape::GC::release(rstring); - SPItem *text_item = SP_ITEM(desktop->currentLayer()->appendChildRepr(rtext)); - Inkscape::GC::release(rtext); - text_item->updateRepr(); - Geom::OptRect bbox = text_item->geometricBounds(); - if (!measure_repr && bbox) { - Geom::Point center = bbox->midpoint(); - text_item->transform *= Geom::Translate(center).inverse(); - pos += Geom::Point::polar(angle+ Geom::rad_from_deg(90), -bbox->height()); - } - if(measure_repr) { - /* Create <group> */ - Inkscape::XML::Node *rgroup = xml_doc->createElement("svg:g"); - /* Create <rect> */ - Inkscape::XML::Node *rrect = xml_doc->createElement("svg:rect"); - SPCSSAttr *css = sp_repr_css_attr_new (); - gchar color_line[64]; - sp_svg_write_color (color_line, sizeof(color_line), background); - sp_repr_css_set_property (css, "fill", color_line); - sp_repr_css_set_property (css, "fill-opacity", "0.5"); - sp_repr_css_set_property (css, "stroke-width", "0"); - Glib::ustring css_str; - sp_repr_css_write_string(css,css_str); - rrect->setAttribute("style", css_str.c_str()); - sp_repr_css_attr_unref (css); - sp_repr_set_svg_double(rgroup, "x", 0); - sp_repr_set_svg_double(rgroup, "y", 0); - sp_repr_set_svg_double(rrect, "x", -bbox->width()/2.0); - sp_repr_set_svg_double(rrect, "y", -bbox->height()); - sp_repr_set_svg_double(rrect, "width", bbox->width() + 6); - sp_repr_set_svg_double(rrect, "height", bbox->height() + 6); - Inkscape::XML::Node *rtextitem = text_item->getRepr(); - text_item->deleteObject(); - rgroup->addChild(rtextitem, nullptr); - Inkscape::GC::release(rtextitem); - rgroup->addChild(rrect, nullptr); - Inkscape::GC::release(rrect); - SPItem *text_item_box = SP_ITEM(desktop->currentLayer()->appendChildRepr(rgroup)); - Geom::Scale scale = Geom::Scale(desktop->current_zoom()).inverse(); - if(bbox && text_anchor == TEXT_ANCHOR_CENTER) { - text_item_box->transform *= Geom::Translate(bbox->midpoint() - Geom::Point(1.0,1.0)).inverse(); - } - text_item_box->transform *= scale; - text_item_box->transform *= Geom::Translate(Geom::Point() - (scale.vector() * 0.5)); - text_item_box->transform *= Geom::Translate(pos); - text_item_box->transform *= SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse(); - text_item_box->updateRepr(); - text_item_box->doWriteTransform(text_item_box->transform, nullptr, true); - Inkscape::XML::Node *rlabel = text_item_box->getRepr(); - text_item_box->deleteObject(); - measure_repr->addChild(rlabel, nullptr); - Inkscape::GC::release(rlabel); - } else { - text_item->transform *= Geom::Rotate(angle); - text_item->transform *= Geom::Translate(pos); - text_item->transform *= SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse(); - text_item->doWriteTransform(text_item->transform, nullptr, true); - } -} - -void LivecodeTool::reset() -{ - this->knot_start->hide(); - this->knot_end->hide(); - for (auto & measure_tmp_item : measure_tmp_items) { - sp_canvas_item_destroy(measure_tmp_item); - } - measure_tmp_items.clear(); -} - -void LivecodeTool::setMeasureCanvasText(bool is_angle, double precision, double amount, double fontsize, Glib::ustring unit_name, Geom::Point position, guint32 background, CanvasTextAnchorPositionEnum text_anchor, bool to_item, bool to_phantom, Inkscape::XML::Node *measure_repr) -{ - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - std::stringstream precision_str; - precision_str.imbue(std::locale::classic()); - if(is_angle){ - precision_str << "%." << precision << "f °"; - } else { - precision_str << "%." << precision << "f %s"; - } - gchar *measure_str = g_strdup_printf(precision_str.str().c_str(), amount, unit_name.c_str()); - SPCanvasText *canvas_tooltip = sp_canvastext_new(desktop->getTempGroup(), - desktop, - position, - measure_str); - sp_canvastext_set_fontsize(canvas_tooltip, fontsize); - canvas_tooltip->rgba = 0xffffffff; - canvas_tooltip->rgba_background = background; - canvas_tooltip->outline = false; - canvas_tooltip->background = true; - canvas_tooltip->anchor_position = text_anchor; - if(to_phantom){ - canvas_tooltip->rgba_background = 0x4444447f; - measure_phantom_items.push_back(SP_CANVAS_ITEM(canvas_tooltip)); - sp_canvas_item_show(SP_CANVAS_ITEM(canvas_tooltip)); - } else { - measure_tmp_items.push_back(SP_CANVAS_ITEM(canvas_tooltip)); - sp_canvas_item_show(SP_CANVAS_ITEM(canvas_tooltip)); - } - - if(to_item) { - setLabelText(measure_str, position, fontsize, 0, background, measure_repr); - } - g_free(measure_str); -} - -void LivecodeTool::setMeasureCanvasItem(Geom::Point position, bool to_item, bool to_phantom, Inkscape::XML::Node *measure_repr){ - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - guint32 color = 0xff0000ff; - if(to_phantom){ - color = 0x888888ff; - } - SPCanvasItem * canvasitem = sp_canvas_item_new(desktop->getTempGroup(), - SP_TYPE_CTRL, - "anchor", SP_ANCHOR_CENTER, - "size", 9, - "stroked", TRUE, - "stroke_color", color, - "mode", SP_KNOT_MODE_XOR, - "shape", SP_KNOT_SHAPE_CROSS, - NULL ); - - SP_CTRL(canvasitem)->moveto(position); - if(to_phantom){ - measure_phantom_items.push_back(canvasitem); - } else { - measure_tmp_items.push_back(canvasitem); - } - sp_canvas_item_show(canvasitem); - sp_canvas_item_move_to_z(canvasitem, 0); - - if(to_item) { - setPoint(position, measure_repr); - } -} + api.setup_frame(); -void LivecodeTool::setMeasureCanvasControlLine(Geom::Point start, Geom::Point end, bool to_item, bool to_phantom, Inkscape::CtrlLineType ctrl_line_type, Inkscape::XML::Node *measure_repr){ - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - gint32 color = ctrl_line_type == CTLINE_PRIMARY ? 0x0000ff7f : 0xff00007f; - if(to_phantom){ - color = ctrl_line_type == CTLINE_PRIMARY ? 0x4444447f : 0x8888887f; - } - SPCtrlLine *control_line = ControlManager::getManager().createControlLine(desktop->getTempGroup(), - start, - end, - ctrl_line_type); - control_line->rgba = color; - if(to_phantom){ - measure_phantom_items.push_back(SP_CANVAS_ITEM(control_line)); - } else { - measure_tmp_items.push_back(SP_CANVAS_ITEM(control_line)); - } - sp_canvas_item_move_to_z(SP_CANVAS_ITEM(control_line), 0); - sp_canvas_item_show(SP_CANVAS_ITEM(control_line)); - if(to_item) { - setLine(start, - end, - false, - color, - measure_repr); - } -} + api.input_point("p", &p); + api.input_arrow("a->b", &a, &b); + api.input_rect("rect", &rect); -void LivecodeTool::showItemInfoText(Geom::Point pos, gchar *measure_str, double fontsize) -{ - SPCanvasText *canvas_tooltip = sp_canvastext_new(desktop->getTempGroup(), - desktop, - pos, - measure_str); - sp_canvastext_set_fontsize(canvas_tooltip, fontsize); - canvas_tooltip->rgba = 0xffffffff; - canvas_tooltip->outline = false; - canvas_tooltip->background = true; - canvas_tooltip->anchor_position = TEXT_ANCHOR_LEFT; - canvas_tooltip->rgba_background = 0x00000099; - measure_item.push_back(SP_CANVAS_ITEM(canvas_tooltip)); - sp_canvas_item_show(SP_CANVAS_ITEM(canvas_tooltip)); + api.finish_frame(); + return true; } -void LivecodeTool::showInfoBox(Geom::Point cursor, bool into_groups) -{ - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - Inkscape::Util::Unit const * unit = desktop->getNamedView()->getDisplayUnit(); - for (auto & idx : measure_item) { - sp_canvas_item_destroy(idx); - } - measure_item.clear(); - - SPItem *newover = desktop->getItemAtPoint(cursor, into_groups); - if (newover) { - Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - double fontsize = prefs->getDouble("/tools/measure/fontsize", 10.0); - double scale = prefs->getDouble("/tools/measure/scale", 100.0) / 100.0; - int precision = prefs->getInt("/tools/measure/precision", 2); - Glib::ustring unit_name = prefs->getString("/tools/measure/unit"); - bool only_selected = prefs->getBool("/tools/measure/only_selected", false); - if (!unit_name.compare("")) { - unit_name = DEFAULT_UNIT_NAME; - } - Geom::Scale zoom = Geom::Scale(Inkscape::Util::Quantity::convert(desktop->current_zoom(), "px", unit->abbr)).inverse(); - if(newover != over){ - over = newover; - Preferences *prefs = Preferences::get(); - int prefs_bbox = prefs->getBool("/tools/bounding_box", false); - SPItem::BBoxType bbox_type = !prefs_bbox ? SPItem::VISUAL_BBOX : SPItem::GEOMETRIC_BBOX; - Geom::OptRect bbox = over->bounds(bbox_type); - if (bbox) { - - item_width = Inkscape::Util::Quantity::convert((*bbox).width() * scale, unit->abbr, unit_name); - item_height = Inkscape::Util::Quantity::convert((*bbox).height() * scale, unit->abbr, unit_name); - item_x = Inkscape::Util::Quantity::convert((*bbox).left(), unit->abbr, unit_name); - Geom::Point y_point(0,Inkscape::Util::Quantity::convert((*bbox).bottom() * scale, unit->abbr, "px")); - y_point *= desktop->doc2dt(); - item_y = Inkscape::Util::Quantity::convert(y_point[Geom::Y] * scale, "px", unit_name); - if (SP_IS_SHAPE(over)) { - Geom::PathVector shape = SP_SHAPE(over)->getCurve()->get_pathvector(); - item_length = Geom::length(paths_to_pw(shape)); - item_length = Inkscape::Util::Quantity::convert(item_length * scale, unit->abbr, unit_name); - } - } - } - gchar *measure_str = nullptr; - std::stringstream precision_str; - precision_str.imbue(std::locale::classic()); - double origin = Inkscape::Util::Quantity::convert(14, "px", unit->abbr); - Geom::Point rel_position = Geom::Point(origin, origin); - Geom::Point pos = desktop->w2d(cursor); - double gap = Inkscape::Util::Quantity::convert(7 + fontsize, "px", unit->abbr); - if (only_selected) { - if (desktop->getSelection()->includes(over)) { - showItemInfoText(pos + (rel_position * zoom),_("Selected"),fontsize); - } else { - showItemInfoText(pos + (rel_position * zoom),_("Not selected"),fontsize); - } - rel_position = Geom::Point(rel_position[Geom::X], rel_position[Geom::Y] + gap); - } - if (SP_IS_SHAPE(over)) { - precision_str << _("Length") << ": %." << precision << "f %s"; - measure_str = g_strdup_printf(precision_str.str().c_str(), item_length, unit_name.c_str()); - precision_str.str(""); - showItemInfoText(pos + (rel_position * zoom),measure_str,fontsize); - rel_position = Geom::Point(rel_position[Geom::X], rel_position[Geom::Y] + gap); - } else if (SP_IS_GROUP(over)) { - measure_str = _("Press 'CTRL' to measure into group"); - showItemInfoText(pos + (rel_position * zoom),measure_str,fontsize); - rel_position = Geom::Point(rel_position[Geom::X], rel_position[Geom::Y] + gap); - } - precision_str << "Y: %." << precision << "f %s"; - measure_str = g_strdup_printf(precision_str.str().c_str(), item_y, unit_name.c_str()); - precision_str.str(""); - showItemInfoText(pos + (rel_position * zoom),measure_str,fontsize); - rel_position = Geom::Point(rel_position[Geom::X], rel_position[Geom::Y] + gap); - - precision_str << "X: %." << precision << "f %s"; - measure_str = g_strdup_printf(precision_str.str().c_str(), item_x, unit_name.c_str()); - precision_str.str(""); - showItemInfoText(pos + (rel_position * zoom),measure_str,fontsize); - rel_position = Geom::Point(rel_position[Geom::X], rel_position[Geom::Y] + gap); - - precision_str << _("Height") << ": %." << precision << "f %s"; - measure_str = g_strdup_printf(precision_str.str().c_str(), item_height, unit_name.c_str()); - precision_str.str(""); - showItemInfoText(pos + (rel_position * zoom),measure_str,fontsize); - rel_position = Geom::Point(rel_position[Geom::X], rel_position[Geom::Y] + gap); - - precision_str << _("Width") << ": %." << precision << "f %s"; - measure_str = g_strdup_printf(precision_str.str().c_str(), item_width, unit_name.c_str()); - precision_str.str(""); - showItemInfoText(pos + (rel_position * zoom),measure_str,fontsize); - g_free(measure_str); - } -} - -void LivecodeTool::showCanvasItems(bool to_guides, bool to_item, bool to_phantom, Inkscape::XML::Node *measure_repr) -{ - SPDesktop *desktop = SP_ACTIVE_DESKTOP; - if(!desktop || !start_p.isFinite() || !end_p.isFinite() || start_p == end_p) { - return; - } - writeMeasurePoint(start_p, true); - writeMeasurePoint(end_p, false); - //clear previous canvas items, we'll draw new ones - for (auto & measure_tmp_item : measure_tmp_items) { - sp_canvas_item_destroy(measure_tmp_item); - } - measure_tmp_items.clear(); - //TODO:Calculate the measure area for current length and origin - // and use canvas->requestRedraw. In the calculation need a gap for outside text - // maybe this remove the trash lines on measure use - Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - bool show_in_between = prefs->getBool("/tools/measure/show_in_between", true); - bool all_layers = prefs->getBool("/tools/measure/all_layers", true); - dimension_offset = 70; - Geom::PathVector lineseg; - Geom::Path p; - Geom::Point start_p_doc = start_p * desktop->dt2doc(); - Geom::Point end_p_doc = end_p * desktop->dt2doc(); - p.start(start_p_doc); - p.appendNew<Geom::LineSegment>(end_p_doc); - lineseg.push_back(p); - - double angle = atan2(end_p - start_p); - double baseAngle = 0; - - if (explicit_base) { - baseAngle = atan2(explicit_base.get() - start_p); - angle -= baseAngle; - } - - std::vector<SPItem*> items; - SPDocument *doc = desktop->getDocument(); - Geom::Rect rect(start_p_doc, end_p_doc); - items = doc->getItemsPartiallyInBox(desktop->dkey, rect, false, true, false, true); - Inkscape::LayerModel *layer_model = nullptr; - SPObject *current_layer = nullptr; - if(desktop){ - layer_model = desktop->layers; - current_layer = desktop->currentLayer(); - } - std::vector<double> intersection_times; - bool only_selected = prefs->getBool("/tools/measure/only_selected", false); - for (std::vector<SPItem*>::const_iterator i=items.begin(); i!=items.end(); ++i) { - SPItem *item = *i; - if (!desktop->getSelection()->includes(*i) && only_selected) { - continue; - } - if(all_layers || (layer_model && layer_model->layerForObject(item) == current_layer)){ - if (SP_IS_SHAPE(item)) { - calculate_intersections(desktop, item, lineseg, SP_SHAPE(item)->getCurve(), intersection_times); - } else { - if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) { - Inkscape::Text::Layout::iterator iter = te_get_layout(item)->begin(); - do { - Inkscape::Text::Layout::iterator iter_next = iter; - iter_next.nextGlyph(); // iter_next is one glyph ahead from iter - if (iter == iter_next) { - break; - } - - // get path from iter to iter_next: - SPCurve *curve = te_get_layout(item)->convertToCurves(iter, iter_next); - iter = iter_next; // shift to next glyph - if (!curve) { - continue; // error converting this glyph - } - if (curve->is_empty()) { // whitespace glyph? - curve->unref(); - continue; - } - - calculate_intersections(desktop, item, lineseg, curve, intersection_times); - if (iter == te_get_layout(item)->end()) { - break; - } - } while (true); - } - } - } - } - Glib::ustring unit_name = prefs->getString("/tools/measure/unit"); - if (!unit_name.compare("")) { - unit_name = DEFAULT_UNIT_NAME; - } - double scale = prefs->getDouble("/tools/measure/scale", 100.0) / 100.0; - double fontsize = prefs->getDouble("/tools/measure/fontsize", 10.0); - // Normal will be used for lines and text - Geom::Point windowNormal = Geom::unit_vector(Geom::rot90(desktop->d2w(end_p - start_p))); - Geom::Point normal = desktop->w2d(windowNormal); - - std::vector<Geom::Point> intersections; - std::sort(intersection_times.begin(), intersection_times.end()); - for (double & intersection_time : intersection_times) { - intersections.push_back(lineseg[0].pointAt(intersection_time)); - } - - if(!show_in_between && intersection_times.size() > 1) { - Geom::Point start = lineseg[0].pointAt(intersection_times[0]); - Geom::Point end = lineseg[0].pointAt(intersection_times[intersection_times.size()-1]); - intersections.clear(); - intersections.push_back(start); - intersections.push_back(end); - } - if (!prefs->getBool("/tools/measure/ignore_1st_and_last", true)) { - intersections.insert(intersections.begin(),lineseg[0].pointAt(0)); - intersections.push_back(lineseg[0].pointAt(1)); - } - std::vector<LabelPlacement> placements; - for (size_t idx = 1; idx < intersections.size(); ++idx) { - LabelPlacement placement; - placement.lengthVal = (intersections[idx] - intersections[idx - 1]).length(); - placement.lengthVal = Inkscape::Util::Quantity::convert(placement.lengthVal, "px", unit_name); - placement.offset = dimension_offset / 2; - placement.start = desktop->doc2dt( (intersections[idx - 1] + intersections[idx]) / 2 ); - placement.end = placement.start - (normal * placement.offset); - - placements.push_back(placement); - } - int precision = prefs->getInt("/tools/measure/precision", 2); - // Adjust positions - repositionOverlappingLabels(placements, desktop, windowNormal, fontsize, precision); - for (auto & place : placements) { - setMeasureCanvasText(false, precision, place.lengthVal * scale, fontsize, unit_name, place.end, 0x0000007f, TEXT_ANCHOR_CENTER, to_item, to_phantom, measure_repr); - } - Geom::Point angleDisplayPt = calcAngleDisplayAnchor(desktop, angle, baseAngle, - start_p, end_p, - fontsize); - { - setMeasureCanvasText(true, precision, Geom::deg_from_rad(angle), fontsize, unit_name, angleDisplayPt, 0x337f337f, TEXT_ANCHOR_CENTER, to_item, to_phantom, measure_repr); - } - - { - double totallengthval = (end_p - start_p).length(); - totallengthval = Inkscape::Util::Quantity::convert(totallengthval, "px", unit_name); - Geom::Point origin = end_p + desktop->w2d(Geom::Point(3*fontsize, -fontsize)); - setMeasureCanvasText(false, precision, totallengthval * scale, fontsize, unit_name, origin, 0x3333337f, TEXT_ANCHOR_LEFT, to_item, to_phantom, measure_repr); - } - - if (intersections.size() > 2) { - double totallengthval = (intersections[intersections.size()-1] - intersections[0]).length(); - totallengthval = Inkscape::Util::Quantity::convert(totallengthval, "px", unit_name); - Geom::Point origin = desktop->doc2dt((intersections[0] + intersections[intersections.size()-1])/2) + normal * dimension_offset; - setMeasureCanvasText(false, precision, totallengthval * scale, fontsize, unit_name, origin, 0x33337f7f, TEXT_ANCHOR_CENTER, to_item, to_phantom, measure_repr); - } - - // Initial point - { - setMeasureCanvasItem(start_p, false, to_phantom, measure_repr); - } - - // Now that text has been added, we can add lines and controls so that they go underneath - for (size_t idx = 0; idx < intersections.size(); ++idx) { - setMeasureCanvasItem(desktop->doc2dt(intersections[idx]), to_item, to_phantom, measure_repr); - if(to_guides) { - gchar *cross_number; - if (!prefs->getBool("/tools/measure/ignore_1st_and_last", true)) { - cross_number= g_strdup_printf(_("Crossing %lu"), static_cast<unsigned long>(idx)); - } else { - cross_number= g_strdup_printf(_("Crossing %lu"), static_cast<unsigned long>(idx + 1)); - } - if (!prefs->getBool("/tools/measure/ignore_1st_and_last", true) && idx == 0) { - setGuide(desktop->doc2dt(intersections[idx]), angle + Geom::rad_from_deg(90), ""); - } else { - setGuide(desktop->doc2dt(intersections[idx]), angle + Geom::rad_from_deg(90), cross_number); - } - g_free(cross_number); - } - } - // Since adding goes to the bottom, do all lines last. - - // draw main control line - { - setMeasureCanvasControlLine(start_p, end_p, false, to_phantom, CTLINE_PRIMARY, measure_repr); - double length = std::abs((end_p - start_p).length()); - Geom::Point anchorEnd = start_p; - anchorEnd[Geom::X] += length; - if (explicit_base) { - anchorEnd *= (Geom::Affine(Geom::Translate(-start_p)) - * Geom::Affine(Geom::Rotate(baseAngle)) - * Geom::Affine(Geom::Translate(start_p))); - } - setMeasureCanvasControlLine(start_p, anchorEnd, to_item, to_phantom, CTLINE_SECONDARY, measure_repr); - createAngleDisplayCurve(desktop, start_p, end_p, angleDisplayPt, angle, to_phantom, measure_phantom_items, measure_tmp_items, measure_repr); - } - - if (intersections.size() > 2) { - setMeasureCanvasControlLine(desktop->doc2dt(intersections[0]) + normal * dimension_offset, desktop->doc2dt(intersections[intersections.size() - 1]) + normal * dimension_offset, to_item, to_phantom, CTLINE_PRIMARY , measure_repr); - - setMeasureCanvasControlLine(desktop->doc2dt(intersections[0]), desktop->doc2dt(intersections[0]) + normal * dimension_offset, to_item, to_phantom, CTLINE_PRIMARY , measure_repr); - - setMeasureCanvasControlLine(desktop->doc2dt(intersections[intersections.size() - 1]), desktop->doc2dt(intersections[intersections.size() - 1]) + normal * dimension_offset, to_item, to_phantom, CTLINE_PRIMARY , measure_repr); - } - - // call-out lines - for (auto & place : placements) { - setMeasureCanvasControlLine(place.start, place.end, to_item, to_phantom, CTLINE_SECONDARY, measure_repr); - } - - { - for (size_t idx = 1; idx < intersections.size(); ++idx) { - Geom::Point measure_text_pos = (intersections[idx - 1] + intersections[idx]) / 2; - setMeasureCanvasControlLine(desktop->doc2dt(measure_text_pos), desktop->doc2dt(measure_text_pos) - (normal * dimension_offset / 2), to_item, to_phantom, CTLINE_SECONDARY, measure_repr); - } - } -} } } diff --git a/src/ui/tools/livecode-tool.h b/src/ui/tools/livecode-tool.h index 1b56561de..fdeb61264 100644 --- a/src/ui/tools/livecode-tool.h +++ b/src/ui/tools/livecode-tool.h @@ -3,12 +3,11 @@ #define SEEN_SP_LIVECODE_CONTEXT_H /* - * Our fine measuring tool + * An experimental Livecoding Tool * * Authors: - * Felipe Correa da Silva Sanches <juca@members.fsf.org> - * Jabiertxo Arraiza <jabier.arraiza@marker.es> - * Copyright (C) 2011 Authors + * Sol Bekic <s+inkscape@s-ol.nu> + * Copyright (C) 2019 Authors * * Released under GNU GPL v2+, read the file 'COPYING' for more information. */ @@ -20,6 +19,7 @@ #include "display/canvas-text.h" #include "display/canvas-temporary-item.h" #include "ui/control-manager.h" +#include "livecode/api.h" #include <boost/optional.hpp> #define SP_LIVECODE_CONTEXT(obj) (dynamic_cast<Inkscape::UI::Tools::LivecodeTool*>((Inkscape::UI::Tools::ToolBase*)obj)) @@ -38,58 +38,16 @@ public: static const std::string prefsPath; + void setup() override; void finish() override; bool root_handler(GdkEvent* event) override; - virtual void showCanvasItems(bool to_guides = false, bool to_item = false, bool to_phantom = false, Inkscape::XML::Node *measure_repr = nullptr); - virtual void reverseKnots(); - virtual void toGuides(); - virtual void toPhantom(); - virtual void toMarkDimension(); - virtual void toItem(); - virtual void reset(); - virtual void setMarkers(); - virtual void setMarker(bool isStart); const std::string& getPrefsPath() override; - Geom::Point readMeasurePoint(bool is_start); - void showInfoBox(Geom::Point cursor, bool into_groups); - void showItemInfoText(Geom::Point pos, gchar *measure_str, double fontsize); - void writeMeasurePoint(Geom::Point point, bool is_start); - void setGuide(Geom::Point origin, double angle, const char *label); - void setPoint(Geom::Point origin, Inkscape::XML::Node *measure_repr); - void setLine(Geom::Point start_point,Geom::Point end_point, bool markers, guint32 color, Inkscape::XML::Node *measure_repr = nullptr); - void setMeasureCanvasText(bool is_angle, double precision, double amount, double fontsize, Glib::ustring unit_name, Geom::Point position, guint32 background, CanvasTextAnchorPositionEnum text_anchor, bool to_item, bool to_phantom, Inkscape::XML::Node *measure_repr); - void setMeasureCanvasItem(Geom::Point position, bool to_item, bool to_phantom, Inkscape::XML::Node *measure_repr); - void setMeasureCanvasControlLine(Geom::Point start, Geom::Point end, bool to_item, bool to_phantom, Inkscape::CtrlLineType ctrl_line_type, Inkscape::XML::Node *measure_repr); - void setLabelText(const char *value, Geom::Point pos, double fontsize, Geom::Coord angle, guint32 background , Inkscape::XML::Node *measure_repr = nullptr, CanvasTextAnchorPositionEnum text_anchor = TEXT_ANCHOR_CENTER ); - void knotStartMovedHandler(SPKnot */*knot*/, Geom::Point const &ppointer, guint state); - void knotEndMovedHandler(SPKnot */*knot*/, Geom::Point const &ppointer, guint state); - void knotClickHandler(SPKnot *knot, guint state); - void knotUngrabbedHandler(SPKnot */*knot*/, unsigned int /*state*/); + private: - SPCanvasItem* grabbed; - boost::optional<Geom::Point> explicit_base; - boost::optional<Geom::Point> last_end; - SPKnot *knot_start; - SPKnot *knot_end; - gint dimension_offset; - Geom::Point start_p; - Geom::Point end_p; - Geom::Point last_pos; - std::vector<SPCanvasItem *> measure_tmp_items; - std::vector<SPCanvasItem *> measure_phantom_items; - std::vector<SPCanvasItem *> measure_item; - double item_width; - double item_height; - double item_x; - double item_y; - double item_length; - SPItem *over; - sigc::connection _knot_start_moved_connection; - sigc::connection _knot_start_ungrabbed_connection; - sigc::connection _knot_start_click_connection; - sigc::connection _knot_end_moved_connection; - sigc::connection _knot_end_click_connection; - sigc::connection _knot_end_ungrabbed_connection; + bool handle_tick(Glib::RefPtr<Gdk::FrameClock> const &frame_clock); + + guint tick_callback; + Livecode::API api; }; } |
