/
hu_log.c
506 lines (429 loc) · 14.8 KB
/
hu_log.c
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/**\file hu_log.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2005-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2013 Daniel Swanson <danij@dengine.net>
*
* 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 2 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "doomsday.h"
#if __JDOOM__
# include "jdoom.h"
#elif __JDOOM64__
# include "jdoom64.h"
#elif __JHERETIC__
# include "jheretic.h"
#elif __JHEXEN__
# include "jhexen.h"
#endif
#include "p_tick.h"
#include "hu_stuff.h"
#include "hu_log.h"
/**
* @addtogroup logMessageFlags
* Flags private to this module.
* @{
*/
#define LMF_JUSTADDED (0x2)
/**@}*/
/// Mask for clearing non-public flags @ref logMessageFlags
#define LOG_INTERNAL_MESSAGEFLAGMASK 0xfe
void UILog_Register(void)
{
cvartemplate_t cvars[] = {
// Behavior
{ "msg-uptime", 0, CVT_FLOAT, &cfg.msgUptime, 1, 60 },
// Display
{ "msg-align", 0, CVT_INT, &cfg.msgAlign, 0, 2, ST_LogUpdateAlignment },
{ "msg-blink", CVF_NO_MAX, CVT_INT, &cfg.msgBlink, 0, 0 },
{ "msg-color-r", 0, CVT_FLOAT, &cfg.msgColor[CR], 0, 1 },
{ "msg-color-g", 0, CVT_FLOAT, &cfg.msgColor[CG], 0, 1 },
{ "msg-color-b", 0, CVT_FLOAT, &cfg.msgColor[CB], 0, 1 },
{ "msg-count", 0, CVT_INT, &cfg.msgCount, 1, 8 },
{ "msg-scale", 0, CVT_FLOAT, &cfg.msgScale, 0.1f, 1 },
{ "msg-show", 0, CVT_BYTE, &cfg.hudShown[HUD_LOG], 0, 1, ST_LogPostVisibilityChangeNotification },
{ NULL }
};
int i;
for(i = 0; cvars[i].path; ++i)
Con_AddVariable(cvars + i);
}
/// @return Index of the first (i.e., earliest) message that is potentially visible.
static int UILog_FirstPVisMessageIdx(const uiwidget_t* obj)
{
guidata_log_t* log = (guidata_log_t*)obj->typedata;
assert(obj->type == GUI_LOG);
if(0 != log->_pvisMsgCount)
{
int n = log->_nextUsedMsg - MIN_OF(log->_pvisMsgCount, MAX_OF(0, cfg.msgCount));
if(n < 0) n += LOG_MAX_MESSAGES; // Wrap around.
return n;
}
return -1;
}
/// @return Index of the first (i.e., earliest) message.
static int UILog_FirstMessageIdx(const uiwidget_t* obj)
{
guidata_log_t* log = (guidata_log_t*)obj->typedata;
assert(obj->type == GUI_LOG);
if(0 != log->_pvisMsgCount)
{
int n = log->_nextUsedMsg - log->_pvisMsgCount;
if(n < 0) n += LOG_MAX_MESSAGES; // Wrap around.
return n;
}
return -1;
}
/// @return Index of the next (possibly already used) message.
static __inline int UILog_NextMessageIdx(const uiwidget_t* obj, int current)
{
if(current < LOG_MAX_MESSAGES - 1) return current + 1;
return 0; // Wrap around.
}
/// @return Index of the previous (possibly already used) message.
static __inline int UILog_PrevMessageIdx(const uiwidget_t* obj, int current)
{
if(current > 0) return current - 1;
return LOG_MAX_MESSAGES - 1; // Wrap around.
}
/**
* Push a new message into the log.
*
* @param flags @ref messageFlags
* @param text Message to be added.
* @param tics Length of time the message should be visible.
*/
static guidata_log_message_t* UILog_Push(uiwidget_t* obj, int flags, const char* text, int tics)
{
guidata_log_t* log = (guidata_log_t*)obj->typedata;
guidata_log_message_t* msg;
int len;
assert(obj->type == GUI_LOG && text);
len = (int)strlen(text);
if(0 == len)
{
Con_Error("Log::Push: Attempted to log zero-length message.");
exit(1); // Unreachable.
}
msg = &log->_msgs[log->_nextUsedMsg];
log->_nextUsedMsg = UILog_NextMessageIdx(obj, log->_nextUsedMsg);
if(len >= msg->textMaxLen)
{
msg->textMaxLen = len+1;
msg->text = (char*) Z_Realloc(msg->text, msg->textMaxLen, PU_GAMESTATIC);
if(!msg->text)
Con_Error("Log::Push: Failed on (re)allocation of %lu bytes for log message.", (unsigned long) msg->textMaxLen);
}
if(log->_msgCount < LOG_MAX_MESSAGES)
++log->_msgCount;
if(log->_pvisMsgCount < LOG_MAX_MESSAGES)
++log->_pvisMsgCount;
dd_snprintf(msg->text, msg->textMaxLen, "%s", text);
msg->ticsRemain = msg->tics = tics;
msg->flags = LMF_JUSTADDED | flags;
return msg;
}
/// Remove the oldest message from the log.
static guidata_log_message_t* UILog_Pop(uiwidget_t* obj)
{
guidata_log_t* log = (guidata_log_t*)obj->typedata;
guidata_log_message_t* msg;
int oldest;
assert(obj->type == GUI_LOG);
oldest = UILog_FirstMessageIdx(obj);
if(0 > oldest) return NULL;
msg = &log->_msgs[oldest];
--log->_pvisMsgCount;
msg->ticsRemain = LOG_MESSAGE_SCROLLTICS;
msg->flags &= ~LMF_JUSTADDED;
return msg;
}
void UILog_Empty(uiwidget_t* obj)
{
while(UILog_Pop(obj)) {}
}
void UILog_Post(uiwidget_t* obj, byte flags, const char* text)
{
#define SMALLBUF_MAXLEN 128
//guidata_log_t* log = (guidata_log_t*)obj->typedata;
char smallBuf[SMALLBUF_MAXLEN+1];
char* bigBuf = NULL, *p;
size_t requiredLen = strlen(text);
assert(obj->type == GUI_LOG && text);
if(0 == requiredLen) return;
flags &= ~LOG_INTERNAL_MESSAGEFLAGMASK;
if(requiredLen <= SMALLBUF_MAXLEN)
{
p = smallBuf;
}
else
{
bigBuf = (char*) malloc(requiredLen + 1);
if(NULL == bigBuf)
Con_Error("Log::Post: Failed on allocation of %lu bytes for temporary "
"local message buffer.", (unsigned long) (requiredLen + 1));
p = bigBuf;
}
p[requiredLen] = '\0';
sprintf(p, "%s", text);
UILog_Push(obj, flags, p, cfg.msgUptime * TICSPERSEC);
if(NULL != bigBuf)
free(bigBuf);
#undef SMALLBUF_MAXLEN
}
void UILog_Refresh(uiwidget_t* obj)
{
guidata_log_t* log = (guidata_log_t*)obj->typedata;
int i, n;
assert(obj->type == GUI_LOG);
log->_pvisMsgCount = MIN_OF(log->_msgCount, MAX_OF(0, cfg.msgCount));
n = UILog_FirstMessageIdx(obj);
if(0 > n) return;
for(i = 0; i < log->_pvisMsgCount; ++i, n = UILog_NextMessageIdx(obj, n))
{
guidata_log_message_t* msg = &log->_msgs[n];
// Change the tics remaining to that at post time plus a small bonus
// so that they do not all disappear at once.
msg->ticsRemain = msg->tics + i * TICSPERSEC;
msg->flags &= ~LMF_JUSTADDED;
}
}
/**
* Process gametic. Jobs include ticking messages and adjusting values
* used when drawing the buffer for animation.
*/
void UILog_Ticker(uiwidget_t* obj, timespan_t ticLength)
{
guidata_log_t* log = (guidata_log_t*)obj->typedata;
int i, oldest;
assert(obj->type == GUI_LOG);
if(Pause_IsPaused() || !DD_IsSharpTick()) return;
// All messags tic away.
for(i = 0; i < LOG_MAX_MESSAGES; ++i)
{
guidata_log_message_t* msg = &log->_msgs[i];
if(0 == msg->ticsRemain) continue;
--msg->ticsRemain;
}
// Is it time to pop?
oldest = UILog_FirstMessageIdx(obj);
if(oldest >= 0)
{
guidata_log_message_t* msg = &log->_msgs[oldest];
if(0 == msg->ticsRemain)
{
UILog_Pop(obj);
}
}
}
void UILog_Drawer(uiwidget_t* obj, const Point2Raw* offset)
{
guidata_log_t* log = (guidata_log_t*)obj->typedata;
const int alignFlags = ALIGN_TOP| ((cfg.msgAlign == 0)? ALIGN_LEFT : (cfg.msgAlign == 2)? ALIGN_RIGHT : 0);
const short textFlags = DTF_NO_EFFECTS;
const float textAlpha = uiRendState->pageAlpha * cfg.hudColor[3];
//const float iconAlpha = uiRendState->pageAlpha * cfg.hudIconAlpha;
int lineHeight;
int i, n, pvisMsgCount = MIN_OF(log->_pvisMsgCount, MAX_OF(0, cfg.msgCount));
int drawnMsgCount, firstPVisMsg, firstMsg, lastMsg;
float y, yOffset, scrollFactor, col[4];
float offsetDueToMapTitle = 0;
guidata_log_message_t* msg;
assert(obj->type == GUI_LOG);
if(Hu_IsMapTitleVisible() && !cfg.automapTitleAtBottom)
{
offsetDueToMapTitle = Hu_MapTitleHeight();
}
if(!pvisMsgCount) return;
DGL_MatrixMode(DGL_MODELVIEW);
DGL_PushMatrix();
if(offset) DGL_Translatef(offset->x, offset->y, 0);
DGL_Translatef(0, offsetDueToMapTitle, 0);
DGL_Scalef(cfg.msgScale, cfg.msgScale, 1);
firstMsg = firstPVisMsg = UILog_FirstPVisMessageIdx(obj);
if(!cfg.hudShown[HUD_LOG])
{
// Advance to the first non-hidden message.
i = 0;
while(0 == (log->_msgs[firstMsg].flags & LMF_NO_HIDE) && ++i < pvisMsgCount)
{
firstMsg = UILog_NextMessageIdx(obj, firstMsg);
}
// Nothing visible?
if(i == pvisMsgCount) goto stateCleanup;
// There is possibly fewer potentially-visible messages now.
pvisMsgCount -= firstMsg - firstPVisMsg;
}
lastMsg = firstMsg + pvisMsgCount-1;
if(lastMsg > LOG_MAX_MESSAGES-1)
lastMsg -= LOG_MAX_MESSAGES; // Wrap around.
if(!cfg.hudShown[HUD_LOG])
{
// Rewind to the last non-hidden message.
i = 0;
while(0 == (log->_msgs[lastMsg].flags & LMF_NO_HIDE) && ++i < pvisMsgCount)
{
lastMsg = UILog_PrevMessageIdx(obj, lastMsg);
}
}
FR_SetFont(obj->font);
/// @todo Query line height from the font.
lineHeight = FR_CharHeight('Q')+1;
// Scroll offset is calculated using the timeout of the first visible message.
msg = &log->_msgs[firstMsg];
if(msg->ticsRemain > 0 && msg->ticsRemain <= (unsigned) lineHeight)
{
scrollFactor = 1.0f - (((float)msg->ticsRemain)/lineHeight);
yOffset = -lineHeight * scrollFactor;
}
else
{
scrollFactor = 0;
yOffset = 0;
}
DGL_MatrixMode(DGL_MODELVIEW);
DGL_Translatef(0, yOffset, 0);
DGL_Enable(DGL_TEXTURE_2D);
y = 0;
n = firstMsg;
drawnMsgCount = 0;
for(i = 0; i < pvisMsgCount; ++i, n = UILog_NextMessageIdx(obj, n))
{
msg = &log->_msgs[n];
if(!cfg.hudShown[HUD_LOG] && !(msg->flags & LMF_NO_HIDE))
continue;
// Default color and alpha.
col[CR] = cfg.msgColor[CR];
col[CG] = cfg.msgColor[CG];
col[CB] = cfg.msgColor[CB];
if(n != firstMsg)
{
col[CA] = textAlpha;
}
else
{
// Fade out.
col[CA] = textAlpha * 1.f - scrollFactor * (4/3.f);
col[CA] = MINMAX_OF(0, col[CA], 1);
}
if((msg->flags & LMF_JUSTADDED) && 0 != cfg.msgBlink)
{
const uint blinkSpeed = cfg.msgBlink;
const uint msgTics = msg->tics - msg->ticsRemain;
if(msgTics < blinkSpeed)
{
const uint td = (cfg.msgUptime * TICSPERSEC) - msg->ticsRemain;
if(n == lastMsg && (0 == msgTics || (td & 2)))
{
// Use the "flash" color.
col[CR] = col[CG] = col[CB] = 1;
}
}
else if(msgTics < blinkSpeed + LOG_MESSAGE_FLASHFADETICS && msgTics >= blinkSpeed)
{
// Fade color to normal.
const float fade = (blinkSpeed + LOG_MESSAGE_FLASHFADETICS - msgTics);
col[CR] += (1.0f - col[CR]) / LOG_MESSAGE_FLASHFADETICS * fade;
col[CG] += (1.0f - col[CG]) / LOG_MESSAGE_FLASHFADETICS * fade;
col[CB] += (1.0f - col[CB]) / LOG_MESSAGE_FLASHFADETICS * fade;
}
}
FR_SetColorAndAlpha(col[CR], col[CG], col[CB], col[CA]);
FR_DrawTextXY3(msg->text, 0, y, alignFlags, textFlags);
++drawnMsgCount;
y += lineHeight;
}
stateCleanup:
DGL_Disable(DGL_TEXTURE_2D);
DGL_MatrixMode(DGL_MODELVIEW);
DGL_PopMatrix();
}
void UILog_UpdateGeometry(uiwidget_t* obj)
{
guidata_log_t* log = (guidata_log_t*)obj->typedata;
guidata_log_message_t* msg;
int lineHeight;
int i, n, pvisMsgCount = MIN_OF(log->_pvisMsgCount, MAX_OF(0, cfg.msgCount));
int drawnMsgCount, firstPVisMsg, firstMsg, lastMsg;
float scrollFactor;
RectRaw lineGeometry;
assert(obj->type == GUI_LOG);
Rect_SetWidthHeight(obj->geometry, 0, 0);
if(0 == pvisMsgCount) return;
firstMsg = firstPVisMsg = UILog_FirstPVisMessageIdx(obj);
if(!cfg.hudShown[HUD_LOG])
{
// Advance to the first non-hidden message.
i = 0;
while(0 == (log->_msgs[firstMsg].flags & LMF_NO_HIDE) && ++i < pvisMsgCount)
{
firstMsg = UILog_NextMessageIdx(obj, firstMsg);
}
// Nothing visible?
if(i == pvisMsgCount) return;
// There is possibly fewer potentially-visible messages now.
pvisMsgCount -= firstMsg - firstPVisMsg;
}
lastMsg = firstMsg + pvisMsgCount-1;
if(lastMsg > LOG_MAX_MESSAGES-1)
lastMsg -= LOG_MAX_MESSAGES; // Wrap around.
if(!cfg.hudShown[HUD_LOG])
{
// Rewind to the last non-hidden message.
i = 0;
while(0 == (log->_msgs[lastMsg].flags & LMF_NO_HIDE) && ++i < pvisMsgCount)
{
lastMsg = UILog_PrevMessageIdx(obj, lastMsg);
}
}
FR_SetFont(FID(GF_FONTA));
/// @todo Query line height from the font.
lineHeight = FR_CharHeight('Q')+1;
// Scroll offset is calculated using the timeout of the first visible message.
msg = &log->_msgs[firstMsg];
if(msg->ticsRemain > 0 && msg->ticsRemain <= (unsigned) lineHeight)
{
scrollFactor = 1.0f - (((float)msg->ticsRemain)/lineHeight);
}
else
{
scrollFactor = 0;
}
n = firstMsg;
drawnMsgCount = 0;
lineGeometry.origin.x = lineGeometry.origin.y = 0;
for(i = 0; i < pvisMsgCount; ++i, n = UILog_NextMessageIdx(obj, n))
{
msg = &log->_msgs[n];
if(!cfg.hudShown[HUD_LOG] && !(msg->flags & LMF_NO_HIDE)) continue;
++drawnMsgCount;
FR_TextSize(&lineGeometry.size, msg->text);
Rect_UniteRaw(obj->geometry, &lineGeometry);
lineGeometry.origin.y += lineHeight;
}
if(0 != drawnMsgCount)
{
// Subtract the scroll offset.
Rect_SetHeight(obj->geometry, Rect_Height(obj->geometry) - lineHeight * scrollFactor);
}
Rect_SetWidthHeight(obj->geometry, Rect_Width(obj->geometry) * cfg.msgScale,
Rect_Height(obj->geometry) * cfg.msgScale);
}