@bettergi/utils 0.1.15 → 0.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/ocr.js +24 -14
  2. package/package.json +2 -2
package/dist/ocr.js CHANGED
@@ -1,13 +1,5 @@
1
1
  import { mouseScrollDownLines } from "./mouse";
2
2
  import { waitForAction } from "./workflow";
3
- const findFirst = (ir, ro, predicate) => {
4
- const candidates = ir.findMulti(ro);
5
- for (let i = 0; i < candidates.count; i++) {
6
- if (predicate(candidates[i]))
7
- return candidates[i];
8
- }
9
- return undefined;
10
- };
11
3
  const directionToBounds = (direction) => {
12
4
  const x = direction.includes("east") ? genshin.width / 2 : 0;
13
5
  const y = direction.includes("south") ? genshin.height / 2 : 0;
@@ -32,8 +24,11 @@ export const findImage = (image, config = {}) => {
32
24
  try {
33
25
  const mat = typeof image === "string" ? file.readImageMatSync(image) : image;
34
26
  const ro = RecognitionObject.templateMatch(mat);
35
- Object.assign(ro, config);
36
- return findFirst(ir, ro, region => region.isExist());
27
+ if (Object.keys(config).length > 0) {
28
+ Object.assign(ro, config) && ro.initTemplate();
29
+ }
30
+ const region = ir.find(ro);
31
+ return region.isExist() ? region : undefined;
37
32
  }
38
33
  catch (err) {
39
34
  log.warn(`${err.message || err}`);
@@ -57,8 +52,11 @@ export const findImageWithinBounds = (image, x, y, w, h, config = {}) => {
57
52
  try {
58
53
  const mat = typeof image === "string" ? file.readImageMatSync(image) : image;
59
54
  const ro = RecognitionObject.templateMatch(mat, x, y, w, h);
60
- Object.assign(ro, config);
61
- return findFirst(ir, ro, region => region.isExist());
55
+ if (Object.keys(config).length > 0) {
56
+ Object.assign(ro, config) && ro.initTemplate();
57
+ }
58
+ const region = ir.find(ro);
59
+ return region.isExist() ? region : undefined;
62
60
  }
63
61
  catch (err) {
64
62
  log.warn(`${err.message || err}`);
@@ -91,6 +89,14 @@ export const findImageInDirection = (image, direction, config = {}) => {
91
89
  const { x, y, w, h } = directionToBounds(direction);
92
90
  return findImageWithinBounds(image, x, y, w, h, config);
93
91
  };
92
+ const findFirst = (ir, ro, predicate) => {
93
+ const candidates = ir.findMulti(ro);
94
+ for (let i = 0; i < candidates.count; i++) {
95
+ if (predicate(candidates[i]))
96
+ return candidates[i];
97
+ }
98
+ return undefined;
99
+ };
94
100
  /**
95
101
  * 在整个画面内搜索文本
96
102
  * @param text 待搜索文本
@@ -104,7 +110,9 @@ export const findText = (text, options, config = {}) => {
104
110
  const ir = captureGameRegion();
105
111
  try {
106
112
  const ro = RecognitionObject.ocrThis;
107
- Object.assign(ro, config);
113
+ if (Object.keys(config).length > 0) {
114
+ Object.assign(ro, config) && ro.initTemplate();
115
+ }
108
116
  return findFirst(ir, ro, region => {
109
117
  const itemText = ignoreCase ? region.text.toLowerCase() : region.text;
110
118
  const isMatch = contains ? itemText.includes(searchText) : itemText === searchText;
@@ -135,7 +143,9 @@ export const findTextWithinBounds = (text, x, y, w, h, options, config = {}) =>
135
143
  const ir = captureGameRegion();
136
144
  try {
137
145
  const ro = RecognitionObject.ocr(x, y, w, h);
138
- Object.assign(ro, config);
146
+ if (Object.keys(config).length > 0) {
147
+ Object.assign(ro, config) && ro.initTemplate();
148
+ }
139
149
  return findFirst(ir, ro, region => {
140
150
  const itemText = ignoreCase ? region.text.toLowerCase() : region.text;
141
151
  const isMatch = contains ? itemText.includes(searchText) : itemText === searchText;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bettergi/utils",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
4
4
  "description": "开发 BetterGI 脚本常用工具集",
5
5
  "type": "module",
6
6
  "author": "Bread Grocery<https://github.com/breadgrocery>",
@@ -33,7 +33,7 @@
33
33
  "build": "rimraf dist && tsc"
34
34
  },
35
35
  "devDependencies": {
36
- "@bettergi/types": "^0.1.3",
36
+ "@bettergi/types": "workspace:latest",
37
37
  "rimraf": "^6.1.0",
38
38
  "typescript": "^5.9.3"
39
39
  }