Переглянути джерело

History in der Konsole, bessere Platzierung des Scrollens

Kajetan Johannes Hammerle 7 роки тому
батько
коміт
c963565bbc

+ 2 - 3
nbproject/private/private.xml

@@ -3,9 +3,8 @@
     <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
     <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
         <group>
-            <file>file:/Users/kajetanjohannes/Dropbox/Projekte/Informatik/SnuviScript/src/me/hammerle/code/ScriptControl.java</file>
-            <file>file:/Users/kajetanjohannes/Dropbox/Projekte/Informatik/SnuviScript/src/me/hammerle/code/CodeParser.java</file>
-            <file>file:/Users/kajetanjohannes/Dropbox/Projekte/Informatik/SnuviScript/src/me/hammerle/code/Code.java</file>
+            <file>file:/Users/kajetanjohannes/Dropbox/Projekte/Informatik/SnuviScript/src/me/hammerle/console/ConsoleUtils.java</file>
+            <file>file:/Users/kajetanjohannes/Dropbox/Projekte/Informatik/SnuviScript/src/me/hammerle/console/InputBox.java</file>
             <file>file:/Users/kajetanjohannes/Dropbox/Projekte/Informatik/SnuviScript/src/me/hammerle/code/Script.java</file>
         </group>
     </open-files>

+ 4 - 0
src/me/hammerle/code/Script.java

@@ -4,6 +4,7 @@ import java.awt.Graphics;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Stack;
+import me.hammerle.console.ConsoleUtils;
 import me.hammerle.exceptions.CodeTooLongException;
 import me.hammerle.exceptions.GotoLabelNotFoundException;
 import me.hammerle.exceptions.HoldCodeException;
@@ -139,9 +140,11 @@ public class Script
                     position++;
                     if(isHalt())
                     {
+                        ConsoleUtils.scrollToBottom();
                         return;
                     }
                 }
+                ConsoleUtils.scrollToBottom();
                 ScriptControl.term(this);
             }
             catch(Exception ex)
@@ -151,6 +154,7 @@ public class Script
                     CodeParser.printQuestException(this, ex, code[position].toString());
                 }
                 position++;
+                ConsoleUtils.scrollToBottom();
             }
         }
     }

+ 1 - 1
src/me/hammerle/console/BlackBox.java

@@ -9,6 +9,7 @@ import javax.swing.text.BadLocationException;
 import javax.swing.text.Style;
 import javax.swing.text.StyleConstants;
 import javax.swing.text.StyledDocument;
+import me.hammerle.scheduler.SnuviScheduler;
 
 public class BlackBox extends JScrollPane
 {
@@ -61,7 +62,6 @@ public class BlackBox extends JScrollPane
         catch (BadLocationException ex) 
         {
         }
-        scrollToBottom();
     }
     
     public void append(String s, Color c)

+ 5 - 0
src/me/hammerle/console/ConsoleUtils.java

@@ -14,4 +14,9 @@ public class ConsoleUtils
     {
         SnuviScript.console.output.clear();
     }
+    
+    public static void scrollToBottom()
+    {
+        SnuviScript.console.output.scrollToBottom();
+    }
 }

+ 58 - 9
src/me/hammerle/console/InputBox.java

@@ -3,31 +3,80 @@ package me.hammerle.console;
 import java.awt.Color;
 import java.awt.event.KeyAdapter;
 import java.awt.event.KeyEvent;
+import java.util.ArrayList;
 import me.hammerle.code.ScriptControl;
 
 public class InputBox extends BlackBox
 {
+    private final ArrayList<String> history;
+    private int historyPosition;
+    
     public InputBox(Console c)
     {
         super(c);
+        history = new ArrayList<>();
+        historyPosition = -1;
         area.addKeyListener(new KeyAdapter() 
         {
             @Override
             public void keyPressed(KeyEvent e) 
             {
-                if(e.getKeyCode() == KeyEvent.VK_ENTER)
+                //System.out.println(e.getKeyCode());
+                switch(e.getKeyCode())
                 {
-                    if(e.isShiftDown())
+                    case KeyEvent.VK_ENTER:
                     {
-                        insertAtCaret("\n", null);
-                        e.consume();
+                        if(e.isShiftDown())
+                        {
+                            insertAtCaret("\n", null);
+                            e.consume();
+                        }
+                        else
+                        {
+                            ConsoleUtils.sendMessage("> " + area.getText(), Color.GRAY);
+                            history.add(area.getText());
+                            ScriptControl.startScriptFromCode(area.getText());
+                            area.setText(null);
+                            e.consume();
+                            historyPosition = -1;
+                        }
+                        break;
                     }
-                    else
+                    case KeyEvent.VK_UP:
                     {
-                        ConsoleUtils.sendMessage("> " + area.getText(), Color.GRAY);
-                        ScriptControl.startScriptFromCode(area.getText());
-                        area.setText(null);
-                        e.consume();
+                        if(e.isShiftDown())
+                        {
+                            if(historyPosition == -1)
+                            {
+                                historyPosition = history.size();
+                            }
+                            else if(historyPosition == 0)
+                            {
+                                return;
+                            }
+                            historyPosition--;
+                            area.setText(history.get(historyPosition));
+                        }
+                        break;
+                    }
+                    case KeyEvent.VK_DOWN:
+                    {
+                        if(e.isShiftDown())
+                        {
+                            if(historyPosition == -1)
+                            {
+                                return;
+                            }
+                            else if(historyPosition >= history.size() - 1)
+                            {
+                                historyPosition = -1;
+                                area.setText("");
+                                return;
+                            }
+                            historyPosition++;
+                            area.setText(history.get(historyPosition));
+                        }
+                        break;
                     }
                 }
             }

+ 1 - 1
src/me/hammerle/scheduler/SnuviScheduler.java

@@ -13,7 +13,7 @@ public class SnuviScheduler
     
     public static void doScheduledTask(Runnable r, int delay)
     {
-        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();   
+        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();  
         executor.schedule(r, delay, TimeUnit.MILLISECONDS);
         executor.shutdown();
     }

+ 11 - 2
start.snuvi

@@ -1,4 +1,13 @@
-size = 500;
+x = 0;
+while(x < 256)
+{
+    print(read.color(0,0,x), "■■■■■■■■■■■");
+    x += 1;
+    reset();
+}
+wait();
+
+/*size = 500;
 g.new(size, size);
 x = 0;
 
@@ -9,4 +18,4 @@ while(x < 256)
     reset();
 }
 g.repaint();
-wait();
+wait();*/