public void TestEmbeddedProperties() { //Given var node = new DrawableNode(); //When node.Load(@" <Box xmlns=""osufx://osu.Framework/*"" Size=""100,200"" RelativeSizeAxes=""Both""/>"); //Then Assert.Equal(typeof(Box).FullName, node.DrawableType.FullName); Assert.Collection(node.Properties, size => { Assert.Equal("Size", size.Name); Assert.IsType <EmbeddedDrawableProperty>(size); Assert.Equal(new Vector2(100, 200), ((EmbeddedDrawableProperty)size).ParsedValue); }, axes => { Assert.Equal("RelativeSizeAxes", axes.Name); Assert.IsType <EmbeddedDrawableProperty>(axes); Assert.Equal(Axes.Both, ((EmbeddedDrawableProperty)axes).ParsedValue); }); }
public void NumTextureChanged(int Value, int TexId) { NumTextures = Value; foreach (XmlNode tex in DrawableNode.SelectNodes("aTexData")) { if (onCreateDocElement == null) { return; } tex.RemoveAll(); XmlElement elemtex = (XmlElement)tex; elemtex.SetAttribute("itemType", "CPVTextureData"); for (int i = 0; i < Value; i++) { XmlElement elem_item = onCreateDocElement("Item"); elemtex.AppendChild(elem_item); XmlElement elem_texId = onCreateDocElement("texId"); elem_item.AppendChild(elem_texId); elem_texId.SetAttribute("value", TexId.ToString()); XmlElement elem_distr = onCreateDocElement("distribution"); elem_item.AppendChild(elem_distr); elem_distr.SetAttribute("value", "255"); } } }
public void TestLoadValidContainer() { //Given var node = new DrawableNode(); //When node.Load(@" <Container xmlns=""osufx://osu.Framework/*""> <Box/> <SpriteText/> <Container Name=""ContainerInContainer""> <Box/> </Container> </Container>"); // Then Assert.Equal(typeof(Container).FullName, node.DrawableType.FullName); Assert.Collection(node, box => Assert.Equal(typeof(Box).FullName, box.DrawableType.FullName), spriteText => Assert.Equal(typeof(SpriteText).FullName, spriteText.DrawableType.FullName), container => { Assert.Equal(typeof(Container).FullName, container.DrawableType.FullName); Assert.Collection(container, box => Assert.Equal(typeof(Box).FullName, box.DrawableType.FullName)); }); }
public DrawableCom() { DrawableNode = new DrawableNode() { Drawable = this }; }
public static ExpressionSyntax GenerateChildInitializer(DrawableNode node) { var list = new List <ExpressionSyntax>(); list.AddRange(node.Properties.Select(DrawablePropertyGenerator.GenerateInitializerSyntax)); if (node.IsContainer && node.Any()) { list.Add(GenerateChildrenInitializers(node)); } var expression = (ExpressionSyntax)ObjectCreationExpression( type: ParseTypeName(node.DrawableType.FullName), argumentList: null, initializer: InitializerExpression( kind: SyntaxKind.ObjectInitializerExpression, expressions: SeparatedList <ExpressionSyntax>(list) ) ); if (node.IsNameSpecified) { expression = AssignmentExpression( kind: SyntaxKind.SimpleAssignmentExpression, left: IdentifierName(node.GivenName), right: expression ); } return(expression); }
public static BlockSyntax GenerateBody(DrawableNode node) { var list = new List <StatementSyntax>(); list.AddRange(GenerateLocalBoundPropertyInitializers(node)); list.Add(ExpressionStatement(GenerateChildrenInitializers(node))); return(Block(List <StatementSyntax>(list))); }
public static SyntaxList <MemberDeclarationSyntax> GenerateMembers(DrawableNode node) { var list = new List <MemberDeclarationSyntax>(); list.AddRange(node.GeneratePropertySyntaxesOfDescendants()); list.Add(node.GenerateConstructorSyntax()); list.Add(node.GenerateBackgroundLoaderSyntax()); return(List <MemberDeclarationSyntax>(list)); }
public void TestNonexistentProperty() { //Given var node = new DrawableNode(); //Then Assert.Throws <MarkupException>(() => node.Load(@" <Box xmlns=""osufx://osu.Framework/*"" MyNonexistentProperty=""1234""/>")); }
public void MaskChanged(int Value) { Mask = Value; if (DrawableNode != null) { foreach (XmlNode mask in DrawableNode.SelectNodes("propMask")) { mask.Attributes["value"].Value = Value.ToString(); } } }
public static NamespaceDeclarationSyntax GenerateSyntax(this DrawableNode node, string namespaceName) { return(NamespaceDeclaration( name: ParseName(namespaceName), externs: List <ExternAliasDirectiveSyntax>(), usings: List <UsingDirectiveSyntax>(), members: SingletonList <MemberDeclarationSyntax>( node.GenerateClassSyntax() ) )); }
public void TestInvalidChildrenProperty() { //Given var node = new DrawableNode(); //Then Assert.Throws <MarkupException>(() => node.Load(@" <Box xmlns=""osufx://osu.Framework/*"" Name=""MyBox"" Children=""Should throw exception here""/>")); }
public void TestLoadInvalidContainer() { //Given var node = new DrawableNode(); //Then Assert.Throws <MarkupException>(() => node.Load(@" <Box xmlns=""osufx://osu.Framework/*""> <Box/> <SpriteText/> </Box>")); }
public void TestGenerate() { //Given var doc = new DrawableNode(); var code = @"public partial class MyContainer : osu.Framework.Graphics.Containers.Container { public osu.Framework.Graphics.Shapes.Box MyBox { get; private set; } public MyContainer() { Name = ""MyContainer""; RelativeSizeAxes = osu.Framework.Graphics.Axes.Both; } [osu.Framework.Allocation.BackgroundDependencyLoaderAttribute] private void load() { Children = new osu.Framework.Graphics.Drawable[] { MyBox = new osu.Framework.Graphics.Shapes.Box { Name = ""MyBox"", Colour = osu.Framework.Graphics.Colour.ColourInfo.SingleColour(new osuTK.Graphics.Color4(1F, 0F, 1F, 1F)) } }; } }"; doc.Load(@" <Container xmlns=""osufx://osu.Framework/*"" Name=""MyContainer"" RelativeSizeAxes=""Both""> <Box Name=""MyBox"" Colour=""#ff00ff""/> </Container>"); //When string generated; using (var writer = new StringWriter()) { doc.GenerateClass(writer); generated = writer.ToString(); } //Then Console.WriteLine(generated); Assert.Equal(code.RemoveAllSpaces(), generated.RemoveAllSpaces()); }
public static ConstructorDeclarationSyntax GenerateConstructorSyntax(this DrawableNode node) { return(ConstructorDeclaration( attributeLists: List <AttributeListSyntax>(), modifiers: TokenList( Token(SyntaxKind.PublicKeyword) ), identifier: Identifier(node.GivenName), parameterList: ParameterList(), initializer: null, body: GenerateBody(node) )); }
public void TestInvalidBindingName() { //Given var node = new DrawableNode(); //When node.Load(@" <Box xmlns=""osufx://osu.Framework/*"" Name=""{Binding Local=BoxName}""/>"); //Then Assert.Throws <MarkupException>(() => node.GivenName); }
private KeyValuePair <List <Drawable>, List <KeyValuePair <int, int> > > GetDrawableStructureFromNodes(List <Node <int, string> > nodes) { var objects = new List <Drawable>(); var connections = new List <KeyValuePair <int, int> >(); var r = new Random(); for (var i = 1; i < nodes.Count + 1; i++) { var drawableNode = new DrawableNode(new Point(r.Next(60, displayPanel.Width - 60), r.Next(60, displayPanel.Height - 60)), i); objects.Add(drawableNode); connections.AddRange(nodes[i - 1].Neighbors.Select(n => new KeyValuePair <int, int>(i, nodes.IndexOf(nodes.First(p => p.GetId().Id == n.Id)) + 1)).ToArray()); } return(new KeyValuePair <List <Drawable>, List <KeyValuePair <int, int> > >(objects, connections)); }
public void TestLoad() { //Given var node = new DrawableNode(); //When node.Load(@" <Box xmlns=""osufx://osu.Framework/*"" Name=""MyBox""/>"); //Then Assert.Equal(typeof(Box).FullName, node.DrawableType.FullName); Assert.Equal("MyBox", node.GivenName); }
public static void GenerateClass( this DrawableNode node, TextWriter writer, string indentation = " ", string lineSeparator = null ) { var syntax = node.GenerateClassSyntax().NormalizeWhitespace( indentation: indentation, eol: lineSeparator ?? Environment.NewLine ); syntax.WriteTo(writer); }
public static ClassDeclarationSyntax GenerateClassSyntax(this DrawableNode node) { return(ClassDeclaration( attributeLists: List <AttributeListSyntax>(), modifiers: TokenList( Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.PartialKeyword) ), identifier: Identifier(node.GivenName), typeParameterList: null, baseList: BaseList(SingletonSeparatedList <BaseTypeSyntax>( SimpleBaseType(ParseTypeName(node.DrawableType.FullName)) )), constraintClauses: List <TypeParameterConstraintClauseSyntax>(), members: GenerateMembers(node) )); }
public static ExpressionSyntax GenerateChildrenInitializers(DrawableNode node) { return(AssignmentExpression( kind: SyntaxKind.SimpleAssignmentExpression, left: IdentifierName(nameof(Container.Children)), right: ArrayCreationExpression( type: ArrayType( elementType: ParseTypeName(typeof(Drawable).FullName), rankSpecifiers: SingletonList <ArrayRankSpecifierSyntax>(ArrayRankSpecifier()) ), initializer: InitializerExpression( kind: SyntaxKind.ArrayInitializerExpression, expressions: SeparatedList <ExpressionSyntax>( node.Select(GenerateChildInitializer) ) ) ) )); }
public void TextIDChanged(string Value) { TexId = Convert.ToInt32(Value); if (DrawableNode != null) { foreach (XmlNode tex in DrawableNode.SelectNodes("aTexData")) { foreach (XmlNode texItem in tex.SelectNodes("Item")) { foreach (XmlNode texIdItem in texItem.SelectNodes("texId")) { XmlElement elemtexId = (XmlElement)texIdItem; elemtexId.SetAttribute("value", Value); } } } } }
public static MethodDeclarationSyntax GenerateBackgroundLoaderSyntax(this DrawableNode node) { return(MethodDeclaration( attributeLists: SingletonList <AttributeListSyntax>(AttributeList(SingletonSeparatedList <AttributeSyntax>( Attribute(ParseName(typeof(BackgroundDependencyLoaderAttribute).FullName)) ))), modifiers: TokenList( Token(SyntaxKind.PrivateKeyword) ), returnType: PredefinedType( Token(SyntaxKind.VoidKeyword) ), explicitInterfaceSpecifier: null, identifier: Identifier("load"), typeParameterList: null, parameterList: ParameterList(), constraintClauses: List <TypeParameterConstraintClauseSyntax>(), body: GenerateBody(node), expressionBody: null )); }
async Task updateAsync() { await Task.Delay(TimeSpan.FromMilliseconds(UpdateDebounceTime)); try { // Read and parse markup var node = new DrawableNode(); node.Load(_documentContent); // Create drawable from markup var drawable = node.CreateDrawable(); Schedule(() => { _content.Child = drawable; _content.FadeIn(200); _statusText.Text = "Waiting..."; _statusText.FadeColour(Color4.White, 200); _errorDisplay.FadeOut(200); }); } catch (Exception e) { Schedule(() => { _statusText.Text = "Error!"; _statusText.FadeColour(DesignerColours.Error, 200); _error.Value = e; _errorDisplay.FadeIn(200); }); } }
public void TestGenerate2() { var doc = new DrawableNode(); var code = @"public partial class MyScreen : osu.Framework.Screens.Screen { public osu.Framework.Graphics.Containers.Container MyFirstContainer { get; private set; } public osu.Framework.Graphics.Containers.Container MySecondContainer { get; private set; } public osu.Framework.Graphics.Shapes.Box MyInnermostBox { get; private set; } public MyScreen() { Name = ""MyScreen""; } [osu.Framework.Allocation.BackgroundDependencyLoaderAttribute] private void load() { Children = new osu.Framework.Graphics.Drawable[] { MyFirstContainer = new osu.Framework.Graphics.Containers.Container { Name = ""MyFirstContainer"", RelativeSizeAxes = osu.Framework.Graphics.Axes.Both, Colour = osu.Framework.Graphics.Colour.ColourInfo.SingleColour(new osuTK.Graphics.Color4(1F, 0F, 1F, 1F)), Children = new osu.Framework.Graphics.Drawable[] { new osu.Framework.Graphics.Shapes.Box { RelativeSizeAxes = osu.Framework.Graphics.Axes.Both }, MySecondContainer = new osu.Framework.Graphics.Containers.Container { Name = ""MySecondContainer"", RelativeSizeAxes = osu.Framework.Graphics.Axes.Both, Colour = osu.Framework.Graphics.Colour.ColourInfo.GradientVertical(new osuTK.Graphics.Color4(0F, 0F, 1F, 1F), new osuTK.Graphics.Color4(1F, 0F, 0F, 1F)), Padding = new osu.Framework.Graphics.MarginPadding(100F), Children = new osu.Framework.Graphics.Drawable[] { MyInnermostBox = new osu.Framework.Graphics.Shapes.Box { Name = ""MyInnermostBox"", RelativeSizeAxes = osu.Framework.Graphics.Axes.Both } } } } } }; } }"; doc.Load(@" <Screen xmlns=""osufx://osu.Framework/*"" Name=""MyScreen""> <Container Name=""MyFirstContainer"" RelativeSizeAxes=""Both"" Colour=""#ff00ff""> <Box RelativeSizeAxes=""Both""/> <Container Name=""MySecondContainer"" RelativeSizeAxes=""Both"" Colour=""gradient(vertical, #0000ff, #ff0000)"" Padding=""100""> <Box Name=""MyInnermostBox"" RelativeSizeAxes=""Both""/> </Container> </Container> </Screen>"); //When string generated; using (var writer = new StringWriter()) { doc.GenerateClass(writer); generated = writer.ToString(); } //Then Console.WriteLine(generated); Assert.Equal(code.RemoveAllSpaces(), generated.RemoveAllSpaces()); }
public static IEnumerable <DrawableNode> EnumerateDescendantsWithName(DrawableNode node) => node.RecursiveSelect(n => n).Where(n => n.IsNameSpecified);
public static IEnumerable <PropertyDeclarationSyntax> GeneratePropertySyntaxesOfDescendants(this DrawableNode node) { foreach (var d in EnumerateDescendantsWithName(node)) { yield return(PropertyDeclaration( attributeLists: List <AttributeListSyntax>(), modifiers: TokenList( Token(SyntaxKind.PublicKeyword) ), type: ParseTypeName(d.DrawableType.FullName), explicitInterfaceSpecifier: null, identifier: Identifier(d.GivenName), accessorList: AccessorList(List <AccessorDeclarationSyntax>(new[] { AccessorDeclaration( kind: SyntaxKind.GetAccessorDeclaration, attributeLists: List <AttributeListSyntax>(), modifiers: TokenList(), keyword: Token(SyntaxKind.GetKeyword), body: null, semicolonToken: Token(SyntaxKind.SemicolonToken) ), AccessorDeclaration( kind: SyntaxKind.SetAccessorDeclaration, attributeLists: List <AttributeListSyntax>(), modifiers: TokenList( Token(SyntaxKind.PrivateKeyword) ), keyword: Token(SyntaxKind.SetKeyword), body: null, semicolonToken: Token(SyntaxKind.SemicolonToken) ) })) )); } }
public static IEnumerable <StatementSyntax> GenerateEmbeddedPropertyInitializers(DrawableNode node) { foreach (var property in node.Properties.OfType <EmbeddedDrawableProperty>()) { yield return(ExpressionStatement(property.GenerateInitializerSyntax())); } }
public static BlockSyntax GenerateBody(DrawableNode node) { return(Block(List <StatementSyntax>( GenerateEmbeddedPropertyInitializers(node) ))); }