so://phie

outline

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

blob: 5e21e3b575b4225dddd4a2ab8f3494292e63b3fd (4810 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

23let gamepad;

24let on_connect_callback;

25let on_disconnect_callback;

26

27/**

28 Binds gamepad event handler.

29

30 @function init

31 @contructor

32*/

33function init() {

34 window.addEventListener('gamepadconnected', function(event) {

35 gamepad = event.gamepad;

36 if(gamepad.vibrationActuator) {

37 gamepad.vibrationActuator.playEffect('dual-rumble', {

38 startDelay: 0,

39 duration: 100,

40 weakMagnitude: 1.0,

41 strongMagnitude: 1.0

42 });

43 }

44 if(on_connect_callback) {

45 on_connect_callback(gamepad);

46 }

47 });

48 window.addEventListener('gamepaddisconnected', function() {

49 gamepad = null;

50 if(on_disconnect_callback) {

51 on_disconnect_callback(gamepad);

52 }

53 });

54}

55

56/**

57 Return controller formatted input.

58

59 @function get

60 @return {Object} The formatted input object.

61*/

62function get() {

63 let result = {

64 button: {},

65 axes: {}

66 };

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

68 if(gamepad.buttons[id]) {

69 result.button[key] = gamepad.buttons[id];

70 }

71 }

72 result.axes['Left'] = {

73 x: gamepad.axes[0],

74 y: gamepad.axes[1]

75 };

76 result.axes['Right'] = {

77 x: gamepad.axes[2],

78 y: gamepad.axes[3]

79 };

80 return result;

81}

82

83function on_connect(callback) {

84 on_connect_callback = callback;

85}

86

87function on_disconnect(callback) {

88 on_disconnect_callback = callback;

89}

90

91/**

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

93

94 @function on_pressed

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

96 @callback callback

97*/

98function on_pressed(key, callback) {

99 /*

100 if(input.button[key]) {

101 if(!pressed[key]) {

102 pressed[key] = true;

103 if(callback) {

104 callback();

105 }

106 }

107 }

108 else {

109 pressed[key] = false;

110 }

111 */

112}

113

114export default { init, get, on_connect, on_disconnect, on_pressed };

115

116// !!! TODO !!!

117/*

118// cursor visible

119canvas.style.cursor = device === 'mouse' ? 'none' : 'default';

120if(device !== 'gamepad') {

121 let pos = arrow_y * 5 + arrow_x;

122 if(modal_colors[pos].classList.contains('focus')) {

123 modal_colors[pos].classList.remove('focus');

124 }

125}

126// navigate colors

127if(device === 'gamepad') {

128 let pos = arrow_y * 5 + arrow_x;

129 for(let [ i, item ] of Object.entries(modal_colors)) {

130 item.classList[parseInt(i) == pos ? 'add' : 'remove']('focus');

131 }

132}

133*/

134

135// !!! TODO !!!

136/*

137let gamepad = navigator.getGamepads()[0];

138if(gamepad) {

139 if(device === 'gamepad') {

140 if(point.x == null && point.y == null) {

141 point.x = parseInt(window.innerWidth / 2);

142 point.y = parseInt(window.innerHeight / 2);

143 }

144 let speed = 4;

145 if(pressed['L2'] && !pressed['R2']) {

146 speed = 10;

147 }

148 if(pressed['R2'] && !pressed['L2']) {

149 speed = 1;

150 }

151 // colors

152 key_pressed('Option', input, function() {

153 toggle_modal(colors_modal);

154 });

155 // capture

156 key_pressed('Capture', input, function() {

157 toggle_modal(capture_modal);

158 });

159 // draw

160 key_pressed('A', input, function() {

161 last.x = null;

162 last.y = null;

163 if(!current_modal) {

164 left_modal = false;

165 }

166 });

167 // erase

168 key_pressed('B', input, function() {

169 last.x = null;

170 last.y = null;

171 if(current_tab) {

172 close_tab(current_tab);

173 left_modal = true;

174 }

175 else if(current_modal) {

176 toggle_modal(current_modal);

177 left_modal = true;

178 }

179 else {

180 left_modal = false;

181 }

182 });

183 // sprint

184 key_pressed('L2', input);

185 // sneak

186 key_pressed('R2', input);

187 // clear

188 key_pressed('Y', input, function() {

189 toggle_modal(clear_modal);

190 });

191 // radius

192 let y = -(input.axes['Right'].y / 2);

193 if(y !== 0 && radius + y > 2 && radius + y < 50 + 1) {

194 radius += y;

195 scale_range.value = radius;

196 }

197 // lock y

198 key_pressed('L3', input, function() {

199 lock_y = !lock_y;

200 lock_x = false;

201 });

202 // lock x

203 key_pressed('R3', input, function() {

204 lock_x = !lock_x;

205 lock_y = false;

206 });

207 // color-wheel left

208 key_pressed('L1', input, function() {

209 if(index > 0) {

210 index--;

211 for(let i in colors) {

212 if(COLOR_LENGTH > i) {

213 colors[i].classList[i == index ? 'add' : 'remove']('active');

214 }

215 }

216 }

217 });

218 // color-wheel right

219 key_pressed('R1', input, function() {

220 if(index < COLOR_LENGTH - 1) {

221 index++;

222 for(let i in colors) {

223 if(COLOR_LENGTH > i) {

224 colors[i].classList[i == index ? 'add' : 'remove']('active');

225 }

226 }

227 }

228 });

229 key_pressed('Up', input, function() {

230 if(arrow_y > 0) {

231 arrow_y--;

232 }

233 });

234 key_pressed('Down', input, function() {

235 if(arrow_y < 10) {

236 arrow_y++;

237 }

238 });

239 key_pressed('Left', input, function() {

240 if(arrow_x > 0) {

241 arrow_x--;

242 }

243 });

244 key_pressed('Right', input, function() {

245 if(arrow_x < 4) {

246 arrow_x++;

247 }

248 });

249 }

250}

251*/