Exemplo n.º 1
0
        public void SubmitUI()
        {
            yes = true;
            #region MAIN MENU BAR
            ImGui.BeginMainMenuBar();

            // File thing
            if (ImGui.BeginMenu("File"))
            {
                if (ImGui.MenuItem("New Project..."))
                {
                    // TODO: Modal for new project
                }

                if (ImGui.MenuItem("Open Project"))
                {
                    state = EditorState.SelectingProjectFile;
                    fd    = new FileDialog(Utility.QUICKLINK_DIRS["Documents"], "Open Project", FileDialogType.SelectFile, new string[] { Utility.EXT_PROJ });
                }

                if (ImGui.BeginMenu("Include"))
                {
                    if (ImGui.MenuItem("IC Collection"))
                    {
                        state = EditorState.IncludeCollection;
                        fd    = new FileDialog(Utility.QUICKLINK_DIRS["Documents"], "Include Collection", FileDialogType.SelectMultipleFiles, new string[] { Utility.EXT_ICCOLLECTION });
                    }
                    if (ImGui.MenuItem("IC Description"))
                    {
                        state = EditorState.IncludeDescription;
                        fd    = new FileDialog(Utility.QUICKLINK_DIRS["Documents"], "Include Description", FileDialogType.SelectMultipleFiles, new string[] { Utility.EXT_IC });
                    }
                    ImGui.EndMenu();
                }

                ImGui.Separator();

                if (ImGui.MenuItem("Save"))
                {
                    string path = Utility.CreateProjectFilePath(currentProject.ProjectName);
                    currentProject.SaveProjectToFile(path);
                    SettingManager.SetSetting("latest-project", path);
                    SettingManager.SaveSettings();
                }

                if (ImGui.MenuItem("Save as..."))
                {
                    state = EditorState.SavingProjectFile;
                    fd    = new FileDialog(Utility.PROJECTS_DIR, "Save Project As", FileDialogType.SaveFile, new string[] { Utility.EXT_PROJ });
                }

                if (ImGui.MenuItem("Settings"))
                {
                    editingSettings = true;
                }

                ImGui.EndMenu();
            }

            // Edit thing
            if (ImGui.BeginMenu("Edit"))
            {
                ImGui.MenuItem("Empty for now");
                ImGui.EndMenu();
            }

            // Simulation
            if (ImGui.BeginMenu("Simulation"))
            {
                ImGui.Checkbox("Simulating", ref _simulationOn);
                if (ImGui.Button("Update simulation"))
                {
                    currentProject.Simulation.UpdateLogic(GetMousePositionInWorld());
                }
                ImGui.EndMenu();
            }

            if (ImGui.BeginMenu("Integrated Circuits"))
            {
                if (ImGui.MenuItem("New..."))
                {
                    _makingIc = true;
                }

                if (ImGui.MenuItem("Open Folder..."))
                {
                    Utility.OpenPath(Utility.ASSETS_DIR);
                }

                if (ImGui.MenuItem("Show Window", "", icWindowOpen))
                {
                    icWindowOpen = !icWindowOpen;
                }

                if (ImGui.MenuItem("Create Collection"))
                {
                    creatingCollection = true;
                }

                ImGui.EndMenu();
            }

            ImGui.Separator();

            ImGui.TextDisabled(currentProject.ProjectName);

            ImGui.EndMainMenuBar();

            #endregion

            #region COMPONENTS WINDOW
            // Components window
            {
                ImGui.SetNextWindowPos(new Vector2(25, 50), ImGuiCond.Appearing);
                ImGui.SetNextWindowCollapsed(false, ImGuiCond.Appearing);
                ImGui.Begin("Components", ImGuiWindowFlags.AlwaysAutoResize);

                ImGui.Text("Input/Output");
                Vector2 buttonSize = Utility.BUTTON_DEFAULT_SIZE;

                ImGui.Button("SWITCH", buttonSize);
                if (ImGui.IsItemClicked())
                {
                    SetNewComponent(new DrawableCircuitSwitch(GetMousePositionInWorld(), true));
                }

                ImGui.Button("LAMP", buttonSize);
                if (ImGui.IsItemClicked())
                {
                    SetNewComponent(new DrawableCircuitLamp(GetMousePositionInWorld(), true));
                }

                ImGui.Separator();
                ImGui.Text("Logic Gates");

                // TODO: Do the rest of the buttons like this
                ImGui.Button("AND", buttonSize);
                if (ImGui.IsItemClicked())
                {
                    SetNewComponent(new DrawableLogicGate(GetMousePositionInWorld(), "AND", new ANDGateLogic(), true));
                }
                ImGui.Button("NAND", buttonSize);
                if (ImGui.IsItemClicked())
                {
                    SetNewComponent(new DrawableLogicGate(GetMousePositionInWorld(), "NAND", new NANDGateLogic(), true));
                }

                ImGui.Button("OR", buttonSize);
                if (ImGui.IsItemClicked())
                {
                    SetNewComponent(new DrawableLogicGate(GetMousePositionInWorld(), "OR", new ORGateLogic(), true));
                }
                ImGui.Button("NOR", buttonSize);
                if (ImGui.IsItemClicked())
                {
                    SetNewComponent(new DrawableLogicGate(GetMousePositionInWorld(), "NOR", new NORGateLogic(), true));
                }

                ImGui.Button("XOR", buttonSize);
                if (ImGui.IsItemClicked())
                {
                    SetNewComponent(new DrawableLogicGate(GetMousePositionInWorld(), "XOR", new XORGateLogic(), true));
                }
                ImGui.Button("XNOR", buttonSize);
                if (ImGui.IsItemClicked())
                {
                    SetNewComponent(new DrawableLogicGate(GetMousePositionInWorld(), "XNOR", new XNORGateLogic(), true));
                }

                ImGui.Button("NOT", buttonSize);
                if (ImGui.IsItemClicked())
                {
                    SetNewComponent(new DrawableLogicGate(GetMousePositionInWorld(), "NOT", new NOTGateLogic(), true));
                }


                ImGui.End();
            }

            #endregion

            #region IC WINDOW

            if (icWindowOpen)
            {
                if (ImGui.Begin("ICs", ImGuiWindowFlags.AlwaysAutoResize))
                {
                    foreach (ICCollection collection in collections)
                    {
                        ImGui.Text(collection.Name);

                        foreach (ICDescription descr in collection.Descriptions)
                        {
                            ImGui.Button(descr.Name, Utility.BUTTON_DEFAULT_SIZE);
                            if (ImGui.IsItemClicked())
                            {
                                SetNewComponent(new DrawableIC(GetMousePositionInWorld(), descr.Name, descr, true));
                            }
                        }
                        ImGui.Separator();
                    }

                    ImGui.Text("In Project");
                    foreach (ICDescription desc in nonCollectionDescriptions)
                    {
                        ImGui.Button(desc.Name, Utility.BUTTON_DEFAULT_SIZE);
                        if (ImGui.IsItemClicked())
                        {
                            SetNewComponent(new DrawableIC(GetMousePositionInWorld(), desc.Name, desc, true));
                        }
                    }

                    ImGui.End();
                }
            }

            #endregion

            #region SET ID TO CIRCUIT IO WINDOW
            if (currentProject.Simulation.SelectedComponents.Count == 1)
            {
                if (currentProject.Simulation.SelectedComponents[0] is DrawableCircuitSwitch || currentProject.Simulation.SelectedComponents[0] is DrawableCircuitLamp)
                {
                    if (ImGui.Begin("Setting IO ID", ImGuiWindowFlags.AlwaysAutoResize))
                    {
                        if (currentProject.Simulation.SelectedComponents[0] is DrawableCircuitSwitch)
                        {
                            DrawableCircuitSwitch dcs = (DrawableCircuitSwitch)currentProject.Simulation.SelectedComponents[0];
                            ImGui.InputText("ID", ref dcs.ID, 10);
                        }
                        if (currentProject.Simulation.SelectedComponents[0] is DrawableCircuitLamp)
                        {
                            DrawableCircuitLamp dcs = (DrawableCircuitLamp)currentProject.Simulation.SelectedComponents[0];
                            ImGui.InputText("ID", ref dcs.ID, 10);
                        }

                        ImGui.End();
                    }
                }
            }
            #endregion

            #region CREATE NEW IC WINDOW
            if (_makingIc)
            {
                if (ICDescription.ValidateComponents(currentProject.Simulation.SelectedComponents))
                {
                    ImGui.OpenPopup("Create Integrated Circuit");

                    if (icInputs == null)
                    {
                        icInputs  = currentProject.Simulation.SelectedComponents.Where(x => x.GetType() == typeof(DrawableCircuitSwitch)).ToArray();
                        icOutputs = currentProject.Simulation.SelectedComponents.Where(x => x.GetType() == typeof(DrawableCircuitLamp)).ToArray();
                        theRest   = currentProject.Simulation.SelectedComponents.Where(x => x.GetType() != typeof(DrawableCircuitLamp) && x.GetType() != typeof(DrawableCircuitSwitch)).ToArray();
                    }
                }
                else
                {
                    _makingIc = false;
                    ImGui.OpenPopup("Error");
                }
            }

            Vector2 middleOfWindow = WindowSize / 2f;
            ImGui.SetNextWindowPos(middleOfWindow, ImGuiCond.Always, new Vector2(0.5f, 0.5f));
            if (ImGui.BeginPopupModal("Error", ref yes, ImGuiWindowFlags.AlwaysAutoResize))
            {
                ImGui.Text("Unable to create IC from selection.");
                ImGui.TextWrapped("Make sure you've named all IOs and selected all components!");
                ImGui.Separator();

                if (ImGui.Button("Close", Utility.BUTTON_DEFAULT_SIZE))
                {
                    ImGui.CloseCurrentPopup();
                }
                ImGui.EndPopup();
            }

            //ImGui.SetNextWindowPos(WindowSize / 2f, ImGuiCond.Appearing, new Vector2(0.5f, 0.5f));
            if (ImGui.BeginPopupModal("Create Integrated Circuit", ref yes, ImGuiWindowFlags.AlwaysAutoResize | ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoSavedSettings | ImGuiWindowFlags.NoTitleBar | ImGuiWindowFlags.NoResize))
            {
                ImGui.InputText("Name", ref newIcName, 18);
                ImGui.Columns(2);
                ImGui.Text("Inputs");
                ImGui.SameLine();
                Utility.GuiHelpMarker("Here you can name your IC, as well as reorder your inputs and outputs.");
                ImGui.NextColumn();
                ImGui.Text("Outputs");
                ImGui.Columns(1);
                ImGui.Columns(2, "IOs");
                ImGui.Separator();

                for (int n = 0; n < icInputs.Length; n++)
                {
                    DrawableCircuitSwitch item = (DrawableCircuitSwitch)icInputs[n];
                    ImGui.Selectable(item.ID);

                    if (ImGui.IsItemActive() && !ImGui.IsItemHovered())
                    {
                        int n_next = n + (ImGui.GetMouseDragDelta(ImGuiMouseButton.Left).Y < 0 ? -1 : 1);
                        if (n_next >= 0 && n_next < icInputs.Length)
                        {
                            icInputs[n]      = icInputs[n_next];
                            icInputs[n_next] = item;
                            ImGui.ResetMouseDragDelta();
                        }
                    }
                }

                ImGui.NextColumn();

                for (int n = 0; n < icOutputs.Length; n++)
                {
                    DrawableCircuitLamp item = (DrawableCircuitLamp)icOutputs[n];
                    ImGui.Selectable(item.ID);

                    if (ImGui.IsItemActive() && !ImGui.IsItemHovered())
                    {
                        int n_next = n + (ImGui.GetMouseDragDelta(0).Y < 0 ? -1 : 1);
                        if (n_next >= 0 && n_next < icOutputs.Length)
                        {
                            icOutputs[n]      = icOutputs[n_next];
                            icOutputs[n_next] = item;
                            ImGui.ResetMouseDragDelta();
                        }
                    }
                }

                ImGui.NextColumn();
                ImGui.Columns(1);
                ImGui.Separator();

                if (ImGui.Button("Close", Utility.BUTTON_DEFAULT_SIZE))
                {
                    _makingIc = false;
                    icInputs  = null;
                    ImGui.CloseCurrentPopup();
                }

                ImGui.SameLine();

                if (ImGui.Button("Create", Utility.BUTTON_DEFAULT_SIZE))
                {
                    List <DrawableComponent> comps = icInputs.ToList();
                    comps.AddRange(icOutputs);
                    comps.AddRange(theRest);

                    ICDescription icd  = new ICDescription(comps);
                    string        path = icd.SaveToFile(newIcName);
                    //currentProject.Simulation.AddComponent(new DrawableIC(new Vector2(100, 100), newIcName, icd, true));
                    IncludeDescriptions(path);
                    newIcName = "";
                    _makingIc = false;
                    icInputs  = null;
                    ImGui.CloseCurrentPopup();
                }

                ImGui.EndPopup();
            }
            #endregion

            #region DEBUG WINDOW

            {
                ImGui.Begin("Debug", ImGuiWindowFlags.AlwaysAutoResize);
                ImGui.Text($"Editor State: {state.ToString()}");
                ImGui.Separator();

                int switches = currentProject.Simulation.SelectedComponents.Where(x => x.GetType() == typeof(DrawableCircuitSwitch)).Count();
                int lamps    = currentProject.Simulation.SelectedComponents.Where(x => x.GetType() == typeof(DrawableCircuitLamp)).Count();
                int gates    = currentProject.Simulation.SelectedComponents.Where(x => x.GetType() == typeof(DrawableLogicGate)).Count();
                int ics      = currentProject.Simulation.SelectedComponents.Where(x => x.GetType() == typeof(DrawableIC)).Count();

                if (ImGui.CollapsingHeader($"Selected Components", ImGuiTreeNodeFlags.CollapsingHeader))
                {
                    ImGui.Text($"Switches: {switches}");
                    ImGui.Text($"Lamps: {lamps}");
                    ImGui.Text($"Gates: {gates}");
                    ImGui.Text($"ICs: {ics}");
                    ImGui.Separator();
                    ImGui.Text($"Total: {currentProject.Simulation.SelectedComponents.Count}");
                }
                ImGui.End();
            }

            #endregion

            #region SETTINGS WINDOW

            if (editingSettings)
            {
                if (ImGui.Begin("Settings", ImGuiWindowFlags.AlwaysAutoResize))
                {
                    if (ImGui.BeginTabBar("Settings"))
                    {
                        if (ImGui.BeginTabItem("Appearance"))
                        {
                            // IO Vertical distance
                            Utility.GuiSettingFloatSlider("IO Vertical Distance", "io-v-dist", 14f, 1, 50);

                            // IO Horizontal distance
                            Utility.GuiSettingFloatSlider("IO Horizontal Distance", "io-h-dist", 10f, 1, 50);

                            // IO Size
                            Utility.GuiSettingFloatSlider("IO Size", "io-size", 6, 1, 20);
                        }

                        ImGui.EndTabBar();
                    }

                    if (ImGui.Button("Save", Utility.BUTTON_DEFAULT_SIZE))
                    {
                        SettingManager.SaveSettings();
                        editingSettings = false;
                    }
                    ImGui.SameLine();
                    if (ImGui.Button("Close", Utility.BUTTON_DEFAULT_SIZE))
                    {
                        editingSettings = false;
                    }
                    ImGui.End();
                }
            }

            #endregion

            #region CREATE COLLECTION WINDOW

            if (creatingCollection)
            {
                ImGui.OpenPopup("Create IC Collection");
                creatingCollection = false;
                selectedIcs        = new List <ICDescription>();
                newCollectionName  = "";
            }

            ImGui.SetNextWindowPos(WindowSize / 2f, ImGuiCond.Appearing, new Vector2(0.5f, 0.5f));
            if (ImGui.BeginPopupModal("Create IC Collection", ref yes, ImGuiWindowFlags.AlwaysAutoResize))
            {
                ImGui.Text("Select integrated components.");

                Vector2 size = new Vector2(300, 200);

                if (ImGui.BeginChild("ics", size, true))
                {
                    foreach (ICCollection coll in collections)
                    {
                        ImGui.TextDisabled(coll.Name);

                        foreach (ICDescription icd in coll.Descriptions)
                        {
                            if (ImGui.Selectable(icd.Name, selectedIcs.Contains(icd)))
                            {
                                if (!selectedIcs.Contains(icd))
                                {
                                    selectedIcs.Add(icd);
                                }
                                else
                                {
                                    selectedIcs.Remove(icd);
                                }
                            }
                        }
                    }

                    ImGui.TextDisabled("In Project");

                    foreach (ICDescription icd in nonCollectionDescriptions)
                    {
                        if (ImGui.Selectable(icd.Name, selectedIcs.Contains(icd)))
                        {
                            if (!selectedIcs.Contains(icd))
                            {
                                selectedIcs.Add(icd);
                            }
                            else
                            {
                                selectedIcs.Remove(icd);
                            }
                        }
                    }

                    ImGui.EndChild();
                }

                ImGui.InputText("Collection name", ref newCollectionName, 20);
                ImGui.SameLine();
                if (ImGui.Button("Close"))
                {
                    ImGui.CloseCurrentPopup();
                }
                ImGui.SameLine();
                if (ImGui.Button("Create"))
                {
                    ICCollection icc = new ICCollection(newCollectionName, selectedIcs);

                    using (StreamWriter sw = new StreamWriter(Utility.ASSETS_DIR + @"/" + icc.Name + Utility.EXT_ICCOLLECTION))
                    {
                        sw.Write(JsonConvert.SerializeObject(icc));
                    }
                    ImGui.CloseCurrentPopup();
                }

                ImGui.EndPopup();
            }

            #endregion

            if (fd?.Done() == true)
            {
                if (fd.SelectedFiles.Count > 0)
                {
                    switch (state)
                    {
                    case EditorState.SelectingProjectFile:
                        SetProject(LogiXProject.LoadFromFile(fd.SelectedFiles[0]));
                        break;

                    case EditorState.IncludeCollection:
                        foreach (string collection in fd.SelectedFiles)
                        {
                            IncludeCollection(collection);
                        }
                        break;

                    case EditorState.IncludeDescription:
                        foreach (string description in fd.SelectedFiles)
                        {
                            IncludeDescriptions(description);
                        }
                        break;

                    case EditorState.SavingProjectFile:
                        currentProject.SaveProjectToFile(fd.SelectedFiles[0]);
                        SettingManager.SetSetting("latest-project", fd.SelectedFiles[0]);
                        SettingManager.SaveSettings();
                        break;
                    }
                    state = EditorState.None;
                }
                fd = null;
            }
        }