From b7c8296b21ab84296487f956b035aa59966f3793 Mon Sep 17 00:00:00 2001 From: Julian Date: Sat, 22 Oct 2016 22:24:10 +0200 Subject: [PATCH] fix reversed z-order --- src/dfEditor/GraphicPanel.java | 4 +- src/dfEditor/animation/AnimationCell.java | 125 +++++++++--------- .../animation/AnimationController.java | 15 ++- src/dfEditor/animation/AnimationPanel.java | 20 +-- src/dfEditor/io/AnimationSetWriter.java | 11 +- 5 files changed, 86 insertions(+), 89 deletions(-) diff --git a/src/dfEditor/GraphicPanel.java b/src/dfEditor/GraphicPanel.java index af63a8b..0882689 100644 --- a/src/dfEditor/GraphicPanel.java +++ b/src/dfEditor/GraphicPanel.java @@ -319,10 +319,8 @@ protected void drawStack(Graphics g) protected void drawStack(Graphics g, Point aOrigin, float aZoom, float aAlpha) { - for (int i=_drawStack.size(); --i>=0;) + for (GraphicObject graphic : _drawStack) { - GraphicObject graphic = _drawStack.get(i); - drawGraphicRotated(graphic, g, aOrigin, aZoom, aAlpha); } } diff --git a/src/dfEditor/animation/AnimationCell.java b/src/dfEditor/animation/AnimationCell.java index 5887f96..58f1c80 100644 --- a/src/dfEditor/animation/AnimationCell.java +++ b/src/dfEditor/animation/AnimationCell.java @@ -20,10 +20,9 @@ package dfEditor.animation; import dfEditor.*; -import java.util.ArrayList; + import java.awt.Graphics; import java.awt.Graphics2D; -import java.awt.Color; import java.awt.image.VolatileImage; import java.awt.GraphicsEnvironment; import java.awt.GraphicsConfiguration; @@ -39,10 +38,9 @@ * @author s4m20 */ public class AnimationCell -{ - private Dictionary graphicZOrderDict; +{ private Dictionary graphicNodeDict; - private ArrayList graphicOrderList; + private ArrayList graphicOrderList; private VolatileImage vImage = null; private int delay; @@ -51,13 +49,12 @@ public AnimationCell() { setDelay(1); - graphicZOrderDict = new Hashtable(); graphicNodeDict = new Hashtable(); - graphicOrderList = new ArrayList(); + graphicOrderList = new ArrayList(); rebuild(); } - public Point getImageSize() + public Point getImageSize() { if (vImage == null) { @@ -80,7 +77,7 @@ public void rebuild() //System.out.println("Attempting to rebuild animation cell volatile image"); Rectangle r = getSpreadRect(); - + if (r.width <= 0 || r.height <= 0) { vImage = null; @@ -102,22 +99,21 @@ public void rebuild() g.setComposite(AlphaComposite.SrcOver); //for (Enumeration e = graphicNodeDict.keys(); e.hasMoreElements();) - for (int i=graphicOrderList.size()-1; i>=0; i--) + for (GraphicZOrderPair pair : graphicOrderList) { - //GraphicObject graphic = e.nextElement(); - GraphicObject graphic = graphicOrderList.get(i); + GraphicObject graphic = pair.graphic; // backup selected state and remove... don't want to draw it selected in the cell boolean isSelected = graphic.isSelected(); graphic.setSelected(false); - Graphics2D g2d = (Graphics2D)g; + Graphics2D g2d = (Graphics2D)g; AffineTransform transform = new AffineTransform(g2d.getTransform()); AffineTransform oldTransform = g2d.getTransform(); Rectangle gr = graphic.getRect(); - transform.rotate(Math.toRadians(graphic.getAngle()), -r.x+gr.x+gr.width/2, -r.y+gr.y+gr.height/2); + transform.rotate(Math.toRadians(graphic.getAngle()), -r.x+gr.x+gr.width/2, -r.y+gr.y+gr.height/2); g2d.setTransform(transform); graphic.draw(g2d, new Point(-r.x,-r.y), 1.0f, 1.0f, false); @@ -237,60 +233,42 @@ public void addSprite(CustomNode aNode, GraphicObject aGraphic) { if (aNode != null && aGraphic != null) { - graphicZOrderDict.put(aGraphic, 0); graphicNodeDict.put(aGraphic, aNode); - graphicOrderList.add(aGraphic); + graphicOrderList.add(new GraphicZOrderPair(aGraphic, 0)); } } public void setZOrder(final GraphicObject aGraphic, final int zOrder) { - graphicZOrderDict.remove(aGraphic); - graphicZOrderDict.put(aGraphic, new Integer(zOrder)); - - boolean bRebuild = false; - - // bubble - int n = graphicOrderList.size(); - for (int i = 0; i < n; i++) - { - for (int j = n-1; j > i; j--) - { - GraphicObject A = graphicOrderList.get(j-1); - GraphicObject B = graphicOrderList.get(j); - - int a = graphicZOrderDict.get(A).intValue(); - int b = graphicZOrderDict.get(B).intValue(); - - if (a > b) - { - this.swapGraphics(A, B); - bRebuild = true; - } - } + boolean orderDirty = false; + int lastZOrder = Integer.MIN_VALUE; + + for (GraphicZOrderPair pair : graphicOrderList) + { + if (pair.graphic == aGraphic) pair.zOrder = zOrder; + + if (pair.zOrder < lastZOrder) orderDirty = true; + lastZOrder = pair.zOrder; } - - if (bRebuild) + + if (orderDirty) { - this.rebuild(); + Collections.sort(graphicOrderList); + rebuild(); } + } public int zOrderOfGraphic(final GraphicObject aGraphic) { - return graphicZOrderDict.get(aGraphic).intValue(); + for (GraphicZOrderPair pair : graphicOrderList) { + if (pair.graphic == aGraphic) return pair.zOrder; + } + return 0; } - public ArrayList getGraphicList() + public ArrayList getGraphicList() { -// ArrayList list = new ArrayList(); -// for (Enumeration e = graphicNodeDict.keys(); e.hasMoreElements();) -// { -// GraphicObject graphic = e.nextElement(); -// list.add(graphic); -// } -// return list; - return graphicOrderList; } @@ -302,35 +280,50 @@ public CustomNode nodeForGraphic(GraphicObject aGraphic) public void removeGraphic(GraphicObject aGraphic) { graphicNodeDict.remove(aGraphic); - graphicOrderList.remove(aGraphic); + + for (GraphicZOrderPair pair : graphicOrderList) + { + if (pair.graphic == aGraphic) + { + graphicOrderList.remove(pair); + break; + } + } } - public void swapGraphics(GraphicObject aA, GraphicObject aB) - { - int indexA = graphicOrderList.indexOf(aA); - int indexB = graphicOrderList.indexOf(aB); - - graphicOrderList.set(indexA, aB); - graphicOrderList.set(indexB, aA); - - //this.rebuild(); - } public AnimationCell copy() { AnimationCell newCell = new AnimationCell(); newCell.setDelay(this.getDelay()); - for (int i=0; i + { + public int zOrder; + public GraphicObject graphic; + + public GraphicZOrderPair(GraphicObject graphic, int zOrder) + { + this.graphic = graphic; + this.zOrder = zOrder; + } + + @Override + public int compareTo(GraphicZOrderPair other) + { + return Integer.compare(zOrder, other.zOrder); + } + } } \ No newline at end of file diff --git a/src/dfEditor/animation/AnimationController.java b/src/dfEditor/animation/AnimationController.java index 85b999f..efb95bc 100644 --- a/src/dfEditor/animation/AnimationController.java +++ b/src/dfEditor/animation/AnimationController.java @@ -42,7 +42,9 @@ import javax.swing.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; -import dfEditor.CustomComponents.*; +import dfEditor.CustomComponents.*; +import dfEditor.animation.AnimationCell.GraphicZOrderPair; + import javax.swing.JTextField; import javax.swing.plaf.basic.*; import java.awt.Dimension; @@ -1407,7 +1409,7 @@ private void zOrderSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN if (value != cell.zOrderOfGraphic(graphic)) { - commands.add(new SetGraphicZOrderCommand(cell, graphic, this, value)); + commands.add(new SetGraphicZOrderCommand(cell, graphic, this, value)); } } } @@ -1443,12 +1445,11 @@ private void populateSpriteListFromCell(AnimationCell aCell) if (aCell != null) { - ArrayList array = aCell.getGraphicList(); - for (int i=0; i graphicObjectList = new ArrayList(); + for (GraphicZOrderPair pair : aCell.getGraphicList()) + { + graphicObjectList.add(pair.graphic); + } + + setDrawStack(graphicObjectList); + } else clear(); //populateFromCell(aCell); @@ -192,13 +199,10 @@ private void drawOnionSkins(Graphics g) { alpha /= 3.0f; AnimationCell cell = onionSkins[i]; - ArrayList graphics = cell.getGraphicList(); - for (int j=0; j graphicList = cell.getGraphicList(); - for (int i=0; i