public void SetSpeechThingPositionAndSide(string side, DynValue position) { CheckExists(); bubbleLastVar = position; try { bubbleSide = side != null ? (BubbleSide)Enum.Parse(typeof(BubbleSide), side.ToUpper()) : BubbleSide.NONE; } catch { throw new CYFException("The speech thing (tail) can only take \"RIGHT\", \"DOWN\" ,\"LEFT\" ,\"UP\" or \"NONE\" as a positional value, but you entered \"" + side.ToUpper() + "\"."); } if (bubbleSide != BubbleSide.NONE) { speechThing.gameObject.SetActive(true); speechThingShadow.gameObject.SetActive(true); speechThing.anchorMin = speechThing.anchorMax = speechThingShadow.anchorMin = speechThingShadow.anchorMax = new Vector2(bubbleSide == BubbleSide.LEFT ? 0 : bubbleSide == BubbleSide.RIGHT ? 1 : 0.5f, bubbleSide == BubbleSide.DOWN ? 0 : bubbleSide == BubbleSide.UP ? 1 : 0.5f); speechThing.rotation = speechThingShadow.rotation = Quaternion.Euler(0, 0, (int)bubbleSide); bool isSide = bubbleSide == BubbleSide.LEFT || bubbleSide == BubbleSide.RIGHT; int size = isSide ? (int)containerBubble.GetComponent <RectTransform>().sizeDelta.y - 20 : (int)containerBubble.GetComponent <RectTransform>().sizeDelta.x - 20; if (position == null) { speechThing.anchoredPosition = speechThingShadow.anchoredPosition = new Vector3(0, 0); } else { switch (position.Type) { case DataType.Number: { float number = (float)position.Number < 0 ? (float)position.Number : (float)position.Number - size / 2f; speechThing.anchoredPosition = speechThingShadow.anchoredPosition = new Vector3(isSide ? 0 : Mathf.Clamp(number, -size / 2f, size / 2f), !isSide ? 0 : Mathf.Clamp(number, -size / 2f, size / 2f)); break; } case DataType.String: { string str = position.String.Replace(" ", ""); if (str.Contains("%")) { try { float percentage = Mathf.Clamp01(ParseUtil.GetFloat(str.Replace("%", "")) / 100), x = isSide ? 0 : Mathf.Round(percentage * size) - size / 2f, y = !isSide ? 0 : Mathf.Round(percentage * size) - size / 2f; speechThing.anchoredPosition = speechThingShadow.anchoredPosition = new Vector3(x, y); } catch { throw new CYFException("If you use a '%' in your string, you should only have a number with it."); } } else { throw new CYFException("You need to use a '%' in order to exploit the string."); } break; } } } } else { speechThing.gameObject.SetActive(false); speechThingShadow.gameObject.SetActive(false); } }
private static UnderFont GetUnderFont(string fontName) { XmlDocument xml = new XmlDocument(); string fontPath = FileLoader.requireFile("Sprites/UI/Fonts/" + fontName + ".png"); string xmlPath = FileLoader.requireFile("Sprites/UI/Fonts/" + fontName + ".xml", false); if (xmlPath == null) { return(null); } try { xml.Load(xmlPath); } catch (XmlException ex) { UnitaleUtil.DisplayLuaError("Instanciating a font", "An error was encountered while loading the font \"" + fontName + "\":\n\n" + ex.Message); return(null); } if (xml["font"] == null) { UnitaleUtil.DisplayLuaError("Instanciating a font", "The font '" + fontName + "' doesn't have a font element at its root."); return(null); } Dictionary <char, Sprite> fontMap = LoadBuiltInFont(xml["font"]["spritesheet"], fontPath); UnderFont underfont; try { underfont = new UnderFont(fontMap, fontName); } catch { UnitaleUtil.DisplayLuaError("Instanciating a font", "The fonts need a space character to compute their line height, and the font '" + fontName + "' doesn't have one."); return(null); } if (xml["font"]["voice"] != null) { underfont.Sound = AudioClipRegistry.GetVoice(xml["font"]["voice"].InnerText); } if (xml["font"]["linespacing"] != null) { underfont.LineSpacing = ParseUtil.GetFloat(xml["font"]["linespacing"].InnerText); } if (xml["font"]["charspacing"] != null) { underfont.CharSpacing = ParseUtil.GetFloat(xml["font"]["charspacing"].InnerText); } if (xml["font"]["color"] != null) { underfont.DefaultColor = ParseUtil.GetColor(xml["font"]["color"].InnerText); } return(underfont); }
private static Table ConstructTable(string text) { Table t = new Table(null); int inOtherTable = 0; string currentValue = ""; DynValue valueName = null; bool inString = false, inSlashEffect1 = false, inSlashEffect2 = false; for (int i = 0; i < text.Length; i++) { if (inSlashEffect1) { inSlashEffect1 = false; } else if (inSlashEffect2) { inSlashEffect2 = false; } if (!inSlashEffect2) { if (text[i] == '{' && !inString) { if (inOtherTable != 1) { currentValue += text[i]; } inOtherTable++; } else if (text[i] == '}' && !inString) { inOtherTable--; if (inOtherTable == 0) { if (valueName == null) { t.Append(DynValue.NewTable(ConstructTable(currentValue))); } else { t.Set(valueName, DynValue.NewTable(ConstructTable(currentValue))); } currentValue = ""; valueName = null; } else { currentValue += text[i]; } } else if (text[i] == '"' && inOtherTable != 0) { inString = !inString; } else if (text[i] == '\\') { inSlashEffect1 = true; inSlashEffect2 = true; } else if (text[i] == ',' && (!inString || inOtherTable != 0)) { currentValue = currentValue.Trim(); Type type = CheckRealType(currentValue); DynValue dv; if (type == typeof(bool)) { dv = DynValue.NewBoolean(currentValue == "true"); } else if (type == typeof(float)) { dv = DynValue.NewNumber(ParseUtil.GetFloat(currentValue)); } else { dv = DynValue.NewString(currentValue.Trim('"')); } if (valueName == null) { t.Append(dv); } else { t.Set(valueName, dv); } valueName = null; currentValue = ""; } else if (text[i] == '=' && (!inString || inOtherTable != 0)) { currentValue = currentValue.Trim(); valueName = DynValue.NewString(currentValue); Type type = CheckRealType(currentValue); if (type == typeof(bool)) { valueName = DynValue.NewBoolean(currentValue == "true"); } else if (type == typeof(float)) { valueName = DynValue.NewNumber(ParseUtil.GetFloat(currentValue)); } else { valueName = DynValue.NewString(currentValue.Trim('"')); } } else { currentValue += text[i]; } } else { currentValue += text[i]; } } return(t); }
public static float CalcTotalLength(TextManager txtmgr, int fromLetter = -1, int toLetter = -1) { float totalWidth = 0, totalMaxWidth = 0, hSpacing = txtmgr.Charset.CharSpacing; if (fromLetter == -1) { fromLetter = 0; } if (toLetter == -1) { toLetter = txtmgr.textQueue[txtmgr.currentLine].Text.Length; } if (fromLetter > toLetter || fromLetter < 0 || toLetter > txtmgr.textQueue[txtmgr.currentLine].Text.Length) { return(-1); } if (fromLetter == toLetter) { return(0); } for (int i = fromLetter; i < toLetter; i++) { switch (txtmgr.textQueue[txtmgr.currentLine].Text[i]) { case '[': string str = ""; for (int j = i + 1; j < txtmgr.textQueue[txtmgr.currentLine].Text.Length; j++) { if (txtmgr.textQueue[txtmgr.currentLine].Text[j] == ']') { i = j + 1; break; } str += txtmgr.textQueue[txtmgr.currentLine].Text[j]; } i--; if (str.Split(':')[0] == "charspacing") { hSpacing = ParseUtil.GetFloat(str.Split(':')[1]); } break; case '\r': case '\n': if (totalMaxWidth < totalWidth - hSpacing) { totalMaxWidth = totalWidth - hSpacing; } totalWidth = 0; break; default: if (txtmgr.Charset.Letters.ContainsKey(txtmgr.textQueue[txtmgr.currentLine].Text[i])) { totalWidth += txtmgr.Charset.Letters[txtmgr.textQueue[txtmgr.currentLine].Text[i]].textureRect.size.x + hSpacing; } break; } } if (totalMaxWidth < totalWidth - hSpacing) { totalMaxWidth = totalWidth - hSpacing; } return(totalMaxWidth); }
public static float CalcTextWidth(TextManager txtmgr, int fromLetter = -1, int toLetter = -1, bool countEOLSpace = false, bool getLastSpace = false) { float totalWidth = 0, totalWidthSpaceTest = 0, totalMaxWidth = 0, hSpacing = txtmgr.Charset.CharSpacing; if (fromLetter == -1) { fromLetter = 0; } if (txtmgr.textQueue == null) { return(0); } if (txtmgr.textQueue[txtmgr.currentLine] == null) { return(0); } if (toLetter == -1) { toLetter = txtmgr.textQueue[txtmgr.currentLine].Text.Length - 1; } if (fromLetter > toLetter || fromLetter < 0 || toLetter > txtmgr.textQueue[txtmgr.currentLine].Text.Length) { return(-1); } for (int i = fromLetter; i <= toLetter; i++) { switch (txtmgr.textQueue[txtmgr.currentLine].Text[i]) { case '[': string str = ParseCommandInline(txtmgr.textQueue[txtmgr.currentLine].Text, ref i); if (str == null) { totalWidth += txtmgr.Charset.Letters[txtmgr.textQueue[txtmgr.currentLine].Text[i]].textureRect.size.x + hSpacing; } else if (str.Split(':')[0] == "charspacing") { hSpacing = str.Split(':')[1].ToLower() == "default" ? txtmgr.Charset.CharSpacing : ParseUtil.GetFloat(str.Split(':')[1]); } break; case '\r': case '\n': if (totalMaxWidth < totalWidthSpaceTest - hSpacing) { totalMaxWidth = totalWidthSpaceTest - hSpacing; } totalWidth = 0; totalWidthSpaceTest = 0; break; default: if (txtmgr.Charset.Letters.ContainsKey(txtmgr.textQueue[txtmgr.currentLine].Text[i])) { totalWidth += txtmgr.Charset.Letters[txtmgr.textQueue[txtmgr.currentLine].Text[i]].textureRect.size.x + hSpacing; // Do not count end of line spaces if (txtmgr.textQueue[txtmgr.currentLine].Text[i] != ' ' || countEOLSpace) { totalWidthSpaceTest = totalWidth; } } break; } } if (totalMaxWidth < totalWidthSpaceTest - hSpacing) { totalMaxWidth = totalWidthSpaceTest - hSpacing; } return(totalMaxWidth + (getLastSpace ? hSpacing : 0)); }
public static float CalcTextWidth(TextManager txtmgr, int fromLetter = -1, int toLetter = -1) { float totalWidth = 0, totalWidthSpaceTest = 0, totalMaxWidth = 0, hSpacing = txtmgr.Charset.CharSpacing; if (fromLetter == -1) { fromLetter = 0; } if (txtmgr.textQueue == null) { return(0); } if (txtmgr.textQueue[txtmgr.currentLine] == null) { return(0); } if (toLetter == -1) { toLetter = txtmgr.textQueue[txtmgr.currentLine].Text.Length; } if (fromLetter > toLetter || fromLetter < 0 || toLetter > txtmgr.textQueue[txtmgr.currentLine].Text.Length) { return(-1); } if (fromLetter == toLetter) { return(0); } for (int i = fromLetter; i < toLetter; i++) { switch (txtmgr.textQueue[txtmgr.currentLine].Text[i]) { case '[': string str = ""; bool failSafe = false; for (int j = i + 1; j < txtmgr.textQueue[txtmgr.currentLine].Text.Length; j++) { if (txtmgr.textQueue[txtmgr.currentLine].Text[j] == ']') { i = j + 1; break; } str += txtmgr.textQueue[txtmgr.currentLine].Text[j]; // unclosed [ has been detected if (j == txtmgr.textQueue[txtmgr.currentLine].Text.Length - 1) { failSafe = true; break; } } // used to protect against unclosed open brackets if (failSafe) { break; } i--; if (str.Split(':')[0] == "charspacing") { hSpacing = ParseUtil.GetFloat(str.Split(':')[1]); } break; case '\r': case '\n': if (totalMaxWidth < totalWidthSpaceTest - hSpacing) { totalMaxWidth = totalWidthSpaceTest - hSpacing; } totalWidth = 0; totalWidthSpaceTest = 0; break; default: if (txtmgr.Charset.Letters.ContainsKey(txtmgr.textQueue[txtmgr.currentLine].Text[i])) { totalWidth += txtmgr.Charset.Letters[txtmgr.textQueue[txtmgr.currentLine].Text[i]].textureRect.size.x + hSpacing; //Do not count end of line spaces if (txtmgr.textQueue[txtmgr.currentLine].Text[i] != ' ') { totalWidthSpaceTest = totalWidth; } } break; } } if (totalMaxWidth < totalWidthSpaceTest - hSpacing) { totalMaxWidth = totalWidthSpaceTest - hSpacing; } return(totalMaxWidth); }
public static void SwapSpriteFromFile(Component target, string filename, int bubbleID = -1) { try { if (bubbleID != -1) { FileInfo fi = new FileInfo(Path.ChangeExtension(FileLoader.pathToModFile("Sprites/" + filename + ".png"), "xml")); if (!fi.Exists) { fi = new FileInfo(Path.ChangeExtension(FileLoader.pathToDefaultFile("Sprites/" + filename + ".png"), "xml")); } if (fi.Exists) { XmlDocument xmld = new XmlDocument(); xmld.Load(fi.FullName); if (xmld["spritesheet"] != null && "single".Equals(xmld["spritesheet"].GetAttribute("type"))) { if (!UnitaleUtil.IsOverworld) { UIController.instance.encounter.EnabledEnemies[bubbleID].bubbleWidth = ParseUtil.GetFloat(xmld["spritesheet"].GetElementsByTagName("width").Count > 0 ? xmld["spritesheet"].GetElementsByTagName("width")[0].InnerText : xmld["spritesheet"].GetElementsByTagName("wideness")[0].InnerText); } } } else { UIController.instance.encounter.EnabledEnemies[bubbleID].bubbleWidth = 0; } } } catch (Exception) { UIController.instance.encounter.EnabledEnemies[bubbleID].bubbleWidth = 0; } Sprite newSprite = SpriteRegistry.Get(filename); if (newSprite == null) { if (filename.Length == 0) { Debug.LogError("SwapSprite: Filename is empty!"); return; } newSprite = FromFile(FileLoader.pathToModFile("Sprites/" + filename + ".png")); if (newSprite == null) { throw new CYFException("The sprite Sprites/" + filename + ".png doesn't exist."); } SpriteRegistry.Set(filename, newSprite); } Image img = target.GetComponent <Image>(); if (!img) { SpriteRenderer img2 = target.GetComponent <SpriteRenderer>(); Vector2 pivot = img2.GetComponent <RectTransform>().pivot; img2.sprite = newSprite; img2.GetComponent <RectTransform>().sizeDelta = new Vector2(newSprite.texture.width, newSprite.texture.height); img2.GetComponent <RectTransform>().pivot = pivot; } else { Vector2 pivot = img.rectTransform.pivot; img.sprite = newSprite; //enemyImg.SetNativeSize(); img.rectTransform.sizeDelta = new Vector2(newSprite.texture.width, newSprite.texture.height); img.rectTransform.pivot = pivot; } }