tahoma2d/toonz/sources/tnztools/geometrictool.cpp

2411 lines
81 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include "toonz/tpalettehandle.h"
#include "tools/toolhandle.h"
#include "tools/toolcommandids.h"
2016-03-19 06:57:51 +13:00
#include "toonz/tobjecthandle.h"
#include "toonz/txsheethandle.h"
#include "toonz/txshlevelhandle.h"
#include "toonz/tframehandle.h"
#include "tools/tool.h"
#include "tcolorstyles.h"
#include "tpalette.h"
#include "tools/toolutils.h"
#include "tstroke.h"
#include "tmathutil.h"
#include "tools/cursors.h"
#include "tproperty.h"
#include "ttoonzimage.h"
#include "drawutil.h"
#include "tvectorimage.h"
#include "tenv.h"
#include "bluredbrush.h"
#include "toonz/ttileset.h"
#include "toonz/toonzimageutils.h"
#include "toonz/tstageobject.h"
#include "toonz/tstageobjectspline.h"
#include "toonzqt/imageutils.h"
#include "toonzqt/dvdialog.h"
#include "toonz/trasterimageutils.h"
#include "toonz/preferences.h"
#include "historytypes.h"
#include "toonzvectorbrushtool.h"
2016-03-19 06:57:51 +13:00
// For Qt translation support
#include <QCoreApplication>
using namespace ToolUtils;
class GeometricTool;
class MultiLinePrimitive;
TEnv::DoubleVar GeometricSize("InknpaintGeometricSize", 1);
TEnv::DoubleVar GeometricRasterSize("InknpaintGeometricRasterSize", 1);
TEnv::StringVar GeometricType("InknpaintGeometricType", "Rectangle");
TEnv::IntVar GeometricEdgeCount("InknpaintGeometricEdgeCount", 3);
TEnv::IntVar GeometricSelective("InknpaintGeometricSelective", 0);
TEnv::IntVar GeometricGroupIt("InknpaintGeometricGroupIt", 0);
TEnv::IntVar GeometricAutofill("InknpaintGeometricAutofill", 0);
TEnv::IntVar GeometricPencil("InknpaintGeometricPencil", 0);
TEnv::DoubleVar GeometricBrushHardness("InknpaintGeometricHardness", 100);
TEnv::DoubleVar GeometricOpacity("InknpaintGeometricOpacity", 100);
TEnv::IntVar GeometricCapStyle("InknpaintGeometricCapStyle", 0);
TEnv::IntVar GeometricJoinStyle("InknpaintGeometricJoinStyle", 0);
TEnv::IntVar GeometricMiterValue("InknpaintGeometricMiterValue", 4);
TEnv::IntVar GeometricSnap("InknpaintGeometricSnap", 0);
TEnv::IntVar GeometricSnapSensitivity("InknpaintGeometricSnapSensitivity", 0);
2016-03-19 06:57:51 +13:00
//-------------------------------------------------------------------
#define ROUNDC_WSTR L"round_cap"
#define BUTT_WSTR L"butt_cap"
#define PROJECTING_WSTR L"projecting_cap"
#define ROUNDJ_WSTR L"round_join"
#define BEVEL_WSTR L"bevel_join"
#define MITER_WSTR L"miter_join"
#define LOW_WSTR L"Low"
#define MEDIUM_WSTR L"Medium"
#define HIGH_WSTR L"High"
const double SNAPPING_LOW = 5.0;
const double SNAPPING_MEDIUM = 25.0;
const double SNAPPING_HIGH = 100.0;
2016-03-19 06:57:51 +13:00
//=============================================================================
// Utility Functions
//-----------------------------------------------------------------------------
static TPointD rectify(const TPointD &oldPos, const TPointD &pos) {
2016-06-15 18:43:10 +12:00
const double h = sqrt(2.0) / 2.0;
const TPointD directions[] = {TPointD(1, 0), TPointD(h, h), TPointD(0, 1),
TPointD(-h, h), TPointD(-1, 0), TPointD(-h, -h),
TPointD(0, -1), TPointD(h, -h)};
2019-10-11 15:57:49 +13:00
TPointD v = pos - oldPos;
int j = 0;
double bestValue = v * directions[j];
2016-06-15 18:43:10 +12:00
for (int k = 1; k < 8; k++) {
double value = v * directions[k];
if (value > bestValue) {
bestValue = value;
j = k;
}
}
return oldPos + bestValue * directions[j];
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
static TPointD computeSpeed(TPointD p0, TPointD p1, double factor) {
2016-06-15 18:43:10 +12:00
TPointD d = p1 - p0;
return (d == TPointD()) ? TPointD() : d * (factor / norm(d));
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-08-04 19:23:36 +12:00
static TRect drawBluredBrush(const TRasterImageP &ri, TStroke *stroke,
int thick, double hardness, double opacity) {
2016-06-15 18:43:10 +12:00
TStroke *s = new TStroke(*stroke);
TPointD riCenter = ri->getRaster()->getCenterD();
s->transform(TTranslation(riCenter));
int styleId = s->getStyle();
TPalette *plt = ri->getPalette();
TPixel32 color = plt->getStyle(styleId)->getMainColor();
TRectD bbox = s->getBBox();
TRect bboxI = convert(bbox).enlarge(1);
TRect rectRender = bboxI * ri->getRaster()->getBounds();
if (rectRender.isEmpty()) return TRect();
TRaster32P workRas(ri->getRaster()->getSize());
TRaster32P backupRas = ri->getRaster()->clone();
workRas->clear();
QRadialGradient brushPad = ToolUtils::getBrushPad(thick, hardness);
BluredBrush brush(workRas, thick, brushPad, false);
int i, chunkCount = s->getChunkCount();
for (i = 0; i < chunkCount; i++) {
const TThickQuadratic *q = s->getChunk(i);
std::vector<TThickPoint> points;
points.push_back(q->getThickP0());
points.push_back(q->getThickP1());
points.push_back(q->getThickP2());
// i punti contenuti nello stroke sembra che haano la tickness pari al
// raggio e non al diametro del punto!
points[0].thick *= 2;
points[1].thick *= 2;
points[2].thick *= 2;
TRect chunkBox = brush.getBoundFromPoints(points);
brush.addArc(points[0], points[1], points[2], 1, 1);
brush.updateDrawing(ri->getRaster(), backupRas, color, chunkBox, opacity);
}
delete s;
return rectRender;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
static TRect drawBluredBrush(const TToonzImageP &ti, TStroke *stroke, int thick,
double hardness, bool selective) {
2016-06-15 18:43:10 +12:00
TStroke *s = new TStroke(*stroke);
TPointD riCenter = ti->getRaster()->getCenterD();
s->transform(TTranslation(riCenter));
int styleId = s->getStyle();
TRectD bbox = s->getBBox();
TRect bboxI = convert(bbox).enlarge(1);
TRect rectRender = bboxI * ti->getRaster()->getBounds();
if (rectRender.isEmpty()) return TRect();
TRaster32P workRas(ti->getRaster()->getSize());
TRasterCM32P backupRas = ti->getRaster()->clone();
workRas->clear();
QRadialGradient brushPad = ToolUtils::getBrushPad(thick, hardness);
BluredBrush brush(workRas, thick, brushPad, false);
int i, chunkCount = s->getChunkCount();
for (i = 0; i < chunkCount; i++) {
const TThickQuadratic *q = s->getChunk(i);
std::vector<TThickPoint> points;
points.push_back(q->getThickP0());
points.push_back(q->getThickP1());
points.push_back(q->getThickP2());
// i punti contenuti nello stroke sembra che haano la tickness pari al
// raggio e non al diametro del punto!
points[0].thick *= 2;
points[1].thick *= 2;
points[2].thick *= 2;
TRect chunkBox = brush.getBoundFromPoints(points);
brush.addArc(points[0], points[1], points[2], 1, 1);
brush.updateDrawing(ti->getRaster(), backupRas, chunkBox, styleId,
selective);
}
delete s;
return rectRender;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
class MultilinePrimitiveUndo final : public TUndo {
2016-06-15 18:43:10 +12:00
std::vector<TPointD> m_oldVertex;
std::vector<TPointD> m_newVertex;
MultiLinePrimitive *m_tool;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
MultilinePrimitiveUndo(const std::vector<TPointD> &vertex,
MultiLinePrimitive *tool)
: TUndo(), m_oldVertex(vertex), m_tool(tool), m_newVertex() {}
~MultilinePrimitiveUndo() {}
2016-06-19 20:06:29 +12:00
void undo() const override;
void redo() const override;
2016-06-15 18:43:10 +12:00
void setNewVertex(const std::vector<TPointD> &vertex) {
m_newVertex = vertex;
}
2016-06-19 20:06:29 +12:00
int getSize() const override { return sizeof(this); }
2016-06-15 18:43:10 +12:00
QString getToolName();
2016-06-19 20:06:29 +12:00
int getHistoryType() override { return HistoryType::GeometricTool; }
2016-03-19 06:57:51 +13:00
};
//-----------------------------------------------------------------------------
class FullColorBluredPrimitiveUndo final : public UndoFullColorPencil {
2016-06-15 18:43:10 +12:00
int m_thickness;
double m_hardness;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
FullColorBluredPrimitiveUndo(TXshSimpleLevel *level, const TFrameId &frameId,
TStroke *stroke, int thickness, double hardness,
double opacity, bool doAntialias,
bool createdFrame, bool createdLevel)
: UndoFullColorPencil(level, frameId, stroke, opacity, doAntialias,
createdFrame, createdLevel)
, m_thickness(thickness)
, m_hardness(hardness) {
TRasterP raster = getImage()->getRaster();
TDimension d = raster->getSize();
m_tiles = new TTileSetFullColor(d);
TRect rect = convert(stroke->getBBox()) +
TPoint((int)(d.lx * 0.5), (int)(d.ly * 0.5));
m_tiles->add(raster, rect.enlarge(2));
m_stroke = new TStroke(*stroke);
}
~FullColorBluredPrimitiveUndo() {}
2016-06-19 20:06:29 +12:00
void redo() const override {
2016-06-15 18:43:10 +12:00
insertLevelAndFrameIfNeeded();
TRasterImageP ri = getImage();
if (!ri) return;
drawBluredBrush(ri, m_stroke, m_thickness, m_hardness, m_opacity);
TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
notifyImageChanged();
}
2016-06-20 14:23:05 +12:00
int getSize() const override {
return UndoFullColorPencil::getSize() + sizeof(this);
}
2016-03-19 06:57:51 +13:00
};
//-----------------------------------------------------------------------------
/*-- Hardness<100 のときの GeometricToolのUndo --*/
class CMBluredPrimitiveUndo final : public UndoRasterPencil {
2016-06-15 18:43:10 +12:00
int m_thickness;
double m_hardness;
bool m_selective;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
CMBluredPrimitiveUndo(TXshSimpleLevel *level, const TFrameId &frameId,
TStroke *stroke, int thickness, double hardness,
bool selective, bool doAntialias, bool createdFrame,
bool createdLevel, std::string primitiveName)
: UndoRasterPencil(level, frameId, stroke, selective, false, doAntialias,
createdFrame, createdLevel, primitiveName)
, m_thickness(thickness)
, m_hardness(hardness)
, m_selective(selective) {
TRasterCM32P raster = getImage()->getRaster();
TDimension d = raster->getSize();
m_tiles = new TTileSetCM32(d);
TRect rect = convert(stroke->getBBox()) +
TPoint((int)(d.lx * 0.5), (int)(d.ly * 0.5));
m_tiles->add(raster, rect.enlarge(2));
m_stroke = new TStroke(*stroke);
}
~CMBluredPrimitiveUndo() {}
2016-06-19 20:06:29 +12:00
void redo() const override {
2016-06-15 18:43:10 +12:00
insertLevelAndFrameIfNeeded();
TToonzImageP ti = getImage();
if (!ti) return;
drawBluredBrush(ti, m_stroke, m_thickness, m_hardness, m_selective);
TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
notifyImageChanged();
}
2016-06-20 14:23:05 +12:00
int getSize() const override {
return UndoRasterPencil::getSize() + sizeof(this);
}
2016-03-19 06:57:51 +13:00
};
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
class PrimitiveParam {
Q_DECLARE_TR_FUNCTIONS(PrimitiveParam)
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
TDoubleProperty m_toolSize;
TIntProperty m_rasterToolSize;
TDoubleProperty m_opacity;
TDoubleProperty m_hardness;
TEnumProperty m_type;
TIntProperty m_edgeCount;
TBoolProperty m_autogroup;
TBoolProperty m_autofill;
TBoolProperty m_selective;
TBoolProperty m_pencil;
TEnumProperty m_capStyle;
TEnumProperty m_joinStyle;
TIntProperty m_miterJoinLimit;
TBoolProperty m_snap;
TEnumProperty m_snapSensitivity;
2016-06-15 18:43:10 +12:00
TPropertyGroup m_prop[2];
int m_targetType;
// for snapping
int m_strokeIndex1;
double m_w1, m_pixelSize, m_currThickness, m_minDistance2;
bool m_foundSnap = false;
TPointD m_snapPoint;
2016-06-15 18:43:10 +12:00
PrimitiveParam(int targetType)
: m_type("Shape:") // "W_ToolOptions_ShapeType")
, m_toolSize("Size:", 0, 100,
1) // "W_ToolOptions_ShapeThickness", 0,30,1)
, m_rasterToolSize("Size:", 1, 100, 1)
, m_opacity("Opacity:", 0, 100, 100)
, m_hardness("Hardness:", 0, 100, 100)
, m_edgeCount("Polygon Sides:", 3, 15, 3)
, m_autogroup("Auto Group", false)
, m_autofill("Auto Fill", false)
, m_selective("Selective", false)
, m_pencil("Pencil Mode", false)
, m_capStyle("Cap")
, m_joinStyle("Join")
, m_miterJoinLimit("Miter:", 0, 100, 4)
, m_snap("Snap", false)
, m_snapSensitivity("Sensitivity:")
2016-06-15 18:43:10 +12:00
, m_targetType(targetType) {
if (targetType & TTool::Vectors) m_prop[0].bind(m_toolSize);
if (targetType & TTool::ToonzImage || targetType & TTool::RasterImage) {
m_prop[0].bind(m_rasterToolSize);
m_prop[0].bind(m_hardness);
}
if (targetType & TTool::RasterImage) m_prop[0].bind(m_opacity);
m_prop[0].bind(m_type);
m_prop[0].bind(m_edgeCount);
if (targetType & TTool::Vectors) {
m_prop[0].bind(m_autogroup);
m_prop[0].bind(m_autofill);
m_prop[0].bind(m_snap);
m_snap.setId("Snap");
m_prop[0].bind(m_snapSensitivity);
m_snapSensitivity.addValue(LOW_WSTR);
m_snapSensitivity.addValue(MEDIUM_WSTR);
m_snapSensitivity.addValue(HIGH_WSTR);
m_snapSensitivity.setId("SnapSensitivity");
2016-06-15 18:43:10 +12:00
}
if (targetType & TTool::ToonzImage) {
m_prop[0].bind(m_selective);
m_prop[0].bind(m_pencil);
m_pencil.setId("PencilMode");
}
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_prop[1].bind(m_capStyle);
m_prop[1].bind(m_joinStyle);
m_prop[1].bind(m_miterJoinLimit);
m_selective.setId("Selective");
m_autogroup.setId("AutoGroup");
m_autofill.setId("Autofill");
m_type.setId("GeometricShape");
m_edgeCount.setId("GeometricEdge");
}
void updateTranslation() {
m_type.setQStringName(tr("Shape:"));
m_type.setItemUIName(L"Rectangle", tr("Rectangle"));
m_type.setItemUIName(L"Circle", tr("Circle"));
m_type.setItemUIName(L"Ellipse", tr("Ellipse"));
m_type.setItemUIName(L"Line", tr("Line"));
m_type.setItemUIName(L"Polyline", tr("Polyline"));
m_type.setItemUIName(L"Arc", tr("Arc"));
m_type.setItemUIName(L"Polygon", tr("Polygon"));
2016-06-15 18:43:10 +12:00
m_toolSize.setQStringName(tr("Size:"));
m_rasterToolSize.setQStringName(tr("Thickness:"));
m_opacity.setQStringName(tr("Opacity:"));
m_hardness.setQStringName(tr("Hardness:"));
m_edgeCount.setQStringName(tr("Polygon Sides:"));
m_autogroup.setQStringName(tr("Auto Group"));
m_autofill.setQStringName(tr("Auto Fill"));
m_selective.setQStringName(tr("Selective"));
m_pencil.setQStringName(tr("Pencil Mode"));
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:"));
m_snap.setQStringName(tr("Snap"));
m_snapSensitivity.setQStringName(tr(""));
if (m_targetType & TTool::Vectors) {
m_snapSensitivity.setItemUIName(LOW_WSTR, tr("Low"));
m_snapSensitivity.setItemUIName(MEDIUM_WSTR, tr("Med"));
m_snapSensitivity.setItemUIName(HIGH_WSTR, tr("High"));
}
2016-06-15 18:43:10 +12:00
}
2016-03-19 06:57:51 +13:00
};
//=============================================================================
// Abstract Class Primitive
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
class Primitive {
2016-03-19 06:57:51 +13:00
protected:
2016-06-15 18:43:10 +12:00
bool m_isEditing, m_rasterTool, m_isPrompting;
GeometricTool *m_tool;
PrimitiveParam *m_param;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
Primitive(PrimitiveParam *param, GeometricTool *tool, bool rasterTool)
: m_param(param)
, m_tool(tool)
, m_isEditing(false)
, m_rasterTool(rasterTool)
, m_isPrompting(false) {}
double getThickness() const {
if (m_rasterTool)
return m_param->m_rasterToolSize.getValue() * 0.5;
else
return m_param->m_toolSize.getValue() * 0.5;
}
void setIsPrompting(bool value) { m_isPrompting = value; }
virtual std::string getName() const = 0;
virtual ~Primitive() {}
virtual void leftButtonDown(const TPointD &p, const TMouseEvent &e){};
virtual void leftButtonDrag(const TPointD &p, const TMouseEvent &e){};
virtual void leftButtonUp(const TPointD &p, const TMouseEvent &e){};
virtual void leftButtonDoubleClick(const TPointD &, const TMouseEvent &e){};
virtual void rightButtonDown(const TPointD &p, const TMouseEvent &e){};
virtual void mouseMove(const TPointD &p, const TMouseEvent &e){};
2018-02-19 19:34:13 +13:00
virtual bool keyDown(QKeyEvent *event) { return false; }
2016-06-15 18:43:10 +12:00
virtual void onEnter(){};
virtual void draw(){};
virtual void onActivate(){};
virtual void onDeactivate(){};
virtual void onImageChanged(){};
TPointD calculateSnap(TPointD pos);
void drawSnap();
TPointD getSnap(TPointD pos);
void resetSnap();
TPointD checkGuideSnapping(TPointD pos);
2016-06-15 18:43:10 +12:00
virtual TStroke *makeStroke() const = 0;
2016-03-19 06:57:51 +13:00
};
//-----------------------------------------------------------------------------
void Primitive::resetSnap() {
m_param->m_strokeIndex1 = -1;
m_param->m_w1 = -1;
m_param->m_foundSnap = false;
}
//-----------------------------------------------------------------------------
TPointD Primitive::calculateSnap(TPointD pos) {
m_param->m_foundSnap = false;
if (Preferences::instance()->getVectorSnappingTarget() == 1) return pos;
TVectorImageP vi(TTool::getImage(false));
TPointD snapPoint = pos;
if (vi && m_param->m_snap.getValue()) {
double minDistance2 = m_param->m_minDistance2;
m_param->m_strokeIndex1 = -1;
int i, strokeNumber = vi->getStrokeCount();
TStroke *stroke;
double distance2, outW;
for (i = 0; i < strokeNumber; i++) {
stroke = vi->getStroke(i);
if (stroke->getNearestW(pos, outW, distance2) &&
distance2 < minDistance2) {
minDistance2 = distance2;
m_param->m_strokeIndex1 = i;
if (areAlmostEqual(outW, 0.0, 1e-3))
m_param->m_w1 = 0.0;
else if (areAlmostEqual(outW, 1.0, 1e-3))
m_param->m_w1 = 1.0;
else
2019-10-11 15:57:49 +13:00
m_param->m_w1 = outW;
TThickPoint point1 = stroke->getPoint(m_param->m_w1);
snapPoint = TPointD(point1.x, point1.y);
m_param->m_foundSnap = true;
m_param->m_snapPoint = snapPoint;
}
}
}
return snapPoint;
}
//-----------------------------------------------------------------------------
TPointD Primitive::getSnap(TPointD pos) {
if (m_param->m_foundSnap)
return m_param->m_snapPoint;
else
return pos;
}
// Primitive::drawSnap and Primitive::checkGuideSnapping are below the
// Geometric Tool definition since they use the m_tool property of Primitive
// but it isn't defined yet up here.
2016-03-19 06:57:51 +13:00
//=============================================================================
// Rectangle Primitive Class Declaration
//-----------------------------------------------------------------------------
class RectanglePrimitive final : public Primitive {
2016-06-15 18:43:10 +12:00
TRectD m_selectingRect;
TPointD m_pos;
TPointD m_startPoint;
TPixel32 m_color;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
RectanglePrimitive(PrimitiveParam *param, GeometricTool *tool,
bool reasterTool)
: Primitive(param, tool, reasterTool) {}
2016-06-19 20:06:29 +12:00
std::string getName() const override {
2016-06-15 18:43:10 +12:00
return "Rectangle";
} // W_ToolOptions_ShapeRect"; }
2016-06-19 20:06:29 +12:00
TStroke *makeStroke() const override;
void draw() override;
void leftButtonDown(const TPointD &pos, const TMouseEvent &) override;
void leftButtonDrag(const TPointD &realPos, const TMouseEvent &e) override;
void leftButtonUp(const TPointD &pos, const TMouseEvent &) override;
void mouseMove(const TPointD &pos, const TMouseEvent &e) override;
void onEnter() override;
2016-03-19 06:57:51 +13:00
};
//=============================================================================
// Circle Primitive Class Declaration
//-----------------------------------------------------------------------------
class CirclePrimitive final : public Primitive {
2016-06-15 18:43:10 +12:00
TPointD m_centre;
TPointD m_pos;
double m_radius;
TPixel32 m_color;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
CirclePrimitive(PrimitiveParam *param, GeometricTool *tool, bool reasterTool)
: Primitive(param, tool, reasterTool) {}
2016-06-19 20:06:29 +12:00
std::string getName() const override {
2016-06-15 18:43:10 +12:00
return "Circle";
} // W_ToolOptions_ShapeCircle";}
2016-06-19 20:06:29 +12:00
void draw() override;
void leftButtonDown(const TPointD &pos, const TMouseEvent &) override;
void leftButtonDrag(const TPointD &pos, const TMouseEvent &e) override;
TStroke *makeStroke() const override;
void leftButtonUp(const TPointD &pos, const TMouseEvent &) override;
void mouseMove(const TPointD &pos, const TMouseEvent &e) override;
void onEnter() override;
2016-03-19 06:57:51 +13:00
};
//=============================================================================
// MultiLine Primitive Class Declaration
//-----------------------------------------------------------------------------
const double joinDistance = 5.0;
2016-06-15 18:43:10 +12:00
class MultiLinePrimitive : public Primitive {
2016-03-19 06:57:51 +13:00
protected:
2016-06-15 18:43:10 +12:00
std::vector<TPointD> m_vertex;
TPointD m_mousePosition;
TPixel32 m_color;
bool m_closed, m_isSingleLine;
bool m_speedMoved; //!< True after moveSpeed(), false after addVertex()
bool m_beforeSpeedMoved; //!< Old value of m_speedMoved
bool m_ctrlDown; //!< Whether ctrl is hold down
MultilinePrimitiveUndo *m_undo;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
MultiLinePrimitive(PrimitiveParam *param, GeometricTool *tool,
bool reasterTool)
: Primitive(param, tool, reasterTool)
, m_closed(false)
, m_isSingleLine(false)
, m_speedMoved(false)
, m_beforeSpeedMoved(false)
, m_ctrlDown(false) {}
2016-06-19 20:06:29 +12:00
std::string getName() const override {
2016-06-15 18:43:10 +12:00
return "Polyline";
} // W_ToolOptions_ShapePolyline";}
void addVertex(const TPointD &pos);
void moveSpeed(const TPointD &delta);
2016-06-19 20:06:29 +12:00
void draw() override;
void leftButtonDown(const TPointD &pos, const TMouseEvent &e) override;
void leftButtonDrag(const TPointD &pos, const TMouseEvent &e) override;
void leftButtonDoubleClick(const TPointD &, const TMouseEvent &e) override;
void leftButtonUp(const TPointD &pos, const TMouseEvent &) override;
void mouseMove(const TPointD &pos, const TMouseEvent &e) override;
2018-02-19 19:34:13 +13:00
bool keyDown(QKeyEvent *event) override;
2016-06-19 20:06:29 +12:00
TStroke *makeStroke() const override;
2016-06-15 18:43:10 +12:00
void endLine();
2016-06-19 20:06:29 +12:00
void onActivate() override;
void onDeactivate() override {
2016-06-15 18:43:10 +12:00
m_vertex.clear();
m_speedMoved = false;
m_beforeSpeedMoved = false;
}
2016-06-19 20:06:29 +12:00
void onEnter() override;
void onImageChanged() override;
2016-06-15 18:43:10 +12:00
void setVertexes(const std::vector<TPointD> &vertex) { m_vertex = vertex; };
void setSpeedMoved(bool speedMoved) { m_speedMoved = speedMoved; };
2016-03-19 06:57:51 +13:00
};
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void MultilinePrimitiveUndo::undo() const {
m_tool->setVertexes(m_oldVertex);
int count = m_oldVertex.size();
bool speedMoved = (count != 0 && count % 4 != 1);
m_tool->setSpeedMoved(speedMoved);
TTool::getApplication()->getCurrentTool()->getTool()->invalidate();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void MultilinePrimitiveUndo::redo() const {
m_tool->setVertexes(m_newVertex);
int count = m_newVertex.size();
bool speedMoved = (count != 0 && count % 4 != 1);
m_tool->setSpeedMoved(speedMoved);
TTool::getApplication()->getCurrentTool()->getTool()->invalidate();
2016-03-19 06:57:51 +13:00
}
//----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
QString MultilinePrimitiveUndo::getToolName() {
return QString("Geometric Tool %1")
.arg(QString::fromStdString(m_tool->getName()));
2016-03-19 06:57:51 +13:00
}
//=============================================================================
// Line Primitive Class Declaration
//-----------------------------------------------------------------------------
class LinePrimitive final : public MultiLinePrimitive {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
LinePrimitive(PrimitiveParam *param, GeometricTool *tool, bool reasterTool)
: MultiLinePrimitive(param, tool, reasterTool) {
m_isSingleLine = true;
}
2016-06-19 20:06:29 +12:00
std::string getName() const override {
2016-06-15 18:43:10 +12:00
return "Line";
} // W_ToolOptions_ShapePolyline";}
2016-06-19 20:06:29 +12:00
void draw() override;
void leftButtonDown(const TPointD &pos, const TMouseEvent &e) override;
void leftButtonUp(const TPointD &pos, const TMouseEvent &e) override;
void leftButtonDrag(const TPointD &pos, const TMouseEvent &e) override;
void mouseMove(const TPointD &pos, const TMouseEvent &e) override;
2016-06-19 20:06:29 +12:00
void leftButtonDoubleClick(const TPointD &, const TMouseEvent &e) override {}
2016-03-19 06:57:51 +13:00
};
//=============================================================================
// Ellipse Primitive Class Declaration
//-----------------------------------------------------------------------------
class EllipsePrimitive final : public Primitive {
2016-06-15 18:43:10 +12:00
TPointD m_pos;
TRectD m_selectingRect;
TPointD m_startPoint;
TPixel32 m_color;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
EllipsePrimitive(PrimitiveParam *param, GeometricTool *tool, bool reasterTool)
: Primitive(param, tool, reasterTool) {}
2016-06-19 20:06:29 +12:00
std::string getName() const override {
2016-06-15 18:43:10 +12:00
return "Ellipse";
} // W_ToolOptions_ShapeEllipse";}
2016-06-19 20:06:29 +12:00
void draw() override;
void leftButtonDown(const TPointD &pos, const TMouseEvent &) override;
void leftButtonDrag(const TPointD &realPos, const TMouseEvent &e) override;
TStroke *makeStroke() const override;
void leftButtonUp(const TPointD &pos, const TMouseEvent &) override;
void mouseMove(const TPointD &pos, const TMouseEvent &e) override;
void onEnter() override;
2016-03-19 06:57:51 +13:00
};
//=============================================================================
// Arc Primitive Class Declaration
//-----------------------------------------------------------------------------
class ArcPrimitive final : public Primitive {
2016-06-15 18:43:10 +12:00
TStroke *m_stroke;
TPointD m_startPoint, m_endPoint, m_centralPoint;
int m_clickNumber;
TPixel32 m_color;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
ArcPrimitive(PrimitiveParam *param, GeometricTool *tool, bool reasterTool)
: Primitive(param, tool, reasterTool), m_stroke(0), m_clickNumber(0) {}
~ArcPrimitive() {
if (m_stroke) delete m_stroke;
}
2016-06-19 20:06:29 +12:00
std::string getName() const override {
2016-06-15 18:43:10 +12:00
return "Arc";
} // _ToolOptions_ShapeArc";}
2016-06-19 20:06:29 +12:00
TStroke *makeStroke() const override;
void draw() override;
void leftButtonUp(const TPointD &pos, const TMouseEvent &) override;
void mouseMove(const TPointD &pos, const TMouseEvent &e) override;
void onEnter() override;
2016-03-19 06:57:51 +13:00
};
//=============================================================================
// Polygon Primitive Class Declaration
//-----------------------------------------------------------------------------
class PolygonPrimitive final : public Primitive {
2016-06-15 18:43:10 +12:00
TPointD m_startPoint, m_centre;
double m_radius;
TPixel32 m_color;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
PolygonPrimitive(PrimitiveParam *param, GeometricTool *tool, bool reasterTool)
: Primitive(param, tool, reasterTool) {}
2016-06-19 20:06:29 +12:00
std::string getName() const override {
2016-06-15 18:43:10 +12:00
return "Polygon";
} // W_ToolOptions_ShapePolygon";}
2016-06-19 20:06:29 +12:00
TStroke *makeStroke() const override;
void draw() override;
void leftButtonDown(const TPointD &pos, const TMouseEvent &) override;
void leftButtonDrag(const TPointD &pos, const TMouseEvent &e) override;
void leftButtonUp(const TPointD &pos, const TMouseEvent &) override;
void mouseMove(const TPointD &pos, const TMouseEvent &e) override;
2016-03-19 06:57:51 +13:00
};
//=============================================================================
// Geometric Tool
//-----------------------------------------------------------------------------
class GeometricTool final : public TTool {
2016-03-19 06:57:51 +13:00
protected:
2016-06-15 18:43:10 +12:00
Primitive *m_primitive;
std::map<std::wstring, Primitive *> m_primitiveTable;
PrimitiveParam m_param;
std::wstring m_typeCode;
bool m_active;
bool m_firstTime;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
GeometricTool(int targetType)
: TTool("T_Geometric")
, m_primitive(0)
, m_param(targetType)
, m_active(false)
, m_firstTime(true) {
bind(targetType);
if ((targetType & TTool::RasterImage) || (targetType & TTool::ToonzImage)) {
addPrimitive(new RectanglePrimitive(&m_param, this, true));
addPrimitive(new CirclePrimitive(&m_param, this, true));
addPrimitive(new EllipsePrimitive(&m_param, this, true));
addPrimitive(new LinePrimitive(&m_param, this, true));
addPrimitive(new MultiLinePrimitive(&m_param, this, true));
addPrimitive(new ArcPrimitive(&m_param, this, true));
addPrimitive(new PolygonPrimitive(&m_param, this, true));
} else // targetType == 1
{
// vector
addPrimitive(m_primitive = new RectanglePrimitive(&m_param, this, false));
addPrimitive(new CirclePrimitive(&m_param, this, false));
addPrimitive(new EllipsePrimitive(&m_param, this, false));
addPrimitive(new LinePrimitive(&m_param, this, false));
addPrimitive(new MultiLinePrimitive(&m_param, this, false));
addPrimitive(new ArcPrimitive(&m_param, this, false));
addPrimitive(new PolygonPrimitive(&m_param, this, false));
}
}
~GeometricTool() {
std::map<std::wstring, Primitive *>::iterator it;
for (it = m_primitiveTable.begin(); it != m_primitiveTable.end(); ++it)
delete it->second;
}
2016-06-19 20:06:29 +12:00
ToolType getToolType() const override { return TTool::LevelWriteTool; }
2016-06-15 18:43:10 +12:00
2016-06-19 20:06:29 +12:00
void updateTranslation() override { m_param.updateTranslation(); }
2016-06-15 18:43:10 +12:00
void addPrimitive(Primitive *p) {
// TODO: aggiungere il controllo per evitare nomi ripetuti
std::wstring name = ::to_wstring(p->getName());
// wstring name = TStringTable::translate(p->getName());
m_primitiveTable[name] = p;
m_param.m_type.addValue(name);
}
void changeType(std::wstring name) {
std::map<std::wstring, Primitive *>::iterator it =
m_primitiveTable.find(name);
if (it != m_primitiveTable.end()) m_primitive = it->second;
}
2016-06-19 20:06:29 +12:00
void leftButtonDown(const TPointD &p, const TMouseEvent &e) override {
if (getViewer() && getViewer()->getGuidedStrokePickerMode()) {
getViewer()->doPickGuideStroke(p);
return;
}
2016-06-15 18:43:10 +12:00
/* m_active = getApplication()->getCurrentObject()->isSpline() ||
(bool) getImage(true);*/
2019-10-11 15:57:49 +13:00
if (!getApplication()->getCurrentObject()->isSpline())
m_active = touchImage(); // NEEDS to be done even if(m_active), due
if (!m_active) // to the HORRIBLE m_frameCreated / m_levelCreated
return; // mechanism. touchImage() is the ONLY function
2016-06-15 18:43:10 +12:00
// resetting them to false... >_<
if (m_primitive) m_primitive->leftButtonDown(p, e);
invalidate();
}
2016-06-19 20:06:29 +12:00
void leftButtonDrag(const TPointD &p, const TMouseEvent &e) override {
2016-06-15 18:43:10 +12:00
if (!m_active) return;
if (m_primitive) m_primitive->leftButtonDrag(p, e);
invalidate();
}
2016-06-19 20:06:29 +12:00
void leftButtonUp(const TPointD &p, const TMouseEvent &e) override {
2016-06-15 18:43:10 +12:00
if (!m_active) return;
if (m_primitive) m_primitive->leftButtonUp(p, e);
invalidate();
}
2016-06-19 20:06:29 +12:00
void leftButtonDoubleClick(const TPointD &p, const TMouseEvent &e) override {
2016-06-15 18:43:10 +12:00
if (!m_active) return;
if (m_primitive) m_primitive->leftButtonDoubleClick(p, e);
invalidate();
}
2018-02-19 19:34:13 +13:00
bool keyDown(QKeyEvent *event) override {
return m_primitive->keyDown(event);
2016-06-15 18:43:10 +12:00
}
2016-06-19 20:06:29 +12:00
void onImageChanged() override {
2016-06-15 18:43:10 +12:00
if (m_primitive) m_primitive->onImageChanged();
invalidate();
}
2016-06-19 20:06:29 +12:00
void rightButtonDown(const TPointD &p, const TMouseEvent &e) override {
2016-06-15 18:43:10 +12:00
if (m_primitive) m_primitive->rightButtonDown(p, e);
invalidate();
}
2016-06-19 20:06:29 +12:00
void mouseMove(const TPointD &p, const TMouseEvent &e) override {
2016-06-15 18:43:10 +12:00
if (m_primitive) m_primitive->mouseMove(p, e);
}
2016-06-19 20:06:29 +12:00
void onActivate() override {
2016-06-15 18:43:10 +12:00
if (m_firstTime) {
m_param.m_toolSize.setValue(GeometricSize);
m_param.m_rasterToolSize.setValue(GeometricRasterSize);
m_param.m_opacity.setValue(GeometricOpacity);
m_param.m_hardness.setValue(GeometricBrushHardness);
m_param.m_selective.setValue(GeometricSelective ? 1 : 0);
m_param.m_autogroup.setValue(GeometricGroupIt ? 1 : 0);
m_param.m_autofill.setValue(GeometricAutofill ? 1 : 0);
std::wstring typeCode = ::to_wstring(GeometricType.getValue());
m_param.m_type.setValue(typeCode);
GeometricType = ::to_string(typeCode);
m_typeCode = typeCode;
changeType(typeCode);
m_param.m_edgeCount.setValue(GeometricEdgeCount);
m_param.m_pencil.setValue(GeometricPencil ? 1 : 0);
m_param.m_capStyle.setIndex(GeometricCapStyle);
m_param.m_joinStyle.setIndex(GeometricJoinStyle);
m_param.m_miterJoinLimit.setValue(GeometricMiterValue);
m_firstTime = false;
m_param.m_snap.setValue(GeometricSnap);
if (m_targetType & TTool::Vectors) {
m_param.m_snapSensitivity.setIndex(GeometricSnapSensitivity);
switch (GeometricSnapSensitivity) {
case 0:
m_param.m_minDistance2 = SNAPPING_LOW;
break;
case 1:
m_param.m_minDistance2 = SNAPPING_MEDIUM;
break;
case 2:
m_param.m_minDistance2 = SNAPPING_HIGH;
break;
}
}
2016-06-15 18:43:10 +12:00
}
m_primitive->resetSnap();
2016-06-15 18:43:10 +12:00
/*--
onEnterを通らない場合
LineToolが反応しないことがある対策 --*/
m_active = (getImage(false) != 0 ||
Preferences::instance()->isAutoCreateEnabled());
if (m_primitive) m_primitive->onActivate();
}
2016-06-19 20:06:29 +12:00
void onDeactivate() override {
2016-06-15 18:43:10 +12:00
if (m_primitive) m_primitive->onDeactivate();
}
2016-06-19 20:06:29 +12:00
void onEnter() override {
2016-06-15 18:43:10 +12:00
m_active = getImage(false) != 0;
if (m_active && m_primitive) m_primitive->onEnter();
}
2016-06-19 20:06:29 +12:00
void draw() override {
2016-06-15 18:43:10 +12:00
if (m_primitive) m_primitive->draw();
}
int getCursorId() const override {
if (m_viewer && m_viewer->getGuidedStrokePickerMode())
return m_viewer->getGuidedStrokePickerCursor();
return ToolCursor::PenCursor;
}
2016-06-15 18:43:10 +12:00
int getColorClass() const { return 1; }
2016-06-20 14:23:05 +12:00
TPropertyGroup *getProperties(int idx) override {
return &m_param.m_prop[idx];
}
2016-06-15 18:43:10 +12:00
2016-06-19 20:06:29 +12:00
bool onPropertyChanged(std::string propertyName) override {
2016-06-15 18:43:10 +12:00
/*--- 変更されたPropertyごとに処理を分ける。
m_toolSizeとm_rasterToolSizeは同じName(="Size:")
---*/
if (propertyName == m_param.m_toolSize.getName()) {
TImageP img = getImage(false);
TToonzImageP ri(img); /*-- ラスタかどうかの判定 --*/
if (ri)
GeometricRasterSize = m_param.m_rasterToolSize.getValue();
else
GeometricSize = m_param.m_toolSize.getValue();
} else if (propertyName == m_param.m_type.getName()) {
std::wstring typeCode = m_param.m_type.getValue();
GeometricType = ::to_string(typeCode);
if (typeCode != m_typeCode) {
m_typeCode = typeCode;
changeType(typeCode);
}
} else if (propertyName == m_param.m_edgeCount.getName())
GeometricEdgeCount = m_param.m_edgeCount.getValue();
else if (propertyName == m_param.m_autogroup.getName()) {
if (!m_param.m_autogroup.getValue()) {
m_param.m_autofill.setValue(false);
// this is ugly: it's needed to refresh the GUI of the toolbar after
// having set to false the autofill...
TTool::getApplication()->getCurrentTool()->setTool(
""); // necessary, otherwise next setTool is ignored...
TTool::getApplication()->getCurrentTool()->setTool(
QString::fromStdString(getName()));
}
GeometricGroupIt = m_param.m_autogroup.getValue();
} else if (propertyName == m_param.m_autofill.getName()) {
if (m_param.m_autofill.getValue()) {
m_param.m_autogroup.setValue(true);
// this is ugly: it's needed to refresh the GUI of the toolbar after
// having set to false the autofill...
TTool::getApplication()->getCurrentTool()->setTool(
""); // necessary, otherwise next setTool is ignored...
TTool::getApplication()->getCurrentTool()->setTool(
QString::fromStdString(getName()));
}
GeometricGroupIt = m_param.m_autofill.getValue();
} else if (propertyName == m_param.m_selective.getName())
GeometricSelective = m_param.m_selective.getValue();
else if (propertyName == m_param.m_pencil.getName())
GeometricPencil = m_param.m_pencil.getValue();
else if (propertyName == m_param.m_hardness.getName())
GeometricBrushHardness = m_param.m_hardness.getValue();
else if (propertyName == m_param.m_opacity.getName())
GeometricOpacity = m_param.m_opacity.getValue();
else if (propertyName == m_param.m_capStyle.getName())
GeometricCapStyle = m_param.m_capStyle.getIndex();
else if (propertyName == m_param.m_joinStyle.getName())
GeometricJoinStyle = m_param.m_joinStyle.getIndex();
else if (propertyName == m_param.m_miterJoinLimit.getName())
GeometricMiterValue = m_param.m_miterJoinLimit.getValue();
else if (propertyName == m_param.m_snap.getName())
GeometricSnap = m_param.m_snap.getValue();
else if (propertyName == m_param.m_snapSensitivity.getName()) {
GeometricSnapSensitivity = m_param.m_snapSensitivity.getIndex();
switch (GeometricSnapSensitivity) {
case 0:
m_param.m_minDistance2 = SNAPPING_LOW;
break;
case 1:
m_param.m_minDistance2 = SNAPPING_MEDIUM;
break;
case 2:
m_param.m_minDistance2 = SNAPPING_HIGH;
break;
}
}
2016-06-15 18:43:10 +12:00
return false;
}
void addStroke() {
if (!m_primitive) return;
TStroke *stroke = m_primitive->makeStroke();
if (!stroke) return;
TStroke::OutlineOptions &options = stroke->outlineOptions();
options.m_capStyle = m_param.m_capStyle.getIndex();
options.m_joinStyle = m_param.m_joinStyle.getIndex();
options.m_miterUpper = m_param.m_miterJoinLimit.getValue();
TImage *image = getImage(true);
TToonzImageP ti(image);
TVectorImageP vi(image);
TRasterImageP ri(image);
TXshSimpleLevel *sl =
TTool::getApplication()->getCurrentLevel()->getSimpleLevel();
TFrameId id = getCurrentFid();
/*-- ToonzImageの場合 --*/
if (ti) {
int styleId = TTool::getApplication()->getCurrentLevelStyleIndex();
bool selective = m_param.m_selective.getValue();
bool filled = false;
stroke->setStyle(styleId);
double hardness = m_param.m_hardness.getValue() * 0.01;
TRect savebox;
if (hardness == 1 || m_param.m_pencil.getValue()) {
TUndoManager::manager()->add(new UndoRasterPencil(
sl, id, stroke, selective, filled, !m_param.m_pencil.getValue(),
m_isFrameCreated, m_isLevelCreated, m_primitive->getName()));
savebox = ToonzImageUtils::addInkStroke(ti, stroke, styleId, selective,
filled, TConsts::infiniteRectD,
!m_param.m_pencil.getValue());
} else {
int thickness = m_param.m_rasterToolSize.getValue();
TUndoManager::manager()->add(new CMBluredPrimitiveUndo(
sl, id, stroke, thickness, hardness, selective, false,
m_isFrameCreated, m_isLevelCreated, m_primitive->getName()));
savebox = drawBluredBrush(ti, stroke, thickness, hardness, selective);
}
ToolUtils::updateSaveBox();
delete stroke;
}
/*-- VectorImageの場合 --*/
else if (vi) {
if (TTool::getApplication()->getCurrentObject()->isSpline()) {
if (!ToolUtils::isJustCreatedSpline(vi.getPointer())) {
m_primitive->setIsPrompting(true);
QString question("Are you sure you want to replace the motion path?");
int ret =
DVGui::MsgBox(question, QObject::tr("Yes"), QObject::tr("No"), 0);
m_primitive->setIsPrompting(false);
if (ret == 2 || ret == 0) return;
}
2019-10-11 15:57:49 +13:00
QMutexLocker lock(vi->getMutex());
2016-06-15 18:43:10 +12:00
TUndo *undo = new UndoPath(
getXsheet()->getStageObject(getObjectId())->getSpline());
while (vi->getStrokeCount() > 0) vi->deleteStroke(0);
vi->addStroke(stroke, false);
TUndoManager::manager()->add(undo);
} else {
int styleId = TTool::getApplication()->getCurrentLevelStyleIndex();
if (styleId >= 0) stroke->setStyle(styleId);
QMutexLocker lock(vi->getMutex());
std::vector<TFilledRegionInf> *fillInformation =
new std::vector<TFilledRegionInf>;
ImageUtils::getFillingInformationOverlappingArea(vi, *fillInformation,
stroke->getBBox());
2020-01-29 13:22:35 +13:00
vi->addStroke(stroke);
TUndoManager::manager()->add(new UndoPencil(
vi->getStroke(vi->getStrokeCount() - 1), fillInformation, sl, id,
m_isFrameCreated, m_isLevelCreated, m_param.m_autogroup.getValue(),
m_param.m_autofill.getValue()));
if ((Preferences::instance()->getGuidedDrawingType() == 1 ||
Preferences::instance()->getGuidedDrawingType() == 2) &&
Preferences::instance()->getGuidedAutoInbetween()) {
TTool *tool =
TTool::getTool(T_Brush, TTool::ToolTargetType::VectorImage);
ToonzVectorBrushTool *vbTool = (ToonzVectorBrushTool *)tool;
if (vbTool) {
vbTool->setViewer(m_viewer);
2020-01-29 13:22:35 +13:00
vbTool->doGuidedAutoInbetween(id, vi, stroke, false,
m_param.m_autogroup.getValue(),
m_param.m_autofill.getValue(), false);
}
}
2016-06-15 18:43:10 +12:00
}
if (m_param.m_autogroup.getValue() && stroke->isSelfLoop()) {
int index = vi->getStrokeCount() - 1;
vi->group(index, 1);
if (m_param.m_autofill.getValue()) {
// to avoid filling other strokes, I enter into the new stroke group
int currentGroup = vi->exitGroup();
vi->enterGroup(index);
vi->selectFill(stroke->getBBox().enlarge(1, 1), 0, stroke->getStyle(),
false, true, false);
if (currentGroup != -1)
vi->enterGroup(currentGroup);
else
vi->exitGroup();
}
}
}
/*-- RasterImageの場合 --*/
else if (ri) {
int styleId = TTool::getApplication()->getCurrentLevelStyleIndex();
stroke->setStyle(styleId);
double opacity = m_param.m_opacity.getValue() * 0.01;
double hardness = m_param.m_hardness.getValue() * 0.01;
TRect savebox;
if (hardness == 1) {
TUndoManager::manager()->add(new UndoFullColorPencil(
sl, id, stroke, opacity, true, m_isFrameCreated, m_isLevelCreated));
savebox = TRasterImageUtils::addStroke(ri, stroke, TRectD(), opacity);
} else {
int thickness = m_param.m_rasterToolSize.getValue();
TUndoManager::manager()->add(new FullColorBluredPrimitiveUndo(
sl, id, stroke, thickness, hardness, opacity, true,
m_isFrameCreated, m_isLevelCreated));
savebox = drawBluredBrush(ri, stroke, thickness, hardness, opacity);
}
ToolUtils::updateSaveBox();
delete stroke;
}
notifyImageChanged();
m_active = false;
}
2016-03-19 06:57:51 +13:00
};
GeometricTool GeometricVectorTool(TTool::Vectors | TTool::EmptyTarget);
GeometricTool GeometricRasterTool(TTool::ToonzImage | TTool::EmptyTarget);
2016-06-15 18:43:10 +12:00
GeometricTool GeometricRasterFullColorTool(TTool::RasterImage |
TTool::EmptyTarget);
2016-03-19 06:57:51 +13:00
//-------------------------------------------------------------------------------------------------------------
void Primitive::drawSnap() {
// snapping
if ((m_param->m_targetType & TTool::Vectors) && m_param->m_snap.getValue()) {
m_param->m_pixelSize = m_tool->getPixelSize();
double thick = 6.0 * m_param->m_pixelSize;
if (m_param->m_foundSnap) {
tglColor(TPixelD(0.1, 0.9, 0.1));
tglDrawCircle(m_param->m_snapPoint, thick);
}
}
}
//-------------------------------------------------------------------------------------------------------------
TPointD Primitive::checkGuideSnapping(TPointD pos) {
if (Preferences::instance()->getVectorSnappingTarget() == 0) {
if (m_param->m_foundSnap)
return m_param->m_snapPoint;
else
return pos;
}
if ((m_param->m_targetType & TTool::Vectors) && m_param->m_snap.getValue()) {
int vGuideCount = 0, hGuideCount = 0;
double guideDistance = sqrt(m_param->m_minDistance2);
TTool::Viewer *viewer = m_tool->getViewer();
if (viewer) {
vGuideCount = viewer->getVGuideCount();
hGuideCount = viewer->getHGuideCount();
}
double distanceToVGuide = -1.0, distanceToHGuide = -1.0;
double vGuide, hGuide;
bool useGuides = false;
if (vGuideCount) {
for (int j = 0; j < vGuideCount; j++) {
double guide = viewer->getVGuide(j);
2020-04-12 17:19:20 +12:00
double tempDistance = std::abs(guide - pos.y);
if (tempDistance < guideDistance &&
(distanceToVGuide < 0 || tempDistance < distanceToVGuide)) {
distanceToVGuide = tempDistance;
vGuide = guide;
useGuides = true;
}
}
}
if (hGuideCount) {
for (int j = 0; j < hGuideCount; j++) {
double guide = viewer->getHGuide(j);
2020-04-12 17:19:20 +12:00
double tempDistance = std::abs(guide - pos.x);
if (tempDistance < guideDistance &&
(distanceToHGuide < 0 || tempDistance < distanceToHGuide)) {
distanceToHGuide = tempDistance;
hGuide = guide;
useGuides = true;
}
}
}
if (useGuides && m_param->m_foundSnap) {
2020-04-12 17:19:20 +12:00
double currYDistance = std::abs(m_param->m_snapPoint.y - pos.y);
double currXDistance = std::abs(m_param->m_snapPoint.x - pos.x);
double hypotenuse =
sqrt(pow(currYDistance, 2.0) + pow(currXDistance, 2.0));
if ((distanceToVGuide >= 0 && distanceToVGuide < hypotenuse) ||
(distanceToHGuide >= 0 && distanceToHGuide < hypotenuse))
useGuides = true;
else
useGuides = false;
}
if (useGuides) {
assert(distanceToHGuide >= 0 || distanceToVGuide >= 0);
if (distanceToHGuide < 0 ||
(distanceToVGuide <= distanceToHGuide && distanceToVGuide >= 0)) {
m_param->m_snapPoint.y = vGuide;
m_param->m_snapPoint.x = pos.x;
} else {
m_param->m_snapPoint.y = pos.y;
m_param->m_snapPoint.x = hGuide;
}
m_param->m_foundSnap = true;
}
if (m_param->m_foundSnap)
return m_param->m_snapPoint;
else
return pos;
} else
return pos;
}
2016-03-19 06:57:51 +13:00
//=============================================================================
// Rectangle Primitive Class Implementation
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RectanglePrimitive::draw() {
drawSnap();
2016-06-15 18:43:10 +12:00
if (m_isEditing || m_isPrompting ||
areAlmostEqual(m_selectingRect.x0, m_selectingRect.x1) ||
areAlmostEqual(m_selectingRect.y0, m_selectingRect.y1)) {
tglColor(m_isEditing ? m_color : TPixel32::Green);
glBegin(GL_LINE_LOOP);
tglVertex(m_selectingRect.getP00());
tglVertex(m_selectingRect.getP01());
tglVertex(m_selectingRect.getP11());
tglVertex(m_selectingRect.getP10());
glEnd();
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RectanglePrimitive::leftButtonDown(const TPointD &pos,
const TMouseEvent &) {
TTool::Application *app = TTool::getApplication();
if (app->getCurrentObject()->isSpline()) {
m_color = TPixel32::Red;
m_isEditing = true;
} else {
// app->getCurrentTool()->getTool()->touchImage();
const TColorStyle *style = app->getCurrentLevelStyle();
m_isEditing = style != 0 && style->isStrokeStyle();
m_color = (style) ? style->getAverageColor() : TPixel32::Black;
}
if (!m_isEditing) return;
TPointD newPos = getSnap(pos);
2016-06-15 18:43:10 +12:00
if (m_param->m_pencil.getValue() &&
(m_param->m_targetType & TTool::ToonzImage ||
m_param->m_targetType & TTool::RasterImage)) {
if (m_param->m_rasterToolSize.getValue() % 2 != 0)
m_startPoint = TPointD((int)pos.x, (int)pos.y);
else
m_startPoint = TPointD((int)pos.x + 0.5, (int)pos.y + 0.5);
} else
2019-10-11 15:57:49 +13:00
m_startPoint = newPos;
2016-06-15 18:43:10 +12:00
m_selectingRect.x0 = m_startPoint.x;
m_selectingRect.y0 = m_startPoint.y;
m_selectingRect.x1 = m_startPoint.x;
m_selectingRect.y1 = m_startPoint.y;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RectanglePrimitive::leftButtonDrag(const TPointD &realPos,
const TMouseEvent &e) {
if (!m_isEditing) return;
TPointD pos;
if (e.isShiftPressed()) {
double distance = tdistance(realPos, m_startPoint) * M_SQRT1_2;
pos.x = (realPos.x > m_startPoint.x) ? m_startPoint.x + distance
: m_startPoint.x - distance;
pos.y = (realPos.y > m_startPoint.y) ? m_startPoint.y + distance
: m_startPoint.y - distance;
} else {
pos = calculateSnap(realPos);
pos = checkGuideSnapping(realPos);
2016-06-15 18:43:10 +12:00
}
if (m_param->m_pencil.getValue() &&
(m_param->m_targetType & TTool::ToonzImage ||
m_param->m_targetType & TTool::RasterImage)) {
if (m_param->m_rasterToolSize.getValue() % 2 != 0)
pos = TPointD((int)pos.x, (int)pos.y);
else
pos = TPointD((int)pos.x + 0.5, (int)pos.y + 0.5);
}
m_selectingRect.x1 = pos.x;
m_selectingRect.y1 = pos.y;
if (!e.isAltPressed()) {
m_selectingRect.x0 = m_startPoint.x;
m_selectingRect.y0 = m_startPoint.y;
} else {
m_selectingRect.x0 = m_startPoint.x + m_startPoint.x - pos.x;
m_selectingRect.y0 = m_startPoint.y + m_startPoint.y - pos.y;
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TStroke *RectanglePrimitive::makeStroke() const {
if (areAlmostEqual(m_selectingRect.x0, m_selectingRect.x1) ||
areAlmostEqual(m_selectingRect.y0, m_selectingRect.y1))
return 0;
TRectD selArea;
selArea.x0 = std::min(m_selectingRect.x0, m_selectingRect.x1);
selArea.y0 = std::min(m_selectingRect.y0, m_selectingRect.y1);
selArea.x1 = std::max(m_selectingRect.x0, m_selectingRect.x1);
selArea.y1 = std::max(m_selectingRect.y0, m_selectingRect.y1);
double thick = getThickness();
TStroke *stroke = 0;
if (m_param->m_targetType & TTool::Vectors) {
std::vector<TThickPoint> points(17);
points[0] = TThickPoint(selArea.x1, selArea.y1, thick);
points[1] = TThickPoint(selArea.x1, selArea.y1, thick) + TPointD(-0.01, 0);
points[3] = TThickPoint(selArea.x0, selArea.y1, thick) + TPointD(0.01, 0);
points[4] = TThickPoint(selArea.x0, selArea.y1, thick);
points[5] = TThickPoint(selArea.x0, selArea.y1, thick) + TPointD(0, -0.01);
points[7] = TThickPoint(selArea.x0, selArea.y0, thick) + TPointD(0, 0.01);
points[8] = TThickPoint(selArea.x0, selArea.y0, thick);
points[9] = TThickPoint(selArea.x0, selArea.y0, thick) + TPointD(0.01, 0);
points[11] = TThickPoint(selArea.x1, selArea.y0, thick) + TPointD(-0.01, 0);
points[12] = TThickPoint(selArea.x1, selArea.y0, thick);
points[13] = TThickPoint(selArea.x1, selArea.y0, thick) + TPointD(0, 0.01);
points[15] = points[0] + TPointD(0, -0.01);
points[16] = points[0];
points[2] = 0.5 * (points[1] + points[3]);
points[6] = 0.5 * (points[5] + points[7]);
points[10] = 0.5 * (points[9] + points[11]);
points[14] = 0.5 * (points[13] + points[15]);
stroke = new TStroke(points);
} else if (m_param->m_targetType & TTool::ToonzImage ||
m_param->m_targetType & TTool::RasterImage) {
std::vector<TThickPoint> points(9);
double middleX = (selArea.x0 + selArea.x1) * 0.5;
double middleY = (selArea.y0 + selArea.y1) * 0.5;
points[0] = TThickPoint(selArea.x1, selArea.y1, thick);
points[1] = TThickPoint(middleX, selArea.y1, thick);
points[2] = TThickPoint(selArea.x0, selArea.y1, thick);
points[3] = TThickPoint(selArea.x0, middleY, thick);
points[4] = TThickPoint(selArea.x0, selArea.y0, thick);
points[5] = TThickPoint(middleX, selArea.y0, thick);
points[6] = TThickPoint(selArea.x1, selArea.y0, thick);
points[7] = TThickPoint(selArea.x1, middleY, thick);
points[8] = points[0];
stroke = new TStroke(points);
}
stroke->setSelfLoop();
return stroke;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RectanglePrimitive::leftButtonUp(const TPointD &pos, const TMouseEvent &) {
if (!m_isEditing) return;
m_isEditing = false;
m_tool->addStroke();
resetSnap();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RectanglePrimitive::mouseMove(const TPointD &pos, const TMouseEvent &e) {
TPointD newPos = calculateSnap(pos);
newPos = checkGuideSnapping(pos);
m_pos = newPos;
m_tool->invalidate();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RectanglePrimitive::onEnter() {
TTool::Application *app = TTool::getApplication();
if (!app) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (app->getCurrentObject()->isSpline())
m_color = TPixel32::Red;
else {
const TColorStyle *style = app->getCurrentLevelStyle();
2019-10-11 15:57:49 +13:00
if (style) m_color = style->getAverageColor();
2016-06-15 18:43:10 +12:00
}
2016-03-19 06:57:51 +13:00
}
//=============================================================================
// Circle Primitive Class Implementation
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void CirclePrimitive::draw() {
drawSnap();
2016-06-15 18:43:10 +12:00
if (m_isEditing || m_isPrompting) {
tglColor(m_isEditing ? m_color : TPixel32::Green);
tglDrawCircle(m_centre, m_radius);
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void CirclePrimitive::leftButtonDown(const TPointD &pos, const TMouseEvent &) {
m_pos = getSnap(pos);
m_centre = m_pos;
2016-06-15 18:43:10 +12:00
TTool::Application *app = TTool::getApplication();
if (!app) return;
if (app->getCurrentObject()->isSpline()) {
m_color = TPixel32::Red;
m_isEditing = true;
} else {
const TColorStyle *style = app->getCurrentLevelStyle();
if (style) {
m_isEditing = style->isStrokeStyle();
m_color = style->getAverageColor();
} else {
m_isEditing = false;
m_color = TPixel32::Black;
}
}
if (!m_isEditing) return;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void CirclePrimitive::leftButtonDrag(const TPointD &pos, const TMouseEvent &e) {
if (!m_isEditing) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_pos = pos;
2018-02-19 19:34:13 +13:00
m_pos = calculateSnap(pos);
m_pos = checkGuideSnapping(pos);
m_radius = tdistance(m_centre, m_pos);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TStroke *CirclePrimitive::makeStroke() const {
return makeEllipticStroke(getThickness(), m_centre, m_radius, m_radius);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void CirclePrimitive::leftButtonUp(const TPointD &pos, const TMouseEvent &) {
if (!m_isEditing) return;
m_isEditing = false;
if (isAlmostZero(m_radius)) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_tool->addStroke();
m_radius = 0;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void CirclePrimitive::mouseMove(const TPointD &pos, const TMouseEvent &e) {
m_pos = calculateSnap(pos);
m_pos = checkGuideSnapping(pos);
m_tool->invalidate();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void CirclePrimitive::onEnter() {
TTool::Application *app = TTool::getApplication();
if (!app) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (app->getCurrentObject()->isSpline())
m_color = TPixel32::Red;
else {
const TColorStyle *style = app->getCurrentLevelStyle();
2019-10-11 15:57:49 +13:00
if (style) m_color = style->getAverageColor();
2016-06-15 18:43:10 +12:00
}
2016-03-19 06:57:51 +13:00
}
//=============================================================================
// MultiLine Primitive Class Implementation
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void MultiLinePrimitive::addVertex(const TPointD &pos) {
int count = m_vertex.size();
// Inserisco il primo punto
if (count == 0) {
m_vertex.push_back(pos);
return;
}
TPointD &vertex = m_vertex[count - 1];
// Caso particolare in cui inizio una curva e la chiudo subito cliccando sul
// punto di pertenza
if (count == 1 && pos == vertex) {
m_vertex.push_back(pos);
m_vertex.push_back(pos);
m_vertex.push_back(pos);
return;
}
// Calcolo lo speedOut
TPointD speedOutPoint;
if (!m_speedMoved) // Se non e' stato mosso lo speedOut devo calcolarlo e
// inserirlo
{
speedOutPoint = vertex + computeSpeed(vertex, pos, 0.01);
m_vertex.push_back(speedOutPoint);
} else {
if (m_ctrlDown)
vertex =
m_vertex[count - 2] + computeSpeed(m_vertex[count - 2], pos, 0.01);
speedOutPoint = vertex;
}
// Calcolo lo speedIn
TPointD speedInPoint = pos + computeSpeed(pos, speedOutPoint, 0.01);
// Calcolo il "punto di mezzo" e lo inserisco
TPointD middlePoint = 0.5 * (speedInPoint + speedOutPoint);
// Inserisco il "punto di mezzo"
m_vertex.push_back(middlePoint);
// Inserisco lo speedIn
m_vertex.push_back(speedInPoint);
// Inserisco il nuovo punto
m_vertex.push_back(pos);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void MultiLinePrimitive::moveSpeed(const TPointD &delta) {
int count = m_vertex.size();
assert(count > 0);
TPointD lastPoint = m_vertex[count - 1];
TPointD newSpeedOutPoint = lastPoint - delta;
if (m_speedMoved)
m_vertex[count - 1] = newSpeedOutPoint;
else {
m_vertex.push_back(newSpeedOutPoint);
++count;
}
if (count < 5) {
assert(count == 2);
return;
}
TPointD vertex = m_vertex[count - 2];
TPointD v(0, 0);
if (newSpeedOutPoint != vertex) v = normalize(newSpeedOutPoint - vertex);
double speedOut = tdistance(newSpeedOutPoint, vertex);
TPointD newSpeedInPoint = vertex - TPointD(speedOut * v.x, speedOut * v.y);
m_vertex[count - 3] = newSpeedInPoint;
if (tdistance(m_vertex[count - 5], m_vertex[count - 6]) <= 0.02)
// see ControlPointEditorStroke::isSpeedOutLinear() from
// controlpointselection.cpp
m_vertex[count - 5] =
m_vertex[count - 6] +
computeSpeed(m_vertex[count - 6], m_vertex[count - 3], 0.01);
m_vertex[count - 4] = 0.5 * (m_vertex[count - 3] + m_vertex[count - 5]);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void MultiLinePrimitive::draw() {
UINT size = m_vertex.size();
drawSnap();
2016-06-15 18:43:10 +12:00
if ((m_isEditing || m_isPrompting) && size > 0) {
tglColor(m_isEditing ? m_color : TPixel32::Green);
std::vector<TPointD> points;
points.assign(m_vertex.begin(), m_vertex.end());
int count = points.size();
if (count % 4 == 1) {
// No speedOut
points.push_back(points[count - 1]);
count++;
} else if (m_ctrlDown)
points[count - 1] = points[count - 2];
points.push_back(0.5 * (m_mousePosition + points[count - 1]));
points.push_back(m_mousePosition);
points.push_back(m_mousePosition);
double pixelSize = m_tool->getPixelSize();
TStroke *stroke = new TStroke(points);
drawStrokeCenterline(*stroke, pixelSize);
delete stroke;
if (m_vertex.size() > 1) {
tglColor(TPixel(79, 128, 255));
int index = (count < 5) ? count - 1 : count - 5;
// Disegno lo speedOut precedente (che e' quello corrente solo nel caso in
// cui count < 5)
TPointD p0 = m_vertex[index];
TPointD p1 = m_vertex[index - 1];
if (tdistance(p0, p1) > 0.1) {
tglDrawSegment(p0, p1);
tglDrawDisk(p0, 2 * pixelSize);
tglDrawDisk(p1, 4 * pixelSize);
}
// Disegno lo speedIn/Out corrente nel caso in cui count > 5
if (m_speedMoved && count > 5) {
TPointD p0 = m_vertex[count - 1];
TPointD p1 = m_vertex[count - 2];
TPointD p2 = m_vertex[count - 3];
tglDrawSegment(p0, p2);
tglDrawDisk(p0, 2 * pixelSize);
tglDrawDisk(p1, 4 * pixelSize);
tglDrawDisk(p2, 2 * pixelSize);
}
}
if (m_closed)
tglColor(TPixel32((m_color.r + 127) % 255, m_color.g,
(m_color.b + 127) % 255, m_color.m));
else
tglColor(m_color);
tglDrawCircle(m_vertex[0], joinDistance * pixelSize);
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void MultiLinePrimitive::leftButtonDown(const TPointD &pos,
const TMouseEvent &e) {
TTool::Application *app = TTool::getApplication();
if (!app) return;
if (app->getCurrentObject()->isSpline()) {
m_color = TPixel32::Red;
m_isEditing = true;
} else {
const TColorStyle *style = app->getCurrentLevelStyle();
if (style) {
m_isEditing = style->isStrokeStyle();
m_color = style->getAverageColor();
} else {
m_isEditing = false;
m_color = TPixel32::Black;
}
}
if (!m_isEditing) return;
m_undo = new MultilinePrimitiveUndo(m_vertex, this);
TUndoManager::manager()->add(m_undo);
m_mousePosition = pos;
TPointD newPos;
newPos = getSnap(pos);
2016-06-15 18:43:10 +12:00
// Se clicco nell'ultimo vertice chiudo la linea.
2019-10-11 15:57:49 +13:00
TPointD _pos = pos;
2016-06-15 18:43:10 +12:00
if (m_closed) _pos = m_vertex.front();
if (e.isShiftPressed() && !m_vertex.empty())
addVertex(rectify(m_vertex.back(), _pos));
else
addVertex(newPos);
2016-06-15 18:43:10 +12:00
m_undo->setNewVertex(m_vertex);
m_beforeSpeedMoved = m_speedMoved;
m_speedMoved = false;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void MultiLinePrimitive::leftButtonDrag(const TPointD &pos,
const TMouseEvent &e) {
if (m_vertex.size() == 0 || m_isSingleLine) return;
2019-10-11 15:57:49 +13:00
if (m_speedMoved || tdistance2(m_vertex[m_vertex.size() - 1], pos) >
sq(7.0 * m_tool->getPixelSize())) {
2016-06-15 18:43:10 +12:00
moveSpeed(m_mousePosition - pos);
m_speedMoved = true;
m_undo->setNewVertex(m_vertex);
m_mousePosition = pos;
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void MultiLinePrimitive::leftButtonDoubleClick(const TPointD &,
const TMouseEvent &e) {
endLine();
resetSnap();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void MultiLinePrimitive::leftButtonUp(const TPointD &pos, const TMouseEvent &) {
if (m_closed) endLine();
resetSnap();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void MultiLinePrimitive::mouseMove(const TPointD &pos, const TMouseEvent &e) {
m_ctrlDown = e.isCtrlPressed();
TPointD newPos;
newPos = calculateSnap(pos);
newPos = checkGuideSnapping(pos);
2016-06-15 18:43:10 +12:00
if (m_isEditing) {
if (e.isShiftPressed() && !m_vertex.empty())
m_mousePosition = rectify(m_vertex.back(), newPos);
2016-06-15 18:43:10 +12:00
else
m_mousePosition = newPos;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
double dist = joinDistance * joinDistance;
2016-03-19 06:57:51 +13:00
if (!m_vertex.empty() &&
(tdistance2(pos, m_vertex.front()) < dist * m_tool->getPixelSize())) {
2016-06-15 18:43:10 +12:00
m_closed = true;
m_mousePosition = m_vertex.front();
} else
m_closed = false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
} else
m_mousePosition = newPos;
2016-06-15 18:43:10 +12:00
m_tool->invalidate();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2018-02-19 19:34:13 +13:00
bool MultiLinePrimitive::keyDown(QKeyEvent *event) {
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
2017-07-25 21:28:13 +12:00
endLine();
return true;
}
2018-02-19 19:34:13 +13:00
if (event->key() != Qt::Key_Escape || !m_isEditing) return false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
UINT size = m_vertex.size();
if (size <= 1) return 0;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!m_isSingleLine) TUndoManager::manager()->popUndo((size - 1) / 4 + 1);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_isEditing = false;
m_speedMoved = false;
m_beforeSpeedMoved = false;
m_closed = false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_vertex.clear();
return true;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TStroke *MultiLinePrimitive::makeStroke() const {
double thick = getThickness();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
/*---
* Pencilの場合はThickness1の線を1ピクセルにするためthick
* = 0 ---*/
if (m_param->m_pencil.getValue()) thick -= 1.0;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
UINT size = m_vertex.size();
if (size <= 1) return 0;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!m_isSingleLine) TUndoManager::manager()->popUndo((size - 1) / 4 + 1);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TStroke *stroke = 0;
std::vector<TThickPoint> points;
int i;
for (i = 0; i < (int)size; i++) {
TPointD vertex = m_vertex[i];
points.push_back(TThickPoint(vertex, thick));
}
stroke = new TStroke(points);
if (m_closed) stroke->setSelfLoop();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return stroke;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void MultiLinePrimitive::endLine() {
if (!m_isEditing) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_isEditing = false;
m_speedMoved = false;
m_beforeSpeedMoved = false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!m_isSingleLine && !m_vertex.empty() &&
m_vertex.size() % 4 != 1 /* && !m_rasterTool*/) {
m_vertex.erase(--m_vertex.end());
assert(m_vertex.size() == 3 || m_vertex.size() % 4 == 1);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_tool->addStroke();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_closed) m_closed = false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_vertex.clear();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void MultiLinePrimitive::onActivate() {
m_isEditing = false;
m_closed = false;
m_vertex.clear();
m_speedMoved = false;
m_beforeSpeedMoved = false;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void MultiLinePrimitive::onEnter() {
TTool::Application *app = TTool::getApplication();
if (!app) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (app->getCurrentObject()->isSpline())
m_color = TPixel32::Red;
else {
const TColorStyle *style = app->getCurrentLevelStyle();
2019-10-11 15:57:49 +13:00
if (style) m_color = style->getAverageColor();
2016-06-15 18:43:10 +12:00
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void MultiLinePrimitive::onImageChanged() { onActivate(); }
2016-03-19 06:57:51 +13:00
//=============================================================================
// Line Primitive Class Implementation
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void LinePrimitive::draw() {
UINT size = m_vertex.size();
2016-03-19 06:57:51 +13:00
drawSnap();
2016-06-15 18:43:10 +12:00
tglColor(TPixel32::Red);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_isEditing || m_isPrompting) {
glBegin(GL_LINE_STRIP);
tglVertex(m_vertex[0]);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
tglVertex(m_mousePosition);
glEnd();
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
void LinePrimitive::mouseMove(const TPointD &pos, const TMouseEvent &e) {
TPointD newPos = calculateSnap(pos);
newPos = checkGuideSnapping(pos);
m_tool->invalidate();
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void LinePrimitive::leftButtonDown(const TPointD &pos, const TMouseEvent &e) {
TTool::Application *app = TTool::getApplication();
if (!app) return;
if (app->getCurrentObject()->isSpline()) {
m_color = TPixel32::Red;
m_isEditing = true;
} else {
const TColorStyle *style = app->getCurrentLevelStyle();
if (style) {
m_isEditing = style->isStrokeStyle();
m_color = style->getAverageColor();
} else {
m_isEditing = false;
m_color = TPixel32::Black;
}
}
if (!m_isEditing) return;
TPointD newPos = getSnap(pos);
m_mousePosition = newPos;
2016-06-15 18:43:10 +12:00
TPointD _pos = newPos;
2016-06-15 18:43:10 +12:00
if (m_param->m_pencil.getValue() &&
(m_param->m_targetType & TTool::ToonzImage ||
m_param->m_targetType & TTool::RasterImage)) {
if (m_param->m_rasterToolSize.getValue() % 2 != 0)
_pos = TPointD((int)newPos.x, (int)newPos.y);
2016-06-15 18:43:10 +12:00
else
_pos = TPointD((int)newPos.x + 0.5, (int)newPos.y + 0.5);
2016-06-15 18:43:10 +12:00
}
if (m_vertex.size() == 0)
addVertex(_pos);
else {
if (e.isShiftPressed() && !m_vertex.empty())
addVertex(rectify(m_vertex.back(), pos));
2016-06-15 18:43:10 +12:00
else
addVertex(_pos);
endLine();
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void LinePrimitive::leftButtonDrag(const TPointD &pos, const TMouseEvent &e) {
if (!m_isEditing) return;
TPointD newPos = calculateSnap(pos);
newPos = checkGuideSnapping(pos);
2016-03-19 06:57:51 +13:00
m_mousePosition = newPos;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void LinePrimitive::leftButtonUp(const TPointD &pos, const TMouseEvent &e) {
// snapping
TPointD newPos = getSnap(pos);
m_mousePosition = newPos;
2016-06-15 18:43:10 +12:00
if (e.isShiftPressed() && !m_vertex.empty())
m_vertex.push_back(rectify(m_vertex.back(), pos));
else
m_vertex.push_back(newPos);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
endLine();
resetSnap();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
//=============================================================================
// Ellipse Primitive Class Implementation
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void EllipsePrimitive::draw() {
drawSnap();
2016-06-15 18:43:10 +12:00
if (m_isEditing || m_isPrompting ||
areAlmostEqual(m_selectingRect.x0, m_selectingRect.x1) ||
areAlmostEqual(m_selectingRect.y0, m_selectingRect.y1)) {
tglColor(m_isEditing ? m_color : TPixel32::Green);
TPointD centre = TPointD((m_selectingRect.x0 + m_selectingRect.x1) * 0.5,
(m_selectingRect.y0 + m_selectingRect.y1) * 0.5);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
glPushMatrix();
tglMultMatrix(TScale(centre, m_selectingRect.x1 - m_selectingRect.x0,
m_selectingRect.y1 - m_selectingRect.y0));
tglDrawCircle(centre, 0.5);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
glPopMatrix();
drawRect(m_selectingRect, m_color, 0x5555, true);
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void EllipsePrimitive::leftButtonDown(const TPointD &pos, const TMouseEvent &) {
TTool::Application *app = TTool::getApplication();
if (!app) return;
TPointD newPos = getSnap(pos);
m_startPoint = newPos;
m_selectingRect.x0 = newPos.x;
m_selectingRect.y0 = newPos.y;
m_selectingRect.x1 = newPos.x;
m_selectingRect.y1 = newPos.y;
2016-06-15 18:43:10 +12:00
if (app->getCurrentObject()->isSpline()) {
m_isEditing = true;
m_color = TPixel32::Red;
} else {
const TColorStyle *style = app->getCurrentLevelStyle();
if (style) {
m_isEditing = style->isStrokeStyle();
m_color = style->getAverageColor();
} else {
m_isEditing = false;
m_color = TPixel32::Black;
}
}
if (!m_isEditing) return;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void EllipsePrimitive::leftButtonDrag(const TPointD &realPos,
const TMouseEvent &e) {
if (!m_isEditing) return;
TPointD pos;
if (e.isShiftPressed()) {
double distance = tdistance(realPos, m_startPoint) * M_SQRT1_2;
pos.x = (realPos.x > m_startPoint.x) ? m_startPoint.x + distance
: m_startPoint.x - distance;
pos.y = (realPos.y > m_startPoint.y) ? m_startPoint.y + distance
: m_startPoint.y - distance;
} else {
pos = calculateSnap(realPos);
pos = checkGuideSnapping(realPos);
2016-06-15 18:43:10 +12:00
}
m_pos = pos;
m_selectingRect.x1 = pos.x;
m_selectingRect.y1 = pos.y;
if (!e.isAltPressed()) {
m_selectingRect.x0 = m_startPoint.x;
m_selectingRect.y0 = m_startPoint.y;
} else {
m_selectingRect.x0 = m_startPoint.x + m_startPoint.x - pos.x;
m_selectingRect.y0 = m_startPoint.y + m_startPoint.y - pos.y;
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TStroke *EllipsePrimitive::makeStroke() const {
if (areAlmostEqual(m_selectingRect.x0, m_selectingRect.x1) ||
areAlmostEqual(m_selectingRect.y0, m_selectingRect.y1))
return 0;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return makeEllipticStroke(
2020-01-29 13:22:35 +13:00
getThickness(),
2019-10-11 15:57:49 +13:00
TPointD(0.5 * (m_selectingRect.x0 + m_selectingRect.x1),
0.5 * (m_selectingRect.y0 + m_selectingRect.y1)),
2016-06-15 18:43:10 +12:00
fabs(0.5 * (m_selectingRect.x1 - m_selectingRect.x0)),
fabs(0.5 * (m_selectingRect.y1 - m_selectingRect.y0)));
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void EllipsePrimitive::leftButtonUp(const TPointD &pos, const TMouseEvent &) {
if (!m_isEditing) return;
m_isEditing = false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_tool->addStroke();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void EllipsePrimitive::mouseMove(const TPointD &pos, const TMouseEvent &e) {
m_pos = calculateSnap(pos);
m_pos = checkGuideSnapping(pos);
m_tool->invalidate();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void EllipsePrimitive::onEnter() {
TTool::Application *app = TTool::getApplication();
if (!app) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (app->getCurrentObject()->isSpline()) {
m_color = TPixel32::Red;
} else {
const TColorStyle *style = app->getCurrentLevelStyle();
2019-10-11 15:57:49 +13:00
if (style) m_color = style->getAverageColor();
2016-06-15 18:43:10 +12:00
}
2016-03-19 06:57:51 +13:00
}
//=============================================================================
// Arc Primitive Class Implementation
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void ArcPrimitive::draw() {
drawSnap();
2016-06-15 18:43:10 +12:00
switch (m_clickNumber) {
case 1:
tglColor(m_color);
tglDrawSegment(m_startPoint, m_endPoint);
break;
case 2:
assert(m_stroke);
if (m_stroke) {
tglColor(m_isPrompting ? TPixel32::Green : m_color);
if (!m_isPrompting) {
glLineStipple(1, 0x5555);
glEnable(GL_LINE_STIPPLE);
glBegin(GL_LINE_STRIP);
tglVertex(m_stroke->getControlPoint(0));
tglVertex(m_centralPoint);
tglVertex(m_stroke->getControlPoint(8));
glEnd();
glDisable(GL_LINE_STIPPLE);
}
drawStrokeCenterline(*m_stroke, sqrt(tglGetPixelSize2()));
}
break;
};
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TStroke *ArcPrimitive::makeStroke() const { return new TStroke(*m_stroke); }
//-----------------------------------------------------------------------------
void ArcPrimitive::leftButtonUp(const TPointD &pos, const TMouseEvent &) {
TTool::Application *app = TTool::getApplication();
if (!app) return;
TPointD newPos = getSnap(pos);
2016-06-15 18:43:10 +12:00
std::vector<TThickPoint> points(9);
double thick = getThickness();
switch (m_clickNumber) {
case 0:
if (app->getCurrentObject()->isSpline()) {
m_isEditing = true;
m_color = TPixel32::Red;
} else {
const TColorStyle *style = app->getCurrentLevelStyle();
if (style) {
m_isEditing = style->isStrokeStyle();
m_color = style->getAverageColor();
} else {
m_isEditing = false;
m_color = TPixel32::Black;
}
}
if (!m_isEditing) return;
m_endPoint = m_startPoint = newPos;
2016-06-15 18:43:10 +12:00
m_clickNumber++;
break;
case 1:
points[0] = TThickPoint(m_startPoint, thick);
points[8] = TThickPoint(newPos, thick);
2016-06-15 18:43:10 +12:00
points[4] = TThickPoint(0.5 * (points[0] + points[8]), thick);
points[2] = TThickPoint(0.5 * (points[0] + points[4]), thick);
points[6] = TThickPoint(0.5 * (points[4] + points[8]), thick);
points[1] = TThickPoint(0.5 * (points[0] + points[2]), thick);
points[3] = TThickPoint(0.5 * (points[2] + points[4]), thick);
points[5] = TThickPoint(0.5 * (points[4] + points[6]), thick);
points[7] = TThickPoint(0.5 * (points[6] + points[8]), thick);
if (m_stroke) delete m_stroke;
m_stroke = new TStroke(points);
m_clickNumber++;
break;
case 2:
m_tool->addStroke();
m_stroke = 0;
m_clickNumber = 0;
break;
}
resetSnap();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void ArcPrimitive::mouseMove(const TPointD &pos, const TMouseEvent &e) {
TPointD newPos = calculateSnap(pos);
newPos = checkGuideSnapping(pos);
2016-06-15 18:43:10 +12:00
switch (m_clickNumber) {
case 1:
if (e.isShiftPressed())
m_endPoint = rectify(m_startPoint, pos);
else
m_endPoint = newPos;
2016-06-15 18:43:10 +12:00
break;
case 2:
m_centralPoint = TThickPoint(newPos, getThickness());
2016-06-15 18:43:10 +12:00
TThickQuadratic q(m_stroke->getControlPoint(0), m_centralPoint,
m_stroke->getControlPoint(8));
TThickQuadratic q0, q1, q00, q01, q10, q11;
q.split(0.5, q0, q1);
q0.split(0.5, q00, q01);
q1.split(0.5, q10, q11);
assert(q00.getP2() == q01.getP0());
assert(q01.getP2() == q10.getP0());
assert(q10.getP2() == q11.getP0());
m_stroke->setControlPoint(0, q00.getP0());
m_stroke->setControlPoint(1, q00.getP1());
m_stroke->setControlPoint(2, q00.getP2());
m_stroke->setControlPoint(3, q01.getP1());
m_stroke->setControlPoint(4, q01.getP2());
m_stroke->setControlPoint(5, q10.getP1());
m_stroke->setControlPoint(6, q10.getP2());
m_stroke->setControlPoint(7, q11.getP1());
m_stroke->setControlPoint(8, q11.getP2());
break;
}
m_tool->invalidate();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void ArcPrimitive::onEnter() {
TTool::Application *app = TTool::getApplication();
if (!app) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (app->getCurrentObject()->isSpline())
m_color = TPixel32::Red;
else {
const TColorStyle *style = app->getCurrentLevelStyle();
2019-10-11 15:57:49 +13:00
if (style) m_color = style->getAverageColor();
2016-06-15 18:43:10 +12:00
}
2016-03-19 06:57:51 +13:00
}
//=============================================================================
// Polygon Primitive Class Declaration
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PolygonPrimitive::draw() {
drawSnap();
2016-06-15 18:43:10 +12:00
if (!m_isEditing && !m_isPrompting) return;
tglColor(m_isEditing ? m_color : TPixel32::Green);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int edgeCount = m_param->m_edgeCount.getValue();
if (edgeCount == 0) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
double angleDiff = M_2PI / edgeCount;
double angle = (3 * M_PI + angleDiff) * 0.5;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
glBegin(GL_LINE_LOOP);
for (int i = 0; i < edgeCount; i++) {
tglVertex(m_centre + TPointD(cos(angle) * m_radius, sin(angle) * m_radius));
angle += angleDiff;
}
glEnd();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PolygonPrimitive::leftButtonDown(const TPointD &pos, const TMouseEvent &) {
TTool::Application *app = TTool::getApplication();
if (!app) return;
if (app->getCurrentObject()->isSpline()) {
m_isEditing = true;
m_color = TPixel32::Red;
} else {
const TColorStyle *style = app->getCurrentLevelStyle();
if (style) {
m_isEditing = style->isStrokeStyle();
m_color = style->getAverageColor();
} else {
m_isEditing = false;
m_color = TPixel32::Black;
}
}
if (!m_isEditing) return;
m_centre = getSnap(pos);
2016-06-15 18:43:10 +12:00
m_radius = 0;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PolygonPrimitive::leftButtonDrag(const TPointD &pos,
const TMouseEvent &e) {
if (!m_isEditing) return;
TPointD newPos = calculateSnap(pos);
2018-02-19 19:34:13 +13:00
newPos = checkGuideSnapping(pos);
m_radius = tdistance(m_centre, newPos);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TStroke *PolygonPrimitive::makeStroke() const {
double thick = getThickness();
int edgeCount = m_param->m_edgeCount.getValue();
if (edgeCount == 0) return 0;
double angleDiff = M_2PI / (double)edgeCount;
double angle = (3 * M_PI + angleDiff) * 0.5;
TStroke *stroke = 0;
if (m_param->m_targetType & TTool::Vectors) {
std::vector<TThickPoint> points(4 * edgeCount + 1);
int i;
// Posiziono gli angoli
for (i = 0; i <= (int)points.size(); i += 4) {
points[i] = TThickPoint(
m_centre + TPointD(cos(angle) * m_radius, sin(angle) * m_radius),
thick);
angle += angleDiff;
}
// posiziono i punti medi e i punti per gestire la linearita'
for (i = 0; i < (int)points.size() - 1; i += 4) {
TPointD vertex = convert(points[i]);
TPointD nextVertex = convert(points[i + 4]);
TPointD speed = computeSpeed(vertex, nextVertex, 0.01);
TPointD speedOutPoint = vertex + speed;
TPointD speedInNextPoint = nextVertex - speed;
TPointD middlePoint = 0.5 * (speedOutPoint + speedInNextPoint);
points[i + 1] = TThickPoint(speedOutPoint, thick);
points[i + 2] = TThickPoint(middlePoint, thick);
points[i + 3] = TThickPoint(speedInNextPoint, thick);
}
stroke = new TStroke(points);
} else if (m_param->m_targetType & TTool::ToonzImage ||
m_param->m_targetType & TTool::RasterImage) {
std::vector<TThickPoint> points(edgeCount + edgeCount + 1);
points[0] = TThickPoint(
m_centre + TPointD(cos(angle) * m_radius, sin(angle) * m_radius),
thick);
for (int i = 1; i <= edgeCount; i++) {
angle += angleDiff;
points[i + i] = TThickPoint(
m_centre + TPointD(cos(angle) * m_radius, sin(angle) * m_radius),
thick);
points[i + i - 1] = (points[i + i - 2] + points[i + i]) * 0.5;
}
stroke = new TStroke(points);
}
stroke->setSelfLoop();
return stroke;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PolygonPrimitive::leftButtonUp(const TPointD &pos, const TMouseEvent &) {
if (!m_isEditing) return;
m_isEditing = false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_tool->addStroke();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
void PolygonPrimitive::mouseMove(const TPointD &pos, const TMouseEvent &e) {
TPointD newPos = calculateSnap(pos);
newPos = checkGuideSnapping(pos);
m_tool->invalidate();
}