summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/citra/citra_emu/overlay/InputOverlayDrawableJoystick.java
blob: 956a8b1e901c1bd867a528486e72f862ffc96793 (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
/**
 * Copyright 2013 Dolphin Emulator Project
 * Licensed under GPLv2+
 * Refer to the license.txt file included.
 */

package org.citra.citra_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.citra.citra_emu.NativeLibrary.ButtonType;
import org.citra.citra_emu.utils.EmulationMenuSettings;

/**
 * Custom {@link BitmapDrawable} that is capable
 * of storing it's own ID.
 */
public final class InputOverlayDrawableJoystick {
    private final int[] axisIDs = {0, 0, 0, 0};
    private final float[] axises = {0f, 0f};
    private int trackId = -1;
    private int mJoystickType;
    private int mControlPositionX, mControlPositionY;
    private int mPreviousTouchX, mPreviousTouchY;
    private int mWidth;
    private int mHeight;
    private Rect mVirtBounds;
    private Rect mOrigBounds;
    private BitmapDrawable mOuterBitmap;
    private BitmapDrawable mDefaultStateInnerBitmap;
    private BitmapDrawable mPressedStateInnerBitmap;
    private BitmapDrawable mBoundsBoxBitmap;
    private boolean mPressedState = false;

    /**
     * Constructor
     *
     * @param res                {@link Resources} instance.
     * @param bitmapOuter        {@link Bitmap} which represents the outer non-movable part of the joystick.
     * @param bitmapInnerDefault {@link Bitmap} which represents the default inner movable part of the joystick.
     * @param bitmapInnerPressed {@link Bitmap} which represents the pressed inner movable part of the joystick.
     * @param rectOuter          {@link Rect} which represents the outer joystick bounds.
     * @param rectInner          {@link Rect} which represents the inner joystick bounds.
     * @param joystick           Identifier for which joystick this is.
     */
    public InputOverlayDrawableJoystick(Resources res, Bitmap bitmapOuter,
                                        Bitmap bitmapInnerDefault, Bitmap bitmapInnerPressed,
                                        Rect rectOuter, Rect rectInner, int joystick) {
        axisIDs[0] = joystick + 1; // Up
        axisIDs[1] = joystick + 2; // Down
        axisIDs[2] = joystick + 3; // Left
        axisIDs[3] = joystick + 4; // Right
        mJoystickType = joystick;

        mOuterBitmap = new BitmapDrawable(res, bitmapOuter);
        mDefaultStateInnerBitmap = new BitmapDrawable(res, bitmapInnerDefault);
        mPressedStateInnerBitmap = new BitmapDrawable(res, bitmapInnerPressed);
        mBoundsBoxBitmap = new BitmapDrawable(res, bitmapOuter);
        mWidth = bitmapOuter.getWidth();
        mHeight = bitmapOuter.getHeight();

        setBounds(rectOuter);
        mDefaultStateInnerBitmap.setBounds(rectInner);
        mPressedStateInnerBitmap.setBounds(rectInner);
        mVirtBounds = getBounds();
        mOrigBounds = mOuterBitmap.copyBounds();
        mBoundsBoxBitmap.setAlpha(0);
        mBoundsBoxBitmap.setBounds(getVirtBounds());
        SetInnerBounds();
    }

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

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

    public void TrackEvent(MotionEvent event) {
        int pointerIndex = event.getActionIndex();

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_POINTER_DOWN:
                if (getBounds().contains((int) event.getX(pointerIndex), (int) event.getY(pointerIndex))) {
                    mPressedState = true;
                    mOuterBitmap.setAlpha(0);
                    mBoundsBoxBitmap.setAlpha(255);
                    if (EmulationMenuSettings.getJoystickRelCenter()) {
                        getVirtBounds().offset((int) event.getX(pointerIndex) - getVirtBounds().centerX(),
                                (int) event.getY(pointerIndex) - getVirtBounds().centerY());
                    }
                    mBoundsBoxBitmap.setBounds(getVirtBounds());
                    trackId = event.getPointerId(pointerIndex);
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                if (trackId == event.getPointerId(pointerIndex)) {
                    mPressedState = false;
                    axises[0] = axises[1] = 0.0f;
                    mOuterBitmap.setAlpha(255);
                    mBoundsBoxBitmap.setAlpha(0);
                    setVirtBounds(new Rect(mOrigBounds.left, mOrigBounds.top, mOrigBounds.right,
                            mOrigBounds.bottom));
                    setBounds(new Rect(mOrigBounds.left, mOrigBounds.top, mOrigBounds.right,
                            mOrigBounds.bottom));
                    SetInnerBounds();
                    trackId = -1;
                }
                break;
        }

        if (trackId == -1)
            return;

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

                // Clamp the circle pad input to a circle
                final float angle = (float) Math.atan2(AxisY, AxisX);
                float radius = (float) Math.sqrt(AxisX * AxisX + AxisY * AxisY);
                if(radius > 1.0f)
                {
                    radius = 1.0f;
                }
                axises[0] = ((float)Math.cos(angle) * radius);
                axises[1] = ((float)Math.sin(angle) * radius);
                SetInnerBounds();
            }
        }
    }

    public boolean onConfigureTouch(MotionEvent event) {
        int pointerIndex = event.getActionIndex();
        int fingerPositionX = (int) event.getX(pointerIndex);
        int fingerPositionY = (int) event.getY(pointerIndex);

        int scale = 1;
        if (mJoystickType == ButtonType.STICK_C) {
            // C-stick is scaled down to be half the size of the circle pad
            scale = 2;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPreviousTouchX = fingerPositionX;
                mPreviousTouchY = fingerPositionY;
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaX = fingerPositionX - mPreviousTouchX;
                int deltaY = fingerPositionY - mPreviousTouchY;
                mControlPositionX += deltaX;
                mControlPositionY += deltaY;
                setBounds(new Rect(mControlPositionX, mControlPositionY,
                        mOuterBitmap.getIntrinsicWidth() / scale + mControlPositionX,
                        mOuterBitmap.getIntrinsicHeight() / scale + mControlPositionY));
                setVirtBounds(new Rect(mControlPositionX, mControlPositionY,
                        mOuterBitmap.getIntrinsicWidth() / scale + mControlPositionX,
                        mOuterBitmap.getIntrinsicHeight() / scale + mControlPositionY));
                SetInnerBounds();
                setOrigBounds(new Rect(new Rect(mControlPositionX, mControlPositionY,
                        mOuterBitmap.getIntrinsicWidth() / scale + mControlPositionX,
                        mOuterBitmap.getIntrinsicHeight() / scale + mControlPositionY)));
                mPreviousTouchX = fingerPositionX;
                mPreviousTouchY = fingerPositionY;
                break;
        }
        return true;
    }


    public float[] getAxisValues() {
        return axises;
    }

    public int[] getAxisIDs() {
        return axisIDs;
    }

    private void SetInnerBounds() {
        int X = getVirtBounds().centerX() + (int) ((axises[0]) * (getVirtBounds().width() / 2));
        int Y = getVirtBounds().centerY() + (int) ((axises[1]) * (getVirtBounds().height() / 2));

        if (mJoystickType == ButtonType.STICK_LEFT) {
            X += 1;
            Y += 1;
        }

        if (X > getVirtBounds().centerX() + (getVirtBounds().width() / 2))
            X = getVirtBounds().centerX() + (getVirtBounds().width() / 2);
        if (X < getVirtBounds().centerX() - (getVirtBounds().width() / 2))
            X = getVirtBounds().centerX() - (getVirtBounds().width() / 2);
        if (Y > getVirtBounds().centerY() + (getVirtBounds().height() / 2))
            Y = getVirtBounds().centerY() + (getVirtBounds().height() / 2);
        if (Y < getVirtBounds().centerY() - (getVirtBounds().height() / 2))
            Y = getVirtBounds().centerY() - (getVirtBounds().height() / 2);

        int width = mPressedStateInnerBitmap.getBounds().width() / 2;
        int height = mPressedStateInnerBitmap.getBounds().height() / 2;
        mDefaultStateInnerBitmap.setBounds(X - width, Y - height, X + width, Y + height);
        mPressedStateInnerBitmap.setBounds(mDefaultStateInnerBitmap.getBounds());
    }

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

    private BitmapDrawable getCurrentStateBitmapDrawable() {
        return mPressedState ? mPressedStateInnerBitmap : mDefaultStateInnerBitmap;
    }

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

    public void setBounds(Rect bounds) {
        mOuterBitmap.setBounds(bounds);
    }

    private void setOrigBounds(Rect bounds) {
        mOrigBounds = bounds;
    }

    private Rect getVirtBounds() {
        return mVirtBounds;
    }

    private void setVirtBounds(Rect bounds) {
        mVirtBounds = bounds;
    }

    public int getWidth() {
        return mWidth;
    }

    public int getHeight() {
        return mHeight;
    }
}