
net.vectorpublish.desktop.vp.api.io.DrawArea Maven / Gradle / Ivy
/*
* Copyright (c) 2016, Peter Rader. All rights reserved.
* ___ ___ __ ______ __ __ __ __
* | | |.-----..----.| |_ .-----..----.| __ \.--.--.| |--.| ||__|.-----.| |--.
* | | || -__|| __|| _|| _ || _|| __/| | || _ || || ||__ --|| |
* \_____/ |_____||____||____||_____||__| |___| |_____||_____||__||__||_____||__|__|
*
* http://www.gnu.org/licenses/gpl-3.0.html
*/
package net.vectorpublish.desktop.vp.api.io;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Set;
import net.vectorpublish.desktop.vp.api.DrawParticipant;
import net.vectorpublish.desktop.vp.api.layer.Hoverable;
import net.vectorpublish.desktop.vp.api.ui.MouseParticipant;
import net.vectorpublish.desktop.vp.api.vpd.DocumentNode;
import net.vectorpublish.desktop.vp.exception.IllegalHeightException;
import net.vectorpublish.desktop.vp.exception.IllegalWidthException;
import net.vectorpublish.desktop.vp.pd.official.RelativeKeyframeRecalculator;
import net.vectorpublish.desktop.vp.pd.official.TechnicalMouseDrag;
import net.vectorpublish.desktop.vp.pd.official.VectorPublishGraphics;
/**
* The drawarea, knows about the Document and paint all elements on the
* Document.
*/
public class DrawArea implements DrawParticipant, Hoverable {
private final int documentWidth;
private final int documentHeight;
private int mousex, mousey;
private DocumentNode doc;
/**
* Creates a DrawArea having a specific size.
*
* @param documentWidth
* The width of the document, somehow a user was asked for the
* size.
* @param documentHeight
* The height of the document, somehow a user was asked for the
* size.
*/
public DrawArea(int documentWidth, int documentHeight) {
if (documentWidth <= 0) {
throw new IllegalWidthException(documentWidth);
}
if (documentHeight <= 0) {
throw new IllegalHeightException(documentHeight);
}
this.documentWidth = documentWidth;
this.documentHeight = documentHeight;
}
@Override
public Dimension getDimensions() {
return new Dimension(documentWidth, documentHeight);
}
/**
* Returns the acutal Document.
*
* If this method is called while painting you will never have
* null
returned because the paint-chain will not be called
* when there is no {@link DocumentNode}.
*
* @return The current {@link DocumentNode} or null
, if no
* document has been selected yet.
*/
public DocumentNode getDocument() {
return doc;
}
@Override
public Cursor getMouseCursor() {
return null;
}
@Override
public boolean isHovered() {
return false;
}
@Override
public boolean opacity() {
return true;
}
@Override
public void paint(VectorPublishGraphics graphics, int width, int height) {
graphics.setColor(Color.white);
graphics.fillRect(0, 0, width, height);
if (doc != null) {
doc.paintChildren(this, graphics, width, height);
}
}
/**
* Paints the grid to the document.
*
* The grid is only painted when the Zoom is deep enougth.
*
* @param graphics
* The Graphics to paint the grid to.
* @param rel
* The recalculator to find the distances.
* @param width
* The width of the Document.
* @param height
* The height of the Document.
*/
public void paintGrid(Graphics graphics, RelativeKeyframeRecalculator rel, int width, int height) {
int calcTechnicalWidth = rel.calcTechnicalWidth(1) / 5;
int alpha = 50 + calcTechnicalWidth;
if (alpha > 250) {
alpha = 250;
}
graphics.setColor(new Color(100, 100, 100, alpha));
Rectangle r = rel.getViewport(true);
int[] mx = new int[r.width], my = new int[r.height];
for (int mxi = 0; mxi < mx.length; mxi++) {
mx[mxi] = rel.calcTechnicalX(mxi + r.x);
}
for (int myi = 0; myi < my.length; myi++) {
my[myi] = rel.calcTechnicalY(myi + r.y);
}
for (int y : my) {
for (int x : mx) {
graphics.drawLine(x - calcTechnicalWidth, y, x + calcTechnicalWidth, y);
graphics.drawLine(x, y - calcTechnicalWidth, x, y + calcTechnicalWidth);
}
}
}
@Override
public void paintOutside(VectorPublishGraphics graphics, RelativeKeyframeRecalculator rel, int width, int height) {
// page border
graphics.setColor(Color.darkGray);
graphics.drawRect(rel.calcTechnicalX(0), rel.calcTechnicalY(0), rel.calcTechnicalX(width) - rel.calcTechnicalX(0),
rel.calcTechnicalY(height) - rel.calcTechnicalY(0));
if (rel.getZoom() > 500) {
paintGrid(graphics, rel, width, height);
}
if (doc != null) {
doc.paintChildrenOuter(this, graphics, rel, width, height);
}
}
/**
* Set the document to work with.
*
* Before the document has been set: a painting is not allowed.
*
* @param doc
* The Document.
*/
public void setDocument(DocumentNode doc) {
this.doc = doc;
}
@Override
public void setHover(boolean hover) {
}
@Override
public Cursor updateMouse(int markerX, int markerY, float docRelX, float docRelY, RelativeKeyframeRecalculator rel, TechnicalMouseDrag pressedLMBSince) {
Cursor lastCursor = null;
if (doc != null) {
final Set pps = doc.getAllPaintParticipants();
for (final MouseParticipant paintParticipant : pps) {
final Cursor proposedCursor = paintParticipant.updateMouse(markerX, markerY, docRelX, docRelY, rel, pressedLMBSince);
if (proposedCursor != null) {
lastCursor = proposedCursor;
}
}
}
mousex = markerX;
mousey = markerY;
return lastCursor;
}
}