Subversion Repositories spk

Rev

Rev 1 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 cycrow 1
// LcdCustomScreen.cpp: implementation of the CLcdCustomScreen class.
2
//
3
//////////////////////////////////////////////////////////////////////
4
 
5
#include "stdafx.h"
6
#include "LcdCustomScreen.h"
7
 
8
//////////////////////////////////////////////////////////////////////
9
// Construction/Destruction
10
//////////////////////////////////////////////////////////////////////
11
 
242 cycrow 12
Utils::WString GetEndOfLine(FILE* id, int* line, bool upper)
13
{
14
	std::string word;
15
 
16
	char c = fgetc(id);
17
	if (c == -1)
18
		return L"";
19
 
20
	while ((c != 13) && (!feof(id)) && (c != '\n'))
21
	{
22
		word += c;
23
		c = fgetc(id);
24
	}
25
 
26
	if (line)
27
		++(*line);
28
 
29
	if (upper)
30
		return Utils::WString(word).upper();
31
 
32
	return Utils::WString(word);
33
}
34
 
35
 
1 cycrow 36
CLcdCustomScreen::CLcdCustomScreen( CEzLcd *lcd )
37
{
38
	m_pLcd = lcd;
39
	m_iArraySize = 10;
40
	m_pHandles = (HANDLE *)malloc(sizeof(HANDLE) * m_iArraySize);
41
	m_iCycles = 10;
42
	m_bDebug = false;
43
	m_pMoveObject = m_pEndMoveObject = NULL;
44
	m_pText = m_pEndText = NULL;
45
}
46
 
47
CLcdCustomScreen::~CLcdCustomScreen()
48
{
49
	Reset ();
50
	free ( m_pHandles );
51
}
52
 
53
void CLcdCustomScreen::IncreaseArraySize ()
54
{
55
	m_iArraySize += 5;
56
	m_pHandles = (HANDLE *)realloc(m_pHandles, sizeof(HANDLE) * m_iArraySize);
57
}
58
 
242 cycrow 59
Utils::WString CLcdCustomScreen::parseFilename(const Utils::WString &filename, const Utils::WString &dir)
1 cycrow 60
{
242 cycrow 61
	Utils::WString f = filename;
1 cycrow 62
 
242 cycrow 63
	f = f.findReplace(L"$progdir", m_sProgDir);
64
	f = f.findReplace(L"$gamedir", m_sGameDir);
1 cycrow 65
 
66
	// relative filename
242 cycrow 67
	if ( !f.contains(L":") )
68
		f = dir + L"/" + f;
1 cycrow 69
 
242 cycrow 70
	f = f.findReplace(L"/", L"\\");
71
	f = f.findReplace(L"\\\\", L"\\");
1 cycrow 72
	return f;
73
}
242 cycrow 74
bool CLcdCustomScreen::loadScript (const Utils::WString &sDir, const Utils::WString& sProgDir, const Utils::WString& filename)
1 cycrow 75
{
242 cycrow 76
	_lDebugLog.clear();
77
	_lVaribles.clear();
1 cycrow 78
 
79
	int iCurrentPos = 0;
80
 
81
	m_iStartPage = m_pLcd->AddNewPage () - 1;
82
    m_pLcd->ModifyControlsOnPage( m_iStartPage );
83
 
84
	int iLastX, iLastY;
85
 
86
	m_bDebug = false;
242 cycrow 87
	Utils::WString fname = filename;
88
	fname = fname.findReplace(L"$progdir", sProgDir);
1 cycrow 89
 
90
	// relative path
242 cycrow 91
	if ( !fname.contains(L":") )
92
		fname = sDir + L"/" + filename;
93
	fname = fname.findReplace(L"\\", L"/"); // make sure we're using the correct slash
94
	fname = fname.findReplace(L"//", L"/"); // remove any double slashes
95
	Utils::WString dir = fname.tokens(L"/", 1);
1 cycrow 96
 
97
	m_sScriptDir = dir;
98
	m_sProgDir = sProgDir;
99
	m_sGameDir = sDir;
100
 
242 cycrow 101
	FILE *id = _wfopen ( fname.c_str(), L"r" );
1 cycrow 102
	if ( id )
103
	{
104
		int iLine = 0;
105
		while ( !feof(id) )
106
		{
242 cycrow 107
			Utils::WString line;
108
			line = GetEndOfLine ( id, &iLine, false );
109
			if ( (line.left (2) == L"//") || (line.empty()) )
1 cycrow 110
				continue;
242 cycrow 111
			Utils::WString arg = line.token(L" ", 1).lower();
112
			if ( arg == L"displayfor:" )
1 cycrow 113
			{
242 cycrow 114
				m_iCycles = line.token(L" ", 2).toInt();
1 cycrow 115
				if ( (!m_iCycles) || (m_iCycles < 0) )
242 cycrow 116
					_lDebugLog.pushBack (Utils::WString((long)iLine) + L": Invalid argument for \"DisplayFor\" (" + line.token(L" ", 2) + L")");
1 cycrow 117
			}
242 cycrow 118
			else if ( arg == L"debug" )
1 cycrow 119
				m_bDebug = true;
242 cycrow 120
			else if ( arg == L"varible:" )
1 cycrow 121
			{
242 cycrow 122
				if ( line.countToken(L" ") < 2)
123
					_lDebugLog.pushBack(Utils::WString((long)iLine) + L": Missing Argument for \"Varible\"");
1 cycrow 124
				else
242 cycrow 125
					_lVaribles.pushBack(line.token(L" ", 2));
1 cycrow 126
			}
242 cycrow 127
			else if ( arg == L"icon:" )
1 cycrow 128
			{
242 cycrow 129
				if ( line.countToken(L" ") < 6)
130
					_lDebugLog.pushBack(Utils::WString((long)iLine) + L": Missing Arguments for \"Icon\"");
1 cycrow 131
				else
132
				{
242 cycrow 133
					Utils::WString sIcon = line.tokens(L" ", 6);
134
					int iSizeX = line.token(L" ", 4).toInt();
135
					int iSizeY = line.token(L" ", 5).toInt();
136
					HICON hIcon = static_cast<HICON>(LoadImage(0, (LPCTSTR)parseFilename(sIcon, dir).c_str(), IMAGE_ICON, iSizeX, iSizeY, LR_LOADFROMFILE));
1 cycrow 137
					if ( FAILED (hIcon) )
242 cycrow 138
						_lDebugLog.pushBack (Utils::WString((long)iLine) + L": Unable to open icon file: " + sIcon);
1 cycrow 139
					else
140
					{
141
						m_pHandles[iCurrentPos] = m_pLcd->AddIcon(hIcon, iSizeX, iSizeY);
242 cycrow 142
						int iX = line.token(L" ", 2).toInt();
143
						int iY = line.token (L" ", 3).toInt();
1 cycrow 144
						m_pLcd->SetOrigin ( m_pHandles[iCurrentPos], iX, iY );
145
						iLastX = iX;
146
						iLastY = iY;
147
						iCurrentPos++;
148
					}
149
				}
150
			}
242 cycrow 151
			else if ( (arg == L"move:") || (arg == L"moverepeat:") )
1 cycrow 152
			{
242 cycrow 153
				if ( line.countToken(L" ") < 4)
154
					_lDebugLog.pushBack(Utils::WString((long)iLine) + L": Missing Argument for \"Move\"");
1 cycrow 155
				else if ( !iCurrentPos )
242 cycrow 156
					_lDebugLog.pushBack(Utils::WString((long)iLine) + L": No object before move command");
1 cycrow 157
				else
158
				{
159
					SMoveObject *o = new SMoveObject;
160
					o->hObject = m_pHandles[iCurrentPos - 1];
242 cycrow 161
					if ( arg == L"moverepeat:" )
1 cycrow 162
						o->bRepeat = true;
163
					else
164
						o->bRepeat = false;
165
					o->iStartX = iLastX;
166
					o->iStartY = iLastY;
242 cycrow 167
					if ( line.token(L" ", 2) == L"-")
1 cycrow 168
						o->iEndX = o->iStartX;
169
					else
242 cycrow 170
						o->iEndX = line.token(L" ", 2).toInt();
171
					if ( line.token(L" ", 3) == L"-")
1 cycrow 172
						o->iEndY = o->iStartY;
173
					else
242 cycrow 174
						o->iEndY = line.token(L" ", 3).toInt();
175
					o->iTimeRemaining = o->iTotalTime = line.token(L" ", 4).toInt();
1 cycrow 176
					AddMoveObject ( o );
177
				}
178
			}
242 cycrow 179
			else if ( ((arg.left(9) == L"textsmall") || (arg.left(10) == L"textmedium") || (arg.left(9) == L"textlarge")) && (arg.right(1) == L":") )
1 cycrow 180
			{
242 cycrow 181
				if ( line.countToken(L" ") < 6)
182
					_lDebugLog.pushBack(Utils::WString((long)iLine) + L": Missing Argument for \"Text\"");
1 cycrow 183
				else
184
				{
242 cycrow 185
					Utils::WString align = line.token(L" ", 5).lower();
1 cycrow 186
					int iAlign = DT_CENTER;
187
					if ( align == "center" )
188
						iAlign = DT_CENTER;
189
					else if ( align == "left" )
190
						iAlign = DT_LEFT;
191
					else if ( align == "right" )
192
						iAlign = DT_RIGHT;
193
					else
242 cycrow 194
						_lDebugLog.pushBack(Utils::WString((long)iLine) + L": Invalid alignment setting \"" + align + L"\"");
1 cycrow 195
 
196
					LGTextSize iSize = LG_SMALL;
242 cycrow 197
					if ( arg.left(10) == L"textmedium" )
1 cycrow 198
						iSize = LG_MEDIUM;
242 cycrow 199
					else if ( arg.left(9) == L"textsmall" )
1 cycrow 200
						iSize = LG_SMALL;
242 cycrow 201
					else if ( arg.left(9) == L"textlarge" )
1 cycrow 202
						iSize = LG_BIG;
203
 
204
					bool scroll = false;
242 cycrow 205
					if ( arg.right (7) == L"Scroll:" )
1 cycrow 206
						scroll = true;
207
 
242 cycrow 208
					int len = line.token(L" ", 4).toInt();
209
					int x = line.token(L" ", 2).toInt();
210
					int y = line.token(L" ", 3).toInt();
211
					Utils::WString str = line.tokens(L" ", 6);
1 cycrow 212
					m_pHandles[iCurrentPos] = m_pLcd->AddText ( (scroll) ? LG_SCROLLING_TEXT : LG_STATIC_TEXT, iSize, iAlign, len );
213
					m_pLcd->SetOrigin ( m_pHandles[iCurrentPos], x, y );
214
					m_pLcd->SetText ( m_pHandles[iCurrentPos], (LPCTSTR)str.c_str() );
215
 
216
					SText *text = new SText;
217
					text->hObject = m_pHandles[iCurrentPos];
218
					text->sText = str;
219
					AddText ( text );
220
					iCurrentPos++;
221
				}
222
			}
223
			else
242 cycrow 224
				_lDebugLog.pushBack(Utils::WString((long)iLine) + L": Invalid Command: " + arg );
1 cycrow 225
 
226
			if ( iCurrentPos >= m_iArraySize )
227
				IncreaseArraySize();
228
		}
229
 
230
		fclose ( id );
231
	}
232
 
233
	// debug
234
	m_pLcd->AddNewPage ();
235
    m_pLcd->ModifyControlsOnPage(m_iStartPage + 1);
236
	for ( int i = 0; i < 5; i++ )
237
	{
238
		m_hDebug[i] = m_pLcd->AddText ( LG_SCROLLING_TEXT, LG_SMALL, DT_LEFT, 160 );
239
		m_pLcd->SetOrigin ( m_hDebug, 0, (i * 9) );
240
	}
241
 
242
	if ( m_bDebug )
243
		m_iCycles = 100;
244
 
245
	return true;
246
}
247
 
242 cycrow 248
void CLcdCustomScreen::setVarible(const Utils::WString &v, size_t pos )
1 cycrow 249
{
242 cycrow 250
	if ( pos >= _lVaribles.size() )
1 cycrow 251
		return;
252
 
242 cycrow 253
	_lVaribles[pos]->data = v;
1 cycrow 254
}
255
 
256
void CLcdCustomScreen::Reset ()
257
{
258
	m_pLcd->RemovePage ( m_iStartPage + 1 );
259
	m_pLcd->RemovePage ( m_iStartPage );
260
 
242 cycrow 261
	_lDebugLog.clear();
262
	_lVaribles.clear();
1 cycrow 263
 
264
	SMoveObject *o = m_pMoveObject;
265
	while ( o )
266
	{
267
		SMoveObject *next = o->pNext;
268
		delete o;
269
		o = next;
270
	}
271
 
272
	SText *t = m_pText;
273
	while ( t )
274
	{
275
		SText *next = t->pNext;
276
		delete t;
277
		t = next;
278
	}
279
	m_pMoveObject = m_pEndMoveObject = NULL;
280
	m_pText = m_pEndText = NULL;
281
}
282
 
283
void CLcdCustomScreen::AddMoveObject ( SMoveObject *o )
284
{
285
	o->pNext = NULL;
286
	if ( !m_pEndMoveObject )
287
		m_pMoveObject = m_pEndMoveObject = o;
288
	else
289
	{
290
		m_pEndMoveObject->pNext = o;
291
		m_pEndMoveObject = o;
292
	}
293
}
294
 
295
void CLcdCustomScreen::AddText ( SText *t )
296
{
297
	t->pNext = NULL;
298
	if ( !m_pEndText )
299
		m_pText = m_pEndText = t;
300
	else
301
	{
302
		m_pEndText->pNext = t;
303
		m_pEndText = t;
304
	}
305
}
306
 
307
bool CLcdCustomScreen::Display ()
308
{
309
	--m_iCycles;
310
 
311
	if ( m_bDebug )
312
	{
313
		m_pLcd->ShowPage ( m_iStartPage + 1 );
314
	    m_pLcd->ModifyControlsOnPage ( m_iStartPage + 1 );
242 cycrow 315
 
316
		if (_lDebugLog.empty())
317
			m_pLcd->SetText(m_hDebug[0], _T("No errors found!"));
318
		else
1 cycrow 319
		{
242 cycrow 320
			size_t max = _lDebugLog.size() > 5 ? 5 : _lDebugLog.size();
321
			for (size_t i = 0; i < max; i++)
322
				m_pLcd->SetText(m_hDebug[i], _lDebugLog[i]->str);
1 cycrow 323
		}
324
 
325
	}
326
	else
327
	{
328
		m_pLcd->ShowPage ( m_iStartPage );
329
	    m_pLcd->ModifyControlsOnPage ( m_iStartPage );
330
 
331
		for ( SText *text = m_pText; text; text = text->pNext )
332
		{
242 cycrow 333
			Utils::WString newtext = text->sText;
334
			for(auto itr = _lVaribles.begin(); itr != _lVaribles.end(); itr++)
335
				newtext = newtext.findReplace(L"$" + (*itr)->str, (*itr)->data );
336
			m_pLcd->SetText ( text->hObject, newtext);
1 cycrow 337
		}
338
 
339
		// now do moving objects
340
		for ( SMoveObject *o = m_pMoveObject; o; o = o->pNext )
341
		{
342
			if ( o->iTimeRemaining > 0 )
343
				o->iTimeRemaining--;
344
			else if ( o->bRepeat )
345
				o->iTimeRemaining = o->iTotalTime;
346
 
347
			float t = (o->iTotalTime - o->iTimeRemaining)  / (float)o->iTotalTime;
348
			float u = 1 - t;
349
 
350
			int iX, iY;
351
			if ( o->iStartX == o->iEndX ) iX = o->iStartX;
352
			else						  iX = (int)((double)((1 - t) * o->iStartX) + (t * o->iEndX));
353
 
354
			if ( o->iStartY == o->iEndY ) iY = o->iStartY;
355
			else						  iY = (int)((double)((1 - t) * o->iStartY) + (t * o->iEndY));
356
 
357
			m_pLcd->SetOrigin ( o->hObject, iX, iY );
358
		}
359
	}
360
 
361
	if ( m_iCycles > 0 )
362
		return true;
363
	return false;
364
}