Subversion Repositories spk

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 cycrow 1
//#include "stdafx.h"
2
#include "DisplayCustom.h"
3
#include "RenderText.h"
4
#include "RenderTexture.h"
5
#include "RenderSprite.h"
6
//#include "RenderVideo.h"
7
 
8
 
9
CDisplayCustom::CDisplayCustom ( COverlay *overlay, MyDirect3DDevice9 *device, CyString filename, CyString name, int width, int height, CyStringList *varibles ) : CDisplay ( overlay, device, name )
10
{
11
	m_iMaxAlpha = 255;
12
	m_iFadeIn = 0;
13
 
14
	SStringList *curVari = varibles->Head();
15
 
16
	FILE *id;
17
 
18
	int sizeX = -1, sizeY = -1;
19
 
20
	if ( !fopen_s ( &id, filename.c_str(), "r" ) )
21
	{
22
		CyString section;
23
		while ( !feof ( id ) )
24
		{
25
			CyString line;
26
			line.GetEndOfLine ( id, NULL, false );
27
 
28
			if ( line.Empty() )
29
				continue;
30
			if ( line.Left(2) == "//" )
31
				continue;
32
 
33
			line = line.GetToken ( "//", 1, 1 );
34
 
35
			// first, find if its a block
36
			if ( line.IsIn ( "{" ) )
37
			{
38
				// parse the block
39
				CyStringList strings;
40
 
41
				CyString cmd = ParseBlock ( id, &line, &strings );
42
 
43
				// parse strings for varibles
44
				for ( SStringList *it = strings.Head(); it; it = it->next )
45
				{
46
					if ( !it->str.IsIn ( "$" ) )
47
						continue;
48
					it->str = ParseVaribles ( it->str, varibles );
49
				}
50
 
51
				CRenderObject *ro = NULL;
52
				if ( cmd.Left(5) == "FUNC_" )
53
				{
54
					SFunction *func = new SFunction;
55
					func->sFunction = cmd.upper();
56
					func->sFunction.Erase ( 0, 5 );
57
					func->sBlock = line;
58
					func->lStrings = strings;
59
 
60
					// now get the varibles passed
61
					if ( func->sFunction.IsIn ( "(" ) )
62
					{
63
						CyString vari = func->sFunction.GetToken ( "(", 2 ).GetToken ( ")", 1, 1 );
64
						func->sFunction = func->sFunction.GetToken ( "(", 1, 1 );
65
 
66
						int num = 0;
67
						vari.RemoveChar ( ' ' );
68
						CyString *varis = vari.SplitToken ( ',', &num );
69
						for ( int i = 0; i < num; i++ )
70
							func->lVaribles.PushBack ( CyString("$") + varis[i] + "$" );
71
					}
72
 
73
					m_lFunctions.push_back ( func );
74
				}
75
				else if ( cmd == "TEXTSMALL" )
76
					ro = (CRenderObject *)new CRenderText ( 20 );
77
				else if ( cmd == "TEXTMEDIUM" )
78
					ro = (CRenderObject *)new CRenderText ( 35 );
79
				else if ( cmd == "TEXTLARGE" )
80
					ro = (CRenderObject *)new CRenderText ( 50 );
81
				else if ( cmd == "TEXTHUGE" )
82
					ro = (CRenderObject *)new CRenderText ( 80 );
83
				else if ( cmd == "TEXTCUSTOM" )
84
					ro = (CRenderObject *)new CRenderText ( -1 );
85
				else if ( cmd == "TEXTURE" )
86
					ro = (CRenderObject *)new CRenderTexture ( device );
87
//				else if ( cmd == "VIDEO" )
88
//					ro = (CRenderObject *)new CRenderVideo ();
89
				else if ( cmd == "SPRITE" )
90
				{
91
					ro = (CRenderObject *)new CRenderSprite ( device );
92
					if ( !((CRenderSprite *)ro)->Init() )
93
						ro = NULL;
94
				}
95
				else if ( (cmd == "BORDER") || (cmd == "INPUT") || (cmd == "CLOSEON") )
96
				{
97
					SCloseOn *close = NULL;
98
					if ( cmd == "BORDER" )
99
						m_bNoBorder = false;
100
					else if ( cmd == "CLOSEON" )
101
					{				
102
						close = new SCloseOn;
103
						m_lClose.push_back ( close );
104
					}
105
 
106
					int num = 0;
107
					CyString *str = line.SplitToken ( ';', &num );
108
					for ( int i = 0; i < num; i++ )
109
					{
110
						CyString c = str[i].GetToken ( ":", 1, 1 );
111
						CyString r = str[i].GetToken ( ":", 2 );
112
						if ( r.IsIn("\"") )
113
							r = ReplaceString ( r, &strings );
114
						r = ParseVaribles (r, varibles);
115
						if ( cmd == "BORDER" )
116
							ParseBorder ( c, r );
117
						else if ( cmd == "INPUT" )
118
							ParseInput ( c, r );
119
						else if ( cmd == "CLOSEON" )
120
							ParseCloseEvent ( close, c, r );
121
					}
122
				}
123
 
124
				if ( ro )
125
				{
126
					ro->SetDevice ( device );
127
 
128
					DoBlock ( ro, line, &strings, varibles, section );
129
 
130
					AddObject ( ro );
131
 
132
					ro->FinishedLoad();
133
				}
134
			}
135
			else
136
			{
137
				CyString cmd = line.GetToken ( 1, ' ' ).ToUpper();
138
				CyString rest = ParseVaribles ( line.GetToken ( 2, -1, ' ' ), varibles );
139
 
140
				if ( (cmd == "VARIABLE:") || (cmd == "VARIBLE:") )
141
				{
142
					if ( curVari )
143
					{
144
						curVari->data = CyString("$") + rest + "$";
145
						curVari = curVari->next;
146
					}
147
				}
148
				else if ( cmd == "SOUND:" )
149
					CreateSound ( rest.GetToken ( ",", 2, 2 ), ParseNumber ( rest.GetToken ( ",", 1, 1 ) ) );
150
				else if ( line.lower() == "noborder" )
151
					m_bNoBorder = true;
152
				else if ( cmd == "DISPLAYFOR:" )
153
				{
154
					m_lTimeout = ParseNumber ( rest );
155
					if ( m_lTimeout != -1 )
156
						m_lTimeout += timeGetTime();
157
				}
158
				else if ( cmd == "TEXTURE:" )
159
				{
160
					CRenderTexture *t = new CRenderTexture ( device );
161
					t->OpenTextureFile ( rest.GetToken ( 3, ',' ).RemoveFirstSpace() );
162
					t->SetPosition ( ParseNumber ( rest.GetToken ( 1, ',' ) ), ParseNumber ( rest.GetToken ( 2, ',' ) ) );
163
					AddObject ( t );
164
				}
165
				else if ( cmd == "SIZE:" )
166
				{
167
					if ( rest.lower() == "full" )
168
					{
169
						sizeX = width;
170
						sizeY = height;
171
					}
172
					else
173
					{
174
						sizeX = ParseNumber ( rest.GetToken ( 1, ',' ) );
175
						sizeY = ParseNumber ( rest.GetToken ( 2, ',' ) );
176
					}
177
 
178
					varibles->PushBack ( CyString::Number(sizeX), CyString("$GUI.SIZEX$") );
179
					varibles->PushBack ( CyString::Number(sizeY), CyString("$GUI.SIZEY$") );
180
				}
181
			}
182
		}
183
 
184
		fclose ( id );
185
	}
186
 
187
	if ( (sizeX != -1) && (sizeY != -1) )
188
		SetSize ( sizeX, sizeY, width, height );
189
	else
190
		SetSize ( 520, 300, width, height );
191
 
192
	if ( !m_iFadeIn )
193
		m_iAlpha = m_iMaxAlpha;
194
	else
195
		m_iAlpha = 0;
196
}
197
 
198
CyString CDisplayCustom::ParseBlock ( FILE *id, CyString *str, CyStringList *strings )
199
{
200
	strings->Clear();
201
 
202
	CyString rest;
203
	CyString line = *str;
204
	int depth = -1;
205
	while ( !feof(id) )
206
	{
207
		if ( (line.Left(2) == "//") || (line.Empty()) )
208
		{
209
			line.GetEndOfLine ( id, NULL, false );
210
			continue;
211
		}
212
 
213
		line = line.GetToken ( "//", 1, 1 );
214
		line.RemoveChar ( 9 );
215
 
216
		if ( line.IsIn ( "{" ) )
217
			++depth;
218
		if ( line.IsIn ("}") )
219
		{
220
			if ( !depth )
221
			{
222
				rest += line.GetToken ( "}", 1, 1 );
223
				break;
224
			}
225
			else
226
			{
227
				rest += line.GetToken ( "}", 1, 1 );
228
				rest += "};";
229
				line = line.GetToken ( "}", 2 );
230
				--depth;
231
 
232
				if ( !line.Empty() )
233
				{
234
					line.GetEndOfLine ( id, NULL, false );
235
					continue;
236
				}
237
			}
238
		}
239
 
240
		rest += line;
241
		line.GetEndOfLine ( id, NULL, false );
242
	}
243
 
244
	CyString cmd = rest.GetToken ( "{", 1, 1 ).RemoveSpaces();
245
	rest = rest.GetToken ( "{", 2 );
246
 
247
	// parse strings
248
	CyString done;
249
	while ( rest.IsIn("\"") )
250
	{
251
		done += rest.GetToken ( "\"", 1, 1 );
252
		CyString s = rest.GetToken ( "\"", 2, 2 );
253
 
254
		if ( s.Empty() )
255
			break;
256
 
257
		strings->PushBack ( s );
258
		done += "\"";
259
		done += "$STRING";
260
		done += (long)strings->Count();
261
		done += "\"";
262
 
263
		rest = rest.GetToken ( "\"", 3 );
264
	}
265
 
266
	if ( !rest.Empty() )
267
		done += rest;
268
	done.RemoveChar ( ' ' );
269
 
270
	*str = done;
271
	return cmd;
272
}
273
 
274
CyString CDisplayCustom::ReplaceString ( CyString s, CyStringList *strings )
275
{
276
	int num = 1;
277
	for ( SStringList *it = strings->Head(); it; it = it->next, num++ )
278
	{
279
		CyString str = CyString("\"$STRING") + (long)num + "\"";
280
		CyString newstr = it->str;
281
		newstr = newstr.FindReplace ( ",", "$COMMA$" );
282
		s = s.FindReplace ( str, newstr );
283
	}
284
 
285
 
286
	return s;
287
}
288
 
289
void CDisplayCustom::ParseBorder ( CyString cmd, CyString rest )
290
{
291
	cmd.ToUpper();
292
	if ( cmd == "TEXTURE" )
293
		SetBorderTexture ( rest );
294
	else if ( cmd == "ALPHA" )
295
		m_iMaxAlpha = ParseNumber ( rest );
296
	else if ( cmd == "FADEIN" )
297
		m_iFadeIn = ParseNumber ( rest );
298
	else if ( cmd == "RENDERAFTER" )
299
		m_iFadeState = FADE_AFTER;
300
	else if ( cmd == "FADEALLOBJECTS" )
301
		m_iFadeState = FADE_ALL;
302
	else if ( cmd == "SIZE" )
303
		m_iBorderSizeX = m_iBorderSizeY = ParseNumber ( rest );
304
}
305
 
306
void CDisplayCustom::ParseCloseEvent ( SCloseOn *close, CyString cmd, CyString rest )
307
{
308
	cmd.ToUpper();
309
	if ( cmd == "START" )
310
		close->iStart = rest.ConvertEquation();
311
	else if ( cmd == "END" )
312
		close->iEnd = rest.ConvertEquation();
313
	else if ( cmd == "LEFTCLICK" )
314
		close->iMouseCommands |= COMMAND_LEFTCLICK;
315
	else if ( cmd == "RIGHTCLICK" )
316
		close->iMouseCommands |= COMMAND_RIGHTCLICK;
317
	else if ( cmd == "LEFTCLICKIN" )
318
		close->iMouseCommands |= COMMAND_LEFTCLICKIN;
319
	else if ( cmd == "RIGHTCLICKIN" )
320
		close->iMouseCommands |= COMMAND_RIGHTCLICKIN;
321
	else if ( cmd == "KEY_ENTER" )
322
		close->iKeyCommands |= COMMAND_KEY_ENTER;
323
	else if ( cmd == "KEY_SPACE" )
324
		close->iKeyCommands |= COMMAND_KEY_SPACE;
325
}
326
 
327
void CDisplayCustom::ParseInput ( CyString cmd, CyString rest )
328
{
329
	cmd.ToUpper();
330
	if ( cmd == "MOUSECURSOR" )
331
		SetCursorTexture ( rest );
332
	else if ( cmd == "HALTMOUSE" )
333
		m_bHaltMouse = true;
334
	else if ( cmd == "CONTAINMOUSE" )
335
		m_bContainMouse = true;
336
}
337
 
338
 
339
void CDisplayCustom::DoBlock ( CRenderObject *ro, CyString line, CyStringList *strings, CyStringList *varibles, CyString &section )
340
{
341
	int num = 0;
342
	CyString *str = line.SplitToken ( ';', &num );
343
 
344
	for ( int i = 0; i < num; i++ )
345
	{
346
		CyString c = str[i].GetToken ( ":", 1, 1 ), r;
347
		if ( c.Empty() )
348
			continue;
349
 
350
		if ( c.IsIn ("{") )
351
		{
352
			if ( c.IsIn ("}") )
353
			{
354
				r = CyString("{") + c.GetToken ( "{", 2 ).GetToken ( "}", 1, 1 ) + "}";
355
				c = c.GetToken ( "{", 1, 1 );
356
			}
357
			else
358
			{
359
				if ( section.Empty() )
360
					section = c.GetToken ( "{", 1, 1 );
361
				else
362
					section += CyString(":") + c.GetToken ( "{", 1, 1 );
363
 
364
				c = c.GetToken ( "{", 2 );
365
				r = str[i].GetToken ( ":", 2 );
366
			}
367
		}
368
		else if ( c.IsIn ( "}" ) )
369
		{
370
			if ( section.IsIn (":") )
371
				section = section.GetToken ( ":", 1, section.NumToken(":") - 1 );
372
			else
373
				section = "";
374
			continue;
375
		}
376
		else
377
		{
378
			r = str[i].GetToken ( ":", 2 );
379
			if ( r.IsIn ( "}" ) )
380
			{
381
				r = r.GetToken ( "}", 1, 1 );
382
				if ( section.IsIn (":") )
383
					section = section.GetToken ( ":", 1, section.NumToken(":") - 1 );
384
				else
385
					section = "";
386
			}
387
		}
388
 
389
		if ( r.IsIn("\"") )
390
			r = ReplaceString ( r, strings );
391
 
392
		if ( c == "CALL" )
393
		{
394
			CyString func = r.GetToken ( ",", 1, 1 ).upper();
395
			for ( SFunction *f = m_lFunctions.First(); f; f = m_lFunctions.Next() )
396
			{
397
				if ( f->sFunction == func )
398
				{
399
					int num = 0;
400
					CyString *varis = r.GetToken ( ",", 2 ).SplitToken ( ',', &num );
401
					int i = 0;
402
 
403
					int stringSize = varibles->Count();
404
					for ( SStringList *sl = f->lVaribles.Head(); (sl) && (i < num); sl = sl->next, i++ )
405
						varibles->PushBack ( varis[i], sl->str );
406
 
407
					CyString oldsection = section;
408
					DoBlock ( ro, f->sBlock, &f->lStrings, varibles, section );
409
					section = oldsection;
410
 
411
					for ( i = varibles->Count(); i > stringSize; i-- )
412
						varibles->PopBack();
413
					break;
414
				}
415
			}
416
		}
417
		else
418
			ro->ParseSetting ( m_pOverlay, c, ParseVaribles (r, varibles), section );
419
	}
420
}
421
int CDisplayCustom::ParseNumber ( CyString n )
422
{
423
	// equations
424
	if ( n.IsAnyIn ( "+-/*" ) )
425
		return n.ConvertEquation ();
426
 
427
	return n.ToInt();
428
}
429
 
430
float CDisplayCustom::Update ( DWORD tNow )
431
{
432
	float dt = CDisplay::Update( tNow );
433
	if ( !dt )
434
		return 0;
435
 
436
	if ( m_iFadeIn )
437
	{
438
		if ( m_iStartDisplay >= m_iFadeIn )
439
		{
440
			m_iFadeIn = 0;
441
			m_iAlpha = m_iMaxAlpha;
442
			m_iFadeState = FADE_NONE;
443
		}
444
		else
445
		{
446
			float t = (float)m_iStartDisplay / (float)m_iFadeIn;
447
			m_iAlpha = (int) (t * (float)m_iMaxAlpha);
448
		}
449
	}
450
 
451
	return dt;
452
}