Skip to content

Commit

Permalink
sync with master
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Nov 28, 2024
1 parent 205a5b7 commit 27c9b0a
Show file tree
Hide file tree
Showing 136 changed files with 6,712 additions and 2,478 deletions.
4 changes: 2 additions & 2 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<!-- BEGIN_INCLUDE(manifest) -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.classicube.android.client"
android:versionCode="1360"
android:versionName="1.3.6">
android:versionCode="1370"
android:versionName="1.3.7">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
Expand Down
57 changes: 57 additions & 0 deletions android/app/src/main/java/com/classicube/CCMotionListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.classicube;
import android.view.InputDevice;
import android.view.MotionEvent;
import android.view.View;

public class CCMotionListener implements View.OnGenericMotionListener {
MainActivity activity;

public CCMotionListener(MainActivity activity) {
this.activity = activity;
}

// https://developer.android.com/develop/ui/views/touch-and-input/game-controllers/controller-input#java
@Override
public boolean onGenericMotion(View view, MotionEvent event) {
if (event.getAction() != MotionEvent.ACTION_MOVE) return false;
boolean source_joystick = (event.getSource() & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK;
boolean source_gamepad = (event.getSource() & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD;

if (source_joystick || source_gamepad) {
int historySize = event.getHistorySize();
for (int i = 0; i < historySize; i++) {
processJoystickInput(event, i);
}

processJoystickInput(event, -1);
return true;
}
return false;
}

void processJoystickInput(MotionEvent event, int historyPos) {
float x1 = getAxisValue(event, MotionEvent.AXIS_X, historyPos);
float y1 = getAxisValue(event, MotionEvent.AXIS_Y, historyPos);

float x2 = getAxisValue(event, MotionEvent.AXIS_Z, historyPos);
float y2 = getAxisValue(event, MotionEvent.AXIS_RZ, historyPos);

if (x1 != 0 || y1 != 0)
pushAxisMovement(MainActivity.CMD_GPAD_AXISL, x1, y1);
if (x2 != 0 || y2 != 0)
pushAxisMovement(MainActivity.CMD_GPAD_AXISR, x2, y2);
}

float getAxisValue(MotionEvent event, int axis, int historyPos) {
float value = historyPos < 0 ? event.getAxisValue(axis) :
event.getHistoricalAxisValue(axis, historyPos);

// Deadzone detection
if (value >= -0.25f && value <= 0.25f) value = 0;
return value;
}

void pushAxisMovement(int axis, float x, float y) {
activity.pushCmd(axis, (int)(x * 4096), (int)(y * 4096));
}
}
108 changes: 108 additions & 0 deletions android/app/src/main/java/com/classicube/CCView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.classicube;
import android.text.Editable;
import android.text.Selection;
import android.text.SpannableStringBuilder;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;

public class CCView extends SurfaceView {
SpannableStringBuilder kbText;
MainActivity activity;

public CCView(MainActivity activity) {
// setFocusable, setFocusableInTouchMode - API level 1
super(activity);
this.activity = activity;
setFocusable(true);
setFocusableInTouchMode(true);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
return activity.handleTouchEvent(ev) || super.dispatchTouchEvent(ev);
}

@Override
public InputConnection onCreateInputConnection(EditorInfo attrs) {
// BaseInputConnection, IME_ACTION_GO, IME_FLAG_NO_EXTRACT_UI - API level 3
attrs.actionLabel = null;
attrs.inputType = MainActivity.calcKeyboardType(activity.keyboardType);
attrs.imeOptions = MainActivity.calcKeyboardOptions(activity.keyboardType);

kbText = new SpannableStringBuilder(activity.keyboardText);

InputConnection ic = new BaseInputConnection(this, true) {
boolean inited;

void updateText() {
activity.pushCmd(MainActivity.CMD_KEY_TEXT, kbText.toString());
}

@Override
public Editable getEditable() {
if (!inited) {
// needed to set selection, otherwise random crashes later with backspacing
// set selection to end, so backspacing after opening keyboard with text still works
Selection.setSelection(kbText, kbText.toString().length());
inited = true;
}
return kbText;
}

@Override
public boolean setComposingText(CharSequence text, int newCursorPosition) {
boolean success = super.setComposingText(text, newCursorPosition);
updateText();
return success;
}

@Override
public boolean deleteSurroundingText(int beforeLength, int afterLength) {
boolean success = super.deleteSurroundingText(beforeLength, afterLength);
updateText();
return success;
}

@Override
public boolean commitText(CharSequence text, int newCursorPosition) {
boolean success = super.commitText(text, newCursorPosition);
updateText();
return success;
}

@Override
public boolean sendKeyEvent(KeyEvent ev) {
// getSelectionStart - API level 1
if (ev.getAction() != KeyEvent.ACTION_DOWN) return super.sendKeyEvent(ev);
int code = ev.getKeyCode();
int uni = ev.getUnicodeChar();

// start is -1 sometimes, and trying to insert/delete there crashes
int start = Selection.getSelectionStart(kbText);
if (start == -1) start = kbText.toString().length();

if (code == KeyEvent.KEYCODE_ENTER) {
// enter maps to \n but that should not be intercepted
} else if (code == KeyEvent.KEYCODE_DEL) {
if (start <= 0) return false;
kbText.delete(start - 1, start);
updateText();
return false;
} else if (uni != 0) {
kbText.insert(start, String.valueOf((char) uni));
updateText();
return false;
}
return super.sendKeyEvent(ev);
}

};
//String text = MainActivity.this.keyboardText;
//if (text != null) ic.setComposingText(text, 0);
return ic;
}
}
Loading

0 comments on commit 27c9b0a

Please sign in to comment.