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.gallery3d.photoeditor.actions;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.graphics.drawable.Drawable;
     22 import android.os.SystemClock;
     23 import android.view.LayoutInflater;
     24 import android.view.MotionEvent;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.widget.TextView;
     28 
     29 import com.android.gallery3d.R;
     30 import com.android.gallery3d.photoeditor.PhotoView;
     31 
     32 /**
     33  * Tool kit used by effect actions to retrieve tools, including managing tool creation/removal.
     34  */
     35 public class EffectToolKit {
     36 
     37     public enum ScaleType {
     38         LIGHT, SHADOW, COLOR, GENERIC
     39     }
     40 
     41     private final LayoutInflater inflater;
     42     private final PhotoView photoView;
     43     private final ViewGroup toolPanel;
     44     private final ViewGroup toolFullscreen;
     45 
     46     public EffectToolKit(View root, CharSequence label) {
     47         inflater = (LayoutInflater) root.getContext().getSystemService(
     48                 Context.LAYOUT_INFLATER_SERVICE);
     49 
     50         // Create effect tool panel as the first child of effects-bar.
     51         ViewGroup effectsBar = (ViewGroup) root.findViewById(R.id.effects_bar);
     52         toolPanel = (ViewGroup) inflater.inflate(
     53                 R.layout.photoeditor_effect_tool_panel, effectsBar, false);
     54         ((TextView) toolPanel.findViewById(R.id.effect_label)).setText(label);
     55         effectsBar.addView(toolPanel, 0);
     56 
     57         // Create effect tool full-screen on top of photo-view and place it within the same
     58         // view group that contains photo-view.
     59         photoView = (PhotoView) root.findViewById(R.id.photo_view);
     60         ViewGroup parent = (ViewGroup) photoView.getParent();
     61         toolFullscreen = (ViewGroup) inflater.inflate(
     62                 R.layout.photoeditor_effect_tool_fullscreen, parent, false);
     63         parent.addView(toolFullscreen, parent.indexOfChild(photoView) + 1);
     64     }
     65 
     66     public PhotoView getPhotoView() {
     67         return photoView;
     68     }
     69 
     70     /**
     71      * Cancel pending touch events and stop dispatching further touch events to tools.
     72      */
     73     public void cancel() {
     74         long now = SystemClock.uptimeMillis();
     75         MotionEvent cancelEvent = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, 0, 0, 0);
     76         toolFullscreen.dispatchTouchEvent(cancelEvent);
     77         toolPanel.dispatchTouchEvent(cancelEvent);
     78         cancelEvent.recycle();
     79         View.OnTouchListener listener = new View.OnTouchListener() {
     80 
     81             @Override
     82             public boolean onTouch(View v, MotionEvent event) {
     83                 // Consume all further touch events and don't dispatch them.
     84                 return true;
     85             }
     86         };
     87         toolFullscreen.setOnTouchListener(listener);
     88         toolPanel.setOnTouchListener(listener);
     89     }
     90 
     91     /**
     92      * Close to remove all created tools.
     93      */
     94     public void close() {
     95         ((ViewGroup) toolFullscreen.getParent()).removeView(toolFullscreen);
     96         ((ViewGroup) toolPanel.getParent()).removeView(toolPanel);
     97     }
     98 
     99     private View addFullscreenTool(int toolId) {
    100         FullscreenToolView tool = (FullscreenToolView) inflater.inflate(
    101                 toolId, toolFullscreen, false);
    102         tool.setPhotoBounds(getPhotoView().getPhotoBounds());
    103         toolFullscreen.addView(tool);
    104         return tool;
    105     }
    106 
    107     private View addPanelTool(int toolId) {
    108         // Add the tool right above the effect-label in the panel.
    109         View tool = inflater.inflate(toolId, toolPanel, false);
    110         toolPanel.addView(tool, toolPanel.indexOfChild(toolPanel.findViewById(R.id.effect_label)));
    111         return tool;
    112     }
    113 
    114     private Drawable getScalePickerProgressDrawable(Resources res, ScaleType type) {
    115         switch (type) {
    116             case LIGHT:
    117                 return res.getDrawable(R.drawable.photoeditor_scale_seekbar_light);
    118 
    119             case SHADOW:
    120                 return res.getDrawable(R.drawable.photoeditor_scale_seekbar_shadow);
    121 
    122             case COLOR:
    123                 return res.getDrawable(R.drawable.photoeditor_scale_seekbar_color);
    124         }
    125         return res.getDrawable(R.drawable.photoeditor_scale_seekbar_generic);
    126     }
    127 
    128     public ScaleSeekBar addScalePicker(ScaleType type) {
    129         ScaleSeekBar scalePicker = (ScaleSeekBar) addPanelTool(
    130                 R.layout.photoeditor_scale_seekbar);
    131         scalePicker.setProgressDrawable(getScalePickerProgressDrawable(
    132                 toolPanel.getResources(), type));
    133         return scalePicker;
    134     }
    135 
    136     public ColorSeekBar addColorPicker() {
    137         return (ColorSeekBar) addPanelTool(R.layout.photoeditor_color_seekbar);
    138     }
    139 
    140     public DoodleView addDoodleView() {
    141         return (DoodleView) addFullscreenTool(R.layout.photoeditor_doodle_view);
    142     }
    143 
    144     public TouchView addTouchView() {
    145         return (TouchView) addFullscreenTool(R.layout.photoeditor_touch_view);
    146     }
    147 
    148     public FlipView addFlipView() {
    149         return (FlipView) addFullscreenTool(R.layout.photoeditor_flip_view);
    150     }
    151 
    152     public RotateView addRotateView() {
    153         return (RotateView) addFullscreenTool(R.layout.photoeditor_rotate_view);
    154     }
    155 
    156     public CropView addCropView() {
    157         return (CropView) addFullscreenTool(R.layout.photoeditor_crop_view);
    158     }
    159 }
    160