Code

Packaging. New Brazilian Portuguese NSH file. Set trunk package date to 2011.
[inkscape.git] / src / live_effects / lpe-perspective_path.cpp
index eb05b5702f33dc4af4f0fad91419000ae095469e..58efe4ef51f7822b3229a667f30a6ef5e01905e2 100644 (file)
@@ -1,22 +1,11 @@
-#define INKSCAPE_LPE_PERSPECTIVE_PATH_CPP
-/** \file
- * LPE <perspective_path> implementation, used as an example for a base starting class
- * when implementing new LivePathEffects.
- *
- * In vi, three global search-and-replaces will let you rename everything
- * in this and the .h file:
- *
- *   :%s/PERSPECTIVE_PATH/YOURNAME/g
- *   :%s/PerspectivePath/Yourname/g
- *   :%s/perspective_path/yourname/g
+/** @file
+ * @brief LPE perspective path effect implementation.
  */
-/*
- * Authors:
- *   Johan Engelen
- *   Maximilian Albert
+/* Authors:
+ *   Maximilian Albert <maximilian.albert@gmail.com>
+ *   Johan Engelen <j.b.c.engelen@utwente.nl>
  *
- * Copyright (C) Johan Engelen 2007 <j.b.c.engelen@utwente.nl>
- * Copyright (C) Maximilian Albert 2008 <maximilian.albert@gmail.com>
+ * Copyright (C) 2007-2008 Authors
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 #include "document.h"
 
 #include "live_effects/lpe-perspective_path.h"
-#include "display/curve.h"
-#include <libnr/n-art-bpath.h>
 #include "sp-item-group.h"
+#include "knot-holder-entity.h"
 
 #include "inkscape.h"
 
-// You might need to include other 2geom files. You can add them here:
 #include <2geom/path.h>
 
 namespace Inkscape {
 namespace LivePathEffect {
 
+namespace PP {
+
+class KnotHolderEntityOffset : public LPEKnotHolderEntity
+{
+public:
+    virtual void knot_set(Geom::Point const &p, Geom::Point const &origin, guint state);
+    virtual Geom::Point knot_get();
+};
+
+} // namespace PP
+
 LPEPerspectivePath::LPEPerspectivePath(LivePathEffectObject *lpeobject) :
     Effect(lpeobject),
     // initialise your parameters here:
@@ -54,13 +52,15 @@ LPEPerspectivePath::LPEPerspectivePath(LivePathEffectObject *lpeobject) :
     registerParameter( dynamic_cast<Parameter *>(&offsety) );
     registerParameter( dynamic_cast<Parameter *>(&uses_plane_xy) );
 
+    registerKnotHolderHandle(new PP::KnotHolderEntityOffset(), _("Adjust the origin"));
+
+    concatenate_before_pwd2 = true; // don't split the path into its subpaths
+
     Persp3D *persp = persp3d_document_first_persp(inkscape_active_document());
 
-    Proj::TransfMat3x4 pmat = persp->tmat;
+    Proj::TransfMat3x4 pmat = persp->perspective_impl->tmat;
 
     pmat.copy_tmat(tmat);
-    
-    groupSpecialBehavior = false;
 }
 
 LPEPerspectivePath::~LPEPerspectivePath()
@@ -71,14 +71,9 @@ LPEPerspectivePath::~LPEPerspectivePath()
 void
 LPEPerspectivePath::doBeforeEffect (SPLPEItem *lpeitem)
 {
-    if(SP_IS_GROUP(lpeitem))
-    {
-        groupSpecialBehavior = true;
-        original_bbox(lpeitem);
-    }    
+    original_bbox(lpeitem, true);
 }
 
-
 Geom::Piecewise<Geom::D2<Geom::SBasis> >
 LPEPerspectivePath::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > const & pwd2_in)
 {
@@ -90,22 +85,16 @@ LPEPerspectivePath::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > cons
     //        remove this once we have unified coordinate systems
     path_a_pw = path_a_pw + Geom::Point(offsetx, -offsety);
 
-    D2<Piecewise<SBasis> > B = make_cuts_independant(path_a_pw);
+    D2<Piecewise<SBasis> > B = make_cuts_independent(path_a_pw);
     Piecewise<SBasis> preimage[4];
 
-    if(!groupSpecialBehavior)
-    {
-        boundingbox_X = bounds_fast(pwd2_in)[0];
-        boundingbox_Y = bounds_fast(pwd2_in)[1];
-    }
-
-    Geom::Point orig = Geom::Point(uses_plane_xy ? boundingbox_X.max() : boundingbox_X.min(),
-                                   boundingbox_Y.middle());
     //Geom::Point orig = Geom::Point(bounds_X.min(), bounds_Y.middle());
     //orig = Geom::Point(orig[X], sp_document_height(inkscape_active_document()) - orig[Y]);
 
     //double offset = uses_plane_xy ? boundingbox_X.extent() : 0.0;
 
+    orig = Point(uses_plane_xy ? boundingbox_X.max() : boundingbox_X.min(), boundingbox_Y.middle());
+
     /**
     g_print ("Orig: (%8.2f, %8.2f)\n", orig[X], orig[Y]);
 
@@ -140,7 +129,7 @@ LPEPerspectivePath::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > cons
             + preimage[2] * tmat[j][2]
             +               tmat[j][3];
     }
-    D2<Piecewise<SBasis> > result(divide(res[0],res[2], 3), 
+    D2<Piecewise<SBasis> > result(divide(res[0],res[2], 3),
                                   divide(res[1],res[2], 3));
 
     Piecewise<D2<SBasis> > output = sectionize(result);
@@ -148,7 +137,44 @@ LPEPerspectivePath::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > cons
     return output;
 }
 
-/* ######################## */
+namespace PP {
+
+// TODO: make this more generic
+static LPEPerspectivePath *
+get_effect(SPItem *item)
+{
+    Effect *effect = sp_lpe_item_get_current_lpe(SP_LPE_ITEM(item));
+    if (effect->effectType() != PERSPECTIVE_PATH) {
+        g_print ("Warning: Effect is not of type LPEPerspectivePath!\n");
+        return NULL;
+    }
+    return static_cast<LPEPerspectivePath *>(effect);
+}
+
+void
+KnotHolderEntityOffset::knot_set(Geom::Point const &p, Geom::Point const &origin, guint /*state*/)
+{
+    using namespace Geom;
+    LPEPerspectivePath* lpe = get_effect(item);
+
+    Geom::Point const s = snap_knot_position(p);
+
+    lpe->offsetx.param_set_value((s - origin)[Geom::X]);
+    lpe->offsety.param_set_value(-(s - origin)[Geom::Y]); // additional minus sign is due to coordinate system flipping
+
+    // FIXME: this should not directly ask for updating the item. It should write to SVG, which triggers updating.
+    sp_lpe_item_update_patheffect (SP_LPE_ITEM(item), false, true);
+}
+
+Geom::Point
+KnotHolderEntityOffset::knot_get()
+{
+    LPEPerspectivePath* lpe = get_effect(item);
+    return lpe->orig + Geom::Point(lpe->offsetx, -lpe->offsety);
+}
+
+} // namespace PP
 
 } //namespace LivePathEffect
 } /* namespace Inkscape */
@@ -162,4 +188,4 @@ LPEPerspectivePath::doEffect_pwd2 (Geom::Piecewise<Geom::D2<Geom::SBasis> > cons
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :