1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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
17package com.android.gallery3d.filtershow.imageshow;
18
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.graphics.Canvas;
22import android.graphics.Color;
23import android.graphics.Paint;
24import android.graphics.Path;
25import android.graphics.RectF;
26import android.util.AttributeSet;
27
28import com.android.gallery3d.R;
29
30public class ImageStraighten extends ImageGeometry {
31
32    private float mBaseAngle = 0;
33    private float mAngle = 0;
34
35    private static final String LOGTAG = "ImageStraighten";
36    private static final Paint gPaint = new Paint();
37    public ImageStraighten(Context context) {
38        super(context);
39    }
40
41    public ImageStraighten(Context context, AttributeSet attrs) {
42        super(context, attrs);
43    }
44
45    @Override
46    public String getName() {
47        return getContext().getString(R.string.straighten);
48    }
49
50    @Override
51    protected void setActionDown(float x, float y) {
52        super.setActionDown(x, y);
53        mBaseAngle = mAngle = getLocalStraighten();
54    }
55
56    private void setCropToStraighten(){
57        setLocalCropBounds(getUntranslatedStraightenCropBounds(getLocalPhotoBounds(),
58                getLocalStraighten()));
59    }
60
61    @Override
62    protected void setActionMove(float x, float y) {
63        super.setActionMove(x, y);
64        computeValue();
65        setLocalStraighten(mAngle);
66        setCropToStraighten();
67    }
68
69    private void computeValue() {
70        float angle = getCurrentTouchAngle();
71        mAngle = (mBaseAngle - angle) % 360;
72        mAngle = Math.max(MIN_STRAIGHTEN_ANGLE, mAngle);
73        mAngle = Math.min(MAX_STRAIGHTEN_ANGLE, mAngle);
74    }
75
76    @Override
77    protected void lostVisibility() {
78        saveAndSetPreset();
79    }
80
81    @Override
82    protected void gainedVisibility(){
83        setCropToStraighten();
84    }
85
86    @Override
87    protected void setActionUp() {
88        super.setActionUp();
89        setCropToStraighten();
90    }
91
92    @Override
93    public void onNewValue(int value) {
94        setLocalStraighten(GeometryMath.clamp(value, MIN_STRAIGHTEN_ANGLE, MAX_STRAIGHTEN_ANGLE));
95        if (getPanelController() != null) {
96            getPanelController().onNewValue((int) getLocalStraighten());
97        }
98        invalidate();
99    }
100
101    @Override
102    protected int getLocalValue() {
103        return (int) getLocalStraighten();
104    }
105
106    @Override
107    protected void drawShape(Canvas canvas, Bitmap image) {
108        drawTransformed(canvas, image, gPaint);
109
110        // Draw the grid
111        RectF bounds = straightenBounds();
112        Path path = new Path();
113        path.addRect(bounds, Path.Direction.CCW);
114        gPaint.setARGB(255, 255, 255, 255);
115        gPaint.setStrokeWidth(3);
116        gPaint.setStyle(Paint.Style.FILL_AND_STROKE);
117
118        RectF display = getLocalDisplayBounds();
119        float dWidth = display.width();
120        float dHeight = display.height();
121
122        if (mMode == MODES.MOVE) {
123            canvas.save();
124            canvas.clipPath(path);
125
126            int n = 16;
127            float step = dWidth / n;
128            float p = 0;
129            for (int i = 1; i < n; i++) {
130                p = i * step;
131                gPaint.setARGB(60, 255, 255, 255);
132                canvas.drawLine(p, 0, p, dHeight, gPaint);
133                canvas.drawLine(0, p, dWidth, p, gPaint);
134            }
135            canvas.restore();
136        }
137    }
138
139}
140