-
Notifications
You must be signed in to change notification settings - Fork 4
/
vgui_surf.cpp
436 lines (348 loc) · 11 KB
/
vgui_surf.cpp
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
/*
vgui_surf.cpp - main vgui layer
Copyright (C) 2011 Uncle Mike
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
In addition, as a special exception, the author gives permission
to link the code of this program with VGUI library developed by
Valve, L.L.C ("Valve"). You must obey the GNU General Public License
in all respects for all of the code used other than VGUI library.
If you modify this file, you may extend this exception to your
version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement
from your version.
*/
#include <ctype.h>
#include "vgui_main.h"
#define MAX_PAINT_STACK 16
#define FONT_SIZE 512
#define FONT_PAGES 8
struct FontInfo
{
FontInfo( int id, int contextCount ) : id( id ), pageCount(),
pageForChar(), bindIndex(), contextCount( contextCount )
{
}
int id;
int pageCount;
int pageForChar[256];
int bindIndex[FONT_PAGES];
float texCoord[256][FONT_PAGES];
int contextCount;
};
static int staticContextCount = 0;
static char staticRGBA[FONT_SIZE * FONT_SIZE * 4];
static Font* staticFont = NULL;
static FontInfo* staticFontInfo;
static Dar<FontInfo*> staticFontInfoDar;
static PaintStack paintStack[MAX_PAINT_STACK];
static int staticPaintStackPos = 0;
#define ColorIndex( c )((( c ) - '0' ) & 7 )
CEngineSurface :: CEngineSurface( Panel *embeddedPanel ):SurfaceBase( embeddedPanel )
{
_embeddedPanel = embeddedPanel;
_drawColor[0] = _drawColor[1] = _drawColor[2] = _drawColor[3] = 255;
_drawTextColor[0] = _drawTextColor[1] = _drawTextColor[2] = _drawTextColor[3] = 255;
_surfaceExtents[0] = _surfaceExtents[1] = 0;
//_surfaceExtents[2] = menu.globals->scrWidth;
//_surfaceExtents[3] = menu.globals->scrHeight;
embeddedPanel->getSize(_surfaceExtents[2], _surfaceExtents[3]);
_drawTextPos[0] = _drawTextPos[1] = 0;
_hCurrentCursor = null;
staticFont = NULL;
staticFontInfo = NULL;
staticFontInfoDar.setCount( 0 );
staticPaintStackPos = 0;
staticContextCount++;
_translateX = _translateY = 0;
}
CEngineSurface :: ~CEngineSurface( void )
{
g_api->DrawShutdown ();
}
Panel *CEngineSurface :: getEmbeddedPanel( void )
{
return _embeddedPanel;
}
bool CEngineSurface :: hasFocus( void )
{
// What differs when window does not has focus?
//return host.state != HOST_NOFOCUS;
return true;
}
void CEngineSurface :: setCursor( Cursor *cursor )
{
_currentCursor = cursor;
if( cursor )
{
g_api->CursorSelect( (VGUI_DefaultCursor)cursor->getDefaultCursor() );
}
}
void CEngineSurface :: SetupPaintState( const PaintStack *paintState )
{
_translateX = paintState->iTranslateX;
_translateY = paintState->iTranslateY;
SetScissorRect( paintState->iScissorLeft, paintState->iScissorTop,
paintState->iScissorRight, paintState->iScissorBottom );
}
void CEngineSurface :: InitVertex( vpoint_t &vertex, int x, int y, float u, float v )
{
vertex.point[0] = x + _translateX;
vertex.point[1] = y + _translateY;
vertex.coord[0] = u;
vertex.coord[1] = v;
}
int CEngineSurface :: createNewTextureID( void )
{
return g_api->GenerateTexture();
}
void CEngineSurface :: drawSetColor( int r, int g, int b, int a )
{
_drawColor[0] = r;
_drawColor[1] = g;
_drawColor[2] = b;
_drawColor[3] = a;
}
void CEngineSurface :: drawSetTextColor( int r, int g, int b, int a )
{
_drawTextColor[0] = r;
_drawTextColor[1] = g;
_drawTextColor[2] = b;
_drawTextColor[3] = a;
}
void CEngineSurface :: drawFilledRect( int x0, int y0, int x1, int y1 )
{
vpoint_t rect[2];
vpoint_t clippedRect[2];
if( _drawColor[3] >= 255 ) return;
InitVertex( rect[0], x0, y0, 0, 0 );
InitVertex( rect[1], x1, y1, 0, 0 );
// fully clipped?
if( !ClipRect( rect[0], rect[1], &clippedRect[0], &clippedRect[1] ))
return;
g_api->SetupDrawingRect( _drawColor );
g_api->EnableTexture( false );
g_api->DrawQuad( &clippedRect[0], &clippedRect[1] );
g_api->EnableTexture( true );
}
void CEngineSurface :: drawOutlinedRect( int x0, int y0, int x1, int y1 )
{
if( _drawColor[3] >= 255 ) return;
drawFilledRect( x0, y0, x1, y0 + 1 ); // top
drawFilledRect( x0, y1 - 1, x1, y1 ); // bottom
drawFilledRect( x0, y0 + 1, x0 + 1, y1 - 1 ); // left
drawFilledRect( x1 - 1, y0 + 1, x1, y1 - 1 ); // right
}
void CEngineSurface :: drawSetTextFont( Font *font )
{
staticFont = font;
if( font )
{
bool buildFont = false;
staticFontInfo = NULL;
for( int i = 0; i < staticFontInfoDar.getCount(); i++ )
{
if( staticFontInfoDar[i]->id == font->getId( ))
{
staticFontInfo = staticFontInfoDar[i];
if( staticFontInfo->contextCount != staticContextCount )
buildFont = true;
}
}
if( !staticFontInfo || buildFont )
{
staticFontInfo = new FontInfo( staticFont->getId(), staticContextCount );
staticFontInfoDar.putElement( staticFontInfo );
int currentPage = 0;
int x = 0, y = 0;
memset( staticRGBA, 0, sizeof( staticRGBA ));
for( int i = 0; i < 256; i++ )
{
int abcA = 0, abcB = 0, abcC = 0;
staticFont->getCharABCwide( i, abcA, abcB, abcC );
int wide = abcB;
if( isspace( i )) continue;
int tall = staticFont->getTall();
if( x + wide + 1 > FONT_SIZE )
{
x = 0;
y += tall + 1;
}
if( y + tall + 1 > FONT_SIZE )
{
if( !staticFontInfo->bindIndex[currentPage] )
staticFontInfo->bindIndex[currentPage] = createNewTextureID();
drawSetTextureRGBA( staticFontInfo->bindIndex[currentPage], staticRGBA, FONT_SIZE, FONT_SIZE );
currentPage++;
if( currentPage == FONT_PAGES )
break;
memset( staticRGBA, 0, sizeof( staticRGBA ));
x = y = 0;
}
staticFont->getCharRGBA( i, x, y, FONT_SIZE, FONT_SIZE, (byte *)staticRGBA );
staticFontInfo->pageForChar[i] = currentPage;
staticFontInfo->texCoord[i][0] = (float)((double)x / (double)FONT_SIZE );
staticFontInfo->texCoord[i][1] = (float)((double)y / (double)FONT_SIZE );
staticFontInfo->texCoord[i][2] = (float)((double)(x + wide)/(double)FONT_SIZE );
staticFontInfo->texCoord[i][3] = (float)((double)(y + tall)/(double)FONT_SIZE );
x += wide + 1;
}
if( currentPage != FONT_PAGES )
{
if( !staticFontInfo->bindIndex[currentPage] )
staticFontInfo->bindIndex[currentPage] = createNewTextureID();
drawSetTextureRGBA( staticFontInfo->bindIndex[currentPage], staticRGBA, FONT_SIZE, FONT_SIZE );
}
staticFontInfo->pageCount = currentPage + 1;
}
}
}
void CEngineSurface :: drawSetTextPos( int x, int y )
{
_drawTextPos[0] = x;
_drawTextPos[1] = y;
}
void CEngineSurface :: drawPrintChar( int x, int y, int wide, int tall, float s0, float t0, float s1, float t1, int color[4] )
{
vpoint_t ul, lr;
ul.point[0] = x;
ul.point[1] = y;
lr.point[0] = x + wide;
lr.point[1] = y + tall;
// gets at the texture coords for this character in its texture page
ul.coord[0] = s0;
ul.coord[1] = t0;
lr.coord[0] = s1;
lr.coord[1] = t1;
vpoint_t clippedRect[2];
if( !ClipRect( ul, lr, &clippedRect[0], &clippedRect[1] ))
return;
g_api->SetupDrawingImage( color );
g_api->DrawQuad( &clippedRect[0], &clippedRect[1] ); // draw the letter
}
void CEngineSurface :: drawPrintText( const char* text, int textLen )
{
//return;
static bool hasColor = 0;
static int numColor = 7;
if( !text || !staticFont || _drawTextColor[3] >= 255 )
return;
int x = _drawTextPos[0] + _translateX;
int y = _drawTextPos[1] + _translateY;
int tall = staticFont->getTall();
int j, iTotalWidth = 0;
int curTextColor[4];
// HACKHACK: allow color strings in VGUI
if( numColor != 7 )
{
for( j = 0; j < 3; j++ ) // grab predefined color
curTextColor[j] = g_api->GetColor(numColor,j);
}
else
{
for( j = 0; j < 3; j++ ) // revert default color
curTextColor[j] = _drawTextColor[j];
}
curTextColor[3] = _drawTextColor[3]; // copy alpha
if( textLen == 1 )
{
if( *text == '^' )
{
hasColor = true;
return; // skip '^'
}
else if( hasColor && isdigit( *text ))
{
numColor = ColorIndex( *text );
hasColor = false; // handled
return; // skip colornum
}
else hasColor = false;
}
for( int i = 0; i < textLen; i++ )
{
int curCh = g_api->ProcessUtfChar( (unsigned char)text[i] );
if( !curCh )
{
continue;
}
int abcA = 0, abcB = 0, abcC = 0;
staticFont->getCharABCwide( curCh, abcA, abcB, abcC );
float s0 = staticFontInfo->texCoord[curCh][0];
float t0 = staticFontInfo->texCoord[curCh][1];
float s1 = staticFontInfo->texCoord[curCh][2];
float t1 = staticFontInfo->texCoord[curCh][3];
int wide = abcB;
iTotalWidth += abcA;
drawSetTexture( staticFontInfo->bindIndex[staticFontInfo->pageForChar[curCh]] );
drawPrintChar( x + iTotalWidth, y, wide, tall, s0, t0, s1, t1, curTextColor );
iTotalWidth += wide + abcC;
}
_drawTextPos[0] += iTotalWidth;
}
void CEngineSurface :: drawSetTextureRGBA( int id, const char* rgba, int wide, int tall )
{
g_api->UploadTexture( id, rgba, wide, tall );
}
void CEngineSurface :: drawSetTexture( int id )
{
g_api->BindTexture( id );
}
void CEngineSurface :: drawTexturedRect( int x0, int y0, int x1, int y1 )
{
vpoint_t rect[2];
vpoint_t clippedRect[2];
InitVertex( rect[0], x0, y0, 0, 0 );
InitVertex( rect[1], x1, y1, 1, 1 );
// fully clipped?
if( !ClipRect( rect[0], rect[1], &clippedRect[0], &clippedRect[1] ))
return;
g_api->SetupDrawingImage( _drawColor );
g_api->DrawQuad( &clippedRect[0], &clippedRect[1] );
}
void CEngineSurface :: pushMakeCurrent( Panel* panel, bool useInsets )
{
int insets[4] = { 0, 0, 0, 0 };
int absExtents[4];
int clipRect[4];
if( useInsets )
panel->getInset( insets[0], insets[1], insets[2], insets[3] );
panel->getAbsExtents( absExtents[0], absExtents[1], absExtents[2], absExtents[3] );
panel->getClipRect( clipRect[0], clipRect[1], clipRect[2], clipRect[3] );
PaintStack *paintState = &paintStack[staticPaintStackPos];
assert( staticPaintStackPos < MAX_PAINT_STACK );
paintState->m_pPanel = panel;
// determine corrected top left origin
paintState->iTranslateX = insets[0] + absExtents[0];
paintState->iTranslateY = insets[1] + absExtents[1];
// setup clipping rectangle for scissoring
paintState->iScissorLeft = clipRect[0];
paintState->iScissorTop = clipRect[1];
paintState->iScissorRight = clipRect[2];
paintState->iScissorBottom = clipRect[3];
SetupPaintState( paintState );
staticPaintStackPos++;
}
void CEngineSurface :: popMakeCurrent( Panel *panel )
{
int top = staticPaintStackPos - 1;
// more pops that pushes?
assert( top >= 0 );
// didn't pop in reverse order of push?
assert( paintStack[top].m_pPanel == panel );
staticPaintStackPos--;
if( top > 0 ) SetupPaintState( &paintStack[top-1] );
}
bool CEngineSurface :: setFullscreenMode( int wide, int tall, int bpp )
{
return false;
}
void CEngineSurface :: setWindowedMode( void )
{
}