Changeset 963 for trunk/src/org/expeditee
- Timestamp:
- 09/15/15 12:32:33 (9 years ago)
- Location:
- trunk/src/org/expeditee/items/MagneticConstraint
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/items/MagneticConstraint/Actions/AttractTextAction.java
r934 r963 1 1 package org.expeditee.items.MagneticConstraint.Actions; 2 2 3 import java.awt.Point; 4 5 import org.expeditee.gui.FrameUtils; 3 6 import org.expeditee.items.Item; 4 7 import org.expeditee.items.MagneticConstraint.MagneticConstraintActionWithArguments; 5 import org.expeditee.items.MagneticConstraint.MagneticConstraints;6 8 import org.expeditee.items.MagneticConstraint.Utilities.Line; 7 9 … … 16 18 @Override 17 19 public boolean exec(Item item, Float... args) { 18 if(item .getParent() == null || item.getText().startsWith("@")) {20 if(item == null || item.getParent() == null || item.getText().startsWith("@") || !isSpIDERCodePage(item.getParent())) { 19 21 return false; 20 22 } 21 if(item.getMagnetizedItemRight() == -1) { 22 MagneticConstraints.Log(this.getClass(), new Item[]{item}, 23 Line.getLineContainingToken(item).toArray(new Item[]{}), 24 "No right neighbor to attract."); 23 if(FrameUtils.getCurrentItem() == null) return false; 24 final Point selectedItemPosition = FrameUtils.getCurrentItem().getPosition(); 25 if(!selectedItemPosition.equals(item.getPosition())) return false; 26 27 if(item.getMagnetizedItemRight() == -1) return false; 28 else if(item.getParent().getItemWithID(item.getMagnetizedItemRight()) == null) { 29 item.setMagnetizedItemRight(null); 25 30 return false; 26 } else if (item.getParent().getItemWithID(item.getMagnetizedItemRight()) == null) { 27 item.setMagnetizedItemRight(null); 28 MagneticConstraints.Log(this.getClass(), new Item[]{item}, 29 Line.getLineContainingToken(item).toArray(new Item[]{}), 30 "Right neighbor has id: " + item.getMagnetizedItemRight() + 31 " according to datastructure but no item on frame has that id."); 32 return false; 33 } 34 else { 31 } else { 35 32 final Item toMyRight = item.getParent().getItemWithID(item.getMagnetizedItemRight()); 36 float newX = toMyRight.getX() - args[0]; 37 if(newX < toMyRight.getX()) { 38 System.err.println(); 39 } 40 toMyRight.setX(newX); 41 MagneticConstraints.Log(this.getClass(), new Item[]{item, toMyRight}, 42 Line.getLineContainingToken(item).toArray(new Item[]{}), 43 "Attracting second primary to first. Delta: " + args[0] + ". NewX: " + newX + ". Recursing."); 44 new AttractTextAction().exec(toMyRight, args); 33 final Line toAttract = Line.getLineFromToken(toMyRight); 34 toAttract.deltaX(-args[0].intValue()); 45 35 return true; 46 36 } -
trunk/src/org/expeditee/items/MagneticConstraint/Actions/BackspaceAction.java
r934 r963 1 1 package org.expeditee.items.MagneticConstraint.Actions; 2 2 3 import java.awt.Point;4 import java.awt.Rectangle;5 6 3 import org.expeditee.gui.DisplayIO; 7 import org.expeditee.gui.FrameIO;8 4 import org.expeditee.items.Item; 9 5 import org.expeditee.items.Text; 10 6 import org.expeditee.items.MagneticConstraint.MagneticConstraintAction; 11 import org.expeditee.items.MagneticConstraint.MagneticConstraints;12 import org.expeditee.items.MagneticConstraint.Utilities.BoxLogic;13 7 import org.expeditee.items.MagneticConstraint.Utilities.Line; 14 8 import org.expeditee.items.MagneticConstraint.Utilities.Paragraph; … … 17 11 public class BackspaceAction extends MagneticConstraintAction { 18 12 19 // if(newX < this._x) {20 // final java.util.Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();21 // for(final Thread thr : allStackTraces.keySet()) {22 // System.err.println(thr.getName());23 // final StackTraceElement[] stackTraceElements = allStackTraces.get(thr);24 // for(final StackTraceElement ln : stackTraceElements) System.err.println(ln);25 // System.err.println();26 // }27 // }28 13 @Override 29 14 public boolean exec(final Item item) { 30 if(item == null) return false; 31 if(item.getMagnetizedItemTop() != -1 && TextLogic.XIsBeforeCharacters((Text) item, DisplayIO.getMouseX())) { 32 MagneticConstraints.Log(this.getClass(), new Item[]{item}, 33 Line.getLineContainingToken(item).toArray(new Item[]{}), 34 "Top Neighbor detected (id:" + item.getMagnetizedItemTop() + "). Merging this line with line above."); 35 return mergeLines(item); 36 } else if(TextLogic.XIsBeforeCharacters((Text) item, DisplayIO.getMouseX())) { 37 if(item.getMagnetizedItemLeft() != -1) { 38 callback(null); 39 final Item toLeft = item.getParent().getItemWithID(item.getMagnetizedItemLeft()); 40 final int initialDistance = item.getX() + item.getBoundsWidth() - toLeft.getX(); 41 toLeft.setText(toLeft.getText() + item.getText()); 42 item.delete(); 43 final int alteredDistance = toLeft.getBoundsWidth(); 44 final float delta = (initialDistance - alteredDistance) * 0.8f; 15 if(item == null || ((Text) item).isEmpty()) { 16 final Text temp = DisplayIO.getCurrentFrame().createNewText("."); 17 callback(temp); 18 if(temp.getMagnetizedItemLeft() != -1) 19 DisplayIO.setTextCursor((Text) temp.getParent().getItemWithID(temp.getMagnetizedItemLeft()), Text.END); 20 else if(temp.getMagnetizedItemTop() != -1) 21 DisplayIO.setTextCursor((Text) temp.getParent().getItemWithID(temp.getMagnetizedItemTop()), Text.END); 22 temp.delete(); 23 callback(temp); 24 return true; 25 } else if(item.getMagnetizedItemLeft() != -1 && TextLogic.XIsBeforeCharacters((Text) item, DisplayIO.getMouseX())) { 26 new LastItemAction().exec(item); 27 return true; 28 } else if(item.getMagnetizedItemTop() != -1 && TextLogic.XIsBeforeCharacters((Text) item, DisplayIO.getMouseX())) { 29 callback(item); 30 final Line currentLine = Line.getLineFromToken(item); 31 final Line lastLine = currentLine.last(); 32 if(lastLine != null && Paragraph.getParagraphFromLine(lastLine).contains(currentLine)) { 33 lastLine.appendLine(currentLine); 34 DisplayIO.setTextCursor((Text) item, Text.HOME); 45 35 callback(item); 46 new AttractTextAction().exec(toLeft, delta);47 FrameIO.ForceSaveFrame(toLeft.getParent());48 FrameIO.Reload();49 callback(null);50 MagneticConstraints.Log(this.getClass(), new Item[]{item, toLeft},51 Line.getLineContainingToken(item).toArray(new Item[]{}),52 "Left Neighbor detected (id:" + item.getMagnetizedItemLeft() +53 "). Merging second primary into first. Calling AttractTextAction.");54 36 return true; 55 } else return false; 56 } else return false; 57 } 58 59 private boolean mergeLines(final Item item) { 60 callback(null); 61 //1. Get current and last line 62 final Line currentLine = Line.getLineFromToken(item); 63 final Line lastLine = currentLine.last(); 64 //2. Is there a last line? If not we don't do anything 65 if(lastLine == null) return false; 66 //3. Is the last line in the same paragraph? 67 final Paragraph currentLineParagraph = Paragraph.getParagraphFromLine(currentLine); 68 final Paragraph lastLineParagraph = Paragraph.getParagraphFromLine(lastLine); 69 if(!lastLineParagraph.containsAll(currentLineParagraph)) { 70 //3a. No. We don't do anything. 71 return false; 37 } 72 38 } 73 //NB: at this point we know we have a last line that is in the same paragraph. So we have to move tokens about. 74 //4. Move the tokens. 75 for(final Item token: currentLine) if(!moveToLineAbove(token)) break; 76 //5. Move the cursor 77 DisplayIO.MoveCursorToEndOfItem(lastLine.getLast()); 78 79 //6. Move lines below up 80 final Line nextLine = currentLine.next(); 81 if(nextLine != null && currentLineParagraph.contains(nextLine)) 82 Paragraph.getParagraphFromLine(nextLine).deltaY(-Line.getLineHeight(currentLine)); 83 //7. Refresh the screen and use callback to update the magnets. 84 FrameIO.ForceSaveFrame(item.getParent()); 85 FrameIO.Reload(); 86 callback(item); 87 callback(null); 88 return true; 89 } 90 91 private boolean moveToLineAbove(final Item token) { 92 final Line currentLine = Line.getLineContainingToken(token); 93 final Line lineAbove = currentLine.last(); 94 final Rectangle lastLineBoundingBox = lineAbove.getBoundingBox(); 95 final Point locationToMoveTo = new Point((int) (lastLineBoundingBox.getX() + lastLineBoundingBox.getWidth() + 10), (int) lastLineBoundingBox.getMinY()); 96 token.setPosition(locationToMoveTo); 97 currentLine.removeFirst(); 98 currentLine.deltaX(-token.getBoundsWidth()); 99 callback(token); 100 if(BoxLogic.boxCollision(token)) { 101 BoxLogic.resolveCollision(token, this.callbackAction); 102 callback(token); 103 return false; 104 } 105 return true; 39 return false; 106 40 } 107 41 -
trunk/src/org/expeditee/items/MagneticConstraint/Actions/DeleteAction.java
r934 r963 1 1 package org.expeditee.items.MagneticConstraint.Actions; 2 3 import java.awt.event.KeyEvent;4 2 5 3 import org.expeditee.gui.DisplayIO; … … 7 5 import org.expeditee.items.Text; 8 6 import org.expeditee.items.MagneticConstraint.MagneticConstraintAction; 9 import org.expeditee.items.MagneticConstraint.MagneticConstraints; 7 import org.expeditee.items.MagneticConstraint.Utilities.Line; 8 import org.expeditee.items.MagneticConstraint.Utilities.Paragraph; 10 9 import org.expeditee.items.MagneticConstraint.Utilities.TextLogic; 11 10 … … 14 13 @Override 15 14 public boolean exec(final Item item) { 16 if(TextLogic.XIsAfterCharacters((Text) item, DisplayIO.getMouseX())) { 17 if(item.getMagnetizedItemRight() != -1) { 18 final Text toMyRight = (Text) item.getParent().getItemWithID(item.getMagnetizedItemRight()); 19 final int deltaX = TextLogic.DistanceThroughTextWithXCharacters(toMyRight, 1); 20 DisplayIO.setCursorPosition((float) toMyRight.getPixelBoundsUnion().getX() + deltaX, DisplayIO.getFloatMouseY()); 21 MagneticConstraints.getInstance().keyHit(KeyEvent.VK_BACK_SPACE, toMyRight); 22 DisplayIO.MoveCursorToEndOfItem(item); 15 if(item == null || ((Text) item).isEmpty()) { 16 final Text temp = DisplayIO.getCurrentFrame().createNewText("."); 17 callback(temp); 18 if(temp.getMagnetizedItemRight() != -1) 19 DisplayIO.setTextCursor((Text) temp.getParent().getItemWithID(temp.getMagnetizedItemRight()), Text.HOME); 20 else if(temp.getMagnetizedItemBottom() != -1) 21 DisplayIO.setTextCursor((Text) temp.getParent().getItemWithID(temp.getMagnetizedItemBottom()), Text.HOME); 22 temp.delete(); 23 callback(temp); 24 return true; 25 } else if(item.getMagnetizedItemRight() != -1 && TextLogic.XIsAfterCharacters((Text) item, DisplayIO.getMouseX())) { 26 new NextItemAction().exec(item); 27 return true; 28 } else if(item.getMagnetizedItemBottom() != -1 && TextLogic.XIsAfterCharacters((Text) item, DisplayIO.getMouseX())) { 29 callback(item); 30 final Line currentLine = Line.getLineFromToken(item); 31 final Line nextLine = currentLine.next(); 32 if(nextLine != null && Paragraph.getParagraphFromLine(currentLine).contains(nextLine)) { 33 currentLine.appendLine(nextLine); 34 DisplayIO.setTextCursor((Text) item, Text.END); 35 callback(item); 23 36 return true; 24 } else if (item.getMagnetizedItemBottom() != -1) { 25 final Text belowMe = (Text) item.getParent().getItemWithID(item.getMagnetizedItemBottom()); 26 DisplayIO.setCursorPosition(belowMe.getPosition()); 27 MagneticConstraints.getInstance().keyHit(KeyEvent.VK_BACK_SPACE, belowMe); 28 return true; 29 } else return false; 30 } else return false; 37 } 38 } 39 return false; 31 40 } 32 41 -
trunk/src/org/expeditee/items/MagneticConstraint/Actions/RepelTextAction.java
r962 r963 31 31 32 32 if(item.getMagnetizedItemRight() == -1) { 33 33 if(BoxLogic.boxCollision(item)) { 34 RepelTextDownAction.MergeSameLineParagraphCollisions = true; 35 final RepelTextDownAction textDownAction = new RepelTextDownAction(); 36 textDownAction.setCallbackAction(this.callbackAction); 37 textDownAction.exec(item); 38 RepelTextDownAction.MergeSameLineParagraphCollisions = false; 39 } 34 40 } else if(item.getParent().getItemWithID(item.getMagnetizedItemRight()) == null) { 35 41 item.setMagnetizedItemRight(null); … … 41 47 for(final Item token : tokensToMove) { 42 48 if(BoxLogic.boxCollision(token)) { 49 RepelTextDownAction.MergeSameLineParagraphCollisions = true; 43 50 final RepelTextDownAction textDownAction = new RepelTextDownAction(); 44 51 textDownAction.setCallbackAction(this.callbackAction); 45 52 textDownAction.exec(token); 53 RepelTextDownAction.MergeSameLineParagraphCollisions = false; 46 54 break; 47 55 } -
trunk/src/org/expeditee/items/MagneticConstraint/Actions/RepelTextDownAction.java
r962 r963 15 15 public class RepelTextDownAction extends MagneticConstraintAction { 16 16 17 // private void f(final Item item) { 18 // final List<Item> items = item.getParent().getItems(); 19 // for (final Item item2 : items) { 20 // if(item2.getText().compareTo("main") == 0) System.err.println("#Found main"); 21 // } 22 // } 17 public static boolean MergeSameLineParagraphCollisions = false; 23 18 24 19 @Override 25 20 public boolean exec(final Item item) { 26 if(item == null || item.getParent() == null) return false; 27 callback(item); 21 if(item == null || item.getParent() == null) return false; 22 callback(item); 28 23 //Calculate content that needs to be moved to next line. 29 24 //1. Get remainder of line … … 37 32 } 38 33 39 //Move content below this line down40 //1. Get current line & current paragraph.41 34 final Line currentLine = Line.getLineContainingToken(item); 42 35 final Paragraph currentParagraph = Paragraph.getParagraphFromLine(currentLine); 43 //2. Split current paragraph 44 final Point newLineAt = currentParagraph.split(currentLine); 36 final Line nextLine = currentLine.next(); 37 38 final Point newLineAt; 45 39 //Move content contained in 'remainderOfLine' 46 //1. Is remainderOfLine not Empty? 47 if(!remainderOfLine.isEmpty()) { 48 //2a. Yes. Move the content 40 41 42 if(!remainderOfLine.isEmpty() && currentParagraph.contains(nextLine) && MergeSameLineParagraphCollisions) { 43 nextLine.prependLine(remainderOfLine); 44 DisplayIO.setTextCursor((Text) remainderOfLine.getFirst(), Text.HOME); 45 callback(remainderOfLine.getFirst()); 46 FrameIO.SaveFrame(remainderOfLine.getLast().getParent()); 47 } else if(!remainderOfLine.isEmpty()) { 48 newLineAt = currentParagraph.split(currentLine); 49 49 final Point first = remainderOfLine.getBoundingBox().getLocation(); 50 50 for(final Item token : remainderOfLine) { … … 52 52 token.setPosition((float) newX, (float) newLineAt.getY()); 53 53 } 54 //2b. Move the cursor; refresh the screen and use callback to update the magnets55 54 DisplayIO.setTextCursor((Text) remainderOfLine.getFirst(), Text.HOME); 56 callback( null);55 callback(remainderOfLine.getFirst()); 57 56 FrameIO.SaveFrame(remainderOfLine.getLast().getParent()); 58 } else { 59 //3a No. Move the cursor; refresh the screen and use callback to update the magnets 57 }else { 58 //3a No. Move the cursor 59 newLineAt = currentParagraph.split(currentLine); 60 60 DisplayIO.setCursorPosition(newLineAt); 61 61 } 62 62 63 63 return true; 64 64 } -
trunk/src/org/expeditee/items/MagneticConstraint/Utilities/BoxLogic.java
r960 r963 1 1 package org.expeditee.items.MagneticConstraint.Utilities; 2 2 3 import java.awt.Point;4 3 import java.awt.Rectangle; 5 4 import java.util.Collection; … … 7 6 import java.util.List; 8 7 9 import org.expeditee.gui.DisplayIO;10 8 import org.expeditee.gui.Frame; 11 import org.expeditee.gui.FrameIO;12 import org.expeditee.gui.FrameUtils;13 9 import org.expeditee.items.Dot; 14 10 import org.expeditee.items.Item; 15 import org.expeditee.items.MagneticConstraint.MagneticConstraintAction;16 11 17 12 public class BoxLogic { … … 66 61 // } 67 62 68 public static void resolveCollision(final Item item, final MagneticConstraintAction remagnet) {69 boolean updateCursor = FrameUtils.getCurrentItem() == item;70 final Line currentLine = Line.getLineContainingToken(item);71 final Paragraph currentParagraph = Paragraph.getParagraphFromLine(currentLine);72 final Point newLineAt = currentParagraph.split(currentLine);73 final Line lineFromToken = Line.getLineFromToken(item);74 lineFromToken.deltaX((int) newLineAt.getX() - lineFromToken.getFirst().getX());75 lineFromToken.deltaY((int) newLineAt.getY() - lineFromToken.getFirst().getY());76 remagnet.callback(null);77 final Line nextLine = lineFromToken.next();78 if(nextLine != null && nextLine.getBoundingBox().getX() >= lineFromToken.getBoundingBox().getX()) {79 final Paragraph paragraphFromNewLine = Paragraph.getParagraphFromLine(lineFromToken);80 final Paragraph paragraphFromNextLine = Paragraph.getParagraphFromLine(nextLine);81 if(paragraphFromNewLine.containsAll(paragraphFromNextLine)) {82 final Rectangle newLineBoundingBox = lineFromToken.getBoundingBox();83 nextLine.setX((int) newLineBoundingBox.getMaxX());84 nextLine.setY((int) newLineBoundingBox.getY());85 }86 }87 if(updateCursor) DisplayIO.MoveCursorToEndOfItem(item);88 FrameIO.ForceSaveFrame(item.getParent());89 }63 // public static void resolveCollision(final Item item, final MagneticConstraintAction remagnet) { 64 // boolean updateCursor = FrameUtils.getCurrentItem() == item; 65 // final Line currentLine = Line.getLineContainingToken(item); 66 // final Paragraph currentParagraph = Paragraph.getParagraphFromLine(currentLine); 67 // final Point newLineAt = currentParagraph.split(currentLine); 68 // final Line lineFromToken = Line.getLineFromToken(item); 69 // lineFromToken.deltaX((int) newLineAt.getX() - lineFromToken.getFirst().getX()); 70 // lineFromToken.deltaY((int) newLineAt.getY() - lineFromToken.getFirst().getY()); 71 // remagnet.callback(null); 72 // final Line nextLine = lineFromToken.next(); 73 // if(nextLine != null && nextLine.getBoundingBox().getX() >= lineFromToken.getBoundingBox().getX()) { 74 // final Paragraph paragraphFromNewLine = Paragraph.getParagraphFromLine(lineFromToken); 75 // final Paragraph paragraphFromNextLine = Paragraph.getParagraphFromLine(nextLine); 76 // if(paragraphFromNewLine.containsAll(paragraphFromNextLine)) { 77 // final Rectangle newLineBoundingBox = lineFromToken.getBoundingBox(); 78 // nextLine.setX((int) newLineBoundingBox.getMaxX()); 79 // nextLine.setY((int) newLineBoundingBox.getY()); 80 // } 81 // } 82 // if(updateCursor) DisplayIO.MoveCursorToEndOfItem(item); 83 // FrameIO.ForceSaveFrame(item.getParent()); 84 // } 90 85 } -
trunk/src/org/expeditee/items/MagneticConstraint/Utilities/Line.java
r962 r963 2 2 3 3 import java.awt.Rectangle; 4 import java.util. *;4 import java.util.LinkedList; 5 5 6 6 import org.expeditee.items.Item; … … 8 8 @SuppressWarnings("serial") 9 9 public class Line extends LinkedList<Item> { 10 10 11 11 private Line() { 12 12 super(); … … 17 17 } 18 18 19 public Line prependLine(final Line line) { 20 final int x = this.getBoundingBox().x; 21 final int width = line.getBoundingBox().width; 22 this.deltaX(width); 23 line.setX(x); 24 line.setY(this.getFirst().getY()); 25 return Line.getLineFromToken(line.getFirst()); 26 } 27 28 public Line appendLine(final Line line) { 29 final int x = this.getBoundingBox().x; 30 final int width = this.getBoundingBox().width; 31 line.setX(x + width); 32 line.setY(this.getFirst().getY()); 33 return Line.getLineFromToken(this.getFirst()); 34 } 35 19 36 public static Line getLineFromToken(final Item token) { 20 37 if(token == null) return new Line(); 21 else return getLineFromToken(token, new Line()); 38 else { 39 return getLineFromToken(token, new Line()); 40 } 22 41 } 23 42 24 43 public static Line getLineContainingToken(final Item token) { 25 44 if(token == null) return new Line(); 45 final Line ret = new Line(); 26 46 Item current = token; 27 while(current.getMagnetizedItemLeft() != -1) 47 while(current.getMagnetizedItemLeft() != -1) { 28 48 current = current.getParent().getItemWithID(current.getMagnetizedItemLeft()); 29 return getLineFromToken(current, new Line()); 49 ret.addFirst(current); 50 } 51 final Line following = getLineFromToken(token, new Line()); 52 ret.addAll(following); 53 return ret; 54 // return getLineFromToken(current, new Line()); 30 55 } 31 56 -
trunk/src/org/expeditee/items/MagneticConstraint/Utilities/TextLogic.java
r962 r963 2 2 3 3 import org.expeditee.gui.Browser; 4 import org.expeditee.gui.DisplayIO; 4 5 import org.expeditee.items.Text; 5 6 … … 39 40 } 40 41 41 public static int DistanceThroughTextWithXCharacters(final Text text, final int x) {42 return Browser._theBrowser.getFontMetrics(Browser._theBrowser.getFont()).stringWidth(text.getText().substring(0, Math.min(text.getText().length() - 1, x - 1)));42 public static int GetInsertionIndexSelected(final Text text) { 43 return text.getCharPosition(0, DisplayIO.getMouseX()).getInsertionIndex(); 43 44 } 44 45 }
Note:
See TracChangeset
for help on using the changeset viewer.