Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions src/MPQEditor/MPQCircleSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export type MPQSelectionCmdOpenArgs = {
panel: vscode.WebviewPanel;
};

export type MPQVisqData = {
visqPath: string;
};

export type MPQSelectionCmdCloseArgs = {
modelPath: string;
document: vscode.TextDocument;
Expand All @@ -55,6 +59,7 @@ export class MPQSelectionPanel
public static readonly cmdOpen = "one.viewer.mpq.openGraphSelector";
public static readonly cmdClose = "one.viewer.mpq.closeGraphSelector";
public static readonly cmdChanged = "one.viewer.mpq.layersChangedByOwner";
public static readonly cmdOpenVisq = "one.viewer.mpq.loadVisq";

public static panels: MPQSelectionPanel[] = [];

Expand All @@ -66,14 +71,20 @@ export class MPQSelectionPanel
private _modelPath: string; // circle file path
private _mpqEventHandler?: MPQSelectionEvent;
private _lastSelected: string[];
private _visqData: string[];
private _closedByOwner: boolean = false;

public static register(context: vscode.ExtensionContext): void {
const registrations = [
vscode.commands.registerCommand(
MPQSelectionPanel.cmdOpen,
(args: MPQSelectionCmdOpenArgs, handler: MPQSelectionEvent) => {
MPQSelectionPanel.createOrShow(context.extensionUri, args, handler);
MPQSelectionPanel.createOrShow(
context.extensionUri,
args,
"",
handler
);
}
),
vscode.commands.registerCommand(
Expand All @@ -88,6 +99,21 @@ export class MPQSelectionPanel
MPQSelectionPanel.forwardSelectionByOwner(context.extensionUri, args);
}
),
vscode.commands.registerCommand(
MPQSelectionPanel.cmdOpenVisq,
(
args: MPQSelectionCmdOpenArgs,
visqData: MPQVisqData,
handler: MPQSelectionEvent
) => {
MPQSelectionPanel.createOrShow(
context.extensionUri,
args,
visqData.visqPath,
handler
);
}
),
// TODO add more commands
];

Expand All @@ -99,6 +125,7 @@ export class MPQSelectionPanel
public static createOrShow(
extensionUri: vscode.Uri,
args: MPQSelectionCmdOpenArgs,
visqPath: string,
handler: MPQSelectionEvent | undefined
) {
let column = args.panel.viewColumn;
Expand Down Expand Up @@ -132,6 +159,7 @@ export class MPQSelectionPanel
panel,
extensionUri,
args,
visqPath,
handler
);

Expand Down Expand Up @@ -190,6 +218,7 @@ export class MPQSelectionPanel
panel: vscode.WebviewPanel,
extensionUri: vscode.Uri,
args: MPQSelectionCmdOpenArgs,
visqPath: string,
handler: MPQSelectionEvent | undefined
) {
super(extensionUri, panel.webview);
Expand All @@ -201,13 +230,31 @@ export class MPQSelectionPanel
this._modelPath = args.modelPath;
this._mpqEventHandler = handler;
this._lastSelected = args.names;
this._visqData = [];

// Listen for when the panel is disposed
// This happens when the user closes the panel or when the panel is closed programmatically
this._panel.onDidDispose(() => this.dispose(), null, this._disposables);

this.initGraphCtrl(this._modelPath, this);
this.setMode("selector");
if (visqPath.length < 1) {
this.setMode("selector");
} else {
this.setMode("visqselector");
const visqUri = vscode.Uri.file(visqPath);
vscode.workspace.fs.readFile(visqUri).then((visqData) => {
try {
this._visqData = JSON.parse(visqData.toString());
} catch (error) {
this.onInvalidVISQData(visqPath);
}

// check whether _visqData pretend to be valid
if (!("error" in this._visqData) || !("meta" in this._visqData)) {
this.onInvalidVISQData(visqPath);
Copy link
Contributor

@dayo09 dayo09 May 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q. What to you mean by 'pretend to be valid' here..?
This condition says that there is one "error" or one "meta" or none of them. It only excludes the case that both "error" and "meta" inside the json, so...?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition says, that _visqData should contain error section and meta section, otherwise (doesn't contain error or doesn't contain meta) it is treated as invalid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition was meant to prevent user from loading other file, so it is very simple.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dayo09
The simplest form for _visqData to be valid is that it contains both "error" and "meta", otherwise it can be treated as invalid.
So negation of

("error" in _visqData) and ("meta" in _visqData)

gives us

("eror" isn't in _visqData) or ("meta" isn't in _visqData)

i.e. if _visqData doesn't contain either "error" or "meta" it can be treated as invalid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dayo09
Do i need to remove this condition for now?

Copy link
Contributor

@dayo09 dayo09 May 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha... I didn't know that. What I thought was: if it includes 'error', than isn't it 'invalid' itself?

The simplest form for _visqData to be valid is that it contains both "error" and "meta"

If so, I think if you make a function that describes the assumption, it will be easy to understand.!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha... I didn't know that. What I thought was: if it includes 'error', than isn't it 'invalid' itself?

No. It's valid. Valid visq.json file should contain error along with meta section at least.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry that now I understand it. So 'Error' is the error ratio. I thought it's a problematic log. Thanks for the explanation.

}
});
}
if (this._mpqEventHandler) {
this._mpqEventHandler.onOpened(this._ownerPanel);
}
Expand Down Expand Up @@ -256,6 +303,9 @@ export class MPQSelectionPanel
case MessageDefs.finishload:
this.onForwardSelection(this._lastSelected);
break;
case MessageDefs.visq:
this.sendVisq(this._visqData);
break;
}
}

Expand All @@ -269,4 +319,11 @@ export class MPQSelectionPanel
}
this.setSelection(selections);
}

private onInvalidVISQData(_visqPath: string) {
this._visqData = [];
if (this._mpqEventHandler) {
// TODO
}
}
}