/// <summary> /// Calculates the bounding rect (in absolute pixel coordinates) of the text object based on the text string contents, text formatting style and container unit rect. /// </summary> /// <param name="VirtualScale">The scaling factor used to map virtual units to pixel units if component unit type is virtual.</param> /// <param name="Parent">The parent rect (in pixel units) if component unit type is percentage.</param> /// <param name="Container">The container unit rect of the element this text object belongs to.</param> /// <returns>The bounding rect (in absolute pixel coordinates) of this text object.</returns> /// <remarks>Note: if either the width or height of the container are null, the size of the bounding rect will be that of the container.</remarks> /// <example> /// Calculate the absolute pixel bounding rect for a label widget within the specified parent rect: /// <code> /// UnityEngine.Rect rPx = text.CalculatePixelRect(VirtualScale, Parent, rect); /// </code> /// </example> public UnityEngine.Rect CalculatePixelRect(float VirtualScale, UnityEngine.Rect Parent, UnitRect Container) { UnityEngine.Vector2 size = CalculatePixelSize(VirtualScale); if (Container == null) { if (wordWrap == true) { throw new Exception("CalculatePixelRect with null container can only be used when wordwrap is disabled"); } return(new UnityEngine.Rect(0, 0, size.x, size.y)); } UnitRect r = new UnitRect(Container.pos.x, Container.pos.y, Container.dim.x, Container.dim.y, Container.hAlign, Container.vAlign); if (Container.dim.x.value == null) { r.dim.x.type = Unit.Type.Pixel; r.dim.x.value = size.x; } if (Container.dim.y.value == null) { if (wordWrap) { float w = r.dim.x.ToPixel(VirtualScale, Parent.width); float h = CalculateGUIStyle(VirtualScale).CalcHeight(new UnityEngine.GUIContent(text), w); r.dim.y.type = Unit.Type.Pixel; r.dim.y.value = h; } else { r.dim.y.type = Unit.Type.Pixel; r.dim.y.value = size.y; } } return(r.ToPixelRect(VirtualScale, Parent)); }
/// <summary> /// Presents the tab widget visually on the screen. /// </summary> /// <param name="VirtualScale">The scaling factor used to map virtual units to pixel units if component unit type is virtual.</param> /// <param name="Parent">The parent rect (in pixel units) that this object resides within.</param> public override void Present(float VirtualScale, UnityEngine.Rect Parent) { // Bounding pixel rect of entire tab widget container UnityEngine.Rect rPx = rect.ToPixelRect((float)VirtualScale, (UnityEngine.Rect)Parent); // Bounding pixel rects for the tab header and body elements UnityEngine.Rect pxHeader, pxBody; // Header height null, deduce from body height if (headerHeight != null && headerHeight.value != null) { UnitRect rectHeader = new UnitRect(new Unit(0, Unit.Type.Virtual), new Unit(0, Unit.Type.Virtual), new Unit(rPx.width, Unit.Type.Pixel), headerHeight, null, null); pxHeader = rectHeader.ToPixelRect(VirtualScale, rPx); pxBody = new UnityEngine.Rect(rPx.x, rPx.y + pxHeader.height, rPx.width, rPx.height - pxHeader.height); } // Body height null, deduce from header height? else if (bodyHeight != null && bodyHeight.value != null) { UnitRect rectBody = new UnitRect(new Unit(0, Unit.Type.Virtual), new Unit(0, Unit.Type.Virtual), new Unit(rPx.width, Unit.Type.Pixel), bodyHeight, null, null); pxBody = rectBody.ToPixelRect(VirtualScale, rPx); pxHeader = new UnityEngine.Rect(rPx.x, rPx.y + (rPx.height - pxBody.height), rPx.width, rPx.height - pxBody.height); pxHeader.y += rPx.height - pxBody.height; } else { throw new Exception("Tab must have either header or body element height specified"); } // Header background (not tabs) if (headerBackground != null) { headerBackground.Present(VirtualScale, pxHeader); } // Container pixel width for a given tab float tabContainerW = pxHeader.width / pages.pages.Count; // Actual tab width as constrained by maxTabWidth float tabW = tabContainerW; // Attempt to constrain tab width if (maxTabWidth != null && maxTabWidth.value != null && maxTabWidth.value >= 0) { float pxMaxTabWidth = maxTabWidth.ToPixel(VirtualScale, rPx.width); tabW = pxMaxTabWidth < tabW ? pxMaxTabWidth : tabW; } int i = 0; foreach (KeyValuePair <string, Page> p in pages.pages) { // Tab container (actual tab rect will be centered inside of this) float x = pxHeader.x + (i * tabContainerW); float y = pxHeader.y; float w = tabContainerW; float h = pxHeader.height; UnityEngine.Rect pxTabContainer = new UnityEngine.Rect(x, y, w, h); // Actual tab UnitRect rectTab = new UnitRect(new Unit(0, Unit.Type.Pixel), new Unit(0, Unit.Type.Pixel), new Unit(tabW, Unit.Type.Pixel), new Unit(100, Unit.Type.Percentage), new Alignment(Alignment.Type.Horizontal, Alignment.Direction.Center), null); UnityEngine.Rect pxTab = rectTab.ToPixelRect(VirtualScale, pxTabContainer); // Active tabs if (pages.activePage.title == p.Value.title) { if (tabText != null) { tabText.text = p.Value.title; activeTab.text = tabText; } activeTab.guiDispatcher.SetEventArg("onClick", "id", p.Value.id); activeTab.Present(VirtualScale, pxTab); } // Inactive tab else { if (tabText != null) { tabText.text = p.Value.title; inactiveTab.text = tabText; } inactiveTab.guiDispatcher.SetEventArg("onClick", "id", p.Value.id); inactiveTab.Present(VirtualScale, pxTab); } i++; } if (pages.activePage.visible) { pages.activePage.Present(VirtualScale, pxBody); } }
/// <summary> /// Initializes the presentable with a unit rect of null values. /// </summary> //public Presentable(bool Visible) //{ // rect = new UnitRect(); // visible = true; //} ///// <summary> ///// Initializes the presentable with a unit rect of null values and the id specified by <paramref name="ID"/>. ///// </summary> ///// <param name="ID"></param> //public Presentable(string ID, bool Visible) : base(ID) //{ // rect = new UnitRect(); // visible = Visible; //} /// <summary> /// Initializes the components of the presentable to the values specified by the strings. /// </summary> /// <param name="ID">The unique id (if any) of this object.</param> /// <param name="X">The x positioning of the unit rect relative to the parent as governed by the horizontal alignment as follows: /// <list type="table"> /// <listheader> /// <term>Alignment</term> /// <term>X Position</term> /// </listheader> /// <item> /// <term>null or empty</term> /// <term>x units from parent's left edge</term> /// </item> /// <item> /// <term>Left</term> /// <term>x units from parent's left edge</term> /// </item> /// <item> /// <term>Center</term> /// <term>x position is ignored</term> /// </item> /// <item> /// <term>Right</term> /// <term>x units from parent's right edge</term> /// </item> /// </list> /// </param> /// <param name="Y">The y positioning of the unit rect relative to the parent as governed by the vertical alignment as follows: /// <list type="table"> /// <listheader> /// <term>Alignment</term> /// <term>Y Position</term> /// </listheader> /// <item> /// <term>null or empty</term> /// <term>y units from parent's top edge</term> /// </item> /// <item> /// <term>Top</term> /// <term>y units from parent's top edge</term> /// </item> /// <item> /// <term>Center</term> /// <term>y position is ignored</term> /// </item> /// <item> /// <term>Bottom</term> /// <term>y units from parent's bottom edge</term> /// </item> /// </list> /// </param> /// <param name="W">The width of the unit rect.</param> /// <param name="H">The height of the unit rect.</param> /// <param name="HAlign">The horizontal alignemnt of the unit rect within the parent rect as specified by the following values: /// <list type="table"> /// <listheader> /// <term>String Value</term> /// <term>Alignment.Type</term> /// </listheader> /// <item> /// <term>null or empty</term> /// <term>null</term> /// </item> /// <item> /// <term>left</term> /// <term>Alignment.Type.Left</term> /// </item> /// <item> /// <term>center</term> /// <term>Alignment.Type.Center</term> /// </item> /// <item> /// <term>right</term> /// <term>Alignment.Type.Right</term> /// </item> /// </list> /// </param> /// <param name="VAlign">The horizontal alignemnt of the unit rect within the parent rect as specified by the following values: /// <list type="table"> /// <listheader> /// <term>String Value</term> /// <term>Alignment.Type</term> /// </listheader> /// <item> /// <term>null or empty</term> /// <term>null</term> /// </item> /// <item> /// <term>top</term> /// <term>Alignment.Type.Top</term> /// </item> /// <item> /// <term>center</term> /// <term>Alignment.Type.Center</term> /// </item> /// <item> /// <term>bottom</term> /// <term>Alignment.Type.Bottom</term> /// </item> /// </list> /// </param> /// <remarks>This constructor is used primarily in conjunction with XML-based layouts</remarks> public Presentable(string ID, string X, string Y, string W, string H, string HAlign, string VAlign, string Visible) : base(ID) { rect = new UnitRect(X, Y, W, H, HAlign, VAlign); visible = Visible == null || Visible == "" ? true : bool.Parse(Visible); }
/// <summary> /// Presents the list visually on the screen. /// </summary> /// <param name="VirtualScale">The scaling factor used to map virtual units to pixel units if component unit type is virtual.</param> /// <param name="Parent">The parent rect (in pixel units) that this object resides within.</param> public override void Present(float VirtualScale, UnityEngine.Rect Parent) { UnityEngine.Rect rPx = rect.ToPixelRect(VirtualScale, Parent); //UnityEngine.GUIStyle style = text.CalculateGUIStyle(VirtualScale); //// Line height of text item //text.text = "Dummy"; //float lineHeight = text.CalculatePixelSize(VirtualScale).y; //// Icon dimensions are square and of the same dimensions as the line height //UnitRect rectIcon = new UnitRect(null, null, new Unit(lineHeight, Unit.Type.Pixel), new Unit(lineHeight, Unit.Type.Pixel), null, null); //UnityEngine.Rect pxIcon = rectIcon.ToPixelRect(VirtualScale, rPx); float yOffset = 0; foreach (KeyValuePair <string, Button> item in items) { UnityEngine.GUIStyle style = item.Value.text.CalculateGUIStyle(VirtualScale); style.normal.textColor = item.Value.enabled ? text.colour ?? style.normal.textColor : _text.textDisabled ?? (text.colour ?? style.normal.textColor); // Line height of text item float lineHeight = item.Value.text.CalculatePixelSize(VirtualScale).y; // Icon dimensions are square and of the same dimensions as the line height UnitRect rectIcon = new UnitRect(null, null, new Unit(lineHeight, Unit.Type.Pixel), new Unit(lineHeight, Unit.Type.Pixel), null, null); UnityEngine.Rect pxIcon = rectIcon.ToPixelRect(VirtualScale, rPx); pxIcon.y += yOffset; // Text item UnityEngine.Rect pxItem = new UnityEngine.Rect(pxIcon); //text.text = item.Value; if (icon != null) { // Icon if (item.Value.visible) { icon.Present(VirtualScale, pxIcon); } // Offset the text item by one icon's worth of distance pxItem.x += lineHeight; } // Text item width pxItem.width = item.Value.text.CalculatePixelSize(VirtualScale).x; if (item.Value.visible && UnityEngine.GUI.Button(pxItem, item.Value.text.text, style) && item.Value.enabled) { guiDispatcher.SetEventArg("onClick", "key", item.Key); guiDispatcher.SetEventArg("onClick", "value", item.Value.text.text); guiDispatcher.Dispatch("onClick", this); } // Offset next item by one line yOffset += lineHeight * 1.5f; } }