public string GenerateFrom(string workingDirectory, string assetName, DOMDocument document) { m_File = new CSFile(document.@namespace) .AddWarningDisable(414) .AddInclude("System.ComponentModel") .AddInclude("UnityEngine") .AddInclude("UnityEditor.Experimental.VisualElements") .AddInclude("UnityEditor"); m_Class = new CSClass(Scope.Internal, assetName, CSClass.Modifier.Partial) .AddParent("EditorWindow"); m_File.AddClass(m_Class); m_AwakeMethod = new CSMethod(Scope.Private, "void", "Awake"); m_Class.AddMethod(m_AwakeMethod); m_OnDestroyMethod = new CSMethod(Scope.Private, "void", "OnDestroy"); m_Class.AddMethod(m_OnDestroyMethod); m_OnVMPropertyChanged = new CSMethod(Scope.Private, "void", "VmOnPropertyChanged") .AddArgument("object", "sender") .AddArgument("PropertyChangedEventArgs", "propertyChangedEventArgs") .AddBodyLine("Repaint();"); m_Class.AddMethod(m_OnVMPropertyChanged); VisitIn(document); VisitIn(document.usings); VisitIn(document.editorPrefsProperties); var builder = new StringBuilder(); m_File.GenerateIn(builder); return(builder.ToString()); }
static void WriteStyles(int counterIndex, List <string> tokens, DOMStyle[] styles, CSMethod constructor, CSClass @class) { for (int i = 0; i < styles.Length; i++) { tokens.Clear(); var style = styles[i]; ParseGUILayoutTokens(style, tokens); var buildMethod = new CSMethod(Scope.Private, "Style", "BuildStyle" + ++counterIndex); @class.AddMethod(buildMethod); if (tokens.Count > 0) { if (!string.IsNullOrEmpty(style.name)) { constructor.AddBodyLine(string.Format("SetClassStyle(\"{0}\", {1}());", style.name, buildMethod.name)); } if (!string.IsNullOrEmpty(style.elementType)) { constructor.AddBodyLine(string.Format("SetClassStyle(typeof({0}), {1}());", style.elementType, buildMethod.name)); } } buildMethod.AddBodyLine("var s = new Style();"); for (int j = 0; j < tokens.Count; j++) { buildMethod.AddBodyLine(tokens[j] + ";"); } buildMethod.AddBodyLine("return s;"); } }
public void Build() { if (m_OverrideOnGUI) { m_Class.AddMethod(m_OnGUIMethod); } m_BindMethod.AddBodyLine("base.Bind();"); m_BuildMethod.AddBodyLine("base.Build();"); }
void VisitIn(DOMRoot root) { if (root == null) { return; } m_BuildMethod = new CSMethod(Scope.Protected, "void", "Build", CSMethod.Modifier.Override); m_Class.AddMethod(m_BuildMethod); var visitor = new VXMLDOMElementVisitor(m_File, m_Class, m_BuildMethod); visitor.Visit(root); visitor.Build(); }
public VXMLDOMElementVisitor(CSFile file, CSClass @class, CSMethod buildMethod) { m_File = file; m_Class = @class; SetHandler(this); m_BuildMethod = buildMethod; m_BindMethod = new CSMethod(Scope.Protected, "void", "Bind", CSMethod.Modifier.Override); m_Class.AddMethod(m_BindMethod); m_OnGUIMethod = new CSMethod(Scope.Public, "void", "OnGUI", CSMethod.Modifier.Override); m_WriteChildFormatStack = new Stack <string>(); PushAddChildMethod(string.Empty); }
void Visit(DOMStyle[] styles) { if (styles == null) { return; } var constructor = new CSMethod(Scope.Public, string.Empty, m_Class.name); m_Class.AddMethod(constructor); var tokens = new List <string>(); WriteStyles(0, tokens, s_BuiltinStyles, constructor, m_Class); WriteStyles(s_BuiltinStyles.Length, tokens, styles, constructor, m_Class); }
private void VisitIn(DOMDocument document) { // Create window method var createWindowMethod = new CSMethod(Scope.Internal, m_Class.name, "CreateWindow", CSMethod.Modifier.Static) .AddBodyLine("var window = GetWindow<" + m_Class.name + ">();"); if (!string.IsNullOrEmpty(document.title)) { createWindowMethod.AddBodyLine("window.titleContent = new GUIContent(\"" + document.title + "\");"); } createWindowMethod.AddBodyLine("return window;"); m_Class.AddMethod(createWindowMethod); m_HasService = false; m_HasRoot = false; if (!string.IsNullOrEmpty(document.serviceClass)) { m_HasService = true; m_Class.AddField(new CSField(Scope.Private, "m_Service", document.serviceClass)); m_AwakeMethod.AddBodyLine("m_Service = new " + document.serviceClass + "();"); var exposedClass = string.IsNullOrEmpty(document.exposedServiceClass) ? document.serviceClass : document.exposedServiceClass; m_Class.AddProperty(new CSProperty(Scope.Public, "service", exposedClass) .SetGetter("return m_Service;")); } if (!string.IsNullOrEmpty(document.rootClass)) { m_HasRoot = true; m_Class.AddField(new CSField(Scope.Private, "m_Root", document.rootClass)); m_AwakeMethod.AddBodyLine("m_Root = new " + document.rootClass + "();"); } if (m_HasRoot) { m_AwakeMethod.AddBodyLine("m_Root.parent = new VisualElementRoot();"); m_AwakeMethod.AddBodyLine("m_Root.RepaintRequested += Repaint;"); m_OnDestroyMethod.AddBodyLine("m_Root.RepaintRequested -= Repaint;"); m_OnDestroyMethod.AddBodyLine("m_Root.Dispose();"); m_OnDestroyMethod.AddBodyLine("m_Root = null;"); if (m_HasService) { m_AwakeMethod.AddBodyLine("m_Root.dataContext = m_Service.vm;"); } m_Class.AddMethod(new CSMethod(Scope.Private, "void", "OnGUI") .AddBodyLine("m_Root.OnGUI();")); } if (m_HasService) { m_AwakeMethod.AddBodyLine("m_Service.vm.PropertyChanged += VmOnPropertyChanged;"); m_AwakeMethod.AddBodyLine("m_Service.vm.SetPropertyChanged((ClassProperty)null);"); m_OnDestroyMethod.AddBodyLine("m_Service.vm.PropertyChanged -= VmOnPropertyChanged;"); } if (m_HasRoot || m_HasService) { var onEnableMethod = new CSMethod(Scope.Private, "void", "OnEnable"); var cond = "m_Service == null || m_Root == null"; if (!m_HasRoot) { cond = "m_Service == null"; } if (!m_HasService) { cond = "m_Root == null"; } onEnableMethod.AddBodyLine("if (" + cond + ")"); onEnableMethod.AddBodyLine(" Awake();"); m_Class.AddMethod(onEnableMethod); } }
void GeneratePropertyInternal(string type, string name, string fieldName, string pptName, string getterContent, string setterContent, string fieldType = null) { var csProperty = new CSProperty(Scope.Public, name, type); m_Class.AddProperty(csProperty); if (string.IsNullOrEmpty(getterContent)) { csProperty.SetGetter("return (" + type + ")" + fieldName + ";"); } else { csProperty.SetGetter(kVariableReference.Replace(getterContent, "$1") + ";"); } var hasSetter = !string.IsNullOrEmpty(setterContent) || string.IsNullOrEmpty(getterContent) && string.IsNullOrEmpty(setterContent); if (hasSetter) { var setterMethod = new CSMethod(Scope.Private, "bool", "Set" + NameUtility.PascalCase(name)) .AddArgument(type, "value"); m_Class.AddMethod(setterMethod); if (!string.IsNullOrEmpty(setterContent)) { setterMethod.AddBodyLine(setterContent); } else { var fieldTypeCast = (string.IsNullOrEmpty(fieldType) ? "" : "(" + fieldType + ")"); setterMethod .AddBodyLine("if (" + fieldName + " != " + fieldTypeCast + "value)") .AddBodyLine("{") .AddBodyLine(" " + fieldName + " = " + fieldTypeCast + "value;") .AddBodyLine(" return true;") .AddBodyLine("}") .AddBodyLine("return false;"); } var dependentProperties = new HashSet <string>() { name }; if (m_PropertyGetterDependencies.ContainsKey(name)) { dependentProperties.UnionWith(m_PropertyGetterDependencies[name]); } var properties = dependentProperties.Select(p => "Properties." + NameUtility.SlugifyConstName(p)).Aggregate((l, r) => l + ", " + r); csProperty.SetSetter("if (" + setterMethod.name + "(value)) { SetPropertyChanged(" + properties + "); }"); } m_PropertyClass.AddField( new CSField( Scope.Public, pptName, "ClassProperty<" + type + ">", "new StaticClassProperty<" + type + ", " + m_Class.name + ">(\"" + name + "\")", CSField.Modifier.Static | CSField.Modifier.Readonly)); }