Skip to content

Commit

Permalink
added show and hide realty scene methods
Browse files Browse the repository at this point in the history
  • Loading branch information
alxart committed Apr 21, 2024
1 parent 5cebf9a commit ad77087
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 31 deletions.
14 changes: 14 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,20 @@ export class GltfPlugin extends Evented<GltfPluginEventTable> {
return this.realtyScene.init(scene, state);
}

/**
* Shows a hidden realty scene on the map.
*/
public showRealtyScene() {
this.realtyScene?.show();
}

/**
* Hides a shown realty scene on the map.
*/
public hideRealtyScene() {
this.realtyScene?.hide();
}

/**
* Removes an interactive realty scene from the map.
*/
Expand Down
113 changes: 82 additions & 31 deletions src/realtyScene/realtyScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export class RealtyScene {
private state: RealtySceneState = {
activeModelId: undefined,
buildingVisibility: new Map(),
status: 'visible',
};
private isDestroyed = false;

private groundCoveringSource: GeoJsonSource;
private control: GltfFloorControl;
Expand Down Expand Up @@ -83,6 +83,15 @@ export class RealtyScene {
}

private setState(newState: RealtySceneState) {
if (this.state.status === 'destroyed') {
return;
}

if (newState.status === 'destroyed') {
this.state = newState;
return;
}

const prevState = this.state;

// т.к. стейт может меняться асинхронно и иногда нужно показывать
Expand All @@ -95,7 +104,10 @@ export class RealtyScene {
const newModelOptions = newState.buildingVisibility.get(buildingId);

// если опции не изменились, то ничего не делаем
if (prevModelOptions?.modelId === newModelOptions?.modelId) {
if (
prevModelOptions?.modelId === newModelOptions?.modelId &&
prevState.status === newState.status
) {
buildingVisibility.set(buildingId, prevModelOptions);
return;
}
Expand Down Expand Up @@ -135,40 +147,43 @@ export class RealtyScene {

// если новая модель готова, то показываем ее
if (modelStatus === ModelStatus.Loaded) {
this.plugin.showModel(newModelOptions.modelId);
buildingVisibility.set(buildingId, newModelOptions);

// если модель активна, то применяем опции карты и включаем подложку, если нужно
if (
newState.activeModelId !== undefined &&
newState.activeModelId === newModelOptions.modelId
) {
const options =
this.buildings.get(newModelOptions.modelId) ??
this.floors.get(newModelOptions.modelId);

if (options) {
this.setMapOptions(options.mapOptions);
}
if (newState.status === 'visible') {
this.plugin.showModel(newModelOptions.modelId);

if (this.undergroundFloors.has(newModelOptions.modelId)) {
this.switchOnGroundCovering();
}
// если модель активна, то применяем опции карты и включаем подложку, если нужно
if (
newState.activeModelId !== undefined &&
newState.activeModelId === newModelOptions.modelId
) {
const options =
this.buildings.get(newModelOptions.modelId) ??
this.floors.get(newModelOptions.modelId);

const floorOptions = this.floors.get(newModelOptions.modelId);
if (floorOptions) {
floorOptions.labelGroups?.forEach((group) => {
this.plugin.addLabelGroup(group, {
buildingId,
floorId: floorOptions.id,
if (options) {
this.setMapOptions(options.mapOptions);
}

if (this.undergroundFloors.has(newModelOptions.modelId)) {
this.switchOnGroundCovering();
}

const floorOptions = this.floors.get(newModelOptions.modelId);
if (floorOptions) {
floorOptions.labelGroups?.forEach((group) => {
this.plugin.addLabelGroup(group, {
buildingId,
floorId: floorOptions.id,
});
});
});
}
}
}
} else {
if (modelStatus === ModelStatus.NoModel) {
this.plugin.addModel(newModelOptions, true).then(() => {
if (this.isDestroyed) {
if (this.state.status === 'destroyed') {
return;
}

Expand All @@ -177,7 +192,10 @@ export class RealtyScene {
}

// откладываем выставление нужного стейта до момента загрузки модели
this.setState(newState);
this.setState({
...newState,
status: this.state.status,
});
});
}

Expand All @@ -191,8 +209,10 @@ export class RealtyScene {
const prevBuildingModelId = this.getBuildingModelId(prevState.activeModelId);
const newBuildingModelId = this.getBuildingModelId(newState.activeModelId);

if (prevBuildingModelId !== newBuildingModelId) {
if (newBuildingModelId !== undefined && newState.activeModelId !== undefined) {
if (prevBuildingModelId !== newBuildingModelId || prevState.status !== newState.status) {
if (newState.status === 'hidden') {
this.control.hide();
} else if (newBuildingModelId !== undefined && newState.activeModelId !== undefined) {
const buildingOptions = this.buildings.get(newBuildingModelId);
if (buildingOptions) {
this.control.show({
Expand All @@ -214,6 +234,7 @@ export class RealtyScene {
this.state = {
buildingVisibility,
activeModelId: newState.activeModelId,
status: newState.status,
};
}

Expand Down Expand Up @@ -297,13 +318,14 @@ export class RealtyScene {
return this.plugin
.addModels(Array.from(modelsToLoad.values()), Array.from(buildingVisibility.keys()))
.then(() => {
if (this.isDestroyed) {
if (this.state.status === 'destroyed') {
return;
}

this.setState({
activeModelId,
buildingVisibility,
status: this.state.status,
});

this.plugin.on('click', this.onSceneClick);
Expand All @@ -323,8 +345,34 @@ export class RealtyScene {
}
}

public show() {
if (this.state.status !== 'hidden') {
return;
}

this.setState({
...this.state,
status: 'visible',
});
}

public hide() {
if (this.state.status !== 'visible') {
return;
}

this.setState({
...this.state,
status: 'hidden',
});
}

public destroy() {
this.isDestroyed = true;
if (this.state.status === 'destroyed') {
return;
}

this.setState({ ...this.state, status: 'destroyed' });
this.map.off('styleload', this.onStyleLoad);
this.plugin.off('click', this.onSceneClick);
this.plugin.off('mouseover', this.onSceneMouseOver);
Expand Down Expand Up @@ -433,6 +481,7 @@ export class RealtyScene {
this.setState({
activeModelId: ev.modelId,
buildingVisibility,
status: this.state.status,
});
return;
}
Expand All @@ -449,6 +498,7 @@ export class RealtyScene {
this.setState({
activeModelId: ev.modelId,
buildingVisibility,
status: this.state.status,
});
return;
}
Expand Down Expand Up @@ -486,6 +536,7 @@ export class RealtyScene {
this.setState({
buildingVisibility,
activeModelId,
status: this.state.status,
});
};

Expand Down
3 changes: 3 additions & 0 deletions src/types/realtyScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ export interface RealtySceneState {

// id здания мапится на опции здания или опции этажа этого здания
buildingVisibility: Map<string, ModelOptions | undefined>;

// статус сцены недвижимости
status: 'visible' | 'hidden' | 'destroyed';
}

/**
Expand Down

0 comments on commit ad77087

Please sign in to comment.