summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlayDrawableDpad.java
blob: d8ee6895ba8abe1bfa90271330ffd2cf3065ddff (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
 * Copyright 2016 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 InputOverlayDrawableDpad {
    public static final float VIRT_AXIS_DEADZONE = 0.5f;
    // The ID identifying what type of button this Drawable represents.
    private int mUpButtonId;
    private int mDownButtonId;
    private int mLeftButtonId;
    private int mRightButtonId;
    private int mTrackId;
    private int mControlPositionX, mControlPositionY;
    private int mWidth;
    private int mHeight;
    private BitmapDrawable mDefaultStateBitmap;
    private BitmapDrawable mPressedOneDirectionStateBitmap;
    private BitmapDrawable mPressedTwoDirectionsStateBitmap;
    private boolean mUpButtonState;
    private boolean mDownButtonState;
    private boolean mLeftButtonState;
    private boolean mRightButtonState;

    /**
     * Constructor
     *
     * @param res                             {@link Resources} instance.
     * @param defaultStateBitmap              {@link Bitmap} of the default state.
     * @param pressedOneDirectionStateBitmap  {@link Bitmap} of the pressed state in one direction.
     * @param pressedTwoDirectionsStateBitmap {@link Bitmap} of the pressed state in two direction.
     * @param buttonUp                        Identifier for the up button.
     * @param buttonDown                      Identifier for the down button.
     * @param buttonLeft                      Identifier for the left button.
     * @param buttonRight                     Identifier for the right button.
     */
    public InputOverlayDrawableDpad(Resources res,
                                    Bitmap defaultStateBitmap,
                                    Bitmap pressedOneDirectionStateBitmap,
                                    Bitmap pressedTwoDirectionsStateBitmap,
                                    int buttonUp, int buttonDown,
                                    int buttonLeft, int buttonRight) {
        mDefaultStateBitmap = new BitmapDrawable(res, defaultStateBitmap);
        mPressedOneDirectionStateBitmap = new BitmapDrawable(res, pressedOneDirectionStateBitmap);
        mPressedTwoDirectionsStateBitmap = new BitmapDrawable(res, pressedTwoDirectionsStateBitmap);

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

        mUpButtonId = buttonUp;
        mDownButtonId = buttonDown;
        mLeftButtonId = buttonLeft;
        mRightButtonId = buttonRight;

        mTrackId = -1;
    }

    public boolean updateStatus(MotionEvent event, boolean dpad_slide) {
        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;

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

        if (isActionUp) {
            if (mTrackId != pointerId) {
                return false;
            }
            mTrackId = -1;
            mUpButtonState = false;
            mDownButtonState = false;
            mLeftButtonState = false;
            mRightButtonState = false;
            return true;
        }

        if (mTrackId == -1) {
            return false;
        }

        if (!dpad_slide && !isActionDown) {
            return false;
        }

        for (int i = 0; i < event.getPointerCount(); i++) {
            if (mTrackId != event.getPointerId(i)) {
                continue;
            }
            float touchX = event.getX(i);
            float touchY = event.getY(i);
            float maxY = getBounds().bottom;
            float maxX = getBounds().right;
            touchX -= getBounds().centerX();
            maxX -= getBounds().centerX();
            touchY -= getBounds().centerY();
            maxY -= getBounds().centerY();
            final float AxisX = touchX / maxX;
            final float AxisY = touchY / maxY;

            mUpButtonState = AxisY < -InputOverlayDrawableDpad.VIRT_AXIS_DEADZONE;
            mDownButtonState = AxisY > InputOverlayDrawableDpad.VIRT_AXIS_DEADZONE;
            mLeftButtonState = AxisX < -InputOverlayDrawableDpad.VIRT_AXIS_DEADZONE;
            mRightButtonState = AxisX > InputOverlayDrawableDpad.VIRT_AXIS_DEADZONE;
            return true;
        }

        return false;
    }

    public void draw(Canvas canvas) {
        int px = mControlPositionX + (getWidth() / 2);
        int py = mControlPositionY + (getHeight() / 2);

        // Pressed up
        if (mUpButtonState && !mLeftButtonState && !mRightButtonState) {
            mPressedOneDirectionStateBitmap.draw(canvas);
            return;
        }

        // Pressed down
        if (mDownButtonState && !mLeftButtonState && !mRightButtonState) {
            canvas.save();
            canvas.rotate(180, px, py);
            mPressedOneDirectionStateBitmap.draw(canvas);
            canvas.restore();
            return;
        }

        // Pressed left
        if (mLeftButtonState && !mUpButtonState && !mDownButtonState) {
            canvas.save();
            canvas.rotate(270, px, py);
            mPressedOneDirectionStateBitmap.draw(canvas);
            canvas.restore();
            return;
        }

        // Pressed right
        if (mRightButtonState && !mUpButtonState && !mDownButtonState) {
            canvas.save();
            canvas.rotate(90, px, py);
            mPressedOneDirectionStateBitmap.draw(canvas);
            canvas.restore();
            return;
        }

        // Pressed up left
        if (mUpButtonState && mLeftButtonState && !mRightButtonState) {
            mPressedTwoDirectionsStateBitmap.draw(canvas);
            return;
        }

        // Pressed up right
        if (mUpButtonState && !mLeftButtonState && mRightButtonState) {
            canvas.save();
            canvas.rotate(180, px, py);
            mPressedTwoDirectionsStateBitmap.draw(canvas);
            canvas.restore();
            return;
        }

        // Pressed down left
        if (mDownButtonState && mLeftButtonState && !mRightButtonState) {
            canvas.save();
            canvas.rotate(270, px, py);
            mPressedTwoDirectionsStateBitmap.draw(canvas);
            canvas.restore();
            return;
        }

        // Pressed down right
        if (mDownButtonState && !mLeftButtonState && mRightButtonState) {
            canvas.save();
            canvas.rotate(180, px, py);
            mPressedTwoDirectionsStateBitmap.draw(canvas);
            canvas.restore();
            return;
        }

        // Not pressed
        mDefaultStateBitmap.draw(canvas);
    }

    /**
     * Gets one of the InputOverlayDrawableDpad's button IDs.
     *
     * @return the requested InputOverlayDrawableDpad's button ID.
     */
    public int getUpId() {
        return mUpButtonId;
    }

    public int getDownId() {
        return mDownButtonId;
    }

    public int getLeftId() {
        return mLeftButtonId;
    }

    public int getRightId() {
        return mRightButtonId;
    }

    public int getTrackId() {
        return mTrackId;
    }

    public int getUpStatus() {
        return mUpButtonState ? ButtonState.PRESSED : ButtonState.RELEASED;
    }

    public int getDownStatus() {
        return mDownButtonState ? ButtonState.PRESSED : ButtonState.RELEASED;
    }

    public int getLeftStatus() {
        return mLeftButtonState ? ButtonState.PRESSED : ButtonState.RELEASED;
    }

    public int getRightStatus() {
        return mRightButtonState ? ButtonState.PRESSED : ButtonState.RELEASED;
    }

    public void setPosition(int x, int y) {
        mControlPositionX = x;
        mControlPositionY = y;
    }

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

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

    public int getWidth() {
        return mWidth;
    }

    public int getHeight() {
        return mHeight;
    }
}