-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameSetupPlayerNamesDialog.js
95 lines (79 loc) · 2.9 KB
/
gameSetupPlayerNamesDialog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"use strict";
function createPlayerNamesDialog(assets, onDone) {
// TODO: Prevent key input while fading out.
let playerIndex = 0;
let playerName = "";
let playerHeadlineText = null;
let playerNameText = null;
let playerNamesDialog = createPlayerNamesDialog();
return {
container: playerNamesDialog,
setPlayerIndex: setPlayerIndex,
onKeyPress: onKeyPress,
onKeyDown: onKeyDown,
};
function createPlayerNamesDialog() {
let dialogContainer = new createjs.Container();
let x1 = SCREEN_WIDTH_CENTER - 91;
let y1 = SCREEN_HEIGHT_CENTER - 16;
let x2 = x1 + 182;
let y2 = y1 + 32;
drawBox(assets, dialogContainer, GameColors, x1, y1, x2, y2);
playerHeadlineText = outTextXYAsText(GameColors.WHITE, "", x1 + 3, y1 + 3);
playerHeadlineText.shadow = new createjs.Shadow(GameColors.DARKGRAY, 1, 1, 0);
setCurrentPlayerHeadlineText();
dialogContainer.addChild(playerHeadlineText);
// DrawFrame(x[1]+4,y[1]+14,x[2]-4,y[2]-4);
drawFrame(dialogContainer, GameColors, x1 + 4, y1 + 14, x2 - 4, y2 - 4, GameColors.DARKGRAY);
/*
SetFillStyle(SolidFill,DarkGray);
bar(x[1]+5,y[1]+15,x[2]-5,y[2]-5);
*/
// OutTextXY(x[1]+7,y[1]+17,str+'_');
playerNameText = outTextXYAsText(GameColors.WHITE, "", x1 + 7, y1 + 17, "left", GameColors.BLACK);
playerNameText.shadow = new createjs.Shadow(GameColors.BLACK, 1, 1, 0);
updatePlayerNameText();
dialogContainer.addChild(playerNameText);
return dialogContainer;
}
function setCurrentPlayerHeadlineText() {
playerHeadlineText.text = "Name for Player " + (playerIndex + 1);
playerHeadlineText.color = playerColorTable[playerIndex];
}
function updatePlayerNameText() {
playerNameText.text = playerName + "_";
}
function setPlayerIndex(index) {
playerIndex = index;
playerName = "";
updatePlayerNameText();
setCurrentPlayerHeadlineText();
}
function onKeyDown(stage, key) {
// BACKSPACE is not triggered by onKeyPress, because we "preventDefault" in the GameEngine to suppress the Browser's "back" functionality.
if (key === Keys.BACKSPACE) {
if (playerName.length > 0) {
playerName = playerName.substring(0, playerName.length - 1);
updatePlayerNameText();
}
}
}
function onKeyPress(stage, key) {
if (playerName.length < 18) {
// Note: Pressed keys do not always translate to the values defined in "Keys", so we use Strings and chatCodeAt instead.
if ((key >= "a".charCodeAt(0) && key <= "z".charCodeAt(0)) ||
(key >= "A".charCodeAt(0) && key <= "Z".charCodeAt(0)) ||
(key >= "0".charCodeAt(0) && key <= "9".charCodeAt(0)) ||
key === " ".charCodeAt(0) ||
key === ".".charCodeAt(0) ||
key === "-".charCodeAt(0)) {
let chr = String.fromCharCode(key);
playerName += chr;
updatePlayerNameText();
}
}
if (key === Keys.ENTER && playerName.length > 0) {
onDone(playerName);
}
}
}