|
| 1 | +import { getElementNamesRecursive, UIAdvancedPresetDefinition, UIAnchor, UIBgFill, UIElement, UIParams } from '../../models/types'; |
| 2 | +import { IAdvancedExportGenerator, loadTemplate, replacePlaceholders } from '../services/export/advanced-export.builder'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Export generator for the counter preset. |
| 6 | + * |
| 7 | + * Generates a TypeScript class that wraps a counter widget with automatic increment functionality. |
| 8 | + * This implementation uses a template file (counter.widget.template.ts) for better maintainability. |
| 9 | + * The template approach provides: |
| 10 | + * - Proper TypeScript syntax highlighting and validation in the template file |
| 11 | + * - Easier testing and iteration on the generated code structure |
| 12 | + * - Clear separation between template logic and placeholder replacement |
| 13 | + * - Ability to share common patterns across multiple presets |
| 14 | + * |
| 15 | + * @implements {IAdvancedExportGenerator} |
| 16 | + * |
| 17 | + * @example |
| 18 | + * ```typescript |
| 19 | + * // Generated class usage in Battlefield modding: |
| 20 | + * const counter = new CounterWidget(); |
| 21 | + * const widget = counter.create(); // Creates the UI widget |
| 22 | + * counter.increment(); // Increments from 0 to 1 |
| 23 | + * counter.decrement(); // Increments from 1 to 0 |
| 24 | + * ``` |
| 25 | + */ |
| 26 | +export class CounterExportGenerator implements IAdvancedExportGenerator { |
| 27 | + private templateCache: string | null = null; |
| 28 | + |
| 29 | + /** |
| 30 | + * Generates TypeScript class code for a counter widget instance. |
| 31 | + * |
| 32 | + * Loads the counter widget template and replaces placeholders with actual values: |
| 33 | + * - {{CLASS_NAME}} - Unique class name for this instance |
| 34 | + * - {{TIMESTAMP}} - Generation timestamp |
| 35 | + * - {{UI_PARAMS}} - Serialized UI parameters for modlib.ParseUI() |
| 36 | + * |
| 37 | + * The template is cached after first load to avoid repeated network requests. |
| 38 | + * |
| 39 | + * @param rootElement - The root container element of the counter preset |
| 40 | + * @param preset - The counter preset definition |
| 41 | + * @param className - Unique class name for this counter instance |
| 42 | + * @param serializeHelpers - Helper functions for converting UI elements to TypeScript |
| 43 | + * @param serializeHelpers.serializeParamToTypescript - Converts UIParams to object literal code |
| 44 | + * @param serializeHelpers.serializeElement - Extracts UIParams from UIElement |
| 45 | + * @param strings - Localization strings (passed through to serialization) |
| 46 | + * @param timestamp - ISO timestamp for code generation comment header |
| 47 | + * @returns Complete TypeScript class definition as a string |
| 48 | + */ |
| 49 | + async generateClassCode( |
| 50 | + rootElement: UIElement, |
| 51 | + preset: UIAdvancedPresetDefinition, |
| 52 | + className: string, |
| 53 | + serializeHelpers: { |
| 54 | + serializeParamToTypescript: (param: UIParams, indentLevel: number, strings: Record<string, string>) => string; |
| 55 | + serializeElement: (e: UIElement) => UIParams; |
| 56 | + }, |
| 57 | + strings: Record<string, string>, |
| 58 | + timestamp: string |
| 59 | + ): Promise<string> { |
| 60 | + const { serializeParamToTypescript, serializeElement } = serializeHelpers; |
| 61 | + |
| 62 | + // Load template (cached after first load) |
| 63 | + if (!this.templateCache) { |
| 64 | + this.templateCache = await loadTemplate('counter.widget.template.ts'); |
| 65 | + } |
| 66 | + |
| 67 | + // Serialize the root element into TypeScript object literal format |
| 68 | + const params = serializeElement(rootElement); |
| 69 | + const uiParams = serializeParamToTypescript(params, 3, strings); |
| 70 | + |
| 71 | + // Replace placeholders in template |
| 72 | + return replacePlaceholders(this.templateCache, { |
| 73 | + CLASS_NAME: className, |
| 74 | + TIMESTAMP: timestamp, |
| 75 | + UI_PARAMS: uiParams |
| 76 | + }); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Unique identifier for the counter preset. |
| 82 | + * Used to register and look up this preset in the preset registry. |
| 83 | + */ |
| 84 | +export const COUNTER_PRESET_ID = 'counter-container'; |
| 85 | + |
| 86 | +/** |
| 87 | + * UI element blueprint for the counter preset. |
| 88 | + * |
| 89 | + * Defines the visual structure of the counter widget: |
| 90 | + * - A semi-transparent dark container (360x140px) |
| 91 | + * - A centered text element inside for displaying the counter value |
| 92 | + * |
| 93 | + * This blueprint is instantiated when a user adds a counter preset to the canvas. |
| 94 | + * The actual counter value (0, 1, 2...) is set at runtime via the increment() decrement() methods |
| 95 | + * in the generated TypeScript class. |
| 96 | + */ |
| 97 | +const blueprint: UIParams = { |
| 98 | + name: "CounterContainer", |
| 99 | + type: "Container", |
| 100 | + position: [49.97, 28.93], |
| 101 | + size: [210, 210], |
| 102 | + anchor: UIAnchor.TopLeft, |
| 103 | + visible: true, |
| 104 | + padding: 0, |
| 105 | + bgColor: [0.2, 0.2, 0.2], |
| 106 | + bgAlpha: 1, |
| 107 | + bgFill: UIBgFill.Blur, |
| 108 | + children: [ |
| 109 | + { |
| 110 | + name: "CounterText", |
| 111 | + type: "Text", |
| 112 | + position: [0, 0], |
| 113 | + size: [150, 150], |
| 114 | + anchor: UIAnchor.Center, |
| 115 | + visible: true, |
| 116 | + padding: 0, |
| 117 | + bgColor: [0.2, 0.2, 0.2], |
| 118 | + bgAlpha: 1, |
| 119 | + bgFill: UIBgFill.None, |
| 120 | + textLabel: "0", |
| 121 | + textColor: [1, 1, 1], |
| 122 | + textAlpha: 1, |
| 123 | + textSize: 38, |
| 124 | + textAnchor: UIAnchor.Center |
| 125 | + } |
| 126 | + ] |
| 127 | +}; |
| 128 | + |
| 129 | +/** |
| 130 | + * Counter preset definition. |
| 131 | + * |
| 132 | + * Defines a reusable counter widget that can be added to the UI builder canvas. |
| 133 | + * When exported, generates a TypeScript class with: |
| 134 | + * - A create() method that instantiates the widget |
| 135 | + * - A increment() and decrement() method to modify the counter value |
| 136 | + * - Public properties for accessing the container and text widgets |
| 137 | + * |
| 138 | + * This preset demonstrates the advanced preset system's capabilities: |
| 139 | + * - Complex multi-element widgets |
| 140 | + * - Custom export logic via CounterExportGenerator |
| 141 | + * - Slot definitions for customizable child elements |
| 142 | + * - Integration with the Battlefield modlib.ParseUI() system |
| 143 | + * |
| 144 | + * @see CounterExportGenerator for the export code generation logic |
| 145 | + * @see UIAdvancedPresetDefinition for the preset type definition |
| 146 | + */ |
| 147 | +export const counterPreset: UIAdvancedPresetDefinition = { |
| 148 | + id: COUNTER_PRESET_ID, |
| 149 | + label: 'Counter Container', |
| 150 | + version: '1.0.0', |
| 151 | + description: 'Container with a text widget that be incremented or decremented.', |
| 152 | + defaultClassName: 'CounterWidget', |
| 153 | + defaultRootName: 'CounterContainer', |
| 154 | + blueprint, |
| 155 | + slots: [ |
| 156 | + { |
| 157 | + id: 'counterText', |
| 158 | + label: 'Counter Text', |
| 159 | + description: 'Text widget displaying the numeric counter.', |
| 160 | + allowedTypes: ['Text'], |
| 161 | + }, |
| 162 | + ], |
| 163 | + exportGenerator: new CounterExportGenerator(), |
| 164 | +}; |
0 commit comments