From bbb98967943e07af897fa465ca141401d5e13a62 Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 7 Dec 2015 19:35:47 +0200 Subject: [PATCH] Parse menu statement --- public_html/css/run.css | 34 ++++++++++++++++++++++++++- public_html/index.html | 5 ++++ public_html/js/Parser.js | 51 ++++++++++++++++++++++++++++++++++++++-- public_html/js/Utils.js | 20 +++++++++++++++- public_html/js/Views.js | 27 +++++++++++++++++++++ 5 files changed, 133 insertions(+), 4 deletions(-) diff --git a/public_html/css/run.css b/public_html/css/run.css index 63192cd..afae6c0 100644 --- a/public_html/css/run.css +++ b/public_html/css/run.css @@ -6,7 +6,7 @@ html, body { overflow: hidden; } -html, body, div, span, p { +html, body, div, span, p, li, ul { margin: 0; padding: 0; } @@ -51,4 +51,36 @@ body { .sprite { position: absolute; bottom: 0px; +} + +#menu { + position: absolute; + width: 100%; + height: 100%; + top: 0; + text-align: center; + background-color: rgba(0, 0, 0, 0.5); + padding: 10px; + color: white; +} +#menuTitle { + font-size: 120%; + margin: 16px 0; +} +#menuChoose { + list-style: none; + width: 70%; + margin: 0 auto; + color: #eee; +} +#menuChoose li { + display: block; + background-color: rgba(0, 0, 0, 0.5); + padding: 5px; + margin: 5px 10px; +} +#menuChoose li:hover { + background-color: rgba(200, 200, 200, 0.5); + cursor: pointer; + color: #fff; } \ No newline at end of file diff --git a/public_html/index.html b/public_html/index.html index 39d948d..34a961d 100644 --- a/public_html/index.html +++ b/public_html/index.html @@ -26,6 +26,11 @@
+ diff --git a/public_html/js/Parser.js b/public_html/js/Parser.js index d079cb3..a393f0c 100644 --- a/public_html/js/Parser.js +++ b/public_html/js/Parser.js @@ -19,6 +19,11 @@ Parser.prototype.getInstance = function () { return this; }; +Parser.prototype.setPosition = function (position) { + this.position = position; + this.next(); +}; + Parser.prototype.jumpLabel = function (label) { if (label in this.labels) { this.position = this.labels[label]; @@ -72,7 +77,7 @@ Parser.prototype.statement = function() { if (this.match(token, TokenType.JUMP)) return this.jump(); if (this.match(token, TokenType.IF)) return this.ifStatement(); - /*if (this.lookMatch(1, TokenType.COLON)) { + if (this.lookMatch(1, TokenType.COLON)) { // menu: if (this.match(token, TokenType.MENU)) return this.menu(); @@ -87,7 +92,7 @@ Parser.prototype.statement = function() { if (this.hasEndIf) this.position += this.skipIf(); return false; } - }*/ + } // Текст с именем автора реплики. if (this.lookMatch(1, TokenType.TEXT) && this.match(token, TokenType.WORD)) { @@ -323,6 +328,48 @@ Parser.prototype.playAmbience = function() { return false; }; +Parser.prototype.menu = function() { + // menu: title? + this.consume(TokenType.COLON); + + var title = ""; + if (this.lookMatch(0, TokenType.TEXT) && !this.lookMatch(1, TokenType.COLON)) { + title = this.consume(TokenType.TEXT).getText(); + } + + if (!this.hasEndMenu) return false; + // Ищем элементы выбора + var menu = new Menu(title); + var pos = 0; + var level = 1; // уровень вложенности меню + while (true) { + // Расчёт уровня меню. + if (this.lookMatch(pos, TokenType.MENU) && this.lookMatch(pos + 1, TokenType.COLON)) { + level++; + pos++; + } + if (this.lookMatch(pos, TokenType.ENDMENU)) { + level--; + // Завершаем работу по достижению ENDMENU первого уровня. + if (level <= 0) break; + } + + if (level == 1) { + // Добавляем только пункты из меню первого уровня. + if (this.lookMatch(pos, TokenType.TEXT) && this.lookMatch(pos + 1, TokenType.COLON)) { + menu.addItem(this.get(pos).getText(), this.position + pos + 2); + pos++; + } + } + + if (this.lookMatch(pos, TokenType.EOF)) return false; + pos++; + } + + ViewActivity.getInstance().menu(menu); + return true; +}; + Parser.prototype.jump = function() { var labelName = this.consume(TokenType.WORD).getText(); this.jumpLabel(labelName); diff --git a/public_html/js/Utils.js b/public_html/js/Utils.js index d52eb3f..24c0c8c 100644 --- a/public_html/js/Utils.js +++ b/public_html/js/Utils.js @@ -75,4 +75,22 @@ var PathResolver = new function () { path += ".ogg"; return path; }; -}; \ No newline at end of file +}; + +function Menu(title) { + this.title = title; + this.items = new Array(); +} + +Menu.prototype.getTitle = function () { return this.title; }; + +Menu.prototype.getItems = function () { return this.items; }; + +Menu.prototype.addItem = function (name, position) { + this.items.push({name: name, position: position}); +}; + +Menu.prototype.getPosition = function (itemIndex) { + return this.items[itemIndex].position; +}; + diff --git a/public_html/js/Views.js b/public_html/js/Views.js index 46ba84a..416e4ae 100644 --- a/public_html/js/Views.js +++ b/public_html/js/Views.js @@ -26,6 +26,8 @@ function Views(parser) { Views.blockTap = false; if (!Views.cancelNextStep) parser.next(); }; + + $('#menu').hide(); } Views.prototype.getInstance = function () { @@ -34,6 +36,7 @@ Views.prototype.getInstance = function () { Views.prototype.onTouch = function (e) { if (this.blockTap) return; + if ($('#menu').is(":visible")) return; this.cancelNextStep = true; this.parser.next(); @@ -290,6 +293,30 @@ Views.prototype.showMap = function () { }; Views.prototype.menu = function (menu) { + if (!TextUtils.isEmpty(menu.getTitle())) { + $('#menuTitle').text(menu.getTitle()); + } else { + $('#menuTitle').text('Выберите: '); + } + $('#menuChoose').empty(); + var items = menu.getItems(); + for(var i = 0; i < items.length; i++) { + var li = $('
  • ', {text: items[i].name}); + li.click(this.createMenuItemClickFunction(i, menu)); + li.appendTo($('#menuChoose')); + } + $('#menu').show(); +}; + +Views.prototype.createMenuItemClickFunction = function (index, menu) { + var views = this; + return function () { + $('#menu').hide(); + views.parser.setPosition(menu.getPosition(index)); + }; +}; + +Views.prototype.showGameMenuDialog = function (menu) { }; Views.prototype.music = function (name, fade) {