/*
 * Copyright (C) 2013 Chen Hui <calmer91@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package master.flame.danmaku.controller;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;

public class DrawHelper {

    public static Paint PAINT, PAINT_FPS;

    public static RectF RECT;
    
    private static boolean USE_DRAWCOLOR_TO_CLEAR_CANVAS = true;
    
    static {
        PAINT = new Paint();
        PAINT.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        PAINT.setColor(Color.TRANSPARENT);
        RECT = new RectF();
    }
    
    public static void useDrawColorToClearCanvas(boolean use) {
        USE_DRAWCOLOR_TO_CLEAR_CANVAS = use;
    }

    public static void drawFPS(Canvas canvas, String text) {
        if (PAINT_FPS == null) {
            PAINT_FPS = new Paint();
            PAINT_FPS.setColor(Color.RED);
            PAINT_FPS.setTextSize(30);
        }
        int top = canvas.getHeight() - 50;
        
        clearCanvas(canvas, 10, top - 50, (int) (PAINT_FPS.measureText(text) + 20), canvas.getHeight());
        canvas.drawText(text, 10, top, PAINT_FPS);
    }

    public static void clearCanvas(Canvas canvas) {
        if (USE_DRAWCOLOR_TO_CLEAR_CANVAS) {
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        } else {
            RECT.set(0, 0, canvas.getWidth(), canvas.getHeight());
            clearCanvas(canvas, RECT);
        }
    }
    
    public static void fillTransparent(Canvas canvas){
        canvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
    }

    public static void clearCanvas(Canvas canvas, float left, float top, float right, float bottom) {
        RECT.set(left, top, right, bottom);
        clearCanvas(canvas, RECT);
    }

    private static void clearCanvas(Canvas canvas, RectF rect) {
        if (rect.width() <= 0 || rect.height() <= 0) {
            return;
        }
        canvas.drawRect(rect, PAINT);
    }
}

@@ -1064,24 +1064,92 @@
 rue;%0D%0A    %0D%0A
+    private static boolean USE_DRAWCOLOR_MODE_CLEAR = true; %0D%0A    %0D%0A
     static %7B
@@ -1401,16 +1401,38 @@
 lean use
+, boolean useClearMode
 ) %7B%0D%0A   
@@ -1470,24 +1470,74 @@
 VAS = use;%0D%0A
+        USE_DRAWCOLOR_MODE_CLEAR = useClearMode;%0D%0A
     %7D%0D%0A%0D%0A   
@@ -2078,24 +2078,72 @@
 _CANVAS) %7B%0D%0A
+            if(USE_DRAWCOLOR_MODE_CLEAR) %7B%0D%0A    
             
@@ -2199,24 +2199,115 @@
 de.CLEAR);%0D%0A
+            %7D else %7B%0D%0A                canvas.drawColor(Color.TRANSPARENT);%0D%0A            %7D%0D%0A
         %7D el

add USE_DRAWCOLOR_MODE_CLEAR flag