Advertisement
Guest User

Untitled

a guest
Sep 12th, 2023
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.10 KB | None | 0 0
  1. public static final int TOUCH_IDLE = 0;
  2.     public static final int TOUCH_PRESSED = 1;
  3.     public static final int TOUCH_RELEASED = 2;
  4.  
  5.     public interface TextCallback {
  6.         void onEnteredText(String str);
  7.     }
  8.  
  9.     public static class TouchState {
  10.         public boolean State;
  11.         public int Id;
  12.         public float X, Y;
  13.     }
  14.  
  15.     public static int GAMEPAD_A = 0;
  16.     public static int GAMEPAD_B = 1;
  17.     public static int GAMEPAD_Y = 2;
  18.     public static int GAMEPAD_X = 3;
  19.     public static int GAMEPAD_LT = 4;
  20.     public static int GAMEPAD_RT = 5;
  21.     public static int GAMEPAD_DPAD_LEFT = 6;
  22.     public static int GAMEPAD_DPAD_RIGHT = 7;
  23.     public static int GAMEPAD_DPAD_UP = 8;
  24.     public static int GAMEPAD_DPAD_DOWN = 9;
  25.     public static int GAMEPAD_BUTTON_COUNT = 10;
  26.  
  27.     public static class GamepadState {
  28.         public float AnalogX, AnalogY;
  29.         public boolean[] Buttons;
  30.  
  31.         GamepadState() {
  32.             Buttons = new boolean[GAMEPAD_BUTTON_COUNT];
  33.         }
  34.     }
  35.  
  36.     class TouchListener implements View.OnTouchListener {
  37.  
  38.         @Override
  39.         public boolean onTouch(View view, MotionEvent motionEvent) {
  40.             for(int i = 0; i < motionEvent.getPointerCount(); i++) {
  41.                 Touches[i].Id = motionEvent.getPointerId(i);
  42.  
  43.                 // Convert from device-space to view-space.
  44.                 float xVal = motionEvent.getX() / Engine.Current.Graphics.DeviceWidth;
  45.                 float yVal = motionEvent.getY() / Engine.Current.Graphics.DeviceHeight;
  46.                 Touches[i].X = xVal * Engine.Current.Graphics.ViewWidth;
  47.                 Touches[i].Y = yVal * Engine.Current.Graphics.ViewHeight;
  48.  
  49.                 if(motionEvent.getAction() == MotionEvent.ACTION_DOWN)
  50.                     Touches[i].State = true;
  51.  
  52.                 if(motionEvent.getAction() == MotionEvent.ACTION_UP)
  53.                     Touches[i].State = false;
  54.             }
  55.  
  56.             return true;
  57.         }
  58.     }
  59.  
  60.     public TouchState[] Touches;
  61.     public GamepadState Gamepad;
  62.     // Format - first int is KEYCODE mapped on Android, second is gamepad button
  63.     private final int[] gamePadMapping =
  64.             {
  65.                     KeyEvent.KEYCODE_DPAD_CENTER, GAMEPAD_A,
  66.                     KeyEvent.KEYCODE_BACK, GAMEPAD_B,
  67.                     KeyEvent.KEYCODE_BUTTON_Y, GAMEPAD_Y,
  68.                     KeyEvent.KEYCODE_BUTTON_X, GAMEPAD_X,
  69.                     KeyEvent.KEYCODE_DPAD_UP, GAMEPAD_DPAD_UP,
  70.                     KeyEvent.KEYCODE_DPAD_RIGHT, GAMEPAD_DPAD_RIGHT,
  71.                     KeyEvent.KEYCODE_DPAD_LEFT, GAMEPAD_DPAD_LEFT,
  72.                     KeyEvent.KEYCODE_DPAD_UP, GAMEPAD_DPAD_UP,
  73.                     KeyEvent.KEYCODE_DPAD_DOWN, GAMEPAD_DPAD_DOWN
  74.             };
  75.  
  76.     public Input() {
  77.         Touches = new TouchState[5];
  78.  
  79.         for(int i = 0; i < Touches.length; i++)
  80.             Touches[i] = new TouchState();
  81.  
  82.         Gamepad = new GamepadState();
  83.  
  84.         Engine.log("Initializing input subsystem...");
  85.         Engine.Current.Graphics.GLView.setOnTouchListener(new TouchListener());
  86.     }
  87.  
  88.     public int isTouchingZone(float x, float y, float w, float h) {
  89.         boolean touching = false;
  90.  
  91.         for(int i = 0; i < Touches.length; i++) {
  92.             touching = Touches[i].X > x && Touches[i].Y > y && Touches[i].X < x + w && Touches[i].Y < y + h;
  93.  
  94.             if(touching && Touches[i].State)
  95.                 return i;
  96.         }
  97.  
  98.         return -1;
  99.     }
  100.  
  101.     public boolean isAnyFingerInZone(float x, float y, float w, float h) {
  102.         boolean touching = false;
  103.  
  104.         for(int i = 0; i < Touches.length; i++) {
  105.             touching = Touches[i].X > x && Touches[i].Y > y && Touches[i].X < x + w && Touches[i].Y < y + h;
  106.  
  107.             if(touching && Touches[i].State)
  108.                 return true;
  109.         }
  110.  
  111.         return false;
  112.     }
  113.  
  114.     public void requestTextInput(String title, String target, TextCallback callback) {
  115.         AlertDialog.Builder dlgBuilder = new AlertDialog.Builder(Engine.Current.MainActivity);
  116.  
  117.         TextView text = new TextView(Engine.Current.MainActivity);
  118.         EditText editor = new EditText(Engine.Current.MainActivity);
  119.  
  120.         text.setText(target + ":");
  121.         editor.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
  122.         dlgBuilder.setTitle(title);
  123.  
  124.         LinearLayout layout = new LinearLayout(Engine.Current.MainActivity);
  125.         layout.addView(text);
  126.         layout.addView(editor);
  127.         layout.setOrientation(LinearLayout.VERTICAL);
  128.         layout.setPadding(5, 5, 5, 5);
  129.         dlgBuilder.setView(layout);
  130.         dlgBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  131.             @Override
  132.             public void onClick(DialogInterface dialogInterface, int i) {
  133.                 callback.onEnteredText(editor.getText().toString());
  134.             }
  135.         });
  136.  
  137.         Engine.Current.MainActivity.runOnUiThread(new Runnable() {
  138.             @Override
  139.             public void run() {
  140.                 dlgBuilder.show();
  141.             }
  142.         });
  143.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement