1d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford/*
2d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford * Copyright (C) 2013 The Android Open Source Project
3d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford *
4d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford * Licensed under the Apache License, Version 2.0 (the "License");
5d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford * you may not use this file except in compliance with the License.
6d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford * You may obtain a copy of the License at
7d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford *
8d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford *      http://www.apache.org/licenses/LICENSE-2.0
9d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford *
10d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford * Unless required by applicable law or agreed to in writing, software
11d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford * distributed under the License is distributed on an "AS IS" BASIS,
12d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford * See the License for the specific language governing permissions and
14d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford * limitations under the License.
15d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford */
16d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford
17d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hofordpackage com.android.gallery3d.filtershow.editors;
18d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford
19d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hofordimport android.content.Context;
20b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunkimport android.util.Log;
21b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunkimport android.util.SparseArray;
22b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunkimport android.view.MenuItem;
2391be98c35f6c5f19d7629bb29e7261970b2f4c67nicolasroardimport android.view.View;
24b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunkimport android.view.View.OnClickListener;
25b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunkimport android.widget.Button;
26d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hofordimport android.widget.FrameLayout;
27b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunkimport android.widget.LinearLayout;
28b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunkimport android.widget.PopupMenu;
29d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford
30d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hofordimport com.android.gallery3d.R;
3164fc2ed005e2f1dc9ed0b34481c46a370c244b86John Hofordimport com.android.gallery3d.filtershow.FilterShowActivity;
32b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunkimport com.android.gallery3d.filtershow.filters.FilterCropRepresentation;
33b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunkimport com.android.gallery3d.filtershow.filters.FilterRepresentation;
34d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hofordimport com.android.gallery3d.filtershow.imageshow.ImageCrop;
35b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunkimport com.android.gallery3d.filtershow.imageshow.MasterImage;
36d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford
37d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hofordpublic class EditorCrop extends Editor implements EditorInfo {
38b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    public static final String TAG = EditorCrop.class.getSimpleName();
39d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford    public static final int ID = R.id.editorCrop;
40d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford
41b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    // Holder for an aspect ratio it's string id
42b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    protected static final class AspectInfo {
43b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        int mAspectX;
44b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        int mAspectY;
45b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        int mStringId;
46b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        AspectInfo(int stringID, int x, int y) {
47b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk            mStringId = stringID;
48b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk            mAspectX = x;
49b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk            mAspectY = y;
50b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        }
51b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    };
52b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk
53b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    // Mapping from menu id to aspect ratio
54b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    protected static final SparseArray<AspectInfo> sAspects;
55b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    static {
56b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        sAspects = new SparseArray<AspectInfo>();
57b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        sAspects.put(R.id.crop_menu_1to1, new AspectInfo(R.string.aspect1to1_effect, 1, 1));
58b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        sAspects.put(R.id.crop_menu_4to3, new AspectInfo(R.string.aspect4to3_effect, 4, 3));
59b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        sAspects.put(R.id.crop_menu_3to4, new AspectInfo(R.string.aspect3to4_effect, 3, 4));
60b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        sAspects.put(R.id.crop_menu_5to7, new AspectInfo(R.string.aspect5to7_effect, 5, 7));
61b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        sAspects.put(R.id.crop_menu_7to5, new AspectInfo(R.string.aspect7to5_effect, 7, 5));
62b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        sAspects.put(R.id.crop_menu_none, new AspectInfo(R.string.aspectNone_effect, 0, 0));
63b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        sAspects.put(R.id.crop_menu_original, new AspectInfo(R.string.aspectOriginal_effect, 0, 0));
64b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    }
65b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk
66b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    protected ImageCrop mImageCrop;
67839d529f800adcd1ef5fcd262322bc939c2c2089nicolasroard    private String mAspectString = "";
68d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford
69d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford    public EditorCrop() {
70d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford        super(ID);
71b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        mChangesGeometry = true;
72d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford    }
73d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford
74d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford    @Override
75d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford    public void createEditor(Context context, FrameLayout frameLayout) {
76d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford        super.createEditor(context, frameLayout);
77839d529f800adcd1ef5fcd262322bc939c2c2089nicolasroard        if (mImageCrop == null) {
78839d529f800adcd1ef5fcd262322bc939c2c2089nicolasroard            mImageCrop = new ImageCrop(context);
79839d529f800adcd1ef5fcd262322bc939c2c2089nicolasroard        }
80839d529f800adcd1ef5fcd262322bc939c2c2089nicolasroard        mView = mImageShow = mImageCrop;
81d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford        mImageCrop.setEditor(this);
82b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    }
83b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk
84b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    @Override
85b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    public void reflectCurrentFilter() {
86b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        MasterImage master = MasterImage.getImage();
87b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        master.setCurrentFilterRepresentation(master.getPreset()
88b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk                .getFilterWithSerializationName(FilterCropRepresentation.SERIALIZATION_NAME));
89b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        super.reflectCurrentFilter();
90b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        FilterRepresentation rep = getLocalRepresentation();
91b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        if (rep == null || rep instanceof FilterCropRepresentation) {
92b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk            mImageCrop.setFilterCropRepresentation((FilterCropRepresentation) rep);
93839d529f800adcd1ef5fcd262322bc939c2c2089nicolasroard        } else {
94b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk            Log.w(TAG, "Could not reflect current filter, not of type: "
95b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk                    + FilterCropRepresentation.class.getSimpleName());
96839d529f800adcd1ef5fcd262322bc939c2c2089nicolasroard        }
97b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        mImageCrop.invalidate();
98b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    }
99b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk
100b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    @Override
101b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    public void finalApplyCalled() {
102b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        commitLocalRepresentation(mImageCrop.getFinalRepresentation());
103d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford    }
104d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford
105d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford    @Override
106b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk    public void openUtilityPanel(final LinearLayout accessoryViewList) {
107b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk        Button view = (Button) accessoryViewList.findViewById(R.id.applyEffect);
108b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk        view.setText(mContext.getString(R.string.crop));
109b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk        view.setOnClickListener(new OnClickListener() {
110b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk            @Override
111b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk            public void onClick(View arg0) {
112b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk                showPopupMenu(accessoryViewList);
113b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk            }
114b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk        });
115b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk    }
116b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk
117b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    private void changeCropAspect(int itemId) {
118b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        AspectInfo info = sAspects.get(itemId);
119b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        if (info == null) {
120b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk            throw new IllegalArgumentException("Invalid resource ID: " + itemId);
121b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk        }
122b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        if (itemId == R.id.crop_menu_original) {
123b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk            mImageCrop.applyOriginalAspect();
124b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        } else if (itemId == R.id.crop_menu_none) {
125b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk            mImageCrop.applyFreeAspect();
126b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        } else {
127b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk            mImageCrop.applyAspect(info.mAspectX, info.mAspectY);
128b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        }
129b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        setAspectString(mContext.getString(info.mStringId));
130b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    }
131b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk
132b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    private void showPopupMenu(LinearLayout accessoryViewList) {
133b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk        final Button button = (Button) accessoryViewList.findViewById(R.id.applyEffect);
134b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk        final PopupMenu popupMenu = new PopupMenu(mImageShow.getActivity(), button);
135b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk        popupMenu.getMenuInflater().inflate(R.menu.filtershow_menu_crop, popupMenu.getMenu());
136b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
137b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk            @Override
138b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk            public boolean onMenuItemClick(MenuItem item) {
139b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk                changeCropAspect(item.getItemId());
140b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk                return true;
141b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk            }
142b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk        });
143b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk        popupMenu.show();
14464fc2ed005e2f1dc9ed0b34481c46a370c244b86John Hoford        ((FilterShowActivity)mContext).onShowMenu(popupMenu);
145b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk    }
146b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk
147b08acdc5bf6b6fd937636434fa28849ef0a8c040Ruben Brunk    @Override
148b867739b4b748fd702b693843220e7691c2e5979John Hoford    public void setUtilityPanelUI(View actionButton, View editControl) {
149b867739b4b748fd702b693843220e7691c2e5979John Hoford        super.setUtilityPanelUI(actionButton,editControl);
150b867739b4b748fd702b693843220e7691c2e5979John Hoford        setMenuIcon(true);
151b867739b4b748fd702b693843220e7691c2e5979John Hoford    }
152b867739b4b748fd702b693843220e7691c2e5979John Hoford
153b867739b4b748fd702b693843220e7691c2e5979John Hoford    @Override
15491be98c35f6c5f19d7629bb29e7261970b2f4c67nicolasroard    public boolean showsSeekBar() {
15591be98c35f6c5f19d7629bb29e7261970b2f4c67nicolasroard        return false;
15691be98c35f6c5f19d7629bb29e7261970b2f4c67nicolasroard    }
15791be98c35f6c5f19d7629bb29e7261970b2f4c67nicolasroard
15891be98c35f6c5f19d7629bb29e7261970b2f4c67nicolasroard    @Override
159d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford    public int getTextId() {
160d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford        return R.string.crop;
161d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford    }
162d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford
163d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford    @Override
164d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford    public int getOverlayId() {
165d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford        return R.drawable.filtershow_button_geometry_crop;
166d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford    }
167d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford
168d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford    @Override
169d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford    public boolean getOverlayOnly() {
170d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford        return true;
171d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford    }
172be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hoford
173b0f7a8f7f7d95ae12e92f529fd9a8a37f75b105cRuben Brunk    private void setAspectString(String s) {
174be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hoford        mAspectString = s;
175be61c1fcfae87f00d2b3111b15e90bbf4a88b6bcJohn Hoford    }
176d61a2f9fcaad0309132b6b9b666c3dc6df62fed7John Hoford}
177