1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2019 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
23 * @short_description: Arrow rendering related stuff
25 * Contains parameters on how to draw arrows, providing a way to register a
26 * custom rendering callback.
29 * By default, the #AdgEntity:local-mix property is set to #ADG_MIX_PARENT
30 * on #AdgArrow entities.
39 * All fields are private and should not be used directly.
40 * Use its public methods instead.
46 #include "adg-internal.h"
47 #include "adg-model.h"
48 #include "adg-trail.h"
50 #include "adg-marker.h"
52 #include "adg-arrow.h"
53 #include "adg-arrow-private.h"
56 G_DEFINE_TYPE(AdgArrow
, adg_arrow
, ADG_TYPE_MARKER
)
64 static void _adg_get_property (GObject
*object
,
68 static void _adg_set_property (GObject
*object
,
72 static void _adg_arrange (AdgEntity
*entity
);
73 static void _adg_render (AdgEntity
*entity
,
75 static AdgModel
* _adg_create_model (AdgMarker
*marker
);
79 adg_arrow_class_init(AdgArrowClass
*klass
)
81 GObjectClass
*gobject_class
;
82 AdgEntityClass
*entity_class
;
83 AdgMarkerClass
*marker_class
;
86 gobject_class
= (GObjectClass
*) klass
;
87 entity_class
= (AdgEntityClass
*) klass
;
88 marker_class
= (AdgMarkerClass
*) klass
;
90 g_type_class_add_private(klass
, sizeof(AdgArrowPrivate
));
92 gobject_class
->set_property
= _adg_set_property
;
93 gobject_class
->get_property
= _adg_get_property
;
95 entity_class
->arrange
= _adg_arrange
;
96 entity_class
->render
= _adg_render
;
98 marker_class
->create_model
= _adg_create_model
;
100 param
= g_param_spec_double("angle",
102 P_("The opening angle of the arrow"),
103 -G_PI
, G_PI
, G_PI
/ 6,
105 g_object_class_install_property(gobject_class
, PROP_ANGLE
, param
);
109 adg_arrow_init(AdgArrow
*arrow
)
111 AdgArrowPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(arrow
,
115 data
->angle
= G_PI
/6;
119 adg_entity_set_local_mix((AdgEntity
*) arrow
, ADG_MIX_PARENT
);
123 _adg_get_property(GObject
*object
, guint prop_id
,
124 GValue
*value
, GParamSpec
*pspec
)
126 AdgArrowPrivate
*data
= ((AdgArrow
*) object
)->data
;
130 g_value_set_double(value
, data
->angle
);
133 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
139 _adg_set_property(GObject
*object
, guint prop_id
,
140 const GValue
*value
, GParamSpec
*pspec
)
142 AdgArrowPrivate
*data
= ((AdgArrow
*) object
)->data
;
146 data
->angle
= cpml_angle(g_value_get_double(value
));
149 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
158 * Creates a new undefined arrow entity. The position must be defined
159 * by setting the #AdgMarker:trail and #AdgMarker:pos properties.
161 * Returns: the newly created arrow entity
168 return g_object_new(ADG_TYPE_ARROW
, NULL
);
172 * adg_arrow_new_with_trail:
173 * @trail: the #AdgTrail where the arrow should be added
174 * @pos: the position ratio on @trail
176 * Creates a new arrow on the first segment on @trail at position
177 * @pos, where @pos is a ratio of the @trail length (being 0 the
178 * start point, 1 the end point, 0.5 the middle point and so on).
179 * By default, an arrow as #AdgEntity:local-mix set to #ADG_MIX_PARENT.
181 * Returns: the newly created arrow entity
186 adg_arrow_new_with_trail(AdgTrail
*trail
, gdouble pos
)
188 return g_object_new(ADG_TYPE_ARROW
,
189 "local-mix", ADG_MIX_PARENT
,
197 * adg_arrow_set_angle:
198 * @arrow: an #AdgArrow
199 * @angle: the new angle
201 * Sets a new angle: @angle will be the new opening angle of @arrow.
202 * Changing the arrow angle will invalidate @arrow.
207 adg_arrow_set_angle(AdgArrow
*arrow
, gdouble angle
)
209 g_return_if_fail(ADG_IS_ARROW(arrow
));
210 g_object_set(arrow
, "angle", angle
, NULL
);
214 * adg_arrow_get_angle:
215 * @arrow: an #AdgArrow
217 * Gets the current angle of @arrow.
219 * Returns: the arrow angle, in radians
224 adg_arrow_get_angle(AdgArrow
*arrow
)
226 AdgArrowPrivate
*data
;
228 g_return_val_if_fail(ADG_IS_ARROW(arrow
), 0);
237 _adg_arrange(AdgEntity
*entity
)
240 const CpmlExtents
*extents
;
241 CpmlExtents new_extents
;
243 model
= adg_marker_model((AdgMarker
*) entity
);
247 extents
= adg_trail_get_extents((AdgTrail
*) model
);
251 cpml_extents_copy(&new_extents
, extents
);
252 cpml_extents_transform(&new_extents
, adg_entity_get_local_matrix(entity
));
253 adg_entity_set_extents(entity
, &new_extents
);
257 _adg_render(AdgEntity
*entity
, cairo_t
*cr
)
260 const cairo_path_t
*cairo_path
;
262 model
= adg_marker_model((AdgMarker
*) entity
);
266 cairo_path
= adg_trail_get_cairo_path((AdgTrail
*) model
);
268 if (cairo_path
!= NULL
) {
270 cairo_transform(cr
, adg_entity_get_global_matrix(entity
));
271 cairo_transform(cr
, adg_entity_get_local_matrix(entity
));
272 cairo_append_path(cr
, cairo_path
);
280 _adg_create_model(AdgMarker
*marker
)
282 AdgArrowPrivate
*data
;
286 data
= ((AdgArrow
*) marker
)->data
;
287 path
= adg_path_new();
288 cpml_vector_from_angle(&p1
, data
->angle
/ 2);
292 adg_path_move_to_explicit(path
, 0, 0);
293 adg_path_line_to(path
, &p1
);
294 adg_path_line_to(path
, &p2
);
295 adg_path_close(path
);
297 return (AdgModel
*) path
;