package com.iakob.bramblecriste;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.Button;

public class ResetButton extends Button{
    private Paint paint = new Paint();
    private int gap;
    private int canvasWidth;
    private int canvasHeight;
    private Rect block;
    private int resetTrigger = 5;
    private int resetCount;
    private int resetColor;
    
    public ResetButton(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        paint.setColor(Color.RED);
        paint.setStyle(Style.FILL_AND_STROKE);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    	super.onSizeChanged(w, h, oldw, oldh);
    	canvasWidth = w;
    	canvasHeight = h;
    	calculateBlock();
    }

	private void calculateBlock() {
		gap = canvasHeight / 100;
		block = new Rect(
				gap, 
				gap, 
				canvasWidth - 2*gap, 
				(canvasHeight - (2*resetTrigger+1)*gap)/resetTrigger/2);
	}

    @Override
    public void onDraw(Canvas canvas) {
    	int start = resetTrigger - resetCount;
    	for (int i = start; i < start+resetCount*2; i++) {
    		block.offsetTo(gap, (canvasHeight-gap)*i/resetTrigger/2 + gap);
    		canvas.drawRect(block, paint);
		}
    }

	public int getResetTrigger() {
		return resetTrigger;
	}

	public void setResetTrigger(int resetTrigger) {
		this.resetTrigger = resetTrigger;
		calculateBlock();
	}

	public int getResetCount() {
		return resetCount;
	}

	public boolean increment() {
		if (resetCount < resetTrigger)
		{
			resetCount++;
			invalidate();
		}
		return resetCount >= resetTrigger;
	}
	
	public void clear() {
		invalidate();
		resetCount = 0;
	}

	public int getResetColor() {
		return resetColor;
	}

	public void setResetColor(int resetColor) {
		this.resetColor = resetColor;
	}
}

@@ -704,11 +704,42 @@
 lor.
-RED
+WHITE);%0A        paint.setAlpha(128
 );%0A 
@@ -774,19 +774,8 @@
 FILL
-_AND_STROKE
 );%0A 
@@ -1042,10 +1042,9 @@
 t / 
-10
+6
 0;%0A%09
@@ -1070,23 +1070,13 @@
 %09%09%09%09
-gap, %0A%09%09%09%09gap, 
+0, 0,
 %0A%09%09%09
@@ -1341,16 +1341,18 @@
 etTo(gap
+-1
 , (canva
@@ -1386,16 +1386,21 @@
 /2 + gap
+ + 1 
 );%0A    %09

make reset progress 50% white and increase gap, since rounding errors make narrow gaps look uneven