@@ -5,16 +5,21 @@ import { FileType } from "./file-type.enum";
5
5
import { generateOutputPath } from "./generate-output-path.function" ;
6
6
import { LocationParameters } from "./locationparameters.class" ;
7
7
import { MatchRequest } from "./match-request.class" ;
8
+ import { MatchResult } from "./match-result.class" ;
8
9
import { Region } from "./region.class" ;
9
10
import { timeout } from "./util/poll-action.function" ;
10
11
12
+ export type FindHookCallback = ( target : MatchResult ) => Promise < void > ;
13
+
11
14
export class Screen {
12
15
public config = {
13
16
confidence : 0.99 ,
14
17
resourceDirectory : cwd ( ) ,
15
18
} ;
16
19
17
- constructor ( private vision : VisionAdapter ) {
20
+ constructor (
21
+ private vision : VisionAdapter ,
22
+ private findHooks : Map < string , FindHookCallback > = new Map < string , FindHookCallback > ( ) ) {
18
23
}
19
24
20
25
public width ( ) {
@@ -34,7 +39,6 @@ export class Screen {
34
39
( params && params . searchRegion ) || await this . vision . screenSize ( ) ;
35
40
36
41
const fullPathToNeedle = normalize ( join ( this . config . resourceDirectory , pathToNeedle ) ) ;
37
- // console.log(`Full path to needle: ${fullPathToNeedle}`);
38
42
39
43
const screenImage = await this . vision . grabScreen ( ) ;
40
44
@@ -49,6 +53,10 @@ export class Screen {
49
53
try {
50
54
const matchResult = await this . vision . findOnScreenRegion ( matchRequest ) ;
51
55
if ( matchResult . confidence >= minMatch ) {
56
+ const possibleHook = this . findHooks . get ( pathToNeedle ) ;
57
+ if ( possibleHook ) {
58
+ await possibleHook ( matchResult ) ;
59
+ }
52
60
resolve ( matchResult . location ) ;
53
61
} else {
54
62
reject (
@@ -73,6 +81,10 @@ export class Screen {
73
81
return timeout ( 500 , timeoutMs , ( ) => this . find ( pathToNeedle , params ) ) ;
74
82
}
75
83
84
+ public on ( pathToNeedle : string , callback : FindHookCallback ) {
85
+ this . findHooks . set ( pathToNeedle , callback ) ;
86
+ }
87
+
76
88
public async capture (
77
89
fileName : string ,
78
90
fileFormat : FileType = FileType . PNG ,
0 commit comments