/// <summary> /// Show the tool tip UI. /// </summary> /// <param name="info">The tool tip to show.</param> /// <param name="pointerPos">Where to position the UI in UI space (apply offset before calling).</param> private void ShowToolTip(ToolTipInfo info, Vector2 pointerPos) { bool positionToolTip = false; if (_currentToolTip != info || _hover) { // Changing or new tool tip. positionToolTip = true; } else { // Validate positioning: is the cursor over the tooltip? RectTransform ttui = ToolTipUI; if (ttui != null) { Rect rect = GetGlobalRect(ttui); positionToolTip = rect.Contains(pointerPos); } } if (positionToolTip) { Text ttx = ToolTipUIText; if (ttx != null) { _currentToolTip = info; ttx.text = info.ToolTip; RectTransform ttui = ToolTipUI; if (ttui != null) { ResizeToolTip(ttui, ttx, info); PositionToolTip(ttui, info, pointerPos); ttui.gameObject.SetActive(true); } } } _hover = false; }
protected int PopulateScrollRect(ScrollRect scroll, GameObject template, IEnumerable <FileSystemEntry> items, FileIconSet icons, int targetWidth, bool startHorizontal) { if (scroll == null) { return(0); } ClearScrollRect(scroll); if (template == null) { return(0); } // We modify the template width because it's the the simplest way to implement this feature. if (targetWidth > 0) { // Resize the icon. RectTransform rect = template.transform as RectTransform; if (rect != null) { var sd = rect.sizeDelta; sd.x = targetWidth; rect.sizeDelta = sd; } } GridLayoutGroup layoutGrid = scroll.content.GetComponent <GridLayoutGroup>(); if (layoutGrid != null) { layoutGrid.startAxis = (startHorizontal) ? GridLayoutGroup.Axis.Horizontal : GridLayoutGroup.Axis.Vertical; } // Create and position each item. int itemCount = 0; // Dodgy way to simulate many items for testing. foreach (FileSystemEntry item in items) { // Create item GameObject itemUI = GameObject.Instantiate(template); Text itemText = itemUI.GetComponentInChildren <Text>(); FileEntryComponent entry = itemUI.GetComponent <FileEntryComponent>(); Sprite sprite = (icons != null) ? icons.GetIcon(item) : null; ToolTipInfo toolTip = itemUI.GetComponentInChildren <ToolTipInfo>(); // We are only allowed to modify an empty tool tip. if (toolTip != null && toolTip.ToolTip.Length > 0) { toolTip = null; } // Ensure tag if (entry == null) { entry = itemUI.AddComponent <FileEntryComponent>(); } entry.Entry = item; // We expect the item to have two images; an icon image and an disabled highlight. // We can expect that the highlight is disabled, to hide it for now, and must come // first because of Unity uGUI draw order/hierarchy constraints. // For simplicity, we assume the first image is the highlight, the second image is // the icon. The icon image is optional. foreach (Image image in itemUI.GetComponentsInChildren <Image>()) { if (entry.Highlight == null) { entry.Highlight = image; } else if (entry.Icon == null) { // All done if we are here. entry.Icon = image; break; } } // Set text. if (itemText != null) { itemText.text = item.Name; if (toolTip != null) { toolTip.ToolTip = item.Name; } } if (entry.Highlight) { entry.Highlight.enabled = false; } // Set image if (entry.Icon != null && sprite != null) { entry.Icon.sprite = sprite; } itemUI.transform.SetParent(scroll.content, false); ++itemCount; } return(itemCount); }