package net.yaht.m.dynview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class StandardPaint extends View implements OnTouchListener {

    Paint mPaint;
    float mX;
    float mY;
    TextView _txt_coords;

    // note: important, in order to have our view initialized from
    // .xml markup
    public StandardPaint(Context context,AttributeSet attributeSet){

        super(context, attributeSet);

        mPaint = new Paint();
        mX = mY = -100;
        _txt_coords = null;

    }

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

        // our circle will have color...
        mPaint.setColor(Color.GREEN);

        // Draw the circle at (x,y) with radius 15
        canvas.drawCircle(mX, mY, 15, mPaint);

    }

    public void setTextView(TextView tv){

        // where to show our circle coords;
        _txt_coords = tv;

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {

            // when we have a touch from user...
            case MotionEvent.ACTION_DOWN:

                // our X
                mX = event.getX();
                // our Y
                mY = event.getY();

                // Setting them...
                if(_txt_coords !=null) {
                    _txt_coords.setText("down x :" + mX + " , " + "y :" + mY);
                }

                // redraw the canvas
                invalidate();

                break;

            case MotionEvent.ACTION_UP:

                // our X
                mX = event.getX();
                // our Y
                mY = event.getY();

                // Setting them...
                if(_txt_coords !=null) {
                    _txt_coords.setText("up x :" + mX + " , " + "y :" + mY);
                }

                // redraw the canvas
                invalidate();

                break;

        }

        return true;

    }

}