Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix reversed z-order #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/dfEditor/GraphicPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
125 changes: 59 additions & 66 deletions src/dfEditor/animation/AnimationCell.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,10 +38,9 @@
* @author s4m20
*/
public class AnimationCell
{
private Dictionary<GraphicObject, Integer> graphicZOrderDict;
{
private Dictionary<GraphicObject, CustomNode> graphicNodeDict;
private ArrayList<GraphicObject> graphicOrderList;
private ArrayList<GraphicZOrderPair> graphicOrderList;

private VolatileImage vImage = null;
private int delay;
Expand All @@ -51,13 +49,12 @@ public AnimationCell()
{
setDelay(1);

graphicZOrderDict = new Hashtable<GraphicObject, Integer>();
graphicNodeDict = new Hashtable<GraphicObject, CustomNode>();
graphicOrderList = new ArrayList<GraphicObject>();
graphicOrderList = new ArrayList<GraphicZOrderPair>();
rebuild();
}

public Point getImageSize()
public Point getImageSize()
{
if (vImage == null)
{
Expand All @@ -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;
Expand All @@ -102,22 +99,21 @@ public void rebuild()
g.setComposite(AlphaComposite.SrcOver);

//for (Enumeration<GraphicObject> 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);
Expand Down Expand Up @@ -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<GraphicObject> getGraphicList()
public ArrayList<GraphicZOrderPair> getGraphicList()
{
// ArrayList<GraphicObject> list = new ArrayList<GraphicObject>();
// for (Enumeration<GraphicObject> e = graphicNodeDict.keys(); e.hasMoreElements();)
// {
// GraphicObject graphic = e.nextElement();
// list.add(graphic);
// }
// return list;

return graphicOrderList;
}

Expand All @@ -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<graphicOrderList.size(); ++i)
for (GraphicZOrderPair pair : graphicOrderList)
{
GraphicObject graphic = graphicOrderList.get(i);
GraphicObject graphic = pair.graphic;
GraphicObject newGraphic = graphic.copy();
newCell.addSprite(this.nodeForGraphic(graphic), newGraphic);
newCell.setZOrder(newGraphic, this.zOrderOfGraphic(graphic));
newCell.setZOrder(newGraphic, pair.zOrder);
}

return newCell;

}

public static class GraphicZOrderPair implements Comparable<GraphicZOrderPair>
{
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);
}
}
}
15 changes: 8 additions & 7 deletions src/dfEditor/animation/AnimationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
}
Expand Down Expand Up @@ -1443,12 +1445,11 @@ private void populateSpriteListFromCell(AnimationCell aCell)

if (aCell != null)
{
ArrayList<GraphicObject> array = aCell.getGraphicList();
for (int i=0; i<array.size(); ++i)
for (GraphicZOrderPair pair : aCell.getGraphicList())
{
array.get(i).setDescription(aCell.nodeForGraphic(array.get(i)).getFullPathName());
model.addElement(array.get(i));
array.get(i).setDescription(aCell.nodeForGraphic(array.get(i)).getFullPathName());
pair.graphic.setDescription(aCell.nodeForGraphic(pair.graphic).getFullPathName());
model.addElement(pair.graphic);
pair.graphic.setDescription(aCell.nodeForGraphic(pair.graphic).getFullPathName());
}

if (selected != null)
Expand Down
20 changes: 12 additions & 8 deletions src/dfEditor/animation/AnimationPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package dfEditor.animation;
import dfEditor.*;
import dfEditor.animation.AnimationCell.GraphicZOrderPair;

import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.util.ArrayList;
Expand Down Expand Up @@ -111,9 +113,14 @@ public void setCell(AnimationCell aCell)
{
if (aCell != null)
{
setDrawStack(aCell.getGraphicList());
}
else
ArrayList<GraphicObject> graphicObjectList = new ArrayList<GraphicObject>();
for (GraphicZOrderPair pair : aCell.getGraphicList())
{
graphicObjectList.add(pair.graphic);
}

setDrawStack(graphicObjectList);
} else
clear();

//populateFromCell(aCell);
Expand Down Expand Up @@ -192,13 +199,10 @@ private void drawOnionSkins(Graphics g)
{
alpha /= 3.0f;
AnimationCell cell = onionSkins[i];
ArrayList<GraphicObject> graphics = cell.getGraphicList();

for (int j=0; j<graphics.size(); ++j)
for (GraphicZOrderPair pair : cell.getGraphicList())
{
GraphicObject graphic = graphics.get(j);

this.drawGraphicRotated(graphic, g, _origin, _zoom, alpha);
this.drawGraphicRotated(pair.graphic, g, _origin, _zoom, alpha);
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/dfEditor/io/AnimationSetWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import dfEditor.*;
import java.io.File;
import dfEditor.animation.*;
import dfEditor.animation.AnimationCell.GraphicZOrderPair;

import com.generationjava.io.xml.*;
import java.io.BufferedWriter;
import java.io.IOException;
Expand Down Expand Up @@ -89,16 +91,15 @@ private void writeAnimToXml(XmlWriter aXmlWriter, Animation aAnimation) throws I
aXmlWriter.writeAttribute("index", aAnimation.getCurrentCellIndex());
aXmlWriter.writeAttribute("delay", cell.getDelay());

ArrayList<GraphicObject> graphicList = cell.getGraphicList();
for (int i=0; i<graphicList.size(); ++i)
for (GraphicZOrderPair pair : cell.getGraphicList())
{
SpriteGraphic graphic = (SpriteGraphic)graphicList.get(i);
SpriteGraphic graphic = (SpriteGraphic)pair.graphic;
CustomNode node = cell.nodeForGraphic(graphic);
aXmlWriter.writeEntity("spr");
aXmlWriter.writeAttribute("name", node.getFullPathName());
aXmlWriter.writeAttribute("x", graphic.getRect().x + graphic.getRect().width/2);
aXmlWriter.writeAttribute("y", graphic.getRect().y + graphic.getRect().height/2);
aXmlWriter.writeAttribute("z", cell.zOrderOfGraphic(graphic));
aXmlWriter.writeAttribute("y", graphic.getRect().y + graphic.getRect().height/2);
aXmlWriter.writeAttribute("z", pair.zOrder);

if (graphic.getAngle() != 0)
aXmlWriter.writeAttribute("angle", graphic.getAngle());
Expand Down