Search in sources :

Example 16 with Transform

use of com.codename1.ui.Transform in project CodenameOne by codenameone.

the class ChartComponent method pointerDragged.

@Override
public void pointerDragged(int[] x, int[] y) {
    if (x.length > 1) {
        if (!zoomEnabled) {
            super.pointerDragged(x, y);
            return;
        }
        // Pinch zoom
        if (chart instanceof XYChart) {
            XYChart xyChart = (XYChart) chart;
            double[] panLimits = xyChart.getRenderer().getPanLimits();
            if (zoomStart == null) {
                zoomStart = new Point((x[0] + x[1]) / 2, (y[0] + y[1]) / 2);
                zoomDistStartX = Math.abs(x[0] - x[1]);
                zoomDistStartY = Math.abs(y[0] - y[1]);
                zoomStartBBox = getBBox();
                if (initialZoomBBox == null) {
                    initialZoomBBox = zoomStartBBox.translateScreenCoords(0, 0);
                }
            } else {
                int dx = Math.abs(x[0] - x[1]);
                int dy = Math.abs(y[0] - y[1]);
                if (dx == 0)
                    dx = 1;
                if (dy == 0)
                    dy = 1;
                double zoomX = zoomDistStartX / dx;
                double zoomY = zoomDistStartY / dy;
                BBox newBounds = zoomStartBBox.scaleScreenCoords((float) zoomX, (float) zoomY);
                double minX = newBounds.minX;
                double minY = newBounds.minY;
                double maxX = newBounds.maxX;
                double maxY = newBounds.maxY;
                if (panLimits != null) {
                    // Make sure that this zoom doesn't exceed the maxZoomIn value
                    if (minX < panLimits[0]) {
                        maxX = maxX + panLimits[0] - minX;
                        minX = panLimits[0];
                    }
                    if (minY < panLimits[2]) {
                        maxY = maxY + panLimits[2] - minY;
                        minY = panLimits[2];
                    }
                    if (maxX > panLimits[1]) {
                        minX = minX + panLimits[1] - maxX;
                        maxX = panLimits[1];
                    }
                    if (maxY > panLimits[3]) {
                        minY = minY + panLimits[3] - maxY;
                        maxY = panLimits[3];
                    }
                }
                double[] zoomLimits = xyChart.getRenderer().getZoomLimits();
                if (zoomLimits != null && zoomLimits[0] != 0) {
                    if (maxX - minX < zoomLimits[0]) {
                        maxX = xyChart.getRenderer().getXAxisMax();
                        minX = xyChart.getRenderer().getXAxisMin();
                    }
                }
                if (zoomLimits != null && zoomLimits[1] != 0) {
                    if (maxX - minX > zoomLimits[1]) {
                        maxX = xyChart.getRenderer().getXAxisMax();
                        minX = xyChart.getRenderer().getXAxisMin();
                    }
                }
                if (zoomLimits != null && zoomLimits[2] != 0) {
                    if (maxY - minY < zoomLimits[2]) {
                        maxY = xyChart.getRenderer().getYAxisMax();
                        minY = xyChart.getRenderer().getYAxisMin();
                    }
                }
                if (zoomLimits != null && zoomLimits[3] != 0) {
                    if (maxY - minY > zoomLimits[3]) {
                        maxY = xyChart.getRenderer().getYAxisMax();
                        minY = xyChart.getRenderer().getYAxisMin();
                    }
                }
                if (!xyChart.getRenderer().isZoomXEnabled()) {
                    minX = xyChart.getRenderer().getXAxisMin();
                    maxX = xyChart.getRenderer().getXAxisMax();
                }
                if (!xyChart.getRenderer().isZoomYEnabled()) {
                    minY = xyChart.getRenderer().getYAxisMin();
                    maxY = xyChart.getRenderer().getYAxisMax();
                }
                xyChart.getRenderer().setRange(new double[] { minX, maxX, minY, maxY });
                chartBoundsChanged();
                this.repaint();
            }
        } else {
            if (zoomStart == null) {
                zoomStart = new Point((x[0] + x[1]) / 2, (y[0] + y[1]) / 2);
                zoomTransformStart = Transform.makeIdentity();
                if (transform != null) {
                    zoomTransformStart.concatenate(transform);
                }
                int dx = Math.abs(x[0] - x[1]) / 2;
                int dy = Math.abs(y[0] - y[1]) / 2;
                zoomDistStart = Math.sqrt(dx * dx + dy * dy);
            } else {
                int dx = Math.abs(x[0] - x[1]) / 2;
                int dy = Math.abs(y[0] - y[1]) / 2;
                double zoomDist = Math.sqrt(dx * dx + dy * dy);
                if (zoomDist == 0) {
                    zoomDist = 1;
                }
                transform = Transform.makeIdentity();
                transform.translate(zoomStart.getX(), zoomStart.getY());
                transform.scale((float) (zoomDist / zoomDistStart), (float) (zoomDist / zoomDistStart));
                transform.translate(-zoomStart.getX(), -zoomStart.getY());
                transform.concatenate(zoomTransformStart);
                this.repaint();
            }
        }
    } else {
        if (!isPanEnabled()) {
            super.pointerDragged(x, y);
            return;
        }
        if (chart instanceof XYChart) {
            XYChart xyChart = (XYChart) chart;
            double[] panLimits = xyChart.getRenderer().getPanLimits();
            if (dragStartBBox == null) {
                dragStart = new Point(x[0], y[0]);
                dragStartBBox = getBBox();
            } else {
                float tx = x[0] - dragStart.getX();
                float ty = y[0] - dragStart.getY();
                BBox newBounds = dragStartBBox.translateScreenCoords(-tx, ty);
                double minX = newBounds.minX;
                double minY = newBounds.minY;
                double maxX = newBounds.maxX;
                double maxY = newBounds.maxY;
                if (panLimits != null) {
                    if (minX < panLimits[0]) {
                        maxX = maxX + panLimits[0] - minX;
                        minX = panLimits[0];
                    }
                    if (minY < panLimits[2]) {
                        maxY = maxY + panLimits[2] - minY;
                        minY = panLimits[2];
                    }
                    if (maxX > panLimits[1]) {
                        minX = minX + panLimits[1] - maxX;
                        maxX = panLimits[1];
                    }
                    if (maxY > panLimits[3]) {
                        minY = minY + panLimits[3] - maxY;
                        maxY = panLimits[3];
                    }
                }
                if (!xyChart.getRenderer().isPanXEnabled()) {
                    minX = xyChart.getRenderer().getXAxisMin();
                    maxX = xyChart.getRenderer().getXAxisMax();
                }
                if (!xyChart.getRenderer().isPanYEnabled()) {
                    minY = xyChart.getRenderer().getYAxisMin();
                    maxY = xyChart.getRenderer().getYAxisMax();
                }
                xyChart.getRenderer().setRange(new double[] { minX, maxX, minY, maxY });
                chartBoundsChanged();
            }
        } else {
            if (dragStart == null) {
                dragStart = new Point(x[0], y[0]);
                dragTransformStart = Transform.makeIdentity();
                if (transform != null) {
                    dragTransformStart.concatenate(transform);
                }
            } else {
                transform = Transform.makeIdentity();
                transform.translate(x[0] - dragStart.getX(), y[0] - dragStart.getY());
                transform.concatenate(dragTransformStart);
                this.repaint();
            }
        }
    }
    // To change body of generated methods, choose Tools | Templates.
    super.pointerDragged(x, y);
}
Also used : XYChart(com.codename1.charts.views.XYChart) Point(com.codename1.charts.models.Point) Point(com.codename1.charts.models.Point)

Example 17 with Transform

use of com.codename1.ui.Transform in project CodenameOne by codenameone.

the class Canvas method translate.

public void translate(float x, float y) {
    Transform t = g.getTransform();
    t.translate(x, y);
    g.setTransform(t);
}
Also used : Transform(com.codename1.ui.Transform)

Aggregations

Transform (com.codename1.ui.Transform)9 Point (com.codename1.charts.models.Point)3 GeneralPath (com.codename1.ui.geom.GeneralPath)2 Bitmap (android.graphics.Bitmap)1 Canvas (android.graphics.Canvas)1 Matrix (android.graphics.Matrix)1 Paint (android.graphics.Paint)1 Path (android.graphics.Path)1 RectF (android.graphics.RectF)1 Paint (com.codename1.charts.compat.Paint)1 XYSeries (com.codename1.charts.models.XYSeries)1 Orientation (com.codename1.charts.renderers.XYMultipleSeriesRenderer.Orientation)1 XYSeriesRenderer (com.codename1.charts.renderers.XYSeriesRenderer)1 XYChart (com.codename1.charts.views.XYChart)1 CodenameOneImplementation (com.codename1.impl.CodenameOneImplementation)1 Component (com.codename1.ui.Component)1 Container (com.codename1.ui.Container)1 Image (com.codename1.ui.Image)1 Label (com.codename1.ui.Label)1 Stroke (com.codename1.ui.Stroke)1