Home | History | Annotate | Download | only in actions
      1 /*
      2  * Copyright (C) 2010 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 
     17 package com.android.photoeditor.actions;
     18 
     19 import android.graphics.Matrix;
     20 import android.graphics.RectF;
     21 import android.view.View;
     22 import android.view.ViewGroup;
     23 
     24 import com.android.photoeditor.FilterStack;
     25 import com.android.photoeditor.R;
     26 import com.android.photoeditor.RectUtils;
     27 import com.android.photoeditor.filters.StraightenFilter;
     28 
     29 /**
     30  * An action handling straighten effect.
     31  */
     32 public class StraightenAction extends FilterAction {
     33 
     34     private static final float DEFAULT_ANGLE = 0.0f;
     35     private static final float DEFAULT_ROTATE_SPAN = 60.0f;
     36 
     37     private StraightenFilter filter;
     38 
     39     public StraightenAction(FilterStack filterStack, ViewGroup tools) {
     40         super(filterStack, tools, R.string.straighten_tooltip);
     41     }
     42 
     43     @Override
     44     public void onBegin() {
     45         filter = new StraightenFilter();
     46 
     47         final Matrix matrix = new Matrix();
     48         final RectF photoBounds = photoView.getPhotoBounds();
     49         final float displayScale = RectUtils.getDisplayScale(photoBounds, photoView);
     50 
     51         RectF displayBounds = photoView.getPhotoDisplayBounds();
     52         photoView.clipPhoto(displayBounds);
     53 
     54         // Directly transform photo-view instead of waiting for top-filter output callback.
     55         rotateView.setOnAngleChangeListener(new RotateView.OnRotateChangeListener() {
     56 
     57             @Override
     58             public void onAngleChanged(float angle, boolean fromUser){
     59                 if (fromUser) {
     60                     filter.setAngle(angle);
     61                     notifyFilterChanged(filter, false);
     62                     RectUtils.getStraightenMatrix(photoBounds, angle, matrix);
     63                     matrix.postScale(displayScale, displayScale);
     64                     photoView.transformDisplay(matrix);
     65                 }
     66             }
     67 
     68             @Override
     69             public void onStartTrackingTouch() {
     70                 // no-op
     71             }
     72 
     73             @Override
     74             public void onStopTrackingTouch() {
     75                 // no-op
     76             }
     77         });
     78         rotateView.setGridBounds(displayBounds);
     79         rotateView.setRotatedAngle(DEFAULT_ANGLE);
     80         rotateView.setRotateSpan(DEFAULT_ROTATE_SPAN);
     81         rotateView.setVisibility(View.VISIBLE);
     82     }
     83 
     84     @Override
     85     public void onEnd() {
     86         notifyFilterChanged(filter, true);
     87     }
     88 }
     89