mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 17:22:09 +10:00
Added optional arrow to the "Line" component together with configurable
thickness
This commit is contained in:
@@ -21,9 +21,11 @@
|
|||||||
*/
|
*/
|
||||||
package org.diylc.core.measures;
|
package org.diylc.core.measures;
|
||||||
|
|
||||||
|
import org.diylc.utils.Constants;
|
||||||
|
|
||||||
public enum SizeUnit implements Unit {
|
public enum SizeUnit implements Unit {
|
||||||
|
|
||||||
mm(1d), cm(10d), m(1e4d), in(25.4d), ft(25.4d * 12), yd(9144d);
|
px(25.4d / Constants.PIXELS_PER_INCH), mm(1d), cm(10d), m(1e4d), in(25.4d), ft(25.4d * 12), yd(9144d);
|
||||||
|
|
||||||
double factor;
|
double factor;
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -23,8 +23,11 @@ package org.diylc.components.connectivity;
|
|||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.awt.Polygon;
|
||||||
import java.awt.Shape;
|
import java.awt.Shape;
|
||||||
import java.awt.Stroke;
|
import java.awt.Stroke;
|
||||||
|
import java.awt.geom.AffineTransform;
|
||||||
|
|
||||||
import org.diylc.common.Display;
|
import org.diylc.common.Display;
|
||||||
import org.diylc.common.LineStyle;
|
import org.diylc.common.LineStyle;
|
||||||
@@ -40,9 +43,10 @@ import org.diylc.core.annotations.BomPolicy;
|
|||||||
import org.diylc.core.annotations.ComponentDescriptor;
|
import org.diylc.core.annotations.ComponentDescriptor;
|
||||||
import org.diylc.core.annotations.EditableProperty;
|
import org.diylc.core.annotations.EditableProperty;
|
||||||
import org.diylc.core.measures.Size;
|
import org.diylc.core.measures.Size;
|
||||||
|
import org.diylc.core.measures.SizeUnit;
|
||||||
|
|
||||||
@ComponentDescriptor(name = "Line", author = "Branislav Stojkovic", category = "Shapes",
|
@ComponentDescriptor(name = "Line", author = "Branislav Stojkovic", category = "Shapes",
|
||||||
creationMethod = CreationMethod.POINT_BY_POINT, instanceNamePrefix = "LN", description = "",
|
creationMethod = CreationMethod.POINT_BY_POINT, instanceNamePrefix = "LN", description = "Line with optional arrows",
|
||||||
zOrder = IDIYComponent.COMPONENT, bomPolicy = BomPolicy.NEVER_SHOW, autoEdit = false,
|
zOrder = IDIYComponent.COMPONENT, bomPolicy = BomPolicy.NEVER_SHOW, autoEdit = false,
|
||||||
transformer = SimpleComponentTransformer.class)
|
transformer = SimpleComponentTransformer.class)
|
||||||
public class Line extends AbstractLeadedComponent<Void> {
|
public class Line extends AbstractLeadedComponent<Void> {
|
||||||
@@ -53,6 +57,12 @@ public class Line extends AbstractLeadedComponent<Void> {
|
|||||||
|
|
||||||
private Color color = COLOR;
|
private Color color = COLOR;
|
||||||
protected LineStyle style = LineStyle.SOLID;
|
protected LineStyle style = LineStyle.SOLID;
|
||||||
|
private Size thickness = new Size(1d, SizeUnit.px);
|
||||||
|
private Size arrowSize = new Size(5d, SizeUnit.px);
|
||||||
|
private Polygon arrow = null;
|
||||||
|
private AffineTransform arrowTx = new AffineTransform();
|
||||||
|
private boolean arrowStart = false;
|
||||||
|
private boolean arrowEnd = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawIcon(Graphics2D g2d, int width, int height) {
|
public void drawIcon(Graphics2D g2d, int width, int height) {
|
||||||
@@ -64,11 +74,11 @@ public class Line extends AbstractLeadedComponent<Void> {
|
|||||||
@Override
|
@Override
|
||||||
public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode, Project project,
|
public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode, Project project,
|
||||||
IDrawingObserver drawingObserver) {
|
IDrawingObserver drawingObserver) {
|
||||||
float thickness = 1f;
|
float thickness = (float) getThickness().convertToPixels();
|
||||||
Stroke stroke = null;
|
Stroke stroke = null;
|
||||||
switch (getStyle()) {
|
switch (getStyle()) {
|
||||||
case SOLID:
|
case SOLID:
|
||||||
stroke = ObjectCache.getInstance().fetchBasicStroke(thickness);
|
stroke = ObjectCache.getInstance().fetchZoomableStroke(thickness);
|
||||||
break;
|
break;
|
||||||
case DASHED:
|
case DASHED:
|
||||||
stroke = ObjectCache.getInstance().fetchStroke(thickness, new float[] {thickness * 2, thickness * 4}, thickness * 4);
|
stroke = ObjectCache.getInstance().fetchStroke(thickness, new float[] {thickness * 2, thickness * 4}, thickness * 4);
|
||||||
@@ -78,8 +88,47 @@ public class Line extends AbstractLeadedComponent<Void> {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
g2d.setStroke(stroke);
|
g2d.setStroke(stroke);
|
||||||
g2d.setColor(color);
|
g2d.setColor(componentState == ComponentState.SELECTED ? SELECTION_COLOR : color);
|
||||||
g2d.drawLine(getControlPoint(0).x, getControlPoint(0).y, getControlPoint(1).x, getControlPoint(1).y);
|
|
||||||
|
Point startPoint = new Point(getControlPoint(0));
|
||||||
|
Point endPoint = new Point(getControlPoint(1));
|
||||||
|
|
||||||
|
if (arrowStart) {
|
||||||
|
arrowTx.setToIdentity();
|
||||||
|
double angle = Math.atan2(getControlPoint(1).y - getControlPoint(0).y, getControlPoint(1).x - getControlPoint(0).x);
|
||||||
|
arrowTx.translate(getControlPoint(0).x, getControlPoint(0).y);
|
||||||
|
arrowTx.rotate((angle + Math.PI / 2d));
|
||||||
|
AffineTransform oldTx = g2d.getTransform();
|
||||||
|
g2d.transform(arrowTx);
|
||||||
|
g2d.fill(getArrow());
|
||||||
|
g2d.setTransform(oldTx);
|
||||||
|
|
||||||
|
double distance = distance(startPoint, endPoint);
|
||||||
|
interpolate(startPoint, endPoint, getArrowSize().convertToPixels() * 0.9 / distance, startPoint);
|
||||||
|
}
|
||||||
|
if (arrowEnd) {
|
||||||
|
arrowTx.setToIdentity();
|
||||||
|
double angle = Math.atan2(getControlPoint(1).y - getControlPoint(0).y, getControlPoint(1).x - getControlPoint(0).x);
|
||||||
|
arrowTx.translate(getControlPoint(1).x, getControlPoint(1).y);
|
||||||
|
arrowTx.rotate((angle - Math.PI / 2d));
|
||||||
|
AffineTransform oldTx = g2d.getTransform();
|
||||||
|
g2d.transform(arrowTx);
|
||||||
|
g2d.fill(getArrow());
|
||||||
|
g2d.setTransform(oldTx);
|
||||||
|
|
||||||
|
double distance = distance(startPoint, endPoint);
|
||||||
|
interpolate(endPoint, startPoint, getArrowSize().convertToPixels() * 0.9 / distance, endPoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
g2d.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void interpolate(Point p1, Point p2, double t, Point p) {
|
||||||
|
p.setLocation((int)Math.round(p1.x * (1-t) + p2.x * t), (int)Math.round(p1.y * (1-t) + p2.y * t));
|
||||||
|
}
|
||||||
|
|
||||||
|
private double distance(Point p1, Point p2) {
|
||||||
|
return Math.sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -88,6 +137,40 @@ public class Line extends AbstractLeadedComponent<Void> {
|
|||||||
: color;
|
: color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Polygon getArrow() {
|
||||||
|
if (arrow == null) {
|
||||||
|
arrow = new Polygon();
|
||||||
|
int t = (int) getArrowSize().convertToPixels();
|
||||||
|
arrow.addPoint(0, 0);
|
||||||
|
arrow.addPoint(-t, -t * 2);
|
||||||
|
arrow.addPoint(t, -t * 2);
|
||||||
|
}
|
||||||
|
return arrow;
|
||||||
|
}
|
||||||
|
|
||||||
|
@EditableProperty
|
||||||
|
public Size getThickness() {
|
||||||
|
if (thickness == null)
|
||||||
|
thickness = new Size(1d, SizeUnit.px);
|
||||||
|
return thickness;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setThickness(Size thickness) {
|
||||||
|
this.thickness = thickness;
|
||||||
|
}
|
||||||
|
|
||||||
|
@EditableProperty
|
||||||
|
public Size getArrowSize() {
|
||||||
|
if (arrowSize == null)
|
||||||
|
arrowSize = thickness = new Size(1d, SizeUnit.px);
|
||||||
|
return arrowSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setArrowSize(Size arrowSize) {
|
||||||
|
this.arrowSize = arrowSize;
|
||||||
|
arrow = null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Color getLeadColor() {
|
public Color getLeadColor() {
|
||||||
return super.getLeadColor();
|
return super.getLeadColor();
|
||||||
@@ -113,6 +196,24 @@ public class Line extends AbstractLeadedComponent<Void> {
|
|||||||
this.style = style;
|
this.style = style;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@EditableProperty(name = "Start Arrow")
|
||||||
|
public boolean getArrowStart() {
|
||||||
|
return arrowStart;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setArrowStart(boolean arrowStart) {
|
||||||
|
this.arrowStart = arrowStart;
|
||||||
|
}
|
||||||
|
|
||||||
|
@EditableProperty(name = "End Arrow")
|
||||||
|
public boolean getArrowEnd() {
|
||||||
|
return arrowEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setArrowEnd(boolean arrowEnd) {
|
||||||
|
this.arrowEnd = arrowEnd;
|
||||||
|
}
|
||||||
|
|
||||||
public Color getBodyColor() {
|
public Color getBodyColor() {
|
||||||
return super.getBodyColor();
|
return super.getBodyColor();
|
||||||
}
|
}
|
||||||
@@ -177,4 +278,10 @@ public class Line extends AbstractLeadedComponent<Void> {
|
|||||||
public Display getDisplay() {
|
public Display getDisplay() {
|
||||||
return super.getDisplay();
|
return super.getDisplay();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
|
@Override
|
||||||
|
public org.diylc.components.AbstractLeadedComponent.LabelOriantation getLabelOriantation() {
|
||||||
|
return super.getLabelOriantation();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@@ -2197,5 +2197,21 @@
|
|||||||
</changes>
|
</changes>
|
||||||
<url>https://github.com/bancika/diy-layout-creator/releases</url>
|
<url>https://github.com/bancika/diy-layout-creator/releases</url>
|
||||||
</org.diylc.appframework.update.Version>
|
</org.diylc.appframework.update.Version>
|
||||||
|
<org.diylc.appframework.update.Version>
|
||||||
|
<versionNumber>
|
||||||
|
<major>3</major>
|
||||||
|
<minor>49</minor>
|
||||||
|
<build>0</build>
|
||||||
|
</versionNumber>
|
||||||
|
<releaseDate>2018-12-26 00:00:00.000 CET</releaseDate>
|
||||||
|
<name></name>
|
||||||
|
<changes>
|
||||||
|
<org.diylc.appframework.update.Change>
|
||||||
|
<changeType>NEW_FEATURES</changeType>
|
||||||
|
<description>Added optional arrow to the "Line" component together with configurable thickness</description>
|
||||||
|
</org.diylc.appframework.update.Change>
|
||||||
|
</changes>
|
||||||
|
<url>https://github.com/bancika/diy-layout-creator/releases</url>
|
||||||
|
</org.diylc.appframework.update.Version>
|
||||||
</a>
|
</a>
|
||||||
</java.util.Arrays_-ArrayList>
|
</java.util.Arrays_-ArrayList>
|
||||||
|
|||||||
Reference in New Issue
Block a user