Skip to content

Commit

Permalink
fixed: issue #270: observe: setActive/setInactive did not work
Browse files Browse the repository at this point in the history
  • Loading branch information
RaiMan committed Dec 21, 2019
1 parent e366c8f commit 60f3993
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
54 changes: 34 additions & 20 deletions API/src/main/java/org/sikuli/script/ObserveEvent.java
Expand Up @@ -30,7 +30,7 @@ public enum Type {
private List<Match> changes = null;
private long time;
private String name;
private Object[] vals = new Object[] {null, null, null};
private Object[] vals = new Object[]{null, null, null};

public ObserveEvent() {
}
Expand All @@ -42,7 +42,7 @@ public ObserveEvent(String name, Type type, Object v1, Object v2, Object v3, lon
init(name, type, v1, v2, v3, now);
}

private void init(String name, Type type, Object v1, Object v2, Object v3, long now) {
private void init(String name, Type type, Object v1, Object v2, Object v3, long now) {
this.name = name;
this.type = type;
if (now > 0) {
Expand All @@ -61,10 +61,11 @@ private void init(String name, Type type, Object v1, Object v2, Object v3, long
setMatch(v2);
setPattern(v1);
}
}
}

/**
* get the observe event type
*
* @return a string containing either APPEAR, VANISH, CHANGE or GENERIC
*/
public String getType() {
Expand All @@ -73,55 +74,70 @@ public String getType() {

/**
* check the observe event type
*
* @return true if it is APPEAR, false otherwise
*/
public boolean isAppear() {
return Type.APPEAR.equals(type);
}

/**
/**
* check the observe event type
*
* @return true if it is VANISH, false otherwise
*/
public boolean isVanish() {
public boolean isVanish() {
return Type.VANISH.equals(type);
}

/**
/**
* check the observe event type
*
* @return true if it is CHANGE, false otherwise
*/
public boolean isChange() {
public boolean isChange() {
return Type.CHANGE.equals(type);
}

/**
/**
* check the observe event type
*
* @return true if it is GENERIC, false otherwise
*/
public boolean isGeneric() {
public boolean isGeneric() {
return Type.GENERIC.equals(type);
}

/**
/**
* check the observe event type
*
* @return true if it is FINDFAILED, false otherwise
*/
public boolean isFindFailed() {
public boolean isFindFailed() {
return Type.FINDFAILED.equals(type);
}

/**
/**
* check the observe event type
*
* @return true if it is MISSING, false otherwise
*/
public boolean isMissing() {
public boolean isMissing() {
return Type.MISSING.equals(type);
}

public void setActive() {
getRegion().setActive(getName());
}

public void setInactive() {
getRegion().setInactive(getName());
}

/**
* for type GENERIC: 3 values can be stored in the event
* (the value's type is known by creator and user of getVals as some private protocol)
*
* @param v1
* @param v2
* @param v3
Expand All @@ -134,22 +150,21 @@ public void setVals(Object v1, Object v2, Object v3) {

/**
* for type GENERIC: (the value's type is known by creator and user of getVals as some private protocol)
*
* @return an array with the 3 stored values (might be null)
*/
public Object[] getVals() {
return vals;
}

/**
*
* @return the observer name of this event
*/
public String getName() {
return name;
}

/**
*
* @return this event's observer's region
*/
public Region getRegion() {
Expand All @@ -163,7 +178,6 @@ public void setRegion(Object r) {
}

/**
*
* @return the observed match (APEAR, VANISH)
*/
public Match getMatch() {
Expand All @@ -181,7 +195,6 @@ public void setIndex(int index) {
}

/**
*
* @return a list of observed changes as matches (CHANGE)
*/
public List<Match> getChanges() {
Expand All @@ -196,7 +209,6 @@ public void setChanges(List<Match> actualChanges) {
}

/**
*
* @return the used pattern for this event's observing
*/
public Pattern getPattern() {
Expand Down Expand Up @@ -246,6 +258,7 @@ public void repeat() {
/**
* tell the observer to repeat this event's observe action after given time in secs
* after returning from this handler (APPEAR, VANISH)
*
* @param secs seconds
*/
public void repeat(long secs) {
Expand All @@ -268,6 +281,7 @@ public void stopObserver() {

/**
* stops the observer and prints the given text
*
* @param text text
*/
public void stopObserver(String text) {
Expand All @@ -278,10 +292,10 @@ public void stopObserver(String text) {
public String toString() {
if (type == Type.CHANGE) {
return String.format("Event(%s) %s on: %s with: %d count: %d",
type, name, region, index, getCount());
type, name, region, index, getCount());
} else {
return String.format("Event(%s) %s on: %s with: %s\nmatch: %s count: %d",
type, name, region, pattern, match, getCount());
type, name, region, pattern, match, getCount());
}
}
}
7 changes: 6 additions & 1 deletion API/src/main/java/org/sikuli/script/support/Observer.java
Expand Up @@ -50,7 +50,9 @@ public void initialize() {
log(3, "resetting observe states for " + observedRegion.toStringShort());
synchronized (eventNames) {
for (String name : eventNames.keySet()) {
eventStates.put(name, State.FIRST);
if (eventStates.get(name) != State.INACTIVE) {
eventStates.put(name, State.FIRST);
}
eventCounts.put(name, 0);
eventMatches.put(name, null);
}
Expand Down Expand Up @@ -169,6 +171,9 @@ private boolean checkPatterns(ScreenImage simg) {
eventStates.put(name, State.UNKNOWN);
}
}
if (eventStates.get(name) == State.INACTIVE || eventStates.get(name) == State.MISSING) {
continue;
}
Object ptn = eventNames.get(name);
Image img = Image.getImageFromTarget(ptn);
if (img == null || !img.isUseable()) {
Expand Down

0 comments on commit 60f3993

Please sign in to comment.