package com.ys.bdtp.graph.items

import android.graphics.*
import com.ys.bdtp.graph.SGraphyItem
import com.ys.bdtp.graph.data.SLineF

/**
 * SGraphy图形引擎线段Item
 *
 * @author  庞利祥(sybotan@126.com)
 */
open class SGraphyLineItem(line: SLineF = SLineF(), parent: SGraphyItem? = null) : SGraphyItem(parent) {

    /** 保存线段的坐标 */
    var line = line
        set(value) {
            field = value
            invalidate()
        }

    /** 画笔 */
    private var pen = Paint()

    /**
     * 默认构造函数
     */
    init {
        pen.color = Color.RED
        pen.strokeWidth = 5f
    } // init

    /**
     * 构造函数
     *
     * @param   x1          端点1的x坐标
     * @param   y1          端点1的y坐标
     * @param   x2          端点2的x坐标
     * @param   y2          端点2的y坐标
     * @param   parent      指向父节点
     */
    constructor(x1: Float, y1: Float, x2: Float, y2: Float, parent: SGraphyItem? = null) : this(SLineF(x1, y1, x2, y2), parent)

    /**
     * Item对象边界区域
     *
     * @return  边界区域
     */
    override fun boundingRect(): RectF {
        return RectF(minOf(line.x1, line.x2) -5, minOf(line.y1, line.y2) - 5,
                maxOf(line.x1, line.x2) + 5, maxOf(line.y1, line.y2) + 5)
    } // Function boundingRect()

    /**
     * Item绘制操作
     *
     * @param   canvas      画布对象
     * @param   rect        更新区域
     */
    override fun onDraw(canvas: Canvas) {
        canvas.drawLine(line.x1, line.y1, line.x2, line.y2, pen)
        return
    } // Function paint()
} // Class SGraphyLineItem