Skip to content
Open

Ui #17

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
132 changes: 58 additions & 74 deletions src/dfEditor/GraphicPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.Cursor;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

import dfEditor.command.*;
import dfEditor.commands.*;
import java.awt.geom.AffineTransform;
Expand All @@ -45,13 +48,29 @@ public class GraphicPanel extends javax.swing.JDesktopPane implements MouseMotio
private static final int SELECT_BUTTON = MouseEvent.BUTTON1;
private static final int DRAG_BUTTON = MouseEvent.BUTTON3;
private static final int DRAG_BUTTON_2 = MouseEvent.BUTTON2;

protected static final float MIN_ZOOM = 0.1f;

private static final Color[] checkerBoardCols = {new Color(210,210,210), new Color(255,255,255)};//{new Color(255,200,200), new Color(255,210,210)};
private static final int[] checkerBoardColors = {210, 255};
private static final int CHECKER_BOARD_SIZE = 16;

private static TexturePaint checkerBoardPaint;

static {
BufferedImage checkerBoardImage = new BufferedImage(2, 2, BufferedImage.TYPE_BYTE_GRAY);
int[] pixels = {
checkerBoardColors[0], checkerBoardColors[1],
checkerBoardColors[1], checkerBoardColors[0],
};

checkerBoardImage.getRaster().setPixels(0, 0, 2, 2, pixels);

checkerBoardPaint = new TexturePaint(checkerBoardImage, new Rectangle(CHECKER_BOARD_SIZE * 2, CHECKER_BOARD_SIZE * 2));
}

protected ArrayList<GraphicObject> _drawStack;
protected float _zoom;
protected Point _origin;
protected BufferedImage _checkerBoard;
private Point _lastOrigin;
private Point _lastClickPoint;
protected Rectangle _graphicBounds;
Expand All @@ -62,7 +81,7 @@ public class GraphicPanel extends javax.swing.JDesktopPane implements MouseMotio
private ArrayList<GraphicPanelChangeListener> _changeListeners;
protected boolean _bAllowsEditing;
protected GraphicObject _lastAddedGraphic;
protected Rectangle _multiSelectRect;
protected Rectangle _multiSelectRect;
private static final Color _multiSelectFill = new Color(0,0,0,20);
private GraphicObject _movingGraphic = null;
private int _keyDeltaX = 0;
Expand Down Expand Up @@ -217,8 +236,8 @@ public void setCommandManager(CommandManager aCM)

public void setZoom(float zoom)
{
if (zoom < 0.1f)
zoom = 0.1f;
if (zoom < MIN_ZOOM)
zoom = MIN_ZOOM;

this._zoom = zoom;

Expand Down Expand Up @@ -327,9 +346,9 @@ protected void drawStack(Graphics g, Point aOrigin, float aZoom, float aAlpha)
}
}

protected void drawGraphicRotated(GraphicObject graphic, Graphics g, Point aOrigin, float aZoom, float aAlpha)
protected void drawGraphicRotated(GraphicObject graphic, Graphics g, Point aOrigin, float aZoom, float aAlpha)
{
Graphics2D g2d = (Graphics2D)g;
Graphics2D g2d = (Graphics2D)g;

Rectangle graphicRect = graphic.getRect();

Expand Down Expand Up @@ -580,11 +599,7 @@ protected void dropGraphic(GraphicObject aGraphic, boolean aUndoable)
protected void setDrawStack(ArrayList<GraphicObject> aDrawStack)
{
_drawStack.clear();

for (int i=0; i<aDrawStack.size(); ++i)
{
_drawStack.add(aDrawStack.get(i));
}
_drawStack.addAll(aDrawStack);

repaint();
}
Expand Down Expand Up @@ -695,67 +710,25 @@ public ArrayList<GraphicObject> selectedGraphics()
return selectedGraphics;
}

protected void drawCheckerBoardBuffer(final Graphics g, final Rectangle aRect)
protected void drawCheckerBoard(final Graphics g, final Rectangle aRect)
{
if (_checkerBoard == null)
_checkerBoard = this.makeCheckerBoardBuffer(new Rectangle(0, 0, 256, 256));
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(checkerBoardPaint);

g.clipRect(aRect.x, aRect.y, aRect.width, aRect.height);
for (int x=aRect.x; x<aRect.x + aRect.width; ++x)
{
for (int y=aRect.y; y<aRect.y + aRect.height; ++y)
{
g.drawImage(_checkerBoard, x, y, null);
y += _checkerBoard.getHeight()-1;
}
x += _checkerBoard.getWidth()-1;
}
g.setClip(0, 0, this.getWidth(), this.getHeight());
}

protected BufferedImage makeCheckerBoardBuffer(Rectangle r)
{
BufferedImage img = new BufferedImage(r.width, r.height, BufferedImage.TYPE_INT_ARGB);

if (img != null)
{
Graphics gImg = img.getGraphics();
this.drawTransparencyCheckerBoard(gImg, r);
}

return img;
}

private void drawTransparencyCheckerBoard(Graphics g, Rectangle r)
{
final int SQUARE_SIZE = r.width / 8;

g.setClip(r.x,r.y,r.width,r.height);
// because the TexturePaint is anchored at (0, 0), we need to draw our recangle at (0, 0) to get
// the right texture coordinates. To still have our checkerboard end up at the right location, we can
// use an affine transformation.
AffineTransform oldTransform = g2d.getTransform();
AffineTransform transform = new AffineTransform(oldTransform);
transform.translate(aRect.x, aRect.y);
g2d.setTransform(transform);

g.setColor(checkerBoardCols[0]);
g.fillRect(r.x,r.y,r.width,r.height);

Point squarePoint = new Point(0,0);
for (int y=0; y<(r.height/SQUARE_SIZE)+1; ++y)
{
for (int x=0; x<(r.width/SQUARE_SIZE)+1; ++x)
{
int index = (x + (y % checkerBoardCols.length)) % checkerBoardCols.length;
if (index != 0) //already painted as large bg rect
{
Color curCol = checkerBoardCols[index];
g.setColor(curCol);
g.fillRect(r.x + squarePoint.x, r.y + squarePoint.y, SQUARE_SIZE, SQUARE_SIZE);
}
squarePoint.x += SQUARE_SIZE;
}
squarePoint.x = 0;
squarePoint.y += SQUARE_SIZE;
}

g.setClip(null);
g2d.fillRect(0, 0, aRect.width, aRect.height);

g2d.setPaint(null);
g2d.setTransform(oldTransform);
}

public void notifyGraphicMoved(GraphicObject aGraphic)
{
for (int i=0; i<_changeListeners.size(); ++i)
Expand All @@ -776,12 +749,10 @@ public void mouseMoved(MouseEvent evt)
if (!_bAllowsEditing)
return;

Point p = evt.getPoint();

if (_resizingGraphic != null)
{
_resizeDirection = getResizeDirectionFromPointOnRect(p, convertRectToViewRect(_resizingGraphic.getRect()));
setCursorToResizeCursor(_resizeDirection);
_resizeDirection = getResizeDirectionFromPointOnRect(evt.getPoint(), convertRectToViewRect(_resizingGraphic.getRect()));
setCursorToResizeCursor(_resizeDirection);
}
else
{
Expand Down Expand Up @@ -911,7 +882,20 @@ else if ((evt.getModifiers() & ActionEvent.CTRL_MASK) != 0)

public void mouseWheelMoved(MouseWheelEvent evt)
{
setZoom(getZoom() - (evt.getWheelRotation() * 0.2f));
float oldZoom = getZoom();
float newZoom = oldZoom - (evt.getWheelRotation() * 0.2f);

if (oldZoom == MIN_ZOOM && newZoom <= MIN_ZOOM) return;

float zoomDiffFactor = newZoom / oldZoom;
setZoom(newZoom);

Point mousePosition = evt.getPoint();
Point originDiff = new Point(getOrigin().x - mousePosition.x, getOrigin().y - mousePosition.y);
originDiff.x *= zoomDiffFactor;
originDiff.y *= zoomDiffFactor;

moveContent(originDiff, mousePosition);
}

public void mouseReleased(MouseEvent evt)
Expand Down
2 changes: 1 addition & 1 deletion src/dfEditor/SpriteImagePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected void draw(Graphics g)
{
super.draw(g);

this.drawCheckerBoardBuffer(g, convertRectToViewRect(_graphicBounds));
this.drawCheckerBoard(g, convertRectToViewRect(_graphicBounds));
}

@Override
Expand Down
28 changes: 3 additions & 25 deletions src/dfEditor/SpritesheetPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void setController(SpritesheetController aController)

@Override
protected void draw(Graphics g)
{
{
super.draw(g);

if (_pix != null)
Expand All @@ -61,9 +61,9 @@ protected void draw(Graphics g)
Point origin = getOrigin();
Rectangle r = new Rectangle(origin.x, origin.y, actualSize.x, actualSize.y);

this.drawCheckerBoardBuffer(g, r);
this.drawCheckerBoard(g, r);

g.drawImage(_pix.getImage(), r.x, r.y, r.width, r.height, this);
g.drawImage(_pix.getImage(), r.x, r.y, r.width, r.height, this);
}
}

Expand Down Expand Up @@ -96,26 +96,6 @@ public void setImage(BufferedImage aImage)
_pix = new PixelBuffer(aImage);
}

@Override
public void setZoom(float aZoom)
{
if (_pix == null)
{
super.setZoom(aZoom);
return;
}

Point oldImgSize = actualImageSize();
super.setZoom(aZoom);
Point imgSize = actualImageSize();

int xDiff = imgSize.x - oldImgSize.x;
int yDiff = imgSize.y - oldImgSize.y;

getOrigin().x -= xDiff >> 1;
getOrigin().y -= yDiff >> 1;
}

public Rectangle suggestVisibleSpriteRect()
{
final int edgeInsetPix = 0;
Expand Down Expand Up @@ -224,8 +204,6 @@ private Point actualImageSize()
getZoom() );
}



@Override
public void mouseClicked(MouseEvent e)
{
Expand Down
20 changes: 17 additions & 3 deletions src/dfEditor/animation/AnimationStripPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@
* @author s4m20
*/
public class AnimationStripPanel extends javax.swing.JPanel implements AnimationDataListener, MouseMotionListener, MouseListener, ActionListener
{
{
// the distance in pixels an animation cell needs to be dragged to count as dragging
private static final int MOUSE_DRAG_THRESHOLD = 10;

private Animation animation = null;
private ArrayList<Slot> slotList = null;
private AnimationController controller = null;
private Point mousePoint = null;
private Point mousePressedPoint = null;
private boolean dragging = false;
private int insertBeforeSlotIndex = -1;
private Timer timer = null;
private int currentSlotInAnimation = -1;
Expand Down Expand Up @@ -446,6 +451,13 @@ public void mouseDragged(MouseEvent evt)
Point p = evt.getPoint();

mousePoint = p;

if (!dragging && (Math.abs(p.x - mousePressedPoint.x) <= MOUSE_DRAG_THRESHOLD))
{
return;
}

dragging = true;

int xOffset = 0;

Expand Down Expand Up @@ -490,6 +502,7 @@ public void mouseDragged(MouseEvent evt)
public void mousePressed(MouseEvent evt)
{
Point p = evt.getPoint();
mousePressedPoint = p;
controller.stripIndexSelected(-1);

boolean selectedSlot = false;
Expand Down Expand Up @@ -551,7 +564,8 @@ public void mouseReleased(MouseEvent evt)
if (orderChanged && insertBeforeSlotIndex < slotList.size())
slotList.get(insertBeforeSlotIndex).setSelected(true);

insertBeforeSlotIndex = -1;
insertBeforeSlotIndex = -1;
dragging = false;
}

public void mouseExited(MouseEvent evt)
Expand All @@ -572,7 +586,7 @@ public void mouseClicked(MouseEvent evt)
// just a dumb object really... could have come up with an elaborate
// design for this whole strip class but just got it working... can always
// revisit!
private class Slot
private static class Slot
{
static final int MARGIN = 3;

Expand Down