Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.34 KB | None | 0 0
  1. package com.rsqtechnologies.rehapointphysio.model.utils.views;
  2.  
  3. import android.content.Context;
  4. import android.graphics.drawable.ShapeDrawable;
  5. import android.support.annotation.Nullable;
  6. import android.view.MotionEvent;
  7.  
  8. import com.rey.material.widget.ImageView;
  9. import com.rsqtechnologies.rehapointphysio.R;
  10. import com.rsqtechnologies.rehapointphysio.model.businessobjects.BodyPartArea;
  11. import com.rsqtechnologies.rehapointphysio.model.businessobjects.BodyPartGroup;
  12. import com.rsqtechnologies.rehapointphysio.model.businessobjects.SkeletonClickableArea;
  13. import com.rsqtechnologies.rehapointphysio.model.utils.VectorShape;
  14. import com.rsqtechnologies.rehapointphysio.skeleton.SkeletonActivity;
  15.  
  16. import java.util.ArrayList;
  17. import java.util.Deque;
  18. import java.util.LinkedList;
  19.  
  20. public class SkeletonWithLayers extends ImageView {
  21.  
  22. private Context context;
  23. private VectorShape shape;
  24. private Deque<VectorShape.Layer> layers = new LinkedList<>();
  25. private ArrayList<String> upperLimbPathNames;
  26. private ArrayList<String> lowerLimbPathNames;
  27. private ArrayList<String> spinePathNames;
  28.  
  29. public SkeletonWithLayers(Context context) {
  30. super(context);
  31. this.context = context;
  32. shape = new VectorShape(context, R.drawable.skeleton_front);
  33. setBackground(new ShapeDrawable(shape));
  34. setupPathNames();
  35. }
  36.  
  37. @Override
  38. public boolean onTouchEvent(MotionEvent event) {
  39. float posX = event.getX()/this.getWidth();
  40. float posY = event.getY()/this.getHeight();
  41. BodyPartGroup bodyPartGroup = null;
  42. if (checkIfClickedOnBodyPartGroup(BodyPartGroup.SPINE, posX, posY)){
  43. bodyPartGroup = BodyPartGroup.SPINE;
  44. } else if (checkIfClickedOnBodyPartGroup(BodyPartGroup.LOWER_LIMB, posX, posY)) {
  45. bodyPartGroup = BodyPartGroup.LOWER_LIMB;
  46. } else if (checkIfClickedOnBodyPartGroup(BodyPartGroup.UPPER_LIMB, posX, posY)) {
  47. bodyPartGroup = BodyPartGroup.UPPER_LIMB;
  48. }
  49. if (bodyPartGroup != null) {
  50. highlightBodyPartGroup(bodyPartGroup);
  51. ((SkeletonActivity) context).bodyPartOnClick(bodyPartGroup);
  52. }
  53. invalidate();
  54. return false;
  55. }
  56.  
  57. private boolean checkIfClickedOnBodyPartGroup(BodyPartGroup bodyPartGroup, float posX, float posY){
  58. switch (bodyPartGroup){
  59. case SPINE:
  60. if (checkIfPositionInArea(posX, posY, getAreaCoordinates(SkeletonClickableArea.SPINE)))
  61. return true;
  62. break;
  63. case LOWER_LIMB:
  64. if (checkIfPositionInArea(posX, posY, getAreaCoordinates(SkeletonClickableArea.LEFT_LEG)) || checkIfPositionInArea(posX, posY, getAreaCoordinates(SkeletonClickableArea.RIGHT_LEG)))
  65. return true;
  66. break;
  67. case UPPER_LIMB:
  68. if (checkIfPositionInArea(posX, posY, getAreaCoordinates(SkeletonClickableArea.LEFT_TOP_ARM)) || checkIfPositionInArea(posX, posY, getAreaCoordinates(SkeletonClickableArea.LEFT_BOTTOM_ARM)) ||
  69. checkIfPositionInArea(posX, posY, getAreaCoordinates(SkeletonClickableArea.RIGHT_TOP_ARM))|| checkIfPositionInArea(posX, posY, getAreaCoordinates(SkeletonClickableArea.RIGHT_BOTTOM_ARM)))
  70. return true;
  71. break;
  72. }
  73. return false;
  74. }
  75.  
  76. private boolean checkIfPositionInArea(double posX, double posY, ArrayList<Double> coordinates){
  77. if (posX > coordinates.get(0) && posX < coordinates.get(1) && posY > coordinates.get(2) && posY < coordinates.get(3)) {
  78. return true;
  79. } else {
  80. return false;
  81. }
  82. }
  83.  
  84. public void highlightBodyAreas(BodyPartArea bodyPartArea){
  85. ArrayList<String> bodyParts = setupDetailesBodyPart(bodyPartArea);
  86. if (bodyParts != null && !bodyParts.isEmpty()) {
  87. for (String name : bodyParts) {
  88. shape.getLayersAt(name, layers);
  89. if (layers.size() != 0) {
  90. layers.getFirst().highlight();
  91. invalidate();
  92. }
  93. }
  94. }
  95. }
  96.  
  97. public void highlightBodyPartGroup(BodyPartGroup bodyPartGroup){
  98. ArrayList<String> bodyPartsToHighlight = null;
  99. switch (bodyPartGroup){
  100. case UPPER_LIMB:
  101. bodyPartsToHighlight = upperLimbPathNames;
  102. break;
  103. case LOWER_LIMB:
  104. bodyPartsToHighlight = lowerLimbPathNames;
  105. break;
  106. case SPINE:
  107. bodyPartsToHighlight = spinePathNames;
  108. break;
  109. }
  110. unhighlightAll();
  111. for (String name : bodyPartsToHighlight) {
  112. shape.getLayersAt(name, layers);
  113. if (layers.size() != 0) {
  114. layers.getFirst().highlight();
  115. invalidate();
  116. }
  117. }
  118. }
  119.  
  120. public void unhighlightAll() {
  121. unhighlightBodyParts(upperLimbPathNames);
  122. unhighlightBodyParts(lowerLimbPathNames);
  123. unhighlightBodyParts(spinePathNames);
  124. }
  125.  
  126. private void unhighlightBodyParts(ArrayList<String> bodyParts) {
  127. for (String name : bodyParts) {
  128. shape.getLayersAt(name, layers);
  129. if (layers.size() != 0) {
  130. layers.getFirst().unhighlight();
  131. invalidate();
  132. }
  133. }
  134. }
  135.  
  136. @Nullable
  137. private ArrayList<String> setupDetailesBodyPart(BodyPartArea bodyPartArea) {
  138. ArrayList<String> bodyParts = new ArrayList<>();
  139. switch (bodyPartArea) {
  140. case SHOULDER:
  141. bodyParts.add("shoulder_right_1");
  142. bodyParts.add("shoulder_right_2");
  143. break;
  144. case ELBOW:
  145. bodyParts.add("elbow_right_1");
  146. bodyParts.add("elbow_right_2");
  147. break;
  148. case WRIST:
  149. bodyParts.add("wrist_right");
  150. break;
  151. case HAND:
  152. bodyParts.add("hand_right");
  153. bodyParts.add("elbow_right_1");
  154. bodyParts.add("elbow_right_2");
  155. bodyParts.add("forearm_right");
  156. bodyParts.add("arm_right");
  157. bodyParts.add("wrist_right");
  158. break;
  159. case HIP_JOINT:
  160. bodyParts.add("hip_right");
  161. break;
  162. case KNEE_JOINT:
  163. bodyParts.add("knee_right_1");
  164. bodyParts.add("knee_right_2");
  165. break;
  166. case ANKLE:
  167. bodyParts.add("ankle_right_1");
  168. bodyParts.add("ankle_right_2");
  169. break;
  170. case FOOT:
  171. bodyParts.add("foot_right");
  172. bodyParts.add("heel_right");
  173. break;
  174. case CERVICAL_SPINE:
  175. bodyParts.add("neck");
  176. break;
  177. case THORARIC_SPINE:
  178. bodyParts.add("thoracic_spine");
  179. break;
  180. case LUMBAR_SPINE:
  181. bodyParts.add("lumbar_spine");
  182. break;
  183. case FAULTY_POSTURE:
  184. bodyParts.add("thoracic_spine");
  185. bodyParts.add("neck");
  186. bodyParts.add("lumbar_spine");
  187. break;
  188. case OTHER:
  189. break;
  190. }
  191. return bodyParts;
  192. }
  193.  
  194. private void setupPathNames(){
  195. setupUpperLimbPathNames();
  196. setupLowerLimbPathNames();
  197. setupSpinePathNames();
  198. }
  199.  
  200. private void setupUpperLimbPathNames() {
  201. upperLimbPathNames = new ArrayList<>();
  202. upperLimbPathNames.add("hand_right");
  203. upperLimbPathNames.add("hand_left");
  204. upperLimbPathNames.add("forearm_right");
  205. upperLimbPathNames.add("forearm_left");
  206. upperLimbPathNames.add("elbow_right_1");
  207. upperLimbPathNames.add("elbow_right_2");
  208. upperLimbPathNames.add("elbow_left_1");
  209. upperLimbPathNames.add("elbow_left_2");
  210. upperLimbPathNames.add("arm_right");
  211. upperLimbPathNames.add("arm_left");
  212. upperLimbPathNames.add("wrist_right");
  213. upperLimbPathNames.add("wrist_left");
  214. upperLimbPathNames.add("collarbone_right");
  215. upperLimbPathNames.add("collarbone_left");
  216. upperLimbPathNames.add("shoulder_right_1");
  217. upperLimbPathNames.add("shoulder_right_2");
  218. }
  219.  
  220. private void setupLowerLimbPathNames() {
  221. lowerLimbPathNames = new ArrayList<>();
  222. lowerLimbPathNames.add("foot_right");
  223. lowerLimbPathNames.add("heel_left");
  224. lowerLimbPathNames.add("foot_left");
  225. lowerLimbPathNames.add("heel_right");
  226. lowerLimbPathNames.add("lowerleg_right");
  227. lowerLimbPathNames.add("lowerleg_left");
  228. lowerLimbPathNames.add("ankle_right_1");
  229. lowerLimbPathNames.add("ankle_right_2");
  230. lowerLimbPathNames.add("ankle_left_1");
  231. lowerLimbPathNames.add("ankle_left_2");
  232. lowerLimbPathNames.add("knee_right_1");
  233. lowerLimbPathNames.add("knee_right_2");
  234. lowerLimbPathNames.add("knee_left_1");
  235. lowerLimbPathNames.add("knee_left_2");
  236. lowerLimbPathNames.add("femur_right");
  237. lowerLimbPathNames.add("femur_left");
  238. lowerLimbPathNames.add("hip_right");
  239. lowerLimbPathNames.add("hip_left");
  240. }
  241.  
  242. private void setupSpinePathNames(){
  243. spinePathNames = new ArrayList<>();
  244. spinePathNames.add("thoracic_spine");
  245. spinePathNames.add("neck");
  246. spinePathNames.add("lumbar_spine");
  247. }
  248.  
  249. private ArrayList<Double> getAreaCoordinates(SkeletonClickableArea skeletonClickableArea){
  250. ArrayList<Double> coordinates = new ArrayList<>();
  251. switch (skeletonClickableArea){
  252. case SPINE:
  253. coordinates.add(0.3134);
  254. coordinates.add(0.6300);
  255. coordinates.add(0.1222);
  256. coordinates.add(0.4635);
  257. break;
  258. case LEFT_LEG:
  259. coordinates.add(0.2829);
  260. coordinates.add(0.4495);
  261. coordinates.add(0.4636);
  262. coordinates.add(0.9854);
  263. break;
  264. case RIGHT_LEG:
  265. coordinates.add(0.5095);
  266. coordinates.add(0.6795);
  267. coordinates.add(0.4636);
  268. coordinates.add(0.9854);
  269. break;
  270. case LEFT_TOP_ARM:
  271. coordinates.add(0.0);
  272. coordinates.add(0.3133);
  273. coordinates.add(0.1222);
  274. coordinates.add(0.3824);
  275. break;
  276. case LEFT_BOTTOM_ARM:
  277. coordinates.add(0.6300);
  278. coordinates.add(1.0);
  279. coordinates.add(0.1222);
  280. coordinates.add(0.3824);
  281. break;
  282. case RIGHT_TOP_ARM:
  283. coordinates.add(0.0);
  284. coordinates.add(0.2800);
  285. coordinates.add(0.3824);
  286. coordinates.add(0.6024);
  287. break;
  288. case RIGHT_BOTTOM_ARM:
  289. coordinates.add(0.6795);
  290. coordinates.add(1.0);
  291. coordinates.add(0.3824);
  292. coordinates.add(0.6024);
  293. break;
  294. }
  295. return coordinates;
  296. }
  297.  
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement