internal static void DrawNodesToImage(out byte[] imageRawBytes, IList <Node> nodes, int width, int height) { MeshBuffer meshBuffer = new MeshBuffer(); MeshList meshList = new MeshList(); IPrimitiveRenderer primitiveRenderer = new BuiltinPrimitiveRenderer(); using (var context = new RenderContextForTest(width, height)) { //This must be called after the context is created, for creating textures when the OpenGL context is built. foreach (var node in nodes) { node.Draw(primitiveRenderer, meshList); } //rebuild mesh buffer meshBuffer.Clear(); meshBuffer.Init(); meshBuffer.Build(meshList); //draw mesh buffer to screen context.Clear(); context.DrawMeshes(meshBuffer); imageRawBytes = context.GetRenderedRawBytes(); } }
internal static void DrawNodeToImage_NewPipeline(out byte[] imageRawBytes, Node node, int width, int height) { Application.EnableMSAA = false; MeshBuffer meshBuffer = new MeshBuffer(); MeshList meshList = new MeshList(); BuiltinGeometryRenderer geometryRenderer = new BuiltinGeometryRenderer(); using (var context = new RenderContextForTest(width, height)) { RenderContext renderContext = new RenderContext(geometryRenderer, meshList); //This must be called after the RenderContextForTest is created, for uploading textures to GPU via OpenGL. node.Render(renderContext); //rebuild mesh buffer meshBuffer.Clear(); meshBuffer.Init(); meshBuffer.Build(meshList); //draw mesh buffer to screen context.Clear(); context.DrawMeshes(meshBuffer); imageRawBytes = context.GetRenderedRawBytes(); } }
internal static void CheckRectangle(Brush brush, Pen pen, Rect rectangle, int width, int height, [CallerMemberName] string methodName = "unknown") { Application.EnableMSAA = false; MeshBuffer meshBuffer = new MeshBuffer(); MeshList meshList = new MeshList(); BuiltinGeometryRenderer renderer = new BuiltinGeometryRenderer(); byte[] bytes; using (var context = new RenderContextForTest(width, height)) { renderer.OnBeforeRead(); renderer.DrawRectangle(brush, pen, rectangle);//This must be called after the RenderContextForTest is created, for uploading textures to GPU via OpenGL. renderer.OnAfterRead(meshList); //rebuild mesh buffer meshBuffer.Clear(); meshBuffer.Init(); meshBuffer.Build(meshList); //draw mesh buffer to screen context.Clear(); context.DrawMeshes(meshBuffer); bytes = context.GetRenderedRawBytes(); } Util.CheckExpectedImage(bytes, width, height, $"{RootDir}{nameof(DrawRectangle)}\\{methodName}.png"); }
internal static void DrawNodeTreeToImage(out byte[] imageRawBytes, Node root, int width, int height, Rect clipRect) { if (root == null) { throw new ArgumentNullException(nameof(root)); } MeshBuffer meshBuffer = new MeshBuffer(); MeshList meshList = new MeshList(); BuiltinGeometryRenderer geometryRenderer = new BuiltinGeometryRenderer(); using (var context = new RenderContextForTest(width, height)) { if (root is Node rootNode) { using (var dc = rootNode.RenderOpen()) { dc.DrawBoxModel(rootNode.RuleSet, rootNode.Rect); } } root.Foreach(visual => { if (!(visual is Node node)) { return(true); } using (var dc = node.RenderOpen()) { dc.DrawBoxModel(node.RuleSet, node.Rect); } return(true); }); //This must be called after the context is created, for uploading textures to GPU via OpenGL. root.Render(new RenderContext(geometryRenderer, meshList)); //rebuild mesh buffer meshBuffer.Clear(); meshBuffer.Init(); meshBuffer.Build(meshList); //draw mesh buffer to screen context.Clear(); context.DrawMeshes(meshBuffer); imageRawBytes = context.GetRenderedRawBytes(); } }
internal void Render() { GUIContext g = this.uiContext; WindowManager w = g.WindowManager; Debug.Assert(g.Initialized); // Forgot to call NewFrame() if (g.FrameCountEnded != g.FrameCount) { EndFrame(); } g.FrameCountRendered = g.FrameCount; var openGLRenderer = (Win32OpenGLRenderer)renderer; //FIXME TEMP openGLRenderer.Clear(this.BackgroundColor); foreach (var window in w.Windows) { if (window.Active) { window.RenderTree.Foreach(node => { if (!node.ActiveInTree) { return(false); } node.Draw(this.primitiveRenderer); return(true); }); //rebuild mesh buffer MeshBuffer.Clear(); MeshBuffer.Init(); MeshBuffer.Build(); //draw mesh buffer openGLRenderer.DrawMeshes((int)this.ClientSize.Width, (int)this.ClientSize.Height); } } openGLRenderer.SwapBuffers(); }
internal static void DrawNodeTreeToImage(out byte[] imageRawBytes, Node root, int width, int height, Rect clipRect) { MeshBuffer meshBuffer = new MeshBuffer(); MeshList meshList = new MeshList(); var primitiveRenderer = new BuiltinPrimitiveRenderer(); using (var context = new RenderContextForTest(width, height)) { //This must be called after the context is created, for uploading textures to GPU via OpenGL. root.Foreach(n => primitiveRenderer.Draw(n, clipRect, meshList)); //rebuild mesh buffer meshBuffer.Clear(); meshBuffer.Init(); meshBuffer.Build(meshList); //draw mesh buffer to screen context.Clear(); context.DrawMeshes(meshBuffer); imageRawBytes = context.GetRenderedRawBytes(); } }
internal static void DrawDrawingVisualToImage(out byte[] imageRawBytes, int width, int height, DrawingVisual drawingVisual) { //convert geometries inside the drawingVisual's content to meshes stored in a MeshList with a BuiltinGeometryRenderer MeshList meshList = new MeshList(); using BuiltinGeometryRenderer geometryRenderer = new BuiltinGeometryRenderer(); RenderContext renderContext = new RenderContext(geometryRenderer, meshList); drawingVisual.RenderContent(renderContext); //merge meshes in the MeshList to a MeshBuffer MeshBuffer meshBuffer = new MeshBuffer(); meshBuffer.Clear(); meshBuffer.Init(); meshBuffer.Build(meshList); //created a mesh IRenderer Application.Init(); var window = Application.PlatformContext.CreateWindow(Point.Zero, new Size(100, 100), WindowTypes.Hidden); var renderer = Application.PlatformContext.CreateRenderer() as Win32OpenGLRenderer;//TEMP HACK Debug.Assert(renderer != null, nameof(renderer) + " != null"); renderer.Init(window.Pointer, window.ClientSize); //clear the canvas and draw mesh in the MeshBuffer with the mesh renderer renderer.Clear(Color.White); renderer.StartDrawMeshToImage(width, height); imageRawBytes = renderer.DrawMeshToImage(width, height, meshBuffer.ShapeMesh, OpenGLMaterial.shapeMaterial); renderer.EndDrawMeshToImage(); //clear native resources: window and IRenderer renderer.ShutDown(); window.Close(); }
public void DrawOneTextNodeAtPosition() { Application.IsRunningInUnitTest = true; Application.InitSysDependencies(); var primitiveRenderer = new BuiltinPrimitiveRenderer(); Node node = new Node(1); var primitive = new TextPrimitive(); primitive.Text = "AAA"; node.Primitive = primitive; node.Draw(primitiveRenderer); node.Rect.X = 100; node.Rect.Y = 30; var window = new Win32Window(); window.Init(new Point(100, 100), new Size(300, 400), WindowTypes.Regular); var renderer = new Win32OpenGLRenderer(); renderer.Init(window.Pointer, window.ClientSize); window.Show(); while (true) { Time.OnFrameBegin(); Keyboard.Instance.OnFrameBegin(); window.MainLoop(() => { if (Keyboard.Instance.KeyDown(Key.Escape)) { Application.Quit(); } //update the node if (node.ActiveInTree) { node.Draw(primitiveRenderer); } //rebuild mesh buffer MeshBuffer.Clear(); MeshBuffer.Init(); MeshBuffer.Build(); //draw mesh buffer to screen renderer.Clear(Color.FrameBg); renderer.DrawMeshes((int)window.ClientSize.Width, (int)window.ClientSize.Height); renderer.SwapBuffers(); }); if (Application.RequestQuit) { break; } Keyboard.Instance.OnFrameEnd(); Time.OnFrameEnd(); } }
public void ShowHideANode() { Application.IsRunningInUnitTest = true; Application.InitSysDependencies(); var primitiveRenderer = new BuiltinPrimitiveRenderer(); var nodes = new List <Node>(); { Node node = new Node(1); nodes.Add(node); var primitive = new PathPrimitive(); primitive.PathMoveTo(new Point(10, 10)); primitive.PathLineTo(new Point(10, 100)); primitive.PathLineTo(new Point(100, 100)); primitive.PathLineTo(new Point(100, 10)); primitive.PathClose(); node.Primitive = primitive; node.IsFill = true; node.Brush = new Brush(); node.Brush.FillColor = Color.Red; node.Draw(primitiveRenderer); } var theNode = nodes[0]; var window = new Win32Window(); window.Init(new Point(100, 100), new Size(300, 400), WindowTypes.Regular); var renderer = new Win32OpenGLRenderer(); renderer.Init(window.Pointer, window.ClientSize); window.Show(); while (true) { Time.OnFrameBegin(); Keyboard.Instance.OnFrameBegin(); window.MainLoop(() => { if (Keyboard.Instance.KeyPressed(Key.Space)) { theNode.ActiveSelf = !theNode.ActiveSelf; Log.Msg("Key.Space Pressed. theNode becomes " + (theNode.ActiveSelf ? "visible" : "invisible")); } if (Keyboard.Instance.KeyDown(Key.Escape)) { Application.Quit(); } //update nodes foreach (var node in nodes) { if (node.ActiveInTree) { node.Draw(primitiveRenderer); } } //rebuild mesh buffer MeshBuffer.Clear(); MeshBuffer.Init(); MeshBuffer.Build(); //draw mesh buffer to screen renderer.Clear(Color.FrameBg); renderer.DrawMeshes((int)window.ClientSize.Width, (int)window.ClientSize.Height); renderer.SwapBuffers(); }); if (Application.RequestQuit) { break; } Keyboard.Instance.OnFrameEnd(); Time.OnFrameEnd(); } }
public void UpdateANode() { Application.IsRunningInUnitTest = true; Application.InitSysDependencies(); var primitiveRenderer = new BuiltinPrimitiveRenderer(); Node node = new Node(1); var primitive = new PathPrimitive(); primitive.PathMoveTo(new Point(10, 10)); primitive.PathLineTo(new Point(10, 100)); primitive.PathLineTo(new Point(100, 100)); primitive.PathLineTo(new Point(100, 10)); primitive.PathClose(); node.Primitive = primitive; node.IsFill = true; node.Brush = new Brush(); node.Draw(primitiveRenderer); var window = new Win32Window(); window.Init(new Point(100, 100), new Size(300, 400), WindowTypes.Regular); var renderer = new Win32OpenGLRenderer(); renderer.Init(window.Pointer, window.ClientSize); window.Show(); while (true) { Time.OnFrameBegin(); Keyboard.Instance.OnFrameBegin(); window.MainLoop(() => { if (Keyboard.Instance.KeyDown(Key.D1)) { node.Brush.FillColor = Color.Red; } if (Keyboard.Instance.KeyDown(Key.D2)) { node.Brush.FillColor = Color.Green; } if (Keyboard.Instance.KeyDown(Key.D3)) { node.Brush.FillColor = Color.Blue; } if (Keyboard.Instance.KeyDown(Key.Escape)) { Application.Quit(); } //update nodes if (node.ActiveInTree)//this is actually always true { node.Draw(primitiveRenderer); } //rebuild mesh buffer MeshBuffer.Clear(); MeshBuffer.Init(); MeshBuffer.Build(); //draw mesh buffer to screen renderer.Clear(Color.FrameBg); renderer.DrawMeshes((int)window.ClientSize.Width, (int)window.ClientSize.Height); renderer.SwapBuffers(); }); if (Application.RequestQuit) { break; } Keyboard.Instance.OnFrameEnd(); Time.OnFrameEnd(); } }
public void ShowAnimateNode() { //FIXME make this test automatable Application.IsRunningInUnitTest = true; Application.InitSysDependencies(); MeshBuffer meshBuffer = new MeshBuffer(); MeshList meshList = new MeshList(); var primitiveRenderer = new BuiltinPrimitiveRenderer(); Node node = new Node(1); node.Primitive = new PathPrimitive(); var window = new Win32Window(); window.Init(new Point(100, 100), new Size(800, 600), WindowTypes.Regular); var renderer = new Win32OpenGLRenderer(); renderer.Init(window.Pointer, window.ClientSize); window.Show(); var A = new Point(200, 200); var B = new Point(600, 200); var C = new Point(400, 400); while (true) { Time.OnFrameBegin(); Keyboard.Instance.OnFrameBegin(); window.MainLoop(() => { if (Keyboard.Instance.KeyDown(Key.Escape)) { Application.Quit(); } var normal = (Time.time % 1000) / 1000f * 2 - 1; var rad = normal * Math.PI; var A_ = A + 50 * new Vector(Math.Cos(rad) - Math.Sin(rad), Math.Sin(rad) + Math.Cos(rad)); rad += Math.PI * 0.333; var B_ = B + 30 * new Vector(Math.Cos(rad) - Math.Sin(rad), Math.Sin(rad) + Math.Cos(rad)); rad += Math.PI * 0.666; var C_ = C + 70 * new Vector(Math.Cos(rad) - Math.Sin(rad), Math.Sin(rad) + Math.Cos(rad)); var d = node.Primitive as PathPrimitive; d.PathClear(); d.PathMoveTo(A_); d.PathLineTo(B_); d.PathLineTo(C_); d.PathStroke(2, Color.Blue); //update nodes if (node.ActiveInTree)//this is actually always true { node.Draw(primitiveRenderer, meshList); } //rebuild mesh buffer meshBuffer.Clear(); meshBuffer.Init(); meshBuffer.Build(meshList); //draw mesh buffer to screen renderer.Clear(Color.FrameBg); renderer.DrawMeshes((int)window.ClientSize.Width, (int)window.ClientSize.Height, (shapeMesh: meshBuffer.ShapeMesh, imageMesh: meshBuffer.ImageMesh, meshBuffer.TextMesh)); renderer.SwapBuffers(); }); if (Application.RequestQuit) { break; } Keyboard.Instance.OnFrameEnd(); Time.OnFrameEnd(); } }
public void DrawAWindow() { Application.IsRunningInUnitTest = true; Application.InitSysDependencies(); var primitiveRenderer = new BuiltinPrimitiveRenderer(); MeshBuffer meshBuffer = new MeshBuffer(); MeshList meshList = new MeshList(); var window = new Win32Window(); window.Init(new Point(100, 100), new Size(800, 600), WindowTypes.Regular); var renderer = new Win32OpenGLRenderer(); renderer.Init(window.Pointer, window.ClientSize); window.Show(); bool DrawNode(Node n, MeshList list) { if (!n.ActiveInTree) { return(false); } n.Draw(primitiveRenderer, list); return(true); } //window var windowContainer = new Node("#window"); windowContainer.AttachLayoutGroup(true); windowContainer.RuleSet.ApplyOptions(GUILayout.Width(400)); windowContainer.UseBoxModel = true; windowContainer.RuleSet.Border = (1, 1, 1, 1); windowContainer.RuleSet.BackgroundColor = Color.White; //title bar { var titleBarContainer = new Node(1, "#titleBar"); titleBarContainer.AttachLayoutGroup(false); titleBarContainer.RuleSet.ApplyOptions(GUILayout.ExpandWidth(true).Height(40)); titleBarContainer.UseBoxModel = true; StyleRuleSetBuilder b = new StyleRuleSetBuilder(titleBarContainer); b.Padding((top: 8, right: 8, bottom: 8, left: 8)) .FontColor(Color.Black) .FontSize(12) .BackgroundColor(Color.White) .AlignmentVertical(Alignment.Center) .AlignmentHorizontal(Alignment.Start); var icon = new Node(2, "#icon"); icon.AttachLayoutEntry(new Size(20, 20)); icon.RuleSet.ApplyOptions(GUILayout.Width(20).Height(20)); icon.UseBoxModel = false; icon.Primitive = new ImagePrimitive(@"assets\images\logo.png"); var title = new Node(3, "#title"); title.AttachLayoutEntry(Size.Zero); title.RuleSet.ApplyOptions(GUILayout.Height(20)); title.UseBoxModel = false; title.Primitive = new TextPrimitive("The Window Title"); var closeButton = new Node(4, "#close button"); closeButton.AttachLayoutEntry(new Size(20, 20)); closeButton.RuleSet.ApplyOptions(GUILayout.Width(20).Height(20)); closeButton.UseBoxModel = false; PathPrimitive path = new PathPrimitive(); path.PathRect(new Rect(0, 0, 20, 20)); closeButton.Primitive = path; titleBarContainer.AppendChild(icon); titleBarContainer.AppendChild(title); titleBarContainer.AppendChild(closeButton); windowContainer.AppendChild(titleBarContainer); } Node clientArea; //client area background { clientArea = new Node("#ClientArea_Background"); clientArea.AttachLayoutGroup(true); clientArea.RuleSet.ApplyOptions(GUILayout.ExpandWidth(true).Height(200)); windowContainer.AppendChild(clientArea); } while (true) { Time.OnFrameBegin(); Keyboard.Instance.OnFrameBegin(); window.MainLoop(() => { if (Keyboard.Instance.KeyDown(Key.Escape)) { Application.Quit(); } if (Keyboard.Instance.KeyPressed(Key.Space)) { clientArea.ActiveSelf = !clientArea.ActiveSelf; } { DrawNode(windowContainer, meshList); windowContainer.Foreach(n => DrawNode(n, meshList)); windowContainer.Layout(); } //rebuild mesh buffer meshBuffer.Clear(); meshBuffer.Init(); meshBuffer.Build(meshList); //draw mesh buffer to screen renderer.Clear(Color.FrameBg); renderer.DrawMeshes((int)window.ClientSize.Width, (int)window.ClientSize.Height, (shapeMesh: meshBuffer.ShapeMesh, imageMesh: meshBuffer.ImageMesh, meshBuffer.TextMesh)); renderer.SwapBuffers(); }); if (Application.RequestQuit) { break; } Keyboard.Instance.OnFrameEnd(); Time.OnFrameEnd(); } }
public void DrawAndLayoutEmptyContainer() { Application.IsRunningInUnitTest = true; Application.InitSysDependencies(); var primitiveRenderer = new BuiltinPrimitiveRenderer(); MeshBuffer meshBuffer = new MeshBuffer(); MeshList meshList = new MeshList(); var window = new Win32Window(); window.Init(new Point(100, 100), new Size(400, 400), WindowTypes.Regular); var renderer = new Win32OpenGLRenderer(); renderer.Init(window.Pointer, window.ClientSize); window.Show(); bool DrawNode(Node n, MeshList list) { if (!n.ActiveInTree) { return(false); } n.Draw(primitiveRenderer, list); return(true); } Node node = null; while (true) { Time.OnFrameBegin(); Keyboard.Instance.OnFrameBegin(); window.MainLoop(() => { if (Keyboard.Instance.KeyDown(Key.Escape)) { Application.Quit(); } if (node == null) { node = new Node(1, "container"); node.AttachLayoutGroup(true); node.RuleSet.ApplyOptions(GUILayout.Width(300).Height(40)); node.UseBoxModel = true; StyleRuleSetBuilder b = new StyleRuleSetBuilder(node); b.Border(1) .BorderColor(Color.Black) .Padding((top: 1, right: 2, bottom: 1, left: 2)) .BackgroundColor(Color.Silver); } { DrawNode(node, meshList); node.Foreach(n => DrawNode(n, meshList)); node.Layout(); } //rebuild mesh buffer meshBuffer.Clear(); meshBuffer.Init(); meshBuffer.Build(meshList); //draw mesh buffer to screen renderer.Clear(Color.FrameBg); renderer.DrawMeshes((int)window.ClientSize.Width, (int)window.ClientSize.Height, (shapeMesh: meshBuffer.ShapeMesh, imageMesh: meshBuffer.ImageMesh, meshBuffer.TextMesh)); renderer.SwapBuffers(); }); if (Application.RequestQuit) { break; } Keyboard.Instance.OnFrameEnd(); Time.OnFrameEnd(); } }
public void DrawAndLayoutContainerWithElements() { Application.IsRunningInUnitTest = true; Application.InitSysDependencies(); var primitiveRenderer = new BuiltinPrimitiveRenderer(); MeshBuffer meshBuffer = new MeshBuffer(); MeshList meshList = new MeshList(); var window = new Win32Window(); window.Init(new Point(100, 100), new Size(400, 400), WindowTypes.Regular); var renderer = new Win32OpenGLRenderer(); renderer.Init(window.Pointer, window.ClientSize); window.Show(); bool DrawNode(Node n, MeshList list) { if (!n.ActiveInTree) { return(false); } n.Draw(primitiveRenderer, list); return(true); } Node container = null; Node icon; Node title; Node closeButton; while (true) { Time.OnFrameBegin(); Keyboard.Instance.OnFrameBegin(); window.MainLoop(() => { if (Keyboard.Instance.KeyDown(Key.Escape)) { Application.Quit(); } if (container == null) { container = new Node(1, "container"); container.AttachLayoutGroup(false); container.RuleSet.ApplyOptions(GUILayout.Width(300).Height(40)); container.UseBoxModel = true; StyleRuleSetBuilder b = new StyleRuleSetBuilder(container); b.Border(1) .BorderColor(Color.Black) .Padding((top: 4, right: 3, bottom: 4, left: 3)) .BackgroundColor(Color.Silver) .AlignmentVertical(Alignment.Center) .AlignmentHorizontal(Alignment.Center); icon = new Node(2, "icon"); icon.AttachLayoutEntry(new Size(20, 20)); icon.RuleSet.ApplyOptions(GUILayout.Width(20).Height(20)); icon.UseBoxModel = false; icon.Primitive = new ImagePrimitive(@"assets\images\logo.png"); title = new Node(3, "title"); var titleTextSize = GUIStyle.Default.CalcSize("title", GUIState.Normal);//TODO consider this title.AttachLayoutEntry(titleTextSize); title.RuleSet.ApplyOptions(GUILayout.Height(20).ExpandWidth(true)); title.UseBoxModel = false; title.Primitive = new TextPrimitive("title"); closeButton = new Node(4, "close button"); closeButton.AttachLayoutEntry(new Size(20, 20)); closeButton.UseBoxModel = false; PathPrimitive path = new PathPrimitive(); path.PathRect(new Rect(0, 0, 20, 20)); path.PathFill(Color.Black); //path.PathClear(); //path.PathMoveTo((0, 0)); //path.PathLineTo((20,20)); //path.PathStroke(1, Color.Black); //path.PathClear(); // //path.PathMoveTo((0, 20)); //path.PathLineTo((20,0)); //path.PathStroke(1, Color.Black); //path.PathClear(); closeButton.Primitive = path; container.AppendChild(icon); container.AppendChild(title); container.AppendChild(closeButton); } { DrawNode(container, meshList); container.Foreach(n => DrawNode(n, meshList)); container.Layout(); } //rebuild mesh buffer meshBuffer.Clear(); meshBuffer.Init(); meshBuffer.Build(meshList); //draw mesh buffer to screen renderer.Clear(Color.FrameBg); renderer.DrawMeshes((int)window.ClientSize.Width, (int)window.ClientSize.Height, (shapeMesh: meshBuffer.ShapeMesh, imageMesh: meshBuffer.ImageMesh, meshBuffer.TextMesh)); renderer.SwapBuffers(); }); if (Application.RequestQuit) { break; } Keyboard.Instance.OnFrameEnd(); Time.OnFrameEnd(); } }