tahoma2d/toonz/sources/tnztools/vectorselectiontool.cpp

2254 lines
72 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include "vectorselectiontool.h"
// TnzTools includes
#include "tools/toolhandle.h"
#include "tools/imagegrouping.h"
#include "tools/cursors.h"
// TnzQt includes
#include "toonzqt/selectioncommandids.h"
#include "toonzqt/tselectionhandle.h"
#include "toonzqt/imageutils.h"
// TnzLib includes
#include "toonz/txsheethandle.h"
#include "toonz/txshlevelhandle.h"
#include "toonz/tobjecthandle.h"
#include "toonz/tstageobject.h"
// TnzBase includes
#include "tenv.h"
// TnzCore includes
#include "drawutil.h"
// boost includes
#include <boost/bind.hpp>
using namespace ToolUtils;
using namespace DragSelectionTool;
//********************************************************************************
// Global variables
//********************************************************************************
2016-06-15 18:43:10 +12:00
namespace {
2016-03-19 06:57:51 +13:00
VectorSelectionTool l_vectorSelectionTool(TTool::Vectors);
2016-06-15 18:43:10 +12:00
TEnv::IntVar l_strokeSelectConstantThickness("SelectionToolConstantThickness",
0);
TEnv::IntVar l_strokeSelectIncludeIntersection(
"SelectionToolIncludeIntersection", 0);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const int l_dragThreshold = 10; //!< Distance, in pixels, the user has to
//! move from a button press to trigger a
//! selection drag.
} // namespace
2016-03-19 06:57:51 +13:00
//********************************************************************************
// Local namespace stuff
//********************************************************************************
2016-06-15 18:43:10 +12:00
namespace {
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
FourPoints getFourPointsFromVectorImage(const TVectorImageP &img,
const std::set<int> &styleIds,
double &maxThickness) {
FourPoints p;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (styleIds.empty()) {
p = img->getBBox();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
for (UINT i = 0; i < img->getStrokeCount(); i++) {
TStroke *s = img->getStroke(i);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
for (int j = 0; j < s->getControlPointCount(); j++) {
double thick = s->getControlPoint(j).thick;
2016-06-15 18:43:10 +12:00
if (maxThickness < thick) maxThickness = thick;
}
}
} else {
TRectD bbox;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
for (UINT i = 0; i < img->getStrokeCount(); i++) {
TStroke *s = img->getStroke(i);
if (!styleIds.count(s->getStyle())) continue;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (bbox.isEmpty())
bbox = s->getBBox();
else
bbox += s->getBBox();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
for (int j = 0; j < s->getControlPointCount(); j++) {
double thick = s->getControlPoint(j).thick;
2016-06-15 18:43:10 +12:00
if (maxThickness < thick) maxThickness = thick;
}
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
p = bbox;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return p;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool getStrokeIndexFromPos(UINT &index, const TVectorImageP &vi,
2019-09-27 15:14:14 +12:00
const TPointD &pos, double pixelSize, TAffine aff) {
2016-06-15 18:43:10 +12:00
if (!vi) return false;
double t, dist2 = 0;
2019-09-27 15:14:14 +12:00
double maxDist = 5 * pixelSize;
double maxDist2 = maxDist * maxDist;
double checkDist = maxDist2 * 4;
if (vi->getNearestStroke(pos, t, index, dist2)) {
TStroke *strokeRef = vi->getStroke(index);
TThickPoint cursor = strokeRef->getThickPoint(t);
double len = cursor.thick * pixelSize * sqrt(aff.det());
checkDist = std::max(checkDist, (len * len));
}
return (dist2 < checkDist);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
static bool currentOrNotSelected(const VectorSelectionTool &tool,
const TFrameId &fid) {
return (tool.getCurrentFid() == fid ||
(tool.isSelectedFramesType() &&
tool.getSelectedFrames().count(fid) == 0));
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
inline void notifySelectionChanged() {
TTool::getApplication()->getCurrentSelection()->notifySelectionChanged();
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
} // namespace
2016-03-19 06:57:51 +13:00
//********************************************************************************
// VectorFreeDeformer implementation
//********************************************************************************
2016-06-15 18:43:10 +12:00
VectorFreeDeformer::VectorFreeDeformer(TVectorImageP vi,
std::set<int> strokeIndexes)
: FreeDeformer()
, m_vi(vi)
, m_strokeIndexes(strokeIndexes)
, m_preserveThickness(false)
, m_computeRegion(false)
, m_flip(false) {
TRectD r;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::set<int>::iterator it, iEnd = m_strokeIndexes.end();
for (it = m_strokeIndexes.begin(); it != iEnd; ++it) {
TStroke *stroke = m_vi->getStroke(*it);
r += stroke->getBBox();
m_originalStrokes.push_back(new TStroke(*stroke));
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_originalP00 = r.getP00();
m_originalP11 = r.getP11();
m_newPoints.push_back(m_originalP00);
m_newPoints.push_back(r.getP10());
m_newPoints.push_back(m_originalP11);
m_newPoints.push_back(r.getP01());
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
VectorFreeDeformer::~VectorFreeDeformer() {
clearPointerContainer(m_originalStrokes);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorFreeDeformer::setPreserveThickness(bool preserveThickness) {
m_preserveThickness = preserveThickness;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorFreeDeformer::setComputeRegion(bool computeRegion) {
m_computeRegion = computeRegion;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorFreeDeformer::setFlip(bool flip) { m_flip = flip; }
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorFreeDeformer::setPoint(int index, const TPointD &p) {
m_newPoints[index] = p;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorFreeDeformer::setPoints(const TPointD &p0, const TPointD &p1,
const TPointD &p2, const TPointD &p3) {
m_newPoints[0] = p0;
m_newPoints[1] = p1;
m_newPoints[2] = p2;
m_newPoints[3] = p3;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorFreeDeformer::deformRegions() {
if (m_strokeIndexes.empty() || !m_computeRegion) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::vector<int> selectedIndexes(m_strokeIndexes.begin(),
m_strokeIndexes.end());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_vi->notifyChangedStrokes(selectedIndexes, m_originalStrokes, m_flip);
m_computeRegion = false;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorFreeDeformer::deformImage() {
// debug
assert(m_strokeIndexes.size() == m_originalStrokes.size());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// release
if (m_strokeIndexes.size() != m_originalStrokes.size()) {
return;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
QMutexLocker lock(m_vi->getMutex());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::size_t i = 0;
for (auto it = m_strokeIndexes.begin(), end = m_strokeIndexes.end();
it != end; ++it) {
TStroke *stroke = m_vi->getStroke(*it);
TStroke *originalStroke = m_originalStrokes[i++];
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
assert(stroke->getControlPointCount() ==
originalStroke->getControlPointCount());
for (int j = 0, count = stroke->getControlPointCount(); j < count; ++j) {
TThickPoint p = deform(originalStroke->getControlPoint(j));
stroke->setControlPoint(j, p);
}
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_computeRegion) deformRegions();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TThickPoint VectorFreeDeformer::deform(TThickPoint point) {
double vs = m_originalP11.x - m_originalP00.x;
double s = (vs == 0) ? 0 : (point.x - m_originalP00.x) / vs;
double vt = m_originalP11.y - m_originalP00.y;
double t = (vt == 0) ? 0 : (point.y - m_originalP00.y) / vt;
TPointD A = m_newPoints[0];
TPointD B = m_newPoints[1];
TPointD C = m_newPoints[2];
TPointD D = m_newPoints[3];
TPointD AD = (1 - t) * A + t * D;
TPointD BC = (1 - t) * B + t * C;
TPointD p = (1 - s) * AD + s * BC;
double thickness = point.thick;
if (!m_preserveThickness) {
double eps = 1.e-2;
TPointD p0x = TPointD(p.x - eps, p.x);
TPointD p1x = TPointD(p.x + eps, p.x);
TPointD p0y = TPointD(p.x, p.y - eps);
TPointD p1y = TPointD(p.x, p.y + eps);
m_preserveThickness = true;
TThickPoint newp0x = deform(p0x);
TThickPoint newp1x = deform(p1x);
TThickPoint newp0y = deform(p0y);
TThickPoint newp1y = deform(p1y);
m_preserveThickness = false;
double newA = fabs(cross(newp1x - newp0x, newp1y - newp0y));
double a = 4 * eps * eps;
thickness *= sqrt(newA / a);
}
return TThickPoint(p, thickness);
2016-03-19 06:57:51 +13:00
}
//********************************************************************************
// UndoChangeStrokes implementation
//********************************************************************************
DragSelectionTool::UndoChangeStrokes::UndoChangeStrokes(
2016-06-15 18:43:10 +12:00
TXshSimpleLevel *level, const TFrameId &frameId, VectorSelectionTool *tool,
const StrokeSelection &selection)
: ToolUtils::TToolUndo(level, frameId)
, m_tool(tool)
, m_selectionCount(tool->getSelectionCount()) // Not related to selection
, m_oldBBox(tool->getBBox())
, m_oldCenter(tool->getCenter())
, m_oldDeformValues(tool->m_deformValues)
, m_newDeformValues()
, m_flip(false) {
TVectorImageP vi = m_level->getFrame(m_frameId, false);
if (!vi) {
assert(vi);
return;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const StrokeSelection::IndexesContainer &indexes = selection.getSelection();
m_indexes.assign(indexes.begin(), indexes.end());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
registerStrokes(true);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
DragSelectionTool::UndoChangeStrokes::UndoChangeStrokes(
2016-06-15 18:43:10 +12:00
TXshSimpleLevel *level, const TFrameId &frameId, VectorSelectionTool *tool,
const LevelSelection &selection)
: ToolUtils::TToolUndo(level, frameId)
, m_tool(tool)
, m_selectionCount(tool->getSelectionCount()) // Not related to selection
, m_oldBBox(tool->getBBox())
, m_oldCenter(tool->getCenter())
, m_oldDeformValues(tool->m_deformValues)
, m_newDeformValues()
, m_flip(false) {
TVectorImageP vi = m_level->getFrame(m_frameId, false);
if (!vi) {
assert(vi);
return;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_indexes = getSelectedStrokes(*vi, selection);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
registerStrokes(true);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
DragSelectionTool::UndoChangeStrokes::~UndoChangeStrokes() {
clearPointerContainer(m_oldStrokes);
clearPointerContainer(m_newStrokes);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::UndoChangeStrokes::registerStrokes(bool beforeModify) {
TVectorImageP vi = m_level->getFrame(m_frameId, false);
if (!vi) {
assert(vi);
return;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::vector<TStroke *> &strokes = beforeModify ? m_oldStrokes : m_newStrokes;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TRectD bbox;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int s, sCount = int(m_indexes.size());
for (s = 0; s != sCount; ++s) {
TStroke *stroke = vi->getStroke(m_indexes[s]);
bbox += stroke->getBBox();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
strokes.push_back(new TStroke(*stroke));
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (beforeModify && !bbox.isEmpty()) {
ImageUtils::getFillingInformationOverlappingArea(vi, m_regionsData, bbox);
} else {
m_newBBox = m_tool->getBBox();
m_newCenter = m_tool->getCenter();
m_newDeformValues = m_tool->m_deformValues;
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
void DragSelectionTool::UndoChangeStrokes::transform(
2016-06-15 18:43:10 +12:00
const std::vector<TStroke *> &strokes, FourPoints bbox, TPointD center,
DragSelectionTool::DeformValues deformValues) const {
TVectorImageP image = m_level->getFrame(m_frameId, true);
if (!image) {
assert(image);
return;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int s, sCount = int(m_indexes.size());
for (s = 0; s != sCount; ++s) {
int index = m_indexes[s];
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TStroke *sourcesStroke = strokes[s], *stroke = image->getStroke(index);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int cp, cpCount = stroke->getControlPointCount();
for (cp = 0; cp != cpCount; ++cp)
stroke->setControlPoint(cp, sourcesStroke->getControlPoint(cp));
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
image->notifyChangedStrokes(m_indexes, strokes, m_flip);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!m_tool->isSelectionEmpty() &&
m_selectionCount == m_tool->getSelectionCount()) {
m_tool->setBBox(bbox);
m_tool->setCenter(center);
} else
m_tool->computeBBox();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_tool->notifyImageChanged(m_frameId);
m_tool->m_deformValues = deformValues;
TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
TTool::getApplication()->getCurrentTool()->notifyToolChanged();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::UndoChangeStrokes::restoreRegions() const {
TVectorImageP vi = m_level->getFrame(m_frameId, true);
if (!vi) {
assert(vi);
return;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
ImageUtils::assignFillingInformation(*vi, m_regionsData);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::UndoChangeStrokes::undo() const {
transform(m_oldStrokes, m_oldBBox, m_oldCenter, m_oldDeformValues);
restoreRegions();
2020-10-02 17:30:20 +13:00
m_tool->notifyImageChanged();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::UndoChangeStrokes::redo() const {
transform(m_newStrokes, m_newBBox, m_newCenter, m_newDeformValues);
2020-10-02 17:30:20 +13:00
m_tool->notifyImageChanged();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
int DragSelectionTool::UndoChangeStrokes::getSize() const {
return sizeof(*this) + sizeof(*m_tool);
2016-03-19 06:57:51 +13:00
}
//=============================================================================
// UndoChangeOutlineStyle
//-----------------------------------------------------------------------------
class UndoChangeOutlineStyle final : public ToolUtils::TToolUndo {
2016-06-15 18:43:10 +12:00
std::vector<TStroke::OutlineOptions> m_oldOptions, m_newOptions;
FourPoints m_oldBBox, m_newBBox;
VectorSelectionTool *m_tool;
std::vector<int> m_indexes;
int m_selectionCount;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
UndoChangeOutlineStyle(TXshSimpleLevel *level, const TFrameId &frameId,
VectorSelectionTool *tool);
~UndoChangeOutlineStyle() {}
void registerStrokes(bool beforeModify = false);
void transform(const std::vector<TStroke::OutlineOptions> &options,
FourPoints bbox) const;
2016-06-19 20:06:29 +12:00
void undo() const override;
void redo() const override;
int getSize() const override;
2016-03-19 06:57:51 +13:00
};
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
UndoChangeOutlineStyle::UndoChangeOutlineStyle(TXshSimpleLevel *level,
const TFrameId &frameId,
VectorSelectionTool *tool)
: ToolUtils::TToolUndo(level, frameId)
, m_tool(tool)
, m_oldBBox(tool->getBBox())
, m_selectionCount(tool->getSelectionCount()) {
TVectorImageP image = m_level->getFrame(m_frameId, false);
assert(!!image);
if (!image) return;
StrokeSelection *strokeSelection =
dynamic_cast<StrokeSelection *>(tool->getSelection());
int i;
for (i = 0; i < (int)image->getStrokeCount(); i++) {
if (!strokeSelection->isSelected(i) && !m_tool->isLevelType() &&
!m_tool->isSelectedFramesType())
continue;
m_indexes.push_back(i);
}
registerStrokes(true);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void UndoChangeOutlineStyle::registerStrokes(bool beforeModify) {
TVectorImageP image = m_level->getFrame(m_frameId, false);
assert(!!image);
if (!image) return;
int i;
for (i = 0; i < (int)m_indexes.size(); i++) {
if (beforeModify)
m_oldOptions.push_back(image->getStroke(m_indexes[i])->outlineOptions());
else
m_newOptions.push_back(image->getStroke(m_indexes[i])->outlineOptions());
}
if (!beforeModify) m_newBBox = m_tool->getBBox();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void UndoChangeOutlineStyle::transform(
const std::vector<TStroke::OutlineOptions> &options,
FourPoints bbox) const {
TVectorImageP image = m_level->getFrame(m_frameId, true);
assert(!!image);
if (!image) return;
int i;
for (i = 0; i < (int)m_indexes.size(); i++) {
int index = m_indexes[i];
image->getStroke(index)->outlineOptions() = options[i];
}
if (!m_tool->isSelectionEmpty() &&
m_selectionCount == m_tool->getSelectionCount())
m_tool->setBBox(bbox);
else
m_tool->computeBBox();
m_tool->notifyImageChanged(m_frameId);
TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
TTool::getApplication()->getCurrentTool()->notifyToolChanged();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void UndoChangeOutlineStyle::undo() const {
transform(m_oldOptions, m_oldBBox);
TTool::getApplication()->getCurrentTool()->notifyToolChanged();
2020-10-02 17:30:20 +13:00
m_tool->notifyImageChanged();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void UndoChangeOutlineStyle::redo() const {
transform(m_newOptions, m_newBBox);
TTool::getApplication()->getCurrentTool()->notifyToolChanged();
2020-10-02 17:30:20 +13:00
m_tool->notifyImageChanged();
2016-06-15 18:43:10 +12:00
}
//-----------------------------------------------------------------------------
int UndoChangeOutlineStyle::getSize() const {
// NOTE: This is definitely wrong... sizeof(vector) DOES NOT correspond to its
// actual size - as it is internally allocated with new!!
return sizeof(*this) + sizeof(*m_tool);
2016-03-19 06:57:51 +13:00
}
//=============================================================================
// VectorDeformTool::VFDScopedBlock
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
struct VectorDeformTool::VFDScopedBlock //! Type bounding the scope of free
//! deformers in the tool.
{
VFDScopedBlock(SelectionTool *tool) : m_tool(tool) {
m_tool->setNewFreeDeformer();
}
~VFDScopedBlock() { m_tool->clearDeformers(); }
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
SelectionTool *m_tool; //!< [\p external] Tool owning the deformers.
2016-03-19 06:57:51 +13:00
};
//=============================================================================
// VectorDeformTool
//-----------------------------------------------------------------------------
DragSelectionTool::VectorDeformTool::VectorDeformTool(VectorSelectionTool *tool)
2016-06-15 18:43:10 +12:00
: DeformTool(tool), m_undo() {
if (!TTool::getApplication()->getCurrentObject()->isSpline()) {
m_undo.reset(new UndoChangeStrokes(
TTool::getApplication()->getCurrentLevel()->getSimpleLevel(),
tool->getCurrentFid(), tool, tool->strokeSelection()));
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
DragSelectionTool::VectorDeformTool::~VectorDeformTool() {
// DO NOT REMOVE - DESTRUCTS TYPES INCOMPLETE IN THE HEADER
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2021-04-21 20:42:56 +12:00
void DragSelectionTool::VectorDeformTool::applyTransform(FourPoints bbox,
bool onFastDragging) {
2016-06-15 18:43:10 +12:00
SelectionTool *tool = getTool();
2016-03-19 06:57:51 +13:00
std::unique_ptr<VFDScopedBlock> localVfdScopedBlock;
if (!m_vfdScopedBlock) {
std::unique_ptr<VFDScopedBlock> &vfdScopedBlock =
2016-06-15 18:43:10 +12:00
m_isDragging ? m_vfdScopedBlock : localVfdScopedBlock;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
vfdScopedBlock.reset(new VFDScopedBlock(tool));
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
VectorFreeDeformer *freeDeformer =
static_cast<VectorFreeDeformer *>(tool->getFreeDeformer());
const bool stayedTheSame = bbox.getP00() == freeDeformer->getPoint(0) &&
bbox.getP10() == freeDeformer->getPoint(1) &&
bbox.getP11() == freeDeformer->getPoint(2) &&
bbox.getP01() == freeDeformer->getPoint(3);
2016-06-15 18:43:10 +12:00
freeDeformer->setPoints(bbox.getP00(), bbox.getP10(), bbox.getP11(),
bbox.getP01());
freeDeformer->setComputeRegion(!m_isDragging);
freeDeformer->setPreserveThickness(tool->isConstantThickness());
freeDeformer->setFlip(isFlip());
2016-03-19 06:57:51 +13:00
if (!TTool::getApplication()->getCurrentObject()->isSpline() && m_undo)
2016-06-15 18:43:10 +12:00
m_undo->setFlip(isFlip());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
freeDeformer->deformImage();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
tool->invalidate();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!m_isDragging) tool->notifyImageChanged();
2016-03-19 06:57:51 +13:00
if (!stayedTheSame) tool->m_deformValues.m_isSelectionModified = true;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!m_isDragging && (tool->isLevelType() || tool->isSelectedFramesType()))
transformWholeLevel();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorDeformTool::addTransformUndo() {
if (TTool::getApplication()->getCurrentObject()->isSpline())
TUndoManager::manager()->add(
new UndoPath(getTool()
->getXsheet()
->getStageObject(getTool()->getObjectId())
->getSpline()));
else if (m_undo) {
2016-06-15 18:43:10 +12:00
m_undo->registerStrokes();
TUndoManager::manager()->add(m_undo.release());
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorDeformTool::transformWholeLevel() {
VectorSelectionTool *tool = dynamic_cast<VectorSelectionTool *>(m_tool);
assert(tool);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
assert(!tool->levelSelection().isEmpty());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TXshSimpleLevel *level =
TTool::getApplication()->getCurrentLevel()->getSimpleLevel();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::vector<TFrameId> fids;
level->getFids(fids);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Remove unwanted fids
fids.erase(std::remove_if(
fids.begin(), fids.end(),
boost::bind(::currentOrNotSelected, boost::cref(*tool), _1)),
fids.end());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TUndoManager::manager()->beginBlock();
{
addTransformUndo(); // For current frame
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int f, fCount = int(fids.size());
for (f = 0; f != fCount; ++f) {
const TFrameId &fid = fids[f];
int t = f + 1; // Current frame's data is always at index 0
// The others are thus shifted by 1
// Skip nonselected frames
if (tool->getCurrentFid() == fid ||
(tool->isSelectedFramesType() &&
tool->getSelectedFrames().count(fid) == 0))
continue;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TVectorImageP vi = level->getFrame(fid, true);
if (!vi) continue;
2016-03-19 06:57:51 +13:00
std::unique_ptr<UndoChangeStrokes> undo(
2016-06-15 18:43:10 +12:00
new UndoChangeStrokes(level, fid, tool, tool->levelSelection()));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::set<int> strokesIndices;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
for (int s = 0; s < (int)vi->getStrokeCount(); ++s)
strokesIndices.insert(s);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
FourPoints bbox = tool->getBBox(t);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
VectorFreeDeformer *freeDeformer =
static_cast<VectorFreeDeformer *>(tool->getFreeDeformer(t));
freeDeformer->setPoints(bbox.getPoint(0), bbox.getPoint(1),
bbox.getPoint(2), bbox.getPoint(3));
freeDeformer->setComputeRegion(true);
freeDeformer->setPreserveThickness(tool->isConstantThickness());
freeDeformer->setFlip(isFlip());
freeDeformer->deformImage();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
undo->registerStrokes();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TUndoManager::manager()->add(undo.release());
}
}
TUndoManager::manager()->endBlock();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Finally, notify changed frames
std::for_each(fids.begin(), fids.end(),
boost::bind( // NOTE: current frame is not here - it should be,
&TTool::notifyImageChanged, m_tool,
_1)); // but it's currently unnecessary, in fact...
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// notifyImageChanged(fid) must be invoked OUTSIDE of the loop - since it
// seems to imply notifyImageChanged()
// on CURRENT image - which seems wrong, but it's too low-level and I'm not
// changing it.
// This reasonably leads to computeBBox(), but bboxes are taken as INPUT to
// this transformation... >:(
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool DragSelectionTool::VectorDeformTool::isFlip() {
TPointD scaleValue = getTool()->m_deformValues.m_scaleValue;
return m_startScaleValue.x * scaleValue.x < 0 ||
m_startScaleValue.y * scaleValue.y < 0;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorDeformTool::leftButtonUp(const TPointD &pos,
const TMouseEvent &e) {
std::unique_ptr<VFDScopedBlock> vfdScopedBlock(std::move(m_vfdScopedBlock));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
SelectionTool *tool = getTool();
VectorFreeDeformer *deformer =
dynamic_cast<VectorFreeDeformer *>(tool->getFreeDeformer());
if (!deformer) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
deformer->setComputeRegion(true);
deformer->setFlip(isFlip());
deformer->deformRegions();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!tool->isLevelType() && !tool->isSelectedFramesType())
addTransformUndo();
else
transformWholeLevel();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_isDragging = false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
tool->notifyImageChanged();
2019-11-01 07:29:23 +13:00
VectorSelectionTool *selectionTool =
dynamic_cast<VectorSelectionTool *>(m_tool);
selectionTool->setResetCenter(true);
2016-03-19 06:57:51 +13:00
}
//=============================================================================
// VectorRotationTool
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
DragSelectionTool::VectorRotationTool::VectorRotationTool(
VectorSelectionTool *tool)
: VectorDeformTool(tool), m_rotation(new Rotation(this)) {}
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorRotationTool::transform(TAffine aff,
double angle) {
SelectionTool *tool = getTool();
FourPoints newBbox(tool->getBBox() * aff);
TPointD center = m_rotation->getStartCenter();
int i;
for (i = 0; i < tool->getBBoxsCount(); i++) {
aff = TRotation(center, angle);
tool->setBBox(tool->getBBox(i) * aff, i);
}
applyTransform(newBbox);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorRotationTool::leftButtonDrag(
const TPointD &pos, const TMouseEvent &e) {
2019-11-01 07:29:23 +13:00
VectorSelectionTool *tool = dynamic_cast<VectorSelectionTool *>(m_tool);
tool->setResetCenter(false);
2016-06-15 18:43:10 +12:00
m_rotation->leftButtonDrag(pos, e);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorRotationTool::draw() { m_rotation->draw(); }
2016-03-19 06:57:51 +13:00
//=============================================================================
// VectorFreeDeformTool
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
DragSelectionTool::VectorFreeDeformTool::VectorFreeDeformTool(
VectorSelectionTool *tool)
: VectorDeformTool(tool), m_freeDeform(new FreeDeform(this)) {}
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorFreeDeformTool::leftButtonDrag(
const TPointD &pos, const TMouseEvent &e) {
2019-11-01 07:29:23 +13:00
VectorSelectionTool *tool = dynamic_cast<VectorSelectionTool *>(m_tool);
tool->setResetCenter(false);
2016-06-15 18:43:10 +12:00
m_freeDeform->leftButtonDrag(pos, e);
2016-03-19 06:57:51 +13:00
}
//=============================================================================
// VectorMoveSelectionTool
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
DragSelectionTool::VectorMoveSelectionTool::VectorMoveSelectionTool(
VectorSelectionTool *tool)
: VectorDeformTool(tool), m_moveSelection(new MoveSelection(this)) {}
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorMoveSelectionTool::transform(TAffine aff) {
SelectionTool *tool = getTool();
int i;
for (i = 0; i < (int)tool->getBBoxsCount(); i++)
tool->setBBox(tool->getBBox(i) * aff, i);
getTool()->setCenter(aff * tool->getCenter());
2018-02-19 19:34:13 +13:00
applyTransform(tool->getBBox());
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorMoveSelectionTool::leftButtonDown(
const TPointD &pos, const TMouseEvent &e) {
m_moveSelection->leftButtonDown(pos, e);
VectorDeformTool::leftButtonDown(pos, e);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorMoveSelectionTool::leftButtonDrag(
const TPointD &pos, const TMouseEvent &e) {
2019-11-01 07:29:23 +13:00
VectorSelectionTool *tool = dynamic_cast<VectorSelectionTool *>(m_tool);
tool->setResetCenter(false);
2018-02-19 19:34:13 +13:00
if (e.isCtrlPressed() ||
norm2(pos - getStartPos()) > l_dragThreshold * getTool()->getPixelSize())
2016-06-15 18:43:10 +12:00
m_moveSelection->leftButtonDrag(pos, e);
2018-02-19 19:34:13 +13:00
else // snap to the original position
m_moveSelection->leftButtonDrag(getStartPos(), e);
2016-03-19 06:57:51 +13:00
}
//=============================================================================
// VectorScaleTool
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
DragSelectionTool::VectorScaleTool::VectorScaleTool(VectorSelectionTool *tool,
ScaleType type)
2016-06-15 18:43:10 +12:00
: VectorDeformTool(tool), m_scale(new Scale(this, type)) {}
//-----------------------------------------------------------------------------
2021-04-21 20:42:56 +12:00
TPointD DragSelectionTool::VectorScaleTool::transform(int index, TPointD newPos,
bool onFastDragging) {
2016-06-15 18:43:10 +12:00
SelectionTool *tool = getTool();
TPointD scaleValue = tool->m_deformValues.m_scaleValue;
std::vector<FourPoints> startBboxs = m_scale->getStartBboxs();
TPointD center = m_scale->getStartCenter();
FourPoints bbox = m_scale->bboxScaleInCenter(index, startBboxs[0], newPos,
scaleValue, center, true);
if (bbox == startBboxs[0]) return scaleValue;
bool scaleInCenter = m_scale->scaleInCenter();
// Se non ho scalato rispetto al centro calcolo la posizione del nuovo centro
if (!scaleInCenter)
tool->setCenter(m_scale->getNewCenter(index, startBboxs[0], scaleValue));
if (tool->isLevelType() || tool->isSelectedFramesType()) {
int i;
for (i = 1; i < (int)tool->getBBoxsCount(); i++) {
FourPoints oldBbox = startBboxs[i];
TPointD frameCenter =
scaleInCenter ? center
2016-09-06 01:20:21 +12:00
: oldBbox.getPoint(getSymmetricPointIndex(index));
2016-06-15 18:43:10 +12:00
TPointD newp =
m_scale->getScaledPoint(index, oldBbox, scaleValue, frameCenter);
FourPoints newBbox = m_scale->bboxScaleInCenter(
index, oldBbox, newp, scaleValue, frameCenter, false);
tool->setBBox(newBbox, i);
if (!scaleInCenter)
tool->setCenter(m_scale->getNewCenter(index, oldBbox, scaleValue), i);
}
}
tool->setBBox(bbox);
applyTransform(bbox);
return scaleValue;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorScaleTool::leftButtonDown(const TPointD &pos,
const TMouseEvent &e) {
m_scale->leftButtonDown(pos, e);
VectorDeformTool::leftButtonDown(pos, e);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorScaleTool::leftButtonDrag(const TPointD &pos,
const TMouseEvent &e) {
2019-11-01 07:29:23 +13:00
VectorSelectionTool *tool = dynamic_cast<VectorSelectionTool *>(m_tool);
tool->setResetCenter(false);
2016-06-15 18:43:10 +12:00
m_scale->leftButtonDrag(pos, e);
2016-03-19 06:57:51 +13:00
}
//=============================================================================
// VectorChangeThicknessTool
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
DragSelectionTool::VectorChangeThicknessTool::VectorChangeThicknessTool(
VectorSelectionTool *tool)
: DragTool(tool), m_curPos(), m_firstPos(), m_thicknessChange(0) {
TVectorImageP vi = tool->getImage(false);
assert(vi);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
setStrokesThickness(*vi);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TXshSimpleLevel *level =
TTool::getApplication()->getCurrentLevel()->getSimpleLevel();
m_undo.reset(new UndoChangeStrokes(level, tool->getCurrentFid(), tool,
tool->strokeSelection()));
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
VectorChangeThicknessTool::~VectorChangeThicknessTool() {}
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
namespace {
namespace SetStrokeThickness {
2016-03-19 06:57:51 +13:00
using namespace DragSelectionTool;
struct Data {
2016-06-15 18:43:10 +12:00
VectorChangeThicknessTool &m_tool;
const TVectorImage &m_vi;
2016-03-19 06:57:51 +13:00
};
} // namespace SetStrokeThickness
} // namespace
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorChangeThicknessTool::setStrokesThickness(
TVectorImage &vi) {
struct locals {
static void setThickness(const SetStrokeThickness::Data &data, int s) {
const TStroke &stroke = *data.m_vi.getStroke(s);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::vector<double> strokeThickness;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int cp, cpCount = stroke.getControlPointCount();
strokeThickness.reserve(cpCount);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
for (cp = 0; cp != cpCount; ++cp)
strokeThickness.push_back(stroke.getControlPoint(cp).thick);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
data.m_tool.m_strokesThickness[s] = strokeThickness;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
}; // locals
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
SetStrokeThickness::Data data = {*this, vi};
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
VectorSelectionTool *vsTool = static_cast<VectorSelectionTool *>(m_tool);
const LevelSelection &levelSelection = vsTool->levelSelection();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (levelSelection.isEmpty()) {
StrokeSelection *strokeSelection =
static_cast<StrokeSelection *>(m_tool->getSelection());
const std::set<int> &selectedStrokeIdxs = strokeSelection->getSelection();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::for_each(selectedStrokeIdxs.begin(), selectedStrokeIdxs.end(),
boost::bind(locals::setThickness, boost::cref(data), _1));
} else {
std::vector<int> strokeIdxs = getSelectedStrokes(vi, levelSelection);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::for_each(strokeIdxs.begin(), strokeIdxs.end(),
boost::bind(locals::setThickness, boost::cref(data), _1));
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
namespace {
namespace ChangeImageThickness {
2016-03-19 06:57:51 +13:00
using namespace DragSelectionTool;
struct Data {
2016-06-15 18:43:10 +12:00
VectorChangeThicknessTool &m_tool;
TVectorImage &m_vi;
double m_newThickness;
2016-03-19 06:57:51 +13:00
};
} // namespace ChangeImageThickness
} // namespace
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorChangeThicknessTool::changeImageThickness(
TVectorImage &vi, double newThickness) {
struct locals {
static void changeThickness(const ChangeImageThickness::Data &data, int s) {
TStroke &stroke = *data.m_vi.getStroke(s);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
for (int cp = 0; cp < (int)stroke.getControlPointCount(); ++cp) {
double thickness =
tcrop(data.m_tool.m_strokesThickness[s][cp] + data.m_newThickness,
0.0, 255.0);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TThickPoint point(TPointD(stroke.getControlPoint(cp)), thickness);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
stroke.setControlPoint(cp, point);
}
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
}; // locals
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
ChangeImageThickness::Data data = {*this, vi, newThickness};
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
VectorSelectionTool *vsTool = static_cast<VectorSelectionTool *>(getTool());
const LevelSelection &levelSelection = vsTool->levelSelection();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (levelSelection.isEmpty()) {
StrokeSelection *strokeSelection =
static_cast<StrokeSelection *>(m_tool->getSelection());
const std::set<int> &selectedStrokeIdxs = strokeSelection->getSelection();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::for_each(selectedStrokeIdxs.begin(), selectedStrokeIdxs.end(),
boost::bind(locals::changeThickness, boost::ref(data), _1));
} else {
std::vector<int> strokeIdxs = getSelectedStrokes(vi, levelSelection);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::for_each(strokeIdxs.begin(), strokeIdxs.end(),
boost::bind(locals::changeThickness, boost::ref(data), _1));
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorChangeThicknessTool::addUndo() {
TVectorImageP curVi = getTool()->getImage(true);
if (!curVi) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_undo->registerStrokes();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
SelectionTool *tool = getTool();
if (tool->isLevelType() || tool->isSelectedFramesType()) {
VectorSelectionTool *vtool = dynamic_cast<VectorSelectionTool *>(tool);
assert(vtool && !vtool->levelSelection().isEmpty());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TXshSimpleLevel *level =
TTool::getApplication()->getCurrentLevel()->getSimpleLevel();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Retrieve frames available in the level
std::vector<TFrameId> fids;
level->getFids(fids);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Remove unwanted frames
fids.erase(std::remove_if(fids.begin(), fids.end(),
boost::bind(::currentOrNotSelected,
boost::cref(*vtool), _1)),
fids.end());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TUndoManager::manager()->beginBlock();
{
// Current frame added separately
TUndoManager::manager()->add(m_undo.release()); // Inside an undo block
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Iterate remaining ones
int f, fCount = int(fids.size());
for (f = 0; f != fCount; ++f) {
const TFrameId &fid = fids[f];
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TVectorImageP vi = level->getFrame(fid, true);
if (!vi) {
assert(vi);
continue;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Transform fid's selection
std::unique_ptr<UndoChangeStrokes> undo(
2016-06-15 18:43:10 +12:00
new UndoChangeStrokes(level, fid, vtool, vtool->levelSelection()));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
setStrokesThickness(*vi);
changeImageThickness(*vi, m_thicknessChange);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_strokesThickness.clear();
undo->registerStrokes();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TUndoManager::manager()->add(undo.release());
}
}
TUndoManager::manager()->endBlock();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Finally, notify changed frames
std::for_each(fids.begin(), fids.end(),
boost::bind( // NOTE: current frame is not here - it was
&TTool::notifyImageChanged, m_tool,
_1)); // aldready notified
} else
TUndoManager::manager()->add(m_undo.release()); // Outside any undo block
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorChangeThicknessTool::leftButtonDown(
const TPointD &pos, const TMouseEvent &e) {
m_curPos = pos;
m_firstPos = pos;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorChangeThicknessTool::leftButtonDrag(
const TPointD &pos, const TMouseEvent &e) {
TAffine aff;
TPointD delta = pos - m_curPos;
TVectorImageP vi = getTool()->getImage(true);
if (!vi) return;
2019-11-01 07:29:23 +13:00
VectorSelectionTool *tool = dynamic_cast<VectorSelectionTool *>(m_tool);
tool->setResetCenter(false);
2016-06-15 18:43:10 +12:00
m_thicknessChange = (pos.y - m_firstPos.y) * 0.2;
changeImageThickness(*vi, m_thicknessChange);
getTool()->m_deformValues.m_maxSelectionThickness = m_thicknessChange;
getTool()->computeBBox();
getTool()->invalidate();
m_curPos = pos;
getTool()->notifyImageChanged();
TTool::getApplication()->getCurrentTool()->notifyToolChanged();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::VectorChangeThicknessTool::leftButtonUp(
const TPointD &pos, const TMouseEvent &e) {
TVectorImageP curVi = getTool()->getImage(true);
if (!curVi) return;
addUndo();
m_strokesThickness.clear();
2016-03-19 06:57:51 +13:00
}
//=============================================================================
2016-06-15 18:43:10 +12:00
namespace {
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool getGroupBBox(const TVectorImage &vi, int strokeIndex, TRectD &gBox) {
if (!vi.isStrokeGrouped(strokeIndex)) return false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
gBox = vi.getStroke(strokeIndex)->getBBox();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int s, sCount = int(vi.getStrokeCount());
for (s = 0; s != sCount; ++s) {
if (vi.sameGroup(s, strokeIndex)) gBox += vi.getStroke(s)->getBBox();
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return true;
2016-03-19 06:57:51 +13:00
}
//=============================================================================
// UndoEnterGroup
//-----------------------------------------------------------------------------
class UndoEnterGroup final : public TUndo {
2016-06-15 18:43:10 +12:00
int m_strokeIndex;
TVectorImageP m_vi;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
UndoEnterGroup(TVectorImageP vi, int strokeIndex)
: m_vi(vi), m_strokeIndex(strokeIndex) {}
2016-06-19 20:06:29 +12:00
void undo() const override {
2016-06-15 18:43:10 +12:00
m_vi->exitGroup();
TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
}
2016-06-19 20:06:29 +12:00
void redo() const override {
2016-06-15 18:43:10 +12:00
m_vi->enterGroup(m_strokeIndex);
TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
}
2016-06-19 20:06:29 +12:00
int getSize() const override { return sizeof(*this); }
2016-03-19 06:57:51 +13:00
};
//=============================================================================
// UndoExitGroup
//-----------------------------------------------------------------------------
class UndoExitGroup final : public TUndo {
2016-06-15 18:43:10 +12:00
int m_strokeIndex;
TVectorImageP m_vi;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
UndoExitGroup(TVectorImageP vi, int strokeIndex)
: m_vi(vi), m_strokeIndex(strokeIndex) {}
2016-06-19 20:06:29 +12:00
void undo() const override {
2016-06-15 18:43:10 +12:00
m_vi->enterGroup(m_strokeIndex);
TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
}
2016-06-19 20:06:29 +12:00
void redo() const override {
2016-06-15 18:43:10 +12:00
m_vi->exitGroup();
TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
}
2016-06-19 20:06:29 +12:00
int getSize() const override { return sizeof(*this); }
2016-03-19 06:57:51 +13:00
};
2016-06-15 18:43:10 +12:00
} // namespace
2016-03-19 06:57:51 +13:00
//=============================================================================
// VectorSelectionTool
//-----------------------------------------------------------------------------
VectorSelectionTool::VectorSelectionTool(int targetType)
2016-06-15 18:43:10 +12:00
: SelectionTool(targetType)
, m_selectionTarget("Mode:")
, m_includeIntersection("Include Intersection", false)
2016-06-15 18:43:10 +12:00
, m_constantThickness("Preserve Thickness", false)
, m_levelSelection(m_strokeSelection)
, m_capStyle("Cap")
, m_joinStyle("Join")
, m_miterJoinLimit("Miter:", 0, 100, 4)
, m_selectionCount(0)
2019-11-01 07:29:23 +13:00
, m_canEnterGroup(true)
, m_resetCenter(true) {
assert(targetType == TTool::Vectors);
m_prop.bind(m_selectionTarget);
m_prop.bind(m_includeIntersection);
m_prop.bind(m_constantThickness);
m_selectionTarget.addValue(NORMAL_TYPE);
m_selectionTarget.addValue(SELECTED_FRAMES_TYPE);
m_selectionTarget.addValue(ALL_LEVEL_TYPE);
m_selectionTarget.addValue(SAME_STYLE_TYPE);
m_selectionTarget.addValue(STYLE_SELECTED_FRAMES_TYPE);
m_selectionTarget.addValue(STYLE_LEVEL_TYPE);
m_selectionTarget.addValue(BOUNDARY_TYPE);
m_selectionTarget.addValue(BOUNDARY_SELECTED_FRAMES_TYPE);
m_selectionTarget.addValue(BOUNDARY_LEVEL_TYPE);
2016-06-15 18:43:10 +12:00
m_strokeSelection.setView(this);
m_includeIntersection.setId("IncludeIntersection");
2016-06-15 18:43:10 +12:00
m_constantThickness.setId("PreserveThickness");
m_selectionTarget.setId("SelectionMode");
m_capStyle.addValue(BUTT_WSTR, QString::fromStdWString(BUTT_WSTR));
m_capStyle.addValue(ROUNDC_WSTR, QString::fromStdWString(ROUNDC_WSTR));
m_capStyle.addValue(PROJECTING_WSTR,
QString::fromStdWString(PROJECTING_WSTR));
2016-06-15 18:43:10 +12:00
m_capStyle.setId("Cap");
m_joinStyle.addValue(MITER_WSTR, QString::fromStdWString(MITER_WSTR));
m_joinStyle.addValue(ROUNDJ_WSTR, QString::fromStdWString(ROUNDJ_WSTR));
m_joinStyle.addValue(BEVEL_WSTR, QString::fromStdWString(BEVEL_WSTR));
2016-06-15 18:43:10 +12:00
m_joinStyle.setId("Join");
m_miterJoinLimit.setId("Miter");
m_outlineProps.bind(m_capStyle);
m_outlineProps.bind(m_joinStyle);
m_outlineProps.bind(m_miterJoinLimit);
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::setNewFreeDeformer() {
clearDeformers();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TVectorImageP vi =
getImage(true); // BAD: Should not be done at tool creation...
if (!vi) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Current freeDeformer always at index 0
m_freeDeformers.push_back(
new VectorFreeDeformer(vi, m_strokeSelection.getSelection()));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (isLevelType() || isSelectedFramesType()) {
TXshSimpleLevel *level =
TTool::getApplication()->getCurrentLevel()->getSimpleLevel();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// All SELECTED frames' subsequent freeDeformers are stored sequentially
// after that
std::vector<TFrameId> fids;
level->getFids(fids);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
fids.erase(std::remove_if(
fids.begin(), fids.end(),
boost::bind(::currentOrNotSelected, boost::cref(*this), _1)),
fids.end());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::vector<TFrameId>::iterator ft, fEnd = fids.end();
for (ft = fids.begin(); ft != fEnd; ++ft) {
if (TVectorImageP levelVi = level->getFrame(*ft, false)) {
const std::vector<int> &selectedStrokeIdxs =
::getSelectedStrokes(*levelVi, m_levelSelection);
std::set<int> strokesIndices(selectedStrokeIdxs.begin(),
selectedStrokeIdxs.end());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_freeDeformers.push_back(
new VectorFreeDeformer(levelVi, strokesIndices));
}
}
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool VectorSelectionTool::isLevelType() const {
return m_levelSelection.framesMode() == LevelSelection::FRAMES_ALL;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool VectorSelectionTool::isSelectedFramesType() const {
return m_levelSelection.framesMode() == LevelSelection::FRAMES_SELECTED;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool VectorSelectionTool::isSameStyleType() const {
return m_levelSelection.filter() == LevelSelection::SELECTED_STYLES;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool VectorSelectionTool::isModifiableSelectionType() const {
return (m_levelSelection.isEmpty() ||
m_levelSelection.filter() == LevelSelection::SELECTED_STYLES);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::updateTranslation() {
m_selectionTarget.setQStringName(tr("Mode:"));
m_selectionTarget.setItemUIName(NORMAL_TYPE, tr("Standard"));
m_selectionTarget.setItemUIName(SELECTED_FRAMES_TYPE, tr("Selected Frames"));
m_selectionTarget.setItemUIName(ALL_LEVEL_TYPE, tr("Whole Level"));
m_selectionTarget.setItemUIName(SAME_STYLE_TYPE, tr("Same Style"));
m_selectionTarget.setItemUIName(STYLE_SELECTED_FRAMES_TYPE,
tr("Same Style on Selected Frames"));
m_selectionTarget.setItemUIName(STYLE_LEVEL_TYPE,
tr("Same Style on Whole Level"));
m_selectionTarget.setItemUIName(BOUNDARY_TYPE, tr("Boundary Strokes"));
m_selectionTarget.setItemUIName(BOUNDARY_SELECTED_FRAMES_TYPE,
tr("Boundaries on Selected Frames"));
m_selectionTarget.setItemUIName(BOUNDARY_LEVEL_TYPE,
tr("Boundaries on Whole Level"));
m_includeIntersection.setQStringName(tr("Include Intersection"));
2016-06-15 18:43:10 +12:00
m_constantThickness.setQStringName(tr("Preserve Thickness"));
2016-06-15 18:43:10 +12:00
m_capStyle.setQStringName(tr("Cap"));
m_capStyle.setItemUIName(BUTT_WSTR, tr("Butt cap"));
m_capStyle.setItemUIName(ROUNDC_WSTR, tr("Round cap"));
m_capStyle.setItemUIName(PROJECTING_WSTR, tr("Projecting cap"));
2016-06-15 18:43:10 +12:00
m_joinStyle.setQStringName(tr("Join"));
m_joinStyle.setItemUIName(MITER_WSTR, tr("Miter join"));
m_joinStyle.setItemUIName(ROUNDJ_WSTR, tr("Round join"));
m_joinStyle.setItemUIName(BEVEL_WSTR, tr("Bevel join"));
2016-06-15 18:43:10 +12:00
m_miterJoinLimit.setQStringName(tr("Miter:"));
SelectionTool::updateTranslation();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::updateSelectionTarget() {
// Make the correct selection current
if (m_selectionTarget.getIndex() == NORMAL_TYPE_IDX) {
std::set<int> selectedStrokes; // Retain previously selected strokes across
selectedStrokes.swap(
m_strokeSelection.getSelection()); // current selection change
m_strokeSelection.makeCurrent(); // Empties any (different) previously
// current selection on its own
2016-06-15 18:43:10 +12:00
selectedStrokes.swap(m_strokeSelection.getSelection());
return;
}
m_levelSelection.makeCurrent(); // Same here
// Choose frames mode
LevelSelection::FramesMode framesMode;
switch (m_selectionTarget.getIndex()) {
case SAME_STYLE_TYPE_IDX:
case BOUNDARY_TYPE_IDX:
framesMode = LevelSelection::FRAMES_CURRENT;
break;
case ALL_LEVEL_TYPE_IDX:
case STYLE_LEVEL_TYPE_IDX:
case BOUNDARY_LEVEL_TYPE_IDX:
framesMode = LevelSelection::FRAMES_ALL;
break;
case SELECTED_FRAMES_TYPE_IDX:
case STYLE_SELECTED_FRAMES_TYPE_IDX:
case BOUNDARY_SELECTED_FRAMES_TYPE_IDX:
framesMode = LevelSelection::FRAMES_SELECTED;
break;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (framesMode != m_levelSelection.framesMode()) clearSelectedStrokes();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_levelSelection.framesMode() = framesMode;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Choose filter
LevelSelection::Filter filter;
switch (m_selectionTarget.getIndex()) {
case SELECTED_FRAMES_TYPE_IDX:
case ALL_LEVEL_TYPE_IDX:
filter = LevelSelection::WHOLE;
selectedStyles().clear();
break;
case SAME_STYLE_TYPE_IDX:
case STYLE_SELECTED_FRAMES_TYPE_IDX:
case STYLE_LEVEL_TYPE_IDX:
filter = LevelSelection::SELECTED_STYLES;
break;
case BOUNDARY_TYPE_IDX:
case BOUNDARY_SELECTED_FRAMES_TYPE_IDX:
case BOUNDARY_LEVEL_TYPE_IDX:
filter = LevelSelection::BOUNDARY_STROKES;
selectedStyles().clear();
break;
}
if (filter != m_levelSelection.filter()) clearSelectedStrokes();
m_levelSelection.filter() = filter;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::finalizeSelection() {
TVectorImageP vi = getImage(false);
if (vi && !m_levelSelection.isEmpty()) {
std::set<int> &selection = m_strokeSelection.getSelection();
selection.clear();
// Apply base additive selection
if (!isSelectedFramesType() || m_selectedFrames.count(getCurrentFid())) {
// Apply filters
std::vector<int> selectedStrokes =
getSelectedStrokes(*vi, m_levelSelection);
std::set<int>(selectedStrokes.begin(), selectedStrokes.end())
.swap(selection);
}
}
computeBBox();
TTool::getApplication()
->getCurrentTool()
->notifyToolChanged(); // Refreshes toolbar values
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::clearSelectedStrokes() {
m_strokeSelection.selectNone();
m_levelSelection.styles().clear();
m_deformValues.reset();
}
//-----------------------------------------------------------------------------
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::modifySelectionOnClick(TImageP image,
const TPointD &pos,
const TMouseEvent &e) {
TVectorImageP vi = TVectorImageP(image);
assert(m_strokeSelection.getImage() == vi);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!vi) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
updateSelectionTarget(); // Make selection current. This is necessary in case
// some other selection context made another selection current.
m_firstPos = m_curPos = pos;
m_selectingRect = FourPoints();
m_selecting = false;
m_justSelected = false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
updateAction(pos, e);
if (m_what != Inside && m_what != Outside && m_what != ADD_SELECTION) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
UINT index = 0;
bool modifiableSel = isModifiableSelectionType(),
2019-09-27 15:14:14 +12:00
strokeAtPos = getStrokeIndexFromPos(index, vi, pos, getPixelSize(),
getViewer()->getViewMatrix()),
addStroke = strokeAtPos && !m_strokeSelection.isSelected(index),
toggleStroke = strokeAtPos && e.isShiftPressed();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_selecting =
(modifiableSel && !strokeAtPos // There must be no stroke under cursor
&& (e.isShiftPressed() // and either the user is explicitly performing
// additional selection
|| (m_strokeSelectionType.getIndex() !=
POLYLINE_SELECTION_IDX) // or the tool support immediate
// selection on clear
|| m_strokeSelection
.isEmpty())); // or the strokes list was already cleared
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bool clearTargets = !(strokeAtPos || e.isShiftPressed() || m_selecting),
clearSelection = (addStroke || !strokeAtPos) && !e.isShiftPressed(),
selectionChanged = clearSelection;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
assert(clearTargets ? clearSelection : true);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (clearTargets) m_levelSelection.selectNone();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (clearSelection) {
m_strokeSelection.selectNone();
selectedStyles().clear(); // Targets are preserved here
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (strokeAtPos)
selectionChanged = m_justSelected = selectStroke(index, toggleStroke);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (selectionChanged) {
m_deformValues.reset(); // Resets selection values shown in the toolbar
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
finalizeSelection();
notifySelectionChanged();
invalidate();
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::leftButtonDoubleClick(const TPointD &pos,
const TMouseEvent &e) {
TVectorImageP vi = getImage(false);
if (!vi) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_strokeSelectionType.getIndex() == POLYLINE_SELECTION_IDX &&
!m_polyline.empty()) {
closePolyline(pos);
selectRegionVectorImage(m_includeIntersection.getValue());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_selecting = false;
invalidate();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int strokeIndex;
if ((strokeIndex = vi->pickGroup(pos)) >= 0) {
if (vi->canEnterGroup(strokeIndex) && m_canEnterGroup) {
if (vi->enterGroup(strokeIndex)) {
clearSelectedStrokes();
TUndoManager::manager()->add(new UndoEnterGroup(vi, strokeIndex));
}
}
} else if ((strokeIndex = vi->exitGroup()) >= 0)
TUndoManager::manager()->add(new UndoExitGroup(vi, strokeIndex));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
finalizeSelection();
invalidate();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::leftButtonDrag(const TPointD &pos,
const TMouseEvent &e) {
if (m_dragTool) {
if (!m_strokeSelection.isEditable()) return;
2016-06-15 18:43:10 +12:00
m_dragTool->leftButtonDrag(pos, e);
return;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TVectorImageP vi = getImage(false);
if (!vi) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
double pixelSize = getPixelSize();
TTool::Application *app = TTool::getApplication();
if (!app || m_justSelected || !m_selecting ||
tdistance2(pos, m_curPos) < 9.0 * pixelSize * pixelSize)
return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_curPos = pos;
2016-03-19 06:57:51 +13:00
2017-11-14 16:38:01 +13:00
if (m_strokeSelectionType.getIndex() == FREEHAND_SELECTION_IDX) {
2016-06-15 18:43:10 +12:00
freehandDrag(pos);
2017-11-14 16:38:01 +13:00
invalidate();
} else if (m_strokeSelectionType.getIndex() == RECT_SELECTION_IDX) {
2016-06-15 18:43:10 +12:00
bool selectOverlappingStroke = (m_firstPos.x > pos.x);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TRectD rect(m_firstPos, pos);
m_selectingRect = rect;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::set<int> oldSelection;
if (m_shiftPressed) oldSelection = m_strokeSelection.getSelection();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
clearSelectedStrokes();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
QMutexLocker lock(vi->getMutex());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_strokeSelection.setImage(vi); // >_< Shouldn't be done here...?
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bool selectionChanged = false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int s, sCount = vi->getStrokeCount();
for (s = 0; s != sCount; ++s) {
if (!vi->isEnteredGroupStroke(s)) continue;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TStroke *stroke = vi->getStroke(s);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_strokeSelection.isSelected(s)) continue;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bool inSelection = selectOverlappingStroke
? rect.overlaps(stroke->getBBox())
: rect.contains(stroke->getBBox());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (inSelection || (m_shiftPressed && oldSelection.count(s)))
selectionChanged = (selectStroke(s, false) || selectionChanged);
}
2016-03-19 06:57:51 +13:00
2019-11-01 07:29:23 +13:00
if (selectionChanged) finalizeSelection();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
invalidate();
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
void VectorSelectionTool::leftButtonDown(const TPointD &pos,
const TMouseEvent &e) {
if (getViewer() && getViewer()->getGuidedStrokePickerMode()) {
getViewer()->doPickGuideStroke(pos);
return;
}
SelectionTool::leftButtonDown(pos, e);
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::leftButtonUp(const TPointD &pos,
const TMouseEvent &e) {
m_leftButtonMousePressed = false;
m_shiftPressed = false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_dragTool) {
if (!m_strokeSelection.isEditable()) {
delete m_dragTool;
m_dragTool = 0;
return;
}
2016-06-15 18:43:10 +12:00
m_dragTool->leftButtonUp(pos, e);
delete m_dragTool;
m_dragTool = 0;
invalidate();
return;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!m_selecting) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Complete selection
TVectorImageP vi = getImage(false);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (vi) {
if (m_strokeSelectionType.getIndex() == RECT_SELECTION_IDX)
notifySelectionChanged();
else if (m_strokeSelectionType.getIndex() == FREEHAND_SELECTION_IDX) {
QMutexLocker lock(vi->getMutex());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
closeFreehand(pos);
2016-03-19 06:57:51 +13:00
if (m_stroke->getControlPointCount() > 3)
selectRegionVectorImage(m_includeIntersection.getValue());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
delete m_stroke; // >:(
m_stroke = 0;
2017-11-14 16:38:01 +13:00
m_track.clear();
2016-06-15 18:43:10 +12:00
}
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_selecting = false;
m_justSelected = false;
invalidate();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::addContextMenuItems(QMenu *menu) {
menu->addAction(CommandManager::instance()->getAction(MI_RemoveEndpoints));
menu->addSeparator();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_strokeSelection.getGroupCommand()->addMenuItems(menu);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::drawInLevelType(const TVectorImage &vi) {
glPushMatrix();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
FourPoints bbox = getBBox();
if (!bbox.isEmpty()) {
TPixel32 frameColor(127, 127, 127);
double pixelSize = getPixelSize();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
drawFourPoints(bbox, TPixel32::Black, 0x5555, true);
drawFourPoints(bbox.enlarge(pixelSize * (-4)), frameColor, 0xffff, true);
drawFourPoints(bbox.enlarge(pixelSize * (-2)), frameColor, 0x8888, true);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
drawCommandHandle(&vi);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
drawSelectedStrokes(vi);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_selecting && !m_selectingRect.isEmpty()) drawRectSelection(&vi);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_strokeSelectionType.getIndex() == POLYLINE_SELECTION_IDX)
drawPolylineSelection();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
glPopMatrix();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::drawSelectedStrokes(const TVectorImage &vi) {
glEnable(GL_LINE_STIPPLE);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
double pixelSize = getPixelSize();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int s, sCount = vi.getStrokeCount();
for (s = 0; s != sCount; ++s) {
if (m_strokeSelection.isSelected(s)) {
TStroke *stroke = vi.getStroke(s);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
glLineStipple(1, 0xF0F0);
tglColor(TPixel32::Black);
drawStrokeCenterline(*stroke, pixelSize);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
glLineStipple(1, 0x0F0F);
tglColor(TPixel32::White);
drawStrokeCenterline(*stroke, pixelSize);
}
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
glDisable(GL_LINE_STIPPLE);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::drawGroup(const TVectorImage &vi) {
int s, sCount = vi.getStrokeCount();
for (s = 0; s != sCount; ++s) {
if (m_strokeSelection.isSelected(s)) {
TRectD gBox;
if (getGroupBBox(vi, s, gBox)) drawRect(gBox, TPixel::Blue, 0xffff);
}
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::draw() {
TVectorImageP vi = getImage(false);
if (!vi) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (isLevelType() || isSelectedFramesType()) {
drawInLevelType(*vi);
return;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
glPushMatrix();
2016-03-19 06:57:51 +13:00
2019-10-15 18:51:29 +13:00
if (m_strokeSelection.isEmpty()) // o_o WTF!?
m_bboxs.clear(); //
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// common draw
if (getBBoxsCount() > 0) drawCommandHandle(vi.getPointer());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_selecting && !m_selectingRect.isEmpty())
drawRectSelection(vi.getPointer());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TRectD bbox = vi->getBBox();
TPixel32 frameColor(140, 140, 140);
tglColor(frameColor);
drawRect(bbox, frameColor, 0x5555, true);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
drawSelectedStrokes(*vi);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_strokeSelectionType.getIndex() == POLYLINE_SELECTION_IDX)
drawPolylineSelection();
2017-11-14 16:38:01 +13:00
else if (m_strokeSelectionType.getIndex() == FREEHAND_SELECTION_IDX)
drawFreehandSelection();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_levelSelection.isEmpty()) drawGroup(*vi);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
glPopMatrix();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TSelection *VectorSelectionTool::getSelection() {
TImage *image = getImage(false);
TVectorImageP vi = image;
if (!vi) return 0;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return &m_strokeSelection;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool VectorSelectionTool::isSelectionEmpty() {
TVectorImageP vi =
getImage(false); // We want at least current image to be visible.
if (!vi) // This is somewhat in line to preventing tool actions
return true; // on non-visible levels.
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return m_strokeSelection
.isEmpty(); // Same here, something should be visibly selected.
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::computeBBox() {
m_bboxs.clear();
2019-11-01 07:29:23 +13:00
if (canResetCenter()) m_centers.clear();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TVectorImageP vi = getImage(false);
if (!vi) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (isLevelType() || isSelectedFramesType()) {
double maxThickness = 0;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Calculate current image's bbox - it is always the first one (index 0)
if (vi && (isLevelType() || m_selectedFrames.count(getCurrentFid()) > 0)) {
FourPoints bbox =
getFourPointsFromVectorImage(vi, selectedStyles(), maxThickness);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_bboxs.push_back(bbox);
m_centers.push_back((bbox.getP00() + bbox.getP11()) * 0.5);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// All subsequent SELECTED frames' bboxes come sequentially starting from 1
if (TXshSimpleLevel *level =
TTool::getApplication()->getCurrentLevel()->getSimpleLevel()) {
std::vector<TFrameId> fids;
level->getFids(fids);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
for (int i = 0; i < (int)fids.size(); ++i) {
if (getCurrentFid() == fids[i] ||
(isSelectedFramesType() && m_selectedFrames.count(fids[i]) == 0))
continue;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TVectorImageP vi = level->getFrame(fids[i], false);
if (!vi) continue;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
FourPoints p =
getFourPointsFromVectorImage(vi, selectedStyles(), maxThickness);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_bboxs.push_back(p);
m_centers.push_back(0.5 * (p.getP00() + p.getP11()));
2016-06-15 18:43:10 +12:00
m_deformValues.m_maxSelectionThickness = maxThickness;
}
}
} else if (vi) {
TRectD newBbox;
double maxThickness = 0;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
for (int i = 0; i < (int)vi->getStrokeCount(); i++) {
TStroke *stroke = vi->getStroke(i);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_strokeSelection.isSelected(i)) {
newBbox += stroke->getBBox();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
for (int j = 0; j < stroke->getControlPointCount(); j++) {
double thick = stroke->getControlPoint(j).thick;
if (maxThickness < thick) maxThickness = thick;
}
}
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_deformValues.m_maxSelectionThickness = maxThickness;
FourPoints bbox;
bbox = newBbox;
m_bboxs.push_back(bbox);
2019-11-01 07:29:23 +13:00
if (canResetCenter())
2019-10-15 18:51:29 +13:00
m_centers.push_back(0.5 * (bbox.getP11() + bbox.getP00()));
2016-06-15 18:43:10 +12:00
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
++m_selectionCount;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool VectorSelectionTool::selectStroke(int index, bool toggle) {
TVectorImageP vi = getImage(false);
assert(vi);
assert(m_strokeSelection.getImage() == vi);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!vi->isEnteredGroupStroke(index)) // If index is not in current group
return false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (index < 0 || index >= int(vi->getStrokeCount())) // Should be asserted...
return false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bool wasSelected = m_strokeSelection.isSelected(index),
selectState = !(wasSelected && toggle);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// (De)Select additional strokes related to ours
std::set<int> &selectedStyles = this->selectedStyles();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (isSameStyleType()) // Color selection
{
TStroke *refStroke = vi->getStroke(index);
assert(refStroke);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int style = refStroke->getStyle();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (selectState)
selectedStyles.insert(style);
else
selectedStyles.erase(style);
} else if (vi->isStrokeGrouped(index) &&
vi->selectable(index)) // Group selection
{
int s, sCount = vi->getStrokeCount();
for (s = 0; s != sCount; ++s) {
if (vi->selectable(s) && vi->sameSubGroup(index, s))
m_strokeSelection.select(s, selectState);
}
} else // Single stroke selection
m_strokeSelection.select(index, selectState);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return (selectState != wasSelected);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::onActivate() {
if (m_firstTime) {
m_includeIntersection.setValue(l_strokeSelectIncludeIntersection ? 1 : 0);
2016-06-15 18:43:10 +12:00
m_constantThickness.setValue(l_strokeSelectConstantThickness ? 1 : 0);
m_strokeSelection.setSceneHandle(
TTool::getApplication()->getCurrentScene());
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
SelectionTool::onActivate();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::onDeactivate() {
if (isLevelType()) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
SelectionTool::onDeactivate();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::doOnActivate() {
TVectorImageP vi = getImage(false);
m_strokeSelection.setImage(vi);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
updateSelectionTarget();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
finalizeSelection();
invalidate();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::onImageChanged() {
TVectorImageP vi = getImage(false);
TVectorImageP selectedImg = m_strokeSelection.getImage();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (vi != selectedImg) {
m_strokeSelection.selectNone();
m_strokeSelection.setImage(vi);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!(vi && selectedImg) // Retain the styles selection ONLY
|| vi->getPalette() !=
selectedImg->getPalette()) // if palettes still match
2016-06-15 18:43:10 +12:00
selectedStyles().clear();
} else {
// Remove any eventual stroke index outside the valid range
if (!m_strokeSelection.isEmpty()) {
const std::set<int> &indexes = m_strokeSelection.getSelection();
int strokesCount = selectedImg->getStrokeCount();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::set<int>::const_iterator it;
for (it = indexes.begin(); it != indexes.end(); ++it) {
int index = *it;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (index >= strokesCount) m_strokeSelection.select(index, false);
}
}
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
finalizeSelection();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
bool VectorSelectionTool::isDragging() const {
return m_enabled && m_leftButtonMousePressed;
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::doOnDeactivate() {
m_strokeSelection.selectNone();
m_levelSelection.selectNone();
m_deformValues.reset();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_polyline.clear();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TTool::getApplication()->getCurrentSelection()->setSelection(0);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
invalidate();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TPropertyGroup *VectorSelectionTool::getProperties(int idx) {
switch (idx) {
case 0:
return &m_prop;
case 1:
return &m_outlineProps;
default:
return 0;
};
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool VectorSelectionTool::onPropertyChanged(std::string propertyName) {
if (!m_strokeSelection.isEditable()) return false;
2016-06-15 18:43:10 +12:00
if (SelectionTool::onPropertyChanged(propertyName)) return true;
2016-03-19 06:57:51 +13:00
if (propertyName == m_includeIntersection.getName())
l_strokeSelectIncludeIntersection = (int)(m_includeIntersection.getValue());
2016-06-15 18:43:10 +12:00
if (propertyName == m_constantThickness.getName())
l_strokeSelectConstantThickness = (int)(m_constantThickness.getValue());
else if (propertyName == m_selectionTarget.getName())
doOnActivate();
else if (propertyName == m_capStyle.getName()) {
if (m_strokeSelection.isEmpty()) return true;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TXshSimpleLevel *level =
TTool::getApplication()->getCurrentLevel()->getSimpleLevel();
UndoChangeOutlineStyle *undo =
new UndoChangeOutlineStyle(level, getCurrentFid(), this);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int newCapStyle = m_capStyle.getIndex();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TVectorImageP vi = m_strokeSelection.getImage();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const std::set<int> &indices = m_strokeSelection.getSelection();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::set<int>::iterator it;
for (it = indices.begin(); it != indices.end(); ++it) {
TStroke *stroke = vi->getStroke(*it);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
stroke->outlineOptions().m_capStyle =
(TStroke::OutlineOptions::CapStyle)newCapStyle;
stroke->invalidate();
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
computeBBox();
invalidate();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
level->setDirtyFlag(true);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
undo->registerStrokes();
TUndoManager::manager()->add(undo);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
notifyImageChanged();
} else if (propertyName == m_joinStyle.getName()) {
if (m_strokeSelection.isEmpty()) return true;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TXshSimpleLevel *level =
TTool::getApplication()->getCurrentLevel()->getSimpleLevel();
UndoChangeOutlineStyle *undo =
new UndoChangeOutlineStyle(level, getCurrentFid(), this);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int newJoinStyle = m_joinStyle.getIndex();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TVectorImageP vi = m_strokeSelection.getImage();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const std::set<int> &indices = m_strokeSelection.getSelection();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::set<int>::iterator it;
for (it = indices.begin(); it != indices.end(); ++it) {
TStroke *stroke = vi->getStroke(*it);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
stroke->outlineOptions().m_joinStyle =
(TStroke::OutlineOptions::JoinStyle)newJoinStyle;
stroke->invalidate();
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
computeBBox();
invalidate();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
level->setDirtyFlag(true);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
undo->registerStrokes();
TUndoManager::manager()->add(undo);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
notifyImageChanged();
} else if (propertyName == m_miterJoinLimit.getName()) {
if (m_strokeSelection.isEmpty()) return true;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TXshSimpleLevel *level =
TTool::getApplication()->getCurrentLevel()->getSimpleLevel();
UndoChangeOutlineStyle *undo =
new UndoChangeOutlineStyle(level, getCurrentFid(), this);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int upper = m_miterJoinLimit.getValue();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TVectorImageP vi = m_strokeSelection.getImage();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const std::set<int> &indices = m_strokeSelection.getSelection();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::set<int>::iterator it;
for (it = indices.begin(); it != indices.end(); ++it) {
TStroke *stroke = vi->getStroke(*it);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
stroke->outlineOptions().m_miterUpper = upper;
stroke->invalidate();
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
computeBBox();
invalidate();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
level->setDirtyFlag(true);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
undo->registerStrokes();
TUndoManager::manager()->add(undo);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
notifyImageChanged();
} else
return false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return true;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::selectionOutlineStyle(int &capStyle, int &joinStyle) {
// Check the selection's outline style group validity
const std::set<int> &selection = m_strokeSelection.getSelection();
if (selection.empty()) {
capStyle = joinStyle = -1;
return;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TVectorImageP vi = m_strokeSelection.getImage();
const TStroke::OutlineOptions &beginOptions =
vi->getStroke(*selection.begin())->outlineOptions();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
capStyle = beginOptions.m_capStyle;
joinStyle = beginOptions.m_joinStyle;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::set<int>::const_iterator it;
for (it = selection.begin(); it != selection.end(); ++it) {
const TStroke::OutlineOptions &options =
vi->getStroke(*it)->outlineOptions();
2016-03-19 06:57:51 +13:00
if (capStyle != options.m_capStyle) capStyle = -1;
2016-06-15 18:43:10 +12:00
if (joinStyle != options.m_joinStyle) joinStyle = -1;
if (capStyle < 0 && joinStyle < 0) return;
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
void VectorSelectionTool::selectRegionVectorImage(bool includeIntersect) {
2016-06-15 18:43:10 +12:00
if (!m_stroke) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TVectorImageP vi(getImage(false));
if (!vi) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_strokeSelection.setImage(vi);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TVectorImage selectImg;
selectImg.addStroke(new TStroke(*m_stroke));
selectImg.findRegions();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int sCount = int(vi->getStrokeCount()),
rCount = int(selectImg.getRegionCount());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bool selectionChanged = false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
for (int s = 0; s != sCount; ++s) {
TStroke *currentStroke = vi->getStroke(s);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
for (int r = 0; r != rCount; ++r) {
TRegion *region = selectImg.getRegion(r);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (region->contains(*currentStroke, true))
selectionChanged = selectStroke(s, false) || selectionChanged;
}
if (includeIntersect) {
std::vector<DoublePair> intersections;
intersect(m_stroke, currentStroke, intersections, false);
if (intersections.size() > 0) {
selectionChanged = selectStroke(s, false) || selectionChanged;
}
}
2016-06-15 18:43:10 +12:00
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (selectionChanged) {
finalizeSelection();
notifySelectionChanged();
invalidate();
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::updateAction(TPointD pos, const TMouseEvent &e) {
TImageP image = getImage(false);
TVectorImageP vi = (TVectorImageP)image;
if (!vi) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
SelectionTool::updateAction(pos, e);
if (m_what != Outside || m_cursorId != ToolCursor::StrokeSelectCursor) return;
2016-03-19 06:57:51 +13:00
if (!m_strokeSelection.isEditable()) return;
2016-06-15 18:43:10 +12:00
FourPoints bbox = getBBox();
UINT index = 0;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if ((isLevelType() &&
bbox.contains(pos)) // What about isSelectedFramesType()??
2019-09-27 15:14:14 +12:00
|| (getStrokeIndexFromPos(index, vi, pos, getPixelSize(),
getViewer()->getViewMatrix()) &&
2016-06-15 18:43:10 +12:00
m_strokeSelection.isSelected(index))) {
m_what = Inside;
m_cursorId =
isLevelType() ? ToolCursor::LevelSelectCursor : ToolCursor::MoveCursor;
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void VectorSelectionTool::onSelectedFramesChanged() {
if (isSelectedFramesType()) // False also in case m_levelSelection is not
// current
finalizeSelection();
2016-03-19 06:57:51 +13:00
}