Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void doDraw(Canvas canvas)
- {
- PathPoint xya = null;
- canvas.drawColor(Color.WHITE);
- //draw the basic node grid that user will draw the lines on
- for (int i = 0; i < 5; i++)
- {
- for (int j = 0; j < 5; j++)
- {
- int xPos = j * mNodeGap;
- int yPos = i * mNodeGap;
- try {
- xya = new PathPoint(xPos, yPos, null);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- mNodeCoordinates[i][j] = xya;
- canvas.drawBitmap(mBitmap, xPos, yPos, null);
- }
- }
- synchronized (mViewThread.getSurefaceHolder())
- {
- //draw path user is creating with her finger on screen
- for (Path path : mGraphics)
- {
- float aStartCoordinates[] = {0f, 0f};
- float aEndCoordinates[] = {0f, 0f};
- //get path values
- PathMeasure pm = new PathMeasure(path, true);
- pm.getPosTan(0f, aStartCoordinates, null);
- //System.out.println("aStartCoordinates X:" + aStartCoordinates[0] + " aStartCoordinates Y:" + aStartCoordinates[1]);
- pm.getPosTan(pm.getLength(), aEndCoordinates, null);
- //System.out.println("aEndCoordinates X:" + aEndCoordinates[0] + " aEndCoordinates Y:" + aEndCoordinates[1]);
- //coordinates are within game board boundaries
- if((aStartCoordinates[0] >= 1 && aStartCoordinates[1] >= 1) && (aEndCoordinates[0] >= 1 && aEndCoordinates[1] >= 1))
- {
- canvas.drawPath(path, mPathPaint);
- }
- }
- //nodes that the path goes through, are repainted green
- //these nodes are building the drawn pattern
- for (ArrayList<PathPoint> nodePattern : mNodesHitPatterns)
- {
- for (PathPoint nodeHit : nodePattern)
- {
- canvas.drawBitmap(mDotOK, nodeHit.x - ((mDotOK.getWidth()/2) - (mBitmap.getWidth()/2)), nodeHit.y - ((mDotOK.getHeight()/2) - (mBitmap.getHeight()/2)), null);
- }
- }
- this.destroyDrawingCache();
- }
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- synchronized (mViewThread.getSurefaceHolder()) {
- if(event.getAction() == MotionEvent.ACTION_DOWN)
- {
- System.out.println("Action downE x: " + event.getX() + " y: " + event.getY());
- for (int i = 0; i < mGridSize; i++)
- {
- for (int j = 0; j < mGridSize; j++)
- {
- PathPoint pathPoint = mNodeCoordinates[i][j];
- if((Math.abs((int)event.getX() - pathPoint.x) <= 35) && (Math.abs((int)event.getY() - pathPoint.y) <= 35))
- {
- //mPath.moveTo(pathPoint.x + mBitmap.getWidth() / 2, pathPoint.y + mBitmap.getHeight() / 2);
- //System.out.println("Action down x: " + pathPoint.x + " y: " + pathPoint.y);
- ArrayList<PathPoint> newNodesPattern = new ArrayList<PathPoint>();
- mNodesHitPatterns.add(newNodesPattern);
- //mNodesHitPatterns.add(nh);
- // pathPoint.setAction("down");
- break;
- }
- }
- }
- }
- else if(event.getAction() == MotionEvent.ACTION_MOVE)
- {
- final int historySize = event.getHistorySize();
- System.out.println("historySize: " + historySize);
- System.out.println("Action moveE x: " + event.getX() + " y: " + event.getY());
- coordinateFound:
- for (int i = 0; i < mGridSize; i++)
- {
- for (int j = 0; j < mGridSize; j++)
- {
- PathPoint pathPoint = mNodeCoordinates[i][j];
- if((Math.abs((int)event.getX() - pathPoint.x) <= 35) && (Math.abs((int)event.getY() - pathPoint.y) <= 35))
- {
- int lastPatternIndex = mNodesHitPatterns.size()-1;
- ArrayList<PathPoint> lastPattern = mNodesHitPatterns.get(lastPatternIndex);
- int lastPatternLastNode = lastPattern.size()-1;
- if(lastPatternLastNode != -1)
- {
- if(!pathPoint.equals(lastPattern.get(lastPatternLastNode).x, lastPattern.get(lastPatternLastNode).y))
- {
- lastPattern.add(pathPoint);
- System.out.println("Action moveC [add point] x: " + pathPoint.x + " y: " + pathPoint.y);
- }
- }
- else
- {
- lastPattern.add(pathPoint);
- System.out.println("Action moveC [add point] x: " + pathPoint.x + " y: " + pathPoint.y);
- }
- break coordinateFound;
- }
- else //no current match => try historical
- {
- if(historySize > 0)
- {
- for (int k = 0; k < historySize; k++)
- {
- //System.out.println("Action moveH x: " + event.getHistoricalX(k) + " y: " + event.getHistoricalY(k));
- if((Math.abs((int)event.getHistoricalX(k) - pathPoint.x) <= 35) && (Math.abs((int)event.getHistoricalY(k) - pathPoint.y) <= 35))
- {
- int lastPatternIndex = mNodesHitPatterns.size()-1;
- ArrayList<PathPoint> lastPattern = mNodesHitPatterns.get(lastPatternIndex);
- int lastPatternLastNode = lastPattern.size()-1;
- if(lastPatternLastNode != -1)
- {
- if(!pathPoint.equals(lastPattern.get(lastPatternLastNode).x, lastPattern.get(lastPatternLastNode).y))
- {
- lastPattern.add(pathPoint);
- System.out.println("Action moveH [add point] x: " + pathPoint.x + " y: " + pathPoint.y);
- }
- }
- else
- {
- lastPattern.add(pathPoint);
- System.out.println("Action moveH [add point] x: " + pathPoint.x + " y: " + pathPoint.y);
- }
- break coordinateFound;
- }
- }
- }
- }
- }
- }
- }
- else if(event.getAction() == MotionEvent.ACTION_UP)
- {
- for (int i = 0; i < mGridSize; i++) {
- for (int j = 0; j < mGridSize; j++) {
- PathPoint pathPoint = mNodeCoordinates[i][j];
- if((Math.abs((int)event.getX() - pathPoint.x) <= 35) && (Math.abs((int)event.getY() - pathPoint.y) <= 35))
- {
- //the location of the node
- //mPath.lineTo(pathPoint.x + mBitmap.getWidth() / 2, pathPoint.y + mBitmap.getHeight() / 2);
- //System.out.println("Action up x: " + pathPoint.x + " y: " + pathPoint.y);
- //mGraphics.add(mPath);
- // mNodesHit.add(pathPoint);
- // pathPoint.setAction("up");
- break;
- }
- }
- }
- }
- System.out.println(mNodesHitPatterns.toString());
- //create mPath
- for (ArrayList<PathPoint> nodePattern : mNodesHitPatterns)
- {
- for (int i = 0; i < nodePattern.size(); i++)
- {
- if(i == 0) //first node in pattern
- {
- mPath.moveTo(nodePattern.get(i).x + mBitmap.getWidth() / 2, nodePattern.get(i).y + mBitmap.getHeight() / 2);
- }
- else
- {
- mPath.lineTo(nodePattern.get(i).x + mBitmap.getWidth() / 2, nodePattern.get(i).y + mBitmap.getHeight() / 2);
- }
- mGraphics.add(mPath);
- }
- }
- return true;
- }
- }
- public class ViewThread extends Thread {
- private Panel mPanel;
- private SurfaceHolder mHolder;
- private boolean mRun = false;
- public ViewThread(Panel panel) {
- mPanel = panel;
- mHolder = mPanel.getHolder();
- }
- public void setRunning(boolean run) {
- mRun = run;
- }
- public SurfaceHolder getSurefaceHolder()
- {
- return mHolder;
- }
- @Override
- public void run()
- {
- Canvas canvas = null;
- while (mRun)
- {
- canvas = mHolder.lockCanvas();
- if (canvas != null)
- {
- mPanel.doDraw(canvas);
- mHolder.unlockCanvasAndPost(canvas);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment