0.1.1 • Published 8 years ago

cordova-plugin-canvas-gamepad v0.1.1

Weekly downloads
3
License
Apache 2.0
Repository
github
Last release
8 years ago

#CDVGamepad

So you want to add a gamepad to a html5/canvas based app in cordova

npm i cordova-plugin-canvas-gamepad

##Cordova

###The Basics

install cordova

[sudo] npm install -g cordova

navigate to the directory where you want to create your app

cd [directory]

create an app

cordova create [app-directory] [reverse domain-style identifier] [application display name] && cd [app-directory]

add CDVGamepad plugin

cordova plugin add cordova-plugin-canvas-gamepad

iOS9 long press fix (optional)

this prevents the copy/select bubble from coming up in games where the user needs to press and hold)

cordova plugin add cordova-plugin-ios-longpress-fix

Add some platforms

cordova platform add ios
cordova platform add android
cordova platform add amazon-fireos
cordova platform add blackberry10
cordova platform add firefoxos

Prepare your platforms

prepare platform add ios
prepare platform add android
prepare platform add amazon-fireos
prepare platform add blackberry10
prepare platform add firefoxos

###CDVGamepad setup and configurations

in you cordova index.js add CDVGamepad.setup()

/*
** this is a basic joystick and 1 button setup with start and select buttons
*/
onDeviceReady: function() {
	CDVGamepad.setup();
}

##Configuration options

CDVGamepad is fully customizable, from button names, colors, layout and more.

propertytypevalue(s)descriptionexample
debugbooleantrue|falseshow or hide event debug infodefault is falsedebug:false
tracebooleantrue|falseshow or hide gamepad trace infodefault is falsetrace:false
canvasstringid of target canvasif left out, creates a new canvas objectcanvas:"game"
buttonsarray[]collection of button objects[{name:"x",color:"rgba(255,255,0,0.5)"}]
buttonobject{name:string,color:hex|rgb|rgba}properties for custom buttons[{name:"x",color:"rgba(255,255,0,0.5)"},{name:"y",color:"rgba(255,0,255,0.5)"}]
layoutstringTOP_LEFT | TOP_RIGHT | BOTTOM_LEFT | BOTTOM_RIGHTcardinal position of buttons*default is BOTTOM_RIGHT*layout:"BOTTOM_RIGHT"
startbooleantrue|falsedisplay start buttondefault is truestart:false
selectbooleantrue|falsedisplay select buttondefault is falseselect:false
joystickbooleantrue|falsedisplay joystick/dpaddefault is falsedebug:false
hiddenbooleantrue|falseshow or hide the gamepaddefault is falsethis can be used to hide the gamepad if you are doing something else on screenhidden:false

###Config examples ######default options

default options

CDVGamepad.setup();

######one button, custom name, no start button

default options

CDVGamepad.setup({
	start:false,
	buttons:[
		{name:"jump"}
	]
});

######two buttons, custom names, custom colors, with select button

default options

CDVGamepad.setup({
	select:true,
	buttons:[
		{name:"x",color:"rgba(255,255,0,0.5)"},
		{name:"y",color:"rgba(0,255,255,0.75)"}
	]
});

######target canvas

default options

CDVGamepad.setup({
	canvas:"game"
});

######change layout canvas

default options

CDVGamepad.setup({
	layout:"BOTTOM_LEFT"
});

######show trace & debug info

default options

CDVGamepad.setup({
	trace:true,
	debug:true
});

######all out everything

default options

CDVGamepad.setup({
	select:true,
	trace:true,
	debug:true,
	canvas:"game",
	buttons:[
		{name:"z", color:"#17861c"},
		{name:"y", color:"rgb(134, 83, 23)"},
		{name:"x", color:"rgba(204, 0, 51, 0.5)"},		
	]
});

######hidden gamepad

default options

CDVGamepad.setup({
	hidden:true
});

######real world example

default options

onDeviceReady: function() {
		/*
		** @description start the game
		*/
    game.init();
		/*
		** @description setup gamepad, no stick, no start, one button
		*/    
    CDVGamepad.setup({
    	canvas:"controller",
    	joystick:false,
    	start:false, 
    	buttons:[
    		{name:"jump", color:"rgba(0,0,0,0.25)"}
    	]
    });    
}

###CDVGamepad observable method

CDVGamepad has an observable method that returns the current state map of the gamepad

observe();

CDVGamepad.setup()
/*
** @description the below example simply logs out the observe method return
*/
setInterval(
	function()
	{
		var map = CDVGamepad.observe();
		console.log(new Date() + ":" + JSON.stringify(map))
	}
	,1000
);
/*
** @description additionally, you can throw it into your main loop in canvas
*/
function draw()
{
	if(CDVGamepad)
	{
		gamepad(CDVGamepad.observe())
	}
}