Search in sources :

Example 1 with Message

use of org.terasology.engine.logic.console.Message in project Terasology by MovingBlocks.

the class CyclingTabCompletionEngine method complete.

@Override
public String complete(String rawCommand) {
    if (rawCommand.length() <= 0) {
        reset();
        previousMessage = new Message("Type 'help' to list all commands.");
        console.addMessage(previousMessage);
        return null;
    } else if (query == null) {
        query = rawCommand;
    }
    String commandNameRaw = console.processCommandName(query);
    Name commandName = new Name(commandNameRaw);
    List<String> commandParameters = console.processParameters(query);
    ConsoleCommand command = console.getCommand(commandName);
    int suggestedIndex = commandParameters.size() + (query.charAt(query.length() - 1) == ' ' ? 1 : 0);
    Set<String> matches = findMatches(commandName, commandParameters, command, suggestedIndex);
    if (matches == null || matches.size() <= 0) {
        return query;
    }
    if (previousMatches == null || !matches.equals(Sets.newHashSet(previousMatches))) {
        reset(false);
        if (matches.size() == 1) {
            return generateResult(matches.iterator().next(), commandName, commandParameters, suggestedIndex);
        }
        /*            if (matches.length > MAX_CYCLES) {
                console.addMessage(new Message("Too many hits, please refine your search"));
                return query;
            }*/
        // TODO Find out a better way to handle too many results while returning useful information
        previousMatches = Lists.newArrayList(matches);
        Collections.sort(previousMatches);
    }
    StringBuilder matchMessageString = new StringBuilder();
    for (int i = 0; i < previousMatches.size(); i++) {
        if (i > 0) {
            matchMessageString.append(' ');
        }
        String match = previousMatches.get(i);
        if (selectionIndex == i) {
            match = FontColor.getColored(match, ConsoleColors.COMMAND);
        }
        matchMessageString.append(match);
    }
    Message matchMessage = new Message(matchMessageString.toString());
    String suggestion = previousMatches.get(selectionIndex);
    if (previousMessage != null) {
        console.replaceMessage(previousMessage, matchMessage);
    } else {
        console.addMessage(matchMessage);
    }
    previousMessage = matchMessage;
    selectionIndex = (selectionIndex + 1) % previousMatches.size();
    return generateResult(suggestion, commandName, commandParameters, suggestedIndex);
}
Also used : Message(org.terasology.engine.logic.console.Message) ConsoleCommand(org.terasology.engine.logic.console.commandSystem.ConsoleCommand) Name(org.terasology.gestalt.naming.Name)

Example 2 with Message

use of org.terasology.engine.logic.console.Message in project Terasology by MovingBlocks.

the class ConsoleScreen method initialise.

@Override
public void initialise() {
    setAnimationSystem(new SwipeMenuAnimationSystem(0.2f, Direction.TOP_TO_BOTTOM));
    final ScrollableArea scrollArea = find("scrollArea", ScrollableArea.class);
    scrollArea.moveToBottom();
    commandLine = find("commandLine", UICommandEntry.class);
    getManager().setFocus(commandLine);
    commandLine.setTabCompletionEngine(new CyclingTabCompletionEngine(console, localPlayer));
    commandLine.bindCommandHistory(new ReadOnlyBinding<List<String>>() {

        @Override
        public List<String> get() {
            return console.getPreviousCommands();
        }
    });
    commandLine.subscribe(widget -> {
        String text = commandLine.getText();
        if (StringUtils.isNotBlank(text)) {
            console.execute(text, localPlayer.getClientEntity());
        }
        scrollArea.moveToBottom();
    });
    final UIText history = find("messageHistory", UIText.class);
    history.bindText(new ReadOnlyBinding<String>() {

        @Override
        public String get() {
            StringBuilder messageList = new StringBuilder();
            for (Message message : console.getMessages()) {
                messageList.append(FontColor.getColored(message.getMessage(), message.getType().getColor()));
                if (message.hasNewLine()) {
                    messageList.append(Console.NEW_LINE);
                }
            }
            return messageList.toString();
        }
    });
}
Also used : SwipeMenuAnimationSystem(org.terasology.engine.rendering.nui.animation.SwipeMenuAnimationSystem) Message(org.terasology.engine.logic.console.Message) ScrollableArea(org.terasology.nui.layouts.ScrollableArea) UIText(org.terasology.nui.widgets.UIText) List(java.util.List)

Example 3 with Message

use of org.terasology.engine.logic.console.Message in project Terasology by MovingBlocks.

the class ChatScreen method initialise.

@Override
public void initialise() {
    final ScrollableArea scrollArea = find("scrollArea", ScrollableArea.class);
    scrollArea.moveToBottom();
    commandLine = find("commandLine", UIText.class);
    getManager().setFocus(commandLine);
    commandLine.subscribe(widget -> {
        String text = commandLine.getText();
        if (StringUtils.isNotBlank(text)) {
            String command = "say";
            List<String> params = Collections.singletonList(text);
            // TODO: move command execution to separate class
            console.execute(new Name(command), params, localPlayer.getClientEntity());
            commandLine.setText("");
            scrollArea.moveToBottom();
            NotificationOverlay overlay = nuiManager.addOverlay(NotificationOverlay.ASSET_URI, NotificationOverlay.class);
            overlay.setVisible(true);
            nuiManager.closeScreen(this);
        } else {
            commandLine.setText("");
            nuiManager.closeScreen(this);
        }
    });
    final UILabel history = find("messageHistory", UILabel.class);
    history.bindText(new ReadOnlyBinding<String>() {

        @Override
        public String get() {
            Iterable<Message> messageIterable = console.getMessages(CoreMessageType.CHAT, CoreMessageType.NOTIFICATION);
            Stream<Message> messageStream = StreamSupport.stream(messageIterable.spliterator(), false);
            return messageStream.map(Message::getMessage).collect(Collectors.joining(Console.NEW_LINE));
        }
    });
}
Also used : UILabel(org.terasology.nui.widgets.UILabel) Message(org.terasology.engine.logic.console.Message) ScrollableArea(org.terasology.nui.layouts.ScrollableArea) UIText(org.terasology.nui.widgets.UIText) Stream(java.util.stream.Stream) Name(org.terasology.gestalt.naming.Name)

Aggregations

Message (org.terasology.engine.logic.console.Message)3 Name (org.terasology.gestalt.naming.Name)2 ScrollableArea (org.terasology.nui.layouts.ScrollableArea)2 UIText (org.terasology.nui.widgets.UIText)2 List (java.util.List)1 Stream (java.util.stream.Stream)1 ConsoleCommand (org.terasology.engine.logic.console.commandSystem.ConsoleCommand)1 SwipeMenuAnimationSystem (org.terasology.engine.rendering.nui.animation.SwipeMenuAnimationSystem)1 UILabel (org.terasology.nui.widgets.UILabel)1