/// <summary> /// Expands the button so that it's just slightly more than tall enough to show its /// text contents. /// </summary> IEnumerator ExpandTestimonyCoroutine(TestimonyEntry entry) { // Get some values needed to calculate whether this entry needs to be expanded //Debug.Log("Expanding testimony!"); RectTransform entryRect = entry.rectTransform; Text textField = entry.textField; RectTransform textRect = textField.rectTransform; float textHeight = textRect.rect.height; bool needExpansion = textHeight >= baseButtonHeight; if (!needExpansion) { Debug.Log("This testimony doesn't need expanding."); sizeChangeCoroutine = null; yield break; } Debug.Log("This testimony DOES need expanding."); // Calculate the height the entry needs to expand to float extraSpace = baseButtonHeight * 0.1f; float expandedHeight = textHeight + extraSpace; Vector2 newSize = entryRect.sizeDelta; // And expand it float timer = 0; float frameRate = 1f / Time.deltaTime; float timeToExpand = frameRate * sizeChangeTime; while (timer < timeToExpand) { newSize.y = Mathf.Lerp(newSize.y, expandedHeight, timer / timeToExpand); entryRect.sizeDelta = newSize; //Debug.Log("New testimony height: " + entryRect.sizeDelta.y); timer++; yield return(null); } // Just to make sure newSize.y = expandedHeight; entryRect.sizeDelta = newSize; // avoid a visual bug textRect.ApplyAnchorPreset(TextAnchor.UpperCenter, true, true); buttonsManaged[entry.gameObject] = ButtonState.expanded; sizeChangeCoroutine = null; Debug.Log("Done expanding! The button state is now: " + buttonsManaged[entry.gameObject]); }
public void ShrinkTestimony(TestimonyEntry entry) { if (!buttonsManaged.ContainsKey(entry.gameObject)) { // register this new button, and assume it's at a normal state buttonsManaged.Add(entry.gameObject, ButtonState.normal); } if (buttonsManaged[entry.gameObject] != ButtonState.normal && sizeChangeCoroutine == null) { sizeChangeCoroutine = ShrinkTestimonyCoroutine(entry); StartCoroutine(sizeChangeCoroutine); } }
/// <summary> /// Shrinks the button so that its height becomes the base height. /// </summary> IEnumerator ShrinkTestimonyCoroutine(TestimonyEntry entry) { // Some values we'll need Debug.Log("Shrinking testimony!"); RectTransform entryRect = entry.rectTransform; float entryHeight = entryRect.rect.height; Text textField = entry.textField; RectTransform textRect = textField.rectTransform; float textHeight = textRect.rect.height; bool needShrink = entryHeight > baseButtonHeight; if (!needShrink) { Debug.Log("This testimony doesn't need shrinking."); sizeChangeCoroutine = null; yield break; } Debug.Log("This testimony DOES need shrinking."); Vector2 newSize = entryRect.sizeDelta; float timer = 0; float frameRate = 1f / Time.deltaTime; float timeToShrink = frameRate * sizeChangeTime; while (timer < timeToShrink) { newSize.y = Mathf.Lerp(newSize.y, baseButtonHeight, timer / timeToShrink); entryRect.sizeDelta = newSize; timer++; //Debug.Log("New testimony height: " + entryRect.sizeDelta.y); yield return(null); } // Just to make sure newSize.y = baseButtonHeight; textRect.sizeDelta = newSize; // avoid a visual bug textRect.ApplyAnchorPreset(TextAnchor.UpperCenter, true, true); buttonsManaged[entry.gameObject] = ButtonState.normal; sizeChangeCoroutine = null; Debug.Log("Done shrinking! The button state is now: " + buttonsManaged[entry.gameObject]); }
public void AddEntry(string text, string imageName) { Debug.Log("Adding testimony!"); GameObject newEntryGo = Instantiate <GameObject>(entryPrefab, entryPrefab.transform.position, Quaternion.identity); TestimonyEntry entry = newEntryGo.GetComponent <TestimonyEntry>(); // The text may contain a variable, so make sure that the right value is shown instead // of the variable name string entryText = YarnUtils.ParseYarnText(variableStorage, text); // Load the sprite based on the name Sprite giverSprite = Resources.Load <Sprite>("Graphics/Portraits/" + imageName); if (giverSprite == null) { throw new System.ArgumentException(imageName + " is not in the Portraits subfolder of the Graphics folder."); } entry.text = entryText; entry.giverMugshot = giverSprite; newEntryGo.transform.SetParent(testimonyHolder, false); // Make it expand when clicked, go back to normal when clicked while expanded Button entryButton = newEntryGo.GetComponent <Button>(); crewManager.buttonsManaged[newEntryGo] = ButtonState.normal; entryButton.onClick.AddListener(() => { if (crewManager.EntryNormal(newEntryGo)) { //crewManager.expandNews(newEntryGo); crewManager.ExpandTestimony(entry); } else { //crewManager.shrinkNews(newEntryGo); crewManager.ShrinkTestimony(entry); } }); entries.Add(entry); }