Filter:   InfoImg
download interfacehandler.cc
Language: C++
LOC: 432
Project Info
Ultima Online Project(uop)
Server: SourceForge
Type: cvs
SourceForge\u\uop\uop\uop\
   acinclude.m4
   aclocal.m4
   action.cc
   action.h
   animation.cc
   animation.h
   artexport.cc
   artfactory.cc
   artfactory.h
   audio.cc
   audio.h
   btree.cc
   btree.h
   cache.h
   cliloc.cc
   cliloc.h
   config.h.in
   configure.in
   cvs2cl.pl
   entity.cc
   entity.h
   gumps.cc
   gumps.h
   interface.cc
   interface.h
   interfacegumps.cc
   interfacegumps.h
   interfacehandler.cc
   interfacehandler.h
   intrface.def
   Makefile.am
   Makefile.in
   message.cc
   message.h
   musicmap.def
   network.cc
   network.h
   patchhandler.cc
   patchhandler.h
   readable.pl
   serverhandler.cc
   serverhandler.h
   sprite.cc
   sprite.h
   stamp-h.in
   texthandler.cc
   texthandler.h
   tiledata.cc
   tiledata.h
   unreadable.pl
   uop.cc
   uop.cfg
   uop.dox
   uop.h
   uopconfig.cc
   uopconfig.h
   uosprite.h
   uostub.pl
   world.cc
   world.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// vim:ts=2 sw=2

#include "uop.h"
#include <SDL/SDL.h>
#include "interfacehandler.h"
#include "interface.h"
#include "action.h"
#include "network.h"

extern SDL_Surface *screen;
extern Interface *intrface;
extern Action *action;
extern Network *network;

char *hairstyles[]={
"hairbald","hairshort","hairlong","hairpony","hairmohawk","hairpageboy",
"hairtopknot","haircurly","hairreceding",NULL};
char *beardstyles[]={
"facialnone","facialgoatee","faciallongbeard","facialshortbeard",
"facialmustache","facialshorfullbeard","faciallongfullbeard",
"facialvandyke",NULL};

InterfaceHandler::InterfaceHandler()
{
	activeGump=NULL;
	mouseX=mouseY=oldX=oldY=false;
	activeText=primedText=NULL;
	textFocus=0;
	currentGump=NULL;
	screenH = screen->h;
	screenW = screen->w;
}
InterfaceHandler::~InterfaceHandler()
{
}

bool InterfaceHandler::handleEvents()
{
	SDL_Event event;
	bool updateAction=false;
	while (SDL_PollEvent(&event))
	{
		switch (event.type)
		{
			case SDL_MOUSEMOTION:
				mouseX=event.motion.x;
				mouseY=event.motion.y;
				break;
			case SDL_MOUSEBUTTONDOWN:
				if (activeGump)
					activeGump->mouseDown(event.button.button,
							event.button.x,event.button.y);
				else if (gumpList.size())
				{
					bool hit=false;
					vector<GumpMain *>::reverse_iterator i;
					for (i=gumpList.rbegin();i!=gumpList.rend() && !hit;i++)
						hit=(*i)->mouseDown(event.button.button,
								event.button.x,event.button.y);
					if (hit) currentGump=(*i);
				}
				updateAction=true;
				break;
			case SDL_MOUSEBUTTONUP:
				if (activeGump)
					activeGump->mouseUp(event.button.button,
							event.button.x,event.button.y);
				else if (gumpList.size())
				{
					bool hit=false;
					vector<GumpMain *>::reverse_iterator i;
					for (i=gumpList.rbegin();i!=gumpList.rend() && !hit;i++)
						hit=(*i)->mouseUp(event.button.button,
								event.button.x,event.button.y);
					if (hit) currentGump=(*i);
				}
				updateAction=true;
				break;
			case SDL_KEYDOWN:
				updateAction=processKey(event.key.keysym.sym,event.key.keysym.mod);
				break;
			case SDL_VIDEORESIZE:
				screenW = event.resize.w;
				screenH = event.resize.h;
				break;
			case SDL_QUIT:
				exit(0);
		}
	}
	if (mouseX!=oldX || mouseY!=oldY)
	{
		oldX=mouseX;
		oldY=mouseY;
		if (activeGump && !activeGump->mouseMove(mouseX,mouseY))
		{
			activeGump->mouseExit();
			activeGump=NULL;
			updateAction=true;
		}
		if (!activeGump && gumpList.size())
		{
			vector<GumpMain *>::reverse_iterator i;
			for (i=gumpList.rbegin();i!=gumpList.rend() && !activeGump;i++)
			{
				currentGump=(*i);
				activeGump=currentGump->mouseEnter(mouseX,mouseY);
			}
			if (!activeGump) currentGump=NULL;
		}
		if (activeGump) updateAction=true;
	}
	return updateAction;
}

void InterfaceHandler::addGump(string gump,int x,int y,int w,int h)
{
	GumpMain *g=intrface->getGump(gump);
	g->move(x,y,w,h);
	gumpList.push_back(g);
}
void InterfaceHandler::addGump(string gump,void (Action::*onClose)(),
		int x,int y,int w,int h)
{
	GumpMain *g=intrface->getGump(gump);
	g->close=onClose;
	g->move(x,y,w,h);
	gumpList.push_back(g);
}
void InterfaceHandler::removeAllGumps()
{
	activeGump=NULL;
	gumpList.clear();
	primedText=activeText=textMessage=NULL;
	activeStrings.clear();
	textFocus=0;
	currentGump=NULL;
}

void InterfaceHandler::draw()
{
	vector<GumpMain *>::iterator i;
	for (i=gumpList.begin();i!=gumpList.end();i++)
		(*i)->draw();
}

void InterfaceHandler::buttonClicked(Uint32 ID)
{
	switch (ID)
	{
		case 10013: //cancel
			{
				GumpMain *g=currentGump;
				gumpList.pop_back();
				if (g->close)
					(action->*(g->close))();
			}
			break;
		case 10019: //prev
			if (currentGump && currentGump->gumpName=="shardselection")
			{
				removeAllGumps();
				addGump("accountlogin");
				network->disconnect();
			}
			break;
		case 10020: //next
			if (currentGump && currentGump->gumpName=="accountlogin")
				action->signOn(*activeStrings[0],*activeStrings[1]);
			if (currentGump && currentGump->gumpName=="shardselection")
				action->selectServer(1);
			if (currentGump && currentGump->gumpName=="charcreation-appearance")
				action->selectName(*activeStrings[0]);
			break;
		case 10026:	//quit
			exit(0);
		case 10042: //skin hue
			showColorPicker(130502);
			break;
		case 10043: //hair hue
			showColorPicker(130503);
			break;
		case 10044: //beard hue
			showColorPicker(130504);
			break;
		case 10045: //shirt hue
			showColorPicker(130505);
			break;
		case 10046: //pants hue
			showColorPicker(130506);
			break;
		case 10047: //hairstyle
			{
				if (activeGump)
				{
					activeGump->mouseExit();
					activeGump=NULL;
				}
				GumpVList *vlist=getVList(110005);
				for (int i=0;hairstyles[i];i++)
					vlist->add(new GumpVListChild(i+110005*100,
								hairstyles[i],hairstyles[i],hairstyles[i],""));
			}
			break;
		case 10048: //beard style
			{
				if (activeGump)
				{
					activeGump->mouseExit();
					activeGump=NULL;
				}
				GumpVList *vlist=getVList(110006);
				for (int i=0;beardstyles[i];i++)
					vlist->add(new GumpVListChild(i+110006*100,
								beardstyles[i],beardstyles[i],beardstyles[i],""));
			}
			break;
	}
	if ((ID/10)==10021) // char sel
	{
		int chr=ID%10;
		if (chr>=maxCharacters) // create character
			action->createCharacter(chr);
		else
			action->selectCharacter(chr);
	}
}
void InterfaceHandler::buttonToggled(Uint32 ID,bool state)
{
}
void InterfaceHandler::vListChosen(Uint32 ID)
{
	if (ID/100==110002)	//server selection
		action->selectServer(ID%100);
	if (ID/100==110005) //hair
	{
		action->selectHair(ID%100);
		GumpVList *vlist=getVList(110005);
		if (activeGump)
		{
			activeGump->mouseExit();
			activeGump=NULL;
		}
		vlist->clear();
	}
	if (ID/100==110006) //beard
	{
		action->selectBeard(ID%100);
		GumpVList *vlist=getVList(110006);
		if (activeGump)
		{
			activeGump->mouseExit();
			activeGump=NULL;
		}
		vlist->clear();
	}
	if (ID/100==110007) // profession
		action->selectProfession(ID%100);
}
void InterfaceHandler::colorChosen(Uint32 ID,Uint16 hue)
{
	showColorPicker(0);
	switch (ID)
	{
		case 130502:
			action->selectSkinHue(hue);
			break;
		case 130503:
			action->selectHairHue(hue);
			break;
		case 130504:
			action->selectBeardHue(hue);
			break;
		case 130505:
			action->selectShirtHue(hue);
			break;
		case 130506:
			action->selectPantHue(hue);
			break;
	}
}

void InterfaceHandler::primeText(string &text)
{
	primedText=&text;
}

void InterfaceHandler::message(string &message)
{
	textMessage=&message;
}

GumpTextArea *InterfaceHandler::registerTextArea(GumpTextArea *textarea)
{
	if (textarea->ID==30005)
	{
		textarea->textarea=new string();
		activeStrings.push_back(textarea->textarea);
		activeText=textarea->textarea;
	}
	if (textarea->ID==30006)
	{
		textarea->textarea=new string();
		activeStrings.push_back(textarea->textarea);
		textarea->password=true;
	}
	if (textarea->ID==30007)
	{
		version="Project:UO version 0.2.9 (alpha)";
		textarea->textarea=&version;
	}
	if (textarea->ID==30008)
			textarea->textarea=primedText;
	if (textarea->ID==30013)
			textarea->textarea=primedText;
	if (textarea->ID==30019)	//character name
	{
		textarea->textarea=new string();
		activeStrings.push_back(textarea->textarea);
		activeText=textarea->textarea;
	}
	if (textarea->ID==140402)
		textarea->textarea=textMessage;
	return textarea;
}
GumpVList *InterfaceHandler::registerVList(GumpVList *vlist)
{
	vLists[vlist->ID]=vlist;
	return vlist;
}
GumpVList *InterfaceHandler::getVList(Uint32 ID)
{
	return vLists[ID];
}
GumpColorPicker *InterfaceHandler::registerColorPicker(GumpColorPicker *picker)
{
	switch (picker->ID)
	{
		case 130502: picker->baseHue=1001; break;
		case 130503: picker->baseHue=1101; break;
		case 130504: picker->baseHue=1101; break;
		case 130505: picker->baseHue=1; break;
		case 130506: picker->baseHue=1; break;
	}
	colorPickers[picker->ID]=picker;
	return picker;
}
void InterfaceHandler::showColorPicker(Uint32 ID)
{
	map<Uint32,GumpColorPicker *>::iterator i;
	for (i=colorPickers.begin();i!=colorPickers.end();i++)
		(*i).second->visible=false;
	if (ID) colorPickers[ID]->visible=true;
	if (activeGump)
	{
		activeGump->mouseExit();
		activeGump=NULL;
	}
}

void InterfaceHandler::initCharacters(Uint8 numChars)
{
	characters.clear();
	maxCharacters=numChars;
	curCharacter=0;
}
void InterfaceHandler::addCharacter(string chr)
{
	characters.push_back(chr);
}
int InterfaceHandler::getNextCharacter()
{
	return curCharacter++;
}
string &InterfaceHandler::getCharacter(int id)
{
	return characters[id];
}
void InterfaceHandler::setProfession(string &profession)
{
	this->profession=profession;
}

static char shifts[]={
	'`','~','1','!','2','@','3','#','4','$','5','%','6','^','7','&',
	'8','*','9','(','0',')','-','_','=','+','[','{',']','}',';',':',
	'\'','\"',',','<','.','>','/','?','\\','|',0,0};

char InterfaceHandler::SDL2ASC(SDLKey c,SDLMod m)
{
	char ch;
	int i;
	ch=c;
	if (m&KMOD_SHIFT)
	{
		i=0;
		if (c>='a' && c<='z')
			ch=c-('a'-'A');
		else
		{
			while (shifts[i*2] && shifts[(i++)*2]!=c);
			if (shifts[(i-1)*2])
				ch=shifts[(i-1)*2+1];
		}
	}
	return ch;
}


bool InterfaceHandler::processKey(SDLKey c,SDLMod m)
{
	char ch;
	if (c>255) return false;
	if (!activeText) return false;
	if (c==SDLK_TAB)
	{
		textFocus++;
		if (textFocus==activeStrings.size())
			textFocus=0;
		activeText=activeStrings[textFocus];
	}
	else if (c==SDLK_RETURN)
	{
		textFocus++;
		if (textFocus==activeStrings.size())
		{
			//submit here
		}
		else
			activeText=activeStrings[textFocus];
	}
	else if (c==SDLK_BACKSPACE || c==SDLK_DELETE)
	{
		if (activeText->length())
			activeText->erase(activeText->end()-1);
	}
	else if (c)
	{
		ch=SDL2ASC(c,m);
		(*activeText)+=ch;
	}
	return true;
}

void InterfaceHandler::focusTextArea(string *textArea)
{
	textFocus = 0;
	while ((activeStrings[textFocus] != textArea)
				 && (textFocus < activeStrings.size()))
		textFocus++;
	activeText = activeStrings[textFocus];
}