summaryrefslogtreecommitdiffstats
path: root/src/display/sodipodi-ctrlrect.cpp
blob: b47f67680875ccaf3f0cf6fe70075a33009aedc0 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Simple non-transformed rectangle, usable for rubberband
 *
 * Author:
 *   Lauris Kaplinski <lauris@ximian.com>
 *   bulia byak <buliabyak@users.sf.net>
 *   Carl Hetherington <inkscape@carlh.net>
 *   Jon A. Cruz <jon@joncruz.org>
 *   Tavmjong Bah <tavmjong@free.fr>
 *
 * Copyright (C) 1999-2001 Lauris Kaplinski
 * Copyright (C) 2000-2001 Ximian, Inc.
 * Copyright (C) 2017 Tavmjong Bah
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 *
 */

#include "inkscape.h"
#include "sodipodi-ctrlrect.h"
#include "sp-canvas-util.h"
#include "display/cairo-utils.h"
#include "display/sp-canvas.h"
#include <2geom/transforms.h>

/*
 * Currently we do not have point method, as it should always be painted
 * during some transformation, which takes care of events...
 *
 * Corner coords can be in any order - i.e. x1 < x0 is allowed
 */

static void sp_ctrlrect_destroy(SPCanvasItem *object);

static void sp_ctrlrect_update(SPCanvasItem *item, Geom::Affine const &affine, unsigned int flags);
static void sp_ctrlrect_render(SPCanvasItem *item, SPCanvasBuf *buf);

G_DEFINE_TYPE(CtrlRect, sp_ctrlrect, SP_TYPE_CANVAS_ITEM);

static void sp_ctrlrect_class_init(CtrlRectClass *c)
{
    SPCanvasItemClass *item_class = SP_CANVAS_ITEM_CLASS(c);

    item_class->destroy = sp_ctrlrect_destroy;
    item_class->update = sp_ctrlrect_update;
    item_class->render = sp_ctrlrect_render;
}

static void sp_ctrlrect_init(CtrlRect *cr)
{
    cr->init();
}

static void sp_ctrlrect_destroy(SPCanvasItem *object)
{
    if (SP_CANVAS_ITEM_CLASS(sp_ctrlrect_parent_class)->destroy) {
        (* SP_CANVAS_ITEM_CLASS(sp_ctrlrect_parent_class)->destroy)(object);
    }
}


static void sp_ctrlrect_render(SPCanvasItem *item, SPCanvasBuf *buf)
{
    SP_CTRLRECT(item)->render(buf);
}


static void sp_ctrlrect_update(SPCanvasItem *item, Geom::Affine const &affine, unsigned int flags)
{
    SP_CTRLRECT(item)->update(affine, flags);
}



void CtrlRect::init()
{
    _has_fill = false;
    _dashed = false;
    _checkerboard = false;

    _shadow_width = 0;

    _area = Geom::OptIntRect();

    _rect = Geom::Rect(Geom::Point(0,0),Geom::Point(0,0));

    _border_color = 0x000000ff;
    _fill_color = 0xffffffff;
    _shadow_color = 0x000000ff;
    _inverted = false;
}


void CtrlRect::render(SPCanvasBuf *buf)
{
    using Geom::X;
    using Geom::Y;

    if (!_area) {
        return;
    }

    Geom::IntRect area = *_area; // _area already includes space for shadow.
    if ( area.intersects(buf->rect) )
    {

        cairo_save(buf->ct);
        cairo_translate(buf->ct, -buf->rect.left(), -buf->rect.top());
 
        // Get canvas rotation (scale is isotropic).
        double rotation = atan2( _affine[1], _affine[0] );

        // Are we axis aligned?
        double mod_rot = fmod(rotation * M_2_PI, 1);
        bool axis_aligned = Geom::are_near( mod_rot, 0 ) || Geom::are_near( mod_rot, 1.0 );

        // Get the points we need transformed to window coordinates.
        Geom::Point rect_transformed[4];
        for (unsigned i = 0; i < 4; ++i) {
            rect_transformed[i] = _rect.corner(i) * _affine;
        }

        if(_inverted) {
            cairo_set_operator(buf->ct, CAIRO_OPERATOR_DIFFERENCE);
        }

        // Draw shadow first. Shadow extends under rectangle to reduce aliasing effects.
        if (_shadow_width > 0 && !_dashed) {
            Geom::Point const * corners = rect_transformed;
            double shadowydir = _affine.det() > 0 ? -1 : 1;

            // is the desktop y-axis downwards?
            if (SP_ACTIVE_DESKTOP && SP_ACTIVE_DESKTOP->is_yaxisdown()) {
                ++corners; // need corners 1/2/3 instead of 0/1/2
                shadowydir *= -1;
            }

            // Offset by half stroke width (_shadow_width is in window coordinates).
            // Need to handle change in handedness with flips.
            Geom::Point shadow( _shadow_width/2.0, shadowydir * _shadow_width/2.0 );
            shadow *= Geom::Rotate( rotation );

            if (axis_aligned) {
                // Snap to pixel grid (add 0.5 to center on pixel).
                cairo_move_to( buf->ct,
                               floor(corners[0][X] + shadow[X]+0.5) + 0.5,
                               floor(corners[0][Y] + shadow[Y]+0.5) + 0.5 );
                cairo_line_to( buf->ct,
                               floor(corners[1][X] + shadow[X]+0.5) + 0.5,
                               floor(corners[1][Y] + shadow[Y]+0.5) + 0.5 );
                cairo_line_to( buf->ct,
                               floor(corners[2][X] + shadow[X]+0.5) + 0.5,
                               floor(corners[2][Y] + shadow[Y]+0.5) + 0.5 );
            } else {
                cairo_move_to( buf->ct,
                               corners[0][X] + shadow[X],
                               corners[0][Y] + shadow[Y] );
                cairo_line_to( buf->ct,
                               corners[1][X] + shadow[X],
                               corners[1][Y] + shadow[Y] );
                cairo_line_to( buf->ct,
                               corners[2][X] + shadow[X],
                               corners[2][Y] + shadow[Y] );
            }               

            ink_cairo_set_source_rgba32( buf->ct, _shadow_color );
            cairo_set_line_width( buf->ct, _shadow_width + 1 );
            cairo_stroke( buf->ct );
        }


        // Setup rectangle path
        if (axis_aligned) {

            // Snap to pixel grid
            Geom::Rect outline( _rect.min() * _affine, _rect.max() * _affine);
            cairo_rectangle (buf->ct,
                             floor(outline.min()[X])+0.5,
                             floor(outline.min()[Y])+0.5,
                             floor(outline.max()[X]) - floor(outline.min()[X]),
                             floor(outline.max()[Y]) - floor(outline.min()[Y]));
        } else {

            // Angled
            cairo_move_to( buf->ct, rect_transformed[0][X], rect_transformed[0][Y] );
            cairo_line_to( buf->ct, rect_transformed[1][X], rect_transformed[1][Y] );
            cairo_line_to( buf->ct, rect_transformed[2][X], rect_transformed[2][Y] );
            cairo_line_to( buf->ct, rect_transformed[3][X], rect_transformed[3][Y] );
            cairo_close_path( buf->ct );
        }

        // This doesn't seem to be used anywhere. If it is, then we should
        // probably rotate the coordinate system and fill using a cairo_rectangle().
        if (_checkerboard) {
            cairo_pattern_t *cb = ink_cairo_pattern_create_checkerboard();
            cairo_set_source(buf->ct, cb);
            cairo_pattern_destroy(cb);
            cairo_fill_preserve(buf->ct);
        }

        if (_has_fill) {
            ink_cairo_set_source_rgba32(buf->ct, _fill_color);
            cairo_fill_preserve(buf->ct);
        }

        // Set up stroke.
        ink_cairo_set_source_rgba32(buf->ct, _border_color);
        cairo_set_line_width(buf->ct, 1);
        static double const dashes[2] = {4.0, 4.0};
        if (_dashed) cairo_set_dash(buf->ct, dashes, 2, 0);

        // Stroke rectangle.
        cairo_stroke_preserve(buf->ct);

        // Highlight the border by drawing it in _shadow_color.
        if (_shadow_width == 1 && _dashed) {
            ink_cairo_set_source_rgba32(buf->ct, _shadow_color);
            cairo_set_dash(buf->ct, dashes, 2, 4); // Dash offset by dash length.
            cairo_stroke_preserve(buf->ct);
        }

        cairo_new_path(buf->ct); // Clear path or get weird artifacts.
        cairo_restore(buf->ct);
    }
}


void CtrlRect::update(Geom::Affine const &affine, unsigned int flags)
{
    using Geom::X;
    using Geom::Y;

    if ((SP_CANVAS_ITEM_CLASS(sp_ctrlrect_parent_class))->update) {
        (SP_CANVAS_ITEM_CLASS(sp_ctrlrect_parent_class))->update(this, affine, flags);
    }

    // Note: There is no harm if 'area' is too large other than a possible small slow down in
    // rendering speed.

    // Calculate an axis-aligned bounding box that include all of transformed _rect.
    Geom::Rect bbox = _rect;
    bbox *= affine;

    // Enlarge bbox by twice shadow size (to allow for shadow on any side with a 45deg rotation).
    bbox.expandBy( 2.0 *_shadow_width );

    // Generate an integer rectangle that includes bbox.
    Geom::OptIntRect _area_old = _area;
    _area = bbox.roundOutwards();

    // std::cout << "  _rect: " << _rect << std::endl;
    // std::cout << "   bbox: " <<  bbox << std::endl;
    // std::cout << "   area: " << *_area << std::endl;

    if (_area) {
        Geom::IntRect area = *_area;
        // Windows glitches sometimes in cairo, possibly due to the way the surface is cleared.
        // Increasing '_area' won't work as the box must be drawn 'inside' the updated area.
        sp_canvas_update_bbox(this, area.left(), area.top(), area.right() + 1, area.bottom() + 1);

    } else {
        std::cerr << "CtrlRect::update: No area!!" << std::endl;
    }

    // At rendering stage, we need to know affine:
    _affine = affine;
}


void CtrlRect::setColor(guint32 b, bool h, guint f)
{
    _border_color = b;
    _has_fill = h;
    _fill_color = f;
    _requestUpdate();
}

void CtrlRect::setShadow(int s, guint c)
{
    _shadow_width = s;
    _shadow_color = c;
    _requestUpdate();
}

void CtrlRect::setInvert(bool invert) {
    _inverted = invert;
    _requestUpdate();
}

void CtrlRect::setRectangle(Geom::Rect const &r)
{
    _rect = r;
    _requestUpdate();
}

void CtrlRect::setDashed(bool d)
{
    _dashed = d;
    _requestUpdate();
}

void CtrlRect::setCheckerboard(bool d)
{
    _checkerboard = d;
    _requestUpdate();
}

void CtrlRect::_requestUpdate()
{
    sp_canvas_item_request_update(SP_CANVAS_ITEM(this));
}

/*
  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 :