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

120
LINES

< > BotCompany Repo | #1005714 // SingleObjectFinder [with GUI]

JavaX fragment (include)

1  
// please include #1005686 [PIF Classes v2]
2  
3  
sclass SingleObjectFinder {
4  
  S obj = "start button";
5  
  
6  
  *() {}
7  
  *(S *obj) {}
8  
9  
  int maxTotal = 10;
10  
  bool fullSearch = true;
11  
  JComponent form;
12  
  JLabel lFound, lSearchTime, lScreenshots;
13  
  volatile L<Found> found;
14  
15  
  void showGUI() {
16  
    form = showForm(
17  
      "Number of \*obj*/ pictures stored:", jConceptCountLabel(Clip),
18  
      "Number of screenshots stored:", lScreenshots = jConceptDependentLabel(func {
19  
        int n = countConcepts(Screenshot);
20  
        int t = countConcepts(TransientScreenshot);
21  
        ret n + " (" + (n-t) + " with image)";
22  
      }),
23  
      "Currently found:", lFound = jlabel(),
24  
      "Search time:", lSearchTime = jlabel(),
25  
      "", jbutton("Show pictures", r { showClips() }),
26  
      "", jbutton("Grab picture of \*obj*/", r { addClip(false) }),
27  
      "", jbutton("Grab picture with delay", r { addClip(true) })
28  
    );
29  
    
30  
    onDoubleClick(lFound, r {
31  
      Found f = first(found);
32  
      if (f == null) ret;
33  
      moveMouseTo(centerOfRect(f.fi.r));
34  
    });
35  
    
36  
    componentPopupMenu(lFound, voidfunc(JPopupMenu menu) {
37  
      menu.add(jmenuItem("Show all", r {
38  
        for (Found f: unnull(found))
39  
          pointArrowsToArea(f.fi.r);
40  
      }));
41  
    });
42  
    
43  
    componentPopupMenu(lScreenshots, voidfunc(JPopupMenu menu) {
44  
      menu.add(jmenuItem("Delete older screenshots", r {
45  
        deleteOlderScreenshots()
46  
      }));
47  
    });
48  
    
49  
    final new MonoThread thread;
50  
    installTimer(form, 5000, 0, r {
51  
      thread.runIfNotRunning(r {
52  
        BufferedImage img = shootScreen2();
53  
        long start = now();
54  
        found = continuousFind_step(goodClips(), img, maxTotal, fullSearch);
55  
        final long time = now()-start;
56  
        awt {
57  
          lSearchTime.setText(time + " ms");
58  
          S s = "no";
59  
          if (nempty(found))
60  
            s = "at " + first(found).fi.r + " [clip " + first(found).clip->id + "]";
61  
          if (l(found) > 1)
62  
            s += " +" + (l(found)-1);
63  
          lFound.setText(s);
64  
        }
65  
      });
66  
    });
67  
  }
68  
  
69  
  void addClip(bool delay) {
70  
    letUserMarkScreenArea(delay ? null : shootScreen2(), voidfunc(final Rectangle r, BufferedImage img) {
71  
      if (r == null) print("Cancelled!");
72  
      else {
73  
        Screenshot screenshot = new Screenshot(img);
74  
    
75  
        showAnimation("#1005611", "You marked " + r, 3);
76  
        uniq(MarkedClip, +screenshot, positionInScreenshot := new Rect(r), 
77  
          description := obj,
78  
          img := new BWImage(img).clip(r));
79  
        darkenScreenArea(r, 5);
80  
        swingLater(250, r {
81  
          pointArrowsToArea(r, 5-0.25);
82  
        });
83  
      }
84  
    });
85  
  }
86  
87  
  void showClips {
88  
    new L<L> data;
89  
    for (MarkedClip clip) pcall {
90  
      data.add(ll(clip.id, clip.img == null ? null : clip.img.getBufferedImage(), clip.description));
91  
    }
92  
    final JTable table = showTableWithImages("Clips", splitAtSpace("ID Picture Description"), data);
93  
    tablePopupMenu(table, voidfunc(JPopupMenu menu, int row) {
94  
      L line = getTableLine(table, row);
95  
      long id = parseLong(line.get(0));
96  
      final Clip clip = cast getConcept(id);
97  
      if (neq(clip.description, "bad"))
98  
        menu.add(jmenuItem("Mark bad", r {
99  
          cset(clip, description := "bad");
100  
          disposeFrame(table);
101  
          showClips();
102  
        }));
103  
    });
104  
  }
105  
  
106  
  L<Clip> goodClips() {
107  
    ret [Clip c in list(Clip) | swic(c.description, obj)];
108  
  }
109  
  
110  
  void deleteOlderScreenshots() {
111  
    for (Screenshot s : dropLast(list(Screenshot))) {
112  
      if (hasBackRef(s, Clip)) continue;
113  
      print("Backrefs to screenshot:");
114  
      L<Concept> backRefs = findBackRefs(s, Concept);
115  
      printIndent(renderConcepts(backRefs));
116  
      deleteConcepts(backRefs);
117  
      s.delete();
118  
    }
119  
  }
120  
}

Author comment

Began life as a copy of #1005676

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: #1005714
Snippet name: SingleObjectFinder [with GUI]
Eternal ID of this version: #1005714/1
Text MD5: 137c35930f5b77c038b3551e6245a922
Author: stefan
Category: javax / gui / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-12-05 22:44:57
Source code size: 3840 bytes / 120 lines
Pitched / IR pitched: No / No
Views / Downloads: 404 / 760
Referenced in: [show references]