diff options
| author | Jabier Arraiza <jabier.arraiza@marker.es> | 2017-12-21 00:25:47 +0000 |
|---|---|---|
| committer | Jabier Arraiza <jabier.arraiza@marker.es> | 2017-12-21 00:25:47 +0000 |
| commit | e94c84e82f183e118b3f65c52d814c0155276c3f (patch) | |
| tree | 5e5717f9a0d6688113d12fb524c5c3dafd60bf1a /src | |
| parent | Tiny fix to improbe release pentool (diff) | |
| download | inkscape-e94c84e82f183e118b3f65c52d814c0155276c3f.tar.gz inkscape-e94c84e82f183e118b3f65c52d814c0155276c3f.zip | |
Refactoring pencil powerstroke
Diffstat (limited to 'src')
| -rw-r--r-- | src/ui/tools/freehand-base.cpp | 49 | ||||
| -rw-r--r-- | src/ui/tools/pencil-tool.cpp | 119 | ||||
| -rw-r--r-- | src/ui/tools/pencil-tool.h | 5 |
3 files changed, 103 insertions, 70 deletions
diff --git a/src/ui/tools/freehand-base.cpp b/src/ui/tools/freehand-base.cpp index 8d701d057..e09219a23 100644 --- a/src/ui/tools/freehand-base.cpp +++ b/src/ui/tools/freehand-base.cpp @@ -241,8 +241,51 @@ static void spdc_apply_powerstroke_shape(std::vector<Geom::Point> points, Freeha if (!c) { return; } - pt->addPowerStrokePencil(c); - sp_shape->setCurve(c, true); + if(dc->sa) { + c = dc->sa_overwrited->copy(); + Effect* lpe = SP_LPE_ITEM(dc->white_item)->getCurrentLPE(); + LPEPowerStroke* ps = static_cast<LPEPowerStroke*>(lpe); + std::vector<Geom::Point> points; + if (ps) { + if (dc->sa->start) { + points = ps->offset_points.reverse_controlpoints(false); + } else { + points = ps->offset_points.data(); + } + } + size_t sa_curve_size = dc->sa->curve->get_segment_count(); + for (auto ptp:pt->points) { + ptp[Geom::X] = ptp[Geom::X] + sa_curve_size; + points.push_back(ptp); + } + pt->addPowerStrokePencil(c); + if (lpe) { + gchar * pvector_str = sp_svg_write_path(c->get_pathvector()); + item->setAttribute("inkscape:original-d" , pvector_str); + g_free(pvector_str); + } else { + gchar * pvector_str = sp_svg_write_path(c->get_pathvector()); + item->setAttribute("d" , pvector_str); + g_free(pvector_str); + } + if (ps) { + ps->offset_points.param_set_and_write_new_value(points); + points.clear(); + return; + } + } else { + Effect* lpe = SP_LPE_ITEM(item)->getCurrentLPE(); + pt->addPowerStrokePencil(c); + if (lpe) { + gchar * pvector_str = sp_svg_write_path(c->get_pathvector()); + item->setAttribute("inkscape:original-d" , pvector_str); + g_free(pvector_str); + } else { + gchar * pvector_str = sp_svg_write_path(c->get_pathvector()); + item->setAttribute("d" , pvector_str); + g_free(pvector_str); + } + } } if(pt->points.empty()){ Inkscape::Preferences *prefs = Inkscape::Preferences::get(); @@ -868,7 +911,7 @@ static void spdc_flush_white(FreehandBase *dc, SPCurve *gc) Inkscape::Preferences *prefs = Inkscape::Preferences::get(); SPItem *item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr)); //Bend needs the transforms applied after, Other effects best before - (dc, item, c, true); + spdc_check_for_and_apply_waiting_LPE(dc, item, c, true); Inkscape::GC::release(repr); item->transform = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse(); item->updateRepr(); diff --git a/src/ui/tools/pencil-tool.cpp b/src/ui/tools/pencil-tool.cpp index 7779f6687..bf2edadba 100644 --- a/src/ui/tools/pencil-tool.cpp +++ b/src/ui/tools/pencil-tool.cpp @@ -73,6 +73,7 @@ PencilTool::PencilTool() , is_drawing(false) , sketch_n(0) , _powerpreview(NULL) + , _curve(NULL) { } @@ -81,7 +82,7 @@ void PencilTool::setup() { if (prefs->getBool("/tools/freehand/pencil/selcue")) { this->enableSelectionCue(); } - + this->_curve = new SPCurve(); FreehandBase::setup(); this->is_drawing = false; @@ -91,6 +92,7 @@ void PencilTool::setup() { PencilTool::~PencilTool() { + this->_curve->unref(); } void PencilTool::_extinput(GdkEvent *event) { @@ -453,9 +455,8 @@ bool PencilTool::_handleButtonRelease(GdkEventButton const &revent) { desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing freehand")); this->_interpolate(); - //Remove here because points are recalculated once finish. This is because we want live preview. - this->points.clear(); spdc_concat_colors_and_flush(this, FALSE); + this->points.clear(); this->sa = NULL; this->ea = NULL; this->ps.clear(); @@ -732,7 +733,18 @@ PencilTool::removePowerStrokePreview() } } void -PencilTool::addPowerStrokePencil(SPCurve * c) +PencilTool::addPowerStrokePencil(SPCurve *& c) +{ + if(sa) { + c->append_continuous( this->_curve->copy(), 0.0625); + } else { + c = this->_curve->copy(); + } + return; +} + +void +PencilTool::addPowerStrokePencil() { using namespace Inkscape::LivePathEffect; this->points.clear(); @@ -744,59 +756,31 @@ PencilTool::addPowerStrokePencil(SPCurve * c) double max = prefs->getIntLimited("/tools/freehand/pencil/maxpressure", 100, 1, 100) / 100.0; //This is a calculated number of nodes from 2 to 7 to get a simplify simil int tol = 2 + (prefs->getIntLimited("/tools/freehand/pencil/tolerance",10, 1, 100)/20); - + Geom::Affine transform_coordinate = SP_ITEM(SP_ACTIVE_DESKTOP->currentLayer())->i2dt_affine(); if (min > max){ min = max; } - bool live = false; - SPCurve * curve = new SPCurve(); - if (sa) { - Effect* lpe = SP_LPE_ITEM(white_item)->getCurrentLPE(); - LPEPowerStroke* ps = static_cast<LPEPowerStroke*>(lpe); - if (ps) { - if (sa->start) { - this->points = ps->offset_points.reverse_controlpoints(false); - } else { - this->points = ps->offset_points.data(); - } - } - } - if(!c) { - live = true; - if (sa && sa->curve) { - curve = sa_overwrited->copy(); - if (!green_curve->is_unset()) { - curve->append_continuous( green_curve, 0.0625); - if (!red_curve->is_unset()) { - curve->append_continuous( red_curve, 0.0625); - } - } - } else { - if (!green_curve->is_unset()) { - curve = green_curve->copy(); - if (!red_curve->is_unset()) { - curve->append_continuous( red_curve, 0.0625); - } - } else { - curve = NULL; - } - } - if (!curve || curve->is_unset()) { - curve = NULL; + + if (!green_curve->is_unset()) { + this->_curve = green_curve->copy(); + if (!red_curve->is_unset()) { + this->_curve->append_continuous( red_curve, 0.0625); } } else { - curve = c->copy(); - removePowerStrokePreview(); + this->_curve = NULL; + } + if (!this->_curve || this->_curve->is_unset()) { + this->_curve = NULL; } SPDocument * document = SP_ACTIVE_DOCUMENT; if (!document) { return; } - if (!curve) { + if (!this->_curve) { return; } - Geom::Affine transformCoordinate = SP_ITEM(SP_ACTIVE_DESKTOP->currentLayer())->i2dt_affine(); - Geom::PathVector pathvector = curve->get_pathvector(); + + Geom::PathVector pathvector = this->_curve->get_pathvector(); if (!pathvector.size()) { return; } @@ -807,11 +791,7 @@ PencilTool::addPowerStrokePencil(SPCurve * c) bool start = true; auto pressure = this->wps.begin(); size_t counter = 0; - Geom::Point position = *this->ps.begin(); - if (!live) { - position *= transformCoordinate.inverse(); - } - double pos = Geom::nearest_time(position, path); + Geom::Point position = Geom::Point(); size_t last = 0; for (auto point = this->ps.begin(); point != this->ps.end(); ++point, ++pressure) { position = *point; @@ -821,6 +801,9 @@ PencilTool::addPowerStrokePencil(SPCurve * c) //We need half width for power stroke pressure_computed = pressure_shrunk * dezoomify_factor/2.0; //remove start pressure gap + position *= transform_coordinate.inverse(); + + double pos = Geom::nearest_time(position, path); if (start) { start = false; this->points.push_back(Geom::Point(pos, pressure_computed)); @@ -829,14 +812,11 @@ PencilTool::addPowerStrokePencil(SPCurve * c) previous_pressure = pressure_shrunk; continue; } - if (!live) { - position *= transformCoordinate.inverse(); - } - pos = Geom::nearest_time(position, path); + if (counter%tol == 0) { this->key_nodes.push_back(position); } - if (pos < 1e6 && std::abs(previous_pressure - pressure_shrunk) > step && pos < path.size() - 1) { + if (pos < 1e6 && std::abs(previous_pressure - pressure_shrunk) > step ) { previous_pressure = pressure_shrunk; this->points.push_back(Geom::Point(pos, pressure_computed)); this->points_pos.push_back(position); @@ -848,20 +828,23 @@ PencilTool::addPowerStrokePencil(SPCurve * c) this->key_nodes.pop_back(); } } - bool last_added = counter%tol == 0; - if (!last_added) { + std::cout << *pressure << "pressure" << std::endl; + std::cout << this->pressure << "pressure222" << std::endl; + //Smooth end + if (counter%tol != 0) { + this->key_nodes.pop_back(); + } else { + this->key_nodes.pop_back(); this->key_nodes.pop_back(); - this->key_nodes.push_back(position); } + this->key_nodes.push_back(position); + if (this->points.size() > 0) { - _interpolate(curve); - if (live && !curve->is_empty()) { - _powerStrokePreview(curve->get_pathvector()[0] * transformCoordinate.inverse(), this->points); - } else { - c = curve->copy(); + _interpolate(this->_curve); + if (!this->_curve->is_empty()) { + _powerStrokePreview(this->_curve->get_pathvector()[0], this->points); } } - curve->unref(); } void PencilTool::_addFreehandPoint(Geom::Point const &p, guint /*state*/) { @@ -881,7 +864,7 @@ void PencilTool::_addFreehandPoint(Geom::Point const &p, guint /*state*/) { sp_canvas_item_destroy(i); } this->green_bpaths.clear(); - this->addPowerStrokePencil(NULL); + this->addPowerStrokePencil(); } } } @@ -982,7 +965,11 @@ void PencilTool::_interpolate() { this->green_curve->reset(); this->red_curve->reset(); this->red_curve_is_valid = false; - _interpolate(this->green_curve); + if (input_has_pressure && _curve) { + this->green_curve = _curve->copy(); + } else { + _interpolate(this->green_curve); + } } diff --git a/src/ui/tools/pencil-tool.h b/src/ui/tools/pencil-tool.h index 354acfbba..7d009e035 100644 --- a/src/ui/tools/pencil-tool.h +++ b/src/ui/tools/pencil-tool.h @@ -44,13 +44,15 @@ public: Geom::Point req_tangent; bool is_drawing; + double previous_pressure; std::vector<Geom::Point> ps; std::vector<Geom::Point> key_nodes; std::vector<Geom::Point> points; std::vector<Geom::Point> points_pos; std::vector<double> wps; - void addPowerStrokePencil(SPCurve * c); + void addPowerStrokePencil(SPCurve *& c); + void addPowerStrokePencil(); void removePowerStrokePreview(); Geom::Piecewise<Geom::D2<Geom::SBasis> > sketch_interpolation; // the current proposal from the sketched paths unsigned sketch_n; // number of sketches done @@ -65,6 +67,7 @@ protected: virtual bool root_handler(GdkEvent* event); private: + SPCurve * _curve; bool _handleButtonPress(GdkEventButton const &bevent); bool _handleMotionNotify(GdkEventMotion const &mevent); bool _handleButtonRelease(GdkEventButton const &revent); |
