More rendering speedup
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Thu, 6 Jun 2019 19:41:51 +0000 (21:41 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Thu, 6 Jun 2019 19:41:51 +0000 (21:41 +0200)
net.mograsim.logic.ui/src/net/mograsim/logic/ui/LogicUIRenderer.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/wires/GUIWire.java

index 5182a24..4b451fc 100644 (file)
@@ -23,7 +23,13 @@ public class LogicUIRenderer
        public void render(GeneralGC gc, Rectangle visibleRegion)
        {
                gc.setLineWidth(.5);
-               model.getWires().forEach(w -> w.render(gc));
+               model.getWires().forEach(w ->
+               {
+                       Rectangle bounds = w.getBounds();
+                       double lw = gc.getLineWidth();
+                       if (visibleRegion.intersects(bounds.x - lw, bounds.y - lw, bounds.width + lw + lw, bounds.height + lw + lw))
+                               w.render(gc);
+               });
                model.getComponents().forEach(c -> renderComponent(gc, c, visibleRegion));
        }
 
index 2ad79c5..6de3b51 100644 (file)
@@ -6,6 +6,7 @@ import java.util.List;
 \r
 import net.haspamelodica.swt.helper.gcs.GeneralGC;\r
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;\r
+import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;\r
 import net.mograsim.logic.core.LogicObservable;\r
 import net.mograsim.logic.core.LogicObserver;\r
 import net.mograsim.logic.core.types.BitVectorFormatter;\r
@@ -20,6 +21,7 @@ public class GUIWire
        private Pin pin1;\r
        private Pin pin2;\r
        private Point[] path;\r
+       private final Rectangle bounds;\r
        private double[] effectivePath;\r
 \r
        private final List<Runnable> redrawListeners;\r
@@ -74,6 +76,7 @@ public class GUIWire
                this.pin2 = pin2;\r
 \r
                this.path = path == null ? null : Arrays.copyOf(path, path.length);\r
+               this.bounds = new Rectangle(0, 0, -1, -1);\r
 \r
                redrawListeners = new ArrayList<>();\r
 \r
@@ -88,6 +91,12 @@ public class GUIWire
        private void recalculateEffectivePath()\r
        {\r
                Point pos1 = pin1.getPos(), pos2 = pin2.getPos();\r
+\r
+               double boundsX1 = Math.min(pos1.x, pos2.x);\r
+               double boundsY1 = Math.min(pos1.y, pos2.y);\r
+               double boundsX2 = Math.max(pos1.x, pos2.x);\r
+               double boundsY2 = Math.max(pos1.y, pos2.y);\r
+\r
                if (path == null)\r
                        effectivePath = new double[] { pos1.x, pos1.y, (pos1.x + pos2.x) / 2, pos1.y, (pos1.x + pos2.x) / 2, pos2.y, pos2.x, pos2.y };\r
                else\r
@@ -97,12 +106,27 @@ public class GUIWire
                        effectivePath[1] = pos1.y;\r
                        for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2)\r
                        {\r
-                               effectivePath[dstI + 0] = path[srcI].x;\r
-                               effectivePath[dstI + 1] = path[srcI].y;\r
+                               double pathX = path[srcI].x;\r
+                               double pathY = path[srcI].y;\r
+                               effectivePath[dstI + 0] = pathX;\r
+                               effectivePath[dstI + 1] = pathY;\r
+                               if (pathX < boundsX1)\r
+                                       boundsX1 = pathX;\r
+                               if (pathX > boundsX2)\r
+                                       boundsX2 = pathX;\r
+                               if (pathY < boundsY1)\r
+                                       boundsY1 = pathY;\r
+                               if (pathY > boundsY2)\r
+                                       boundsY2 = pathY;\r
                        }\r
                        effectivePath[effectivePath.length - 2] = pos2.x;\r
                        effectivePath[effectivePath.length - 1] = pos2.y;\r
                }\r
+\r
+               bounds.x = boundsX1;\r
+               bounds.y = boundsY1;\r
+               bounds.width = boundsX2 - boundsX1;\r
+               bounds.height = boundsY2 - boundsY1;\r
        }\r
 \r
        private void pin1Moved()\r
@@ -122,6 +146,11 @@ public class GUIWire
                model.wireDestroyed(this);\r
        }\r
 \r
+       public Rectangle getBounds()\r
+       {\r
+               return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);\r
+       }\r
+\r
        public void render(GeneralGC gc)\r
        {\r
                ColorHelper.executeWithDifferentForeground(gc, BitVectorFormatter.formatAsColor(end), () -> gc.drawPolyline(effectivePath));\r