Search in sources :

Example 1 with TextCharacter

use of com.googlecode.lanterna.TextCharacter in project java-docs-samples by GoogleCloudPlatform.

the class MqttCommandsDemo method startGui.

public static void startGui(Screen screen, TextColor theColor) throws IOException {
    try {
        /*
      You can use the DefaultTerminalFactory to create a Screen, this will generally give you the
      TerminalScreen implementation that is probably what you want to use. Please see
      VirtualScreen for more details on a separate implementation that allows you to create a
      terminal surface that is bigger than the physical size of the terminal emulator the software
      is running in. Just to demonstrate that a Screen sits on top of a Terminal,
      we are going to create one manually instead of using DefaultTerminalFactory.
       */
        /*
      Screens will only work in private mode and while you can call methods to mutate its state,
      before you can make any of these changes visible, you'll need to call startScreen()
      which will prepare and setup the terminal.
       */
        screen.startScreen();
        System.out.println("Starting the terminal...");
        /*
      Let's turn off the cursor for this tutorial
       */
        screen.setCursorPosition(null);
        /*
      Now let's draw some random content in the screen buffer
       */
        TerminalSize terminalSize = screen.getTerminalSize();
        for (int column = 0; column < terminalSize.getColumns(); column++) {
            for (int row = 0; row < terminalSize.getRows(); row++) {
                screen.setCharacter(column, row, new TextCharacter(' ', TextColor.ANSI.DEFAULT, // This will pick a random background color
                theColor));
            }
        }
        /*
      So at this point, we've only modified the back buffer in the screen, nothing is visible yet.
      In order to move the content from the back buffer to the front buffer and refresh the
      screen, we need to call refresh()
       */
        screen.refresh();
        System.out.println("Starting the terminal...");
        /*
      Ok, now we loop and keep modifying the screen until the user exits by pressing escape on
      the keyboard or the input stream is closed. When using the Swing/AWT bundled emulator,
      if the user closes the window this will result in an EOF KeyStroke.
       */
        while (true) {
            KeyStroke keyStroke = screen.pollInput();
            if (keyStroke != null && (keyStroke.getKeyType() == KeyType.Escape || keyStroke.getKeyType() == KeyType.EOF)) {
                break;
            }
            /*
        Screens will automatically listen and record size changes, but you have to let the Screen
        know when is a good time to update its internal buffers. Usually you should do this at the
        start of your "drawing" loop, if you have one. This ensures that the dimensions of the
        buffers stays constant and doesn't change while you are drawing content. The method
        doReizeIfNecessary() will check if the terminal has been resized since last time it
        was called (or since the screen was created if this is the first time calling) and
        update the buffer dimensions accordingly. It returns null if the terminal
        has not changed size since last time.
         */
            TerminalSize newSize = screen.doResizeIfNecessary();
            if (newSize != null) {
                terminalSize = newSize;
            }
            /*
        Just like with Terminal, it's probably easier to draw using TextGraphics.
        Let's do that to put a little box with information on the size of the terminal window
         */
            String sizeLabel = "Terminal Size: " + terminalSize;
            TerminalPosition labelBoxTopLeft = new TerminalPosition(1, 1);
            TerminalSize labelBoxSize = new TerminalSize(sizeLabel.length() + 2, 3);
            TerminalPosition labelBoxTopRightCorner = labelBoxTopLeft.withRelativeColumn(labelBoxSize.getColumns() - 1);
            TextGraphics textGraphics = screen.newTextGraphics();
            // This isn't really needed as we are overwriting everything below anyway, but just for
            // demonstrative purpose
            textGraphics.fillRectangle(labelBoxTopLeft, labelBoxSize, ' ');
            /*
        Draw horizontal lines, first upper then lower
         */
            textGraphics.drawLine(labelBoxTopLeft.withRelativeColumn(1), labelBoxTopLeft.withRelativeColumn(labelBoxSize.getColumns() - 2), Symbols.DOUBLE_LINE_HORIZONTAL);
            textGraphics.drawLine(labelBoxTopLeft.withRelativeRow(2).withRelativeColumn(1), labelBoxTopLeft.withRelativeRow(2).withRelativeColumn(labelBoxSize.getColumns() - 2), Symbols.DOUBLE_LINE_HORIZONTAL);
            /*
        Manually do the edges and (since it's only one) the vertical lines,
        first on the left then on the right
         */
            textGraphics.setCharacter(labelBoxTopLeft, Symbols.DOUBLE_LINE_TOP_LEFT_CORNER);
            textGraphics.setCharacter(labelBoxTopLeft.withRelativeRow(1), Symbols.DOUBLE_LINE_VERTICAL);
            textGraphics.setCharacter(labelBoxTopLeft.withRelativeRow(2), Symbols.DOUBLE_LINE_BOTTOM_LEFT_CORNER);
            textGraphics.setCharacter(labelBoxTopRightCorner, Symbols.DOUBLE_LINE_TOP_RIGHT_CORNER);
            textGraphics.setCharacter(labelBoxTopRightCorner.withRelativeRow(1), Symbols.DOUBLE_LINE_VERTICAL);
            textGraphics.setCharacter(labelBoxTopRightCorner.withRelativeRow(2), Symbols.DOUBLE_LINE_BOTTOM_RIGHT_CORNER);
            /*
        Finally put the text inside the box
         */
            textGraphics.putString(labelBoxTopLeft.withRelative(1, 1), sizeLabel);
            /*
        Ok, we are done and can display the change. Let's also be nice and allow the OS
        to schedule other threads so we don't clog up the core completely.
         */
            screen.refresh();
            Thread.yield();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (screen != null) {
            try {
                /*
          The close() call here will restore the terminal by exiting from private mode which
          was done in the call to startScreen()
           */
                screen.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    screen.stopScreen();
}
Also used : TextCharacter(com.googlecode.lanterna.TextCharacter) TextGraphics(com.googlecode.lanterna.graphics.TextGraphics) KeyStroke(com.googlecode.lanterna.input.KeyStroke) TerminalSize(com.googlecode.lanterna.TerminalSize) TerminalPosition(com.googlecode.lanterna.TerminalPosition) IOException(java.io.IOException)

Example 2 with TextCharacter

use of com.googlecode.lanterna.TextCharacter in project narchy by automenta.

the class ConsoleSurface method doPaint.

void doPaint(GL2 gl) {
    float charScaleX = this.charScaleX;
    float charScaleY = this.charScaleY;
    // HACK
    long t = System.currentTimeMillis();
    float dz = 0f;
    gl.glPushMatrix();
    gl.glScalef(1f / (cols), 1f / (rows), 1f);
    // background extents
    // gl.glColor3f(0.25f, 0.25f, 0.25f);
    // Draw.rect(gl, 0, 0, cols, rows);
    gl.glLineWidth(fontThickness);
    // gl.glColor4f(0.75f, 0.75f, 0.75f, fgAlpha);
    bg = TRANSLUCENT;
    for (int row = 0; row < rows; row++) {
        gl.glPushMatrix();
        Draw.textStart(gl, charScaleX, charScaleY, // 0, (rows - 1 - jj) * charAspect,
        0.5f, (rows - 1 - row), dz);
        for (int col = 0; col < cols; col++) {
            TextCharacter c = charAt(col, row);
            if (setBackgroundColor(gl, c, col, row)) {
                Draw.rect(gl, Math.round((col - 0.5f) * 20 / charScaleX), 0, Math.round(20f / charScaleX), 24);
            }
            // if (c == null)
            // continue;
            // TODO: Background color
            char cc = visible(c.getCharacter());
            if (cc != 0) {
                // gl.glColor3f(1f, 1f, 1f);
                // TODO TextColor fg = c.getForegroundColor();
                // TODO: if (!fg.equals(previousFG))
                Color fg = c.getForegroundColor().toColor();
                gl.glColor4f(fg.getRed() / 256f, fg.getGreen() / 256f, fg.getBlue() / 256f, fgAlpha);
                Draw.textNext(gl, cc, col / charScaleX);
            }
        }
        // next line
        gl.glPopMatrix();
    }
    int[] cursor = getCursorPos();
    int curx = cursor[0];
    int cury = cursor[1];
    // DRAW CURSOR
    float p = (1f + (float) Math.sin(t / 100.0)) * 0.5f;
    float m = (p);
    gl.glColor4f(1f, 0.7f, 0f, 0.4f + p * 0.4f);
    Draw.rect(gl, (float) (curx) + m / 2f, (rows - 1 - cury), 1 - m, (1 - m), -dz);
    gl.glPopMatrix();
}
Also used : TextCharacter(com.googlecode.lanterna.TextCharacter)

Aggregations

TextCharacter (com.googlecode.lanterna.TextCharacter)2 TerminalPosition (com.googlecode.lanterna.TerminalPosition)1 TerminalSize (com.googlecode.lanterna.TerminalSize)1 TextGraphics (com.googlecode.lanterna.graphics.TextGraphics)1 KeyStroke (com.googlecode.lanterna.input.KeyStroke)1 IOException (java.io.IOException)1