Skip to content

Commit

Permalink
Add Swipe Key View #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Xtr126 committed Mar 23, 2023
1 parent ebdfa8c commit 55c4e34
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 15 deletions.
32 changes: 23 additions & 9 deletions app/src/main/java/xtr/keymapper/EditorUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import xtr.keymapper.dpad.Dpad.DpadType;
import xtr.keymapper.floatingkeys.MovableFloatingActionKey;
import xtr.keymapper.floatingkeys.MovableFrameLayout;
import xtr.keymapper.floatingkeys.SwipeKeyView;
import xtr.keymapper.mouse.MouseAimConfig;
import xtr.keymapper.mouse.MouseAimSettings;
import xtr.keymapper.profiles.KeymapProfiles;
Expand All @@ -49,7 +50,6 @@ public class EditorUI extends OnKeyEventListener.Stub {

private MovableFrameLayout dpadWasd, dpadUdlr, crosshair;
// Default position of new views added
private static final Float DEFAULT_X = 200f, DEFAULT_Y = 200f;
private final KeymapEditorBinding binding;
private final Context context;
private final OnHideListener onHideListener;
Expand All @@ -73,6 +73,8 @@ public EditorUI (Context context, String profileName) {

binding = KeymapEditorBinding.inflate(layoutInflater);
mainView = binding.getRoot();


binding.speedDial.inflate(R.menu.keymap_editor_menu);
binding.speedDial.open();
setupButtons();
Expand Down Expand Up @@ -193,16 +195,20 @@ private void saveKeymap() {

public void setupButtons() {
binding.speedDial.setOnActionSelectedListener(actionItem -> {
// X y coordinates of center of root view
float defaultX = mainView.getPivotX();
float defaultY = mainView.getPivotY();

switch (actionItem.getId()) {
case R.id.add:
addKey();
addKey(defaultX, defaultY);
break;
case R.id.dpad:
final CharSequence[] items = { "Arrow Keys", "WASD Keys"};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Select Dpad").setItems(items, (dialog, i) -> {
if (i == 0) addArrowKeysDpad(DEFAULT_X, DEFAULT_Y);
else addWasdDpad(DEFAULT_X, DEFAULT_Y);
if (i == 0) addArrowKeysDpad(defaultX, defaultY);
else addWasdDpad(defaultX, defaultY);
});
AlertDialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
Expand All @@ -214,14 +220,17 @@ public void setupButtons() {
break;

case R.id.mouse_left:
addLeftClick(DEFAULT_X, DEFAULT_Y);
addLeftClick(defaultX, defaultY);
break;

case R.id.crosshair:
profile.mouseAimConfig = new MouseAimConfig();
addCrosshair(DEFAULT_X, DEFAULT_Y);
addCrosshair(defaultX, defaultY);
break;

case R.id.swipe_key:
addSwipeKey();
break;
}
return true;
});
Expand Down Expand Up @@ -283,11 +292,11 @@ private void addKey(KeymapProfiles.Key key) {
keyList.add(floatingKey);
}

private void addKey() {
private void addKey(float x, float y) {
final KeymapProfiles.Key key = new KeymapProfiles.Key();
key.code = "KEY_X";
key.x = DEFAULT_X;
key.y = DEFAULT_Y;
key.x = x;
key.y = y;
addKey(key);
}

Expand Down Expand Up @@ -326,6 +335,11 @@ private void addLeftClick(float x, float y) {
.start();
}

@SuppressLint("ClickableViewAccessibility")
private void addSwipeKey() {
SwipeKeyView swipeKeyView = new SwipeKeyView(mainView);
}

private void resizeView(View view, float x, float y) {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width += x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class MovableFloatingActionKey extends FrameLayout implements View.OnTouc

public FloatingActionKey key;
private float dX, dY;
private OnXyChangeListener xyChangeListener;

public MovableFloatingActionKey(Context context) {
super(context);
Expand Down Expand Up @@ -80,7 +81,6 @@ public String getText(){
@Override
public boolean onTouch(View view, MotionEvent motionEvent){
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams)view.getLayoutParams();

int action = motionEvent.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN: {
Expand Down Expand Up @@ -109,15 +109,23 @@ public boolean onTouch(View view, MotionEvent motionEvent){
newY = Math.max(layoutParams.topMargin, newY); // Don't allow the FAB past the top of the parent
newY = Math.min(parentHeight - viewHeight - layoutParams.bottomMargin, newY); // Don't allow the FAB past the bottom of the parent

view.animate()
.x(newX)
.y(newY)
.setDuration(0)
.start();
view.animate().x(newX).y(newY).setDuration(0).start();

if (xyChangeListener != null)
xyChangeListener.onNewXY(newX, newY);

return true; // Consumed
}
default:
return super.onTouchEvent(motionEvent);
}
}
public void setXyChangeListener(OnXyChangeListener xyChangeListener) {
this.xyChangeListener = xyChangeListener;
xyChangeListener.onNewXY(getX(), getY());
}

public interface OnXyChangeListener {
void onNewXY(float x, float y);
}
}
51 changes: 51 additions & 0 deletions app/src/main/java/xtr/keymapper/floatingkeys/SwipeKeyOverlay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package xtr.keymapper.floatingkeys;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.UiContext;
import androidx.annotation.UiThread;

public class SwipeKeyOverlay extends View {
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float lineStartX, lineStartY;
private float lineStopX, lineStopY;

public SwipeKeyOverlay(Context context) {
this(context, null);
}

public SwipeKeyOverlay(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public SwipeKeyOverlay(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mPaint.setColor(Color.CYAN);
mPaint.setStrokeWidth(5f);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setPathEffect(new DashPathEffect(new float[]{5, 10, 15, 20}, 0));
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(lineStartX, lineStartY, lineStopX, lineStopY, mPaint);
}

@UiThread
public void setLineXyFrom(View view1, View view2) {
this.lineStartX = view1.getX() + view1.getPivotX();
this.lineStartY = view1.getY() + view1.getPivotY();

this.lineStopX = view2.getX() + view2.getPivotX();
this.lineStopY = view2.getY() + view2.getPivotY();
invalidate();
}

}
30 changes: 30 additions & 0 deletions app/src/main/java/xtr/keymapper/floatingkeys/SwipeKeyView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package xtr.keymapper.floatingkeys;

import android.content.Context;
import android.view.ViewGroup;

public class SwipeKeyView {

public SwipeKeyView(ViewGroup rootView){
Context context = rootView.getContext();
MovableFloatingActionKey button1 = new MovableFloatingActionKey(context);
MovableFloatingActionKey button2 = new MovableFloatingActionKey(context);

SwipeKeyOverlay overlay = new SwipeKeyOverlay(context);
rootView.addView(overlay);

rootView.addView(button1);
rootView.addView(button2);

button1.setX(rootView.getPivotX() - 100);
button1.setY(rootView.getPivotY() - 100);

button2.setX(rootView.getPivotX() + 100);
button2.setY(rootView.getPivotY() + 100);

MovableFloatingActionKey.OnXyChangeListener xyChangeListener = (x, y) -> overlay.setLineXyFrom(button1, button2);

button1.setXyChangeListener(xyChangeListener);
button2.setXyChangeListener(xyChangeListener);
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_baseline_touch_app_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M9,11.24V7.5C9,6.12 10.12,5 11.5,5S14,6.12 14,7.5v3.74c1.21,-0.81 2,-2.18 2,-3.74C16,5.01 13.99,3 11.5,3S7,5.01 7,7.5C7,9.06 7.79,10.43 9,11.24zM18.84,15.87l-4.54,-2.26c-0.17,-0.07 -0.35,-0.11 -0.54,-0.11H13v-6C13,6.67 12.33,6 11.5,6S10,6.67 10,7.5v10.74c-3.6,-0.76 -3.54,-0.75 -3.67,-0.75c-0.31,0 -0.59,0.13 -0.79,0.33l-0.79,0.8l4.94,4.94C9.96,23.83 10.34,24 10.75,24h6.79c0.75,0 1.33,-0.55 1.44,-1.28l0.75,-5.27c0.01,-0.07 0.02,-0.14 0.02,-0.2C19.75,16.63 19.37,16.09 18.84,15.87z"/>
</vector>
35 changes: 35 additions & 0 deletions app/src/main/res/layout/swipe_key.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<xtr.keymapper.floatingkeys.MovableFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="320dp"
android:layout_height="240dp"
android:translationZ="2dp"
android:focusable="false"
android:theme="@style/Theme.XtMapper"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:ignore="HardcodedText,ContentDescription">

<com.google.android.material.button.MaterialButton
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="top|left"
android:id="@+id/key_1"
style="@style/Widget.Material3.Button.IconButton.Filled" />
<com.google.android.material.button.MaterialButton
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="bottom|right"
android:id="@+id/key_2"
style="@style/Widget.Material3.Button.IconButton.Filled" />


<com.google.android.material.button.MaterialButton
android:id="@+id/close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:icon="@android:drawable/ic_delete"
style="@style/Widget.Material3.Button.IconButton.Filled.Tonal"
/>

</xtr.keymapper.floatingkeys.MovableFrameLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/menu/keymap_editor_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
android:id="@+id/mouse_left"
android:icon="@drawable/ic_baseline_mouse_36"
android:title="Left Click" />
<item
android:id="@+id/swipe_key"
android:icon="@drawable/ic_baseline_touch_app_24"
android:title="Swipe Key" />
<item
android:id="@+id/add"
android:icon="@drawable/newkey"
Expand Down

0 comments on commit 55c4e34

Please sign in to comment.