ttpg-darrell 1.0.17 → 1.0.18

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.
@@ -9,4 +9,5 @@ export * from "./setup/layout/layout-objects";
9
9
  export * from "./setup/spawn/spawn";
10
10
  export * from "./shuffle/shuffle";
11
11
  export * from "./triggerable-multicast-delegate/triggerable-multicast-delegate";
12
- export * from "./ui/player-name-take-seat";
12
+ export * from "./ui/object/player-name-take-seat/player-name-take-seat";
13
+ export * from "./ui/widget/d6widget/d6widget";
@@ -25,4 +25,5 @@ __exportStar(require("./setup/layout/layout-objects"), exports);
25
25
  __exportStar(require("./setup/spawn/spawn"), exports);
26
26
  __exportStar(require("./shuffle/shuffle"), exports);
27
27
  __exportStar(require("./triggerable-multicast-delegate/triggerable-multicast-delegate"), exports);
28
- __exportStar(require("./ui/player-name-take-seat"), exports);
28
+ __exportStar(require("./ui/object/player-name-take-seat/player-name-take-seat"), exports);
29
+ __exportStar(require("./ui/widget/d6widget/d6widget"), exports);
@@ -12,6 +12,10 @@ class Spawn {
12
12
  return undefined;
13
13
  }
14
14
  const obj = api_1.world.createObjectFromTemplate(templateId, position);
15
+ if (obj) {
16
+ const name = api_1.world.getTemplateName(templateId);
17
+ obj.setName(name);
18
+ }
15
19
  return obj;
16
20
  }
17
21
  static spawnOrThrow(nsid, position) {
@@ -0,0 +1,19 @@
1
+ import { Color, GameObject } from "@tabletop-playground/api";
2
+ /**
3
+ * Display player name above-and-behind the card holder.
4
+ * Show a "take seat" button when no player in slot.
5
+ */
6
+ export declare class PlayerNameTakeSeat {
7
+ static readonly DEFAULT_FONT_SIZE = 30;
8
+ private readonly _gameObject;
9
+ private readonly _nameText;
10
+ private readonly _nameBorder;
11
+ private readonly _takeSeatButton;
12
+ private readonly _widgetSwitcher;
13
+ private readonly _ui;
14
+ constructor(gameObject: GameObject);
15
+ setColor(color: Color): this;
16
+ setFont(fontName: string, fontPackageId?: string): this;
17
+ setFontSize(fontSize: number): this;
18
+ private _updatePlayerStatus;
19
+ }
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PlayerNameTakeSeat = void 0;
4
+ const api_1 = require("@tabletop-playground/api");
5
+ /**
6
+ * Display player name above-and-behind the card holder.
7
+ * Show a "take seat" button when no player in slot.
8
+ */
9
+ class PlayerNameTakeSeat {
10
+ constructor(gameObject) {
11
+ if (!gameObject) {
12
+ throw new Error("missing gameObject");
13
+ }
14
+ this._gameObject = gameObject;
15
+ this._nameText = new api_1.Text()
16
+ .setBold(true)
17
+ .setJustification(api_1.TextJustification.Center)
18
+ .setText(" Player Name ");
19
+ this._nameBorder = new api_1.Border()
20
+ .setColor([0, 0, 0, 0.75])
21
+ .setChild(this._nameText);
22
+ this._takeSeatButton = new api_1.Button().setBold(true).setText(" TAKE SEAT ");
23
+ this._widgetSwitcher = new api_1.WidgetSwitcher()
24
+ .addChild(this._takeSeatButton)
25
+ .addChild(this._nameBorder);
26
+ this._ui = new api_1.UIElement();
27
+ this._ui.presentationStyle = api_1.UIPresentationStyle.ViewAligned;
28
+ this._ui.useTransparency = true;
29
+ this._ui.useWidgetSize = true;
30
+ this._ui.widget = this._widgetSwitcher;
31
+ this._takeSeatButton.onClicked.add((button, player) => {
32
+ const thisSlot = this._gameObject.getOwningPlayerSlot();
33
+ if (thisSlot < 0) {
34
+ throw new Error("invalid player slot");
35
+ }
36
+ player.switchSlot(thisSlot);
37
+ // Make sure attached card holder follows.
38
+ const delayedResetCardHolder = () => {
39
+ if (this._gameObject instanceof api_1.CardHolder) {
40
+ player.setHandHolder(this._gameObject);
41
+ }
42
+ };
43
+ process.nextTick(delayedResetCardHolder);
44
+ });
45
+ // Listen for events (delay processing by a frame for "final" state).
46
+ const eventHandler = () => {
47
+ process.nextTick(() => {
48
+ this._updatePlayerStatus();
49
+ });
50
+ };
51
+ api_1.globalEvents.onPlayerJoined.add(eventHandler);
52
+ api_1.globalEvents.onPlayerLeft.add(eventHandler);
53
+ api_1.globalEvents.onPlayerSwitchedSlots.add(eventHandler);
54
+ this._gameObject.onDestroyed.add(() => {
55
+ api_1.globalEvents.onPlayerJoined.remove(eventHandler);
56
+ api_1.globalEvents.onPlayerLeft.remove(eventHandler);
57
+ api_1.globalEvents.onPlayerSwitchedSlots.remove(eventHandler);
58
+ });
59
+ this._gameObject.addUI(this._ui);
60
+ this.setColor(gameObject.getPrimaryColor()).setFontSize(PlayerNameTakeSeat.DEFAULT_FONT_SIZE);
61
+ this._updatePlayerStatus();
62
+ }
63
+ setColor(color) {
64
+ this._nameText.setTextColor(color);
65
+ this._takeSeatButton.setTextColor(color);
66
+ return this;
67
+ }
68
+ setFont(fontName, fontPackageId) {
69
+ this._nameText.setFont(fontName, fontPackageId);
70
+ this._takeSeatButton.setFont(fontName, fontPackageId);
71
+ return this;
72
+ }
73
+ setFontSize(fontSize) {
74
+ this._nameText.setFontSize(fontSize);
75
+ this._takeSeatButton.setFontSize(fontSize);
76
+ // UI position.
77
+ const x = (this._gameObject.getPosition().x > 0 ? 1 : -1) * 15;
78
+ const z = fontSize / 5;
79
+ this._ui.position = new api_1.Vector(x, 0, z);
80
+ this._gameObject.updateUI(this._ui);
81
+ return this;
82
+ }
83
+ _updatePlayerStatus() {
84
+ // Calculate widget.
85
+ let widget = this._takeSeatButton;
86
+ // If seated player, show name.
87
+ const playerSlot = this._gameObject.getOwningPlayerSlot();
88
+ for (const player of api_1.world.getAllPlayers()) {
89
+ if (player.getSlot() === playerSlot) {
90
+ this._nameText.setText(` ${player.getName()} `);
91
+ widget = this._nameBorder;
92
+ break;
93
+ }
94
+ }
95
+ if (this._widgetSwitcher.getActiveWidget() !== widget) {
96
+ this._widgetSwitcher.setActiveWidget(widget);
97
+ this._gameObject.updateUI(this._ui);
98
+ }
99
+ }
100
+ }
101
+ exports.PlayerNameTakeSeat = PlayerNameTakeSeat;
102
+ PlayerNameTakeSeat.DEFAULT_FONT_SIZE = 30;
@@ -0,0 +1,48 @@
1
+ import { Widget } from "@tabletop-playground/api";
2
+ /**
3
+ * Show a single D6 face as a square widget.
4
+ *
5
+ * Do not extend a widget class, the class shell can be lost when retrieving
6
+ * via getChild, etc. Use an explicit getWidget method for the widget.
7
+ */
8
+ export declare class D6Widget {
9
+ private readonly _imageWidget;
10
+ private readonly _canvas;
11
+ private readonly _layoutBox;
12
+ /**
13
+ * Constructor.
14
+ */
15
+ constructor();
16
+ /**
17
+ * Set the widget / single-face image size.
18
+ *
19
+ * @param size
20
+ * @returns self, for chaining
21
+ */
22
+ setSize(size: number): this;
23
+ /**
24
+ * Set the 3x3 dice face sheet:
25
+ *
26
+ * [ - 1 - ]
27
+ * [ 2 3 6 ]
28
+ * [ 5 4 - ]
29
+ *
30
+ * @param textureName
31
+ * @param texturePackageId
32
+ * @returns self, for chaining
33
+ */
34
+ setDiceImage(textureName: string, texturePackageId?: string): this;
35
+ /**
36
+ * Set which face is visible in the widget.
37
+ *
38
+ * @param index
39
+ * @returns self, for chaining
40
+ */
41
+ setFace(index: number): this;
42
+ /**
43
+ * Get a widget suitable for UI.
44
+ *
45
+ * @returns Widget
46
+ */
47
+ getWidget(): Widget;
48
+ }
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.D6Widget = void 0;
4
+ const api_1 = require("@tabletop-playground/api");
5
+ /**
6
+ * Show a single D6 face as a square widget.
7
+ *
8
+ * Do not extend a widget class, the class shell can be lost when retrieving
9
+ * via getChild, etc. Use an explicit getWidget method for the widget.
10
+ */
11
+ class D6Widget {
12
+ /**
13
+ * Constructor.
14
+ */
15
+ constructor() {
16
+ this._imageWidget = new api_1.ImageWidget();
17
+ this._canvas = new api_1.Canvas().addChild(this._imageWidget, 0, 0, 1, 1);
18
+ this._layoutBox = new api_1.LayoutBox().setChild(this._canvas);
19
+ this.setSize(50);
20
+ this.setFace(0);
21
+ }
22
+ /**
23
+ * Set the widget / single-face image size.
24
+ *
25
+ * @param size
26
+ * @returns self, for chaining
27
+ */
28
+ setSize(size) {
29
+ this._layoutBox.setOverrideHeight(size).setOverrideWidth(size);
30
+ this._imageWidget.setImageSize(size * 3, size * 3);
31
+ return this;
32
+ }
33
+ /**
34
+ * Set the 3x3 dice face sheet:
35
+ *
36
+ * [ - 1 - ]
37
+ * [ 2 3 6 ]
38
+ * [ 5 4 - ]
39
+ *
40
+ * @param textureName
41
+ * @param texturePackageId
42
+ * @returns self, for chaining
43
+ */
44
+ setDiceImage(textureName, texturePackageId) {
45
+ this._imageWidget.setImage(textureName, texturePackageId);
46
+ return this;
47
+ }
48
+ /**
49
+ * Set which face is visible in the widget.
50
+ *
51
+ * @param index
52
+ * @returns self, for chaining
53
+ */
54
+ setFace(index) {
55
+ const colRow = [
56
+ { col: 1, row: 0 }, // 1
57
+ { col: 0, row: 1 }, // 2
58
+ { col: 1, row: 1 }, // 3
59
+ { col: 1, row: 2 }, // 4
60
+ { col: 0, row: 2 }, // 5
61
+ { col: 2, row: 1 }, // 6
62
+ ];
63
+ const { col, row } = colRow[index];
64
+ if (typeof col !== "number" || typeof row !== "number") {
65
+ throw new Error("bad index");
66
+ }
67
+ // Shift the image so the visible portion is col/row of the 3x3.
68
+ const s = this._layoutBox.getOverrideWidth();
69
+ let x = col * -s;
70
+ let y = row * -s;
71
+ this._canvas.updateChild(this._imageWidget, x, y, s * 3, s * 3);
72
+ return this;
73
+ }
74
+ /**
75
+ * Get a widget suitable for UI.
76
+ *
77
+ * @returns Widget
78
+ */
79
+ getWidget() {
80
+ return this._layoutBox;
81
+ }
82
+ }
83
+ exports.D6Widget = D6Widget;
@@ -9,4 +9,5 @@ export * from "./setup/layout/layout-objects";
9
9
  export * from "./setup/spawn/spawn";
10
10
  export * from "./shuffle/shuffle";
11
11
  export * from "./triggerable-multicast-delegate/triggerable-multicast-delegate";
12
- export * from "./ui/player-name-take-seat";
12
+ export * from "./ui/object/player-name-take-seat/player-name-take-seat";
13
+ export * from "./ui/widget/d6widget/d6widget";
@@ -9,4 +9,5 @@ export * from "./setup/layout/layout-objects";
9
9
  export * from "./setup/spawn/spawn";
10
10
  export * from "./shuffle/shuffle";
11
11
  export * from "./triggerable-multicast-delegate/triggerable-multicast-delegate";
12
- export * from "./ui/player-name-take-seat";
12
+ export * from "./ui/object/player-name-take-seat/player-name-take-seat";
13
+ export * from "./ui/widget/d6widget/d6widget";
@@ -9,6 +9,10 @@ export class Spawn {
9
9
  return undefined;
10
10
  }
11
11
  const obj = world.createObjectFromTemplate(templateId, position);
12
+ if (obj) {
13
+ const name = world.getTemplateName(templateId);
14
+ obj.setName(name);
15
+ }
12
16
  return obj;
13
17
  }
14
18
  static spawnOrThrow(nsid, position) {
@@ -0,0 +1,19 @@
1
+ import { Color, GameObject } from "@tabletop-playground/api";
2
+ /**
3
+ * Display player name above-and-behind the card holder.
4
+ * Show a "take seat" button when no player in slot.
5
+ */
6
+ export declare class PlayerNameTakeSeat {
7
+ static readonly DEFAULT_FONT_SIZE = 30;
8
+ private readonly _gameObject;
9
+ private readonly _nameText;
10
+ private readonly _nameBorder;
11
+ private readonly _takeSeatButton;
12
+ private readonly _widgetSwitcher;
13
+ private readonly _ui;
14
+ constructor(gameObject: GameObject);
15
+ setColor(color: Color): this;
16
+ setFont(fontName: string, fontPackageId?: string): this;
17
+ setFontSize(fontSize: number): this;
18
+ private _updatePlayerStatus;
19
+ }
@@ -0,0 +1,98 @@
1
+ import { Border, Button, CardHolder, Text, TextJustification, UIElement, UIPresentationStyle, Vector, WidgetSwitcher, globalEvents, world, } from "@tabletop-playground/api";
2
+ /**
3
+ * Display player name above-and-behind the card holder.
4
+ * Show a "take seat" button when no player in slot.
5
+ */
6
+ export class PlayerNameTakeSeat {
7
+ constructor(gameObject) {
8
+ if (!gameObject) {
9
+ throw new Error("missing gameObject");
10
+ }
11
+ this._gameObject = gameObject;
12
+ this._nameText = new Text()
13
+ .setBold(true)
14
+ .setJustification(TextJustification.Center)
15
+ .setText(" Player Name ");
16
+ this._nameBorder = new Border()
17
+ .setColor([0, 0, 0, 0.75])
18
+ .setChild(this._nameText);
19
+ this._takeSeatButton = new Button().setBold(true).setText(" TAKE SEAT ");
20
+ this._widgetSwitcher = new WidgetSwitcher()
21
+ .addChild(this._takeSeatButton)
22
+ .addChild(this._nameBorder);
23
+ this._ui = new UIElement();
24
+ this._ui.presentationStyle = UIPresentationStyle.ViewAligned;
25
+ this._ui.useTransparency = true;
26
+ this._ui.useWidgetSize = true;
27
+ this._ui.widget = this._widgetSwitcher;
28
+ this._takeSeatButton.onClicked.add((button, player) => {
29
+ const thisSlot = this._gameObject.getOwningPlayerSlot();
30
+ if (thisSlot < 0) {
31
+ throw new Error("invalid player slot");
32
+ }
33
+ player.switchSlot(thisSlot);
34
+ // Make sure attached card holder follows.
35
+ const delayedResetCardHolder = () => {
36
+ if (this._gameObject instanceof CardHolder) {
37
+ player.setHandHolder(this._gameObject);
38
+ }
39
+ };
40
+ process.nextTick(delayedResetCardHolder);
41
+ });
42
+ // Listen for events (delay processing by a frame for "final" state).
43
+ const eventHandler = () => {
44
+ process.nextTick(() => {
45
+ this._updatePlayerStatus();
46
+ });
47
+ };
48
+ globalEvents.onPlayerJoined.add(eventHandler);
49
+ globalEvents.onPlayerLeft.add(eventHandler);
50
+ globalEvents.onPlayerSwitchedSlots.add(eventHandler);
51
+ this._gameObject.onDestroyed.add(() => {
52
+ globalEvents.onPlayerJoined.remove(eventHandler);
53
+ globalEvents.onPlayerLeft.remove(eventHandler);
54
+ globalEvents.onPlayerSwitchedSlots.remove(eventHandler);
55
+ });
56
+ this._gameObject.addUI(this._ui);
57
+ this.setColor(gameObject.getPrimaryColor()).setFontSize(PlayerNameTakeSeat.DEFAULT_FONT_SIZE);
58
+ this._updatePlayerStatus();
59
+ }
60
+ setColor(color) {
61
+ this._nameText.setTextColor(color);
62
+ this._takeSeatButton.setTextColor(color);
63
+ return this;
64
+ }
65
+ setFont(fontName, fontPackageId) {
66
+ this._nameText.setFont(fontName, fontPackageId);
67
+ this._takeSeatButton.setFont(fontName, fontPackageId);
68
+ return this;
69
+ }
70
+ setFontSize(fontSize) {
71
+ this._nameText.setFontSize(fontSize);
72
+ this._takeSeatButton.setFontSize(fontSize);
73
+ // UI position.
74
+ const x = (this._gameObject.getPosition().x > 0 ? 1 : -1) * 15;
75
+ const z = fontSize / 5;
76
+ this._ui.position = new Vector(x, 0, z);
77
+ this._gameObject.updateUI(this._ui);
78
+ return this;
79
+ }
80
+ _updatePlayerStatus() {
81
+ // Calculate widget.
82
+ let widget = this._takeSeatButton;
83
+ // If seated player, show name.
84
+ const playerSlot = this._gameObject.getOwningPlayerSlot();
85
+ for (const player of world.getAllPlayers()) {
86
+ if (player.getSlot() === playerSlot) {
87
+ this._nameText.setText(` ${player.getName()} `);
88
+ widget = this._nameBorder;
89
+ break;
90
+ }
91
+ }
92
+ if (this._widgetSwitcher.getActiveWidget() !== widget) {
93
+ this._widgetSwitcher.setActiveWidget(widget);
94
+ this._gameObject.updateUI(this._ui);
95
+ }
96
+ }
97
+ }
98
+ PlayerNameTakeSeat.DEFAULT_FONT_SIZE = 30;
@@ -0,0 +1,48 @@
1
+ import { Widget } from "@tabletop-playground/api";
2
+ /**
3
+ * Show a single D6 face as a square widget.
4
+ *
5
+ * Do not extend a widget class, the class shell can be lost when retrieving
6
+ * via getChild, etc. Use an explicit getWidget method for the widget.
7
+ */
8
+ export declare class D6Widget {
9
+ private readonly _imageWidget;
10
+ private readonly _canvas;
11
+ private readonly _layoutBox;
12
+ /**
13
+ * Constructor.
14
+ */
15
+ constructor();
16
+ /**
17
+ * Set the widget / single-face image size.
18
+ *
19
+ * @param size
20
+ * @returns self, for chaining
21
+ */
22
+ setSize(size: number): this;
23
+ /**
24
+ * Set the 3x3 dice face sheet:
25
+ *
26
+ * [ - 1 - ]
27
+ * [ 2 3 6 ]
28
+ * [ 5 4 - ]
29
+ *
30
+ * @param textureName
31
+ * @param texturePackageId
32
+ * @returns self, for chaining
33
+ */
34
+ setDiceImage(textureName: string, texturePackageId?: string): this;
35
+ /**
36
+ * Set which face is visible in the widget.
37
+ *
38
+ * @param index
39
+ * @returns self, for chaining
40
+ */
41
+ setFace(index: number): this;
42
+ /**
43
+ * Get a widget suitable for UI.
44
+ *
45
+ * @returns Widget
46
+ */
47
+ getWidget(): Widget;
48
+ }
@@ -0,0 +1,79 @@
1
+ import { Canvas, ImageWidget, LayoutBox, } from "@tabletop-playground/api";
2
+ /**
3
+ * Show a single D6 face as a square widget.
4
+ *
5
+ * Do not extend a widget class, the class shell can be lost when retrieving
6
+ * via getChild, etc. Use an explicit getWidget method for the widget.
7
+ */
8
+ export class D6Widget {
9
+ /**
10
+ * Constructor.
11
+ */
12
+ constructor() {
13
+ this._imageWidget = new ImageWidget();
14
+ this._canvas = new Canvas().addChild(this._imageWidget, 0, 0, 1, 1);
15
+ this._layoutBox = new LayoutBox().setChild(this._canvas);
16
+ this.setSize(50);
17
+ this.setFace(0);
18
+ }
19
+ /**
20
+ * Set the widget / single-face image size.
21
+ *
22
+ * @param size
23
+ * @returns self, for chaining
24
+ */
25
+ setSize(size) {
26
+ this._layoutBox.setOverrideHeight(size).setOverrideWidth(size);
27
+ this._imageWidget.setImageSize(size * 3, size * 3);
28
+ return this;
29
+ }
30
+ /**
31
+ * Set the 3x3 dice face sheet:
32
+ *
33
+ * [ - 1 - ]
34
+ * [ 2 3 6 ]
35
+ * [ 5 4 - ]
36
+ *
37
+ * @param textureName
38
+ * @param texturePackageId
39
+ * @returns self, for chaining
40
+ */
41
+ setDiceImage(textureName, texturePackageId) {
42
+ this._imageWidget.setImage(textureName, texturePackageId);
43
+ return this;
44
+ }
45
+ /**
46
+ * Set which face is visible in the widget.
47
+ *
48
+ * @param index
49
+ * @returns self, for chaining
50
+ */
51
+ setFace(index) {
52
+ const colRow = [
53
+ { col: 1, row: 0 }, // 1
54
+ { col: 0, row: 1 }, // 2
55
+ { col: 1, row: 1 }, // 3
56
+ { col: 1, row: 2 }, // 4
57
+ { col: 0, row: 2 }, // 5
58
+ { col: 2, row: 1 }, // 6
59
+ ];
60
+ const { col, row } = colRow[index];
61
+ if (typeof col !== "number" || typeof row !== "number") {
62
+ throw new Error("bad index");
63
+ }
64
+ // Shift the image so the visible portion is col/row of the 3x3.
65
+ const s = this._layoutBox.getOverrideWidth();
66
+ let x = col * -s;
67
+ let y = row * -s;
68
+ this._canvas.updateChild(this._imageWidget, x, y, s * 3, s * 3);
69
+ return this;
70
+ }
71
+ /**
72
+ * Get a widget suitable for UI.
73
+ *
74
+ * @returns Widget
75
+ */
76
+ getWidget() {
77
+ return this._layoutBox;
78
+ }
79
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ttpg-darrell",
3
- "version": "1.0.17",
3
+ "version": "1.0.18",
4
4
  "description": "TTPG TypeScript library",
5
5
  "main": "./build/cjs/index.js",
6
6
  "module": "./build/esm/index.js",