aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/layout/gle2/CanvasTransform.java
blob: ad5bd52e5b216b55d0d27462209a9cd594c09a91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 * Copyright (C) 2009 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;

import static com.android.ide.eclipse.adt.internal.editors.layout.gle2.ImageUtils.SHADOW_SIZE;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.ScrollBar;

/**
 * Helper class to convert between control pixel coordinates and canvas coordinates.
 * Takes care of the zooming and offset of the canvas.
 */
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.
     */
    private final LayoutCanvas mCanvas;

    /** Canvas image size (original, before zoom), in pixels. */
    private int mImgSize;

    /** Full size being scrolled (after zoom), in pixels */
    private int mFullSize;;

    /** Client size, in pixels. */
    private int mClientSize;

    /** Left-top offset in client pixel coordinates. */
    private int mTranslate;

    /** Current margin */
    private int mMargin = DEFAULT_MARGIN;

    /** Scaling factor, > 0. */
    private double mScale;

    /** Scrollbar widget. */
    private ScrollBar mScrollbar;

    public CanvasTransform(LayoutCanvas layoutCanvas, ScrollBar scrollbar) {
        mCanvas = layoutCanvas;
        mScrollbar = scrollbar;
        mScale = 1.0;
        mTranslate = 0;

        mScrollbar.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                // User requested scrolling. Changes translation and redraw canvas.
                mTranslate = mScrollbar.getSelection();
                CanvasTransform.this.mCanvas.redraw();
            }
        });
        mScrollbar.setIncrement(20);
    }

    /**
     * Sets the new scaling factor. Recomputes scrollbars.
     * @param scale Scaling factor, > 0.
     */
    public void setScale(double scale) {
        if (mScale != scale) {
            mScale = scale;
            resizeScrollbar();
        }
    }

    /** Recomputes the scrollbar and view port settings */
    public void refresh() {
        resizeScrollbar();
    }

    /**
     * Returns current scaling factor.
     *
     * @return The current scaling factor
     */
    public double getScale() {
        return mScale;
    }

    /**
     * Returns Canvas image size (original, before zoom), in pixels.
     *
     * @return Canvas image size (original, before zoom), in pixels
     */
    public int getImgSize() {
        return mImgSize;
    }

    /**
     * Returns the scaled image size in pixels.
     *
     * @return The scaled image size in pixels.
     */
    public int getScaledImgSize() {
        return (int) (mImgSize * mScale);
    }

    /**
     * Changes the size of the canvas image and the client size. Recomputes
     * scrollbars.
     *
     * @param imgSize the size of the image being scaled
     * @param fullSize the size of the full view area being scrolled
     * @param clientSize the size of the view port
     */
    public void setSize(int imgSize, int fullSize, int clientSize) {
        mImgSize = imgSize;
        mFullSize = fullSize;
        mClientSize = clientSize;
        mScrollbar.setPageIncrement(clientSize);
        resizeScrollbar();
    }

    private void resizeScrollbar() {
        // scaled image size
        int sx = (int) (mScale * mFullSize);

        // 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;

            ImageOverlay imageOverlay = mCanvas.getImageOverlay();
            if (imageOverlay != null && imageOverlay.getShowDropShadow()
                    && delta >= SHADOW_SIZE / 2) {
                mMargin -= SHADOW_SIZE / 2;
                // Add a little padding on the top too, if there's room. The shadow assets
                // include enough padding on the bottom to not make this look clipped.
                if (mMargin < 4) {
                    mMargin += 4;
                }
            }
        } else {
            mMargin = DEFAULT_MARGIN;
        }

        if (mCanvas.getPreviewManager().hasPreviews()) {
            // Make more room for the previews
            mMargin = 2;
        }

        // actual client area is always reduced by the margins
        int cx = mClientSize - 2 * mMargin;

        if (sx < cx) {
            mTranslate = 0;
            mScrollbar.setEnabled(false);
        } else {
            mScrollbar.setEnabled(true);

            int selection = mScrollbar.getSelection();
            int thumb = cx;
            int maximum = sx;

            if (selection + thumb > maximum) {
                selection = maximum - thumb;
                if (selection < 0) {
                    selection = 0;
                }
            }

            mScrollbar.setValues(selection, mScrollbar.getMinimum(), maximum, thumb, mScrollbar
                    .getIncrement(), mScrollbar.getPageIncrement());

            mTranslate = selection;
        }
    }

    public int getMargin() {
        return mMargin;
    }

    public int translate(int canvasX) {
        return mMargin - mTranslate + (int) (mScale * canvasX);
    }

    public int scale(int canwasW) {
        return (int) (mScale * canwasW);
    }

    public int inverseTranslate(int screenX) {
        return (int) ((screenX - mMargin + mTranslate) / mScale);
    }

    public int inverseScale(int canwasW) {
        return (int) (canwasW / mScale);
    }
}