Untitled

 avatar
unknown
plain_text
6 months ago
3.6 kB
5
Indexable
package ja.burhanrashid52.photoeditor

import android.content.Context
import android.media.Image
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import com.caps.photoeditor.R
import ja.burhanrashid52.photoeditor.MultiTouchListener.OnGestureControl
import java.util.UUID

/**
 * Created by Burhanuddin Rashid on 14/05/21.
 *
 * @author <https:></https:>//github.com/burhanrashid52>
 */
internal abstract class Graphic(
    val context: Context,
    val layoutId: Int,
    val viewType: ViewType,
    val graphicManager: GraphicManager?,
    val uniqueId: String) {
    val rootView: View

    open fun updateView(view: View) {
        //Optional for subclass to override
    }

    init {
        if (layoutId == 0) {
            throw UnsupportedOperationException("Layout id cannot be zero. Please define a layout")
        }
        rootView = LayoutInflater.from(context).inflate(layoutId, null)
        setupView(rootView)
        setupRemoveView(rootView)
        setupZoomInView(rootView)
        setupZoomOutView(rootView)
    }


    private fun setupRemoveView(rootView: View) {
        //We are setting tag as ViewType to identify what type of the view it is
        //when we remove the view from stack i.e onRemoveViewListener(ViewType viewType, int numberOfAddedViews);
        rootView.tag = Pair(uniqueId, viewType)
        val imgClose = rootView.findViewById<ImageView>(R.id.imgPhotoEditorClose)
        imgClose?.setOnClickListener { graphicManager?.removeView(this@Graphic) }
    }

    private fun setupZoomInView(rootView: View) {
        rootView.tag = Pair(uniqueId, viewType)
        val imgZoomIn = rootView.findViewById<ImageView>(R.id.imgPhotoEditorZoomIn)
        imgZoomIn?.setOnClickListener {
            graphicManager?.zoomInView(this@Graphic)}
    }

    private fun setupZoomOutView(rootView: View) {
        rootView.tag = Pair(uniqueId, viewType)
        val imgZoomOut = rootView.findViewById<ImageView>(R.id.imgPhotoEditorZoomOut)
        imgZoomOut?.setOnClickListener {
            graphicManager?.zoomOutView(this@Graphic)
        }
    }

    protected fun toggleSelection() {
        val frmBorder = rootView.findViewById<View>(R.id.frmBorder)
        val imgClose = rootView.findViewById<View>(R.id.imgPhotoEditorClose)
        val imgZoomIn = rootView.findViewById<View>(R.id.imgPhotoEditorZoomIn)
        val imgZoomOut = rootView.findViewById<View>(R.id.imgPhotoEditorZoomOut)
        if (frmBorder != null) {
            frmBorder.setBackgroundResource(R.drawable.rounded_border_tv)
            frmBorder.tag = true
        }
        if (imgClose != null) {
            imgClose.visibility = View.VISIBLE
        }
        if (imgZoomIn != null) {
            imgZoomIn.visibility = View.VISIBLE
        }
        if (imgZoomOut != null) {
            imgZoomOut.visibility = View.VISIBLE
        }
    }

    protected fun buildGestureController(
        photoEditorView: PhotoEditorView,
        viewState: PhotoEditorViewState
    ): OnGestureControl {
        val boxHelper = BoxHelper(photoEditorView, viewState)
        return object : OnGestureControl {
            override fun onClick() {
                boxHelper.clearHelperBox()
                toggleSelection()
                // Change the in-focus view
                viewState.currentSelectedView = rootView
            }

            override fun onLongClick() {
                updateView(rootView)
            }
        }
    }

    open fun setupView(rootView: View) {}
}
Editor is loading...
Leave a Comment