LibGDX In 10 Minute Slices: Adding an In-Game Console

Add your Gradle dependencies:

implementation "com.strongjoshua:libgdx-inGameConsole:1.0.0"

Code: ConsoleDemo

package com.consoledemo.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.strongjoshua.console.CommandExecutor;
import com.strongjoshua.console.Console;
import com.strongjoshua.console.GUIConsole;

public class ConsoleCommands extends CommandExecutor {

    private static GUIConsole console;

    public ConsoleCommands(){

        Skin skin = new Skin(Gdx.files.internal("console_skin/uiskin.json"), new TextureAtlas(Gdx.files.internal("console_skin/uiskin.atlas")));
        console = new GUIConsole(skin, false);
        console.setCommandExecutor(this);
        console.setSizePercent(50, 50);
        console.enableSubmitButton(true);
    }

    public static Console getConsole(){
        return console;
    }

    public static void draw(){
        if (console.isVisible()) {
            console.draw();
        }
    }

    public void test(){
        Gdx.app.log("Hi!","I am your friendly console");
    }

    public void hide(){
        console.setVisible(false);
    }
}

Code: UserInput

package com.consoledemo.game;

import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;

public class UserInput implements InputProcessor{

    public UserInput(){

    }

    @Override
    public boolean keyDown(int keycode) {

        if ((keycode == Input.Keys.TAB) )
        {
            ConsoleCommands.getConsole().setVisible(true);
            ConsoleCommands.getConsole().select();
        }
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(float amountX, float amountY) {
        return false;
    }
}

Code: ConsoleCommands

package com.consoledemo.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.strongjoshua.console.CommandExecutor;
import com.strongjoshua.console.Console;
import com.strongjoshua.console.GUIConsole;

public class ConsoleCommands extends CommandExecutor {

    private static GUIConsole console;

    public ConsoleCommands(){

        Skin skin = new Skin(Gdx.files.internal("console_skin/uiskin.json"), new TextureAtlas(Gdx.files.internal("console_skin/uiskin.atlas")));
        console = new GUIConsole(skin, false);
        console.setCommandExecutor(this);
        console.setSizePercent(50, 50);
        console.enableSubmitButton(true);
    }

    public static Console getConsole(){
        return console;
    }

    public static void draw(){
        if (console.isVisible()) {
            console.draw();
        }
    }

    public void test(){
        Gdx.app.log("Hi!","I am your friendly console");
    }

    public void hide(){
        console.setVisible(false);
    }
}
0
YOUR CART
  • No products in the cart.