source: trunk/src/org/expeditee/items/MagneticConstraint/Actions/RepelTextAction.java@ 974

Last change on this file since 974 was 974, checked in by bln4, 9 years ago

Publicified one of the formatter strings so we can use it for comparing dates.
Attract and Repel Text can now be turned on and off via a static field

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