GetAtlasSprite() public method

Retrieve the atlas sprite referenced by the spriteName field.
public GetAtlasSprite ( ) : UIAtlas.Sprite,
return UIAtlas.Sprite,
	/// <summary>
	/// Show the popup list dialog.
	/// </summary>

	public void Show ()
	{
		if (enabled && NGUITools.GetActive(gameObject) && mChild == null && atlas != null && isValid && items.Count > 0)
		{
			mLabelList.Clear();

			// Automatically locate the panel responsible for this object
			if (mPanel == null)
			{
				mPanel = UIPanel.Find(transform);
				if (mPanel == null) return;
			}

			// Disable the navigation script
			handleEvents = true;

			// Calculate the dimensions of the object triggering the popup list so we can position it below it
			Transform myTrans = transform;
			Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(myTrans.parent, myTrans);

			// Create the root object for the list
			mChild = new GameObject("Drop-down List");
			mChild.layer = gameObject.layer;

			Transform t = mChild.transform;
			t.parent = myTrans.parent;
			t.localPosition = bounds.min;
			t.localRotation = Quaternion.identity;
			t.localScale = Vector3.one;

			// Add a sprite for the background
			mBackground = NGUITools.AddSprite(mChild, atlas, backgroundSprite);
			mBackground.pivot = UIWidget.Pivot.TopLeft;
			mBackground.depth = NGUITools.CalculateNextDepth(mPanel.gameObject);
			mBackground.color = backgroundColor;

			// We need to know the size of the background sprite for padding purposes
			Vector4 bgPadding = mBackground.border;
			mBgBorder = bgPadding.y;
			mBackground.cachedTransform.localPosition = new Vector3(0f, bgPadding.y, 0f);

			// Add a sprite used for the selection
			mHighlight = NGUITools.AddSprite(mChild, atlas, highlightSprite);
			mHighlight.pivot = UIWidget.Pivot.TopLeft;
			mHighlight.color = highlightColor;

			UISpriteData hlsp = mHighlight.GetAtlasSprite();
			if (hlsp == null) return;

			float hlspHeight = hlsp.borderTop;
			float fontHeight = activeFontSize;
			float dynScale = activeFontScale;
			float labelHeight = fontHeight * dynScale;
			float x = 0f, y = -padding.y;
			int labelFontSize = (bitmapFont != null) ? bitmapFont.defaultSize : fontSize;
			List<UILabel> labels = new List<UILabel>();

			// Clear the selection if it's no longer present
			if (!items.Contains(mSelectedItem))
				mSelectedItem = null;

			// Run through all items and create labels for each one
			for (int i = 0, imax = items.Count; i < imax; ++i)
			{
				string s = items[i];

				UILabel lbl = NGUITools.AddWidget<UILabel>(mChild);
				lbl.name = i.ToString();
				lbl.pivot = UIWidget.Pivot.TopLeft;
				lbl.bitmapFont = bitmapFont;
				lbl.trueTypeFont = trueTypeFont;
				lbl.fontSize = labelFontSize;
				lbl.fontStyle = fontStyle;
				lbl.text = isLocalized ? Localization.Get(s) : s;
				lbl.color = textColor;
				lbl.cachedTransform.localPosition = new Vector3(bgPadding.x + padding.x, y, -1f);
				lbl.overflowMethod = UILabel.Overflow.ResizeFreely;
				lbl.alignment = alignment;
				lbl.MakePixelPerfect();
				if (dynScale != 1f) lbl.cachedTransform.localScale = Vector3.one * dynScale;
				labels.Add(lbl);

				y -= labelHeight;
				y -= padding.y;
				x = Mathf.Max(x, lbl.printedSize.x);

				// Add an event listener
				UIEventListener listener = UIEventListener.Get(lbl.gameObject);
				listener.onHover = OnItemHover;
				listener.onPress = OnItemPress;
				listener.onClick = OnItemClick;
				listener.parameter = s;

				// Move the selection here if this is the right label
				if (mSelectedItem == s || (i == 0 && string.IsNullOrEmpty(mSelectedItem)))
					Highlight(lbl, true);

				// Add this label to the list
				mLabelList.Add(lbl);
			}

			// The triggering widget's width should be the minimum allowed width
			x = Mathf.Max(x, bounds.size.x * dynScale - (bgPadding.x + padding.x) * 2f);

			float cx = x / dynScale;
			Vector3 bcCenter = new Vector3(cx * 0.5f, -fontHeight * 0.5f, 0f);
			Vector3 bcSize = new Vector3(cx, (labelHeight + padding.y) / dynScale, 1f);

			// Run through all labels and add colliders
			for (int i = 0, imax = labels.Count; i < imax; ++i)
			{
				UILabel lbl = labels[i];
				NGUITools.AddWidgetCollider(lbl.gameObject);
				lbl.autoResizeBoxCollider = false;
				BoxCollider bc = lbl.GetComponent<BoxCollider>();

				if (bc != null)
				{
					bcCenter.z = bc.center.z;
					bc.center = bcCenter;
					bc.size = bcSize;
				}
				else
				{
					BoxCollider2D b2d = lbl.GetComponent<BoxCollider2D>();
					b2d.offset = bcCenter;
					b2d.size = bcSize;
				}
			}

			int lblWidth = Mathf.RoundToInt(x);
			x += (bgPadding.x + padding.x) * 2f;
			y -= bgPadding.y;

			// Scale the background sprite to envelop the entire set of items
			mBackground.width = Mathf.RoundToInt(x);
			mBackground.height = Mathf.RoundToInt(-y + bgPadding.y);

			// Set the label width to make alignment work
			for (int i = 0, imax = labels.Count; i < imax; ++i)
			{
				UILabel lbl = labels[i];
				lbl.overflowMethod = UILabel.Overflow.ShrinkContent;
				lbl.width = lblWidth;
			}

			// Scale the highlight sprite to envelop a single item
			float scaleFactor = 2f * atlas.pixelSize;
			float w = x - (bgPadding.x + padding.x) * 2f + hlsp.borderLeft * scaleFactor;
			float h = labelHeight + hlspHeight * scaleFactor;
			mHighlight.width = Mathf.RoundToInt(w);
			mHighlight.height = Mathf.RoundToInt(h);

			bool placeAbove = (position == Position.Above);

			if (position == Position.Auto)
			{
				UICamera cam = UICamera.FindCameraForLayer(gameObject.layer);

				if (cam != null)
				{
					Vector3 viewPos = cam.cachedCamera.WorldToViewportPoint(myTrans.position);
					placeAbove = (viewPos.y < 0.5f);
				}
			}

			// If the list should be animated, let's animate it by expanding it
			if (isAnimated)
			{
				float bottom = y + labelHeight;
				Animate(mHighlight, placeAbove, bottom);
				for (int i = 0, imax = labels.Count; i < imax; ++i) Animate(labels[i], placeAbove, bottom);
				AnimateColor(mBackground);
				AnimateScale(mBackground, placeAbove, bottom);
			}

			// If we need to place the popup list above the item, we need to reposition everything by the size of the list
			if (placeAbove)
			{
				t.localPosition = new Vector3(bounds.min.x, bounds.max.y - y - bgPadding.y, bounds.min.z);
			}
		}
		else OnSelect(false);
	}
Exemplo n.º 2
0
    /// <summary>
    /// Display the drop-down list when the game object gets clicked on.
    /// </summary>
    void OnClick()
    {
        if (mChild == null && atlas != null && font != null && items.Count > 0)
        {
            mLabelList.Clear();

            // Disable the navigation script
            handleEvents = true;

            // Automatically locate the panel responsible for this object
            if (mPanel == null) mPanel = UIPanel.Find(transform, true);

            // Calculate the dimensions of the object triggering the popup list so we can position it below it
            Transform myTrans = transform;
            Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(myTrans.parent, myTrans);

            // Create the root object for the list
            mChild = new GameObject("Drop-down List");
            mChild.layer = gameObject.layer;

            Transform t = mChild.transform;
            t.parent = myTrans.parent;
            t.localPosition = bounds.min;
            t.localRotation = Quaternion.identity;
            t.localScale = Vector3.one;

            // Add a sprite for the background
            mBackground = NGUITools.AddSprite(mChild, atlas, backgroundSprite);
            mBackground.pivot = UIWidget.Pivot.TopLeft;
            mBackground.depth = NGUITools.CalculateNextDepth(mPanel.gameObject);
            mBackground.color = backgroundColor;

            // We need to know the size of the background sprite for padding purposes
            Vector4 bgPadding = mBackground.border;
            mBgBorder = bgPadding.y;

            mBackground.cachedTransform.localPosition = new Vector3(0f, bgPadding.y, 0f);

            // Add a sprite used for the selection
            mHighlight = NGUITools.AddSprite(mChild, atlas, highlightSprite);
            mHighlight.pivot = UIWidget.Pivot.TopLeft;
            mHighlight.color = highlightColor;

            UIAtlas.Sprite hlsp = mHighlight.GetAtlasSprite();
            if (hlsp == null) return;

            float hlspHeight = hlsp.inner.yMin - hlsp.outer.yMin;
            float fontScale = font.size * font.pixelSize * textScale;
            float x = 0f, y = -padding.y;
            List<UILabel> labels = new List<UILabel>();

            // Run through all items and create labels for each one
            for (int i = 0, imax = items.Count; i < imax; ++i)
            {
                string s = items[i];

                UILabel lbl = NGUITools.AddWidget<UILabel>(mChild);
                lbl.pivot = UIWidget.Pivot.TopLeft;
                lbl.font = font;
                lbl.text = (isLocalized && Localization.instance != null) ? Localization.instance.Get(s) : s;
                lbl.color = textColor;
                lbl.cachedTransform.localPosition = new Vector3(bgPadding.x + padding.x, y, -0.01f);
                lbl.MakePixelPerfect();

                if (textScale != 1f)
                {
                    Vector3 scale = lbl.cachedTransform.localScale;
                    lbl.cachedTransform.localScale = scale * textScale;
                }
                labels.Add(lbl);

                y -= fontScale;
                y -= padding.y;
                x = Mathf.Max(x, lbl.relativeSize.x * fontScale);

                // Add an event listener
                UIEventListener listener = UIEventListener.Get(lbl.gameObject);
                listener.onHover = OnItemHover;
                listener.onPress = OnItemPress;
                listener.parameter = s;

                // Move the selection here if this is the right label
                if (mSelectedItem == s) Highlight(lbl, true);

                // Add this label to the list
                mLabelList.Add(lbl);
            }

            // The triggering widget's width should be the minimum allowed width
            x = Mathf.Max(x, bounds.size.x - (bgPadding.x + padding.x) * 2f);

            Vector3 bcCenter = new Vector3((x * 0.5f) / fontScale, -0.5f, 0f);
            Vector3 bcSize = new Vector3(x / fontScale, (fontScale + padding.y) / fontScale, 1f);

            // Run through all labels and add colliders
            for (int i = 0, imax = labels.Count; i < imax; ++i)
            {
                UILabel lbl = labels[i];
                BoxCollider bc = NGUITools.AddWidgetCollider(lbl.gameObject);
                bcCenter.z = bc.center.z;
                bc.center = bcCenter;
                bc.size = bcSize;
            }

            x += (bgPadding.x + padding.x) * 2f;
            y -= bgPadding.y;

            // Scale the background sprite to envelop the entire set of items
            mBackground.cachedTransform.localScale = new Vector3(x, -y + bgPadding.y, 1f);

            // Scale the highlight sprite to envelop a single item
            mHighlight.cachedTransform.localScale = new Vector3(
                x - (bgPadding.x + padding.x) * 2f + (hlsp.inner.xMin - hlsp.outer.xMin) * 2f,
                fontScale + hlspHeight * 2f, 1f);

            bool placeAbove = (position == Position.Above);

            if (position == Position.Auto)
            {
                UICamera cam = UICamera.FindCameraForLayer(gameObject.layer);

                if (cam != null)
                {
                    Vector3 viewPos = cam.cachedCamera.WorldToViewportPoint(myTrans.position);
                    placeAbove = (viewPos.y < 0.5f);
                }
            }

            // If the list should be animated, let's animate it by expanding it
            if (isAnimated)
            {
                float bottom = y + fontScale;
                Animate(mHighlight, placeAbove, bottom);
                for (int i = 0, imax = labels.Count; i < imax; ++i) Animate(labels[i], placeAbove, bottom);
                AnimateColor(mBackground);
                AnimateScale(mBackground, placeAbove, bottom);
            }

            // If we need to place the popup list above the item, we need to reposition everything by the size of the list
            if (placeAbove)
            {
                t.localPosition = new Vector3(bounds.min.x, bounds.max.y - y - bgPadding.y, bounds.min.z);
            }
        }
        else OnSelect(false);
    }
Exemplo n.º 3
0
	/// <summary>
	/// Show the popup list dialog.
	/// </summary>

	public void Show ()
	{
		if (enabled && NGUITools.GetActive(gameObject) && mChild == null && atlas != null && isValid && items.Count > 0)
		{
			mLabelList.Clear();
			StopCoroutine("CloseIfUnselected");

			// Ensure the popup's source has the selection
			UICamera.selectedObject = (UICamera.hoveredObject ?? gameObject);
			mSelection = UICamera.selectedObject;
			source = UICamera.selectedObject;

			if (source == null)
			{
				Debug.LogError("Popup list needs a source object...");
				return;
			}

			mOpenFrame = Time.frameCount;

			// Automatically locate the panel responsible for this object
			if (mPanel == null)
			{
				mPanel = UIPanel.Find(transform);
				if (mPanel == null) return;
			}

			// Calculate the dimensions of the object triggering the popup list so we can position it below it
			Vector3 min;
			Vector3 max;

			// Create the root object for the list
			mChild = new GameObject("Drop-down List");
			mChild.layer = gameObject.layer;
			current = this;

			Transform t = mChild.transform;
			t.parent = mPanel.cachedTransform;
			Vector3 pos;

			// Manually triggered popup list on some other game object
			if (openOn == OpenOn.Manual && mSelection != gameObject)
			{
				pos = UICamera.lastEventPosition;
				min = mPanel.cachedTransform.InverseTransformPoint(mPanel.anchorCamera.ScreenToWorldPoint(pos));
				max = min;
				t.localPosition = min;
				pos = t.position;
			}
			else
			{
				Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(mPanel.cachedTransform, transform, false, false);
				min = bounds.min;
				max = bounds.max;
				t.localPosition = min;
				pos = t.position;
			}

			StartCoroutine("CloseIfUnselected");

			t.localRotation = Quaternion.identity;
			t.localScale = Vector3.one;

			// Add a sprite for the background
			mBackground = NGUITools.AddSprite(mChild, atlas, backgroundSprite);
			mBackground.pivot = UIWidget.Pivot.TopLeft;
			mBackground.depth = NGUITools.CalculateNextDepth(mPanel.gameObject);
			mBackground.color = backgroundColor;

			// We need to know the size of the background sprite for padding purposes
			Vector4 bgPadding = mBackground.border;
			mBgBorder = bgPadding.y;
			mBackground.cachedTransform.localPosition = new Vector3(0f, bgPadding.y, 0f);

			// Add a sprite used for the selection
			mHighlight = NGUITools.AddSprite(mChild, atlas, highlightSprite);
			mHighlight.pivot = UIWidget.Pivot.TopLeft;
			mHighlight.color = highlightColor;

			UISpriteData hlsp = mHighlight.GetAtlasSprite();
			if (hlsp == null) return;

			float hlspHeight = hlsp.borderTop;
			float fontHeight = activeFontSize;
			float dynScale = activeFontScale;
			float labelHeight = fontHeight * dynScale;
			float x = 0f, y = -padding.y;
			List<UILabel> labels = new List<UILabel>();

			// Clear the selection if it's no longer present
			if (!items.Contains(mSelectedItem))
				mSelectedItem = null;

			// Run through all items and create labels for each one
			for (int i = 0, imax = items.Count; i < imax; ++i)
			{
				string s = items[i];

				UILabel lbl = NGUITools.AddWidget<UILabel>(mChild);
				lbl.name = i.ToString();
				lbl.pivot = UIWidget.Pivot.TopLeft;
				lbl.bitmapFont = bitmapFont;
				lbl.trueTypeFont = trueTypeFont;
				lbl.fontSize = fontSize;
				lbl.fontStyle = fontStyle;
				lbl.text = isLocalized ? Localization.Get(s) : s;
				lbl.color = textColor;
				lbl.cachedTransform.localPosition = new Vector3(bgPadding.x + padding.x - lbl.pivotOffset.x, y, -1f);
				lbl.overflowMethod = UILabel.Overflow.ResizeFreely;
				lbl.alignment = alignment;
				labels.Add(lbl);

				y -= labelHeight;
				y -= padding.y;
				x = Mathf.Max(x, lbl.printedSize.x);

				// Add an event listener
				UIEventListener listener = UIEventListener.Get(lbl.gameObject);
				listener.onHover = OnItemHover;
				listener.onPress = OnItemPress;
				listener.parameter = s;

				// Move the selection here if this is the right label
				if (mSelectedItem == s || (i == 0 && string.IsNullOrEmpty(mSelectedItem)))
					Highlight(lbl, true);

				// Add this label to the list
				mLabelList.Add(lbl);
			}

			// The triggering widget's width should be the minimum allowed width
			x = Mathf.Max(x, (max.x - min.x) - (bgPadding.x + padding.x) * 2f);

			float cx = x;
			Vector3 bcCenter = new Vector3(cx * 0.5f, -labelHeight * 0.5f, 0f);
			Vector3 bcSize = new Vector3(cx, (labelHeight + padding.y), 1f);

			// Run through all labels and add colliders
			for (int i = 0, imax = labels.Count; i < imax; ++i)
			{
				UILabel lbl = labels[i];
				NGUITools.AddWidgetCollider(lbl.gameObject);
				lbl.autoResizeBoxCollider = false;
				BoxCollider bc = lbl.GetComponent<BoxCollider>();

				if (bc != null)
				{
					bcCenter.z = bc.center.z;
					bc.center = bcCenter;
					bc.size = bcSize;
				}
				else
				{
					BoxCollider2D b2d = lbl.GetComponent<BoxCollider2D>();
#if UNITY_4_3 || UNITY_4_5 || UNITY_4_6
					b2d.center = bcCenter;
#else
					b2d.offset = bcCenter;
#endif
					b2d.size = bcSize;
				}
			}

			int lblWidth = Mathf.RoundToInt(x);
			x += (bgPadding.x + padding.x) * 2f;
			y -= bgPadding.y;

			// Scale the background sprite to envelop the entire set of items
			mBackground.width = Mathf.RoundToInt(x);
			mBackground.height = Mathf.RoundToInt(-y + bgPadding.y);

			// Set the label width to make alignment work
			for (int i = 0, imax = labels.Count; i < imax; ++i)
			{
				UILabel lbl = labels[i];
				lbl.overflowMethod = UILabel.Overflow.ShrinkContent;
				lbl.width = lblWidth;
			}

			// Scale the highlight sprite to envelop a single item
			float scaleFactor = 2f * atlas.pixelSize;
			float w = x - (bgPadding.x + padding.x) * 2f + hlsp.borderLeft * scaleFactor;
			float h = labelHeight + hlspHeight * scaleFactor;
			mHighlight.width = Mathf.RoundToInt(w);
			mHighlight.height = Mathf.RoundToInt(h);

			bool placeAbove = (position == Position.Above);

			if (position == Position.Auto)
			{
				UICamera cam = UICamera.FindCameraForLayer(mSelection.layer);

				if (cam != null)
				{
					Vector3 viewPos = cam.cachedCamera.WorldToViewportPoint(pos);
					placeAbove = (viewPos.y < 0.5f);
				}
			}

			// If the list should be animated, let's animate it by expanding it
			if (isAnimated)
			{
				AnimateColor(mBackground);

				if (Time.timeScale == 0f || Time.timeScale >= 0.1f)
				{
					float bottom = y + labelHeight;
					Animate(mHighlight, placeAbove, bottom);
					for (int i = 0, imax = labels.Count; i < imax; ++i)
						Animate(labels[i], placeAbove, bottom);
					AnimateScale(mBackground, placeAbove, bottom);
				}
			}

			// If we need to place the popup list above the item, we need to reposition everything by the size of the list
			if (placeAbove)
			{
				min.y = max.y - bgPadding.y;
				max.y = min.y + mBackground.height;
				max.x = min.x + mBackground.width;
				t.localPosition = new Vector3(min.x, max.y - bgPadding.y, min.z);
			}
			else
			{
				max.y = min.y + bgPadding.y;
				min.y = max.y - mBackground.height;
				max.x = min.x + mBackground.width;
			}

			Transform pt = mPanel.cachedTransform.parent;

			if (pt != null)
			{
				min = mPanel.cachedTransform.TransformPoint(min);
				max = mPanel.cachedTransform.TransformPoint(max);
				min = pt.InverseTransformPoint(min);
				max = pt.InverseTransformPoint(max);
			}

			// Ensure that everything fits into the panel's visible range
			Vector3 offset = mPanel.CalculateConstrainOffset(min, max);
			pos = t.localPosition + offset;
			pos.x = Mathf.Round(pos.x);
			pos.y = Mathf.Round(pos.y);
			t.localPosition = pos;
		}
		else OnSelect(false);
	}
Exemplo n.º 4
0
 public void Show()
 {
     //IL_000c: Unknown result type (might be due to invalid IL or missing references)
     //IL_0011: Expected O, but got Unknown
     //IL_007b: Unknown result type (might be due to invalid IL or missing references)
     //IL_00d5: Unknown result type (might be due to invalid IL or missing references)
     //IL_00da: Expected O, but got Unknown
     //IL_00fb: Unknown result type (might be due to invalid IL or missing references)
     //IL_0100: Expected O, but got Unknown
     //IL_010b: Unknown result type (might be due to invalid IL or missing references)
     //IL_0125: Unknown result type (might be due to invalid IL or missing references)
     //IL_012a: Expected O, but got Unknown
     //IL_014f: Unknown result type (might be due to invalid IL or missing references)
     //IL_015e: Unknown result type (might be due to invalid IL or missing references)
     //IL_0163: Unknown result type (might be due to invalid IL or missing references)
     //IL_0168: Unknown result type (might be due to invalid IL or missing references)
     //IL_017f: Unknown result type (might be due to invalid IL or missing references)
     //IL_0180: Unknown result type (might be due to invalid IL or missing references)
     //IL_0185: Unknown result type (might be due to invalid IL or missing references)
     //IL_018a: Unknown result type (might be due to invalid IL or missing references)
     //IL_018b: Unknown result type (might be due to invalid IL or missing references)
     //IL_018c: Unknown result type (might be due to invalid IL or missing references)
     //IL_018e: Unknown result type (might be due to invalid IL or missing references)
     //IL_0195: Unknown result type (might be due to invalid IL or missing references)
     //IL_019a: Unknown result type (might be due to invalid IL or missing references)
     //IL_01ac: Unknown result type (might be due to invalid IL or missing references)
     //IL_01b3: Expected O, but got Unknown
     //IL_01b3: Unknown result type (might be due to invalid IL or missing references)
     //IL_01b8: Unknown result type (might be due to invalid IL or missing references)
     //IL_01bc: Unknown result type (might be due to invalid IL or missing references)
     //IL_01c1: Unknown result type (might be due to invalid IL or missing references)
     //IL_01c4: Unknown result type (might be due to invalid IL or missing references)
     //IL_01c9: Unknown result type (might be due to invalid IL or missing references)
     //IL_01cb: Unknown result type (might be due to invalid IL or missing references)
     //IL_01d2: Unknown result type (might be due to invalid IL or missing references)
     //IL_01d7: Unknown result type (might be due to invalid IL or missing references)
     //IL_01de: Unknown result type (might be due to invalid IL or missing references)
     //IL_01e5: Unknown result type (might be due to invalid IL or missing references)
     //IL_01f0: Unknown result type (might be due to invalid IL or missing references)
     //IL_022e: Unknown result type (might be due to invalid IL or missing references)
     //IL_0233: Expected O, but got Unknown
     //IL_0244: Unknown result type (might be due to invalid IL or missing references)
     //IL_0254: Unknown result type (might be due to invalid IL or missing references)
     //IL_0259: Unknown result type (might be due to invalid IL or missing references)
     //IL_0284: Unknown result type (might be due to invalid IL or missing references)
     //IL_02bd: Unknown result type (might be due to invalid IL or missing references)
     //IL_03a7: Unknown result type (might be due to invalid IL or missing references)
     //IL_03d4: Unknown result type (might be due to invalid IL or missing references)
     //IL_03fa: Unknown result type (might be due to invalid IL or missing references)
     //IL_03ff: Unknown result type (might be due to invalid IL or missing references)
     //IL_0410: Unknown result type (might be due to invalid IL or missing references)
     //IL_0453: Unknown result type (might be due to invalid IL or missing references)
     //IL_0458: Unknown result type (might be due to invalid IL or missing references)
     //IL_046a: Unknown result type (might be due to invalid IL or missing references)
     //IL_046f: Expected O, but got Unknown
     //IL_0580: Unknown result type (might be due to invalid IL or missing references)
     //IL_0585: Expected O, but got Unknown
     //IL_05ac: Unknown result type (might be due to invalid IL or missing references)
     //IL_05b1: Unknown result type (might be due to invalid IL or missing references)
     //IL_05c1: Unknown result type (might be due to invalid IL or missing references)
     //IL_05ca: Unknown result type (might be due to invalid IL or missing references)
     //IL_05e1: Unknown result type (might be due to invalid IL or missing references)
     //IL_05e3: Unknown result type (might be due to invalid IL or missing references)
     //IL_05ef: Unknown result type (might be due to invalid IL or missing references)
     //IL_05f1: Unknown result type (might be due to invalid IL or missing references)
     //IL_074d: Unknown result type (might be due to invalid IL or missing references)
     //IL_074e: Unknown result type (might be due to invalid IL or missing references)
     //IL_0753: Unknown result type (might be due to invalid IL or missing references)
     //IL_0865: Unknown result type (might be due to invalid IL or missing references)
     //IL_08cb: Unknown result type (might be due to invalid IL or missing references)
     //IL_08d0: Expected O, but got Unknown
     //IL_08ea: Unknown result type (might be due to invalid IL or missing references)
     //IL_08eb: Unknown result type (might be due to invalid IL or missing references)
     //IL_08f0: Unknown result type (might be due to invalid IL or missing references)
     //IL_08fc: Unknown result type (might be due to invalid IL or missing references)
     //IL_08fd: Unknown result type (might be due to invalid IL or missing references)
     //IL_0902: Unknown result type (might be due to invalid IL or missing references)
     //IL_0905: Unknown result type (might be due to invalid IL or missing references)
     //IL_0906: Unknown result type (might be due to invalid IL or missing references)
     //IL_090b: Unknown result type (might be due to invalid IL or missing references)
     //IL_090e: Unknown result type (might be due to invalid IL or missing references)
     //IL_090f: Unknown result type (might be due to invalid IL or missing references)
     //IL_0914: Unknown result type (might be due to invalid IL or missing references)
     //IL_091b: Unknown result type (might be due to invalid IL or missing references)
     //IL_091c: Unknown result type (might be due to invalid IL or missing references)
     //IL_0921: Unknown result type (might be due to invalid IL or missing references)
     //IL_0922: Unknown result type (might be due to invalid IL or missing references)
     //IL_0927: Unknown result type (might be due to invalid IL or missing references)
     //IL_092c: Unknown result type (might be due to invalid IL or missing references)
     //IL_092f: Unknown result type (might be due to invalid IL or missing references)
     //IL_0934: Unknown result type (might be due to invalid IL or missing references)
     //IL_0936: Unknown result type (might be due to invalid IL or missing references)
     //IL_093b: Unknown result type (might be due to invalid IL or missing references)
     //IL_0963: Unknown result type (might be due to invalid IL or missing references)
     if (this.get_enabled() && NGUITools.GetActive(this.get_gameObject()) && mChild == null && atlas != null && isValid && items.Count > 0)
     {
         mLabelList.Clear();
         this.StopCoroutine("CloseIfUnselected");
         UICamera.selectedObject = (UICamera.hoveredObject ?? this.get_gameObject());
         mSelection = UICamera.selectedObject;
         source     = UICamera.selectedObject;
         if (source == null)
         {
             Debug.LogError((object)"Popup list needs a source object...");
         }
         else
         {
             mOpenFrame = Time.get_frameCount();
             if (mPanel == null)
             {
                 mPanel = UIPanel.Find(this.get_transform());
                 if (mPanel == null)
                 {
                     return;
                 }
             }
             mChild = new GameObject("Drop-down List");
             mChild.set_layer(this.get_gameObject().get_layer());
             current = this;
             Transform val = mChild.get_transform();
             val.set_parent(mPanel.cachedTransform);
             Vector3 val2;
             Vector3 val3;
             Vector3 val4;
             if (openOn == OpenOn.Manual && mSelection != this.get_gameObject())
             {
                 val2 = Vector2.op_Implicit(UICamera.lastEventPosition);
                 val3 = mPanel.cachedTransform.InverseTransformPoint(mPanel.anchorCamera.ScreenToWorldPoint(val2));
                 val4 = val3;
                 val.set_localPosition(val3);
                 val2 = val.get_position();
             }
             else
             {
                 Bounds val5 = NGUIMath.CalculateRelativeWidgetBounds(mPanel.cachedTransform, this.get_transform(), false, false);
                 val3 = val5.get_min();
                 val4 = val5.get_max();
                 val.set_localPosition(val3);
                 val2 = val.get_position();
             }
             this.StartCoroutine("CloseIfUnselected");
             val.set_localRotation(Quaternion.get_identity());
             val.set_localScale(Vector3.get_one());
             mBackground       = NGUITools.AddSprite(mChild, atlas, backgroundSprite);
             mBackground.pivot = UIWidget.Pivot.TopLeft;
             mBackground.depth = NGUITools.CalculateNextDepth(mPanel.get_gameObject());
             mBackground.color = backgroundColor;
             Vector4 border = mBackground.border;
             mBgBorder = border.y;
             mBackground.cachedTransform.set_localPosition(new Vector3(0f, border.y, 0f));
             mHighlight       = NGUITools.AddSprite(mChild, atlas, highlightSprite);
             mHighlight.pivot = UIWidget.Pivot.TopLeft;
             mHighlight.color = highlightColor;
             UISpriteData atlasSprite = mHighlight.GetAtlasSprite();
             if (atlasSprite != null)
             {
                 float          num             = (float)atlasSprite.borderTop;
                 float          num2            = (float)activeFontSize;
                 float          activeFontScale = this.activeFontScale;
                 float          num3            = num2 * activeFontScale;
                 float          num4            = 0f;
                 float          num5            = 0f - padding.y;
                 List <UILabel> list            = new List <UILabel>();
                 if (!items.Contains(mSelectedItem))
                 {
                     mSelectedItem = null;
                 }
                 int i = 0;
                 for (int count = items.Count; i < count; i++)
                 {
                     string  text    = items[i];
                     UILabel uILabel = NGUITools.AddWidget <UILabel>(mChild);
                     uILabel.set_name(i.ToString());
                     uILabel.pivot        = UIWidget.Pivot.TopLeft;
                     uILabel.bitmapFont   = bitmapFont;
                     uILabel.trueTypeFont = trueTypeFont;
                     uILabel.fontSize     = fontSize;
                     uILabel.fontStyle    = fontStyle;
                     uILabel.text         = ((!isLocalized) ? text : Localization.Get(text));
                     uILabel.color        = textColor;
                     object  cachedTransform = (object)uILabel.cachedTransform;
                     float   num6            = border.x + padding.x;
                     Vector2 pivotOffset     = uILabel.pivotOffset;
                     cachedTransform.set_localPosition(new Vector3(num6 - pivotOffset.x, num5, -1f));
                     uILabel.overflowMethod = UILabel.Overflow.ResizeFreely;
                     uILabel.alignment      = alignment;
                     list.Add(uILabel);
                     num5 -= num3;
                     num5 -= padding.y;
                     float   num7        = num4;
                     Vector2 printedSize = uILabel.printedSize;
                     num4 = Mathf.Max(num7, printedSize.x);
                     UIEventListener uIEventListener = UIEventListener.Get(uILabel.get_gameObject());
                     uIEventListener.onHover   = OnItemHover;
                     uIEventListener.onPress   = OnItemPress;
                     uIEventListener.parameter = text;
                     if (mSelectedItem == text || (i == 0 && string.IsNullOrEmpty(mSelectedItem)))
                     {
                         Highlight(uILabel, true);
                     }
                     mLabelList.Add(uILabel);
                 }
                 num4 = Mathf.Max(num4, val4.x - val3.x - (border.x + padding.x) * 2f);
                 float   num8 = num4;
                 Vector3 val6 = default(Vector3);
                 val6._002Ector(num8 * 0.5f, (0f - num3) * 0.5f, 0f);
                 Vector3 val7 = default(Vector3);
                 val7._002Ector(num8, num3 + padding.y, 1f);
                 int j = 0;
                 for (int count2 = list.Count; j < count2; j++)
                 {
                     UILabel uILabel2 = list[j];
                     NGUITools.AddWidgetCollider(uILabel2.get_gameObject());
                     uILabel2.autoResizeBoxCollider = false;
                     BoxCollider component = uILabel2.GetComponent <BoxCollider>();
                     if (component != null)
                     {
                         Vector3 center = component.get_center();
                         val6.z = center.z;
                         component.set_center(val6);
                         component.set_size(val7);
                     }
                     else
                     {
                         BoxCollider2D component2 = uILabel2.GetComponent <BoxCollider2D>();
                         component2.set_offset(Vector2.op_Implicit(val6));
                         component2.set_size(Vector2.op_Implicit(val7));
                     }
                 }
                 int width = Mathf.RoundToInt(num4);
                 num4 += (border.x + padding.x) * 2f;
                 num5 -= border.y;
                 mBackground.width  = Mathf.RoundToInt(num4);
                 mBackground.height = Mathf.RoundToInt(0f - num5 + border.y);
                 int k = 0;
                 for (int count3 = list.Count; k < count3; k++)
                 {
                     UILabel uILabel3 = list[k];
                     uILabel3.overflowMethod = UILabel.Overflow.ShrinkContent;
                     uILabel3.width          = width;
                 }
                 float num9  = 2f * atlas.pixelSize;
                 float num10 = num4 - (border.x + padding.x) * 2f + (float)atlasSprite.borderLeft * num9;
                 float num11 = num3 + num * num9;
                 mHighlight.width  = Mathf.RoundToInt(num10);
                 mHighlight.height = Mathf.RoundToInt(num11);
                 bool flag = position == Position.Above;
                 if (position == Position.Auto)
                 {
                     UICamera uICamera = UICamera.FindCameraForLayer(mSelection.get_layer());
                     if (uICamera != null)
                     {
                         Vector3 val8 = uICamera.cachedCamera.WorldToViewportPoint(val2);
                         flag = (val8.y < 0.5f);
                     }
                 }
                 if (isAnimated)
                 {
                     AnimateColor(mBackground);
                     if (Time.get_timeScale() == 0f || Time.get_timeScale() >= 0.1f)
                     {
                         float bottom = num5 + num3;
                         Animate(mHighlight, flag, bottom);
                         int l = 0;
                         for (int count4 = list.Count; l < count4; l++)
                         {
                             Animate(list[l], flag, bottom);
                         }
                         AnimateScale(mBackground, flag, bottom);
                     }
                 }
                 if (flag)
                 {
                     val3.y = val4.y - border.y;
                     val4.y = val3.y + (float)mBackground.height;
                     val4.x = val3.x + (float)mBackground.width;
                     val.set_localPosition(new Vector3(val3.x, val4.y - border.y, val3.z));
                 }
                 else
                 {
                     val4.y = val3.y + border.y;
                     val3.y = val4.y - (float)mBackground.height;
                     val4.x = val3.x + (float)mBackground.width;
                 }
                 Transform val9 = mPanel.cachedTransform.get_parent();
                 if (val9 != null)
                 {
                     val3 = mPanel.cachedTransform.TransformPoint(val3);
                     val4 = mPanel.cachedTransform.TransformPoint(val4);
                     val3 = val9.InverseTransformPoint(val3);
                     val4 = val9.InverseTransformPoint(val4);
                 }
                 Vector3 val10 = mPanel.CalculateConstrainOffset(Vector2.op_Implicit(val3), Vector2.op_Implicit(val4));
                 val2   = val.get_localPosition() + val10;
                 val2.x = Mathf.Round(val2.x);
                 val2.y = Mathf.Round(val2.y);
                 val.set_localPosition(val2);
             }
         }
     }
     else
     {
         OnSelect(false);
     }
 }
Exemplo n.º 5
0
    /// <summary>
    /// Show the popup list dialog.
    /// </summary>

    public virtual void Show()
    {
        if (enabled && NGUITools.GetActive(gameObject) && mChild == null && atlas != null && isValid && items.Count > 0)
        {
            mLabelList.Clear();
            StopCoroutine("CloseIfUnselected");

            // Ensure the popup's source has the selection
            UICamera.selectedObject = (UICamera.hoveredObject ?? gameObject);
            mSelection = UICamera.selectedObject;
            source     = UICamera.selectedObject;

            if (source == null)
            {
                Debug.LogError("Popup list needs a source object...");
                return;
            }

            mOpenFrame = Time.frameCount;

            // Automatically locate the panel responsible for this object
            if (mPanel == null)
            {
                mPanel = UIPanel.Find(transform);
                if (mPanel == null)
                {
                    return;
                }
            }

            // Calculate the dimensions of the object triggering the popup list so we can position it below it
            Vector3 min;
            Vector3 max;

            // Create the root object for the list
            mChild       = new GameObject("Drop-down List");
            mChild.layer = gameObject.layer;

            if (separatePanel)
            {
                if (GetComponent <Collider>() != null)
                {
                    Rigidbody rb = mChild.AddComponent <Rigidbody>();
                    rb.isKinematic = true;
                }
                else if (GetComponent <Collider2D>() != null)
                {
                    Rigidbody2D rb = mChild.AddComponent <Rigidbody2D>();
                    rb.isKinematic = true;
                }
                mChild.AddComponent <UIPanel>().depth = 1000000;
            }
            current = this;

            Transform t = mChild.transform;
            t.parent = mPanel.cachedTransform;
            Vector3 pos;

            // Manually triggered popup list on some other game object
            if (openOn == OpenOn.Manual && mSelection != gameObject)
            {
                pos             = UICamera.lastEventPosition;
                min             = mPanel.cachedTransform.InverseTransformPoint(mPanel.anchorCamera.ScreenToWorldPoint(pos));
                max             = min;
                t.localPosition = min;
                pos             = t.position;
            }
            else
            {
                Bounds bounds = NGUIMath.CalculateRelativeWidgetBounds(mPanel.cachedTransform, transform, false, false);
                min             = bounds.min;
                max             = bounds.max;
                t.localPosition = min;
                pos             = t.position;
            }


            StartCoroutine("CloseIfUnselected");

            //t.localRotation = Quaternion.identity;
            t.eulerAngles = gameObject.transform.eulerAngles;

            t.localScale = Vector3.one;

            // Add a sprite for the background
            mBackground       = NGUITools.AddSprite(mChild, atlas, backgroundSprite, separatePanel ? 0 : NGUITools.CalculateNextDepth(mPanel.gameObject));
            mBackground.pivot = UIWidget.Pivot.TopLeft;
            mBackground.color = backgroundColor;

            // We need to know the size of the background sprite for padding purposes
            Vector4 bgPadding = mBackground.border;
            mBgBorder = bgPadding.y;
            mBackground.cachedTransform.localPosition = new Vector3(0f, bgPadding.y, 0f);

            // Add a sprite used for the selection
            mHighlight       = NGUITools.AddSprite(mChild, atlas, highlightSprite, mBackground.depth + 1);
            mHighlight.pivot = UIWidget.Pivot.TopLeft;
            mHighlight.color = highlightColor;

            UISpriteData hlsp = mHighlight.GetAtlasSprite();
            if (hlsp == null)
            {
                return;
            }

            float          hlspHeight = hlsp.borderTop;
            float          fontHeight = activeFontSize;
            float          dynScale = activeFontScale;
            float          labelHeight = fontHeight * dynScale;
            float          x = 0f, y = -padding.y;
            List <UILabel> labels = new List <UILabel>();

            // Clear the selection if it's no longer present
            if (!items.Contains(mSelectedItem))
            {
                mSelectedItem = null;
            }

            // Run through all items and create labels for each one
            for (int i = 0, imax = items.Count; i < imax; ++i)
            {
                string s = items[i];

                UILabel lbl = NGUITools.AddWidget <UILabel>(mChild, mBackground.depth + 2);
                lbl.name         = i.ToString();
                lbl.pivot        = UIWidget.Pivot.TopLeft;
                lbl.bitmapFont   = bitmapFont;
                lbl.trueTypeFont = trueTypeFont;
                lbl.fontSize     = fontSize;
                lbl.fontStyle    = fontStyle;
                lbl.text         = isLocalized ? Localization.Get(s) : s;
                lbl.color        = textColor;
                lbl.cachedTransform.localPosition = new Vector3(bgPadding.x + padding.x - lbl.pivotOffset.x, y, -1f);
                lbl.overflowMethod = UILabel.Overflow.ResizeFreely;
                lbl.alignment      = alignment;
                labels.Add(lbl);

                y -= labelHeight;
                y -= padding.y;
                x  = Mathf.Max(x, lbl.printedSize.x);

                // Add an event listener
                UIEventListener listener = UIEventListener.Get(lbl.gameObject);
                listener.onHover  = OnItemHover;
                listener.onPress  = OnItemPress;
                listener.onScroll = OnItemScroll;

                listener.parameter = s;

                // Move the selection here if this is the right label
                if (mSelectedItem == s || (i == 0 && string.IsNullOrEmpty(mSelectedItem)))
                {
                    Highlight(lbl, true);
                }

                // Add this label to the list
                mLabelList.Add(lbl);
            }

            // The triggering widget's width should be the minimum allowed width
            x = Mathf.Max(x, (max.x - min.x) - (bgPadding.x + padding.x) * 2f);

            float   cx       = x;
            Vector3 bcCenter = new Vector3(cx * 0.5f, -labelHeight * 0.5f, 0f);
            Vector3 bcSize   = new Vector3(cx, (labelHeight + padding.y), 1f);

            // Run through all labels and add colliders
            for (int i = 0, imax = labels.Count; i < imax; ++i)
            {
                UILabel lbl = labels[i];
                NGUITools.AddWidgetCollider(lbl.gameObject);
                lbl.autoResizeBoxCollider = false;
                BoxCollider bc = lbl.GetComponent <BoxCollider>();

                if (bc != null)
                {
                    bcCenter.z = bc.center.z;
                    bc.center  = bcCenter;
                    bc.size    = bcSize;
                }
                else
                {
                    BoxCollider2D b2d = lbl.GetComponent <BoxCollider2D>();
#if UNITY_4_3 || UNITY_4_5 || UNITY_4_6
                    b2d.center = bcCenter;
#else
                    b2d.offset = bcCenter;
#endif
                    b2d.size = bcSize;
                }
            }

            int lblWidth = Mathf.RoundToInt(x);
            x += (bgPadding.x + padding.x) * 2f;
            y -= bgPadding.y;

            // Scale the background sprite to envelop the entire set of items
            mBackground.width  = Mathf.RoundToInt(x);
            mBackground.height = Mathf.RoundToInt(-y + bgPadding.y);

            // Set the label width to make alignment work
            for (int i = 0, imax = labels.Count; i < imax; ++i)
            {
                UILabel lbl = labels[i];
                lbl.overflowMethod = UILabel.Overflow.ShrinkContent;
                lbl.width          = lblWidth;
            }

            // Scale the highlight sprite to envelop a single item
            float scaleFactor = 2f * atlas.pixelSize;
            float w           = x - (bgPadding.x + padding.x) * 2f + hlsp.borderLeft * scaleFactor;
            float h           = labelHeight + hlspHeight * scaleFactor;
            mHighlight.width  = Mathf.RoundToInt(w);
            mHighlight.height = Mathf.RoundToInt(h);

            bool placeAbove = (position == Position.Above);

            if (position == Position.Auto)
            {
                UICamera cam = UICamera.FindCameraForLayer(mSelection.layer);

                if (cam != null)
                {
                    Vector3 viewPos = cam.cachedCamera.WorldToViewportPoint(pos);
                    placeAbove = (viewPos.y < 0.5f);
                }
            }

            // If the list should be animated, let's animate it by expanding it
            if (isAnimated)
            {
                AnimateColor(mBackground);

                if (Time.timeScale == 0f || Time.timeScale >= 0.1f)
                {
                    float bottom = y + labelHeight;
                    Animate(mHighlight, placeAbove, bottom);
                    for (int i = 0, imax = labels.Count; i < imax; ++i)
                    {
                        Animate(labels[i], placeAbove, bottom);
                    }
                    AnimateScale(mBackground, placeAbove, bottom);
                }
            }

            // If we need to place the popup list above the item, we need to reposition everything by the size of the list
            if (placeAbove)
            {
                min.y           = max.y - bgPadding.y;
                max.y           = min.y + mBackground.height;
                max.x           = min.x + mBackground.width;
                t.localPosition = new Vector3(min.x, max.y - bgPadding.y, min.z);
            }
            else
            {
                max.y = min.y + bgPadding.y;
                min.y = max.y - mBackground.height;
                max.x = min.x + mBackground.width;
            }

            Transform pt = mPanel.cachedTransform.parent;

            if (pt != null)
            {
                min = mPanel.cachedTransform.TransformPoint(min);
                max = mPanel.cachedTransform.TransformPoint(max);
                min = pt.InverseTransformPoint(min);
                max = pt.InverseTransformPoint(max);
            }

            // Ensure that everything fits into the panel's visible range
            Vector3 offset = mPanel.hasClipping ? Vector3.zero : mPanel.CalculateConstrainOffset(min, max);
            pos             = t.localPosition + offset;
            pos.x           = Mathf.Round(pos.x);
            pos.y           = Mathf.Round(pos.y);
            t.localPosition = pos;
        }
        else
        {
            OnSelect(false);
        }
        if (mChild != null)
        {
            //if (gameObject.layer == Program.ui_back_ground_2d.layer)
            //{
            //    mChild.transform.SetParent(Program.ui_main_2d.transform, true);
            //    var trans = mChild.GetComponentsInChildren<Transform>();
            //    foreach (var item in trans)
            //    {
            //        item.gameObject.layer = Program.ui_main_2d.layer;
            //    }
            //}

            if (gameObject.layer == Program.ui_main_3d.layer)
            {
                mChild.transform.SetParent(Program.ui_main_3d.transform, true);
            }
            if (gameObject.layer == Program.ui_windows_2d.layer)
            {
                mChild.transform.SetParent(Program.ui_windows_2d.transform, true);
            }
            if (gameObject.layer == Program.ui_main_2d.layer)
            {
                mChild.transform.SetParent(Program.ui_main_2d.transform, true);
            }
        }
    }
Exemplo n.º 6
0
    /// <summary>
    /// Display the drop-down list when the game object gets clicked on.
    /// </summary>

    void OnClick()
    {
        if (enabled && NGUITools.GetActive(gameObject) && mChild == null && atlas != null && font != null && items.Count > 0)
        {
            mLabelList.Clear();

            // Disable the navigation script
            handleEvents = true;

            // Automatically locate the panel responsible for this object
            if (mPanel == null)
            {
                mPanel = UIPanel.Find(transform, true);
            }

            // Calculate the dimensions of the object triggering the popup list so we can position it below it
            Transform myTrans = transform;
            Bounds    bounds  = NGUIMath.CalculateRelativeWidgetBounds(myTrans.parent, myTrans);

            // Create the root object for the list
            mChild       = new GameObject("Drop-down List");
            mChild.layer = gameObject.layer;

            Transform t = mChild.transform;
            t.parent        = myTrans.parent;
            t.localPosition = bounds.min;
            t.localRotation = Quaternion.identity;
            t.localScale    = Vector3.one;

            // Add a sprite for the background
            mBackground       = NGUITools.AddSprite(mChild, atlas, backgroundSprite);
            mBackground.pivot = UIWidget.Pivot.TopLeft;
            mBackground.depth = NGUITools.CalculateNextDepth(mPanel.gameObject);
            mBackground.color = backgroundColor;

            // We need to know the size of the background sprite for padding purposes
            Vector4 bgPadding = mBackground.border;
            mBgBorder = bgPadding.y;

            mBackground.cachedTransform.localPosition = new Vector3(0f, bgPadding.y, 0f);

            // Add a sprite used for the selection
            mHighlight       = NGUITools.AddSprite(mChild, atlas, highlightSprite);
            mHighlight.pivot = UIWidget.Pivot.TopLeft;
            mHighlight.color = highlightColor;

            UIAtlas.Sprite hlsp = mHighlight.GetAtlasSprite();
            if (hlsp == null)
            {
                return;
            }

            float          hlspHeight = hlsp.inner.yMin - hlsp.outer.yMin;
            float          fontScale = font.size * font.pixelSize * textScale;
            float          x = 0f, y = -padding.y;
            List <UILabel> labels = new List <UILabel>();

            // Run through all items and create labels for each one
            for (int i = 0, imax = items.Count; i < imax; ++i)
            {
                string s = items[i];

                UILabel lbl = NGUITools.AddWidget <UILabel>(mChild);
                lbl.pivot = UIWidget.Pivot.TopLeft;
                lbl.font  = font;
                lbl.text  = (isLocalized && Localization.instance != null) ? Localization.instance.Get(s) : s;
                lbl.color = textColor;
                lbl.cachedTransform.localPosition = new Vector3(bgPadding.x + padding.x, y, -1f);
                lbl.MakePixelPerfect();

                if (textScale != 1f)
                {
                    Vector3 scale = lbl.cachedTransform.localScale;
                    lbl.cachedTransform.localScale = scale * textScale;
                }
                labels.Add(lbl);

                y -= fontScale;
                y -= padding.y;
                x  = Mathf.Max(x, lbl.relativeSize.x * fontScale);

                // Add an event listener
                UIEventListener listener = UIEventListener.Get(lbl.gameObject);
                listener.onHover   = OnItemHover;
                listener.onPress   = OnItemPress;
                listener.parameter = s;

                // Move the selection here if this is the right label
                if (mSelectedItem == s)
                {
                    Highlight(lbl, true);
                }

                // Add this label to the list
                mLabelList.Add(lbl);
            }

            // The triggering widget's width should be the minimum allowed width
            x = Mathf.Max(x, bounds.size.x - (bgPadding.x + padding.x) * 2f);

            Vector3 bcCenter = new Vector3((x * 0.5f) / fontScale, -0.5f, 0f);
            Vector3 bcSize   = new Vector3(x / fontScale, (fontScale + padding.y) / fontScale, 1f);

            // Run through all labels and add colliders
            for (int i = 0, imax = labels.Count; i < imax; ++i)
            {
                UILabel     lbl = labels[i];
                BoxCollider bc  = NGUITools.AddWidgetCollider(lbl.gameObject);
                bcCenter.z = bc.center.z;
                bc.center  = bcCenter;
                bc.size    = bcSize;
            }

            x += (bgPadding.x + padding.x) * 2f;
            y -= bgPadding.y;

            // Scale the background sprite to envelop the entire set of items
            mBackground.cachedTransform.localScale = new Vector3(x, -y + bgPadding.y, 1f);

            // Scale the highlight sprite to envelop a single item
            float scaleFactor = 2f * atlas.pixelSize;
            mHighlight.cachedTransform.localScale = new Vector3(
                x - (bgPadding.x + padding.x) * 2f + (hlsp.inner.xMin - hlsp.outer.xMin) * scaleFactor,
                fontScale + hlspHeight * scaleFactor, 1f);

            bool placeAbove = (position == Position.Above);

            if (position == Position.Auto)
            {
                UICamera cam = UICamera.FindCameraForLayer(gameObject.layer);

                if (cam != null)
                {
                    Vector3 viewPos = cam.cachedCamera.WorldToViewportPoint(myTrans.position);
                    placeAbove = (viewPos.y < 0.5f);
                }
            }

            // If the list should be animated, let's animate it by expanding it
            if (isAnimated)
            {
                float bottom = y + fontScale;
                Animate(mHighlight, placeAbove, bottom);
                for (int i = 0, imax = labels.Count; i < imax; ++i)
                {
                    Animate(labels[i], placeAbove, bottom);
                }
                AnimateColor(mBackground);
                AnimateScale(mBackground, placeAbove, bottom);
            }

            // If we need to place the popup list above the item, we need to reposition everything by the size of the list
            if (placeAbove)
            {
                t.localPosition = new Vector3(bounds.min.x, bounds.max.y - y - bgPadding.y, bounds.min.z);
            }
        }
        else
        {
            OnSelect(false);
        }
    }
Exemplo n.º 7
0
    /// <summary>
    /// Display the drop-down list when the game object gets clicked on.
    /// </summary>

    void OnClick()
    {
        if (enabled && NGUITools.GetActive(gameObject) && mChild == null && atlas != null && isValid && items.Count > 0)
        {
            mLabelList.Clear();

            // Automatically locate the panel responsible for this object
            if (mPanel == null)
            {
                mPanel = UIPanel.Find(transform);
                if (mPanel == null)
                {
                    return;
                }
            }

            // Disable the navigation script
            handleEvents = true;

            // Calculate the dimensions of the object triggering the popup list so we can position it below it
            Transform myTrans = transform;
            Bounds    bounds  = NGUIMath.CalculateRelativeWidgetBounds(myTrans.parent, myTrans);

            // Create the root object for the list
            mChild       = new GameObject("Drop-down List");
            mChild.layer = gameObject.layer;

            Transform t = mChild.transform;
            t.parent        = myTrans.parent;
            t.localPosition = bounds.min;
            t.localRotation = Quaternion.identity;
            t.localScale    = Vector3.one;

            // Add a sprite for the background
            mBackground       = NGUITools.AddSprite(mChild, atlas, backgroundSprite);
            mBackground.pivot = UIWidget.Pivot.TopLeft;
            mBackground.depth = NGUITools.CalculateNextDepth(mPanel.gameObject);
            mBackground.color = backgroundColor;

            // We need to know the size of the background sprite for padding purposes
            Vector4 bgPadding = mBackground.border;
            mBgBorder = bgPadding.y;
            mBackground.cachedTransform.localPosition = new Vector3(0f, bgPadding.y, 0f);

            // Add a sprite used for the selection
            mHighlight       = NGUITools.AddSprite(mChild, atlas, highlightSprite);
            mHighlight.pivot = UIWidget.Pivot.TopLeft;
            mHighlight.color = highlightColor;

            UISpriteData hlsp = mHighlight.GetAtlasSprite();
            if (hlsp == null)
            {
                return;
            }

            float          hlspHeight = hlsp.borderTop;
            float          fontHeight = activeFontSize;
            float          dynScale = activeFontScale;
            float          labelHeight = fontHeight * dynScale;
            float          x = 0f, y = -padding.y;
            int            labelFontSize = (bitmapFont != null) ? bitmapFont.defaultSize : fontSize;
            List <UILabel> labels        = new List <UILabel>();

            // Run through all items and create labels for each one
            for (int i = 0, imax = items.Count; i < imax; ++i)
            {
                string s = items[i];

                UILabel lbl = NGUITools.AddWidget <UILabel>(mChild);
                lbl.name         = i.ToString();
                lbl.pivot        = UIWidget.Pivot.TopLeft;
                lbl.bitmapFont   = bitmapFont;
                lbl.trueTypeFont = trueTypeFont;
                lbl.fontSize     = labelFontSize;
                lbl.fontStyle    = fontStyle;
                lbl.text         = isLocalized ? Localization.Get(s) : s;
                lbl.color        = textColor;
                lbl.cachedTransform.localPosition = new Vector3(bgPadding.x + padding.x, y, -1f);
                lbl.overflowMethod = UILabel.Overflow.ResizeFreely;
                lbl.alignment      = alignment;
                lbl.MakePixelPerfect();
                if (dynScale != 1f)
                {
                    lbl.cachedTransform.localScale = Vector3.one * dynScale;
                }
                labels.Add(lbl);

                y -= labelHeight;
                y -= padding.y;
                x  = Mathf.Max(x, lbl.printedSize.x);

                // Add an event listener
                UIEventListener listener = UIEventListener.Get(lbl.gameObject);
                listener.onHover   = OnItemHover;
                listener.onPress   = OnItemPress;
                listener.onClick   = OnItemClick;
                listener.parameter = s;

                // Move the selection here if this is the right label
                if (mSelectedItem == s || (i == 0 && string.IsNullOrEmpty(mSelectedItem)))
                {
                    Highlight(lbl, true);
                }

                // Add this label to the list
                mLabelList.Add(lbl);
            }

            // The triggering widget's width should be the minimum allowed width
            x = Mathf.Max(x, bounds.size.x * dynScale - (bgPadding.x + padding.x) * 2f);

            float   cx       = x / dynScale;
            Vector3 bcCenter = new Vector3(cx * 0.5f, -fontHeight * 0.5f, 0f);
            Vector3 bcSize   = new Vector3(cx, (labelHeight + padding.y) / dynScale, 1f);

            // Run through all labels and add colliders
            for (int i = 0, imax = labels.Count; i < imax; ++i)
            {
                UILabel lbl = labels[i];
                NGUITools.AddWidgetCollider(lbl.gameObject);
                lbl.autoResizeBoxCollider = false;
                BoxCollider bc = lbl.GetComponent <BoxCollider>();

                if (bc != null)
                {
                    bcCenter.z = bc.center.z;
                    bc.center  = bcCenter;
                    bc.size    = bcSize;
                }
                else
                {
                    BoxCollider2D b2d = lbl.GetComponent <BoxCollider2D>();
                    b2d.center = bcCenter;
                    b2d.size   = bcSize;
                }
            }

            int lblWidth = Mathf.RoundToInt(x);
            x += (bgPadding.x + padding.x) * 2f;
            y -= bgPadding.y;

            // Scale the background sprite to envelop the entire set of items
            mBackground.width  = Mathf.RoundToInt(x);
            mBackground.height = Mathf.RoundToInt(-y + bgPadding.y);

            // Set the label width to make alignment work
            for (int i = 0, imax = labels.Count; i < imax; ++i)
            {
                UILabel lbl = labels[i];
                lbl.overflowMethod = UILabel.Overflow.ShrinkContent;
                lbl.width          = lblWidth;
            }

            // Scale the highlight sprite to envelop a single item
            float scaleFactor = 2f * atlas.pixelSize;
            float w           = x - (bgPadding.x + padding.x) * 2f + hlsp.borderLeft * scaleFactor;
            float h           = labelHeight + hlspHeight * scaleFactor;
            mHighlight.width  = Mathf.RoundToInt(w);
            mHighlight.height = Mathf.RoundToInt(h);

            bool placeAbove = (position == Position.Above);

            if (position == Position.Auto)
            {
                UICamera cam = UICamera.FindCameraForLayer(gameObject.layer);

                if (cam != null)
                {
                    Vector3 viewPos = cam.cachedCamera.WorldToViewportPoint(myTrans.position);
                    placeAbove = (viewPos.y < 0.5f);
                }
            }

            // If the list should be animated, let's animate it by expanding it
            if (isAnimated)
            {
                float bottom = y + labelHeight;
                Animate(mHighlight, placeAbove, bottom);
                for (int i = 0, imax = labels.Count; i < imax; ++i)
                {
                    Animate(labels[i], placeAbove, bottom);
                }
                AnimateColor(mBackground);
                AnimateScale(mBackground, placeAbove, bottom);
            }

            // If we need to place the popup list above the item, we need to reposition everything by the size of the list
            if (placeAbove)
            {
                t.localPosition = new Vector3(bounds.min.x, bounds.max.y - y - bgPadding.y, bounds.min.z);
            }
        }
        else
        {
            OnSelect(false);
        }
    }
Exemplo n.º 8
0
 public void Show()
 {
     if (base.enabled && NGUITools.GetActive(base.gameObject) && mChild == null && atlas != null && isValid && items.Count > 0)
     {
         mLabelList.Clear();
         if (mPanel == null)
         {
             mPanel = UIPanel.Find(base.transform);
             if (mPanel == null)
             {
                 return;
             }
         }
         handleEvents = true;
         Transform transform = base.transform;
         Bounds    bounds    = NGUIMath.CalculateRelativeWidgetBounds(transform.parent, transform);
         mChild       = new GameObject("Drop-down List");
         mChild.layer = base.gameObject.layer;
         Transform transform2 = mChild.transform;
         transform2.parent        = transform.parent;
         transform2.localPosition = bounds.min;
         transform2.localRotation = Quaternion.identity;
         transform2.localScale    = Vector3.one;
         mBackground       = NGUITools.AddSprite(mChild, atlas, backgroundSprite);
         mBackground.pivot = UIWidget.Pivot.TopLeft;
         mBackground.depth = NGUITools.CalculateNextDepth(mPanel.gameObject);
         mBackground.color = backgroundColor;
         Vector4 border = mBackground.border;
         mBgBorder = border.y;
         mBackground.cachedTransform.localPosition = new Vector3(0f, border.y, 0f);
         mHighlight       = NGUITools.AddSprite(mChild, atlas, highlightSprite);
         mHighlight.pivot = UIWidget.Pivot.TopLeft;
         mHighlight.color = highlightColor;
         UISpriteData atlasSprite = mHighlight.GetAtlasSprite();
         if (atlasSprite != null)
         {
             float          num             = (float)atlasSprite.borderTop;
             float          num2            = (float)activeFontSize;
             float          activeFontScale = this.activeFontScale;
             float          num3            = num2 * activeFontScale;
             float          num4            = 0f;
             float          num5            = 0f - padding.y;
             List <UILabel> list            = new List <UILabel>();
             if (!items.Contains(mSelectedItem))
             {
                 mSelectedItem = null;
             }
             int i = 0;
             for (int count = items.Count; i < count; i++)
             {
                 string  text    = items[i];
                 UILabel uILabel = NGUITools.AddWidget <UILabel>(mChild);
                 uILabel.name         = i.ToString();
                 uILabel.pivot        = UIWidget.Pivot.TopLeft;
                 uILabel.bitmapFont   = bitmapFont;
                 uILabel.trueTypeFont = trueTypeFont;
                 uILabel.fontSize     = fontSize;
                 uILabel.fontStyle    = fontStyle;
                 uILabel.text         = ((!isLocalized) ? text : Localization.Get(text));
                 uILabel.color        = textColor;
                 Transform cachedTransform = uILabel.cachedTransform;
                 float     num6            = border.x + padding.x;
                 Vector2   pivotOffset     = uILabel.pivotOffset;
                 cachedTransform.localPosition = new Vector3(num6 - pivotOffset.x, num5, -1f);
                 uILabel.overflowMethod        = UILabel.Overflow.ResizeFreely;
                 uILabel.alignment             = alignment;
                 list.Add(uILabel);
                 num5 -= num3;
                 num5 -= padding.y;
                 float   a           = num4;
                 Vector2 printedSize = uILabel.printedSize;
                 num4 = Mathf.Max(a, printedSize.x);
                 UIEventListener uIEventListener = UIEventListener.Get(uILabel.gameObject);
                 uIEventListener.onHover   = OnItemHover;
                 uIEventListener.onPress   = OnItemPress;
                 uIEventListener.onClick   = OnItemClick;
                 uIEventListener.parameter = text;
                 if (mSelectedItem == text || (i == 0 && string.IsNullOrEmpty(mSelectedItem)))
                 {
                     Highlight(uILabel, instant: true);
                 }
                 mLabelList.Add(uILabel);
             }
             float   a2   = num4;
             Vector3 size = bounds.size;
             num4 = Mathf.Max(a2, size.x * activeFontScale - (border.x + padding.x) * 2f);
             float   num7    = num4;
             Vector3 vector  = new Vector3(num7 * 0.5f, (0f - num2) * 0.5f, 0f);
             Vector3 vector2 = new Vector3(num7, num3 + padding.y, 1f);
             int     j       = 0;
             for (int count2 = list.Count; j < count2; j++)
             {
                 UILabel uILabel2 = list[j];
                 NGUITools.AddWidgetCollider(uILabel2.gameObject);
                 uILabel2.autoResizeBoxCollider = false;
                 BoxCollider component = uILabel2.GetComponent <BoxCollider>();
                 if (component != null)
                 {
                     Vector3 center = component.center;
                     vector.z         = center.z;
                     component.center = vector;
                     component.size   = vector2;
                 }
                 else
                 {
                     BoxCollider2D component2 = uILabel2.GetComponent <BoxCollider2D>();
                     component2.offset = vector;
                     component2.size   = vector2;
                 }
             }
             int width = Mathf.RoundToInt(num4);
             num4 += (border.x + padding.x) * 2f;
             num5 -= border.y;
             mBackground.width  = Mathf.RoundToInt(num4);
             mBackground.height = Mathf.RoundToInt(0f - num5 + border.y);
             int k = 0;
             for (int count3 = list.Count; k < count3; k++)
             {
                 UILabel uILabel3 = list[k];
                 uILabel3.overflowMethod = UILabel.Overflow.ShrinkContent;
                 uILabel3.width          = width;
             }
             float num8 = 2f * atlas.pixelSize;
             float f    = num4 - (border.x + padding.x) * 2f + (float)atlasSprite.borderLeft * num8;
             float f2   = num3 + num * num8;
             mHighlight.width  = Mathf.RoundToInt(f);
             mHighlight.height = Mathf.RoundToInt(f2);
             bool flag = position == Position.Above;
             if (position == Position.Auto)
             {
                 UICamera uICamera = UICamera.FindCameraForLayer(base.gameObject.layer);
                 if (uICamera != null)
                 {
                     Vector3 vector3 = uICamera.cachedCamera.WorldToViewportPoint(transform.position);
                     flag = (vector3.y < 0.5f);
                 }
             }
             if (isAnimated)
             {
                 float bottom = num5 + num3;
                 Animate(mHighlight, flag, bottom);
                 int l = 0;
                 for (int count4 = list.Count; l < count4; l++)
                 {
                     Animate(list[l], flag, bottom);
                 }
                 AnimateColor(mBackground);
                 AnimateScale(mBackground, flag, bottom);
             }
             if (flag)
             {
                 Transform transform3 = transform2;
                 Vector3   min        = bounds.min;
                 float     x          = min.x;
                 Vector3   max        = bounds.max;
                 float     y          = max.y - num5 - border.y;
                 Vector3   min2       = bounds.min;
                 transform3.localPosition = new Vector3(x, y, min2.z);
             }
         }
     }
     else
     {
         OnSelect(isSelected: false);
     }
 }