Skip to content

Context Menu

Jason Stallings edited this page Dec 6, 2022 · 9 revisions

Create Context Sub-Menu

// Create context sub-menu
const mainOption = context.addOption("Main Option")
const subContext = context.getNew(context) as ISContextMenu
context.addSubMenu(mainOption, subContext)

subContext.addOption("Sub Option")

Create Context Item Tooltip

const option = context.addOption("Context item")

const tooltip = new ISToolTip();
tooltip.setName(`Context tooltip name`)
tooltip.description = `Context tooltip description`

option.toolTip = tooltip

Create World Context Menu

import { getSpecificPlayer, ISContextMenu, IsoGridSquare, IsoPlayer, ISTimedActionQueue, sendClientCommand } from "@asledgehammer/pipewrench"
import { onFillInventoryObjectContextMenu, onFillWorldObjectContextMenu } from "@asledgehammer/pipewrench-events"
import { TestAction } from "./TimedActions/TestAction"

function doTestAction(player: IsoPlayer, square: IsoGridSquare) {
    ISTimedActionQueue.add(TestAction(player, square))
}

function doSendPing(player: IsoPlayer) {
    player.Say("Sending command ping!")
    sendClientCommand("TestMod", "ping", {})
}

onFillWorldObjectContextMenu.addListener((playerNum: number, context: ISContextMenu, worldObjects: IsoObject[], test: boolean) => {
    if (test) return

    const player = getSpecificPlayer(playerNum)

    if (player.getVehicle()) return

    context.addDebugOption("Do test action", player, doTestAction, player.getSquare())
    context.addDebugOption("Send ping to server", player, doSendPing)
})

Create Inventory Context Menu

import { getSpecificPlayer, HandWeapon, InventoryItem, ISContextMenu, ISEquipWeaponAction, IsoPlayer, ISTimedActionQueue, _instanceof_ } from "@asledgehammer/pipewrench"
import { onFillInventoryObjectContextMenu } from "@asledgehammer/pipewrench-events"


function doEquipHammer(player: IsoPlayer, hammer: HandWeapon) {
    ISTimedActionQueue.add(new ISEquipWeaponAction(player, hammer, 50, true, false))
}

onFillInventoryObjectContextMenu.addListener((playerNum: number, context: ISContextMenu, items: any[]) => {
    const player = getSpecificPlayer(playerNum)

    const hammers: InventoryItem[] = [];

    // Find all selected hammers
    items.forEach(item => {
        // Is it an InventoryItem?
        if (_instanceof_(item, "InventoryItem")) {
            if ((item as InventoryItem).hasTag("Hammer")) {
                hammers.push(item);
            }
        }
        // Or it's a collasped item
        else {
            const list: any[] = item.items
            for (let i = 1; i < list.length; i++) {
                const item: InventoryItem = list[i]
                if (_instanceof_(item, "InventoryItem") && item.hasTag("Hammer")) {
                    hammers.push(item)
                }
            }
        }
    })

    if (hammers.length > 0) {

        // Create context sub-menu
        const hammersOption = context.addOptionOnTop("Equip Hammer")
        const hammersContext = context.getNew(context) as ISContextMenu
        context.addSubMenu(hammersOption, hammersContext)
    
        // Add hammers to context sub-menu
        hammers.forEach((hammer: InventoryItem) => {
            const option = hammersContext.addOption(hammer.getDisplayName(), player, doEquipHammer, hammer)

            const tooltip = new ISToolTip();
            tooltip.setName(`Equip ${hammer.getDisplayName()}`)
            const conditionPercent = luautils.round(hammer.getCondition() / hammer.getConditionMax() * 100, 2)
            tooltip.description = `Condition: ${conditionPercent}%`
            
            option.toolTip = tooltip
        })

    }

})