From 2ced29ab827c57c6199837922c85f71879e652e9 Mon Sep 17 00:00:00 2001 From: dexy Date: Mon, 5 Aug 2024 16:04:56 +1000 Subject: [PATCH] Fix updating entity position/rotation from project window --- .../GameFiles/FileTypes/YmapFile.cs | 94 +++++++++++++++++++ .../GameFiles/MetaTypes/Archetype.cs | 19 +--- .../Project/Panels/EditYmapEntityPanel.cs | 38 ++------ CodeWalker/Project/ProjectForm.cs | 2 +- 4 files changed, 106 insertions(+), 47 deletions(-) diff --git a/CodeWalker.Core/GameFiles/FileTypes/YmapFile.cs b/CodeWalker.Core/GameFiles/FileTypes/YmapFile.cs index d59c238..81955e3 100644 --- a/CodeWalker.Core/GameFiles/FileTypes/YmapFile.cs +++ b/CodeWalker.Core/GameFiles/FileTypes/YmapFile.cs @@ -1776,6 +1776,31 @@ namespace CodeWalker.GameFiles UpdateEntityHash(); } + public YmapEntityDef(YmapEntityDef mloParent, MCEntityDef ent, int index) + { + Ymap = null; + Index = index; + CEntityDef = ent._Data; + Scale = new Vector3(new Vector2(_CEntityDef.scaleXY), _CEntityDef.scaleZ); + MloRefPosition = _CEntityDef.position; + MloRefOrientation = new Quaternion(_CEntityDef.rotation); + if (MloRefOrientation != Quaternion.Identity) + { + MloRefOrientation = Quaternion.Invert(MloRefOrientation); + } + IsMlo = false; + + Extensions = ent.Extensions; + MloParent = mloParent; + Position = mloParent.Position + mloParent.Orientation.Multiply(MloRefPosition); + Orientation = Quaternion.Multiply(mloParent.Orientation, MloRefOrientation); + + UpdateWidgetPosition(); + UpdateWidgetOrientation(); + UpdateEntityHash(); + } + + public void SetArchetype(Archetype arch) { @@ -1859,6 +1884,36 @@ namespace CodeWalker.GameFiles UpdateWidgetPosition(); } + public void SetPositionRaw(Vector3 pos) + { + //set the raw position value in the CEntityDef, and update everything from that. + //used by the EditYmapEntityPanel + + _CEntityDef.position = pos; + + if (MloParent != null) + { + MloRefPosition = pos; + Position = MloParent.Position + MloParent.Orientation.Multiply(MloRefPosition); + UpdateBB(); + UpdateMloArchetype(); + } + else + { + Position = pos; + UpdateBB(); + } + + if (MloInstance != null) + { + MloInstance.SetPosition(Position); + MloInstance.UpdateEntities(); + } + + UpdateEntityHash(); + UpdateWidgetPosition(); + } + private void UpdateBB() { if (Archetype != null) @@ -1945,6 +2000,45 @@ namespace CodeWalker.GameFiles UpdateWidgetOrientation(); } + public void SetOrientationRaw(Quaternion ori) + { + //set the raw rotation value in the CEntityDef, and update everything from that. + //used by the EditYmapEntityPanel + + _CEntityDef.rotation = ori.ToVector4(); + + if (MloParent != null) + { + MloRefOrientation = ori; + if (MloRefOrientation != Quaternion.Identity) + { + MloRefOrientation = Quaternion.Invert(MloRefOrientation); + } + Orientation = Quaternion.Multiply(MloParent.Orientation, MloRefOrientation); + } + else + { + Orientation = ori; + if (MloInstance == null)//CMloInstanceDef quaternions aren't inverted, but CEntityDef ones are + { + if (Orientation != Quaternion.Identity) + { + Orientation = Quaternion.Invert(Orientation); + } + } + } + + if (MloInstance != null) + { + MloInstance.SetOrientation(Orientation); + MloInstance.UpdateEntities(); + } + + UpdateBB(); + UpdateWidgetPosition(); + UpdateWidgetOrientation(); + } + public void SetScale(Vector3 s) { Scale = new Vector3(s.X, s.X, s.Z); diff --git a/CodeWalker.Core/GameFiles/MetaTypes/Archetype.cs b/CodeWalker.Core/GameFiles/MetaTypes/Archetype.cs index 41a1e27..8692d43 100644 --- a/CodeWalker.Core/GameFiles/MetaTypes/Archetype.cs +++ b/CodeWalker.Core/GameFiles/MetaTypes/Archetype.cs @@ -620,7 +620,7 @@ namespace CodeWalker.GameFiles var entlist = new List(); for (int i = 0; i < ec; i++) { - var e = CreateYmapEntity(Owner, MloArch.entities[i], i); + var e = new YmapEntityDef(Owner, MloArch.entities[i], i); entlist.Add(e); } @@ -638,7 +638,7 @@ namespace CodeWalker.GameFiles { for (int j = 0; j < entitySet.Entities.Length; j++) { - var e = CreateYmapEntity(Owner, entitySet.Entities[j], lasti); + var e = new YmapEntityDef(Owner, entitySet.Entities[j], lasti); instset.Entities.Add(e); e.MloEntitySet = instset; lasti++; @@ -762,21 +762,6 @@ namespace CodeWalker.GameFiles } } - public YmapEntityDef CreateYmapEntity(YmapEntityDef owner, MCEntityDef ment, int index) - { - YmapEntityDef e = new YmapEntityDef(null, index, ref ment._Data); - e.Extensions = ment.Extensions; - e.MloRefPosition = e.Position; - e.MloRefOrientation = e.Orientation; - e.MloParent = owner; - e.Position = owner.Position + owner.Orientation.Multiply(e.MloRefPosition); - e.Orientation = Quaternion.Multiply(owner.Orientation, e.MloRefOrientation); - e.UpdateWidgetPosition(); - e.UpdateWidgetOrientation(); - e.UpdateEntityHash(); - return e; - } - public MCEntityDef TryGetArchetypeEntity(YmapEntityDef ymapEntity) { if (ymapEntity == null) return null; diff --git a/CodeWalker/Project/Panels/EditYmapEntityPanel.cs b/CodeWalker/Project/Panels/EditYmapEntityPanel.cs index fa09a43..f8738f5 100644 --- a/CodeWalker/Project/Panels/EditYmapEntityPanel.cs +++ b/CodeWalker/Project/Panels/EditYmapEntityPanel.cs @@ -381,26 +381,17 @@ namespace CodeWalker.Project.Panels Vector3 v = FloatUtil.ParseVector3String(EntityPositionTextBox.Text); lock (ProjectForm.ProjectSyncRoot) { - if (CurrentEntity.MloParent != null) + if (CurrentEntity._CEntityDef.position != v) { - v = CurrentEntity.MloParent.Position + CurrentEntity.MloParent.Orientation.Multiply(v); - CurrentEntity.SetPosition(v); + CurrentEntity.SetPositionRaw(v); ProjectItemChanged(); - } - else - { - if (CurrentEntity.Position != v) + var wf = ProjectForm.WorldForm; + if (wf != null) { - CurrentEntity.SetPosition(v); - ProjectItemChanged(); - var wf = ProjectForm.WorldForm; - if (wf != null) + wf.BeginInvoke(new Action(() => { - wf.BeginInvoke(new Action(() => - { - wf.SetWidgetPosition(CurrentEntity.WidgetPosition, true); - })); - } + wf.SetWidgetPosition(CurrentEntity.WidgetPosition, true); + })); } } } @@ -416,20 +407,9 @@ namespace CodeWalker.Project.Panels { if (CurrentEntity._CEntityDef.rotation != v) { - var wf = ProjectForm.WorldForm; - - if (CurrentEntity.MloParent != null) - { - var world = Quaternion.Normalize(Quaternion.Multiply(q, CurrentEntity.MloParent.Orientation)); - CurrentEntity.SetOrientation(world); - } - else - { - bool useInverse = (CurrentEntity.MloInstance == null); - CurrentEntity.SetOrientation(q, useInverse); - } - + CurrentEntity.SetOrientationRaw(q); ProjectItemChanged(); + var wf = ProjectForm.WorldForm; wf?.BeginInvoke(new Action(() => { wf.SetWidgetRotation(CurrentEntity.WidgetOrientation, true); diff --git a/CodeWalker/Project/ProjectForm.cs b/CodeWalker/Project/ProjectForm.cs index 45c60e3..367ff83 100644 --- a/CodeWalker/Project/ProjectForm.cs +++ b/CodeWalker/Project/ProjectForm.cs @@ -3588,7 +3588,7 @@ namespace CodeWalker.Project var createindex = mloArch.entities.Length; var ment = new MCEntityDef(ref cent, mloArch); - var outEnt = mloInstance.CreateYmapEntity(mloInstance.Owner, ment, createindex); + var outEnt = new YmapEntityDef(mloInstance.Owner, ment, createindex); try {