Skip to content

Commit

Permalink
LayerStyle refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Deubler <[email protected]>
  • Loading branch information
TerminalTim committed Jul 12, 2024
1 parent 4fdf983 commit 111aa34
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
20 changes: 10 additions & 10 deletions packages/core/src/layers/TileLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class TileLayer extends Layer {

private _fp: FeatureProvider;

private _sd: XYZLayerStyle = null;
private _styleManager: XYZLayerStyle = null;
// pointer events active
private _pev = true;

Expand Down Expand Up @@ -345,10 +345,10 @@ export class TileLayer extends Layer {
setStyleGroup(feature: Feature, styleGroup?: Style[] | false | null): void;

setStyleGroup(feature, styleGroup?, merge?) {
if (this._sd) {
if (this._styleManager) {
this.dispatchEvent(STYLEGROUP_CHANGE_EVENT, {
feature,
styleGroup: this._sd.setStyleGroup(feature, styleGroup, merge)
styleGroup: this._styleManager.setStyleGroup(feature, styleGroup, merge)
});
}
};
Expand All @@ -361,12 +361,12 @@ export class TileLayer extends Layer {
*
*/
getStyleGroup(feature: Feature, zoomlevel?: number, layerDefault?: boolean): readonly Style[] {
return this._sd?.getStyleGroup(feature, zoomlevel, layerDefault);
return this._styleManager?.getStyleGroup(feature, zoomlevel, layerDefault);
};


_getCustomStyleGroup(feature: Feature): Style[] {
return this._sd?.getCustomStyleGroup(feature);
return this._styleManager?.getCustomStyleGroup(feature);
}

/**
Expand Down Expand Up @@ -707,7 +707,7 @@ export class TileLayer extends Layer {
* @param keepCustom - keep and reuse custom set feature styles that have been set via layer.setStyleGroup(...)
*/
setStyle(layerStyle: LayerStyle| XYZLayerStyle, keepCustom: boolean = false) {
const _customFeatureStyles = keepCustom && this._sd?.getCustomStyles();
const _customFeatureStyles = keepCustom && this._styleManager?.getCustomStyles();
// const isFnc = (fnc) => typeof fnc == 'function';
// if (!isFnc(layerStyle.getStyleGroup) || !isFnc(layerStyle.setStyleGroup)) {
if (!(layerStyle instanceof XYZLayerStyle)) {
Expand All @@ -716,20 +716,20 @@ export class TileLayer extends Layer {

(layerStyle as XYZLayerStyle).init?.(this, _customFeatureStyles);

this._sd = layerStyle as XYZLayerStyle;
this._styleManager = layerStyle as XYZLayerStyle;

this.dispatchEvent(STYLE_CHANGE_EVENT, {style: layerStyle});
};


getStyleManager(): XYZLayerStyle {
return this._sd;
return this._styleManager;
};
/**
* Get the current layerStyle.
*/
getStyle(): LayerStyle {
return this._sd;
return this._styleManager.getLayerStyle();
};

getMargin() {
Expand Down Expand Up @@ -815,7 +815,7 @@ export class TileLayer extends Layer {
};

getStyleDefinitions(): LayerStyle['definitions'] {
return this._sd?.getDefinitions();
return this._styleManager?.getDefinitions();
}
}

Expand Down
16 changes: 12 additions & 4 deletions packages/core/src/styles/XYZLayerStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,19 @@ export class XYZLayerStyle implements LayerStyle {
private flatStyles?: Style[];
private _filteredStyleGrp: StyleGroup;

constructor(styleCfg) {
for (let p in styleCfg) {
const property = styleCfg[p];
private _style: LayerStyle;

constructor(styleJSON: LayerStyle) {
for (let p in styleJSON) {
const property = styleJSON[p];
this[p] = p == 'styleGroups' ? deepCopy(property) : property;
}
// layerStyle._l = layer;
this.definitions ||= {};

this.expParser = new ExpressionParser(this.definitions, this.exprContext);

if (!styleCfg.assign) {
if (!styleJSON.assign) {
const flatStyles = [];
for (let name in this.styleGroups) {
let styleGrp = this.styleGroups[name];
Expand All @@ -122,6 +124,8 @@ export class XYZLayerStyle implements LayerStyle {

this._filteredStyleGrp = [];
}

this._style = styleJSON;
}

private createExpEvaluator(expr: JSONExpression) {
Expand Down Expand Up @@ -286,4 +290,8 @@ export class XYZLayerStyle implements LayerStyle {
clearCache() {
this.expParser.clearCache?.();
}

getLayerStyle(): LayerStyle {
return this._style;
}
}
5 changes: 4 additions & 1 deletion packages/display/src/displays/BasicDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ abstract class Display {

'styleChange': (ev) => {
const {layer, style} = ev.detail;
const index = display.layers.indexOf(layer);
// const index = display.layers.indexOf(layer);
const displayLayer = display.layers.get(layer);
const {index} = displayLayer;
displayLayer.initStyle();
display.setLayerBgColor(style, display.layers[index]);
display.buckets.tiles.forEach((t) => t.clear(index));
}
Expand Down
3 changes: 3 additions & 0 deletions packages/display/src/displays/Layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ class Layer {
this.tileSize = (<TileLayer>layer).tileSize || null;
this.layers = layers;
this.id = Math.floor(Math.random() * 1e16);
this.initStyle();
}

initStyle() {
this.expParser = (this.layer as TileLayer).getStyleManager?.().getExpressionParser?.() as StyleExpressionParser;
}

Expand Down

0 comments on commit 111aa34

Please sign in to comment.