/
DisplayStyleSettings.ts
1384 lines (1195 loc) · 59.1 KB
/
DisplayStyleSettings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module DisplayStyles
*/
// cspell:ignore greyscale ovrs
import {
assert, BeEvent, CompressedId64Set, Id64, Id64Array, Id64String, JsonUtils, MutableCompressedId64Set, OrderedId64Iterable,
} from "@itwin/core-bentley";
import { XYZProps } from "@itwin/core-geometry";
import { AmbientOcclusion } from "./AmbientOcclusion";
import { AnalysisStyle, AnalysisStyleProps } from "./AnalysisStyle";
import { BackgroundMapSettings, PersistentBackgroundMapProps } from "./BackgroundMapSettings";
import { ClipStyle, ClipStyleProps } from "./ClipStyle";
import { ColorDef, ColorDefProps } from "./ColorDef";
import { DefinitionElementProps } from "./ElementProps";
import { HiddenLine } from "./HiddenLine";
import { FeatureAppearance, FeatureAppearanceProps } from "./FeatureSymbology";
import { PlanarClipMaskProps, PlanarClipMaskSettings } from "./PlanarClipMask";
import { SubCategoryOverride } from "./SubCategoryOverride";
import { LightSettings, LightSettingsProps } from "./LightSettings";
import { MapImageryProps, MapImagerySettings } from "./MapImagerySettings";
import { PlanProjectionSettings, PlanProjectionSettingsProps } from "./PlanProjectionSettings";
import { RenderSchedule } from "./RenderSchedule";
import { Environment, EnvironmentProps } from "./Environment";
import { SolarShadowSettings, SolarShadowSettingsProps } from "./SolarShadows";
import { SubCategoryAppearance } from "./SubCategoryAppearance";
import { ThematicDisplay, ThematicDisplayMode, ThematicDisplayProps } from "./ThematicDisplay";
import { ViewFlagProps, ViewFlags } from "./ViewFlags";
import { Cartographic } from "./geometry/Cartographic";
import { IModel } from "./IModel";
import { calculateSolarDirection } from "./SolarCalculate";
import { ContextRealityModel, ContextRealityModelProps, ContextRealityModels } from "./ContextRealityModel";
import { RealityModelDisplayProps, RealityModelDisplaySettings } from "./RealityModelDisplaySettings";
import { WhiteOnWhiteReversalProps, WhiteOnWhiteReversalSettings } from "./WhiteOnWhiteReversalSettings";
/** Describes the [[SubCategoryOverride]]s applied to a [[SubCategory]] by a [[DisplayStyle]].
* @see [[DisplayStyleSettingsProps]]
* @public
* @extensions
*/
export interface DisplayStyleSubCategoryProps extends SubCategoryAppearance.Props {
/** The Id of the [[SubCategory]] whose appearance is to be overridden. */
subCategory?: Id64String;
}
/** A [[FeatureAppearanceProps]] applied to a specific model to override its appearance within the context of a [DisplayStyle]($backend).
* @see [[DisplayStyleSettingsProps.modelOvr]].
* @public
* @extensions
*/
export interface DisplayStyleModelAppearanceProps extends FeatureAppearanceProps {
/** The Id of the model whose appearance is to be overridden. */
modelId: Id64String;
}
/** [[RealityModelDisplaySettings]] applied to a reality [Model]($backend) to change how it is rendered within the context of a [DisplayStyle]($backend).
* @see [[DisplayStyleSettingsProps.realityModelDisplay]].
* @beta
*/
export interface DisplayStyleRealityModelDisplayProps extends RealityModelDisplayProps {
/** The Id of the reality [Model]($backend) to which the settings apply. */
modelId?: Id64String;
}
/** A [[PlanarClipMaskProps]] associated with a specific reality model.
* @see [[DisplayStyleSettingsProps.planarClipOvr]].
* @public
* @extensions
*/
export interface DisplayStylePlanarClipMaskProps extends PlanarClipMaskProps {
/** The Id of the model to mask. */
modelId?: Id64String;
}
/** Describes the style in which monochrome color is applied by a [[DisplayStyleSettings]].
* @public
* @extensions
*/
export enum MonochromeMode {
/** The color of the geometry is replaced with the monochrome color. e.g., if monochrome color is white, the geometry will be white. */
Flat = 0,
/** The color of surfaces is computed as normal, then scaled to a shade of the monochrome color based on the surface color's intensity.
* For example, if the monochrome color is white, this results in a greyscale effect.
* Geometry other than surfaces is treated the same as [[MonochromeMode.Flat]].
*/
Scaled = 1,
}
/** JSON representation of the [[DisplayStyleSettings]] associated with a [[DisplayStyleProps]].
* These settings are not stored directly as members of the [[DisplayStyleProps]]. Instead, they are stored
* as members of [[DisplayStyleProps.jsonProperties.styles]].
* @public
* @extensions
*/
export interface DisplayStyleSettingsProps {
/** See [[DisplayStyleSettings.viewFlags]].
* @note Be careful with the case of this field - it is spelled in all lower-case letters, while [[DisplayStyleSettings.viewFlags]] is spelled `viewFlags`.
*/
viewflags?: ViewFlagProps;
/** See [[DisplayStyleSettings.backgroundColor]]. */
backgroundColor?: ColorDefProps;
/** See [[DisplayStyleSettings.monochromeColor]]. */
monochromeColor?: ColorDefProps;
/** See [[DisplayStyleSettings.monochromeMode]]. */
monochromeMode?: MonochromeMode;
/** See [[DisplayStyleSettings.analysisStyle]]. */
analysisStyle?: AnalysisStyleProps;
/** See [[DisplayStyleSettings.analysisFraction]]. */
analysisFraction?: number;
/** See [[DisplayStyleSettings.scheduleScriptProps]]. */
scheduleScript?: RenderSchedule.ScriptProps;
/** See [[DisplayStyleSettings.renderTimeline]]. */
renderTimeline?: Id64String;
/** See [[DisplayStyleSettings.timePoint]]. */
timePoint?: number;
/** Overrides applied to the appearances of subcategories in the view.
* See [[DisplayStyleSettings.overrideSubCategory]].
*/
subCategoryOvr?: DisplayStyleSubCategoryProps[];
/** See [[DisplayStyleSettings.backgroundMap]]. */
backgroundMap?: PersistentBackgroundMapProps;
/** See [[DisplayStyleSettings.contextRealityModels]]. */
contextRealityModels?: ContextRealityModelProps[];
/** Ids of elements not to be displayed in the view. Prefer the compressed format, especially when sending between frontend and backend - the number of Ids may be quite large.
* See [[DisplayStyleSettings.excludedElements]].
*/
excludedElements?: Id64Array | CompressedId64Set;
/** See [[DisplayStyleSettings.mapImagery]].
* @beta
*/
mapImagery?: MapImageryProps;
/** Overrides applied to the appearance of models in the view.
* See [[DisplayStyleSettings.overrideModelAppearance]].
*/
modelOvr?: DisplayStyleModelAppearanceProps[];
/** Display settings applied to specific reality models in the view.
* @see [[DisplayStyleSettings.setRealityModelDisplaySettings]].
* @beta
*/
realityModelDisplay?: DisplayStyleRealityModelDisplayProps[];
/** See [[DisplayStyleSettings.clipStyle]]. */
clipStyle?: ClipStyleProps;
/** See [[DisplayStyleSettings.planarClipMasks]]. */
planarClipOvr?: DisplayStylePlanarClipMaskProps[];
/** See [[DisplayStyleSettings.whiteOnWhiteReversal]]. */
whiteOnWhiteReversal?: WhiteOnWhiteReversalProps;
}
/** JSON representation of [[DisplayStyle3dSettings]] associated with a [[DisplayStyle3dProps]].
* @public
* @extensions
*/
export interface DisplayStyle3dSettingsProps extends DisplayStyleSettingsProps {
/** See [[DisplayStyle3dSettings.environment]]. */
environment?: EnvironmentProps;
/** See [[DisplayStyle3dSettings.thematic]]. */
thematic?: ThematicDisplayProps;
/** See [[DisplayStyle3dSettings.hiddenLineSettings]]. */
hline?: HiddenLine.SettingsProps;
/** See [[DisplayStyle3dSettings.ambientOcclusionSettings]]. */
ao?: AmbientOcclusion.Props;
/** See [[DisplayStyle3dSettings.solarShadows]]. */
solarShadows?: SolarShadowSettingsProps;
/** See [[DisplayStyle3dSettings.lights]]. */
lights?: LightSettingsProps;
/** See [[DisplayStyle3dSettings.planProjections]]. */
planProjections?: { [modelId: string]: PlanProjectionSettingsProps };
/** Old lighting settings - only `sunDir` was ever used; it is now part of [[lights]].
* DisplayStyle3dSettings will construct a LightSettings from sceneLights.sunDir IFF [[lights]] is not present.
* @internal
*/
sceneLights?: { sunDir?: XYZProps };
}
/** JSON representation of a [[DisplayStyle]] or [[DisplayStyleState]].
* @public
* @extensions
*/
export interface DisplayStyleProps extends DefinitionElementProps {
/** Display styles store their settings in a `styles` property within [[ElementProps.jsonProperties]]. */
jsonProperties?: {
styles?: DisplayStyleSettingsProps;
};
}
/** JSON representation of a [[DisplayStyle3d]] or [[DisplayStyle3dState]].
* @public
* @extensions
*/
export interface DisplayStyle3dProps extends DisplayStyleProps {
/** Display styles store their settings in a `styles` property within [[ElementProps.jsonProperties]]. */
jsonProperties?: {
styles?: DisplayStyle3dSettingsProps;
};
}
/** Controls which settings are serialized by [[DisplayStyleSettings.toOverrides]]. A display style includes some settings that are specific to a given iModel - for example,
* the subcategory overrides are indexed by subcategory Ids and model appearance overrides are indexed by model ids. Other settings are specific to a given iTwin, like the set of displayed context reality models. Such settings can be useful
* when creating display style overrides intended for use with a specific iModel or iTwin, but should be omitted when creating general-purpose display style overrides intended
* for use with any iModel or iTwin. This is the default behavior if no more specific options are provided.
* @public
* @extensions
*/
export interface DisplayStyleOverridesOptions {
/** Serialize all settings. Applying the resultant [[DisplayStyleSettingsProps]] will produce a [[DisplayStyleSettings]] identical to the original settings. */
includeAll?: true;
/** Serialize iModel-specific settings. These settings are only meaningful within the context of a specific iModel. Setting this to `true` implies all iTwin-specific settings will be serialized too.
* The following are iModel-specific settings:
* * Subcategory overrides.
* * Model Appearance overrides.
* * Classifiers associated with context reality models.
* * Analysis style.
* * Schedule script.
* * Excluded elements.
* * Plan projection settings.
* * Thematic sensor settings and height range. If iModel-specific settings are *not* serialized, sensors will be omitted and, for thematic height mode, the range will be omitted.
* * If the display style settings are associated with a [DisplayStyleState]($frontend), then overriding thematic settings will compute a default height range based on the iModel's project extents.
*/
includeIModelSpecific?: true;
/** Serialize iTwin-specific settings. These settings are only meaningful within the context of a specific iTwin. These settings are always included if `includeIModelSpecific` is `true`.
* The following are iTwin-specific settings:
* * Context reality models. If iModel-specific settings are *not* serialized, the classifiers will be omitted.
* * Time point.
*/
includeITwinSpecific?: true;
/** Serialize settings related to drawing aid decorations (the ACS triad and the grid). */
includeDrawingAids?: true;
/** Serialize the background map settings. */
includeBackgroundMap?: true;
}
/** DisplayStyleSettings initially persisted its excluded elements as an array of Id64Strings in JSON, and exposed them as a Set<string>.
* This becomes problematic when these arrays become very large, in terms of the amount of data and the time required to convert them to a Set.
* The Ids are now persisted to JSON as a [[CompressedId64Set]], significantly reducing their size. However, for backwards API compatibility we must
* continue to expose [[DisplayStyleSettings.excludedElements]] as a Set<string>. The [[ExcludedElements]] class tries to minimize the impact of that requirement by
* maintaining the Ids primarily as a [[MutableCompressedId64Set]], only allocating the Set<string> if a caller actually requests it.
* The only operation Set provides more efficiently than MutableCompressedId64Set is checking for the presence of an Id (the `has()` method).
* @internal
*/
class ExcludedElements implements OrderedId64Iterable {
private readonly _json: DisplayStyleSettingsProps;
private readonly _ids: MutableCompressedId64Set;
private _synchronizing = false;
public constructor(json: DisplayStyleSettingsProps) {
this._json = json;
if (Array.isArray(json.excludedElements))
this._ids = new MutableCompressedId64Set(CompressedId64Set.compressIds(OrderedId64Iterable.sortArray(json.excludedElements)));
else
this._ids = new MutableCompressedId64Set(json.excludedElements);
}
public reset(ids: CompressedId64Set | OrderedId64Iterable | undefined) {
this.synchronize(() => {
this._ids.reset((ids && "string" !== typeof ids) ? CompressedId64Set.compressIds(ids) : ids);
});
}
public get ids(): CompressedId64Set {
return this._ids.ids;
}
public add(ids: Iterable<Id64String>): void {
this.synchronize(() => {
for (const id of ids)
this._ids.add(id);
});
}
public delete(ids: Iterable<Id64String>): void {
this.synchronize(() => {
for (const id of ids)
this._ids.delete(id);
});
}
public [Symbol.iterator]() {
return this._ids[Symbol.iterator]();
}
/** The JSON must be kept up-to-date at all times. */
private synchronize(func: () => void): void {
if (this._synchronizing)
return;
this._synchronizing = true;
try {
func();
} finally {
this._synchronizing = false;
const ids = this._ids.ids;
if (0 === ids.length)
delete this._json.excludedElements;
else
this._json.excludedElements = ids;
}
}
}
type OverridesArrayKey = "subCategoryOvr" | "modelOvr" | "planarClipOvr" | "realityModelDisplay";
/** An implementation of Map that is based on a JSON array, used for a display styles subcategory overrides, model appearance overrides,
* and planar clip masks. Ensures:
* - JSON representation kept in sync with changes to map; and
* - Events dispatched when map contents change.
*/
class OverridesMap<OverrideProps, Override> extends Map<Id64String, Override> {
// This is required for mock framework used by ui libraries, which otherwise try to clone this as a standard Map.
public override get [Symbol.toStringTag]() { return "OverridesMap"; }
public constructor(
private readonly _json: DisplayStyleSettingsProps,
private readonly _arrayKey: OverridesArrayKey,
private readonly _event: BeEvent<(id: Id64String, ovr: Override | undefined) => void>,
private readonly _idFromProps: (props: OverrideProps) => Id64String | undefined,
private readonly _overrideToProps: (ovr: Override, id: Id64String) => OverrideProps,
private readonly _overrideFromProps: (props: OverrideProps) => Override | undefined) {
super();
this.populate();
}
public override set(id: Id64String, override: Override): this {
this._event.raiseEvent(id, override);
super.set(id, override);
const index = this.findOrAllocateIndex(id);
const array = this._array;
assert(undefined !== array);
array[index] = this._overrideToProps(override, id);
return this;
}
public override delete(id: Id64String): boolean {
this._event.raiseEvent(id, undefined);
if (!super.delete(id))
return false;
const index = this.findExistingIndex(id);
if (undefined !== index) {
assert(undefined !== this._array);
this._array.splice(index, 1);
}
return true;
}
public override clear(): void {
for (const id of this.keys())
this.delete(id);
this._json[this._arrayKey] = undefined;
}
public populate(): void {
super.clear();
const ovrs = this._array;
if (!ovrs)
return;
for (const props of ovrs) {
const id = this._idFromProps(props);
if (undefined !== id && Id64.isValidId64(id)) {
const ovr = this._overrideFromProps(props);
if (ovr)
super.set(id, ovr);
}
}
}
private get _array(): OverrideProps[] | undefined {
return JsonUtils.asArray(this._json[this._arrayKey]);
}
private findOrAllocateIndex(id: Id64String): number {
const index = this.findExistingIndex(id);
if (undefined !== index)
return index;
let ovrs = this._array;
if (!ovrs)
ovrs = this._json[this._arrayKey] = [];
return ovrs.length;
}
private findExistingIndex(id: Id64String): number | undefined {
const ovrs = this._array;
if (!ovrs)
return undefined;
for (let i = 0; i < ovrs.length; i++)
if (this._idFromProps(ovrs[i]) === id)
return i;
return undefined;
}
}
/** Options supplied when constructing a [[DisplayStyleSettings]].
* @public
* @extensions
*/
export interface DisplayStyleSettingsOptions {
/** A function that instantiates a [[ContextRealityModel]] to be stored in [[DisplayStyleSettings.contextRealityModels]]. */
createContextRealityModel?: (props: ContextRealityModelProps) => ContextRealityModel;
/** If true, the caller will populate contextRealityModels after construction.
* @internal used by DisplayStyleState constructor.
*/
deferContextRealityModels?: boolean;
}
/** Provides access to the settings defined by a [[DisplayStyle]] or [[DisplayStyleState]], and ensures that
* the style's JSON properties are kept in sync.
* @see [[DisplayStyleSettingsProps]] for the JSON representation of these settings.
* @public
*/
export class DisplayStyleSettings {
protected readonly _json: DisplayStyleSettingsProps;
private _viewFlags: ViewFlags;
private _background: ColorDef;
private _monochrome: ColorDef;
private _monochromeMode: MonochromeMode;
private readonly _subCategoryOverrides: OverridesMap<DisplayStyleSubCategoryProps, SubCategoryOverride>;
private readonly _modelAppearanceOverrides: OverridesMap<DisplayStyleModelAppearanceProps, FeatureAppearance>;
private readonly _realityModelDisplaySettings: OverridesMap<DisplayStyleRealityModelDisplayProps, RealityModelDisplaySettings>;
private readonly _planarClipMasks: OverridesMap<DisplayStylePlanarClipMaskProps, PlanarClipMaskSettings>;
private readonly _excludedElements: ExcludedElements;
private _backgroundMap: BackgroundMapSettings;
private _mapImagery: MapImagerySettings;
private _analysisStyle?: AnalysisStyle;
private _clipStyle: ClipStyle;
private readonly _contextRealityModels: ContextRealityModels;
private _whiteOnWhiteReversal: WhiteOnWhiteReversalSettings;
/** Returns true if this is a [[DisplayStyle3dSettings]]. */
public is3d(): this is DisplayStyle3dSettings {
return false;
}
/** Planar clip masks to be applied to persistent reality models (@see [SpatialModelState.isRealityModel]($frontend).
* The key for each entry is the Id of the model to which the mask settings apply.
*/
public get planarClipMasks(): Map<Id64String, PlanarClipMaskSettings> {
return this._planarClipMasks;
}
/** Reality models to be displayed in the view. */
public get contextRealityModels(): ContextRealityModels {
return this._contextRealityModels;
}
/** Event raised by [[applyOverrides]] just before the overrides are applied. */
public readonly onApplyOverrides = new BeEvent<(overrides: Readonly<DisplayStyleSettingsProps>) => void>();
/** Event raised by [[applyOverrides]] after the overrides are applied. */
public readonly onOverridesApplied = new BeEvent<(overrides: Readonly<DisplayStyleSettingsProps>) => void>();
/** Event raised just prior to assignment to the [[viewFlags]] property. */
public readonly onViewFlagsChanged = new BeEvent<(newFlags: Readonly<ViewFlags>) => void>();
/** Event raised just prior to assignment to the [[backgroundColor]] property. */
public readonly onBackgroundColorChanged = new BeEvent<(newColor: ColorDef) => void>();
/** Event raised just prior to assignment to the [[monochromeColor]] property. */
public readonly onMonochromeColorChanged = new BeEvent<(newColor: ColorDef) => void>();
/** Event raised just prior to assignment to the [[monochromeMode]] property. */
public readonly onMonochromeModeChanged = new BeEvent<(newMode: MonochromeMode) => void>();
/** Event raised just prior to assignment to the [[backgroundMap]] property. */
public readonly onBackgroundMapChanged = new BeEvent<(newMap: BackgroundMapSettings) => void>();
/** Event raised just prior to assignment to the [[mapImagery]] property.
* @beta
*/
public readonly onMapImageryChanged = new BeEvent<(newImagery: Readonly<MapImagerySettings>) => void>();
/** Event raised just prior to assignment to the `scheduleScriptProps` property.
* @see [[onRenderTimelineChanged]] to be notified when the [[renderTimeline]] property from which a script can be obtained is changed.
*/
public readonly onScheduleScriptPropsChanged = new BeEvent<(newProps: Readonly<RenderSchedule.ScriptProps> | undefined) => void>();
/** Event raised just prior to assignment to the [[renderTimeline]] property. */
public readonly onRenderTimelineChanged = new BeEvent<(newRenderTimeline: Id64String | undefined) => void>();
/** Event raised just prior to assignment to the [[timePoint]] property. */
public readonly onTimePointChanged = new BeEvent<(newTimePoint: number | undefined) => void>();
/** Event raised just prior to assignment to the [[analysisStyle]] property. */
public readonly onAnalysisStyleChanged = new BeEvent<(newStyle: Readonly<AnalysisStyle> | undefined) => void>();
/** Event raised just prior to assignment to the [[analysisFraction]] property. */
public readonly onAnalysisFractionChanged = new BeEvent<(newFraction: number) => void>();
/** Event raised when the contents of [[excludedElementIds]] changes. */
public readonly onExcludedElementsChanged = new BeEvent<() => void>();
/** Event raised just prior to assignment to the [[clipStyle]] property. */
public readonly onClipStyleChanged = new BeEvent<(newStyle: ClipStyle) => void>();
/** Event raised when the [[SubCategoryOverride]]s change. */
public readonly onSubCategoryOverridesChanged = new BeEvent<(subCategoryId: Id64String, newOverrides: SubCategoryOverride | undefined) => void>();
/** Event raised just before changing the appearance override for a model. */
public readonly onModelAppearanceOverrideChanged = new BeEvent<(modelId: Id64String, newAppearance: FeatureAppearance | undefined) => void>();
/** Event raised just before [[setRealityModelDisplaySettings]] changes the display settings for a reality model.
* @beta
*/
public readonly onRealityModelDisplaySettingsChanged = new BeEvent<(modelId: Id64String, newSettings: RealityModelDisplaySettings | undefined) => void>();
/** Event raised just prior to assignment to the [[DisplayStyle3dSettings.thematic]] property. */
public readonly onThematicChanged = new BeEvent<(newThematic: ThematicDisplay) => void>();
/** Event raised just prior to assignment to the [[DisplayStyle3dSettings.hiddenLineSettings]] property. */
public readonly onHiddenLineSettingsChanged = new BeEvent<(newSettings: HiddenLine.Settings) => void>();
/** Event raised just prior to assignment to the [[DisplayStyle3dSettings.ambientOcclusionSettings]] property. */
public readonly onAmbientOcclusionSettingsChanged = new BeEvent<(newSettings: AmbientOcclusion.Settings) => void>();
/** Event raised just prior to assignment to the [[DisplayStyle3dSettings.solarShadows]] property. */
public readonly onSolarShadowsChanged = new BeEvent<(newSettings: SolarShadowSettings) => void>();
/** Event raised just prior to assignment to the [[DisplayStyle3dSettings.environment]] property. */
public readonly onEnvironmentChanged = new BeEvent<(newEnv: Readonly<Environment>) => void>();
/** Event raised just prior to assignment to the [[DisplayStyle3dSettings.lights]] property. */
public readonly onLightsChanged = new BeEvent<(newLights: LightSettings) => void>();
/** Event raised just before changing the plan projection settings for a model. */
public readonly onPlanProjectionSettingsChanged = new BeEvent<(modelId: Id64String, newSettings: PlanProjectionSettings | undefined) => void>();
/** Event raised just before adding or removing an entry from [[planarClipMasks]]. */
public readonly onPlanarClipMaskChanged = new BeEvent<(modelId: Id64String, newSettings: PlanarClipMaskSettings | undefined) => void>();
/** Event raised just prior to assignment to the [[whiteOnWhiteReversal]] property. */
public readonly onWhiteOnWhiteReversalChanged = new BeEvent<(newSettings: WhiteOnWhiteReversalSettings) => void>();
/** Construct a new DisplayStyleSettings from an [[ElementProps.jsonProperties]].
* @param jsonProperties An object with an optional `styles` property containing a display style's settings.
* @param options Options for customizing the display style settings.
* @note When the `DisplayStyleSetting`'s properties are modified by public setters, the `jsonProperties`'s `styles` object will be updated to reflect the change.
* @note If `jsonProperties` contains no `styles` member, one will be added as an empty object.
* @note Generally there is no reason to create an object of this type directly; a [[DisplayStyle]] or [[DisplayStyleState]] constructs one as part of its own construction.
*/
public constructor(jsonProperties: { styles?: DisplayStyleSettingsProps }, options?: DisplayStyleSettingsOptions) {
if (undefined === jsonProperties.styles)
jsonProperties.styles = {};
this._json = jsonProperties.styles;
this._viewFlags = ViewFlags.fromJSON(this._json.viewflags);
this._background = ColorDef.fromJSON(this._json.backgroundColor);
this._monochrome = undefined !== this._json.monochromeColor ? ColorDef.fromJSON(this._json.monochromeColor) : ColorDef.white;
this._monochromeMode = MonochromeMode.Flat === this._json.monochromeMode ? MonochromeMode.Flat : MonochromeMode.Scaled;
this._backgroundMap = BackgroundMapSettings.fromPersistentJSON(this._json.backgroundMap);
this._mapImagery = MapImagerySettings.createFromJSON(this._json.mapImagery, this._json.backgroundMap);
// Ensure that if we used the deprecated imagery properties from this._backgroundMap to set up the base layer of this._mapImagery,
// we update our JSON to include that base layer.
this._json.mapImagery = this._mapImagery.toJSON();
this._excludedElements = new ExcludedElements(this._json);
if (this._json.analysisStyle)
this._analysisStyle = AnalysisStyle.fromJSON(this._json.analysisStyle);
this._whiteOnWhiteReversal = WhiteOnWhiteReversalSettings.fromJSON(this._json.whiteOnWhiteReversal);
this._clipStyle = ClipStyle.fromJSON(this._json.clipStyle);
this._subCategoryOverrides = new OverridesMap<DisplayStyleSubCategoryProps, SubCategoryOverride>(this._json, "subCategoryOvr", this.onSubCategoryOverridesChanged,
(props) => props.subCategory,
(ovr, subCategory) => { return { ...ovr.toJSON(), subCategory }; },
(props) => {
const ovr = SubCategoryOverride.fromJSON(props);
return ovr.anyOverridden ? ovr : undefined;
});
this._modelAppearanceOverrides = new OverridesMap<DisplayStyleModelAppearanceProps, FeatureAppearance>(this._json, "modelOvr", this.onModelAppearanceOverrideChanged,
(props) => props.modelId,
(ovr, modelId) => { return { ...ovr.toJSON(), modelId }; },
(props) => {
const app = FeatureAppearance.fromJSON(props);
return app.anyOverridden ? app : undefined;
});
this._realityModelDisplaySettings = new OverridesMap<DisplayStyleRealityModelDisplayProps, RealityModelDisplaySettings>(this._json, "realityModelDisplay", this.onRealityModelDisplaySettingsChanged,
(props) => props.modelId,
(settings, modelId) => { return { ...settings.toJSON(), modelId }; },
(props) => RealityModelDisplaySettings.fromJSON(props),
);
this._planarClipMasks = new OverridesMap<DisplayStylePlanarClipMaskProps, PlanarClipMaskSettings>(this._json, "planarClipOvr", this.onPlanarClipMaskChanged,
(props) => props.modelId,
(ovr, modelId) => { return { ...ovr.toJSON(), modelId }; },
(props) => {
const settings = PlanarClipMaskSettings.fromJSON(props);
return settings.isValid ? settings : undefined;
});
this._contextRealityModels = new ContextRealityModels({
container: this._json,
createContextRealityModel: options?.createContextRealityModel,
deferPopulating: options?.deferContextRealityModels,
});
}
/** Flags controlling various aspects of the display style. */
public get viewFlags(): ViewFlags { return this._viewFlags; }
public set viewFlags(flags: ViewFlags) {
if (this.viewFlags.equals(flags))
return;
this.onViewFlagsChanged.raiseEvent(flags);
this._viewFlags = flags;
this._json.viewflags = flags.toJSON();
}
/** The color displayed in the view background - by default, [[ColorDef.black]]. */
public get backgroundColor(): ColorDef { return this._background; }
public set backgroundColor(color: ColorDef) {
if (this.backgroundColor.equals(color))
return;
this.onBackgroundColorChanged.raiseEvent(color);
this._background = color;
this._json.backgroundColor = color.toJSON();
}
/** The color used to draw geometry when [[ViewFlags.monochrome]] is enabled - by default, [[ColorDef.white]].
* The monochrome color is applied to all surfaces and linear geometry, but only applied to the **edges** of surfaces in [[RenderMode.Wireframe]].
* @see [[monochromeMode]] to control how the color is applied.
*/
public get monochromeColor(): ColorDef { return this._monochrome; }
public set monochromeColor(color: ColorDef) {
if (this.monochromeColor.equals(color))
return;
this.onMonochromeColorChanged.raiseEvent(color);
this._monochrome = color;
this._json.monochromeColor = color.toJSON();
}
/** The style in which [[monochromeColor]] is applied when [[ViewFlags.monochrome]] is enabled - by default, [[MonochromeMode.Scaled]]. */
public get monochromeMode(): MonochromeMode { return this._monochromeMode; }
public set monochromeMode(mode: MonochromeMode) {
if (this.monochromeMode === mode)
return;
this.onMonochromeModeChanged.raiseEvent(mode);
this._monochromeMode = mode;
this._json.monochromeMode = mode;
}
/** Settings controlling display of the background map within views of geolocated models. */
public get backgroundMap(): BackgroundMapSettings { return this._backgroundMap; }
public set backgroundMap(map: BackgroundMapSettings) {
if (!this.backgroundMap.equals(map)) {
this.onBackgroundMapChanged.raiseEvent(map);
this._backgroundMap = map; // it's an immutable type.
this._json.backgroundMap = map.toPersistentJSON();
}
}
/** Settings defining the map imagery layers to be displayed within the view.
* @beta
*/
public get mapImagery(): MapImagerySettings { return this._mapImagery; }
public set mapImagery(mapImagery: MapImagerySettings) {
this.onMapImageryChanged.raiseEvent(mapImagery);
this._mapImagery = mapImagery;
this._json.mapImagery = this._mapImagery.toJSON();
}
/** @internal
* Handles keeping the map imagery layers in synch after changes have been made (used internally only by front end)
*/
public synchMapImagery() {
this.onMapImageryChanged.raiseEvent(this._mapImagery);
this._json.mapImagery = this._mapImagery.toJSON();
}
/** The Id of a [RenderTimeline]($backend) element containing a [[RenderSchedule.Script]] used to animate the view.
* If [[scheduleScriptProps]] is defined, it takes precedence over the script supplied by the RenderTimeline.
* @note If this [[DisplayStyleSettings]] is associated with a [DisplayStyleState]($frontend), assigning to [[renderTimeline]] will enqueue asynchronous loading of
* the script from the [RenderTimeline]($backend) element; for more readable code, prefer instead to `await` [DisplayStyleState.changeRenderTimeline]($frontend).
* @see [[onRenderTimelineChanged]] to be notified of changes to this property.
*/
public get renderTimeline(): Id64String | undefined {
return this._json.renderTimeline;
}
public set renderTimeline(id: Id64String | undefined) {
if (id !== this.renderTimeline) {
this.onRenderTimelineChanged.raiseEvent(id);
this._json.renderTimeline = id;
}
}
/** JSON representation of a [[RenderSchedule.Script]] embedded in the display style describing how to animate the contents of the view over time.
* This script, if present, takes precedence over a script supplied by [[renderTimeline]].
* @see [[onScheduleScriptPropsChanged]] to be notified when this property changes.
* @see [DisplayStyleState.scheduleScript]($frontend) to change the [[RenderSchedule.Script]] object directly rather than via JSON.
*/
public get scheduleScriptProps(): RenderSchedule.ScriptProps | undefined {
return this._json.scheduleScript;
}
public set scheduleScriptProps(props: RenderSchedule.ScriptProps | undefined) {
this.onScheduleScriptPropsChanged.raiseEvent(props);
this._json.scheduleScript = props;
}
/** The point in time currently reflected by the view, expressed in seconds in the [Unix epoch](https://en.wikipedia.org/wiki/Unix_time).
* This identifies a point on the timeline of the style's [[RenderSchedule.Script]], if any; it may also affect display of four-dimensional reality models.
* @see [[onTimePointChanged]] to be notified of changes to this property.
*/
public get timePoint(): number | undefined {
return this._json.timePoint;
}
public set timePoint(timePoint: number | undefined) {
if (timePoint !== this.timePoint) {
this.onTimePointChanged.raiseEvent(timePoint);
this._json.timePoint = timePoint;
}
}
/** Settings controlling the display of analytical models.
* @see [[analysisFraction]] to control playback of the animation.
*/
public get analysisStyle(): AnalysisStyle | undefined { return this._analysisStyle; }
public set analysisStyle(style: AnalysisStyle | undefined) {
if (style === this.analysisStyle)
return;
this.onAnalysisStyleChanged.raiseEvent(style);
this._analysisStyle = style;
if (style)
this._json.analysisStyle = style.toJSON();
else
delete this._json.analysisStyle;
}
/** A floating point value in [0..1] indicating the current point in animation of the [[analysisStyle]], where 0 corresponds to the beginning of
* the animation and 1 to the end. Default: 0.0.
*/
public get analysisFraction(): number {
const fraction = this._json.analysisFraction ?? 0;
return Math.max(0, Math.min(1, fraction));
}
public set analysisFraction(fraction: number) {
if (this.analysisFraction === fraction)
return;
this.onAnalysisFractionChanged.raiseEvent(fraction);
this._json.analysisFraction = Math.max(0, Math.min(1, fraction));
}
/** Settings controlling how white-on-white reversal is applied when [[ViewFlags.whiteOnWhiteReversal]] is enabled. */
public get whiteOnWhiteReversal(): WhiteOnWhiteReversalSettings { return this._whiteOnWhiteReversal; }
public set whiteOnWhiteReversal(settings: WhiteOnWhiteReversalSettings) {
if (settings.equals(this.whiteOnWhiteReversal))
return;
this.onWhiteOnWhiteReversalChanged.raiseEvent(settings);
this._whiteOnWhiteReversal = settings;
const json = settings.toJSON();
if (json)
this._json.whiteOnWhiteReversal = json;
else
delete this._json.whiteOnWhiteReversal;
}
/** Customize the way geometry belonging to a [[SubCategory]] is drawn by this display style.
* @param id The Id of the SubCategory whose appearance is to be overridden.
* @param ovr The overrides to apply to the [[SubCategoryAppearance]].
* @see [[dropSubCategoryOverride]]
*/
public overrideSubCategory(id: Id64String, ovr: SubCategoryOverride): void {
this.subCategoryOverrides.set(id, ovr);
}
/** Remove any [[SubCategoryOverride]] applied to a [[SubCategoryAppearance]] by this style.
* @param id The Id of the [[SubCategory]].
* @see [[overrideSubCategory]]
*/
public dropSubCategoryOverride(id: Id64String): void {
this.subCategoryOverrides.delete(id);
}
/** The overrides applied by this style. */
public get subCategoryOverrides(): Map<Id64String, SubCategoryOverride> {
return this._subCategoryOverrides;
}
/** Obtain the override applied to a [[SubCategoryAppearance]] by this style.
* @param id The Id of the [[SubCategory]].
* @returns The corresponding SubCategoryOverride, or undefined if the SubCategory's appearance is not overridden.
* @see [[overrideSubCategory]]
*/
public getSubCategoryOverride(id: Id64String): SubCategoryOverride | undefined {
return this.subCategoryOverrides.get(id);
}
/** Returns true if an [[SubCategoryOverride]]s are defined by this style. */
public get hasSubCategoryOverride(): boolean {
return this.subCategoryOverrides.size > 0;
}
/** Customize the way a [Model]($backend) is drawn by this display style.
* @param modelId The Id of the [Model]($backend) whose appearance is to be overridden.
* @param ovr The overrides to apply to the [Model]($backend) .
* @see [[dropModelAppearanceOverride]]
*/
public overrideModelAppearance(modelId: Id64String, ovr: FeatureAppearance): void {
this.modelAppearanceOverrides.set(modelId, ovr);
}
/** Remove any appearance overrides applied to a [Model]($backend) by this style.
* @param modelId The Id of the [Model]($backend) .
* @param ovr The overrides to apply to the [Model]($backend) .
* @see [[overrideModelAppearance]]
*/
public dropModelAppearanceOverride(id: Id64String): void {
this.modelAppearanceOverrides.delete(id);
}
/** The overrides applied by this style. */
public get modelAppearanceOverrides(): Map<Id64String, FeatureAppearance> {
return this._modelAppearanceOverrides;
}
/** Obtain the override applied to a [Model]($backend) by this style.
* @param id The Id of the [Model]($backend).
* @returns The corresponding FeatureAppearance, or undefined if the Model's appearance is not overridden.
* @see [[overrideModelAppearance]]
*/
public getModelAppearanceOverride(id: Id64String): FeatureAppearance | undefined {
return this.modelAppearanceOverrides.get(id);
}
/** Returns true if model appearance overrides are defined by this style. */
public get hasModelAppearanceOverride(): boolean {
return this.modelAppearanceOverrides.size > 0;
}
/** Get any settings that override how the reality model with the specified Id is displayed.
* @param modelId The Id of the [Model]($backend).
* @returns the display settings, or `undefined` if no settings have been associated with `modelId`.
* @see [[setRealityModelDisplaySettings]] to change the settings.
* @beta
*/
public getRealityModelDisplaySettings(modelId: Id64String): RealityModelDisplaySettings | undefined {
return this._realityModelDisplaySettings.get(modelId);
}
/** Change the settings that control how the reality model with the specified Id is displayed.
* @param modelId The Id of the [Model]($backend) to which the settings apply.
* @param settings The settings to apply to the model, or `undefined` to clear any previous settings for that model.
* @beta
*/
public setRealityModelDisplaySettings(modelId: Id64String, settings: RealityModelDisplaySettings | undefined): void {
if (settings)
this._realityModelDisplaySettings.set(modelId, settings);
else
this._realityModelDisplaySettings.delete(modelId);
}
/** The set of elements that will not be drawn by this display style.
* @returns An iterable over the elements' Ids.
*/
public get excludedElementIds(): OrderedId64Iterable {
return this._excludedElements;
}
/** @internal */
public get compressedExcludedElementIds(): CompressedId64Set {
return this._excludedElements.ids;
}
/** Add one or more elements to the set of elements not to be displayed.
* @param id The Ids of the element(s) to be excluded.
*/
public addExcludedElements(id: Id64String | Iterable<Id64String>) {
this._excludedElements.add("string" === typeof id ? [id] : id);
this.onExcludedElementsChanged.raiseEvent();
}
/** Remove an element from the set of elements not to be displayed. */
public dropExcludedElement(id: Id64String): void {
this._excludedElements.delete([id]);
this.onExcludedElementsChanged.raiseEvent();
}
/** Remove one or more elements from the set of elements not to be displayed.
* @param id The Ids of the element(s) to be removed from the set of excluded elements.
*/
public dropExcludedElements(id: Id64String | Iterable<Id64String>) {
this._excludedElements.delete("string" === typeof id ? [id] : id);
this.onExcludedElementsChanged.raiseEvent();
}
/** Remove all elements from the set of elements not to be displayed. */
public clearExcludedElements(): void {
this._excludedElements.reset(undefined);
this.onExcludedElementsChanged.raiseEvent();
}
/** The style applied to the view's [ClipVector]($core-geometry). */
public get clipStyle(): ClipStyle {
return this._clipStyle;
}
public set clipStyle(style: ClipStyle) {
this.onClipStyleChanged.raiseEvent(style);
this._clipStyle = style;
if (style.matchesDefaults)
delete this._json.clipStyle;
else
this._json.clipStyle = style.toJSON();
}
/** Convert these settings to their JSON representation. */
public toJSON(): DisplayStyleSettingsProps {
return this._json;
}
/** Serialize a subset of these settings to JSON, such that they can be applied to another DisplayStyleSettings to selectively override those settings.
* @param options Specifies which settings should be serialized. By default, settings that are specific to an iModel (e.g., subcategory overrides) or iTwin (e.g., context reality models)
* are omitted, as are drawing aids (e.g., ACS triad and grid).
* @returns a JSON representation of the selected settings suitable for passing to [[applyOverrides]].
* @see [[applyOverrides]] to apply the overrides to another DisplayStyleSettings..
*/
public toOverrides(options?: DisplayStyleOverridesOptions): DisplayStyleSettingsProps {
if (options?.includeAll) {
return {
...this.toJSON(),
viewflags: this.viewFlags.toFullyDefinedJSON(),
};
}
const viewflags: Partial<ViewFlagProps> = this.viewFlags.toFullyDefinedJSON();
const props: DisplayStyleSettingsProps = {
viewflags,
backgroundColor: this.backgroundColor.toJSON(),
monochromeColor: this.monochromeColor.toJSON(),
monochromeMode: this.monochromeMode,
whiteOnWhiteReversal: this.whiteOnWhiteReversal.toJSON() ?? { ignoreBackgroundColor: false },
};
if (options?.includeBackgroundMap) {
props.backgroundMap = this.backgroundMap.toPersistentJSON();
props.mapImagery = this.mapImagery.toJSON();
} else {
delete viewflags.backgroundMap;
}
if (!options?.includeDrawingAids) {
delete viewflags.acs;
delete viewflags.grid;
}
if (options?.includeITwinSpecific || options?.includeIModelSpecific) {
props.timePoint = this.timePoint;
if (this._json.contextRealityModels) {
props.contextRealityModels = this._json.contextRealityModels;
if (!options?.includeIModelSpecific)
for (const model of this._json.contextRealityModels)
delete model.classifiers;
}
}
if (options?.includeIModelSpecific) {
if (this.analysisStyle) {
props.analysisStyle = this.analysisStyle.toJSON();
props.analysisFraction = this.analysisFraction;
}
if (this.scheduleScriptProps)
props.scheduleScript = [...this.scheduleScriptProps];
if (this.renderTimeline)
props.renderTimeline = this.renderTimeline;
props.subCategoryOvr = this._json.subCategoryOvr ? [...this._json.subCategoryOvr] : [];
props.modelOvr = this._json.modelOvr ? [...this._json.modelOvr] : [];
props.excludedElements = this._excludedElements.ids;
}
return props;
}
/** Selectively override some of these settings. Any field that is explicitly defined by the input will be overridden in these settings; any fields left undefined in the input
* will retain their current values in these settings. The input's [[ViewFlags]] are applied individually - only those flags that are explicitly defined will be overridden.
* For example, the following overrides will set the render mode to "smooth", change the background color to white, turn shadows off, and leave all other settings intact:
* ```ts
* {
* viewflags: {
* renderMode: RenderMode.SmoothShade,
* shadows: false,
* },
* backgroundColor: ColorByName.white,
* }
* ```
* @see [[toOverrides]] to produce overrides from an existing DisplayStyleSettings.
*/
public applyOverrides(overrides: DisplayStyleSettingsProps): void {
this._applyOverrides(overrides);
this.onOverridesApplied.raiseEvent(overrides);
}
/** @internal */
protected _applyOverrides(overrides: DisplayStyleSettingsProps): void {
this.onApplyOverrides.raiseEvent(overrides);
if (overrides.viewflags) {
this.viewFlags = ViewFlags.fromJSON({