Exemple #1
0
void Shape::drawBackground(SkCanvas & canvas, float opacity)
{
    if (kNone == category_)
        return;

    SkPaint paint;
    paint.setAntiAlias(Core::AntiAliasEnable());
    paint.setColor(color_);
    paint.setShader(shader_);
    paint.setAlpha(ClampAlpha(opacity, ColorGetA(color_)));
    paint.setStrokeWidth(stroke_width_);
    if (effect_)
        paint.setPathEffect(effect_);

    if (kFill == style_)
        paint.setStyle(SkPaint::kFill_Style);
    else if (kStroke == style_)
        paint.setStyle(SkPaint::kStroke_Style);
        

    if(kRect == category_)
    {
        SkRect r;
        Rect re = Rect::MakeLTRB(param_.rect.left, 
                                 param_.rect.top, 
                                 param_.rect.right, 
                                 param_.rect.bottom);
        if (re.isEmpty())
        {
            if (kStroke == style_)
            {
                auto offset = stroke_width_ / 2;
                re = Rect::MakeLTRB(offset, offset, getWidth() - offset, getHeight() - offset);
            }               
            else
                re = Rect::MakeLTRB(0, 0, getWidth(), getHeight());

            r = Helper::ToSkRect(re);
        }
        else
        {
            if (kStroke == style_)
            {
                auto offset = stroke_width_ / 2;
                re = Rect::MakeLTRB(re.left() + offset, re.top() + offset, 
                                    re.right() - offset, re.bottom()- offset);
            }

            r = Helper::ToSkRect(re);

        }
            
        canvas.drawRoundRect(r, param_.rect.rx, param_.rect.ry, paint);
    }
    else if( kCircle == category_)
    {
        canvas.drawCircle(param_.circle.cx,
                          param_.circle.cy,
                          param_.circle.radius, paint);
    }
    else if (kLine == category_)
    {
        canvas.drawLine(param_.line.xs, param_.line.ys, 
                        param_.line.xe, param_.line.ye, 
                        paint);
    }
    else if (kPoint == category_)
    {
        canvas.drawPoint(param_.point.x, param_.point.y, paint);
    }
    else if (kOval == category_)
    {
        Rect re = Rect::MakeLTRB(param_.oval.left,
            param_.oval.top,
            param_.oval.right,
            param_.oval.bottom);
        if (re.isEmpty())
            re.setLTRB(0, 0, getWidth(), getHeight());

        canvas.drawOval(Helper::ToSkRect(re), paint);
    }
    else if (kArc == category_)
    {
        Rect re = Rect::MakeLTRB(param_.arc.left,
            param_.arc.top,
            param_.arc.right,
            param_.arc.bottom);
        if (re.isEmpty())
            re.setLTRB(0, 0, getWidth(), getHeight());

        canvas.drawArc(Helper::ToSkRect(re),
            param_.arc.start, param_.arc.sweep, param_.arc.center, paint);
    }
    else if (kPath == category_)
        canvas.drawPath(*path_, paint);
}