@wwawing/virtual-pad 3.9.0-beta.3 → 3.10.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,32 +1,8 @@
1
- export declare enum VirtualPadButtonCode {
2
- BUTTON_ENTER = 0,
3
- BUTTON_ESC = 1,
4
- BUTTON_SIDEBAR = 2,
5
- BUTTON_ESTIMATE = 3,
6
- BUTTON_FAST = 4,
7
- BUTTON_SLOW = 5,
8
- BUTTON_LEFT = 6,
9
- BUTTON_UP = 7,
10
- BUTTON_RIGHT = 8,
11
- BUTTON_DOWN = 9
12
- }
13
- export declare enum VirtualPadState {
14
- NONE = 0,
15
- TOUCH = 1,
16
- TOUCHING = 2,
17
- LEAVE = 3
18
- }
1
+ export declare const VirtualPadButtonCodes: readonly ["BUTTON_ENTER", "BUTTON_ESC", "BUTTON_SIDEBAR", "BUTTON_FAST", "BUTTON_SLOW", "BUTTON_LEFT", "BUTTON_UP", "BUTTON_RIGHT", "BUTTON_DOWN"];
2
+ export declare type VirtualPadButtonCode = typeof VirtualPadButtonCodes[number];
3
+ export declare type VirtualPadState = "NONE" | "TOUCH" | "TOUCHING" | "LEAVE";
19
4
  export declare type VirtualPadButtons = {
20
- [VirtualPadButtonCode.BUTTON_ENTER]: HTMLButtonElement;
21
- [VirtualPadButtonCode.BUTTON_ESC]: HTMLButtonElement;
22
- [VirtualPadButtonCode.BUTTON_SIDEBAR]?: HTMLButtonElement;
23
- [VirtualPadButtonCode.BUTTON_ESTIMATE]: HTMLButtonElement;
24
- [VirtualPadButtonCode.BUTTON_FAST]: HTMLButtonElement;
25
- [VirtualPadButtonCode.BUTTON_SLOW]: HTMLButtonElement;
26
- [VirtualPadButtonCode.BUTTON_LEFT]: HTMLButtonElement;
27
- [VirtualPadButtonCode.BUTTON_UP]: HTMLButtonElement;
28
- [VirtualPadButtonCode.BUTTON_RIGHT]: HTMLButtonElement;
29
- [VirtualPadButtonCode.BUTTON_DOWN]: HTMLButtonElement;
5
+ [key in string]: HTMLButtonElement;
30
6
  };
31
7
  declare type VirtualPadEventFunction = (buttonCode: VirtualPadButtonCode) => void;
32
8
  export default class VirtualPadStore {
@@ -38,7 +14,7 @@ export default class VirtualPadStore {
38
14
  private _isTouchingButtons;
39
15
  private _moveButtonsElement;
40
16
  private _buttonWrapperElement;
41
- constructor(buttons: VirtualPadButtons | {}, firstVisible?: boolean, buttonWrapper?: HTMLElement, moveButtons?: HTMLElement, onTouchStart?: VirtualPadEventFunction, onTouchEnd?: VirtualPadEventFunction);
17
+ constructor(buttons: VirtualPadButtons | null, firstVisible?: boolean, buttonWrapper?: HTMLElement | null, moveButtons?: HTMLElement | null, onTouchStart?: VirtualPadEventFunction | null, onTouchEnd?: VirtualPadEventFunction | null);
42
18
  private _detectMovingMoveButton;
43
19
  private _setVisible;
44
20
  checkTouchButton(buttonType: VirtualPadButtonCode): boolean;
package/lib/VirtualPad.js CHANGED
@@ -1,28 +1,19 @@
1
- export var VirtualPadButtonCode;
2
- (function (VirtualPadButtonCode) {
3
- VirtualPadButtonCode[VirtualPadButtonCode["BUTTON_ENTER"] = 0] = "BUTTON_ENTER";
4
- VirtualPadButtonCode[VirtualPadButtonCode["BUTTON_ESC"] = 1] = "BUTTON_ESC";
5
- VirtualPadButtonCode[VirtualPadButtonCode["BUTTON_SIDEBAR"] = 2] = "BUTTON_SIDEBAR";
6
- VirtualPadButtonCode[VirtualPadButtonCode["BUTTON_ESTIMATE"] = 3] = "BUTTON_ESTIMATE";
7
- VirtualPadButtonCode[VirtualPadButtonCode["BUTTON_FAST"] = 4] = "BUTTON_FAST";
8
- VirtualPadButtonCode[VirtualPadButtonCode["BUTTON_SLOW"] = 5] = "BUTTON_SLOW";
9
- VirtualPadButtonCode[VirtualPadButtonCode["BUTTON_LEFT"] = 6] = "BUTTON_LEFT";
10
- VirtualPadButtonCode[VirtualPadButtonCode["BUTTON_UP"] = 7] = "BUTTON_UP";
11
- VirtualPadButtonCode[VirtualPadButtonCode["BUTTON_RIGHT"] = 8] = "BUTTON_RIGHT";
12
- VirtualPadButtonCode[VirtualPadButtonCode["BUTTON_DOWN"] = 9] = "BUTTON_DOWN";
13
- })(VirtualPadButtonCode || (VirtualPadButtonCode = {}));
14
- export var VirtualPadState;
15
- (function (VirtualPadState) {
16
- VirtualPadState[VirtualPadState["NONE"] = 0] = "NONE";
17
- VirtualPadState[VirtualPadState["TOUCH"] = 1] = "TOUCH";
18
- VirtualPadState[VirtualPadState["TOUCHING"] = 2] = "TOUCHING";
19
- VirtualPadState[VirtualPadState["LEAVE"] = 3] = "LEAVE";
20
- })(VirtualPadState || (VirtualPadState = {}));
1
+ export var VirtualPadButtonCodes = [
2
+ "BUTTON_ENTER",
3
+ "BUTTON_ESC",
4
+ "BUTTON_SIDEBAR",
5
+ "BUTTON_FAST",
6
+ "BUTTON_SLOW",
7
+ "BUTTON_LEFT",
8
+ "BUTTON_UP",
9
+ "BUTTON_RIGHT",
10
+ "BUTTON_DOWN"
11
+ ];
21
12
  var VirtualPadMoveButtonCodes = [
22
- VirtualPadButtonCode.BUTTON_LEFT,
23
- VirtualPadButtonCode.BUTTON_UP,
24
- VirtualPadButtonCode.BUTTON_RIGHT,
25
- VirtualPadButtonCode.BUTTON_DOWN
13
+ "BUTTON_LEFT",
14
+ "BUTTON_UP",
15
+ "BUTTON_RIGHT",
16
+ "BUTTON_DOWN"
26
17
  ];
27
18
  var VirtualPadStore = (function () {
28
19
  function VirtualPadStore(buttons, firstVisible, buttonWrapper, moveButtons, onTouchStart, onTouchEnd) {
@@ -32,7 +23,7 @@ var VirtualPadStore = (function () {
32
23
  if (onTouchStart === void 0) { onTouchStart = null; }
33
24
  if (onTouchEnd === void 0) { onTouchEnd = null; }
34
25
  var _this = this;
35
- this._enabled = Object.keys(buttons).length > 0;
26
+ this._enabled = buttons !== null;
36
27
  this._visible = firstVisible;
37
28
  this._onTouchStart = onTouchStart || null;
38
29
  this._onTouchEnd = onTouchEnd || null;
@@ -41,9 +32,21 @@ var VirtualPadStore = (function () {
41
32
  this._moveButtonsElement = moveButtons;
42
33
  this._buttonWrapperElement = buttonWrapper;
43
34
  this._setVisible(firstVisible);
35
+ if (this._moveButtonsElement) {
36
+ this._moveButtonsElement.style.visibility = "visible";
37
+ }
38
+ if (this._buttonWrapperElement) {
39
+ this._buttonWrapperElement.style.visibility = "visible";
40
+ }
41
+ if (buttons === null) {
42
+ return;
43
+ }
44
44
  var _loop_1 = function (buttonTypeString) {
45
- var buttonCode = parseInt(buttonTypeString);
46
- var button = buttons[buttonCode];
45
+ var button = buttons[buttonTypeString];
46
+ if (!button) {
47
+ return "continue";
48
+ }
49
+ var buttonCode = buttonTypeString;
47
50
  this_1._availableButtons.push(buttonCode);
48
51
  button.setAttribute("type", buttonTypeString);
49
52
  this_1._isTouchingButtons[buttonCode] = {
@@ -64,7 +67,6 @@ var VirtualPadStore = (function () {
64
67
  cancelBrowserTouchEvent(event);
65
68
  _this.setTouchInfo(buttonCode);
66
69
  });
67
- button.addEventListener("cancel", cancelVirtualPad);
68
70
  button.addEventListener("touchcancel", cancelVirtualPad);
69
71
  if (VirtualPadStore.isMoveButton(buttonCode)) {
70
72
  button.addEventListener("touchmove", this_1._detectMovingMoveButton.bind(this_1));
@@ -88,27 +90,30 @@ var VirtualPadStore = (function () {
88
90
  VirtualPadStore.prototype._detectMovingMoveButton = function (event) {
89
91
  event.preventDefault();
90
92
  this.allMoveClear();
93
+ var touch = event.targetTouches.item(0);
94
+ if (!this._moveButtonsElement || touch === null) {
95
+ return;
96
+ }
91
97
  var moveButtonRect = this._moveButtonsElement.getBoundingClientRect();
92
98
  var bodyRect = document.body.getBoundingClientRect();
93
99
  var centerPositionX = moveButtonRect.left - bodyRect.left + (moveButtonRect.width / 2);
94
100
  var centerPositionY = moveButtonRect.top - bodyRect.top + (moveButtonRect.height / 2);
95
- var touch = event.targetTouches.item(0);
96
101
  var touchX = touch.pageX - centerPositionX;
97
102
  var touchY = touch.pageY - centerPositionY;
98
103
  if (touchX >= touchY) {
99
104
  if (touchX <= -touchY) {
100
- this.setTouchInfo(VirtualPadButtonCode.BUTTON_UP);
105
+ this.setTouchInfo("BUTTON_UP");
101
106
  }
102
107
  else {
103
- this.setTouchInfo(VirtualPadButtonCode.BUTTON_RIGHT);
108
+ this.setTouchInfo("BUTTON_RIGHT");
104
109
  }
105
110
  }
106
111
  else {
107
112
  if (touchX >= -touchY) {
108
- this.setTouchInfo(VirtualPadButtonCode.BUTTON_DOWN);
113
+ this.setTouchInfo("BUTTON_DOWN");
109
114
  }
110
115
  else {
111
- this.setTouchInfo(VirtualPadButtonCode.BUTTON_LEFT);
116
+ this.setTouchInfo("BUTTON_LEFT");
112
117
  }
113
118
  }
114
119
  };
@@ -116,7 +121,7 @@ var VirtualPadStore = (function () {
116
121
  if (this._moveButtonsElement !== null) {
117
122
  this._moveButtonsElement.style.display = visible ? "grid" : "none";
118
123
  }
119
- if (this._moveButtonsElement !== null) {
124
+ if (this._buttonWrapperElement !== null) {
120
125
  this._buttonWrapperElement.style.display = visible ? "grid" : "none";
121
126
  }
122
127
  this._visible = visible;
@@ -126,40 +131,41 @@ var VirtualPadStore = (function () {
126
131
  return false;
127
132
  }
128
133
  var state = this.getButtonState(buttonType);
129
- return state === VirtualPadState.TOUCH;
134
+ return state === "TOUCH";
130
135
  };
131
136
  VirtualPadStore.prototype.checkTouchingButton = function (buttonType) {
132
137
  if (!this._enabled) {
133
138
  return false;
134
139
  }
135
140
  var state = this.getButtonState(buttonType);
136
- return state === VirtualPadState.TOUCH || state === VirtualPadState.TOUCHING;
141
+ return state === "TOUCH" || state === "TOUCHING";
137
142
  };
138
143
  VirtualPadStore.prototype.getButtonState = function (buttonType) {
139
- if (!this._enabled) {
140
- return VirtualPadState.NONE;
144
+ var button = this._isTouchingButtons[buttonType];
145
+ if (!this._enabled || button === undefined) {
146
+ return "NONE";
141
147
  }
142
- var touched = this._isTouchingButtons[buttonType].prev;
143
- var isTouching = this._isTouchingButtons[buttonType].current;
148
+ var touched = button.prev;
149
+ var isTouching = button.current;
144
150
  if (touched && isTouching) {
145
- return VirtualPadState.TOUCHING;
151
+ return "TOUCHING";
146
152
  }
147
153
  else if (isTouching) {
148
- return VirtualPadState.TOUCH;
154
+ return "TOUCH";
149
155
  }
150
156
  else if (touched) {
151
- return VirtualPadState.LEAVE;
157
+ return "LEAVE";
152
158
  }
153
- return VirtualPadState.NONE;
159
+ return "NONE";
154
160
  };
155
161
  VirtualPadStore.prototype.setTouchInfo = function (buttonType) {
156
- if (!this._enabled) {
162
+ if (!this._enabled || this._isTouchingButtons[buttonType] === undefined) {
157
163
  return;
158
164
  }
159
165
  this._isTouchingButtons[buttonType].next = true;
160
166
  };
161
167
  VirtualPadStore.prototype.clearTouchInfo = function (buttonType) {
162
- if (!this._enabled) {
168
+ if (!this._enabled || this._isTouchingButtons[buttonType] === undefined) {
163
169
  return;
164
170
  }
165
171
  this._isTouchingButtons[buttonType].next = false;
@@ -175,7 +181,9 @@ var VirtualPadStore = (function () {
175
181
  VirtualPadStore.prototype.allClear = function () {
176
182
  var _this = this;
177
183
  this._availableButtons.forEach(function (buttonCode) {
178
- _this._isTouchingButtons[buttonCode].next = false;
184
+ if (buttonCode !== undefined && _this._isTouchingButtons[buttonCode] !== undefined) {
185
+ _this._isTouchingButtons[buttonCode].next = false;
186
+ }
179
187
  });
180
188
  };
181
189
  VirtualPadStore.isMoveButton = function (buttonType) {
@@ -184,15 +192,22 @@ var VirtualPadStore = (function () {
184
192
  VirtualPadStore.prototype.update = function () {
185
193
  var _this = this;
186
194
  this._availableButtons.forEach(function (buttonCode) {
195
+ var _a, _b;
196
+ if (buttonCode === undefined) {
197
+ return;
198
+ }
187
199
  var currentButtonTouching = _this._isTouchingButtons[buttonCode];
200
+ if (currentButtonTouching === undefined) {
201
+ return;
202
+ }
188
203
  _this._isTouchingButtons[buttonCode].prev = currentButtonTouching.current;
189
204
  _this._isTouchingButtons[buttonCode].current = currentButtonTouching.next;
190
205
  switch (_this.getButtonState(buttonCode)) {
191
- case VirtualPadState.TOUCH:
192
- _this._onTouchStart(buttonCode);
206
+ case "TOUCH":
207
+ (_a = _this._onTouchStart) === null || _a === void 0 ? void 0 : _a.call(_this, buttonCode);
193
208
  break;
194
- case VirtualPadState.LEAVE:
195
- _this._onTouchEnd(buttonCode);
209
+ case "LEAVE":
210
+ (_b = _this._onTouchEnd) === null || _b === void 0 ? void 0 : _b.call(_this, buttonCode);
196
211
  break;
197
212
  }
198
213
  });
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import VirtualPadStore, { VirtualPadState, VirtualPadButtonCode, VirtualPadButtons } from './VirtualPad';
1
+ import VirtualPadStore, { VirtualPadState, VirtualPadButtonCodes, VirtualPadButtonCode, VirtualPadButtons } from './VirtualPad';
2
2
  import viewportFit, { initializeViewport } from './viewportFit';
3
3
  import makeInject from './makeInject';
4
- export { VirtualPadStore, VirtualPadState, VirtualPadButtonCode, VirtualPadButtons, viewportFit, initializeViewport, makeInject };
4
+ export { VirtualPadStore, VirtualPadState, VirtualPadButtonCodes, VirtualPadButtonCode, VirtualPadButtons, viewportFit, initializeViewport, makeInject };
package/lib/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import VirtualPadStore, { VirtualPadState, VirtualPadButtonCode } from './VirtualPad';
1
+ import VirtualPadStore, { VirtualPadButtonCodes } from './VirtualPad';
2
2
  import viewportFit, { initializeViewport } from './viewportFit';
3
3
  import makeInject from './makeInject';
4
- export { VirtualPadStore, VirtualPadState, VirtualPadButtonCode, viewportFit, initializeViewport, makeInject };
4
+ export { VirtualPadStore, VirtualPadButtonCodes, viewportFit, initializeViewport, makeInject };
package/lib/makeInject.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export default function makeInject() {
2
- return "\n <div id=\"wwa-virtualpad-left\">\n <button class=\"wwa-virtualpad__button\" id=\"wwa-up-button\"></button>\n <button class=\"wwa-virtualpad__button\" id=\"wwa-left-button\"></button>\n <button class=\"wwa-virtualpad__button\" id=\"wwa-right-button\"></button>\n <button class=\"wwa-virtualpad__button\" id=\"wwa-down-button\"></button>\n <div class=\"wwa-virtualpad__hole\"></div>\n </div>\n <div id=\"wwa-virtualpad-right\">\n <button class=\"wwa-virtualpad__button\" id=\"wwa-slow-button\">I</button>\n <button class=\"wwa-virtualpad__button\" id=\"wwa-fast-button\">P</button>\n <button class=\"wwa-virtualpad__button\" id=\"wwa-enter-button\">Y</button>\n <button class=\"wwa-virtualpad__button\" id=\"wwa-esc-button\">N</button>\n <button class=\"wwa-virtualpad__button\" id=\"wwa-estimate-button\">M</button>\n </div>";
2
+ return "\n <div id=\"wwa-virtualpad-left\">\n <button class=\"wwa-virtualpad__button\" id=\"wwa-up-button\"></button>\n <button class=\"wwa-virtualpad__button\" id=\"wwa-left-button\"></button>\n <button class=\"wwa-virtualpad__button\" id=\"wwa-right-button\"></button>\n <button class=\"wwa-virtualpad__button\" id=\"wwa-down-button\"></button>\n <div class=\"wwa-virtualpad__hole\"></div>\n </div>\n <div id=\"wwa-virtualpad-right\">\n <button class=\"wwa-virtualpad__button\" id=\"wwa-slow-button\">I</button>\n <button class=\"wwa-virtualpad__button\" id=\"wwa-fast-button\">P</button>\n <button class=\"wwa-virtualpad__button\" id=\"wwa-enter-button\">Y</button>\n <button class=\"wwa-virtualpad__button\" id=\"wwa-esc-button\">N</button>\n </div>";
3
3
  }
@@ -20,5 +20,5 @@ export default function viewportFit() {
20
20
  else if (browserWidth <= browserHeight) {
21
21
  viewportValue = "width=".concat(WWA_WIDTH + (BROWSER_MARGIN * 2), ",").concat(viewportValue);
22
22
  }
23
- viewportElement.setAttribute("content", "".concat(viewportValue));
23
+ viewportElement === null || viewportElement === void 0 ? void 0 : viewportElement.setAttribute("content", "".concat(viewportValue));
24
24
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wwawing/virtual-pad",
3
- "version": "3.9.0-beta.3",
3
+ "version": "3.10.0-beta.3",
4
4
  "description": "WWA Wing Virtual Pad for mobile phone",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -16,5 +16,5 @@
16
16
  "publishConfig": {
17
17
  "access": "public"
18
18
  },
19
- "gitHead": "42a88c02b75b6f8ce6bb22fb38f2b8a952ee38d6"
19
+ "gitHead": "90ec9f72ca5ce1588812fe42cf97518a5fda9b49"
20
20
  }
package/src/VirtualPad.ts CHANGED
@@ -1,15 +1,16 @@
1
- export enum VirtualPadButtonCode {
2
- BUTTON_ENTER,
3
- BUTTON_ESC,
4
- BUTTON_SIDEBAR,
5
- BUTTON_ESTIMATE,
6
- BUTTON_FAST,
7
- BUTTON_SLOW,
8
- BUTTON_LEFT,
9
- BUTTON_UP,
10
- BUTTON_RIGHT,
11
- BUTTON_DOWN
12
- }
1
+ export const VirtualPadButtonCodes = [
2
+ "BUTTON_ENTER",
3
+ "BUTTON_ESC",
4
+ "BUTTON_SIDEBAR",
5
+ "BUTTON_FAST",
6
+ "BUTTON_SLOW",
7
+ "BUTTON_LEFT",
8
+ "BUTTON_UP",
9
+ "BUTTON_RIGHT",
10
+ "BUTTON_DOWN"
11
+ ] as const;
12
+
13
+ export type VirtualPadButtonCode = typeof VirtualPadButtonCodes[number];
13
14
 
14
15
  /**
15
16
  * NONE: ボタンに一切触れていません
@@ -17,25 +18,9 @@ export enum VirtualPadButtonCode {
17
18
  * TOUCHING: ボタンに触れています
18
19
  * LEAVE: ボタンから離れました
19
20
  */
20
- export enum VirtualPadState {
21
- NONE,
22
- TOUCH,
23
- TOUCHING,
24
- LEAVE
25
- }
21
+ export type VirtualPadState = "NONE" | "TOUCH" | "TOUCHING" | "LEAVE";
26
22
 
27
- export type VirtualPadButtons = {
28
- [VirtualPadButtonCode.BUTTON_ENTER]: HTMLButtonElement,
29
- [VirtualPadButtonCode.BUTTON_ESC]: HTMLButtonElement,
30
- [VirtualPadButtonCode.BUTTON_SIDEBAR]?: HTMLButtonElement,
31
- [VirtualPadButtonCode.BUTTON_ESTIMATE]: HTMLButtonElement,
32
- [VirtualPadButtonCode.BUTTON_FAST]: HTMLButtonElement,
33
- [VirtualPadButtonCode.BUTTON_SLOW]: HTMLButtonElement,
34
- [VirtualPadButtonCode.BUTTON_LEFT]: HTMLButtonElement,
35
- [VirtualPadButtonCode.BUTTON_UP]: HTMLButtonElement,
36
- [VirtualPadButtonCode.BUTTON_RIGHT]: HTMLButtonElement,
37
- [VirtualPadButtonCode.BUTTON_DOWN]: HTMLButtonElement
38
- };
23
+ export type VirtualPadButtons = { [key in string]: HTMLButtonElement };
39
24
 
40
25
  type VirtualPadButtonTouching = {
41
26
  prev: boolean,
@@ -46,10 +31,10 @@ type VirtualPadButtonTouching = {
46
31
  type VirtualPadEventFunction = (buttonCode: VirtualPadButtonCode) => void;
47
32
 
48
33
  const VirtualPadMoveButtonCodes = [
49
- VirtualPadButtonCode.BUTTON_LEFT,
50
- VirtualPadButtonCode.BUTTON_UP,
51
- VirtualPadButtonCode.BUTTON_RIGHT,
52
- VirtualPadButtonCode.BUTTON_DOWN
34
+ "BUTTON_LEFT",
35
+ "BUTTON_UP",
36
+ "BUTTON_RIGHT",
37
+ "BUTTON_DOWN"
53
38
  ];
54
39
 
55
40
  /**
@@ -83,12 +68,12 @@ export default class VirtualPadStore {
83
68
  * 移動ボタンの要素自体です。主に移動ボタンで指を動かす際に使用します。
84
69
  * @property
85
70
  */
86
- private _moveButtonsElement: HTMLElement;
71
+ private _moveButtonsElement: HTMLElement | null;
87
72
 
88
73
  /**
89
74
  * 操作ボタン群の親要素です。 {@code _moveButtonsElement} と併せて表示や非表示の切り替えに使用します。
90
75
  */
91
- private _buttonWrapperElement: HTMLElement;
76
+ private _buttonWrapperElement: HTMLElement | null;
92
77
 
93
78
  /**
94
79
  * @param buttons 仮想パッドの各要素
@@ -99,14 +84,14 @@ export default class VirtualPadStore {
99
84
  * @param onTouchEnd 仮想パッドを話した場合に発生するコールバック関数
100
85
  */
101
86
  constructor(
102
- buttons: VirtualPadButtons | {},
87
+ buttons: VirtualPadButtons | null,
103
88
  firstVisible: boolean = false,
104
- buttonWrapper: HTMLElement = null,
105
- moveButtons: HTMLElement = null,
106
- onTouchStart: VirtualPadEventFunction = null,
107
- onTouchEnd: VirtualPadEventFunction = null,
89
+ buttonWrapper: HTMLElement | null = null,
90
+ moveButtons: HTMLElement | null = null,
91
+ onTouchStart: VirtualPadEventFunction | null = null,
92
+ onTouchEnd: VirtualPadEventFunction | null = null,
108
93
  ) {
109
- this._enabled = Object.keys(buttons).length > 0;
94
+ this._enabled = buttons !== null;
110
95
  this._visible = firstVisible;
111
96
  this._onTouchStart = onTouchStart || null;
112
97
  this._onTouchEnd = onTouchEnd || null;
@@ -116,15 +101,25 @@ export default class VirtualPadStore {
116
101
  this._buttonWrapperElement = buttonWrapper;
117
102
 
118
103
  this._setVisible(firstVisible);
104
+ // PC でアクセスする場合、 VirtualPadStore が生成されるまでの間一瞬表示されてしまう
105
+ // そうならないようにするために、 CSS 側で visibility: hidden を付与し、インラインスタイルシートで解除している
106
+ if (this._moveButtonsElement) {
107
+ this._moveButtonsElement.style.visibility = "visible";
108
+ }
109
+ if (this._buttonWrapperElement) {
110
+ this._buttonWrapperElement.style.visibility = "visible";
111
+ }
119
112
 
120
- for (let buttonTypeString in buttons) {
121
- /**
122
- * for in ... で渡される buttonTypeString は必ず文字列型になります。
123
- * このまま buttons の配列のキーに渡すと、正しく要素が受け取れません。
124
- * このため、一度 parseInt で数字に変換した上で渡しています。
125
- */
126
- let buttonCode = parseInt(buttonTypeString);
127
- let button: HTMLButtonElement = buttons[buttonCode];
113
+ if (buttons === null) {
114
+ return;
115
+ }
116
+ for (const buttonTypeString in buttons) {
117
+ const button = buttons[buttonTypeString];
118
+ if (!button) {
119
+ continue;
120
+ }
121
+ // ここまでくれば buttonTypeString は VirtualPadButtonCode になる
122
+ const buttonCode = buttonTypeString as VirtualPadButtonCode;
128
123
  this._availableButtons.push(buttonCode);
129
124
 
130
125
  /**
@@ -160,7 +155,7 @@ export default class VirtualPadStore {
160
155
  this.setTouchInfo(buttonCode);
161
156
  });
162
157
 
163
- button.addEventListener("cancel", cancelVirtualPad);
158
+ // button.addEventListener("cancel", cancelVirtualPad);
164
159
  button.addEventListener("touchcancel", cancelVirtualPad);
165
160
 
166
161
  /**
@@ -188,6 +183,11 @@ export default class VirtualPadStore {
188
183
  private _detectMovingMoveButton(event: TouchEvent) {
189
184
  event.preventDefault();
190
185
  this.allMoveClear();
186
+
187
+ const touch = event.targetTouches.item(0);
188
+ if (!this._moveButtonsElement || touch === null) {
189
+ return;
190
+ }
191
191
 
192
192
  // 中央のポジションを取得
193
193
  const moveButtonRect = this._moveButtonsElement.getBoundingClientRect();
@@ -195,7 +195,6 @@ export default class VirtualPadStore {
195
195
  const centerPositionX = moveButtonRect.left - bodyRect.left + (moveButtonRect.width / 2);
196
196
  const centerPositionY = moveButtonRect.top - bodyRect.top + (moveButtonRect.height / 2);
197
197
 
198
- const touch = event.targetTouches.item(0);
199
198
  const touchX = touch.pageX - centerPositionX;
200
199
  const touchY = touch.pageY - centerPositionY;
201
200
 
@@ -204,15 +203,15 @@ export default class VirtualPadStore {
204
203
  * touchY をマイナスにして、グラフの空間上で擬似的に表現します。
205
204
  */
206
205
  if (touchX <= -touchY) { // 上
207
- this.setTouchInfo(VirtualPadButtonCode.BUTTON_UP);
206
+ this.setTouchInfo("BUTTON_UP");
208
207
  } else { // 右
209
- this.setTouchInfo(VirtualPadButtonCode.BUTTON_RIGHT);
208
+ this.setTouchInfo("BUTTON_RIGHT");
210
209
  }
211
210
  } else { // 左と下
212
211
  if (touchX >= -touchY) { // 下
213
- this.setTouchInfo(VirtualPadButtonCode.BUTTON_DOWN);
212
+ this.setTouchInfo("BUTTON_DOWN");
214
213
  } else { // 左
215
- this.setTouchInfo(VirtualPadButtonCode.BUTTON_LEFT);
214
+ this.setTouchInfo("BUTTON_LEFT");
216
215
  }
217
216
  }
218
217
  }
@@ -221,7 +220,7 @@ export default class VirtualPadStore {
221
220
  if (this._moveButtonsElement !== null) {
222
221
  this._moveButtonsElement.style.display = visible ? "grid" : "none";
223
222
  }
224
- if (this._moveButtonsElement !== null) {
223
+ if (this._buttonWrapperElement !== null) {
225
224
  this._buttonWrapperElement.style.display = visible ? "grid" : "none";
226
225
  }
227
226
  this._visible = visible;
@@ -237,7 +236,7 @@ export default class VirtualPadStore {
237
236
  return false;
238
237
  }
239
238
  const state = this.getButtonState(buttonType);
240
- return state === VirtualPadState.TOUCH;
239
+ return state === "TOUCH";
241
240
  }
242
241
 
243
242
  /**
@@ -250,7 +249,7 @@ export default class VirtualPadStore {
250
249
  return false;
251
250
  }
252
251
  const state = this.getButtonState(buttonType);
253
- return state === VirtualPadState.TOUCH || state === VirtualPadState.TOUCHING;
252
+ return state === "TOUCH" || state === "TOUCHING";
254
253
  }
255
254
 
256
255
  /**
@@ -258,20 +257,21 @@ export default class VirtualPadStore {
258
257
  * @param buttonType
259
258
  */
260
259
  public getButtonState(buttonType: VirtualPadButtonCode): VirtualPadState {
261
- if (!this._enabled) {
262
- return VirtualPadState.NONE;
260
+ const button = this._isTouchingButtons[buttonType];
261
+ if (!this._enabled || button === undefined) {
262
+ return "NONE";
263
263
  }
264
264
 
265
- const touched = this._isTouchingButtons[buttonType].prev;
266
- const isTouching = this._isTouchingButtons[buttonType].current;
265
+ const touched = button.prev;
266
+ const isTouching = button.current;
267
267
  if (touched && isTouching) {
268
- return VirtualPadState.TOUCHING;
268
+ return "TOUCHING";
269
269
  } else if (isTouching) {
270
- return VirtualPadState.TOUCH;
270
+ return "TOUCH";
271
271
  } else if (touched) {
272
- return VirtualPadState.LEAVE;
272
+ return "LEAVE";
273
273
  }
274
- return VirtualPadState.NONE;
274
+ return "NONE";
275
275
  }
276
276
 
277
277
  /**
@@ -279,11 +279,11 @@ export default class VirtualPadStore {
279
279
  * @param buttonType
280
280
  */
281
281
  public setTouchInfo(buttonType: VirtualPadButtonCode) {
282
- if (!this._enabled) {
282
+ if (!this._enabled || this._isTouchingButtons[buttonType] === undefined) {
283
283
  return;
284
284
  }
285
285
 
286
- this._isTouchingButtons[buttonType].next = true;
286
+ (this._isTouchingButtons[buttonType] as VirtualPadButtonTouching).next = true;
287
287
  }
288
288
 
289
289
  /**
@@ -291,11 +291,11 @@ export default class VirtualPadStore {
291
291
  * @param buttonType
292
292
  */
293
293
  public clearTouchInfo(buttonType: VirtualPadButtonCode) {
294
- if (!this._enabled) {
294
+ if (!this._enabled || this._isTouchingButtons[buttonType] === undefined) {
295
295
  return;
296
296
  }
297
297
 
298
- this._isTouchingButtons[buttonType].next = false;
298
+ (this._isTouchingButtons[buttonType] as VirtualPadButtonTouching).next = false;
299
299
  }
300
300
 
301
301
  /**
@@ -303,8 +303,8 @@ export default class VirtualPadStore {
303
303
  */
304
304
  public allMoveClear(): void {
305
305
  VirtualPadMoveButtonCodes.forEach((buttonCode) => {
306
- if (this._availableButtons.includes(buttonCode)) {
307
- this.clearTouchInfo(buttonCode);
306
+ if (this._availableButtons.includes(buttonCode as VirtualPadButtonCode)) {
307
+ this.clearTouchInfo(buttonCode as VirtualPadButtonCode);
308
308
  }
309
309
  });
310
310
  }
@@ -314,7 +314,9 @@ export default class VirtualPadStore {
314
314
  */
315
315
  public allClear(): void {
316
316
  this._availableButtons.forEach((buttonCode) => {
317
- this._isTouchingButtons[buttonCode].next = false;
317
+ if (buttonCode !== undefined && this._isTouchingButtons[buttonCode] !== undefined) {
318
+ (this._isTouchingButtons[buttonCode] as VirtualPadButtonTouching).next = false;
319
+ }
318
320
  });
319
321
  }
320
322
 
@@ -331,19 +333,25 @@ export default class VirtualPadStore {
331
333
  */
332
334
  public update() {
333
335
  this._availableButtons.forEach((buttonCode) => {
334
- let currentButtonTouching = this._isTouchingButtons[buttonCode];
335
- this._isTouchingButtons[buttonCode].prev = currentButtonTouching.current;
336
- this._isTouchingButtons[buttonCode].current = currentButtonTouching.next;
336
+ if (buttonCode === undefined) {
337
+ return;
338
+ }
339
+ const currentButtonTouching = this._isTouchingButtons[buttonCode];
340
+ if (currentButtonTouching === undefined) {
341
+ return;
342
+ }
343
+ (this._isTouchingButtons[buttonCode] as VirtualPadButtonTouching).prev = currentButtonTouching.current;
344
+ (this._isTouchingButtons[buttonCode] as VirtualPadButtonTouching).current = currentButtonTouching.next;
337
345
 
338
346
  /**
339
347
  * ボタンの状態に応じて、コールバック関数を実行
340
348
  */
341
349
  switch (this.getButtonState(buttonCode)) {
342
- case VirtualPadState.TOUCH:
343
- this._onTouchStart(buttonCode);
350
+ case "TOUCH":
351
+ this._onTouchStart?.(buttonCode);
344
352
  break;
345
- case VirtualPadState.LEAVE:
346
- this._onTouchEnd(buttonCode);
353
+ case "LEAVE":
354
+ this._onTouchEnd?.(buttonCode);
347
355
  break;
348
356
  }
349
357
  });
package/src/index.ts CHANGED
@@ -1,10 +1,11 @@
1
- import VirtualPadStore, { VirtualPadState, VirtualPadButtonCode, VirtualPadButtons } from './VirtualPad';
1
+ import VirtualPadStore, { VirtualPadState, VirtualPadButtonCodes, VirtualPadButtonCode, VirtualPadButtons } from './VirtualPad';
2
2
  import viewportFit, { initializeViewport } from './viewportFit';
3
3
  import makeInject from './makeInject';
4
4
 
5
5
  export {
6
6
  VirtualPadStore,
7
7
  VirtualPadState,
8
+ VirtualPadButtonCodes,
8
9
  VirtualPadButtonCode,
9
10
  VirtualPadButtons,
10
11
  viewportFit,
package/src/makeInject.ts CHANGED
@@ -12,6 +12,5 @@ export default function makeInject() {
12
12
  <button class="wwa-virtualpad__button" id="wwa-fast-button">P</button>
13
13
  <button class="wwa-virtualpad__button" id="wwa-enter-button">Y</button>
14
14
  <button class="wwa-virtualpad__button" id="wwa-esc-button">N</button>
15
- <button class="wwa-virtualpad__button" id="wwa-estimate-button">M</button>
16
15
  </div>`;
17
16
  }
@@ -35,5 +35,5 @@ export default function viewportFit() {
35
35
  } else if (browserWidth <= browserHeight) {
36
36
  viewportValue = `width=${WWA_WIDTH + (BROWSER_MARGIN * 2)},${viewportValue}`;
37
37
  }
38
- viewportElement.setAttribute("content", `${viewportValue}`);
38
+ viewportElement?.setAttribute("content", `${viewportValue}`);
39
39
  }
package/tsconfig.json CHANGED
@@ -3,7 +3,9 @@
3
3
  "compilerOptions": {
4
4
  "rootDir": "./src",
5
5
  "outDir": "./lib",
6
- "module": "esnext"
6
+ "module": "esnext",
7
+ "strict": true,
8
+ "noImplicitAny": true,
7
9
  },
8
10
  "include": [
9
11
  "src/**/*.ts"