Skip to content

Commit cc85169

Browse files
committed
Updated Screen class to Adapter changes, added capture method
1 parent 3183b91 commit cc85169

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

lib/screen.class.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { join, normalize } from "path";
2-
import { NativeAdapter } from "./adapter/native.adapter.class";
2+
import { cwd } from "process";
33
import { VisionAdapter } from "./adapter/vision.adapter.class";
4+
import { FileType } from "./file-type.enum";
5+
import { generateOutputPath } from "./generate-output-path.function";
46
import { LocationParameters } from "./locationparameters.class";
57
import { MatchRequest } from "./match-request.class";
68
import { Region } from "./region.class";
@@ -11,14 +13,15 @@ export class Screen {
1113
resourceDirectory: "./",
1214
};
1315

14-
constructor(private vision: VisionAdapter, private native: NativeAdapter) {}
16+
constructor(private vision: VisionAdapter) {
17+
}
1518

1619
public width() {
17-
return this.native.screenWidth();
20+
return this.vision.screenWidth();
1821
}
1922

2023
public height() {
21-
return this.native.screenHeight();
24+
return this.vision.screenHeight();
2225
}
2326

2427
public async findOnScreen(
@@ -27,12 +30,12 @@ export class Screen {
2730
): Promise<Region> {
2831
const minMatch = (params && params.confidence) || this.config.confidence;
2932
const searchRegion =
30-
(params && params.searchRegion) || await this.native.screenSize();
33+
(params && params.searchRegion) || await this.vision.screenSize();
3134

3235
const fullPathToNeedle = normalize(join(this.config.resourceDirectory, pathToNeedle));
33-
console.log(`Full path to needle: ${fullPathToNeedle}`);
36+
// console.log(`Full path to needle: ${fullPathToNeedle}`);
3437

35-
const screenImage = await this.native.grabScreen();
38+
const screenImage = await this.vision.grabScreen();
3639

3740
const matchRequest = new MatchRequest(
3841
screenImage,
@@ -50,9 +53,27 @@ export class Screen {
5053
reject(
5154
`No match for ${pathToNeedle}. Required: ${minMatch}, given: ${
5255
matchResult.confidence
53-
}`,
56+
}`,
5457
);
5558
}
5659
});
5760
}
61+
62+
public async capture(
63+
fileName: string,
64+
fileFormat: FileType = FileType.PNG,
65+
filePath: string = cwd(),
66+
fileNamePrefix: string = "",
67+
fileNamePostfix: string = ""): Promise<string> {
68+
const outputPath = generateOutputPath(fileName, {
69+
path: filePath,
70+
postfix: fileNamePostfix,
71+
prefix: fileNamePrefix,
72+
type: fileFormat,
73+
});
74+
75+
const currentScreen = await this.vision.grabScreen();
76+
this.vision.saveImage(currentScreen, outputPath);
77+
return outputPath;
78+
}
5879
}

0 commit comments

Comments
 (0)