We use proprietary and third party's cookies to improve your experience and our services, identifying your Internet Browsing preferences on our website; develop analytic activities and display advertising based on your preferences. If you keep browsing, you accept its use. You can get more information on our Cookie Policy
Cookies Policy
2D-UI Open API Specification - FIWARE Forge Wiki

2D-UI Open API Specification

From FIWARE Forge Wiki

Jump to: navigation, search

Contents

InputAPI

Javascript library that enables registering/unregistering to receive input events fired by e.g. touch pad, gamepad, keyboard and mouse. API is extendable via plugin system and input abstraction. As default API provides mouse and keyboard event handling. By default plugins for touch pad and gamepad are also supported.

  • Instantiate InputAPI
var inputAPI = new InputAPI({
	//Give container for event registering. Not mandatory. Register can be done manually.
	container: "#my-container"
});
  • Create hooks for signals
function onTouchEvent (obj, event)
{
	console.log("Touch event "+event);
}

function onKeyEvent(event)
{
	console.log("Keyboard event "+event);
}

function onMouseEvent(event)
{
	console.log("Mouse event "+event);
}

function onGamepadEvent(event)
{
	console.log("Gamepad event "+event);
}

function onGamepadStatusEvent(event)
{
	console.log("GamepadStatus event "+event);
}
  • Hook to mouse signals
inputAPI.mouseEvent.add(onMouseEvent);
  • Hook to keyboard signals
inputAPI.keyEvent.add(onKeyEvent);
  • Hook to touch plugin
var touch = inputAPI.getPlugin("Touch");
if (touch)
{
	touch.touchEvent.add(onTouchEvent);
}
  • Hook to gamepad plugin
var gamepad = inputAPI.getPlugin("Gamepad");
if (gamepad)
{
	//Ask if gamepad is supported
	if (gamepad.isBrowserSupported())
	{
		console.log("Gamepad supported");
	}
	else
	{
		console.log("Gamepad not supported");
	}

	gamepad.gamepadEvent.add(onGamepadEvent);
	gamepad.gamepadStatusEvent.add(onGamepadStatusEvent);
}

IInputPlugin

Extend IInputPlugin -class and create your own InputMyPlugin.js -file:

var IInputPlugin = Class.$extend(
{
    __init__ : function(name)
    {
        if (name === undefined)
        {
            console.error("[IInputPlugin]: Constructor called without a plugin name!");
            name = "Unknown";
        }
        this.name = name;
        this.running = false;
    },

    __classvars__ :
    {
        register : function()
        {
            var plugin = new this();
            InputAPI.registerPlugin(plugin);
        }
    },

    _start : function(container)
    {
        this.start(container);
        this.running = true;
    },

    start : function()
    {
        console.log("[IInputPlugin]: Plugin '" + name + "' has not implemented start()");
    },

    _stop : function()
    {
        this.stop();
        this.running = false;
    },

    stop : function()
    {
        console.log("[IInputPlugin]: Plugin '" + name + "' has not implemented stop()");
    },

    reset : function()
    {
    }
});
  • Register IInputPlugin

Place created plugin file reference after InputAPI.js -reference on your html -page:

<!-- Input SDK -->
<script src="../src/InputAPI.js"></script>
<!-- Touch plugin-->
<script src="../src/InputTouchPlugin.js"></script>
<!-- Gamepad plugin -->
<script src="../src/InputGamepadPlugin.js"></script>

InputAbstraction

  • Include InputState.js to your html -page
<!-- InputState -->
<script src="../src/InputState.js"></script>
  • Create input state
//InputState 1
var inputState = new InputState ({
	name : "Forward",
	keyBindings : ["w"],
	mouseDown : null,
	timeslot : 0
});
  • Register InputState
var inputStateSignal = inputAPI.registerInputState(inputState);
  • Create hook for signal returned
function onInputSignal(event)
{
	console.log("InputState "+inputState.name+" fired!");
}
  • Hook signal returned from registering
inputStateSignal.add(onInputSignal);
  • Update InputState
inputState.setName("new name");
inputState.setKeyBindings(["s"]);
inputState.setMouseDown(1);
inputState.setTimeslot(0);
inputState.setPriority(100);
inputState.setMultiplier(0)
inputAPI.updateInputState(inputState);
Personal tools
Create a book