<commit_before>/*
 * Copyright (c) 2014 <NAME> <<EMAIL>>
 *
 * This file is part of ITEC MediaPlayer.
 *
 * ITEC MediaPlayer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ITEC MediaPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ITEC MediaPlayer.  If not, see <http://www.gnu.org/licenses/>.
 */

package at.aau.itec.android.mediaplayer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.SurfaceTexture;
import android.opengl.*;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;

import at.aau.itec.android.mediaplayer.effects.Effect;
import at.aau.itec.android.mediaplayer.gles.*;

/**
 * Created by Mario on 14.06.2014.
 */
public class GLTextureView extends GLSurfaceView implements
        GLVideoRenderer.OnExternalSurfaceTextureCreatedListener,
        SurfaceTexture.OnFrameAvailableListener,
        Effect.Listener, GLVideoRenderer.OnEffectInitializedListener,
        GLVideoRenderer.OnFrameCapturedCallback {

    private static final String TAG = GLTextureView.class.getSimpleName();

    public interface OnEffectInitializedListener extends GLVideoRenderer.OnEffectInitializedListener {}
    public interface OnFrameCapturedCallback extends GLVideoRenderer.OnFrameCapturedCallback {}

    private GLVideoRenderer mRenderer;
    private Handler mRunOnUiThreadHandler = new Handler();
    private ScaleGestureDetector mScaleGestureDetector;
    private GestureDetector mGestureDetector;

    private OnEffectInitializedListener mOnEffectInitializedListener;
    private OnFrameCapturedCallback mOnFrameCapturedCallback;

    private float mTotalZoomLevel = 1.0f;
    private float mZoomSnappingRange = 0.02f;
    private float mTotalPanX;
    private float mTotalPanY;
    private float mPanSnappingRange = 0.02f;

    protected int mVideoWidth;
    protected int mVideoHeight;

    protected GLTextureView(Context context) {
        super(context);
        init(context);
    }

    protected GLTextureView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        if(isInEditMode()) {
            // do not start renderer in layout editor
            return;
        }
        if(!at.aau.itec.android.mediaplayer.gles.GLUtils.isGlEs2Supported(context)) {
            Log.e(TAG, "GLES 2.0 is not supported");
            return;
        }

        LibraryHelper.setContext(context);

        mRenderer = new GLVideoRenderer();
        mRenderer.setOnExternalSurfaceTextureCreatedListener(this);
        mRenderer.setOnEffectInitializedListener(this);

        setEGLContextClientVersion(2);
        setRenderer(mRenderer);
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

        mScaleGestureDetector = new ScaleGestureDetector(context,
                new ScaleGestureDetector.SimpleOnScaleGestureListener() {
                    @Override
                    public boolean onScale(ScaleGestureDetector detector) {
                        mTotalZoomLevel *= detector.getScaleFactor();

                        if(LibraryHelper.isBetween(mTotalZoomLevel, 1-mZoomSnappingRange, 1+mZoomSnappingRange)) {
                            mTotalZoomLevel = 1.0f;
                        }

                        // limit zooming to magnification zooms (zoom-ins)
                        if(mTotalZoomLevel < 1.0f) {
                            mTotalZoomLevel = 1.0f;
                        }

                        setZoom(mTotalZoomLevel);
                        return true;
                    }
                });

        mGestureDetector = new GestureDetector(context,
                new GestureDetector.SimpleOnGestureListener() {
                    @Override
                    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                        // divide by zoom level to adjust panning speed to zoomed picture size
                        // multiply by fixed scaling factor to compensate for panning lag
                        mTotalPanX += distanceX / getWidth() / mTotalZoomLevel * 1.2f;
                        mTotalPanY += distanceY / getHeight() / mTotalZoomLevel * 1.2f;

                        float panSnappingRange = mPanSnappingRange / mTotalZoomLevel;
                        if(LibraryHelper.isBetween(mTotalPanX, -panSnappingRange, +panSnappingRange)) {
                            mTotalPanX = 0;
                        }
                        if(LibraryHelper.isBetween(mTotalPanY, -panSnappingRange, +panSnappingRange)) {
                            mTotalPanY = 0;
                        }

                        // limit panning to the texture bounds so it always covers the complete view
                        float maxPanX = Math.abs((1.0f / mTotalZoomLevel) - 1.0f);
                        float maxPanY = Math.abs((1.0f / mTotalZoomLevel) - 1.0f);
                        mTotalPanX = LibraryHelper.clamp(mTotalPanX, -maxPanX, maxPanX);
                        mTotalPanY = LibraryHelper.clamp(mTotalPanY, -maxPanY, maxPanY);

                        setPan(mTotalPanX, mTotalPanY);
                        return true;
                    }

                    @Override
                    public boolean onDoubleTap(MotionEvent e) {
                        mTotalZoomLevel = 1;
                        mTotalPanX = 0;
                        mTotalPanY = 0;

                        setZoom(mTotalZoomLevel);
                        setPan(mTotalPanX, mTotalPanY);

                        return true;
                    }
                });
    }

    /**
     * Sets the zoom factor of the texture in the view. 1.0 means no zoom, 2.0 2x zoom, etc.
     */
    public void setZoom(float zoomFactor) {
        mTotalZoomLevel = zoomFactor;
        mRenderer.setZoomLevel(mTotalZoomLevel);
        requestRender(GLVideoRenderer.RenderRequest.GEOMETRY);
    }

    public float getZoomLevel() {
        return mTotalZoomLevel;
    }

    /**
     * Sets the panning of the texture in the view. (0.0, 0.0) centers the texture and means no
     * panning, (-1.0, -1.0) moves the texture to the lower right quarter.
     * @param x
     * @param y
     */
    public void setPan(float x, float y) {
        mRenderer.setPan(-x, y);
        requestRender(GLVideoRenderer.RenderRequest.GEOMETRY);
    }

    public float getPanX() {
        return mTotalPanX;
    }<commit_msg>Store panning state<commit_after>/*
 * Copyright (c) 2014 <NAME> <<EMAIL>>
 *
 * This file is part of ITEC MediaPlayer.
 *
 * ITEC MediaPlayer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ITEC MediaPlayer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ITEC MediaPlayer.  If not, see <http://www.gnu.org/licenses/>.
 */

package at.aau.itec.android.mediaplayer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.SurfaceTexture;
import android.opengl.*;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;

import at.aau.itec.android.mediaplayer.effects.Effect;
import at.aau.itec.android.mediaplayer.gles.*;

/**
 * Created by Mario on 14.06.2014.
 */
public class GLTextureView extends GLSurfaceView implements
        GLVideoRenderer.OnExternalSurfaceTextureCreatedListener,
        SurfaceTexture.OnFrameAvailableListener,
        Effect.Listener, GLVideoRenderer.OnEffectInitializedListener,
        GLVideoRenderer.OnFrameCapturedCallback {

    private static final String TAG = GLTextureView.class.getSimpleName();

    public interface OnEffectInitializedListener extends GLVideoRenderer.OnEffectInitializedListener {}
    public interface OnFrameCapturedCallback extends GLVideoRenderer.OnFrameCapturedCallback {}

    private GLVideoRenderer mRenderer;
    private Handler mRunOnUiThreadHandler = new Handler();
    private ScaleGestureDetector mScaleGestureDetector;
    private GestureDetector mGestureDetector;

    private OnEffectInitializedListener mOnEffectInitializedListener;
    private OnFrameCapturedCallback mOnFrameCapturedCallback;

    private float mTotalZoomLevel = 1.0f;
    private float mZoomSnappingRange = 0.02f;
    private float mTotalPanX;
    private float mTotalPanY;
    private float mPanSnappingRange = 0.02f;

    protected int mVideoWidth;
    protected int mVideoHeight;

    protected GLTextureView(Context context) {
        super(context);
        init(context);
    }

    protected GLTextureView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        if(isInEditMode()) {
            // do not start renderer in layout editor
            return;
        }
        if(!at.aau.itec.android.mediaplayer.gles.GLUtils.isGlEs2Supported(context)) {
            Log.e(TAG, "GLES 2.0 is not supported");
            return;
        }

        LibraryHelper.setContext(context);

        mRenderer = new GLVideoRenderer();
        mRenderer.setOnExternalSurfaceTextureCreatedListener(this);
        mRenderer.setOnEffectInitializedListener(this);

        setEGLContextClientVersion(2);
        setRenderer(mRenderer);
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

        mScaleGestureDetector = new ScaleGestureDetector(context,
                new ScaleGestureDetector.SimpleOnScaleGestureListener() {
                    @Override
                    public boolean onScale(ScaleGestureDetector detector) {
                        mTotalZoomLevel *= detector.getScaleFactor();

                        if(LibraryHelper.isBetween(mTotalZoomLevel, 1-mZoomSnappingRange, 1+mZoomSnappingRange)) {
                            mTotalZoomLevel = 1.0f;
                        }

                        // limit zooming to magnification zooms (zoom-ins)
                        if(mTotalZoomLevel < 1.0f) {
                            mTotalZoomLevel = 1.0f;
                        }

                        setZoom(mTotalZoomLevel);
                        return true;
                    }
                });

        mGestureDetector = new GestureDetector(context,
                new GestureDetector.SimpleOnGestureListener() {
                    @Override
                    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                        // divide by zoom level to adjust panning speed to zoomed picture size
                        // multiply by fixed scaling factor to compensate for panning lag
                        mTotalPanX += distanceX / getWidth() / mTotalZoomLevel * 1.2f;
                        mTotalPanY += distanceY / getHeight() / mTotalZoomLevel * 1.2f;

                        float panSnappingRange = mPanSnappingRange / mTotalZoomLevel;
                        if(LibraryHelper.isBetween(mTotalPanX, -panSnappingRange, +panSnappingRange)) {
                            mTotalPanX = 0;
                        }
                        if(LibraryHelper.isBetween(mTotalPanY, -panSnappingRange, +panSnappingRange)) {
                            mTotalPanY = 0;
                        }

                        // limit panning to the texture bounds so it always covers the complete view
                        float maxPanX = Math.abs((1.0f / mTotalZoomLevel) - 1.0f);
                        float maxPanY = Math.abs((1.0f / mTotalZoomLevel) - 1.0f);
                        mTotalPanX = LibraryHelper.clamp(mTotalPanX, -maxPanX, maxPanX);
                        mTotalPanY = LibraryHelper.clamp(mTotalPanY, -maxPanY, maxPanY);

                        setPan(mTotalPanX, mTotalPanY);
                        return true;
                    }

                    @Override
                    public boolean onDoubleTap(MotionEvent e) {
                        mTotalZoomLevel = 1;
                        mTotalPanX = 0;
                        mTotalPanY = 0;

                        setZoom(mTotalZoomLevel);
                        setPan(mTotalPanX, mTotalPanY);

                        return true;
                    }
                });
    }

    /**
     * Sets the zoom factor of the texture in the view. 1.0 means no zoom, 2.0 2x zoom, etc.
     */
    public void setZoom(float zoomFactor) {
        mTotalZoomLevel = zoomFactor;
        mRenderer.setZoomLevel(mTotalZoomLevel);
        requestRender(GLVideoRenderer.RenderRequest.GEOMETRY);
    }

    public float getZoomLevel() {
        return mTotalZoomLevel;
    }

    /**
     * Sets the panning of the texture in the view. (0.0, 0.0) centers the texture and means no
     * panning, (-1.0, -1.0) moves the texture to the lower right quarter.
     * @param x
     * @param y
     */
    public void setPan(float x, float y) {
        mTotalPanX = x;
        mTotalPanY = y;
        mRenderer.setPan(-mTotalPanX, mTotalPanY);
        requestRender(GLVideoRenderer.RenderRequest.GEOMETRY);
    }

    public float getPanX() {
        return mTotalPanX;
    }
{'fraction_non_alphanumeric': 0.07200338839474799, 'fraction_numerical': 0.00917690244246788, 'mean_word_length': 2.0684427117175654, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7324310', 'n_tokens_mistral': 3872, 'n_tokens_neox': 3600, 'n_words': 960}