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);
}
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);
}
Aggregations