-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
205a5b7
commit 27c9b0a
Showing
136 changed files
with
6,712 additions
and
2,478 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
android/app/src/main/java/com/classicube/CCMotionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.