টাচ ইভেন্টে প্রতিক্রিয়া জানানো

ঘূর্ণায়মান ত্রিভুজের মতো আগে থেকে সেট করা প্রোগ্রাম অনুযায়ী অবজেক্টকে ঘোরানো মনোযোগ আকর্ষণ করার জন্য উপযোগী, কিন্তু ব্যবহারকারীকে আপনার OpenGL ES গ্রাফিক্সের সাথে ইন্টার‍্যাক্ট করতে দিতে চাইলে কী করবেন? আপনার OpenGL ES অ্যাপ্লিকেশনকে টাচ ইন্টার‍্যাক্টিভ করার মূল চাবিকাঠি হল আপনার GLSurfaceView-এর ইমপ্লিমেন্টেশনকে প্রসারিত করা যাতে onTouchEvent()-কে ওভাররাইড করে টাচ ইভেন্ট শোনা যায়।

ব্যবহারকারী যাতে OpenGL ES অবজেক্ট ঘোরাতে পারেন তার জন্য টাচ ইভেন্ট কীভাবে শুনতে হয় তা এই পাঠে দেখানো হয়েছে।

টাচ লিসনার সেট-আপ করা

আপনার OpenGL ES অ্যাপ্লিকেশনকে টাচ ইভেন্টে রেসপন্ড করার জন্য, আপনাকে অবশ্যই GLSurfaceView ক্লাসে onTouchEvent() পদ্ধতি প্রয়োগ করতে হবে। নিচের উদাহরণে দেখানো হয়েছে কীভাবে MotionEvent.ACTION_MOVE ইভেন্ট শুনতে হয় এবং সেগুলিকে একটি শেপের জন্য ঘূর্ণনের কোণে অনুবাদ করতে হয়।

Kotlin

private const val TOUCH_SCALE_FACTOR: Float = 180.0f / 320f
...
private var previousX: Float = 0f
private var previousY: Float = 0f

override fun onTouchEvent(e: MotionEvent): Boolean {
    // MotionEvent reports input details from the touch screen
    // and other input controls. In this case, you are only
    // interested in events where the touch position changed.

    val x: Float = e.x
    val y: Float = e.y

    when (e.action) {
        MotionEvent.ACTION_MOVE -> {

            var dx: Float = x - previousX
            var dy: Float = y - previousY

            // reverse direction of rotation above the mid-line
            if (y > height / 2) {
                dx *= -1
            }

            // reverse direction of rotation to left of the mid-line
            if (x < width / 2) {
                dy *= -1
            }

            renderer.angle += (dx + dy) * TOUCH_SCALE_FACTOR
            requestRender()
        }
    }

    previousX = x
    previousY = y
    return true
}

জাভা

private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
private float previousX;
private float previousY;

@Override
public boolean onTouchEvent(MotionEvent e) {
    // MotionEvent reports input details from the touch screen
    // and other input controls. In this case, you are only
    // interested in events where the touch position changed.

    float x = e.getX();
    float y = e.getY();

    switch (e.getAction()) {
        case MotionEvent.ACTION_MOVE:

            float dx = x - previousX;
            float dy = y - previousY;

            // reverse direction of rotation above the mid-line
            if (y > getHeight() / 2) {
              dx = dx * -1 ;
            }

            // reverse direction of rotation to left of the mid-line
            if (x < getWidth() / 2) {
              dy = dy * -1 ;
            }

            renderer.setAngle(
                    renderer.getAngle() +
                    ((dx + dy) * TOUCH_SCALE_FACTOR));
            requestRender();
    }

    previousX = x;
    previousY = y;
    return true;
}

লক্ষ্য করুন যে রোটেশন অ্যাঙ্গেল গণনা করার পরে, এই মেথড requestRender()-কে কল করে রেন্ডারারকে জানায় যে ফ্রেম রেন্ডার করার সময় হয়ে গেছে। এই উদাহরণে এই পদ্ধতিটি সবচেয়ে কার্যকর কারণ রোটেশনে পরিবর্তন না হলে ফ্রেমটি আবার আঁকার প্রয়োজন হয় না। তবে, setRenderMode() পদ্ধতি ব্যবহার করে ডেটা পরিবর্তন হলে, রেন্ডারারকে শুধুমাত্র রিড্র করার অনুরোধ না করলে, এটি দক্ষতার উপর কোনও প্রভাব ফেলে না, তাই রেন্ডারারে এই লাইনটি আনকমেন্ট করা আছে কিনা তা নিশ্চিত করুন:

Kotlin

class MyGlSurfaceView(context: Context) : GLSurfaceView(context) {

    init {
        // Render the view only when there is a change in the drawing data
        renderMode = GLSurfaceView.RENDERMODE_WHEN_DIRTY
    }
}

জাভা

public MyGLSurfaceView(Context context) {
    ...
    // Render the view only when there is a change in the drawing data
    setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}

রোটেশন অ্যাঙ্গেল এক্সপোজ করুন

উপরের কোডের উদাহরণে, আপনাকে পাবলিক মেম্বার যোগ করে আপনার রেন্ডারারের মাধ্যমে রোটেশন অ্যাঙ্গেল দেখাতে হবে। যেহেতু রেন্ডারার কোড আপনার অ্যাপ্লিকেশনের মূল ইউজার ইন্টারফেস থ্রেড থেকে আলাদা থ্রেডে চলছে, তাই আপনাকে অবশ্যই এই পাবলিক ভেরিয়েবলকে volatile হিসেবে ঘোষণা করতে হবে। ভেরিয়েবেল ঘোষণা করা এবং গেটার ও সেটার পেয়ার এক্সপোজ করার কোড এখানে দেওয়া হল:

Kotlin

class MyGLRenderer4 : GLSurfaceView.Renderer {

    @Volatile
    var angle: Float = 0f
}

জাভা

public class MyGLRenderer implements GLSurfaceView.Renderer {
    ...

    public volatile float mAngle;

    public float getAngle() {
        return mAngle;
    }

    public void setAngle(float angle) {
        mAngle = angle;
    }
}

রোটেশন প্রয়োগ করুন

টাচ করে ইনপুট দেওয়ার পদ্ধতির মাধ্যমে জেনারেট করা রোটেশন প্রয়োগ করতে, কোণ জেনারেট করে এমন কোড কমেন্ট হিসেবে চিহ্নিত করুন এবং টাচ করে ইনপুট দেওয়ার পদ্ধতির মাধ্যমে জেনারেট করা কোণ আছে এমন ভেরিয়েবেল যোগ করুন:

Kotlin

override fun onDrawFrame(gl: GL10) {
    ...
    val scratch = FloatArray(16)

    // Create a rotation for the triangle
    // long time = SystemClock.uptimeMillis() % 4000L;
    // float angle = 0.090f * ((int) time);
    Matrix.setRotateM(rotationMatrix, 0, angle, 0f, 0f, -1.0f)

    // Combine the rotation matrix with the projection and camera view
    // Note that the mvpMatrix factor *must be first* in order
    // for the matrix multiplication product to be correct.
    Matrix.multiplyMM(scratch, 0, mvpMatrix, 0, rotationMatrix, 0)

    // Draw triangle
    triangle.draw(scratch)
}

জাভা

public void onDrawFrame(GL10 gl) {
    ...
    float[] scratch = new float[16];

    // Create a rotation for the triangle
    // long time = SystemClock.uptimeMillis() % 4000L;
    // float angle = 0.090f * ((int) time);
    Matrix.setRotateM(rotationMatrix, 0, mAngle, 0, 0, -1.0f);

    // Combine the rotation matrix with the projection and camera view
    // Note that the vPMatrix factor *must be first* in order
    // for the matrix multiplication product to be correct.
    Matrix.multiplyMM(scratch, 0, vPMatrix, 0, rotationMatrix, 0);

    // Draw triangle
    mTriangle.draw(scratch);
}

উপরে বর্ণনা করা ধাপগুলি সম্পূর্ণ করার পরে, প্রোগ্রামটি চালান এবং ত্রিভুজটি ঘোরাতে স্ক্রিনের উপর আপনার আঙুল টেনে আনুন:

ছবি ১. টাচ ইনপুটের মাধ্যমে ত্রিভুজটি ঘোরানো হচ্ছে (বৃত্তটি টাচ করার লোকেশন দেখাচ্ছে)।