ModelViewerActivity.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package app.mar.activities;
  2. import android.support.v4.view.MotionEventCompat;
  3. import android.os.Bundle;
  4. import android.view.MotionEvent;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.FrameLayout;
  8. import android.widget.TextView;
  9. import app.mar.utils.FontChangeCrawler;
  10. import app.mar.game.Resource;
  11. import app.mar.utils.geometry.Point;
  12. import min3d.core.Object3d;
  13. import min3d.core.RendererActivity;
  14. import min3d.vos.CameraVo;
  15. import min3d.vos.Light;
  16. import min3d.vos.LightType;
  17. public class ModelViewerActivity extends RendererActivity implements View.OnTouchListener {
  18. private Resource mResource;
  19. private Button mInfo;
  20. private TextView mTitle;
  21. private FrameLayout mFrameLayout;
  22. protected float mPreviousX;
  23. protected float mPreviousY;
  24. protected float mPreviousDist=0;
  25. protected CameraVo mCamera = new CameraVo();
  26. protected boolean mIsZooming = false;
  27. protected boolean mIsMoving = false;
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.activity_model_viewer);
  32. mInfo = (Button) findViewById(R.id.info);
  33. mFrameLayout = (FrameLayout) findViewById(R.id.frame_viewer);
  34. mTitle = (TextView) findViewById(R.id.title);
  35. if(getIntent().hasExtra("resource"))
  36. {
  37. mResource = (Resource) getIntent().getSerializableExtra("resource");
  38. if(mResource!=null) setTitle(mResource.getTitle());
  39. mFrameLayout.addView(_glSurfaceView);
  40. mTitle.setText(mResource.getTitle());
  41. }else
  42. {
  43. mTitle.setText("Error: Res not found");
  44. }
  45. FontChangeCrawler.setFont(this);
  46. }
  47. public void onClickInfo(View v)
  48. {
  49. }
  50. public void initScene()
  51. {
  52. scene.backgroundColor().setAll(0x00000000);
  53. Light myLight = new Light();
  54. myLight.position.setZ(0);
  55. myLight.position.setY(0);
  56. myLight.type(LightType.POSITIONAL);
  57. scene.lights().add(myLight);
  58. Object3d oo = mResource.get3DModel(this);
  59. oo.position().z=mResource.getPosition().z;
  60. oo.position().y=mResource.getPosition().y;
  61. oo.position().x=mResource.getPosition().x;
  62. oo.position().z-=20;
  63. mCamera.frustum.zFar(0.1f);
  64. mCamera.frustum.zFar(1000);
  65. scene.addChild(mResource.get3DModel(this));
  66. }
  67. @Override
  68. public void updateScene() {
  69. }
  70. @Override
  71. public boolean onTouch(View view, MotionEvent motionEvent) {
  72. float x = motionEvent.getX();
  73. float y = motionEvent.getY();
  74. final int action = motionEvent.getAction()%256;
  75. int index = MotionEventCompat.getActionIndex(motionEvent);
  76. if (motionEvent.getPointerCount() > 1) {
  77. switch (action) {
  78. case MotionEvent.ACTION_MOVE:
  79. {
  80. if(!mIsZooming) break;
  81. Point a = new Point(motionEvent.getX(0), motionEvent.getY(0));
  82. Point b = new Point(motionEvent.getX(1), motionEvent.getY(1));
  83. double d = a.getDistanceWith(b);
  84. double delta = d - mPreviousDist;
  85. mCamera.position.z+=delta/2;
  86. mPreviousDist = (float)a.getDistanceWith(b);
  87. scene.camera(mCamera);
  88. break;
  89. }
  90. case MotionEvent.ACTION_POINTER_DOWN:
  91. case MotionEvent.ACTION_DOWN: {
  92. Point a = new Point(motionEvent.getX(0), motionEvent.getY(0));
  93. Point b = new Point(motionEvent.getX(1), motionEvent.getY(1));
  94. mPreviousDist = (float)a.getDistanceWith(b);
  95. mIsZooming=true;
  96. break;
  97. }
  98. case MotionEvent.ACTION_UP:
  99. case MotionEvent.ACTION_POINTER_UP:
  100. case MotionEvent.ACTION_CANCEL:
  101. mIsZooming=false;
  102. mIsMoving=false;
  103. break;
  104. }
  105. }
  106. else {
  107. mIsZooming=false;
  108. switch (motionEvent.getAction()) {
  109. case MotionEvent.ACTION_MOVE:
  110. {
  111. if(!mIsMoving) break;
  112. float dx = x - mPreviousX;
  113. float dy = y - mPreviousY;
  114. Object3d oo = mResource.get3DModel(this);
  115. oo.rotation().y += dx / 3;
  116. oo.rotation().x += dy / 3;
  117. _glSurfaceView.requestRender();
  118. break;
  119. }
  120. case MotionEvent.ACTION_DOWN:
  121. mIsMoving=true;
  122. break;
  123. case MotionEvent.ACTION_UP:
  124. case MotionEvent.ACTION_CANCEL:
  125. mIsMoving=false;
  126. mIsZooming=false;
  127. break;
  128. }
  129. mPreviousX = x;
  130. mPreviousY = y;
  131. }
  132. return true;
  133. }
  134. }