diff --git a/.gitignore b/.gitignore index abd9be1..f75c473 100644 --- a/.gitignore +++ b/.gitignore @@ -60,3 +60,4 @@ docs/_build/ # PyBuilder target/ +.vs diff --git a/src/core/table.py b/src/core/table.py index 0341009..2925f05 100644 --- a/src/core/table.py +++ b/src/core/table.py @@ -105,6 +105,11 @@ def calc_coin(self, winner): coins.append(-coin - tax) return coins + def handle_chat(self, player, msg): + response = [Pt.RSP_CHAT, player.uid, msg] + for p in self.players: + p.send(response) + def add(self, player): for i, p in enumerate(self.players): if not p: diff --git a/src/net/protocol.py b/src/net/protocol.py index 2341849..da2bc9b 100644 --- a/src/net/protocol.py +++ b/src/net/protocol.py @@ -28,3 +28,6 @@ class Protocol(IntEnum): REQ_GAME_OVER = 41 RSP_GAME_OVER = 42 + + REQ_CHAT = 43 + RSP_CHAT = 44 \ No newline at end of file diff --git a/src/net/socket.py b/src/net/socket.py index f67ea51..43d09b9 100644 --- a/src/net/socket.py +++ b/src/net/socket.py @@ -92,6 +92,8 @@ def on_message(self, message): elif code == Pt.REQ_SHOT_POKER: self.handle_shot_poker(packet) + elif code == Pt.REQ_CHAT: + self.handle_chat(packet) else: logger.info('UNKNOWN PACKET: %s', code) @@ -110,6 +112,10 @@ def find_table(self, table_id): return self.room.first_waiting_table() return self.room.find_waiting_table(table_id) + def handle_chat(self, packet): + if self.player and self.player.table: + self.player.table.handle_chat(self.player, packet[1]) + def write_message(self, message, binary=False): if self.ws_connection is None: raise WebSocketClosedError() diff --git a/src/static/js/boot.js b/src/static/js/boot.js index 02242d1..1c2d29c 100644 --- a/src/static/js/boot.js +++ b/src/static/js/boot.js @@ -94,6 +94,21 @@ PG.MainMenu.prototype = { var setting = this.add.button(this.world.width/2, this.world.height * 3/4, 'btn', this.gotoSetting, this, 'setting.png', 'setting.png', 'setting.png'); setting.anchor.set(0.5); this.world.add(setting); + + this.game.add.plugin(PhaserInput.Plugin); + var input = this.add.inputField(100, 90, { + font: '18px Arial', + fill: '#ff0000', + fontWeight: 'bold', + width: 150, + padding: 8, + borderWidth: 1, + borderColor: '#000', + borderRadius: 6, + placeHolder: 'Password', + type: PhaserInput.InputType.password + }); + input.setText("My custom text"); }, startGame: function () { diff --git a/src/static/js/phaser-input.js b/src/static/js/phaser-input.js new file mode 100644 index 0000000..1ff8d1f --- /dev/null +++ b/src/static/js/phaser-input.js @@ -0,0 +1,720 @@ +/*! + * phaser-input - version 2.0.5 + * Adds input boxes to Phaser like CanvasInput, but also works for WebGL and Mobile, made for Phaser only. + * + * OrangeGames + * Build at 02-06-2017 + * Released under MIT License + */ + +var __extends = (this && this.__extends) || (function () { + var extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +var PhaserInput; +(function (PhaserInput) { + var InputType; + (function (InputType) { + InputType[InputType["text"] = 0] = "text"; + InputType[InputType["password"] = 1] = "password"; + InputType[InputType["number"] = 2] = "number"; + })(InputType = PhaserInput.InputType || (PhaserInput.InputType = {})); + var InputElement = (function () { + function InputElement(game, id, type, value, focusIn, focusOut) { + if (type === void 0) { type = InputType.text; } + if (value === void 0) { value = ''; } + var _this = this; + this.id = id; + this.type = type; + this.game = game; + this.focusIn = focusIn; + this.focusOut = focusOut; + var canvasTopX = this.game.canvas.getBoundingClientRect().top + document.body.scrollTop; + this.element = document.createElement('input'); + this.element.id = id; + this.element.style.position = 'absolute'; + this.element.style.top = canvasTopX + 'px'; + this.element.style.left = (-40).toString() + 'px'; + this.element.style.width = (10).toString() + 'px'; + this.element.style.height = (10).toString() + 'px'; + this.element.style.border = '0px'; + this.element.value = this.value; + this.element.type = InputType[type]; + this.element.addEventListener('focusin', function () { + if (_this.focusIn instanceof Phaser.Signal) { + _this.focusIn.dispatch(); + } + }); + this.element.addEventListener('focusout', function () { + if (_this.focusOut instanceof Phaser.Signal) { + _this.focusOut.dispatch(); + } + }); + document.body.appendChild(this.element); + } + InputElement.prototype.addKeyUpListener = function (callback) { + this.keyUpCallback = callback; + document.addEventListener('keyup', this.keyUpCallback); + this.element.addEventListener('input', this.keyUpCallback); + }; + InputElement.prototype.blockKeyDownEvents = function () { + document.addEventListener('keydown', this.preventKeyPropagation); + }; + InputElement.prototype.preventKeyPropagation = function (evt) { + if (evt.stopPropagation) { + evt.stopPropagation(); + } + else { + event.cancelBubble = true; + } + }; + InputElement.prototype.unblockKeyDownEvents = function () { + document.removeEventListener('keydown', this.preventKeyPropagation); + }; + InputElement.prototype.removeEventListener = function () { + document.removeEventListener('keyup', this.keyUpCallback); + this.element.removeEventListener('input', this.keyUpCallback); + }; + InputElement.prototype.destroy = function () { + document.body.removeChild(this.element); + }; + InputElement.prototype.setMax = function (max, min) { + if (max === undefined) { + return; + } + if (this.type === InputType.text || this.type === InputType.password) { + this.element.maxLength = parseInt(max, 10); + } + else if (this.type === InputType.number) { + this.element.max = max; + if (min === undefined) { + return; + } + this.element.min = min; + } + }; + Object.defineProperty(InputElement.prototype, "value", { + get: function () { + return this.element.value; + }, + set: function (value) { + this.element.value = value; + }, + enumerable: true, + configurable: true + }); + InputElement.prototype.focus = function () { + var _this = this; + this.element.focus(); + if (!this.game.device.desktop && this.game.device.chrome) { + var originalWidth_1 = window.innerWidth, originalHeight_1 = window.innerHeight; + var kbAppeared_1 = false; + var interval_1 = setInterval(function () { + if (originalWidth_1 > window.innerWidth || originalHeight_1 > window.innerHeight) { + kbAppeared_1 = true; + } + if (kbAppeared_1 && originalWidth_1 === window.innerWidth && originalHeight_1 === window.innerHeight) { + if (_this.focusOut instanceof Phaser.Signal) { + _this.focusOut.dispatch(); + } + clearInterval(interval_1); + } + }, 50); + } + }; + InputElement.prototype.blur = function () { + this.element.blur(); + }; + Object.defineProperty(InputElement.prototype, "hasSelection", { + get: function () { + if (this.type === InputType.number) { + return false; + } + return this.element.selectionStart !== this.element.selectionEnd; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(InputElement.prototype, "caretStart", { + get: function () { + return this.element.selectionEnd; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(InputElement.prototype, "caretEnd", { + get: function () { + return this.element.selectionStart; + }, + enumerable: true, + configurable: true + }); + InputElement.prototype.getCaretPosition = function () { + if (this.type === InputType.number) { + return -1; + } + return this.element.selectionStart; + }; + InputElement.prototype.setCaretPosition = function (pos) { + if (this.type === InputType.number) { + return; + } + this.element.setSelectionRange(pos, pos); + }; + return InputElement; + }()); + PhaserInput.InputElement = InputElement; +})(PhaserInput || (PhaserInput = {})); +var PhaserInput; +(function (PhaserInput) { + var ForceCase; + (function (ForceCase) { + ForceCase[ForceCase["none"] = 0] = "none"; + ForceCase[ForceCase["lower"] = 1] = "lower"; + ForceCase[ForceCase["upper"] = 2] = "upper"; + })(ForceCase = PhaserInput.ForceCase || (PhaserInput.ForceCase = {})); + var InputField = (function (_super) { + __extends(InputField, _super); + function InputField(game, x, y, inputOptions) { + if (inputOptions === void 0) { inputOptions = {}; } + var _this = _super.call(this, game, x, y) || this; + _this.focusOutOnEnter = true; + _this.placeHolder = null; + _this.box = null; + _this.focus = false; + _this.value = ''; + _this.windowScale = 1; + _this.blockInput = true; + _this.focusIn = new Phaser.Signal(); + _this.focusOut = new Phaser.Signal(); + _this.blink = true; + _this.cnt = 0; + _this.inputOptions = inputOptions; + _this.inputOptions.width = (typeof inputOptions.width === 'number') ? inputOptions.width : 150; + _this.inputOptions.padding = (typeof inputOptions.padding === 'number') ? inputOptions.padding : 0; + _this.inputOptions.textAlign = inputOptions.textAlign || 'left'; + _this.inputOptions.type = inputOptions.type || PhaserInput.InputType.text; + _this.inputOptions.forceCase = (inputOptions.forceCase) ? inputOptions.forceCase : ForceCase.none; + _this.inputOptions.borderRadius = (typeof inputOptions.borderRadius === 'number') ? inputOptions.borderRadius : 0; + _this.inputOptions.height = (typeof inputOptions.height === 'number') ? inputOptions.height : 14; + _this.inputOptions.fillAlpha = (inputOptions.fillAlpha === undefined) ? 1 : inputOptions.fillAlpha; + _this.inputOptions.selectionColor = inputOptions.selectionColor || 'rgba(179, 212, 253, 0.8)'; + _this.inputOptions.zoom = (!game.device.desktop) ? inputOptions.zoom || false : false; + _this.box = new PhaserInput.InputBox(_this.game, inputOptions); + _this.setTexture(_this.box.generateTexture()); + _this.textMask = new PhaserInput.TextMask(_this.game, inputOptions); + _this.addChild(_this.textMask); + _this.domElement = new PhaserInput.InputElement(_this.game, 'phaser-input-' + (Math.random() * 10000 | 0).toString(), _this.inputOptions.type, _this.value, _this.focusIn, _this.focusOut); + _this.domElement.setMax(_this.inputOptions.max, _this.inputOptions.min); + _this.selection = new PhaserInput.SelectionHighlight(_this.game, _this.inputOptions); + _this.selection.mask = _this.textMask; + _this.addChild(_this.selection); + if (inputOptions.placeHolder && inputOptions.placeHolder.length > 0) { + _this.placeHolder = new Phaser.Text(game, _this.inputOptions.padding, _this.inputOptions.padding, inputOptions.placeHolder, { + font: inputOptions.font || '14px Arial', + fontWeight: inputOptions.fontWeight || 'normal', + fill: inputOptions.placeHolderColor || '#bfbebd' + }); + _this.placeHolder.mask = _this.textMask; + _this.addChild(_this.placeHolder); + } + _this.cursor = new Phaser.Text(game, _this.inputOptions.padding, _this.inputOptions.padding - 2, '|', { + font: inputOptions.font || '14px Arial', + fontWeight: inputOptions.fontWeight || 'normal', + fill: inputOptions.cursorColor || '#000000' + }); + _this.cursor.visible = false; + _this.addChild(_this.cursor); + _this.text = new Phaser.Text(game, _this.inputOptions.padding, _this.inputOptions.padding, '', { + font: inputOptions.font || '14px Arial', + fontWeight: inputOptions.fontWeight || 'normal', + fill: inputOptions.fill || '#000000' + }); + _this.text.mask = _this.textMask; + _this.addChild(_this.text); + _this.offscreenText = new Phaser.Text(game, _this.inputOptions.padding, _this.inputOptions.padding, '', { + font: inputOptions.font || '14px Arial', + fontWeight: inputOptions.fontWeight || 'normal', + fill: inputOptions.fill || '#000000' + }); + _this.updateTextAlignment(); + _this.inputEnabled = true; + _this.input.useHandCursor = true; + _this.game.input.onDown.add(_this.checkDown, _this); + _this.focusOut.add(function () { + if (PhaserInput.KeyboardOpen) { + _this.endFocus(); + if (_this.inputOptions.zoom) { + _this.zoomOut(); + } + } + }); + return _this; + } + Object.defineProperty(InputField.prototype, "width", { + get: function () { + return this.inputOptions.width; + }, + set: function (width) { + this.inputOptions.width = width; + this.box.resize(width); + this.textMask.resize(width); + this.updateTextAlignment(); + }, + enumerable: true, + configurable: true + }); + InputField.prototype.updateTextAlignment = function () { + switch (this.inputOptions.textAlign) { + case 'left': + this.text.anchor.set(0, 0); + this.text.x = this.inputOptions.padding; + if (null !== this.placeHolder) { + this.placeHolder.anchor.set(0, 0); + } + this.cursor.x = this.inputOptions.padding + this.getCaretPosition(); + break; + case 'center': + this.text.anchor.set(0.5, 0); + this.text.x = this.inputOptions.padding + this.inputOptions.width / 2; + if (null !== this.placeHolder) { + this.placeHolder.anchor.set(0.5, 0); + this.placeHolder.x = this.inputOptions.padding + this.inputOptions.width / 2; + } + this.cursor.x = this.inputOptions.padding + this.inputOptions.width / 2 - this.text.width / 2 + this.getCaretPosition(); + break; + case 'right': + this.text.anchor.set(1, 0); + this.text.x = this.inputOptions.padding + this.inputOptions.width; + if (null !== this.placeHolder) { + this.placeHolder.anchor.set(1, 0); + this.placeHolder.x = this.inputOptions.padding + this.inputOptions.width; + } + this.cursor.x = this.inputOptions.padding + this.inputOptions.width; + break; + } + }; + InputField.prototype.checkDown = function (e) { + if (!this.value) { + this.resetText(); + } + if (this.input.checkPointerOver(e)) { + if (this.focus) { + this.setCaretOnclick(e); + return; + } + if (this.inputOptions.zoom && !PhaserInput.Zoomed) { + this.zoomIn(); + } + this.startFocus(); + } + else { + if (this.focus === true) { + this.endFocus(); + if (this.inputOptions.zoom) { + this.zoomOut(); + } + } + } + }; + InputField.prototype.update = function () { + this.text.update(); + if (this.placeHolder) { + this.placeHolder.update(); + } + if (!this.focus) { + return; + } + if (this.cnt !== 30) { + return this.cnt++; + } + this.cursor.visible = this.blink; + this.blink = !this.blink; + this.cnt = 0; + }; + InputField.prototype.endFocus = function () { + var _this = this; + if (!this.focus) { + return; + } + this.domElement.removeEventListener(); + if (this.blockInput === true) { + this.domElement.unblockKeyDownEvents(); + } + this.focus = false; + if (this.value.length === 0 && null !== this.placeHolder) { + this.placeHolder.visible = true; + } + this.cursor.visible = false; + if (this.game.device.desktop) { + setTimeout(function () { + _this.domElement.blur(); + }, 0); + } + else { + this.domElement.blur(); + } + if (!this.game.device.desktop) { + PhaserInput.KeyboardOpen = false; + PhaserInput.onKeyboardClose.dispatch(); + } + }; + InputField.prototype.startFocus = function () { + var _this = this; + this.focus = true; + if (null !== this.placeHolder) { + this.placeHolder.visible = false; + } + if (this.game.device.desktop) { + setTimeout(function () { + _this.keyUpProcessor(); + }, 0); + } + else { + this.keyUpProcessor(); + } + if (!this.game.device.desktop) { + PhaserInput.KeyboardOpen = true; + PhaserInput.onKeyboardOpen.dispatch(); + } + }; + InputField.prototype.keyUpProcessor = function () { + this.domElement.addKeyUpListener(this.keyListener.bind(this)); + this.domElement.focus(); + if (this.blockInput === true) { + this.domElement.blockKeyDownEvents(); + } + }; + InputField.prototype.updateText = function () { + var text = ''; + if (this.inputOptions.type === PhaserInput.InputType.password) { + for (var i = 0; i < this.value.length; i++) { + text += '*'; + } + } + else if (this.inputOptions.type === PhaserInput.InputType.number) { + var val = parseInt(this.value); + if (val < parseInt(this.inputOptions.min)) { + text = this.value = this.domElement.value = this.inputOptions.min; + } + else if (val > parseInt(this.inputOptions.max)) { + text = this.value = this.domElement.value = this.inputOptions.max; + } + else { + text = this.value; + } + } + else { + text = this.value; + } + this.text.setText(text); + if (this.text.width > this.inputOptions.width) { + this.text.anchor.x = 1; + this.text.x = this.inputOptions.padding + this.inputOptions.width; + } + else { + switch (this.inputOptions.textAlign) { + case 'left': + this.text.anchor.set(0, 0); + this.text.x = this.inputOptions.padding; + break; + case 'center': + this.text.anchor.set(0.5, 0); + this.text.x = this.inputOptions.padding + this.inputOptions.width / 2; + break; + case 'right': + this.text.anchor.set(1, 0); + this.text.x = this.inputOptions.padding + this.inputOptions.width; + break; + } + } + }; + InputField.prototype.updateCursor = function () { + if (this.text.width > this.inputOptions.width || this.inputOptions.textAlign === 'right') { + this.cursor.x = this.inputOptions.padding + this.inputOptions.width; + } + else { + switch (this.inputOptions.textAlign) { + case 'left': + this.cursor.x = this.inputOptions.padding + this.getCaretPosition(); + break; + case 'center': + this.cursor.x = this.inputOptions.padding + this.inputOptions.width / 2 - this.text.width / 2 + this.getCaretPosition(); + break; + } + } + }; + InputField.prototype.getCaretPosition = function () { + var caretPosition = this.domElement.getCaretPosition(); + if (-1 === caretPosition) { + return this.text.width; + } + var text = this.value; + if (this.inputOptions.type === PhaserInput.InputType.password) { + text = ''; + for (var i = 0; i < this.value.length; i++) { + text += '*'; + } + } + this.offscreenText.setText(text.slice(0, caretPosition)); + return this.offscreenText.width; + }; + InputField.prototype.setCaretOnclick = function (e) { + var localX = (this.text.toLocal(new PIXI.Point(e.x, e.y), this.game.world)).x; + if (this.inputOptions.textAlign && this.inputOptions.textAlign === 'center') { + localX += this.text.width / 2; + } + var characterWidth = this.text.width / this.value.length; + var index = 0; + for (var i = 0; i < this.value.length; i++) { + if (localX >= i * characterWidth && localX <= (i + 1) * characterWidth) { + index = i; + break; + } + } + if (localX > (this.value.length - 1) * characterWidth) { + index = this.value.length; + } + this.startFocus(); + this.domElement.setCaretPosition(index); + this.updateCursor(); + }; + InputField.prototype.updateSelection = function () { + if (this.domElement.hasSelection) { + var text = this.value; + if (this.inputOptions.type === PhaserInput.InputType.password) { + text = ''; + for (var i = 0; i < this.value.length; i++) { + text += '*'; + } + } + text = text.substring(this.domElement.caretStart, this.domElement.caretEnd); + this.offscreenText.setText(text); + this.selection.updateSelection(this.offscreenText.getBounds()); + switch (this.inputOptions.textAlign) { + case 'left': + this.selection.x = this.inputOptions.padding; + break; + case 'center': + this.selection.x = this.inputOptions.padding + this.inputOptions.width / 2 - this.text.width / 2; + break; + } + } + else { + this.selection.clear(); + } + }; + InputField.prototype.zoomIn = function () { + if (PhaserInput.Zoomed) { + return; + } + var bounds = this.getBounds(); + if (window.innerHeight > window.innerWidth) { + this.windowScale = this.game.width / (bounds.width * 1.5); + } + else { + this.windowScale = (this.game.width / 2) / (bounds.width * 1.5); + } + var offsetX = ((this.game.width - bounds.width * 1.5) / 2) / this.windowScale; + this.game.world.scale.set(this.game.world.scale.x * this.windowScale, this.game.world.scale.y * this.windowScale); + this.game.world.pivot.set(bounds.x - offsetX, bounds.y - this.inputOptions.padding * 2); + PhaserInput.Zoomed = true; + }; + InputField.prototype.zoomOut = function () { + if (!PhaserInput.Zoomed) { + return; + } + this.game.world.scale.set(this.game.world.scale.x / this.windowScale, this.game.world.scale.y / this.windowScale); + this.game.world.pivot.set(0, 0); + PhaserInput.Zoomed = false; + }; + InputField.prototype.keyListener = function (evt) { + this.value = this.getFormattedText(this.domElement.value); + if (evt.keyCode === 13) { + if (this.focusOutOnEnter) { + this.endFocus(); + } + return; + } + this.updateText(); + this.updateCursor(); + this.updateSelection(); + evt.preventDefault(); + }; + InputField.prototype.destroy = function (destroyChildren) { + this.game.input.onDown.remove(this.checkDown, this); + this.focusIn.removeAll(); + this.focusOut.removeAll(); + this.domElement.destroy(); + _super.prototype.destroy.call(this, destroyChildren); + }; + InputField.prototype.resetText = function () { + this.setText(); + }; + InputField.prototype.setText = function (text) { + if (text === void 0) { text = ''; } + if (null !== this.placeHolder) { + if (text.length > 0) { + this.placeHolder.visible = false; + } + else { + this.placeHolder.visible = true; + } + } + this.value = this.getFormattedText(text); + this.domElement.value = this.value; + this.updateText(); + this.updateCursor(); + this.endFocus(); + }; + InputField.prototype.getFormattedText = function (text) { + switch (this.inputOptions.forceCase) { + default: + case ForceCase.none: + return text; + case ForceCase.lower: + return text.toLowerCase(); + case ForceCase.upper: + return text.toUpperCase(); + } + }; + return InputField; + }(Phaser.Sprite)); + PhaserInput.InputField = InputField; +})(PhaserInput || (PhaserInput = {})); +var PhaserInput; +(function (PhaserInput) { + var InputBox = (function (_super) { + __extends(InputBox, _super); + function InputBox(game, inputOptions) { + var _this = _super.call(this, game, 0, 0) || this; + _this.bgColor = (inputOptions.backgroundColor) ? parseInt(inputOptions.backgroundColor.slice(1), 16) : 0xffffff; + _this.borderRadius = inputOptions.borderRadius || 0; + _this.borderWidth = inputOptions.borderWidth || 1; + _this.borderColor = (inputOptions.borderColor) ? parseInt(inputOptions.borderColor.slice(1), 16) : 0x959595; + _this.boxAlpha = inputOptions.fillAlpha; + _this.padding = inputOptions.padding; + var height = inputOptions.height; + var width = inputOptions.width; + var height; + if (inputOptions.font) { + height = Math.max(parseInt(inputOptions.font.substr(0, inputOptions.font.indexOf('px')), 10), height); + } + _this.boxHeight = _this.padding * 2 + height; + var width = inputOptions.width; + _this.boxWidth = _this.padding * 2 + width; + _this.drawBox(); + return _this; + } + InputBox.prototype.resize = function (newWidth) { + this.boxWidth = this.padding * 2 + newWidth; + this.drawBox(); + }; + InputBox.prototype.drawBox = function () { + this.clear() + .beginFill(this.bgColor, this.boxAlpha) + .lineStyle(this.borderWidth, this.borderColor, this.boxAlpha); + if (this.borderRadius > 0) { + this.drawRoundedRect(0, 0, this.boxWidth, this.boxHeight, this.borderRadius); + } + else { + this.drawRect(0, 0, this.boxWidth, this.boxHeight); + } + }; + return InputBox; + }(Phaser.Graphics)); + PhaserInput.InputBox = InputBox; +})(PhaserInput || (PhaserInput = {})); +var PhaserInput; +(function (PhaserInput) { + var SelectionHighlight = (function (_super) { + __extends(SelectionHighlight, _super); + function SelectionHighlight(game, inputOptions) { + var _this = _super.call(this, game, inputOptions.padding, inputOptions.padding) || this; + _this.inputOptions = inputOptions; + return _this; + } + SelectionHighlight.prototype.updateSelection = function (rect) { + var color = Phaser.Color.webToColor(this.inputOptions.selectionColor); + this.clear(); + this.beginFill(SelectionHighlight.rgb2hex(color), color.a); + this.drawRect(rect.x, rect.y, rect.width, rect.height - this.inputOptions.padding); + }; + SelectionHighlight.rgb2hex = function (color) { + return parseInt(("0" + color.r.toString(16)).slice(-2) + + ("0" + color.g.toString(16)).slice(-2) + + ("0" + color.b.toString(16)).slice(-2), 16); + }; + return SelectionHighlight; + }(Phaser.Graphics)); + PhaserInput.SelectionHighlight = SelectionHighlight; +})(PhaserInput || (PhaserInput = {})); +var PhaserInput; +(function (PhaserInput) { + var TextMask = (function (_super) { + __extends(TextMask, _super); + function TextMask(game, inputOptions) { + var _this = _super.call(this, game, inputOptions.padding, inputOptions.padding) || this; + var height = inputOptions.height; + if (inputOptions.font) { + height = Math.max(parseInt(inputOptions.font.substr(0, inputOptions.font.indexOf('px')), 10), height); + } + _this.maskWidth = inputOptions.width; + _this.maskHeight = height * 1.3; + _this.drawMask(); + return _this; + } + TextMask.prototype.resize = function (newWidth) { + this.maskWidth = newWidth; + this.drawMask(); + }; + TextMask.prototype.drawMask = function () { + this.clear() + .beginFill(0x000000) + .drawRect(0, 0, this.maskWidth, this.maskHeight) + .endFill(); + }; + return TextMask; + }(Phaser.Graphics)); + PhaserInput.TextMask = TextMask; +})(PhaserInput || (PhaserInput = {})); +var PhaserInput; +(function (PhaserInput) { + PhaserInput.Zoomed = false; + PhaserInput.KeyboardOpen = false; + PhaserInput.onKeyboardOpen = new Phaser.Signal(); + PhaserInput.onKeyboardClose = new Phaser.Signal(); + var Plugin = (function (_super) { + __extends(Plugin, _super); + function Plugin(game, parent) { + var _this = _super.call(this, game, parent) || this; + _this.addInputFieldFactory(); + return _this; + } + Plugin.prototype.addInputFieldFactory = function () { + Phaser.GameObjectFactory.prototype.inputField = function (x, y, inputOptions, group) { + if (group === undefined) { + group = this.world; + } + var nineSliceObject = new PhaserInput.InputField(this.game, x, y, inputOptions); + return group.add(nineSliceObject); + }; + Phaser.GameObjectCreator.prototype.inputField = function (x, y, inputOptions) { + return new PhaserInput.InputField(this.game, x, y, inputOptions); + }; + }; + return Plugin; + }(Phaser.Plugin)); + Plugin.Zoomed = false; + Plugin.KeyboardOpen = false; + Plugin.onKeyboardOpen = new Phaser.Signal(); + Plugin.onKeyboardClose = new Phaser.Signal(); + PhaserInput.Plugin = Plugin; +})(PhaserInput || (PhaserInput = {})); +//# sourceMappingURL=phaser-input.js.map \ No newline at end of file diff --git a/src/static/js/phaser-input.min.js b/src/static/js/phaser-input.min.js new file mode 100644 index 0000000..a44a0ff --- /dev/null +++ b/src/static/js/phaser-input.min.js @@ -0,0 +1,10 @@ +/*! + * phaser-input - version 2.0.5 + * Adds input boxes to Phaser like CanvasInput, but also works for WebGL and Mobile, made for Phaser only. + * + * OrangeGames + * Build at 02-06-2017 + * Released under MIT License + */ + +var __extends=this&&this.__extends||function(){var a=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(a,b){a.__proto__=b}||function(a,b){for(var c in b)b.hasOwnProperty(c)&&(a[c]=b[c])};return function(b,c){function d(){this.constructor=b}a(b,c),b.prototype=null===c?Object.create(c):(d.prototype=c.prototype,new d)}}(),PhaserInput;!function(a){var b;!function(a){a[a.text=0]="text",a[a.password=1]="password",a[a.number=2]="number"}(b=a.InputType||(a.InputType={}));var c=function(){function a(a,c,d,e,f,g){void 0===d&&(d=b.text),void 0===e&&(e="");var h=this;this.id=c,this.type=d,this.game=a,this.focusIn=f,this.focusOut=g;var i=this.game.canvas.getBoundingClientRect().top+document.body.scrollTop;this.element=document.createElement("input"),this.element.id=c,this.element.style.position="absolute",this.element.style.top=i+"px",this.element.style.left=(-40).toString()+"px",this.element.style.width=10..toString()+"px",this.element.style.height=10..toString()+"px",this.element.style.border="0px",this.element.value=this.value,this.element.type=b[d],this.element.addEventListener("focusin",function(){h.focusIn instanceof Phaser.Signal&&h.focusIn.dispatch()}),this.element.addEventListener("focusout",function(){h.focusOut instanceof Phaser.Signal&&h.focusOut.dispatch()}),document.body.appendChild(this.element)}return a.prototype.addKeyUpListener=function(a){this.keyUpCallback=a,document.addEventListener("keyup",this.keyUpCallback),this.element.addEventListener("input",this.keyUpCallback)},a.prototype.blockKeyDownEvents=function(){document.addEventListener("keydown",this.preventKeyPropagation)},a.prototype.preventKeyPropagation=function(a){a.stopPropagation?a.stopPropagation():event.cancelBubble=!0},a.prototype.unblockKeyDownEvents=function(){document.removeEventListener("keydown",this.preventKeyPropagation)},a.prototype.removeEventListener=function(){document.removeEventListener("keyup",this.keyUpCallback),this.element.removeEventListener("input",this.keyUpCallback)},a.prototype.destroy=function(){document.body.removeChild(this.element)},a.prototype.setMax=function(a,c){if(void 0!==a)if(this.type===b.text||this.type===b.password)this.element.maxLength=parseInt(a,10);else if(this.type===b.number){if(this.element.max=a,void 0===c)return;this.element.min=c}},Object.defineProperty(a.prototype,"value",{get:function(){return this.element.value},set:function(a){this.element.value=a},enumerable:!0,configurable:!0}),a.prototype.focus=function(){var a=this;if(this.element.focus(),!this.game.device.desktop&&this.game.device.chrome)var b=window.innerWidth,c=window.innerHeight,d=!1,e=setInterval(function(){(b>window.innerWidth||c>window.innerHeight)&&(d=!0),d&&b===window.innerWidth&&c===window.innerHeight&&(a.focusOut instanceof Phaser.Signal&&a.focusOut.dispatch(),clearInterval(e))},50)},a.prototype.blur=function(){this.element.blur()},Object.defineProperty(a.prototype,"hasSelection",{get:function(){return this.type===b.number?!1:this.element.selectionStart!==this.element.selectionEnd},enumerable:!0,configurable:!0}),Object.defineProperty(a.prototype,"caretStart",{get:function(){return this.element.selectionEnd},enumerable:!0,configurable:!0}),Object.defineProperty(a.prototype,"caretEnd",{get:function(){return this.element.selectionStart},enumerable:!0,configurable:!0}),a.prototype.getCaretPosition=function(){return this.type===b.number?-1:this.element.selectionStart},a.prototype.setCaretPosition=function(a){this.type!==b.number&&this.element.setSelectionRange(a,a)},a}();a.InputElement=c}(PhaserInput||(PhaserInput={}));var PhaserInput;!function(a){var b;!function(a){a[a.none=0]="none",a[a.lower=1]="lower",a[a.upper=2]="upper"}(b=a.ForceCase||(a.ForceCase={}));var c=function(c){function d(d,e,f,g){void 0===g&&(g={});var h=c.call(this,d,e,f)||this;return h.focusOutOnEnter=!0,h.placeHolder=null,h.box=null,h.focus=!1,h.value="",h.windowScale=1,h.blockInput=!0,h.focusIn=new Phaser.Signal,h.focusOut=new Phaser.Signal,h.blink=!0,h.cnt=0,h.inputOptions=g,h.inputOptions.width="number"==typeof g.width?g.width:150,h.inputOptions.padding="number"==typeof g.padding?g.padding:0,h.inputOptions.textAlign=g.textAlign||"left",h.inputOptions.type=g.type||a.InputType.text,h.inputOptions.forceCase=g.forceCase?g.forceCase:b.none,h.inputOptions.borderRadius="number"==typeof g.borderRadius?g.borderRadius:0,h.inputOptions.height="number"==typeof g.height?g.height:14,h.inputOptions.fillAlpha=void 0===g.fillAlpha?1:g.fillAlpha,h.inputOptions.selectionColor=g.selectionColor||"rgba(179, 212, 253, 0.8)",h.inputOptions.zoom=d.device.desktop?!1:g.zoom||!1,h.box=new a.InputBox(h.game,g),h.setTexture(h.box.generateTexture()),h.textMask=new a.TextMask(h.game,g),h.addChild(h.textMask),h.domElement=new a.InputElement(h.game,"phaser-input-"+(1e4*Math.random()|0).toString(),h.inputOptions.type,h.value,h.focusIn,h.focusOut),h.domElement.setMax(h.inputOptions.max,h.inputOptions.min),h.selection=new a.SelectionHighlight(h.game,h.inputOptions),h.selection.mask=h.textMask,h.addChild(h.selection),g.placeHolder&&g.placeHolder.length>0&&(h.placeHolder=new Phaser.Text(d,h.inputOptions.padding,h.inputOptions.padding,g.placeHolder,{font:g.font||"14px Arial",fontWeight:g.fontWeight||"normal",fill:g.placeHolderColor||"#bfbebd"}),h.placeHolder.mask=h.textMask,h.addChild(h.placeHolder)),h.cursor=new Phaser.Text(d,h.inputOptions.padding,h.inputOptions.padding-2,"|",{font:g.font||"14px Arial",fontWeight:g.fontWeight||"normal",fill:g.cursorColor||"#000000"}),h.cursor.visible=!1,h.addChild(h.cursor),h.text=new Phaser.Text(d,h.inputOptions.padding,h.inputOptions.padding,"",{font:g.font||"14px Arial",fontWeight:g.fontWeight||"normal",fill:g.fill||"#000000"}),h.text.mask=h.textMask,h.addChild(h.text),h.offscreenText=new Phaser.Text(d,h.inputOptions.padding,h.inputOptions.padding,"",{font:g.font||"14px Arial",fontWeight:g.fontWeight||"normal",fill:g.fill||"#000000"}),h.updateTextAlignment(),h.inputEnabled=!0,h.input.useHandCursor=!0,h.game.input.onDown.add(h.checkDown,h),h.focusOut.add(function(){a.KeyboardOpen&&(h.endFocus(),h.inputOptions.zoom&&h.zoomOut())}),h}return __extends(d,c),Object.defineProperty(d.prototype,"width",{get:function(){return this.inputOptions.width},set:function(a){this.inputOptions.width=a,this.box.resize(a),this.textMask.resize(a),this.updateTextAlignment()},enumerable:!0,configurable:!0}),d.prototype.updateTextAlignment=function(){switch(this.inputOptions.textAlign){case"left":this.text.anchor.set(0,0),this.text.x=this.inputOptions.padding,null!==this.placeHolder&&this.placeHolder.anchor.set(0,0),this.cursor.x=this.inputOptions.padding+this.getCaretPosition();break;case"center":this.text.anchor.set(.5,0),this.text.x=this.inputOptions.padding+this.inputOptions.width/2,null!==this.placeHolder&&(this.placeHolder.anchor.set(.5,0),this.placeHolder.x=this.inputOptions.padding+this.inputOptions.width/2),this.cursor.x=this.inputOptions.padding+this.inputOptions.width/2-this.text.width/2+this.getCaretPosition();break;case"right":this.text.anchor.set(1,0),this.text.x=this.inputOptions.padding+this.inputOptions.width,null!==this.placeHolder&&(this.placeHolder.anchor.set(1,0),this.placeHolder.x=this.inputOptions.padding+this.inputOptions.width),this.cursor.x=this.inputOptions.padding+this.inputOptions.width}},d.prototype.checkDown=function(b){if(this.value||this.resetText(),this.input.checkPointerOver(b)){if(this.focus)return void this.setCaretOnclick(b);this.inputOptions.zoom&&!a.Zoomed&&this.zoomIn(),this.startFocus()}else this.focus===!0&&(this.endFocus(),this.inputOptions.zoom&&this.zoomOut())},d.prototype.update=function(){if(this.text.update(),this.placeHolder&&this.placeHolder.update(),this.focus){if(30!==this.cnt)return this.cnt++;this.cursor.visible=this.blink,this.blink=!this.blink,this.cnt=0}},d.prototype.endFocus=function(){var b=this;this.focus&&(this.domElement.removeEventListener(),this.blockInput===!0&&this.domElement.unblockKeyDownEvents(),this.focus=!1,0===this.value.length&&null!==this.placeHolder&&(this.placeHolder.visible=!0),this.cursor.visible=!1,this.game.device.desktop?setTimeout(function(){b.domElement.blur()},0):this.domElement.blur(),this.game.device.desktop||(a.KeyboardOpen=!1,a.onKeyboardClose.dispatch()))},d.prototype.startFocus=function(){var b=this;this.focus=!0,null!==this.placeHolder&&(this.placeHolder.visible=!1),this.game.device.desktop?setTimeout(function(){b.keyUpProcessor()},0):this.keyUpProcessor(),this.game.device.desktop||(a.KeyboardOpen=!0,a.onKeyboardOpen.dispatch())},d.prototype.keyUpProcessor=function(){this.domElement.addKeyUpListener(this.keyListener.bind(this)),this.domElement.focus(),this.blockInput===!0&&this.domElement.blockKeyDownEvents()},d.prototype.updateText=function(){var b="";if(this.inputOptions.type===a.InputType.password)for(var c=0;cparseInt(this.inputOptions.max)?this.value=this.domElement.value=this.inputOptions.max:this.value}else b=this.value;if(this.text.setText(b),this.text.width>this.inputOptions.width)this.text.anchor.x=1,this.text.x=this.inputOptions.padding+this.inputOptions.width;else switch(this.inputOptions.textAlign){case"left":this.text.anchor.set(0,0),this.text.x=this.inputOptions.padding;break;case"center":this.text.anchor.set(.5,0),this.text.x=this.inputOptions.padding+this.inputOptions.width/2;break;case"right":this.text.anchor.set(1,0),this.text.x=this.inputOptions.padding+this.inputOptions.width}},d.prototype.updateCursor=function(){if(this.text.width>this.inputOptions.width||"right"===this.inputOptions.textAlign)this.cursor.x=this.inputOptions.padding+this.inputOptions.width;else switch(this.inputOptions.textAlign){case"left":this.cursor.x=this.inputOptions.padding+this.getCaretPosition();break;case"center":this.cursor.x=this.inputOptions.padding+this.inputOptions.width/2-this.text.width/2+this.getCaretPosition()}},d.prototype.getCaretPosition=function(){var b=this.domElement.getCaretPosition();if(-1===b)return this.text.width;var c=this.value;if(this.inputOptions.type===a.InputType.password){c="";for(var d=0;d=e*c&&(e+1)*c>=b){d=e;break}b>(this.value.length-1)*c&&(d=this.value.length),this.startFocus(),this.domElement.setCaretPosition(d),this.updateCursor()},d.prototype.updateSelection=function(){if(this.domElement.hasSelection){var b=this.value;if(this.inputOptions.type===a.InputType.password){b="";for(var c=0;cwindow.innerWidth?this.windowScale=this.game.width/(1.5*b.width):this.windowScale=this.game.width/2/(1.5*b.width);var c=(this.game.width-1.5*b.width)/2/this.windowScale;this.game.world.scale.set(this.game.world.scale.x*this.windowScale,this.game.world.scale.y*this.windowScale),this.game.world.pivot.set(b.x-c,b.y-2*this.inputOptions.padding),a.Zoomed=!0}},d.prototype.zoomOut=function(){a.Zoomed&&(this.game.world.scale.set(this.game.world.scale.x/this.windowScale,this.game.world.scale.y/this.windowScale),this.game.world.pivot.set(0,0),a.Zoomed=!1)},d.prototype.keyListener=function(a){return this.value=this.getFormattedText(this.domElement.value),13===a.keyCode?void(this.focusOutOnEnter&&this.endFocus()):(this.updateText(),this.updateCursor(),this.updateSelection(),void a.preventDefault())},d.prototype.destroy=function(a){this.game.input.onDown.remove(this.checkDown,this),this.focusIn.removeAll(),this.focusOut.removeAll(),this.domElement.destroy(),c.prototype.destroy.call(this,a)},d.prototype.resetText=function(){this.setText()},d.prototype.setText=function(a){void 0===a&&(a=""),null!==this.placeHolder&&(a.length>0?this.placeHolder.visible=!1:this.placeHolder.visible=!0),this.value=this.getFormattedText(a),this.domElement.value=this.value,this.updateText(),this.updateCursor(),this.endFocus()},d.prototype.getFormattedText=function(a){switch(this.inputOptions.forceCase){default:case b.none:return a;case b.lower:return a.toLowerCase();case b.upper:return a.toUpperCase()}},d}(Phaser.Sprite);a.InputField=c}(PhaserInput||(PhaserInput={}));var PhaserInput;!function(a){var b=function(a){function b(b,c){var d=a.call(this,b,0,0)||this;d.bgColor=c.backgroundColor?parseInt(c.backgroundColor.slice(1),16):16777215,d.borderRadius=c.borderRadius||0,d.borderWidth=c.borderWidth||1,d.borderColor=c.borderColor?parseInt(c.borderColor.slice(1),16):9803157,d.boxAlpha=c.fillAlpha,d.padding=c.padding;var e,e=c.height,f=c.width;c.font&&(e=Math.max(parseInt(c.font.substr(0,c.font.indexOf("px")),10),e)),d.boxHeight=2*d.padding+e;var f=c.width;return d.boxWidth=2*d.padding+f,d.drawBox(),d}return __extends(b,a),b.prototype.resize=function(a){this.boxWidth=2*this.padding+a,this.drawBox()},b.prototype.drawBox=function(){this.clear().beginFill(this.bgColor,this.boxAlpha).lineStyle(this.borderWidth,this.borderColor,this.boxAlpha),this.borderRadius>0?this.drawRoundedRect(0,0,this.boxWidth,this.boxHeight,this.borderRadius):this.drawRect(0,0,this.boxWidth,this.boxHeight)},b}(Phaser.Graphics);a.InputBox=b}(PhaserInput||(PhaserInput={}));var PhaserInput;!function(a){var b=function(a){function b(b,c){var d=a.call(this,b,c.padding,c.padding)||this;return d.inputOptions=c,d}return __extends(b,a),b.prototype.updateSelection=function(a){var c=Phaser.Color.webToColor(this.inputOptions.selectionColor);this.clear(),this.beginFill(b.rgb2hex(c),c.a),this.drawRect(a.x,a.y,a.width,a.height-this.inputOptions.padding)},b.rgb2hex=function(a){return parseInt(("0"+a.r.toString(16)).slice(-2)+("0"+a.g.toString(16)).slice(-2)+("0"+a.b.toString(16)).slice(-2),16)},b}(Phaser.Graphics);a.SelectionHighlight=b}(PhaserInput||(PhaserInput={}));var PhaserInput;!function(a){var b=function(a){function b(b,c){var d=a.call(this,b,c.padding,c.padding)||this,e=c.height;return c.font&&(e=Math.max(parseInt(c.font.substr(0,c.font.indexOf("px")),10),e)),d.maskWidth=c.width,d.maskHeight=1.3*e,d.drawMask(),d}return __extends(b,a),b.prototype.resize=function(a){this.maskWidth=a,this.drawMask()},b.prototype.drawMask=function(){this.clear().beginFill(0).drawRect(0,0,this.maskWidth,this.maskHeight).endFill()},b}(Phaser.Graphics);a.TextMask=b}(PhaserInput||(PhaserInput={}));var PhaserInput;!function(a){a.Zoomed=!1,a.KeyboardOpen=!1,a.onKeyboardOpen=new Phaser.Signal,a.onKeyboardClose=new Phaser.Signal;var b=function(b){function c(a,c){var d=b.call(this,a,c)||this;return d.addInputFieldFactory(),d}return __extends(c,b),c.prototype.addInputFieldFactory=function(){Phaser.GameObjectFactory.prototype.inputField=function(b,c,d,e){void 0===e&&(e=this.world);var f=new a.InputField(this.game,b,c,d);return e.add(f)},Phaser.GameObjectCreator.prototype.inputField=function(b,c,d){return new a.InputField(this.game,b,c,d)}},c}(Phaser.Plugin);b.Zoomed=!1,b.KeyboardOpen=!1,b.onKeyboardOpen=new Phaser.Signal,b.onKeyboardClose=new Phaser.Signal,a.Plugin=b}(PhaserInput||(PhaserInput={})); \ No newline at end of file diff --git a/src/static/poker.html b/src/static/poker.html index 5b1c3d0..2133537 100644 --- a/src/static/poker.html +++ b/src/static/poker.html @@ -6,6 +6,7 @@ 斗地主 +