/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/CanvasTransform.java

https://github.com/CyanogenMod/android_sdk · Java · 215 lines · 111 code · 34 blank · 70 comment · 16 complexity · 5154d32914165f9691852a7fd68ff794 MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Eclipse Public License, Version 1.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.eclipse.org/org/documents/epl-v10.php
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.android.ide.eclipse.adt.internal.editors.layout.gle2;
  17. import static com.android.ide.eclipse.adt.internal.editors.layout.gle2.ImageUtils.SHADOW_SIZE;
  18. import org.eclipse.swt.events.SelectionAdapter;
  19. import org.eclipse.swt.events.SelectionEvent;
  20. import org.eclipse.swt.widgets.ScrollBar;
  21. /**
  22. * Helper class to convert between control pixel coordinates and canvas coordinates.
  23. * Takes care of the zooming and offset of the canvas.
  24. */
  25. public class CanvasTransform {
  26. /**
  27. * Default margin around the rendered image, reduced
  28. * when the contents do not fit.
  29. */
  30. public static final int DEFAULT_MARGIN = 25;
  31. /**
  32. * The canvas which controls the zooming.
  33. */
  34. private final LayoutCanvas mCanvas;
  35. /** Canvas image size (original, before zoom), in pixels. */
  36. private int mImgSize;
  37. /** Full size being scrolled (after zoom), in pixels */
  38. private int mFullSize;;
  39. /** Client size, in pixels. */
  40. private int mClientSize;
  41. /** Left-top offset in client pixel coordinates. */
  42. private int mTranslate;
  43. /** Current margin */
  44. private int mMargin = DEFAULT_MARGIN;
  45. /** Scaling factor, > 0. */
  46. private double mScale;
  47. /** Scrollbar widget. */
  48. private ScrollBar mScrollbar;
  49. public CanvasTransform(LayoutCanvas layoutCanvas, ScrollBar scrollbar) {
  50. mCanvas = layoutCanvas;
  51. mScrollbar = scrollbar;
  52. mScale = 1.0;
  53. mTranslate = 0;
  54. mScrollbar.addSelectionListener(new SelectionAdapter() {
  55. @Override
  56. public void widgetSelected(SelectionEvent e) {
  57. // User requested scrolling. Changes translation and redraw canvas.
  58. mTranslate = mScrollbar.getSelection();
  59. CanvasTransform.this.mCanvas.redraw();
  60. }
  61. });
  62. mScrollbar.setIncrement(20);
  63. }
  64. /**
  65. * Sets the new scaling factor. Recomputes scrollbars.
  66. * @param scale Scaling factor, > 0.
  67. */
  68. public void setScale(double scale) {
  69. if (mScale != scale) {
  70. mScale = scale;
  71. resizeScrollbar();
  72. }
  73. }
  74. /** Recomputes the scrollbar and view port settings */
  75. public void refresh() {
  76. resizeScrollbar();
  77. }
  78. /**
  79. * Returns current scaling factor.
  80. *
  81. * @return The current scaling factor
  82. */
  83. public double getScale() {
  84. return mScale;
  85. }
  86. /**
  87. * Returns Canvas image size (original, before zoom), in pixels.
  88. *
  89. * @return Canvas image size (original, before zoom), in pixels
  90. */
  91. public int getImgSize() {
  92. return mImgSize;
  93. }
  94. /**
  95. * Returns the scaled image size in pixels.
  96. *
  97. * @return The scaled image size in pixels.
  98. */
  99. public int getScaledImgSize() {
  100. return (int) (mImgSize * mScale);
  101. }
  102. /**
  103. * Changes the size of the canvas image and the client size. Recomputes
  104. * scrollbars.
  105. *
  106. * @param imgSize the size of the image being scaled
  107. * @param fullSize the size of the full view area being scrolled
  108. * @param clientSize the size of the view port
  109. */
  110. public void setSize(int imgSize, int fullSize, int clientSize) {
  111. mImgSize = imgSize;
  112. mFullSize = fullSize;
  113. mClientSize = clientSize;
  114. mScrollbar.setPageIncrement(clientSize);
  115. resizeScrollbar();
  116. }
  117. private void resizeScrollbar() {
  118. // scaled image size
  119. int sx = (int) (mScale * mFullSize);
  120. // Adjust margin such that for zoomed out views
  121. // we don't waste space (unless the viewport is
  122. // large enough to accommodate it)
  123. int delta = mClientSize - sx;
  124. if (delta < 0) {
  125. mMargin = 0;
  126. } else if (delta < 2 * DEFAULT_MARGIN) {
  127. mMargin = delta / 2;
  128. ImageOverlay imageOverlay = mCanvas.getImageOverlay();
  129. if (imageOverlay != null && imageOverlay.getShowDropShadow()
  130. && delta >= SHADOW_SIZE / 2) {
  131. mMargin -= SHADOW_SIZE / 2;
  132. // Add a little padding on the top too, if there's room. The shadow assets
  133. // include enough padding on the bottom to not make this look clipped.
  134. if (mMargin < 4) {
  135. mMargin += 4;
  136. }
  137. }
  138. } else {
  139. mMargin = DEFAULT_MARGIN;
  140. }
  141. if (mCanvas.getPreviewManager().hasPreviews()) {
  142. // Make more room for the previews
  143. mMargin = 2;
  144. }
  145. // actual client area is always reduced by the margins
  146. int cx = mClientSize - 2 * mMargin;
  147. if (sx < cx) {
  148. mTranslate = 0;
  149. mScrollbar.setEnabled(false);
  150. } else {
  151. mScrollbar.setEnabled(true);
  152. int selection = mScrollbar.getSelection();
  153. int thumb = cx;
  154. int maximum = sx;
  155. if (selection + thumb > maximum) {
  156. selection = maximum - thumb;
  157. if (selection < 0) {
  158. selection = 0;
  159. }
  160. }
  161. mScrollbar.setValues(selection, mScrollbar.getMinimum(), maximum, thumb, mScrollbar
  162. .getIncrement(), mScrollbar.getPageIncrement());
  163. mTranslate = selection;
  164. }
  165. }
  166. public int getMargin() {
  167. return mMargin;
  168. }
  169. public int translate(int canvasX) {
  170. return mMargin - mTranslate + (int) (mScale * canvasX);
  171. }
  172. public int scale(int canwasW) {
  173. return (int) (mScale * canwasW);
  174. }
  175. public int inverseTranslate(int screenX) {
  176. return (int) ((screenX - mMargin + mTranslate) / mScale);
  177. }
  178. public int inverseScale(int canwasW) {
  179. return (int) (canwasW / mScale);
  180. }
  181. }