Subversion Repositories spk

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 cycrow 1
//#include "stdafx.h"
2
#include "Display.h"
3
 
4
#include "../Hook/MyDirect3DDevice9.h"
5
 
6
CDisplay::CDisplay ( COverlay *overlay, MyDirect3DDevice9 *device, CyString name )
7
{
8
	m_sGuiName = name;
9
	m_pOverlay = overlay;
10
	m_iWidth = m_iHeight = 0;
11
	m_bLongWait = false;
12
	m_bClosed = false;
13
	m_pMouseX = m_pMouseY = NULL;
14
	m_bHaltMouse = false;
15
	m_pBorderFile = NULL;
16
	m_pCursorFile = NULL;
17
	m_lastUpdate = timeGetTime();
18
	m_pDevice = device;
19
	m_iFadeState = FADE_NONE;
20
	m_bNoBorder = false;
21
	m_iBorderSizeX = m_iBorderSizeY = 42;
22
	m_bRender = true;
23
	m_iAlpha = 255;
24
	m_iStartDisplay = 0;
25
	m_bLog = false;
26
 
27
	m_borderTexture = NULL;
28
	D3DXCreateTextureFromFile ( device, "Overlay/border.png", &m_borderTexture );
29
 
30
	m_cursorTexture = NULL;
31
 
32
	if ( FAILED(D3DXCreateSprite (device, &m_sprite) ) )
33
		m_sprite = NULL;
34
}
35
 
36
CDisplay::~CDisplay ()
37
{
38
	if ( m_borderTexture )
39
		m_borderTexture->Release ();
40
 
41
	if ( m_sprite )
42
		m_sprite->Release ();
43
 
44
	ClearObjects ();
45
 
46
	for ( SSound *s = m_lSounds.First(); s; s = m_lSounds.Next() )
47
		delete s;
48
 
49
	if ( m_pBorderFile )
50
		delete m_pBorderFile;
51
 
52
	if ( m_pCursorFile )
53
		delete m_pCursorFile;
54
 
55
	for ( SCloseOn *close = m_lClose.First(); close; close = m_lClose.Next() )
56
		delete close;
57
 
58
	for ( SFunction *func = m_lFunctions.First(); func; func = m_lFunctions.Next() )
59
		delete func;
60
 
61
	m_lFunctions.clear();
62
	m_lSounds.clear();
63
	m_lClose.clear();
64
}
65
 
66
void CDisplay::CreateSound ( CyString file, long start )
67
{
68
	file = file.FindReplace ( "\\", "/" );
69
	file.RemoveSpaces ();
70
 
71
	if ( !m_pOverlay )
72
		return;
73
 
74
	if ( m_pOverlay->CreateSound ( file ) )
75
	{
76
		SSound *sound = new SSound;
77
		sound->iStart = start;
78
		m_lSounds.push_back ( sound );
79
	}
80
}
81
 
82
void CDisplay::SetCursorTexture ( CyString t )
83
{
84
	CyString ext = t.GetToken ( ".", t.NumToken ('.') ).ToLower();
85
	if ( ext == "stx" )
86
	{
87
		m_pCursorFile = new CTextureFile ();
88
		if ( !m_pCursorFile->Load ( t.c_str() ) )
89
		{
90
			delete m_pCursorFile;
91
			m_pCursorFile = NULL;
92
		}
93
		else
94
			m_cursorTexture = m_pCursorFile->CreateTexture ( m_pDevice );
95
	}
96
 
97
	if ( t == "default" )
98
		D3DXCreateTextureFromFile ( m_pDevice, "Overlay/cursor.png", &m_cursorTexture );
99
	else
100
		D3DXCreateTextureFromFile ( m_pDevice, t.c_str(), &m_cursorTexture );
101
}
102
 
103
void CDisplay::SetBorderTexture ( CyString t )
104
{
105
	if ( m_borderTexture )
106
		m_borderTexture->Release();
107
	m_borderTexture = NULL;
108
 
109
	CyString ext = t.GetToken ( ".", t.NumToken ('.') ).ToLower();
110
	if ( ext == "stx" )
111
	{
112
		m_pBorderFile = new CTextureFile ();
113
		if ( !m_pBorderFile->Load ( t.c_str() ) )
114
		{
115
			delete m_pBorderFile;
116
			m_pBorderFile = NULL;
117
		}
118
		else
119
		{
120
			m_borderTexture = m_pBorderFile->CreateTexture ( m_pDevice );
121
			m_iBorderSizeX = m_pBorderFile->GetFrameWidth();
122
			m_iBorderSizeY = m_pBorderFile->GetFrameHeight();
123
		}
124
	}
125
	else
126
		D3DXCreateTextureFromFile ( m_pDevice, t.c_str(), &m_borderTexture );
127
 
128
	if ( (!m_borderTexture) && (!m_pBorderFile) )
129
		D3DXCreateTextureFromFile ( m_pDevice, "Overlay/border.png", &m_borderTexture );
130
}
131
 
132
float CDisplay::Update ( DWORD tNow )
133
{
134
	float dt = tNow - (float) m_lastUpdate;
135
	if ( dt < 15 ) // dont update so quickly
136
		return 0;
137
 
138
	m_lastUpdate = tNow;
139
 
140
	// dont update more than 1 second at a time
141
	m_bLongWait = false;
142
	if ( dt > 1000 )
143
	{
144
		dt = 1000;
145
		m_bLongWait = true;
146
		m_bLog = true;
147
 
148
		CDisplay::Log ( "LongWait(), setting log flag\n" );
149
//		for ( CRenderObject *o = m_objects.Last(); o; o = m_objects.Prev() )
150
//		{
151
//			if ( o->DoReopen () )
152
//				m_bClosed = true;
153
//		}
154
	}
155
 
156
	m_iStartDisplay += (int)dt;
157
 
158
	dt /= 1000;
159
 
160
	if ( m_iFadeState != FADE_AFTER )
161
	{
162
		bool mouseOver = (!m_pMouseX || !m_pMouseY) ? true : false;
163
		for ( CListNode<CRenderObject> *node = m_objects.Back(); node; node = node->prev() )
164
		{
165
			CRenderObject *o = node->Data();
166
			if ( mouseOver )
167
				o->Update ( m_pOverlay, dt, -1, -1, m_iCornerX, m_iCornerY );
168
			else
169
			{
170
				o->Update ( m_pOverlay, dt, *m_pMouseX, *m_pMouseY, m_iCornerX, m_iCornerY );
171
				if ( o->MouseOver() )
172
					mouseOver = true;
173
			}
174
		}
175
	}
176
 
177
	if ( m_pOverlay )
178
	{
179
		for ( SSound *s = m_lSounds.First(); s; s = m_lSounds.Next() )
180
		{
181
			if ( s->bStarted )
182
				continue;
183
			if ( s->iStart > m_iStartDisplay )
184
				continue;
185
 
186
			s->bStarted = true;
187
			m_pOverlay->PlaySound ( s->sFilename );
188
		}
189
	}
190
 
191
	return dt;
192
}
193
 
194
bool CDisplay::IsMouseInGui ()
195
{
196
	if ( !m_pMouseX || !m_pMouseY )
197
		return false;
198
	if ( *m_pMouseX < m_iCornerX )
199
		return false;
200
	else if ( *m_pMouseX > (m_iCornerX + m_iSizeX) )
201
		return false;
202
	else if ( *m_pMouseY < m_iCornerY )
203
		return false;
204
	else if ( *m_pMouseY > (m_iCornerY + m_iSizeY) )
205
		return false;
206
 
207
	return true;
208
}
209
 
210
bool CDisplay::MouseClick ( int b, bool on )
211
{
212
	bool clicked = false;
213
 
214
	if ( on )
215
	{
216
		for ( SCloseOn *close = m_lClose.First(); close; close = m_lClose.Next() )
217
		{
218
			if ( (close->iStart) && (close->iStart > m_iStartDisplay) )
219
				continue;
220
			if ( (close->iEnd) && (close->iEnd < m_iStartDisplay) )
221
				continue;
222
 
223
			if ( (b == 0) && (close->iMouseCommands & COMMAND_LEFTCLICK) )
224
				clicked = true;
225
			else if ( (b == 0) && (close->iMouseCommands & COMMAND_LEFTCLICKIN) && (IsMouseInGui()) )
226
				clicked = true;
227
			else if ( (b == 1) && (close->iMouseCommands & COMMAND_RIGHTCLICK) )
228
				clicked = true;
229
			else if ( (b == 1) && (close->iMouseCommands & COMMAND_RIGHTCLICKIN) && (IsMouseInGui()) )
230
				clicked = true;
231
 
232
			if ( clicked )
233
				break;
234
		}
235
 
236
		if ( clicked )
237
			m_bClosed = true;
238
 
239
		// now click all objects
240
		bool c = false;
241
 
242
		for ( CRenderObject *o = m_objects.Last(); o; o = m_objects.Prev() )
243
		{
244
			if ( (!c) && (o->CheckMouse ( *m_pMouseX, *m_pMouseY, m_iCornerX, m_iCornerY )) )
245
			{
246
				int ret = o->SetMouseClicked ( m_pOverlay, m_sGuiName, b, true );
247
				c = true;
248
				if ( ret == 1 )
249
					m_bClosed = true;
250
			}
251
			else
252
				o->SetMouseClicked ( m_pOverlay, m_sGuiName, b, false );
253
		}
254
 
255
		if ( c )
256
			clicked = true;
257
	}
258
	// declick all objects
259
	else
260
	{
261
		for ( CRenderObject *o = m_objects.Last(); o; o = m_objects.Prev() )
262
			o->SetMouseClicked ( m_pOverlay, m_sGuiName, b, false );
263
	}
264
 
265
	return clicked;
266
}
267
 
268
bool CDisplay::CheckCloseEventKey ( int key )
269
{
270
	bool pressed = false;
271
	for ( SCloseOn *close = m_lClose.First(); close; close = m_lClose.Next() )
272
	{
273
		if ( (close->iStart) && (close->iStart > m_iStartDisplay) )
274
			continue;
275
		if ( (close->iEnd) && (close->iEnd < m_iStartDisplay) )
276
			continue;
277
 
278
		if ( (key == DIK_RETURN) && (close->iKeyCommands &  COMMAND_KEY_ENTER) )
279
			pressed = true;
280
		else if ( (key == DIK_SPACE) && (close->iKeyCommands &  COMMAND_KEY_SPACE) )
281
			pressed = true;
282
 
283
		if ( pressed )
284
			break;
285
	}
286
 
287
	if ( pressed )
288
		m_bClosed = true;
289
	return pressed;
290
}
291
 
292
int CDisplay::GetMouseX ()
293
{
294
	if ( !m_pMouseX )
295
		return 0;
296
 
297
	if ( m_bContainMouse )
298
	{
299
		if ( *m_pMouseX < m_iCornerX )
300
			return m_iCornerX;
301
		else if ( *m_pMouseX > (m_iCornerX + m_iSizeX) )
302
			return m_iCornerX + m_iSizeX;
303
	}
304
	return *m_pMouseX;
305
}
306
 
307
int CDisplay::GetMouseY ()
308
{
309
	if ( !m_pMouseY )
310
		return 0;
311
 
312
	if ( m_bContainMouse )
313
	{
314
		if ( *m_pMouseY < m_iCornerY )
315
			return m_iCornerY;
316
		else if ( *m_pMouseY > (m_iCornerY + m_iSizeY) )
317
			return m_iCornerY + m_iSizeY;
318
	}
319
	return *m_pMouseY;
320
}
321
 
322
void CDisplay::render ( MyDirect3DDevice9 *device )
323
{
324
	if ( m_bLongWait )
325
		m_bLog = true;
326
 
327
	if ( (m_bLongWait) || (m_bClosed) )
328
		return;
329
 
330
	if ( !m_bRender )
331
		return;
332
 
333
	if ( m_bLog ) CDisplay::Log ( "Setting render state\n" );
334
	device->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE);
335
 
336
	if ( m_bLog ) CDisplay::Log ( "Setting up sprite rendering\n" );
337
	m_sprite->Begin(D3DXSPRITE_ALPHABLEND);
338
 
339
	if ( m_bLog ) CDisplay::Log ( "Rendering the border\n" );
340
	if ( !m_bNoBorder )
341
		RenderBorder ( device );
342
 
343
	if ( m_iFadeState != FADE_AFTER )
344
	{
345
		if ( m_bLog ) CDisplay::Log ( "Fading in all objects\n" );
346
		for ( CRenderObject *o = m_objects.First(); o; o = m_objects.Next() )
347
		{
348
			if ( (m_iFadeIn) && (m_iFadeState == FADE_ALL) && (!o->StartHidden()) )
349
				o->SetAlpha ( m_iAlpha );
350
			o->render ( device, m_iCornerX, m_iCornerY, m_sprite );
351
		}
352
	}
353
 
354
	if ( m_bLog ) CDisplay::Log ( "Rendering of the cursor\n" );
355
	RenderCursor ( device );
356
 
357
	if ( m_bLog ) CDisplay::Log ( "Ending the sprite render\n" );
358
	m_sprite->End ();
359
 
360
	if ( m_bLog ) CDisplay::Log ( "render() finished\n" );
361
}
362
 
363
CyString CDisplay::ParseVaribles ( CyString str, CyStringList *varibles )
364
{
365
	if ( !str.IsIn ( "$" ) )
366
		return str;
367
 
368
	for ( SStringList *it = varibles->Head(); it; it = it->next )
369
	{
370
		if ( it->data.Empty() )
371
			continue;
372
		str.FindReplace ( it->data, it->str );
373
	}
374
	return str;
375
}
376
 
377
void CDisplay::RenderCursor ( MyDirect3DDevice9 *device )
378
{
379
	if ( (!m_cursorTexture) || (!m_sprite) )
380
		return;
381
 
382
	if ( (!m_pMouseX) || (!m_pMouseY) )
383
		return;
384
 
385
	D3DXMATRIX mat;
386
	D3DXMatrixTransformation2D ( &mat, NULL, 0.0, NULL, NULL, 0, &D3DXVECTOR2 ((float)GetMouseX(), (float)GetMouseY()) );
387
	m_sprite->SetTransform(&mat);
388
	m_sprite->Draw ( m_cursorTexture, NULL, NULL, NULL, 0xFFFFFFFF );
389
}
390
 
391
void CDisplay::RenderBorder ( MyDirect3DDevice9 *device )
392
{
393
	if ( !m_borderTexture )
394
		return;
395
 
396
	if ( !m_sprite )
397
		return;
398
 
399
	int bx = GetBorderSizeX();
400
	int by = GetBorderSizeY();
401
 
402
	D3DXMATRIX mat;
403
 
404
	D3DCOLOR color = D3DCOLOR_RGBA ( 255,255, 255, m_iAlpha );
405
 
406
	// render top left corner
407
	D3DXMatrixTransformation2D ( &mat, NULL, 0.0, NULL, NULL, 0, &D3DXVECTOR2 ((float)m_iCornerX - bx, (float)m_iCornerY - by) );
408
	m_sprite->SetTransform(&mat);
409
	m_sprite->Draw ( m_borderTexture, GetFrame(1), NULL, NULL, color );
410
 
411
	// render top right corner
412
	D3DXMatrixTransformation2D ( &mat, NULL, 0.0, NULL, NULL, 0, &D3DXVECTOR2 ((float)m_iCornerX + m_iSizeX, (float)m_iCornerY - by) );
413
	m_sprite->SetTransform(&mat);
414
	m_sprite->Draw ( m_borderTexture, GetFrame(3), NULL, NULL, color );
415
 
416
	// render bottom left corner
417
	D3DXMatrixTransformation2D ( &mat, NULL, 0.0, NULL, NULL, 0, &D3DXVECTOR2 ((float)m_iCornerX - bx, (float)m_iCornerY + m_iSizeY) );
418
	m_sprite->SetTransform(&mat);
419
	m_sprite->Draw ( m_borderTexture, GetFrame(7), NULL, NULL, color );
420
 
421
	// render bottom right corner
422
	D3DXMatrixTransformation2D ( &mat, NULL, 0.0, NULL, NULL, 0, &D3DXVECTOR2 ((float)m_iCornerX + m_iSizeX, (float)m_iCornerY + m_iSizeY) );
423
	m_sprite->SetTransform(&mat);
424
	m_sprite->Draw ( m_borderTexture, GetFrame(9), NULL, NULL, color );
425
 
426
	// render top/bottom
427
	int i;
428
	for ( i = 1; i <= (m_iSizeX / bx); i++ )
429
	{
430
		D3DXMatrixTransformation2D ( &mat, NULL, 0.0, NULL, NULL, 0, &D3DXVECTOR2 ( ((float)m_iCornerX - bx) + (i * bx), (float)m_iCornerY - by) );
431
		m_sprite->SetTransform(&mat);
432
		m_sprite->Draw ( m_borderTexture, GetFrame(2), NULL, NULL, color );
433
		D3DXMatrixTransformation2D ( &mat, NULL, 0.0, NULL, NULL, 0, &D3DXVECTOR2 ( ((float)m_iCornerX - bx) + (i * bx), (float)m_iCornerY + m_iSizeY) );
434
		m_sprite->SetTransform(&mat);
435
		m_sprite->Draw ( m_borderTexture, GetFrame(8), NULL, NULL, color );
436
	}
437
 
438
	//render sides
439
	for ( i = 1; i <= (m_iSizeY / by); i++ )
440
	{
441
		D3DXMatrixTransformation2D ( &mat, NULL, 0.0, NULL, NULL, 0, &D3DXVECTOR2 ( ((float)m_iCornerX - bx), ((float)m_iCornerY - by) + (i * by)) );
442
		m_sprite->SetTransform(&mat);
443
		m_sprite->Draw ( m_borderTexture, GetFrame(4), NULL, NULL, color );
444
		D3DXMatrixTransformation2D ( &mat, NULL, 0.0, NULL, NULL, 0, &D3DXVECTOR2 ((float)m_iCornerX + m_iSizeX, ((float)m_iCornerY - by) + (i * by)) );
445
		m_sprite->SetTransform(&mat);
446
		m_sprite->Draw ( m_borderTexture, GetFrame(6), NULL, NULL, color );
447
	}
448
 
449
	// render center
450
	for ( i = 1; i <= (m_iSizeX / bx); i++ )
451
	{
452
		for ( int j = 1; j <= (m_iSizeY / by); j++ )
453
		{
454
			D3DXMatrixTransformation2D ( &mat, NULL, 0.0, NULL, NULL, 0, &D3DXVECTOR2 ( ((float)m_iCornerX - bx) + (i * bx), ((float)m_iCornerY - by) + (j * by)) );
455
			m_sprite->SetTransform(&mat);
456
			m_sprite->Draw ( m_borderTexture, GetFrame(5), NULL, NULL, color );
457
		}
458
	}
459
}
460
 
461
RECT *CDisplay::GetFrame ( int frame )
462
{
463
	RECT *rect = new RECT;
464
 
465
	int row = ((frame - 1) / 3) + 1;
466
	int col = frame % 3;
467
	if ( col == 0)
468
		col = 3;
469
 
470
	int bx = GetBorderSizeX();
471
	int by = GetBorderSizeY();
472
 
473
	int x = bx * col;
474
	int y = by * row;
475
 
476
	SetRect ( rect, x - bx, y - by, x, y );
477
 
478
	return rect;
479
}
480
 
481
void CDisplay::SetPosition(int x, int y)
482
{
483
	m_iCornerX = x;
484
	m_iCornerY = y;
485
}
486
 
487
void CDisplay::SetSize(int width, int height)
488
{
489
	m_iSizeX = width;
490
	m_iSizeY = height;
491
}
492
void CDisplay::SetSize ( int width, int height, int maxwidth, int maxheight )
493
{
494
	SetMaxSize(maxwidth, maxheight);
495
	int bx = GetBorderSizeX();
496
	int by = GetBorderSizeY();
497
	if ( (width == maxwidth) && (height == maxheight) )
498
	{
499
		m_iSizeX = maxwidth;
500
		m_iSizeY = maxheight;
501
		m_iCornerX = m_iCornerY = 0;
502
	}
503
	else
504
	{
505
		if ( !m_bNoBorder )
506
		{
507
			width += (bx - (width % bx));
508
			height += (by - (height % by));
509
		}
510
 
511
		m_iSizeX = width;
512
		m_iSizeY = height;
513
 
514
		m_iCornerX = (maxwidth / 2) - (width / 2);
515
		m_iCornerY = (maxheight / 2) - (height / 2);
516
	}
517
 
518
	m_iWidth = maxwidth;
519
	m_iHeight = maxheight;
520
}