Constructor
new ControllerPad(holder, x, y, w, h, leftp, rightp, upp, downp, startp, backp, ap, bp, cp, dp, directionButtonsWidth, directionButtonsHeight, startAndBackWidth, startAndBackHeight, buttonWidth, buttonHeight)
Name | Type | Description |
---|---|---|
holder |
HTMLElement | The html div that is to hold the controller |
x |
Number | placeholder for x, actual position is done during the show method. |
y |
Number | placeholder for y |
w |
Number | placeholder for width |
h |
Number | placeholder for height |
leftp |
MoverPoint | MoverPoint defining the middle point of the left button |
rightp |
MoverPoint | MoverPoint defining the middle point of the right button |
upp |
MoverPoint | |
downp |
MoverPoint | |
startp |
MoverPoint | |
backp |
MoverPoint | |
ap |
MoverPoint | MoverPoint defining the middle point of the a button |
bp |
MoverPoint | |
cp |
MoverPoint | |
dp |
MoverPoint | |
directionButtonsWidth |
Number | The width of each directional button |
directionButtonsHeight |
Number | |
startAndBackWidth |
Number | The width of the start and back buttons. |
startAndBackHeight |
Number | |
buttonWidth |
Number | The width of each letter button, a,b,c,d. |
buttonHeight |
Number |
Members
(static) instance
The instance of the ControllerPad, set during construction.
_addedButtonRectStyle :String
Optional additional css to add to each buttons style.
- String
_buttonRectColor :String
Defines the html button color used during showButtons.
- String
_dontAbsolutePositionHolder
Don't absolute position the controllers holder div. if you are positioning the controller in a special way you may need to set this so that the holder divs position style is not auto set to absolute.
_gpb
._gpb is the private mirror of .gamePadButtons. (in JavaScript many developers will use an underscore to denote private or static vars, which is the case in tbgs.js. ControllerPad.js is using them as setter/getters, so very strictly not supposed to be changed, because that JavaScript can't do real getters/setters in all browers) Your not supposed to change ._gpb, together with .configureGamePadButtons, .gamePadButtons therefore opperates as a setter. Call .configureGamePadButtons() during a loop to properly set up buttons based on the users usb controller inputs. There is no better way to accomplish setting up the buttons in a universal manner, because each controller may send out different numbers for each button, therefore the best thing to do is let the user press the buttons first, and assign them that way, all that is set up already and done for you by simply calling .configureGamePadButtons in a loop, see .handleGamePad.
arrows
defines the keyboard equivalents for the other controllers besides basicController.
basicArrows
defines the keyboard equivalents for basicController
basicWasd
defines the keyboard equivalents for basicController
buttons
test as follows: if(controller.buttons.right) { move right code }
centerRotationX
The center x of the rotation pads circle on the whole screen. For example, if the game has a width of 496, the middle of the rotation circle at full screen is about 64 to 120. If the game is not full screen you will need to calculate where the game edge starts and count from there plus about 64 to 120. This number should define the middle x of the rotation circle image when the controller is shown.
centerRotationX and centerRotationY are pretty much arrived at by trial and error.
But the general formula when fullscreen is:
centerRotationX = 0 + 64;
centerRotationY = gameHeight + (this._scaleRectRef.height/(this._scaleRectRef.height/gameHeight)) - 64;
('this' would be a setup inherited instance of the GameSkeleton which would have _scaleRectRef available)
And the general formula for when not full screen is:
var fif = window.innerWidth/2;//50%
var containerleft = fif - 248;//50% - half game width is what the container is positioned at when using the GameSkeleton Class.
controller.centerRotationX = containerleft + 64;
controller.centerRotationY = gameHeight + 64;
Use the above as general guides, test in dev tools and adjust as needed. The controller should generally work in dev tools with your finger able to rotate around the black part of the rotation circle. Arrive at definite numbers and it should work at all sizes, so generally you would just need to change the 64's in the formulas.
centerRotationY
The center y of the rotation pads circle on the whole screen. see centerRotationX
gamePadButtons
the pad.buttons[index] of each game pad button In general you should not change these yourself, unless you know the specific usb controller your players will be using. Use .configureGamePadButtons when you don't know what specific usb game pad your users will have. As is, these indexes are the numbers given by the Radio Shack Playstation controller to usb converter, obviously it is most likely that players will have many different types of usb controllers.
keyboardEquivalents
defines keyboard equivalents for button presses
wasd
defines the keyboard equivalents for the other controllers besides basicController.
Methods
_basicControllerButtonTakedown()
_customControllerButtonTakedown()
_directionalsControllerButtonTakedown()
_preHandleKeys()
For override, called before keyCode checks during handleKeys. let's say you wanted users to be able to use either wasd or arrows then you can overide ._preHandleKeys and do for example; if(e.keyCode == 39 || e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 40) { controller.keyboardEquivalents = controller.arrows; } else { controller.keyboardEquivalents = controller.wasd; } or you wanted the keyboard equivalents to change based on the level or some other factor, then this overideable blank function is for that it gets called just before any checks of keyboardEquivalents are done.
If you want both WASD and Arrows to be used, you can also just call .acceptWASDAndArrows, which overrides ._preHandleKeys for you.
_rotationalControllerButtonTakedown()
_selfStyle()
Used internally
_standardControllerButtonTakedown()
acceptWASDAndArrows()
Sets up _preHandleKeys to change keyboardEquivalents based on what is pressed wasd or arrows. Allowing for both to be used.
addCustomImage(imgLocationopt, customStyleName, imgElementopt)
Ads a custom background image for the controller
Name | Type | Attributes | Description |
---|---|---|---|
imgLocation |
String |
<optional> |
The location of an image to use as the background |
customStyleName |
String | The html style id for the custom background |
|
imgElement |
Image |
<optional> |
If present will copy from this image using its full width and height. |
assignStartAndBackMethods(startMethodString, backMethodString, methodObjectRef)
Assings default methods for start and back button presses.
Name | Type | Description |
---|---|---|
startMethodString |
String | name of start method to call |
backMethodString |
String | name of back method to call |
methodObjectRef |
Object | Object that has the start and back methods |
basicControllerButtonSetup()
For use with ControllerPad.css, this setup does not include a specific up down or D buttons. You can still assign any one of the buttons to do anything, just that the specific 'up' 'down' and 'D' buttons are not set up via this method. This image has a big left and right button, and big A B and C buttons with start and back buttons in the middle.
Used with .show(w,h) (no style param passed) These methods automagically assign the button positions, you need to also call tabageos.MouseController.defineMousePositionOffset during any resizing of the games container, or use the tabageos.Resize method passing in an intance of the controller.
changeTouches()
Used internally
configureGamePadButtons()
To be called during a loop. see .handleGamePad
customControllerButtonSetup(leftp, rightp, upp, downp, startp, backp, ap, bp, cp, dp, directionButtonsWidth, directionButtonsHeight, buttonWidth, buttonHeight, totalWidth, totalHeight)
Set up a custom controller pad. Each MoverPoint param defines the x,y position of each button in relation to the top corner of the customImage, set with addCustomImage. The top left point of each button in your image, the top left corner of the image being 0,0.
Name | Type | Description |
---|---|---|
leftp |
MoverPoint | |
rightp |
MoverPoint | |
upp |
MoverPoint | |
downp |
MoverPoint | |
startp |
MoverPoint | |
backp |
MoverPoint | |
ap |
MoverPoint | |
bp |
MoverPoint | |
cp |
MoverPoint | |
dp |
MoverPoint | |
directionButtonsWidth |
Number | |
directionButtonsHeight |
Number | |
buttonWidth |
Number | |
buttonHeight |
Number | |
totalWidth |
Number | |
totalHeight |
Number |
destroy(startMethodString, backMethodString, methodObjectRef)
Trys to destroy the ControllerPad instance
Name | Type | Description |
---|---|---|
startMethodString |
String | optional name of start method that was added via assignStartAndBackMethods |
backMethodString |
String | optional name of back method that was added via assignStartAndBackMethods |
methodObjectRef |
Object | optional Object that has the start and back methods |
directionalControllerButtonSetup()
For use with ControllerPad.css and the .show method Use .show(w,h,2) to display the directionalsController image.
dispatch()
routes all touch and mouse events to their respective methods. Calls showButtons if 1 has been set on a showButtons call. Used internally by each button.
enableMouseControl()
Enables mouse clicks on the controller
establish(addEventsopt)
If you put anything for addEvents, events will Not be added just don't pass anything, leave addEvents out, if you intended to use the controller as normal. This method needs to be called after construction to establish events.
Name | Type | Attributes | Description |
---|---|---|---|
addEvents |
Boolean |
<optional> |
If set events will not be added. |
gamePadAvailable() → {Number}
Returns 1 if a game pad is available.
- Type
- Number
handleGamePad()
Call during your game loop, after movement has already happened. Basic GamePad functionality. Should produce correct directional response from any pad, But each game pad is different and each may send out different index values for the specific buttons. Directional input is done by axsis therefore it should work for all pads, button input however may vary more, best thing is to let the user set the buttons (not directionals) call configureGamePadButtons during your game loop, until it returns 1 or until gamePadButtonsUserDefined is 1. then start calling handleGamePad, for example;
if(!controller.gamePadButtonsUserDefined) {
controller.configureGamePadButtons();
} else {
controller.handleGamePad();
}
handleKeys()
Used internally
handleTouches()
Used internally
hide()
hide the ControllerPad
hideButtons()
hide buttons, by default this is called during setup.
keyEstablish()
establishes keydown and keyup events on the window for handleKeys and releaseKeys used internally.
releaseKeys()
Used internally
releaseTouches()
Used internally
removeStartAndBackMethods(startMethodString, backMethodString, methodObjectRef)
Removes the methods assigned with assignStartAndBackMethods
Name | Type | Description |
---|---|---|
startMethodString |
String | name of start method to call |
backMethodString |
String | name of back method to call |
methodObjectRef |
Object | Object that has the start and back methods |
removeTouchMove()
Removes touch move handling, such that only touchstart triggers anything. Needed if you want tile based non-fluid movement, frogger stlye or sokoban style.
rotateWithPoint(x, y, addedX, addedY, centerX, centerY)
for rotation the principle is to get the middle point and use atan2() * 180 / pi. the other things making this possible are in the MouseController Class and the static tabageos.ResizeGame method. Together they translate the touch point into a point within gameWidth/Height. when set up for capturing rotation, the controller calls rotateWithPoint on touchmove with the translate cords. the Rotating[Traveler/Shooter] Classes then need only use .setRotation(controller.rotation) to rotate via the controller.
Name | Type | Description |
---|---|---|
x |
Number | |
y |
Number | |
addedX |
Number | |
addedY |
Number | |
centerX |
Number | The center x point of the rotation see centerRotationX, centerRotationX is used by default. |
centerY |
Number | The center y point of the rotation see centerRotationY, centerRotationY is used by default. |
rotationalControllerButtonSetup()
centerRotationX and centerRotationY also need to be set.
show(w, h, style, styleOriginalWidth, styleOriginalHeight)
displays the controller with the given style. show just displays it, it does not set up the buttons, to change the controller use one of the setup methods. The GameSkeleton Class and tabageos.ResizeGame call this method for you as needed. Default style is 'basicController' see ControllerPad.css The other built in style options are 'directionalsController' 'standController', style as 5 is the rotationController.
The ControllerPad.css stylesheet needs to be applied to the page before this method is called.
Name | Type | Description |
---|---|---|
w |
Number | The width the controller should be, it will get scaled down if smaller than default |
h |
Number | The height the controller should be, both w and h must be defined, otherwise it uses the default w/h. |
style |
Number | The style id string or number, to define a custom style use the addCustomImag method and pass in your customStyleName here for this param. |
styleOriginalWidth |
||
styleOriginalHeight |
showButtons(ifPressed)
Can be used to display the actual rectangle hit regions of each button.
Name | Type | Description |
---|---|---|
ifPressed |
Boolean | only show each button if it is pressed, once set, to unset pass -1. |