Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

156
LINES

< > BotCompany Repo | #1015126 // Mouse ToolTip & Log [OK]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 4281K of libraries. Click here for Pure Java version (14359L/106K).

1  
!7
2  
3  
sbool showToolTipWhenKnownSymbol = true;
4  
sbool doOCR = true;
5  
static double interval = 1.0;
6  
static new LatestAppearance<S> latest;
7  
sS tooltip;
8  
static RectAndMD5 mouse;
9  
static Rect rectShown;
10  
static int maxClipWidth = min(300, screenWidth()/2);
11  
static int maxClipHeight = min(100, screenHeight()/2);
12  
static ImageSurface lastIS;
13  
static Runnable rTeach;
14  
static S hotkey;
15  
16  
p-subst {
17  
  autoRestart(5);
18  
  startBotHere("Screen Segmenter.", #1015121);
19  
  consoleIcon(#1101243);
20  
  
21  
  onGlobalHotKey(hotkey = hotKey_teach(), r { callF(rTeach) });
22  
23  
  onGlobalMousePress(voidfunc(NativeMouseEvent e) {
24  
    lock defaultLock();
25  
    int x = e.getX(), y = e.getY();
26  
    makeToolTip(Pt(x, y));
27  
    S text;
28  
    if (mouse == null)
29  
      text = "User clicked at " + x + "/" + y + " (" + screenWidth() + "x" + screenHeight() + ")";
30  
    else
31  
      text = "User clicked on " + (empty(tooltip) ? "uncommented object of size " + mouse.r.w + "x" + mouse.r.h : multiLineQuote(tooltip)) + " at " + x + "/" + y + " (screen: " + screenWidth() + "x" + screenHeight() + ", rect: " + mouse.r + ", md5: " + mouse.md5 + ")";
32  
    mechAppend_noUniq_inQ("Mouse log", text);
33  
  });
34  
  
35  
  S tooltip = ""; // mouse
36  
  repeat with sleep interval {
37  
    time2 {
38  
      L<S> list = latest.recordAndSort(thingsOnScreen2());
39  
      
40  
      makeToolTip(mousePt());
41  
    }
42  
    print(localDateWithSeconds() + ": Found: " + list);
43  
    consoleTitle(l(list) + " " + joinWithComma(list));
44  
  }
45  
}
46  
47  
svoid makeToolTip(Pt mousePt) {
48  
  lock defaultLock();
49  
  L<RectAndMD5> rects = segmentedScreenFromCacheFile_last;
50  
  mouse = smallestRectAndMD5Containing(rects, mousePt);
51  
  S name = "";
52  
  
53  
  /*if (mouse != null)
54  
    print("Mouse: " + sfu(mouse));*/
55  
  
56  
  if (mouse != null)
57  
    name = unnull(thingsOnScreenDefinitions().get(mouse.md5));
58  
    
59  
  /*if (neq(name, tooltip))
60  
    flatInfoBox_topRightCorner(name, 1.0);*/
61  
  tooltip = name;
62  
  
63  
  bool interpreted = nempty(name);
64  
  final Rect r = mouse == null ? null : mouse.r;
65  
  if (r != null && neq(r, rectShown)
66  
   && r.w <= maxClipWidth && r.h <= maxClipHeight
67  
   && !mouseInWindow(lastIS)) {
68  
    disposeWindow(lastIS);
69  
    double zoom = 1;
70  
    int x = max(r.w, r.h);
71  
    if (x < 30) zoom = 3.0;
72  
    else if (x < 100) zoom = 2.0;
73  
    final BufferedImage clip = shootScreenArea_possiblyBroken(mouse.r);
74  
    final ImageSurface is = zoomedImageSurface(clip, zoom);
75  
    lastIS = is;
76  
    hideWindowOnComponentMouseOver(is);
77  
    fS _name = name;
78  
    final RectAndMD5 _mouse = mouse;
79  
    JLabel lblSimilarity = jcenteredlabel("Interpretation:");
80  
    JLabel lblInterpretation = jcenteredboldlabel(or2(name, "?"));
81  
    final Var<S> interpretation = new(name);
82  
    JComponent panel = withMargin(10,
83  
      jMinWidth(250, vstackWithSpacing(
84  
        jcenteredlabel("Symbol at mouse cursor:"),
85  
        jFullCenter(is),
86  
        lblSimilarity,
87  
        lblInterpretation,
88  
        jbutton((nempty(hotkey) ? "[" + hotkey + "] " : "") + "Teach this symbol...", rTeach = r-thread {
89  
          teach(clip, interpretation!, _mouse);
90  
        }))));
91  
    if (!interpreted) panel = jBackground(Color.red, panel);
92  
    if (showToolTipWhenKnownSymbol || !interpreted) {
93  
      showWindowInTopRightCorner(withLineBorder(Color.green, panel));
94  
      disposeWindowAfter_unlessMouseInIt(3.0, is);
95  
    }
96  
    
97  
    if (!interpreted)
98  
      slowSearch(clip, _mouse, interpretation, lblSimilarity, lblInterpretation);
99  
  }
100  
  rectShown = r;
101  
}
102  
103  
svoid teach(BufferedImage clip, S _name, RectAndMD5 r) {
104  
  fS md5 = ocrMD5OfBufferedImage(clip);
105  
  bool imageChanged = neq(md5, r.md5);
106  
  fS name = unnull(thingsOnScreenDefinitions().get(md5));
107  
  S displayName = or3(name, _name, "\"\"");
108  
  final JTextField tf = jtextfield(displayName);
109  
  if (isQuoted(displayName))
110  
    selectRange(tf, 1, l(displayName)-1);
111  
  biggerSubmitButton("Save", showFormTitled("Teach symbol",
112  
    "MD5:", md5 + (imageChanged ? " (warning: changed)" : ""),
113  
    "Symbol:", imageChooser(clip),
114  
    "Meaning:", tf,
115  
    r {
116  
      S text = getTextTrim(tf);
117  
      addThingOnScreenDefinition(md5, text);
118  
      if (neq(name, text))
119  
        infoBoxTopLeftCorner("Taught new symbol!");
120  
    }));
121  
}
122  
123  
svoid slowSearch(final BufferedImage clip, final RectAndMD5 _mouse, final Var<S> interpretation, final JLabel lblSimilarity, final JLabel lblInterpretation) {
124  
  thread "Slow Image Search" {
125  
    BWImage bw = new BWImage(clip);
126  
    final new Best<S> best;
127  
    
128  
    Runnable update = r {
129  
      interpretation.set(best!);
130  
      setText(lblInterpretation, best!);
131  
      setText(lblSimilarity, "Possible interpretation (" + ratioToIntPercent_avoid100(best.score(), 1) + "%):");
132  
    };
133  
    
134  
    long updateInterval = 100, lastUpdate = sysNow();
135  
    for (File imgFile : listFiles(screenClipsDir())) {
136  
      ping();
137  
      if (mouse != _mouse) break; // new image shown in the mean time
138  
      
139  
      S md5 = md5FromScreenClipFile(imgFile);
140  
      if (md5 == null) continue;
141  
      S def = thingsOnScreenDefinitions().get(md5);
142  
      if (def == null) continue;
143  
      BWImage pat = loadBWImage(imgFile);
144  
      float similarity = bwImageSimilarityResized(bw, pat, (float) best.bestScore());
145  
      if (best.put(def, similarity) && sysNow() >= lastUpdate+updateInterval) {
146  
        lastUpdate = sysNow();
147  
        callF(update);
148  
      }
149  
    }
150  
    
151  
    if (best.has()) callF(update);
152  
    
153  
    if (doOCR)
154  
      topLeftInfoBox(lines(ocr_recognizeMultiLine(bw)));
155  
  }
156  
}

Author comment

Began life as a copy of #1010713

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1015126
Snippet name: Mouse ToolTip & Log [OK]
Eternal ID of this version: #1015126/104
Text MD5: 98c987a8f3312442cb5c31c9f34c6d81
Transpilation MD5: 8e17504a3091e4d45e88038e1484f518
Author: stefan
Category: javax / ocr
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-18 11:30:35
Source code size: 5492 bytes / 156 lines
Pitched / IR pitched: No / No
Views / Downloads: 646 / 1765
Version history: 103 change(s)
Referenced in: [show references]