/** * Create a region with the provided coordinate / size * * @param X X position * @param Y Y position * @param W width * @param H heigth */ public Region(int X, int Y, int W, int H) { this(X, Y, W, H, null); this.rows = 0; log(lvl, "init: (%d, %d, %d, %d)", X, Y, W, H); }
/** * stops a running observer */ public void stopObserver() { log(lvl, "observe: request to stop observer for " + this.toStringShort()); observing = false; observingInBackground = false; }
public void setImageMissingHandler(Object handler) { imageMissingHandler = setHandler(handler, ObserveEvent.Type.MISSING); log(lvl, "Setting ImageMissingHandler"); }
public void setFindFailedHandler(Object handler) { findFailedHandler = setHandler(handler, ObserveEvent.Type.FINDFAILED); log(lvl, "Setting FindFailedHandler"); }
/** * move the mouse pointer to the given target location<br> same as mouseMove<br> Pattern or Filename - do a find * before and use the match<br> Region - position at center<br> Match - position at match's targetOffset<br> Location * - position at that point<br> * * @param <PFRML> to search: Pattern, Filename, Text, Region, Match or Location * @param target Pattern, Filename, Text, Region, Match or Location * @return 1 if possible, 0 otherwise * @throws FindFailed for Pattern or Filename */ public <PFRML> int hover(PFRML target) throws FindFailed { log(lvl, "hover: " + target); return mouseMove(target); }
public String onChangeDo(Integer threshold, Object observer) { String name = Observing.add(this, (ObserverCallBack) observer, ObserveEvent.Type.CHANGE, threshold); log(lvl, "%s: onChange%s: %s minSize: %d", toStringShort(), (observer == null ? "" : " with callback"), name, threshold); return name; }
private FindFailedResponse handleFindFailedShowDialog(Image img, boolean shouldCapture) { log(lvl, "handleFindFailedShowDialog: requested %s", (shouldCapture ? "(with capture)" : "")); FindFailedResponse response; FindFailedDialog fd = new FindFailedDialog(img, shouldCapture); fd.setVisible(true); response = fd.getResponse(); fd.dispose(); wait(0.5); log(lvl, "handleFindFailedShowDialog: answer is %s", response); return response; }
/** * start an observer in this region for the given time that runs in background - for details about the observe event * handler: {@link ObserverCallBack} for details about APPEAR/VANISH/CHANGE events: {@link ObserveEvent} * * @param secs time in seconds the observer should run * @return false if not possible, true otherwise */ public boolean observeInBackground(double secs) { if (observing) { Debug.error("Region: observeInBackground: already running for this region. Only one allowed!"); return false; } observing = true; observingInBackground = true; Thread observeThread = new Thread(new ObserverThread(secs)); observeThread.start(); log(lvl, "observeInBackground now running"); return true; }
/** * waits until target vanishes or timeout (in seconds) is passed * * @param <PSI> Pattern, String or Image * @param target Pattern, String or Image * @param timeout time in seconds * @return true if target vanishes, false otherwise and if imagefile is missing. */ public <PSI> boolean waitVanish(PSI target, double timeout) { RepeatableVanish rv = new RepeatableVanish(target); Image img = rv._image; String targetStr = img.getName(); Boolean response = true; if (!img.isValid() && img.hasIOException()) { response = handleImageMissing(img, false); } if (null != response && response) { log(lvl, "waiting for " + targetStr + " to vanish within %.1f secs", timeout); if (rv.repeat(timeout)) { log(lvl, "%s vanished", targetStr); return true; } log(lvl, "%s did not vanish before timeout", targetStr); return false; } return false; }
private <PSIC> String onEvent(PSIC targetThreshhold, Object observer, ObserveEvent.Type obsType) { if (observer != null && (observer.getClass().getName().contains("org.python") || observer.getClass().getName().contains("org.jruby"))) { observer = new ObserverCallBack(observer, obsType); } if (!(targetThreshhold instanceof Integer)) { Image img = Image.getImageFromTarget(targetThreshhold); Boolean response = true; if (!img.isValid() && img.hasIOException()) { response = handleImageMissing(img, false); if (response == null) { runTime.abortScripting("onEvent(" + obsType.name() + "): Abort:", "ImageMissing: " + targetThreshhold.toString()); } } } String name = Observing.add(this, (ObserverCallBack) observer, obsType, targetThreshhold); log(lvl, "%s: observer %s %s: %s with: %s", toStringShort(), obsType, (observer == null ? "" : " with callback"), name, targetThreshhold); return name; }
private Boolean handleImageMissing(Image img, boolean recap) { log(lvl, "handleImageMissing: %s", img.getName()); ObserveEvent evt = null; FindFailedResponse response = findFailedResponse; ObserveEvent.Type type = ObserveEvent.Type.MISSING; if (imageMissingHandler != null && ((ObserverCallBack) imageMissingHandler).getType().equals(type)) { log(lvl, "handleImageMissing: Response.HANDLE: calling handler"); evt = new ObserveEvent("", type, null, img, this, 0); ((ObserverCallBack) imageMissingHandler).missing(evt); log(lvl, "handleImageMissing: Response.PROMPT"); response = handleFindFailedShowDialog(img, true); log(lvl, "handleImageMissing: Response.RETRY: %s", (recap ? "recapture " : "capture missing ")); getRobotForRegion().delay(500); ScreenImage simg = getScreen().userCapture( String path = ImagePath.getBundlePath(); if (path == null) { log(-1, "handleImageMissing: no bundle path - aborting"); return null; Image.set(img); if (img.isValid()) { log(lvl, "handleImageMissing: %scaptured: %s", (recap ? "re" : ""), img); Image.setIDEshouldReload(img); return true; log(lvl, "handleImageMissing: Response.ABORT: aborting");
log(lvl, "findAll: waiting %.1f secs for (multiple) %s to appear in %s", autoWaitTimeout, targetStr, this.toStringShort()); if (autoWaitTimeout > 0) { log(lvl, "findAll: %s has appeared", targetStr); break; } else { log(lvl, "findAll: %s did not appear", targetStr); response = handleFindFailed(target, img, false);
log(lvl, "find: waiting 0 secs for %s to appear in %s", targetStr, this.toStringShort()); lastMatch = doFind(target, img, null); if (lastMatch != null) { img.setLastSeen(lastMatch.getRect(), lastMatch.getScore()); log(lvl, "find: %s appeared (%s)", targetStr, lastMatch); break; log(lvl, "find: %s did not appear [%d msec]", targetStr, new Date().getTime() - lastFindTime); if (null == lastMatch) { response = handleFindFailed(target, img, false);
/** * VERY EXPERIMENTAL: returns a list of matches, that represent single words, that have been found in this region<br> * the match's x,y,w,h the region of the word<br> Match.getText() returns the word (utf8) at this match<br> * Match.getScore() returns a value between 0 ... 1, that represents some OCR-confidence value<br > (the higher, the * better the OCR engine thinks the result is) * * @return a list of matches */ public List<Match> listText() { if (Settings.OcrTextRead) { ScreenImage simg = getScreen().capture(x, y, w, h); TextRecognizer tr = TextRecognizer.getInstance(); if (tr == null) { Debug.error("text: text recognition is now switched off"); return null; } log(lvl, "listText: scanning %s", this); return tr.listText(simg, this); } Debug.error("text: text recognition is currently switched off"); return null; } //</editor-fold>
/** * STILL EXPERIMENTAL: tries to read the text in this region<br> might contain misread characters, NL characters and * other stuff, when interpreting contained grafics as text<br> * Best results: one line of text with no grafics in the line * * @return the text read (utf8 encoded) */ public String text() { if (Settings.OcrTextRead) { ScreenImage simg = getScreen().capture(x, y, w, h); TextRecognizer tr = TextRecognizer.getInstance(); if (tr == null) { Debug.error("text: text recognition is now switched off"); return "--- no text ---"; } String textRead = tr.recognize(simg); log(lvl, "text: #(" + textRead + ")#"); return textRead; } Debug.error("text: text recognition is currently switched off"); return "--- no text ---"; }
log(lvl, "exists: waiting %.1f secs for %s to appear in %s", timeout, targetStr, this.toStringShort()); if (rf.repeat(timeout)) { lastMatch = rf.getMatch(); img.setLastSeen(lastMatch.getRect(), lastMatch.getScore()); log(lvl, "exists: %s has appeared (%s)", targetStr, lastMatch); return lastMatch; } else { runTime.abortScripting("Exists: Abort:", "FindFailed: " + shouldAbort); } else { log(lvl, "exists: %s did not appear [%d msec]", targetStr, new Date().getTime() - lastFindTime);
log(lvl, "handleFindFailed: %s", target); Boolean state = null; ObserveEvent evt = null; ObserveEvent.Type type = ObserveEvent.Type.FINDFAILED; if (findFailedHandler != null && ((ObserverCallBack) findFailedHandler).getType().equals(type)) { log(lvl, "handleFindFailed: Response.HANDLE: calling handler"); evt = new ObserveEvent("", type, target, img, this, 0); ((ObserverCallBack) findFailedHandler).findfailed(evt);