Code

Store cached icons to disk between runs, and invalidate/purge as needed.
[inkscape.git] / src / box3d-side.cpp
index 741bf97b40c61f58321bfa6cfe6ee0589bd9a35f..72a479ca4ab0957ead622c60404acbdfd15e131b 100644 (file)
@@ -1,10 +1,9 @@
-#define __BOX3D_SIDE_C__
-
 /*
  * 3D box face implementation
  *
  * Authors:
  *   Maximilian Albert <Anhalter42@gmx.de>
+ *   Abhishek Sharma
  *
  * Copyright (C) 2007  Authors
  *
@@ -21,7 +20,7 @@
 #include "inkscape.h"
 #include "persp3d.h"
 #include "box3d-context.h"
-#include "prefs-utils.h"
+#include "preferences.h"
 #include "desktop-style.h"
 #include "box3d.h"
 
@@ -29,20 +28,13 @@ static void box3d_side_class_init (Box3DSideClass *klass);
 static void box3d_side_init (Box3DSide *side);
 
 static void box3d_side_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
-static Inkscape::XML::Node *box3d_side_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *box3d_side_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 static void box3d_side_set (SPObject *object, unsigned int key, const gchar *value);
 static void box3d_side_update (SPObject *object, SPCtx *ctx, guint flags);
 
-//static gchar * box3d_side_description (SPItem * item);
-//static void box3d_side_snappoints(SPItem const *item, SnapPointsIter p);
-
-//static void box3d_side_set_shape (SPShape *shape);
-//static void box3d_side_update_patheffect (SPShape *shape, bool write);
+static void box3d_side_set_shape (SPShape *shape);
 
-static void box3d_side_apply_style (Box3DSide *side);
-static Proj::Pt3 box3d_side_corner (Box3DSide *side, guint index);
-static std::vector<Proj::Pt3> box3d_side_corners (Box3DSide *side);
-// static gint box3d_side_descr_to_id (gchar const *descr);
+static void box3d_side_compute_corner_ids(Box3DSide *side, unsigned int corners[4]);
 
 static SPShapeClass *parent_class;
 
@@ -89,11 +81,7 @@ box3d_side_class_init (Box3DSideClass *klass)
     sp_object_class->set = box3d_side_set;
     sp_object_class->update = box3d_side_update;
 
-    //item_class->description = box3d_side_description;
-    //item_class->snappoints = box3d_side_snappoints;
-
     shape_class->set_shape = box3d_side_set_shape;
-    //shape_class->update_patheffect = box3d_side_update_patheffect;
 }
 
 static void
@@ -104,51 +92,44 @@ box3d_side_init (Box3DSide * side)
     side->front_or_rear = Box3D::FRONT;
 }
 
-static void
-box3d_side_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
+static void box3d_side_build(SPObject * object, SPDocument * document, Inkscape::XML::Node * repr)
 {
-    if (((SPObjectClass *) parent_class)->build)
-        ((SPObjectClass *) parent_class)->build (object, document, repr);
+    if (((SPObjectClass *) parent_class)->build) {
+        ((SPObjectClass *) parent_class)->build(object, document, repr);
+    }
 
-    sp_object_read_attr (object, "inkscape:box3dsidetype");
+    object->readAttr( "inkscape:box3dsidetype" );
 }
 
 static Inkscape::XML::Node *
-box3d_side_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
+box3d_side_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     Box3DSide *side = SP_BOX3D_SIDE (object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        g_print ("Do we ever end up here?\n");
-        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
+        // this is where we end up when saving as plain SVG (also in other circumstances?)
+        // thus we don' set "sodipodi:type" so that the box is only saved as an ordinary svg:path
         repr = xml_doc->createElement("svg:path");
-        repr->setAttribute("sodipodi:type", "inkscape:box3dside"); // FIXME: Does this double the 
     }
 
     if (flags & SP_OBJECT_WRITE_EXT) {
         sp_repr_set_int(repr, "inkscape:box3dsidetype", side->dir1 ^ side->dir2 ^ side->front_or_rear);
     }
 
-    sp_shape_set_shape ((SPShape *) object); // FIXME: necessary? YES!
+    static_cast<SPShape *>(object)->setShape();
 
     /* Duplicate the path */
-    SPCurve *curve = ((SPShape *) object)->curve;
+    SPCurve const *curve = ((SPShape *) object)->curve;
     //Nulls might be possible if this called iteratively
     if ( !curve ) {
         return NULL;
     }
-    NArtBpath *bpath = SP_CURVE_BPATH(curve);
-    if ( !bpath ) {
-        return NULL;
-    }
-    char *d = sp_svg_write_path ( bpath );
+    char *d = sp_svg_write_path ( curve->get_pathvector() );
     repr->setAttribute("d", d);
     g_free (d);
 
-    box3d_side_apply_style (side);
-
     if (((SPObjectClass *) (parent_class))->write)
-        ((SPObjectClass *) (parent_class))->write (object, repr, flags);
+        ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
 
     return repr;
 }
@@ -190,71 +171,39 @@ box3d_side_set (SPObject *object, unsigned int key, const gchar *value)
 static void
 box3d_side_update (SPObject *object, SPCtx *ctx, guint flags)
 {
-    //g_print ("box3d_side_update\n");
+    if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
+        flags &= ~SP_OBJECT_USER_MODIFIED_FLAG_B; // since we change the description, it's not a "just translation" anymore
+    }
+
     if (flags & (SP_OBJECT_MODIFIED_FLAG |
-                 //SP_OBJECT_CHILD_MODIFIED_FLAG |
                  SP_OBJECT_STYLE_MODIFIED_FLAG |
                  SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
-        /***
-        g_print ("\n\nIn box3d_side_update: ");
-        if (flags & SP_OBJECT_MODIFIED_FLAG) g_print ("SP_OBJECT_MODIFIED_FLAG ");
-        if (flags & SP_OBJECT_CHILD_MODIFIED_FLAG) g_print ("SP_OBJECT_CHILD_MODIFIED_FLAG ");
-        if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) g_print ("SP_OBJECT_STYLE_MODIFIED_FLAG ");
-        if (flags & SP_OBJECT_VIEWPORT_MODIFIED_FLAG) g_print ("SP_OBJECT_VIEWPORT_MODIFIED_FLAG ");
-        g_print ("\n");
-        ***/
-        sp_shape_set_shape ((SPShape *) object);
+        static_cast<SPShape *>(object)->setShape ();
     }
 
     if (((SPObjectClass *) parent_class)->update)
         ((SPObjectClass *) parent_class)->update (object, ctx, flags);
 }
 
-/***
-static void
-box3d_side_update_patheffect(SPShape *shape, bool write)
+/* Create a new Box3DSide and append it to the parent box */
+Box3DSide * Box3DSide::createBox3DSide(SPBox3D *box)
 {
-    box3d_side_set_shape(shape);
-
-    if (write) {
-        Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
-        if ( shape->curve != NULL ) {
-            NArtBpath *abp = sp_curve_first_bpath(shape->curve);
-            if (abp) {
-                gchar *str = sp_svg_write_path(abp);
-                repr->setAttribute("d", str);
-                g_free(str);
-            } else {
-                repr->setAttribute("d", "");
-            }
-        } else {
-            repr->setAttribute("d", NULL);
-        }
-    }
-
-    ((SPObject *)shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
+       Box3DSide *box3d_side = 0;
+       Inkscape::XML::Document *xml_doc = box->document->rdoc;
+       Inkscape::XML::Node *repr_side = xml_doc->createElement("svg:path");
+       repr_side->setAttribute("sodipodi:type", "inkscape:box3dside");
+       box3d_side = static_cast<Box3DSide *>(box->appendChildRepr(repr_side));
+       return box3d_side;
 }
-***/
 
-/***
-static gchar *
-box3d_side_description (SPItem *item)
+/*
+ * Function which return the type attribute for Box3D. 
+ * Acts as a replacement for directly accessing the XML Tree directly.
+ */
+long long int Box3DSide::getFaceId()
 {
-    Box3DSide *side = SP_BOX3D_SIDE (item);
-
-    // while there will never be less than 3 vertices, we still need to
-    // make calls to ngettext because the pluralization may be different
-    // for various numbers >=3.  The singular form is used as the index.
-    if (side->flatsided == false )
-       return g_strdup_printf (ngettext("<b>Star</b> with %d vertex",
-                                        "<b>Star</b> with %d vertices",
-                                        star->sides), star->sides);
-    else
-        return g_strdup_printf (ngettext("<b>Polygon</b> with %d vertex",
-                                        "<b>Polygon</b> with %d vertices",
-                                        star->sides), star->sides);
+           return this->getIntAttribute("inkscape:box3dsidetype", -1);
 }
-***/
 
 void
 box3d_side_position_set (Box3DSide *side) {
@@ -267,7 +216,6 @@ box3d_side_position_set (Box3DSide *side) {
 void
 box3d_side_set_shape (SPShape *shape)
 {
-    //g_print ("box3d_side_set_shape\n");
     Box3DSide *side = SP_BOX3D_SIDE (shape);
     if (!SP_OBJECT_DOCUMENT(side)->root) {
         // avoid a warning caused by sp_document_height() (which is called from sp_item_i2d_affine() below)
@@ -275,78 +223,58 @@ box3d_side_set_shape (SPShape *shape)
         return;
     }
 
-    if (!SP_IS_BOX3D(SP_OBJECT(side)->parent)) {
+    SPObject *parent = SP_OBJECT(side)->parent;
+    if (!SP_IS_BOX3D(parent)) {
         g_warning ("Parent of 3D box side is not a 3D box.\n");
-        /**
-        g_print ("Removing the inkscape:box3dside attribute and returning from box3d_side_set_shape().\n");
-        SP_OBJECT_REPR (shape)->setAttribute("sodipodi:type", NULL);
-        SP_OBJECT_REPR (shape)->setAttribute("inkscape:box3dside", NULL);
-        **/
         return;
     }
+    SPBox3D *box = SP_BOX3D(parent);
 
     Persp3D *persp = box3d_side_perspective(side);
-    //g_return_if_fail (persp != NULL);
     if (!persp) {
-        //g_warning ("persp != NULL in box3d_side_set_shape failed!\n");
-        //persp = SP_OBJECT_DOCUMENT(side)->current_persp3d;
         return;
     }
 
-    SPCurve *c = sp_curve_new ();
     // TODO: Draw the correct quadrangle here
     //       To do this, determine the perspective of the box, the orientation of the side (e.g., XY-FRONT)
     //       compute the coordinates of the corners in P^3, project them onto the canvas, and draw the
     //       resulting path.
 
-    std::vector<Proj::Pt3> corners = box3d_side_corners (side);
-
-    NR::Matrix const i2d (sp_item_i2d_affine (SP_ITEM(shape)));
+    unsigned int corners[4];
+    box3d_side_compute_corner_ids(side, corners);
 
-    // FIXME: This can better be implemented by using box3d_get_corner
-    sp_curve_moveto (c, persp->tmat.image(corners[0]).affine() * i2d);
-    sp_curve_lineto (c, persp->tmat.image(corners[1]).affine() * i2d);
-    sp_curve_lineto (c, persp->tmat.image(corners[2]).affine() * i2d);
-    sp_curve_lineto (c, persp->tmat.image(corners[3]).affine() * i2d);
+    SPCurve *c = new SPCurve();
 
-    sp_curve_closepath (c);
-    //sp_shape_perform_path_effect(c, SP_SHAPE (side));
-    sp_shape_set_curve_insync (SP_SHAPE (side), c, TRUE);
-    sp_curve_unref (c);
-}
-
-static void
-//box3d_side_apply_style (SPBox3D *box, bool extruded) {
-box3d_side_apply_style (Box3DSide *side) {
-    Inkscape::XML::Node *repr_face = SP_OBJECT_REPR(SP_OBJECT(side));
-
-    /**
-    if (!extruded && !strcmp (box3d_side_axes_string (), "XYrear")) {
-        // to avoid "flashing" during the initial dragging process, we make the rear face invisible in this case
-        repr_face->setAttribute("style", "fill:none");
+    if (!box3d_get_corner_screen(box, corners[0]).isFinite() ||
+        !box3d_get_corner_screen(box, corners[1]).isFinite() ||
+        !box3d_get_corner_screen(box, corners[2]).isFinite() ||
+        !box3d_get_corner_screen(box, corners[3]).isFinite() )
+    {
+        g_warning ("Trying to draw a 3D box side with invalid coordinates.\n");
         return;
     }
-    **/
-
-    gchar *descr = g_strconcat ("desktop.", box3d_side_axes_string (side), NULL);
-    const gchar * cur_style = prefs_get_string_attribute(descr, "style");
-    g_free (descr);    
-    
-    SPDesktop *desktop = inkscape_active_desktop();
-    bool use_current = prefs_get_int_attribute("tools.shapes.3dbox", "usecurrent", 0);
-    if (use_current && cur_style !=NULL) {
-        /* use last used style */
-        repr_face->setAttribute("style", cur_style);
-    } else {
-        /* use default style */
-        GString *pstring = g_string_new("");
-        g_string_printf (pstring, "tools.shapes.3dbox.%s", box3d_side_axes_string(side));
-        sp_desktop_apply_style_tool (desktop, repr_face, pstring->str, false);
+
+    c->moveto(box3d_get_corner_screen(box, corners[0]));
+    c->lineto(box3d_get_corner_screen(box, corners[1]));
+    c->lineto(box3d_get_corner_screen(box, corners[2]));
+    c->lineto(box3d_get_corner_screen(box, corners[3]));
+    c->closepath();
+
+    /* Reset the shape'scurve to the "original_curve"
+     * This is very important for LPEs to work properly! (the bbox might be recalculated depending on the curve in shape)*/
+    shape->setCurveInsync( c, TRUE);
+    if (sp_lpe_item_has_path_effect(SP_LPE_ITEM(shape)) && sp_lpe_item_path_effects_enabled(SP_LPE_ITEM(shape))) {
+        SPCurve *c_lpe = c->copy();
+        bool success = sp_lpe_item_perform_path_effect(SP_LPE_ITEM (shape), c_lpe);
+        if (success) {
+            shape->setCurveInsync( c_lpe, TRUE);
+        }
+        c_lpe->unref();
     }
+    c->unref();
 }
 
-gchar *
-box3d_side_axes_string(Box3DSide *side)
+gchar *box3d_side_axes_string(Box3DSide *side)
 {
     GString *pstring = g_string_new("");
     g_string_printf (pstring, "%s", Box3D::string_from_axes ((Box3D::Axis) (side->dir1 ^ side->dir2)));
@@ -366,53 +294,33 @@ box3d_side_axes_string(Box3DSide *side)
     return pstring->str;
 }
 
-static Proj::Pt3
-box3d_side_corner (Box3DSide *side, guint index) {
-    SPBox3D *box = SP_BOX3D(SP_OBJECT_PARENT(side));
-    return Proj::Pt3 ((index & 0x1) ? box->orig_corner7[Proj::X] : box->orig_corner0[Proj::X],
-                      (index & 0x2) ? box->orig_corner7[Proj::Y] : box->orig_corner0[Proj::Y],
-                      (index & 0x4) ? box->orig_corner7[Proj::Z] : box->orig_corner0[Proj::Z],
-                      1.0);
-}
-
-static std::vector<Proj::Pt3>
-box3d_side_corners (Box3DSide *side) {
-    std::vector<Proj::Pt3> corners;
+static void
+box3d_side_compute_corner_ids(Box3DSide *side, unsigned int corners[4]) {
     Box3D::Axis orth = Box3D::third_axis_direction (side->dir1, side->dir2);
-    unsigned int i0 = (side->front_or_rear ? orth : 0);
-    unsigned int i1 = i0 ^ side->dir1;
-    unsigned int i2 = i0 ^ side->dir1 ^ side->dir2;
-    unsigned int i3 = i0 ^ side->dir2;
-
-    corners.push_back (box3d_side_corner (side, i0));
-    corners.push_back (box3d_side_corner (side, i1));
-    corners.push_back (box3d_side_corner (side, i2));
-    corners.push_back (box3d_side_corner (side, i3));
-    return corners;
-}
 
-/*
-static gint
-box3d_side_descr_to_id (gchar const *descr)
-{
-    if (!strcmp (descr, "XYrear")) { return 5; }
-    if (!strcmp (descr, "XYfront")) { return 2; }
-    if (!strcmp (descr, "XZbottom")) { return 1; }
-    if (!strcmp (descr, "XZtop")) { return 4; }
-    if (!strcmp (descr, "YZleft")) { return 3; }
-    if (!strcmp (descr, "YZright")) { return 0; }
-
-    g_warning ("Invalid description of 3D box face.\n");
-    g_print ("         (description is: %s)\n", descr);
-    return -1;
+    corners[0] = (side->front_or_rear ? orth : 0);
+    corners[1] = corners[0] ^ side->dir1;
+    corners[2] = corners[0] ^ side->dir1 ^ side->dir2;
+    corners[3] = corners[0] ^ side->dir2;
 }
-*/
 
 Persp3D *
 box3d_side_perspective(Box3DSide *side) {
     return SP_BOX3D(SP_OBJECT(side)->parent)->persp_ref->getObject();
 }
 
+Inkscape::XML::Node *box3d_side_convert_to_path(Box3DSide *side) {
+    // TODO: Copy over all important attributes (see sp_selected_item_to_curved_repr() for an example)
+    SPDocument *doc = SP_OBJECT_DOCUMENT(side);
+    Inkscape::XML::Document *xml_doc = doc->getReprDoc();
+
+    Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
+    repr->setAttribute("d", side->getAttribute("d"));
+    repr->setAttribute("style", side->getAttribute("style"));
+
+    return repr;
+}
+
 /*
   Local Variables:
   mode:c++
@@ -422,4 +330,4 @@ box3d_side_perspective(Box3DSide *side) {
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :