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

81
LINES

< > BotCompany Repo | #1000974 // Dialog with delayed answers

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

Libraryless. Click here for Pure Java version (311L/3K/11K).

1  
!747
2  
!awt {
3  
4  
m {
5  
  static JTextArea textArea;
6  
  static float fontSize = 24f;
7  
  static S fontSnippet =
8  
    //"#1000970"
9  
    "#1000973" // Classic Trash 1
10  
    ;
11  
  static S thinking = "[...]";
12  
  
13  
  static Font loadFont() ctex {
14  
    return Font.createFont(Font.TRUETYPE_FONT, loadLibrary(fontSnippet)).deriveFont(fontSize);
15  
  }
16  
  
17  
  p {
18  
    print("Chat ID: " + get(getJavaX(), "chatSend_id"));
19  
    
20  
    textArea = new JTextArea();
21  
    textArea.setFont(loadFont());
22  
    textArea.setMargin(new Insets(10, 10, 10, 10));
23  
    
24  
    textArea.addKeyListener(new KeyAdapter() {
25  
      public void keyPressed(KeyEvent e) {
26  
        if (e.getKeyCode() == KeyEvent.VK_ENTER && e.getModifiers() == 0) {
27  
          enterPressed();
28  
        }
29  
      }
30  
    });
31  
    
32  
    makeFrame("Dialog 2", new JScrollPane(textArea));
33  
    sleep(); // important for auto-logging as of now
34  
  }
35  
  
36  
  
37  
  static void enterPressed() {
38  
    S text = textArea.getText();
39  
    int idx = textArea.getCaretPosition();
40  
    
41  
    // move to end of line first
42  
    while (idx < text.length() && text.charAt(idx) != '\n') ++idx;
43  
    textArea.setCaretPosition(idx);
44  
    
45  
    //print(quote(text.substring(0, idx)));
46  
    int lineStart = text.lastIndexOf("\n", idx-1);
47  
    //print(lineStart + " " + idx);
48  
    final S line = text.substring(lineStart+1, idx);
49  
    print("?? " + line);
50  
    type(thinking);
51  
    thread {
52  
      processLine(line);
53  
    }
54  
  }
55  
  
56  
  static void processLine(S line) {
57  
    sleep(1000);
58  
    answer("hihi");
59  
  }
60  
  
61  
  static void answer(final S s) {
62  
    awt {
63  
      print("!! " + s);
64  
      S text = textArea.getText();
65  
      int idx = text.indexOf(thinking);
66  
      if (idx < 0) {
67  
        print("Weird! Maybe user edited?");
68  
        return;
69  
      }
70  
      int cursor = textArea.getCaretPosition();
71  
      textArea.replaceRange(s, idx, idx+thinking.length());
72  
      if (cursor > idx+thinking.length())
73  
        textArea.setCaretPosition(cursor+s.length()-thinking.length());
74  
    }
75  
  }
76  
  
77  
  static void type(S bla) {
78  
    print("!! " + bla);
79  
    textArea.insert("\n" + bla, textArea.getCaretPosition());
80  
  }
81  
}

Author comment

Began life as a copy of #1000966

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1000974
Snippet name: Dialog with delayed answers
Eternal ID of this version: #1000974/1
Text MD5: 0dfc9dd7cae0b93379a06c214182fefc
Transpilation MD5: cb6a70836a9083fc630665ef60e7f70c
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-13 02:50:00
Source code size: 2137 bytes / 81 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 614 / 664
Referenced in: [show references]