Skip to content

Commit ceecc41

Browse files
author
terrabyte25
committed
Finally reimplement touch control as an option
1 parent 2b132f9 commit ceecc41

File tree

6 files changed

+110
-66
lines changed

6 files changed

+110
-66
lines changed

app/src/main/java/cursor/MouseCursor.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.content.Context;
44
import android.content.res.Resources;
55
import android.content.SharedPreferences;
6+
import android.preference.PreferenceManager;
67
import android.util.TypedValue;
78
import android.view.Choreographer;
89
import android.view.View;
@@ -25,12 +26,18 @@ public class MouseCursor implements Choreographer.FrameCallback {
2526

2627
private SharedPreferences Settings;
2728

29+
private boolean touchcontrol = false;
30+
2831
public MouseCursor(GameActivity activity) {
2932
Settings = activity.getSharedPreferences(
3033
Constants.APP_PREFERENCES, Context.MODE_PRIVATE);
34+
35+
touchcontrol = PreferenceManager.getDefaultSharedPreferences(activity).getBoolean("touchControl", false);
3136

3237
cursor = new ImageView(activity);
33-
cursor.setImageResource(R.drawable.pointer_arrow);
38+
if (!touchcontrol)
39+
cursor.setImageResource(R.drawable.pointer_arrow);
40+
3441
Resources r = activity.getResources();
3542
int px = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, r.getDisplayMetrics());
3643
cursor.setLayoutParams(new RelativeLayout.LayoutParams((int) Math.round(px / 1.5), px));
@@ -43,16 +50,22 @@ public MouseCursor(GameActivity activity) {
4350

4451
float alpha = Settings.getFloat(Constants.MOUSE_TRANSPARENCY, 100.0f);
4552

46-
cursor.setAlpha((alpha / 100.0f)); // currently deprecated
53+
cursor.setAlpha((alpha / 100.0f));
4754

4855
}
4956

5057
@Override
5158
public void doFrame(long frameTimeNanos) {
5259
if (SDLActivity.isMouseShown() == 0) {
53-
cursor.setVisibility(View.GONE);
60+
if (!touchcontrol)
61+
cursor.setVisibility(View.GONE);
62+
else
63+
GameActivity.osc.disableElements(false);
5464
} else {
55-
cursor.setVisibility(View.VISIBLE);
65+
if (!touchcontrol)
66+
cursor.setVisibility(View.VISIBLE);
67+
else
68+
GameActivity.osc.disableElements(true);
5669

5770
View surface = SDLActivity.getSurface();
5871

app/src/main/java/org/libsdl/app/SDLActivity.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.app.*;
99
import android.content.*;
1010
import android.content.res.Configuration;
11+
import android.preference.PreferenceManager;
1112
import android.text.InputType;
1213
import android.view.*;
1314
import android.view.inputmethod.BaseInputConnection;
@@ -1155,7 +1156,8 @@ public SDLSurface(Context context) {
11551156
setFocusableInTouchMode(true);
11561157
requestFocus();
11571158
setOnKeyListener(this);
1158-
// setOnTouchListener(this);
1159+
if (PreferenceManager.getDefaultSharedPreferences(SDL.getContext()).getBoolean("touchControl", false))
1160+
setOnTouchListener(this);
11591161

11601162
mDisplay = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
11611163
mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
@@ -1200,7 +1202,8 @@ public void handleResume() {
12001202
setFocusableInTouchMode(true);
12011203
requestFocus();
12021204
setOnKeyListener(this);
1203-
// setOnTouchListener(this);
1205+
if (PreferenceManager.getDefaultSharedPreferences(SDL.getContext()).getBoolean("touchControl", false))
1206+
setOnTouchListener(this);
12041207
enableSensor(Sensor.TYPE_ACCELEROMETER, true);
12051208
}
12061209

app/src/main/java/ui/activity/GameActivity.java

Lines changed: 69 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ public class GameActivity extends SDLActivity {
4343

4444
public static native void commandLine(int argc, String[] argv);
4545

46+
public static Osc osc;
47+
4648
private boolean hideControls = false;
49+
private boolean touchControls = false;
50+
4751
private MouseCursor cursor;
4852
private SharedPreferences prefs;
4953

@@ -103,6 +107,8 @@ public void onCreate(Bundle savedInstanceState) {
103107
int minRes = Math.min(dm.widthPixels, dm.heightPixels);
104108
mouseDeadzone = minRes / 40; // fairly arbitrary
105109

110+
touchControls = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("touchControl", false);
111+
106112
try {
107113
mouseScalingFactor = Float.parseFloat(prefs.getString("pref_touchpadSensitivity", "1.8"));
108114
} catch (NumberFormatException e) {
@@ -114,7 +120,7 @@ private void showControls() {
114120
hideControls = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Constants.HIDE_CONTROLS, false);
115121
if (!hideControls) {
116122
RelativeLayout layout = getLayout();
117-
Osc osc = new Osc(PreferenceManager.getDefaultSharedPreferences(getContext()).getBoolean("multiplayer", false));
123+
osc = new Osc(PreferenceManager.getDefaultSharedPreferences(getContext()).getBoolean("multiplayer", false));
118124
osc.placeElements(layout);
119125
}
120126
cursor = new MouseCursor(this);
@@ -146,70 +152,73 @@ public void onWindowFocusChanged(boolean hasFocus) {
146152
// Touch events
147153
@Override
148154
public boolean onTouchEvent(MotionEvent event) {
149-
switch (event.getActionMasked()) {
150-
case MotionEvent.ACTION_DOWN:
151-
case MotionEvent.ACTION_POINTER_DOWN:
152-
if (numPointersDown == 0) {
153-
startX = event.getX();
154-
startY = event.getY();
155-
}
156-
++numPointersDown;
157-
maxPointersDown = Math.max(numPointersDown, maxPointersDown);
158-
break;
159-
case MotionEvent.ACTION_UP:
160-
case MotionEvent.ACTION_POINTER_UP:
161-
numPointersDown = Math.max(0, numPointersDown - 1);
162-
if (numPointersDown == 0) {
163-
// everything's up, do the action
164-
if (!isMoving && SDLActivity.isMouseShown() != 0) {
165-
// only send clicks if we didn't move
166-
int mouseX = SDLActivity.getMouseX();
167-
int mouseY = SDLActivity.getMouseY();
168-
int mouseButton = 0;
169-
170-
if (maxPointersDown == 1)
171-
mouseButton = 1;
172-
else if (maxPointersDown == 2)
173-
mouseButton = 2;
174-
175-
if (mouseButton != 0) {
176-
SDLActivity.onNativeMouse(mouseButton, MotionEvent.ACTION_DOWN, mouseX, mouseY);
177-
final Handler handler = new Handler();
178-
handler.postDelayed(() -> SDLActivity.onNativeMouse(0, MotionEvent.ACTION_UP, mouseX, mouseY), 100);
179-
}
180-
}
181-
182-
maxPointersDown = 0;
183-
isMoving = false;
184-
}
185-
break;
186-
case MotionEvent.ACTION_MOVE:
187-
if (maxPointersDown == 1) {
188-
float diffX = event.getX() - startX;
189-
float diffY = event.getY() - startY;
190-
double distance = Math.sqrt(diffX * diffX + diffY * diffY);
191-
192-
if (distance > mouseDeadzone) {
193-
isMoving = true;
155+
if (SDLActivity.isMouseShown() != 0 && touchControls)
156+
return false;
157+
else {
158+
switch (event.getActionMasked()) {
159+
case MotionEvent.ACTION_DOWN:
160+
case MotionEvent.ACTION_POINTER_DOWN:
161+
if (numPointersDown == 0) {
194162
startX = event.getX();
195163
startY = event.getY();
196-
} else if (isMoving) {
197-
int mouseX = SDLActivity.getMouseX();
198-
int mouseY = SDLActivity.getMouseY();
199-
200-
long newMouseX = Math.round(mouseX + diffX * mouseScalingFactor);
201-
long newMouseY = Math.round(mouseY + diffY * mouseScalingFactor);
202-
203-
if (SDLActivity.isMouseShown() != 0)
204-
SDLActivity.onNativeMouse(0, MotionEvent.ACTION_MOVE, newMouseX, newMouseY);
164+
}
165+
++numPointersDown;
166+
maxPointersDown = Math.max(numPointersDown, maxPointersDown);
167+
break;
168+
case MotionEvent.ACTION_UP:
169+
case MotionEvent.ACTION_POINTER_UP:
170+
numPointersDown = Math.max(0, numPointersDown - 1);
171+
if (numPointersDown == 0) {
172+
// everything's up, do the action
173+
if (!isMoving && SDLActivity.isMouseShown() != 0) {
174+
// only send clicks if we didn't move
175+
int mouseX = SDLActivity.getMouseX();
176+
int mouseY = SDLActivity.getMouseY();
177+
int mouseButton = 0;
178+
179+
if (maxPointersDown == 1)
180+
mouseButton = 1;
181+
else if (maxPointersDown == 2)
182+
mouseButton = 2;
183+
184+
if (mouseButton != 0) {
185+
SDLActivity.onNativeMouse(mouseButton, MotionEvent.ACTION_DOWN, mouseX, mouseY);
186+
final Handler handler = new Handler();
187+
handler.postDelayed(() -> SDLActivity.onNativeMouse(0, MotionEvent.ACTION_UP, mouseX, mouseY), 100);
188+
}
189+
}
205190

206-
startX = event.getX();
207-
startY = event.getY();
191+
maxPointersDown = 0;
192+
isMoving = false;
208193
}
209-
}
210-
break;
194+
break;
195+
case MotionEvent.ACTION_MOVE:
196+
if (maxPointersDown == 1) {
197+
float diffX = event.getX() - startX;
198+
float diffY = event.getY() - startY;
199+
double distance = Math.sqrt(diffX * diffX + diffY * diffY);
200+
201+
if (distance > mouseDeadzone) {
202+
isMoving = true;
203+
startX = event.getX();
204+
startY = event.getY();
205+
} else if (isMoving) {
206+
int mouseX = SDLActivity.getMouseX();
207+
int mouseY = SDLActivity.getMouseY();
208+
209+
long newMouseX = Math.round(mouseX + diffX * mouseScalingFactor);
210+
long newMouseY = Math.round(mouseY + diffY * mouseScalingFactor);
211+
212+
if (SDLActivity.isMouseShown() != 0)
213+
SDLActivity.onNativeMouse(0, MotionEvent.ACTION_MOVE, newMouseX, newMouseY);
214+
215+
startX = event.getX();
216+
startY = event.getY();
217+
}
218+
}
219+
break;
220+
}
211221
}
212-
213222
return true;
214223
}
215224

app/src/main/java/ui/controls/Osc.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,19 @@ class Osc(
369369
}
370370
}
371371

372+
fun disableElements(disable: Boolean) {
373+
if (keyboardVisible)
374+
return
375+
376+
for (element in elements) {
377+
if (element == keyboardButton || element.uniqueId == "chat" || element.uniqueId == "pause" || element.uniqueId == "inventory" || element.uniqueId == "shift")
378+
continue
379+
if (!disable && element is OscHiddenButton && element !is OscHiddenToggle)
380+
continue
381+
element.view?.visibility = if (disable) View.GONE else View.VISIBLE
382+
}
383+
}
384+
372385
fun placeConfigurableElements(target: RelativeLayout, listener: View.OnTouchListener) {
373386
for (element in elements) {
374387
element.placeConfigurable(target, listener)

app/src/main/res/xml/settings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
android:summary="Keep screen always on"
2525
android:title="Screen" />
2626

27+
<CheckBoxPreference
28+
android:key="touchControl"
29+
android:summary="Change between touch control and cursor mode"
30+
android:title="Touch Control" />
31+
2732
<CheckBoxPreference
2833
android:key="multiplayer"
2934
android:summary="Change between Singleplayer and Multiplayer mode"

buildscripts/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ set(OPENMW_PATCH
473473
patch -d <SOURCE_DIR> -p1 -t -N < ${CMAKE_SOURCE_DIR}/patches/openmw-revert-ui-dimensions.patch &&
474474
patch -d <SOURCE_DIR> -p1 -t -N < ${CMAKE_SOURCE_DIR}/patches/openmw-add-simpler-collisions-option.patch &&
475475
patch -d <SOURCE_DIR> -p1 -t -N < ${CMAKE_SOURCE_DIR}/patches/openmw-shadows-red-sky.patch &&
476+
patch -d <SOURCE_DIR> -p1 -t -N < ${CMAKE_SOURCE_DIR}/patches/openmw-mouse-visibility.patch &&
476477
patch -d <SOURCE_DIR> -p1 -t -N < ${CMAKE_SOURCE_DIR}/patches/openmw/0001-loadingscreen-disable-for-now.patch &&
477478
patch -d <SOURCE_DIR> -p1 -t -N < ${CMAKE_SOURCE_DIR}/patches/openmw/0002-scenemanager-disable-state-share-inc-compile-openmw.patch &&
478479
patch -d <SOURCE_DIR> -p1 -t -N < ${CMAKE_SOURCE_DIR}/patches/openmw/0003-globalmap-fix-race-vs-savegame-load.patch &&

0 commit comments

Comments
 (0)