1 | package org.expeditee.items.MagneticConstraint.Actions;
|
---|
2 |
|
---|
3 | import org.expeditee.core.Point;
|
---|
4 | import org.expeditee.gio.EcosystemManager;
|
---|
5 | import org.expeditee.gui.FrameUtils;
|
---|
6 | import org.expeditee.items.Item;
|
---|
7 | import org.expeditee.items.Text;
|
---|
8 | import org.expeditee.items.MagneticConstraint.MagneticConstraintActionWithArguments;
|
---|
9 | import org.expeditee.items.MagneticConstraint.MagneticConstraints;
|
---|
10 | import org.expeditee.items.MagneticConstraint.Utilities.BoxLogic;
|
---|
11 | import org.expeditee.items.MagneticConstraint.Utilities.Line;
|
---|
12 |
|
---|
13 | public class RepelTextAction extends
|
---|
14 | MagneticConstraintActionWithArguments<Float> {
|
---|
15 | public static boolean Enabled = true;
|
---|
16 |
|
---|
17 | @Override
|
---|
18 | public boolean exec(final Item item) {
|
---|
19 | MagneticConstraints.Log(this.getClass(), new Item[]{item},
|
---|
20 | Line.getLineContainingToken(item).toArray(new Item[]{}),
|
---|
21 | "YOU SHOULD NOT SEE THIS");
|
---|
22 | return exec(item, 0f);
|
---|
23 | }
|
---|
24 |
|
---|
25 | @Override
|
---|
26 | public boolean exec(final Item item, final Float... args) {
|
---|
27 | if(!Enabled || item == null || item.getParent() == null || item.getText().startsWith("@") || !isSpIDERCodePage(item.getParent())) {
|
---|
28 | return false;
|
---|
29 | }
|
---|
30 | if(FrameUtils.getCurrentItem() == null) return false;
|
---|
31 | final Point selectedItemPosition = FrameUtils.getCurrentItem().getPosition();
|
---|
32 | if(!selectedItemPosition.equals(item.getPosition())) return false;
|
---|
33 |
|
---|
34 | if(item.getMagnetizedItemRight() == -1) {
|
---|
35 | if(BoxLogic.boxCollision(item)) {
|
---|
36 | RepelTextDownAction.MergeSameLineParagraphCollisions = true;
|
---|
37 | final RepelTextDownAction textDownAction = new RepelTextDownAction();
|
---|
38 | textDownAction.setCallbackAction(this.callbackAction);
|
---|
39 | textDownAction.exec(item);
|
---|
40 | RepelTextDownAction.MergeSameLineParagraphCollisions = false;
|
---|
41 | }
|
---|
42 | } else if(item.getParent().getItemWithID(item.getMagnetizedItemRight()) == null) {
|
---|
43 | item.setMagnetizedItemRight(null);
|
---|
44 | return false;
|
---|
45 | } else {
|
---|
46 | final int charDistance = EcosystemManager.getTextLayoutManager().getStringWidth(((Text)item).getFont(), "X");
|
---|
47 | final Line tokensToMove = Line.getLineFromToken(item);
|
---|
48 | tokensToMove.removeFirst();
|
---|
49 | boolean afterGap = false;
|
---|
50 | for(int i = 1; i < tokensToMove.size();) {
|
---|
51 | if(afterGap) tokensToMove.remove(i);
|
---|
52 | else if((tokensToMove.get(i).getX() - charDistance) > (tokensToMove.get(i - 1).getX() + tokensToMove.get(i - 1).getBoundsWidth())) {
|
---|
53 | afterGap = true;
|
---|
54 | tokensToMove.remove(i);
|
---|
55 | } else i++;
|
---|
56 | }
|
---|
57 | tokensToMove.deltaX(args[0].intValue());
|
---|
58 | for(final Item token : tokensToMove) {
|
---|
59 | if(BoxLogic.boxCollision(token)) {
|
---|
60 | RepelTextDownAction.MergeSameLineParagraphCollisions = true;
|
---|
61 | final RepelTextDownAction textDownAction = new RepelTextDownAction();
|
---|
62 | textDownAction.setCallbackAction(this.callbackAction);
|
---|
63 | textDownAction.exec(token);
|
---|
64 | RepelTextDownAction.MergeSameLineParagraphCollisions = false;
|
---|
65 | break;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 |
|
---|
72 |
|
---|
73 | callback(item);
|
---|
74 |
|
---|
75 | return true;
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public boolean invert(final Item item) {
|
---|
80 | System.err.println("Repel text action does not have a inverted implementation.");
|
---|
81 | return false;
|
---|
82 | }
|
---|
83 | }
|
---|