Changeset 740


Ignore:
Timestamp:
01/22/14 16:45:34 (10 years ago)
Author:
jts21
Message:

Modify the undo stack to use a stack of List rather than a stack of connected items (easier to understand, and since we have to parse it linearly anyways it's not any slower)
Also started making other stuff undo-able, so far vertical auto-format can be undone, probably pretty inefficient (every time I add to the list of items modified by the vertical format I have to walk the entire list of modified items to make sure I don't add it multiple times).
I think it might be a good idea to add a redo functionality as well (simple enough to do, but we need a key/mouse binding for it)
Also modified copy/pasting to fix a bug where cutting a point on a shape would leave the rest of the shape on the screen (and broken). Now it just takes the whole shape (again, this is done inefficiently since for every item we walk the entire list of known items to see if we've seen it before. IDK if that will need to be rewritten to speed it up somehow).

Location:
trunk/src/org/expeditee
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/agents/Format.java

    r427 r740  
    5151                // same box as the cursor
    5252                Collection<Text> itemsToFormat = getItemsToFormat(start);
     53               
     54                List<Item> changedItems = new ArrayList<Item>();
    5355
    5456                ArrayList<Item> columnHeads = new ArrayList<Item>();
     
    129131
    130132                        if (xCheck < FrameGraphics.getMaxSize().width) {
    131                                 if (columnHeads.get(i + 1).getX() < maxX
    132                                                 && columnHeads.get(i + 1).getY() < maxY)
    133                                         columnHeads.get(i + 1).setX(maxX);
    134 
    135                                 for (Item it : columns.get(i + 1))
    136                                         if (it.getX() < maxX && it.getY() < maxY)
     133                                Item columnHead = columnHeads.get(i + 1);
     134                                if (columnHead.getX() < maxX && columnHead.getY() < maxY) {
     135                                        if(columnHead.getX() != maxX && !changedItems.contains(columnHead)) {
     136                                                Item copy = columnHead.copy();
     137                                                copy.setID(columnHead.getID());
     138                                                changedItems.add(copy);
     139                                        }
     140                                        columnHead.setX(maxX);
     141                                }
     142
     143                                for (Item it : columns.get(i + 1)) {
     144                                        if (it.getX() < maxX && it.getY() < maxY) {
     145                                        if(it.getX() != maxX && !changedItems.contains(it)) {
     146                                                Item copy = it.copy();
     147                                                copy.setID(it.getID());
     148                                                changedItems.add(copy);
     149                                        }
    137150                                                it.setX(maxX);
    138                         }
    139 
    140                         res = FrameUtils.Align(columns.get(i + 1), true, _adjust);
     151                                        }
     152                                }
     153                        }
     154                       
     155                        res = FrameUtils.Align(columns.get(i + 1), true, _adjust, changedItems);
    141156                        _success = _success && (res >= 0);
    142157                }
     
    148163                 */
    149164                start.setChanged(true);
     165                start.addToUndo(changedItems);
    150166                FrameGraphics.requestRefresh(true);
    151167                return null;
  • trunk/src/org/expeditee/gui/Frame.java

    r737 r740  
    9595
    9696        // list of deleted items that can be restored
    97         private Stack<Item> _undo = new Stack<Item>();
     97        private Stack<List<Item>> _undo = new Stack<List<Item>>();
    9898
    9999        // basically just a list of smaller objects?
     
    898898         *            The List of Items to add to the undo stack.
    899899         */
    900         public void addAllToUndo(Collection<Item> items) {
     900        public void addToUndo(Collection<Item> items) {
    901901                if (items.size() < 1)
    902902                        return;
    903 
    904                 String id = "" + _undo.size();
    905 
    906                 for (Item i : items) {
    907                         i.setTag(id);
    908                         _undo.push(i);
    909                 }
    910         }
    911 
    912         public void addToUndo(Item item) {
    913                 if (item == null)
    914                         return;
    915 
    916                 item.setTag("" + _undo.size());
    917                 _undo.push(item);
     903               
     904//              System.out.println("Added: " + items);
     905
     906                _undo.push(new LinkedList<Item>(items));
    918907        }
    919908
    920909        public void undo() {
    921                 Item undo = null;
     910                List<Item> undo = null;
    922911                boolean bReparse = false;
    923912                boolean bRecalculate = false;
     
    927916
    928917                undo = _undo.pop();
    929 
    930                 // if the change was to characteristics
    931                 if (undo.isVisible() && _body.contains(undo)) {
    932                         Item old = _body.get(_body.indexOf(undo));
    933                         _body.set(_body.indexOf(old), undo);
     918               
     919//              System.out.println("Undoing: " + undo);
     920
     921                for(Item i : undo) {
     922                        // if the change was to characteristics
     923                        if(i.isVisible() && _body.contains(i)) {
     924                                _body.set(_body.indexOf(i), i);
    934925                        // the item was deleted
    935                 } else {
    936                         List<Item> toRestore = new LinkedList<Item>();
    937                         toRestore.add(undo);
    938                         if (undo.hasOverlay())
    939                                 bReparse = true;
    940 
    941                         // remove any connected items at the top of the stack
    942                         while (_undo.size() > 0) {
    943                                 Item next = _undo.peek();
    944                                 // if this item was connected to one already picked up, remove
    945                                 // it from the stack
    946                                 if (toRestore.contains(next))
    947                                         _undo.pop();
    948                                 // else, if this item should be restored (deleted in an enclosed
    949                                 // set)
    950                                 else if (next.getTag().equals(undo.getTag())) {
    951                                         _undo.pop();
    952                                         toRestore.add(next);
    953                                         if (next.hasOverlay())
    954                                                 bReparse = true;
    955 
    956                                         // otherwise, we are done
    957                                 } else
    958                                         break;
    959                         }
    960 
    961                         // if these items were deleted from a frame, add them
    962                         addAllItems(toRestore);
    963 
    964                         for (Item i : toRestore) {
     926                        } else {
     927                                _body.add(i);
    965928                                bReparse |= i.hasOverlay();
    966929                                bRecalculate |= i.recalculateWhenChanged();
     
    974937                        }
    975938                }
    976 
    977939                change();
    978940                FrameMouseActions.getInstance().refreshHighlights();
     
    15151477                }
    15161478                _body.removeAll(newBody);
    1517                 addAllToUndo(_body);
     1479                addToUndo(_body);
    15181480                _body = newBody;
    15191481                change();
  • trunk/src/org/expeditee/gui/FrameKeyboardActions.java

    r737 r740  
    774774                case KeyEvent.VK_C:
    775775                        if(FreeItems.itemsAttachedToCursor()) {
    776                                 ItemSelection.copyClone(FreeItems.getInstance());
     776                                ItemSelection.copyClone();
    777777                                return;
    778778                        }
     
    801801                        return;
    802802                case KeyEvent.VK_X:
    803                         ItemSelection.cut(FreeItems.getInstance());
     803                        ItemSelection.cut();
    804804                        return;
    805805                case KeyEvent.VK_M:
  • trunk/src/org/expeditee/gui/FrameMouseActions.java

    r738 r740  
    16761676                // System.out.println("Left window");
    16771677                if(FreeItems.itemsAttachedToCursor())
    1678                         ItemSelection.cut(FreeItems.getInstance());
     1678                        ItemSelection.cut();
    16791679        }
    16801680       
     
    26692669                        SessionStats.DeletedItems(toUndo);
    26702670                        if (parent != null) {
    2671                                 parent.addAllToUndo(toUndo);
     2671                                parent.addToUndo(toUndo);
    26722672                                parent.removeAllItems(toUndo); // toDelete.getConnected()
    26732673                        }
     
    27552755                // the current frame as well as the overlay frame
    27562756                Frame currentFrame = DisplayIO.getCurrentFrame();
    2757                 currentFrame.addAllToUndo(itemList);
     2757                currentFrame.addToUndo(itemList);
    27582758                itemList.clear();
    27592759                if (bReparse) {
  • trunk/src/org/expeditee/gui/FrameUtils.java

    r737 r740  
    163163         * @return
    164164         */
    165         public static int Align(List<Text> toAlign, boolean moveAll, int adjust) {
     165        public static int Align(List<Text> toAlign, boolean moveAll, int adjust, List<Item> changedItems) {
    166166                Collections.sort(toAlign);
    167167
     
    199199
    200200                                if (sameBulletType(lastBullet, currentBullet)) {
     201                                        String oldText = currentText.getText();
     202                                       
    201203                                        currentText.stripFirstWord();
    202204
     
    204206                                        lastBullet = FrameKeyboardActions.getAutoBullet(currentText
    205207                                                        .getText());
     208                                       
     209                                        // if we changed the item, add to changedItems list
     210                                        if(changedItems != null && oldText != currentText.getText() && !changedItems.contains(currentText)) {
     211                                                Item copy = currentText.copy();
     212                                                copy.setID(currentText.getID());
     213                                                copy.setText(oldText);
     214                                                changedItems.add(copy);
     215                                        }
    206216                                }
    207217                        }
     
    255265                                        && above != curr.getTitleItem()) {
    256266                                x = above.getX();
    257                                 from.setY((int) above.getPolygon().getBounds().getMaxY()
     267                                int y = (int) above.getPolygon().getBounds().getMaxY()
    258268                                                + space
    259269                                                + ((int) (from.getY() - from.getPolygon().getBounds()
    260                                                                 .getMinY())));
    261                                 from.setX(x);
     270                                                                .getMinY()));
     271                               
     272                                if(changedItems != null && (from.getX() != x || from.getY() != y) && !changedItems.contains(from)) {
     273                                        Item copy = from.copy();
     274                                        copy.setID(from.getID());
     275                                        changedItems.add(copy);
     276                                }
     277                                from.setPosition(x, y);
    262278                        } else {
    263279                                x = from.getX();
     
    280296                        int newPos = bottom + space + diff;
    281297
     298                        if(changedItems != null && ((moveAll && current.getX() != x) || current.getY() != newPos) && !changedItems.contains(current)) {
     299                                Item copy = current.copy();
     300                                copy.setID(current.getID());
     301                                changedItems.add(copy);
     302                        }
     303                       
    282304                        if (moveAll) {
    283305                                current.setPosition(x, newPos);
     
    294316                int y = from.getY() + from.getBoundsHeight() + space;
    295317                return y;
     318        }
     319       
     320        public static int Align(List<Text> toAlign, boolean moveAll, int adjust) {
     321                return Align(toAlign, moveAll, adjust, null);
    296322        }
    297323
  • trunk/src/org/expeditee/io/ItemSelection.java

    r738 r740  
    1212import java.io.IOException;
    1313import java.io.Serializable;
     14import java.util.ArrayList;
    1415import java.util.Arrays;
    1516import java.util.List;
     
    102103        }
    103104       
    104        
    105         public static void cut(List<Item> items) {
    106                
    107                 copy(items);
     105        private static List<Item> getAllToCopy() {
     106                List<Item> tmp = new ArrayList<Item>(FreeItems.getInstance());
     107                List<Item> toCopy = new ArrayList<Item>(tmp);
     108                for(Item i : tmp) {
     109                        for(Item c : i.getAllConnected()) {
     110                                if(! toCopy.contains(c)) {
     111                                        toCopy.add(c);
     112                                        FrameMouseActions.pickup(c);
     113                                }
     114                        }
     115                }
     116                return toCopy;
     117        }
     118       
     119        public static void cut() {
     120               
     121                List<Item> toCopy = getAllToCopy();
     122               
     123                copy(toCopy);
    108124               
    109125                // remove the items attached to the cursor
    110                 items.clear();
     126                DisplayIO.getCurrentFrame().removeAllItems(toCopy);
     127                FreeItems.getInstance().clear();
    111128               
    112129                FrameGraphics.refresh(false);
    113130        }
    114131       
    115         public static void copyClone(List<Item> items) {
    116                
    117                 copy(ItemUtils.CopyItems(items));
     132        public static void copyClone() {
     133               
     134                copy(ItemUtils.CopyItems(getAllToCopy()));
    118135               
    119136        }
Note: See TracChangeset for help on using the changeset viewer.