ttpg-darrell 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
1
  import { StaticObject, Vector } from "@tabletop-playground/api";
2
+ import { TriggerableMulticastDelegate } from "../triggerable-multicast-delegate/triggerable-multicast-delegate";
2
3
  export type LayoutObjectsSize = {
3
4
  w: number;
4
5
  h: number;
@@ -11,6 +12,8 @@ export declare class LayoutObjects {
11
12
  private _isVertical;
12
13
  private _overrideHeight;
13
14
  private _overrideWidth;
15
+ private _layoutCenter;
16
+ readonly afterLayout: TriggerableMulticastDelegate<unknown>;
14
17
  constructor();
15
18
  setChildDistanace(value: number): this;
16
19
  setHorizontalAlignment(value: number): this;
@@ -19,6 +22,7 @@ export declare class LayoutObjects {
19
22
  setOverrideHeight(value: number): this;
20
23
  setOverrideWidth(value: number): this;
21
24
  add(item: StaticObject | LayoutObjects): this;
25
+ flip(flipH: boolean, flipV: boolean): this;
22
26
  /**
23
27
  * Get size of self, applying any overrides.
24
28
  *
@@ -33,6 +37,7 @@ export declare class LayoutObjects {
33
37
  calculateChildrenSize(): LayoutObjectsSize;
34
38
  static _calculateChildSize(child: StaticObject | LayoutObjects): LayoutObjectsSize;
35
39
  doLayoutAtPoint(center: Vector, yaw: number): this;
40
+ getCenter(): Vector;
36
41
  layoutLeftOf(peer: StaticObject, gap: number): this;
37
42
  layoutRightOf(peer: StaticObject, gap: number): this;
38
43
  layoutAbove(peer: StaticObject, gap: number): this;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LayoutObjects = void 0;
4
4
  const api_1 = require("@tabletop-playground/api");
5
+ const triggerable_multicast_delegate_1 = require("../triggerable-multicast-delegate/triggerable-multicast-delegate");
5
6
  class LayoutObjects {
6
7
  constructor() {
7
8
  this._children = [];
@@ -11,6 +12,8 @@ class LayoutObjects {
11
12
  this._isVertical = false;
12
13
  this._overrideHeight = 0;
13
14
  this._overrideWidth = 0;
15
+ this._layoutCenter = new api_1.Vector(0, 0, 0);
16
+ this.afterLayout = new triggerable_multicast_delegate_1.TriggerableMulticastDelegate();
14
17
  }
15
18
  setChildDistanace(value) {
16
19
  this._childDistance = value;
@@ -41,6 +44,36 @@ class LayoutObjects {
41
44
  this._children.push(item);
42
45
  return this;
43
46
  }
47
+ flip(flipH, flipV) {
48
+ // Children.
49
+ if ((flipH && !this._isVertical) || (flipV && this._isVertical)) {
50
+ this._children.reverse();
51
+ }
52
+ // Layout.
53
+ if (flipH) {
54
+ if (this._horizontalAlignment === api_1.HorizontalAlignment.Left) {
55
+ this._horizontalAlignment = api_1.HorizontalAlignment.Right;
56
+ }
57
+ else if (this._horizontalAlignment === api_1.HorizontalAlignment.Right) {
58
+ this._horizontalAlignment = api_1.HorizontalAlignment.Left;
59
+ }
60
+ }
61
+ if (flipV) {
62
+ if (this._verticalAlignment === api_1.VerticalAlignment.Top) {
63
+ this._verticalAlignment = api_1.VerticalAlignment.Bottom;
64
+ }
65
+ else if (this._verticalAlignment === api_1.VerticalAlignment.Bottom) {
66
+ this._verticalAlignment = api_1.VerticalAlignment.Top;
67
+ }
68
+ }
69
+ // Recurse.
70
+ for (const child of this._children) {
71
+ if (child instanceof LayoutObjects) {
72
+ child.flip(flipH, flipV);
73
+ }
74
+ }
75
+ return this;
76
+ }
44
77
  /**
45
78
  * Get size of self, applying any overrides.
46
79
  *
@@ -99,34 +132,61 @@ class LayoutObjects {
99
132
  return childSize;
100
133
  }
101
134
  doLayoutAtPoint(center, yaw) {
102
- const size = this.calculateSize();
135
+ this._layoutCenter = center;
136
+ const overrideSize = this.calculateSize();
103
137
  const childrenSize = this.calculateChildrenSize();
138
+ // Position accounting for override size.
104
139
  let padLeft;
140
+ let padTop;
105
141
  if (this._horizontalAlignment === api_1.HorizontalAlignment.Left) {
106
142
  padLeft = 0;
107
143
  }
108
144
  else if (this._horizontalAlignment === api_1.HorizontalAlignment.Right) {
109
- padLeft = size.w - childrenSize.w;
145
+ padLeft = overrideSize.w - childrenSize.w;
110
146
  }
111
147
  else {
112
- padLeft = (size.w - childrenSize.w) / 2; // center (even if "Fill")
148
+ padLeft = (overrideSize.w - childrenSize.w) / 2; // center (even if "Fill")
113
149
  }
114
- let padTop;
115
150
  if (this._verticalAlignment === api_1.VerticalAlignment.Top) {
116
151
  padTop = 0;
117
152
  }
118
153
  else if (this._verticalAlignment === api_1.VerticalAlignment.Bottom) {
119
- padTop = size.w - childrenSize.w;
154
+ padTop = overrideSize.h - childrenSize.h;
120
155
  }
121
156
  else {
122
- padTop = (size.w - childrenSize.w) / 2; // center (even if "Fill")
157
+ padTop = (overrideSize.h - childrenSize.h) / 2; // center (even if "Fill")
123
158
  }
124
- let left = -size.w / 2 + padLeft;
125
- let top = -size.h / 2 + padTop;
159
+ let left = -overrideSize.w / 2 + padLeft;
160
+ let top = overrideSize.h / 2 - padTop;
126
161
  for (const child of this._children) {
127
162
  const childSize = LayoutObjects._calculateChildSize(child);
163
+ // Apply layout in row/col.
164
+ padLeft = 0;
165
+ padTop = 0;
166
+ if (this._isVertical) {
167
+ if (this._horizontalAlignment === api_1.HorizontalAlignment.Left) {
168
+ padLeft = 0;
169
+ }
170
+ else if (this._horizontalAlignment === api_1.HorizontalAlignment.Right) {
171
+ padLeft = childrenSize.w - childSize.w;
172
+ }
173
+ else {
174
+ padLeft = (childrenSize.w - childSize.w) / 2; // center (even if "Fill")
175
+ }
176
+ }
177
+ else {
178
+ if (this._verticalAlignment === api_1.VerticalAlignment.Top) {
179
+ padTop = 0;
180
+ }
181
+ else if (this._verticalAlignment === api_1.VerticalAlignment.Bottom) {
182
+ padTop = childrenSize.h - childSize.h;
183
+ }
184
+ else {
185
+ padTop = (childrenSize.h - childSize.h) / 2; // center (even if "Fill")
186
+ }
187
+ }
128
188
  // Calculate child center (world).
129
- const childCenter = new api_1.Vector(top + childSize.h / 2, left + childSize.w / 2, 0)
189
+ const childCenter = new api_1.Vector(top - childSize.h / 2 - padTop, left + childSize.w / 2 + padLeft, 0)
130
190
  .rotateAngleAxis(yaw, [0, 0, 1])
131
191
  .add(center);
132
192
  // Position child.
@@ -139,14 +199,18 @@ class LayoutObjects {
139
199
  }
140
200
  // Move "cursor" to next open spot top-left.
141
201
  if (this._isVertical) {
142
- top += childSize.h + this._childDistance;
202
+ top -= childSize.h + this._childDistance;
143
203
  }
144
204
  else {
145
205
  left += childSize.w + this._childDistance;
146
206
  }
147
207
  }
208
+ this.afterLayout.trigger();
148
209
  return this;
149
210
  }
211
+ getCenter() {
212
+ return this._layoutCenter;
213
+ }
150
214
  layoutLeftOf(peer, gap) {
151
215
  const peerSize = LayoutObjects._calculateChildSize(peer);
152
216
  const size = this.calculateSize();
@@ -5,6 +5,7 @@ import { GameObject, Vector } from "@tabletop-playground/api";
5
5
  export declare abstract class Spawn {
6
6
  private static _nsidToTemplateId;
7
7
  static spawn(nsid: string, position: Vector | [x: number, y: number, z: number]): GameObject | undefined;
8
+ static spawnOrThrow(nsid: string, position: Vector | [x: number, y: number, z: number]): GameObject;
8
9
  static inject(dict: {
9
10
  [key: string]: string;
10
11
  }): void;
@@ -9,11 +9,18 @@ class Spawn {
9
9
  static spawn(nsid, position) {
10
10
  const templateId = Spawn._nsidToTemplateId[nsid];
11
11
  if (!templateId) {
12
- throw new Error(`spawn nsid "${nsid}" has no templateId`);
12
+ return undefined;
13
13
  }
14
14
  const obj = api_1.world.createObjectFromTemplate(templateId, position);
15
15
  return obj;
16
16
  }
17
+ static spawnOrThrow(nsid, position) {
18
+ const obj = Spawn.spawn(nsid, position);
19
+ if (!obj) {
20
+ throw new Error(`spawnOrThrow failed for "${nsid}"`);
21
+ }
22
+ return obj;
23
+ }
17
24
  static inject(dict) {
18
25
  for (const [k, v] of Object.entries(dict)) {
19
26
  Spawn._nsidToTemplateId[k] = v;
@@ -1,4 +1,5 @@
1
1
  import { StaticObject, Vector } from "@tabletop-playground/api";
2
+ import { TriggerableMulticastDelegate } from "../triggerable-multicast-delegate/triggerable-multicast-delegate";
2
3
  export type LayoutObjectsSize = {
3
4
  w: number;
4
5
  h: number;
@@ -11,6 +12,8 @@ export declare class LayoutObjects {
11
12
  private _isVertical;
12
13
  private _overrideHeight;
13
14
  private _overrideWidth;
15
+ private _layoutCenter;
16
+ readonly afterLayout: TriggerableMulticastDelegate<unknown>;
14
17
  constructor();
15
18
  setChildDistanace(value: number): this;
16
19
  setHorizontalAlignment(value: number): this;
@@ -19,6 +22,7 @@ export declare class LayoutObjects {
19
22
  setOverrideHeight(value: number): this;
20
23
  setOverrideWidth(value: number): this;
21
24
  add(item: StaticObject | LayoutObjects): this;
25
+ flip(flipH: boolean, flipV: boolean): this;
22
26
  /**
23
27
  * Get size of self, applying any overrides.
24
28
  *
@@ -33,6 +37,7 @@ export declare class LayoutObjects {
33
37
  calculateChildrenSize(): LayoutObjectsSize;
34
38
  static _calculateChildSize(child: StaticObject | LayoutObjects): LayoutObjectsSize;
35
39
  doLayoutAtPoint(center: Vector, yaw: number): this;
40
+ getCenter(): Vector;
36
41
  layoutLeftOf(peer: StaticObject, gap: number): this;
37
42
  layoutRightOf(peer: StaticObject, gap: number): this;
38
43
  layoutAbove(peer: StaticObject, gap: number): this;
@@ -1,4 +1,5 @@
1
1
  import { HorizontalAlignment, StaticObject, Vector, VerticalAlignment, } from "@tabletop-playground/api";
2
+ import { TriggerableMulticastDelegate } from "../triggerable-multicast-delegate/triggerable-multicast-delegate";
2
3
  export class LayoutObjects {
3
4
  constructor() {
4
5
  this._children = [];
@@ -8,6 +9,8 @@ export class LayoutObjects {
8
9
  this._isVertical = false;
9
10
  this._overrideHeight = 0;
10
11
  this._overrideWidth = 0;
12
+ this._layoutCenter = new Vector(0, 0, 0);
13
+ this.afterLayout = new TriggerableMulticastDelegate();
11
14
  }
12
15
  setChildDistanace(value) {
13
16
  this._childDistance = value;
@@ -38,6 +41,36 @@ export class LayoutObjects {
38
41
  this._children.push(item);
39
42
  return this;
40
43
  }
44
+ flip(flipH, flipV) {
45
+ // Children.
46
+ if ((flipH && !this._isVertical) || (flipV && this._isVertical)) {
47
+ this._children.reverse();
48
+ }
49
+ // Layout.
50
+ if (flipH) {
51
+ if (this._horizontalAlignment === HorizontalAlignment.Left) {
52
+ this._horizontalAlignment = HorizontalAlignment.Right;
53
+ }
54
+ else if (this._horizontalAlignment === HorizontalAlignment.Right) {
55
+ this._horizontalAlignment = HorizontalAlignment.Left;
56
+ }
57
+ }
58
+ if (flipV) {
59
+ if (this._verticalAlignment === VerticalAlignment.Top) {
60
+ this._verticalAlignment = VerticalAlignment.Bottom;
61
+ }
62
+ else if (this._verticalAlignment === VerticalAlignment.Bottom) {
63
+ this._verticalAlignment = VerticalAlignment.Top;
64
+ }
65
+ }
66
+ // Recurse.
67
+ for (const child of this._children) {
68
+ if (child instanceof LayoutObjects) {
69
+ child.flip(flipH, flipV);
70
+ }
71
+ }
72
+ return this;
73
+ }
41
74
  /**
42
75
  * Get size of self, applying any overrides.
43
76
  *
@@ -96,34 +129,61 @@ export class LayoutObjects {
96
129
  return childSize;
97
130
  }
98
131
  doLayoutAtPoint(center, yaw) {
99
- const size = this.calculateSize();
132
+ this._layoutCenter = center;
133
+ const overrideSize = this.calculateSize();
100
134
  const childrenSize = this.calculateChildrenSize();
135
+ // Position accounting for override size.
101
136
  let padLeft;
137
+ let padTop;
102
138
  if (this._horizontalAlignment === HorizontalAlignment.Left) {
103
139
  padLeft = 0;
104
140
  }
105
141
  else if (this._horizontalAlignment === HorizontalAlignment.Right) {
106
- padLeft = size.w - childrenSize.w;
142
+ padLeft = overrideSize.w - childrenSize.w;
107
143
  }
108
144
  else {
109
- padLeft = (size.w - childrenSize.w) / 2; // center (even if "Fill")
145
+ padLeft = (overrideSize.w - childrenSize.w) / 2; // center (even if "Fill")
110
146
  }
111
- let padTop;
112
147
  if (this._verticalAlignment === VerticalAlignment.Top) {
113
148
  padTop = 0;
114
149
  }
115
150
  else if (this._verticalAlignment === VerticalAlignment.Bottom) {
116
- padTop = size.w - childrenSize.w;
151
+ padTop = overrideSize.h - childrenSize.h;
117
152
  }
118
153
  else {
119
- padTop = (size.w - childrenSize.w) / 2; // center (even if "Fill")
154
+ padTop = (overrideSize.h - childrenSize.h) / 2; // center (even if "Fill")
120
155
  }
121
- let left = -size.w / 2 + padLeft;
122
- let top = -size.h / 2 + padTop;
156
+ let left = -overrideSize.w / 2 + padLeft;
157
+ let top = overrideSize.h / 2 - padTop;
123
158
  for (const child of this._children) {
124
159
  const childSize = LayoutObjects._calculateChildSize(child);
160
+ // Apply layout in row/col.
161
+ padLeft = 0;
162
+ padTop = 0;
163
+ if (this._isVertical) {
164
+ if (this._horizontalAlignment === HorizontalAlignment.Left) {
165
+ padLeft = 0;
166
+ }
167
+ else if (this._horizontalAlignment === HorizontalAlignment.Right) {
168
+ padLeft = childrenSize.w - childSize.w;
169
+ }
170
+ else {
171
+ padLeft = (childrenSize.w - childSize.w) / 2; // center (even if "Fill")
172
+ }
173
+ }
174
+ else {
175
+ if (this._verticalAlignment === VerticalAlignment.Top) {
176
+ padTop = 0;
177
+ }
178
+ else if (this._verticalAlignment === VerticalAlignment.Bottom) {
179
+ padTop = childrenSize.h - childSize.h;
180
+ }
181
+ else {
182
+ padTop = (childrenSize.h - childSize.h) / 2; // center (even if "Fill")
183
+ }
184
+ }
125
185
  // Calculate child center (world).
126
- const childCenter = new Vector(top + childSize.h / 2, left + childSize.w / 2, 0)
186
+ const childCenter = new Vector(top - childSize.h / 2 - padTop, left + childSize.w / 2 + padLeft, 0)
127
187
  .rotateAngleAxis(yaw, [0, 0, 1])
128
188
  .add(center);
129
189
  // Position child.
@@ -136,14 +196,18 @@ export class LayoutObjects {
136
196
  }
137
197
  // Move "cursor" to next open spot top-left.
138
198
  if (this._isVertical) {
139
- top += childSize.h + this._childDistance;
199
+ top -= childSize.h + this._childDistance;
140
200
  }
141
201
  else {
142
202
  left += childSize.w + this._childDistance;
143
203
  }
144
204
  }
205
+ this.afterLayout.trigger();
145
206
  return this;
146
207
  }
208
+ getCenter() {
209
+ return this._layoutCenter;
210
+ }
147
211
  layoutLeftOf(peer, gap) {
148
212
  const peerSize = LayoutObjects._calculateChildSize(peer);
149
213
  const size = this.calculateSize();
@@ -5,6 +5,7 @@ import { GameObject, Vector } from "@tabletop-playground/api";
5
5
  export declare abstract class Spawn {
6
6
  private static _nsidToTemplateId;
7
7
  static spawn(nsid: string, position: Vector | [x: number, y: number, z: number]): GameObject | undefined;
8
+ static spawnOrThrow(nsid: string, position: Vector | [x: number, y: number, z: number]): GameObject;
8
9
  static inject(dict: {
9
10
  [key: string]: string;
10
11
  }): void;
@@ -6,11 +6,18 @@ export class Spawn {
6
6
  static spawn(nsid, position) {
7
7
  const templateId = Spawn._nsidToTemplateId[nsid];
8
8
  if (!templateId) {
9
- throw new Error(`spawn nsid "${nsid}" has no templateId`);
9
+ return undefined;
10
10
  }
11
11
  const obj = world.createObjectFromTemplate(templateId, position);
12
12
  return obj;
13
13
  }
14
+ static spawnOrThrow(nsid, position) {
15
+ const obj = Spawn.spawn(nsid, position);
16
+ if (!obj) {
17
+ throw new Error(`spawnOrThrow failed for "${nsid}"`);
18
+ }
19
+ return obj;
20
+ }
14
21
  static inject(dict) {
15
22
  for (const [k, v] of Object.entries(dict)) {
16
23
  Spawn._nsidToTemplateId[k] = v;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ttpg-darrell",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "TTPG TypeScript library",
5
5
  "main": "./build/cjs/index.js",
6
6
  "module": "./build/esm/index.js",