source: trunk/src/org/expeditee/items/MagneticConstraint/Utilities/BoxLogic.java@ 934

Last change on this file since 934 was 934, checked in by bln4, 10 years ago
File size: 3.3 KB
Line 
1package org.expeditee.items.MagneticConstraint.Utilities;
2
3import java.awt.Point;
4import java.awt.Rectangle;
5import java.util.Collection;
6import java.util.LinkedList;
7import java.util.List;
8
9import org.expeditee.gui.DisplayIO;
10import org.expeditee.gui.Frame;
11import org.expeditee.gui.FrameIO;
12import org.expeditee.gui.FrameUtils;
13import org.expeditee.items.Dot;
14import org.expeditee.items.Item;
15import org.expeditee.items.MagneticConstraint.MagneticConstraintAction;
16
17public 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 //System.err.println("Resolving box collision");
59 boolean updateCursor = false;
60 if(FrameUtils.getCurrentItem() == item) updateCursor = true;
61 final Line currentLine = Line.getLineContainingToken(item);
62 final Paragraph currentParagraph = Paragraph.getParagraphFromLine(currentLine);
63 final Point newLineAt = currentParagraph.split(currentLine);
64 final Line lineFromToken = Line.getLineFromToken(item);
65 lineFromToken.deltaX((int) newLineAt.getX() - lineFromToken.getFirst().getX());
66 lineFromToken.deltaY((int) newLineAt.getY() - lineFromToken.getFirst().getY());
67 remagnet.callback(null);
68 final Line nextLine = lineFromToken.next();
69 if(nextLine != null && nextLine.getBoundingBox().getX() >= lineFromToken.getBoundingBox().getX()) {
70 final Paragraph paragraphFromNewLine = Paragraph.getParagraphFromLine(lineFromToken);
71 final Paragraph paragraphFromNextLine = Paragraph.getParagraphFromLine(nextLine);
72 if(paragraphFromNewLine.containsAll(paragraphFromNextLine)) {
73 final Rectangle newLineBoundingBox = lineFromToken.getBoundingBox();
74 nextLine.setX((int) newLineBoundingBox.getMaxX());
75 nextLine.setY((int) newLineBoundingBox.getY());
76 }
77 }
78 if(updateCursor) DisplayIO.MoveCursorToEndOfItem(item);
79 FrameIO.ForceSaveFrame(item.getParent());
80 FrameIO.Reload();
81 }
82}
Note: See TracBrowser for help on using the repository browser.