private void CheckWidgetOffset(OGWidget w)
    {
        if (IsInfiniteY())
        {
            // Top
            if (position.y < bounds.y && w.transform.position.y + w.transform.localScale.y + w.scrollOffset.y <= this.transform.position.y)
            {
                w.scrollOffset.y += drawRct.height + padding.y * 4;

                // Bottom
            }
            else if (position.y > 0 && w.transform.position.y + w.scrollOffset.y >= this.transform.position.y + size.y)
            {
                w.scrollOffset.y -= drawRct.height + padding.y * 4;
            }
        }

        if (IsInfiniteX())
        {
            // Right
            if (position.x < bounds.x && w.transform.position.x + w.transform.localScale.x + w.scrollOffset.x <= this.transform.position.x)
            {
                w.scrollOffset.x += drawRct.width + padding.x * 4;

                // Left
            }
            else if (position.x > 0 && w.transform.position.x + w.scrollOffset.x >= this.transform.position.x + size.x)
            {
                w.scrollOffset.x -= drawRct.width + padding.x * 4;
            }
        }
    }
Example #2
0
    public static OGWidgetType GetWidgetEnum(OGWidget w)
    {
        if (widgetEnums.Count < 1)
        {
            widgetEnums.Add(typeof(OGButton), OGWidgetType.Button);
            widgetEnums.Add(typeof(OGDropDown), OGWidgetType.DropDown);
            widgetEnums.Add(typeof(OGDropContainer), OGWidgetType.DropContainer);
            widgetEnums.Add(typeof(OGLabel), OGWidgetType.Label);
            widgetEnums.Add(typeof(OGListItem), OGWidgetType.ListItem);
            widgetEnums.Add(typeof(OGPopUp), OGWidgetType.PopUp);
            widgetEnums.Add(typeof(OGProgressBar), OGWidgetType.ProgressBar);
            widgetEnums.Add(typeof(OGScrollView), OGWidgetType.ScrollView);
            widgetEnums.Add(typeof(OGSlider), OGWidgetType.Slider);
            widgetEnums.Add(typeof(OGSlicedSprite), OGWidgetType.SlicedSprite);
            widgetEnums.Add(typeof(OGSprite), OGWidgetType.Sprite);
            widgetEnums.Add(typeof(OGTabs), OGWidgetType.Tabs);
            widgetEnums.Add(typeof(OGTextField), OGWidgetType.TextField);
            widgetEnums.Add(typeof(OGTickBox), OGWidgetType.TickBox);
        }

        if (widgetEnums.ContainsKey(w.GetType()))
        {
            return(widgetEnums [w.GetType()]);
        }
        else
        {
            return(OGWidgetType.NONE);
        }
    }
Example #3
0
	public OGLine ( OGWidget start, Vector3 startDir, OGWidget end, Vector3 endDir, int segments ) {
		this.start = start;
		this.startDir = startDir;
		this.end = end;
		this.endDir = endDir;
		this.segments = segments;
	}
Example #4
0
	////////////////////
	// Update
	////////////////////
	override public void UpdateWidget () {
		// Persistent vars
		isSelectable = true;

		if ( CheckMouseOver () ) {
			currentStyle = styles.hover;
		} else {
			currentStyle = styles.basic;
		}
			
		// Mouse
		if ( Input.GetMouseButtonUp ( 0 ) || GetTouch () == TouchPhase.Ended ) {
			if ( OGRoot.GetInstance().downWidget && OGRoot.GetInstance().downWidget.isDraggable && CheckMouseOver() ) {
				if ( containedObject ) {
					Destroy ( containedObject.gameObject ); 
				}
				
				containedObject = (OGWidget)Instantiate ( OGRoot.GetInstance().downWidget );
				containedObject.gameObject.name = containedObject.gameObject.name.Replace ( "(Clone)", "" );
				containedObject.transform.parent = this.transform;
				containedObject.transform.localScale = new Vector3 ( containedScale, containedScale, 1 );
				containedObject.transform.localPosition = new Vector3 ( 1 - containedScale, 1 - containedScale, -1 );
				containedObject.isSelectable = false;
				containedObject.isDraggable = false;
				
				if ( target && !string.IsNullOrEmpty ( droppedMessage ) ) {
					target.SendMessage ( droppedMessage, this );
				}
			}
		}
		
		mouseRct = drawRct;
	}
Example #5
0
 public OGLine(OGWidget start, Vector3 startDir, OGWidget end, Vector3 endDir, int segments)
 {
     this.start    = start;
     this.startDir = startDir;
     this.end      = end;
     this.endDir   = endDir;
     this.segments = segments;
 }
Example #6
0
    public static void DrawSprite(Rect rect, OGStyle style, float depth, Color tint, OGWidget clipping)
    {
        if (style == null)
        {
            return;
        }

        DrawSprite(rect, style.coordinates, depth, style.color, tint, clipping);
    }
Example #7
0
	private int GetStyleIndex ( OGWidget widget, OGStyle style ) {
		if ( widget.root && widget.root.skin ) {
			for ( int i = 0; i < widget.root.skin.styles.Length; i++ ) {
				if ( widget.root.skin.styles[i].name == style.name ) {
					return i;
				}
			}
		}
	
		return 0;
	}
Example #8
0
	private string[] GetStyles ( OGWidget widget ) {
		List< string > tempList = new List< string >();
		
		if ( widget && widget.root ) {
			foreach ( OGStyle style in widget.root.skin.styles ) {
				tempList.Add ( style.name );
			}
		}
		
		return tempList.ToArray();
	}
Example #9
0
    //////////////////
    // Widget management
    //////////////////
    public void UpdateWidgets()
    {
        if (currentPage == null)
        {
            return;
        }

        mouseOver.Clear();

        // Update widget lists
        widgets = GetCurrentWidgets();

        for (int i = 0; i < widgets.Length; i++)
        {
            OGWidget w = widgets[i];

            if (w == null || !w.isDrawn || w.isDisabled)
            {
                continue;
            }

            // Check mouse
            if (w.CheckMouseOver())
            {
                w.OnMouseOver();
                mouseOver.Add(w);
            }

            // Check scroll offset
            if (!w.clipTo)
            {
                w.scrollOffset.x = 0;
                w.scrollOffset.y = 0;
            }

            w.root             = this;
            w.gameObject.layer = this.gameObject.layer;
            w.UpdateWidget();
            w.Recalculate();

            // Cleanup from previous OpenGUI versions
            if (w.hidden)
            {
                DestroyImmediate(w.gameObject);
            }
        }

        // Is mouse over anything?
        isMouseOver = mouseOver.Count > 0;
    }
Example #10
0
    private void MoveSelection(float x, float y)
    {
        for (int i = 0; i < UnityEditor.Selection.gameObjects.Length; i++)
        {
            OGWidget w = UnityEditor.Selection.gameObjects[i].GetComponent <OGWidget>();

            if (w)
            {
                Vector3 newPos = new Vector3(x, y, 0);

                w.transform.localPosition = w.transform.localPosition + newPos;
            }
        }
    }
    private string[] GetStyles(OGWidget widget)
    {
        List <string> tempList = new List <string>();

        if (widget && widget.root)
        {
            foreach (OGStyle style in widget.root.skin.styles)
            {
                tempList.Add(style.name);
            }
        }

        return(tempList.ToArray());
    }
    private int GetStyleIndex(OGWidget widget, OGStyle style)
    {
        if (widget.root && widget.root.skin)
        {
            for (int i = 0; i < widget.root.skin.styles.Length; i++)
            {
                if (widget.root.skin.styles[i].name == style.name)
                {
                    return(i);
                }
            }
        }

        return(0);
    }
Example #13
0
    ////////////////////
    // Update
    ////////////////////
    override public void UpdateWidget()
    {
        // Persistent vars
        isSelectable = true;

        if (CheckMouseOver())
        {
            currentStyle = styles.hover;
        }
        else
        {
            currentStyle = styles.basic;
        }

        // Mouse
        if (Input.GetMouseButtonUp(0) || GetTouch() == TouchPhase.Ended)
        {
            if (OGRoot.GetInstance().downWidget&& OGRoot.GetInstance().downWidget.isDraggable&& CheckMouseOver())
            {
                if (containedObject)
                {
                    Destroy(containedObject.gameObject);
                }

                containedObject = (OGWidget)Instantiate(OGRoot.GetInstance().downWidget);
                containedObject.gameObject.name         = containedObject.gameObject.name.Replace("(Clone)", "");
                containedObject.transform.parent        = this.transform;
                containedObject.transform.localScale    = new Vector3(containedScale, containedScale, 1);
                containedObject.transform.localPosition = new Vector3(1 - containedScale, 1 - containedScale, -1);
                containedObject.isSelectable            = false;
                containedObject.isDraggable             = false;

                if (target && !string.IsNullOrEmpty(droppedMessage))
                {
                    target.SendMessage(droppedMessage, this);
                }
            }
        }

        mouseRct = drawRct;
    }
Example #14
0
    ////////////////
    // Update
    ////////////////
    private void UpdateChildren()
    {
        this.gameObject.GetComponentsInChildren <OGWidget>(widgets);
        if (widgets.Capacity < widgets.Count)
        {
            widgets.Capacity = widgets.Count;
        }

        bounds = Vector2.zero;

        for (int i = 0; i < widgets.Count; i++)
        {
            OGWidget w = widgets[i];

            if (w != null && w != this)
            {
                w.scrollOffset = new Vector3(padding.x + position.x, padding.y + position.y, 0);

                // Reposition for infinite scrolling
                CheckWidgetOffset(w);

                w.anchor.x = RelativeX.None;
                w.anchor.y = RelativeY.None;
                w.clipTo   = this;

                float bottom = w.transform.position.y - this.transform.position.y + w.transform.localScale.y - size.y + padding.y * 2;
                float right  = w.transform.position.x - this.transform.position.x + w.transform.localScale.x - size.x + padding.x * 2;

                if (-bottom < bounds.y)
                {
                    bounds.y = -bottom;
                }

                if (-right < bounds.x)
                {
                    bounds.x = -right;
                }
            }
        }
    }
Example #15
0
    public void ApplyDefaultStyles(OGWidget w)
    {
        foreach (OGDefault d in GetAllDefaults())
        {
            if (d.widgetType == w.ToEnum())
            {
                if (w.styles == null)
                {
                    w.styles = new OGStyleSet();
                }

                w.styles.basic  = d.styleSet.basic;
                w.styles.active = d.styleSet.active;
                w.styles.hover  = d.styleSet.hover;
                w.styles.thumb  = d.styleSet.thumb;
                w.styles.ticked = d.styleSet.ticked;

                w.styles.Refresh(this);

                return;
            }
        }
    }
Example #16
0
 static int WidgetCompare(OGWidget a, OGWidget b)
 {
     return(b.transform.position.z.CompareTo(a.transform.position.z));
 }
Example #17
0
 public static void DrawLabel(Rect rect, string str, OGTextStyle style, float depth, Color tint, OGWidget clipping)
 {
     DrawLabel(rect, str, style, style.fontSize, style.alignment, depth, tint, clipping, null);
 }
Example #18
0
    public static void DrawLabel(Rect rect, string str, OGTextStyle style, int intSize, TextAnchor alignment, float depth, Color tint, OGWidget clipping, OGTextEditor editor)
    {
        // Check root
        if (root == null)
        {
            root = OGRoot.GetInstance();
            return;
        }

        // Check font
        if (style.font == null)
        {
            style.font = OGRoot.GetInstance().skin.fonts [style.fontIndex];
            return;
        }

        // Check string
        if (string.IsNullOrEmpty(str))
        {
            if (editor != null)
            {
                editor.cursorIndex       = 0;
                editor.cursorSelectIndex = 0;
                editor.cursorPos.x       = rect.xMin;
                editor.cursorPos.y       = rect.yMin - style.fontSize;
            }

            return;
        }

        // Check screen
        if (rect.xMin > root.screenWidth || rect.xMax < 0 || rect.yMax < 0 || rect.yMin > root.screenHeight)
        {
            return;
        }

        // Check clipping
        if (clipping != null)
        {
            if (rect.xMin > clipping.drawRct.xMax || rect.xMax < clipping.drawRct.xMin || rect.yMax < clipping.drawRct.yMin || rect.yMin > clipping.drawRct.yMax)
            {
                return;
            }
        }

        // Scale
        float   size      = (intSize * 1.0f) / style.font.size;
        Vector2 atlasSize = style.font.atlasSize;

        // Bounds
        float left   = style.padding.left;
        float right  = rect.width - style.padding.right - style.padding.left;
        float top    = rect.height - style.padding.top;
        float bottom = style.padding.bottom;
        float middle = (rect.height / 2) + ((style.font.font.lineHeight * size) / 2);
        float center = left + right / 2;

        // Positioning
        Vector2 anchor = Vector2.zero;
        float   space  = (style.font.GetCharacterInfo(" "[0]).advance *size);

        // Line and progression management
        Vector2 advance              = Vector2.zero;
        int     nextLineStart        = 0;
        int     thisLineStart        = 0;
        int     lastSpace            = 0;
        float   lineWidth            = 0;
        float   lineWidthAtLastSpace = 0;
        float   lineHeight           = style.font.font.lineHeight * size;
        int     emergencyBrake       = 0;

        // Temp vars
        CharacterInfo info;

        // Set anchor
        switch (alignment)
        {
        case TextAnchor.UpperLeft:
            anchor.x = left;
            anchor.y = top;
            break;

        case TextAnchor.MiddleLeft:
            anchor.x = left;
            anchor.y = middle;
            break;

        case TextAnchor.LowerLeft:
            anchor.x = left;
            anchor.y = bottom;
            break;

        case TextAnchor.UpperCenter:
            anchor.x = center;
            anchor.y = top;
            break;

        case TextAnchor.MiddleCenter:
            anchor.x = center;
            anchor.y = middle;
            break;

        case TextAnchor.LowerCenter:
            anchor.x = center;
            anchor.y = bottom;
            break;

        case TextAnchor.UpperRight:
            anchor.x = right;
            anchor.y = top;
            break;

        case TextAnchor.MiddleRight:
            anchor.x = right;
            anchor.y = middle;
            break;

        case TextAnchor.LowerRight:
            anchor.x = right;
            anchor.y = bottom;
            break;
        }

        // Color
        Color color = style.fontColor;

        color.r *= tint.r;
        color.g *= tint.g;
        color.b *= tint.b;
        color.a *= tint.a;
        GL.Color(color);

        // Draw loop
        while (nextLineStart < str.Length && advance.y - style.padding.top > -(rect.height - style.padding.top - style.padding.bottom))
        {
            int c = 0;

            // Get next line
            lastSpace     = 0;
            lineWidth     = 0;
            thisLineStart = nextLineStart;

            // ^ Parse remaining string, set start and end integers
            for (c = thisLineStart; c < str.Length; c++)
            {
                info = style.font.GetCharacterInfo(str[c]);

                // This character is a carriage return
                if (str[c] == "\n"[0])
                {
                    nextLineStart = c + 1;
                    break;

                    // This character is a space
                }
                else if (str[c] == " "[0])
                {
                    lineWidthAtLastSpace = lineWidth;
                    lineWidth           += space;
                    lastSpace            = c;

                    // This character is a regular glyph
                }
                else
                {
                    lineWidth += info.advance * size;
                }

                // The line width has exceeded the border
                if (lineWidth >= right)
                {
                    nextLineStart = lastSpace == 0 ? lastSpace : c + 1;
                    lineWidth     = lineWidthAtLastSpace;
                    break;
                }
            }

            // The string has ended
            if (c >= str.Length - 1)
            {
                nextLineStart = str.Length;
            }

            // Alignment advance adjustments
            if (anchor.x == center)
            {
                advance.x -= lineWidth / 2;
            }
            else if (anchor.x == right)
            {
                advance.x -= lineWidth;
            }

            // Draw glyphs
            for (int g = thisLineStart; g < nextLineStart; g++)
            {
                info = style.font.GetCharacterInfo(str[g]);

                Rect    vert          = Rect.MinMaxRect(info.minX * size, info.maxY * size, info.maxX * size, info.minY * size);
                Vector2 uvBottomLeft  = info.uvBottomLeft;
                Vector2 uvTopLeft     = info.uvTopLeft;
                Vector2 uvTopRight    = info.uvTopRight;
                Vector2 uvBottomRight = info.uvBottomRight;

                // Quad corners
                float gLeft   = anchor.x + vert.x + rect.x + advance.x;
                float gRight  = anchor.x + vert.x + rect.x + advance.x + vert.width;
                float gBottom = anchor.y + vert.height + vert.y + rect.y + advance.y - lineHeight;
                float gTop    = anchor.y + vert.height + vert.y + rect.y + advance.y - vert.height - lineHeight;

                // If it's a space, set appropriate corners
                if (str[g] == " "[0])
                {
                    gRight += space;
                }

                // Set cursor position
                if (editor != null)
                {
                    if (editor.cursorIndex == g)
                    {
                        editor.cursorPos.x = gLeft;
                        editor.cursorPos.y = gBottom;
                    }
                    else if (editor.cursorIndex >= editor.str.Length && g == editor.str.Length - 1)
                    {
                        editor.cursorPos.x = gRight;
                        editor.cursorPos.y = gBottom;
                    }


                    if (editor.cursorSelectIndex == g)
                    {
                        editor.cursorSelectPos.x = gLeft;
                        editor.cursorSelectPos.y = gBottom;
                    }
                    else if (editor.cursorSelectIndex >= editor.str.Length && g == editor.str.Length - 1)
                    {
                        editor.cursorSelectPos.x = gRight;
                        editor.cursorSelectPos.y = gBottom;
                    }

                    editor.cursorSize.x = 1;
                    editor.cursorSize.y = style.fontSize;
                }

                // If it's a space, continue the loop
                if (str[g] == " "[0])
                {
                    advance.x += space;
                    continue;
                }

                // Advance regardless if the glyph is drawn or not
                advance.x += info.advance * size;

                // Clipping
                if (clipping != null)
                {
                    if (gLeft < clipping.drawRct.xMin)
                    {
                        uvBottomLeft.x += (clipping.drawRct.xMin - gLeft) / atlasSize.x;
                        uvTopLeft.x    += (clipping.drawRct.xMin - gLeft) / atlasSize.x;
                        gLeft           = clipping.drawRct.xMin;
                    }

                    if (gRight > clipping.drawRct.xMax)
                    {
                        uvTopRight.x    -= (gRight - clipping.drawRct.xMax) / atlasSize.x;
                        uvBottomRight.x -= (gRight - clipping.drawRct.xMax) / atlasSize.x;
                        gRight           = clipping.drawRct.xMax;
                    }

                    if (gBottom < clipping.drawRct.yMin)
                    {
                        uvBottomLeft.y  += (clipping.drawRct.yMin - gBottom) / atlasSize.y;
                        uvBottomRight.y += (clipping.drawRct.yMin - gBottom) / atlasSize.y;
                        gBottom          = clipping.drawRct.yMin;
                    }

                    if (gTop > clipping.drawRct.yMax)
                    {
                        uvTopLeft.y  += (gTop - clipping.drawRct.yMax) / atlasSize.y;
                        uvTopRight.y += (gTop - clipping.drawRct.yMax) / atlasSize.y;
                        gTop          = clipping.drawRct.yMax;
                    }

                    // If the sides overlap, the glyph shouldn't be drawn
                    if (gLeft >= gRight || gBottom >= gTop)
                    {
                        continue;
                    }
                }

                // Bottom Left
                GL.TexCoord2(uvBottomLeft.x, uvBottomLeft.y);
                GL.Vertex3(gLeft, gBottom, depth);

                // Top left
                GL.TexCoord2(uvTopLeft.x, uvTopLeft.y);
                GL.Vertex3(gLeft, gTop, depth);

                // Top right
                GL.TexCoord2(uvTopRight.x, uvTopRight.y);
                GL.Vertex3(gRight, gTop, depth);

                // Bottom right
                GL.TexCoord2(uvBottomRight.x, uvBottomRight.y);
                GL.Vertex3(gRight, gBottom, depth);
            }

            // Next line
            advance.y -= lineHeight;
            advance.x  = 0;

            // Emergency
            if (emergencyBrake > 1000)
            {
                Debug.Log("OGDrawHelper | Label exceeded 1000 lines!");
                return;
            }
            else
            {
                emergencyBrake++;
            }
        }

        GL.Color(Color.white);
    }
Example #19
0
	public static void DrawLabel ( Rect rect, string str, OGTextStyle style, float depth, Color tint, OGWidget clipping ) {
		DrawLabel ( rect, str, style, style.fontSize, style.alignment, depth, tint, clipping, null );
	}
Example #20
0
    public void OnGUI()
    {
        Event e = Event.current;

        if (!Application.isPlaying)
        {
            Color color = Color.white;

            if (!guiTex)
            {
                guiTex = new Texture2D(1, 1);
                guiTex.SetPixel(0, 0, color);
                guiTex.Apply();
            }

            GUIStyle style = new GUIStyle();
            style.normal.background = guiTex;
            OGWidget w = null;

            for (int i = 0; i < UnityEditor.Selection.gameObjects.Length; i++)
            {
                w = UnityEditor.Selection.gameObjects[i].GetComponent <OGWidget>();

                if (w)
                {
                    Rect revRect = w.scaledRct;
                    revRect.y = Screen.height - revRect.y - revRect.height;

                    Rect pivotRect = new Rect(w.transform.position.x - 2, w.transform.position.y - 2, 4, 4);

                    pivotRect.x      *= ratio.x;
                    pivotRect.width  *= ratio.x;
                    pivotRect.y      *= ratio.y;
                    pivotRect.height *= ratio.y;

                    UnityEditor.Handles.color = color;

                    // Draw outline
                    UnityEditor.Handles.DrawPolyLine(
                        new Vector3(revRect.xMin, revRect.yMin, 0),
                        new Vector3(revRect.xMin, revRect.yMax, 0),
                        new Vector3(revRect.xMax, revRect.yMax, 0),
                        new Vector3(revRect.xMax, revRect.yMin, 0),
                        new Vector3(revRect.xMin, revRect.yMin, 0)
                        );

                    // Draw pivot
                    GUI.Box(pivotRect, "", style);
                }
            }

            switch (e.type)
            {
            case EventType.MouseDown:
                w = FindMouseOverWidget(e);

                EditorSelectWidget(w, e.shift);

                break;

            case EventType.KeyDown:
                int modifier = 1;

                if (e.shift)
                {
                    modifier = 10;
                }

                switch (e.keyCode)
                {
                case KeyCode.UpArrow:
                    MoveSelection(0, -modifier);
                    break;

                case KeyCode.DownArrow:
                    MoveSelection(0, modifier);
                    break;

                case KeyCode.LeftArrow:
                    MoveSelection(-modifier, 0);
                    break;

                case KeyCode.RightArrow:
                    MoveSelection(modifier, 0);
                    break;
                }

                break;
            }
        }
    }
Example #21
0
	public static OGWidgetType GetWidgetEnum ( OGWidget w ) {
		if ( widgetEnums.Count < 1 ) {
			widgetEnums.Add ( typeof(OGButton), OGWidgetType.Button );
			widgetEnums.Add ( typeof(OGDropDown), OGWidgetType.DropDown );
			widgetEnums.Add ( typeof(OGDropContainer), OGWidgetType.DropContainer );
			widgetEnums.Add ( typeof(OGLabel), OGWidgetType.Label );
			widgetEnums.Add ( typeof(OGListItem), OGWidgetType.ListItem );
			widgetEnums.Add ( typeof(OGPopUp), OGWidgetType.PopUp );
			widgetEnums.Add ( typeof(OGProgressBar), OGWidgetType.ProgressBar );
			widgetEnums.Add ( typeof(OGScrollView), OGWidgetType.ScrollView );
			widgetEnums.Add ( typeof(OGSlider), OGWidgetType.Slider );
			widgetEnums.Add ( typeof(OGSlicedSprite), OGWidgetType.SlicedSprite );
			widgetEnums.Add ( typeof(OGSprite), OGWidgetType.Sprite );
			widgetEnums.Add ( typeof(OGTabs), OGWidgetType.Tabs );
			widgetEnums.Add ( typeof(OGTextField), OGWidgetType.TextField );
			widgetEnums.Add ( typeof(OGTickBox), OGWidgetType.TickBox );
		}

		if ( widgetEnums.ContainsKey ( w.GetType() ) ) {
			return widgetEnums [ w.GetType() ];
		} else {
			return OGWidgetType.NONE;
		}
	}
Example #22
0
	public static void DrawSprite ( Rect rect, OGStyle style, float depth, Color tint, OGWidget clipping ) {
		if ( style == null ) { return; }

		DrawSprite ( rect, style.coordinates, depth, style.color, tint, clipping );
	}	
Example #23
0
    public static void DrawSprite(Rect rect, Rect uvRect, float depth, Color color, Color tint, OGWidget clipping)
    {
        if (!root)
        {
            root = OGRoot.GetInstance();
            return;
        }

        // Check screen
        if (rect.xMin > root.screenWidth || rect.xMax < 0 || rect.yMax < 0 || rect.yMin > root.screenHeight)
        {
            return;
        }

        // Color
        color.r *= tint.r;
        color.g *= tint.g;
        color.b *= tint.b;
        color.a *= tint.a;
        GL.Color(color);

        // Quad corners
        float left   = rect.x;
        float right  = rect.x + rect.width;
        float bottom = rect.y;
        float top    = rect.y + rect.height;

        // Check clipping
        if (clipping != null)
        {
            if (rect.xMin > clipping.drawRct.xMax || rect.xMax < clipping.drawRct.xMin || rect.yMax < clipping.drawRct.yMin || rect.yMin > clipping.drawRct.yMax)
            {
                return;
            }
            else
            {
                if (left < clipping.drawRct.xMin)
                {
                    left = clipping.drawRct.xMin;
                }
                if (right > clipping.drawRct.xMax)
                {
                    right = clipping.drawRct.xMax;
                }
                if (bottom < clipping.drawRct.yMin)
                {
                    bottom = clipping.drawRct.yMin;
                }
                if (top > clipping.drawRct.yMax)
                {
                    top = clipping.drawRct.yMax;
                }
            }
        }

        uvRect.x      /= texSize.x;
        uvRect.y      /= texSize.y;
        uvRect.width  /= texSize.x;
        uvRect.height /= texSize.y;

        // Bottom Left
        GL.TexCoord2(uvRect.x, uvRect.y);
        GL.Vertex3(left, bottom, depth);

        // Top left
        GL.TexCoord2(uvRect.x, uvRect.y + uvRect.height);
        GL.Vertex3(left, top, depth);

        // Top right
        GL.TexCoord2(uvRect.x + uvRect.width, uvRect.y + uvRect.height);
        GL.Vertex3(right, top, depth);

        // Bottom right
        GL.TexCoord2(uvRect.x + uvRect.width, uvRect.y);
        GL.Vertex3(right, bottom, depth);

        // Reset color
        GL.Color(Color.white);
    }
Example #24
0
    public static void DrawTiledSprite(Rect rect, Rect uvRect, float depth, Color color, Color tint, float tileX, float tileY, OGWidget clipping)
    {
        for (int x = 0; x < tileX; x++)
        {
            for (int y = 0; y < tileY; y++)
            {
                Vector2 newScale    = new Vector2(rect.width / tileX, rect.height / tileY);
                Vector2 newPosition = new Vector2(rect.x + x * newScale.x, rect.y + y * newScale.y);

                DrawSprite(new Rect(newPosition.x, newPosition.y, newScale.x, newScale.y), uvRect, depth, color, tint, clipping);
            }
        }
    }
    override public void OnInspectorGUI()
    {
        OGWidget widget = (OGWidget)target;

        if (!widget || !widget.root)
        {
            return;
        }

        // Check for hidden widgets
        if (widget.hidden)
        {
            EditorGUILayout.LabelField("This widget is rubbish that somehow didn't get deleted.");

            // Kill!
            if (GUILayout.Button("Fix"))
            {
                DestroyImmediate(target as GameObject);
            }
        }
        else
        {
            // Default inspector
            DrawDefaultInspector();

            // OGCameraWindow, OGLineNode and OGTexture don't need styles
            if (target.GetType() != typeof(OGLineNode) && target.GetType() != typeof(OGTexture) && target.GetType() != typeof(OGCameraWindow))
            {
                EditorGUILayout.Space();

                EditorGUILayout.LabelField("Style", EditorStyles.boldLabel);

                foreach (OGStyleType styleType in System.Enum.GetValues(typeof(OGStyleType)) as OGStyleType[])
                {
                    if (OGSkin.IsStyleUsed(styleType, widget.ToEnum()))
                    {
                        // Styles
                        OGStyle wdStyle      = widget.styles.GetStyle(styleType);
                        int     wdStyleIndex = GetStyleIndex(widget, wdStyle);
                        EditorGUILayout.BeginHorizontal();

                        EditorGUILayout.LabelField(styleType.ToString());

                        wdStyleIndex = EditorGUILayout.Popup(wdStyleIndex, GetStyles(widget));
                        widget.styles.SetStyle(styleType, widget.root.skin.styles [wdStyleIndex]);

                        // ^ Edit
                        if (GUILayout.Button("Edit", GUILayout.Width(40)))
                        {
                            Selection.activeObject = widget.root.skin;
                            OGSkinInspector.SetCurrentStyle(wdStyleIndex);
                        }

                        EditorGUILayout.EndHorizontal();
                    }
                }

                EditorGUILayout.BeginHorizontal();

                // Get defaults
                if (GUILayout.Button("Apply default styles"))
                {
                    (target as OGWidget).ApplyDefaultStyles();
                }

                // ^ Edit
                if (GUILayout.Button("Edit", GUILayout.Width(40)))
                {
                    Selection.activeObject = widget.root.skin;
                    OGSkinInspector.SetDefaultsMode();
                }

                EditorGUILayout.EndHorizontal();
            }
        }
    }
Example #26
0
    public static void DrawTiledSlicedSprite(Rect rect, OGStyle style, float depth, Color tint, float tileX, float tileY, OGWidget clipping)
    {
        Rect uvRect = style.coordinates;
        OGSlicedSpriteOffset border = style.border;
        Color color = style.color;

        // If no border is defined, draw a regular sprite
        if (border.left == 0 && border.right == 0 && border.top == 0 && border.bottom == 0)
        {
            DrawSprite(rect, style, depth, tint, clipping);

            // Draw all corners, panels and the center
        }
        else
        {
            // Bottom left corner
            DrawSprite(
                new Rect(rect.x, rect.y, border.left, border.bottom),
                new Rect(uvRect.x, uvRect.y, border.left, border.bottom),
                depth,
                color,
                tint,
                clipping
                );

            // Left panel
            DrawTiledSprite(
                new Rect(rect.x, rect.y + border.bottom, border.left, rect.height - border.bottom - border.top),
                new Rect(uvRect.x, uvRect.y + border.bottom, border.left, uvRect.height - border.top - border.bottom),
                depth,
                color,
                tint,
                1.0f,
                tileY,
                clipping
                );

            // Top left corner
            DrawSprite(
                new Rect(rect.x, rect.y + rect.height - border.top, border.left, border.top),
                new Rect(uvRect.x, uvRect.y + uvRect.height - border.top, border.left, border.top),
                depth,
                color,
                tint,
                clipping
                );

            // Top panel
            DrawTiledSprite(
                new Rect(rect.x + border.left, rect.y + rect.height - border.top, rect.width - border.horizontal, border.top),
                new Rect(uvRect.x + border.left, uvRect.y + uvRect.height - border.top, uvRect.width - border.horizontal, border.top),
                depth,
                color,
                tint,
                tileX,
                1.0f,
                clipping
                );

            // Top right corner
            DrawSprite(
                new Rect(rect.x + rect.width - border.right, rect.y + rect.height - border.top, border.right, border.top),
                new Rect(uvRect.x + uvRect.width - border.right, uvRect.y + uvRect.height - border.top, border.right, border.top),
                depth,
                color,
                tint,
                clipping
                );

            // Right panel
            DrawTiledSprite(
                new Rect(rect.x + rect.width - border.right, rect.y + border.bottom, border.right, rect.height - border.vertical),
                new Rect(uvRect.x + uvRect.width - border.right, uvRect.y + border.bottom, border.right, uvRect.height - border.vertical),
                depth,
                color,
                tint,
                1.0f,
                tileY,
                clipping
                );

            // Bottom left corner
            DrawSprite(
                new Rect(rect.x + rect.width - border.right, rect.y, border.right, border.bottom),
                new Rect(uvRect.x + uvRect.width - border.right, uvRect.y, border.right, border.bottom),
                depth,
                color,
                tint,
                clipping
                );

            // Top panel
            DrawTiledSprite(
                new Rect(rect.x + border.left, rect.y, rect.width - border.horizontal, border.bottom),
                new Rect(uvRect.x + border.left, uvRect.y, uvRect.width - border.horizontal, border.bottom),
                depth,
                color,
                tint,
                tileX,
                1.0f,
                clipping
                );

            // Center
            DrawTiledSprite(
                new Rect(rect.x + border.left, rect.y + border.bottom, rect.width - border.right - border.left, rect.height - border.bottom - border.top),
                new Rect(uvRect.x + border.left, uvRect.y + border.bottom, uvRect.width - border.right - border.left, uvRect.height - border.bottom - border.top),
                depth,
                color,
                tint,
                tileX,
                tileY,
                clipping
                );
        }
    }
Example #27
0
 //////////////////
 // Update
 //////////////////
 public void ReleaseWidget()
 {
     downWidget = null;
 }
Example #28
0
	//////////////////
	// Update
	//////////////////
	public void ReleaseWidget () {
		downWidget = null;
	}
Example #29
0
	public void UpdateMouse () {
		if ( widgets == null ) { return; }
		
		int i = 0;
		OGWidget w;

		// Click
		if ( Input.GetMouseButtonDown ( 0 ) || Input.GetMouseButtonDown ( 2 ) || GetTouch () == TouchPhase.Began ) {
			OGWidget topWidget = null;
			
			for ( i = 0; i < mouseOver.Count; i++ ) {
				w = mouseOver[i];
				
				if ( ( w.GetType() != typeof ( OGScrollView ) || ( w as OGScrollView ).touchControl ) && ( topWidget == null || w.transform.position.z < topWidget.transform.position.z ) && w.isSelectable ) {
					topWidget = w;
				}
			}
			
			if ( downWidget && downWidget != topWidget ) {
				downWidget.OnMouseCancel ();
			}
			
			if ( topWidget != null && topWidget.CheckMouseOver() && !topWidget.isDisabled ) {
				topWidget.OnMouseDown ();
				downWidget = topWidget;
			}

		// Release
		} else if ( Input.GetMouseButtonUp ( 0 ) || Input.GetMouseButtonUp ( 2 ) || GetTouch () == TouchPhase.Ended || GetTouch () == TouchPhase.Canceled ) {
			if ( downWidget ) {
				// Draggable
				if ( downWidget.resetAfterDrag && downWidget.GetType() != typeof ( OGScrollView ) ) {
					downWidget.transform.position = downWidget.dragOrigPos;
					downWidget.dragOffset = Vector3.zero;
					downWidget.dragOrigPos = Vector3.zero;
				}
				
				// Mouse over
				if ( ( downWidget.CheckMouseOver() || GetTouch () == TouchPhase.Ended ) && !downWidget.isDisabled && downWidget.CheckMouseOver()) {
					downWidget.OnMouseUp ();

				// Mouse out
				} else {
					downWidget.OnMouseCancel ();
				
				}
			
			}
		
		// Dragging
		} else if ( GetDragging () != Vector2.zero ) {
			if ( downWidget != null && !downWidget.isDisabled ) {
				if ( downWidget.clipTo && downWidget.clipTo.GetType() == typeof ( OGScrollView ) && ( downWidget.clipTo as OGScrollView ).touchControl ) {
					OGWidget thisWidget = downWidget;
					thisWidget.OnMouseCancel ();
					downWidget = thisWidget.clipTo;
				}
				
				downWidget.OnMouseDrag ();
			
				if ( downWidget.isDraggable && downWidget.GetType() != typeof ( OGScrollView ) ) {
					Vector3 mousePos = Input.mousePosition;
					mousePos.y = screenHeight - mousePos.y;

					if ( downWidget.dragOffset == Vector3.zero ) {
						if ( downWidget.resetAfterDrag ) {
							downWidget.dragOrigPos = downWidget.transform.position;
						}

						downWidget.dragOffset = downWidget.transform.position - mousePos;
					}

					Vector3 newPos = downWidget.transform.position;
					newPos = mousePos + downWidget.dragOffset;
					downWidget.transform.position = newPos;
				}
			}
		}

		// Escape key
		if ( Input.GetKeyDown ( KeyCode.Escape ) ) {
			if ( downWidget != null ) {
				downWidget.OnMouseCancel ();
				ReleaseWidget ();
			}
		}
	}	
Example #30
0
	public static void DrawSprite ( Rect rect, Rect uvRect, float depth, Color color, Color tint, OGWidget clipping ) {
		if ( !root ) {
			root = OGRoot.GetInstance();
			return;
		}
		
		// Check screen
		if ( rect.xMin > root.screenWidth || rect.xMax < 0 || rect.yMax < 0 || rect.yMin > root.screenHeight ) {
			return;
		}

		// Color
		color.r *= tint.r;
		color.g *= tint.g;
		color.b *= tint.b;
		color.a *= tint.a;
		GL.Color ( color );

		// Quad corners
		float left = rect.x;
		float right = rect.x + rect.width;
		float bottom = rect.y;
		float top = rect.y + rect.height;
		
		// Check clipping
		if ( clipping != null ) {
			if ( rect.xMin > clipping.drawRct.xMax || rect.xMax < clipping.drawRct.xMin || rect.yMax < clipping.drawRct.yMin || rect.yMin > clipping.drawRct.yMax ) {
				return;
			} else {
				if ( left < clipping.drawRct.xMin ) { left = clipping.drawRct.xMin; }
				if ( right > clipping.drawRct.xMax ) { right = clipping.drawRct.xMax; }
				if ( bottom < clipping.drawRct.yMin ) { bottom = clipping.drawRct.yMin; }
				if ( top > clipping.drawRct.yMax ) { top = clipping.drawRct.yMax; }
			}
		}
		
		uvRect.x /= texSize.x;
		uvRect.y /= texSize.y;
		uvRect.width /= texSize.x;
		uvRect.height /= texSize.y;

		// Bottom Left	
		GL.TexCoord2 ( uvRect.x, uvRect.y );
		GL.Vertex3 ( left, bottom, depth );
		
		// Top left
		GL.TexCoord2 ( uvRect.x, uvRect.y + uvRect.height );
		GL.Vertex3 ( left, top, depth );
		
		// Top right
		GL.TexCoord2 ( uvRect.x + uvRect.width, uvRect.y + uvRect.height );
		GL.Vertex3 ( right, top, depth );
		
		// Bottom right
		GL.TexCoord2 ( uvRect.x + uvRect.width, uvRect.y );
		GL.Vertex3 ( right, bottom, depth );

		// Reset color
		GL.Color ( Color.white );
	}
Example #31
0
	public void ApplyDefaultStyles ( OGWidget w ) {
		foreach ( OGDefault d in GetAllDefaults() ) {
			if ( d.widgetType == w.ToEnum () ) {
				if ( w.styles == null ) {
					w.styles = new OGStyleSet ();
				}
			
				w.styles.basic = d.styleSet.basic;
				w.styles.active = d.styleSet.active;
				w.styles.hover = d.styleSet.hover;
				w.styles.thumb = d.styleSet.thumb;
				w.styles.ticked = d.styleSet.ticked;

				w.styles.Refresh ( this );
			
				return;
			}
		}	
	}
Example #32
0
	public static void DrawTiledSprite ( Rect rect, Rect uvRect, float depth, Color color, Color tint, float tileX, float tileY, OGWidget clipping ) {
		for ( int x = 0; x < tileX; x++ ) {
			for ( int y = 0; y < tileY; y++ ) {
				Vector2 newScale = new Vector2 ( rect.width / tileX, rect.height / tileY );
				Vector2 newPosition = new Vector2 ( rect.x + x * newScale.x, rect.y + y * newScale.y );

				DrawSprite ( new Rect ( newPosition.x, newPosition.y, newScale.x, newScale.y ), uvRect, depth, color, tint, clipping );
			}
		}
	}
Example #33
0
	public static void DrawTiledSlicedSprite ( Rect rect, OGStyle style, float depth, Color tint, float tileX, float tileY, OGWidget clipping ) {
		Rect uvRect = style.coordinates;
		OGSlicedSpriteOffset border = style.border;
		Color color = style.color;

		// If no border is defined, draw a regular sprite
		if ( border.left == 0 && border.right == 0 && border.top == 0 && border.bottom == 0 ) {
			DrawSprite ( rect, style, depth, tint, clipping );

		// Draw all corners, panels and the center	
		} else {
			// Bottom left corner
			DrawSprite (
				new Rect ( rect.x, rect.y, border.left, border.bottom ),
				new Rect ( uvRect.x, uvRect.y, border.left, border.bottom ),
				depth,
				color,
				tint,
				clipping
			);
		
			// Left panel
			DrawTiledSprite (
				new Rect ( rect.x, rect.y + border.bottom, border.left, rect.height - border.bottom - border.top ),
				new Rect ( uvRect.x, uvRect.y + border.bottom, border.left, uvRect.height - border.top - border.bottom ),
				depth,
				color,
				tint,
				1.0f,
				tileY,
				clipping
			);

			// Top left corner
			DrawSprite (
				new Rect ( rect.x, rect.y + rect.height - border.top, border.left, border.top ),
				new Rect ( uvRect.x, uvRect.y + uvRect.height - border.top, border.left, border.top ),
				depth,
				color,
				tint,
				clipping
			);

			// Top panel
			DrawTiledSprite (
				new Rect ( rect.x + border.left, rect.y + rect.height - border.top, rect.width - border.horizontal, border.top ),
				new Rect ( uvRect.x + border.left, uvRect.y + uvRect.height - border.top, uvRect.width - border.horizontal, border.top ),
				depth,
				color,
				tint,
				tileX,
				1.0f,
				clipping
			);
			
			// Top right corner
			DrawSprite (
				new Rect ( rect.x + rect.width - border.right, rect.y + rect.height - border.top, border.right, border.top ),
				new Rect ( uvRect.x + uvRect.width - border.right, uvRect.y + uvRect.height - border.top, border.right, border.top ),
				depth,
				color,
				tint,
				clipping
			);
			
			// Right panel
			DrawTiledSprite (
				new Rect ( rect.x + rect.width - border.right, rect.y + border.bottom, border.right, rect.height - border.vertical ),
				new Rect ( uvRect.x + uvRect.width - border.right, uvRect.y + border.bottom, border.right, uvRect.height - border.vertical ),
				depth,
				color,
				tint,
				1.0f,
				tileY,
				clipping
			);

			// Bottom left corner
			DrawSprite (
				new Rect ( rect.x + rect.width - border.right, rect.y, border.right, border.bottom ),
				new Rect ( uvRect.x + uvRect.width - border.right, uvRect.y, border.right, border.bottom ),
				depth,
				color,
				tint,
				clipping
			);
			
			// Top panel
			DrawTiledSprite (
				new Rect ( rect.x + border.left, rect.y, rect.width - border.horizontal, border.bottom ),
				new Rect ( uvRect.x + border.left, uvRect.y, uvRect.width - border.horizontal, border.bottom ),
				depth,
				color,
				tint,
				tileX,
				1.0f,
				clipping
			);
			
			// Center
			DrawTiledSprite (
				new Rect ( rect.x + border.left, rect.y + border.bottom, rect.width - border.right - border.left, rect.height - border.bottom - border.top ),
				new Rect ( uvRect.x + border.left, uvRect.y + border.bottom, uvRect.width - border.right - border.left, uvRect.height - border.bottom - border.top ),
				depth,
				color,
				tint,
				tileX,
				tileY,
				clipping
			);
		}
	}
Example #34
0
    public void UpdateMouse()
    {
        if (widgets == null)
        {
            return;
        }

        int      i = 0;
        OGWidget w;

        // Click
        if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(2) || GetTouch() == TouchPhase.Began)
        {
            OGWidget topWidget = null;

            for (i = 0; i < mouseOver.Count; i++)
            {
                w = mouseOver[i];

                if ((w.GetType() != typeof(OGScrollView) || (w as OGScrollView).touchControl) && (topWidget == null || w.transform.position.z < topWidget.transform.position.z) && w.isSelectable)
                {
                    topWidget = w;
                }
            }

            if (downWidget && downWidget != topWidget)
            {
                downWidget.OnMouseCancel();
            }

            if (topWidget != null && topWidget.CheckMouseOver() && !topWidget.isDisabled)
            {
                topWidget.OnMouseDown();
                downWidget = topWidget;
            }

            // Release
        }
        else if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(2) || GetTouch() == TouchPhase.Ended || GetTouch() == TouchPhase.Canceled)
        {
            if (downWidget)
            {
                // Draggable
                if (downWidget.resetAfterDrag && downWidget.GetType() != typeof(OGScrollView))
                {
                    downWidget.transform.position = downWidget.dragOrigPos;
                    downWidget.dragOffset         = Vector3.zero;
                    downWidget.dragOrigPos        = Vector3.zero;
                }

                // Mouse over
                if ((downWidget.CheckMouseOver() || GetTouch() == TouchPhase.Ended) && !downWidget.isDisabled && downWidget.CheckMouseOver())
                {
                    downWidget.OnMouseUp();

                    // Mouse out
                }
                else
                {
                    downWidget.OnMouseCancel();
                }
            }

            // Dragging
        }
        else if (GetDragging() != Vector2.zero)
        {
            if (downWidget != null && !downWidget.isDisabled)
            {
                if (downWidget.clipTo && downWidget.clipTo.GetType() == typeof(OGScrollView) && (downWidget.clipTo as OGScrollView).touchControl)
                {
                    OGWidget thisWidget = downWidget;
                    thisWidget.OnMouseCancel();
                    downWidget = thisWidget.clipTo;
                }

                downWidget.OnMouseDrag();

                if (downWidget.isDraggable && downWidget.GetType() != typeof(OGScrollView))
                {
                    Vector3 mousePos = Input.mousePosition;
                    mousePos.y = screenHeight - mousePos.y;

                    if (downWidget.dragOffset == Vector3.zero)
                    {
                        if (downWidget.resetAfterDrag)
                        {
                            downWidget.dragOrigPos = downWidget.transform.position;
                        }

                        downWidget.dragOffset = downWidget.transform.position - mousePos;
                    }

                    Vector3 newPos = downWidget.transform.position;
                    newPos = mousePos + downWidget.dragOffset;
                    downWidget.transform.position = newPos;
                }
            }
        }

        // Escape key
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (downWidget != null)
            {
                downWidget.OnMouseCancel();
                ReleaseWidget();
            }
        }
    }
Example #35
0
	private void CheckWidgetOffset ( OGWidget w ) {
		if ( IsInfiniteY() ) {
			// Top
			if ( position.y < bounds.y && w.transform.position.y + w.transform.localScale.y + w.scrollOffset.y <= this.transform.position.y ) {
				w.scrollOffset.y += drawRct.height + padding.y * 4;
			
			// Bottom
			} else if ( position.y > 0 && w.transform.position.y + w.scrollOffset.y >= this.transform.position.y + size.y ) {
				w.scrollOffset.y -= drawRct.height + padding.y * 4;

			}	
		}
		
		if ( IsInfiniteX() ) {
			// Right
			if ( position.x < bounds.x && w.transform.position.x + w.transform.localScale.x + w.scrollOffset.x <= this.transform.position.x ) {
				w.scrollOffset.x += drawRct.width + padding.x * 4;
			
			// Left
			} else if ( position.x > 0 && w.transform.position.x + w.scrollOffset.x >= this.transform.position.x + size.x ) {
				w.scrollOffset.x -= drawRct.width + padding.x * 4;

			}	
		}
	}
Example #36
0
	public static void DrawLabel ( Rect rect, string str, OGTextStyle style, int intSize, TextAnchor alignment, float depth, Color tint, OGWidget clipping, OGTextEditor editor ) {
		// Check root
		if ( root == null ) {
			root = OGRoot.GetInstance ();
			return;
		}	
		
		// Check font
		if ( style.font == null ) {
			style.font = OGRoot.GetInstance().skin.fonts [ style.fontIndex ];
			return;
		}

		// Check string
		if ( string.IsNullOrEmpty ( str ) ) {
			if ( editor != null ) {
				editor.cursorIndex = 0;
				editor.cursorSelectIndex = 0;
				editor.cursorPos.x = rect.xMin;
				editor.cursorPos.y = rect.yMin - style.fontSize;
			}

			return;
		}

		// Check screen
		if ( rect.xMin > root.screenWidth || rect.xMax < 0 || rect.yMax < 0 || rect.yMin > root.screenHeight ) {
			return;
		}
		
		// Check clipping
		if ( clipping != null ) {
			if ( rect.xMin > clipping.drawRct.xMax || rect.xMax < clipping.drawRct.xMin || rect.yMax < clipping.drawRct.yMin || rect.yMin > clipping.drawRct.yMax ) {
				return;
			}
		}
		
		// Scale
		float size = ( intSize * 1.0f ) / style.font.size;
		Vector2 atlasSize = style.font.atlasSize;
		
		// Bounds
		float left = style.padding.left;
		float right = rect.width - style.padding.right - style.padding.left;
		float top = rect.height - style.padding.top;
		float bottom = style.padding.bottom;
		float middle = ( rect.height / 2 ) + ( ( style.font.info.lineSpacing * size ) / 2 );
		float center = left + right / 2;
		
		// Positioning
		Vector2 anchor = Vector2.zero;
		float space = ( style.font.GetCharacterInfo ( " "[0] ).width * size );
		
		// Line and progression management
		Vector2 advance = Vector2.zero;
		int nextLineStart = 0;
		int thisLineStart = 0;
		int lastSpace = 0;
		float lineWidth = 0;
		float lineWidthAtLastSpace = 0;
		float lineHeight = style.font.info.lineSpacing * size;
		int emergencyBrake = 0;
		
		// Temp vars
		OGCharacterInfo info;

		// Set anchor
		switch ( alignment ) {
			case TextAnchor.UpperLeft:
				anchor.x = left;
				anchor.y = top;
				break;

			case TextAnchor.MiddleLeft:
				anchor.x = left;
				anchor.y = middle;
				break;

			case TextAnchor.LowerLeft:
				anchor.x = left;
				anchor.y = bottom;
				break;
			
			case TextAnchor.UpperCenter:
				anchor.x = center;
				anchor.y = top;
				break;

			case TextAnchor.MiddleCenter:
				anchor.x = center;
				anchor.y = middle;
				break;

			case TextAnchor.LowerCenter:
				anchor.x = center;
				anchor.y = bottom;
				break;
			
			case TextAnchor.UpperRight:
				anchor.x = right;
				anchor.y = top;
				break;

			case TextAnchor.MiddleRight:
				anchor.x = right;
				anchor.y = middle;
				break;

			case TextAnchor.LowerRight:
				anchor.x = right;
				anchor.y = bottom;
				break;
		}

		// Color
		Color color = style.fontColor;
		color.r *= tint.r;
		color.g *= tint.g;
		color.b *= tint.b;
		color.a *= tint.a;
		GL.Color ( color );
	
		// Draw loop
		while ( nextLineStart < str.Length && advance.y - style.padding.top > - ( rect.height - style.padding.top - style.padding.bottom ) ) {
			int c = 0;

			// Get next line
			lastSpace = 0;
			lineWidth = 0;
			thisLineStart = nextLineStart;

			// ^ Parse remaining string, set start and end integers
			for ( c = thisLineStart; c < str.Length; c++ ) {
				info = style.font.GetCharacterInfo ( str[c] );
				
				// This character is a carriage return	
				if ( str[c] == "\n"[0] ) {
					nextLineStart = c + 1;
					break;
				
				// This character is a space
				} else if ( str[c] == " "[0] ) {
					lineWidthAtLastSpace = lineWidth;
					lineWidth += space;
					lastSpace = c;
				
				// This character is a regular glyph
				} else if ( info != null ) {
					lineWidth += info.width * size;
				
				}

				// The line width has exceeded the border
				if ( lineWidth >= right ) {
					nextLineStart = lastSpace == 0 ? lastSpace : c + 1;
					lineWidth = lineWidthAtLastSpace;
					break;
				}
			}
			
			// The string has ended
			if ( c >= str.Length - 1 ) {
				nextLineStart = str.Length;
			}

			// Alignment advance adjustments
			if ( anchor.x == center ) {
				advance.x -= lineWidth / 2;
			} else if ( anchor.x == right ) {
				advance.x -= lineWidth;
			}
		
			// Draw glyphs
			for ( int g = thisLineStart; g < nextLineStart; g++ ) {
				info = style.font.GetCharacterInfo ( str[g] );
				
				if ( info == null ) {
					continue;
				}

				Rect vert = new Rect ( info.vert.x * size, info.vert.y * size, info.vert.width * size, info.vert.height * size );
				Vector2[] uv = new Vector2[4];

				if ( info.flipped ) {
					uv[3] = new Vector2 ( info.uv.x, info.uv.y + info.uv.height );
					uv[2] = new Vector2 ( info.uv.x + info.uv.width, info.uv.y + info.uv.height );
					uv[1] = new Vector2 ( info.uv.x + info.uv.width, info.uv.y );
					uv[0] = new Vector2 ( info.uv.x, info.uv.y );
				} else {
					uv[0] = new Vector2 ( info.uv.x, info.uv.y );
					uv[1] = new Vector2 ( info.uv.x, info.uv.y + info.uv.height );
					uv[2] = new Vector2 ( info.uv.x + info.uv.width, info.uv.y + info.uv.height );
					uv[3] = new Vector2 ( info.uv.x + info.uv.width, info.uv.y );
				}		

				// Quad corners
				float gLeft = anchor.x + vert.x + rect.x + advance.x;
				float gRight = anchor.x + vert.x + rect.x + advance.x + vert.width;
				float gBottom = anchor.y + vert.height + vert.y + rect.y + advance.y;
				float gTop = anchor.y + vert.height + vert.y + rect.y + advance.y - vert.height;
	
				// If it's a space, set appropriate corners
				if ( str[g] == " "[0] ) {
					gRight += space;
				}

				// Set cursor position
				if ( editor != null ) {
					if ( editor.cursorIndex == g ) {
						editor.cursorPos.x = gLeft;
						editor.cursorPos.y = gBottom;
					
					} else if ( editor.cursorIndex >= editor.str.Length && g == editor.str.Length - 1 ) {
						editor.cursorPos.x = gRight;
						editor.cursorPos.y = gBottom;

					}
					
					
					if ( editor.cursorSelectIndex == g ) {
						editor.cursorSelectPos.x = gLeft;
						editor.cursorSelectPos.y = gBottom;
					
					} else if ( editor.cursorSelectIndex >= editor.str.Length && g == editor.str.Length - 1 ) {
						editor.cursorSelectPos.x = gRight;
						editor.cursorSelectPos.y = gBottom;

					}

					editor.cursorSize.x = 1;
					editor.cursorSize.y = style.fontSize;
				}
				
				// If it's a space, continue the loop
				if ( str[g] == " "[0] ) {
					advance.x += space;
					continue;
				}
			
				// Advance regardless if the glyph is drawn or not	
				advance.x += info.width * size;
		
				// Clipping
				if ( clipping != null ) {
					if ( gLeft < clipping.drawRct.xMin ) {
						uv[0].x += ( clipping.drawRct.xMin - gLeft ) / atlasSize.x;
						uv[1].x += ( clipping.drawRct.xMin - gLeft ) / atlasSize.x;
						gLeft = clipping.drawRct.xMin;
					}
					
					if ( gRight > clipping.drawRct.xMax ) {
						uv[2].x -= ( gRight - clipping.drawRct.xMax ) / atlasSize.x;
						uv[3].x -= ( gRight - clipping.drawRct.xMax ) / atlasSize.x;
						gRight = clipping.drawRct.xMax;
					}
					
					if ( gBottom < clipping.drawRct.yMin ) {
						uv[0].y += ( clipping.drawRct.yMin - gBottom ) / atlasSize.y;
						uv[3].y += ( clipping.drawRct.yMin - gBottom ) / atlasSize.y;
						gBottom = clipping.drawRct.yMin;
					}
					
					if ( gTop > clipping.drawRct.yMax ) {
						uv[1].y += ( gTop - clipping.drawRct.yMax ) / atlasSize.y;
						uv[2].y += ( gTop - clipping.drawRct.yMax ) / atlasSize.y;
						gTop = clipping.drawRct.yMax;
					}

					// If the sides overlap, the glyph shouldn't be drawn
					if ( gLeft >= gRight || gBottom >= gTop ) {
						continue;
					}
				}

				// Bottom Left
				GL.TexCoord2 ( uv[0].x, uv[0].y );
				GL.Vertex3 ( gLeft, gBottom, depth );
				
				// Top left
				GL.TexCoord2 ( uv[1].x, uv[1].y );
				GL.Vertex3 ( gLeft, gTop, depth );

				// Top right
				GL.TexCoord2 ( uv[2].x, uv[2].y );
				GL.Vertex3 ( gRight, gTop, depth );
			
				// Bottom right
				GL.TexCoord2 ( uv[3].x, uv[3].y );
				GL.Vertex3 ( gRight, gBottom, depth );

			}

			// Next line
			advance.y -= lineHeight;
			advance.x = 0;

			// Emergency
			if ( emergencyBrake > 1000 ) {
				Debug.Log ( "OGDrawHelper | Label exceeded 1000 lines!" );
				return;
			} else {
				emergencyBrake++;
			}
		}
		
		GL.Color ( Color.white );
	}
Example #37
0
	public static void SelectActiveObject ( OGWidget w, bool b = false )
	{
		Selection.activeObject = w.gameObject;
	}
 public static void SelectActiveObject(OGWidget w, bool b = false)
 {
     Selection.activeObject = w.gameObject;
 }