aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/draw9patch/ui/StretchesViewer.java
blob: 353312c85a95aa0a22a8e16e90872efd4db3b539 (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * 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.draw9patch.ui;

import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.internal.editors.draw9patch.graphics.GraphicsUtilities;
import com.android.ide.eclipse.adt.internal.editors.draw9patch.graphics.NinePatchedImage;
import com.android.ide.eclipse.adt.internal.editors.draw9patch.graphics.NinePatchedImage.Projection;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;

/**
 * Preview 9-patched image pane.
 */
public class StretchesViewer extends Composite {
    private static final boolean DEBUG = false;

    private static final int PADDING_COLOR = 0x0000CC;

    private static final int PADDING_COLOR_ALPHA = 100;

    private static final PaletteData PADDING_PALLET
            = new PaletteData(new RGB[] {new RGB(0x00, 0x00, 0xCC)});

    private static final String CHECKER_PNG_PATH = "/icons/checker.png";

    private Image mBackgroundLayer = null;

    private final StretchView mHorizontal;
    private final StretchView mVertical;

    private final StretchView mBoth;

    private NinePatchedImage mNinePatchedImage = null;

    private ImageData mContentAreaImageData = null;

    private boolean mIsContentAreaShown = false;

    private Image mContentAreaImage = null;

    private int mScale = 2;

    public StretchesViewer(Composite parent, int style) {
        super(parent, style);

        mBackgroundLayer = AdtPlugin.getImageDescriptor(CHECKER_PNG_PATH).createImage();

        setLayout(new FillLayout(SWT.VERTICAL));

        mHorizontal = new StretchView(this, SWT.NULL);
        mVertical = new StretchView(this, SWT.NULL);
        mBoth = new StretchView(this, SWT.NULL);
    }

    /**
     * Set show/not show content area.
     * @param If show, true
     */
    public void setShowContentArea(boolean show) {
        mIsContentAreaShown = show;
        redraw();
    }

    private static final boolean equalSize(ImageData image1, ImageData image2) {
        return (image1.width == image2.width && image1.height == image2.height);
    }

    /**
     * Update preview image.
     */
    public void updatePreview(NinePatchedImage image) {
        mNinePatchedImage = image;
        ImageData base = mNinePatchedImage.getImageData();

        if (mContentAreaImageData == null
                || (mContentAreaImageData != null && !equalSize(base, mContentAreaImageData))) {
            mContentAreaImageData = new ImageData(
                    base.width,
                    base.height,
                    1,
                    PADDING_PALLET);
        } else {
            GraphicsUtilities.clearImageData(mContentAreaImageData);
        }

        mHorizontal.setImage(mNinePatchedImage);
        mVertical.setImage(mNinePatchedImage);
        mBoth.setImage(mNinePatchedImage);

        mContentAreaImage = buildContentAreaPreview();

        setScale(mScale);
    }

    private Image buildContentAreaPreview() {
        if (mContentAreaImage != null) {
            mContentAreaImage.dispose();
        }

        Rectangle rect = mNinePatchedImage.getContentArea();

        int yLen = rect.y + rect.height;
        for (int y = rect.y; y < yLen; y++) {
            int xLen = rect.x + rect.width;
            for (int x = rect.x; x < xLen; x++) {
                mContentAreaImageData.setPixel(x, y, PADDING_COLOR);
                mContentAreaImageData.setAlpha(x, y, PADDING_COLOR_ALPHA);
            }
        }
        return new Image(AdtPlugin.getDisplay(), mContentAreaImageData);
    }

    public void setScale(int scale) {
        if (DEBUG) {
            System.out.println("scale = " + scale);
        }

        mScale = scale;
        int imageWidth = mNinePatchedImage.getWidth();
        int imageHeight = mNinePatchedImage.getHeight();

        mHorizontal.setSize(imageWidth * scale, imageHeight);
        mVertical.setSize(imageWidth, imageHeight * scale);
        mBoth.setSize(imageWidth * scale, imageHeight * scale);

        redraw();
    }

    @Override
    public void dispose() {
        mBackgroundLayer.dispose();
        super.dispose();
    }

    private class StretchView extends Canvas implements PaintListener {

        private final Point mSize = new Point(0, 0);
        private final Rectangle mPadding = new Rectangle(0, 0, 0, 0);
        private Projection[][] mProjection = null;

        public StretchView(Composite parent, int style) {
            super(parent, style);
            addPaintListener(this);
        }

        private void setImage(NinePatchedImage image) {
            setSize(image.getWidth(), image.getHeight());
        }

        @Override
        public void setSize(int width, int heigh) {
            mSize.x = width;
            mSize.y = heigh;
            mProjection = mNinePatchedImage.getProjections(mSize.x, mSize.y, mProjection);
        }

        private synchronized void calcPaddings(int width, int height) {
            Point canvasSize = getSize();

            mPadding.x = (canvasSize.x - width) / 2;
            mPadding.y = (canvasSize.y - height) / 2;

            mPadding.width = width;
            mPadding.height = height;
        }

        @Override
        public void paintControl(PaintEvent pe) {
            if (mNinePatchedImage == null || mProjection == null) {
                return;
            }

            Point size = getSize();

            // relative scaling
            float ratio = 1.0f;
            float wRatio = ((float) size.x / mSize.x);
            ratio = Math.min(wRatio, ratio);
            float hRatio = ((float) size.y / mSize.y);
            ratio = Math.min(hRatio, ratio);

            int width = Math.round(mSize.x * ratio);
            int height = Math.round(mSize.y * ratio);

            calcPaddings(width, height);

            Rectangle dest = new Rectangle(0, 0, 0, 0);

            GC gc = pe.gc;

            int backgroundLayerWidth = mBackgroundLayer.getImageData().width;
            int backgroundLayerHeight = mBackgroundLayer.getImageData().height;

            int yCount = size.y / backgroundLayerHeight
                    + ((size.y % backgroundLayerHeight) > 0 ? 1 : 0);
            int xCount = size.x / backgroundLayerWidth
                    + ((size.x % backgroundLayerWidth) > 0 ? 1 : 0);

            // draw background layer
            for (int y = 0; y < yCount; y++) {
                for (int x = 0; x < xCount; x++) {
                    gc.drawImage(mBackgroundLayer,
                            x * backgroundLayerWidth,
                            y * backgroundLayerHeight);
                }
            }

            // draw the border line
            gc.setAlpha(0x88);
            gc.drawRectangle(0, 0, size.x, size.y);
            gc.setAlpha(0xFF);

            int yLen = mProjection.length;
            int xLen = mProjection[0].length;
            for (int yPos = 0; yPos < yLen; yPos++) {
                for (int xPos = 0; xPos < xLen; xPos++) {
                    Projection p = mProjection[yPos][xPos];

                    // consider the scale
                    dest.x = (int) Math.ceil(p.dest.x * ratio);
                    dest.y = (int) Math.ceil(p.dest.y * ratio);
                    dest.width = (int) Math.ceil(p.dest.width * ratio);
                    dest.height = (int) Math.ceil(p.dest.height * ratio);

                    gc.drawImage(mNinePatchedImage.getImage(), p.src.x, p.src.y,
                            p.src.width, p.src.height,
                            (mPadding.x + dest.x), (mPadding.y + dest.y),
                            dest.width, dest.height);

                    if (mIsContentAreaShown) {
                        gc.drawImage(mContentAreaImage, p.src.x, p.src.y,
                                p.src.width, p.src.height,
                                (mPadding.x + dest.x), (mPadding.y + dest.y),
                                dest.width, dest.height);
                    }
                }
            }
        }
    }
}