SpatialFear.SFTextWindow

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
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
//=============================================================================
// Spatial Fear
// Name: SFTextWindow
// Description: Sub class of SFWindow for a  Spatial Fear text output windows.
//
// Author: Markus Nuebel
//=============================================================================

#exec AUDIO IMPORT FILE="Sounds\misc\speechtick.WAV" NAME="SpeechTick" GROUP="Misc"

class SFTextWindow extends SFWindow;

var bool				m_bRunningText; // Private: Indicates, if the text will be displayed as a running text.
var bool				m_bUsedRunningText; // Private flag, to indicate that this window has run a running text before. Default: false
var int					m_nTextLength; // Private: Length of text to display, used by a running text to determine the end of the animation.
var int					m_nTextPos; // Private: Currently displayed text length, used by a running text to determine the end of the animation.
var int					m_nTextOffset; // Private: Text offset form the top of the window
var int					m_nTextIndent; // Private: Text offset form the left border of the window
var int					m_nTextIndentR;
var int					m_nTextOffsetB;
var float				m_fIntervall; // Private: Intervall used by the running text timer
var Sound				m_sndTick; // Private: Tick sound for running text.
var Region				m_ScaledClippingRegion; // Private: Clipping region on screen, scaled to HUD size
var localized string	m_strText; // Private: Text to render in window
var localized string	m_strDisplayText; // Private: Text that currently is shown in window


// Called during each frame to prepare the cancas
simulated function prepareCanvas(Canvas C)
{
	// Call to base class
	Super.prepareCanvas(C);

	switch(FontSize)
	{
	case FS_Small:
		C.Font		= m_Fonts.GetSmallFont(C.ClipX);
		break;
	case FS_Medium:
		C.Font		= m_Fonts.GetMediumFont(C.ClipX);
		break;
	case FS_Big:
		C.Font		= m_Fonts.GetBigFont(C.ClipX);
		break;
	default:
		C.Font		= m_Fonts.GetSmallFont(C.ClipX);
	}
    C.Style     = ERenderStyle.STY_Translucent;
}

// Called during each frame to render the window.
simulated function renderForeGround(Canvas C)
{
	// Call to base class
	Super.renderForeGround(C);

	// Check, if we want to display the whole text, or the running text
	if(m_bRunningText)
		renderTextArea(C, int(m_XCur), int(m_YCur), m_nWidth, m_nHeight, m_strDisplayText);
	else
		renderTextArea(C, int(m_XCur), int(m_YCur), m_nWidth, m_nHeight, m_strText);

     //class'sfUtils'.static.sfLogEx(15, "SFTextWindow - render - m_XCur " $ int(m_XCur) $ " m_YCur " $ int(m_YCur)$  " m_X " $ m_X$  " m_Y " $ m_Y $ "m_nWidht: " $ m_nWidth $ " m_nHeight: " $ m_nHeight $ " m_strDisplayText " $ m_strDisplayText);
}

// Stops the runnint text by killing the timer
simulated function stopRunning()
{
	SetTimer(-1.0, false);
	m_bRunningText=false;
}

// Called during running text display;
simulated function Timer()
{

	// Check if visible
	if(!isVisible())
	{
		m_bRunningText=false;
		return;
	}

	// Increment text pos
	m_nTextPos++;

	// 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)
	{
		SetTimer(m_fIntervall, false);
		// Add a dot to the current text output
		if(0 == (m_nTextPos%2))
		{
			// Blinking cursor
			m_strDisplayText = m_strDisplayText $ " .";
			// Play sound
			if(None != m_oPawnOwner)
				m_oPawnOwner.PlaySound(m_sndTick,,2.0);
		}

	}
	else
	{
		SetTimer(3.0, false);

		m_bRunningText=false;

	}
}

simulated function hide()
{
	Super.hide();

	m_bUsedRunningText=false;

}
// Clear the current running text parameters
simulated function clearRunningText()
{
	m_strText			= "";
	m_strDisplayText	= "";
}

// Prepares the text window for a running text
simulated function setRunningText(string strText, float fIntervall)
{
	// Save text
	m_bUsedRunningText	= true;
	m_strText			= strText;
	m_strDisplayText	= "";
	m_nTextLength		= Len(m_strText);
	m_nTextPos			= 0;
	m_fIntervall		= fIntervall;

	// Check for current running text
	if(m_bRunningText)
		stopRunning();

	// Set the flag
	m_bRunningText = true;

	// Start timer
	SetTimer(m_fIntervall, false);
}

// Resets a running text and starts all over again
simulated function resetRunningText(optional string strText)
{
	// Save text
	m_strDisplayText	= "";
	if("" != strText)
		m_strText	= strText;
	m_nTextLength		= Len(m_strText);
	m_nTextPos			= 0;

	// Set the flag
	m_bRunningText = true;

	// Start timer
	SetTimer(m_fIntervall, false);
}

// Renders a clipped text region.
// All parameters must be absolut screen coordinates based on a 1280x1024 screen resoultion
// scaling will be done inside, so make sure you do not passed values calculsted with C.ClipX or C.ClipY
// This would screw up you display.
simulated function renderTextArea(Canvas C, int X, int Y, int nWidth, int nHeight, string strText)
{
     local     int XScaled, YScaled, nWidthScaled, nHeightScaled;

     // First scale the inputs
     XScaled        = X*m_fHUDScale + m_nTextIndent*m_fHUDScale;
     YScaled        = Y*m_fHUDScale + m_nTextOffset*m_fHUDScale;
     nWidthScaled	= nWidth*m_fHUDScale - m_nTextIndentR*m_fHUDScale;
     nHeightScaled  = nHeight*m_fHUDScale;

	 // class'SfUtils'.static.sfLogEx(15, "SFTextWindow - renderTextArea: X" $  XScaled $ " Y" $ m_YCurScaled $  " Width" $ nWidthScaled $  " Height" $ nHeightScaled);

     // First set up the clipping region
     m_ScaledClippingRegion.X = XScaled;
     m_ScaledClippingRegion.Y = YScaled;
     m_ScaledClippingRegion.W = nWidthScaled - m_nTextIndent*m_fHUDScale;
     m_ScaledClippingRegion.H = nHeightScaled;

     // Draw the clipped text
     clipTextArea(C, XScaled, YScaled, strText);
}

// Renders text into a clipped area
// X and Y must be already scaled to the HUD
// The clipping region is determind by the Region: m_ScaledClippingRegion, which is
// already scaled to the HUD
simulated function clipTextArea(Canvas C, int X, int Y, string strText)
{
     local string     strStart, strEnd, strLine, strLine2, strValid, strChar;
     local int          nCharsPerLine;
     local int          nLength, pos, posValid, maxChar;
     local int          nCharLeft;
     local int          nLineLength, nLineLength2;
     local bool          bQuit;
     local float          XL, YL, XL2, YL2;
     local float          fClipWidth;

     bQuit          = false;
     nLength          = Len(strText);
     nCharLeft     = nLength;
     strStart     = strText;
     fClipWidth     = m_ScaledClippingRegion.W;

     // Retrieve text height
     C.StrLen( "F", XL, YL );

     // Calculate number of chars per line
     nCharsPerLine     = int(fClipWidth / XL);

     while((0 < Len(strStart)) && (!bQuit))
     {
          strLine = "";
          // Retrieve next line
          pos = InStr(strStart, "\\n");
          if(-1 != pos)
          {
               strLine = Left(strStart, pos);
          }
          else
               strLine = strStart;

          nLineLength     = Len(strLine);

          // Try to render one line
         C.TextSize( strLine , XL, YL );
//          if( XL < fClipWidth)
          if( nLineLength <= nCharsPerLine)
          {
               // Render complete line
               ClipText(C, X, Y, strLine);
               if(Len(strLine) > 0)
                    Y += YL;

			   // No need to render past the lower boundary
			   if ( (Y+YL) > (m_YCurScaled+m_nHeightScaled))
			   { 
				   strLine2="";
					bQuit = true;
			   }
          }
          else
          {

               // Copy string
               strLine2          = strLine;
               while((Len(strLine2) > 0 )  && (!bQuit))
               {
                    nLineLength2     = Len(strLine2);
                    maxChar          = nCharsPerLine;
                    strValid          = Left(strLine2, nCharsPerLine);
                    // Find last matching space
                    while((Right(strValid, 1) != " " ) && (maxChar>=0) && (nCharsPerLine < nLineLength2))
                    {
                         maxChar--;
                         strValid = Left(strValid, maxChar);
                    }
                    if(0 < maxChar)
                    {
                         // Render line partly
                         ClipText(C, X, Y, strValid);
                         // Update resulting string
                         strLine2 = Right(strLine2, nLineLength2-maxChar);
                    }
                    else
                    {
						// Cut the line
	                    strValid    = Left(strLine2, nCharsPerLine);
                        // Update resulting string
						strLine2	= Right(strLine2, nLineLength2-nCharsPerLine);
						strLine2	= TrimSpaces(strLine2);
                        ClipText(C, X, Y, strValid);
                    }
                    Y+=YL;

					 // No need to render past the lower boundary
					 if ( (Y+YL) > (m_YCurScaled+m_nHeightScaled-YL))
					 {
						 bQuit = true;
					 }

               }
          }


          // Remove rendered line from text
          strStart     = Right(strStart, (nCharLeft - nLineLength-2));
          nCharLeft     -= Len(strLine)+2;

     }

	// Check if we have run out of screen space
	if (bQuit)
	{
		// Call event function
//		EndOfWidowReached(strLine2);
		EndOfWidowReached(Left(strValid, Len(strValid)-1));
	}

}

// Just a copy of the window ClipText function,
// so it can be used here
simulated function ClipText(Canvas C, float X, float Y, coerce string S, optional bool bCheckHotkey)
{
     local float OrgX, OrgY, ClipX, ClipY;

     OrgX = C.OrgX;
     OrgY = C.OrgY;
     ClipX = C.ClipX;
     ClipY = C.ClipY;

     C.SetOrigin(OrgX + m_ScaledClippingRegion.X, OrgY + m_ScaledClippingRegion.Y);
     C.SetClip(m_ScaledClippingRegion.W, m_ScaledClippingRegion.H);

     C.SetPos((X - m_ScaledClippingRegion.X), (Y - m_ScaledClippingRegion.Y));
     C.DrawTextClipped(S, bCheckHotKey);

     C.SetClip(ClipX, ClipY);
     C.SetOrigin(OrgX, OrgY);
}

// Called, when there is no more space on the screen to
// write another line
simulated event EndOfWidowReached(optional string strRestOfLine)
{
    // class'sfUtils'.static.sfLogEx(5, "SFTextWindow - EndOfWidowReached:  Position:" $ int(m_YCur) $ " calling event function");

	// Kill running text
	if(m_bRunningText)
	{
		m_nTextPos=9999999;
	}
}

defaultproperties
{
     m_sndTick=Sound'SpatialFear.Misc.SpeechTick'
}

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