驾驶台--状态数据结构和交互调整

This commit is contained in:
joylink_zhaoerwei 2024-03-19 17:47:35 +08:00
parent 9261a95f78
commit 22d3a1794a
5 changed files with 104 additions and 91 deletions

View File

@ -6,7 +6,11 @@ import { useLineStore } from 'src/stores/line-store';
import { DisplayObject, FederatedMouseEvent } from 'pixi.js';
import { useTccStore } from 'src/stores/tcc-store';
import { state } from 'src/protos/device_state';
import { ITccButtonData, ITccButtonState, TccButton } from 'src/graphics/tccButton/TccButton';
import {
ITccButtonData,
ITccButtonState,
TccButton,
} from 'src/graphics/tccButton/TccButton';
export class TccButtonData extends GraphicDataBase implements ITccButtonData {
constructor(data?: tccGraphicData.TccButton) {
@ -51,12 +55,12 @@ export class TccButtonState
extends GraphicStateBase
implements ITccButtonState
{
constructor(proto?: state.ButtonState) {
constructor(proto?: state.TrainControlState.EmergentButton) {
let states;
if (proto) {
states = proto;
} else {
states = new state.ButtonState();
states = new state.TrainControlState.EmergentButton();
}
super(states, TccButton.Type);
}
@ -64,13 +68,13 @@ export class TccButtonState
return this.states.id + '';
}
get down(): boolean {
return this.states.down;
return this.states.passed;
}
set down(v: boolean) {
this.states.down = v;
this.states.passed = v;
}
get states(): state.ButtonState {
return this.getState<state.ButtonState>();
get states(): state.TrainControlState.EmergentButton {
return this.getState<state.TrainControlState.EmergentButton>();
}
clone(): TccButtonState {
return new TccButtonState(this.states.cloneMessage());
@ -97,47 +101,22 @@ export class TccButtonOperateInteraction extends GraphicInteractionPlugin<TccBut
.map((g) => g as TccButton);
}
bind(g: TccButton): void {
g.eventMode = 'static';
g.cursor = 'pointer';
g.on('mousedown', this.onMouseDown, this);
g.on('mouseup', this.onMouseUp, this);
g.on('mouseout', this.onMouseOut, this);
g._tccButton.eventMode = 'static';
g._tccButton.cursor = 'pointer';
g._tccButton.on('_leftclick', this.onClick);
}
unbind(g: TccButton): void {
g.eventMode = 'none';
g.off('mousedown', this.onMouseDown, this);
g.on('mouseup', this.onMouseUp, this);
g.on('mouseout', this.onMouseOut, this);
g._tccButton.eventMode = 'none';
g._tccButton.off('_leftclick', this.onClick);
}
onMouseOut(e: FederatedMouseEvent) {
onClick(e: FederatedMouseEvent): void {
const target = e.target as DisplayObject;
const tccButton = target.getGraphic() as TccButton;
if (tccButton.states.down && tccButton.datas.isSelfReset) {
tccButton.states.down = false;
tccButton.doRepaint();
}
}
onMouseDown(e: FederatedMouseEvent) {
const simulationId = useLineStore().simulationId;
const mapId = useLineStore().mapId;
const tccId = useTccStore().tccId;
const target = e.target as DisplayObject;
const tccButton = target.getGraphic() as TccButton;
if (!simulationId || !mapId) {
return;
}
console.log('按钮按下');
}
onMouseUp(e: FederatedMouseEvent) {
const simulationId = useLineStore().simulationId;
const mapId = useLineStore().mapId;
const tccId = useTccStore().tccId;
const target = e.target as DisplayObject;
const tccButton = target.getGraphic() as TccButton;
if (!simulationId || !mapId || !tccButton.datas.isSelfReset) {
return;
}
console.log('按钮弹起');
const tccButton = target.getGraphic<TccButton>();
if (!tccButton) return;
tccButton.states.down = !tccButton.states.down;
tccButton.doRepaint();
console.log(tccButton.states.down);
}
}

View File

@ -49,26 +49,26 @@ export class TccHandleState
extends GraphicStateBase
implements ITccHandleState
{
constructor(data?: state.KeyState) {
constructor(data?: state.TrainControlState.PushHandler) {
let tccHandleState;
if (data) {
tccHandleState = data;
} else {
tccHandleState = new state.KeyState();
tccHandleState = new state.TrainControlState.PushHandler();
}
super(tccHandleState, TccHandle.Type);
}
get code(): string {
return this.states.id + '';
}
get states(): state.KeyState {
return this.getState<state.KeyState>();
get states(): state.TrainControlState.PushHandler {
return this.getState<state.TrainControlState.PushHandler>();
}
get gear(): number {
return this.states.gear;
return this.states.val;
}
set gear(v: number) {
this.states.gear = v;
this.states.val = v;
}
clone(): TccHandleState {
return new TccHandleState(this.states.cloneMessage());
@ -97,7 +97,7 @@ export class TccHandleInteraction extends GraphicInteractionPlugin<TccHandle> {
}
bind(g: TccHandle): void {
g._tccHandle.eventMode = 'static';
g._tccHandle.cursor = 'pointer';
g._tccHandle.cursor = 'Move';
g._tccHandle.onmousedown = (e) => {
this.onMouseDown(e);
};

View File

@ -48,26 +48,26 @@ export class TccKeyData extends GraphicDataBase implements ITccKeyData {
}
export class TccKeyState extends GraphicStateBase implements ITccKeyState {
constructor(data?: state.KeyState) {
constructor(data?: state.TrainControlState.DirectionKeySwitch) {
let tccKeyState;
if (data) {
tccKeyState = data;
} else {
tccKeyState = new state.KeyState();
tccKeyState = new state.TrainControlState.DirectionKeySwitch();
}
super(tccKeyState, TccKey.Type);
}
get code(): string {
return this.states.id + '';
}
get states(): state.KeyState {
return this.getState<state.KeyState>();
get states(): state.TrainControlState.DirectionKeySwitch {
return this.getState<state.TrainControlState.DirectionKeySwitch>();
}
get gear(): number {
return this.states.gear;
get position(): number {
return this.states.val;
}
set gear(v: number) {
this.states.gear = v;
set position(v: number) {
this.states.val = v;
}
clone(): TccKeyState {
return new TccKeyState(this.states.cloneMessage());
@ -83,6 +83,7 @@ export class TccKeyState extends GraphicStateBase implements ITccKeyState {
export class TccKeyInteraction extends GraphicInteractionPlugin<TccKey> {
static Name = 'TccKeyInteraction';
isMouseDown = false;
changeOnce = false;
mouseDownBeginPos = new Point();
constructor(app: IGraphicScene) {
super(TccKeyInteraction.Name, app);
@ -96,27 +97,48 @@ export class TccKeyInteraction extends GraphicInteractionPlugin<TccKey> {
bind(g: TccKey): void {
g._tccKey.eventMode = 'static';
g._tccKey.cursor = 'pointer';
g._tccKey.onmousedown = (e) => {
this.onMouseDown(e);
};
g._tccKey.onmouseup = () => {
this.isMouseDown = false;
};
g.onmousemove = (e) => {
this.onMousemove(e);
};
g.onmouseleave = () => {
this.isMouseDown = false;
};
if (
g.datas.type ==
tccGraphicData.TccKey.TccKeyType.driverControllerActivationClint
) {
g._tccKey.on('_leftclick', this.onClick);
} else {
g._tccKey.onmousedown = (e) => {
this.onMouseDown(e);
};
g._tccKey.onmouseup = () => {
this.isMouseDown = false;
this.changeOnce = false;
};
g.onmousemove = (e) => {
this.onMousemove(e);
};
g.onmouseleave = () => {
this.isMouseDown = false;
this.changeOnce = false;
};
}
}
unbind(g: TccKey): void {
g.eventMode = 'none';
g.off('_leftclick', this.onClick);
g._tccKey.eventMode = 'none';
if (
g.datas.type ==
tccGraphicData.TccKey.TccKeyType.driverControllerActivationClint
) {
g._tccKey.off('_leftclick', this.onClick);
} else {
g._tccKey.onmousedown = null;
g._tccKey.onmouseup = null;
g.onmousemove = null;
g.onmouseleave = null;
}
}
onClick(e: FederatedMouseEvent): void {
const g = e.target as TccKey;
/* const changeState = useTccStore().tccKeyOperation;
changeState(g.datas.id, g.state.gear === 0 ? 1 : 0); */
const target = e.target as DisplayObject;
const tccKey = target.getGraphic<TccKey>();
if (!tccKey) return;
tccKey.state.position = tccKey?.state.position == 0 ? 1 : 0;
tccKey.doRepaint();
}
onMouseDown(e: FederatedMouseEvent) {
const target = e.target as DisplayObject;
@ -144,17 +166,19 @@ export class TccKeyInteraction extends GraphicInteractionPlugin<TccKey> {
);
if (
direction == 'ssz' &&
angle > 45 &&
((angle > 45 && !this.changeOnce) || (angle > 90 && this.changeOnce)) &&
(tccKey._tccKey.rotation == -Math.PI / 4 ||
tccKey._tccKey.rotation == 0)
) {
this.changeOnce = true;
tccKey._tccKey.rotation += Math.PI / 4;
}
if (
direction == 'nsz' &&
angle > 45 &&
((angle > 45 && !this.changeOnce) || (angle > 90 && this.changeOnce)) &&
(tccKey._tccKey.rotation == Math.PI / 4 || tccKey._tccKey.rotation == 0)
) {
this.changeOnce = true;
tccKey._tccKey.rotation -= Math.PI / 4;
}
}

View File

@ -65,19 +65,29 @@ function handleSubscribe(tccScene: IGraphicScene) {
app.subscribe({
destination: `/rtsts/simulation/${simulationId}/train/control/${tccId}`,
messageConverter: (message: Uint8Array) => {
console.log('收到消息', message);
const states: GraphicState[] = [];
const storage = state.PushedDevicesStatus.deserialize(message);
if (storage.all) {
// storage.allStatus.buttonState.forEach((item) => {
// if (item.id) {
// states.push(new TccButtonState(item));
// }
// });
const storage = state.TrainControlState.deserialize(message);
if (storage.ebutton) {
states.push(new TccButtonState(storage.ebutton));
}
if (storage.dirKey) {
states.push(new TccKeyState(storage.dirKey));
}
storage?.driverKey.forEach((driverKey) => {
const data = new state.TrainControlState.DirectionKeySwitch({
id: driverKey.id,
val: driverKey.val ? 1 : 0,
});
states.push(new TccKeyState(data));
});
if (storage.pushHandler) {
states.push(new TccHandleState(storage.pushHandler));
}
// console.log(states, 'states');
return states;
},
graphicQueryer: (state, store) => {
return store.queryById(+state.code);
},
});
}

View File

@ -24,8 +24,8 @@ export interface ITccKeyData extends GraphicData {
}
export interface ITccKeyState extends GraphicState {
get gear(): number;
set gear(v: number);
get position(): number;
set position(v: number);
}
export class TccKey extends JlGraphic {
@ -58,13 +58,13 @@ export class TccKey extends JlGraphic {
return this.getStates<ITccKeyState>();
}
doRepaint(): void {
//this._tccKey.rotation = (-Math.PI / 2) * this.state.gear;
if (
this.datas.type ==
tccGraphicData.TccKey.TccKeyType.driverControllerActivationClint
) {
this._tccKey.texture = this.tccKeyTextures.tccKey;
this._tccKey.rotation = Math.PI / 4;
this._tccKey.rotation =
this.state.position == 0 ? Math.PI / 4 : -Math.PI / 4;
} else {
this._tccKey.texture = this.tccKeyTextures.tccKnob;
this._tccKey.scale.set(0.6);