Ejemplo n.º 1
0
	public static void DrawLabel ( Rect rect, string str, OGTextStyle style, float depth, Color tint, OGWidget clipping, OGTextEditor editor ) {
		DrawLabel ( rect, str, style, style.fontSize, style.alignment, depth, tint, clipping, editor );
	}
Ejemplo n.º 2
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);
    }
Ejemplo n.º 3
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 );
	}
Ejemplo n.º 4
0
 public static void DrawLabel(Rect rect, string str, OGTextStyle style, float depth, Color tint, OGWidget clipping, OGTextEditor editor)
 {
     DrawLabel(rect, str, style, style.fontSize, style.alignment, depth, tint, clipping, editor);
 }