Ejemplo n.º 1
0
 public void InitializeFigure(IDrawable figure, IRenderer renderer)
 {
     if (figure != null)
     {
         figure.Initialize(renderer);
     }
 }
        void InitWithDrawable(IDrawable drawable, ResourceManager res, bool draw, bool staticpos, bool havePhys = true)
        {
            Resources = res;
            dr        = drawable;
            PhysicsComponent phys  = null;
            bool             isCmp = false;
            string           name  = "";

            if (draw)
            {
                drawable?.Initialize(res);
            }
            if (dr is SphFile)
            {
                var radius = ((SphFile)dr).Radius;
                phys = new PhysicsComponent(this)
                {
                    SphereRadius = radius
                };
                name       = ((SphFile)dr).SideMaterialNames[0];
                RigidModel = ((SphFile)dr).CreateRigidModel(draw);
            }
            else if (dr is IRigidModelFile mdl)
            {
                //var mdl = dr as ModelFile;
                RigidModel = mdl.CreateRigidModel(draw);
                var path = Path.ChangeExtension(RigidModel.Path, "sur");
                name = Path.GetFileNameWithoutExtension(RigidModel.Path);
                if (File.Exists(path))
                {
                    phys = new PhysicsComponent(this)
                    {
                        SurPath = path
                    }
                }
                ;
                if (RigidModel.Animation != null)
                {
                    AnimationComponent = new AnimationComponent(this, RigidModel.Animation);
                    Components.Add(AnimationComponent);
                }
            }
            if (havePhys && phys != null)
            {
                PhysicsComponent = phys;
                Components.Add(phys);
            }
            PopulateHardpoints();
            if (draw && RigidModel != null)
            {
                RenderComponent = new ModelRenderer(RigidModel)
                {
                    Name = name
                };
            }
        }
Ejemplo n.º 3
0
        public override void Draw()
        {
            //Child Window
            var size = ImGui.GetWindowSize();

            ImGui.BeginChild("##utfchild", new Vector2(size.X - 15, size.Y - 50), false, 0);
            //Layout
            if (selectedNode != null)
            {
                ImGui.Columns(2, "NodeColumns", true);
            }
            //Headers
            ImGui.Separator();
            ImGui.Text("Nodes");
            if (selectedNode != null)
            {
                ImGui.NextColumn();
                ImGui.Text("Node Information");
                ImGui.NextColumn();
            }
            ImGui.Separator();
            //Tree
            ImGui.BeginChild("##scroll");
            var flags  = selectedNode == Utf.Root ? ImGuiTreeNodeFlags.Selected | tflags : tflags;
            var isOpen = ImGui.TreeNodeEx("/", flags);

            if (ImGui.IsItemClicked(0))
            {
                selectedNode = Utf.Root;
            }
            ImGui.PushID("/##ROOT");
            DoNodeMenu("/##ROOT", Utf.Root, null);
            ImGui.PopID();
            if (isOpen)
            {
                for (int i = 0; i < Utf.Root.Children.Count; i++)
                {
                    DoNode(Utf.Root.Children[i], Utf.Root, i);
                }
                ImGui.TreePop();
            }
            ImGui.EndChild();
            //End Tree
            if (selectedNode != null)
            {
                //Node preview
                ImGui.NextColumn();
                NodeInformation();
            }
            //Action Bar
            ImGui.EndChild();
            ImGui.Separator();
            if (ImGui.Button("Actions"))
            {
                ImGui.OpenPopup("actions");
            }
            if (ImGui.BeginPopup("actions"))
            {
                if (ImGui.MenuItem("View Model"))
                {
                    IDrawable  drawable = null;
                    ModelNodes hpn      = new ModelNodes();
                    try
                    {
                        drawable = LibreLancer.Utf.UtfLoader.GetDrawable(Utf.Export(), main.Resources);
                        drawable.Initialize(main.Resources);
                        if (Utf.Root.Children.Any((x) => x.Name.Equals("cmpnd", StringComparison.OrdinalIgnoreCase)))
                        {
                            foreach (var child in Utf.Root.Children.Where((x) => x.Name.EndsWith(".3db", StringComparison.OrdinalIgnoreCase)))
                            {
                                var n = new ModelHpNode();
                                n.Name           = child.Name;
                                n.Node           = child;
                                n.HardpointsNode = child.Children.FirstOrDefault((x) => x.Name.Equals("hardpoints", StringComparison.OrdinalIgnoreCase));
                                hpn.Nodes.Add(n);
                            }
                            var cmpnd = Utf.Root.Children.First((x) => x.Name.Equals("cmpnd", StringComparison.OrdinalIgnoreCase));
                            hpn.Cons = cmpnd.Children.FirstOrDefault((x) => x.Name.Equals("cons", StringComparison.OrdinalIgnoreCase));
                        }
                        else
                        {
                            var n = new ModelHpNode();
                            n.Name           = "ROOT";
                            n.Node           = Utf.Root;
                            n.HardpointsNode = Utf.Root.Children.FirstOrDefault((x) => x.Name.Equals("hardpoints", StringComparison.OrdinalIgnoreCase));
                            hpn.Nodes.Add(n);
                        }
                    }
                    catch (Exception ex) { ErrorPopup("Could not open as model\n" + ex.Message + "\n" + ex.StackTrace); drawable = null; }
                    if (drawable != null)
                    {
                        main.AddTab(new ModelViewer(DocumentName, drawable, main, this, hpn));
                    }
                }
                if (ImGui.MenuItem("Export Collada"))
                {
                    LibreLancer.Utf.Cmp.ModelFile model = null;
                    LibreLancer.Utf.Cmp.CmpFile   cmp   = null;
                    try
                    {
                        var drawable = LibreLancer.Utf.UtfLoader.GetDrawable(Utf.Export(), main.Resources);
                        model = (drawable as LibreLancer.Utf.Cmp.ModelFile);
                        cmp   = (drawable as LibreLancer.Utf.Cmp.CmpFile);
                    }
                    catch (Exception) { ErrorPopup("Could not open as model"); model = null; }
                    if (model != null)
                    {
                        var output = FileDialog.Save();
                        if (output != null)
                        {
                            model.Path = DocumentName;
                            try
                            {
                                ColladaExport.ExportCollada(model, main.Resources, output);
                            }
                            catch (Exception ex)
                            {
                                ErrorPopup("Error\n" + ex.Message + "\n" + ex.StackTrace);
                            }
                        }
                    }
                    if (cmp != null)
                    {
                        var output = FileDialog.Save();
                        if (output != null)
                        {
                            cmp.Path = DocumentName;
                            try
                            {
                                ColladaExport.ExportCollada(cmp, main.Resources, output);
                            }
                            catch (Exception ex)
                            {
                                ErrorPopup("Error\n" + ex.Message + "\n" + ex.StackTrace);
                            }
                        }
                    }
                }
                if (ImGui.MenuItem("View Ale"))
                {
                    AleFile ale = null;
                    try
                    {
                        ale = new AleFile(Utf.Export());
                    }
                    catch (Exception)
                    {
                        ErrorPopup("Could not open as ale");
                        ale = null;
                    }
                    if (ale != null)
                    {
                        main.AddTab(new AleViewer(Title, ale, main));
                    }
                }

                if (ImGui.MenuItem("Resolve Audio Hashes"))
                {
                    var folder = FileDialog.ChooseFolder();
                    if (folder != null)
                    {
                        var idtable = new IDTable(folder);
                        foreach (var n in Utf.Root.IterateAll())
                        {
                            if (n.Name.StartsWith("0x"))
                            {
                                uint v;
                                if (uint.TryParse(n.Name.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out v))
                                {
                                    idtable.UtfNicknameTable.TryGetValue(v, out n.ResolvedName);
                                }
                            }
                            else
                            {
                                n.ResolvedName = null;
                            }
                        }
                    }
                }
                ImGui.EndPopup();
            }
            ImGui.SameLine();
            if (ImGui.Button("Reload Resources"))
            {
                main.Resources.RemoveResourcesForId(Unique.ToString());
                main.Resources.AddResources(Utf.Export(), Unique.ToString());
            }
            Popups();
        }
Ejemplo n.º 4
0
        public override bool Draw()
        {
            //Child Window
            var size = ImGui.GetWindowSize();

            ImGui.BeginChild("##utfchild", new Vector2(size.X - 15, size.Y - 50), false, 0);
            //Layout
            if (selectedNode != null)
            {
                ImGui.Columns(2, "NodeColumns", true);
            }
            //Headers
            ImGui.Separator();
            ImGui.Text("Nodes");
            if (selectedNode != null)
            {
                ImGui.NextColumn();
                ImGui.Text("Node Information");
                ImGui.NextColumn();
            }
            ImGui.Separator();
            //Tree
            ImGui.BeginChild("##scroll", false, 0);
            var flags  = selectedNode == Utf.Root ? TreeNodeFlags.Selected | tflags : tflags;
            var isOpen = ImGui.TreeNodeEx("/", flags);

            if (ImGuiNative.igIsItemClicked(0))
            {
                selectedNode = Utf.Root;
            }
            ImGui.PushID("/##ROOT");
            DoNodeMenu("/##ROOT", Utf.Root, null);
            ImGui.PopID();
            if (isOpen)
            {
                for (int i = 0; i < Utf.Root.Children.Count; i++)
                {
                    DoNode(Utf.Root.Children[i], Utf.Root, i);
                }
                ImGui.TreePop();
            }
            ImGui.EndChild();
            //End Tree
            if (selectedNode != null)
            {
                //Node preview
                ImGui.NextColumn();
                NodeInformation();
            }
            //Action Bar
            ImGui.EndChild();
            ImGui.Separator();
            if (ImGui.Button("Actions"))
            {
                ImGui.OpenPopup("actions");
            }
            if (ImGui.BeginPopup("actions"))
            {
                if (ImGui.MenuItem("View Model"))
                {
                    IDrawable drawable = null;
                    try
                    {
                        drawable = LibreLancer.Utf.UtfLoader.GetDrawable(Utf.Export(), main.Resources);
                        drawable.Initialize(main.Resources);
                    }
                    catch (Exception) { ErrorPopup("Could not open as model"); drawable = null; }
                    if (drawable != null)
                    {
                        main.AddTab(new ModelViewer("Model Viewer (" + Title + ")", Title, drawable, main, this));
                    }
                }
                if (ImGui.MenuItem("View Ale"))
                {
                    AleFile ale = null;
                    try
                    {
                        ale = new AleFile(Utf.Export());
                    }
                    catch (Exception)
                    {
                        ErrorPopup("Could not open as ale");
                        ale = null;
                    }
                    if (ale != null)
                    {
                        main.AddTab(new AleViewer("Ale Viewer (" + Title + ")", Title, ale, main));
                    }
                }
                if (ImGui.MenuItem("Refresh Resources"))
                {
                    main.Resources.RemoveResourcesForId(Unique.ToString());
                    main.Resources.AddResources(Utf.Export(), Unique.ToString());
                }
                ImGui.EndPopup();
            }
            Popups();
            return(open);
        }
Ejemplo n.º 5
0
        void AddEntities(ThnScript thn)
        {
            foreach (var kv in thn.Entities)
            {
                if (Objects.ContainsKey(kv.Key))
                {
                    continue;
                }
                if ((kv.Value.ObjectFlags & ThnObjectFlags.Reference) == ThnObjectFlags.Reference)
                {
                    continue;
                }
                var obj = new ThnObject();
                obj.Name      = kv.Key;
                obj.Translate = kv.Value.Position ?? Vector3.Zero;
                obj.Rotate    = kv.Value.RotationMatrix ?? Matrix4x4.Identity;
                //PlayerShip object
                if (spawnObjects && scriptContext.PlayerShip != null && kv.Value.Type == EntityTypes.Compound &&
                    kv.Value.Template.Equals("playership", StringComparison.InvariantCultureIgnoreCase))
                {
                    obj.Object = scriptContext.PlayerShip;
                    obj.Object.RenderComponent.LitDynamic = (kv.Value.ObjectFlags & ThnObjectFlags.LitDynamic) == ThnObjectFlags.LitDynamic;
                    obj.Object.RenderComponent.LitAmbient = (kv.Value.ObjectFlags & ThnObjectFlags.LitAmbient) == ThnObjectFlags.LitAmbient;
                    obj.Object.RenderComponent.NoFog      = kv.Value.NoFog;
                    ((ModelRenderer)obj.Object.RenderComponent).LightGroup = kv.Value.LightGroup;
                    obj.Entity = kv.Value;
                    Vector3 transform = kv.Value.Position ?? Vector3.Zero;
                    obj.Object.Transform = (kv.Value.RotationMatrix ?? Matrix4x4.Identity) * Matrix4x4.CreateTranslation(transform);
                    obj.HpMount          = scriptContext.PlayerShip.GetHardpoint("HpMount");
                    World.Objects.Add(obj.Object);
                    Objects.Add(kv.Key, obj);
                    continue;
                }

                var    template = kv.Value.Template;
                string replacement;
                if (scriptContext != null &&
                    scriptContext.Substitutions.TryGetValue(kv.Value.Template, out replacement))
                {
                    template = replacement;
                }
                var resman = game.GetService <ResourceManager>();
                if (spawnObjects && kv.Value.Type == EntityTypes.Compound)
                {
                    bool getHpMount = false;
                    //Fetch model
                    IDrawable drawable = null;
                    if (!string.IsNullOrEmpty(template))
                    {
                        switch (kv.Value.MeshCategory.ToLowerInvariant())
                        {
                        case "solar":
                            drawable = gameData.GetSolar(template);
                            break;

                        case "ship":
                        case "spaceship":
                            getHpMount = true;
                            var sh = gameData.GetShip(template);
                            drawable = sh.ModelFile.LoadFile(resman);
                            break;

                        case "prop":
                            drawable = gameData.GetProp(template);
                            break;

                        case "room":
                            drawable = gameData.GetRoom(template);
                            break;

                        case "equipment cart":
                            drawable = gameData.GetCart(template);
                            break;

                        case "equipment":
                            var eq = gameData.GetEquipment(template);
                            drawable = eq?.ModelFile.LoadFile(resman);
                            break;

                        case "asteroid":
                            drawable = gameData.GetAsteroid(kv.Value.Template);
                            break;

                        default:
                            throw new NotImplementedException("Mesh Category " + kv.Value.MeshCategory);
                        }
                    }
                    else
                    {
                        FLLog.Warning("Thn", $"object '{kv.Value.Name}' has empty template, category " +
                                      $"'{kv.Value.MeshCategory}'");
                    }

                    drawable?.Initialize(resman);
                    if (kv.Value.UserFlag != 0)
                    {
                        //This is a starsphere
                        layers.Add(new Tuple <IDrawable, ThnObject>(drawable, obj));
                    }
                    else
                    {
                        obj.Object                  = new GameObject(drawable, game.GetService <ResourceManager>(), true, false, false);
                        obj.Object.Name             = kv.Value.Name;
                        obj.Object.PhysicsComponent = null; //Jitter seems to interfere with directly setting orientation
                        if (getHpMount)
                        {
                            obj.HpMount = obj.Object.GetHardpoint("HpMount");
                        }
                        var r = (ModelRenderer)obj.Object.RenderComponent;
                        if (r != null)
                        {
                            r.LightGroup = kv.Value.LightGroup;
                            r.LitDynamic = (kv.Value.ObjectFlags & ThnObjectFlags.LitDynamic) ==
                                           ThnObjectFlags.LitDynamic;
                            r.LitAmbient = (kv.Value.ObjectFlags & ThnObjectFlags.LitAmbient) ==
                                           ThnObjectFlags.LitAmbient;
                            //HIDDEN just seems to be an editor flag?
                            //r.Hidden = (kv.Value.ObjectFlags & ThnObjectFlags.Hidden) == ThnObjectFlags.Hidden;
                            r.NoFog = kv.Value.NoFog;
                        }
                    }
                }
                else if (kv.Value.Type == EntityTypes.PSys)
                {
                    var fx = gameData.GetEffect(kv.Value.Template);
                    if (fx != null)
                    {
                        obj.Object = new GameObject();
                        obj.Object.RenderComponent = new ParticleEffectRenderer(fx.GetEffect(resman))
                        {
                            Active = false
                        };
                    }
                }
                else if (kv.Value.Type == EntityTypes.Scene)
                {
                    if (kv.Value.DisplayText != null)
                    {
                        text = kv.Value.DisplayText;
                    }
                    if (hasScene)
                    {
                        //throw new Exception("Thn can only have one scene");
                        //TODO: This needs to be handled better
                        continue;
                    }
                    var amb = kv.Value.Ambient.Value;
                    if (amb.X == 0 && amb.Y == 0 && amb.Z == 0)
                    {
                        continue;
                    }
                    hasScene = true;
                    Renderer.SystemLighting.Ambient = new Color4(amb.X / 255f, amb.Y / 255f, amb.Z / 255f, 1);
                }
                else if (kv.Value.Type == EntityTypes.Light)
                {
                    var lt = new DynamicLight();
                    lt.LightGroup = kv.Value.LightGroup;
                    lt.Active     = kv.Value.LightProps.On;
                    lt.Light      = kv.Value.LightProps.Render;
                    obj.Light     = lt;
                    obj.LightDir  = lt.Light.Direction;
                    if (kv.Value.RotationMatrix.HasValue)
                    {
                        var m = kv.Value.RotationMatrix.Value;
                        lt.Light.Direction = Vector3.TransformNormal(lt.Light.Direction, m);
                    }
                    if (Renderer != null)
                    {
                        Renderer.SystemLighting.Lights.Add(lt);
                    }
                }
                else if (kv.Value.Type == EntityTypes.Camera)
                {
                    obj.Camera             = new ThnCameraTransform();
                    obj.Camera.Position    = kv.Value.Position.Value;
                    obj.Camera.Orientation = kv.Value.RotationMatrix ?? Matrix4x4.Identity;
                    obj.Camera.FovH        = kv.Value.FovH ?? obj.Camera.FovH;
                    obj.Camera.AspectRatio = kv.Value.HVAspect ?? obj.Camera.AspectRatio;
                    if (kv.Value.NearPlane != null)
                    {
                        obj.Camera.Znear = kv.Value.NearPlane.Value;
                    }
                    if (kv.Value.FarPlane != null)
                    {
                        obj.Camera.Zfar = kv.Value.FarPlane.Value;
                    }
                }
                else if (kv.Value.Type == EntityTypes.Marker)
                {
                    obj.Object          = new GameObject();
                    obj.Object.Name     = "Marker";
                    obj.Object.Nickname = "";
                }
                else if (kv.Value.Type == EntityTypes.Deformable)
                {
                    obj.Object = new GameObject();
                    gameData.GetCostume(template, out DfmFile body, out DfmFile head, out DfmFile leftHand, out DfmFile rightHand);
                    var skel = new DfmSkeletonManager(body, head, leftHand, rightHand);
                    obj.Object.RenderComponent = new CharacterRenderer(skel);
                    var anmComponent = new AnimationComponent(obj.Object, gameData.GetCharacterAnimations());
                    obj.Object.AnimationComponent = anmComponent;
                    obj.Object.Components.Add(anmComponent);
                }
                else if (kv.Value.Type == EntityTypes.Sound)
                {
                    obj.Sound         = new ThnSound(kv.Value.Template, game.GetService <SoundManager>(), kv.Value.AudioProps, obj);
                    obj.Sound.Spatial = (kv.Value.ObjectFlags & ThnObjectFlags.SoundSpatial) == ThnObjectFlags.SoundSpatial;
                }
                if (obj.Object != null)
                {
                    Vector3 transform = kv.Value.Position ?? Vector3.Zero;
                    obj.Object.Transform = (kv.Value.RotationMatrix ?? Matrix4x4.Identity) * Matrix4x4.CreateTranslation(transform);
                    World.Objects.Add(obj.Object);
                }
                obj.Entity      = kv.Value;
                Objects[kv.Key] = obj;
            }
        }
Ejemplo n.º 6
0
 void NodeInformation()
 {
     ImGui.Text("Name: " + selectedNode.Name);
     if (selectedNode.Children != null)
     {
         ImGui.Text(selectedNode.Children.Count + " children");
     }
     else
     {
         ImGui.Text(string.Format("Size: {0}", LibreLancer.DebugDrawing.SizeSuffix(selectedNode.Data.Length)));
         if (ImGui.Button("Hex Editor"))
         {
             hexdata = new byte[selectedNode.Data.Length];
             selectedNode.Data.CopyTo(hexdata, 0);
             mem       = new MemoryEditor();
             hexEditor = true;
         }
         if (ImGui.Button("Float Editor"))
         {
             floats = new float[selectedNode.Data.Length / 4];
             for (int i = 0; i < selectedNode.Data.Length / 4; i++)
             {
                 floats[i] = BitConverter.ToSingle(selectedNode.Data, i * 4);
             }
             floatEditor = true;
         }
         if (ImGui.Button("Int Editor"))
         {
             ints = new int[selectedNode.Data.Length / 4];
             for (int i = 0; i < selectedNode.Data.Length / 4; i++)
             {
                 ints[i] = BitConverter.ToInt32(selectedNode.Data, i * 4);
             }
             intEditor = true;
         }
         if (ImGui.Button("Color Picker"))
         {
             var len = selectedNode.Data.Length / 4;
             if (len < 3)
             {
                 pickcolor4 = true;
                 color4     = new System.Numerics.Vector4(0, 0, 0, 1);
             }
             else if (len == 3)
             {
                 pickcolor4 = false;
                 color3     = new System.Numerics.Vector3(
                     BitConverter.ToSingle(selectedNode.Data, 0),
                     BitConverter.ToSingle(selectedNode.Data, 4),
                     BitConverter.ToSingle(selectedNode.Data, 8));
             }
             else if (len > 3)
             {
                 pickcolor4 = true;
                 color4     = new System.Numerics.Vector4(
                     BitConverter.ToSingle(selectedNode.Data, 0),
                     BitConverter.ToSingle(selectedNode.Data, 4),
                     BitConverter.ToSingle(selectedNode.Data, 8),
                     BitConverter.ToSingle(selectedNode.Data, 12));
             }
             colorPicker = true;
         }
         if (ImGui.Button("Texture Viewer"))
         {
             Texture2D tex = null;
             try
             {
                 using (var stream = new MemoryStream(selectedNode.Data))
                 {
                     tex = LibreLancer.ImageLib.Generic.FromStream(stream);
                 }
                 var title = string.Format("{0} ({1})", selectedNode.Name, Title);
                 var tab   = new TextureViewer(title, tex);
                 main.AddTab(tab);
             }
             catch (Exception)
             {
                 ErrorPopup("Node data couldn't be opened as texture");
             }
         }
         if (ImGui.Button("Play Audio"))
         {
             var data = main.Audio.AllocateData();
             using (var stream = new MemoryStream(selectedNode.Data))
             {
                 main.Audio.PlaySound(stream);
             }
         }
         if (ImGui.Button("Import Data"))
         {
             string path;
             if ((path = FileDialog.Open()) != null)
             {
                 selectedNode.Data = File.ReadAllBytes(path);
             }
         }
         if (ImGui.Button("Export Data"))
         {
             string path;
             if ((path = FileDialog.Save()) != null)
             {
                 File.WriteAllBytes(path, selectedNode.Data);
             }
         }
         if (ImGui.Button("View Model"))
         {
             IDrawable drawable = null;
             try
             {
                 drawable = LibreLancer.Utf.UtfLoader.GetDrawable(Utf.Export(), main.Resources);
                 drawable.Initialize(main.Resources);
             }
             catch (Exception) { ErrorPopup("Could not open as model"); drawable = null; }
             if (drawable != null)
             {
                 main.AddTab(new ModelViewer("Model Viewer", drawable, main.RenderState, main.Viewport));
             }
         }
     }
 }