package com.gmail.rallen.gridstrument;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.PathShape;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class GridView extends View {
    private ShapeDrawable mGridDrawable;
    private Path mIntersectPath, mIntersectPath1;
    private Paint mIntersectPaint, mIntersectPaint1;
    private float touchX, touchY, curX, curY;

    public GridView(Context context) {
        super(context);
        //init(null, 0);
    }

    public GridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //init(attrs, 0);
    }

    public GridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        //init(attrs, defStyle);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        // in the constructor we don't have the width and height of the view, yet
        init();
    }

    private void init() {
        Log.d("init", String.format("width: %d; height: %d", this.getWidth(), this.getHeight()));
        Path gridPath = new Path();
        int N = 4;
        float dN = 200f;
        for(int i = 0; i <= N; i++) {
            gridPath.moveTo(i * dN, 0);
            gridPath.lineTo(i * dN, N*dN);
            gridPath.moveTo(0, i * dN);
            gridPath.lineTo(N*dN, i * dN);
        }
        gridPath.close();

        mGridDrawable = new ShapeDrawable(new PathShape(gridPath, this.getWidth(), this.getHeight()));
        mGridDrawable.getPaint().setColor(0xffeeeeee);//0xff74AC23);
        mGridDrawable.getPaint().setStyle(Paint.Style.STROKE);
        mGridDrawable.getPaint().setStrokeWidth(2.0f);
        mGridDrawable.setBounds(0, 0, this.getWidth(), this.getHeight());

        mIntersectPath = new Path();
        mIntersectPaint = new Paint();
        mIntersectPaint.setColor(0xff74AC23);
        mIntersectPaint.setStyle(Paint.Style.STROKE);
        mIntersectPaint.setStrokeWidth(3.0f);
        mIntersectPath1 = new Path();
        mIntersectPaint1 = new Paint();
        mIntersectPaint1.setColor(0xffAC7423);
        mIntersectPaint1.setStyle(Paint.Style.STROKE);
        mIntersectPaint1.setStrokeWidth(1.0f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mGridDrawable.draw(canvas);

        mIntersectPath.reset();
        mIntersectPath.moveTo(-this.getWidth(), touchY);
        mIntersectPath.lineTo(this.getWidth(), touchY);
        mIntersectPath.moveTo(touchX, -this.getHeight());
        mIntersectPath.lineTo(touchX, this.getHeight());
        mIntersectPath.close();
        canvas.drawPath(mIntersectPath, mIntersectPaint);

        mIntersectPath1.reset();
        mIntersectPath1.moveTo(-this.getWidth(), curY);
        mIntersectPath1.lineTo(this.getWidth(), curY);
        mIntersectPath1.moveTo(curX, -this.getHeight());
        mIntersectPath1.lineTo(curX, this.getHeight());
        mIntersectPath1.close();
        canvas.drawPath(mIntersectPath1, mIntersectPaint1);

    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        float x = ev.getX();
        float y = ev.getY();
        curX = x;
        curY = y;
        float p = ev.getPressure();
        final int nHistory = ev.getHistorySize();
        final int nPointers = ev.getPointerCount();

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //Log.d("onTouch", String.format("down x: %.2f y: %.2f p: %.2f nHist: %d, nPtr: %d", x, y, p, nHistory, nPointers));
                touchX = x;
                touchY = y;
                this.invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                //Log.d("onTouch", String.format("..move x: %.2f y: %.2f p: %.2f nHist: %d, nPtr: %d", x-touchX, y-touchY, p, nHistory, nPointers));
                this.invalidate();
                break;
            case MotionEvent.ACTION_UP:
                //Log.d("onTouch", String.format("..up   x: %.2f y: %.2f p: %.2f nHist: %d, nPtr: %d", x-touchX, y-touchY, p, nHistory, nPointers));
                this.invalidate();
                break;
        }
        return true;
    }

}

@@ -601,18 +601,31 @@
 rX, curY
+, curPressure
 ;%0A
-
 %0A    pub
@@ -684,33 +684,8 @@
 t);%0A
-        //init(null, 0);%0A
     
@@ -781,34 +781,8 @@
 s);%0A
-        //init(attrs, 0);%0A
     
@@ -902,41 +902,8 @@
 e);%0A
-        //init(attrs, defStyle);%0A
     
@@ -2326,12 +2326,12 @@
 ffAC
-74
 23
+74
 );%0A 
@@ -3260,24 +3260,96 @@
 ctPaint1);%0A%0A
+        canvas.drawCircle(100, 100, 100*curPressure, mIntersectPaint1);%0A
     %7D%0A%0A    @
@@ -3533,24 +3533,49 @@
 Pressure();%0A
+        curPressure = p;%0A
         fina
@@ -4277,16 +4277,16 @@
 ION_UP:%0A
-
         
@@ -4418,32 +4418,68 @@
 y, nPointers));%0A
+                curPressure = 0.0f;%0A
                 

add pressure tracking circle