1 | package org.expeditee.items.MagneticConstraint.Utilities;
|
---|
2 |
|
---|
3 | import java.awt.Point;
|
---|
4 | import java.awt.Rectangle;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.LinkedList;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import org.expeditee.gui.DisplayIO;
|
---|
10 | import org.expeditee.gui.Frame;
|
---|
11 | import org.expeditee.gui.FrameIO;
|
---|
12 | import org.expeditee.gui.FrameUtils;
|
---|
13 | import org.expeditee.items.Dot;
|
---|
14 | import org.expeditee.items.Item;
|
---|
15 | import org.expeditee.items.MagneticConstraint.MagneticConstraintAction;
|
---|
16 |
|
---|
17 | public class BoxLogic {
|
---|
18 | public static List<org.expeditee.items.Line> getVerticalLines(
|
---|
19 | final Frame frame) {
|
---|
20 | final List<org.expeditee.items.Line> ret = new LinkedList<org.expeditee.items.Line>();
|
---|
21 | final Collection<Item> allItems = frame.getAllItems();
|
---|
22 | final List<Dot> dots = new LinkedList<Dot>();
|
---|
23 | for (final Item canditate : allItems)
|
---|
24 | if (canditate instanceof Dot)
|
---|
25 | dots.add((Dot) canditate);
|
---|
26 | for (final Dot dot : dots) {
|
---|
27 | final List<org.expeditee.items.Line> lines = dot.getLines();
|
---|
28 | for (final org.expeditee.items.Line vertCanditate : lines) {
|
---|
29 | if (!ret.contains(vertCanditate)
|
---|
30 | && vertCanditate.getStartItem().getX() == vertCanditate
|
---|
31 | .getEndItem().getX())
|
---|
32 | ret.add(vertCanditate);
|
---|
33 | }
|
---|
34 | }
|
---|
35 | return ret;
|
---|
36 | }
|
---|
37 |
|
---|
38 | public static boolean boxCollision(final Item item) {
|
---|
39 | final List<org.expeditee.items.Line> verticalLines = BoxLogic
|
---|
40 | .getVerticalLines(item.getParent());
|
---|
41 | for (final org.expeditee.items.Line canditate : verticalLines) {
|
---|
42 | final int topPos = Math.min(canditate.getStartItem().getY(),
|
---|
43 | canditate.getEndItem().getY());
|
---|
44 | final int bottomPos = Math.max(canditate.getStartItem().getY(),
|
---|
45 | canditate.getEndItem().getY());
|
---|
46 | if (topPos < item.getY() && bottomPos > item.getY()) {
|
---|
47 | if (item.getX() + item.getBoundsWidth() > canditate
|
---|
48 | .getStartItem().getX()
|
---|
49 | && item.getX() < canditate.getStartItem().getX()) {
|
---|
50 | return true;
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|
54 | return false;
|
---|
55 | }
|
---|
56 |
|
---|
57 | public static void resolveCollision(final Item item, final MagneticConstraintAction remagnet) {
|
---|
58 | boolean updateCursor = FrameUtils.getCurrentItem() == item;
|
---|
59 | final Line currentLine = Line.getLineContainingToken(item);
|
---|
60 | final Paragraph currentParagraph = Paragraph.getParagraphFromLine(currentLine);
|
---|
61 | final Point newLineAt = currentParagraph.split(currentLine);
|
---|
62 | final Line lineFromToken = Line.getLineFromToken(item);
|
---|
63 | lineFromToken.deltaX((int) newLineAt.getX() - lineFromToken.getFirst().getX());
|
---|
64 | lineFromToken.deltaY((int) newLineAt.getY() - lineFromToken.getFirst().getY());
|
---|
65 | remagnet.callback(null);
|
---|
66 | final Line nextLine = lineFromToken.next();
|
---|
67 | if(nextLine != null && nextLine.getBoundingBox().getX() >= lineFromToken.getBoundingBox().getX()) {
|
---|
68 | final Paragraph paragraphFromNewLine = Paragraph.getParagraphFromLine(lineFromToken);
|
---|
69 | final Paragraph paragraphFromNextLine = Paragraph.getParagraphFromLine(nextLine);
|
---|
70 | if(paragraphFromNewLine.containsAll(paragraphFromNextLine)) {
|
---|
71 | final Rectangle newLineBoundingBox = lineFromToken.getBoundingBox();
|
---|
72 | nextLine.setX((int) newLineBoundingBox.getMaxX());
|
---|
73 | nextLine.setY((int) newLineBoundingBox.getY());
|
---|
74 | }
|
---|
75 | }
|
---|
76 | if(updateCursor) DisplayIO.MoveCursorToEndOfItem(item);
|
---|
77 | FrameIO.ForceSaveFrame(item.getParent());
|
---|
78 | }
|
---|
79 | }
|
---|