Package org.terasology.logic.console.ui

Source Code of org.terasology.logic.console.ui.ConsoleScreen

/*
* Copyright 2014 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.terasology.logic.console.ui;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.terasology.input.MouseInput;
import org.terasology.logic.console.Console;
import org.terasology.logic.console.Message;
import org.terasology.logic.console.internal.CommandInfo;
import org.terasology.logic.players.LocalPlayer;
import org.terasology.math.Vector2i;
import org.terasology.registry.In;
import org.terasology.rendering.FontColor;
import org.terasology.rendering.nui.BaseInteractionListener;
import org.terasology.rendering.nui.CoreScreenLayer;
import org.terasology.rendering.nui.InteractionListener;
import org.terasology.rendering.nui.UIWidget;
import org.terasology.rendering.nui.databinding.ReadOnlyBinding;
import org.terasology.rendering.nui.layouts.ScrollableArea;
import org.terasology.rendering.nui.widgets.ActivateEventListener;
import org.terasology.rendering.nui.widgets.UIText;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;

/**
* @author Immortius
*/
public class ConsoleScreen extends CoreScreenLayer {

    @In
    private Console console;

    @In
    private LocalPlayer localPlayer;

    private UICommandEntry commandLine;

    private boolean welcomePrinted;

    private InteractionListener screenListener = new BaseInteractionListener() {
        @Override
        public boolean onMouseClick(MouseInput button, Vector2i pos) {
            if (button == MouseInput.MOUSE_LEFT && commandLine != null) {
                getManager().setFocus(commandLine);
            }
            return true;
        }
    };

    @Override
    public void initialise() {
        final ScrollableArea scrollArea = find("scrollArea", ScrollableArea.class);
        scrollArea.moveToBottom();

        List<CommandInfo> commands = console.getCommandList();
       
        // JAVA8: replace with lamba expression
        Collection<String> commandNames = Collections2.transform(commands, new Function<CommandInfo, String>() {

            @Override
            public String apply(CommandInfo input) {
                return input.getName();
            }
        });

        commandLine = find("commandLine", UICommandEntry.class);
        getManager().setFocus(commandLine);
        commandLine.setTabCompletionEngine(new CyclingTabCompletionEngine(console, commandNames));
        commandLine.bindCommandHistory(new ReadOnlyBinding<List<String>>() {
            @Override
            public List<String> get() {
                return console.getPreviousCommands();
            }
        });
        commandLine.subscribe(new ActivateEventListener() {
            @Override
            public void onActivated(UIWidget widget) {
                console.execute(commandLine.getText(), localPlayer.getClientEntity());
                commandLine.setText("");
                scrollArea.moveToBottom();
            }
        });

        final UIText history = find("messageHistory", UIText.class);
        history.bindText(new ReadOnlyBinding<String>() {
            @Override
            public String get() {
                StringBuilder messageList = new StringBuilder();
                Iterator<Message> messageIterator = console.getMessages().iterator();
                while (messageIterator.hasNext()) {
                    Message message = messageIterator.next();
                    messageList.append(FontColor.getColored(message.getMessage(), message.getType().getColor()));
                    messageList.append(Message.NEW_LINE);
                }
                return messageList.toString();
            }
        });
    }

    @Override
    public void onOpened() {
        super.onOpened();
        getManager().setFocus(commandLine);

        if (!welcomePrinted) {
            console.addMessage("Welcome to the wonderful world of Terasology!" + Message.NEW_LINE +
                Message.NEW_LINE +
                "Type 'help' to see a list with available commands or 'help \"<commandName>\"' for command details." + Message.NEW_LINE +
                "Text parameters should be in quotes, no commas needed between multiple parameters." + Message.NEW_LINE +
                "Commands are case-sensitive, block names and such are not." + Message.NEW_LINE +
                "You can use auto-completion by typing a partial command then hitting 'tab' - examples:" + Message.NEW_LINE +
                "'gh' + 'tab' = 'ghost'" + Message.NEW_LINE +
                "'lS' + 'tab' = 'listShapes' (camel casing abbreviated commands)" + Message.NEW_LINE);
            welcomePrinted = true;
        }
    }

    @Override
    protected InteractionListener getScreenListener() {
        return screenListener;
    }

    @Override
    public boolean canBeFocus() {
        return false;
    }
}
TOP

Related Classes of org.terasology.logic.console.ui.ConsoleScreen

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.