introduce another clip rect for the cache

This commit is contained in:
bancika
2020-04-09 10:52:08 +02:00
parent 9676a21b76
commit ae1aa7bde6
15 changed files with 50 additions and 27 deletions
@@ -26,6 +26,7 @@ import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -192,9 +193,10 @@ public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor, IKeyP
* @param g2d
* @param drawOptions specific drawing options
* @param filter
* @param externalZoom
* @param externalZoom
* @param visibleRect
*/
void draw(Graphics2D g2d, Set<DrawOption> drawOptions, IComponentFiler filter, Double externalZoom);
void draw(Graphics2D g2d, Set<DrawOption> drawOptions, IComponentFiler filter, Double externalZoom, Rectangle2D visibleRect);
Double[] getAvailableZoomLevels();
@@ -87,7 +87,7 @@ public class DrawingCache {
*/
@SuppressWarnings("unchecked")
public void draw(IDIYComponent<?> component, G2DWrapper g2d, ComponentState componentState,
boolean outlineMode, Project project, double zoom) {
boolean outlineMode, Project project, double zoom, Rectangle2D visibleRect) {
ComponentType type = ComponentProcessor.getInstance()
.extractComponentTypeFrom((Class<? extends IDIYComponent<?>>) component.getClass());
@@ -99,17 +99,20 @@ public class DrawingCache {
long componentStart = System.nanoTime();
// temporarily scale the graphic to compensate for the zoom, since we are rendering an image with the zoom appled
if (Math.abs(1.0 - zoom) > 1e-4) {
g2d.getCanvasGraphics().scale(1 / zoom, 1 / zoom);
}
try {
// draw cached image
g2d.getCanvasGraphics().drawImage(value.getImage(), (int) ((firstPoint.x - value.getDx()) * zoom),
(int) ((firstPoint.y - value.getDy())* zoom), null);
} finally {
// only render the cached image if it's within the clipping region
if (visibleRect == null || visibleRect.intersects(value.getCacheBounds())) {
// temporarily scale the graphic to compensate for the zoom, since we are rendering an image with the zoom appled
if (Math.abs(1.0 - zoom) > 1e-4) {
g2d.getCanvasGraphics().scale(zoom, zoom);
g2d.getCanvasGraphics().scale(1 / zoom, 1 / zoom);
}
try {
// draw cached image
g2d.getCanvasGraphics().drawImage(value.getImage(), (int) ((firstPoint.x - value.getDx()) * zoom),
(int) ((firstPoint.y - value.getDy())* zoom), null);
} finally {
if (Math.abs(1.0 - zoom) > 1e-4) {
g2d.getCanvasGraphics().scale(zoom, zoom);
}
}
}
@@ -125,7 +128,7 @@ public class DrawingCache {
}
stats.getSecond().add(componentEnd - componentStart);
// copy over tracking area from the cache
// copy over tracking area from the cache, regardless if we rendered or not
g2d.merge(value.getCurrentArea(), value.getContinuityPositiveAreas(), value.getContinuityNegativeAreas());
} else {
// no caching, just draw as usual
@@ -233,7 +236,7 @@ public class DrawingCache {
// add to the cache
value = new CacheValue(component, image, wrapper.getCurrentArea(), wrapper.getContinuityPositiveAreas(),
wrapper.getContinuityNegativeAreas(), componentState, zoom, dx, dy);
wrapper.getContinuityNegativeAreas(), componentState, zoom, dx, dy, rect);
imageCache.put(component, value);
}
@@ -250,11 +253,13 @@ public class DrawingCache {
double zoom;
int dx;
int dy;
Rectangle2D cacheBounds;
public CacheValue(IDIYComponent<?> component, BufferedImage image, Area currentArea,
Map<String, Area> continuityPositiveAreas, Map<String, Area> continuityNegativeAreas,
ComponentState state, double zoom, int dx, int dy) {
ComponentState state, double zoom, int dx, int dy, Rectangle2D cacheBounds) {
super();
this.cacheBounds = cacheBounds;
// take a copy of the component, so we can check if it changed in the meantime
try {
this.component = component.clone();
@@ -305,6 +310,10 @@ public class DrawingCache {
public int getDy() {
return dy;
}
public Rectangle2D getCacheBounds() {
return cacheBounds;
}
@Override
public String toString() {
@@ -139,12 +139,13 @@ public class DrawingManager {
* @param componentSlot
* @param dragInProgress
* @param externalZoom
* @param visibleRect
* @return
*/
public List<IDIYComponent<?>> drawProject(Graphics2D g2d, Project project, Set<DrawOption> drawOptions,
IComponentFiler filter, Rectangle selectionRect, Collection<IDIYComponent<?>> selectedComponents,
Set<IDIYComponent<?>> lockedComponents, Set<IDIYComponent<?>> groupedComponents, List<Point> controlPointSlot,
List<IDIYComponent<?>> componentSlot, boolean dragInProgress, Double externalZoom) {
List<IDIYComponent<?>> componentSlot, boolean dragInProgress, Double externalZoom, Rectangle2D visibleRect) {
long totalStartTime = System.nanoTime();
failedComponents.clear();
if (project == null) {
@@ -242,11 +243,16 @@ public class DrawingManager {
// translate to the new (0, 0)
g2d.transform(AffineTransform.getTranslateInstance(extraSpace, extraSpace));
if (visibleRect != null)
visibleRect.setRect(visibleRect.getX() - extraSpace, visibleRect.getY() - extraSpace,
visibleRect.getWidth(), visibleRect.getHeight());
}
// apply zoom
if (Math.abs(1.0 - zoom) > 1e-4) {
g2dWrapper.scale(zoom, zoom);
visibleRect.setRect(visibleRect.getX() / zoom, visibleRect.getY() / zoom,
visibleRect.getWidth() / zoom, visibleRect.getHeight() / zoom);
}
// Composite mainComposite = g2d.getComposite();
@@ -301,7 +307,7 @@ public class DrawingManager {
try {
if (drawOptions.contains(DrawOption.ENABLE_CACHING)) // go through the DrawingCache
DrawingCache.Instance.draw(component, g2dWrapper, state, drawOptions.contains(DrawOption.OUTLINE_MODE), project, zoom);
DrawingCache.Instance.draw(component, g2dWrapper, state, drawOptions.contains(DrawOption.OUTLINE_MODE), project, zoom, visibleRect);
else // go stragiht to the wrapper
component.draw(g2dWrapper, state, outlineMode, project, g2dWrapper);
@@ -459,7 +459,7 @@ public class Presenter implements IPlugInPort {
}
@Override
public void draw(Graphics2D g2d, Set<DrawOption> drawOptions, final IComponentFiler filter, Double externalZoom) {
public void draw(Graphics2D g2d, Set<DrawOption> drawOptions, final IComponentFiler filter, Double externalZoom, Rectangle2D visibleRect) {
if (currentProject == null) {
return;
}
@@ -501,7 +501,8 @@ public class Presenter implements IPlugInPort {
getLockedComponents(),
groupedComponents,
Arrays.asList(instantiationManager.getFirstControlPoint(),
instantiationManager.getPotentialControlPoint()), componentSlotToDraw, dragInProgress, externalZoom);
instantiationManager.getPotentialControlPoint()), componentSlotToDraw, dragInProgress, externalZoom,
visibleRect);
List<String> failedComponentNames = new ArrayList<String>();
for (IDIYComponent<?> component : failedComponents) {
failedComponentNames.add(component.getName());
@@ -2902,6 +2902,10 @@
<changeType>IMPROVEMENT</changeType>
<description>Improved performance math calculations used to model "Twisted Wires"</description>
</org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change>
<changeType>IMPROVEMENT</changeType>
<description>Improved performance of "Highlight Connected Areas" feature</description>
</org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change>
<changeType>BUG_FIX</changeType>
<description>Older files that contain "Open Jack 1/4" components sometimes open with the jacks rotated the wrong way</description>