1 | package org.expeditee.items.MagneticConstraint.Utilities;
|
---|
2 |
|
---|
3 | import java.util.LinkedList;
|
---|
4 |
|
---|
5 | import org.expeditee.core.bounds.AxisAlignedBoxBounds;
|
---|
6 | import org.expeditee.items.Item;
|
---|
7 |
|
---|
8 | @SuppressWarnings("serial")
|
---|
9 | public class Line extends LinkedList<Item> {
|
---|
10 |
|
---|
11 | private Line() {
|
---|
12 | super();
|
---|
13 | }
|
---|
14 |
|
---|
15 | private Line(final Line toCopy) {
|
---|
16 | super(toCopy);
|
---|
17 | }
|
---|
18 |
|
---|
19 | public Line prependLine(final Line line) {
|
---|
20 | final int x = this.getBoundingBox().getMinX();
|
---|
21 | final int width = line.getBoundingBox().getWidth();
|
---|
22 | this.deltaX(width);
|
---|
23 | line.setX(x);
|
---|
24 | line.setY(this.getFirst().getY());
|
---|
25 | return Line.getLineFromToken(line.getFirst());
|
---|
26 | }
|
---|
27 |
|
---|
28 | public Line appendLine(final Line line) {
|
---|
29 | final int x = this.getBoundingBox().getMinX();
|
---|
30 | final int width = this.getBoundingBox().getWidth();
|
---|
31 | line.setX(x + width);
|
---|
32 | line.setY(this.getFirst().getY());
|
---|
33 | return Line.getLineFromToken(this.getFirst());
|
---|
34 | }
|
---|
35 |
|
---|
36 | public static Line getLineFromToken(final Item token) {
|
---|
37 | if(token == null) return new Line();
|
---|
38 | else {
|
---|
39 | return getLineFromToken(token, new Line());
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public static Line getLineContainingToken(final Item token) {
|
---|
44 | if(token == null) return new Line();
|
---|
45 | final Line ret = new Line();
|
---|
46 | Item current = token;
|
---|
47 | while(current.getMagnetizedItemLeft() != -1) {
|
---|
48 | final Item potentialStart = current.getParent().getItemWithID(current.getMagnetizedItemLeft());
|
---|
49 | if(potentialStart == null) break;
|
---|
50 | current = potentialStart;
|
---|
51 | ret.addFirst(current);
|
---|
52 | }
|
---|
53 | final Line following = getLineFromToken(token, new Line());
|
---|
54 | ret.addAll(following);
|
---|
55 | return ret;
|
---|
56 | // return getLineFromToken(current, new Line());
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static int getLineHeight(final Line line) {
|
---|
60 | return (int) (line.getBoundingBox().getHeight() * 1.2);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public Line setX(final int newX) {
|
---|
64 | int delta = newX - this.getFirst().getX();
|
---|
65 | deltaX(delta);
|
---|
66 | return this;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public Line setY(final int newY) {
|
---|
70 | int delta = newY - this.getFirst().getY();
|
---|
71 | deltaY(delta);
|
---|
72 | return this;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public Line deltaX(final int delta) {
|
---|
76 | for(final Item token : this) token.setPosition(token.getX() + delta, token.getY());
|
---|
77 | return this;
|
---|
78 | }
|
---|
79 |
|
---|
80 | public Line deltaY(final int delta) {
|
---|
81 | for(final Item token : this) token.setPosition(token.getX(), token.getY() + delta);
|
---|
82 | return this;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public AxisAlignedBoxBounds getBoundingBox() {
|
---|
86 | // final Rectangle rect = new Rectangle(this.getFirst().getX(), this.getFirst().getY(), this.getFirst().getBoundsWidth(), this.getFirst().getBoundsHeight());
|
---|
87 | // for(final Item token : this)
|
---|
88 | // rect.add(new Rectangle(token.getX(), token.getY(), token.getBoundsWidth(), token.getBoundsHeight()));
|
---|
89 | // return rect;
|
---|
90 | final AxisAlignedBoxBounds rect = this.getFirst().getBoundingBox();
|
---|
91 | for(final Item token : this)
|
---|
92 | rect.combineWith(token.getBoundingBox());
|
---|
93 | return rect;
|
---|
94 | }
|
---|
95 |
|
---|
96 | public Line next() {
|
---|
97 | final Item lastInThisLine = this.getLast();//this.get(this.size() - 1);
|
---|
98 | if(lastInThisLine.getMagnetizedItemBottom() == -1) return null;
|
---|
99 | else return getLineFromToken(this.get(0).getParent().getItemWithID(lastInThisLine.getMagnetizedItemBottom()));
|
---|
100 | }
|
---|
101 |
|
---|
102 | public Line last() {
|
---|
103 | final Item firstInThisLine = this.getFirst();//this.get(0);
|
---|
104 | if(firstInThisLine.getMagnetizedItemTop() == -1) return null;
|
---|
105 | else return getLineContainingToken(this.get(0).getParent().getItemWithID(firstInThisLine.getMagnetizedItemTop()));
|
---|
106 | }
|
---|
107 |
|
---|
108 | private static Line getLineFromToken(final Item token, final Line acc) {
|
---|
109 | if(token == null) return acc;
|
---|
110 | if(token.getMagnetizedItemRight() == -1) {
|
---|
111 | acc.add(token);
|
---|
112 | return acc;
|
---|
113 | } else {
|
---|
114 | final Line lineSoFar = new Line(acc);
|
---|
115 | if(lineSoFar.contains(token)) return lineSoFar;
|
---|
116 | lineSoFar.add(token);
|
---|
117 | return getLineFromToken(token.getParent().getItemWithID(token.getMagnetizedItemRight()), lineSoFar);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|