Guest User

Untitled

a guest
Jan 18th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.96 KB | None | 0 0
  1. public void doDraw(Canvas canvas)
  2. {
  3. PathPoint xya = null;
  4.  
  5. canvas.drawColor(Color.WHITE);
  6.  
  7. //draw the basic node grid that user will draw the lines on
  8. for (int i = 0; i < 5; i++)
  9. {
  10. for (int j = 0; j < 5; j++)
  11. {
  12. int xPos = j * mNodeGap;
  13. int yPos = i * mNodeGap;
  14.  
  15. try {
  16. xya = new PathPoint(xPos, yPos, null);
  17. } catch (Exception e) {
  18. // TODO Auto-generated catch block
  19. e.printStackTrace();
  20. }
  21.  
  22. mNodeCoordinates[i][j] = xya;
  23.  
  24. canvas.drawBitmap(mBitmap, xPos, yPos, null);
  25. }
  26. }
  27.  
  28. synchronized (mViewThread.getSurefaceHolder())
  29. {
  30. //draw path user is creating with her finger on screen
  31. for (Path path : mGraphics)
  32. {
  33. float aStartCoordinates[] = {0f, 0f};
  34. float aEndCoordinates[] = {0f, 0f};
  35.  
  36. //get path values
  37. PathMeasure pm = new PathMeasure(path, true);
  38.  
  39. pm.getPosTan(0f, aStartCoordinates, null);
  40. //System.out.println("aStartCoordinates X:" + aStartCoordinates[0] + " aStartCoordinates Y:" + aStartCoordinates[1]);
  41. pm.getPosTan(pm.getLength(), aEndCoordinates, null);
  42. //System.out.println("aEndCoordinates X:" + aEndCoordinates[0] + " aEndCoordinates Y:" + aEndCoordinates[1]);
  43.  
  44. //coordinates are within game board boundaries
  45. if((aStartCoordinates[0] >= 1 && aStartCoordinates[1] >= 1) && (aEndCoordinates[0] >= 1 && aEndCoordinates[1] >= 1))
  46. {
  47. canvas.drawPath(path, mPathPaint);
  48. }
  49. }
  50.  
  51. //nodes that the path goes through, are repainted green
  52. //these nodes are building the drawn pattern
  53. for (ArrayList<PathPoint> nodePattern : mNodesHitPatterns)
  54. {
  55. for (PathPoint nodeHit : nodePattern)
  56. {
  57. canvas.drawBitmap(mDotOK, nodeHit.x - ((mDotOK.getWidth()/2) - (mBitmap.getWidth()/2)), nodeHit.y - ((mDotOK.getHeight()/2) - (mBitmap.getHeight()/2)), null);
  58. }
  59. }
  60.  
  61. this.destroyDrawingCache();
  62. }
  63. }
  64.  
  65. @Override
  66. public boolean onTouchEvent(MotionEvent event) {
  67.  
  68. synchronized (mViewThread.getSurefaceHolder()) {
  69.  
  70. if(event.getAction() == MotionEvent.ACTION_DOWN)
  71. {
  72. System.out.println("Action downE x: " + event.getX() + " y: " + event.getY());
  73.  
  74. for (int i = 0; i < mGridSize; i++)
  75. {
  76. for (int j = 0; j < mGridSize; j++)
  77. {
  78. PathPoint pathPoint = mNodeCoordinates[i][j];
  79.  
  80. if((Math.abs((int)event.getX() - pathPoint.x) <= 35) && (Math.abs((int)event.getY() - pathPoint.y) <= 35))
  81. {
  82. //mPath.moveTo(pathPoint.x + mBitmap.getWidth() / 2, pathPoint.y + mBitmap.getHeight() / 2);
  83.  
  84. //System.out.println("Action down x: " + pathPoint.x + " y: " + pathPoint.y);
  85. ArrayList<PathPoint> newNodesPattern = new ArrayList<PathPoint>();
  86. mNodesHitPatterns.add(newNodesPattern);
  87. //mNodesHitPatterns.add(nh);
  88. // pathPoint.setAction("down");
  89. break;
  90. }
  91. }
  92. }
  93. }
  94. else if(event.getAction() == MotionEvent.ACTION_MOVE)
  95. {
  96. final int historySize = event.getHistorySize();
  97. System.out.println("historySize: " + historySize);
  98. System.out.println("Action moveE x: " + event.getX() + " y: " + event.getY());
  99.  
  100. coordinateFound:
  101. for (int i = 0; i < mGridSize; i++)
  102. {
  103. for (int j = 0; j < mGridSize; j++)
  104. {
  105. PathPoint pathPoint = mNodeCoordinates[i][j];
  106.  
  107. if((Math.abs((int)event.getX() - pathPoint.x) <= 35) && (Math.abs((int)event.getY() - pathPoint.y) <= 35))
  108. {
  109. int lastPatternIndex = mNodesHitPatterns.size()-1;
  110. ArrayList<PathPoint> lastPattern = mNodesHitPatterns.get(lastPatternIndex);
  111. int lastPatternLastNode = lastPattern.size()-1;
  112.  
  113. if(lastPatternLastNode != -1)
  114. {
  115. if(!pathPoint.equals(lastPattern.get(lastPatternLastNode).x, lastPattern.get(lastPatternLastNode).y))
  116. {
  117. lastPattern.add(pathPoint);
  118. System.out.println("Action moveC [add point] x: " + pathPoint.x + " y: " + pathPoint.y);
  119. }
  120. }
  121. else
  122. {
  123. lastPattern.add(pathPoint);
  124. System.out.println("Action moveC [add point] x: " + pathPoint.x + " y: " + pathPoint.y);
  125. }
  126.  
  127. break coordinateFound;
  128. }
  129. else //no current match => try historical
  130. {
  131. if(historySize > 0)
  132. {
  133. for (int k = 0; k < historySize; k++)
  134. {
  135. //System.out.println("Action moveH x: " + event.getHistoricalX(k) + " y: " + event.getHistoricalY(k));
  136. if((Math.abs((int)event.getHistoricalX(k) - pathPoint.x) <= 35) && (Math.abs((int)event.getHistoricalY(k) - pathPoint.y) <= 35))
  137. {
  138. int lastPatternIndex = mNodesHitPatterns.size()-1;
  139. ArrayList<PathPoint> lastPattern = mNodesHitPatterns.get(lastPatternIndex);
  140. int lastPatternLastNode = lastPattern.size()-1;
  141.  
  142. if(lastPatternLastNode != -1)
  143. {
  144. if(!pathPoint.equals(lastPattern.get(lastPatternLastNode).x, lastPattern.get(lastPatternLastNode).y))
  145. {
  146. lastPattern.add(pathPoint);
  147. System.out.println("Action moveH [add point] x: " + pathPoint.x + " y: " + pathPoint.y);
  148. }
  149. }
  150. else
  151. {
  152. lastPattern.add(pathPoint);
  153. System.out.println("Action moveH [add point] x: " + pathPoint.x + " y: " + pathPoint.y);
  154. }
  155.  
  156. break coordinateFound;
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. }
  164. else if(event.getAction() == MotionEvent.ACTION_UP)
  165. {
  166. for (int i = 0; i < mGridSize; i++) {
  167.  
  168. for (int j = 0; j < mGridSize; j++) {
  169.  
  170. PathPoint pathPoint = mNodeCoordinates[i][j];
  171.  
  172. if((Math.abs((int)event.getX() - pathPoint.x) <= 35) && (Math.abs((int)event.getY() - pathPoint.y) <= 35))
  173. {
  174. //the location of the node
  175. //mPath.lineTo(pathPoint.x + mBitmap.getWidth() / 2, pathPoint.y + mBitmap.getHeight() / 2);
  176.  
  177. //System.out.println("Action up x: " + pathPoint.x + " y: " + pathPoint.y);
  178.  
  179. //mGraphics.add(mPath);
  180. // mNodesHit.add(pathPoint);
  181. // pathPoint.setAction("up");
  182. break;
  183. }
  184. }
  185. }
  186. }
  187.  
  188. System.out.println(mNodesHitPatterns.toString());
  189.  
  190. //create mPath
  191. for (ArrayList<PathPoint> nodePattern : mNodesHitPatterns)
  192. {
  193.  
  194. for (int i = 0; i < nodePattern.size(); i++)
  195. {
  196. if(i == 0) //first node in pattern
  197. {
  198. mPath.moveTo(nodePattern.get(i).x + mBitmap.getWidth() / 2, nodePattern.get(i).y + mBitmap.getHeight() / 2);
  199. }
  200. else
  201. {
  202. mPath.lineTo(nodePattern.get(i).x + mBitmap.getWidth() / 2, nodePattern.get(i).y + mBitmap.getHeight() / 2);
  203. }
  204.  
  205. mGraphics.add(mPath);
  206. }
  207. }
  208.  
  209. return true;
  210. }
  211. }
  212.  
  213. public class ViewThread extends Thread {
  214. private Panel mPanel;
  215. private SurfaceHolder mHolder;
  216. private boolean mRun = false;
  217.  
  218. public ViewThread(Panel panel) {
  219. mPanel = panel;
  220. mHolder = mPanel.getHolder();
  221. }
  222.  
  223. public void setRunning(boolean run) {
  224. mRun = run;
  225. }
  226.  
  227. public SurfaceHolder getSurefaceHolder()
  228. {
  229. return mHolder;
  230. }
  231.  
  232. @Override
  233. public void run()
  234. {
  235. Canvas canvas = null;
  236. while (mRun)
  237. {
  238. canvas = mHolder.lockCanvas();
  239. if (canvas != null)
  240. {
  241. mPanel.doDraw(canvas);
  242. mHolder.unlockCanvasAndPost(canvas);
  243. }
  244. }
  245. }
  246. }
Add Comment
Please, Sign In to add comment