Skip to content

Commit

Permalink
support for Swipe
Browse files Browse the repository at this point in the history
  • Loading branch information
ftheirs committed Jul 16, 2024
1 parent 2facede commit aebc648
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
54 changes: 48 additions & 6 deletions src/Zemu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ import EmuContainer from "./emulator";
import GRPCRouter from "./grpc";

import { ClickNavigation, scheduleToNavElement, TouchNavigation } from "./actions";
import { dummyButton, tapContinueButton, TouchElements } from "./buttons";
import { dummyButton, swipeContinueButton, TouchElements } from "./buttons";
import {

Check failure on line 55 in src/Zemu.ts

View workflow job for this annotation

GitHub Actions / Format and Lint

Imports "ISwipeCoordinates" are only used as type
ActionKind,
ButtonKind,
ISwipeCoordinates,
SwipeDirection,
type IButton,
type IDeviceWindow,
type IEvent,
Expand Down Expand Up @@ -671,7 +673,7 @@ export default class Zemu {

let start = new Date();
let found = false;
const isStaxDevice = this.startOptions.model === "stax";
const isTouchDevice = this.startOptions.model === "stax";

const textRegex = new RegExp(text, "i");

Expand All @@ -691,8 +693,8 @@ export default class Zemu {
if (found) break;

const nav: INavElement = {
type: isStaxDevice ? ActionKind.Touch : ActionKind.RightClick,
button: tapContinueButton, // For clicks, this will be ignored
type: isTouchDevice ? ActionKind.Touch : ActionKind.RightClick,
button: swipeContinueButton, // For clicks, this will be ignored
};
await this.runAction(nav, filename, waitForScreenUpdate, true);
start = new Date();
Expand All @@ -704,7 +706,7 @@ export default class Zemu {
const staxApproveButton = TouchElements.get(this.startOptions.approveAction);

const nav: INavElement = {
type: isStaxDevice ? ActionKind.Touch : ActionKind.BothClick,
type: isTouchDevice ? ActionKind.Touch : ActionKind.BothClick,
button: staxApproveButton ?? dummyButton,
};
await this.runAction(nav, filename, waitForScreenUpdate, true);
Expand Down Expand Up @@ -833,6 +835,37 @@ export default class Zemu {
return await this.click("/button/both", filename, waitForScreenUpdate, waitForEventsChange);
}

private getSwipeCoordinates(button: IButton): ISwipeCoordinates {
let newX = button.x
let newY = button.y
const SWIPE_PX_MOVEMENT = 10

switch (button.direction) {
case SwipeDirection.SwipeUp:
newY += SWIPE_PX_MOVEMENT
break

case SwipeDirection.SwipeDown:
newY -= SWIPE_PX_MOVEMENT
break

case SwipeDirection.SwipeRight:
newX += SWIPE_PX_MOVEMENT
break

case SwipeDirection.SwipeLeft:
newX -= SWIPE_PX_MOVEMENT
break


case SwipeDirection.NoSwipe:
break
}
return {
x: newX,
y: newY
}
}

Check failure on line 868 in src/Zemu.ts

View workflow job for this annotation

GitHub Actions / Format and Lint

Expected blank line between class members
async fingerTouch(
button: IButton,
filename: string = "",
Expand All @@ -844,7 +877,16 @@ export default class Zemu {
const prevScreen = await this.snapshot();

const fingerTouchUrl = `${this.transportProtocol}://${this.host}:${this.speculosApiPort}/finger`;
const payload = { action: "press-and-release", x: button.x, y: button.y, delay: button.delay };

// Add x2, y2 params only for Swipe
const swipe = this.getSwipeCoordinates(button);
const payload = {
action: "press-and-release",
x: button.x,
y: button.y,
delay: button.delay,
...(button.direction !== SwipeDirection.NoSwipe ? { x2: swipe.x, y2: swipe.y } : {}),
};
await axios.post(fingerTouchUrl, payload);
this.log(`Touch /finger -> ${filename}`);

Expand Down
27 changes: 26 additions & 1 deletion src/buttons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,114 +13,139 @@
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************* */
import { ButtonKind, type IButton } from "./types";
import { ButtonKind, type IButton, SwipeDirection } from "./types";

export const dummyButton: IButton = {
x: 0,
y: 0,
delay: 0,
direction: SwipeDirection.NoSwipe,
};

const infoButton: IButton = {
x: 335,
y: 65,
delay: 0.25,
direction: SwipeDirection.NoSwipe,
};

const quitAppButton: IButton = {
x: 0,
y: 0,
delay: 0.25,
direction: SwipeDirection.NoSwipe,
};

export const tapContinueButton: IButton = {
x: 200,
y: 250,
delay: 0.25,
direction: SwipeDirection.NoSwipe,
};

export const swipeContinueButton: IButton = {
x: 200,
y: 250,
delay: 0.1,
direction: SwipeDirection.SwipeLeft,
};

const prevPageButton: IButton = {
x: 45,
y: 45,
delay: 0.25,
direction: SwipeDirection.NoSwipe,
};

const toggleOption1: IButton = {
x: 350,
y: 125,
delay: 0.25,
direction: SwipeDirection.NoSwipe,
};

const toggleOption2: IButton = {
x: 350,
y: 200,
delay: 0.25,
direction: SwipeDirection.NoSwipe,
};

const toggleOption3: IButton = {
x: 350,
y: 250,
delay: 0.25,
direction: SwipeDirection.NoSwipe,
};

const navRightButton: IButton = {
x: 300,
y: 625,
delay: 0.25,
direction: SwipeDirection.NoSwipe,
};

const navLeftButton: IButton = {
x: 140,
y: 625,
delay: 0.25,
direction: SwipeDirection.NoSwipe,
};

const quitSettingsButton: IButton = {
x: 40,
y: 40,
delay: 0.25,
direction: SwipeDirection.NoSwipe,
};

const approveTapButton: IButton = {
x: 200,
y: 550,
delay: 0.25,
direction: SwipeDirection.NoSwipe,
};

const approveHoldButton: IButton = {
x: 335,
y: 525,
delay: 5,
direction: SwipeDirection.NoSwipe,
};

const rejectButton: IButton = {
x: 200,
y: 650,
delay: 0.25,
direction: SwipeDirection.NoSwipe,
};

const confirmYesButton: IButton = {
x: 200,
y: 550,
delay: 0.25,
direction: SwipeDirection.NoSwipe,
};

const confirmNoButton: IButton = {
x: 200,
y: 650,
delay: 0.25,
direction: SwipeDirection.NoSwipe,
};

const showQRButton: IButton = {
x: 200,
y: 300,
delay: 0.25,
direction: SwipeDirection.NoSwipe,
};

const closeQRButton: IButton = {
x: 200,
y: 650,
delay: 0.25,
direction: SwipeDirection.NoSwipe,
};

export const TouchElements = new Map<ButtonKind, IButton>([
Expand Down
14 changes: 14 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export interface IEvent {
text: string;
}

export interface ISwipeCoordinates {
x: number;
y: number;
}

export type TModel = "nanos" | "nanosp" | "nanox" | "stax";

export interface IStartOptions {
Expand All @@ -58,13 +63,22 @@ export interface IButton {
x: number;
y: number;
delay: number;
direction: SwipeDirection;
}

export interface INavElement {
type: ActionKind;
button: IButton;
}

export const enum SwipeDirection {
NoSwipe = 0,
SwipeUp,
SwipeDown,
SwipeRight,
SwipeLeft,
}

export const enum ActionKind {
LeftClick = 0,
RightClick,
Expand Down

0 comments on commit aebc648

Please sign in to comment.