Code Search for Developers
 
 
  

ConsoleEngine.java from Riot in Emerald city at Krugle


Show ConsoleEngine.java syntax highlighted

/*
 * Copyright (C) 2001-2005 Pleasant nightmare studio
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.pleasantnightmare.riotinecity.console;

import com.jme.input.InputHandler;
import com.jme.input.KeyInput;
import com.jme.input.action.InputAction;
import com.jme.input.action.InputActionEvent;
import com.jme.scene.Node;
import org.pleasantnightmare.riotinecity.spatials.InteractibleNode;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author deus
 * @version 1.0
 * @since Mar 23, 2007 9:21:26 PM
 */
public class ConsoleEngine extends InputHandler {
    private static final Pattern INPUT_PATTERN = Pattern.compile(".*?#> (.+)");
    private ConsoleModel model;
    private Set<InputProcessor> inputProcessors;
    private Node root;
    private InteractibleNode selectedInteractible;
    private static final String PROMPT = "/#> ";


    public ConsoleEngine(ConsoleModel model, Node root) {
        this.root = root;
        this.model = model;
        model.append(getPrompt());
        inputProcessors();
        keyboard();
        enter();
        backspace();
    }

    private void keyboard() {
        addAction(new InputAction() {
            public void performAction(InputActionEvent evt) {
                if (!evt.getTriggerPressed())
                    return;
                char keychar = evt.getTriggerCharacter();
                if(keychar == '`')
                    return;
                if (keychar >= '0' && keychar <= 'z' || keychar == ' ')
                    model.append(keychar);
            }
        }, InputHandler.DEVICE_KEYBOARD, InputHandler.BUTTON_ALL, InputHandler.AXIS_NONE, false);
    }

    private void enter() {
        addAction(new InputAction() {
            public void performAction(InputActionEvent evt) {
                if (!evt.getTriggerPressed())
                    return;

                model.newLine();
                commitInput(model.getCommitedLine());
                model.append(getPrompt());
            }
        }, InputHandler.DEVICE_KEYBOARD, KeyInput.KEY_RETURN, InputHandler.AXIS_NONE, false);
    }

    private void backspace() {
        addAction(new InputAction() {
            public void performAction(InputActionEvent evt) {
                if (!evt.getTriggerPressed())
                    return;
                if (model.getCurrentLine().length() > getPrompt().length())
                    model.backspace();
            }
        }, InputHandler.DEVICE_KEYBOARD, KeyInput.KEY_BACK, InputHandler.AXIS_NONE, true);
    }

    private void inputProcessors() {
        inputProcessors = new HashSet<InputProcessor>(4);
        inputProcessors.add(new Test());
        inputProcessors.add(new ActionList());
        inputProcessors.add(new Select());
        inputProcessors.add(new Help());
        inputProcessors.add(new Call());

        for (InputProcessor inputProcessor : inputProcessors) {
            inputProcessor.setModel(model);
            inputProcessor.setEngine(this);
        }
    }

    private String getPrompt() {
        if (selectedInteractible != null)
            return selectedInteractible.getName() + PROMPT;
        else
            return PROMPT;
    }

    private void commitInput(String line) {
        String input = cleanInput(line);
        if (input == null)
            return;

        try {
            // Try finding console command
            for (InputProcessor inputProcessor : inputProcessors)
                if (inputProcessor.isProcessable(input)) {
                    inputProcessor.setRootNode(root);
                    inputProcessor.process(input);
                    return;
                }

            // If we're here, there's no processor to process our request
            model.writeLine("Bad command: " + input);
        } catch (ProcessingError processingError) {
            model.writeLine(processingError.getMessage());
        }
    }

    private String cleanInput(String input) {
        Matcher m = INPUT_PATTERN.matcher(input);
        if (!m.matches())
            return null;
        return m.group(1);
    }

    public void select(InteractibleNode node) {
        selectedInteractible = node;
    }

    public InteractibleNode getSelected() {
        return selectedInteractible;
    }
}




See more files for this project here

Riot in Emerald city

Cyberpunk third-person 2100. noir RPG thriller using JMonkey. Its going to be mix between Bloodnet, Neuromancer, DeusEx in gameplay, with pieces of Uplink and many other mini-games for dealing with variety of tasks (lockpicking, hacking...)

Project homepage: http://sourceforge.net/projects/riot-in-ecity
Programming language(s): Java,XML
License: other

  ActionList.java
  Call.java
  ConsoleEngine.java
  ConsoleModel.java
  ConsoleSpatial.java
  ConsoleToggleAction.java
  Help.java
  InputProcessor.java
  ProcessingError.java
  Select.java
  Test.java