UNPKG

@itwin/frontend-devtools

Version:

Debug menu and supporting UI widgets

365 lines • 14.4 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); exports.ChangeBackgroundColorTool = exports.ToggleWiremeshTool = exports.WoWIgnoreBackgroundTool = exports.OverrideSubCategoryTool = exports.ApplyRenderingStyleTool = exports.SaveRenderingStyleTool = exports.SkyCubeTool = exports.SkySphereTool = exports.ToggleSkyboxTool = exports.ChangeViewFlagsTool = exports.DisplayStyleTool = void 0; /** @packageDocumentation * @module Tools */ const core_common_1 = require("@itwin/core-common"); const core_frontend_1 = require("@itwin/core-frontend"); const ClipboardUtilities_1 = require("../ClipboardUtilities"); const parseArgs_1 = require("./parseArgs"); const parseToggle_1 = require("./parseToggle"); // Compiler has the info to construct this array for us, but we have no access to it... const booleanFlagNames = [ "dimensions", "patterns", "weights", "styles", "transparency", "fill", "textures", "materials", "acsTriad", "grid", "visibleEdges", "hiddenEdges", "lighting", "shadows", "clipVolume", "constructions", "monochrome", "backgroundMap", "ambientOcclusion", "forceSurfaceDiscard", "wiremesh", ]; const lowercaseBooleanFlagNames = booleanFlagNames.map((name) => name.toLowerCase()); /** Modifies the selected viewport's DisplayStyleState. * @beta */ class DisplayStyleTool extends core_frontend_1.Tool { get require3d() { return false; } async run() { const vp = core_frontend_1.IModelApp.viewManager.selectedView; if (undefined !== vp && (!this.require3d || vp.view.is3d()) && await this.execute(vp)) vp.displayStyle = vp.view.displayStyle; return true; } async parseAndRun(...args) { const vp = core_frontend_1.IModelApp.viewManager.selectedView; if (undefined !== vp && (!this.require3d || vp.view.is3d()) && await this.parse(args, vp)) return this.run(); else return false; } } exports.DisplayStyleTool = DisplayStyleTool; /** Modifies the selected viewport's ViewFlags. * The keyin syntax is as follows: * fdt change viewflags flag=value * Where 'flag' is one of the BooleanFlagName values, or "renderMode"; and value is an integer. * For boolean flags, value is 0 for false or 1 for true. For renderMode, value is one of the RenderMode enum values. * Flag names are case-insensitive. * @beta */ class ChangeViewFlagsTool extends core_frontend_1.Tool { static toolId = "ChangeViewFlags"; static get maxArgs() { return undefined; } static get minArgs() { return 1; } async run(vf, vp) { if (undefined !== vf && undefined !== vp) vp.viewFlags = vf; return true; } async parseAndRun(...args) { const vp = core_frontend_1.IModelApp.viewManager.selectedView; if (undefined === vp || 0 === args.length) return true; const vf = { ...vp.viewFlags }; for (const arg of args) { const parts = arg.split("="); if (2 !== parts.length) continue; const value = parseInt(parts[1], 10); if (Number.isNaN(value)) continue; const name = parts[0].toLowerCase(); if (name === "rendermode") { switch (value) { case core_common_1.RenderMode.SmoothShade: case core_common_1.RenderMode.Wireframe: case core_common_1.RenderMode.HiddenLine: case core_common_1.RenderMode.SolidFill: vf.renderMode = value; vp.invalidateRenderPlan(); break; } continue; } if (0 !== value && 1 !== value) continue; const index = lowercaseBooleanFlagNames.indexOf(name); if (-1 !== index) { const propName = booleanFlagNames[index]; vf[propName] = 0 !== value; vp.invalidateRenderPlan(); } } return this.run(new core_common_1.ViewFlags(vf), vp); } } exports.ChangeViewFlagsTool = ChangeViewFlagsTool; /** Toggles the skybox. * @beta */ class ToggleSkyboxTool extends DisplayStyleTool { static toolId = "ToggleSkybox"; get require3d() { return true; } async parse(_args) { return true; } // no arguments async execute(vp) { const style = vp.view.displayStyle; style.environment = style.environment.withDisplay({ sky: !style.environment.displaySky }); return true; } } exports.ToggleSkyboxTool = ToggleSkyboxTool; /** Defines a [SkySphere]($common) to apply to the current view. * @beta */ class SkySphereTool extends DisplayStyleTool { _image; static toolId = "SetSkySphere"; static get minArgs() { return 1; } static get maxArgs() { return 1; } get require3d() { return true; } async parse(args) { this._image = args[0]; return true; } async execute(vp) { if (this._image && vp.view.is3d()) { vp.view.displayStyle.environment = vp.view.displayStyle.environment.clone({ displaySky: true, sky: new core_common_1.SkySphere(this._image), }); } return true; } } exports.SkySphereTool = SkySphereTool; /** Defines a [SkyCube]($common) to apply to the current view. * @beta */ class SkyCubeTool extends DisplayStyleTool { _images = []; static toolId = "SetSkyCube"; static get minArgs() { return 1; } static get maxArgs() { return 6; } get require3d() { return true; } async parse(args) { this._images = [...args]; return true; } async execute(vp) { const imgs = this._images; if (imgs.length === 0 || !vp.view.is3d()) return true; let top, bottom, left, right, front, back; switch (imgs.length) { case 1: top = bottom = left = right = front = back = imgs[0]; break; case 2: top = bottom = imgs[0]; left = right = front = back = imgs[1]; break; case 3: top = bottom = imgs[0]; left = right = imgs[1]; front = back = imgs[2]; break; case 4: top = imgs[0]; bottom = imgs[1]; left = right = imgs[2]; front = back = imgs[3]; break; case 5: top = bottom = imgs[0]; left = imgs[1]; right = imgs[2]; front = imgs[3]; back = imgs[4]; break; default: top = imgs[0]; bottom = imgs[1]; left = imgs[2]; right = imgs[3]; front = imgs[4]; back = imgs[5]; break; } vp.view.displayStyle.environment = vp.view.displayStyle.environment.clone({ displaySky: true, sky: new core_common_1.SkyCube({ top, bottom, left, right, front, back }), }); return true; } } exports.SkyCubeTool = SkyCubeTool; /** Outputs (and optionally copies to the clipboard) a "rendering style" as a partial DisplayStyle3dSettingsProps JSON object based * on the current view's display style settings. * All arguments are optional, of the form "name=value" where `value` is 0 for false or 1 for true. All arguments default to `false` if omitted. * @see [DisplayStyleSettings.toOverrides]($common) for details. * Arguments: * * `all`: include all settings. * * `imodel`: include iModel-specific settings. * * `project`: include iTwin-specific (formerly known as project) settings. * * `map`: include background map settings. * * `drawingaids`: include drawing aid decoration settings. * * `copy`: copy result to system clipboard. * * `quote`: format the JSON so it can be parsed directly by [ApplyRenderingStyleTool]. * @beta */ class SaveRenderingStyleTool extends DisplayStyleTool { _options = {}; _copyToClipboard = false; _quote = false; static toolId = "SaveRenderingStyle"; static get minArgs() { return 0; } static get maxArgs() { return 7; } async parse(inputArgs) { const args = (0, parseArgs_1.parseArgs)(inputArgs); function getArg(name) { return args.getBoolean(name) ? true : undefined; } this._options.includeAll = getArg("a"); this._options.includeIModelSpecific = getArg("i"); this._options.includeITwinSpecific = getArg("p"); // "p" for backwards compatibility with old "project" terminology this._options.includeBackgroundMap = getArg("m"); this._options.includeDrawingAids = getArg("d"); this._copyToClipboard = true === getArg("c"); this._quote = true === getArg("q"); return true; } async execute(vp) { let json = JSON.stringify(vp.displayStyle.settings.toOverrides(this._options)); if (this._quote) json = `"${json.replace(/"/g, '""')}"`; core_frontend_1.IModelApp.notifications.outputMessage(new core_frontend_1.NotifyMessageDetails(core_frontend_1.OutputMessagePriority.Info, "Rendering style saved", json)); if (this._copyToClipboard) (0, ClipboardUtilities_1.copyStringToClipboard)(json); return false; } } exports.SaveRenderingStyleTool = SaveRenderingStyleTool; /** Given a "rendering style" as a partial DisplayStyle3dSettingsProperties JSON string, apply it to the selected viewport's display style. * @see [DisplayStyleSettings.applyOverrides]($common) for details. * @beta */ class ApplyRenderingStyleTool extends DisplayStyleTool { _overrides; static toolId = "ApplyRenderingStyle"; static get minArgs() { return 1; } static get maxArgs() { return 1; } async parse(args) { try { this._overrides = JSON.parse(args[0]); return true; } catch { core_frontend_1.IModelApp.notifications.outputMessage(new core_frontend_1.NotifyMessageDetails(core_frontend_1.OutputMessagePriority.Error, "Invalid JSON")); return false; } } async execute(vp) { if (this._overrides) vp.overrideDisplayStyle(this._overrides); return false; } } exports.ApplyRenderingStyleTool = ApplyRenderingStyleTool; /** Apply appearance overrides to one or more subcategories in the active viewport. * @beta */ class OverrideSubCategoryTool extends DisplayStyleTool { _overrideProps = {}; _subcategoryIds = []; static toolId = "OverrideSubCategory"; static get minArgs() { return 1; } static get maxArgs() { return 7; } async parse(inArgs) { const args = (0, parseArgs_1.parseArgs)(inArgs); const ids = args.get("i"); if (ids) this._subcategoryIds = ids.split(","); const props = this._overrideProps; props.color = args.getInteger("c"); props.weight = args.getInteger("w"); props.priority = args.getInteger("p"); props.transp = args.getFloat("t"); props.material = args.get("m"); const visible = args.getBoolean("v"); props.invisible = typeof visible === "boolean" ? !visible : undefined; return true; } async execute(vp) { const ovr = core_common_1.SubCategoryOverride.fromJSON(this._overrideProps); for (const id of this._subcategoryIds) vp.displayStyle.overrideSubCategory(id, ovr); return true; } } exports.OverrideSubCategoryTool = OverrideSubCategoryTool; /** Set whether background color is ignored when applying white-on-white reversal. * @beta */ class WoWIgnoreBackgroundTool extends DisplayStyleTool { _ignore; static toolId = "WoWIgnoreBackground"; static get minArgs() { return 0; } static get maxArgs() { return 1; } async parse(args) { const ignore = (0, parseToggle_1.parseToggle)(args[0]); if (typeof ignore === "string") return false; this._ignore = ignore; return true; } async execute(vp) { const ignoreBackgroundColor = this._ignore ?? !vp.displayStyle.settings.whiteOnWhiteReversal.ignoreBackgroundColor; vp.displayStyle.settings.whiteOnWhiteReversal = core_common_1.WhiteOnWhiteReversalSettings.fromJSON({ ignoreBackgroundColor }); return true; } } exports.WoWIgnoreBackgroundTool = WoWIgnoreBackgroundTool; /** Toggle whether surfaces display with overlaid wiremesh in the active viewport. * @see [ViewFlags.wiremesh]($common). * @beta */ class ToggleWiremeshTool extends DisplayStyleTool { _enable; static toolId = "ToggleWiremesh"; static get minArgs() { return 0; } static get maxArgs() { return 1; } async parse(args) { const enable = (0, parseToggle_1.parseToggle)(args[0]); if (typeof enable === "string") return false; this._enable = enable; return true; } async execute(vp) { vp.viewFlags = vp.viewFlags.with("wiremesh", this._enable ?? !vp.viewFlags.wiremesh); return true; } } exports.ToggleWiremeshTool = ToggleWiremeshTool; /** Change the background color of the active viewport and optionally its transparency. * @beta */ class ChangeBackgroundColorTool extends DisplayStyleTool { _color; static toolId = "ChangeBackgroundColor"; static get minArgs() { return 1; } static get maxArgs() { return 1; } async parse(args) { this._color = core_common_1.ColorDef.fromString(args[0]); return true; } async execute(vp) { if (!this._color) return false; vp.displayStyle.backgroundColor = this._color; return true; } } exports.ChangeBackgroundColorTool = ChangeBackgroundColorTool; //# sourceMappingURL=DisplayStyleTools.js.map