using UnityEngine; using System; namespace PixelCrushers.DialogueSystem.UnityGUI { /// /// A GUI control that implements GUI.ScrollView. This control measures the size of the /// children contained in the scroll view and resizes the content area accordingly. /// [AddComponentMenu("")] // Deprecated public class GUIScrollView : GUIControl { /// /// Show or hide the vertical scrollbar. /// public bool showVerticalScrollbar = true; /// /// Show or hide the horizontal scrollbar. /// public bool showHorizontalScrollbar = false; /// /// The pixel padding inside the scroll view. /// public int padding = 2; /// /// The handler(s) to call when the scroll view needs to measure its children. /// public event Action MeasureContentHandler = null; /// /// The handler(s) to call when the scroll view needs to draw its children. /// public event Action DrawContentHandler = null; /// /// The width of the content as reported by the MeasureContentHandler. /// /// /// The width of the content. /// public float contentWidth { get; set; } /// /// The height of the content as reported by the MeasureContentHandler. /// /// /// The height of the content. /// public float contentHeight { get; set; } /// /// Resets the scroll position. /// public void ResetScrollPosition() { scrollViewVector = Vector2.zero; } /// /// The current scroll position in the scroll area. /// private Vector2 scrollViewVector = Vector2.zero; /// /// Draws the children, taking into account key/controller navigation if enabled. /// /// Relative mouse position. public override void DrawChildren(Vector2 relativeMousePosition) { clipChildren = false; Rect scrollContentRect = GetScrollContentRect(); GUIStyle horizontalScrollbar = showHorizontalScrollbar ? GUI.skin.horizontalScrollbar : GUIStyle.none; GUIStyle verticalScrollbar = showVerticalScrollbar ? GUI.skin.verticalScrollbar : GUIStyle.none; scrollViewVector = GUI.BeginScrollView(rect, scrollViewVector, scrollContentRect, horizontalScrollbar, verticalScrollbar); try { if (DrawContentHandler != null) DrawContentHandler(); base.DrawChildren(relativeMousePosition); } finally { GUI.EndScrollView(); } } private Rect GetScrollContentRect() { float sliderWidth = (GUI.skin.verticalSlider.normal.background != null) ? GUI.skin.verticalSlider.normal.background.width : 16f; contentWidth = rect.width - sliderWidth; MeasureChildrenAsContent(); if (MeasureContentHandler != null) MeasureContentHandler(); return new Rect(0, 0, contentWidth, contentHeight); } private void MeasureChildrenAsContent() { if (Children != null) { foreach (var child in Children) { contentWidth = Mathf.Max(contentWidth, GetChildXMax(child)); contentHeight = Mathf.Max(contentHeight, GetChildYMax(child)); } } } private float GetChildXMax(GUIControl child) { return child.rect.xMax; } private float GetChildYMax(GUIControl child) { if (child is GUILabel) { GUILabel guiLabel = child as GUILabel; if ((guiLabel.autoSize != null) && (guiLabel.autoSize.autoSizeHeight)) { guiLabel.Refresh(new Vector2(rect.width, rect.height)); guiLabel.UpdateLayout(); } } return child.rect.yMax; } } }