Esempio n. 1
0
        static void Main(string[] args)
        {
            var dispose = new DisposeGroup();

            // Initialize window and imgui
            var view = dispose.Add(new RenderView());
            var gd   = view.GraphicsDevice;

            var cl = gd.ResourceFactory.CreateCommandList();

            var imGui = new ImGuiView(gd, view.Window, gd.MainSwapchain.Framebuffer.OutputDescription, view.Width, view.Height);

            ImGui.GetIO().ConfigWindowsMoveFromTitleBarOnly = true;

            //Create ImGui Windows
            var menu     = new UIMenu("Hello World!", new IUIComponent[] { new UIMenuItem("Test 1"), new UIMenuItem("Test 2", "CTRL+Z") });
            var mainmenu = new UIMainMenuBar(new IUIComponent[] { menu });

            var uiwindow = new UIWindow("Abstracted ImGui!", new IUIComponent[] { new UIText("Test Text"), new UICheckbox("Test Checkbox", false) });

            var pattern         = new MIDIPattern();
            var projectConnect  = new ProjectConnect();
            var pianoRollWindow = UIUtils.CreatePianoRollWindow(projectConnect, pattern, gd, imGui);

            // Initialize imgui UI
            var uihost = dispose.Add(new UIHost(new IUIComponent[] { mainmenu, uiwindow, pianoRollWindow }));

            Stopwatch frameTimer = new Stopwatch();

            frameTimer.Start();

            var hotkeys = new HotkeyHandler <GlobalHotkey>();

            // Main application loop
            while (view.Exists)
            {
                if (!view.Exists)
                {
                    break;
                }
                imGui.Update((float)frameTimer.Elapsed.TotalSeconds, view.Width, view.Height);
                frameTimer.Reset();
                frameTimer.Start();

                cl.Begin();

                // Compute UI elements, render canvases
                ImGui.DockSpaceOverViewport();
                uihost.Render(cl);
                ImGui.ShowDemoWindow();
                hotkeys.Update(true);
                if (hotkeys.CurrentHotkey == GlobalHotkey.Undo)
                {
                    projectConnect.Undo();
                }
                if (hotkeys.CurrentHotkey == GlobalHotkey.Redo)
                {
                    projectConnect.Redo();
                }
                Console.WriteLine(hotkeys.CurrentHotkey);

                ImGui.Text(ImGui.GetIO().Framerate.ToString());

                imGui.UpdateViewIO(view);

                cl.SetFramebuffer(gd.MainSwapchain.Framebuffer);
                cl.ClearColorTarget(0, new RgbaFloat(clearColor.X, clearColor.Y, clearColor.Z, 1f));
                imGui.Render(gd, cl);
                cl.End();
                gd.SubmitCommands(cl);
                gd.SwapBuffers(gd.MainSwapchain);
                imGui.SwapExtraWindows(gd);
            }

            dispose.Dispose();
        }
Esempio n. 2
0
        public TestCanvas(GraphicsDevice gd, ImGuiView view, Func <Vector2> computeSize) : base(gd, view, computeSize)
        {
            Buffer = dispose.Add(new BufferList <VertexPositionColor>(gd, 6));

            VertexPositionColor[] quadVertices =
            {
                new VertexPositionColor(new Vector2(-150f,  150f), RgbaFloat.Red),
                new VertexPositionColor(new Vector2(150f,   150f), RgbaFloat.Green),
                new VertexPositionColor(new Vector2(-150f, -150f), RgbaFloat.Blue),
                new VertexPositionColor(new Vector2(150f,  -150f), RgbaFloat.Yellow)
            };

            ushort[] quadIndices = { 0, 1, 2, 3 };

            VertexBuffer = Factory.CreateBuffer(new BufferDescription(4 * VertexPositionColor.SizeInBytes, BufferUsage.VertexBuffer | BufferUsage.Dynamic));
            IndexBuffer  = Factory.CreateBuffer(new BufferDescription(4 * sizeof(ushort), BufferUsage.IndexBuffer));
            dispose.Add(VertexBuffer);
            dispose.Add(IndexBuffer);

            ProjMatrix = Factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer | BufferUsage.Dynamic));
            dispose.Add(ProjMatrix);

            GraphicsDevice.UpdateBuffer(VertexBuffer, 0, quadVertices);
            GraphicsDevice.UpdateBuffer(IndexBuffer, 0, quadIndices);

            VertexLayoutDescription vertexLayout = new VertexLayoutDescription(
                new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2),
                new VertexElementDescription("Color", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float4));

            Layout = Factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                      new ResourceLayoutElementDescription("ProjectionMatrixBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex)));
            dispose.Add(Layout);

            ShaderDescription vertexShaderDesc = new ShaderDescription(
                ShaderStages.Vertex,
                Encoding.UTF8.GetBytes(VertexCode),
                "main");
            ShaderDescription fragmentShaderDesc = new ShaderDescription(
                ShaderStages.Fragment,
                Encoding.UTF8.GetBytes(FragmentCode),
                "main");

            Shaders = dispose.AddArray(Factory.CreateFromSpirv(vertexShaderDesc, fragmentShaderDesc));

            var pipelineDescription = new GraphicsPipelineDescription();

            pipelineDescription.BlendState        = BlendStateDescription.SingleOverrideBlend;
            pipelineDescription.DepthStencilState = new DepthStencilStateDescription(
                depthTestEnabled: true,
                depthWriteEnabled: true,
                comparisonKind: ComparisonKind.LessEqual);
            pipelineDescription.RasterizerState = new RasterizerStateDescription(
                cullMode: FaceCullMode.Front,
                fillMode: PolygonFillMode.Solid,
                frontFace: FrontFace.Clockwise,
                depthClipEnabled: true,
                scissorTestEnabled: false);
            pipelineDescription.PrimitiveTopology = PrimitiveTopology.TriangleList;
            pipelineDescription.ResourceLayouts   = new ResourceLayout[] { Layout };
            pipelineDescription.ShaderSet         = new ShaderSetDescription(
                vertexLayouts: new VertexLayoutDescription[] { vertexLayout },
                shaders: Shaders);
            pipelineDescription.Outputs = Canvas.FrameBuffer.OutputDescription;

            Pipeline = Factory.CreateGraphicsPipeline(pipelineDescription);

            MainResourceSet = Factory.CreateResourceSet(new ResourceSetDescription(Layout, ProjMatrix));
        }