Skip to content

Commit

Permalink
Merge pull request #942 from UFOMelkor/hotbar
Browse files Browse the repository at this point in the history
Update Hotbar
  • Loading branch information
ghost91- authored Jul 12, 2021
2 parents 8b95c6a + 008a8b0 commit af3d928
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 148 deletions.
297 changes: 150 additions & 147 deletions src/foundry/foundry.js/applications/hotbar.d.ts
Original file line number Diff line number Diff line change
@@ -1,152 +1,155 @@
/**
* The global action bar displayed at the bottom of the game view.
* The Hotbar is a UI element at the bottom of the screen which contains Macros as interactive buttons.
* The Hotbar supports 5 pages of global macros which can be dragged and dropped to organize as you wish.
*
* Left clicking a Macro button triggers its effect.
* Right clicking the button displays a context menu of Macro options.
* The number keys 1 through 0 activate numbered hotbar slots.
* Pressing the delete key while hovering over a Macro will remove it from the bar.
*
* @see {@link Macros}
* @see {@link Macro}
*/

declare class Hotbar extends Application {
/**
* The currently viewed macro page
* @defaultValue `1`
*/
page: number;

/**
* The currently displayed set of macros
* @defaultValue `[]`
*/
macros: Macro[];

/**
* Track collapsed state
* @defaultValue `false`
*/
protected _collapsed: boolean;

/**
* Track which hotbar slot is the current hover target, if any
* @defaultValue `null`
*/
protected _hover: number | null;

/**
* @override
* @defaultValue
* ```typescript
* mergeObject(super.defaultOptions, {
* id: "hotbar",
* template: "templates/hud/hotbar.html",
* popOut: false,
* dragDrop: [{ dragSelector: ".macro-icon", dropSelector: "#macro-list" }]
* })
* ```
*/
static get defaultOptions(): typeof Application['defaultOptions'];

/** @override */
getData(options?: Application.RenderOptions): Hotbar.Data | Promise<Hotbar.Data>;

/**
* Get the Array of Macro (or null) values that should be displayed on a numbered page of the bar
* @param page -
*/
protected _getMacrosByPage(page: number): Macro[];

/**
* Collapse the Hotbar, minimizing its display.
* @returns A promise which resolves once the collapse animation completes
*/
collapse(): Promise<boolean>;

/**
* Expand the Hotbar, displaying it normally.
* @returns A promise which resolves once the expand animation completes
*/
expand(): Promise<boolean> | boolean;

/**
* Change to a specific numbered page from 1 to 5
* @param page - The page number to change to.
* (default: `1` )
*/
changePage(page?: number): void;

/**
* Change the page of the hotbar by cycling up (positive) or down (negative)
* @param direction - The direction to cycle
*/
cyclePage(direction: number): void;

/** @override */
activateListeners(html: JQuery): void;

/**
* Create a Context Menu attached to each Macro button
* @param html -
*/
protected _contextMenu(html: JQuery): void;

/**
* Handle left-click events to
* @param event -
*/
protected _onClickMacro(event: JQuery.ClickEvent): Promise<void>;

/**
* Handle hover events on a macro button to track which slot is the hover target
* @param event - The originating mouseover or mouseleave event
*/
protected _onHoverMacro(event: JQuery.MouseEnterEvent | JQuery.MouseLeaveEvent): void;

/**
* Handle pagination controls
* @param event - The originating click event
*/
protected _onClickPageControl(event: JQuery.ClickEvent): void;

/** @override */
protected _canDragStart(selector: string | null): boolean;

/** @override */
protected _canDragDrop(selector: string | null): boolean;

/** @override */
protected _onDrop(event: DragEvent): void;

/**
* Get the Macro entity being dropped in the Hotbar. If the data comes from a non-World source, create the Macro
* @param data - The data transfer attached to the DragEvent
* @returns A Promise which returns the dropped Macro, or null
*/
protected _getDropMacro(data: Hotbar.DropData): Promise<Macro | null>;

/**
* Handle click events to toggle display of the macro bar
* @param event -
*/
protected _onToggleBar(event: JQuery.ClickEvent): void;
}

declare namespace Hotbar {
interface Data {
import { ConfiguredDocumentClass } from '../../../types/helperTypes';

declare global {
/**
* The global action bar displayed at the bottom of the game view.
* The Hotbar is a UI element at the bottom of the screen which contains Macros as interactive buttons.
* The Hotbar supports 5 pages of global macros which can be dragged and dropped to organize as you wish.
*
* Left clicking a Macro button triggers its effect.
* Right clicking the button displays a context menu of Macro options.
* The number keys 1 through 0 activate numbered hotbar slots.
* Pressing the delete key while hovering over a Macro will remove it from the bar.
*
* @see {@link Macros}
* @see {@link Macro}
*
* @typeParam Options - the type of the options object
*/
class Hotbar<Options extends Application.Options = Application.Options> extends Application<Options> {
constructor(options?: Partial<Options>);

/**
* The currently viewed macro page
* @defaultValue `1`
*/
page: number;
macros: Macro[];
barClass: 'collapsed' | '';

/**
* The currently displayed set of macros
* @defaultValue `[]`
*/
macros: InstanceType<ConfiguredDocumentClass<typeof Macro>>[];

/**
* Track collapsed state
* @defaultValue `false`
*/
protected _collapsed: boolean;

/**
* Track which hotbar slot is the current hover target, if any
* @defaultValue `null`
*/
protected _hover: number | null;

/**
* @override
* @defaultValue
* ```typescript
* mergeObject(super.defaultOptions, {
* id: "hotbar",
* template: "templates/hud/hotbar.html",
* popOut: false,
* dragDrop: [{ dragSelector: ".macro-icon", dropSelector: "#macro-list" }]
* })
* ```
*/
static get defaultOptions(): Application.Options;

/** @override */
getData(options?: Application.RenderOptions): Hotbar.Data | Promise<Hotbar.Data>;

/**
* Get the Array of Macro (or null) values that should be displayed on a numbered page of the bar
* @param page -
*/
protected _getMacrosByPage(page: number): InstanceType<ConfiguredDocumentClass<typeof Macro>>[];

/**
* Collapse the Hotbar, minimizing its display.
* @returns A promise which resolves once the collapse animation completes
*/
collapse(): Promise<boolean>;

/**
* Expand the Hotbar, displaying it normally.
* @returns A promise which resolves once the expand animation completes
*/
expand(): Promise<boolean>;

/**
* Change to a specific numbered page from 1 to 5
* @param page - The page number to change to.
* (default: `1` )
*/
changePage(page?: number): void;

/**
* Change the page of the hotbar by cycling up (positive) or down (negative)
* @param direction - The direction to cycle
*/
cyclePage(direction?: number): void;

/** @override */
activateListeners(html: JQuery): void;

/**
* Create a Context Menu attached to each Macro button
* @param html - The HTML being rendered for the hotbar
*/
protected _contextMenu(html: JQuery): void;

/**
* Handle left-click events to
* @param event - The originating click event
*/
protected _onClickMacro(event: JQuery.ClickEvent): Promise<void>;

/**
* Handle hover events on a macro button to track which slot is the hover target
* @param event - The originating mouseover or mouseleave event
*/
protected _onHoverMacro(event: JQuery.MouseEnterEvent | JQuery.MouseLeaveEvent): void;

/**
* Handle pagination controls
* @param event - The originating click event
*/
protected _onClickPageControl(event: JQuery.ClickEvent): void;

/**
* @override
* @param selector - (unused)
*/
protected _canDragStart(selector: string): boolean;

/** @override */
protected _onDragStart(event: DragEvent): false | void;

/**
* @override
* @param selector - (unused)
*/
protected _canDragDrop(selector: string): boolean;

/** @override */
protected _onDrop(
event: DragEvent
): void | ReturnType<InstanceType<ConfiguredDocumentClass<typeof User>>['assignHotbarMacro']>;

/**
* Handle click events to toggle display of the macro bar
* @param event -
*/
protected _onToggleBar(event: JQuery.ClickEvent): void;
}

interface DropData {
type?: string;
data?: DeepPartial<foundry.data.MacroData>; // TODO: Might be incorrect, revisit this!!
id?: string;
pack?: string;
slot?: number;
namespace Hotbar {
interface Data {
page: number;
macros: Macro[];
barClass: 'collapsed' | '';
}
}
}
7 changes: 6 additions & 1 deletion src/foundry/foundry.js/hooks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '../../types/helperTypes.js';
import { DocumentModificationOptions } from '../common/abstract/document.mjs.js';
import { EffectChangeData } from '../common/data/data.mjs/effectChangeData.js';
import { DropData } from './clientDocumentMixin';

declare global {
/**
Expand Down Expand Up @@ -333,7 +334,11 @@ declare global {
* @remarks An explicit return value of `false` prevents the Document being created.
* @see {@link Hotbar#_onDrop}
*/
hotbarDrop: (hotbar: Hotbar, data: Hotbar.DropData, slot: number) => boolean | void;
hotbarDrop: (
hotbar: Hotbar,
data: DropData<InstanceType<ConfiguredDocumentClass<typeof Macro>>>,
slot: number
) => boolean | void;

/**
* @remarks This is called before the {@link Game} is initialized for the current window location.
Expand Down
17 changes: 17 additions & 0 deletions test-d/foundry/foundry.js/applications/hotbar.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expectType } from 'tsd';

const hotbar = new Hotbar();
expectType<number>(hotbar.page);
expectType<Macro[]>(hotbar.macros);
expectType<
| { page: number; macros: Macro[]; barClass: 'collapsed' | '' }
| Promise<{ page: number; macros: Macro[]; barClass: 'collapsed' | '' }>
>(hotbar.getData());

expectType<Promise<boolean>>(hotbar.collapse());
expectType<Promise<boolean>>(hotbar.expand());
expectType<void>(hotbar.changePage());
expectType<void>(hotbar.changePage(5));
expectType<void>(hotbar.cyclePage());
expectType<void>(hotbar.cyclePage(1));
expectType<void>(hotbar.cyclePage(-1));

0 comments on commit af3d928

Please sign in to comment.