diff --git a/ui/package.json b/ui/package.json index c9e409f..d62d998 100644 --- a/ui/package.json +++ b/ui/package.json @@ -39,6 +39,7 @@ "ws": "^8.17.0", "xml-formatter": "^3.6.2" }, + "overrides": { "typescript": "^5.0.2" }, @@ -75,4 +76,4 @@ "@types/three": "^0.158.3", "@types/uuid": "^9.0.2" } -} +} \ No newline at end of file diff --git a/ui/src/core/extensions/extensions.ts b/ui/src/core/extensions/extensions.ts index f8ea695..0cd98ce 100644 --- a/ui/src/core/extensions/extensions.ts +++ b/ui/src/core/extensions/extensions.ts @@ -66,7 +66,8 @@ declare global { overrideValue(key: K, value: OptionalProperties): void; keysToJson(): string; toArray(): V[]; - getPredicateValue(callBack: (value: V) => boolean): K[]; + toArrayEntries(): { key: K; value: V }[]; + getPredicateKey(callBack: (value: V) => boolean): K[]; incrementValue(key: K): void; } interface Boolean { diff --git a/ui/src/core/extensions/map.ts b/ui/src/core/extensions/map.ts index 8521dc7..8595f7c 100644 --- a/ui/src/core/extensions/map.ts +++ b/ui/src/core/extensions/map.ts @@ -35,13 +35,23 @@ export const MapExtensions = () => { return JSON.stringify(result); }; } + if (Map.prototype.toArrayEntries === undefined) { + Map.prototype.toArrayEntries = function () { + const result = []; + for (const el of this.entries()) { + result.push({ key: el[0], value: el[1] }); + } + return result; + }; + } if (Map.prototype.toArray === undefined) { Map.prototype.toArray = function () { return Array.from(this.values()); }; } - if (Map.prototype.getPredicateValue === undefined) { - Map.prototype.getPredicateValue = function (callBack) { + + if (Map.prototype.getPredicateKey === undefined) { + Map.prototype.getPredicateKey = function (callBack) { const result: any[] = []; this.forEach((el, key) => { const callBackExecute = callBack(el); diff --git a/ui/src/features/behavior_tree_builder/model/editor_view.ts b/ui/src/features/behavior_tree_builder/model/editor_view.ts index aef8acd..c2588d2 100644 --- a/ui/src/features/behavior_tree_builder/model/editor_view.ts +++ b/ui/src/features/behavior_tree_builder/model/editor_view.ts @@ -5,6 +5,7 @@ import { Result } from "../../../core/helper/result"; import { AreaPlugin } from "rete-area-plugin"; import { Skills } from "../../../core/model/skill_model"; import xmlFormat from "xml-formatter"; +import { Position } from "rete-react-plugin"; export interface BtDrawDragAndDropView { x: number; @@ -25,6 +26,7 @@ export interface IUpdateEvent { export class NodeRerenderObserver extends TypedEvent {} export class ReteForceUpdateObserver extends TypedEvent {} export class BehaviorTreeBuilderModel { + public static nodeView: Map = new Map(); public static btRootTag: string = '${bt}'; public static result = ""; @@ -35,10 +37,11 @@ export class BehaviorTreeBuilderModel { ): Result { try { this.result = ""; + area.nodeViews.toArrayEntries().forEach((el) => this.nodeView.set(el.key, el.value.position)); this.getFirstSequence(editor).map((sortedSequence) => { const firstNodeId = sortedSequence.getKeyFromValueIsExists(1) as string; - this.findSequence(firstNodeId, editor, sortedSequence, 2); + // sortedSequence.toArrayEntries().forEach((el) => console.log(el)); this.result += `<${this.getNodeLabelAtId(editor, firstNodeId, skills)}>`; this.toXML(sortedSequence as Map, editor, area, firstNodeId, skills); this.result += ``; @@ -87,13 +90,19 @@ export class BehaviorTreeBuilderModel { activeElementId = "", skills?: Skills ) { + const sources: string[] = []; editor.getConnections().forEach((el) => { if (el.source === activeElementId) { - this.result += `<${this.getNodeLabelAtId(editor, el.target, skills)}>`; - this.toXML(sortedSequence, editor, area, el.target, skills); - this.result += ``; + sources.push(el.target); } }); + sources + .sort((a, b) => (this.nodeView.get(a)!.y > this.nodeView.get(b)!.y ? 1 : -1)) + .forEach((el) => { + this.result += `<${this.getNodeLabelAtId(editor, el, skills)}>`; + this.toXML(sortedSequence, editor, area, el, skills); + this.result += ``; + }); } public static findSequence( @@ -102,19 +111,28 @@ export class BehaviorTreeBuilderModel { result: Map, lvl: number ): Map | undefined { - const connections: string[] = []; + const allConnections: { id: string; target: string }[] = []; editor.getConnections().forEach((el) => { if (el.source === nodeId) { - connections.push(el.target); + allConnections.push({ id: el.id, target: el.target }); } }); - if (connections.isEmpty()) return result as Map; - let lv = lvl; - connections.forEach((el) => { - result.set(el, lvl); - this.findSequence(el, editor, result, (lv += 1)); + const connectionsSort = allConnections.sort((a, b) => { + if (this.nodeView.get(a.target)!.y > this.nodeView.get(b.target)!.y) { + return 1; + } + return -1; + }); + + if (connectionsSort.isEmpty()) return result as Map; + let cashedLvl = lvl; + + connectionsSort.forEach((el) => { + result.set(el.target, lvl); + + this.findSequence(el.target, editor, result, (cashedLvl += 1)); }); } public static getFirstSequence(editor: NodeEditor): Result> { @@ -126,7 +144,6 @@ export class BehaviorTreeBuilderModel { editor.getConnections().forEach((el) => { node.set(el.target, false); }); - const key = node.getKeyFromValueIsExists(1); if (key === undefined) { return Result.error(undefined); diff --git a/ui/src/features/behavior_tree_builder/presentation/behavior_tree_builder_store.tsx b/ui/src/features/behavior_tree_builder/presentation/behavior_tree_builder_store.tsx index 88062ea..2d96571 100644 --- a/ui/src/features/behavior_tree_builder/presentation/behavior_tree_builder_store.tsx +++ b/ui/src/features/behavior_tree_builder/presentation/behavior_tree_builder_store.tsx @@ -208,12 +208,12 @@ export class BehaviorTreeBuilderStore extends UiDrawerFormState { - console.log(xml); this.behaviorTreeModel.skills = this.filledOutTemplates; this.behaviorTreeModel.scene = NodeBehaviorTree.fromReteScene( this.editor as NodeEditor, this.areaPlugin as AreaPlugin ); + this.behaviorTreeModel.xml = xml; this.behaviorTreeModel.project = this.activeProject; this.messageHttp(this.behaviorTreeBuilderHttpRepository.editBt(this.behaviorTreeModel), { @@ -274,10 +274,12 @@ export class BehaviorTreeBuilderStore extends UiDrawerFormState console.log("UNKNOWN SID: " + this.selectedSid)