mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 17:22:09 +10:00
hotspot tracking optimization
This commit is contained in:
Binary file not shown.
+6
-1
@@ -9,6 +9,7 @@ import java.awt.Point;
|
||||
import java.awt.geom.CubicCurve2D;
|
||||
|
||||
import org.diylc.core.ComponentState;
|
||||
import org.diylc.core.IDrawingObserver;
|
||||
import org.diylc.core.Project;
|
||||
import org.diylc.core.annotations.EditableProperty;
|
||||
import org.diylc.core.measures.Size;
|
||||
@@ -57,8 +58,11 @@ public abstract class AbstractCurvedComponent<T> extends AbstractTransparentComp
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics2D g2d, ComponentState componentState, Project project) {
|
||||
public void draw(Graphics2D g2d, ComponentState componentState, Project project,
|
||||
IDrawingObserver drawingObserver) {
|
||||
if (componentState == ComponentState.SELECTED || componentState == ComponentState.DRAGGING) {
|
||||
// Do not track guidelines.
|
||||
drawingObserver.stopTracking();
|
||||
g2d.setStroke(new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER, 10f,
|
||||
new float[] { 2f, 3f }, 5f));
|
||||
g2d.setColor(GUIDELINE_COLOR);
|
||||
@@ -68,6 +72,7 @@ public abstract class AbstractCurvedComponent<T> extends AbstractTransparentComp
|
||||
controlPoints[2].y);
|
||||
g2d.drawLine(controlPoints[2].x, controlPoints[2].y, controlPoints[3].x,
|
||||
controlPoints[3].y);
|
||||
drawingObserver.startTracking();
|
||||
}
|
||||
CubicCurve2D curve = new CubicCurve2D.Double(controlPoints[0].x, controlPoints[0].y,
|
||||
controlPoints[1].x, controlPoints[1].y, controlPoints[2].x, controlPoints[2].y,
|
||||
|
||||
+5
-1
@@ -11,6 +11,7 @@ import java.awt.Rectangle;
|
||||
import java.awt.Shape;
|
||||
|
||||
import org.diylc.core.ComponentState;
|
||||
import org.diylc.core.IDrawingObserver;
|
||||
import org.diylc.core.Project;
|
||||
import org.diylc.core.annotations.EditableProperty;
|
||||
import org.diylc.core.measures.Size;
|
||||
@@ -55,7 +56,8 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics2D g2d, ComponentState componentState, Project project) {
|
||||
public void draw(Graphics2D g2d, ComponentState componentState, Project project,
|
||||
IDrawingObserver drawingObserver) {
|
||||
double distance = points[0].distance(points[1]);
|
||||
Shape shape = getBodyShape();
|
||||
if (shape == null) {
|
||||
@@ -92,6 +94,8 @@ public abstract class AbstractLeadedComponent<T> extends AbstractTransparentComp
|
||||
(int) ((shapeRect.width - distance) / 2 + leadLenght), (int) shapeRect.height / 2);
|
||||
g2d.drawLine((int) (shapeRect.width + distance) / 2, (int) shapeRect.height / 2,
|
||||
(int) ((shapeRect.width + distance) / 2 - leadLenght), (int) shapeRect.height / 2);
|
||||
// Do not track the label because that area was already covered.
|
||||
drawingObserver.stopTracking();
|
||||
// Draw label.
|
||||
g2d.setFont(Constants.LABEL_FONT);
|
||||
g2d
|
||||
|
||||
+4
-2
@@ -39,7 +39,7 @@ public interface IDIYComponent<T> extends Serializable {
|
||||
public static final int ABOVE_BOARD = 6;
|
||||
public static final int COMPONENT = 7;
|
||||
public static final int ABOVE_COMPONENT = 8;
|
||||
|
||||
|
||||
public static Color SELECTION_COLOR = Color.red;
|
||||
|
||||
/**
|
||||
@@ -93,8 +93,10 @@ public interface IDIYComponent<T> extends Serializable {
|
||||
* @param g2d
|
||||
* @param componentState
|
||||
* @param project
|
||||
* @param drawingObserver
|
||||
*/
|
||||
void draw(Graphics2D g2d, ComponentState componentState, Project project);
|
||||
void draw(Graphics2D g2d, ComponentState componentState, Project project,
|
||||
IDrawingObserver drawingObserver);
|
||||
|
||||
/**
|
||||
* Draws icon representation of the component. This should not depend on
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package org.diylc.core;
|
||||
|
||||
/**
|
||||
* Interface for tracking component drawing process. Components may use it to
|
||||
* stop or restart tracking. Anything drawn while tracking is stopped will not
|
||||
* be added to the mouse hot-spot area.
|
||||
*
|
||||
* @author Branislav Stojkovic
|
||||
*/
|
||||
public interface IDrawingObserver {
|
||||
|
||||
void stopTracking();
|
||||
|
||||
void startTracking();
|
||||
}
|
||||
+62
-22
@@ -37,6 +37,8 @@ import java.awt.image.renderable.RenderableImage;
|
||||
import java.text.AttributedCharacterIterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.diylc.core.IDrawingObserver;
|
||||
|
||||
/**
|
||||
* {@link Graphics2D} wrapper that keeps track of all drawing actions and
|
||||
* creates an {@link Area} that corresponds to drawn objects. Before each
|
||||
@@ -47,12 +49,13 @@ import java.util.Map;
|
||||
*
|
||||
* @author Branislav Stojkovic
|
||||
*/
|
||||
class G2DWrapper extends Graphics2D {
|
||||
class G2DWrapper extends Graphics2D implements IDrawingObserver {
|
||||
|
||||
public static int LINE_SENSITIVITY_MARGIN = 2;
|
||||
public static int CURVE_SENSITIVITY = 6;
|
||||
|
||||
private boolean drawingComponent = false;
|
||||
private boolean trackingAllowed = true;
|
||||
|
||||
private Graphics2D canvasGraphics;
|
||||
private Stroke originalStroke;
|
||||
@@ -89,6 +92,7 @@ class G2DWrapper extends Graphics2D {
|
||||
originalFont = canvasGraphics.getFont();
|
||||
currentTx = new AffineTransform();
|
||||
lastShape = null;
|
||||
startTracking();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,13 +111,23 @@ class G2DWrapper extends Graphics2D {
|
||||
return currentArea;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startTracking() {
|
||||
this.trackingAllowed = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopTracking() {
|
||||
this.trackingAllowed = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends shape interior to the current component area.
|
||||
*
|
||||
* @param s
|
||||
*/
|
||||
private void appendShape(Shape s) {
|
||||
if (!drawingComponent) {
|
||||
if (!drawingComponent || !trackingAllowed) {
|
||||
return;
|
||||
}
|
||||
Area area = new Area(s);
|
||||
@@ -129,7 +143,7 @@ class G2DWrapper extends Graphics2D {
|
||||
*/
|
||||
private void appendShapeOutline(Shape s) {
|
||||
// Do not add shape outline if the same shape has been filled recently.
|
||||
if (!drawingComponent || s.equals(lastShape)) {
|
||||
if (!drawingComponent || !trackingAllowed || s.equals(lastShape)) {
|
||||
return;
|
||||
}
|
||||
lastShape = s;
|
||||
@@ -235,7 +249,9 @@ class G2DWrapper extends Graphics2D {
|
||||
@Override
|
||||
public void draw(Shape s) {
|
||||
canvasGraphics.draw(s);
|
||||
appendShapeOutline(s);
|
||||
if (drawingComponent && trackingAllowed) {
|
||||
appendShapeOutline(s);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -270,19 +286,23 @@ class G2DWrapper extends Graphics2D {
|
||||
@Override
|
||||
public void drawString(String str, int x, int y) {
|
||||
canvasGraphics.drawString(str, x, y);
|
||||
FontMetrics fontMetrics = canvasGraphics.getFontMetrics();
|
||||
Rectangle2D rect = fontMetrics.getStringBounds(str, canvasGraphics);
|
||||
appendShape(new Rectangle2D.Double(rect.getX() + x, rect.getY() + y, rect.getWidth(), rect
|
||||
.getHeight()));
|
||||
if (drawingComponent && trackingAllowed) {
|
||||
FontMetrics fontMetrics = canvasGraphics.getFontMetrics();
|
||||
Rectangle2D rect = fontMetrics.getStringBounds(str, canvasGraphics);
|
||||
appendShape(new Rectangle2D.Double(rect.getX() + x, rect.getY() + y, rect.getWidth(),
|
||||
rect.getHeight()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawString(String str, float x, float y) {
|
||||
canvasGraphics.drawString(str, x, y);
|
||||
FontMetrics fontMetrics = canvasGraphics.getFontMetrics();
|
||||
Rectangle2D rect = fontMetrics.getStringBounds(str, canvasGraphics);
|
||||
appendShape(new Rectangle2D.Double(rect.getX() + x, rect.getY() + y, rect.getWidth(), rect
|
||||
.getHeight()));
|
||||
if (drawingComponent && trackingAllowed) {
|
||||
FontMetrics fontMetrics = canvasGraphics.getFontMetrics();
|
||||
Rectangle2D rect = fontMetrics.getStringBounds(str, canvasGraphics);
|
||||
appendShape(new Rectangle2D.Double(rect.getX() + x, rect.getY() + y, rect.getWidth(),
|
||||
rect.getHeight()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -508,19 +528,25 @@ class G2DWrapper extends Graphics2D {
|
||||
@Override
|
||||
public void drawLine(int x1, int y1, int x2, int y2) {
|
||||
canvasGraphics.drawLine(x1, y1, x2, y2);
|
||||
appendShapeOutline(new Line2D.Double(x1, y1, x2, y2));
|
||||
if (drawingComponent && trackingAllowed) {
|
||||
appendShapeOutline(new Line2D.Double(x1, y1, x2, y2));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawOval(int x, int y, int width, int height) {
|
||||
canvasGraphics.drawOval(x, y, width, height);
|
||||
appendShapeOutline(new Ellipse2D.Double(x, y, width, height));
|
||||
if (drawingComponent && trackingAllowed) {
|
||||
appendShapeOutline(new Ellipse2D.Double(x, y, width, height));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawPolygon(int[] points, int[] points2, int points3) {
|
||||
canvasGraphics.drawPolygon(points, points2, points3);
|
||||
appendShapeOutline(new Polygon(points, points2, points3));
|
||||
if (drawingComponent && trackingAllowed) {
|
||||
appendShapeOutline(new Polygon(points, points2, points3));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -531,43 +557,57 @@ class G2DWrapper extends Graphics2D {
|
||||
@Override
|
||||
public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
|
||||
canvasGraphics.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
|
||||
appendShapeOutline(new RoundRectangle2D.Double(x, y, width, height, arcWidth, arcHeight));
|
||||
if (drawingComponent && trackingAllowed) {
|
||||
appendShapeOutline(new RoundRectangle2D.Double(x, y, width, height, arcWidth, arcHeight));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
|
||||
canvasGraphics.fillArc(x, y, width, height, startAngle, arcAngle);
|
||||
appendShape(new Arc2D.Double(x, y, width, height, startAngle, arcAngle, Arc2D.PIE));
|
||||
if (drawingComponent && trackingAllowed) {
|
||||
appendShape(new Arc2D.Double(x, y, width, height, startAngle, arcAngle, Arc2D.PIE));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillOval(int x, int y, int width, int height) {
|
||||
canvasGraphics.fillOval(x, y, width, height);
|
||||
appendShape(new Ellipse2D.Double(x, y, width, height));
|
||||
if (drawingComponent && trackingAllowed) {
|
||||
appendShape(new Ellipse2D.Double(x, y, width, height));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillPolygon(int[] points, int[] points2, int points3) {
|
||||
canvasGraphics.fillPolygon(points, points2, points3);
|
||||
appendShape(new Polygon(points, points2, points3));
|
||||
if (drawingComponent && trackingAllowed) {
|
||||
appendShape(new Polygon(points, points2, points3));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawRect(int x, int y, int width, int height) {
|
||||
canvasGraphics.drawRect(x, y, width, height);
|
||||
appendShapeOutline(new Rectangle(x, y, width, height));
|
||||
if (drawingComponent && trackingAllowed) {
|
||||
appendShapeOutline(new Rectangle(x, y, width, height));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillRect(int x, int y, int width, int height) {
|
||||
canvasGraphics.fillRect(x, y, width, height);
|
||||
appendShape(new Rectangle(x, y, width, height));
|
||||
if (drawingComponent && trackingAllowed) {
|
||||
appendShape(new Rectangle(x, y, width, height));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) {
|
||||
canvasGraphics.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
|
||||
appendShape(new RoundRectangle2D.Double(x, y, width, height, arcWidth, arcHeight));
|
||||
if (drawingComponent && trackingAllowed) {
|
||||
appendShape(new RoundRectangle2D.Double(x, y, width, height, arcWidth, arcHeight));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+1
-1
@@ -365,7 +365,7 @@ public class Presenter implements IPlugInPort {
|
||||
}
|
||||
}
|
||||
// Draw the component through the g2dWrapper.
|
||||
component.draw(g2dWrapper, state, currentProject);
|
||||
component.draw(g2dWrapper, state, currentProject, g2dWrapper);
|
||||
componentAreaMap.put(component, g2dWrapper.finishedDrawingComponent());
|
||||
}
|
||||
// Draw control points.
|
||||
|
||||
+3
-1
@@ -6,6 +6,7 @@ import java.awt.Point;
|
||||
|
||||
import org.diylc.core.ComponentState;
|
||||
import org.diylc.core.IDIYComponent;
|
||||
import org.diylc.core.IDrawingObserver;
|
||||
import org.diylc.core.Project;
|
||||
import org.diylc.core.annotations.ComponentDescriptor;
|
||||
import org.diylc.core.annotations.EditableProperty;
|
||||
@@ -115,7 +116,8 @@ public class MockDIYComponent implements IDIYComponent<Capacitance> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics2D g2d, ComponentState componentState, Project project) {
|
||||
public void draw(Graphics2D g2d, ComponentState componentState, Project project,
|
||||
IDrawingObserver drawingObserver) {
|
||||
g2d.setColor(componentState.equals(ComponentState.SELECTED) ? color : color.darker());
|
||||
g2d.fillRect(leftTopCorner.x, leftTopCorner.y, 200, 50);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user