-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcl_chat.cpp
More file actions
120 lines (93 loc) · 3.03 KB
/
cl_chat.cpp
File metadata and controls
120 lines (93 loc) · 3.03 KB
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
#include "shared.h"
#include "render.h"
#include "client.h"
#define GCHAT_BASE_X 50
#define GCHAT_BASE_Y 20
#define GCHAT_FONT_TYPE 2
static const vec4_t gChatColor = {1,1,1,1};
struct chatmsg {
std::string name;
std::string message;
time_t time;
float opacity;
int clearTimer;
};
#define MAX_DISPLAY_MSG 15
class UIGlobalChatMessageWindow : public UIObject {
public:
std::vector<chatmsg> messages;
UIGlobalChatMessageWindow(int a, int b, int c, int d) : UIObject(a, b, c, d) {
classType = UIOT_UIGlobalChatMessageWindow;
shader = whiteShader;
}
virtual void Render() {
vec4_t bg_color_lower = { 0, 0, 0, .5 };
vec4_t bg_color = { 0, 0, 0, .85 };
RE_SetColor(bg_color);
SCR_DrawPic(x, y, width, height, *whiteShader);
RE_SetColor(NULL);
const char *sMessage, *sName;
int name_len, msg_count = 0;
time_t curTime = time(NULL);
vec4_t per_color = { gChatColor[0], gChatColor[1], gChatColor[2], 1 };
static bool reset_other_faders = false;
int faders = 0;
for (std::vector<chatmsg>::iterator it = messages.begin(); it != messages.end();) {
name_len = 0;
sMessage = it->message.c_str();
per_color[3] = it->opacity;
if (it->name.size() > 0) {
//sName = va("(difftime = %f and %d) %s: ", difftime(curTime, it->time), *cls_realtime - it->clearTimer, it->name.c_str());
sName = va("%s: ", it->name.c_str());
name_len = SMALLCHAR_WIDTH * CG_DrawStrlen(sName);
SCR_DrawString(x, y + SMALLCHAR_HEIGHT * (msg_count+1), GCHAT_FONT_TYPE, .3, (float*)per_color, sName, NULL, NULL, NULL);
}
SCR_DrawString(x + name_len, y + SMALLCHAR_HEIGHT * (msg_count+1), GCHAT_FONT_TYPE, .3, (float*)per_color, sMessage, NULL, NULL, NULL);
msg_count++;
if (difftime(curTime, it->time) >= 1 && ((messages.size() > MAX_DISPLAY_MSG && !reset_other_faders) || messages.size() - faders > MAX_DISPLAY_MSG)) {
if (!it->clearTimer) {
it->clearTimer = *cls_realtime;
reset_other_faders = true;
}
}
if (it->clearTimer) {
if (*cls_realtime - it->clearTimer > 500)
it->opacity -= 0.007f;
faders++;
}
if (it->opacity <= 0) {
reset_other_faders = false;
it = messages.erase(it);
continue;
}
++it;
}
}
virtual ~UIGlobalChatMessageWindow() {}
};
UIGlobalChatMessageWindow *gwindow;
bool xui_txt_box_OnEnter(UITextbox *o) {
void Enc_SendTalkMessage(const char *chatmsg);
Enc_SendTalkMessage(o->text.c_str());
return true;
}
void CM_CreateWindow(UIMenu *m) {
int X_POS = 105;
int X_WIDTH = 640;
gwindow = xui->createMenuItem<UIGlobalChatMessageWindow>(m, X_POS, 0, X_WIDTH, 430);
UITextbox *txt_box = xui->createMenuItem<UITextbox>(m, X_POS, 440, 200, 30);
txt_box->SetBackgroundColor(.7, .7, .7, 1);
txt_box->m_OnEnter = xui_txt_box_OnEnter;
}
void CM_ReceiveHandler(const char *name, const char *message) {
//if (gChatMessages.size() > 10)
//gChatMessages.clear();
chatmsg msg;
msg.message = message;
if (name != NULL) //it's a global broadcast
msg.name = name;
msg.time = time(NULL);
msg.opacity = 1;
msg.clearTimer = 0;
gwindow->messages.push_back(msg);
}