Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ android {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
testOptions {
unitTests.includeAndroidResources = true
unitTests.all {
jvmArgs '--add-opens=java.base/jdk.internal.access=ALL-UNNAMED'
}
}
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
Expand All @@ -71,6 +77,9 @@ android {

dependencies {

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.robolectric:robolectric:4.17'

implementation( platform 'org.jetbrains.kotlin:kotlin-bom:1.8.0')

implementation 'com.google.android.material:material:1.11.0'
Expand Down
118 changes: 33 additions & 85 deletions app/src/main/java/com/tgc/sky/GameActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ public class GameActivity extends TGCNativeActivity implements View.OnCapturedPo
private boolean m_motionEventsDisabled = false;
private int m_lastDpadDirection = 23;

// Device motion (accelerometer / gyroscope / rotation), forwarded to native.
// Sky 0.34.0 (401861) calls setMotionEnabled(boolean) via JNI; without it the
// native code aborts at startup with NoSuchMethodError setMotionEnabled(Z)V.
static final float INV_GRAVITY = 0.10197162f; // 1 / SensorManager.GRAVITY_EARTH (m/s^2 -> g)
static final float INV_GRAVITY = 0.10197162f;
private SensorManager m_sensorManager = null;
private Sensor m_gravitySensor = null;
private Sensor m_linearAccelSensor = null;
Expand Down Expand Up @@ -268,41 +265,37 @@ public void onCreate(Bundle bundle) {
}
Intent intent = getIntent();
if (intent != null) HandleNewIntent(intent);
getWindow().getDecorView().setOnApplyWindowInsetsListener((view, windowInsets) -> {
try {
int max = Integer.max(windowInsets.getStableInsetTop(), windowInsets.getStableInsetBottom());
if (Build.VERSION.SDK_INT >= 27) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if (windowInsets.getDisplayCutout() != null) {
max = Integer.max(max, Integer.max(
windowInsets.getDisplayCutout().getSafeInsetLeft(),
windowInsets.getDisplayCutout().getSafeInsetRight()));
}
getWindow().getDecorView().setOnApplyWindowInsetsListener(this::onApplyWindowInsets);
getWindow().getDecorView().requestApplyInsets();
if (Build.VERSION.SDK_INT >= 30) setupDisplayListener();
}

public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
try {
int max = Integer.max(windowInsets.getStableInsetTop(), windowInsets.getStableInsetBottom());
if (Build.VERSION.SDK_INT >= 27) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if (windowInsets.getDisplayCutout() != null) {
max = Integer.max(max, Integer.max(
windowInsets.getDisplayCutout().getSafeInsetLeft(),
windowInsets.getDisplayCutout().getSafeInsetRight()));
}
} catch (NoSuchMethodError ignored) {}
}
GameActivity.this.mSafeAreaInsets.left = max;
GameActivity.this.mSafeAreaInsets.top = 0;
GameActivity.this.mSafeAreaInsets.right = max;
GameActivity.this.mSafeAreaInsets.bottom = 0;
float t = GameActivity.this.transformWidthToProgram(max);
GameActivity.this.onSafeAreaInsetsChanged(new float[]{t, 0.0f, t, 0.0f});
return view.onApplyWindowInsets(windowInsets);
} catch (Exception | NoSuchMethodError unused) {
return windowInsets;
}
} catch (NoSuchMethodError ignored) {}
}
});
if (Build.VERSION.SDK_INT >= 30) setupDisplayListener();
mSafeAreaInsets.left = max;
mSafeAreaInsets.top = 0;
mSafeAreaInsets.right = max;
mSafeAreaInsets.bottom = 0;
float t = transformWidthToProgram(max);
onSafeAreaInsetsChanged(new float[]{t, 0.0f, t, 0.0f});
} catch (Exception | NoSuchMethodError ignored) {}

handleKeyboardInsets(view, windowInsets);
return view.onApplyWindowInsets(windowInsets);
}

// Parity with stock Sky 0.34.5 (410941): the captured-pointer callback
// exists ONLY for Sony gamepad touchpads (button taps forwarded as key
// 0x6d at inputSource 1; touchpad MOTION is dropped, as in stock). ANY
// other captured device - a hardware mouse above all - releases capture
// immediately, so a mouse can never get stuck in relative mode: captured
// input bypasses the whole view tree, which would leave it click-dead
// and delta-driven for as long as capture held.
@Override
public boolean onCapturedPointer(View view, MotionEvent event) {
if (isGamepadWithTouchpadEvent(event)) {
Expand Down Expand Up @@ -368,12 +361,6 @@ public void onPause() {
super.onPause();
}

// ==== Device motion sensors (Sky 0.34.0 / 401861) ====================
// Restores GameActivity.setMotionEnabled(boolean) and its sensor subsystem.
// The game's native code calls setMotionEnabled(boolean) via JNI every frame;
// without it startup aborts with:
// NoSuchMethodError: com.tgc.sky.GameActivity.setMotionEnabled(Z)V
// Ported to match TGC's own GameActivity so native behavior is identical.

private static final class MotionSample {
final float gravityX, gravityY, gravityZ;
Expand All @@ -399,18 +386,13 @@ private void setupMotionSensors() {
if (this.m_sensorManager != null) {
this.m_gravitySensor = this.m_sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
this.m_linearAccelSensor = this.m_sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
// NOTE: TGC registers GAME_ROTATION_VECTOR here but onSensorChanged
// matches ROTATION_VECTOR (below), so in the official app the
// quaternion stays identity. Kept identical to match native behavior.
this.m_rotationVectorSensor = this.m_sensorManager.getDefaultSensor(Sensor.TYPE_GAME_ROTATION_VECTOR);
this.m_gyroscopeSensor = this.m_sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
}
this.m_motionAvailable = this.m_gravitySensor != null && this.m_linearAccelSensor != null
&& this.m_rotationVectorSensor != null && this.m_gyroscopeSensor != null;
}

// Called by native every frame to enable/disable device-motion streaming and
// to pull the latest sample. Must match the JNI signature setMotionEnabled(Z)V.
public void setMotionEnabled(boolean enabled) {
onMotionAvailabilityNative(this.m_motionAvailable);
if (!this.m_motionAvailable) return;
Expand Down Expand Up @@ -486,9 +468,6 @@ private void publishMotionSample() {
float outGravityX, outGravityY, outAccelX, outAccelY, outRotX, outRotY;
float outQuatX, outQuatY, outQuatZ, outQuatW;
if (isNaturalLandscape()) {
// Remap device frame -> game's natural-landscape frame: (x, y) -> (y, -x)
// for the vectors, and rotate the orientation quaternion by 45 degrees
// (0.70710677 = cos 45deg = sqrt(1/2)).
final float c = 0.70710677f;
outGravityX = gy; outGravityY = -gx;
outAccelX = ay; outAccelY = -ax;
Expand Down Expand Up @@ -532,14 +511,14 @@ private int getMotionDeviceOrientation() {
case Surface.ROTATION_90: return 2;
case Surface.ROTATION_180: return 4;
case Surface.ROTATION_270: return 1;
default: return 3; // ROTATION_0
default: return 3;
}
}
switch (rotation) {
case Surface.ROTATION_90: return 3;
case Surface.ROTATION_180: return 2;
case Surface.ROTATION_270: return 4;
default: return 1; // ROTATION_0
default: return 1;
}
}

Expand Down Expand Up @@ -713,11 +692,6 @@ public boolean onTouchEvent(MotionEvent motionEvent) {
motionEvent.getY(actionIndex) + m_gameInputOffset[1]);
}

// Hardware-mouse input for the ImGui overlay (no stock counterpart -
// stock has no overlay): feed the pointer exactly the way onTouchEvent
// feeds touches - bridge-view-relative coordinates into the same
// submitPositionEvent path, with ImGUI.wantsMouse() deciding whether the
// game sees the event at all.
private void submitImGuiMousePosition(MotionEvent motionEvent) {
float x = motionEvent.getX();
float y = motionEvent.getY();
Expand Down Expand Up @@ -760,11 +734,6 @@ public boolean dispatchTouchEvent(MotionEvent motionEvent) {
PointF point = transformPointToProgram(
motionEvent.getX() + m_gameInputOffset[0],
motionEvent.getY() + m_gameInputOffset[1]);
// Mouse-to-ImGui: primary-button gestures mirror the touch path
// - position plus button 0 - and wantsMouse() then arbitrates
// ownership just like onTouchEvent does for touches. The
// backend's MouseDownOwned logic keeps a drag that began on the
// game out of ImGui and vice versa.
int actionMasked = motionEvent.getActionMasked();
if (actionMasked == MotionEvent.ACTION_DOWN
|| actionMasked == MotionEvent.ACTION_MOVE
Expand All @@ -774,10 +743,6 @@ public boolean dispatchTouchEvent(MotionEvent motionEvent) {
if (actionMasked == MotionEvent.ACTION_UP) ImGUI.submitButtonEvent(0, false);
}
if (ImGUI.wantsMouse()) {
// ImGui owns this gesture: the game sees neither the camera
// delta nor the cursor move. Still advance the delta anchor
// so the first game-owned event afterwards computes no
// spurious camera jump.
m_lastMouseLocation = point;
return true;
}
Expand Down Expand Up @@ -842,7 +807,10 @@ protected void toggleKeyboard(boolean z, int i) {
lbm.sendBroadcast(intent);
}

@Override
public void onGlobalLayout() {
super.onGlobalLayout();
if (Build.VERSION.SDK_INT >= 30 || this.m_relativeLayout == null) return;
Rect rect = new Rect();
this.m_relativeLayout.getWindowVisibleDisplayFrame(rect);
int height = this.m_relativeLayout.getHeight() - rect.height();
Expand Down Expand Up @@ -1070,12 +1038,6 @@ public boolean onKeyUp(int keyCode, KeyEvent keyEvent) {
public boolean onGenericMotionEvent(MotionEvent motionEvent) {
if (this.m_motionEventsDisabled) return true;
if (isHardwareMouseEvent(motionEvent)) {
// Hardware-mouse dispatch, parity with stock Sky 0.34.5
// (410941): stock dispatches on the action and forwards mouse
// BUTTONS to the game (onButtonPress at inputSource 3) and one
// synthesized key tap per wheel notch, alongside hover and the
// scroll delta. Dropping the button cases means no
// hardware-mouse click ever reaches the game.
int vendorId = getDeviceVendorId(motionEvent);
int productId = getDeviceProductId(motionEvent);
if (isGamepadWithTouchpadEvent(motionEvent)) {
Expand All @@ -1085,12 +1047,6 @@ public boolean onGenericMotionEvent(MotionEvent motionEvent) {
}
return true;
}
// Mouse-to-ImGui: keep the overlay's pointer position fresh - it
// powers ImGui hover and the wantsMouse() ownership checks
// below. Button 0 itself is submitted from dispatchTouchEvent's
// DOWN/UP, which Android delivers alongside
// ACTION_BUTTON_PRESS/RELEASE for the primary button, so it is
// not repeated here.
submitImGuiMousePosition(motionEvent);
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_HOVER_MOVE:
Expand All @@ -1105,13 +1061,9 @@ public boolean onGenericMotionEvent(MotionEvent motionEvent) {
float scrollX = motionEvent.getAxisValue(MotionEvent.AXIS_HSCROLL);
float scrollY = motionEvent.getAxisValue(MotionEvent.AXIS_VSCROLL);
if (ImGUI.wantsMouse()) {
// Over the overlay the wheel scrolls ImGui and never
// reaches the game - no camera zoom behind the menu.
ImGUI.submitScrollEvent(scrollX, scrollY);
break;
}
// One synthesized key tap per notch, exactly as stock:
// wheel down -> key 11, wheel up -> key 10, inputSource 3.
int key = scrollY < 0.0f ? 11 : (scrollY > 0.0f ? 10 : 0);
if (key != 0) {
onButtonPress(key, 3, true, vendorId, productId);
Expand All @@ -1121,10 +1073,6 @@ public boolean onGenericMotionEvent(MotionEvent motionEvent) {
break;
}
case MotionEvent.ACTION_BUTTON_PRESS:
// wantsMouse() keeps clicks over the overlay away from
// the game; a press that began on the game keeps its
// release too (MouseDownOwned makes wantsMouse() stay
// false for that whole drag, even ending over ImGui).
if (!ImGUI.wantsMouse() && !isEventInTextField(motionEvent)) {
onButtonPress(motionEvent.getActionButton(), 3, true, vendorId, productId);
}
Expand Down
32 changes: 18 additions & 14 deletions app/src/main/java/com/tgc/sky/ui/TextField.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class TextField {
private int m_selectPosProgram = -1;

private boolean m_submitted = false;
private boolean m_hiding = false;
private State m_state = State.kTextFieldState_Hidden;
private boolean m_init = false;

Expand Down Expand Up @@ -149,17 +150,11 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
this.m_activity.getBridgeView().addView(this.m_textField);

this.m_activity.addOnKeyboardListener((visible, kbHeight) -> {
if (visible) {
TextField.this.resizeTextField(false, kbHeight);
return;
TextField.this.resizeTextField(false, kbHeight);
if (!visible && TextField.this.isVirtualKeyboard()
&& TextField.this.getState() == State.kTextFieldState_Showing) {
TextField.this.hideTextField();
}
if (!TextField.this.m_submitted) {
if (TextField.this.m_isDraftEnabled) {
TextField.sChatDraft = TextField.this.m_textField.getText().toString();
}
TextField.this.m_activity.onKeyboardCompleteNative("", TextField.this.m_isCallbackTextfield, true);
}
TextField.this.hideTextField();
});

this.m_init = true;
Expand Down Expand Up @@ -222,10 +217,17 @@ public void hideTextFieldAsync() {
}

public void hideTextField() {
if (!this.m_submitted) {
this.m_activity.onKeyboardCompleteNative("", this.m_isCallbackTextfield, true);
if (getState() == State.kTextFieldState_Hidden || this.m_hiding) {
return;
}
if (getState() != State.kTextFieldState_Hidden) {
this.m_hiding = true;
try {
if (!this.m_submitted) {
if (this.m_isDraftEnabled) {
sChatDraft = this.m_textField.getText().toString();
}
this.m_activity.onKeyboardCompleteNative("", this.m_isCallbackTextfield, true);
}
((InputMethodManager) this.m_activity.getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(this.m_textField.getWindowToken(), 0);
GameActivity.hideNavigationFullScreen(this.m_activity.getBridgeView());
Expand All @@ -238,9 +240,11 @@ public void hideTextField() {
this.m_textField.setVisibility(View.GONE);
this.m_activity.getBridgeView().requestFocus();
this.m_activity.getBridgeView().requestFocusFromTouch();
clearId();
setState(State.kTextFieldState_Hidden);
} finally {
this.m_hiding = false;
}
clearId();
}

public void resizeTextField(boolean top, int keyboardHeight) {
Expand Down
Loading
Loading