From 848ac9d97bda75b8aead1f517552f7b548be15f9 Mon Sep 17 00:00:00 2001 From: Matt Mower Date: Mon, 6 Jan 2025 10:13:20 +0000 Subject: [PATCH] v1.5.4 No longer need to special case $player with $global: true Stdlib: adds Number#round_to_nearest ^i{} initializer now implemented wrapped as immediate function Adds RezDie#open_roll Calling event handlers converts false into RezEvent.noop() Adds events to RezGame for scene/end, scene/pause, and scene/resume All Rez CSS classes now have a "rez-" prefix Adds missing accessors --- assets/templates/runtime.js.eex | 5 ----- assets/templates/runtime/rez_0_basic_object.js | 5 +++-- assets/templates/runtime/rez_die.js | 11 +++++++++++ assets/templates/runtime/rez_event.js | 4 ++-- assets/templates/runtime/rez_game.js | 4 ++++ assets/templates/runtime/rez_view.js | 18 +++++++++++++----- assets/templates/stdlib.rez.eex | 13 +++++++++++++ docs/language_reference.adoc | 4 ++-- lib/AST/card.ex | 4 ++-- lib/AST/game.ex | 2 +- lib/AST/scene.ex | 2 +- mix.exs | 2 +- 12 files changed, 53 insertions(+), 21 deletions(-) diff --git a/assets/templates/runtime.js.eex b/assets/templates/runtime.js.eex index ed3b908..54ff578 100644 --- a/assets/templates/runtime.js.eex +++ b/assets/templates/runtime.js.eex @@ -44,9 +44,4 @@ evt.returnValue = ""; }); } - - const player = $("player", false); - if(player) { - window.$player = player; - } })(); diff --git a/assets/templates/runtime/rez_0_basic_object.js b/assets/templates/runtime/rez_0_basic_object.js index 1613b3d..fdc124b 100644 --- a/assets/templates/runtime/rez_0_basic_object.js +++ b/assets/templates/runtime/rez_0_basic_object.js @@ -262,8 +262,9 @@ class RezBasicObject { if(value.constructor == RezDie) { this.setAttribute(attrName, value.roll(), false); } else { - const initializer = value["initializer"]; - this.setAttribute(attrName, eval(initializer), false); + const initializerDef = value["initializer"]; + const initializerSrc = `(function() {${initializerDef}}).call(this)`; + this.setAttribute(attrName, eval(initializerSrc), false); } } diff --git a/assets/templates/runtime/rez_die.js b/assets/templates/runtime/rez_die.js index 70e0e34..dd39dd9 100644 --- a/assets/templates/runtime/rez_die.js +++ b/assets/templates/runtime/rez_die.js @@ -16,6 +16,17 @@ class RezDie { roll() { return Math.rand_int_between(1, this.sides); } + + open_roll() { + let roll, total = 0; + + do { + roll = this.roll(); + total += roll; + } while(roll == this.sides); + + return total; + } } window.Rez.RezDie = RezDie; diff --git a/assets/templates/runtime/rez_event.js b/assets/templates/runtime/rez_event.js index 320c1e6..1c865aa 100644 --- a/assets/templates/runtime/rez_event.js +++ b/assets/templates/runtime/rez_event.js @@ -391,7 +391,7 @@ class RezEventProcessor { } const card = $(cardId); - return card.runEvent("input", { evt: evt }); + return card.runEvent("input", { evt: evt }) || RezEvent.noop(); } handleBrowserSubmitEvent(evt) { @@ -410,7 +410,7 @@ class RezEventProcessor { const cardId = cardDiv.dataset.card; const card = $(cardId); - return card.runEvent(formName, { form: evt.target }); + return card.runEvent(formName, { form: evt.target }) || RezEvent.noop(); } } diff --git a/assets/templates/runtime/rez_game.js b/assets/templates/runtime/rez_game.js index 16b86ba..9684b5c 100644 --- a/assets/templates/runtime/rez_game.js +++ b/assets/templates/runtime/rez_game.js @@ -416,6 +416,7 @@ class RezGame extends RezBasicObject { // current_scene is a Rez attribute defined by @scene if(this.current_scene) { + this.runEvent("scene_end", {}); this.current_scene.finish(); } @@ -441,6 +442,7 @@ class RezGame extends RezBasicObject { interludeSceneWithId(sceneId, params = {}) { // current_scene is a Rez attribute defined by @scene + this.runEvent("scene_pause", {}); this.pushScene(); const scene = this.getTypedGameObject(sceneId, "scene", true); @@ -466,8 +468,10 @@ class RezGame extends RezBasicObject { throw new Error("Cannot resume without a scene on the stack!"); } else { // Let the interlude know we're done + this.runEvent("scene_end", {}); this.current_scene.finish(); this.popScene(params); + this.runEvent("scene_resume", {}); const layout = this.current_scene.getViewLayout(); // Merge any new params into the existing params diff --git a/assets/templates/runtime/rez_view.js b/assets/templates/runtime/rez_view.js index 0b695cb..d423b45 100644 --- a/assets/templates/runtime/rez_view.js +++ b/assets/templates/runtime/rez_view.js @@ -69,6 +69,10 @@ class RezBlock { return this.#flipped; } + set flipped(is_flipped) { + this.#flipped = is_flipped; + } + get params() { return this.#params; } @@ -329,6 +333,10 @@ class RezStackLayout extends RezLayout { this.#contents = []; } + get contents() { + return this.#contents; + } + // get sourceName() { // return this.#sourceName; // } @@ -501,7 +509,7 @@ window.Rez.RezBlockTransformer = RezBlockTransformer; class RezEventLinkTransformer extends RezEventTransformer { constructor(receiver) { - super("div.front-face a[data-event]", "click", receiver); + super("div.rez-front-face a[data-event]", "click", receiver); } } @@ -513,7 +521,7 @@ window.Rez.RezEventLinkTransformer = RezEventLinkTransformer; class RezButtonTransformer extends RezEventTransformer { constructor(receiver) { - super("div.front-face button[data-event]:not(.inactive)", "click", receiver); + super("div.rez-front-face button[data-event]:not(.inactive)", "click", receiver); } } @@ -525,7 +533,7 @@ window.Rez.RezButtonTransformer = RezButtonTransformer; class RezFormTransformer extends RezEventTransformer { constructor(receiver) { - super("div.front-face form[rez-live]", "submit", receiver); + super("div.rez-front-face form[rez-live]", "submit", receiver); } } @@ -537,7 +545,7 @@ window.Rez.RezFormTransformer = RezFormTransformer; class RezInputTransformer extends RezEventTransformer { constructor(receiver) { - super("div.front-face input[rez-live]", "input", receiver); + super("div.rez-front-face input[rez-live]", "input", receiver); } } @@ -549,7 +557,7 @@ window.Rez.RezInputTransformer = RezInputTransformer; class RezBindingTransformer extends RezTransformer { constructor(receiver) { - super("div.front-face input[rez-bind], select[rez-bind], textarea[rez-bind]"); + super("div.rez-front-face input[rez-bind], select[rez-bind], textarea[rez-bind]"); } decodeBinding(binding_expr) { diff --git a/assets/templates/stdlib.rez.eex b/assets/templates/stdlib.rez.eex index 2b41b51..f8b3c4d 100644 --- a/assets/templates/stdlib.rez.eex +++ b/assets/templates/stdlib.rez.eex @@ -620,6 +620,19 @@ } } +@patch PATCH_NUMBER_ROUND_TO_NEAREST { + $built_in: true + patch: "Number" + method: "round_to_nearest" + impl: function(n) { + if(n === 0) { + return this; + } else { + return Math.round(this / n) * n; + } + } +} + @patch PATCH_NUMBER_CHANCE { $built_in: true patch: "Number" diff --git a/docs/language_reference.adoc b/docs/language_reference.adoc index 130c3f0..4052a3c 100644 --- a/docs/language_reference.adoc +++ b/docs/language_reference.adoc @@ -313,8 +313,8 @@ Rez defines many attribute types, some simple and some more complicated. The mor |`^{Math.rand_int(1,10)}` |Dynamic Initializer -|A Javascript expression that is evaluated when the game starts -|`^i{Math.rand_int(1,10)}` +|A Javascript expression that is evaluated when the game starts. It converted into an expression `(function() {return }).call(this)` +|`^i{return Math.rand_int(1,10)}` |Dynamic Property |A Javascript function expression that is converted into an object property diff --git a/lib/AST/card.ex b/lib/AST/card.ex index 2665b57..fac978d 100644 --- a/lib/AST/card.ex +++ b/lib/AST/card.ex @@ -29,7 +29,7 @@ defmodule Rez.AST.Card do html else custom_css_class = NodeHelper.get_attr_value(card, "css_class", "") - css_classes = Utils.add_css_class("front-face", custom_css_class) + css_classes = Utils.add_css_class("rez-front-face", custom_css_class) ~s|
#{html}
| end @@ -50,7 +50,7 @@ defmodule Rez.AST.Card do html else custom_css_class = NodeHelper.get_attr_value(card, "css_class", "") - css_classes = Utils.add_css_class("flipped-face", custom_css_class) + css_classes = Utils.add_css_class("rez-flipped-face", custom_css_class) ~s|
#{html}
| end diff --git a/lib/AST/game.ex b/lib/AST/game.ex index ff759c8..f6b768b 100644 --- a/lib/AST/game.ex +++ b/lib/AST/game.ex @@ -217,7 +217,7 @@ defmodule Rez.AST.Game do NodeHelper.get_attr_value(game, "layout"), fn html -> custom_css_class = NodeHelper.get_attr_value(game, "css_class", "") - css_classes = add_css_class("game", custom_css_class) + css_classes = add_css_class("rez-game", custom_css_class) ~s|
#{html}
| end diff --git a/lib/AST/scene.ex b/lib/AST/scene.ex index 2e1831b..935150f 100644 --- a/lib/AST/scene.ex +++ b/lib/AST/scene.ex @@ -31,7 +31,7 @@ defmodule Rez.AST.Scene do NodeHelper.get_attr_value(scene, "layout"), fn content -> custom_css_class = NodeHelper.get_attr_value(scene, "css_class", "") - css_classes = Utils.add_css_class("scene", custom_css_class) + css_classes = Utils.add_css_class("rez-scene", custom_css_class) ~s|
#{content}
| end diff --git a/mix.exs b/mix.exs index aed675c..75cbfe9 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule Rez.MixProject do use Mix.Project - @version "1.5.3" + @version "1.5.4" def project do [