Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
canvas-item-quad.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
6/*
7 * Author:
8 * Tavmjong Bah
9 *
10 * Copyright (C) 2020 Tavmjong Bah
11 *
12 * Rewrite of SPCtrlQuadr
13 *
14 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
15 */
16
17#include <cassert>
18
19#include "canvas-item-quad.h"
20
21#include "display/cairo-utils.h"
22#include "helper/geom.h"
23
24namespace Inkscape {
25
30 : CanvasItem(group)
31{
32 _name = "CanvasItemQuad:Null";
33}
34
39 Geom::Point const &p0, Geom::Point const &p1,
40 Geom::Point const &p2, Geom::Point const &p3)
41 : CanvasItem(group)
42 , _p0(p0)
43 , _p1(p1)
44 , _p2(p2)
45 , _p3(p3)
46{
47 _name = "CanvasItemQuad";
48}
49
53void CanvasItemQuad::set_coords(Geom::Point const &p0, Geom::Point const &p1, Geom::Point const &p2, Geom::Point const &p3)
54{
55 defer([=, this] {
56 _p0 = p0;
57 _p1 = p1;
58 _p2 = p2;
59 _p3 = p3;
61 });
62}
63
67bool CanvasItemQuad::contains(Geom::Point const &p, double tolerance)
68{
69 if (tolerance != 0) {
70 std::cerr << "CanvasItemQuad::contains: Non-zero tolerance not implemented!" << std::endl;
71 }
72
73 Geom::Point p0 = _p0 * affine();
74 Geom::Point p1 = _p1 * affine();
75 Geom::Point p2 = _p2 * affine();
76 Geom::Point p3 = _p3 * affine();
77
78 // From 2geom rotated-rect.cpp
79 return
80 Geom::cross(p1 - p0, p - p0) >= 0 &&
81 Geom::cross(p2 - p1, p - p1) >= 0 &&
82 Geom::cross(p3 - p2, p - p2) >= 0 &&
83 Geom::cross(p0 - p3, p - p3) >= 0;
84}
85
90{
91 if (_p0 == _p1 || _p1 == _p2 || _p2 == _p3 || _p3 == _p0) {
92 _bounds = {};
93 return; // Not quad or not initialized.
94 }
95
96 // Queue redraw of old area (erase previous content).
97 request_redraw(); // This is actually never useful as quads are always deleted
98 // and recreated when a node is moved! But keep it in case we change that.
99
100 _bounds = expandedBy(bounds_of(_p0, _p1, _p2, _p3) * affine(), 2); // Room for anti-aliasing effects.
101
102 // Queue redraw of new area
104}
105
110{
111 // Document to canvas
112 Geom::Point p0 = _p0 * affine();
113 Geom::Point p1 = _p1 * affine();
114 Geom::Point p2 = _p2 * affine();
115 Geom::Point p3 = _p3 * affine();
116
117 // Canvas to screen
118 p0 *= Geom::Translate(-buf.rect.min());
119 p1 *= Geom::Translate(-buf.rect.min());
120 p2 *= Geom::Translate(-buf.rect.min());
121 p3 *= Geom::Translate(-buf.rect.min());
122
123 buf.cr->save();
124
125 buf.cr->begin_new_path();
126
127 buf.cr->move_to(p0.x(), p0.y());
128 buf.cr->line_to(p1.x(), p1.y());
129 buf.cr->line_to(p2.x(), p2.y());
130 buf.cr->line_to(p3.x(), p3.y());
131 buf.cr->close_path();
132
133 if (_inverted) {
134 cairo_set_operator(buf.cr->cobj(), CAIRO_OPERATOR_DIFFERENCE);
135 }
136
138 buf.cr->fill_preserve();
139
140 buf.cr->set_line_width(1);
142 buf.cr->stroke_preserve();
143 buf.cr->begin_new_path();
144
145 buf.cr->restore();
146}
147
149{
150 defer([=, this] {
151 if (_inverted == inverted) return;
152 _inverted = inverted;
154 });
155}
156
157} // namespace Inkscape
158
159/*
160 Local Variables:
161 mode:c++
162 c-file-style:"stroustrup"
163 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
164 indent-tabs-mode:nil
165 fill-column:99
166 End:
167*/
168// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
void ink_cairo_set_source_rgba32(cairo_t *ct, guint32 rgba)
Cairo integration helpers.
Two-dimensional point that doubles as a vector.
Definition point.h:66
constexpr Coord y() const noexcept
Definition point.h:106
constexpr Coord x() const noexcept
Definition point.h:104
Translation by a vector.
Definition transforms.h:115
void set_coords(Geom::Point const &p0, Geom::Point const &p1, Geom::Point const &p2, Geom::Point const &p3)
Set a control quad.
bool contains(Geom::Point const &p, double tolerance=0) override
Returns true if point p (in canvas units) is within tolerance (canvas units) distance of quad.
void set_inverted(bool inverted)
CanvasItemQuad(CanvasItemGroup *group)
Create an null control quad.
void _render(Inkscape::CanvasItemBuffer &buf) const override
Render quad to screen via Cairo.
void _update(bool propagate) override
Update and redraw control quad.
Geom::OptRect _bounds
Geom::Affine const & affine() const
Specific geometry functions for Inkscape, not provided my lib2geom.
auto expandedBy(Geom::IntRect rect, int amount)
Definition geom.h:67
auto bounds_of(Geom::Point const &pt, Args const &... args)
Get the bounding box of a collection of points.
Definition geom.h:120
Piecewise< SBasis > cross(Piecewise< D2< SBasis > > const &a, Piecewise< D2< SBasis > > const &b)
Helper class to stream background task notifications as a series of messages.
int buf
Class used when rendering canvas items.