Ejemplo n.º 1
0
        public void Render(RenderContext rc)
        {
            if (_windowOpened)
            {
                Vector2 size = ImGui.GetIO().DisplaySize / ImGui.GetIO().DisplayFramebufferScale;
                ImGui.SetNextWindowSize(size - new Vector2(20, 35), SetCondition.Always);
                ImGui.SetNextWindowPos(new Vector2(10, 25), SetCondition.Always);

                ImGui.PushStyleVar(StyleVar.WindowRounding, 0f);
                if (ImGui.BeginWindow("Editor Window", ref _windowOpened, WindowFlags.ShowBorders | WindowFlags.NoCollapse | WindowFlags.NoMove | WindowFlags.NoResize))
                {
                    ImGui.Columns(2, "EditorColumns", true);
                    if (!_columnWidthSet)
                    {
                        _columnWidthSet = true;
                        float initialOffset = Math.Min(200, ImGui.GetWindowWidth() * 0.3f);
                        ImGui.SetColumnOffset(1, initialOffset);
                    }

                    DirectoryNode node = _assetDb.GetRootDirectoryGraph();
                    DrawRecursiveNode(node, false);

                    ImGui.NextColumn();
                    if (_selectedAsset != null)
                    {
                        Drawer d = DrawerCache.GetDrawer(_selectedAsset.GetType());
                        d.Draw(_selectedAsset.GetType().Name, ref _selectedAsset, rc);

                        ImGui.Text("Asset Name:");
                        ImGui.PushItemWidth(220);
                        ImGui.SameLine();
                        if (ImGui.InputText(" ", _filenameBuffer.Data, _filenameBuffer.CapacityInBytes, InputTextFlags.Default, null))
                        {
                            _loadedAssetPath = _filenameBuffer.StringValue;
                        }
                        ImGui.PopItemWidth();
                        ImGui.SameLine();
                        if (ImGui.Button("Save"))
                        {
                            string path = _assetDb.GetAssetPath(_loadedAssetPath);
                            using (var fs = File.CreateText(path))
                            {
                                var serializer = JsonSerializer.CreateDefault();
                                serializer.TypeNameHandling = TypeNameHandling.All;
                                serializer.Serialize(fs, _selectedAsset);
                            }
                        }
                    }
                }
                ImGui.EndWindow();
                ImGui.PopStyleVar();
            }
        }
Ejemplo n.º 2
0
        protected override bool DrawNonNull(string label, ref object obj, RenderContext rc)
        {
            ImGui.PushID(label);

            if (!_drawRootNode || ImGui.CollapsingHeader(label, label, true, true))
            {
                foreach (PropertyInfo pi in _properties)
                {
                    if (_drawRootNode)
                    {
                        const int levelMargin = 5;
                        ImGui.PushItemWidth(levelMargin);
                        ImGui.LabelText("", "");
                        ImGui.PopItemWidth();
                        ImGui.SameLine();
                    }

                    object originalValue = pi.GetValue(obj);
                    object value         = originalValue;
                    Drawer drawer;
                    if (value != null)
                    {
                        drawer = DrawerCache.GetDrawer(value.GetType());
                    }
                    else
                    {
                        drawer = DrawerCache.GetDrawer(pi.PropertyType);
                    }
                    bool changed = drawer.Draw(pi.Name, ref value, rc);
                    if (changed && originalValue != value)
                    {
                        pi.SetValue(obj, value);
                    }
                }
            }

            ImGui.PopID();

            return(false);
        }
Ejemplo n.º 3
0
        protected override bool DrawNonNull(string label, ref object obj, RenderContext rc)
        {
            T[]  arr      = (T[])obj;
            int  length   = arr.Length;
            bool newArray = false;

            if (ImGui.TreeNode($"{label}[{length}]###{label}"))
            {
                if (ImGui.IsLastItemHovered())
                {
                    ImGui.SetTooltip($"{TypeDrawn.GetElementType()}[{arr.Length}]");
                }

                if (!newArray)
                {
                    if (ImGui.SmallButton("-"))
                    {
                        int newLength = Math.Max(length - 1, 0);
                        Array.Resize(ref arr, newLength);
                        newArray = true;
                    }
                    ImGui.SameLine();
                    ImGui.Spacing();
                    ImGui.SameLine();
                    if (ImGui.SmallButton("+"))
                    {
                        Array.Resize(ref arr, length + 1);
                        newArray = true;
                    }
                }

                length = arr.Length;

                for (int i = 0; i < length; i++)
                {
                    ImGui.PushStyleColor(ColorTarget.Button, RgbaFloat.Red.ToVector4());
                    if (ImGui.Button($"X##{i}", new System.Numerics.Vector2(15, 15)))
                    {
                        ShiftArrayDown(arr, i);
                        Array.Resize(ref arr, length - 1);
                        newArray = true;
                        length  -= 1;
                        ImGui.PopStyleColor();
                        i--;
                        continue;
                    }
                    ImGui.PopStyleColor();
                    ImGui.SameLine();
                    object element = arr[i];
                    Drawer drawer;
                    if (element == null)
                    {
                        drawer = DrawerCache.GetDrawer(typeof(T));
                    }
                    else
                    {
                        Type realType = element.GetType();
                        drawer = DrawerCache.GetDrawer(realType);
                    }

                    bool changed = drawer.Draw($"{TypeDrawn.GetElementType().Name}[{i}]", ref element, rc);
                    if (changed || drawer.TypeDrawn.GetTypeInfo().IsValueType)
                    {
                        arr[i] = (T)element;
                    }
                }

                ImGui.TreePop();
            }
            else if (ImGui.IsLastItemHovered())
            {
                ImGui.SetTooltip($"{TypeDrawn.GetElementType()}[{arr.Length}]");
            }

            if (newArray)
            {
                obj = arr;
                return(true);
            }

            return(false);
        }