1 | package org.expeditee.items.MagneticConstraint.Actions;
|
---|
2 |
|
---|
3 | import org.expeditee.core.Point;
|
---|
4 | import org.expeditee.gui.DisplayController;
|
---|
5 | import org.expeditee.gui.FrameIO;
|
---|
6 | import org.expeditee.gui.FrameUtils;
|
---|
7 | import org.expeditee.items.Item;
|
---|
8 | import org.expeditee.items.Text;
|
---|
9 | import org.expeditee.items.MagneticConstraint.MagneticConstraintAction;
|
---|
10 | import org.expeditee.items.MagneticConstraint.Utilities.Line;
|
---|
11 | import org.expeditee.items.MagneticConstraint.Utilities.Paragraph;
|
---|
12 | import org.expeditee.items.MagneticConstraint.Utilities.TextLogic;
|
---|
13 |
|
---|
14 | public class RepelTextDownAction extends MagneticConstraintAction {
|
---|
15 |
|
---|
16 | public static boolean MergeSameLineParagraphCollisions = false;
|
---|
17 |
|
---|
18 | @Override
|
---|
19 | public boolean exec(final Item item) {
|
---|
20 | if(item == null || item.getParent() == null || !this.isSpIDERCodePage(item.getParent())) return false;
|
---|
21 | callback(item);
|
---|
22 | //Calculate content that needs to be moved to next line.
|
---|
23 | //1. Get remainder of line
|
---|
24 | final Line remainderOfLine = Line.getLineFromToken(item);
|
---|
25 | //2. Check if the current item should be included; remove it if not.
|
---|
26 | if(remainderOfLine.getFirst().getText().isEmpty() ||
|
---|
27 | (FrameUtils.getCurrentItem().getPosition().equals(item.getPosition())
|
---|
28 | && remainderOfLine.size() > 0
|
---|
29 | && TextLogic.XIsTowardsRight(DisplayController.getMouseX(), (Text) remainderOfLine.getFirst()))) {
|
---|
30 | remainderOfLine.removeFirst();
|
---|
31 | }
|
---|
32 |
|
---|
33 | final Line currentLine = Line.getLineContainingToken(item);
|
---|
34 | final Paragraph currentParagraph = Paragraph.getParagraphFromLine(currentLine);
|
---|
35 | final Line nextLine = currentLine.next();
|
---|
36 |
|
---|
37 | final Point newLineAt;
|
---|
38 | //Move content contained in 'remainderOfLine'
|
---|
39 |
|
---|
40 |
|
---|
41 | if(!remainderOfLine.isEmpty() && currentParagraph.contains(nextLine) && MergeSameLineParagraphCollisions) {
|
---|
42 | nextLine.prependLine(remainderOfLine);
|
---|
43 | DisplayController.setTextCursor((Text) remainderOfLine.getFirst(), Text.HOME);
|
---|
44 | callback(remainderOfLine.getFirst());
|
---|
45 | FrameIO.SaveFrame(remainderOfLine.getLast().getParent());
|
---|
46 | } else if(!remainderOfLine.isEmpty()) {
|
---|
47 | newLineAt = currentParagraph.split(currentLine);
|
---|
48 | final Point first = remainderOfLine.getBoundingBox().getTopLeft();
|
---|
49 | for(final Item token : remainderOfLine) {
|
---|
50 | double newX = token.getX() - first.getX() + newLineAt.getX();
|
---|
51 | token.setPosition((float) newX, (float) newLineAt.getY());
|
---|
52 | }
|
---|
53 | DisplayController.setTextCursor((Text) remainderOfLine.getFirst(), Text.HOME);
|
---|
54 | callback(remainderOfLine.getFirst());
|
---|
55 | FrameIO.SaveFrame(remainderOfLine.getLast().getParent());
|
---|
56 | }else {
|
---|
57 | //3a No. Move the cursor
|
---|
58 | newLineAt = currentParagraph.split(currentLine);
|
---|
59 | DisplayController.setCursorPosition(newLineAt);
|
---|
60 | }
|
---|
61 |
|
---|
62 | return true;
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public boolean invert(Item item) {
|
---|
67 | System.err.println("Repel text down action does not have a inverted implementation.");
|
---|
68 | return false;
|
---|
69 | }
|
---|
70 |
|
---|
71 | }
|
---|