/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package br.odb.nehe.lesson10;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import java.io.IOException;

public class GL2JNIActivity extends Activity {

    GL2JNIView mView;
    boolean running = false;
    static AssetManager assets;

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        assets = getAssets();
        GL2JNILib.onCreate(assets);

        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(assets.open("mud.png"));
            GL2JNILib.setTexture(bitmap);
        } catch (IOException e) {
        }

        mView = new GL2JNIView(getApplication());
        setContentView(mView);
        mView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                GL2JNILib.onTouchNormalized( event.getX() / v.getWidth(), event.getY() / v.getHeight() );
                return true;
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        switch( keyCode ) {
            case KeyEvent.KEYCODE_T:
                return true;

            case KeyEvent.KEYCODE_DPAD_UP:
                GL2JNILib.moveForward();
                return true;

            case KeyEvent.KEYCODE_DPAD_RIGHT:
                GL2JNILib.turnRight();
                return true;

            case KeyEvent.KEYCODE_DPAD_DOWN:
                GL2JNILib.moveBackward();
                return true;

            case KeyEvent.KEYCODE_DPAD_LEFT:
                GL2JNILib.turnLeft();
                return true;

            case KeyEvent.KEYCODE_R:
                GL2JNILib.reset();
                return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        super.onPause();

        running = false;
        mView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mView.onResume();

        new Thread(new Runnable() {
            @Override
            public void run() {
                running = true;
                while (running) {
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    GL2JNILib.tick();
                }
            }
        }).start();
    }

    @Override
    protected void onDestroy() {

        GL2JNILib.onDestroy();

        super.onDestroy();
    }
}

@@ -1061,16 +1061,100 @@
  assets;
+%0A    private float fingerX;%0A    private float fingerY;%0A    private boolean touching;
 %0A%0A    @O
@@ -1760,65 +1760,401 @@
     
-GL2JNILib.onTouchNormalized( event.getX() / v.getWidth(),
+switch (event.getAction()) %7B%0A                    case MotionEvent.ACTION_UP:%0A                        touching = false;%0A                        return true;%0A                    case MotionEvent.ACTION_DOWN:%0A                        touching = true;%0A                    case MotionEvent.ACTION_MOVE:%0A                        fingerX = event.getX() / v.getWidth();%0A                        fingerY =
  eve
@@ -2182,12 +2182,18 @@
 ht()
- )
 ;%0A
+        
     
@@ -2209,32 +2209,117 @@
    return true;%0A
+                    default:%0A                        return false;%0A                %7D%0A
             %7D%0A  
@@ -2428,18 +2428,17 @@
 itch
-(
  
+(
 keyCode
- 
 ) %7B%0A
@@ -3531,32 +3531,32 @@
           try %7B%0A
-
                 
@@ -3581,16 +3581,158 @@
 ep(20);%0A
+                        if (touching) %7B%0A                            GL2JNILib.onTouchNormalized(fingerX, fingerY);%0A                        %7D%0A%0A
         

Improve touch controls on Lesson 10