Skip to content

Commit

Permalink
add way to draw spell consumable
Browse files Browse the repository at this point in the history
  • Loading branch information
reonZ committed Jun 23, 2024
1 parent 7d18760 commit 070e361
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 21 deletions.
4 changes: 4 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@
"confirm": {
"title": "Use - {name}",
"message": "Are you sure you want to use <strong>{name}</strong>?"
},
"draw": {
"title": "{type} - {name}",
"message": "Are you sure you want to {type} <strong>{name}</strong>?"
}
}
}
Expand Down
73 changes: 66 additions & 7 deletions src/hud/persistent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import {
addListener,
addListenerAll,
arrayIncludes,
changeCarryType,
confirmDialog,
consumeItem,
createHTMLElement,
createHook,
elementDataset,
getActionAnnotation,
getActionImg,
getActiveModule,
getFlag,
Expand Down Expand Up @@ -58,6 +60,7 @@ import {
variantLabel,
} from "./sidebar/actions";
import { SidebarMenu, SidebarSettings, getSidebars } from "./sidebar/base";
import { getAnnotationTooltip } from "./sidebar/spells";

const ROMAN_RANKS = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"] as const;

Expand Down Expand Up @@ -577,13 +580,26 @@ class PF2eHudPersistent extends makeAdvancedHUD(
isCantrip || isConsumable
? false
: isStaff
? canCastRank
? !canCastRank
: uses
? isCharges
? uses.value < castRank
: uses.value === 0
: !!active?.expended;

const notCarried = isConsumable
? spell.parentItem?.carryType !== "held"
: isStaff
? (entry as dailies.StaffSpellcasting).staff.carryType !== "held"
: false;

const parentItem = isStaff
? dailiesModule?.api.getStaffItem(actor as CharacterPF2e)
: spell.parentItem;

const annotation =
notCarried && parentItem ? getActionAnnotation(parentItem) : undefined;

return {
...shortcut,
isDisabled: expended || isBroken,
Expand All @@ -593,13 +609,17 @@ class PF2eHudPersistent extends makeAdvancedHUD(
categoryIcon,
collection,
item: spell,
name,
name: annotation ? `${getAnnotationTooltip(annotation)} - ${name}` : name,
uses,
entryLabel,
isBroken,
castRank: castRank,
isPrepared: isPrepared && !isFlexible && !isCantrip,
cost: getCost(spell.system.time.value),
notCarried,
isStaff,
parentItem,
annotation,
} satisfies SpellShortcut as T;
}

Expand Down Expand Up @@ -978,7 +998,7 @@ class PF2eHudPersistent extends makeAdvancedHUD(

const actor = this.actor!;

function confimUse(name: string) {
function confirmUse(name: string) {
return confirmDialog({
title: localize("persistent.main.shortcut.confirm.title", {
name,
Expand All @@ -1000,7 +1020,7 @@ class PF2eHudPersistent extends makeAdvancedHUD(
return item.toMessage(event);
}

if (setting === "confirm" && !(await confimUse(item.name))) return;
if (setting === "confirm" && !(await confirmUse(item.name))) return;

return consumeItem(event, item);
}
Expand All @@ -1014,16 +1034,51 @@ class PF2eHudPersistent extends makeAdvancedHUD(
const item = shortcut.item;
if (!item) return;

if (this.getSetting("confirmShortcut") && !(await confimUse(item.name))) return;
if (this.getSetting("confirmShortcut") && !(await confirmUse(item.name))) return;

return useAction(item);
}

case "spell": {
const { castRank: rank, slotId, collection, item: spell } = shortcut;
const {
castRank: rank,
slotId,
collection,
item: spell,
notCarried,
annotation,
parentItem,
} = shortcut;

if (!spell) return;

if (this.getSetting("confirmShortcut") && !(await confimUse(spell.name))) return;
const setting = this.getSetting("confirmShortcut");

if (notCarried) {
if (!annotation || !parentItem) return;

if (setting) {
const type = localize("sidebars.spells.action", annotation);
const name = parentItem.name;

const confirm = await confirmDialog({
title: localize("persistent.main.shortcut.draw.title", {
type,
name,
}),
content: localize("persistent.main.shortcut.draw.message", {
type,
name,
}),
});

if (!confirm) return;
}

return changeCarryType(actor, parentItem, 1, annotation);
}

if (setting && !(await confirmUse(spell.name))) return;

return (
spell.parentItem?.consume() ?? collection.entry.cast(spell, { rank, slotId })
Expand Down Expand Up @@ -1418,6 +1473,10 @@ type SpellShortcut = BaseShortCut<"spell"> &
isPrepared: boolean;
cost: CostValue;
castRank: OneToTen;
notCarried: boolean;
isStaff: boolean;
parentItem: Maybe<ConsumablePF2e<CreaturePF2e> | PhysicalItemPF2e<CreaturePF2e>>;
annotation: AuxiliaryActionPurpose;
};

type CostValue = number | "extra" | undefined;
Expand Down
16 changes: 8 additions & 8 deletions src/hud/sidebar/spells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import {
} from "foundry-pf2e";
import { PF2eHudSidebar, SidebarContext, SidebarName, SidebarRenderOptions } from "./base";

function getAnnotationTooltip(annotation: NonNullable<AuxiliaryActionPurpose>) {
const label = localize("sidebars.spells.action", annotation);
const icon = `<span class='action-glyph'>${annotation === "retrieve" ? 2 : 1}</span>`;
return `${label} ${icon}`;
}

class PF2eHudSidebarSpells extends PF2eHudSidebar {
get key(): SidebarName {
return "spells";
Expand All @@ -28,13 +34,7 @@ class PF2eHudSidebarSpells extends PF2eHudSidebar {
const data: SpellsContext = {
...parentData,
...summarizedData,
annotationTooltip: (annotation: NonNullable<AuxiliaryActionPurpose>) => {
const label = localize("sidebars.spells.action", annotation);
const icon = `<span class='action-glyph'>${
annotation === "retrieve" ? 2 : 1
}</span>`;
return `${label} ${icon}`;
},
annotationTooltip: getAnnotationTooltip,
};

return data;
Expand Down Expand Up @@ -149,4 +149,4 @@ type SpellsContext = SidebarContext &
annotationTooltip: (annotation: NonNullable<AuxiliaryActionPurpose>) => string;
};

export { PF2eHudSidebarSpells };
export { PF2eHudSidebarSpells, getAnnotationTooltip };
12 changes: 10 additions & 2 deletions styles/_persistent.scss
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ $bottom-width: 542px;
display: flex;
}

.broken {
.broken,
.equip {
inset: 0;
font-size: 1.2em;

i {
position: absolute;
Expand All @@ -274,6 +274,14 @@ $bottom-width: 542px;
}
}

.broken {
font-size: 1.2em;
}

.equip {
font-size: 1.5em;
}

.quantity {
right: var(--offset);
bottom: var(--offset);
Expand Down
2 changes: 1 addition & 1 deletion styles/pf2e-hud.css

Large diffs are not rendered by default.

Loading

0 comments on commit 070e361

Please sign in to comment.