Skip to content

Commit fc22865

Browse files
authored
fix: Properties templates area (#190)
* Add bindings for areas * Move function getAreaObject * Update documentation of dynamic property
1 parent 5a9a9be commit fc22865

File tree

6 files changed

+82
-9
lines changed

6 files changed

+82
-9
lines changed

docs/images/areaBindings.png

23.6 KB
Loading

docs/variable-to-property-binding.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ You can create a new variable named "myWebsiteUrl" and bind it to the `openWebsi
3131
You can use template properties in:
3232

3333
- any property of any "tile" layer
34+
- any property of a dynamic "area" object
35+
36+
{.alert.alert-warning}
37+
To use bindings with area objects, you must first set the "dynamic" property to true.
38+
39+
It should be noted that "dynamic" areas are accessible via the scripting API but are not editable in the (upcoming) map editor.
40+
<figure class="figure">
41+
<img class="figure-img img-fluid rounded" src="images/areaBindings.png" alt="" />
42+
<figcaption class="figure-caption">The Dynamic custom property</figcaption>
43+
</figure>
3444

3545
## Configuration
3646

@@ -52,6 +62,8 @@ This means you can use all the features of Mustache like conditional:
5262

5363
The website above will be displayed only if the `enableWebsite` variable is set to `true`.
5464

65+
66+
5567
{.alert.alert-warning}
5668
Be sure to use `{{{ variableName }}}` for binding variable and NOT `{{ variableName }}`. The version with a double
5769
curly-braces will work most of the time, but it escapes HTML characters (which is not needed in properties of a map)

src/AreaObject.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { ITiledMapObject } from "@workadventure/tiled-map-type-guard/dist/ITiledMapObject";
2+
import { getLayersMap } from "./LayersFlattener";
3+
4+
export async function getAreaObject(): Promise<ITiledMapObject[]> {
5+
const layers = await getLayersMap();
6+
const areaArray: Array<ITiledMapObject> = [];
7+
for (const layer of layers.values()) {
8+
if (layer.type === "objectgroup") {
9+
for (const object of layer.objects) {
10+
if (object.type === "area" || object.class === "area") {
11+
areaArray.push(object);
12+
}
13+
}
14+
}
15+
}
16+
return areaArray;
17+
}

src/Features/properties_templates.ts

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { getLayersMap } from "../LayersFlattener";
22
import { TemplateValue } from "../TemplateValue";
3+
import { getAreaObject } from "../AreaObject";
34

4-
export async function initPropertiesTemplates(): Promise<void> {
5-
const layers = await getLayersMap();
6-
7-
for (const [layerName, layer] of layers.entries()) {
8-
const properties = layer.properties ?? [];
5+
export async function initPropertiesTemplatesArea(): Promise<void> {
6+
const areas = await getAreaObject();
7+
for (const area of areas) {
8+
const properties = area.properties ?? [];
99
for (const property of properties) {
1010
if (
1111
property.type === "int" ||
@@ -20,15 +20,54 @@ export async function initPropertiesTemplates(): Promise<void> {
2020
continue;
2121
}
2222
const newValue = template.getValue();
23-
setProperty(layerName, property.name, newValue);
23+
await setPropertyArea(area.name, property.name, newValue);
2424

25-
template.onChange((newValue) => {
26-
setProperty(layerName, property.name, newValue);
25+
template.onChange(async (newValue) => {
26+
await setPropertyArea(area.name, property.name, newValue);
2727
});
2828
}
2929
}
3030
}
3131

32+
export async function initPropertiesTemplates(): Promise<void> {
33+
const layers = await getLayersMap();
34+
35+
for (const [layerName, layer] of layers.entries()) {
36+
if (layer.type !== "objectgroup") {
37+
const properties = layer.properties ?? [];
38+
for (const property of properties) {
39+
if (
40+
property.type === "int" ||
41+
property.type === "bool" ||
42+
property.type === "object" ||
43+
typeof property.value !== "string"
44+
) {
45+
continue;
46+
}
47+
const template = new TemplateValue(property.value, WA.state);
48+
if (template.isPureString()) {
49+
continue;
50+
}
51+
const newValue = template.getValue();
52+
setProperty(layerName, property.name, newValue);
53+
54+
template.onChange((newValue) => {
55+
setProperty(layerName, property.name, newValue);
56+
});
57+
}
58+
}
59+
}
60+
}
61+
62+
async function setPropertyArea(
63+
areaName: string,
64+
propertyName: string,
65+
value: string,
66+
): Promise<void> {
67+
console.log(areaName);
68+
const area = await WA.room.area.get(areaName);
69+
area.setProperty(propertyName, value);
70+
}
3271
/**
3372
* Sets the property value on the map.
3473
* Furthermore, if the property name is "visible", modify the visibility of the layer.

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
export * from "./VariablesExtra";
55
export * from "./Properties";
6+
export * from "./AreaObject";
67
export * from "./LayersFlattener";
78
export * from "./LayersExtra";
89
export * from "./Features/properties_templates";

src/init.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { initDoors } from "./Features/doors";
22
import { initSpecialProperties } from "./Features/special_properties";
33
import { initConfiguration } from "./Features/configuration";
4-
import { initPropertiesTemplates } from "./Features/properties_templates";
4+
import {
5+
initPropertiesTemplates,
6+
initPropertiesTemplatesArea,
7+
} from "./Features/properties_templates";
58

69
/**
710
* Bootstraps all the features of the extra library.
@@ -14,6 +17,7 @@ export function bootstrapExtra(): Promise<void> {
1417
initSpecialProperties().catch((e) => console.error(e));
1518
initConfiguration().catch((e) => console.error(e));
1619
initPropertiesTemplates().catch((e) => console.error(e));
20+
initPropertiesTemplatesArea().catch((e) => console.error(e));
1721
})
1822
.catch((e) => console.error(e));
1923
}

0 commit comments

Comments
 (0)