vector.cpp revision 47badd0035ae8c9135c51444f3770b17ae504dda
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz#define INKSCAPE_LIVEPATHEFFECT_PARAMETER_VECTOR_CPP
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz/*
36d9047104b5881ca587a71ade77b1f21d083bc4Jon A. Cruz * Copyright (C) Johan Engelen 2008 <j.b.c.engelen@utwente.nl>
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz *
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz * Released under GNU GPL, read the file 'COPYING' for more information
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz */
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz#include "live_effects/parameter/vector.h"
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz#include "sp-lpe-item.h"
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz#include "knotholder.h"
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz#include "svg/svg.h"
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz#include "svg/stringstream.h"
ce476a1fb346f635574c285a4815b8de8a6cdb68Jon A. Cruz
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz#include "ui/widget/registered-widget.h"
0dc33d4ce43e0bb49c63aa53b826ec4a1ff68e28Jon A. Cruz#include "live_effects/effect.h"
ce476a1fb346f635574c285a4815b8de8a6cdb68Jon A. Cruz#include "desktop.h"
0dc33d4ce43e0bb49c63aa53b826ec4a1ff68e28Jon A. Cruz#include "verbs.h"
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruznamespace Inkscape {
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruznamespace LivePathEffect {
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz
36d9047104b5881ca587a71ade77b1f21d083bc4Jon A. CruzVectorParam::VectorParam( const Glib::ustring& label, const Glib::ustring& tip,
36d9047104b5881ca587a71ade77b1f21d083bc4Jon A. Cruz const Glib::ustring& key, Inkscape::UI::Widget::Registry* wr,
36d9047104b5881ca587a71ade77b1f21d083bc4Jon A. Cruz Effect* effect, Geom::Point default_vector)
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz : Parameter(label, tip, key, wr, effect),
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz defvalue(default_vector),
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz origin(0.,0.),
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz vector(default_vector)
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz{
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz vec_knot_shape = SP_KNOT_SHAPE_DIAMOND;
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz vec_knot_mode = SP_KNOT_MODE_XOR;
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz vec_knot_color = 0xffffb500;
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz ori_knot_shape = SP_KNOT_SHAPE_CIRCLE;
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz ori_knot_mode = SP_KNOT_MODE_XOR;
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz ori_knot_color = 0xffffb500;
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz}
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruzVectorParam::~VectorParam()
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz{
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz}
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruzvoid
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruzVectorParam::param_set_default()
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz{
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz setOrigin(Geom::Point(0.,0.));
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz setVector(defvalue);
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz}
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruzbool
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruzVectorParam::param_readSVGValue(const gchar * strvalue)
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz{
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz gchar ** strarray = g_strsplit(strvalue, ",", 4);
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz if (!strarray) {
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz return false;
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz }
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz double val[4];
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz unsigned int i = 0;
fac0962a18d1fb6d73726dbbe94d3204459484c7joncruz while (i < 4 && strarray[i]) {
if (sp_svg_number_read_d(strarray[i], &val[i]) != 0) {
i++;
} else {
break;
}
}
g_strfreev (strarray);
if (i == 4) {
setOrigin( Geom::Point(val[0], val[1]) );
setVector( Geom::Point(val[2], val[3]) );
return true;
}
return false;
}
gchar *
VectorParam::param_getSVGValue() const
{
Inkscape::SVGOStringStream os;
os << origin << " , " << vector;
gchar * str = g_strdup(os.str().c_str());
return str;
}
Gtk::Widget *
VectorParam::param_newWidget()
{
Inkscape::UI::Widget::RegisteredVector * pointwdg = Gtk::manage(
new Inkscape::UI::Widget::RegisteredVector( param_label,
param_tooltip,
param_key,
*param_wr,
param_effect->getRepr(),
param_effect->getSPDoc() ) );
pointwdg->setPolarCoords();
pointwdg->setValue( vector, origin );
pointwdg->clearProgrammatically();
pointwdg->set_undo_parameters(SP_VERB_DIALOG_LIVE_PATH_EFFECT, _("Change vector parameter"));
Gtk::HBox * hbox = Gtk::manage( new Gtk::HBox() );
static_cast<Gtk::HBox*>(hbox)->pack_start(*pointwdg, true, true);
static_cast<Gtk::HBox*>(hbox)->show_all_children();
return dynamic_cast<Gtk::Widget *> (hbox);
}
void
VectorParam::set_and_write_new_values(Geom::Point const &new_origin, Geom::Point const &new_vector)
{
setValues(new_origin, new_vector);
gchar * str = param_getSVGValue();
param_write_to_repr(str);
g_free(str);
}
void
VectorParam::param_transform_multiply(Geom::Affine const& postmul, bool /*set*/)
{
set_and_write_new_values( origin * postmul, vector * postmul.withoutTranslation() );
}
void
VectorParam::set_vector_oncanvas_looks(SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
{
vec_knot_shape = shape;
vec_knot_mode = mode;
vec_knot_color = color;
}
void
VectorParam::set_origin_oncanvas_looks(SPKnotShapeType shape, SPKnotModeType mode, guint32 color)
{
ori_knot_shape = shape;
ori_knot_mode = mode;
ori_knot_color = color;
}
class VectorParamKnotHolderEntity_Origin : public LPEKnotHolderEntity {
public:
VectorParamKnotHolderEntity_Origin(VectorParam *p) : param(p) { }
virtual ~VectorParamKnotHolderEntity_Origin() {}
virtual void knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/) {
Geom::Point const s = snap_knot_position(p);
param->setOrigin(s);
sp_lpe_item_update_patheffect(SP_LPE_ITEM(item), false, false);
};
virtual Geom::Point knot_get(){
return param->origin;
};
virtual void knot_click(guint /*state*/){
g_print ("This is the origin handle associated to parameter '%s'\n", param->param_key.c_str());
};
private:
VectorParam *param;
};
class VectorParamKnotHolderEntity_Vector : public LPEKnotHolderEntity {
public:
VectorParamKnotHolderEntity_Vector(VectorParam *p) : param(p) { }
virtual ~VectorParamKnotHolderEntity_Vector() {}
virtual void knot_set(Geom::Point const &p, Geom::Point const &/*origin*/, guint /*state*/) {
Geom::Point const s = p - param->origin;
/// @todo implement angle snapping when holding CTRL
param->setVector(s);
sp_lpe_item_update_patheffect(SP_LPE_ITEM(item), false, false);
};
virtual Geom::Point knot_get(){
return param->origin + param->vector;
};
virtual void knot_click(guint /*state*/){
g_print ("This is the vector handle associated to parameter '%s'\n", param->param_key.c_str());
};
private:
VectorParam *param;
};
void
VectorParam::addKnotHolderEntities(KnotHolder *knotholder, SPDesktop *desktop, SPItem *item)
{
VectorParamKnotHolderEntity_Origin *origin_e = new VectorParamKnotHolderEntity_Origin(this);
origin_e->create(desktop, item, knotholder, handleTip(), ori_knot_shape, ori_knot_mode, ori_knot_color);
knotholder->add(origin_e);
VectorParamKnotHolderEntity_Vector *vector_e = new VectorParamKnotHolderEntity_Vector(this);
vector_e->create(desktop, item, knotholder, handleTip(), vec_knot_shape, vec_knot_mode, vec_knot_color);
knotholder->add(vector_e);
}
} /* namespace LivePathEffect */
} /* namespace Inkscape */
/*
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 :