Advertisement
Guest User

SolidType

a guest
Feb 5th, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 34.42 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. #region SolidText class
  6.  
  7. public class SolidText : StateMachine
  8. {
  9.     #region Debug
  10.  
  11.     private void Start ()
  12.     {
  13.         SolidType.CacheFontResources( 10 );
  14.     }
  15.  
  16.     private void Update ()
  17.     {
  18.         if ( Input.GetKeyDown( KeyCode.U ) )
  19.             SetText( SolidType.Fonts.Test , "awesomer whoop\nverify please\nmoar test\nthen some" , 3 , 1 , 1 , 1.25f , Vector3.zero , SolidType.Alignment.Center , SolidType.BaseLine.Center );
  20.         else if ( Input.GetKeyDown( KeyCode.I ) )
  21.             SetText( SolidType.Fonts.Test , "wonderful stuff\nverify please\nmoar test\nthen some" , 3 , 1 , 1 , 1.25f , Vector3.zero , SolidType.Alignment.Center , SolidType.BaseLine.Center );
  22.         else if ( Input.GetKeyDown( KeyCode.O ) )
  23.             SetText( SolidType.Fonts.Test , "amazing dude\nsuch satisfy\nmoar test\nthen some" , 3 , 1 , 1 , 1.25f , Vector3.zero , SolidType.Alignment.Left , SolidType.BaseLine.Top );
  24.         else if ( Input.GetKeyDown( KeyCode.P ) )
  25.             SetText( SolidType.Fonts.Test , "dude incredible\nworks here\nmoar test\nthen some" , 3 , 1 , 1 , 1.25f , Vector3.zero , SolidType.Alignment.Right , SolidType.BaseLine.Bottom );
  26.         else if ( Input.GetKeyDown( KeyCode.N ) )
  27.             SolidAnimation.VerticalScaleDown( this );
  28.         else if ( Input.GetKeyDown( KeyCode.B ) )
  29.             SolidAnimation.VerticalScaleUp( this );
  30.         else if ( Input.GetKeyDown( KeyCode.M ) )
  31.             SolidUtils.CharacterYScaleZero( SolidUtils.GetCharacters( this ) );
  32.     }
  33.  
  34.     #endregion Debug
  35.  
  36.     #region Initialize
  37.  
  38.     public void Initialize ()
  39.     {
  40.         //
  41.     }
  42.  
  43.     #endregion Initialize
  44.  
  45.     #region Set text
  46.  
  47.     public void SetText ( SolidType.Fonts font , string text , Vector3 scale , float characterSpacing , float wordSpacing , float lineSpacing , Vector3 position , SolidType.Alignment alignment = SolidType.Alignment.Center , SolidType.BaseLine baseLine = SolidType.BaseLine.Bottom )
  48.     {
  49.         if ( text == _text )
  50.             return;
  51.  
  52.         ClearText();
  53.         string[] textLines = text.Split(new string[] { "\r\n", "\n" } , System.StringSplitOptions.None );
  54.         string containerName = string.Empty;
  55.  
  56.         for ( int i = 0 ; textLines.Length > i ; i++ )
  57.             containerName += textLines[ i ] + " ";
  58.  
  59.         MakeContainer( position , containerName );
  60.         _characterSpacing = characterSpacing;
  61.         _wordSpacing = wordSpacing;
  62.         _lineSpacing = lineSpacing;
  63.         _alignment = alignment;
  64.         _baseLine = baseLine;
  65.         _scale = scale;
  66.         _font = font;
  67.  
  68.         Set( text , textLines , position );
  69.  
  70.         switch ( baseLine )
  71.         {
  72.             case SolidType.BaseLine.Center:
  73.                 container.position += Vector3.up * scale.y * 0.5f;
  74.                 break;
  75.  
  76.             case SolidType.BaseLine.Bottom:
  77.                 container.position += Vector3.up * scale.y;
  78.                 break;
  79.         }
  80.     }
  81.  
  82.     private void ClearText ()
  83.     {
  84.         for ( int i = 0 ; _lines.Count > i ; i++ )
  85.         {
  86.             for ( int j = 0 ; _lines[ i ].words.Count > j ; j++ )
  87.             {
  88.                 for ( int k = 0 ; _lines[ i ].words[ j ].characters.Count > k ; k++ )
  89.                     SolidType.DespawnCharacter( _lines[ i ].words[ j ].characters[ k ] );
  90.  
  91.                 _lines[ i ].words[ j ].characters.Clear();
  92.             }
  93.  
  94.             _lines[ i ].words.Clear();
  95.         }
  96.  
  97.         _lines.Clear();
  98.     }
  99.  
  100.     public void SetText ( SolidType.Fonts font , string text , float scale , float characterSpacing , float wordSpacing , float lineSpacing , Vector3 position , SolidType.Alignment alignment = SolidType.Alignment.Center , SolidType.BaseLine baseLine = SolidType.BaseLine.Bottom )
  101.     {
  102.         SetText( font , text , Vector3.one * scale , characterSpacing , wordSpacing , lineSpacing , position , alignment , baseLine );
  103.     }
  104.  
  105.     #endregion Set text
  106.  
  107.     #region Set
  108.  
  109.     private void Set ( string text , string[] textLines , Vector3 position )
  110.     {
  111.         for ( int i = 0 ; textLines.Length > i ; i++ )
  112.         {
  113.             lines.Add( new SolidLine( textLines[ i ] , position , this ) );
  114.             position += Vector3.down * lineSpacing * scale.y;
  115.         }
  116.  
  117.         _text = text;
  118.     }
  119.  
  120.     #endregion Set
  121.  
  122.     #region Container
  123.  
  124.     private void MakeContainer ( Vector3 position , string name )
  125.     {
  126.         if ( _container != null )
  127.             Destroy( _container );
  128.  
  129.         _container = new GameObject( name );
  130.         container.parent = transform;
  131.         container.position = position;
  132.     }
  133.  
  134.     #endregion Container
  135.  
  136.     #region Variables
  137.  
  138.     #region Getters
  139.  
  140.     public SolidType.Alignment alignment
  141.     {
  142.         get
  143.         {
  144.             return _alignment;
  145.         }
  146.     }
  147.  
  148.     public SolidType.BaseLine baseLine
  149.     {
  150.         get
  151.         {
  152.             return _baseLine;
  153.         }
  154.     }
  155.  
  156.     public List<SolidLine> lines
  157.     {
  158.         get
  159.         {
  160.             return _lines;
  161.         }
  162.     }
  163.  
  164.     public Vector3 position
  165.     {
  166.         get
  167.         {
  168.             return transform.position;
  169.         }
  170.     }
  171.  
  172.     public Vector3 scale
  173.     {
  174.         get
  175.         {
  176.             return _scale;
  177.         }
  178.     }
  179.  
  180.     public Quaternion rotation
  181.     {
  182.         get
  183.         {
  184.             return container.localRotation;
  185.         }
  186.     }
  187.  
  188.     public float characterSpacing
  189.     {
  190.         get
  191.         {
  192.             return _characterSpacing;
  193.         }
  194.     }
  195.  
  196.     public float wordSpacing
  197.     {
  198.         get
  199.         {
  200.             return _wordSpacing;
  201.         }
  202.     }
  203.  
  204.     public float lineSpacing
  205.     {
  206.         get
  207.         {
  208.             return _lineSpacing;
  209.         }
  210.     }
  211.  
  212.     public Transform container
  213.     {
  214.         get
  215.         {
  216.             return _container.transform;
  217.         }
  218.     }
  219.  
  220.     public SolidType.Fonts font
  221.     {
  222.         get
  223.         {
  224.             return _font;
  225.         }
  226.     }
  227.  
  228.     #endregion Getters
  229.  
  230.     #region Private
  231.  
  232.     private GameObject _container;
  233.     private SolidType.Alignment _alignment;
  234.     private SolidType.BaseLine _baseLine;
  235.     private SolidType.Fonts _font;
  236.     private float _characterSpacing;
  237.     private float _wordSpacing;
  238.     private float _lineSpacing;
  239.     private List< SolidLine > _lines = new List<SolidLine>();
  240.     private string _text;
  241.     private Vector3 _scale;
  242.  
  243.     #endregion Private
  244.  
  245.     #endregion Variables
  246. }
  247.  
  248. #endregion SolidText class
  249.  
  250. #region SolidLine class
  251.  
  252. public class SolidLine
  253. {
  254.     #region Set text
  255.  
  256.     public void Set ( string text , Vector3 position )
  257.     {
  258.         MakeContainer( position , text );
  259.         char[] chars = text.ToCharArray();
  260.         int count = 0;
  261.  
  262.         while ( chars.Length - 1 > count )
  263.         {
  264.             char c = chars[ count ];
  265.             List< char > word = new List<char>();
  266.  
  267.             while ( chars.Length > count && !char.IsWhiteSpace( c ) )
  268.             {
  269.                 c = chars[ count ];
  270.  
  271.                 if ( !char.IsWhiteSpace( c ) )
  272.                     word.Add( c );
  273.  
  274.                 count++;
  275.             }
  276.  
  277.             words.Add( new SolidWord( new string( word.ToArray() ) , position , this ) );
  278.         }
  279.  
  280.         Layout();
  281.         _text = text;
  282.     }
  283.  
  284.     #endregion Set text
  285.  
  286.     #region Layout
  287.  
  288.     private void Layout ()
  289.     {
  290.         switch ( alignment )
  291.         {
  292.             case SolidType.Alignment.Center:
  293.                 CenterAlignedText();
  294.                 break;
  295.  
  296.             case SolidType.Alignment.Left:
  297.                 LeftAlignedText();
  298.                 break;
  299.  
  300.             case SolidType.Alignment.Right:
  301.                 RightAlignedText();
  302.                 break;
  303.         }
  304.     }
  305.  
  306.     private void CenterAlignedText ()
  307.     {
  308.         int mid = Mathf.FloorToInt( words.Count * 0.5f );
  309.  
  310.         if ( words.Count % 2 == 0 )
  311.         {
  312.             for ( int i = 0 ; mid > i ; i++ )
  313.                 words[ i ].PositionText( position + ( Vector3.left * scale.x * wordSpacing * 0.5f ) , SolidType.Alignment.Right );
  314.  
  315.             for ( int i = mid ; words.Count > i ; i++ )
  316.                 words[ i ].PositionText( position + ( Vector3.right * scale.x * wordSpacing * 0.5f ) , SolidType.Alignment.Left );
  317.  
  318.             Vector3 start = words[ 0 ].characters[ 0 ].position;
  319.             Vector3 end = words[ words.Count - 1 ].characters[ words[ words.Count - 1 ].characters.Count - 1 ].position;
  320.             Vector3 center = start + ( Vector3.right * Vector3.Distance( start , end ) * 0.5f );
  321.             Vector3 offset = position - center;
  322.  
  323.             for ( int i = 0 ; words.Count > i ; i++ )
  324.                 for ( int j = 0 ; words[ i ].characters.Count > j ; j++ )
  325.                     words[ i ].characters[ j ].position += offset;
  326.         }
  327.         else
  328.         {
  329.             words[ mid ].PositionText( position , SolidType.Alignment.Center );
  330.  
  331.             for ( int i = 0 ; mid > i ; i++ )
  332.                 words[ i ].PositionText( words[ mid ].characters[ 0 ].position + ( Vector3.left * scale.x * wordSpacing ) + ( Vector3.left * ( words[ mid ].characters[ 0 ].bounds.extents.x * words[ mid ].characters[ 0 ].scale.x ) ) , SolidType.Alignment.Right );
  333.  
  334.             for ( int i = mid + 1 ; words.Count > i ; i++ )
  335.                 words[ i ].PositionText( words[ mid ].characters[ words[ mid ].characters.Count - 1 ].position + ( Vector3.right * scale.x * wordSpacing ) + ( Vector3.right * ( words[ mid ].characters[ words[ mid ].characters.Count - 1 ].bounds.extents.x * words[ mid ].characters[ words[ mid ].characters.Count - 1 ].scale.x ) ) , SolidType.Alignment.Left );
  336.         }
  337.     }
  338.  
  339.     private void LeftAlignedText ()
  340.     {
  341.         Vector3 position = this.position;
  342.  
  343.         for ( int i = 0 ; words.Count > i ; i++ )
  344.         {
  345.             words[ i ].PositionText( position , alignment );
  346.             SolidCharacter lastChar = words[ i ].characters[ words[ i ].characters.Count - 1 ];
  347.             position = lastChar.container.position + ( Vector3.right * lastChar.bounds.extents.x * lastChar.scale.x );
  348.             position += Vector3.right * scale.x * wordSpacing;
  349.         }
  350.     }
  351.  
  352.     private void RightAlignedText ()
  353.     {
  354.         Vector3 position = this.position;
  355.  
  356.         for ( int i = 0 ; words.Count > i ; i++ )
  357.         {
  358.             words[ words.Count - i - 1 ].PositionText( position , alignment );
  359.             SolidCharacter firstChar = words[ words.Count - i - 1 ].characters[ 0 ];
  360.             position = firstChar.container.position + ( Vector3.left * firstChar.bounds.extents.x * firstChar.scale.x );
  361.             position += Vector3.left * scale.x * wordSpacing;
  362.         }
  363.     }
  364.  
  365.     #endregion Layout
  366.  
  367.     #region Container
  368.  
  369.     private void MakeContainer ( Vector3 position , string name )
  370.     {
  371.         if ( _container != null )
  372.             Object.Destroy( _container );
  373.  
  374.         _container = new GameObject( name );
  375.         container.position = position;
  376.         container.parent = parent.container;
  377.     }
  378.  
  379.     #endregion Container
  380.  
  381.     #region Variables
  382.  
  383.     #region Getters
  384.  
  385.     public SolidText root
  386.     {
  387.         get
  388.         {
  389.             return parent;
  390.         }
  391.     }
  392.  
  393.     public List<SolidWord> words
  394.     {
  395.         get
  396.         {
  397.             return _words;
  398.         }
  399.     }
  400.  
  401.     public SolidType.Alignment alignment
  402.     {
  403.         get
  404.         {
  405.             return root.alignment;
  406.         }
  407.     }
  408.  
  409.     public SolidText parent
  410.     {
  411.         get
  412.         {
  413.             return _parent;
  414.         }
  415.     }
  416.  
  417.     public Vector3 position
  418.     {
  419.         get
  420.         {
  421.             return container.position;
  422.         }
  423.     }
  424.  
  425.     public Vector3 scale
  426.     {
  427.         get
  428.         {
  429.             return root.scale;
  430.         }
  431.     }
  432.  
  433.     public float wordSpacing
  434.     {
  435.         get
  436.         {
  437.             return root.wordSpacing;
  438.         }
  439.     }
  440.  
  441.     public float lineSpacing
  442.     {
  443.         get
  444.         {
  445.             return root.lineSpacing;
  446.         }
  447.     }
  448.  
  449.     public float characterSpacing
  450.     {
  451.         get
  452.         {
  453.             return root.characterSpacing;
  454.         }
  455.     }
  456.  
  457.     public SolidType.BaseLine baseLine
  458.     {
  459.         get
  460.         {
  461.             return root.baseLine;
  462.         }
  463.     }
  464.  
  465.     public Transform container
  466.     {
  467.         get
  468.         {
  469.             return _container.transform;
  470.         }
  471.     }
  472.  
  473.     #endregion Getters
  474.  
  475.     #region Private
  476.  
  477.     private List< SolidWord > _words = new List< SolidWord >();
  478.     private GameObject _container;
  479.     private SolidText _parent;
  480.     private string _text;
  481.  
  482.     #endregion Private
  483.  
  484.     #endregion Variables
  485.  
  486.     #region Constructor
  487.  
  488.     public SolidLine ( string text , Vector3 position , SolidText parent )
  489.     {
  490.         _parent = parent;
  491.         Set( text , position );
  492.     }
  493.  
  494.     #endregion Constructor
  495. }
  496.  
  497. #endregion SolidLine class
  498.  
  499. #region SolidWord class
  500.  
  501. public class SolidWord
  502. {
  503.     #region Set text
  504.  
  505.     private void Set ( string text , bool generate = true )
  506.     {
  507.         if ( _text == text )
  508.             return;
  509.  
  510.         if ( generate )
  511.             GenerateText( text , parent.baseLine );
  512.  
  513.         _text = text;
  514.     }
  515.  
  516.     #endregion Set text
  517.  
  518.     #region Generate text
  519.  
  520.     private void GenerateText ( string text , SolidType.BaseLine baseLine )
  521.     {
  522.         if ( text == null || text.Length == 0 )
  523.             return;
  524.  
  525.         char[] chars = text.ToCharArray();
  526.  
  527.         for ( int i = 0 ; chars.Length > i ; i++ )
  528.             _characters.Add( SolidType.SpawnCharacter( root.font , chars[ i ] , parent.scale , i , this ) );
  529.     }
  530.  
  531.     #endregion Generate text
  532.  
  533.     #region Position text
  534.  
  535.     public void PositionText ( Vector3 position , SolidType.Alignment alignment )
  536.     {
  537.         switch ( alignment )
  538.         {
  539.             case SolidType.Alignment.Center:
  540.                 int mid = Mathf.FloorToInt( characters.Count * 0.5f );
  541.  
  542.                 if ( characters.Count % 2 == 0 )
  543.                 {
  544.                     characters[ mid - 1 ].position = position + ( Vector3.left * characters[ mid - 1 ].bounds.extents.x * characters[ mid - 1 ].scale.x ) + ( Vector3.left * parent.characterSpacing * 0.5f );
  545.                     characters[ mid ].position = position + ( Vector3.right * characters[ mid ].bounds.extents.x * characters[ mid ].scale.x ) + ( Vector3.right * parent.characterSpacing * 0.5f );
  546.  
  547.                     for ( int i = 1 ; mid > i ; i++ )
  548.                         characters[ mid - i - 1 ].position = characters[ mid - i - 1 ].next.position + ( Vector3.left * parent.characterSpacing ) + ( Vector3.left * characters[ mid - i - 1 ].next.bounds.extents.x * characters[ mid - i - 1 ].next.scale.x ) + ( Vector3.left * characters[ mid - i - 1 ].bounds.extents.x * characters[ mid - i - 1 ].scale.x );
  549.  
  550.                     for ( int i = mid + 1 ; characters.Count > i ; i++ )
  551.                         characters[ i ].position = characters[ i ].previous.position + ( Vector3.right * parent.characterSpacing ) + ( Vector3.right * characters[ i ].previous.bounds.extents.x * characters[ i ].previous.scale.x ) + ( Vector3.right * characters[ i ].bounds.extents.x * characters[ i ].scale.x );
  552.                 }
  553.                 else
  554.                 {
  555.                     characters[ mid ].position = position;
  556.  
  557.                     for ( int i = 0 ; mid > i ; i++ )
  558.                         characters[ mid - i - 1 ].position = characters[ mid - i - 1 ].next.position + ( Vector3.left * parent.characterSpacing ) + ( Vector3.left * characters[ mid - i - 1 ].next.bounds.extents.x * characters[ mid - i - 1 ].next.scale.x ) + ( Vector3.left * characters[ mid - i - 1 ].bounds.extents.x * characters[ mid - i - 1 ].scale.x );
  559.  
  560.                     for ( int i = mid + 1 ; characters.Count > i ; i++ )
  561.                         characters[ i ].position = characters[ i ].previous.position + ( Vector3.right * parent.characterSpacing ) + ( Vector3.right * characters[ i ].previous.bounds.extents.x * characters[ i ].previous.scale.x ) + ( Vector3.right * characters[ i ].bounds.extents.x * characters[ i ].scale.x );
  562.                 }
  563.  
  564.                 break;
  565.  
  566.             case SolidType.Alignment.Left:
  567.                 characters[ 0 ].position = position + ( Vector3.right * characters[ 0 ].bounds.extents.x * characters[ 0 ].scale.x );
  568.  
  569.                 for ( int i = 1 ; characters.Count > i ; i++ )
  570.                     characters[ i ].position = characters[ i ].previous.position + ( Vector3.right * parent.characterSpacing ) + ( Vector3.right * characters[ i ].previous.bounds.extents.x * characters[ i ].previous.scale.x ) + ( Vector3.right * characters[ i ].bounds.extents.x * characters[ i ].scale.x );
  571.                 break;
  572.  
  573.             case SolidType.Alignment.Right:
  574.                 characters[ characters.Count - 1 ].position = position + ( Vector3.left * characters[ characters.Count - 1 ].bounds.extents.x * characters[ characters.Count - 1 ].scale.x );
  575.  
  576.                 for ( int i = 2 ; characters.Count >= i ; i++ )
  577.                     characters[ characters.Count - i ].position = characters[ characters.Count - i ].next.position + ( Vector3.left * parent.characterSpacing ) + ( Vector3.left * characters[ characters.Count - i ].next.bounds.extents.x * characters[ characters.Count - i ].next.scale.x ) + ( Vector3.left * characters[ characters.Count - i ].bounds.extents.x * characters[ characters.Count - i ].scale.x );
  578.  
  579.                 break;
  580.         }
  581.     }
  582.  
  583.     #endregion Position text
  584.  
  585.     #region Container
  586.  
  587.     private void MakeContainer ( Vector3 position , string name )
  588.     {
  589.         if ( _container != null )
  590.             Object.Destroy( _container );
  591.  
  592.         _container = new GameObject( name );
  593.         container.parent = _parent.container;
  594.         container.position = new Vector3( position.x , _parent.container.position.y , position.z );
  595.     }
  596.  
  597.     #endregion Container
  598.  
  599.     #region Variables
  600.  
  601.     #region Getters
  602.  
  603.     public SolidText root
  604.     {
  605.         get
  606.         {
  607.             return parent.root;
  608.         }
  609.     }
  610.  
  611.     public SolidType.BaseLine baseLine
  612.     {
  613.         get
  614.         {
  615.             return root.baseLine;
  616.         }
  617.     }
  618.  
  619.     public Transform container
  620.     {
  621.         get
  622.         {
  623.             return _container.transform;
  624.         }
  625.     }
  626.  
  627.     public List<SolidCharacter> characters
  628.     {
  629.         get
  630.         {
  631.             return _characters;
  632.         }
  633.     }
  634.  
  635.     public float lineSpacing
  636.     {
  637.         get
  638.         {
  639.             return root.lineSpacing;
  640.         }
  641.     }
  642.  
  643.     public SolidLine parent
  644.     {
  645.         get
  646.         {
  647.             return _parent;
  648.         }
  649.     }
  650.  
  651.     #endregion Getters
  652.  
  653.     #region Private
  654.  
  655.     private SolidLine _parent;
  656.     private GameObject _container;
  657.     private List< SolidCharacter > _characters = new List< SolidCharacter >();
  658.     private string _text;
  659.  
  660.     #endregion Private
  661.  
  662.     #endregion Variables
  663.  
  664.     #region Constructor
  665.  
  666.     public SolidWord ( string text , Vector3 position , SolidLine parent )
  667.     {
  668.         _parent = parent;
  669.         MakeContainer( position , text );
  670.         Set( text );
  671.     }
  672.  
  673.     #endregion Constructor
  674. }
  675.  
  676. #endregion SolidWord class
  677.  
  678. #region SolidCharacter class
  679.  
  680. public class SolidCharacter
  681. {
  682.     #region Variables
  683.  
  684.     #region Getter/Setters
  685.  
  686.     public SolidText root
  687.     {
  688.         get
  689.         {
  690.             return parent.root;
  691.         }
  692.     }
  693.  
  694.     public Vector3 position
  695.     {
  696.         get
  697.         {
  698.             return container.transform.position;
  699.         }
  700.         set
  701.         {
  702.             container.transform.position = value;
  703.  
  704.             switch ( root.baseLine )
  705.             {
  706.                 case SolidType.BaseLine.Top:
  707.                     transform.position = container.transform.position + Vector3.up * bounds.extents.y * scale.y;
  708.                     break;
  709.  
  710.                 case SolidType.BaseLine.Bottom:
  711.                     transform.position = container.transform.position + Vector3.down * bounds.extents.y * scale.y;
  712.                     break;
  713.             }
  714.         }
  715.     }
  716.  
  717.     public Vector3 scale
  718.     {
  719.         get
  720.         {
  721.             return transform.localScale;
  722.         }
  723.         set
  724.         {
  725.             transform.localScale = value;
  726.         }
  727.     }
  728.  
  729.     #endregion Getter/Setters
  730.  
  731.     #region Getters
  732.  
  733.     public int index
  734.     {
  735.         get
  736.         {
  737.             return _index;
  738.         }
  739.     }
  740.  
  741.     public Mesh mesh
  742.     {
  743.         get
  744.         {
  745.             return _mesh;
  746.         }
  747.     }
  748.  
  749.     public SolidWord parent
  750.     {
  751.         get
  752.         {
  753.             return _parent;
  754.         }
  755.     }
  756.  
  757.     public float lineSpacing
  758.     {
  759.         get
  760.         {
  761.             return root.lineSpacing;
  762.         }
  763.     }
  764.  
  765.     public Transform container
  766.     {
  767.         get
  768.         {
  769.             return transform.parent;
  770.         }
  771.     }
  772.  
  773.     public SolidCharacter previous
  774.     {
  775.         get
  776.         {
  777.             return index > 0 ? parent.characters[ index - 1 ] : null;
  778.         }
  779.     }
  780.  
  781.     public SolidCharacter next
  782.     {
  783.         get
  784.         {
  785.             return parent.characters.Count - 1 > index ? parent.characters[ index + 1 ] : null;
  786.         }
  787.     }
  788.  
  789.     public Bounds bounds
  790.     {
  791.         get
  792.         {
  793.             mesh.RecalculateBounds();
  794.             return mesh.bounds;
  795.         }
  796.     }
  797.  
  798.     public float width
  799.     {
  800.         get
  801.         {
  802.             return scale.x;
  803.         }
  804.     }
  805.  
  806.     public float height
  807.     {
  808.         get
  809.         {
  810.             return scale.y;
  811.         }
  812.     }
  813.  
  814.     public float depth
  815.     {
  816.         get
  817.         {
  818.             return scale.z;
  819.         }
  820.     }
  821.  
  822.     public GameObject gameObject
  823.     {
  824.         get
  825.         {
  826.             return _gameObject;
  827.         }
  828.     }
  829.  
  830.     public Transform transform
  831.     {
  832.         get
  833.         {
  834.             return gameObject.transform;
  835.         }
  836.     }
  837.  
  838.     #endregion Getters
  839.  
  840.     #region Private
  841.  
  842.     public char _character;
  843.     public float _kerning;
  844.     public Mesh _mesh;
  845.  
  846.     public int _index;
  847.     public Quaternion _rotation;
  848.     public GameObject _gameObject;
  849.     public SolidWord _parent;
  850.  
  851.     #endregion Private
  852.  
  853.     #endregion Variables
  854.  
  855.     #region Constructors
  856.  
  857.     public SolidCharacter ( char character , GameObject gameObject , Vector3 scale , int index , SolidWord parent )
  858.     {
  859.         _index = index;
  860.         _parent = parent;
  861.         _character = character;
  862.         _gameObject = gameObject;
  863.         _mesh = SolidType.CharacterMesh( _parent.root.font , SolidUtils.CharToCharacter( character ) , gameObject );
  864.         transform.SetSiblingIndex( index );
  865.         transform.localScale = scale;
  866.     }
  867.  
  868.     #endregion Constructors
  869. }
  870.  
  871. #endregion SolidCharacter class
  872.  
  873. #region SolidType class
  874.  
  875. public static class SolidType
  876. {
  877.     #region Spawn character
  878.  
  879.     public static SolidCharacter SpawnCharacter ( Fonts font , char c , Vector3 scale , int index , SolidWord parent )
  880.     {
  881.         Characters charEnum = SolidUtils.CharToCharacter( c );
  882.  
  883.         if ( _charactersToPool > _characterPool[ font ][ charEnum ].Count )
  884.             FillCharacterPool( font , charEnum );
  885.  
  886.         Transform container = new GameObject( c.ToString() ).transform;
  887.         GameObject character = _characterPool[ font ][ charEnum ].Dequeue();
  888.         character.SetActive( true );
  889.  
  890.         container.transform.parent = parent.container;
  891.         character.transform.parent = container;
  892.         character.transform.position = container.position;
  893.         return new SolidCharacter( c , character , scale , index , parent );
  894.     }
  895.  
  896.     public static void DespawnCharacter ( SolidCharacter character )
  897.     {
  898.         Characters charEnum = ( Characters ) System.Enum.Parse( typeof( Characters ) , character.gameObject.name );
  899.         _characterPool[ character.root.font ][ charEnum ].Enqueue( character.gameObject );
  900.         character.transform.parent = _characterContainers[ character.root.font ][ charEnum ].transform;
  901.         character.transform.localRotation = Quaternion.identity;
  902.         character.transform.position = Vector3.zero;
  903.         character.transform.localScale = Vector3.one;
  904.         character.gameObject.SetActive( false );
  905.         character = null;
  906.     }
  907.  
  908.     #endregion Spawn character
  909.  
  910.     #region Character pool
  911.  
  912.     public static void CacheFontResources ( int charactersToPool )
  913.     {
  914.         if ( !resourcesCached )
  915.         {
  916.             _charactersToPool = charactersToPool;
  917.             string[] fonts = System.IO.Directory.GetDirectories("Assets/Resources/Prefabs/Fonts/" );
  918.             _poolContainer = new GameObject( "SolidType Pool" );
  919.  
  920.             for ( int i = 0 ; fonts.Length > i ; i++ )
  921.             {
  922.                 Fonts font = ( Fonts ) i;
  923.                 string path = fonts[ i ].TrimStart( "Assets/Resources/".ToCharArray() );
  924.                 GameObject fontContainer = new GameObject( ( font ).ToString());
  925.                 _fontContainers.Add( font , fontContainer );
  926.                 fontContainer.transform.parent = _poolContainer.transform;
  927.                 GameObject[] prefabs = Resources.LoadAll<GameObject>( path );
  928.                 _characterPrefabs.Add( font , prefabs );
  929.                 _characterPool.Add( font , new Dictionary<Characters , Queue<GameObject>>() );
  930.                 _characterContainers.Add( font , new Dictionary<Characters , GameObject>() );
  931.                 _characterMeshes.Add( font , new Dictionary<Characters , Dictionary<GameObject , Mesh>>() );
  932.  
  933.                 for ( int j = 0 ; _characterPrefabs[ font ].Length > j ; j++ )
  934.                 {
  935.                     Characters character = ( Characters ) System.Enum.Parse( typeof( Characters ) , _characterPrefabs[ font ][ j ].name );
  936.  
  937.                     if ( !_characterContainers[ font ].ContainsKey( character ) )
  938.                     {
  939.                         GameObject characterContainer = new GameObject( character.ToString() );
  940.                         _characterContainers[ font ].Add( character , characterContainer );
  941.                         characterContainer.transform.parent = fontContainer.transform;
  942.                         _characterPool[ font ].Add( character , new Queue<GameObject>() );
  943.                         _characterMeshes[ font ].Add( character , new Dictionary<GameObject , Mesh>() );
  944.  
  945.                         for ( int k = 0 ; _charactersToPool > k ; k++ )
  946.                         {
  947.                             GameObject newCharacter = Object.Instantiate( _characterPrefabs[ font ][ j ] );
  948.                             newCharacter.name = _characterPrefabs[ font ][ j ].name;
  949.                             newCharacter.transform.parent = characterContainer.transform;
  950.                             _characterPool[ font ][ character ].Enqueue( newCharacter );
  951.                             _characterMeshes[ font ][ character ].Add( newCharacter , newCharacter.GetComponent<MeshFilter>().mesh );
  952.                             newCharacter.SetActive( false );
  953.                         }
  954.                     }
  955.                 }
  956.             }
  957.  
  958.             _resourcesCached = true;
  959.         }
  960.     }
  961.  
  962.     public static void FillCharacterPool ( Fonts font , Characters character )
  963.     {
  964.         GameObject characterContainer = _characterContainers[ font ][ character ];
  965.  
  966.         for ( int i = 0 ; _charactersToPool > i ; i++ )
  967.         {
  968.             GameObject newCharacter = Object.Instantiate( _characterPrefabs[ font ][ ( int ) character ] );
  969.             newCharacter.name = _characterPrefabs[ font ][ ( int ) character ].name;
  970.             newCharacter.transform.parent = characterContainer.transform;
  971.             _characterPool[ font ][ character ].Enqueue( newCharacter );
  972.             _characterMeshes[ font ][ character ].Add( newCharacter , newCharacter.GetComponent<MeshFilter>().mesh );
  973.             newCharacter.SetActive( false );
  974.         }
  975.     }
  976.  
  977.     public static Mesh CharacterMesh ( Fonts font , Characters character , GameObject gameObject )
  978.     {
  979.         return _characterMeshes[ font ][ character ][ gameObject ];
  980.     }
  981.  
  982.     #endregion Character pool
  983.  
  984.     #region Variables
  985.  
  986.     #region Enums
  987.  
  988.     public enum Alignment
  989.     {
  990.         Left = 0,
  991.         Center = 1,
  992.         Right = 2
  993.     }
  994.  
  995.     public enum BaseLine
  996.     {
  997.         Top = 0,
  998.         Center = 1,
  999.         Bottom = 2
  1000.     }
  1001.  
  1002.     public enum Fonts
  1003.     {
  1004.         Test
  1005.     }
  1006.  
  1007.     public enum Characters
  1008.     {
  1009.         LOW_A,
  1010.         LOW_B,
  1011.         LOW_C,
  1012.         LOW_D,
  1013.         LOW_E,
  1014.         LOW_F,
  1015.         LOW_G,
  1016.         LOW_H,
  1017.         LOW_I,
  1018.         LOW_J,
  1019.         LOW_K,
  1020.         LOW_L,
  1021.         LOW_M,
  1022.         LOW_N,
  1023.         LOW_O,
  1024.         LOW_P,
  1025.         LOW_Q,
  1026.         LOW_R,
  1027.         LOW_S,
  1028.         LOW_T,
  1029.         LOW_U,
  1030.         LOW_V,
  1031.         LOW_W,
  1032.         LOW_X,
  1033.         LOW_Y,
  1034.         LOW_Z,
  1035.         NUM_0,
  1036.         NUM_1,
  1037.         NUM_2,
  1038.         NUM_3,
  1039.         NUM_4,
  1040.         NUM_5,
  1041.         NUM_6,
  1042.         NUM_7,
  1043.         NUM_8,
  1044.         NUM_9,
  1045.         SYM_ASTERISK,
  1046.         SYM_COLON,
  1047.         SYM_COMMA,
  1048.         SYM_DOUBLEQUOTE,
  1049.         SYM_EXCLAMATION,
  1050.         SYM_HYPHEN,
  1051.         SYM_PERIOD,
  1052.         SYM_QUESTION,
  1053.         SYM_SEMICOLON,
  1054.         SYM_SINGLEQUOTE,
  1055.         UPP_A,
  1056.         UPP_B,
  1057.         UPP_C,
  1058.         UPP_D,
  1059.         UPP_E,
  1060.         UPP_F,
  1061.         UPP_G,
  1062.         UPP_H,
  1063.         UPP_I,
  1064.         UPP_J,
  1065.         UPP_K,
  1066.         UPP_L,
  1067.         UPP_M,
  1068.         UPP_N,
  1069.         UPP_O,
  1070.         UPP_P,
  1071.         UPP_Q,
  1072.         UPP_R,
  1073.         UPP_S,
  1074.         UPP_T,
  1075.         UPP_U,
  1076.         UPP_V,
  1077.         UPP_W,
  1078.         UPP_X,
  1079.         UPP_Y,
  1080.         UPP_Z
  1081.     }
  1082.  
  1083.     #endregion Enums
  1084.  
  1085.     #region Getters
  1086.  
  1087.     public static bool resourcesCached
  1088.     {
  1089.         get
  1090.         {
  1091.             return _resourcesCached;
  1092.         }
  1093.     }
  1094.  
  1095.     #endregion Getters
  1096.  
  1097.     #region Private
  1098.  
  1099.     private static Dictionary< Fonts , Dictionary< Characters , Queue< GameObject > > > _characterPool = new Dictionary< Fonts, Dictionary< Characters , Queue< GameObject > > >();
  1100.     private static Dictionary< Fonts , Dictionary< Characters , Dictionary< GameObject , Mesh > > > _characterMeshes = new Dictionary<Fonts, Dictionary<Characters, Dictionary<GameObject, Mesh>>>();
  1101.     private static Dictionary< Fonts , GameObject[] > _characterPrefabs = new Dictionary< Fonts , GameObject[] >();
  1102.     private static Dictionary< Fonts , GameObject > _fontContainers = new Dictionary< Fonts , GameObject >();
  1103.     private static Dictionary< Fonts , Dictionary< Characters , GameObject > > _characterContainers = new Dictionary< Fonts , Dictionary< Characters , GameObject > >();
  1104.     private static GameObject _poolContainer;
  1105.     private static bool _resourcesCached;
  1106.     private static int _charactersToPool;
  1107.  
  1108.     #endregion Private
  1109.  
  1110.     #endregion Variables
  1111. }
  1112.  
  1113. #endregion SolidType class
  1114.  
  1115. #region SolidAnimation class
  1116.  
  1117. public static class SolidAnimation
  1118. {
  1119.     public static GoTweenFlow VerticalScale_Enter ()
  1120.     {
  1121.         GoTweenFlow flow = new GoTweenFlow();
  1122.  
  1123.         return flow;
  1124.     }
  1125.  
  1126.     public static void VerticalScaleUp ( SolidText text )
  1127.     {
  1128.         List< SolidCharacter > characters = SolidUtils.GetCharacters( text );
  1129.         GoTweenFlow flow = new GoTweenFlow();
  1130.         float time = 0f;
  1131.         float increment = 0.05f;
  1132.         float duration = 0.2f;
  1133.  
  1134.         for ( int i = 0 ; characters.Count > i ; i++ )
  1135.         {
  1136.             flow.insert( time , new GoTween( characters[ i ].container , duration , new GoTweenConfig().scale( 1 ).setEaseType( GoEaseType.BackOut ) ) );
  1137.             time += increment;
  1138.             increment *= 0.975f;
  1139.             duration *= 1.05f;
  1140.         }
  1141.  
  1142.         flow.play();
  1143.     }
  1144.  
  1145.     public static void VerticalScaleDown ( SolidText text )
  1146.     {
  1147.         List< SolidCharacter > characters = SolidUtils.GetCharacters( text );
  1148.         GoTweenFlow flow = new GoTweenFlow();
  1149.         float time = 0f;
  1150.         float increment = 0.05f;
  1151.         float duration = 0.5f;
  1152.  
  1153.         for ( int i = 0 ; characters.Count > i ; i++ )
  1154.         {
  1155.             flow.insert( time , new GoTween( characters[ i ].container , duration , new GoTweenConfig().scale( new Vector3( characters[ i ].container.localScale.x , 0 , characters[ i ].container.localScale.z ) ).setEaseType( GoEaseType.BackIn ) ) );
  1156.             time += increment;
  1157.             increment *= 0.975f;
  1158.             duration *= 0.975f;
  1159.         }
  1160.  
  1161.         flow.play();
  1162.     }
  1163. }
  1164.  
  1165. #endregion SolidAnimation class
  1166.  
  1167. #region SolidHelper class
  1168.  
  1169. public static class SolidUtils
  1170. {
  1171.     public static List<SolidCharacter> GetCharacters ( SolidText text )
  1172.     {
  1173.         List< SolidCharacter > characters = new List<SolidCharacter>();
  1174.  
  1175.         for ( int i = 0 ; text.lines.Count > i ; i++ )
  1176.             for ( int j = 0 ; text.lines[ i ].words.Count > j ; j++ )
  1177.                 characters.AddRange( text.lines[ i ].words[ j ].characters );
  1178.  
  1179.         return characters;
  1180.     }
  1181.  
  1182.     public static void CharacterYScaleZero ( List<SolidCharacter> characters )
  1183.     {
  1184.         for ( int i = 0 ; characters.Count > i ; i++ )
  1185.             characters[ i ].container.localScale = new Vector3( characters[ i ].container.localScale.x , 0 , characters[ i ].container.localScale.z );
  1186.     }
  1187.  
  1188.     public static SolidType.Characters CharToCharacter ( char character )
  1189.     {
  1190.         if ( char.IsLetterOrDigit( character ) )
  1191.             return ( SolidType.Characters ) System.Enum.Parse( typeof( SolidType.Characters ) , ( char.IsLetter( character ) ? ( char.IsUpper( character ) ? "UPP_" : "LOW_" ) : "NUM_" ) + char.ToUpper( character ) );
  1192.         else
  1193.         {
  1194.             switch ( character )
  1195.             {
  1196.                 case '*':
  1197.                     return SolidType.Characters.SYM_ASTERISK;
  1198.  
  1199.                 case ':':
  1200.                     return SolidType.Characters.SYM_COLON;
  1201.  
  1202.                 case ',':
  1203.                     return SolidType.Characters.SYM_COMMA;
  1204.  
  1205.                 case '"':
  1206.                     return SolidType.Characters.SYM_DOUBLEQUOTE;
  1207.  
  1208.                 case '!':
  1209.                     return SolidType.Characters.SYM_EXCLAMATION;
  1210.  
  1211.                 case '-':
  1212.                     return SolidType.Characters.SYM_HYPHEN;
  1213.  
  1214.                 case '.':
  1215.                     return SolidType.Characters.SYM_PERIOD;
  1216.  
  1217.                 case '?':
  1218.                     return SolidType.Characters.SYM_QUESTION;
  1219.  
  1220.                 case ';':
  1221.                     return SolidType.Characters.SYM_SEMICOLON;
  1222.  
  1223.                 case '\'':
  1224.                     return SolidType.Characters.SYM_SINGLEQUOTE;
  1225.  
  1226.                 default:
  1227.                     return SolidType.Characters.SYM_QUESTION;
  1228.             }
  1229.         }
  1230.     }
  1231. }
  1232.  
  1233. #endregion SolidHelper class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement