1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-02 00:20:49 +00:00
plantuml/src/net/sourceforge/plantuml/activitydiagram3/ftile/Snake.java

282 lines
9.1 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2016-01-09 12:15:40 +00:00
* (C) Copyright 2009-2017, Arnaud Roques
2013-12-10 19:36:50 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2013-12-10 19:36:50 +00:00
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
*
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
*
2013-12-10 19:36:50 +00:00
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
*
*
*/
package net.sourceforge.plantuml.activitydiagram3.ftile;
import java.awt.geom.Dimension2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
2017-09-03 16:59:24 +00:00
import java.util.Collections;
2013-12-10 19:36:50 +00:00
import java.util.List;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.Direction;
2017-04-26 17:48:37 +00:00
import net.sourceforge.plantuml.graphic.HorizontalAlignment;
2016-05-11 21:31:47 +00:00
import net.sourceforge.plantuml.graphic.HtmlColorAndStyle;
import net.sourceforge.plantuml.graphic.Rainbow;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.graphic.StringBounder;
import net.sourceforge.plantuml.graphic.TextBlock;
2015-08-05 20:17:01 +00:00
import net.sourceforge.plantuml.graphic.TextBlockUtils;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.ugraphic.CompressionTransform;
2018-06-12 20:50:45 +00:00
import net.sourceforge.plantuml.ugraphic.MinMax;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.ugraphic.UGraphic;
import net.sourceforge.plantuml.ugraphic.UPolygon;
import net.sourceforge.plantuml.ugraphic.UShape;
import net.sourceforge.plantuml.ugraphic.UTranslate;
public class Snake implements UShape {
2016-05-11 21:31:47 +00:00
private final Worm worm = new Worm();
2016-12-14 21:01:03 +00:00
private UPolygon endDecoration;
2016-05-11 21:31:47 +00:00
private final Rainbow color;
2013-12-10 19:36:50 +00:00
private TextBlock textBlock;
2016-08-25 20:45:37 +00:00
private MergeStrategy mergeable = MergeStrategy.FULL;
2015-04-07 18:18:37 +00:00
private Direction emphasizeDirection;
2017-04-26 17:48:37 +00:00
private final HorizontalAlignment horizontalAlignment;
2013-12-10 19:36:50 +00:00
2015-04-07 18:18:37 +00:00
public Snake transformX(CompressionTransform compressionTransform) {
2017-04-26 17:48:37 +00:00
final Snake result = new Snake(horizontalAlignment, color, endDecoration);
2015-04-07 18:18:37 +00:00
result.textBlock = this.textBlock;
result.mergeable = this.mergeable;
result.emphasizeDirection = this.emphasizeDirection;
2016-05-11 21:31:47 +00:00
for (Point2D.Double pt : worm) {
2015-04-07 18:18:37 +00:00
final double x = compressionTransform.transform(pt.x);
final double y = pt.y;
result.addPoint(x, y);
}
return result;
}
2016-12-14 21:01:03 +00:00
public void removeEndDecoration() {
this.endDecoration = null;
}
2017-04-26 17:48:37 +00:00
public Snake(HorizontalAlignment horizontalAlignment, Rainbow color, UPolygon endDecoration) {
2016-05-11 21:31:47 +00:00
if (color == null) {
throw new IllegalArgumentException();
}
if (color.size() == 0) {
throw new IllegalArgumentException();
}
2013-12-10 19:36:50 +00:00
this.endDecoration = endDecoration;
this.color = color;
2017-04-26 17:48:37 +00:00
this.horizontalAlignment = horizontalAlignment;
2015-04-07 18:18:37 +00:00
}
2017-04-26 17:48:37 +00:00
public Snake(HorizontalAlignment horizontalAlignment, Rainbow color) {
this(horizontalAlignment, color, null);
2013-12-10 19:36:50 +00:00
}
public void setLabel(TextBlock label) {
this.textBlock = label;
}
public Snake move(double dx, double dy) {
2017-04-26 17:48:37 +00:00
final Snake result = new Snake(horizontalAlignment, color, endDecoration);
2016-05-11 21:31:47 +00:00
for (Point2D pt : worm) {
2013-12-10 19:36:50 +00:00
result.addPoint(pt.getX() + dx, pt.getY() + dy);
}
result.textBlock = this.textBlock;
2015-04-07 18:18:37 +00:00
result.mergeable = this.mergeable;
result.emphasizeDirection = this.emphasizeDirection;
2013-12-10 19:36:50 +00:00
return result;
}
public Snake translate(UTranslate translate) {
return move(translate.getDx(), translate.getDy());
}
@Override
public String toString() {
2016-05-11 21:31:47 +00:00
return worm.toString();
2013-12-10 19:36:50 +00:00
}
public void addPoint(double x, double y) {
2016-05-11 21:31:47 +00:00
worm.addPoint(x, y);
2013-12-10 19:36:50 +00:00
}
public void addPoint(Point2D p) {
addPoint(p.getX(), p.getY());
}
public void drawInternal(UGraphic ug) {
2016-05-11 21:31:47 +00:00
if (color.size() > 1) {
drawRainbow(ug);
} else {
worm.drawInternalOneColor(ug, color.getColors().get(0), 1.5, emphasizeDirection, endDecoration);
drawInternalLabel(ug);
2013-12-10 19:36:50 +00:00
}
2016-05-11 21:31:47 +00:00
}
private void drawRainbow(UGraphic ug) {
2017-09-03 16:59:24 +00:00
List<HtmlColorAndStyle> colors = color.getColors();
2016-05-11 21:31:47 +00:00
final int colorArrowSeparationSpace = color.getColorArrowSeparationSpace();
final double move = 2 + colorArrowSeparationSpace;
final WormMutation mutation = WormMutation.create(worm, move);
2017-09-03 16:59:24 +00:00
if (mutation.isDxNegative()) {
colors = new ArrayList<HtmlColorAndStyle>(colors);
Collections.reverse(colors);
}
2016-07-25 19:25:28 +00:00
final double globalMove = -1.0 * (colors.size() - 1) / 2.0;
Worm current = worm.moveFirstPoint(mutation.getFirst().multiplyBy(globalMove));
if (mutation.size() > 2) {
current = current.moveLastPoint(mutation.getLast().multiplyBy(globalMove));
}
2016-05-11 21:31:47 +00:00
for (int i = 0; i < colors.size(); i++) {
double stroke = 1.5;
if (colorArrowSeparationSpace == 0) {
stroke = i == colors.size() - 1 ? 2.0 : 3.0;
}
current.drawInternalOneColor(ug, colors.get(i), stroke, emphasizeDirection, endDecoration);
current = mutation.mute(current);
2013-12-10 19:36:50 +00:00
}
2016-05-19 18:41:37 +00:00
final UTranslate textTranslate = mutation.getTextTranslate(colors.size());
drawInternalLabel(ug.apply(textTranslate));
2016-05-11 21:31:47 +00:00
}
private void drawInternalLabel(UGraphic ug) {
2013-12-10 19:36:50 +00:00
if (textBlock != null) {
final Point2D position = getTextBlockPosition(ug.getStringBounder());
textBlock.drawU(ug.apply(new UTranslate(position)));
}
}
public double getMaxX(StringBounder stringBounder) {
double result = -Double.MAX_VALUE;
2016-05-11 21:31:47 +00:00
for (Point2D pt : worm) {
2013-12-10 19:36:50 +00:00
result = Math.max(result, pt.getX());
}
if (textBlock != null) {
final Point2D position = getTextBlockPosition(stringBounder);
final Dimension2D dim = textBlock.calculateDimension(stringBounder);
result = Math.max(result, position.getX() + dim.getWidth());
}
return result;
}
private Point2D getTextBlockPosition(StringBounder stringBounder) {
2016-05-11 21:31:47 +00:00
final Point2D pt1 = worm.get(0);
final Point2D pt2 = worm.get(1);
2013-12-10 19:36:50 +00:00
final Dimension2D dim = textBlock.calculateDimension(stringBounder);
2017-05-10 19:51:15 +00:00
double x = Math.max(pt1.getX(), pt2.getX()) + 4;
final boolean zigzag = worm.getDirectionsCode().startsWith("DLD") || worm.getDirectionsCode().startsWith("DRD");
if (horizontalAlignment == HorizontalAlignment.CENTER && zigzag) {
2017-04-26 17:48:37 +00:00
final Point2D pt3 = worm.get(2);
x = (pt2.getX() + pt3.getX()) / 2 - dim.getWidth() / 2;
2017-05-10 19:51:15 +00:00
} else if (horizontalAlignment == HorizontalAlignment.RIGHT && zigzag) {
// final Point2D pt3 = worm.get(2);
x = Math.max(pt1.getX(), pt2.getX()) - dim.getWidth() - 4;
2017-04-26 17:48:37 +00:00
}
2013-12-10 19:36:50 +00:00
final double y = (pt1.getY() + pt2.getY()) / 2 - dim.getHeight() / 2;
2017-04-26 17:48:37 +00:00
return new Point2D.Double(x, y);
2013-12-10 19:36:50 +00:00
}
public List<Line2D> getHorizontalLines() {
final List<Line2D> result = new ArrayList<Line2D>();
2016-05-11 21:31:47 +00:00
for (int i = 0; i < worm.size() - 1; i++) {
final Point2D pt1 = worm.get(i);
final Point2D pt2 = worm.get(i + 1);
2013-12-10 19:36:50 +00:00
if (pt1.getY() == pt2.getY()) {
final Line2D line = new Line2D.Double(pt1, pt2);
result.add(line);
}
}
return result;
}
private Point2D getFirst() {
2016-05-11 21:31:47 +00:00
return worm.get(0);
2013-12-10 19:36:50 +00:00
}
public Point2D getLast() {
2016-05-11 21:31:47 +00:00
return worm.get(worm.size() - 1);
2013-12-10 19:36:50 +00:00
}
2016-05-11 21:31:47 +00:00
static boolean same(Point2D pt1, Point2D pt2) {
2015-04-07 18:18:37 +00:00
return pt1.distance(pt2) < 0.001;
2013-12-10 19:36:50 +00:00
}
2016-08-25 20:45:37 +00:00
public Snake merge(Snake other, StringBounder stringBounder) {
final MergeStrategy strategy = this.mergeable.max(other.mergeable);
if (strategy == MergeStrategy.NONE) {
2015-04-07 18:18:37 +00:00
return null;
}
2016-12-14 21:01:03 +00:00
final boolean emptyOther = TextBlockUtils.isEmpty(other.textBlock, stringBounder);
// final boolean emptyThis = TextBlockUtils.isEmpty(this.textBlock, stringBounder);
if (emptyOther == false /* || emptyThis == false */) {
2015-08-05 20:17:01 +00:00
// System.err.println("merge other.textBlock="+other.textBlock+" "+other.textBlock.calculateDimension(TextBlockUtils.getDummyStringBounder()));
2016-12-14 21:01:03 +00:00
return null;
2015-08-05 20:17:01 +00:00
}
2013-12-10 19:36:50 +00:00
if (same(this.getLast(), other.getFirst())) {
2016-07-25 19:25:28 +00:00
final UPolygon oneOf = other.endDecoration == null ? endDecoration : other.endDecoration;
2017-04-26 17:48:37 +00:00
final Snake result = new Snake(horizontalAlignment, color, oneOf);
2016-12-14 21:01:03 +00:00
// result.textBlock = oneOf(this.textBlock, other.textBlock, stringBounder);
2015-04-07 18:18:37 +00:00
result.emphasizeDirection = emphasizeDirection == null ? other.emphasizeDirection : emphasizeDirection;
2016-08-25 20:45:37 +00:00
result.worm.addAll(this.worm.merge(other.worm, strategy));
result.mergeable = strategy;
2013-12-10 19:36:50 +00:00
return result;
}
if (same(this.getFirst(), other.getLast())) {
2016-08-25 20:45:37 +00:00
return other.merge(this, stringBounder);
2013-12-10 19:36:50 +00:00
}
return null;
}
2016-12-14 21:01:03 +00:00
public boolean touches(Snake other) {
if (other.mergeable != MergeStrategy.FULL) {
return false;
}
if (other.worm.isPureHorizontal()) {
return false;
}
return same(this.getLast(), other.getFirst());
}
2016-08-25 20:45:37 +00:00
public void goUnmergeable(MergeStrategy strategy) {
this.mergeable = strategy;
2015-04-07 18:18:37 +00:00
}
public void emphasizeDirection(Direction direction) {
this.emphasizeDirection = direction;
2013-12-10 19:36:50 +00:00
}
2018-06-12 20:50:45 +00:00
public boolean doesHorizontalCross(MinMax minMax) {
return worm.doesHorizontalCross(minMax);
}
2013-12-10 19:36:50 +00:00
}