|
| 1 | +import { |
| 2 | + registerDeletableHandlerPatch, |
| 3 | + registerPropertyOverridePatch, |
| 4 | +} from 'blockbench-patch-manager' |
| 5 | +import { observable } from 'svelte-observable-store' |
| 6 | +import { SvelteDialog } from 'svelte-patching-tools/blockbench' |
| 7 | +import { PACKAGE } from '../../constants' |
| 8 | +import { activeProjectIsBlueprintFormat } from '../../formats/blueprint' |
| 9 | +import { InteractionConfig } from '../../nodeConfigs' |
| 10 | +import { localize } from '../../util/lang' |
| 11 | +import LocatorConfigDialog from './interactionConfig.svelte' |
| 12 | + |
| 13 | +// TODO - Make on-tick function work without requiring use-entity. |
| 14 | + |
| 15 | +export function openInteractionConfigDialog(interaction: BoundingBox) { |
| 16 | + // Blockbench's JSON stringifier doesn't handle custom toJSON functions, so I'm storing the config JSON in the bounding box instead of the actual BoundingBoxConfig object |
| 17 | + const boundingBoxConfig = InteractionConfig.fromJSON( |
| 18 | + // @ts-expect-error - Broken BB types |
| 19 | + (interaction.config ??= new InteractionConfig().toJSON()) |
| 20 | + ) |
| 21 | + |
| 22 | + const response = observable(boundingBoxConfig.response) |
| 23 | + const onSummonFunction = observable(boundingBoxConfig.onSummonFunction) |
| 24 | + const onInteractionFunction = observable(boundingBoxConfig.onInteractionFunction) |
| 25 | + const onAttackFunction = observable(boundingBoxConfig.onAttackFunction) |
| 26 | + const onRemoveFunction = observable(boundingBoxConfig.onRemoveFunction) |
| 27 | + const onTickFunction = observable(boundingBoxConfig.onTickFunction) |
| 28 | + |
| 29 | + new SvelteDialog({ |
| 30 | + id: `${PACKAGE.name}:interactionConfig`, |
| 31 | + title: localize('dialog.interaction_config.title'), |
| 32 | + width: 800, |
| 33 | + component: LocatorConfigDialog, |
| 34 | + props: { |
| 35 | + response, |
| 36 | + onSummonFunction, |
| 37 | + onInteractionFunction, |
| 38 | + onAttackFunction, |
| 39 | + onRemoveFunction, |
| 40 | + onTickFunction, |
| 41 | + }, |
| 42 | + disableKeybinds: true, |
| 43 | + onConfirm() { |
| 44 | + boundingBoxConfig.response = response.get() |
| 45 | + boundingBoxConfig.onSummonFunction = onSummonFunction.get() |
| 46 | + boundingBoxConfig.onInteractionFunction = onInteractionFunction.get() |
| 47 | + boundingBoxConfig.onAttackFunction = onAttackFunction.get() |
| 48 | + boundingBoxConfig.onRemoveFunction = onRemoveFunction.get() |
| 49 | + boundingBoxConfig.onTickFunction = onTickFunction.get() |
| 50 | + |
| 51 | + // @ts-expect-error - Broken BB types |
| 52 | + interaction.config = boundingBoxConfig.toJSON() |
| 53 | + }, |
| 54 | + }).show() |
| 55 | +} |
| 56 | + |
| 57 | +const OPEN_INTERACTION_CONFIG = registerDeletableHandlerPatch({ |
| 58 | + id: `animated_java:action/interaction-config`, |
| 59 | + create() { |
| 60 | + // @ts-expect-error - Broken BB types |
| 61 | + const action = new Blockbench.Action(`animated_java:action/interaction-config`, { |
| 62 | + icon: 'settings', |
| 63 | + name: localize('action.open_interaction_config.name'), |
| 64 | + condition: () => activeProjectIsBlueprintFormat(), |
| 65 | + click: () => { |
| 66 | + const interaction = BoundingBox.selected.at(0) |
| 67 | + if (!interaction) return |
| 68 | + // @ts-expect-error - Broken BB types |
| 69 | + openInteractionConfigDialog(interaction) |
| 70 | + }, |
| 71 | + }) |
| 72 | + return action |
| 73 | + }, |
| 74 | +}) |
| 75 | + |
| 76 | +registerPropertyOverridePatch({ |
| 77 | + id: `animated_java:bounding-box/extend`, |
| 78 | + target: BoundingBox.prototype, |
| 79 | + key: 'extend', |
| 80 | + |
| 81 | + get: function (this, value) { |
| 82 | + if (activeProjectIsBlueprintFormat()) { |
| 83 | + return function (this: BoundingBox, ...args) { |
| 84 | + const result = value.apply(this, args) |
| 85 | + this.menu = BoundingBox.prototype.menu |
| 86 | + return result |
| 87 | + } |
| 88 | + } |
| 89 | + return value |
| 90 | + }, |
| 91 | +}) |
| 92 | + |
| 93 | +registerPropertyOverridePatch({ |
| 94 | + id: `animated_java:bounding-box/menu`, |
| 95 | + dependencies: [`animated_java:action/interaction-config`], |
| 96 | + target: BoundingBox.prototype, |
| 97 | + key: 'menu', |
| 98 | + |
| 99 | + get: function (this, value) { |
| 100 | + if (activeProjectIsBlueprintFormat()) { |
| 101 | + return new Menu([ |
| 102 | + ...Outliner.control_menu_group, |
| 103 | + new MenuSeparator('export'), |
| 104 | + 'generate_bedrock_block_box', |
| 105 | + 'generate_bedrock_entity_box', |
| 106 | + new MenuSeparator('interaction'), |
| 107 | + OPEN_INTERACTION_CONFIG.get(), |
| 108 | + new MenuSeparator('settings'), |
| 109 | + { |
| 110 | + name: 'menu.cube.color', |
| 111 | + icon: 'color_lens', |
| 112 | + children() { |
| 113 | + return markerColors.map((color, i) => { |
| 114 | + return { |
| 115 | + icon: 'bubble_chart', |
| 116 | + color: color.standard, |
| 117 | + // @ts-expect-error - Broken BB types |
| 118 | + name: color.name ?? 'cube.color.' + color.id, |
| 119 | + click(element: BoundingBox) { |
| 120 | + // @ts-expect-error - Broken BB types |
| 121 | + element.forSelected((obj: BoundingBox) => { |
| 122 | + obj.setColor(i) |
| 123 | + }, 'Change color') |
| 124 | + }, |
| 125 | + } |
| 126 | + }) |
| 127 | + }, |
| 128 | + }, |
| 129 | + 'randomize_marker_colors', |
| 130 | + new MenuSeparator('manage'), |
| 131 | + 'rename', |
| 132 | + 'toggle_visibility', |
| 133 | + 'delete', |
| 134 | + ]) |
| 135 | + } |
| 136 | + return value |
| 137 | + }, |
| 138 | +}) |
0 commit comments