aboutsummaryrefslogtreecommitdiff
path: root/hgehelp/hgeguictrls.cpp
blob: 20fae4be5ff3eba6fbaebca22d998597298e3d4e (plain) (blame)
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
/*
** Haaf's Game Engine 1.7
** Copyright (C) 2003-2007, Relish Games
** hge.relishgames.com
**
** hgeGUI default controls implementation
*/


#include "../../include/hgeguictrls.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>


/*
** hgeGUIText
*/

hgeGUIText::hgeGUIText(int _id, float x, float y, float w, float h, hgeFont *fnt)
{
	id=_id;
	bStatic=true;
	bVisible=true;
	bEnabled=true;
	rect.Set(x, y, x+w, y+h);

	font=fnt;
	tx=x;
	ty=y+(h-fnt->GetHeight())/2.0f;

	text[0]=0;
}

void hgeGUIText::SetMode(int _align)
{
	align=_align;
	if(align==HGETEXT_RIGHT) tx=rect.x2;
	else if(align==HGETEXT_CENTER) tx=(rect.x1+rect.x2)/2.0f;
	else tx=rect.x1;
}

void hgeGUIText::SetText(const char *_text)
{
	strncpy(text, _text, sizeof(text)-1);
	text[sizeof(text) - 1] = '\0';
}

void hgeGUIText::printf(const char *format, ...)
{
	va_list vl;
	va_start(vl, format);
	vsnprintf(text, sizeof(text)-1, format, vl);
	va_end(vl);
}

void hgeGUIText::Render()
{
	font->SetColor(color);
	font->Render(tx,ty,align,text);
}

/*
** hgeGUIButton
*/

hgeGUIButton::hgeGUIButton(int _id, float x, float y, float w, float h, HTEXTURE tex, float tx, float ty)
{
	id=_id;
	bStatic=false;
	bVisible=true;
	bEnabled=true;
	rect.Set(x, y, x+w, y+h);

	bPressed=false;
	bTrigger=false;

	sprUp = new hgeSprite(tex, tx, ty, w, h);
	sprDown = new hgeSprite(tex, tx+w, ty, w, h);
}

hgeGUIButton::~hgeGUIButton()
{
	if(sprUp) delete sprUp;
	if(sprDown) delete sprDown;
}

void hgeGUIButton::Render()
{
	if(bPressed) sprDown->Render(rect.x1, rect.y1);
	else sprUp->Render(rect.x1, rect.y1);
}

bool hgeGUIButton::MouseLButton(bool bDown)
{
	if(bDown)
	{
		bOldState=bPressed; bPressed=true;
		return false;
	}
	else
	{
		if(bTrigger) bPressed=!bOldState;
		else bPressed=false;
		return true;
	}
}

/*
** hgeGUISlider
*/

hgeGUISlider::hgeGUISlider(int _id, float x, float y, float w, float h, HTEXTURE tex, float tx, float ty, float sw, float sh, bool vertical)
{
	id=_id;
	bStatic=false;
	bVisible=true;
	bEnabled=true;
	bPressed=false;
	bVertical=vertical;
	rect.Set(x, y, x+w, y+h);

	mode=HGESLIDER_BAR;
	fMin=0; fMax=100; fVal=50;
	sl_w=sw; sl_h=sh;

	sprSlider=new hgeSprite(tex, tx, ty, sw, sh);
}

hgeGUISlider::~hgeGUISlider()
{
	if(sprSlider) delete sprSlider;
}

void hgeGUISlider::SetValue(float _fVal)
{
	if(_fVal<fMin) fVal=fMin;
	else if(_fVal>fMax) fVal=fMax;
	else fVal=_fVal;
}

void hgeGUISlider::Render()
{
	float xx, yy;
	float x1,y1,x2,y2;

	xx=rect.x1+(rect.x2-rect.x1)*(fVal-fMin)/(fMax-fMin);
	yy=rect.y1+(rect.y2-rect.y1)*(fVal-fMin)/(fMax-fMin);

	if(bVertical)
		switch(mode)
		{
			case HGESLIDER_BAR: x1=rect.x1; y1=rect.y1; x2=rect.x2; y2=yy; break;
			case HGESLIDER_BARRELATIVE: x1=rect.x1; y1=(rect.y1+rect.y2)/2; x2=rect.x2; y2=yy; break;
			case HGESLIDER_SLIDER: x1=(rect.x1+rect.x2-sl_w)/2; y1=yy-sl_h/2; x2=(rect.x1+rect.x2+sl_w)/2; y2=yy+sl_h/2; break;
		}
	else
		switch(mode)
		{
			case HGESLIDER_BAR: x1=rect.x1; y1=rect.y1; x2=xx; y2=rect.y2; break;
			case HGESLIDER_BARRELATIVE: x1=(rect.x1+rect.x2)/2; y1=rect.y1; x2=xx; y2=rect.y2; break;
			case HGESLIDER_SLIDER: x1=xx-sl_w/2; y1=(rect.y1+rect.y2-sl_h)/2; x2=xx+sl_w/2; y2=(rect.y1+rect.y2+sl_h)/2; break;
		}

	sprSlider->RenderStretch(x1, y1, x2, y2);
}

bool hgeGUISlider::MouseLButton(bool bDown)
{
	bPressed=bDown;
	return false;
}

bool hgeGUISlider::MouseMove(float x, float y)
{
	if(bPressed)
	{
		if(bVertical)
		{
			if(y>rect.y2-rect.y1) y=rect.y2-rect.y1;
			if(y<0) y=0;
			fVal=fMin+(fMax-fMin)*y/(rect.y2-rect.y1);
		}
		else
		{
			if(x>rect.x2-rect.x1) x=rect.x2-rect.x1;
			if(x<0) x=0;
			fVal=fMin+(fMax-fMin)*x/(rect.x2-rect.x1);
		}
		return true;
	}

	return false;
}


/*
** hgeGUIListbox
*/

hgeGUIListbox::hgeGUIListbox(int _id, float x, float y, float w, float h, hgeFont *fnt, DWORD tColor, DWORD thColor, DWORD hColor)
{
	id=_id;
	bStatic=false;
	bVisible=true;
	bEnabled=true;
	rect.Set(x, y, x+w, y+h);
	font=fnt;
	sprHighlight=new hgeSprite(0, 0, 0, w, fnt->GetHeight());
	sprHighlight->SetColor(hColor);
	textColor=tColor;
	texthilColor=thColor;
	pItems=0;
	nItems=0;

	nSelectedItem=0;
	nTopItem=0;
	mx=0; my=0;
}

hgeGUIListbox::~hgeGUIListbox()
{
	Clear();
	if(sprHighlight) delete sprHighlight;
}


int hgeGUIListbox::AddItem(char *item)
{
	hgeGUIListboxItem *pItem=pItems, *pPrev=0, *pNew;

	pNew = new hgeGUIListboxItem;
	memcpy(pNew->text, item, min(sizeof(pNew->text), strlen(item)+1));
	pNew->text[sizeof(pNew->text)-1]='\0';
	pNew->next=0;

	while(pItem) { pPrev=pItem;	pItem=pItem->next; }

	if(pPrev) pPrev->next=pNew;
	else pItems=pNew;
	nItems++;

	return nItems-1;
}

void hgeGUIListbox::DeleteItem(int n)
{
	int i;
	hgeGUIListboxItem *pItem=pItems, *pPrev=0;

	if(n<0 || n>=GetNumItems()) return;

	for(i=0;i<n;i++) { pPrev=pItem;	pItem=pItem->next; }

	if(pPrev) pPrev->next=pItem->next;
	else pItems=pItem->next;

	delete pItem;
	nItems--;
}

char *hgeGUIListbox::GetItemText(int n)
{
	int i;
	hgeGUIListboxItem *pItem=pItems;

	if(n<0 || n>=GetNumItems()) return 0;

	for(i=0;i<n;i++) pItem=pItem->next;

	return pItem->text;
}

void hgeGUIListbox::Clear()
{
	hgeGUIListboxItem *pItem=pItems, *pNext;

	while(pItem)
	{
		pNext=pItem->next;
		delete pItem;
		pItem=pNext;
	}

	pItems=0;
	nItems=0;
}

void hgeGUIListbox::Render()
{
	int i;
	hgeGUIListboxItem *pItem=pItems;

	for(i=0;i<nTopItem;i++) pItem=pItem->next;
	for(i=0;i<GetNumRows();i++)
	{
		if(i>=nItems) return;

		if(nTopItem+i == nSelectedItem)
		{
			sprHighlight->Render(rect.x1,rect.y1+i*font->GetHeight());
			font->SetColor(texthilColor);
		}
		else
			font->SetColor(textColor);

		font->Render(rect.x1+3, rect.y1+i*font->GetHeight(), HGETEXT_LEFT, pItem->text);
		pItem=pItem->next;
	}
}

bool hgeGUIListbox::MouseLButton(bool bDown)
{
	int nItem;

	if(bDown)
	{
		nItem=nTopItem+int(my)/int(font->GetHeight());
		if(nItem<nItems)
		{
			nSelectedItem=nItem;
			return true;
		}
	}
	return false;
}


bool hgeGUIListbox::MouseWheel(int nNotches)
{
	nTopItem-=nNotches;
	if(nTopItem<0) nTopItem=0;
	if(nTopItem>GetNumItems()-GetNumRows()) nTopItem=GetNumItems()-GetNumRows();

	return true;
}

bool hgeGUIListbox::KeyClick(int key, int chr)
{
	switch(key)
	{
		case HGEK_DOWN:
			if(nSelectedItem < nItems-1)
			{
				nSelectedItem++;
				if(nSelectedItem > nTopItem+GetNumRows()-1) nTopItem=nSelectedItem-GetNumRows()+1;
				return true;
			}
			break;

		case HGEK_UP:
			if(nSelectedItem > 0)
			{
				nSelectedItem--;
				if(nSelectedItem < nTopItem) nTopItem=nSelectedItem;
				return true;
			}
			break;
	}
	return false;
}