# SpatialFear.SFTextParser

00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 00024 00025 00026 00027 00028 00029 00030 00031 00032 00033 00034 00035 00036 00037 00038 00039 00040 00041 00042 00043 00044 00045 00046 00047 00048 00049 00050 00051 00052 00053 00054 00055 00056 00057 00058 00059 00060 00061 00062 00063 00064 00065 00066 00067 00068 00069 00070 00071 00072 00073 00074 00075 00076 00077 00078 00079 00080 00081 00082 00083 00084 00085 00086 00087 00088 00089 00090 00091 00092 00093 00094 00095 00096 00097 00098 00099 00100 00101 00102 00103 00104 00105 00106 00107 00108 00109 00110 00111 00112 00113 00114 00115 00116 00117 00118 00119 00120 00121 00122 00123 00124 00125 00126 00127 00128 00129 00130 00131 00132 00133 00134 00135 00136 00137 00138 00139 00140 00141 00142 00143 00144 00145 00146 00147 00148 00149 00150 00151 00152 00153 00154 00155 00156 00157 00158 00159 00160 00161 00162 00163 00164 00165 00166 00167 00168 00169 00170 00171 00172 00173 00174 00175 00176 00177 00178 00179 00180 00181 00182 00183 00184 00185 00186 00187 00188 00189 00190 00191 00192 00193 00194 00195 00196 00197 00198 00199 00200 00201 00202 00203 00204 00205 00206 00207 00208 00209 00210 00211 00212 00213 00214 00215 00216 00217 00218 00219 00220 00221 00222 00223 00224 00225 |
    //=============================================================================
    // Spatial Fear
    // Name: SFTextParser
    // Description: Generic text parser
    //
    // Author: Markus Nuebel
    //=============================================================================

    class SFTextParser extends SFObject;

    var String	m_strText;
    var String	m_strDisplayText;
    var int		m_nTextPos;
    var int		m_nTextLength;
    var actor	m_actOwner;

    // Inits the parser
    simulated function init(String strText)
    {
    	m_strText		= strText;
    	m_nTextPos		= 0;
    	m_nTextLength	= Len(m_strText);
    }

    // Cleanup
    simulated function terminate()
    {
    }

    // Advances to the end of the text and fills
    // the display text with all of the input text
    simulated function advanceToEnd()
    {
    	m_nTextPos			= m_nTextLength-1;
    	m_strDisplayText	= Left(m_strText, m_nTextPos);
    }

    // Extracts the next char from the original text
    simulated  function string peekChar()
    {
    	return (Mid(m_strText, m_nTextPos-1, 1));
    }

    // Checks, wheather there is currently a macro starting
    simulated function bool isMacroStart()
    {
    	return (peekChar() == "<");
    }

    // Checks, wheather there is currently a macro ending
    simulated function bool isMacroEnd()
    {
    	return (peekChar() == ">");
    }

    // Checks, wheather there is currently a macro parameter delimiter
    simulated function bool isMacroParameter()
    {
    	return (peekChar() == ":");
    }

    // Macro function to clear the screen
    simulated function executeCLS()
    {
    	m_strText		= Right(m_strText, Len(m_strText)-m_nTextPos);

    	m_nTextLength	= Len(m_strText);
    	m_nTextPos		= 0;
    }

    // Macro to play a sound effect
    simulated function	executeSound(string strParameter)
    {
    	local Sound	sndEffect;
    	sndEffect	= Sound(DynamicLoadObject(strParameter, class'Sound'));
    	if(None == m_actOwner)
    		PlaySound(sndEffect,SLOT_Talk);
    	else
    		m_actOwner.PlaySound(sndEffect,SLOT_Talk);
    }

    simulated function bool executeMacro(string strMacro, string strParameter, string strParameter2)
    {
    	// Check for known macros
    	if(strMacro == "cls")
    	{
    		// Call clear screen macro
    		executeCLS();
    	}
    	else
    	if(strMacro == "sound")
    	{
    		executeSound(strParameter);
    	}
    	else
    		class'SFUtils'.static.sfError( "SFTextParser - executeMacro: Unknown macro: " $ strMacro);

    	return false;
    }

    // Checks the current character for a macro sequence
    simulated function bool checkChar()
    {
    	local string	strMacro;
    	local string	strParameter;
    	local string	strParameter2;
    	local string	strChar;
    	local string	strLeft, strRight;
    	local int		nMax;
    	local int		nLeft, nRight;
    	local bool		bRet;
    	local bool		bInParameter1;
    	local bool		bInParameter2;

    	// Some inits
    	bRet			= false;
    	bInParameter1	= false;
    	bInParameter2	= false;
    	nMax			= Len(m_strText);

    	// Check for macro start
    	if(isMacroStart())
    	{
    		// Save pos to extract macro later
    		nLeft = m_nTextPos-1;

    		m_nTextPos++;
    		while(!isMacroEnd() && (m_nTextPos<=nMax))
    		{
    			// Extract the macro and it's paramters
    			if(!bInParameter1)
    			{
    				// Still reading macro tag
    				if(!isMacroParameter())
    					strMacro = strMacro $ peekChar();
    				else
    					bInParameter1 = true;
    			}
    			else
    			{
    				if(!bInParameter2)
    				{
    					// Still reading macro parameter1
    					if(!isMacroParameter())
    						// Already parsing the parameter
    						strParameter	= strParameter $ peekChar();
    					else
    						bInParameter2 = true;
    				}
    				else
    					strParameter2	= strParameter2 $ peekChar();

    			}

    			m_nTextPos++;
    		}
    		nRight = m_nTextPos-1;

    		// class'SFUtils'.static.sfLogEx(15, SFTextParser - checkChar: "MACRO: " $ strMacro $ " Param: " $ strParameter $ " nLeft: " $ nLeft $ " nRight: " $ nRight $ " Size: " $ nRight-nLeft+1);

    		// Extract left and right text part, next to the macro
    		strLeft			= Left (m_strText, nLeft);
    		strRight		= Right(m_strText, nMax-nRight-1);
    		// Build up new text
    		m_strText		= strLeft $ strRight;
    		m_nTextLength	= Len(m_strText);
    		m_nTextPos		= m_nTextPos-(nRight-nLeft+1);

    		//class'sfUtils'.static.sfLogEx(5, "SFTextParser - checkChar: strText: " $ m_strText $ " length " $ m_nTextLength $ " pos " $ m_nTextPos);
    		// Execute the macro
    		//class'sfUtils'.static.sfLogEx(5, "SFTextParser - checkChar: macro: " $ strMacro $ " " $ strParameter $ " " $ strParameter2);
    		executeMacro(strMacro, strParameter, strParameter2);

    	}

    	return bRet;
    }

    // Called every time to process a new char
    simulated function bool process()
    {
    	local string strChar;
    	local string strMacro;

    	// Increment text pos
    	m_nTextPos++;

    	if(checkChar())
    		return false;

    	// Add one more character
    	if(Mid(m_strText, m_nTextPos-1, 1) == "\\")
    		m_strDisplayText = Left(m_strText, m_nTextPos-1);
    	else
    		m_strDisplayText = Left(m_strText, m_nTextPos);

    	// Check, if there is a need for a new timer run
    	if(m_nTextLength >= m_nTextPos)
    		return false;
    	else
    		return true;
    }

    // Fires a normal event
    function FireEvent(name nameEvent, actor Other)
    {
    	local actor A;
    	// Broadcast the Trigger message to all matching actors.
    	if( nameEvent != '' )
    		foreach AllActors( class 'Actor', A, nameEvent )
    		{
    			A.Trigger( Other, Pawn(Owner) );
    		}
    }

    defaultproperties
    {
    }

class file time: 12/7/2003 3:58:24 PM - creation time: 12/7/2003 4:03:45 PM
Created with UnCodeX