harjot-oberai / VectorMaster

Dynamic control over vector drawables!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how i can path name on vector clicked ?

khalidabuhayea opened this issue · comments

You can't get the name of the path that you clicked with the current implementation.

Hi,

Please find here a way to allow the library to retrieve selected path.
Could be very usefull 👍

`class ExtVectorMasterView(context: Context, attrs: AttributeSet? = null) : VectorMasterView(context, attrs) {

// unfortunately pathModels are not accessibles because of privacy
fun getAllPathModel() : ArrayList<PathModel> {
    return vectorModel.pathModels
}

fun isTouched(path: Path, x: Float, y: Float): Boolean {
    val region = Region()
    val rectF = RectF()
    path.computeBounds(rectF, true)
    region.setPath(
        path, Region(
            rectF.left.toInt(),
            rectF.top.toInt(), rectF.right.toInt(), rectF.bottom.toInt()
        )
    )
    val offset = 10
    return region.contains(x.toInt(), y.toInt()) || region.contains(
        x.toInt() + offset,
        y.toInt() + offset
    ) || region.contains(
        x.toInt() + offset, y.toInt() - offset
    ) || region.contains(x.toInt() - offset, y.toInt() - offset) || region.contains(
        x.toInt() - offset, y.toInt() + offset
    )

}

fun getTouchedPath(event: MotionEvent, scale:Float, panX:Float, panY:Float): NFPath? {
    val action = event.action
    if (action == 1) {
        val paths = getAllPathModel()
        paths.forEach { pathModel ->
            val path = NFPath(pathModel.name, pathModel, pathModel.path)
            path.name = pathModel.name
            val newX = event.x/scale-panX
            val newY = event.y/scale-panY
            if (isTouched(path, newX, newY)) {
                return path
            }
        }
    }
    return null
}

}`

Source : I've used a mix of VectorMaster & RichPath library code to compute this.
In my case, the view is created into a ZoomLayout, allowing vector to be zoomed and paned.
getTouchedPath is always returning the right view

It could be awesome to integrate those changes into this lib ;)