summaryrefslogtreecommitdiffstats
path: root/src/ui/knot/knot-holder-entity.h
blob: 62a090602ef5bda3d7c68a5c53f442d424606d93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// SPDX-License-Identifier: GPL-2.0-or-later
#ifndef SEEN_KNOT_HOLDER_ENTITY_H
#define SEEN_KNOT_HOLDER_ENTITY_H
/*
 * Authors:
 *   Mitsuru Oka <oka326@parkcity.ne.jp>
 *   Maximilian Albert <maximilian.albert@gmail.com>
 *
 * Copyright (C) 1999-2001 Lauris Kaplinski
 * Copyright (C) 2000-2001 Ximian, Inc.
 * Copyright (C) 2001 Mitsuru Oka
 * Copyright (C) 2004 Monash University
 * Copyright (C) 2008 Maximilian Albert
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <2geom/forward.h>

#include "knot.h"
#include "snapper.h"

#include "display/control/canvas-item-enums.h"
#include "display/control/canvas-item-quad.h"
#include "display/control/canvas-item-curve.h"
#include "helper/auto-connection.h"
#include "display/control/canvas-item-ptr.h"

class SPHatch;
class SPItem;
class SPKnot;
class SPDesktop;
class SPPattern;
class SPGaussianBlur;
class KnotHolder;

namespace Inkscape {
namespace LivePathEffect {
    class Effect;
} // namespace LivePathEffect
} // namespace Inkscape

typedef void (* SPKnotHolderSetFunc) (SPItem *item, Geom::Point const &p, Geom::Point const &origin, unsigned int state);
typedef Geom::Point (* SPKnotHolderGetFunc) (SPItem *item);

/**
 * KnotHolderEntity definition.
 */
class KnotHolderEntity {
public:
    KnotHolderEntity() {}
    virtual ~KnotHolderEntity();

    void create(SPDesktop *desktop, SPItem *item, KnotHolder *parent,
                Inkscape::CanvasItemCtrlType type = Inkscape::CANVAS_ITEM_CTRL_TYPE_DEFAULT,
                Glib::ustring const & name = Glib::ustring("unknown"),
                char const *tip = "",
                guint32 color = 0xffffff00);

    /* the get/set/click handlers are virtual functions; each handler class for a knot
       should be derived from KnotHolderEntity and override these functions */
    virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, unsigned int state) = 0;
    virtual void knot_grabbed(Geom::Point const &/*grab_position*/, unsigned /*state*/) {}
    virtual void knot_ungrabbed(Geom::Point const &p, Geom::Point const &origin, unsigned int state) = 0;
    virtual bool knot_missing() const { return false; }
    virtual Geom::Point knot_get() const = 0;
    virtual void knot_click(unsigned int /*state*/) {}
    virtual bool set_item_clickpos(Geom::Point loc) { return false; }

    virtual void on_created() {}
    virtual void update_knot();

    // private:
    Geom::Point snap_knot_position(Geom::Point const &p, unsigned int state);
    Geom::Point snap_knot_position_constrained(Geom::Point const &p, Inkscape::Snapper::SnapConstraint const &constraint, unsigned int state);

    SPKnot *knot = nullptr;
    SPItem *item = nullptr;
    SPDesktop *desktop = nullptr;
    KnotHolder *parent_holder = nullptr;

    int my_counter = 0;
    inline static int counter = 0;

    /** Connection to \a knot's "moved" signal. */
    unsigned int   handler_id = 0;
    /** Connection to \a knot's "clicked" signal. */
    unsigned int   _click_handler_id = 0;
    /** Connection to \a knot's "ungrabbed" signal. */
    unsigned int   _ungrab_handler_id = 0;

private:
    sigc::connection _mousedown_connection;
    sigc::connection _moved_connection;
    sigc::connection _click_connection;
    sigc::connection _ungrabbed_connection;
};

// derived KnotHolderEntity class for LPEs
class LPEKnotHolderEntity : public KnotHolderEntity {
public:
    LPEKnotHolderEntity(Inkscape::LivePathEffect::Effect *effect) : _effect(effect) {};
    void knot_ungrabbed(Geom::Point const &p, Geom::Point const &origin, guint state) override;
protected:
    Inkscape::LivePathEffect::Effect *_effect;
};

/* pattern manipulation */

class PatternKnotHolderEntity : public KnotHolderEntity {
  public:
    PatternKnotHolderEntity(bool fill) : KnotHolderEntity(), _fill(fill) {}
    void on_created() override;
    bool knot_missing() const override;
    void knot_ungrabbed(Geom::Point const &p, Geom::Point const &origin, guint state) override{};
    bool set_item_clickpos(Geom::Point loc) override;
    void update_knot() override;

protected:
    // true if the entity tracks fill, false for stroke
    bool _fill;
    SPPattern *_pattern() const;
    Geom::Point _get_pos(gdouble x, gdouble y, bool transform = true) const;
    Geom::IntPoint offset_to_cell(Geom::Point loc) const;
    Geom::IntPoint _cell;
};

class PatternKnotHolderEntityXY : public PatternKnotHolderEntity {
public:
    PatternKnotHolderEntityXY(bool fill) : PatternKnotHolderEntity(fill) {}
    ~PatternKnotHolderEntityXY() override = default;

    void on_created() override;
    void update_knot() override;
    Geom::Point knot_get() const override;
    void knot_set(Geom::Point const &p, Geom::Point const &origin, unsigned int state) override;

private:
    // Extra visual element to show the pattern editing area
    CanvasItemPtr<Inkscape::CanvasItemQuad> _quad;
};

class PatternKnotHolderEntityAngle : public PatternKnotHolderEntity {
public:
    PatternKnotHolderEntityAngle(bool fill) : PatternKnotHolderEntity(fill) {}
    Geom::Point knot_get() const override;
    void knot_set(Geom::Point const &p, Geom::Point const &origin, unsigned int state) override;
};

class PatternKnotHolderEntityScale : public PatternKnotHolderEntity {
public:
    PatternKnotHolderEntityScale(bool fill) : PatternKnotHolderEntity(fill) {}
    Geom::Point knot_get() const override;
    void knot_set(Geom::Point const &p, Geom::Point const &origin, unsigned int state) override;
    void knot_grabbed(Geom::Point const &grab_pos, unsigned state) override;

private:
    /// Maximum number of pattern repetitons allowed in an item
    inline static double const MAX_REPETITIONS = 1e6;
    Geom::Affine _cached_transform, _cached_inverse_linear;
    Geom::Point _cached_origin, _cached_diagonal;
    double _cached_min_scale;
};

/* Hatch manipulation */
class HatchKnotHolderEntity : public KnotHolderEntity {
  public:
    HatchKnotHolderEntity(bool fill)
        : KnotHolderEntity()
        , _fill(fill)
    {
    }
    bool knot_missing() const override;
    void knot_ungrabbed(Geom::Point const &p, Geom::Point const &origin, guint state) override{};

  protected:
    // true if the entity tracks fill, false for stroke
    bool _fill;
    SPHatch *_hatch() const;
};

class HatchKnotHolderEntityXY : public HatchKnotHolderEntity {
  public:
    HatchKnotHolderEntityXY(bool fill)
        : HatchKnotHolderEntity(fill)
    {
    }
    Geom::Point knot_get() const override;
    void knot_set(Geom::Point const &p, Geom::Point const &origin, unsigned int state) override;
};

class HatchKnotHolderEntityAngle : public HatchKnotHolderEntity {
  public:
    HatchKnotHolderEntityAngle(bool fill)
        : HatchKnotHolderEntity(fill)
    {
    }
    Geom::Point knot_get() const override;
    void knot_set(Geom::Point const &p, Geom::Point const &origin, unsigned int state) override;
};

class HatchKnotHolderEntityScale : public HatchKnotHolderEntity {
  public:
    HatchKnotHolderEntityScale(bool fill)
        : HatchKnotHolderEntity(fill)
    {
    }
    Geom::Point knot_get() const override;
    void knot_set(Geom::Point const &p, Geom::Point const &origin, unsigned int state) override;
};


/* Filter manipulation */
class FilterKnotHolderEntity : public KnotHolderEntity {
  public:
    FilterKnotHolderEntity(bool topleft) : KnotHolderEntity(), _topleft(topleft) {}
    Geom::Point knot_get() const override;
    void knot_ungrabbed(Geom::Point const &p, Geom::Point const &origin, guint state) override {};
    void knot_set(Geom::Point const &p, Geom::Point const &origin, unsigned int state) override;
private:
    bool _topleft; // true for topleft point, false for bottomright
};

class BlurKnotHolderEntity : public KnotHolderEntity {
  public:
    BlurKnotHolderEntity(int direction) : KnotHolderEntity(), _dir(direction) {}
    void on_created() override;
    void update_knot() override;
    Geom::Point knot_get() const override;
    void knot_ungrabbed(Geom::Point const &p, Geom::Point const &origin, guint state) override {};
    void knot_set(Geom::Point const &p, Geom::Point const &origin, unsigned int state) override;

  private:
    SPGaussianBlur *_blur() const;
    Geom::Point _pos() const;

    int _dir;
    CanvasItemPtr<Inkscape::CanvasItemCurve> _line;
    Inkscape::auto_connection _watch_filter;
    Inkscape::auto_connection _watch_blur;
};

#endif /* !SEEN_KNOT_HOLDER_ENTITY_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 :