EditorColorBorder.java revision 64fc2ed005e2f1dc9ed0b34481c46a370c244b86
149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford/*
249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford * Copyright (C) 2013 The Android Open Source Project
349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford *
449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford * Licensed under the Apache License, Version 2.0 (the "License");
549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford * you may not use this file except in compliance with the License.
649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford * You may obtain a copy of the License at
749798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford *
849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford *      http://www.apache.org/licenses/LICENSE-2.0
949798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford *
1049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford * Unless required by applicable law or agreed to in writing, software
1149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford * distributed under the License is distributed on an "AS IS" BASIS,
1249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford * See the License for the specific language governing permissions and
1449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford * limitations under the License.
1549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford */
1649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
1749798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordpackage com.android.gallery3d.filtershow.editors;
1849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
1949798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport android.content.Context;
2049798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport android.graphics.Bitmap;
2149798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport android.graphics.BitmapFactory;
2249798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport android.graphics.Color;
23ae068f80895d7d075064a5ded59069425b72271fJohn Hofordimport android.util.Log;
2449798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport android.view.LayoutInflater;
2549798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport android.view.MenuItem;
2649798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport android.view.View;
2749798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport android.view.View.OnClickListener;
2849798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport android.view.ViewGroup;
2949798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport android.widget.Button;
3049798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport android.widget.FrameLayout;
3149798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport android.widget.LinearLayout;
3249798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport android.widget.PopupMenu;
3349798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport android.widget.SeekBar;
3449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
3549798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport com.android.gallery3d.R;
3664fc2ed005e2f1dc9ed0b34481c46a370c244b86John Hofordimport com.android.gallery3d.filtershow.FilterShowActivity;
3749798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport com.android.gallery3d.filtershow.controller.BitmapCaller;
3849798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport com.android.gallery3d.filtershow.controller.ColorChooser;
3949798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport com.android.gallery3d.filtershow.controller.FilterView;
4049798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport com.android.gallery3d.filtershow.filters.FilterColorBorderRepresentation;
4149798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport com.android.gallery3d.filtershow.filters.FilterRepresentation;
4249798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport com.android.gallery3d.filtershow.filters.ImageFilterColorBorder;
4349798939f1bc58eaf5842bbc8bc5424284ab7930John Hofordimport com.android.gallery3d.filtershow.imageshow.ImageShow;
4449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
45ae068f80895d7d075064a5ded59069425b72271fJohn Hofordpublic class EditorColorBorder extends ParametricEditor  {
4649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    private static final String LOGTAG = "EditorColorBorder";
4749798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    public static final int ID = R.id.editorColorBorder;
4849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
4949798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
5049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    int[] mBasColors = {
5149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            FilterColorBorderRepresentation.DEFAULT_MENU_COLOR1,
5249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            FilterColorBorderRepresentation.DEFAULT_MENU_COLOR2,
5349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            FilterColorBorderRepresentation.DEFAULT_MENU_COLOR3,
5449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            FilterColorBorderRepresentation.DEFAULT_MENU_COLOR4,
5549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            FilterColorBorderRepresentation.DEFAULT_MENU_COLOR5,
5649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    };
5749798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    private EditorColorBorderTabletUI mTabletUI;
5849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    private String mParameterString;
5949798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    private int mSelectedColorButton;
6049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
6149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    public EditorColorBorder() {
6249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        super(ID);
6349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    }
6449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
6549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    @Override
6649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    public String calculateUserMessage(Context context, String effectName, Object parameterValue) {
6749798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        FilterColorBorderRepresentation rep = getColorBorderRep();
6849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        if (rep == null) {
6949798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            return "";
7049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        }
7149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        if (mParameterString == null) {
7249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            mParameterString = "";
7349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        }
7449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        String paramString;
7549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        String val = rep.getValueString();
7649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        return mParameterString + val;
7749798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    }
7849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
7949798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    @Override
8049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    public void createEditor(Context context, FrameLayout frameLayout) {
8149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        mView = mImageShow = new ImageShow(context);
8249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        super.createEditor(context, frameLayout);
8349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    }
8449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
8549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    @Override
8649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    public void reflectCurrentFilter() {
8749798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        super.reflectCurrentFilter();
8849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        FilterRepresentation rep = getLocalRepresentation();
8949798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        if (rep != null && getLocalRepresentation() instanceof FilterColorBorderRepresentation) {
9049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            FilterColorBorderRepresentation cbRep =
9149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford                    (FilterColorBorderRepresentation) getLocalRepresentation();
9249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            if (!ParametricEditor.useCompact(mContext)) {
9349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford                if (mTabletUI != null) {
9449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford                    mTabletUI.setColorBorderRepresentation(cbRep);
9549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford                }
96ae068f80895d7d075064a5ded59069425b72271fJohn Hoford
97ae068f80895d7d075064a5ded59069425b72271fJohn Hoford            }
98ae068f80895d7d075064a5ded59069425b72271fJohn Hoford            cbRep.setPramMode(FilterColorBorderRepresentation.PARAM_SIZE);
99ae068f80895d7d075064a5ded59069425b72271fJohn Hoford            mParameterString = mContext.getString(R.string.color_border_size);
100ae068f80895d7d075064a5ded59069425b72271fJohn Hoford            if (mEditControl != null) {
101ae068f80895d7d075064a5ded59069425b72271fJohn Hoford                control(cbRep.getCurrentParam(), mEditControl);
10249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            }
10349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        }
10449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    }
10549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
10649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    @Override
10749798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    public void openUtilityPanel(final LinearLayout accessoryViewList) {
10849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        Button view = (Button) accessoryViewList.findViewById(R.id.applyEffect);
10949798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        view.setText(mContext.getString(R.string.color_border_size));
11049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        view.setOnClickListener(new OnClickListener() {
11149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
11249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            @Override
11349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            public void onClick(View arg0) {
11449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford                showPopupMenu(accessoryViewList);
11549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            }
11649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        });
11749798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    }
11849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
11949798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    @Override
12049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    public boolean showsSeekBar() {
12149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        return false;
12249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    }
12349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
12449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    private void showPopupMenu(LinearLayout accessoryViewList) {
12549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        final Button button = (Button) accessoryViewList.findViewById(
12649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford                R.id.applyEffect);
12749798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        if (button == null) {
12849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            return;
12949798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        }
13049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        final PopupMenu popupMenu = new PopupMenu(mImageShow.getActivity(), button);
13149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        popupMenu.getMenuInflater().inflate(R.menu.filtershow_menu_color_border,
13249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford                popupMenu.getMenu());
13349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
13449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
13549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            @Override
13649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            public boolean onMenuItemClick(MenuItem item) {
13749798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford                selectMenuItem(item);
13849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford                return true;
13949798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            }
14049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        });
14149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        popupMenu.show();
14264fc2ed005e2f1dc9ed0b34481c46a370c244b86John Hoford        ((FilterShowActivity)mContext).onShowMenu(popupMenu);
14349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    }
14449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
14549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    protected void selectMenuItem(MenuItem item) {
14649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        ImageFilterColorBorder filter = (ImageFilterColorBorder) mImageShow.getCurrentFilter();
14749798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        FilterColorBorderRepresentation rep = getColorBorderRep();
14849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        if (rep == null) {
14949798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            return;
15049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        }
15149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        switch (item.getItemId()) {
15249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            case R.id.color_border_menu_clear:
15349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford                clearFrame();
15449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford                break;
15549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            case R.id.color_border_menu_size:
15649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford                rep.setPramMode(FilterColorBorderRepresentation.PARAM_SIZE);
15749798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford                break;
15849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            case R.id.color_border_menu_corner_size:
15949798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford                rep.setPramMode(FilterColorBorderRepresentation.PARAM_RADIUS);
16049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford                break;
16149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            case R.id.color_border_menu_color:
16249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford                rep.setPramMode(FilterColorBorderRepresentation.PARAM_COLOR);
16349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford                break;
16449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        }
16549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        if (item.getItemId() != R.id.color_border_menu_clear) {
16649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            mParameterString = item.getTitle().toString();
16749798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        }
16849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        if (mControl instanceof ColorChooser) {
16949798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            ColorChooser c = (ColorChooser) mControl;
17049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            mBasColors = c.getColorSet();
17149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        }
17249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        control(rep.getCurrentParam(), mEditControl);
17349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        if (mControl instanceof ColorChooser) {
17449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            ColorChooser c = (ColorChooser) mControl;
17549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            c.setColorSet(mBasColors);
17649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        }
177ae068f80895d7d075064a5ded59069425b72271fJohn Hoford        updateText();
17849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        mControl.updateUI();
17949798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        mView.invalidate();
18049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    }
18149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
18249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    public void clearFrame() {
18349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        commitLocalRepresentation();
18449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    }
18549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
18649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    @Override
18749798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    public void setUtilityPanelUI(View actionButton, View editControl) {
18849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        if (ParametricEditor.useCompact(mContext)) {
18949798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            super.setUtilityPanelUI(actionButton, editControl);
19049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            return;
19149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        }
19249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        mSeekBar = (SeekBar) editControl.findViewById(R.id.primarySeekBar);
19349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        if (mSeekBar != null) {
19449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            mSeekBar.setVisibility(View.GONE);
19549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        }
19649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
19749798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        mTabletUI = new EditorColorBorderTabletUI(this, mContext, editControl);
19849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
19949798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    }
20049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
20149798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    FilterColorBorderRepresentation getColorBorderRep() {
20249798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        FilterRepresentation rep = getLocalRepresentation();
20349798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        if (rep instanceof FilterColorBorderRepresentation) {
20449798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford            return (FilterColorBorderRepresentation) rep;
20549798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        }
20649798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford        return null;
20749798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford    }
20849798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
20949798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford
21049798939f1bc58eaf5842bbc8bc5424284ab7930John Hoford}
211