Project

General

Profile

« Previous | Next » 

Revision 643844c9

Added by Leszek Koltunski about 7 years ago

Improve Around the World.

View differences:

src/main/java/org/distorted/examples/aroundtheworld/AroundTheWorldActivity.java
32 32
///////////////////////////////////////////////////////////////////////////////////////////////////
33 33
    
34 34
    @Override
35
    protected void onCreate(Bundle icicle) 
35
    protected void onCreate(Bundle savedState)
36 36
      {
37
      super.onCreate(icicle);
37
      super.onCreate(savedState);
38 38
      setContentView(R.layout.aroundtheworldlayout);
39 39

  
40 40
      AroundTheWorldSurfaceView       view = (AroundTheWorldSurfaceView)       this.findViewById(R.id.aroundTheWorldSurfaceView);
......
42 42
      AroundTheWorldEffectsManager    mana = view.getManager();
43 43
      AroundTheWorldRendererPicker    renp = pick.getRenderer();
44 44

  
45
      renp.move(0,0);
46
      mana.setRace(0.33f,0.33f);
45
      if( savedState==null )
46
        {
47
        renp.move(0, 0);
48
        mana.setRace(0.33f, 0.33f);
49
        }
47 50

  
48 51
      pick.setEffectsManager(mana);
49 52
      }
......
78 81
      Distorted.onDestroy();  
79 82
      super.onDestroy();
80 83
      }
84

  
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

  
87
    @Override
88
    public void onSaveInstanceState(Bundle savedInstanceState)
89
      {
90
      super.onSaveInstanceState(savedInstanceState);
91

  
92
      AroundTheWorldSurfaceViewPicker pick = (AroundTheWorldSurfaceViewPicker) this.findViewById(R.id.aroundTheWorldSurfaceViewPicker);
93

  
94
      savedInstanceState.putFloat("x", pick.getx() );
95
      savedInstanceState.putFloat("y", pick.gety() );
96
      }
97

  
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

  
100
    @Override
101
    public void onRestoreInstanceState(Bundle savedInstanceState)
102
      {
103
      super.onRestoreInstanceState(savedInstanceState);
104

  
105
      AroundTheWorldSurfaceViewPicker pick = (AroundTheWorldSurfaceViewPicker) this.findViewById(R.id.aroundTheWorldSurfaceViewPicker);
106

  
107
      float x = savedInstanceState.getFloat("x");
108
      float y = savedInstanceState.getFloat("y");
109

  
110
      pick.set(x,y);
111
      }
112

  
81 113
}
src/main/java/org/distorted/examples/aroundtheworld/AroundTheWorldSurfaceViewPicker.java
33 33
    private AroundTheWorldRendererPicker mRenderer;
34 34
    private AroundTheWorldEffectsManager mManager;
35 35

  
36
    private float mX, mY;
37

  
36 38
///////////////////////////////////////////////////////////////////////////////////////////////////
37 39

  
38 40
    public AroundTheWorldSurfaceViewPicker(Context context, AttributeSet attrs)
......
66 68

  
67 69
///////////////////////////////////////////////////////////////////////////////////////////////////
68 70

  
71
    float getx()
72
      {
73
      return mX;
74
      }
75

  
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

  
78
    float gety()
79
      {
80
      return mY;
81
      }
82

  
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

  
85
    void set(float x, float y)
86
      {
87
      mX = x;
88
      mY = y;
89

  
90
      mRenderer.move(mX,mY);
91

  
92
      float distance_from_left =(mX+0.865f)/(3*0.865f);
93
      float distance_from_top  =0.333f*(1-mY)-0.192f*mX;
94

  
95
      mManager.setRace( distance_from_top, distance_from_left );
96
      }
97

  
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

  
100

  
69 101
    @Override public boolean onTouchEvent(MotionEvent event)
70 102
      {
71 103
      int x = (int)event.getX();
......
74 106
      int h = mRenderer.getHeight();
75 107
      int w = mRenderer.getWidth();
76 108

  
77
      float xf = (x-w/2)*3.0f/h;
78
      float yf =-(y-h/2)*3.0f/h;
109
      mX = (x-w/2)*3.0f/h;
110
      mY =-(y-h/2)*3.0f/h;
79 111

  
80
      if( yf>-1.5f && yf<1.5f )
112
      if( mY>-1.5f && mY<1.5f )
81 113
        {
82
        if( yf>=0 && yf> 1-0.577f*xf )
114
        if( mY>=0 && mY> 1-0.577f*mX )
83 115
          {
84
          float d = 0.577f*xf + yf;
85
          yf /= d;
86
          xf /= d;
116
          float d = 0.577f*mX + mY;
117
          mY /= d;
118
          mX /= d;
87 119
          }
88
        else if( yf<=0 && yf< 0.577f*xf-1 )
120
        else if( mY<=0 && mY< 0.577f*mX-1 )
89 121
          {
90
          float d = 0.577f*xf - yf;
91
          yf /= d;
92
          xf /= d;
122
          float d = 0.577f*mX - mY;
123
          mY /= d;
124
          mX /= d;
93 125
          }
94
        else if( xf< -0.865f )
126
        else if( mX< -0.865f )
95 127
          {
96
          xf  =  -0.865f;
128
          mX  =  -0.865f;
97 129
          }
98 130

  
99
        mRenderer.move(xf,yf);
100

  
101
        float distance_from_left =(xf+0.865f)/(3*0.865f);
102
        float distance_from_top  =0.333f*(1-yf)-0.192f*xf;
103

  
104
        mManager.setRace( distance_from_top, distance_from_left );
131
        set(mX,mY);
105 132
        }
106 133

  
107 134
      return true;

Also available in: Unified diff