Skip to content

Commit

Permalink
fix: launching encounter from builder works again
Browse files Browse the repository at this point in the history
fix: encounter builder name is editable
fix: can now open combatant view for creatures in encounter builder
fix: new encounter builder ribbon icon
fix: can disable encounter builder ribbon icon
  • Loading branch information
valentine195 committed May 2, 2023
1 parent 5efa71e commit d6d2bee
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 179 deletions.
10 changes: 10 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export interface InitiativeTrackerData {
status: boolean;
plugin: boolean;
player: boolean;
builder: boolean;
};
players: HomebrewCreature[];
parties: Party[];
Expand Down Expand Up @@ -122,6 +123,13 @@ export interface InitiativeTrackerData {
integrateSRD: boolean;
diplayPlayerHPValues: boolean;
rollHP: boolean;
builder: BuilderState;
}

interface BuilderState {
sidebarIcon: boolean;
showXP: boolean;
showParty: boolean;
}

export interface InitiativeViewState {
Expand Down Expand Up @@ -175,6 +183,8 @@ export interface SRDMonster {
reactions?: Trait[];
monster?: string;
source?: string | string[];
friendly?: boolean;
hidden?: boolean;
}

export interface HomebrewCreature {
Expand Down
4 changes: 2 additions & 2 deletions src/builder/view/Builder.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Creatures from "./creatures/Creatures.svelte";
import Encounter from "./encounter/Encounter.svelte";
import Party from "./party/Party.svelte";
import PartyExperience from "./party/PartyExperience.svelte";
export let plugin: InitiativeTracker;
Expand All @@ -17,7 +17,7 @@
</script>

<div class="initiative-tracker encounter-builder markdown-rendered">
<Party />
<PartyExperience />
<Encounter />
<Creatures />
</div>
Expand Down
20 changes: 7 additions & 13 deletions src/builder/view/creatures/Creature.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { getContext } from "svelte";
import { encounter } from "../../stores/encounter";
import Nullable from "../Nullable.svelte";
import { Creature as CreatureCreator } from "src/utils/creature";
const plugin = getContext("plugin");
export let creature: SRDMonster;
Expand Down Expand Up @@ -51,17 +52,6 @@
if (!Array.isArray(source)) return "";
return stringify(source, 0, ", ", false);
}
function show(node: HTMLElement) {
if (plugin.canUseStatBlocks && plugin.statblockVersion?.major >= 2) {
const statblockNode = createDiv();
const statblock = plugin.statblocks.render(creature, statblockNode);
if (statblock) {
const popover = new HoverPopover({ hoverPopover: null }, node);
popover.hoverEl.appendChild(statblockNode);
}
}
}
</script>

<tr class="creature">
Expand All @@ -70,8 +60,9 @@
<div use:add on:click={() => encounter.add(creature)} />
<!-- svelte-ignore a11y-mouse-events-have-key-events -->
<div
class="setting-item-name"
on:mouseover={(evt) => show(evt.currentTarget)}
class="setting-item-name creature-name"
on:click={(evt) =>
plugin.openCombatant(CreatureCreator.from(creature))}
>
{creature.name}
</div>
Expand Down Expand Up @@ -112,6 +103,9 @@
align-items: center;
grid-template-columns: auto 1fr;
}
.creature-name {
cursor: pointer;
}
.setting-item-description {
grid-area: desc;
}
Expand Down
11 changes: 10 additions & 1 deletion src/builder/view/encounter/Creature.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import { convertFraction, DEFAULT_UNDEFINED, XP_PER_CR } from "src/utils";
import { encounter } from "../../stores/encounter";
import Nullable from "../Nullable.svelte";
import { getContext } from "svelte";
import { Creature as CreatureCreator } from "src/utils/creature";
const { players } = encounter;
const { average } = players;
const plugin = getContext("plugin");
const remove = (node: HTMLElement) => {
new ExtraButtonComponent(node).setIcon("minus-circle");
};
Expand All @@ -18,6 +21,8 @@
new ExtraButtonComponent(node).setIcon("trash-2");
};
export let count: number;
export let creature: SRDMonster;
const convertedCR = (cr: string | number) => {
Expand All @@ -40,6 +45,10 @@
const challenge = convertFraction(creature.cr) > $average + 3;
const skull = (node: HTMLElement) => setIcon(node, "skull");
const open = () => {
plugin.openCombatant(CreatureCreator.from(creature));
};
</script>

<div class="encounter-creature-container">
Expand All @@ -55,7 +64,7 @@
<div use:add on:click={() => encounter.add(creature)} />
</div>
<div class="encounter-creature">
<strong class="encounter-creature-name">
<strong class="encounter-creature-name" on:click={open}>
{creature.name}
</strong>
{#if insignificant}
Expand Down
195 changes: 124 additions & 71 deletions src/builder/view/encounter/Encounter.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,75 @@
import { encounter } from "../../stores/encounter";
import { START_ENCOUNTER } from "src/utils";
import { getContext } from "svelte";
import { tracker } from "src/tracker/stores/tracker";
import type { CreatureState } from "index";
import { writable } from "svelte/store";
const { players } = encounter;
const plugin = getContext("plugin");
let name = writable("Encounter");
let tempName = writable("");
let updatingName = writable(false);
const setName = () => {
$updatingName = false;
$name = $tempName.length ? $tempName : $name;
$tempName = "";
};
const cancelName = () => {
$updatingName = false;
$tempName = "";
};
let rollHP = plugin.data.rollHP;
$: items = [...$encounter.entries()];
let startIcon: ExtraButtonComponent;
const start = (node: HTMLElement) => {
startIcon = new ExtraButtonComponent(node)
.setIcon(START_ENCOUNTER)
.onClick(() => {
plugin.view.newEncounter({
creatures: [...$encounter.entries()]
.map((c) =>
[...Array(c[1]).keys()].map(() =>
CreatureCreator.from(c[0])
)
.onClick(async () => {
if (!plugin.view) {
await plugin.addTrackerView();
}
const view = plugin.view;
const creatures: CreatureCreator[] = [];
const transformedCreatures: CreatureState[] = [];
for (const [srd, count] of items) {
const creature = CreatureCreator.from(srd);
const amount = Math.max(
isNaN(Number(count)) ? 1 : Number(count),
1
);
creatures.push(
...[...Array(amount).keys()].map((v) =>
CreatureCreator.new(creature)
)
.flat(),
players: [...$players].map((p) => p.name)
);
}
for (const creature of [
...$players.map(
(p) =>
plugin.getPlayerByName(p.name) ??
CreatureCreator.from(p)
),
...creatures
]) {
transformedCreatures.push(creature.toJSON());
}
tracker.new(plugin, {
creatures: transformedCreatures,
name: $name,
round: 1,
state: false,
logFile: null,
roll: true,
rollHP
});
app.workspace.revealLeaf(plugin.view.leaf);
plugin.app.workspace.revealLeaf(view.leaf);
});
};
Expand All @@ -44,67 +90,55 @@
}
}
}
let saveIcon: ExtraButtonComponent;
const save = (node: HTMLElement) => {
saveIcon = new ExtraButtonComponent(node)
.setIcon("save")
.onClick(() => {
const modal = new Modal(app);
modal.contentEl.createEl("h4", { text: "Save Encounter" });
let name: string = `Encounter ${
Object.keys(plugin.data.encounters).length
}`;
new Setting(modal.contentEl)
.setName("Encounter Name")
.addText((t) => {
t.setPlaceholder(name).onChange((v) => (name = v));
});
new Setting(modal.contentEl).addButton((b) =>
b.setButtonText("Save").onClick(() => {
if (name in plugin.data.encounters) {
new Notice(
"An encounter by that name already exists."
);
return;
}
const creatures = [
...[...$players].map((p) =>
CreatureCreator.from(p)
),
...[...$encounter.entries()]
.map((c) =>
[...Array(c[1]).keys()].map(() =>
CreatureCreator.from(c[0])
)
)
.flat()
];
plugin.data.encounters[name] = {
creatures: [...creatures.map((c) => c.toJSON())],
state: false,
name,
round: 1,
logFile: null
};
modal.close();
})
);
modal.open();
});
const editIcon = (node: HTMLElement) => {
new ExtraButtonComponent(node).setIcon("pencil");
};
$: {
if (saveIcon) {
if (!items.length) {
saveIcon.setDisabled(true);
saveIcon.setTooltip("");
} else {
saveIcon.setDisabled(false);
saveIcon.setTooltip("Save Encounter");
}
}
}
const saveIcon = (node: HTMLElement) => {
new ExtraButtonComponent(node).setIcon("save");
};
const save = () => {
const modal = new Modal(app);
modal.contentEl.createEl("h4", { text: "Save Encounter" });
let encName: string =
$name != "Encounter"
? $name
: `Encounter ${Object.keys(plugin.data.encounters).length}`;
new Setting(modal.contentEl).setName("Encounter Name").addText((t) => {
t.setPlaceholder(encName).onChange((v) => (encName = v));
});
new Setting(modal.contentEl).addButton((b) =>
b.setButtonText("Save").onClick(() => {
if (encName in plugin.data.encounters) {
new Notice("An encounter by that name already exists.");
return;
}
const creatures = [
...[...$players].map((p) => CreatureCreator.from(p)),
...[...$encounter.entries()]
.map((c) =>
[...Array(c[1]).keys()].map(() =>
CreatureCreator.from(c[0])
)
)
.flat()
];
plugin.data.encounters[encName] = {
creatures: [...creatures.map((c) => c.toJSON())],
state: false,
name: encName,
round: 1,
logFile: null
};
modal.close();
})
);
modal.open();
};
const cancelIcon = (node: HTMLElement) => {
new ExtraButtonComponent(node).setIcon("cross-in-box");
};
let exportIcon: ExtraButtonComponent;
const exp = (node: HTMLElement) => {
exportIcon = new ExtraButtonComponent(node).setIcon("code");
Expand Down Expand Up @@ -135,14 +169,29 @@
</script>

<div class="encounter-header">
<h5 class="built-encounter">Encounter</h5>
<div class="encounter-name">
{#if $updatingName}
<input type="text" bind:value={$tempName} placeholder={$name} />
<div use:saveIcon on:click={setName} />
<div use:cancelIcon on:click={cancelName} />
{:else}
<h5 class="built-encounter">{$name}</h5>
<div use:editIcon on:click={() => ($updatingName = true)} />
{/if}
</div>
<div class="encounter-controls">
<div use:start />
<div use:save />
<div use:saveIcon on:click={save} />
<!-- <div use:exp /> -->
<!-- <div use:load /> -->

<div use:clear on:click={() => encounter.empty()} />
<div
use:clear
on:click={() => {
encounter.empty();
$name = "Encounter";
}}
/>
</div>
</div>
{#if !items.length}
Expand All @@ -156,6 +205,10 @@
{/if}

<style scoped>
.encounter-name {
display: flex;
align-items: center;
}
.encounter-header {
display: flex;
align-items: center;
Expand Down
Loading

0 comments on commit d6d2bee

Please sign in to comment.