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).

!747
!awt {

m {
  static JTextArea textArea;
  static float fontSize = 24f;
  static S fontSnippet =
    //"#1000970"
    "#1000973" // Classic Trash 1
    ;
  static S thinking = "[...]";
  
  static Font loadFont() ctex {
    return Font.createFont(Font.TRUETYPE_FONT, loadLibrary(fontSnippet)).deriveFont(fontSize);
  }
  
  p {
    print("Chat ID: " + get(getJavaX(), "chatSend_id"));
    
    textArea = new JTextArea();
    textArea.setFont(loadFont());
    textArea.setMargin(new Insets(10, 10, 10, 10));
    
    textArea.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER && e.getModifiers() == 0) {
          enterPressed();
        }
      }
    });
    
    makeFrame("Dialog 2", new JScrollPane(textArea));
    sleep(); // important for auto-logging as of now
  }
  
  
  static void enterPressed() {
    S text = textArea.getText();
    int idx = textArea.getCaretPosition();
    
    // move to end of line first
    while (idx < text.length() && text.charAt(idx) != '\n') ++idx;
    textArea.setCaretPosition(idx);
    
    //print(quote(text.substring(0, idx)));
    int lineStart = text.lastIndexOf("\n", idx-1);
    //print(lineStart + " " + idx);
    final S line = text.substring(lineStart+1, idx);
    print("?? " + line);
    type(thinking);
    thread {
      processLine(line);
    }
  }
  
  static void processLine(S line) {
    sleep(1000);
    answer("hihi");
  }
  
  static void answer(final S s) {
    awt {
      print("!! " + s);
      S text = textArea.getText();
      int idx = text.indexOf(thinking);
      if (idx < 0) {
        print("Weird! Maybe user edited?");
        return;
      }
      int cursor = textArea.getCaretPosition();
      textArea.replaceRange(s, idx, idx+thinking.length());
      if (cursor > idx+thinking.length())
        textArea.setCaretPosition(cursor+s.length()-thinking.length());
    }
  }
  
  static void type(S bla) {
    print("!! " + bla);
    textArea.insert("\n" + bla, textArea.getCaretPosition());
  }
}

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: 550 / 573
Referenced in: [show references]