summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableButton.java
blob: 16a4b96ad07d2ef20ce0b58222b9325b565fd1b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
 * Copyright 2013 Dolphin Emulator Project
 * Licensed under GPLv2+
 * Refer to the license.txt file included.
 */

package org.yuzu.yuzu_emu.overlay;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.view.MotionEvent;

import org.yuzu.yuzu_emu.NativeLibrary.ButtonState;


/**
 * Custom {@link BitmapDrawable} that is capable
 * of storing it's own ID.
 */
public final class InputOverlayDrawableButton {
    // The ID value what type of button this Drawable represents.
    private int mButtonId;

    // The ID value what motion event is tracking
    private int mTrackId;

    // The drawable position on the screen
    private int mButtonPositionX, mButtonPositionY;
    private int mWidth;
    private int mHeight;
    private BitmapDrawable mDefaultStateBitmap;
    private BitmapDrawable mPressedStateBitmap;
    private boolean mPressedState = false;

    /**
     * Constructor
     *
     * @param res                {@link Resources} instance.
     * @param defaultStateBitmap {@link Bitmap} to use with the default state Drawable.
     * @param pressedStateBitmap {@link Bitmap} to use with the pressed state Drawable.
     * @param buttonId           Identifier for this type of button.
     */
    public InputOverlayDrawableButton(Resources res, Bitmap defaultStateBitmap,
                                      Bitmap pressedStateBitmap, int buttonId) {
        mDefaultStateBitmap = new BitmapDrawable(res, defaultStateBitmap);
        mPressedStateBitmap = new BitmapDrawable(res, pressedStateBitmap);
        mButtonId = buttonId;
        mTrackId = -1;

        mWidth = mDefaultStateBitmap.getIntrinsicWidth();
        mHeight = mDefaultStateBitmap.getIntrinsicHeight();
    }

    /**
     * Updates button status based on the motion event.
     *
     * @return true if value was changed
     */
    public boolean updateStatus(MotionEvent event) {
        int pointerIndex = event.getActionIndex();
        int xPosition = (int) event.getX(pointerIndex);
        int yPosition = (int) event.getY(pointerIndex);
        int pointerId = event.getPointerId(pointerIndex);
        int motion_event = event.getAction() & MotionEvent.ACTION_MASK;
        boolean isActionDown = motion_event == MotionEvent.ACTION_DOWN || motion_event == MotionEvent.ACTION_POINTER_DOWN;
        boolean isActionUp = motion_event == MotionEvent.ACTION_UP || motion_event == MotionEvent.ACTION_POINTER_UP;
        boolean current_state = mPressedState;

        if (isActionDown) {
            if (!getBounds().contains(xPosition, yPosition)) {
                return false;
            }
            mPressedState = true;
            mTrackId = pointerId;
            return true;
        }

        if (isActionUp) {
            if (mTrackId != pointerId) {
                return false;
            }
            mPressedState = false;
            mTrackId = -1;
            return true;
        }

        return false;
    }

    public void setPosition(int x, int y) {
        mButtonPositionX = x;
        mButtonPositionY = y;
    }

    public void draw(Canvas canvas) {
        getCurrentStateBitmapDrawable().draw(canvas);
    }

    private BitmapDrawable getCurrentStateBitmapDrawable() {
        return mPressedState ? mPressedStateBitmap : mDefaultStateBitmap;
    }

    public void setBounds(int left, int top, int right, int bottom) {
        mDefaultStateBitmap.setBounds(left, top, right, bottom);
        mPressedStateBitmap.setBounds(left, top, right, bottom);
    }

    /**
     * Gets this InputOverlayDrawableButton's button ID.
     *
     * @return this InputOverlayDrawableButton's button ID.
     */
    public int getId() {
        return mButtonId;
    }

    public int getTrackId() {
        return mTrackId;
    }

    public int getStatus() {
        return mPressedState ? ButtonState.PRESSED : ButtonState.RELEASED;
    }

    private Rect getBounds() {
        return mDefaultStateBitmap.getBounds();
    }

    public int getWidth() {
        return mWidth;
    }

    public int getHeight() {
        return mHeight;
    }
}