so://phie

outline

A web-based drawing app
git clone git://soophie.de/outline

blob: 9fe28b28a1c58c5e3bb5f478715ffdc5abd4e06c (1282 bytes)[doc][blame][raw]

1const CONTROLLER_PATTERN = {

2 0: 'A',

3 1: 'B',

4 2: 'X',

5 3: 'Y',

6 4: 'L1',

7 5: 'R1',

8 6: 'L2',

9 7: 'R2',

10 8: 'Option',

11 9: 'Menu',

12 10: 'L3',

13 11: 'R3',

14 12: 'Up',

15 13: 'Down',

16 14: 'Left',

17 15: 'Right',

18 16: 'Power',

19 17: 'Assist',

20 18: 'Capture'

21};

22

23// Stores the pressed buttons

24let pressed = {};

25

26/**

27 Converts controller input into readable input.

28

29 @function read

30 @param {Object} gamepad - The gamepad object.

31 @return {Object} The formatted input object.

32*/

33

34function read(gamepad) {

35 let result = {

36 button: {},

37 axes: {}

38 };

39 for(let [ id, key ] of Object.entries(CONTROLLER_PATTERN)) {

40 if(gamepad.buttons[id]) {

41 result.button[key] = gamepad.buttons[id].pressed;

42 }

43 }

44 result.axes['Left'] = {

45 x: gamepad.axes[0],

46 y: gamepad.axes[1]

47 };

48 result.axes['Right'] = {

49 x: gamepad.axes[2],

50 y: gamepad.axes[3]

51 };

52 return result;

53}

54

55/**

56 The callback gets called if a gamepad button was pressed.

57

58 @function key_pressed

59 @param {string} key - The input key.

60 @param {Object} input - The formatted input object.

61 @callback callback

62*/

63function key_pressed(key, input, callback) {

64 if(input.button[key]) {

65 if(!pressed[key]) {

66 pressed[key] = true;

67 if(callback) {

68 callback();

69 }

70 }

71 }

72 else {

73 pressed[key] = false;

74 }

75}

76

77export { read, key_pressed };