public static CompiledTemplateData LoadPrecompiledTemplates(TemplateSettings templateSettings) { Assembly assembly = AppDomain.CurrentDomain.GetAssemblyByName(templateSettings.assemblyName); Type type = assembly.GetType("UIForia.Generated.UIForiaGeneratedTemplates_" + templateSettings.StrippedApplicationName); if (type == null) { throw new ArgumentException("Trying to use precompiled templates for " + templateSettings.StrippedApplicationName + " but couldn't find the type. Maybe you need to regenerate the code?"); } CompiledTemplateData compiledTemplateData = new CompiledTemplateData(templateSettings); compiledTemplateData.styleImporter.importResolutionPath = Path.Combine(UnityEngine.Application.streamingAssetsPath, "UIForia", compiledTemplateData.templateSettings.StrippedApplicationName); ITemplateLoader loader = (ITemplateLoader)Activator.CreateInstance(type); string[] files = loader.StyleFilePaths; compiledTemplateData.styleImporter.Reset(); // reset because in testing we will already have parsed files, nuke these LightList <UIStyleGroupContainer> styleList = new LightList <UIStyleGroupContainer>(128); Dictionary <string, StyleSheet> styleSheetMap = new Dictionary <string, StyleSheet>(128); MaterialDatabase materialDatabase = loader.GetMaterialDatabase(); for (int i = 0; i < files.Length; i++) { StyleSheet sheet = compiledTemplateData.styleImporter.ImportStyleSheetFromFile(files[i], materialDatabase); styleList.EnsureAdditionalCapacity(sheet.styleGroupContainers.Length); for (int j = 0; j < sheet.styleGroupContainers.Length; j++) { styleList.array[styleList.size++] = sheet.styleGroupContainers[j]; } styleSheetMap.Add(sheet.path, sheet); } compiledTemplateData.templates = loader.LoadTemplates(); compiledTemplateData.slots = loader.LoadSlots(); compiledTemplateData.bindings = loader.LoadBindings(); compiledTemplateData.templateMetaData = loader.LoadTemplateMetaData(styleSheetMap, styleList.array); for (int i = 0; i < compiledTemplateData.templateMetaData.Length; i++) { compiledTemplateData.templateMetaData[i].compiledTemplateData = compiledTemplateData; } compiledTemplateData.constructElement = loader.ConstructElement; compiledTemplateData.dynamicTemplates = loader.DynamicTemplates; return(compiledTemplateData); }
private void AppendSharedStyles(LightList <UIStyleGroupContainer> updatedStyles, int index) { int count = updatedStyles.Count; UIStyleGroupContainer[] updatedStyleArray = updatedStyles.Array; LightList <StylePropertyId> toUpdate = LightList <StylePropertyId> .Get(); styleGroupContainers.EnsureAdditionalCapacity(updatedStyles.Count - index); for (int i = index; i < count; i++) { CreateStyleGroups(updatedStyleArray[i], toUpdate); styleGroupContainers.array[i] = updatedStyleArray[i]; } styleGroupContainers.size = count; SortStyles(); UpdatePropertyMap(toUpdate); LightList <StylePropertyId> .Release(ref toUpdate); }
public static int Arc(LightList <Vector2> output, Vector2 p1, float rx, float ry, float angle, bool largeArcFlag, bool sweepFlag, Vector2 p2, int vpm = 1) { int originalSize = output.Count; float _radian = (angle * Mathf.PI / 180.0f); float _CosRadian = Mathf.Cos(_radian); float _SinRadian = Mathf.Sin(_radian); float temp1 = (p1.x - p2.x) / 2.0f; float temp2 = (p1.y - p2.y) / 2.0f; float tx = (_CosRadian * temp1) + (_SinRadian * temp2); float ty = (-_SinRadian * temp1) + (_CosRadian * temp2); double trx2 = rx * rx; double try2 = ry * ry; double tx2 = tx * tx; double ty2 = ty * ty; double radiiCheck = tx2 / trx2 + ty2 / try2; if (radiiCheck > 1) { float sqrt = Mathf.Sqrt((float)radiiCheck); rx = sqrt * rx; ry = sqrt * ry; trx2 = rx * rx; try2 = ry * ry; } double tm1 = (trx2 * try2 - trx2 * ty2 - try2 * tx2) / (trx2 * ty2 + try2 * tx2); tm1 = (tm1 < 0) ? 0 : tm1; float tm2 = (largeArcFlag == sweepFlag) ? -Mathf.Sqrt((float)tm1) : Mathf.Sqrt((float)tm1); float tcx = tm2 * ((rx * ty) / ry); float tcy = tm2 * (-(ry * tx) / rx); float cx = _CosRadian * tcx - _SinRadian * tcy + ((p1.x + p2.x) / 2.0f); float cy = _SinRadian * tcx + _CosRadian * tcy + ((p1.y + p2.y) / 2.0f); float ux = (tx - tcx) / rx; float uy = (ty - tcy) / ry; float vx = (-tx - tcx) / rx; float vy = (-ty - tcy) / ry; float n = Mathf.Sqrt((ux * ux) + (uy * uy)); float p = ux; float _angle = (uy < 0) ? -Mathf.Acos(p / n) : Mathf.Acos(p / n); _angle = _angle * 180.0f / Mathf.PI; _angle %= 360f; n = Mathf.Sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy)); p = ux * vx + uy * vy; float t = p / n; if ((Mathf.Abs(t) >= 0.99999f) && (Mathf.Abs(t) < 1.000009f)) { t = t > 0 ? 1f : -1f; } float _delta = (ux * vy - uy * vx < 0) ? -Mathf.Acos(t) : Mathf.Acos(t); _delta = _delta * 180.0f / Mathf.PI; if (!sweepFlag && _delta > 0) { _delta -= 360f; } else if (sweepFlag && _delta < 0) { _delta += 360f; } _delta %= 360f; int number = Mathf.RoundToInt(Mathf.Clamp((100f / vpm) * Mathf.Abs(_delta) / 360f, 2, 100)); float deltaT = _delta / number; output.EnsureAdditionalCapacity(number + 1); for (int i = 0; i <= number; i++) { float t_angle = (deltaT * i + _angle) * Mathf.PI / 180.0f; float cos = Mathf.Cos(t_angle); float sin = Mathf.Sin(t_angle); output.AddUnchecked(new Vector2( _CosRadian * rx * cos - _SinRadian * ry * sin + cx, _SinRadian * rx * cos + _CosRadian * ry * sin + cy )); } return(output.Count - originalSize); }
public static CompiledTemplateData LoadRuntimeTemplates(Type type, TemplateSettings templateSettings) { CompiledTemplateData compiledTemplateData = TemplateCompiler.CompileTemplates(type, templateSettings); // Stopwatch stopwatch = Stopwatch.StartNew(); Func <UIElement, TemplateScope, UIElement>[] templates = new Func <UIElement, TemplateScope, UIElement> [compiledTemplateData.compiledTemplates.size]; Action <UIElement, UIElement>[] bindings = new Action <UIElement, UIElement> [compiledTemplateData.compiledBindings.size]; Func <UIElement, UIElement, TemplateScope, UIElement>[] slots = new Func <UIElement, UIElement, TemplateScope, UIElement> [compiledTemplateData.compiledSlots.size]; TemplateMetaData[] templateMetaData = new TemplateMetaData[compiledTemplateData.compiledTemplates.size]; OrderablePartitioner <Tuple <int, int> > partition; if (templateMetaData.Length < 10) { for (int i = 0; i < templateMetaData.Length; i++) { templates[i] = (Func <UIElement, TemplateScope, UIElement>)compiledTemplateData.compiledTemplates[i].templateFn.Compile(); } } else { partition = Partitioner.Create(0, templateMetaData.Length); Parallel.ForEach(partition, (range, loopState) => { for (int i = range.Item1; i < range.Item2; i++) { templates[i] = (Func <UIElement, TemplateScope, UIElement>)compiledTemplateData.compiledTemplates[i].templateFn.Compile(); } }); } if (compiledTemplateData.compiledSlots.size < 10) { for (int i = 0; i < compiledTemplateData.compiledSlots.size; i++) { slots[i] = (Func <UIElement, UIElement, TemplateScope, UIElement>)compiledTemplateData.compiledSlots[i].templateFn.Compile(); } } else { partition = Partitioner.Create(0, compiledTemplateData.compiledSlots.size); Parallel.ForEach(partition, (range, loopState) => { for (int i = range.Item1; i < range.Item2; i++) { slots[i] = (Func <UIElement, UIElement, TemplateScope, UIElement>)compiledTemplateData.compiledSlots[i].templateFn.Compile(); } }); } if (bindings.Length < 10) { for (int i = 0; i < bindings.Length; i++) { try { bindings[i] = (Action <UIElement, UIElement>)compiledTemplateData.compiledBindings[i].bindingFn.Compile(); } catch (Exception e) { Debug.Log("binding " + compiledTemplateData.compiledBindings[i].bindingFn.ToCSharpCode()); Debug.Log(e); } } } else { partition = Partitioner.Create(0, bindings.Length); Parallel.ForEach(partition, (range, loopState) => { for (int i = range.Item1; i < range.Item2; i++) { try { bindings[i] = (Action <UIElement, UIElement>)compiledTemplateData.compiledBindings[i].bindingFn.Compile(); } catch (Exception e) { Debug.Log("binding " + compiledTemplateData.compiledBindings[i].bindingFn.ToCSharpCode()); Debug.Log(e); } } }); } LightList <UIStyleGroupContainer> styleList = new LightList <UIStyleGroupContainer>(128); StyleSheet[] sheets = compiledTemplateData.styleImporter.GetImportedStyleSheets(); for (int i = 0; i < sheets.Length; i++) { StyleSheet sheet = sheets[i]; styleList.EnsureAdditionalCapacity(sheet.styleGroupContainers.Length); for (int j = 0; j < sheet.styleGroupContainers.Length; j++) { styleList.array[styleList.size++] = sheet.styleGroupContainers[j]; } } for (int i = 0; i < templateMetaData.Length; i++) { templateMetaData[i] = compiledTemplateData.compiledTemplates[i].templateMetaData; templateMetaData[i].styleMap = styleList.array; templateMetaData[i].BuildSearchMap(); } Dictionary <int, Func <ConstructedElement> > constructorFnMap = new Dictionary <int, Func <ConstructedElement> >(37); ConstructorInfo constructedTypeCtor = typeof(ConstructedElement).GetConstructor(new Type[] { typeof(int), typeof(UIElement) }); System.Diagnostics.Debug.Assert(constructedTypeCtor != null, nameof(constructedTypeCtor) + " != null"); Expression[] parameters = new Expression[2]; // todo -- this can be improved, cannot currently parallelize because the write target (constructorFnMap) is a dictionary which is not threadsafe // can convert the constructorFnMap to an array but would need a unique index for each type that is sequential foreach (KeyValuePair <Type, ProcessedType> kvp in TypeProcessor.typeMap) { if (kvp.Key.IsAbstract || kvp.Value.references == 0 || kvp.Value.id < 0) { continue; } ConstructorInfo ctor = kvp.Key.GetConstructor(Type.EmptyTypes); if (ctor == null) { throw new CompileException(kvp.Key + " must provide a default constructor in order to be used in templates"); } parameters[0] = Expression.Constant(compiledTemplateData.GetTagNameId(kvp.Value.tagName)); parameters[1] = Expression.New(ctor); Func <ConstructedElement> constructorFn = Expression.Lambda <Func <ConstructedElement> >(Expression.New(constructedTypeCtor, parameters)).Compile(); constructorFnMap[kvp.Value.id] = constructorFn; GCHandle.Alloc(constructorFn); } compiledTemplateData.bindings = bindings; compiledTemplateData.slots = slots; compiledTemplateData.templates = templates; compiledTemplateData.templateMetaData = templateMetaData; compiledTemplateData.constructorFnMap = constructorFnMap; compiledTemplateData.constructElement = (typeId) => compiledTemplateData.constructorFnMap[typeId].Invoke(); // stopwatch.Stop(); // Debug.Log("Loaded UIForia templates in " + stopwatch.Elapsed.TotalSeconds.ToString("F2") + " seconds"); return(compiledTemplateData); }
public static CompiledTemplateData LoadRuntimeTemplates(Type type, TemplateSettings templateSettings) { CompiledTemplateData compiledTemplateData = TemplateCompiler.CompileTemplates(type, templateSettings); // Stopwatch stopwatch = Stopwatch.StartNew(); Func <UIElement, TemplateScope, UIElement>[] templates = new Func <UIElement, TemplateScope, UIElement> [compiledTemplateData.compiledTemplates.size]; Action <UIElement, UIElement>[] bindings = new Action <UIElement, UIElement> [compiledTemplateData.compiledBindings.size]; Func <UIElement, UIElement, TemplateScope, UIElement>[] slots = new Func <UIElement, UIElement, TemplateScope, UIElement> [compiledTemplateData.compiledSlots.size]; TemplateMetaData[] templateMetaData = new TemplateMetaData[compiledTemplateData.compiledTemplates.size]; OrderablePartitioner <Tuple <int, int> > partition; if (templateMetaData.Length < 10) { for (int i = 0; i < templateMetaData.Length; i++) { templates[i] = (Func <UIElement, TemplateScope, UIElement>)compiledTemplateData.compiledTemplates[i].templateFn.Compile(); } } else { partition = Partitioner.Create(0, templateMetaData.Length); Parallel.ForEach(partition, (range, loopState) => { for (int i = range.Item1; i < range.Item2; i++) { templates[i] = (Func <UIElement, TemplateScope, UIElement>)compiledTemplateData.compiledTemplates[i].templateFn.Compile(); } }); } if (compiledTemplateData.compiledSlots.size < 10) { for (int i = 0; i < compiledTemplateData.compiledSlots.size; i++) { slots[i] = (Func <UIElement, UIElement, TemplateScope, UIElement>)compiledTemplateData.compiledSlots[i].templateFn.Compile(); } } else { partition = Partitioner.Create(0, compiledTemplateData.compiledSlots.size); Parallel.ForEach(partition, (range, loopState) => { for (int i = range.Item1; i < range.Item2; i++) { slots[i] = (Func <UIElement, UIElement, TemplateScope, UIElement>)compiledTemplateData.compiledSlots[i].templateFn.Compile(); } }); } if (bindings.Length < 10) { for (int i = 0; i < bindings.Length; i++) { try { bindings[i] = (Action <UIElement, UIElement>)compiledTemplateData.compiledBindings[i].bindingFn.Compile(); } catch (Exception e) { Debug.Log("binding " + compiledTemplateData.compiledBindings[i].bindingFn.ToCSharpCode()); Debug.Log(e); } } } else { partition = Partitioner.Create(0, bindings.Length); Parallel.ForEach(partition, (range, loopState) => { for (int i = range.Item1; i < range.Item2; i++) { try { bindings[i] = (Action <UIElement, UIElement>)compiledTemplateData.compiledBindings[i].bindingFn.Compile(); } catch (Exception e) { Debug.Log("binding " + compiledTemplateData.compiledBindings[i].bindingFn.ToCSharpCode()); Debug.Log(e); } } }); } LightList <UIStyleGroupContainer> styleList = new LightList <UIStyleGroupContainer>(128); StyleSheet[] sheets = compiledTemplateData.styleImporter.GetImportedStyleSheets(); for (int i = 0; i < sheets.Length; i++) { StyleSheet sheet = sheets[i]; styleList.EnsureAdditionalCapacity(sheet.styleGroupContainers.Length); for (int j = 0; j < sheet.styleGroupContainers.Length; j++) { styleList.array[styleList.size++] = sheet.styleGroupContainers[j]; } } for (int i = 0; i < templateMetaData.Length; i++) { templateMetaData[i] = compiledTemplateData.compiledTemplates[i].templateMetaData; templateMetaData[i].styleMap = styleList.array; templateMetaData[i].BuildSearchMap(); } Dictionary <int, Func <ConstructedElement> > constructorFnMap = new Dictionary <int, Func <ConstructedElement> >(37); ConstructorInfo constructedTypeCtor = typeof(ConstructedElement).GetConstructor(new Type[] { typeof(int), typeof(UIElement) }); System.Diagnostics.Debug.Assert(constructedTypeCtor != null, nameof(constructedTypeCtor) + " != null"); compiledTemplateData.bindings = bindings; compiledTemplateData.slots = slots; compiledTemplateData.templates = templates; compiledTemplateData.templateMetaData = templateMetaData; compiledTemplateData.constructorFnMap = constructorFnMap; compiledTemplateData.constructElement = (typeId) => compiledTemplateData.constructorFnMap[typeId].Invoke(); // stopwatch.Stop(); // Debug.Log("Loaded UIForia templates in " + stopwatch.Elapsed.TotalSeconds.ToString("F2") + " seconds"); return(compiledTemplateData); }