UIRectEditor.DrawAnchor C# (CSharp) Method

DrawAnchor() protected method

Helper function that draws the suffix after the relative fields.
protected DrawAnchor ( int index, bool targetSelection ) : void
index int
targetSelection bool
return void
	protected void DrawAnchor (int index, bool targetSelection)
	{
		if (targetSelection) GUILayout.Space(3f);

		NGUIEditorTools.SetLabelWidth(16f);
		GUILayout.BeginHorizontal();
		GUILayout.Label(PrefixName[index], GUILayout.Width(56f));

		UIRect myRect = serializedObject.targetObject as UIRect;
		string name = FieldName[index];

		SerializedProperty tar = serializedObject.FindProperty(name + ".target");
		SerializedProperty rel = serializedObject.FindProperty(name + ".relative");
		SerializedProperty abs = serializedObject.FindProperty(name + ".absolute");

		if (targetSelection)
		{
			Object before = tar.objectReferenceValue;
			NGUIEditorTools.DrawProperty("", tar, false);
			Object after = tar.objectReferenceValue;

			if (after != null || tar.hasMultipleDifferentValues)
			{
				if (before != after && after != null)
					UpdateAnchor(index, true);
			}

			GUILayout.EndHorizontal();
			GUILayout.BeginHorizontal();
			GUILayout.Space(64f);
		}

		UIRect targetRect = GetRect(tar);
		Camera targetCam = GetCamera(tar);
		float relative = rel.floatValue;
		bool isCommon = (targetRect == null && targetCam == null) || IsCommon(relative);
		int previousOrigin = 1;

		if (targetRect != null || targetCam != null)
		{
			if (mCustom[index] || !isCommon) previousOrigin = 3;
			else if (relative == 0f) previousOrigin = 0;
			else if (relative == 1f) previousOrigin = 2;
		}

		// Draw the origin selection list
		EditorGUI.BeginDisabledGroup(targetRect == null && targetCam == null);
		int newOrigin = IsHorizontal[index] ?
			EditorGUILayout.Popup(previousOrigin, HorizontalList, GUILayout.MinWidth(110f)) :
			EditorGUILayout.Popup(previousOrigin, VerticalList, GUILayout.MinWidth(110f));
		EditorGUI.EndDisabledGroup();

		// "Set to Current" choice
		if (newOrigin == 4)
		{
			newOrigin = 3;

			Vector3[] sides = targetRect.GetSides(myRect.cachedTransform);

			float f0, f1;

			if (IsHorizontal[index])
			{
				f0 = sides[0].x;
				f1 = sides[2].x;
			}
			else
			{
				f0 = sides[3].y;
				f1 = sides[1].y;
			}

			// Final position after both relative and absolute values are taken into consideration
			float final = Mathf.Floor(0.5f + Mathf.Lerp(0f, f1 - f0, rel.floatValue) + abs.intValue);

			rel.floatValue = final / (f1 - f0);
			abs.intValue = 0;

			serializedObject.ApplyModifiedProperties();
			serializedObject.Update();
		}

		mCustom[index] = (newOrigin == 3);

		// If the origin changes
		if (newOrigin != 3 && previousOrigin != newOrigin)
		{
			// Desired relative value
			if (newOrigin == 0) relative = 0f;
			else if (newOrigin == 2) relative = 1f;
			else relative = 0.5f;

			Vector3[] sides = (targetRect != null) ?
				targetRect.GetSides(myRect.cachedTransform) :
				targetCam.GetSides(myRect.cachedTransform);

			// Calculate the current position based from the bottom-left
			float f0, f1;

			if (IsHorizontal[index])
			{
				f0 = sides[0].x;
				f1 = sides[2].x;
			}
			else
			{
				f0 = sides[3].y;
				f1 = sides[1].y;
			}

			// Final position after both relative and absolute values are taken into consideration
			float final = Mathf.Floor(0.5f + Mathf.Lerp(f0, f1, rel.floatValue) + abs.intValue);

			rel.floatValue = relative;
			abs.intValue = Mathf.FloorToInt(final + 0.5f - Mathf.Lerp(f0, f1, relative));

			serializedObject.ApplyModifiedProperties();
			serializedObject.Update();
		}

		if (!mCustom[index])
		{
			// Draw the absolute value
			NGUIEditorTools.SetLabelWidth(16f);
			NGUIEditorTools.DrawProperty("+", abs, true, GUILayout.MinWidth(10f));
		}
		else
		{
			// Draw the relative value
			NGUIEditorTools.SetLabelWidth(16f);
			NGUIEditorTools.DrawProperty(" ", rel, true, GUILayout.MinWidth(10f));
			GUILayout.EndHorizontal();
			GUILayout.BeginHorizontal();
			GUILayout.Space(64f);

			relative = rel.floatValue;
			bool isOutside01 = relative < 0f || relative > 1f;

			// Horizontal slider for relative values, for convenience
			EditorGUI.BeginDisabledGroup(isOutside01);
			{
				float val = GUILayout.HorizontalSlider(relative, 0f, 1f, GUILayout.MinWidth(110f));

				if (!isOutside01 && val != relative)
				{
					Vector3[] sides = (targetRect != null) ?
						targetRect.GetSides(myRect.cachedTransform) :
						targetCam.GetSides(myRect.cachedTransform);

					// Calculate the current position based from the bottom-left
					float f0, f1;

					if (IsHorizontal[index])
					{
						f0 = sides[0].x;
						f1 = sides[2].x;
					}
					else
					{
						f0 = sides[3].y;
						f1 = sides[1].y;
					}

					float size = (f1 - f0);
					int intVal = Mathf.FloorToInt(val * size + 0.5f);
					//intVal = ((intVal >> 1) << 1);
					rel.floatValue = (size > 0f) ? intVal / size : 0.5f;
				}
			}
			EditorGUI.EndDisabledGroup();

			// Draw the absolute value
			NGUIEditorTools.DrawProperty("+", abs, true, GUILayout.MinWidth(10f));
		}
		
		GUILayout.EndHorizontal();
		NGUIEditorTools.SetLabelWidth(62f);
	}