SDL: refactoring of input event code

This commit is contained in:
Sergii Pylypenko
2017-10-31 23:36:22 +02:00
parent d378ee692f
commit 95ebb73426
6 changed files with 129 additions and 144 deletions

View File

@@ -29,6 +29,7 @@ import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;
import java.net.URLConnection;
@@ -186,7 +187,8 @@ class DataDownloader extends Thread
@Override
public void run()
{
Parent.keyListener = new BackKeyListener(Parent);
Parent.getVideoLayout().setOnKeyListener(new BackKeyListener(Parent));
String [] downloadFiles = Globals.DataDownloadUrl;
int total = 0;
int count = 0;
@@ -217,7 +219,7 @@ class DataDownloader extends Thread
}
}
DownloadComplete = true;
Parent.keyListener = null;
Parent.getVideoLayout().setOnKeyListener(null);
initParent();
}
@@ -803,7 +805,7 @@ class DataDownloader extends Thread
Parent.getPackageName() + "/" + url.substring("obb:".length()) + "." + Parent.getPackageName() + ".obb";
}
public class BackKeyListener implements MainActivity.KeyEventsListener
public class BackKeyListener implements View.OnKeyListener
{
MainActivity p;
public BackKeyListener(MainActivity _p)
@@ -811,7 +813,8 @@ class DataDownloader extends Thread
p = _p;
}
public void onKeyEvent(final int keyCode)
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if( DownloadFailed )
System.exit(1);
@@ -844,6 +847,7 @@ class DataDownloader extends Thread
AlertDialog alert = builder.create();
alert.setOwnerActivity(p);
alert.show();
return true;
}
}

View File

@@ -205,7 +205,12 @@ public class MainActivity extends Activity
loaded.release();
loadedLibraries.release();
if( _btn != null )
{
_btn.setEnabled(true);
_btn.setFocusable(true);
_btn.setFocusableInTouchMode(true);
_btn.requestFocus();
}
}
}
Callback2 cb = new Callback2();
@@ -850,18 +855,6 @@ public class MainActivity extends Activity
}
};
EditText screenKeyboard = new EditText(this);
// This code does not work
/*
screenKeyboard.setMaxLines(100);
ViewGroup.LayoutParams layout = _screenKeyboard.getLayoutParams();
if( layout != null )
{
layout.width = ViewGroup.LayoutParams.FILL_PARENT;
layout.height = ViewGroup.LayoutParams.FILL_PARENT;
screenKeyboard.setLayoutParams(layout);
}
screenKeyboard.setGravity(android.view.Gravity.BOTTOM | android.view.Gravity.LEFT);
*/
String hint = _screenKeyboardHintMessage;
screenKeyboard.setHint(hint != null ? hint : getString(R.string.text_edit_click_here));
screenKeyboard.setText(oldText);
@@ -1046,6 +1039,7 @@ public class MainActivity extends Activity
}
}
/*
@Override
public boolean onKeyDown(int keyCode, final KeyEvent event)
{
@@ -1116,99 +1110,14 @@ public class MainActivity extends Activity
return _btn.onKeyUp(keyCode, event);
return true;
}
@Override
public boolean onKeyMultiple(int keyCode, int repeatCount, final KeyEvent event)
{
if( _screenKeyboard != null )
{
_screenKeyboard.onKeyMultiple(keyCode, repeatCount, event);
return true;
}
else if( mGLView != null && event.getCharacters() != null )
{
// International text input
for(int i = 0; i < event.getCharacters().length(); i++ )
{
mGLView.nativeKey( event.getKeyCode(), 1, event.getCharacters().codePointAt(i) );
mGLView.nativeKey( event.getKeyCode(), 0, event.getCharacters().codePointAt(i) );
}
return true;
}
return false;
}
@Override
public boolean onKeyLongPress (int keyCode, KeyEvent event)
{
if( _screenKeyboard != null )
{
_screenKeyboard.onKeyLongPress(keyCode, event);
return true;
}
return false;
}
@Override
public boolean dispatchTouchEvent(final MotionEvent ev)
{
//Log.i("SDL", "dispatchTouchEvent: " + ev.getAction() + " coords " + ev.getX() + ":" + ev.getY() );
if(_screenKeyboard != null && _screenKeyboard.dispatchTouchEvent(ev))
return true;
if( _ad.getView() != null && // User clicked the advertisement, ignore when user moved finger from game screen to advertisement or touches screen with several fingers
((ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN ||
(ev.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) &&
_ad.getView().getLeft() <= (int)ev.getX() &&
_ad.getView().getRight() > (int)ev.getX() &&
_ad.getView().getTop() <= (int)ev.getY() &&
_ad.getView().getBottom() > (int)ev.getY() )
return super.dispatchTouchEvent(ev);
else
if(mGLView != null)
mGLView.onTouchEvent(ev);
else
if( _btn != null )
return _btn.dispatchTouchEvent(ev);
else
if( touchListener != null )
touchListener.onTouchEvent(ev);
return true;
}
*/
@Override
public boolean dispatchGenericMotionEvent (MotionEvent ev)
{
//Log.i("SDL", "dispatchGenericMotionEvent: " + ev.getAction() + " coords " + ev.getX() + ":" + ev.getY() );
// This code fails to run for Android 1.6, so there will be no generic motion event for Andorid screen keyboard
/*
if(_screenKeyboard != null)
_screenKeyboard.dispatchGenericMotionEvent(ev);
else
*/
if(mGLView != null)
mGLView.onGenericMotionEvent(ev);
return true;
}
//private Configuration oldConfig = null;
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
updateScreenOrientation();
/*
if (oldConfig != null)
{
int diff = newConfig.diff(oldConfig);
Log.i("SDL", "onConfigurationChanged(): " + " diff " + diff +
((diff & ActivityInfo.CONFIG_ORIENTATION) == ActivityInfo.CONFIG_ORIENTATION ? " orientation" : "") +
((diff & ActivityInfo.CONFIG_SCREEN_SIZE) == ActivityInfo.CONFIG_SCREEN_SIZE ? " screen size" : "") +
((diff & ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE) == ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE ? " smallest screen size" : "") +
" " + newConfig.toString());
}
oldConfig = new Configuration(newConfig);
*/
}
public void updateScreenOrientation()
@@ -1644,18 +1553,6 @@ public class MainActivity extends Activity
private boolean sdlInited = false;
public static boolean ApplicationLibraryLoaded = false;
public interface TouchEventsListener
{
public void onTouchEvent(final MotionEvent ev);
}
public interface KeyEventsListener
{
public void onKeyEvent(final int keyCode);
}
public TouchEventsListener touchListener = null;
public KeyEventsListener keyListener = null;
boolean _isPaused = false;
private InputMethodManager _inputManager = null;

View File

@@ -276,10 +276,10 @@ class SettingsMenuKeyboard extends SettingsMenu
void run (final MainActivity p)
{
p.setText(p.getResources().getString(R.string.remap_hwkeys_press));
p.keyListener = new KeyRemapTool(p);
p.getVideoLayout().setOnKeyListener(new KeyRemapTool(p));
}
public static class KeyRemapTool implements MainActivity.KeyEventsListener
public static class KeyRemapTool implements View.OnKeyListener
{
MainActivity p;
public KeyRemapTool(MainActivity _p)
@@ -287,9 +287,10 @@ class SettingsMenuKeyboard extends SettingsMenu
p = _p;
}
public void onKeyEvent(final int keyCode)
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
p.keyListener = null;
p.getVideoLayout().setOnKeyListener(null);
int keyIndex = keyCode;
if( keyIndex < 0 )
keyIndex = 0;
@@ -336,6 +337,7 @@ class SettingsMenuKeyboard extends SettingsMenu
AlertDialog alert = builder.create();
alert.setOwnerActivity(p);
alert.show();
return true;
}
public void ShowAllKeys(final int KeyIndex)
{
@@ -624,12 +626,10 @@ class SettingsMenuKeyboard extends SettingsMenu
{
p.setText(p.getResources().getString(R.string.screenkb_custom_layout_help));
CustomizeScreenKbLayoutTool tool = new CustomizeScreenKbLayoutTool(p);
p.touchListener = tool;
p.keyListener = tool;
Globals.TouchscreenKeyboardSize = Globals.TOUCHSCREEN_KEYBOARD_CUSTOM;
}
static class CustomizeScreenKbLayoutTool implements MainActivity.TouchEventsListener, MainActivity.KeyEventsListener
static class CustomizeScreenKbLayoutTool implements View.OnTouchListener, View.OnKeyListener
{
MainActivity p;
FrameLayout layout = null;
@@ -658,6 +658,11 @@ class SettingsMenuKeyboard extends SettingsMenu
p = _p;
layout = new FrameLayout(p);
p.getVideoLayout().addView(layout);
layout.setFocusable(true);
layout.setFocusableInTouchMode(true);
layout.requestFocus();
layout.setOnTouchListener(this);
layout.setOnKeyListener(this);
boundary = new ImageView(p);
boundary.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
boundary.setScaleType(ImageView.ScaleType.MATRIX);
@@ -744,7 +749,7 @@ class SettingsMenuKeyboard extends SettingsMenu
}
boundary.bringToFront();
if( currentButton == -1 )
onKeyEvent( KeyEvent.KEYCODE_BACK ); // All buttons disabled - do not show anything
onKey( null, KeyEvent.KEYCODE_BACK, null ); // All buttons disabled - do not show anything
else
setupButton(currentButton);
}
@@ -777,7 +782,8 @@ class SettingsMenuKeyboard extends SettingsMenu
p.setText(p.getResources().getString(R.string.screenkb_custom_layout_help) + "\n" + buttonText);
}
public void onTouchEvent(final MotionEvent ev)
@Override
public boolean onTouch(View v, MotionEvent ev)
{
if( ev.getAction() == MotionEvent.ACTION_DOWN )
{
@@ -842,18 +848,19 @@ class SettingsMenuKeyboard extends SettingsMenu
m.setRectToRect(src, dst, Matrix.ScaleToFit.FILL);
boundary.setImageMatrix(m);
}
return true;
}
public void onKeyEvent(final int keyCode)
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
p.getVideoLayout().removeView(layout);
layout = null;
p.touchListener = null;
p.keyListener = null;
goBack(p);
}
return true;
}
}
}

View File

@@ -267,7 +267,7 @@ class SettingsMenuMouse extends SettingsMenu
dialog.dismiss();
Globals.LeftClickMethod = item;
if( item == Mouse.LEFT_CLICK_WITH_KEY )
p.keyListener = new KeyRemapToolMouseClick(p, true);
p.getVideoLayout().setOnKeyListener(new KeyRemapToolMouseClick(p, true));
else if( item == Mouse.LEFT_CLICK_WITH_TIMEOUT || item == Mouse.LEFT_CLICK_WITH_TAP_OR_TIMEOUT )
showLeftClickTimeoutConfig(p);
else
@@ -343,7 +343,7 @@ class SettingsMenuMouse extends SettingsMenu
Globals.RightClickMethod = item;
dialog.dismiss();
if( item == Mouse.RIGHT_CLICK_WITH_KEY )
p.keyListener = new KeyRemapToolMouseClick(p, false);
p.getVideoLayout().setOnKeyListener(new KeyRemapToolMouseClick(p, false));
else if( item == Mouse.RIGHT_CLICK_WITH_TIMEOUT )
showRightClickTimeoutConfig(p);
else
@@ -393,7 +393,7 @@ class SettingsMenuMouse extends SettingsMenu
}
}
public static class KeyRemapToolMouseClick implements MainActivity.KeyEventsListener
public static class KeyRemapToolMouseClick implements View.OnKeyListener
{
MainActivity p;
boolean leftClick;
@@ -404,9 +404,10 @@ class SettingsMenuMouse extends SettingsMenu
this.leftClick = leftClick;
}
public void onKeyEvent(final int keyCode)
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
p.keyListener = null;
p.getVideoLayout().setOnKeyListener(null);
int keyIndex = keyCode;
if( keyIndex < 0 )
keyIndex = 0;
@@ -419,6 +420,7 @@ class SettingsMenuMouse extends SettingsMenu
Globals.RightClickKey = keyIndex;
goBack(p);
return true;
}
}
@@ -686,10 +688,10 @@ class SettingsMenuMouse extends SettingsMenu
void run (final MainActivity p)
{
p.setText(p.getResources().getString(R.string.measurepressure_touchplease));
p.touchListener = new TouchMeasurementTool(p);
p.getVideoLayout().setOnTouchListener(new TouchMeasurementTool(p));
}
public static class TouchMeasurementTool implements MainActivity.TouchEventsListener
public static class TouchMeasurementTool implements View.OnTouchListener
{
MainActivity p;
ArrayList<Integer> force = new ArrayList<Integer>();
@@ -701,7 +703,8 @@ class SettingsMenuMouse extends SettingsMenu
p = _p;
}
public void onTouchEvent(final MotionEvent ev)
@Override
public boolean onTouch(View v, MotionEvent ev)
{
force.add(new Integer((int)(ev.getPressure() * 1000.0)));
radius.add(new Integer((int)(ev.getSize() * 1000.0)));
@@ -712,12 +715,13 @@ class SettingsMenuMouse extends SettingsMenu
if( force.size() >= maxEventAmount )
{
p.touchListener = null;
p.getVideoLayout().setOnTouchListener(null);
Globals.ClickScreenPressure = getAverageForce();
Globals.ClickScreenTouchspotSize = getAverageRadius();
Log.i("SDL", "SDL: measured average force " + Globals.ClickScreenPressure + " radius " + Globals.ClickScreenTouchspotSize);
goBack(p);
}
return true;
}
int getAverageForce()
@@ -756,11 +760,11 @@ class SettingsMenuMouse extends SettingsMenu
Globals.TouchscreenCalibration[2] = 0;
Globals.TouchscreenCalibration[3] = 0;
ScreenEdgesCalibrationTool tool = new ScreenEdgesCalibrationTool(p);
p.touchListener = tool;
p.keyListener = tool;
p.getVideoLayout().setOnTouchListener(tool);
p.getVideoLayout().setOnKeyListener(tool);
}
static class ScreenEdgesCalibrationTool implements MainActivity.TouchEventsListener, MainActivity.KeyEventsListener
static class ScreenEdgesCalibrationTool implements View.OnTouchListener, View.OnKeyListener
{
MainActivity p;
ImageView img;
@@ -783,7 +787,8 @@ class SettingsMenuMouse extends SettingsMenu
p.getVideoLayout().addView(img);
}
public void onTouchEvent(final MotionEvent ev)
@Override
public boolean onTouch(View v, MotionEvent ev)
{
if( Globals.TouchscreenCalibration[0] == Globals.TouchscreenCalibration[1] &&
Globals.TouchscreenCalibration[1] == Globals.TouchscreenCalibration[2] &&
@@ -808,14 +813,17 @@ class SettingsMenuMouse extends SettingsMenu
Globals.TouchscreenCalibration[2], Globals.TouchscreenCalibration[3]);
m.setRectToRect(src, dst, Matrix.ScaleToFit.FILL);
img.setImageMatrix(m);
return true;
}
public void onKeyEvent(final int keyCode)
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
p.touchListener = null;
p.keyListener = null;
p.getVideoLayout().setOnTouchListener(null);
p.getVideoLayout().setOnKeyListener(null);
p.getVideoLayout().removeView(img);
goBack(p);
return true;
}
}
}

View File

@@ -387,7 +387,7 @@ abstract class DifferentTouchInput
}
public void processGenericEvent(final MotionEvent event)
{
// Joysticks are supported since Honeycomb, but I don't care about it, because very little devices have it
// Joysticks are supported since Honeycomb, but I don't care about it, because very few devices have it
if( (event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) == InputDevice.SOURCE_CLASS_JOYSTICK )
{
// event.getAxisValue(AXIS_HAT_X) and event.getAxisValue(AXIS_HAT_Y) are joystick arrow keys, on Nvidia Shield and some other joysticks
@@ -985,6 +985,75 @@ class DemoGLSurfaceView extends GLSurfaceView_SDL {
setRenderer(mRenderer);
}
@Override
public boolean onKeyDown(int keyCode, final KeyEvent event)
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
if( (event.getSource() & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE )
{
// Stupid Samsung and stupid Acer remaps right mouse button to BACK key
nativeMouseButtonsPressed(2, 1);
return true;
}
else if( mParent.keyboardWithoutTextInputShown )
{
return true;
}
}
//if( context._screenKeyboard != null && context._screenKeyboard.onKeyDown(keyCode, event) )
// return true;
if( nativeKey( keyCode, 1, event.getUnicodeChar() ) == 0 )
return super.onKeyDown(keyCode, event);
return true;
}
@Override
public boolean onKeyUp(int keyCode, final KeyEvent event)
{
if( keyCode == KeyEvent.KEYCODE_BACK )
{
if( (event.getSource() & InputDevice.SOURCE_MOUSE) == InputDevice.SOURCE_MOUSE )
{
// Stupid Samsung and stupid Acer remaps right mouse button to BACK key
nativeMouseButtonsPressed(2, 0);
return true;
}
else if( mParent.keyboardWithoutTextInputShown )
{
mParent.showScreenKeyboardWithoutTextInputField(0); // Hide keyboard
return true;
}
}
//if( _screenKeyboard != null && _screenKeyboard.onKeyUp(keyCode, event) )
// return true;
if( nativeKey( keyCode, 0, event.getUnicodeChar() ) == 0 )
return super.onKeyUp(keyCode, event);
if( keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU )
DimSystemStatusBar.get().dim(mParent._videoLayout);
return true;
}
@Override
public boolean onKeyMultiple(int keyCode, int repeatCount, final KeyEvent event)
{
if( event.getCharacters() != null )
{
// International text input
for(int i = 0; i < event.getCharacters().length(); i++ )
{
nativeKey( event.getKeyCode(), 1, event.getCharacters().codePointAt(i) );
nativeKey( event.getKeyCode(), 0, event.getCharacters().codePointAt(i) );
}
}
return true;
}
@Override
public boolean onTouchEvent(final MotionEvent event)
{