private Rect DrawAlignmentButton(ScoreMessage msg, Rect buttonPosition, NGAlignment.ScreenAlign align, string text, Texture icon) { Vector2 clampedPosition = new Vector2(Mathf.Clamp(designed.Position.x, -100F, 100F), Mathf.Clamp(designed.Position.y, -100F, 100F)); if (!designed.SupportsScreenAlign) { align = NGAlignment.ScreenAlign.MiddleCenter; } if (msg != null) { clampedPosition = Vector2.zero; } Vector2 posForButton = (msg == null) ? NGAlignment.GetScreenBasedReferencePosition(align, clampedPosition, isSceneView) : msg.OriginalPosition; Vector2 offset = NGAlignment.GetAlignBasedOffset(align, buttonPosition); buttonPosition.x = Mathf.Clamp(posForButton.x + offset.x, 0F, EditorSceneViewGUI.GetScreenWidth(isSceneView) - buttonPosition.width); buttonPosition.y = Mathf.Clamp(posForButton.y + offset.y, 0F, EditorSceneViewGUI.GetScreenHeight(isSceneView) - buttonPosition.height); Color col = GUI.color; if (IsDraggingPosition) { col.a = 0.4F; } if (clampedPosition != designed.Position) { col.a = 0.2F; } if (msg != null) { col.a = 1F; } GUI.color = col; if (msg == null) { if (GUI.Button(buttonPosition, text)) { designed.ScreenAlign = align; } } else { // otherwise: reset position if (GUI.Button(buttonPosition, icon)) { designed.Position = Vector2.zero; changed = true; } } return buttonPosition; }
/// <summary> /// Returns an offset to render an item anchored via innerAnchor. /// </summary> /// <remarks> /// For example, if an item should be right-aligned, this will return /// <c>-pos.width</c> so that the right border of the item is located /// at the position of the item (in other words, it's anchored on its /// right edge). /// </remarks> /// <param name="innerAnchor">the anchor / alignment</param> /// <param name="pos">the full size position of the item to be rendered</param> /// <returns> /// an offset to be applied to pos to have the item rendered according to innerAnchor /// </returns> public static Vector2 GetAlignBasedOffset(NGAlignment.ScreenAlign innerAnchor, Rect pos) { float x = 0; // determine x-position switch (Horizontal(innerAnchor)) { case HorizontalAlign.Left: x = 0; break; case HorizontalAlign.Center: x = -pos.width * 0.5F; break; case HorizontalAlign.Right: x = -pos.width; break; } // determine y-position and return the complete position vector switch (Vertical(innerAnchor)) { case VerticalAlign.Top: return new Vector2(x, 0); case VerticalAlign.Middle: return new Vector2(x, -pos.height * 0.5F); case VerticalAlign.Bottom: return new Vector2(x, -pos.height); } // fallback - should never be called return Vector2.zero; }
/// <summary> /// Converts pos from <em>relative to screen alignment</em> /// to a screen position that is <em>relative to screen alignment</em> using <c>align</c>. /// This also converts pos to pixels (in case we have a high density display). /// </summary> /// <param name="align">the screen alignment</param> /// <param name="pos">the position relative to screen alignment</param> /// <param name="isInSceneView">needed to work around a Unity bug</param> /// <returns>the screen position</returns> public static Vector2 GetScreenBasedReferencePosition(NGAlignment.ScreenAlign align, Vector2 pos, bool isInSceneView) { float x = 0; // determine x-position switch (Horizontal(align)) { case HorizontalAlign.Left: x = NGUtil.Scale(pos.x); break; case HorizontalAlign.Center: x = EditorSceneViewGUI.GetScreenWidth(isInSceneView) * 0.5F + NGUtil.Scale(pos.x); break; case HorizontalAlign.Right: x = EditorSceneViewGUI.GetScreenWidth(isInSceneView) - NGUtil.Scale(pos.x); break; } // determine y-position and return the complete position vector switch (Vertical(align)) { case VerticalAlign.Top: return new Vector2(x, NGUtil.Scale(pos.y)); case VerticalAlign.Middle: return new Vector2(x, EditorSceneViewGUI.GetScreenHeight(isInSceneView) * 0.5F + NGUtil.Scale(pos.y)); case VerticalAlign.Bottom: return new Vector2(x, EditorSceneViewGUI.GetScreenHeight(isInSceneView) - NGUtil.Scale(pos.y)); } // fallback - should never be called return Vector2.zero; }
/// <summary> /// Converts an alignment to another enum (e.g. Unity's built in types, NGUI or EZ GUI). /// </summary> /// <param name="screenAlign">alignment in our encoding</param> /// <param name="alignmentType">target alignment</param> /// <returns></returns> public static int ConvertAlignment(NGAlignment.ScreenAlign screenAlign, AlignmentType alignmentType) { switch (alignmentType) { case AlignmentType.TextAnchor: switch (screenAlign) { case NGAlignment.ScreenAlign.TopLeft: return (int)TextAnchor.UpperLeft; case NGAlignment.ScreenAlign.MiddleLeft: return (int)TextAnchor.MiddleLeft; case NGAlignment.ScreenAlign.BottomLeft: return (int)TextAnchor.LowerLeft; case NGAlignment.ScreenAlign.TopCenter: return (int)TextAnchor.UpperCenter; case NGAlignment.ScreenAlign.MiddleCenter: return (int)TextAnchor.MiddleCenter; case NGAlignment.ScreenAlign.BottomCenter: return (int)TextAnchor.LowerCenter; case NGAlignment.ScreenAlign.TopRight: return (int)TextAnchor.UpperRight; case NGAlignment.ScreenAlign.MiddleRight: return (int)TextAnchor.MiddleRight; case NGAlignment.ScreenAlign.BottomRight: return (int)TextAnchor.LowerRight; } break; case AlignmentType.TextAlignment: switch (Horizontal(screenAlign)) { case NGAlignment.HorizontalAlign.Left: return (int)TextAlignment.Left; case NGAlignment.HorizontalAlign.Center: return (int)TextAlignment.Center; case NGAlignment.HorizontalAlign.Right: return (int)TextAlignment.Right; } break; case AlignmentType.NGUI_Pivot: switch (screenAlign) { //TopLeft 0 //Top 1 //TopRight 2 //Left 3 //Center 4 //Right 5 //BottomLeft 6 //Bottom 7 //BottomRight 8 case NGAlignment.ScreenAlign.TopLeft: return 0; //(int) UIWidget.Pivot.TopLeft; case NGAlignment.ScreenAlign.MiddleLeft: return 3; //(int) UIWidget.Pivot.Left; case NGAlignment.ScreenAlign.BottomLeft: return 6; //(int) UIWidget.Pivot.BottomLeft; case NGAlignment.ScreenAlign.TopCenter: return 1; //(int) UIWidget.Pivot.Top; case NGAlignment.ScreenAlign.MiddleCenter: return 4; //(int) UIWidget.Pivot.Center; case NGAlignment.ScreenAlign.BottomCenter: return 7; //(int) UIWidget.Pivot.Bottom; case NGAlignment.ScreenAlign.TopRight: return 2; //(int) UIWidget.Pivot.TopRight; case NGAlignment.ScreenAlign.MiddleRight: return 5; //(int) UIWidget.Pivot.Right; case NGAlignment.ScreenAlign.BottomRight: return 8; //(int) UIWidget.Pivot.BottomRight; } break; case AlignmentType.EZGUI_Alignment: //Left, //Center, //Right switch (Horizontal(screenAlign)) { case NGAlignment.HorizontalAlign.Left: return 0; case NGAlignment.HorizontalAlign.Center: return 1; case NGAlignment.HorizontalAlign.Right: return 2; } break; case AlignmentType.EZGUI_Anchor: switch (screenAlign) { //Upper_Left, //Upper_Center, //Upper_Right, //Middle_Left, //Middle_Center, //Middle_Right, //Lower_Left, //Lower_Center, //Lower_Right case NGAlignment.ScreenAlign.TopLeft: return 0; case NGAlignment.ScreenAlign.TopCenter: return 1; case NGAlignment.ScreenAlign.TopRight: return 2; case NGAlignment.ScreenAlign.MiddleLeft: return 3; case NGAlignment.ScreenAlign.MiddleCenter: return 4; case NGAlignment.ScreenAlign.MiddleRight: return 5; case NGAlignment.ScreenAlign.BottomLeft: return 6; case NGAlignment.ScreenAlign.BottomCenter: return 7; case NGAlignment.ScreenAlign.BottomRight: return 8; } break; } // Fallback, should never be called return (int)TextAnchor.UpperLeft; }