Slick Forums

Discuss the Slick 2D Library
It is currently Wed Jun 19, 2013 3:43 pm

All times are UTC




Post new topic Reply to topic  [ 28 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Fri May 07, 2010 6:25 am 
Offline

Joined: Mon May 03, 2010 6:58 pm
Posts: 9
I adapted the chat demo on the twl website into a terminal for my game and so far it's working well, but I'd like to be able to browse my history of commands by pressing the up and down arrows, but as for as I can tell the Callback doesn't respond to arrows.

Code:
public class Console extends ResizableFrame {
    private TextArea output;
    private EditField input;
    private ScrollPane scrollPane;
    private String outputString;
    private ConsoleWriter outputStream;
    private LinkedList<String> history;
    private ListIterator<String> position;

    public Console(){
        setTitle("Console");

        output = new TextArea(new SimpleTextAreaModel());
        input = new EditField();
        outputString = "Felidae Engine 1.0";
        history = new LinkedList<String>();
        position = history.listIterator();

        input.addCallback(new EditField.Callback() {
            public void callback(int key) {
                if(key == Keyboard.KEY_RETURN) {
                    outputString += "\n" + input.getText();
                    Scripting.runString(input.getText());
                    ((SimpleTextAreaModel)output.getModel()).setText(outputString);
                    history.addFirst(input.getText());
                    position = history.listIterator();
                    input.setText("");
                }else if(key == Keyboard.KEY_UP){
                    if(position.hasNext()){
                        input.setText(position.next());
                    }
                }else if(key == Keyboard.KEY_DOWN){
                    if(position.hasPrevious()){
                        input.setText(position.previous());
                    }
                }
            }
        });

        scrollPane = new ScrollPane(output);
        scrollPane.setFixed(ScrollPane.Fixed.HORIZONTAL);

        DialogLayout l = new DialogLayout();
        l.setTheme("content");
        l.setHorizontalGroup(l.createParallelGroup(scrollPane, input));
        l.setVerticalGroup(l.createSequentialGroup(scrollPane, input));
        add(l);

        ((SimpleTextAreaModel)output.getModel()).setText(outputString);

        outputStream = new ConsoleWriter(this);
    }

    /**
     * @return the outputStream
     */
    public ConsoleWriter getOutputStream() {
        return outputStream;
    }

    public class ConsoleWriter extends Writer{
        private Console console;

        public ConsoleWriter(Console console){
            super();

            this.console = console;
        }

        @Override
        public void write(char[] cbuf, int off, int len) throws IOException {
            console.outputString += "\n    ";
            for(int a = off;a < len+off;a++){
                if(cbuf[a] == '\n'){
                    console.outputString += "\n    ";
                }else{
                    console.outputString += cbuf[a];
                }
            }
        }

        @Override
        public void flush() throws IOException {
        }

        @Override
        public void close() throws IOException {
        }
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 07, 2010 4:51 pm 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1188
I added a new flag to EditField which you can use for that:
Code:
input.setForwardUnhandledKeysToCallback(true);


As an alternative (or in addition) you could use the auto completion system - it will pop up a info window with your suggestions.

BTW: You append to a String in a loop inside your ConsoleWriter class. You really should not do that - use a StringBuilder instead. Every append using += will create a new StringBuilder, copy both strings into it, create a new String object from the StringBuilder (using another copy). And that for every character. The result is O(n²) performance instead of O(n) for your write() method.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 10, 2010 12:55 am 
Offline

Joined: Mon May 03, 2010 6:58 pm
Posts: 9
Thank you, great library, and one more question. I'd like to be able to press the tilde key(the key that opens and closes the console window), and start typing in the EditField without clicking on it, is there anyway I could programmatically set the focus to the EditField?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 10, 2010 1:09 am 
Offline
Game Developer
User avatar

Joined: Wed Feb 17, 2010 12:24 am
Posts: 594
fictiveLaark wrote:
Thank you, great library, and one more question. I'd like to be able to press the tilde key(the key that opens and closes the console window), and start typing in the EditField without clicking on it, is there anyway I could programmatically set the focus to the EditField?


Maybe this: http://slick.javaunlimited.net/viewtopic.php?t=2362


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 10, 2010 1:43 am 
Offline
Game Developer
User avatar

Joined: Wed Feb 17, 2010 12:24 am
Posts: 594
MatthiasM wrote:
As an alternative (or in addition) you could use the auto completion system - it will pop up a info window with your suggestions.


Is there an example of this anywhere?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 10, 2010 2:39 am 
Offline

Joined: Mon May 03, 2010 6:58 pm
Posts: 9
dime wrote:
fictiveLaark wrote:
Thank you, great library, and one more question. I'd like to be able to press the tilde key(the key that opens and closes the console window), and start typing in the EditField without clicking on it, is there anyway I could programmatically set the focus to the EditField?


Maybe this: http://slick.javaunlimited.net/viewtopic.php?t=2362


Guess I shouldn't have been looking for setFocus.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 10, 2010 6:13 am 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1188
dime wrote:
Is there an example of this anywhere?


You can take a look at WidgetsDemoDialog1 in TWLExamples or you can look at the TWL Theme Editor's autocompletion for states

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 10, 2010 7:12 am 
Offline
Game Developer
User avatar

Joined: Wed Feb 17, 2010 12:24 am
Posts: 594
MatthiasM wrote:
dime wrote:
Is there an example of this anywhere?


You can take a look at WidgetsDemoDialog1 in TWLExamples or you can look at the TWL Theme Editor's autocompletion for states


That's awesome. Thanks.

One last thing, the autocomplete works, as does the up/down arrows (for history); but they don't seem to work together. I think I have to bail out of the AutoCompletionDataSource if it's a up/down key?


Code:
textInput = new EditField();
      textInput.setMaxTextLength(40);
      textInput.setForwardUnhandledKeysToCallback(true);

      textInput.setAutoCompletion(new AutoCompletionDataSource() {
         public AutoCompletionResult collectSuggestions(String text, int cursorPos, AutoCompletionResult prev) {
            text = text.substring(0, cursorPos);
            ArrayList<String> result = new ArrayList<String>();
            for (int i = 0; i < lm.getNumEntries(); i++) {
               if (lm.matchPrefix(i, text)) {
                  result.add(lm.getEntry(i));
               }
            }
            if (result.isEmpty()) {
               return null;
            }
            return new SimpleAutoCompletionResult(text, 0, result);
         }
      });
      
      textInput.addCallback(new EditField.Callback() {
         public void callback(int key) {
            if (key == Keyboard.KEY_RETURN) {
               String text = textInput.getText();

               history.addFirst(text);
               position = history.listIterator();

               Debug.print(text);
               textInput.setText("");
               DebuggerController.command(text);

            } else if (key == Keyboard.KEY_UP) {
               if (position.hasNext()) {
                  textInput.setText(position.next());
               }
            } else if (key == Keyboard.KEY_DOWN) {
               if (position.hasPrevious()) {
                  textInput.setText(position.previous());
               }
            }
         }
      });


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 10, 2010 7:35 pm 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1188
Hmm, auto completion uses KEY_UP / KEY_DOWN. I fixed an issue where auto completion would consume these keys when no auto completion is available. With this patch you should be able to catch UP/DOWN after pressing ESCAPE to cancel a active auto completion.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 10, 2010 7:46 pm 
Offline
Game Developer
User avatar

Joined: Wed Feb 17, 2010 12:24 am
Posts: 594
MatthiasM wrote:
Hmm, auto completion uses KEY_UP / KEY_DOWN. I fixed an issue where auto completion would consume these keys when no auto completion is available. With this patch you should be able to catch UP/DOWN after pressing ESCAPE to cancel a active auto completion.


Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 13, 2010 3:37 am 
Offline
Game Developer
User avatar

Joined: Wed Feb 17, 2010 12:24 am
Posts: 594
fictiveLaark wrote:
I'd like to be able to press the tilde key(the key that opens and closes the console window),

Did you get this working in TWL?

I can open it in slick with this:
Code:
   if (key == Input.KEY_GRAVE) {
         debugger.toggleDebugConsole();
      }


But can't get it working in TWL (to close it, since TWL nabs the input as expected since editfield has the text input focus):
Code:
else if (key == Keyboard.KEY_GRAVE || key == Keyboard.KEY_ESCAPE) {
               GameStateDebugger.toggleDebugConsole();
}


Escape works and toggles as expected. But it doesn't pick up the KEY_GRAVE.

The key I'm using is the ~ tilde key. Input.KEY_GRAVE in slick. I'm assuming it's Keyboard.KEY_GRAVE in TWL? Without the shift (which is what I want, it comes in as: `). I also tried KEY_APOSTROPHE without luck.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 13, 2010 5:19 am 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1188
You can intercept the KEY_PRESSED event before calling super.handleEvent. Keyboard events are first send to the root widget and from that to it's focused child by Widget.handleEvent. So if you handle that event before calling the super method you get a hot key behavior.

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 13, 2010 7:34 am 
Offline
Game Developer
User avatar

Joined: Wed Feb 17, 2010 12:24 am
Posts: 594
MatthiasM wrote:
Hmm, auto completion uses KEY_UP / KEY_DOWN. I fixed an issue where auto completion would consume these keys when no auto completion is available. With this patch you should be able to catch UP/DOWN after pressing ESCAPE to cancel a active auto completion.


This works as expected. Very intuitive and user friendly.

The only thing, is if you go 'up' and select a command that is in the auto fill list; you can't hit return. You have to space/backspace or click input field if that makes sense?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 13, 2010 7:46 am 
Offline
Game Developer
User avatar

Joined: Wed Feb 17, 2010 12:24 am
Posts: 594
MatthiasM wrote:
You can intercept the KEY_PRESSED event before calling super.handleEvent. Keyboard events are first send to the root widget and from that to it's focused child by Widget.handleEvent. So if you handle that event before calling the super method you get a hot key behavior.


ok, this works nice! I created a new class that extends DesktopArea and overwrote the handleEvent like this:

Code:

    @Override
    protected boolean handleEvent(Event evt) {
         switch (evt.getType()) {
            case KEY_PRESSED:
                switch (evt.getKeyCode()) {
                    case Keyboard.KEY_GRAVE:
doSomething();
                        return true;
                }
        }
       if(super.handleEvent(evt)) {
            return true;
        }
        return false;
    }


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 13, 2010 8:18 am 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1188
dime wrote:
The only thing, is if you go 'up' and select a command that is in the auto fill list; you can't hit return. You have to space/backspace or click input field if that makes sense?

Huh? Can you create a small test case (using one of the example themes) which shows this behavior?

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 28 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group