public override void OnInspectorGUI() { serializedObject.Update(); string[] elementIdentifierMaps = AssetDatabase.FindAssets("t:ElementIdentifierMap"); if (elementIdentifierMaps.Length <= 0) { GUILayout.Label("Couldn't find an element identifier map, here's the default inspector"); DrawDefaultInspector(); return; } if (elementIdentifierMaps.Length > 1) { GUILayout.Label("More than one element identifier map, using the first one"); } GlyphMap glyphMap = (GlyphMap)target; ElementIdentifierMap elementIdentifierMap = AssetDatabase.LoadAssetAtPath <ElementIdentifierMap>(AssetDatabase.GUIDToAssetPath(elementIdentifierMaps[0])); ElementIdentifierMap.ControllerMapping controllerMapping = elementIdentifierMap.GetMappingForGuid(glyphMap.controllerGuid); if (controllerMapping == null) { GUILayout.Label("GUID doesn't match any known controllers, here's the default inspector"); DrawDefaultInspector(); return; } DrawControllerMapping(controllerMapping); serializedObject.ApplyModifiedProperties(); }
public string Process(IEnumerable <Token> tokens) { var sb = new StringBuilder(); foreach (var token in tokens) { if (token.IsSub) { if (GlyphMap.TryGetValue(token.Value, out var glyph)) { sb.Append(glyph); } else { sb.Append('<'); sb.Append(token.Value); sb.Append('>'); } } else { sb.Append(token.Value); } } return(sb.ToString()); }
/// <summary> /// We use a instance conctructor instead of a static constructor here. This is because /// an exception in a static constructur is embedded in another exception and therefore not easy /// to understand. /// </summary> private GlyphFactory() { string configDir = System.Configuration.ConfigurationManager.AppSettings.Get("MathMLRenderingConfig"); string searchDir = null; if (configDir == null || configDir.Length == 0) { searchDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); } else if (Path.IsPathRooted(configDir)) { if (Directory.Exists(configDir)) { searchDir = configDir; } else { throw new ApplicationException("Configured font configuration file directory does not exist! Configured path: " + configDir); } } else // path is not rooted { string basepath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + Path.DirectorySeparatorChar; if (Directory.Exists(basepath + configDir)) { searchDir = basepath + configDir; } else { throw new ApplicationException("Configured font configuration file directory does not exist! Configured path: " + configDir + ", the base path is: " + basepath); } } string[] files = Directory.GetFiles(searchDir, "font-configuration-*.xml"); if (files.Length == 0) { throw new ApplicationException( string.Format( "MathMLRendering: No font configuration files found in directory: {0}\r\n" + "configDir: {1}" , searchDir, configDir)); } Array.Sort(files); maps = new GlyphMap[files.Length]; for (int i = 0; i < maps.Length; i++) { maps[i] = new GlyphMap(files[i]); } }
public void Dispose() { GlyphMap?.Clear(); FontFile?.Dispose(); if (Textures != null) { foreach (var texture in Textures) { texture.Dispose(); } } }
public Vector2 MeasureText(string text) { var width = 0; var height = 0; foreach (var c in text) { if (GlyphMap.TryGetValue(c, out FontChar fc)) { width += fc.XAdvance + fc.XOffset; if (fc.Height + fc.YOffset > height) { height = fc.Height + fc.YOffset; } } } return(new Vector2(width, height)); }
public void DrawTextCenteredStretched(SpriteBatch spriteBatch, string text, Rectangle borders, Color color) { var size = MeasureText(text); var scale = Math.Min(borders.Width / size.X, borders.Height / size.Y); var dx = borders.Center.X - (int)(size.X * scale * 0.5f) - (int)(2 * text.Length * 0.5f); var dy = borders.Y; foreach (var c in text) { if (GlyphMap.TryGetValue(c, out FontChar fc)) { var sourceRectangle = new Rectangle(fc.X, fc.Y, fc.Width, fc.Height); var position = new Vector2(dx + fc.XOffset, dy + fc.YOffset); spriteBatch.Draw(Textures[fc.Page], position, sourceRectangle, color, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f); dx += (int)(fc.XAdvance * scale + FontScale * scale); } } }
public FontRenderer(string fontFolder, string fileName) { #region File var deserializer = new XmlSerializer(typeof(FontFile)); if (File.Exists(Path.Combine(fontFolder, $"{fileName}.{FontXmlDataFileExtension}"))) { using (var stream = File.OpenRead(Path.Combine(fontFolder, $"{fileName}.{FontXmlDataFileExtension}"))) using (var textReader = new StreamReader(stream)) FontFile = (FontFile)deserializer.Deserialize(textReader); } #endregion File #region Texture Textures = new Texture2D[FontFile.Pages.Count]; foreach (var fontPage in FontFile.Pages) { using (var stream = File.OpenRead(Path.Combine(fontFolder, fontPage.File))) Textures[fontPage.ID] = Texture2D.FromStream(GameInstanceProvider.Instance.GraphicsDevice, stream); } #endregion Texture foreach (var glyph in FontFile.Chars) { GlyphMap.Add((char)glyph.ID, glyph); } FontScale = FontFile.Info.Size / 16f; //FontScale = FontFile.Info.Size / 8f; if (FontScale < 0) { FontScale *= -1f; } }
private static void UnpackFfnt(string path, string fileName, string outputPath) { using (FileStream inputStream = new FileStream(path, FileMode.Open)) { FfntFile ffntFile = FfntFile.ReadFfntFile(inputStream); if (ffntFile.Entries.Count != 2) { return; } GlyphMap glyphMap = ffntFile.Entries.ElementAt(0) as GlyphMap; FontData fontData = ffntFile.Entries.ElementAt(1) as FontData; if (glyphMap == null || fontData == null) { return; } SaveFont(ffntFile, fileName, outputPath); SaveFontLayers(fontData.Data, fileName, outputPath); } }
/// <summary> /// We use a instance conctructor instead of a static constructor here. This is because /// an exception in a static constructur is embedded in another exception and therefore not easy /// to understand. /// </summary> private GlyphFactory() { string configDir = System.Configuration.ConfigurationManager.AppSettings.Get("MathMLRenderingConfig"); string searchDir = null; if(configDir==null || configDir.Length==0) searchDir = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetEntryAssembly().Location); else if(Path.IsPathRooted(configDir)) { if(Directory.Exists(configDir)) searchDir = configDir; else throw new ApplicationException("Configured font configuration file directory does not exist! Configured path: " + configDir); } else // path is not rooted { string basepath = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetEntryAssembly().Location) + Path.DirectorySeparatorChar; if(Directory.Exists(basepath+configDir)) searchDir = basepath + configDir; else throw new ApplicationException("Configured font configuration file directory does not exist! Configured path: " + configDir + ", the base path is: " + basepath); } string[] files = Directory.GetFiles(searchDir, "font-configuration-*.xml"); if(files.Length==0) throw new ApplicationException("MathMLRendering: No font configuration files found in directory: " + searchDir); Array.Sort(files); maps = new GlyphMap[files.Length]; for(int i = 0; i < maps.Length; i++) { maps[i] = new GlyphMap(files[i]); } }
void DrawControllerMapping(ElementIdentifierMap.ControllerMapping controllerMapping) { GlyphMap glyphMap = (GlyphMap)target; GUILayout.Label(new GUIContent(controllerMapping.controllerName), titleStyle); GUILayout.BeginHorizontal(); GUILayout.Label("GUID"); guidProperty.stringValue = GUILayout.TextField(glyphMap.controllerGuid); GUILayout.EndHorizontal(); foreach (KeyValuePair <int, ElementIdentifierMap.ElementIdentifier> pair in controllerMapping.elementIdToNameMap) { int elementId = pair.Key; var elementIdentifier = pair.Value; GlyphMap.GlyphMapping currentMapping = glyphMap.GetGlyphForId(elementId); GlyphMap.GlyphMapping newGlyphMapping = new GlyphMap.GlyphMapping(); Sprite mainGlyph = null; Sprite negativeGlyph = null; Sprite positiveGlyph = null; if (currentMapping != null) { mainGlyph = currentMapping.mainGlyph; negativeGlyph = currentMapping.negativeGlyph; positiveGlyph = currentMapping.positiveGlyph; } if (elementIdentifier.type == ElementIdentifierMap.ElementIdentifierType.Button || elementIdentifier.type == ElementIdentifierMap.ElementIdentifierType.Stick) { newGlyphMapping.mainGlyph = (Sprite)EditorGUILayout.ObjectField(new GUIContent(elementIdentifier.name), mainGlyph, typeof(Sprite), false); } else if (elementIdentifier.type == ElementIdentifierMap.ElementIdentifierType.Axis) { GUILayout.Label(new GUIContent(elementIdentifier.name), smallTitleStyle); newGlyphMapping.mainGlyph = (Sprite)EditorGUILayout.ObjectField(new GUIContent("Main"), mainGlyph, typeof(Sprite), false); newGlyphMapping.negativeGlyph = (Sprite)EditorGUILayout.ObjectField(new GUIContent("Negative"), negativeGlyph, typeof(Sprite), false); newGlyphMapping.positiveGlyph = (Sprite)EditorGUILayout.ObjectField(new GUIContent("Positive"), positiveGlyph, typeof(Sprite), false); EditorGUILayout.Space(); } if (newGlyphMapping.mainGlyph != mainGlyph || newGlyphMapping.negativeGlyph != negativeGlyph || newGlyphMapping.positiveGlyph != positiveGlyph) { int index = glyphMap.GetIndexForId(elementId); if (index < 0) { index = glyphMappingsProperty.arraySize; glyphMappingsProperty.InsertArrayElementAtIndex(glyphMappingsProperty.arraySize); glyphMappingsProperty.GetArrayElementAtIndex(index).FindPropertyRelative("elementId").intValue = elementId; } glyphMappingsProperty.GetArrayElementAtIndex(index).FindPropertyRelative("mainGlyph").objectReferenceValue = newGlyphMapping.mainGlyph; glyphMappingsProperty.GetArrayElementAtIndex(index).FindPropertyRelative("negativeGlyph").objectReferenceValue = newGlyphMapping.negativeGlyph; glyphMappingsProperty.GetArrayElementAtIndex(index).FindPropertyRelative("positiveGlyph").objectReferenceValue = newGlyphMapping.positiveGlyph; } } }
public Sprite GetGlyph(ActionElementMap actionElementMap) { Controller controller = actionElementMap.controllerMap.controller; if (controller.type == ControllerType.Joystick) { Joystick joystick = controller as Joystick; string guid = joystick.hardwareTypeGuid.ToString(); GlyphMap glyphMap = glyphMaps.Find((x) => x.controllerGuid == guid); if (glyphMap == null) { return(defaultGlyph); } GlyphMap.GlyphMapping glyphMapping = glyphMap.GetGlyphForId(actionElementMap.elementIdentifierId); if (actionElementMap.axisRange == AxisRange.Full || actionElementMap.elementType == ControllerElementType.Button) { return(glyphMapping.mainGlyph ?? defaultGlyph); } else if (actionElementMap.axisRange == AxisRange.Negative) { return(glyphMapping.negativeGlyph ?? defaultGlyph); } else if (actionElementMap.axisRange == AxisRange.Positive) { return(glyphMapping.positiveGlyph ?? defaultGlyph); } return(defaultGlyph); } else if (controller.type == ControllerType.Keyboard) { var glyphMapping = keyboardGlyphMap.GetGlyphForKeyCode(actionElementMap.keyboardKeyCode); if (glyphMapping != null) { return(glyphMapping.glyph); } return(defaultGlyph); } else if (controller.type == ControllerType.Mouse) { MouseGlyphMap.GlyphMapping glyphMapping = mouseGlyphMap.GetGlyphForId(actionElementMap.elementIdentifierId); if (actionElementMap.axisRange == AxisRange.Full || actionElementMap.elementType == ControllerElementType.Button) { return(glyphMapping.mainGlyph ?? defaultGlyph); } else if (actionElementMap.axisRange == AxisRange.Negative) { return(glyphMapping.negativeGlyph ?? defaultGlyph); } else if (actionElementMap.axisRange == AxisRange.Positive) { return(glyphMapping.positiveGlyph ?? defaultGlyph); } return(defaultGlyph); } else { Debug.LogError("Unsupported controller type: " + controller.type, this); return(defaultGlyph); } }