Skip to content

Commit

Permalink
bring state back
Browse files Browse the repository at this point in the history
  • Loading branch information
alxart committed Feb 28, 2024
1 parent 6b280a9 commit 3743694
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.
5 changes: 3 additions & 2 deletions demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ async function start() {
.getContainer()
.addEventListener('click', () => {
plugin.removeRealtyScene();
// plugin.addRealtyScene(REALTY_SCENE, '235034');
plugin.addRealtyScene(REALTY_SCENE);
});

Expand All @@ -64,7 +63,9 @@ async function start() {
.getContainer()
.addEventListener('click', () => {
plugin.removeRealtyScene();
plugin.addRealtyScene(REALTY_SCENE_1, 'ds321ba234cb');
plugin.addRealtyScene(REALTY_SCENE_1, {
buildingId: 'ds321ba234cb',
});
});

new mapglAPI.Control(map, '<button>Remove Scene 1</button>', {
Expand Down
2 changes: 1 addition & 1 deletion src/control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface ControlShowOptions {
* Event that emitted on button presses of the control
*/
export interface FloorChangeEvent {
modelId: Id;
modelId: Id; // id модели этажа или здания
}

export interface ControlEventTable {
Expand Down
6 changes: 3 additions & 3 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Map as MapGL } from '@2gis/mapgl/types';
import type { BuildingOptions } from './types/realtyScene';
import type { GltfPluginEventTable } from './types/events';
import type { Id, PluginOptions, ModelOptions } from './types/plugin';
import type { Id, PluginOptions, ModelOptions, BuildingState } from './types/plugin';

import { applyOptionalDefaults } from './utils/common';
import { Evented } from './external/evented';
Expand Down Expand Up @@ -187,9 +187,9 @@ export class GltfPlugin extends Evented<GltfPluginEventTable> {
ids.forEach((id) => this.hideModel(id));
}

public async addRealtyScene(scene: BuildingOptions[], activeModelId?: Id) {
public async addRealtyScene(scene: BuildingOptions[], state?: BuildingState) {
this.realtyScene = new RealtyScene(this, this.map, this.options);
return this.realtyScene.init(scene, activeModelId);
return this.realtyScene.init(scene, state);
}

// public showRealtyScene() {}
Expand Down
18 changes: 10 additions & 8 deletions src/realtyScene/realtyScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GltfPlugin } from '../plugin';
import { GltfFloorControl } from '../control';
import classes from './realtyScene.module.css';

import type { Id, ModelOptions, PluginOptions } from '../types/plugin';
import type { BuildingState, Id, ModelOptions, PluginOptions } from '../types/plugin';
import type {
BuildingOptions,
MapOptions,
Expand Down Expand Up @@ -143,7 +143,14 @@ export class RealtyScene {
this.state = newState;
}

public async init(scene: BuildingOptions[], activeModelId?: string) {
public async init(scene: BuildingOptions[], state?: BuildingState) {
// Приводим стейт пользователя к внутреннему виду id
let activeModelId: Id | undefined = state
? state.floorId
? getFloorModelId(state.buildingId, state.floorId)
: state.buildingId
: undefined;

scene.forEach((building) => {
const { floors, ...buildingPart } = building;
const internalBuilding: BuildingOptionsInternal = {
Expand All @@ -160,11 +167,6 @@ export class RealtyScene {
icon: floor.icon,
});

// Подменяем id активного этажа на внутренний формат buildingId_floorId
if (activeModelId === floor.id) {
activeModelId = floorModelId;
}

this.floors.set(floorModelId, {
...floor,
buildingOptions: buildingOptions,
Expand All @@ -178,7 +180,7 @@ export class RealtyScene {
this.buildings.set(building.modelId, internalBuilding);
});

// Оставляем только существующее значение из переданных modelId
// Оставляем только существующее значение из переданных modelId в scene
activeModelId =
activeModelId !== undefined &&
(this.buildings.has(activeModelId) || this.floors.has(activeModelId))
Expand Down
2 changes: 1 addition & 1 deletion src/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface ModelTarget {
data: ModelOptions;

/**
* Identifier of the building's model
* Identifier of the building's or floor's model
*/
modelId: Id;
}
Expand Down
15 changes: 15 additions & 0 deletions src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@ export interface PluginOptions {
groundCoveringColor?: string;
}

/**
* State for the building's scene
*/
export interface BuildingState {
/**
* Identifier of the building's model
*/
buildingId: string;

/**
* Identifier of the floor's model
*/
floorId?: string;
}

/**
* Options for a model
*/
Expand Down

0 comments on commit 3743694

Please sign in to comment.