validator
varargs
verbosity
+viewport
vs
webtools
whilst
* Helper class to convert between control pixel coordinates and canvas coordinates.
* Takes care of the zooming and offset of the canvas.
*/
-public class CanvasTransform implements ICanvasTransform {
+public class CanvasTransform {
+ /**
+ * Default margin around the rendered image, reduced
+ * when the contents do not fit.
+ */
+ public static final int DEFAULT_MARGIN = 25;
+
/**
* The canvas which controls the zooming.
*/
/** Left-top offset in client pixel coordinates. */
private int mTranslate;
+ /** Current margin */
+ private int mMargin = DEFAULT_MARGIN;
+
/** Scaling factor, > 0. */
private double mScale;
// scaled image size
int sx = (int) (mImgSize * mScale);
+ // Adjust margin such that for zoomed out views
+ // we don't waste space (unless the viewport is
+ // large enough to accommodate it)
+ int delta = mClientSize - sx;
+ if (delta < 0) {
+ mMargin = 0;
+ } else if (delta < 2 * DEFAULT_MARGIN) {
+ mMargin = delta / 2;
+ } else {
+ mMargin = DEFAULT_MARGIN;
+ }
+
// actual client area is always reduced by the margins
- int cx = mClientSize - 2 * IMAGE_MARGIN;
+ int cx = mClientSize - 2 * mMargin;
if (sx < cx) {
mTranslate = 0;
}
}
+ public int getMargin() {
+ return mMargin;
+ }
+
public int translate(int canvasX) {
- return IMAGE_MARGIN - mTranslate + (int) (mScale * canvasX);
+ return mMargin - mTranslate + (int) (mScale * canvasX);
}
public int scale(int canwasW) {
}
public int inverseTranslate(int screenX) {
- return (int) ((screenX - IMAGE_MARGIN + mTranslate) / mScale);
+ return (int) ((screenX - mMargin + mTranslate) / mScale);
}
}
private int mFontHeight = 0;
/** The scaling of the canvas in X. */
- private final ICanvasTransform mHScale;
+ private final CanvasTransform mHScale;
/** The scaling of the canvas in Y. */
- private final ICanvasTransform mVScale;
+ private final CanvasTransform mVScale;
- public GCWrapper(ICanvasTransform hScale, ICanvasTransform vScale) {
+ public GCWrapper(CanvasTransform hScale, CanvasTransform vScale) {
mHScale = hScale;
mVScale = vScale;
mGc = null;
+++ /dev/null
-/*
- * Copyright (C) 2010 The Android Open Source Project
- *
- * Licensed under the Eclipse Public License, Version 1.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.eclipse.org/org/documents/epl-v10.php
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.ide.eclipse.adt.internal.editors.layout.gle2;
-
-/**
- * Interface for a class that can convert between client pixel's coordinates
- * and canvas coordinates. Each instance of such a transform deals with only
- * one axis, so clients need to use 2 instances for X and Y.
- */
-/* package */ interface ICanvasTransform {
- /**
- * Margin around the rendered image.
- * Should be enough space to display the layout width and height pseudo widgets.
- */
- public static final int IMAGE_MARGIN = 25;
-
- /**
- * Computes the transformation from a X/Y canvas image coordinate
- * to client pixel coordinate.
- * <p/>
- * This takes into account the {@link #IMAGE_MARGIN},
- * the current scaling and the current translation.
- *
- * @param canvasX A canvas image coordinate (X or Y).
- * @return The transformed coordinate in client pixel space.
- */
- public int translate(int canvasX);
-
- /**
- * Computes the transformation from a canvas image size (width or height) to
- * client pixel coordinates.
- *
- * @param canwasW A canvas image size (W or H).
- * @return The transformed coordinate in client pixel space.
- */
- public int scale(int canwasW);
-
- /**
- * Computes the transformation from a X/Y client pixel coordinate
- * to canvas image coordinate.
- * <p/>
- * This takes into account the {@link #IMAGE_MARGIN},
- * the current scaling and the current translation.
- * <p/>
- * This is the inverse of {@link #translate(int)}.
- *
- * @param screenX A client pixel space coordinate (X or Y).
- * @return The transformed coordinate in canvas image space.
- */
- public int inverseTranslate(int screenX);
-}
mHScale = new CanvasTransform(this, getHorizontalBar());
mVScale = new CanvasTransform(this, getVerticalBar());
- IFile file = layoutEditor.getInputFile();
+ // Unit test suite passes a null here; TODO: Replace with mocking
+ IFile file = layoutEditor != null ? layoutEditor.getInputFile() : null;
if (file != null) {
String zoom = AdtPlugin.getFileProperty(file, NAME_ZOOM);
if (zoom != null) {
/** Scales the canvas to best fit */
void setFitScale() {
- Rectangle canvasSize = getClientArea();
- int canvasWidth = canvasSize.width - 2 * ICanvasTransform.IMAGE_MARGIN;
- int canvasHeight = canvasSize.height - 2 * ICanvasTransform.IMAGE_MARGIN;
-
Image image = getImageOverlay().getImage();
if (image != null) {
+ Rectangle canvasSize = getClientArea();
+ int canvasWidth = canvasSize.width;
+ int canvasHeight = canvasSize.height;
+
ImageData imageData = image.getImageData();
int sceneWidth = imageData.width;
int sceneHeight = imageData.height;
- double hScale = canvasWidth / (double) sceneWidth;
- double vScale = canvasHeight / (double) sceneHeight;
+ if (sceneWidth == 0.0 || sceneHeight == 0.0) {
+ return;
+ }
+
+ // Reduce the margins if necessary
+ int hDelta = canvasWidth - sceneWidth;
+ int hMargin = 0;
+ if (hDelta > 2 * CanvasTransform.DEFAULT_MARGIN) {
+ hMargin = CanvasTransform.DEFAULT_MARGIN;
+ } else if (hDelta > 0) {
+ hMargin = hDelta / 2;
+ }
+
+ int vDelta = canvasHeight - sceneHeight;
+ int vMargin = 0;
+ if (vDelta > 2 * CanvasTransform.DEFAULT_MARGIN) {
+ vMargin = CanvasTransform.DEFAULT_MARGIN;
+ } else if (vDelta > 0) {
+ vMargin = vDelta / 2;
+ }
+
+ double hScale = canvasWidth / (double) (sceneWidth - hMargin);
+ double vScale = canvasHeight / (double) (sceneHeight - vMargin);
double scale = Math.min(hScale, vScale);
setScale(scale, true);
private ViewInfo createMergeInfo(RenderSession session) {
BufferedImage image = session.getImage();
ControlPoint imageSize = ControlPoint.create(mCanvas,
- ICanvasTransform.IMAGE_MARGIN + image.getWidth(),
- ICanvasTransform.IMAGE_MARGIN + image.getHeight());
+ mCanvas.getHorizontalTransform().getMargin() + image.getWidth(),
+ mCanvas.getVerticalTransform().getMargin() + image.getHeight());
LayoutPoint layoutSize = imageSize.toLayout();
UiDocumentNode model = mCanvas.getLayoutEditor().getUiRootNode();
List<UiElementNode> children = model.getUiChildren();